builtin_audio_processing_builder.h (4839B)
1 /* 2 * Copyright (c) 2024 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef API_AUDIO_BUILTIN_AUDIO_PROCESSING_BUILDER_H_ 12 #define API_AUDIO_BUILTIN_AUDIO_PROCESSING_BUILDER_H_ 13 14 #include <memory> 15 #include <optional> 16 #include <utility> 17 18 #include "absl/base/nullability.h" 19 #include "api/audio/audio_processing.h" 20 #include "api/audio/echo_canceller3_config.h" 21 #include "api/audio/echo_control.h" 22 #include "api/audio/neural_residual_echo_estimator.h" 23 #include "api/environment/environment.h" 24 #include "api/scoped_refptr.h" 25 #include "rtc_base/system/rtc_export.h" 26 27 namespace webrtc { 28 29 class RTC_EXPORT BuiltinAudioProcessingBuilder 30 : public AudioProcessingBuilderInterface { 31 public: 32 BuiltinAudioProcessingBuilder() = default; 33 explicit BuiltinAudioProcessingBuilder(const AudioProcessing::Config& config) 34 : config_(config) {} 35 BuiltinAudioProcessingBuilder(const BuiltinAudioProcessingBuilder&) = delete; 36 BuiltinAudioProcessingBuilder& operator=( 37 const BuiltinAudioProcessingBuilder&) = delete; 38 ~BuiltinAudioProcessingBuilder() override = default; 39 40 // Sets the APM configuration. 41 BuiltinAudioProcessingBuilder& SetConfig( 42 const AudioProcessing::Config& config) { 43 config_ = config; 44 return *this; 45 } 46 47 // Sets an echo canceller config to inject when APM is created. If a custom 48 // EchoControlFactory is also specified, this config has no effect. 49 // `echo_canceller_multichannel_config` is an optional config that, if 50 // specified, is applied for non-mono content. 51 BuiltinAudioProcessingBuilder& SetEchoCancellerConfig( 52 const EchoCanceller3Config& echo_canceller_config, 53 std::optional<EchoCanceller3Config> echo_canceller_multichannel_config) { 54 echo_canceller_config_ = echo_canceller_config; 55 echo_canceller_multichannel_config_ = echo_canceller_multichannel_config; 56 return *this; 57 } 58 59 // Sets the echo controller factory to inject when APM is created. 60 BuiltinAudioProcessingBuilder& SetEchoControlFactory( 61 std::unique_ptr<EchoControlFactory> echo_control_factory) { 62 echo_control_factory_ = std::move(echo_control_factory); 63 return *this; 64 } 65 66 // Sets the capture post-processing sub-module to inject when APM is created. 67 BuiltinAudioProcessingBuilder& SetCapturePostProcessing( 68 std::unique_ptr<CustomProcessing> capture_post_processing) { 69 capture_post_processing_ = std::move(capture_post_processing); 70 return *this; 71 } 72 73 // Sets the render pre-processing sub-module to inject when APM is created. 74 BuiltinAudioProcessingBuilder& SetRenderPreProcessing( 75 std::unique_ptr<CustomProcessing> render_pre_processing) { 76 render_pre_processing_ = std::move(render_pre_processing); 77 return *this; 78 } 79 80 // Sets the echo detector to inject when APM is created. 81 BuiltinAudioProcessingBuilder& SetEchoDetector( 82 scoped_refptr<EchoDetector> echo_detector) { 83 echo_detector_ = std::move(echo_detector); 84 return *this; 85 } 86 87 // Sets the capture analyzer sub-module to inject when APM is created. 88 BuiltinAudioProcessingBuilder& SetCaptureAnalyzer( 89 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer) { 90 capture_analyzer_ = std::move(capture_analyzer); 91 return *this; 92 } 93 94 // The BuiltinAudioProcessingBuilder takes ownership of the 95 // neural_residual_echo_estimator. 96 BuiltinAudioProcessingBuilder& SetNeuralResidualEchoEstimator( 97 std::unique_ptr<NeuralResidualEchoEstimator> 98 neural_residual_echo_estimator) { 99 neural_residual_echo_estimator_ = std::move(neural_residual_echo_estimator); 100 return *this; 101 } 102 103 // Creates an APM instance with the specified config or the default one if 104 // unspecified. Injects the specified components transferring the ownership 105 // to the newly created APM instance. 106 absl_nullable scoped_refptr<AudioProcessing> Build( 107 const Environment& env) override; 108 109 private: 110 AudioProcessing::Config config_; 111 std::optional<EchoCanceller3Config> echo_canceller_config_; 112 std::optional<EchoCanceller3Config> echo_canceller_multichannel_config_; 113 std::unique_ptr<EchoControlFactory> echo_control_factory_; 114 std::unique_ptr<CustomProcessing> capture_post_processing_; 115 std::unique_ptr<CustomProcessing> render_pre_processing_; 116 scoped_refptr<EchoDetector> echo_detector_; 117 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer_; 118 std::unique_ptr<NeuralResidualEchoEstimator> neural_residual_echo_estimator_; 119 }; 120 121 } // namespace webrtc 122 123 #endif // API_AUDIO_BUILTIN_AUDIO_PROCESSING_BUILDER_H_