xref: /aosp_15_r20/external/webrtc/modules/audio_processing/test/audio_processing_builder_for_testing.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2020 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 MODULES_AUDIO_PROCESSING_TEST_AUDIO_PROCESSING_BUILDER_FOR_TESTING_H_
12 #define MODULES_AUDIO_PROCESSING_TEST_AUDIO_PROCESSING_BUILDER_FOR_TESTING_H_
13 
14 #include <list>
15 #include <memory>
16 #include <utility>
17 #include <vector>
18 
19 #include "modules/audio_processing/include/audio_processing.h"
20 
21 namespace webrtc {
22 
23 // Facilitates building of AudioProcessingImp for the tests.
24 class AudioProcessingBuilderForTesting {
25  public:
26   AudioProcessingBuilderForTesting();
27   AudioProcessingBuilderForTesting(const AudioProcessingBuilderForTesting&) =
28       delete;
29   AudioProcessingBuilderForTesting& operator=(
30       const AudioProcessingBuilderForTesting&) = delete;
31   ~AudioProcessingBuilderForTesting();
32 
33   // Sets the APM configuration.
SetConfig(const AudioProcessing::Config & config)34   AudioProcessingBuilderForTesting& SetConfig(
35       const AudioProcessing::Config& config) {
36     config_ = config;
37     return *this;
38   }
39 
40   // Sets the echo controller factory to inject when APM is created.
SetEchoControlFactory(std::unique_ptr<EchoControlFactory> echo_control_factory)41   AudioProcessingBuilderForTesting& SetEchoControlFactory(
42       std::unique_ptr<EchoControlFactory> echo_control_factory) {
43     echo_control_factory_ = std::move(echo_control_factory);
44     return *this;
45   }
46 
47   // Sets the capture post-processing sub-module to inject when APM is created.
SetCapturePostProcessing(std::unique_ptr<CustomProcessing> capture_post_processing)48   AudioProcessingBuilderForTesting& SetCapturePostProcessing(
49       std::unique_ptr<CustomProcessing> capture_post_processing) {
50     capture_post_processing_ = std::move(capture_post_processing);
51     return *this;
52   }
53 
54   // Sets the render pre-processing sub-module to inject when APM is created.
SetRenderPreProcessing(std::unique_ptr<CustomProcessing> render_pre_processing)55   AudioProcessingBuilderForTesting& SetRenderPreProcessing(
56       std::unique_ptr<CustomProcessing> render_pre_processing) {
57     render_pre_processing_ = std::move(render_pre_processing);
58     return *this;
59   }
60 
61   // Sets the echo detector to inject when APM is created.
SetEchoDetector(rtc::scoped_refptr<EchoDetector> echo_detector)62   AudioProcessingBuilderForTesting& SetEchoDetector(
63       rtc::scoped_refptr<EchoDetector> echo_detector) {
64     echo_detector_ = std::move(echo_detector);
65     return *this;
66   }
67 
68   // Sets the capture analyzer sub-module to inject when APM is created.
SetCaptureAnalyzer(std::unique_ptr<CustomAudioAnalyzer> capture_analyzer)69   AudioProcessingBuilderForTesting& SetCaptureAnalyzer(
70       std::unique_ptr<CustomAudioAnalyzer> capture_analyzer) {
71     capture_analyzer_ = std::move(capture_analyzer);
72     return *this;
73   }
74 
75   // Creates an APM instance with the specified config or the default one if
76   // unspecified. Injects the specified components transferring the ownership
77   // to the newly created APM instance - i.e., except for the config, the
78   // builder is reset to its initial state.
79   rtc::scoped_refptr<AudioProcessing> Create();
80 
81  private:
82   // Transfers the ownership to a non-testing builder.
83   void TransferOwnershipsToBuilder(AudioProcessingBuilder* builder);
84 
85   AudioProcessing::Config config_;
86   std::unique_ptr<EchoControlFactory> echo_control_factory_;
87   std::unique_ptr<CustomProcessing> capture_post_processing_;
88   std::unique_ptr<CustomProcessing> render_pre_processing_;
89   rtc::scoped_refptr<EchoDetector> echo_detector_;
90   std::unique_ptr<CustomAudioAnalyzer> capture_analyzer_;
91 };
92 
93 }  // namespace webrtc
94 
95 #endif  // MODULES_AUDIO_PROCESSING_TEST_AUDIO_PROCESSING_BUILDER_FOR_TESTING_H_
96