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" |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 17 | #include "modules/audio_processing/include/audio_processing.h" |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 18 | #include "modules/audio_processing/test/echo_control_mock.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "modules/audio_processing/test/test_utils.h" |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 20 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "rtc_base/ref_counted_object.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "test/gmock.h" |
| 23 | #include "test/gtest.h" |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 24 | |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 25 | namespace webrtc { |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 26 | namespace { |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 27 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 28 | using ::testing::Invoke; |
| 29 | using ::testing::NotNull; |
| 30 | |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 31 | class MockInitialize : public AudioProcessingImpl { |
| 32 | public: |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 33 | explicit MockInitialize(const webrtc::Config& config) |
| 34 | : AudioProcessingImpl(config) {} |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 35 | |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 36 | MOCK_METHOD0(InitializeLocked, int()); |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 37 | int RealInitializeLocked() RTC_NO_THREAD_SAFETY_ANALYSIS { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 38 | return AudioProcessingImpl::InitializeLocked(); |
| 39 | } |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 40 | |
Niels Möller | 6f72f56 | 2017-10-19 13:15:17 +0200 | [diff] [blame] | 41 | MOCK_CONST_METHOD0(AddRef, void()); |
| 42 | MOCK_CONST_METHOD0(Release, rtc::RefCountReleaseStatus()); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 45 | // Creates MockEchoControl instances and provides a raw pointer access to |
| 46 | // the next created one. The raw pointer is meant to be used with gmock. |
| 47 | // Returning a pointer of the next created MockEchoControl instance is necessary |
| 48 | // for the following reasons: (i) gmock expectations must be set before any call |
| 49 | // occurs, (ii) APM is initialized the first time that |
| 50 | // AudioProcessingImpl::ProcessStream() is called and the initialization leads |
| 51 | // to the creation of a new EchoControl object. |
| 52 | class MockEchoControlFactory : public EchoControlFactory { |
| 53 | public: |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 54 | MockEchoControlFactory() : next_mock_(std::make_unique<MockEchoControl>()) {} |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 55 | // Returns a pointer to the next MockEchoControl that this factory creates. |
| 56 | MockEchoControl* GetNext() const { return next_mock_.get(); } |
Per Åhgren | 4e5c709 | 2019-11-01 20:44:11 +0100 | [diff] [blame] | 57 | std::unique_ptr<EchoControl> Create(int sample_rate_hz, |
| 58 | int num_render_channels, |
| 59 | int num_capture_channels) override { |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 60 | std::unique_ptr<EchoControl> mock = std::move(next_mock_); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 61 | next_mock_ = std::make_unique<MockEchoControl>(); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 62 | return mock; |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | std::unique_ptr<MockEchoControl> next_mock_; |
| 67 | }; |
| 68 | |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 69 | // Mocks EchoDetector and records the first samples of the last analyzed render |
| 70 | // stream frame. Used to check what data is read by an EchoDetector |
| 71 | // implementation injected into an APM. |
| 72 | class TestEchoDetector : public EchoDetector { |
| 73 | public: |
| 74 | TestEchoDetector() |
| 75 | : analyze_render_audio_called_(false), |
| 76 | last_render_audio_first_sample_(0.f) {} |
| 77 | ~TestEchoDetector() override = default; |
| 78 | void AnalyzeRenderAudio(rtc::ArrayView<const float> render_audio) override { |
| 79 | last_render_audio_first_sample_ = render_audio[0]; |
| 80 | analyze_render_audio_called_ = true; |
| 81 | } |
| 82 | void AnalyzeCaptureAudio(rtc::ArrayView<const float> capture_audio) override { |
| 83 | } |
| 84 | void Initialize(int capture_sample_rate_hz, |
| 85 | int num_capture_channels, |
| 86 | int render_sample_rate_hz, |
| 87 | int num_render_channels) override {} |
| 88 | EchoDetector::Metrics GetMetrics() const override { return {}; } |
| 89 | // Returns true if AnalyzeRenderAudio() has been called at least once. |
| 90 | bool analyze_render_audio_called() const { |
| 91 | return analyze_render_audio_called_; |
| 92 | } |
| 93 | // Returns the first sample of the last analyzed render frame. |
| 94 | float last_render_audio_first_sample() const { |
| 95 | return last_render_audio_first_sample_; |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | bool analyze_render_audio_called_; |
| 100 | float last_render_audio_first_sample_; |
| 101 | }; |
| 102 | |
| 103 | // Mocks CustomProcessing and applies ProcessSample() to all the samples. |
| 104 | // Meant to be injected into an APM to modify samples in a known and detectable |
| 105 | // way. |
| 106 | class TestRenderPreProcessor : public CustomProcessing { |
| 107 | public: |
| 108 | TestRenderPreProcessor() = default; |
| 109 | ~TestRenderPreProcessor() = default; |
| 110 | void Initialize(int sample_rate_hz, int num_channels) override {} |
| 111 | void Process(AudioBuffer* audio) override { |
| 112 | for (size_t k = 0; k < audio->num_channels(); ++k) { |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 113 | rtc::ArrayView<float> channel_view(audio->channels()[k], |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 114 | audio->num_frames()); |
| 115 | std::transform(channel_view.begin(), channel_view.end(), |
| 116 | channel_view.begin(), ProcessSample); |
| 117 | } |
Mirko Bonadei | c4dd730 | 2019-02-25 09:12:02 +0100 | [diff] [blame] | 118 | } |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 119 | std::string ToString() const override { return "TestRenderPreProcessor"; } |
| 120 | void SetRuntimeSetting(AudioProcessing::RuntimeSetting setting) override {} |
| 121 | // Modifies a sample. This member is used in Process() to modify a frame and |
| 122 | // it is publicly visible to enable tests. |
| 123 | static constexpr float ProcessSample(float x) { return 2.f * x; } |
| 124 | }; |
| 125 | |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 126 | } // namespace |
| 127 | |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 128 | TEST(AudioProcessingImplTest, AudioParameterChangeTriggersInit) { |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 129 | webrtc::Config webrtc_config; |
| 130 | MockInitialize mock(webrtc_config); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 131 | ON_CALL(mock, InitializeLocked()) |
| 132 | .WillByDefault(Invoke(&mock, &MockInitialize::RealInitializeLocked)); |
| 133 | |
| 134 | EXPECT_CALL(mock, InitializeLocked()).Times(1); |
| 135 | mock.Initialize(); |
| 136 | |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 137 | constexpr size_t kMaxSampleRateHz = 32000; |
| 138 | constexpr size_t kMaxNumChannels = 2; |
| 139 | std::array<int16_t, kMaxNumChannels * kMaxSampleRateHz / 100> frame; |
| 140 | frame.fill(0); |
| 141 | StreamConfig config(16000, 1, /*has_keyboard=*/false); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 142 | // Call with the default parameters; there should be an init. |
Per Åhgren | 4bdced5 | 2017-06-27 16:00:38 +0200 | [diff] [blame] | 143 | EXPECT_CALL(mock, InitializeLocked()).Times(0); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 144 | EXPECT_NOERR(mock.ProcessStream(frame.data(), config, config, frame.data(), |
| 145 | /*vad_result=*/nullptr)); |
| 146 | EXPECT_NOERR( |
| 147 | mock.ProcessReverseStream(frame.data(), config, config, frame.data())); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 148 | |
| 149 | // New sample rate. (Only impacts ProcessStream). |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 150 | config = StreamConfig(32000, 1, /*has_keyboard=*/false); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 151 | EXPECT_CALL(mock, InitializeLocked()).Times(1); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 152 | EXPECT_NOERR(mock.ProcessStream(frame.data(), config, config, frame.data(), |
| 153 | /*vad_result=*/nullptr)); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 154 | |
| 155 | // New number of channels. |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 156 | // TODO(peah): Investigate why this causes 2 inits. |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 157 | config = StreamConfig(32000, 2, /*has_keyboard=*/false); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 158 | EXPECT_CALL(mock, InitializeLocked()).Times(2); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 159 | EXPECT_NOERR(mock.ProcessStream(frame.data(), config, config, frame.data(), |
| 160 | /*vad_result=*/nullptr)); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 161 | // ProcessStream sets num_channels_ == num_output_channels. |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 162 | EXPECT_NOERR( |
| 163 | mock.ProcessReverseStream(frame.data(), config, config, frame.data())); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 164 | |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 165 | // A new sample rate passed to ProcessReverseStream should cause an init. |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 166 | config = StreamConfig(16000, 2, /*has_keyboard=*/false); |
Alex Luebs | 5b830fe | 2016-03-08 17:52:52 +0100 | [diff] [blame] | 167 | EXPECT_CALL(mock, InitializeLocked()).Times(1); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 168 | EXPECT_NOERR( |
| 169 | mock.ProcessReverseStream(frame.data(), config, config, frame.data())); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 172 | TEST(AudioProcessingImplTest, UpdateCapturePreGainRuntimeSetting) { |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 173 | std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create()); |
| 174 | webrtc::AudioProcessing::Config apm_config; |
| 175 | apm_config.pre_amplifier.enabled = true; |
| 176 | apm_config.pre_amplifier.fixed_gain_factor = 1.f; |
| 177 | apm->ApplyConfig(apm_config); |
| 178 | |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 179 | constexpr int kSampleRateHz = 48000; |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 180 | constexpr int16_t kAudioLevel = 10000; |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 181 | constexpr size_t kNumChannels = 2; |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 182 | |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 183 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
| 184 | StreamConfig config(kSampleRateHz, kNumChannels, /*has_keyboard=*/false); |
| 185 | frame.fill(kAudioLevel); |
| 186 | apm->ProcessStream(frame.data(), config, config, frame.data(), |
| 187 | /*vad_result=*/nullptr); |
| 188 | EXPECT_EQ(frame[100], kAudioLevel) |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 189 | << "With factor 1, frame shouldn't be modified."; |
| 190 | |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 191 | constexpr float kGainFactor = 2.f; |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 192 | apm->SetRuntimeSetting( |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 193 | AudioProcessing::RuntimeSetting::CreateCapturePreGain(kGainFactor)); |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 194 | |
| 195 | // Process for two frames to have time to ramp up gain. |
| 196 | for (int i = 0; i < 2; ++i) { |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 197 | frame.fill(kAudioLevel); |
| 198 | apm->ProcessStream(frame.data(), config, config, frame.data(), |
| 199 | /*vad_result=*/nullptr); |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 200 | } |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 201 | EXPECT_EQ(frame[100], kGainFactor * kAudioLevel) |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 202 | << "Frame should be amplified."; |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 205 | TEST(AudioProcessingImplTest, |
| 206 | EchoControllerObservesPreAmplifierEchoPathGainChange) { |
| 207 | // Tests that the echo controller observes an echo path gain change when the |
| 208 | // pre-amplifier submodule changes the gain. |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 209 | auto echo_control_factory = std::make_unique<MockEchoControlFactory>(); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 210 | const auto* echo_control_factory_ptr = echo_control_factory.get(); |
| 211 | |
| 212 | std::unique_ptr<AudioProcessing> apm( |
| 213 | AudioProcessingBuilder() |
| 214 | .SetEchoControlFactory(std::move(echo_control_factory)) |
| 215 | .Create()); |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 216 | // Disable AGC. |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 217 | webrtc::AudioProcessing::Config apm_config; |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 218 | apm_config.gain_controller1.enabled = false; |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 219 | apm_config.gain_controller2.enabled = false; |
| 220 | apm_config.pre_amplifier.enabled = true; |
| 221 | apm_config.pre_amplifier.fixed_gain_factor = 1.f; |
| 222 | apm->ApplyConfig(apm_config); |
| 223 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 224 | constexpr int16_t kAudioLevel = 10000; |
| 225 | constexpr size_t kSampleRateHz = 48000; |
| 226 | constexpr size_t kNumChannels = 2; |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 227 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
| 228 | StreamConfig config(kSampleRateHz, kNumChannels, /*has_keyboard=*/false); |
| 229 | frame.fill(kAudioLevel); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 230 | |
| 231 | MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext(); |
| 232 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 233 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | bf47f34 | 2019-05-09 10:50:31 +0200 | [diff] [blame] | 234 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 235 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/false)) |
Fredrik Hernqvist | bf47f34 | 2019-05-09 10:50:31 +0200 | [diff] [blame] | 236 | .Times(1); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 237 | apm->ProcessStream(frame.data(), config, config, frame.data(), |
| 238 | /*vad_result=*/nullptr); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 239 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 240 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | bf47f34 | 2019-05-09 10:50:31 +0200 | [diff] [blame] | 241 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 242 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/true)) |
Fredrik Hernqvist | bf47f34 | 2019-05-09 10:50:31 +0200 | [diff] [blame] | 243 | .Times(1); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 244 | apm->SetRuntimeSetting( |
| 245 | AudioProcessing::RuntimeSetting::CreateCapturePreGain(2.f)); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 246 | apm->ProcessStream(frame.data(), config, config, frame.data(), |
| 247 | /*vad_result=*/nullptr); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | TEST(AudioProcessingImplTest, |
| 251 | EchoControllerObservesAnalogAgc1EchoPathGainChange) { |
| 252 | // Tests that the echo controller observes an echo path gain change when the |
| 253 | // AGC1 analog adaptive submodule changes the analog gain. |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 254 | auto echo_control_factory = std::make_unique<MockEchoControlFactory>(); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 255 | const auto* echo_control_factory_ptr = echo_control_factory.get(); |
| 256 | |
| 257 | std::unique_ptr<AudioProcessing> apm( |
| 258 | AudioProcessingBuilder() |
| 259 | .SetEchoControlFactory(std::move(echo_control_factory)) |
| 260 | .Create()); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 261 | webrtc::AudioProcessing::Config apm_config; |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 262 | // Enable AGC1. |
| 263 | apm_config.gain_controller1.enabled = true; |
| 264 | apm_config.gain_controller1.mode = |
| 265 | AudioProcessing::Config::GainController1::kAdaptiveAnalog; |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 266 | apm_config.gain_controller2.enabled = false; |
| 267 | apm_config.pre_amplifier.enabled = false; |
| 268 | apm->ApplyConfig(apm_config); |
| 269 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 270 | constexpr int16_t kAudioLevel = 1000; |
| 271 | constexpr size_t kSampleRateHz = 48000; |
| 272 | constexpr size_t kNumChannels = 2; |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 273 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
| 274 | StreamConfig stream_config(kSampleRateHz, kNumChannels, |
| 275 | /*has_keyboard=*/false); |
| 276 | frame.fill(kAudioLevel); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 277 | |
| 278 | MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext(); |
| 279 | |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 280 | const int initial_analog_gain = apm->recommended_stream_analog_level(); |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 281 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 282 | EXPECT_CALL(*echo_control_mock, ProcessCapture(NotNull(), testing::_, false)) |
| 283 | .Times(1); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 284 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data(), |
| 285 | /*vad_result=*/nullptr); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 286 | |
| 287 | // Force an analog gain change if it did not happen. |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 288 | if (initial_analog_gain == apm->recommended_stream_analog_level()) { |
| 289 | apm->set_stream_analog_level(initial_analog_gain + 1); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 290 | } |
| 291 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 292 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 293 | EXPECT_CALL(*echo_control_mock, ProcessCapture(NotNull(), testing::_, true)) |
| 294 | .Times(1); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 295 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data(), |
| 296 | /*vad_result=*/nullptr); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 297 | } |
| 298 | |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 299 | TEST(AudioProcessingImplTest, EchoControllerObservesPlayoutVolumeChange) { |
| 300 | // Tests that the echo controller observes an echo path gain change when a |
| 301 | // playout volume change is reported. |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 302 | auto echo_control_factory = std::make_unique<MockEchoControlFactory>(); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 303 | const auto* echo_control_factory_ptr = echo_control_factory.get(); |
| 304 | |
| 305 | std::unique_ptr<AudioProcessing> apm( |
| 306 | AudioProcessingBuilder() |
| 307 | .SetEchoControlFactory(std::move(echo_control_factory)) |
| 308 | .Create()); |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 309 | // Disable AGC. |
| 310 | webrtc::AudioProcessing::Config apm_config; |
| 311 | apm_config.gain_controller1.enabled = false; |
| 312 | apm_config.gain_controller2.enabled = false; |
| 313 | apm->ApplyConfig(apm_config); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 314 | |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 315 | constexpr int16_t kAudioLevel = 10000; |
| 316 | constexpr size_t kSampleRateHz = 48000; |
| 317 | constexpr size_t kNumChannels = 2; |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 318 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
| 319 | StreamConfig stream_config(kSampleRateHz, kNumChannels, |
| 320 | /*has_keyboard=*/false); |
| 321 | frame.fill(kAudioLevel); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 322 | |
| 323 | MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext(); |
| 324 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 325 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 326 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 327 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/false)) |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 328 | .Times(1); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 329 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data(), |
| 330 | /*vad_result=*/nullptr); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 331 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 332 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 333 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 334 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/false)) |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 335 | .Times(1); |
| 336 | apm->SetRuntimeSetting( |
| 337 | AudioProcessing::RuntimeSetting::CreatePlayoutVolumeChange(50)); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 338 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data(), |
| 339 | /*vad_result=*/nullptr); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 340 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 341 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 342 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 343 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/false)) |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 344 | .Times(1); |
| 345 | apm->SetRuntimeSetting( |
| 346 | AudioProcessing::RuntimeSetting::CreatePlayoutVolumeChange(50)); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 347 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data(), |
| 348 | /*vad_result=*/nullptr); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 349 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 350 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 351 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 352 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/true)) |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 353 | .Times(1); |
| 354 | apm->SetRuntimeSetting( |
| 355 | AudioProcessing::RuntimeSetting::CreatePlayoutVolumeChange(100)); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 356 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data(), |
| 357 | /*vad_result=*/nullptr); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 358 | } |
| 359 | |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 360 | TEST(AudioProcessingImplTest, RenderPreProcessorBeforeEchoDetector) { |
| 361 | // Make sure that signal changes caused by a render pre-processing sub-module |
| 362 | // take place before any echo detector analysis. |
| 363 | rtc::scoped_refptr<TestEchoDetector> test_echo_detector( |
| 364 | new rtc::RefCountedObject<TestEchoDetector>()); |
| 365 | std::unique_ptr<CustomProcessing> test_render_pre_processor( |
| 366 | new TestRenderPreProcessor()); |
| 367 | // Create APM injecting the test echo detector and render pre-processor. |
| 368 | std::unique_ptr<AudioProcessing> apm( |
| 369 | AudioProcessingBuilder() |
| 370 | .SetEchoDetector(test_echo_detector) |
| 371 | .SetRenderPreProcessing(std::move(test_render_pre_processor)) |
| 372 | .Create()); |
| 373 | webrtc::AudioProcessing::Config apm_config; |
| 374 | apm_config.pre_amplifier.enabled = true; |
| 375 | apm_config.residual_echo_detector.enabled = true; |
| 376 | apm->ApplyConfig(apm_config); |
| 377 | |
| 378 | constexpr int16_t kAudioLevel = 1000; |
| 379 | constexpr int kSampleRateHz = 16000; |
| 380 | constexpr size_t kNumChannels = 1; |
Sam Zackrisson | 12e319a | 2020-01-03 14:54:20 +0100 | [diff] [blame] | 381 | // Explicitly initialize APM to ensure no render frames are discarded. |
| 382 | const ProcessingConfig processing_config = {{ |
| 383 | {kSampleRateHz, kNumChannels, /*has_keyboard=*/false}, |
| 384 | {kSampleRateHz, kNumChannels, /*has_keyboard=*/false}, |
| 385 | {kSampleRateHz, kNumChannels, /*has_keyboard=*/false}, |
| 386 | {kSampleRateHz, kNumChannels, /*has_keyboard=*/false}, |
| 387 | }}; |
| 388 | apm->Initialize(processing_config); |
| 389 | |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 390 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
| 391 | StreamConfig stream_config(kSampleRateHz, kNumChannels, |
| 392 | /*has_keyboard=*/false); |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 393 | |
| 394 | constexpr float kAudioLevelFloat = static_cast<float>(kAudioLevel); |
| 395 | constexpr float kExpectedPreprocessedAudioLevel = |
| 396 | TestRenderPreProcessor::ProcessSample(kAudioLevelFloat); |
| 397 | ASSERT_NE(kAudioLevelFloat, kExpectedPreprocessedAudioLevel); |
| 398 | |
| 399 | // Analyze a render stream frame. |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 400 | frame.fill(kAudioLevel); |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 401 | ASSERT_EQ(AudioProcessing::Error::kNoError, |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 402 | apm->ProcessReverseStream(frame.data(), stream_config, |
| 403 | stream_config, frame.data())); |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 404 | // Trigger a call to in EchoDetector::AnalyzeRenderAudio() via |
| 405 | // ProcessStream(). |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame^] | 406 | frame.fill(kAudioLevel); |
| 407 | ASSERT_EQ(AudioProcessing::Error::kNoError, |
| 408 | apm->ProcessStream(frame.data(), stream_config, stream_config, |
| 409 | frame.data(), /*vad_result=*/nullptr)); |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 410 | // Regardless of how the call to in EchoDetector::AnalyzeRenderAudio() is |
| 411 | // triggered, the line below checks that the call has occurred. If not, the |
| 412 | // APM implementation may have changed and this test might need to be adapted. |
| 413 | ASSERT_TRUE(test_echo_detector->analyze_render_audio_called()); |
| 414 | // Check that the data read in EchoDetector::AnalyzeRenderAudio() is that |
| 415 | // produced by the render pre-processor. |
| 416 | EXPECT_EQ(kExpectedPreprocessedAudioLevel, |
| 417 | test_echo_detector->last_render_audio_first_sample()); |
| 418 | } |
| 419 | |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 420 | } // namespace webrtc |