andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/audio_processing_impl.h" |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 12 | |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 13 | #include <array> |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 14 | #include <memory> |
Hanna Silen | c69188d | 2022-09-16 11:38:56 +0200 | [diff] [blame] | 15 | #include <tuple> |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 16 | |
Hanna Silen | c69188d | 2022-09-16 11:38:56 +0200 | [diff] [blame] | 17 | #include "absl/types/optional.h" |
Niels Möller | 105711e | 2022-06-14 15:48:26 +0200 | [diff] [blame] | 18 | #include "api/make_ref_counted.h" |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 19 | #include "api/scoped_refptr.h" |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 20 | #include "modules/audio_processing/include/audio_processing.h" |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 21 | #include "modules/audio_processing/optionally_built_submodule_creators.h" |
Per Åhgren | cc73ed3 | 2020-04-26 23:56:17 +0200 | [diff] [blame] | 22 | #include "modules/audio_processing/test/audio_processing_builder_for_testing.h" |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 23 | #include "modules/audio_processing/test/echo_canceller_test_tools.h" |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 24 | #include "modules/audio_processing/test/echo_control_mock.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "modules/audio_processing/test/test_utils.h" |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 26 | #include "rtc_base/checks.h" |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 27 | #include "rtc_base/random.h" |
Hanna Silen | c69188d | 2022-09-16 11:38:56 +0200 | [diff] [blame] | 28 | #include "rtc_base/strings/string_builder.h" |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 29 | #include "test/field_trial.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 30 | #include "test/gmock.h" |
| 31 | #include "test/gtest.h" |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 32 | |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 33 | namespace webrtc { |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 34 | namespace { |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 35 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 36 | using ::testing::Invoke; |
| 37 | using ::testing::NotNull; |
| 38 | |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 39 | class MockInitialize : public AudioProcessingImpl { |
| 40 | public: |
Alessio Bazzica | be1b898 | 2021-09-17 08:26:10 +0200 | [diff] [blame] | 41 | MockInitialize() : AudioProcessingImpl() {} |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 42 | |
Per Åhgren | 0ade983 | 2020-09-01 23:57:20 +0200 | [diff] [blame] | 43 | MOCK_METHOD(void, InitializeLocked, (), (override)); |
Niels Möller | 5b74723 | 2021-07-26 17:16:25 +0200 | [diff] [blame] | 44 | void RealInitializeLocked() { |
| 45 | AssertLockedForTest(); |
Per Åhgren | 0ade983 | 2020-09-01 23:57:20 +0200 | [diff] [blame] | 46 | AudioProcessingImpl::InitializeLocked(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 47 | } |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 48 | |
Danil Chapovalov | 704fb55 | 2020-05-18 15:10:15 +0200 | [diff] [blame] | 49 | MOCK_METHOD(void, AddRef, (), (const, override)); |
| 50 | MOCK_METHOD(rtc::RefCountReleaseStatus, Release, (), (const, override)); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 53 | // Creates MockEchoControl instances and provides a raw pointer access to |
| 54 | // the next created one. The raw pointer is meant to be used with gmock. |
| 55 | // Returning a pointer of the next created MockEchoControl instance is necessary |
| 56 | // for the following reasons: (i) gmock expectations must be set before any call |
| 57 | // occurs, (ii) APM is initialized the first time that |
| 58 | // AudioProcessingImpl::ProcessStream() is called and the initialization leads |
| 59 | // to the creation of a new EchoControl object. |
| 60 | class MockEchoControlFactory : public EchoControlFactory { |
| 61 | public: |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 62 | MockEchoControlFactory() : next_mock_(std::make_unique<MockEchoControl>()) {} |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 63 | // Returns a pointer to the next MockEchoControl that this factory creates. |
| 64 | MockEchoControl* GetNext() const { return next_mock_.get(); } |
Per Åhgren | 4e5c709 | 2019-11-01 20:44:11 +0100 | [diff] [blame] | 65 | std::unique_ptr<EchoControl> Create(int sample_rate_hz, |
| 66 | int num_render_channels, |
| 67 | int num_capture_channels) override { |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 68 | std::unique_ptr<EchoControl> mock = std::move(next_mock_); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 69 | next_mock_ = std::make_unique<MockEchoControl>(); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 70 | return mock; |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | std::unique_ptr<MockEchoControl> next_mock_; |
| 75 | }; |
| 76 | |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 77 | // Mocks EchoDetector and records the first samples of the last analyzed render |
| 78 | // stream frame. Used to check what data is read by an EchoDetector |
| 79 | // implementation injected into an APM. |
| 80 | class TestEchoDetector : public EchoDetector { |
| 81 | public: |
| 82 | TestEchoDetector() |
| 83 | : analyze_render_audio_called_(false), |
| 84 | last_render_audio_first_sample_(0.f) {} |
| 85 | ~TestEchoDetector() override = default; |
| 86 | void AnalyzeRenderAudio(rtc::ArrayView<const float> render_audio) override { |
| 87 | last_render_audio_first_sample_ = render_audio[0]; |
| 88 | analyze_render_audio_called_ = true; |
| 89 | } |
| 90 | void AnalyzeCaptureAudio(rtc::ArrayView<const float> capture_audio) override { |
| 91 | } |
| 92 | void Initialize(int capture_sample_rate_hz, |
| 93 | int num_capture_channels, |
| 94 | int render_sample_rate_hz, |
| 95 | int num_render_channels) override {} |
| 96 | EchoDetector::Metrics GetMetrics() const override { return {}; } |
| 97 | // Returns true if AnalyzeRenderAudio() has been called at least once. |
| 98 | bool analyze_render_audio_called() const { |
| 99 | return analyze_render_audio_called_; |
| 100 | } |
| 101 | // Returns the first sample of the last analyzed render frame. |
| 102 | float last_render_audio_first_sample() const { |
| 103 | return last_render_audio_first_sample_; |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | bool analyze_render_audio_called_; |
| 108 | float last_render_audio_first_sample_; |
| 109 | }; |
| 110 | |
| 111 | // Mocks CustomProcessing and applies ProcessSample() to all the samples. |
| 112 | // Meant to be injected into an APM to modify samples in a known and detectable |
| 113 | // way. |
| 114 | class TestRenderPreProcessor : public CustomProcessing { |
| 115 | public: |
| 116 | TestRenderPreProcessor() = default; |
| 117 | ~TestRenderPreProcessor() = default; |
| 118 | void Initialize(int sample_rate_hz, int num_channels) override {} |
| 119 | void Process(AudioBuffer* audio) override { |
| 120 | for (size_t k = 0; k < audio->num_channels(); ++k) { |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 121 | rtc::ArrayView<float> channel_view(audio->channels()[k], |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 122 | audio->num_frames()); |
| 123 | std::transform(channel_view.begin(), channel_view.end(), |
| 124 | channel_view.begin(), ProcessSample); |
| 125 | } |
Mirko Bonadei | c4dd730 | 2019-02-25 09:12:02 +0100 | [diff] [blame] | 126 | } |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 127 | std::string ToString() const override { return "TestRenderPreProcessor"; } |
| 128 | void SetRuntimeSetting(AudioProcessing::RuntimeSetting setting) override {} |
| 129 | // Modifies a sample. This member is used in Process() to modify a frame and |
| 130 | // it is publicly visible to enable tests. |
| 131 | static constexpr float ProcessSample(float x) { return 2.f * x; } |
| 132 | }; |
| 133 | |
Hanna Silen | c69188d | 2022-09-16 11:38:56 +0200 | [diff] [blame] | 134 | // Creates a simple `AudioProcessing` instance for APM input volume testing |
Alessio Bazzica | 9ea5381 | 2022-10-13 17:09:15 +0200 | [diff] [blame^] | 135 | // with analog and digital AGC enabled. |
| 136 | rtc::scoped_refptr<AudioProcessing> CreateApmForInputVolumeTest() { |
Hanna Silen | c69188d | 2022-09-16 11:38:56 +0200 | [diff] [blame] | 137 | webrtc::AudioProcessing::Config config; |
| 138 | // Enable AGC1 analog. |
| 139 | config.gain_controller1.enabled = true; |
| 140 | config.gain_controller1.analog_gain_controller.enabled = true; |
Alessio Bazzica | 9ea5381 | 2022-10-13 17:09:15 +0200 | [diff] [blame^] | 141 | // Enable AGC2 adaptive digital. |
| 142 | config.gain_controller1.analog_gain_controller.enable_digital_adaptive = |
| 143 | false; |
Hanna Silen | c69188d | 2022-09-16 11:38:56 +0200 | [diff] [blame] | 144 | config.gain_controller2.enabled = true; |
| 145 | config.gain_controller2.adaptive_digital.enabled = true; |
| 146 | |
| 147 | auto apm(AudioProcessingBuilder().Create()); |
| 148 | apm->ApplyConfig(config); |
| 149 | return apm; |
| 150 | } |
| 151 | |
| 152 | // Runs `apm` input processing for volume adjustments for `num_frames` random |
| 153 | // frames starting from the volume `initial_volume`. This includes three steps: |
| 154 | // 1) Set the input volume 2) Process the stream 3) Set the new recommended |
| 155 | // input volume. Returns the new recommended input volume. |
| 156 | int ProcessInputVolume(AudioProcessing& apm, |
| 157 | int num_frames, |
| 158 | int initial_volume) { |
| 159 | constexpr int kSampleRateHz = 48000; |
| 160 | constexpr int kNumChannels = 1; |
| 161 | std::array<float, kSampleRateHz / 100> buffer; |
| 162 | float* channel_pointers[] = {buffer.data()}; |
| 163 | StreamConfig stream_config(/*sample_rate_hz=*/kSampleRateHz, |
| 164 | /*num_channels=*/kNumChannels); |
| 165 | int recommended_input_volume = initial_volume; |
| 166 | for (int i = 0; i < num_frames; ++i) { |
| 167 | Random random_generator(2341U); |
| 168 | RandomizeSampleVector(&random_generator, buffer); |
| 169 | |
| 170 | apm.set_stream_analog_level(recommended_input_volume); |
| 171 | apm.ProcessStream(channel_pointers, stream_config, stream_config, |
| 172 | channel_pointers); |
| 173 | recommended_input_volume = apm.recommended_stream_analog_level(); |
| 174 | } |
| 175 | return recommended_input_volume; |
| 176 | } |
| 177 | |
| 178 | constexpr char kMinMicLevelFieldTrial[] = |
| 179 | "WebRTC-Audio-2ndAgcMinMicLevelExperiment"; |
| 180 | constexpr int kMinInputVolume = 12; |
| 181 | |
| 182 | std::string GetMinMicLevelExperimentFieldTrial(absl::optional<int> value) { |
| 183 | char field_trial_buffer[64]; |
| 184 | rtc::SimpleStringBuilder builder(field_trial_buffer); |
| 185 | if (value.has_value()) { |
| 186 | RTC_DCHECK_GE(*value, 0); |
| 187 | RTC_DCHECK_LE(*value, 255); |
| 188 | builder << kMinMicLevelFieldTrial << "/Enabled-" << *value << "/"; |
| 189 | } else { |
| 190 | builder << kMinMicLevelFieldTrial << "/Disabled/"; |
| 191 | } |
| 192 | return builder.str(); |
| 193 | } |
| 194 | |
| 195 | // TODO(webrtc:7494): Remove the fieldtrial from the input volume tests when |
| 196 | // "WebRTC-Audio-2ndAgcMinMicLevelExperiment" is removed. |
| 197 | class InputVolumeStartupParameterizedTest |
Alessio Bazzica | 9ea5381 | 2022-10-13 17:09:15 +0200 | [diff] [blame^] | 198 | : public ::testing::TestWithParam<std::tuple<int, absl::optional<int>>> { |
Hanna Silen | c69188d | 2022-09-16 11:38:56 +0200 | [diff] [blame] | 199 | protected: |
| 200 | InputVolumeStartupParameterizedTest() |
| 201 | : field_trials_( |
Alessio Bazzica | 9ea5381 | 2022-10-13 17:09:15 +0200 | [diff] [blame^] | 202 | GetMinMicLevelExperimentFieldTrial(std::get<1>(GetParam()))) {} |
| 203 | int GetStartupVolume() const { return std::get<0>(GetParam()); } |
Hanna Silen | c69188d | 2022-09-16 11:38:56 +0200 | [diff] [blame] | 204 | int GetMinVolume() const { |
Alessio Bazzica | 9ea5381 | 2022-10-13 17:09:15 +0200 | [diff] [blame^] | 205 | return std::get<1>(GetParam()).value_or(kMinInputVolume); |
Hanna Silen | c69188d | 2022-09-16 11:38:56 +0200 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | private: |
| 209 | test::ScopedFieldTrials field_trials_; |
| 210 | }; |
| 211 | |
| 212 | class InputVolumeNotZeroParameterizedTest |
| 213 | : public ::testing::TestWithParam< |
| 214 | std::tuple<int, int, absl::optional<int>>> { |
| 215 | protected: |
| 216 | InputVolumeNotZeroParameterizedTest() |
| 217 | : field_trials_( |
| 218 | GetMinMicLevelExperimentFieldTrial(std::get<2>(GetParam()))) {} |
| 219 | int GetStartupVolume() const { return std::get<0>(GetParam()); } |
| 220 | int GetVolume() const { return std::get<1>(GetParam()); } |
| 221 | int GetMinVolume() const { |
| 222 | return std::get<2>(GetParam()).value_or(kMinInputVolume); |
| 223 | } |
| 224 | bool GetMinMicLevelExperimentEnabled() { |
| 225 | return std::get<2>(GetParam()).has_value(); |
| 226 | } |
| 227 | |
| 228 | private: |
| 229 | test::ScopedFieldTrials field_trials_; |
| 230 | }; |
| 231 | |
| 232 | class InputVolumeZeroParameterizedTest |
| 233 | : public ::testing::TestWithParam<std::tuple<int, absl::optional<int>>> { |
| 234 | protected: |
| 235 | InputVolumeZeroParameterizedTest() |
| 236 | : field_trials_( |
| 237 | GetMinMicLevelExperimentFieldTrial(std::get<1>(GetParam()))) {} |
| 238 | int GetStartupVolume() const { return std::get<0>(GetParam()); } |
| 239 | int GetMinVolume() const { |
| 240 | return std::get<1>(GetParam()).value_or(kMinInputVolume); |
| 241 | } |
| 242 | |
| 243 | private: |
| 244 | test::ScopedFieldTrials field_trials_; |
| 245 | }; |
| 246 | |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 247 | } // namespace |
| 248 | |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 249 | TEST(AudioProcessingImplTest, AudioParameterChangeTriggersInit) { |
Alessio Bazzica | be1b898 | 2021-09-17 08:26:10 +0200 | [diff] [blame] | 250 | MockInitialize mock; |
| 251 | ON_CALL(mock, InitializeLocked) |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 252 | .WillByDefault(Invoke(&mock, &MockInitialize::RealInitializeLocked)); |
| 253 | |
Alessio Bazzica | be1b898 | 2021-09-17 08:26:10 +0200 | [diff] [blame] | 254 | EXPECT_CALL(mock, InitializeLocked).Times(1); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 255 | mock.Initialize(); |
| 256 | |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 257 | constexpr size_t kMaxSampleRateHz = 32000; |
| 258 | constexpr size_t kMaxNumChannels = 2; |
| 259 | std::array<int16_t, kMaxNumChannels * kMaxSampleRateHz / 100> frame; |
| 260 | frame.fill(0); |
Henrik Lundin | 64253a9 | 2022-02-04 09:02:48 +0000 | [diff] [blame] | 261 | StreamConfig config(16000, 1); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 262 | // Call with the default parameters; there should be an init. |
Alessio Bazzica | be1b898 | 2021-09-17 08:26:10 +0200 | [diff] [blame] | 263 | EXPECT_CALL(mock, InitializeLocked).Times(0); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 264 | EXPECT_NOERR(mock.ProcessStream(frame.data(), config, config, frame.data())); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 265 | EXPECT_NOERR( |
| 266 | mock.ProcessReverseStream(frame.data(), config, config, frame.data())); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 267 | |
| 268 | // New sample rate. (Only impacts ProcessStream). |
Henrik Lundin | 64253a9 | 2022-02-04 09:02:48 +0000 | [diff] [blame] | 269 | config = StreamConfig(32000, 1); |
Alessio Bazzica | be1b898 | 2021-09-17 08:26:10 +0200 | [diff] [blame] | 270 | EXPECT_CALL(mock, InitializeLocked).Times(1); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 271 | EXPECT_NOERR(mock.ProcessStream(frame.data(), config, config, frame.data())); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 272 | |
| 273 | // New number of channels. |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 274 | // TODO(peah): Investigate why this causes 2 inits. |
Henrik Lundin | 64253a9 | 2022-02-04 09:02:48 +0000 | [diff] [blame] | 275 | config = StreamConfig(32000, 2); |
Alessio Bazzica | be1b898 | 2021-09-17 08:26:10 +0200 | [diff] [blame] | 276 | EXPECT_CALL(mock, InitializeLocked).Times(2); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 277 | EXPECT_NOERR(mock.ProcessStream(frame.data(), config, config, frame.data())); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 278 | // ProcessStream sets num_channels_ == num_output_channels. |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 279 | EXPECT_NOERR( |
| 280 | mock.ProcessReverseStream(frame.data(), config, config, frame.data())); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 281 | |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 282 | // A new sample rate passed to ProcessReverseStream should cause an init. |
Henrik Lundin | 64253a9 | 2022-02-04 09:02:48 +0000 | [diff] [blame] | 283 | config = StreamConfig(16000, 2); |
Alessio Bazzica | be1b898 | 2021-09-17 08:26:10 +0200 | [diff] [blame] | 284 | EXPECT_CALL(mock, InitializeLocked).Times(1); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 285 | EXPECT_NOERR( |
| 286 | mock.ProcessReverseStream(frame.data(), config, config, frame.data())); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 289 | TEST(AudioProcessingImplTest, UpdateCapturePreGainRuntimeSetting) { |
Niels Möller | 4f776ac | 2021-07-02 11:30:54 +0200 | [diff] [blame] | 290 | rtc::scoped_refptr<AudioProcessing> apm = |
| 291 | AudioProcessingBuilderForTesting().Create(); |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 292 | webrtc::AudioProcessing::Config apm_config; |
| 293 | apm_config.pre_amplifier.enabled = true; |
| 294 | apm_config.pre_amplifier.fixed_gain_factor = 1.f; |
| 295 | apm->ApplyConfig(apm_config); |
| 296 | |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 297 | constexpr int kSampleRateHz = 48000; |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 298 | constexpr int16_t kAudioLevel = 10000; |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 299 | constexpr size_t kNumChannels = 2; |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 300 | |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 301 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
Henrik Lundin | 64253a9 | 2022-02-04 09:02:48 +0000 | [diff] [blame] | 302 | StreamConfig config(kSampleRateHz, kNumChannels); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 303 | frame.fill(kAudioLevel); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 304 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 305 | EXPECT_EQ(frame[100], kAudioLevel) |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 306 | << "With factor 1, frame shouldn't be modified."; |
| 307 | |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 308 | constexpr float kGainFactor = 2.f; |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 309 | apm->SetRuntimeSetting( |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 310 | AudioProcessing::RuntimeSetting::CreateCapturePreGain(kGainFactor)); |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 311 | |
| 312 | // Process for two frames to have time to ramp up gain. |
| 313 | for (int i = 0; i < 2; ++i) { |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 314 | frame.fill(kAudioLevel); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 315 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 316 | } |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 317 | EXPECT_EQ(frame[100], kGainFactor * kAudioLevel) |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 318 | << "Frame should be amplified."; |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 319 | } |
| 320 | |
Per Åhgren | db5d728 | 2021-03-15 16:31:04 +0000 | [diff] [blame] | 321 | TEST(AudioProcessingImplTest, |
| 322 | LevelAdjustmentUpdateCapturePreGainRuntimeSetting) { |
Niels Möller | 4f776ac | 2021-07-02 11:30:54 +0200 | [diff] [blame] | 323 | rtc::scoped_refptr<AudioProcessing> apm = |
| 324 | AudioProcessingBuilderForTesting().Create(); |
Per Åhgren | db5d728 | 2021-03-15 16:31:04 +0000 | [diff] [blame] | 325 | webrtc::AudioProcessing::Config apm_config; |
| 326 | apm_config.capture_level_adjustment.enabled = true; |
| 327 | apm_config.capture_level_adjustment.pre_gain_factor = 1.f; |
| 328 | apm->ApplyConfig(apm_config); |
| 329 | |
| 330 | constexpr int kSampleRateHz = 48000; |
| 331 | constexpr int16_t kAudioLevel = 10000; |
| 332 | constexpr size_t kNumChannels = 2; |
| 333 | |
| 334 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
Henrik Lundin | 64253a9 | 2022-02-04 09:02:48 +0000 | [diff] [blame] | 335 | StreamConfig config(kSampleRateHz, kNumChannels); |
Per Åhgren | db5d728 | 2021-03-15 16:31:04 +0000 | [diff] [blame] | 336 | frame.fill(kAudioLevel); |
| 337 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
| 338 | EXPECT_EQ(frame[100], kAudioLevel) |
| 339 | << "With factor 1, frame shouldn't be modified."; |
| 340 | |
| 341 | constexpr float kGainFactor = 2.f; |
| 342 | apm->SetRuntimeSetting( |
| 343 | AudioProcessing::RuntimeSetting::CreateCapturePreGain(kGainFactor)); |
| 344 | |
| 345 | // Process for two frames to have time to ramp up gain. |
| 346 | for (int i = 0; i < 2; ++i) { |
| 347 | frame.fill(kAudioLevel); |
| 348 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
| 349 | } |
| 350 | EXPECT_EQ(frame[100], kGainFactor * kAudioLevel) |
| 351 | << "Frame should be amplified."; |
| 352 | } |
| 353 | |
| 354 | TEST(AudioProcessingImplTest, |
| 355 | LevelAdjustmentUpdateCapturePostGainRuntimeSetting) { |
Niels Möller | 4f776ac | 2021-07-02 11:30:54 +0200 | [diff] [blame] | 356 | rtc::scoped_refptr<AudioProcessing> apm = |
| 357 | AudioProcessingBuilderForTesting().Create(); |
Per Åhgren | db5d728 | 2021-03-15 16:31:04 +0000 | [diff] [blame] | 358 | webrtc::AudioProcessing::Config apm_config; |
| 359 | apm_config.capture_level_adjustment.enabled = true; |
| 360 | apm_config.capture_level_adjustment.post_gain_factor = 1.f; |
| 361 | apm->ApplyConfig(apm_config); |
| 362 | |
| 363 | constexpr int kSampleRateHz = 48000; |
| 364 | constexpr int16_t kAudioLevel = 10000; |
| 365 | constexpr size_t kNumChannels = 2; |
| 366 | |
| 367 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
Henrik Lundin | 64253a9 | 2022-02-04 09:02:48 +0000 | [diff] [blame] | 368 | StreamConfig config(kSampleRateHz, kNumChannels); |
Per Åhgren | db5d728 | 2021-03-15 16:31:04 +0000 | [diff] [blame] | 369 | frame.fill(kAudioLevel); |
| 370 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
| 371 | EXPECT_EQ(frame[100], kAudioLevel) |
| 372 | << "With factor 1, frame shouldn't be modified."; |
| 373 | |
| 374 | constexpr float kGainFactor = 2.f; |
| 375 | apm->SetRuntimeSetting( |
| 376 | AudioProcessing::RuntimeSetting::CreateCapturePostGain(kGainFactor)); |
| 377 | |
| 378 | // Process for two frames to have time to ramp up gain. |
| 379 | for (int i = 0; i < 2; ++i) { |
| 380 | frame.fill(kAudioLevel); |
| 381 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
| 382 | } |
| 383 | EXPECT_EQ(frame[100], kGainFactor * kAudioLevel) |
| 384 | << "Frame should be amplified."; |
| 385 | } |
| 386 | |
Per Åhgren | 652ada5 | 2021-03-03 10:52:44 +0000 | [diff] [blame] | 387 | TEST(AudioProcessingImplTest, EchoControllerObservesSetCaptureUsageChange) { |
| 388 | // Tests that the echo controller observes that the capture usage has been |
| 389 | // updated. |
| 390 | auto echo_control_factory = std::make_unique<MockEchoControlFactory>(); |
| 391 | const MockEchoControlFactory* echo_control_factory_ptr = |
| 392 | echo_control_factory.get(); |
| 393 | |
Niels Möller | 4f776ac | 2021-07-02 11:30:54 +0200 | [diff] [blame] | 394 | rtc::scoped_refptr<AudioProcessing> apm = |
Per Åhgren | 652ada5 | 2021-03-03 10:52:44 +0000 | [diff] [blame] | 395 | AudioProcessingBuilderForTesting() |
| 396 | .SetEchoControlFactory(std::move(echo_control_factory)) |
Niels Möller | 4f776ac | 2021-07-02 11:30:54 +0200 | [diff] [blame] | 397 | .Create(); |
Per Åhgren | 652ada5 | 2021-03-03 10:52:44 +0000 | [diff] [blame] | 398 | |
| 399 | constexpr int16_t kAudioLevel = 10000; |
| 400 | constexpr int kSampleRateHz = 48000; |
| 401 | constexpr int kNumChannels = 2; |
| 402 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
Henrik Lundin | 64253a9 | 2022-02-04 09:02:48 +0000 | [diff] [blame] | 403 | StreamConfig config(kSampleRateHz, kNumChannels); |
Per Åhgren | 652ada5 | 2021-03-03 10:52:44 +0000 | [diff] [blame] | 404 | frame.fill(kAudioLevel); |
| 405 | |
| 406 | MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext(); |
| 407 | |
| 408 | // Ensure that SetCaptureOutputUsage is not called when no runtime settings |
| 409 | // are passed. |
| 410 | EXPECT_CALL(*echo_control_mock, SetCaptureOutputUsage(testing::_)).Times(0); |
| 411 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
| 412 | |
| 413 | // Ensure that SetCaptureOutputUsage is called with the right information when |
| 414 | // a runtime setting is passed. |
| 415 | EXPECT_CALL(*echo_control_mock, |
| 416 | SetCaptureOutputUsage(/*capture_output_used=*/false)) |
| 417 | .Times(1); |
| 418 | EXPECT_TRUE(apm->PostRuntimeSetting( |
| 419 | AudioProcessing::RuntimeSetting::CreateCaptureOutputUsedSetting( |
| 420 | /*capture_output_used=*/false))); |
| 421 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
| 422 | |
| 423 | EXPECT_CALL(*echo_control_mock, |
| 424 | SetCaptureOutputUsage(/*capture_output_used=*/true)) |
| 425 | .Times(1); |
| 426 | EXPECT_TRUE(apm->PostRuntimeSetting( |
| 427 | AudioProcessing::RuntimeSetting::CreateCaptureOutputUsedSetting( |
| 428 | /*capture_output_used=*/true))); |
| 429 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
| 430 | |
| 431 | // The number of positions to place items in the queue is equal to the queue |
| 432 | // size minus 1. |
| 433 | constexpr int kNumSlotsInQueue = RuntimeSettingQueueSize(); |
| 434 | |
| 435 | // Ensure that SetCaptureOutputUsage is called with the right information when |
| 436 | // many runtime settings are passed. |
| 437 | for (int k = 0; k < kNumSlotsInQueue - 1; ++k) { |
| 438 | EXPECT_TRUE(apm->PostRuntimeSetting( |
| 439 | AudioProcessing::RuntimeSetting::CreateCaptureOutputUsedSetting( |
| 440 | /*capture_output_used=*/false))); |
| 441 | } |
| 442 | EXPECT_CALL(*echo_control_mock, |
| 443 | SetCaptureOutputUsage(/*capture_output_used=*/false)) |
| 444 | .Times(kNumSlotsInQueue - 1); |
| 445 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
| 446 | |
| 447 | // Ensure that SetCaptureOutputUsage is properly called with the fallback |
| 448 | // value when the runtime settings queue becomes full. |
| 449 | for (int k = 0; k < kNumSlotsInQueue; ++k) { |
| 450 | EXPECT_TRUE(apm->PostRuntimeSetting( |
| 451 | AudioProcessing::RuntimeSetting::CreateCaptureOutputUsedSetting( |
| 452 | /*capture_output_used=*/false))); |
| 453 | } |
| 454 | EXPECT_FALSE(apm->PostRuntimeSetting( |
| 455 | AudioProcessing::RuntimeSetting::CreateCaptureOutputUsedSetting( |
| 456 | /*capture_output_used=*/false))); |
| 457 | EXPECT_FALSE(apm->PostRuntimeSetting( |
| 458 | AudioProcessing::RuntimeSetting::CreateCaptureOutputUsedSetting( |
| 459 | /*capture_output_used=*/false))); |
| 460 | EXPECT_CALL(*echo_control_mock, |
| 461 | SetCaptureOutputUsage(/*capture_output_used=*/false)) |
| 462 | .Times(kNumSlotsInQueue); |
| 463 | EXPECT_CALL(*echo_control_mock, |
| 464 | SetCaptureOutputUsage(/*capture_output_used=*/true)) |
| 465 | .Times(1); |
| 466 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
| 467 | } |
| 468 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 469 | TEST(AudioProcessingImplTest, |
| 470 | EchoControllerObservesPreAmplifierEchoPathGainChange) { |
| 471 | // Tests that the echo controller observes an echo path gain change when the |
| 472 | // pre-amplifier submodule changes the gain. |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 473 | auto echo_control_factory = std::make_unique<MockEchoControlFactory>(); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 474 | const auto* echo_control_factory_ptr = echo_control_factory.get(); |
| 475 | |
Niels Möller | 4f776ac | 2021-07-02 11:30:54 +0200 | [diff] [blame] | 476 | rtc::scoped_refptr<AudioProcessing> apm = |
Per Åhgren | cc73ed3 | 2020-04-26 23:56:17 +0200 | [diff] [blame] | 477 | AudioProcessingBuilderForTesting() |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 478 | .SetEchoControlFactory(std::move(echo_control_factory)) |
Niels Möller | 4f776ac | 2021-07-02 11:30:54 +0200 | [diff] [blame] | 479 | .Create(); |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 480 | // Disable AGC. |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 481 | webrtc::AudioProcessing::Config apm_config; |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 482 | apm_config.gain_controller1.enabled = false; |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 483 | apm_config.gain_controller2.enabled = false; |
| 484 | apm_config.pre_amplifier.enabled = true; |
| 485 | apm_config.pre_amplifier.fixed_gain_factor = 1.f; |
| 486 | apm->ApplyConfig(apm_config); |
| 487 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 488 | constexpr int16_t kAudioLevel = 10000; |
| 489 | constexpr size_t kSampleRateHz = 48000; |
| 490 | constexpr size_t kNumChannels = 2; |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 491 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
Henrik Lundin | 64253a9 | 2022-02-04 09:02:48 +0000 | [diff] [blame] | 492 | StreamConfig config(kSampleRateHz, kNumChannels); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 493 | frame.fill(kAudioLevel); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 494 | |
| 495 | MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext(); |
| 496 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 497 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | bf47f34 | 2019-05-09 10:50:31 +0200 | [diff] [blame] | 498 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 499 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/false)) |
Fredrik Hernqvist | bf47f34 | 2019-05-09 10:50:31 +0200 | [diff] [blame] | 500 | .Times(1); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 501 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 502 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 503 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | bf47f34 | 2019-05-09 10:50:31 +0200 | [diff] [blame] | 504 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 505 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/true)) |
Fredrik Hernqvist | bf47f34 | 2019-05-09 10:50:31 +0200 | [diff] [blame] | 506 | .Times(1); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 507 | apm->SetRuntimeSetting( |
| 508 | AudioProcessing::RuntimeSetting::CreateCapturePreGain(2.f)); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 509 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | TEST(AudioProcessingImplTest, |
Per Åhgren | db5d728 | 2021-03-15 16:31:04 +0000 | [diff] [blame] | 513 | EchoControllerObservesLevelAdjustmentPreGainEchoPathGainChange) { |
| 514 | // Tests that the echo controller observes an echo path gain change when the |
| 515 | // pre-amplifier submodule changes the gain. |
| 516 | auto echo_control_factory = std::make_unique<MockEchoControlFactory>(); |
| 517 | const auto* echo_control_factory_ptr = echo_control_factory.get(); |
| 518 | |
Niels Möller | 4f776ac | 2021-07-02 11:30:54 +0200 | [diff] [blame] | 519 | rtc::scoped_refptr<AudioProcessing> apm = |
Per Åhgren | db5d728 | 2021-03-15 16:31:04 +0000 | [diff] [blame] | 520 | AudioProcessingBuilderForTesting() |
| 521 | .SetEchoControlFactory(std::move(echo_control_factory)) |
Niels Möller | 4f776ac | 2021-07-02 11:30:54 +0200 | [diff] [blame] | 522 | .Create(); |
Per Åhgren | db5d728 | 2021-03-15 16:31:04 +0000 | [diff] [blame] | 523 | // Disable AGC. |
| 524 | webrtc::AudioProcessing::Config apm_config; |
| 525 | apm_config.gain_controller1.enabled = false; |
| 526 | apm_config.gain_controller2.enabled = false; |
| 527 | apm_config.capture_level_adjustment.enabled = true; |
| 528 | apm_config.capture_level_adjustment.pre_gain_factor = 1.f; |
| 529 | apm->ApplyConfig(apm_config); |
| 530 | |
| 531 | constexpr int16_t kAudioLevel = 10000; |
| 532 | constexpr size_t kSampleRateHz = 48000; |
| 533 | constexpr size_t kNumChannels = 2; |
| 534 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
Henrik Lundin | 64253a9 | 2022-02-04 09:02:48 +0000 | [diff] [blame] | 535 | StreamConfig config(kSampleRateHz, kNumChannels); |
Per Åhgren | db5d728 | 2021-03-15 16:31:04 +0000 | [diff] [blame] | 536 | frame.fill(kAudioLevel); |
| 537 | |
| 538 | MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext(); |
| 539 | |
| 540 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
| 541 | EXPECT_CALL(*echo_control_mock, |
| 542 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/false)) |
| 543 | .Times(1); |
| 544 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
| 545 | |
| 546 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
| 547 | EXPECT_CALL(*echo_control_mock, |
| 548 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/true)) |
| 549 | .Times(1); |
| 550 | apm->SetRuntimeSetting( |
| 551 | AudioProcessing::RuntimeSetting::CreateCapturePreGain(2.f)); |
| 552 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
| 553 | } |
| 554 | |
| 555 | TEST(AudioProcessingImplTest, |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 556 | EchoControllerObservesAnalogAgc1EchoPathGainChange) { |
| 557 | // Tests that the echo controller observes an echo path gain change when the |
| 558 | // AGC1 analog adaptive submodule changes the analog gain. |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 559 | auto echo_control_factory = std::make_unique<MockEchoControlFactory>(); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 560 | const auto* echo_control_factory_ptr = echo_control_factory.get(); |
| 561 | |
Niels Möller | 4f776ac | 2021-07-02 11:30:54 +0200 | [diff] [blame] | 562 | rtc::scoped_refptr<AudioProcessing> apm = |
Per Åhgren | cc73ed3 | 2020-04-26 23:56:17 +0200 | [diff] [blame] | 563 | AudioProcessingBuilderForTesting() |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 564 | .SetEchoControlFactory(std::move(echo_control_factory)) |
Niels Möller | 4f776ac | 2021-07-02 11:30:54 +0200 | [diff] [blame] | 565 | .Create(); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 566 | webrtc::AudioProcessing::Config apm_config; |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 567 | // Enable AGC1. |
| 568 | apm_config.gain_controller1.enabled = true; |
Alessio Bazzica | a5aaedb | 2022-09-06 16:39:07 +0200 | [diff] [blame] | 569 | apm_config.gain_controller1.analog_gain_controller.enabled = true; |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 570 | apm_config.gain_controller2.enabled = false; |
| 571 | apm_config.pre_amplifier.enabled = false; |
| 572 | apm->ApplyConfig(apm_config); |
| 573 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 574 | constexpr int16_t kAudioLevel = 1000; |
| 575 | constexpr size_t kSampleRateHz = 48000; |
| 576 | constexpr size_t kNumChannels = 2; |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 577 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
Henrik Lundin | 64253a9 | 2022-02-04 09:02:48 +0000 | [diff] [blame] | 578 | StreamConfig stream_config(kSampleRateHz, kNumChannels); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 579 | frame.fill(kAudioLevel); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 580 | |
| 581 | MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext(); |
| 582 | |
Alessio Bazzica | a5aaedb | 2022-09-06 16:39:07 +0200 | [diff] [blame] | 583 | constexpr int kInitialStreamAnalogLevel = 123; |
| 584 | apm->set_stream_analog_level(kInitialStreamAnalogLevel); |
| 585 | |
| 586 | // When the first fame is processed, no echo path gain change must be |
| 587 | // detected. |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 588 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Alessio Bazzica | a5aaedb | 2022-09-06 16:39:07 +0200 | [diff] [blame] | 589 | EXPECT_CALL(*echo_control_mock, |
| 590 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/false)) |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 591 | .Times(1); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 592 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data()); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 593 | |
Alessio Bazzica | a5aaedb | 2022-09-06 16:39:07 +0200 | [diff] [blame] | 594 | // Simulate the application of the recommended analog level. |
| 595 | int recommended_analog_level = apm->recommended_stream_analog_level(); |
| 596 | if (recommended_analog_level == kInitialStreamAnalogLevel) { |
| 597 | // Force an analog gain change if it did not happen. |
| 598 | recommended_analog_level++; |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 599 | } |
Alessio Bazzica | a5aaedb | 2022-09-06 16:39:07 +0200 | [diff] [blame] | 600 | apm->set_stream_analog_level(recommended_analog_level); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 601 | |
Alessio Bazzica | a5aaedb | 2022-09-06 16:39:07 +0200 | [diff] [blame] | 602 | // After the first fame and with a stream analog level change, the echo path |
| 603 | // gain change must be detected. |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 604 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Alessio Bazzica | a5aaedb | 2022-09-06 16:39:07 +0200 | [diff] [blame] | 605 | EXPECT_CALL(*echo_control_mock, |
| 606 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/true)) |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 607 | .Times(1); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 608 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data()); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 609 | } |
| 610 | |
Alessio Bazzica | a5aaedb | 2022-09-06 16:39:07 +0200 | [diff] [blame] | 611 | // Tests that a stream is successfully processed when AGC2 adaptive digital is |
| 612 | // used and when the field trial |
| 613 | // `WebRTC-Audio-TransientSuppressorVadMode/Enabled-Default/` is set. |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 614 | TEST(AudioProcessingImplTest, |
Alessio Bazzica | a5aaedb | 2022-09-06 16:39:07 +0200 | [diff] [blame] | 615 | ProcessWithAgc2AndTransientSuppressorVadModeDefault) { |
| 616 | webrtc::test::ScopedFieldTrials field_trials( |
| 617 | "WebRTC-Audio-TransientSuppressorVadMode/Enabled-Default/"); |
| 618 | rtc::scoped_refptr<AudioProcessing> apm = AudioProcessingBuilder().Create(); |
| 619 | ASSERT_EQ(apm->Initialize(), AudioProcessing::kNoError); |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 620 | webrtc::AudioProcessing::Config apm_config; |
| 621 | // Disable AGC1 analog. |
| 622 | apm_config.gain_controller1.enabled = false; |
| 623 | // Enable AGC2 digital. |
| 624 | apm_config.gain_controller2.enabled = true; |
| 625 | apm_config.gain_controller2.adaptive_digital.enabled = true; |
| 626 | apm->ApplyConfig(apm_config); |
Alessio Bazzica | a5aaedb | 2022-09-06 16:39:07 +0200 | [diff] [blame] | 627 | constexpr int kSampleRateHz = 48000; |
| 628 | constexpr int kNumChannels = 1; |
| 629 | std::array<float, kSampleRateHz / 100> buffer; |
| 630 | float* channel_pointers[] = {buffer.data()}; |
| 631 | StreamConfig stream_config(/*sample_rate_hz=*/kSampleRateHz, |
| 632 | /*num_channels=*/kNumChannels); |
| 633 | Random random_generator(2341U); |
| 634 | constexpr int kFramesToProcess = 10; |
| 635 | for (int i = 0; i < kFramesToProcess; ++i) { |
| 636 | RandomizeSampleVector(&random_generator, buffer); |
| 637 | ASSERT_EQ(apm->ProcessStream(channel_pointers, stream_config, stream_config, |
| 638 | channel_pointers), |
| 639 | kNoErr); |
| 640 | } |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 641 | } |
| 642 | |
Alessio Bazzica | a5aaedb | 2022-09-06 16:39:07 +0200 | [diff] [blame] | 643 | // Tests that a stream is successfully processed when AGC2 adaptive digital is |
| 644 | // used and when the field trial |
| 645 | // `WebRTC-Audio-TransientSuppressorVadMode/Enabled-RnnVad/` is set. |
| 646 | TEST(AudioProcessingImplTest, |
| 647 | ProcessWithAgc2AndTransientSuppressorVadModeRnnVad) { |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 648 | webrtc::test::ScopedFieldTrials field_trials( |
| 649 | "WebRTC-Audio-TransientSuppressorVadMode/Enabled-RnnVad/"); |
| 650 | rtc::scoped_refptr<AudioProcessing> apm = AudioProcessingBuilder().Create(); |
| 651 | ASSERT_EQ(apm->Initialize(), AudioProcessing::kNoError); |
| 652 | webrtc::AudioProcessing::Config apm_config; |
| 653 | // Disable AGC1 analog. |
| 654 | apm_config.gain_controller1.enabled = false; |
| 655 | // Enable AGC2 digital. |
| 656 | apm_config.gain_controller2.enabled = true; |
| 657 | apm_config.gain_controller2.adaptive_digital.enabled = true; |
| 658 | apm->ApplyConfig(apm_config); |
| 659 | constexpr int kSampleRateHz = 48000; |
| 660 | constexpr int kNumChannels = 1; |
| 661 | std::array<float, kSampleRateHz / 100> buffer; |
| 662 | float* channel_pointers[] = {buffer.data()}; |
| 663 | StreamConfig stream_config(/*sample_rate_hz=*/kSampleRateHz, |
| 664 | /*num_channels=*/kNumChannels); |
| 665 | Random random_generator(2341U); |
| 666 | constexpr int kFramesToProcess = 10; |
| 667 | for (int i = 0; i < kFramesToProcess; ++i) { |
| 668 | RandomizeSampleVector(&random_generator, buffer); |
| 669 | ASSERT_EQ(apm->ProcessStream(channel_pointers, stream_config, stream_config, |
| 670 | channel_pointers), |
| 671 | kNoErr); |
| 672 | } |
| 673 | } |
| 674 | |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 675 | TEST(AudioProcessingImplTest, EchoControllerObservesPlayoutVolumeChange) { |
| 676 | // Tests that the echo controller observes an echo path gain change when a |
| 677 | // playout volume change is reported. |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 678 | auto echo_control_factory = std::make_unique<MockEchoControlFactory>(); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 679 | const auto* echo_control_factory_ptr = echo_control_factory.get(); |
| 680 | |
Niels Möller | 4f776ac | 2021-07-02 11:30:54 +0200 | [diff] [blame] | 681 | rtc::scoped_refptr<AudioProcessing> apm = |
Per Åhgren | cc73ed3 | 2020-04-26 23:56:17 +0200 | [diff] [blame] | 682 | AudioProcessingBuilderForTesting() |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 683 | .SetEchoControlFactory(std::move(echo_control_factory)) |
Niels Möller | 4f776ac | 2021-07-02 11:30:54 +0200 | [diff] [blame] | 684 | .Create(); |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 685 | // Disable AGC. |
| 686 | webrtc::AudioProcessing::Config apm_config; |
| 687 | apm_config.gain_controller1.enabled = false; |
| 688 | apm_config.gain_controller2.enabled = false; |
| 689 | apm->ApplyConfig(apm_config); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 690 | |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 691 | constexpr int16_t kAudioLevel = 10000; |
| 692 | constexpr size_t kSampleRateHz = 48000; |
| 693 | constexpr size_t kNumChannels = 2; |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 694 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
Henrik Lundin | 64253a9 | 2022-02-04 09:02:48 +0000 | [diff] [blame] | 695 | StreamConfig stream_config(kSampleRateHz, kNumChannels); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 696 | frame.fill(kAudioLevel); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 697 | |
| 698 | MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext(); |
| 699 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 700 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 701 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 702 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/false)) |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 703 | .Times(1); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 704 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data()); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 705 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 706 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 707 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 708 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/false)) |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 709 | .Times(1); |
| 710 | apm->SetRuntimeSetting( |
| 711 | AudioProcessing::RuntimeSetting::CreatePlayoutVolumeChange(50)); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 712 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data()); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 713 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 714 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 715 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 716 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/false)) |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 717 | .Times(1); |
| 718 | apm->SetRuntimeSetting( |
| 719 | AudioProcessing::RuntimeSetting::CreatePlayoutVolumeChange(50)); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 720 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data()); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 721 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 722 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 723 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 724 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/true)) |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 725 | .Times(1); |
| 726 | apm->SetRuntimeSetting( |
| 727 | AudioProcessing::RuntimeSetting::CreatePlayoutVolumeChange(100)); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 728 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data()); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 729 | } |
| 730 | |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 731 | TEST(AudioProcessingImplTest, RenderPreProcessorBeforeEchoDetector) { |
| 732 | // Make sure that signal changes caused by a render pre-processing sub-module |
| 733 | // take place before any echo detector analysis. |
Tommi | 87f7090 | 2021-04-27 14:43:08 +0200 | [diff] [blame] | 734 | auto test_echo_detector = rtc::make_ref_counted<TestEchoDetector>(); |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 735 | std::unique_ptr<CustomProcessing> test_render_pre_processor( |
| 736 | new TestRenderPreProcessor()); |
| 737 | // Create APM injecting the test echo detector and render pre-processor. |
Niels Möller | 4f776ac | 2021-07-02 11:30:54 +0200 | [diff] [blame] | 738 | rtc::scoped_refptr<AudioProcessing> apm = |
Per Åhgren | cc73ed3 | 2020-04-26 23:56:17 +0200 | [diff] [blame] | 739 | AudioProcessingBuilderForTesting() |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 740 | .SetEchoDetector(test_echo_detector) |
| 741 | .SetRenderPreProcessing(std::move(test_render_pre_processor)) |
Niels Möller | 4f776ac | 2021-07-02 11:30:54 +0200 | [diff] [blame] | 742 | .Create(); |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 743 | webrtc::AudioProcessing::Config apm_config; |
| 744 | apm_config.pre_amplifier.enabled = true; |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 745 | apm->ApplyConfig(apm_config); |
| 746 | |
| 747 | constexpr int16_t kAudioLevel = 1000; |
| 748 | constexpr int kSampleRateHz = 16000; |
| 749 | constexpr size_t kNumChannels = 1; |
Sam Zackrisson | 12e319a | 2020-01-03 14:54:20 +0100 | [diff] [blame] | 750 | // Explicitly initialize APM to ensure no render frames are discarded. |
| 751 | const ProcessingConfig processing_config = {{ |
Henrik Lundin | 64253a9 | 2022-02-04 09:02:48 +0000 | [diff] [blame] | 752 | {kSampleRateHz, kNumChannels}, |
| 753 | {kSampleRateHz, kNumChannels}, |
| 754 | {kSampleRateHz, kNumChannels}, |
| 755 | {kSampleRateHz, kNumChannels}, |
Sam Zackrisson | 12e319a | 2020-01-03 14:54:20 +0100 | [diff] [blame] | 756 | }}; |
| 757 | apm->Initialize(processing_config); |
| 758 | |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 759 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
Henrik Lundin | 64253a9 | 2022-02-04 09:02:48 +0000 | [diff] [blame] | 760 | StreamConfig stream_config(kSampleRateHz, kNumChannels); |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 761 | |
| 762 | constexpr float kAudioLevelFloat = static_cast<float>(kAudioLevel); |
| 763 | constexpr float kExpectedPreprocessedAudioLevel = |
| 764 | TestRenderPreProcessor::ProcessSample(kAudioLevelFloat); |
| 765 | ASSERT_NE(kAudioLevelFloat, kExpectedPreprocessedAudioLevel); |
| 766 | |
| 767 | // Analyze a render stream frame. |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 768 | frame.fill(kAudioLevel); |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 769 | ASSERT_EQ(AudioProcessing::Error::kNoError, |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 770 | apm->ProcessReverseStream(frame.data(), stream_config, |
| 771 | stream_config, frame.data())); |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 772 | // Trigger a call to in EchoDetector::AnalyzeRenderAudio() via |
| 773 | // ProcessStream(). |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 774 | frame.fill(kAudioLevel); |
| 775 | ASSERT_EQ(AudioProcessing::Error::kNoError, |
| 776 | apm->ProcessStream(frame.data(), stream_config, stream_config, |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 777 | frame.data())); |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 778 | // Regardless of how the call to in EchoDetector::AnalyzeRenderAudio() is |
| 779 | // triggered, the line below checks that the call has occurred. If not, the |
| 780 | // APM implementation may have changed and this test might need to be adapted. |
| 781 | ASSERT_TRUE(test_echo_detector->analyze_render_audio_called()); |
| 782 | // Check that the data read in EchoDetector::AnalyzeRenderAudio() is that |
| 783 | // produced by the render pre-processor. |
| 784 | EXPECT_EQ(kExpectedPreprocessedAudioLevel, |
| 785 | test_echo_detector->last_render_audio_first_sample()); |
| 786 | } |
| 787 | |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 788 | // Disabling build-optional submodules and trying to enable them via the APM |
| 789 | // config should be bit-exact with running APM with said submodules disabled. |
| 790 | // This mainly tests that SetCreateOptionalSubmodulesForTesting has an effect. |
| 791 | TEST(ApmWithSubmodulesExcludedTest, BitexactWithDisabledModules) { |
Alessio Bazzica | be1b898 | 2021-09-17 08:26:10 +0200 | [diff] [blame] | 792 | auto apm = rtc::make_ref_counted<AudioProcessingImpl>(); |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 793 | ASSERT_EQ(apm->Initialize(), AudioProcessing::kNoError); |
| 794 | |
| 795 | ApmSubmoduleCreationOverrides overrides; |
| 796 | overrides.transient_suppression = true; |
| 797 | apm->OverrideSubmoduleCreationForTesting(overrides); |
| 798 | |
| 799 | AudioProcessing::Config apm_config = apm->GetConfig(); |
| 800 | apm_config.transient_suppression.enabled = true; |
| 801 | apm->ApplyConfig(apm_config); |
| 802 | |
| 803 | rtc::scoped_refptr<AudioProcessing> apm_reference = |
| 804 | AudioProcessingBuilder().Create(); |
| 805 | apm_config = apm_reference->GetConfig(); |
| 806 | apm_config.transient_suppression.enabled = false; |
| 807 | apm_reference->ApplyConfig(apm_config); |
| 808 | |
| 809 | constexpr int kSampleRateHz = 16000; |
| 810 | constexpr int kNumChannels = 1; |
| 811 | std::array<float, kSampleRateHz / 100> buffer; |
| 812 | std::array<float, kSampleRateHz / 100> buffer_reference; |
| 813 | float* channel_pointers[] = {buffer.data()}; |
| 814 | float* channel_pointers_reference[] = {buffer_reference.data()}; |
| 815 | StreamConfig stream_config(/*sample_rate_hz=*/kSampleRateHz, |
Henrik Lundin | 64253a9 | 2022-02-04 09:02:48 +0000 | [diff] [blame] | 816 | /*num_channels=*/kNumChannels); |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 817 | Random random_generator(2341U); |
| 818 | constexpr int kFramesToProcessPerConfiguration = 10; |
| 819 | |
| 820 | for (int i = 0; i < kFramesToProcessPerConfiguration; ++i) { |
| 821 | RandomizeSampleVector(&random_generator, buffer); |
| 822 | std::copy(buffer.begin(), buffer.end(), buffer_reference.begin()); |
| 823 | ASSERT_EQ(apm->ProcessStream(channel_pointers, stream_config, stream_config, |
| 824 | channel_pointers), |
| 825 | kNoErr); |
| 826 | ASSERT_EQ( |
| 827 | apm_reference->ProcessStream(channel_pointers_reference, stream_config, |
| 828 | stream_config, channel_pointers_reference), |
| 829 | kNoErr); |
| 830 | for (int j = 0; j < kSampleRateHz / 100; ++j) { |
| 831 | EXPECT_EQ(buffer[j], buffer_reference[j]); |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | // Disable transient suppressor creation and run APM in ways that should trigger |
| 837 | // calls to the transient suppressor API. |
| 838 | TEST(ApmWithSubmodulesExcludedTest, ReinitializeTransientSuppressor) { |
Alessio Bazzica | be1b898 | 2021-09-17 08:26:10 +0200 | [diff] [blame] | 839 | auto apm = rtc::make_ref_counted<AudioProcessingImpl>(); |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 840 | ASSERT_EQ(apm->Initialize(), kNoErr); |
| 841 | |
| 842 | ApmSubmoduleCreationOverrides overrides; |
| 843 | overrides.transient_suppression = true; |
| 844 | apm->OverrideSubmoduleCreationForTesting(overrides); |
| 845 | |
| 846 | AudioProcessing::Config config = apm->GetConfig(); |
| 847 | config.transient_suppression.enabled = true; |
| 848 | apm->ApplyConfig(config); |
| 849 | // 960 samples per frame: 10 ms of <= 48 kHz audio with <= 2 channels. |
| 850 | float buffer[960]; |
| 851 | float* channel_pointers[] = {&buffer[0], &buffer[480]}; |
| 852 | Random random_generator(2341U); |
| 853 | constexpr int kFramesToProcessPerConfiguration = 3; |
| 854 | |
| 855 | StreamConfig initial_stream_config(/*sample_rate_hz=*/16000, |
Henrik Lundin | 64253a9 | 2022-02-04 09:02:48 +0000 | [diff] [blame] | 856 | /*num_channels=*/1); |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 857 | for (int i = 0; i < kFramesToProcessPerConfiguration; ++i) { |
| 858 | RandomizeSampleVector(&random_generator, buffer); |
| 859 | EXPECT_EQ(apm->ProcessStream(channel_pointers, initial_stream_config, |
| 860 | initial_stream_config, channel_pointers), |
| 861 | kNoErr); |
| 862 | } |
| 863 | |
| 864 | StreamConfig stereo_stream_config(/*sample_rate_hz=*/16000, |
Henrik Lundin | 64253a9 | 2022-02-04 09:02:48 +0000 | [diff] [blame] | 865 | /*num_channels=*/2); |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 866 | for (int i = 0; i < kFramesToProcessPerConfiguration; ++i) { |
| 867 | RandomizeSampleVector(&random_generator, buffer); |
| 868 | EXPECT_EQ(apm->ProcessStream(channel_pointers, stereo_stream_config, |
| 869 | stereo_stream_config, channel_pointers), |
| 870 | kNoErr); |
| 871 | } |
| 872 | |
| 873 | StreamConfig high_sample_rate_stream_config(/*sample_rate_hz=*/48000, |
Henrik Lundin | 64253a9 | 2022-02-04 09:02:48 +0000 | [diff] [blame] | 874 | /*num_channels=*/2); |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 875 | for (int i = 0; i < kFramesToProcessPerConfiguration; ++i) { |
| 876 | RandomizeSampleVector(&random_generator, buffer); |
| 877 | EXPECT_EQ( |
| 878 | apm->ProcessStream(channel_pointers, high_sample_rate_stream_config, |
| 879 | high_sample_rate_stream_config, channel_pointers), |
| 880 | kNoErr); |
| 881 | } |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | // Disable transient suppressor creation and run APM in ways that should trigger |
| 885 | // calls to the transient suppressor API. |
| 886 | TEST(ApmWithSubmodulesExcludedTest, ToggleTransientSuppressor) { |
Alessio Bazzica | be1b898 | 2021-09-17 08:26:10 +0200 | [diff] [blame] | 887 | auto apm = rtc::make_ref_counted<AudioProcessingImpl>(); |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 888 | ASSERT_EQ(apm->Initialize(), AudioProcessing::kNoError); |
| 889 | |
| 890 | ApmSubmoduleCreationOverrides overrides; |
| 891 | overrides.transient_suppression = true; |
| 892 | apm->OverrideSubmoduleCreationForTesting(overrides); |
| 893 | |
| 894 | // 960 samples per frame: 10 ms of <= 48 kHz audio with <= 2 channels. |
| 895 | float buffer[960]; |
| 896 | float* channel_pointers[] = {&buffer[0], &buffer[480]}; |
| 897 | Random random_generator(2341U); |
| 898 | constexpr int kFramesToProcessPerConfiguration = 3; |
| 899 | StreamConfig stream_config(/*sample_rate_hz=*/16000, |
Henrik Lundin | 64253a9 | 2022-02-04 09:02:48 +0000 | [diff] [blame] | 900 | /*num_channels=*/1); |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 901 | |
| 902 | AudioProcessing::Config config = apm->GetConfig(); |
| 903 | config.transient_suppression.enabled = true; |
| 904 | apm->ApplyConfig(config); |
| 905 | for (int i = 0; i < kFramesToProcessPerConfiguration; ++i) { |
| 906 | RandomizeSampleVector(&random_generator, buffer); |
| 907 | EXPECT_EQ(apm->ProcessStream(channel_pointers, stream_config, stream_config, |
| 908 | channel_pointers), |
| 909 | kNoErr); |
| 910 | } |
| 911 | |
| 912 | config = apm->GetConfig(); |
| 913 | config.transient_suppression.enabled = false; |
| 914 | apm->ApplyConfig(config); |
| 915 | for (int i = 0; i < kFramesToProcessPerConfiguration; ++i) { |
| 916 | RandomizeSampleVector(&random_generator, buffer); |
| 917 | EXPECT_EQ(apm->ProcessStream(channel_pointers, stream_config, stream_config, |
| 918 | channel_pointers), |
| 919 | kNoErr); |
| 920 | } |
| 921 | |
| 922 | config = apm->GetConfig(); |
| 923 | config.transient_suppression.enabled = true; |
| 924 | apm->ApplyConfig(config); |
| 925 | for (int i = 0; i < kFramesToProcessPerConfiguration; ++i) { |
| 926 | RandomizeSampleVector(&random_generator, buffer); |
| 927 | EXPECT_EQ(apm->ProcessStream(channel_pointers, stream_config, stream_config, |
| 928 | channel_pointers), |
| 929 | kNoErr); |
| 930 | } |
| 931 | } |
Hanna Silen | c69188d | 2022-09-16 11:38:56 +0200 | [diff] [blame] | 932 | |
| 933 | // Tests that the minimum startup volume is applied at the startup. |
| 934 | TEST_P(InputVolumeStartupParameterizedTest, |
| 935 | VerifyStartupMinVolumeAppliedAtStartup) { |
| 936 | const int applied_startup_input_volume = GetStartupVolume(); |
Hanna Silen | c69188d | 2022-09-16 11:38:56 +0200 | [diff] [blame] | 937 | const int expected_volume = |
Alessio Bazzica | 9ea5381 | 2022-10-13 17:09:15 +0200 | [diff] [blame^] | 938 | std::max(applied_startup_input_volume, GetMinVolume()); |
| 939 | auto apm = CreateApmForInputVolumeTest(); |
Hanna Silen | c69188d | 2022-09-16 11:38:56 +0200 | [diff] [blame] | 940 | |
| 941 | const int recommended_input_volume = |
| 942 | ProcessInputVolume(*apm, /*num_frames=*/1, applied_startup_input_volume); |
| 943 | |
| 944 | ASSERT_EQ(recommended_input_volume, expected_volume); |
| 945 | } |
| 946 | |
| 947 | // Tests that the minimum input volume is applied if the volume is manually |
| 948 | // adjusted to a non-zero value only if |
| 949 | // "WebRTC-Audio-2ndAgcMinMicLevelExperiment" is enabled. |
| 950 | TEST_P(InputVolumeNotZeroParameterizedTest, |
| 951 | VerifyMinVolumeMaybeAppliedAfterManualVolumeAdjustments) { |
Hanna Silen | c69188d | 2022-09-16 11:38:56 +0200 | [diff] [blame] | 952 | const int applied_startup_input_volume = GetStartupVolume(); |
| 953 | const int applied_input_volume = GetVolume(); |
| 954 | const int expected_volume = std::max(applied_input_volume, GetMinVolume()); |
Alessio Bazzica | 9ea5381 | 2022-10-13 17:09:15 +0200 | [diff] [blame^] | 955 | auto apm = CreateApmForInputVolumeTest(); |
Hanna Silen | c69188d | 2022-09-16 11:38:56 +0200 | [diff] [blame] | 956 | |
| 957 | ProcessInputVolume(*apm, /*num_frames=*/1, applied_startup_input_volume); |
| 958 | const int recommended_input_volume = |
| 959 | ProcessInputVolume(*apm, /*num_frames=*/1, applied_input_volume); |
| 960 | |
| 961 | ASSERT_NE(applied_input_volume, 0); |
| 962 | if (GetMinMicLevelExperimentEnabled()) { |
| 963 | ASSERT_EQ(recommended_input_volume, expected_volume); |
| 964 | } else { |
| 965 | ASSERT_EQ(recommended_input_volume, applied_input_volume); |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | // Tests that the minimum input volume is not applied if the volume is manually |
| 970 | // adjusted to zero. |
| 971 | TEST_P(InputVolumeZeroParameterizedTest, |
| 972 | VerifyMinVolumeNotAppliedAfterManualVolumeAdjustments) { |
Hanna Silen | c69188d | 2022-09-16 11:38:56 +0200 | [diff] [blame] | 973 | constexpr int kZeroVolume = 0; |
| 974 | const int applied_startup_input_volume = GetStartupVolume(); |
Alessio Bazzica | 9ea5381 | 2022-10-13 17:09:15 +0200 | [diff] [blame^] | 975 | auto apm = CreateApmForInputVolumeTest(); |
Hanna Silen | c69188d | 2022-09-16 11:38:56 +0200 | [diff] [blame] | 976 | |
| 977 | const int recommended_input_volume_after_startup = |
| 978 | ProcessInputVolume(*apm, /*num_frames=*/1, applied_startup_input_volume); |
| 979 | const int recommended_input_volume = |
| 980 | ProcessInputVolume(*apm, /*num_frames=*/1, kZeroVolume); |
| 981 | |
| 982 | ASSERT_NE(recommended_input_volume, recommended_input_volume_after_startup); |
| 983 | ASSERT_EQ(recommended_input_volume, kZeroVolume); |
| 984 | } |
| 985 | |
| 986 | // Tests that the minimum input volume is applied if the volume is not zero |
| 987 | // before it is automatically adjusted. |
| 988 | TEST_P(InputVolumeNotZeroParameterizedTest, |
| 989 | VerifyMinVolumeAppliedAfterAutomaticVolumeAdjustments) { |
Hanna Silen | c69188d | 2022-09-16 11:38:56 +0200 | [diff] [blame] | 990 | const int applied_startup_input_volume = GetStartupVolume(); |
| 991 | const int applied_input_volume = GetVolume(); |
Alessio Bazzica | 9ea5381 | 2022-10-13 17:09:15 +0200 | [diff] [blame^] | 992 | auto apm = CreateApmForInputVolumeTest(); |
Hanna Silen | c69188d | 2022-09-16 11:38:56 +0200 | [diff] [blame] | 993 | |
| 994 | ProcessInputVolume(*apm, /*num_frames=*/1, applied_startup_input_volume); |
| 995 | const int recommended_input_volume = |
| 996 | ProcessInputVolume(*apm, /*num_frames=*/400, applied_input_volume); |
| 997 | |
| 998 | ASSERT_NE(applied_input_volume, 0); |
| 999 | if (recommended_input_volume != applied_input_volume) { |
| 1000 | ASSERT_GE(recommended_input_volume, GetMinVolume()); |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | // Tests that the minimum input volume is not applied if the volume is zero |
| 1005 | // before it is automatically adjusted. |
| 1006 | TEST_P(InputVolumeZeroParameterizedTest, |
| 1007 | VerifyMinVolumeNotAppliedAfterAutomaticVolumeAdjustments) { |
Hanna Silen | c69188d | 2022-09-16 11:38:56 +0200 | [diff] [blame] | 1008 | constexpr int kZeroVolume = 0; |
| 1009 | const int applied_startup_input_volume = GetStartupVolume(); |
Alessio Bazzica | 9ea5381 | 2022-10-13 17:09:15 +0200 | [diff] [blame^] | 1010 | auto apm = CreateApmForInputVolumeTest(); |
Hanna Silen | c69188d | 2022-09-16 11:38:56 +0200 | [diff] [blame] | 1011 | |
| 1012 | const int recommended_input_volume_after_startup = |
| 1013 | ProcessInputVolume(*apm, /*num_frames=*/1, applied_startup_input_volume); |
| 1014 | const int recommended_input_volume = |
| 1015 | ProcessInputVolume(*apm, /*num_frames=*/400, kZeroVolume); |
| 1016 | |
| 1017 | ASSERT_NE(recommended_input_volume, recommended_input_volume_after_startup); |
| 1018 | ASSERT_EQ(recommended_input_volume, kZeroVolume); |
| 1019 | } |
| 1020 | |
| 1021 | INSTANTIATE_TEST_SUITE_P(AudioProcessingImplTest, |
| 1022 | InputVolumeStartupParameterizedTest, |
Alessio Bazzica | 9ea5381 | 2022-10-13 17:09:15 +0200 | [diff] [blame^] | 1023 | ::testing::Combine(::testing::Values(0, 5, 30), |
Hanna Silen | c69188d | 2022-09-16 11:38:56 +0200 | [diff] [blame] | 1024 | ::testing::Values(absl::nullopt, |
| 1025 | 20))); |
| 1026 | |
| 1027 | INSTANTIATE_TEST_SUITE_P(AudioProcessingImplTest, |
| 1028 | InputVolumeNotZeroParameterizedTest, |
| 1029 | ::testing::Combine(::testing::Values(0, 5, 15), |
| 1030 | ::testing::Values(1, 5, 30), |
| 1031 | ::testing::Values(absl::nullopt, |
| 1032 | 20))); |
| 1033 | |
| 1034 | INSTANTIATE_TEST_SUITE_P(AudioProcessingImplTest, |
| 1035 | InputVolumeZeroParameterizedTest, |
| 1036 | ::testing::Combine(::testing::Values(0, 5, 15), |
| 1037 | ::testing::Values(absl::nullopt, |
| 1038 | 20))); |
| 1039 | |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 1040 | } // namespace webrtc |