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