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