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> |
| 15 | |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 16 | #include "api/scoped_refptr.h" |
Per Åhgren | 652ada5 | 2021-03-03 10:52:44 +0000 | [diff] [blame] | 17 | #include "modules/audio_processing/common.h" |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 18 | #include "modules/audio_processing/include/audio_processing.h" |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 19 | #include "modules/audio_processing/optionally_built_submodule_creators.h" |
Per Åhgren | cc73ed3 | 2020-04-26 23:56:17 +0200 | [diff] [blame] | 20 | #include "modules/audio_processing/test/audio_processing_builder_for_testing.h" |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 21 | #include "modules/audio_processing/test/echo_canceller_test_tools.h" |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 22 | #include "modules/audio_processing/test/echo_control_mock.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "modules/audio_processing/test/test_utils.h" |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 24 | #include "rtc_base/checks.h" |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 25 | #include "rtc_base/random.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 26 | #include "rtc_base/ref_counted_object.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "test/gmock.h" |
| 28 | #include "test/gtest.h" |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 29 | |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 30 | namespace webrtc { |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 31 | namespace { |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 32 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 33 | using ::testing::Invoke; |
| 34 | using ::testing::NotNull; |
| 35 | |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 36 | class MockInitialize : public AudioProcessingImpl { |
| 37 | public: |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 38 | explicit MockInitialize(const webrtc::Config& config) |
| 39 | : AudioProcessingImpl(config) {} |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 40 | |
Per Åhgren | 0ade983 | 2020-09-01 23:57:20 +0200 | [diff] [blame] | 41 | MOCK_METHOD(void, InitializeLocked, (), (override)); |
| 42 | void RealInitializeLocked() RTC_NO_THREAD_SAFETY_ANALYSIS { |
| 43 | AudioProcessingImpl::InitializeLocked(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 44 | } |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 45 | |
Danil Chapovalov | 704fb55 | 2020-05-18 15:10:15 +0200 | [diff] [blame] | 46 | MOCK_METHOD(void, AddRef, (), (const, override)); |
| 47 | MOCK_METHOD(rtc::RefCountReleaseStatus, Release, (), (const, override)); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 50 | // Creates MockEchoControl instances and provides a raw pointer access to |
| 51 | // the next created one. The raw pointer is meant to be used with gmock. |
| 52 | // Returning a pointer of the next created MockEchoControl instance is necessary |
| 53 | // for the following reasons: (i) gmock expectations must be set before any call |
| 54 | // occurs, (ii) APM is initialized the first time that |
| 55 | // AudioProcessingImpl::ProcessStream() is called and the initialization leads |
| 56 | // to the creation of a new EchoControl object. |
| 57 | class MockEchoControlFactory : public EchoControlFactory { |
| 58 | public: |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 59 | MockEchoControlFactory() : next_mock_(std::make_unique<MockEchoControl>()) {} |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 60 | // Returns a pointer to the next MockEchoControl that this factory creates. |
| 61 | MockEchoControl* GetNext() const { return next_mock_.get(); } |
Per Åhgren | 4e5c709 | 2019-11-01 20:44:11 +0100 | [diff] [blame] | 62 | std::unique_ptr<EchoControl> Create(int sample_rate_hz, |
| 63 | int num_render_channels, |
| 64 | int num_capture_channels) override { |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 65 | std::unique_ptr<EchoControl> mock = std::move(next_mock_); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 66 | next_mock_ = std::make_unique<MockEchoControl>(); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 67 | return mock; |
| 68 | } |
| 69 | |
| 70 | private: |
| 71 | std::unique_ptr<MockEchoControl> next_mock_; |
| 72 | }; |
| 73 | |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 74 | // Mocks EchoDetector and records the first samples of the last analyzed render |
| 75 | // stream frame. Used to check what data is read by an EchoDetector |
| 76 | // implementation injected into an APM. |
| 77 | class TestEchoDetector : public EchoDetector { |
| 78 | public: |
| 79 | TestEchoDetector() |
| 80 | : analyze_render_audio_called_(false), |
| 81 | last_render_audio_first_sample_(0.f) {} |
| 82 | ~TestEchoDetector() override = default; |
| 83 | void AnalyzeRenderAudio(rtc::ArrayView<const float> render_audio) override { |
| 84 | last_render_audio_first_sample_ = render_audio[0]; |
| 85 | analyze_render_audio_called_ = true; |
| 86 | } |
| 87 | void AnalyzeCaptureAudio(rtc::ArrayView<const float> capture_audio) override { |
| 88 | } |
| 89 | void Initialize(int capture_sample_rate_hz, |
| 90 | int num_capture_channels, |
| 91 | int render_sample_rate_hz, |
| 92 | int num_render_channels) override {} |
| 93 | EchoDetector::Metrics GetMetrics() const override { return {}; } |
| 94 | // Returns true if AnalyzeRenderAudio() has been called at least once. |
| 95 | bool analyze_render_audio_called() const { |
| 96 | return analyze_render_audio_called_; |
| 97 | } |
| 98 | // Returns the first sample of the last analyzed render frame. |
| 99 | float last_render_audio_first_sample() const { |
| 100 | return last_render_audio_first_sample_; |
| 101 | } |
| 102 | |
| 103 | private: |
| 104 | bool analyze_render_audio_called_; |
| 105 | float last_render_audio_first_sample_; |
| 106 | }; |
| 107 | |
| 108 | // Mocks CustomProcessing and applies ProcessSample() to all the samples. |
| 109 | // Meant to be injected into an APM to modify samples in a known and detectable |
| 110 | // way. |
| 111 | class TestRenderPreProcessor : public CustomProcessing { |
| 112 | public: |
| 113 | TestRenderPreProcessor() = default; |
| 114 | ~TestRenderPreProcessor() = default; |
| 115 | void Initialize(int sample_rate_hz, int num_channels) override {} |
| 116 | void Process(AudioBuffer* audio) override { |
| 117 | for (size_t k = 0; k < audio->num_channels(); ++k) { |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 118 | rtc::ArrayView<float> channel_view(audio->channels()[k], |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 119 | audio->num_frames()); |
| 120 | std::transform(channel_view.begin(), channel_view.end(), |
| 121 | channel_view.begin(), ProcessSample); |
| 122 | } |
Mirko Bonadei | c4dd730 | 2019-02-25 09:12:02 +0100 | [diff] [blame] | 123 | } |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 124 | std::string ToString() const override { return "TestRenderPreProcessor"; } |
| 125 | void SetRuntimeSetting(AudioProcessing::RuntimeSetting setting) override {} |
| 126 | // Modifies a sample. This member is used in Process() to modify a frame and |
| 127 | // it is publicly visible to enable tests. |
| 128 | static constexpr float ProcessSample(float x) { return 2.f * x; } |
| 129 | }; |
| 130 | |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 131 | } // namespace |
| 132 | |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 133 | TEST(AudioProcessingImplTest, AudioParameterChangeTriggersInit) { |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 134 | webrtc::Config webrtc_config; |
| 135 | MockInitialize mock(webrtc_config); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 136 | ON_CALL(mock, InitializeLocked()) |
| 137 | .WillByDefault(Invoke(&mock, &MockInitialize::RealInitializeLocked)); |
| 138 | |
| 139 | EXPECT_CALL(mock, InitializeLocked()).Times(1); |
| 140 | mock.Initialize(); |
| 141 | |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 142 | constexpr size_t kMaxSampleRateHz = 32000; |
| 143 | constexpr size_t kMaxNumChannels = 2; |
| 144 | std::array<int16_t, kMaxNumChannels * kMaxSampleRateHz / 100> frame; |
| 145 | frame.fill(0); |
| 146 | StreamConfig config(16000, 1, /*has_keyboard=*/false); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 147 | // Call with the default parameters; there should be an init. |
Per Åhgren | 4bdced5 | 2017-06-27 16:00:38 +0200 | [diff] [blame] | 148 | EXPECT_CALL(mock, InitializeLocked()).Times(0); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 149 | EXPECT_NOERR(mock.ProcessStream(frame.data(), config, config, frame.data())); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 150 | EXPECT_NOERR( |
| 151 | mock.ProcessReverseStream(frame.data(), config, config, frame.data())); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 152 | |
| 153 | // New sample rate. (Only impacts ProcessStream). |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 154 | config = StreamConfig(32000, 1, /*has_keyboard=*/false); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 155 | EXPECT_CALL(mock, InitializeLocked()).Times(1); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 156 | EXPECT_NOERR(mock.ProcessStream(frame.data(), config, config, frame.data())); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 157 | |
| 158 | // New number of channels. |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 159 | // TODO(peah): Investigate why this causes 2 inits. |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 160 | config = StreamConfig(32000, 2, /*has_keyboard=*/false); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 161 | EXPECT_CALL(mock, InitializeLocked()).Times(2); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 162 | EXPECT_NOERR(mock.ProcessStream(frame.data(), config, config, frame.data())); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 163 | // ProcessStream sets num_channels_ == num_output_channels. |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 164 | EXPECT_NOERR( |
| 165 | mock.ProcessReverseStream(frame.data(), config, config, frame.data())); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 166 | |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 167 | // A new sample rate passed to ProcessReverseStream should cause an init. |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 168 | config = StreamConfig(16000, 2, /*has_keyboard=*/false); |
Alex Luebs | 5b830fe | 2016-03-08 17:52:52 +0100 | [diff] [blame] | 169 | EXPECT_CALL(mock, InitializeLocked()).Times(1); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 170 | EXPECT_NOERR( |
| 171 | mock.ProcessReverseStream(frame.data(), config, config, frame.data())); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 174 | TEST(AudioProcessingImplTest, UpdateCapturePreGainRuntimeSetting) { |
Per Åhgren | cc73ed3 | 2020-04-26 23:56:17 +0200 | [diff] [blame] | 175 | std::unique_ptr<AudioProcessing> apm( |
| 176 | AudioProcessingBuilderForTesting().Create()); |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 177 | webrtc::AudioProcessing::Config apm_config; |
| 178 | apm_config.pre_amplifier.enabled = true; |
| 179 | apm_config.pre_amplifier.fixed_gain_factor = 1.f; |
| 180 | apm->ApplyConfig(apm_config); |
| 181 | |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 182 | constexpr int kSampleRateHz = 48000; |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 183 | constexpr int16_t kAudioLevel = 10000; |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 184 | constexpr size_t kNumChannels = 2; |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 185 | |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 186 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
| 187 | StreamConfig config(kSampleRateHz, kNumChannels, /*has_keyboard=*/false); |
| 188 | frame.fill(kAudioLevel); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 189 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 190 | EXPECT_EQ(frame[100], kAudioLevel) |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 191 | << "With factor 1, frame shouldn't be modified."; |
| 192 | |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 193 | constexpr float kGainFactor = 2.f; |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 194 | apm->SetRuntimeSetting( |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 195 | AudioProcessing::RuntimeSetting::CreateCapturePreGain(kGainFactor)); |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 196 | |
| 197 | // Process for two frames to have time to ramp up gain. |
| 198 | for (int i = 0; i < 2; ++i) { |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 199 | frame.fill(kAudioLevel); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 200 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 201 | } |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 202 | EXPECT_EQ(frame[100], kGainFactor * kAudioLevel) |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 203 | << "Frame should be amplified."; |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 204 | } |
| 205 | |
Per Åhgren | db5d728 | 2021-03-15 16:31:04 +0000 | [diff] [blame] | 206 | TEST(AudioProcessingImplTest, |
| 207 | LevelAdjustmentUpdateCapturePreGainRuntimeSetting) { |
| 208 | std::unique_ptr<AudioProcessing> apm( |
| 209 | AudioProcessingBuilderForTesting().Create()); |
| 210 | webrtc::AudioProcessing::Config apm_config; |
| 211 | apm_config.capture_level_adjustment.enabled = true; |
| 212 | apm_config.capture_level_adjustment.pre_gain_factor = 1.f; |
| 213 | apm->ApplyConfig(apm_config); |
| 214 | |
| 215 | constexpr int kSampleRateHz = 48000; |
| 216 | constexpr int16_t kAudioLevel = 10000; |
| 217 | constexpr size_t kNumChannels = 2; |
| 218 | |
| 219 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
| 220 | StreamConfig config(kSampleRateHz, kNumChannels, /*has_keyboard=*/false); |
| 221 | frame.fill(kAudioLevel); |
| 222 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
| 223 | EXPECT_EQ(frame[100], kAudioLevel) |
| 224 | << "With factor 1, frame shouldn't be modified."; |
| 225 | |
| 226 | constexpr float kGainFactor = 2.f; |
| 227 | apm->SetRuntimeSetting( |
| 228 | AudioProcessing::RuntimeSetting::CreateCapturePreGain(kGainFactor)); |
| 229 | |
| 230 | // Process for two frames to have time to ramp up gain. |
| 231 | for (int i = 0; i < 2; ++i) { |
| 232 | frame.fill(kAudioLevel); |
| 233 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
| 234 | } |
| 235 | EXPECT_EQ(frame[100], kGainFactor * kAudioLevel) |
| 236 | << "Frame should be amplified."; |
| 237 | } |
| 238 | |
| 239 | TEST(AudioProcessingImplTest, |
| 240 | LevelAdjustmentUpdateCapturePostGainRuntimeSetting) { |
| 241 | std::unique_ptr<AudioProcessing> apm( |
| 242 | AudioProcessingBuilderForTesting().Create()); |
| 243 | webrtc::AudioProcessing::Config apm_config; |
| 244 | apm_config.capture_level_adjustment.enabled = true; |
| 245 | apm_config.capture_level_adjustment.post_gain_factor = 1.f; |
| 246 | apm->ApplyConfig(apm_config); |
| 247 | |
| 248 | constexpr int kSampleRateHz = 48000; |
| 249 | constexpr int16_t kAudioLevel = 10000; |
| 250 | constexpr size_t kNumChannels = 2; |
| 251 | |
| 252 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
| 253 | StreamConfig config(kSampleRateHz, kNumChannels, /*has_keyboard=*/false); |
| 254 | frame.fill(kAudioLevel); |
| 255 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
| 256 | EXPECT_EQ(frame[100], kAudioLevel) |
| 257 | << "With factor 1, frame shouldn't be modified."; |
| 258 | |
| 259 | constexpr float kGainFactor = 2.f; |
| 260 | apm->SetRuntimeSetting( |
| 261 | AudioProcessing::RuntimeSetting::CreateCapturePostGain(kGainFactor)); |
| 262 | |
| 263 | // Process for two frames to have time to ramp up gain. |
| 264 | for (int i = 0; i < 2; ++i) { |
| 265 | frame.fill(kAudioLevel); |
| 266 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
| 267 | } |
| 268 | EXPECT_EQ(frame[100], kGainFactor * kAudioLevel) |
| 269 | << "Frame should be amplified."; |
| 270 | } |
| 271 | |
Per Åhgren | 652ada5 | 2021-03-03 10:52:44 +0000 | [diff] [blame] | 272 | TEST(AudioProcessingImplTest, EchoControllerObservesSetCaptureUsageChange) { |
| 273 | // Tests that the echo controller observes that the capture usage has been |
| 274 | // updated. |
| 275 | auto echo_control_factory = std::make_unique<MockEchoControlFactory>(); |
| 276 | const MockEchoControlFactory* echo_control_factory_ptr = |
| 277 | echo_control_factory.get(); |
| 278 | |
| 279 | std::unique_ptr<AudioProcessing> apm( |
| 280 | AudioProcessingBuilderForTesting() |
| 281 | .SetEchoControlFactory(std::move(echo_control_factory)) |
| 282 | .Create()); |
| 283 | |
| 284 | constexpr int16_t kAudioLevel = 10000; |
| 285 | constexpr int kSampleRateHz = 48000; |
| 286 | constexpr int kNumChannels = 2; |
| 287 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
| 288 | StreamConfig config(kSampleRateHz, kNumChannels, /*has_keyboard=*/false); |
| 289 | frame.fill(kAudioLevel); |
| 290 | |
| 291 | MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext(); |
| 292 | |
| 293 | // Ensure that SetCaptureOutputUsage is not called when no runtime settings |
| 294 | // are passed. |
| 295 | EXPECT_CALL(*echo_control_mock, SetCaptureOutputUsage(testing::_)).Times(0); |
| 296 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
| 297 | |
| 298 | // Ensure that SetCaptureOutputUsage is called with the right information when |
| 299 | // a runtime setting is passed. |
| 300 | EXPECT_CALL(*echo_control_mock, |
| 301 | SetCaptureOutputUsage(/*capture_output_used=*/false)) |
| 302 | .Times(1); |
| 303 | EXPECT_TRUE(apm->PostRuntimeSetting( |
| 304 | AudioProcessing::RuntimeSetting::CreateCaptureOutputUsedSetting( |
| 305 | /*capture_output_used=*/false))); |
| 306 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
| 307 | |
| 308 | EXPECT_CALL(*echo_control_mock, |
| 309 | SetCaptureOutputUsage(/*capture_output_used=*/true)) |
| 310 | .Times(1); |
| 311 | EXPECT_TRUE(apm->PostRuntimeSetting( |
| 312 | AudioProcessing::RuntimeSetting::CreateCaptureOutputUsedSetting( |
| 313 | /*capture_output_used=*/true))); |
| 314 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
| 315 | |
| 316 | // The number of positions to place items in the queue is equal to the queue |
| 317 | // size minus 1. |
| 318 | constexpr int kNumSlotsInQueue = RuntimeSettingQueueSize(); |
| 319 | |
| 320 | // Ensure that SetCaptureOutputUsage is called with the right information when |
| 321 | // many runtime settings are passed. |
| 322 | for (int k = 0; k < kNumSlotsInQueue - 1; ++k) { |
| 323 | EXPECT_TRUE(apm->PostRuntimeSetting( |
| 324 | AudioProcessing::RuntimeSetting::CreateCaptureOutputUsedSetting( |
| 325 | /*capture_output_used=*/false))); |
| 326 | } |
| 327 | EXPECT_CALL(*echo_control_mock, |
| 328 | SetCaptureOutputUsage(/*capture_output_used=*/false)) |
| 329 | .Times(kNumSlotsInQueue - 1); |
| 330 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
| 331 | |
| 332 | // Ensure that SetCaptureOutputUsage is properly called with the fallback |
| 333 | // value when the runtime settings queue becomes full. |
| 334 | for (int k = 0; k < kNumSlotsInQueue; ++k) { |
| 335 | EXPECT_TRUE(apm->PostRuntimeSetting( |
| 336 | AudioProcessing::RuntimeSetting::CreateCaptureOutputUsedSetting( |
| 337 | /*capture_output_used=*/false))); |
| 338 | } |
| 339 | EXPECT_FALSE(apm->PostRuntimeSetting( |
| 340 | AudioProcessing::RuntimeSetting::CreateCaptureOutputUsedSetting( |
| 341 | /*capture_output_used=*/false))); |
| 342 | EXPECT_FALSE(apm->PostRuntimeSetting( |
| 343 | AudioProcessing::RuntimeSetting::CreateCaptureOutputUsedSetting( |
| 344 | /*capture_output_used=*/false))); |
| 345 | EXPECT_CALL(*echo_control_mock, |
| 346 | SetCaptureOutputUsage(/*capture_output_used=*/false)) |
| 347 | .Times(kNumSlotsInQueue); |
| 348 | EXPECT_CALL(*echo_control_mock, |
| 349 | SetCaptureOutputUsage(/*capture_output_used=*/true)) |
| 350 | .Times(1); |
| 351 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
| 352 | } |
| 353 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 354 | TEST(AudioProcessingImplTest, |
| 355 | EchoControllerObservesPreAmplifierEchoPathGainChange) { |
| 356 | // Tests that the echo controller observes an echo path gain change when the |
| 357 | // pre-amplifier submodule changes the gain. |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 358 | auto echo_control_factory = std::make_unique<MockEchoControlFactory>(); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 359 | const auto* echo_control_factory_ptr = echo_control_factory.get(); |
| 360 | |
| 361 | std::unique_ptr<AudioProcessing> apm( |
Per Åhgren | cc73ed3 | 2020-04-26 23:56:17 +0200 | [diff] [blame] | 362 | AudioProcessingBuilderForTesting() |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 363 | .SetEchoControlFactory(std::move(echo_control_factory)) |
| 364 | .Create()); |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 365 | // Disable AGC. |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 366 | webrtc::AudioProcessing::Config apm_config; |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 367 | apm_config.gain_controller1.enabled = false; |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 368 | apm_config.gain_controller2.enabled = false; |
| 369 | apm_config.pre_amplifier.enabled = true; |
| 370 | apm_config.pre_amplifier.fixed_gain_factor = 1.f; |
| 371 | apm->ApplyConfig(apm_config); |
| 372 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 373 | constexpr int16_t kAudioLevel = 10000; |
| 374 | constexpr size_t kSampleRateHz = 48000; |
| 375 | constexpr size_t kNumChannels = 2; |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 376 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
| 377 | StreamConfig config(kSampleRateHz, kNumChannels, /*has_keyboard=*/false); |
| 378 | frame.fill(kAudioLevel); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 379 | |
| 380 | MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext(); |
| 381 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 382 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | bf47f34 | 2019-05-09 10:50:31 +0200 | [diff] [blame] | 383 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 384 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/false)) |
Fredrik Hernqvist | bf47f34 | 2019-05-09 10:50:31 +0200 | [diff] [blame] | 385 | .Times(1); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 386 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 387 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 388 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | bf47f34 | 2019-05-09 10:50:31 +0200 | [diff] [blame] | 389 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 390 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/true)) |
Fredrik Hernqvist | bf47f34 | 2019-05-09 10:50:31 +0200 | [diff] [blame] | 391 | .Times(1); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 392 | apm->SetRuntimeSetting( |
| 393 | AudioProcessing::RuntimeSetting::CreateCapturePreGain(2.f)); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 394 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | TEST(AudioProcessingImplTest, |
Per Åhgren | db5d728 | 2021-03-15 16:31:04 +0000 | [diff] [blame] | 398 | EchoControllerObservesLevelAdjustmentPreGainEchoPathGainChange) { |
| 399 | // Tests that the echo controller observes an echo path gain change when the |
| 400 | // pre-amplifier submodule changes the gain. |
| 401 | auto echo_control_factory = std::make_unique<MockEchoControlFactory>(); |
| 402 | const auto* echo_control_factory_ptr = echo_control_factory.get(); |
| 403 | |
| 404 | std::unique_ptr<AudioProcessing> apm( |
| 405 | AudioProcessingBuilderForTesting() |
| 406 | .SetEchoControlFactory(std::move(echo_control_factory)) |
| 407 | .Create()); |
| 408 | // Disable AGC. |
| 409 | webrtc::AudioProcessing::Config apm_config; |
| 410 | apm_config.gain_controller1.enabled = false; |
| 411 | apm_config.gain_controller2.enabled = false; |
| 412 | apm_config.capture_level_adjustment.enabled = true; |
| 413 | apm_config.capture_level_adjustment.pre_gain_factor = 1.f; |
| 414 | apm->ApplyConfig(apm_config); |
| 415 | |
| 416 | constexpr int16_t kAudioLevel = 10000; |
| 417 | constexpr size_t kSampleRateHz = 48000; |
| 418 | constexpr size_t kNumChannels = 2; |
| 419 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
| 420 | StreamConfig config(kSampleRateHz, kNumChannels, /*has_keyboard=*/false); |
| 421 | frame.fill(kAudioLevel); |
| 422 | |
| 423 | MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext(); |
| 424 | |
| 425 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
| 426 | EXPECT_CALL(*echo_control_mock, |
| 427 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/false)) |
| 428 | .Times(1); |
| 429 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
| 430 | |
| 431 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
| 432 | EXPECT_CALL(*echo_control_mock, |
| 433 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/true)) |
| 434 | .Times(1); |
| 435 | apm->SetRuntimeSetting( |
| 436 | AudioProcessing::RuntimeSetting::CreateCapturePreGain(2.f)); |
| 437 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
| 438 | } |
| 439 | |
| 440 | TEST(AudioProcessingImplTest, |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 441 | EchoControllerObservesAnalogAgc1EchoPathGainChange) { |
| 442 | // Tests that the echo controller observes an echo path gain change when the |
| 443 | // AGC1 analog adaptive submodule changes the analog gain. |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 444 | auto echo_control_factory = std::make_unique<MockEchoControlFactory>(); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 445 | const auto* echo_control_factory_ptr = echo_control_factory.get(); |
| 446 | |
| 447 | std::unique_ptr<AudioProcessing> apm( |
Per Åhgren | cc73ed3 | 2020-04-26 23:56:17 +0200 | [diff] [blame] | 448 | AudioProcessingBuilderForTesting() |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 449 | .SetEchoControlFactory(std::move(echo_control_factory)) |
| 450 | .Create()); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 451 | webrtc::AudioProcessing::Config apm_config; |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 452 | // Enable AGC1. |
| 453 | apm_config.gain_controller1.enabled = true; |
| 454 | apm_config.gain_controller1.mode = |
| 455 | AudioProcessing::Config::GainController1::kAdaptiveAnalog; |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 456 | apm_config.gain_controller2.enabled = false; |
| 457 | apm_config.pre_amplifier.enabled = false; |
| 458 | apm->ApplyConfig(apm_config); |
| 459 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 460 | constexpr int16_t kAudioLevel = 1000; |
| 461 | constexpr size_t kSampleRateHz = 48000; |
| 462 | constexpr size_t kNumChannels = 2; |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 463 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
| 464 | StreamConfig stream_config(kSampleRateHz, kNumChannels, |
| 465 | /*has_keyboard=*/false); |
| 466 | frame.fill(kAudioLevel); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 467 | |
| 468 | MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext(); |
| 469 | |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 470 | const int initial_analog_gain = apm->recommended_stream_analog_level(); |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 471 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 472 | EXPECT_CALL(*echo_control_mock, ProcessCapture(NotNull(), testing::_, false)) |
| 473 | .Times(1); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 474 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data()); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 475 | |
| 476 | // Force an analog gain change if it did not happen. |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 477 | if (initial_analog_gain == apm->recommended_stream_analog_level()) { |
| 478 | apm->set_stream_analog_level(initial_analog_gain + 1); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 479 | } |
| 480 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 481 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 482 | EXPECT_CALL(*echo_control_mock, ProcessCapture(NotNull(), testing::_, true)) |
| 483 | .Times(1); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 484 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data()); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 485 | } |
| 486 | |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 487 | TEST(AudioProcessingImplTest, EchoControllerObservesPlayoutVolumeChange) { |
| 488 | // Tests that the echo controller observes an echo path gain change when a |
| 489 | // playout volume change is reported. |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 490 | auto echo_control_factory = std::make_unique<MockEchoControlFactory>(); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 491 | const auto* echo_control_factory_ptr = echo_control_factory.get(); |
| 492 | |
| 493 | std::unique_ptr<AudioProcessing> apm( |
Per Åhgren | cc73ed3 | 2020-04-26 23:56:17 +0200 | [diff] [blame] | 494 | AudioProcessingBuilderForTesting() |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 495 | .SetEchoControlFactory(std::move(echo_control_factory)) |
| 496 | .Create()); |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 497 | // Disable AGC. |
| 498 | webrtc::AudioProcessing::Config apm_config; |
| 499 | apm_config.gain_controller1.enabled = false; |
| 500 | apm_config.gain_controller2.enabled = false; |
| 501 | apm->ApplyConfig(apm_config); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 502 | |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 503 | constexpr int16_t kAudioLevel = 10000; |
| 504 | constexpr size_t kSampleRateHz = 48000; |
| 505 | constexpr size_t kNumChannels = 2; |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 506 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
| 507 | StreamConfig stream_config(kSampleRateHz, kNumChannels, |
| 508 | /*has_keyboard=*/false); |
| 509 | frame.fill(kAudioLevel); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 510 | |
| 511 | MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext(); |
| 512 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 513 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 514 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 515 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/false)) |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 516 | .Times(1); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 517 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data()); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 518 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 519 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 520 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 521 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/false)) |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 522 | .Times(1); |
| 523 | apm->SetRuntimeSetting( |
| 524 | AudioProcessing::RuntimeSetting::CreatePlayoutVolumeChange(50)); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 525 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data()); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 526 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 527 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 528 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 529 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/false)) |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 530 | .Times(1); |
| 531 | apm->SetRuntimeSetting( |
| 532 | AudioProcessing::RuntimeSetting::CreatePlayoutVolumeChange(50)); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 533 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data()); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 534 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 535 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 536 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 537 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/true)) |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 538 | .Times(1); |
| 539 | apm->SetRuntimeSetting( |
| 540 | AudioProcessing::RuntimeSetting::CreatePlayoutVolumeChange(100)); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 541 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data()); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 542 | } |
| 543 | |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 544 | TEST(AudioProcessingImplTest, RenderPreProcessorBeforeEchoDetector) { |
| 545 | // Make sure that signal changes caused by a render pre-processing sub-module |
| 546 | // take place before any echo detector analysis. |
Tommi | 87f7090 | 2021-04-27 14:43:08 +0200 | [diff] [blame] | 547 | auto test_echo_detector = rtc::make_ref_counted<TestEchoDetector>(); |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 548 | std::unique_ptr<CustomProcessing> test_render_pre_processor( |
| 549 | new TestRenderPreProcessor()); |
| 550 | // Create APM injecting the test echo detector and render pre-processor. |
| 551 | std::unique_ptr<AudioProcessing> apm( |
Per Åhgren | cc73ed3 | 2020-04-26 23:56:17 +0200 | [diff] [blame] | 552 | AudioProcessingBuilderForTesting() |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 553 | .SetEchoDetector(test_echo_detector) |
| 554 | .SetRenderPreProcessing(std::move(test_render_pre_processor)) |
| 555 | .Create()); |
| 556 | webrtc::AudioProcessing::Config apm_config; |
| 557 | apm_config.pre_amplifier.enabled = true; |
| 558 | apm_config.residual_echo_detector.enabled = true; |
| 559 | apm->ApplyConfig(apm_config); |
| 560 | |
| 561 | constexpr int16_t kAudioLevel = 1000; |
| 562 | constexpr int kSampleRateHz = 16000; |
| 563 | constexpr size_t kNumChannels = 1; |
Sam Zackrisson | 12e319a | 2020-01-03 14:54:20 +0100 | [diff] [blame] | 564 | // Explicitly initialize APM to ensure no render frames are discarded. |
| 565 | const ProcessingConfig processing_config = {{ |
| 566 | {kSampleRateHz, kNumChannels, /*has_keyboard=*/false}, |
| 567 | {kSampleRateHz, kNumChannels, /*has_keyboard=*/false}, |
| 568 | {kSampleRateHz, kNumChannels, /*has_keyboard=*/false}, |
| 569 | {kSampleRateHz, kNumChannels, /*has_keyboard=*/false}, |
| 570 | }}; |
| 571 | apm->Initialize(processing_config); |
| 572 | |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 573 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
| 574 | StreamConfig stream_config(kSampleRateHz, kNumChannels, |
| 575 | /*has_keyboard=*/false); |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 576 | |
| 577 | constexpr float kAudioLevelFloat = static_cast<float>(kAudioLevel); |
| 578 | constexpr float kExpectedPreprocessedAudioLevel = |
| 579 | TestRenderPreProcessor::ProcessSample(kAudioLevelFloat); |
| 580 | ASSERT_NE(kAudioLevelFloat, kExpectedPreprocessedAudioLevel); |
| 581 | |
| 582 | // Analyze a render stream frame. |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 583 | frame.fill(kAudioLevel); |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 584 | ASSERT_EQ(AudioProcessing::Error::kNoError, |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 585 | apm->ProcessReverseStream(frame.data(), stream_config, |
| 586 | stream_config, frame.data())); |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 587 | // Trigger a call to in EchoDetector::AnalyzeRenderAudio() via |
| 588 | // ProcessStream(). |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 589 | frame.fill(kAudioLevel); |
| 590 | ASSERT_EQ(AudioProcessing::Error::kNoError, |
| 591 | apm->ProcessStream(frame.data(), stream_config, stream_config, |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 592 | frame.data())); |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 593 | // Regardless of how the call to in EchoDetector::AnalyzeRenderAudio() is |
| 594 | // triggered, the line below checks that the call has occurred. If not, the |
| 595 | // APM implementation may have changed and this test might need to be adapted. |
| 596 | ASSERT_TRUE(test_echo_detector->analyze_render_audio_called()); |
| 597 | // Check that the data read in EchoDetector::AnalyzeRenderAudio() is that |
| 598 | // produced by the render pre-processor. |
| 599 | EXPECT_EQ(kExpectedPreprocessedAudioLevel, |
| 600 | test_echo_detector->last_render_audio_first_sample()); |
| 601 | } |
| 602 | |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 603 | // Disabling build-optional submodules and trying to enable them via the APM |
| 604 | // config should be bit-exact with running APM with said submodules disabled. |
| 605 | // This mainly tests that SetCreateOptionalSubmodulesForTesting has an effect. |
| 606 | TEST(ApmWithSubmodulesExcludedTest, BitexactWithDisabledModules) { |
Tommi | 87f7090 | 2021-04-27 14:43:08 +0200 | [diff] [blame] | 607 | auto apm = rtc::make_ref_counted<AudioProcessingImpl>(webrtc::Config()); |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 608 | ASSERT_EQ(apm->Initialize(), AudioProcessing::kNoError); |
| 609 | |
| 610 | ApmSubmoduleCreationOverrides overrides; |
| 611 | overrides.transient_suppression = true; |
| 612 | apm->OverrideSubmoduleCreationForTesting(overrides); |
| 613 | |
| 614 | AudioProcessing::Config apm_config = apm->GetConfig(); |
| 615 | apm_config.transient_suppression.enabled = true; |
| 616 | apm->ApplyConfig(apm_config); |
| 617 | |
| 618 | rtc::scoped_refptr<AudioProcessing> apm_reference = |
| 619 | AudioProcessingBuilder().Create(); |
| 620 | apm_config = apm_reference->GetConfig(); |
| 621 | apm_config.transient_suppression.enabled = false; |
| 622 | apm_reference->ApplyConfig(apm_config); |
| 623 | |
| 624 | constexpr int kSampleRateHz = 16000; |
| 625 | constexpr int kNumChannels = 1; |
| 626 | std::array<float, kSampleRateHz / 100> buffer; |
| 627 | std::array<float, kSampleRateHz / 100> buffer_reference; |
| 628 | float* channel_pointers[] = {buffer.data()}; |
| 629 | float* channel_pointers_reference[] = {buffer_reference.data()}; |
| 630 | StreamConfig stream_config(/*sample_rate_hz=*/kSampleRateHz, |
| 631 | /*num_channels=*/kNumChannels, |
| 632 | /*has_keyboard=*/false); |
| 633 | Random random_generator(2341U); |
| 634 | constexpr int kFramesToProcessPerConfiguration = 10; |
| 635 | |
| 636 | for (int i = 0; i < kFramesToProcessPerConfiguration; ++i) { |
| 637 | RandomizeSampleVector(&random_generator, buffer); |
| 638 | std::copy(buffer.begin(), buffer.end(), buffer_reference.begin()); |
| 639 | ASSERT_EQ(apm->ProcessStream(channel_pointers, stream_config, stream_config, |
| 640 | channel_pointers), |
| 641 | kNoErr); |
| 642 | ASSERT_EQ( |
| 643 | apm_reference->ProcessStream(channel_pointers_reference, stream_config, |
| 644 | stream_config, channel_pointers_reference), |
| 645 | kNoErr); |
| 646 | for (int j = 0; j < kSampleRateHz / 100; ++j) { |
| 647 | EXPECT_EQ(buffer[j], buffer_reference[j]); |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | // Disable transient suppressor creation and run APM in ways that should trigger |
| 653 | // calls to the transient suppressor API. |
| 654 | TEST(ApmWithSubmodulesExcludedTest, ReinitializeTransientSuppressor) { |
Tommi | 87f7090 | 2021-04-27 14:43:08 +0200 | [diff] [blame] | 655 | auto apm = rtc::make_ref_counted<AudioProcessingImpl>(webrtc::Config()); |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 656 | ASSERT_EQ(apm->Initialize(), kNoErr); |
| 657 | |
| 658 | ApmSubmoduleCreationOverrides overrides; |
| 659 | overrides.transient_suppression = true; |
| 660 | apm->OverrideSubmoduleCreationForTesting(overrides); |
| 661 | |
| 662 | AudioProcessing::Config config = apm->GetConfig(); |
| 663 | config.transient_suppression.enabled = true; |
| 664 | apm->ApplyConfig(config); |
| 665 | // 960 samples per frame: 10 ms of <= 48 kHz audio with <= 2 channels. |
| 666 | float buffer[960]; |
| 667 | float* channel_pointers[] = {&buffer[0], &buffer[480]}; |
| 668 | Random random_generator(2341U); |
| 669 | constexpr int kFramesToProcessPerConfiguration = 3; |
| 670 | |
| 671 | StreamConfig initial_stream_config(/*sample_rate_hz=*/16000, |
| 672 | /*num_channels=*/1, |
| 673 | /*has_keyboard=*/false); |
| 674 | for (int i = 0; i < kFramesToProcessPerConfiguration; ++i) { |
| 675 | RandomizeSampleVector(&random_generator, buffer); |
| 676 | EXPECT_EQ(apm->ProcessStream(channel_pointers, initial_stream_config, |
| 677 | initial_stream_config, channel_pointers), |
| 678 | kNoErr); |
| 679 | } |
| 680 | |
| 681 | StreamConfig stereo_stream_config(/*sample_rate_hz=*/16000, |
| 682 | /*num_channels=*/2, |
| 683 | /*has_keyboard=*/false); |
| 684 | for (int i = 0; i < kFramesToProcessPerConfiguration; ++i) { |
| 685 | RandomizeSampleVector(&random_generator, buffer); |
| 686 | EXPECT_EQ(apm->ProcessStream(channel_pointers, stereo_stream_config, |
| 687 | stereo_stream_config, channel_pointers), |
| 688 | kNoErr); |
| 689 | } |
| 690 | |
| 691 | StreamConfig high_sample_rate_stream_config(/*sample_rate_hz=*/48000, |
| 692 | /*num_channels=*/1, |
| 693 | /*has_keyboard=*/false); |
| 694 | for (int i = 0; i < kFramesToProcessPerConfiguration; ++i) { |
| 695 | RandomizeSampleVector(&random_generator, buffer); |
| 696 | EXPECT_EQ( |
| 697 | apm->ProcessStream(channel_pointers, high_sample_rate_stream_config, |
| 698 | high_sample_rate_stream_config, channel_pointers), |
| 699 | kNoErr); |
| 700 | } |
| 701 | |
| 702 | StreamConfig keyboard_stream_config(/*sample_rate_hz=*/16000, |
| 703 | /*num_channels=*/1, |
| 704 | /*has_keyboard=*/true); |
| 705 | for (int i = 0; i < kFramesToProcessPerConfiguration; ++i) { |
| 706 | RandomizeSampleVector(&random_generator, buffer); |
| 707 | EXPECT_EQ(apm->ProcessStream(channel_pointers, keyboard_stream_config, |
| 708 | keyboard_stream_config, channel_pointers), |
| 709 | kNoErr); |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | // Disable transient suppressor creation and run APM in ways that should trigger |
| 714 | // calls to the transient suppressor API. |
| 715 | TEST(ApmWithSubmodulesExcludedTest, ToggleTransientSuppressor) { |
Tommi | 87f7090 | 2021-04-27 14:43:08 +0200 | [diff] [blame] | 716 | auto apm = rtc::make_ref_counted<AudioProcessingImpl>(webrtc::Config()); |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 717 | ASSERT_EQ(apm->Initialize(), AudioProcessing::kNoError); |
| 718 | |
| 719 | ApmSubmoduleCreationOverrides overrides; |
| 720 | overrides.transient_suppression = true; |
| 721 | apm->OverrideSubmoduleCreationForTesting(overrides); |
| 722 | |
| 723 | // 960 samples per frame: 10 ms of <= 48 kHz audio with <= 2 channels. |
| 724 | float buffer[960]; |
| 725 | float* channel_pointers[] = {&buffer[0], &buffer[480]}; |
| 726 | Random random_generator(2341U); |
| 727 | constexpr int kFramesToProcessPerConfiguration = 3; |
| 728 | StreamConfig stream_config(/*sample_rate_hz=*/16000, |
| 729 | /*num_channels=*/1, |
| 730 | /*has_keyboard=*/false); |
| 731 | |
| 732 | AudioProcessing::Config config = apm->GetConfig(); |
| 733 | config.transient_suppression.enabled = true; |
| 734 | apm->ApplyConfig(config); |
| 735 | for (int i = 0; i < kFramesToProcessPerConfiguration; ++i) { |
| 736 | RandomizeSampleVector(&random_generator, buffer); |
| 737 | EXPECT_EQ(apm->ProcessStream(channel_pointers, stream_config, stream_config, |
| 738 | channel_pointers), |
| 739 | kNoErr); |
| 740 | } |
| 741 | |
| 742 | config = apm->GetConfig(); |
| 743 | config.transient_suppression.enabled = false; |
| 744 | apm->ApplyConfig(config); |
| 745 | for (int i = 0; i < kFramesToProcessPerConfiguration; ++i) { |
| 746 | RandomizeSampleVector(&random_generator, buffer); |
| 747 | EXPECT_EQ(apm->ProcessStream(channel_pointers, stream_config, stream_config, |
| 748 | channel_pointers), |
| 749 | kNoErr); |
| 750 | } |
| 751 | |
| 752 | config = apm->GetConfig(); |
| 753 | config.transient_suppression.enabled = true; |
| 754 | apm->ApplyConfig(config); |
| 755 | for (int i = 0; i < kFramesToProcessPerConfiguration; ++i) { |
| 756 | RandomizeSampleVector(&random_generator, buffer); |
| 757 | EXPECT_EQ(apm->ProcessStream(channel_pointers, stream_config, stream_config, |
| 758 | channel_pointers), |
| 759 | kNoErr); |
| 760 | } |
| 761 | } |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 762 | } // namespace webrtc |