blob: 3a3133a7885c012ca105a90fb85af169f806fd64 [file] [log] [blame]
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/audio_processing_impl.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000012
Alessio Bazzicae4498052018-12-17 09:44:06 +010013#include <memory>
14
15#include "absl/memory/memory.h"
Alessio Bazzicad2b97402018-08-09 14:23:11 +020016#include "modules/audio_processing/include/audio_processing.h"
Alessio Bazzicae4498052018-12-17 09:44:06 +010017#include "modules/audio_processing/test/echo_control_mock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_processing/test/test_utils.h"
Alessio Bazzicae4498052018-12-17 09:44:06 +010019#include "rtc_base/checks.h"
Alessio Bazzicad2b97402018-08-09 14:23:11 +020020#include "rtc_base/refcountedobject.h"
21#include "rtc_base/scoped_ref_ptr.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "test/gmock.h"
23#include "test/gtest.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000024
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000025namespace webrtc {
peaha9cc40b2017-06-29 08:32:09 -070026namespace {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000027
Alessio Bazzicae4498052018-12-17 09:44:06 +010028using ::testing::Invoke;
29using ::testing::NotNull;
30
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000031class MockInitialize : public AudioProcessingImpl {
32 public:
peah88ac8532016-09-12 16:47:25 -070033 explicit MockInitialize(const webrtc::Config& config)
34 : AudioProcessingImpl(config) {}
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000035
andrew@webrtc.orge84978f2014-01-25 02:09:06 +000036 MOCK_METHOD0(InitializeLocked, int());
danilchap56359be2017-09-07 07:53:45 -070037 int RealInitializeLocked() RTC_NO_THREAD_SAFETY_ANALYSIS {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000038 return AudioProcessingImpl::InitializeLocked();
39 }
peaha9cc40b2017-06-29 08:32:09 -070040
Niels Möller6f72f562017-10-19 13:15:17 +020041 MOCK_CONST_METHOD0(AddRef, void());
42 MOCK_CONST_METHOD0(Release, rtc::RefCountReleaseStatus());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000043};
44
Alessio Bazzicae4498052018-12-17 09:44:06 +010045// 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.
52class MockEchoControlFactory : public EchoControlFactory {
53 public:
54 MockEchoControlFactory() : next_mock_(absl::make_unique<MockEchoControl>()) {}
55 // Returns a pointer to the next MockEchoControl that this factory creates.
56 MockEchoControl* GetNext() const { return next_mock_.get(); }
57 std::unique_ptr<EchoControl> Create(int sample_rate_hz) override {
58 std::unique_ptr<EchoControl> mock = std::move(next_mock_);
59 next_mock_ = absl::make_unique<MockEchoControl>();
60 return mock;
61 }
62
63 private:
64 std::unique_ptr<MockEchoControl> next_mock_;
65};
66
Alessio Bazzicad2b97402018-08-09 14:23:11 +020067void InitializeAudioFrame(size_t input_rate,
68 size_t num_channels,
69 AudioFrame* frame) {
Alex Loikob5c9a792018-04-16 16:31:22 +020070 const size_t samples_per_input_channel = rtc::CheckedDivExact(
71 input_rate, static_cast<size_t>(rtc::CheckedDivExact(
72 1000, AudioProcessing::kChunkSizeMs)));
Alex Loikob5c9a792018-04-16 16:31:22 +020073 RTC_DCHECK_LE(samples_per_input_channel * num_channels,
74 AudioFrame::kMaxDataSizeSamples);
Alessio Bazzicad2b97402018-08-09 14:23:11 +020075 frame->samples_per_channel_ = samples_per_input_channel;
76 frame->sample_rate_hz_ = input_rate;
77 frame->num_channels_ = num_channels;
78}
79
80void FillFixedFrame(int16_t audio_level, AudioFrame* frame) {
81 const size_t num_samples = frame->samples_per_channel_ * frame->num_channels_;
82 for (size_t i = 0; i < num_samples; ++i) {
83 frame->mutable_data()[i] = audio_level;
Alex Loikob5c9a792018-04-16 16:31:22 +020084 }
85}
86
Alessio Bazzicad2b97402018-08-09 14:23:11 +020087// Mocks EchoDetector and records the first samples of the last analyzed render
88// stream frame. Used to check what data is read by an EchoDetector
89// implementation injected into an APM.
90class TestEchoDetector : public EchoDetector {
91 public:
92 TestEchoDetector()
93 : analyze_render_audio_called_(false),
94 last_render_audio_first_sample_(0.f) {}
95 ~TestEchoDetector() override = default;
96 void AnalyzeRenderAudio(rtc::ArrayView<const float> render_audio) override {
97 last_render_audio_first_sample_ = render_audio[0];
98 analyze_render_audio_called_ = true;
99 }
100 void AnalyzeCaptureAudio(rtc::ArrayView<const float> capture_audio) override {
101 }
102 void Initialize(int capture_sample_rate_hz,
103 int num_capture_channels,
104 int render_sample_rate_hz,
105 int num_render_channels) override {}
106 EchoDetector::Metrics GetMetrics() const override { return {}; }
107 // Returns true if AnalyzeRenderAudio() has been called at least once.
108 bool analyze_render_audio_called() const {
109 return analyze_render_audio_called_;
110 }
111 // Returns the first sample of the last analyzed render frame.
112 float last_render_audio_first_sample() const {
113 return last_render_audio_first_sample_;
114 }
115
116 private:
117 bool analyze_render_audio_called_;
118 float last_render_audio_first_sample_;
119};
120
121// Mocks CustomProcessing and applies ProcessSample() to all the samples.
122// Meant to be injected into an APM to modify samples in a known and detectable
123// way.
124class TestRenderPreProcessor : public CustomProcessing {
125 public:
126 TestRenderPreProcessor() = default;
127 ~TestRenderPreProcessor() = default;
128 void Initialize(int sample_rate_hz, int num_channels) override {}
129 void Process(AudioBuffer* audio) override {
130 for (size_t k = 0; k < audio->num_channels(); ++k) {
131 rtc::ArrayView<float> channel_view(audio->channels_f()[k],
132 audio->num_frames());
133 std::transform(channel_view.begin(), channel_view.end(),
134 channel_view.begin(), ProcessSample);
135 }
136 };
137 std::string ToString() const override { return "TestRenderPreProcessor"; }
138 void SetRuntimeSetting(AudioProcessing::RuntimeSetting setting) override {}
139 // Modifies a sample. This member is used in Process() to modify a frame and
140 // it is publicly visible to enable tests.
141 static constexpr float ProcessSample(float x) { return 2.f * x; }
142};
143
peaha9cc40b2017-06-29 08:32:09 -0700144} // namespace
145
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000146TEST(AudioProcessingImplTest, AudioParameterChangeTriggersInit) {
peah88ac8532016-09-12 16:47:25 -0700147 webrtc::Config config;
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000148 MockInitialize mock(config);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000149 ON_CALL(mock, InitializeLocked())
150 .WillByDefault(Invoke(&mock, &MockInitialize::RealInitializeLocked));
151
152 EXPECT_CALL(mock, InitializeLocked()).Times(1);
153 mock.Initialize();
154
155 AudioFrame frame;
peah2ace3f92016-09-10 04:42:27 -0700156 // Call with the default parameters; there should be an init.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000157 frame.num_channels_ = 1;
158 SetFrameSampleRate(&frame, 16000);
Per Ã…hgren4bdced52017-06-27 16:00:38 +0200159 EXPECT_CALL(mock, InitializeLocked()).Times(0);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000160 EXPECT_NOERR(mock.ProcessStream(&frame));
aluebsb0319552016-03-17 20:39:53 -0700161 EXPECT_NOERR(mock.ProcessReverseStream(&frame));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000162
163 // New sample rate. (Only impacts ProcessStream).
164 SetFrameSampleRate(&frame, 32000);
Yves Gerey665174f2018-06-19 15:03:05 +0200165 EXPECT_CALL(mock, InitializeLocked()).Times(1);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000166 EXPECT_NOERR(mock.ProcessStream(&frame));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000167
168 // New number of channels.
peah2ace3f92016-09-10 04:42:27 -0700169 // TODO(peah): Investigate why this causes 2 inits.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000170 frame.num_channels_ = 2;
Yves Gerey665174f2018-06-19 15:03:05 +0200171 EXPECT_CALL(mock, InitializeLocked()).Times(2);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000172 EXPECT_NOERR(mock.ProcessStream(&frame));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000173 // ProcessStream sets num_channels_ == num_output_channels.
174 frame.num_channels_ = 2;
aluebsb0319552016-03-17 20:39:53 -0700175 EXPECT_NOERR(mock.ProcessReverseStream(&frame));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000176
aluebsb0319552016-03-17 20:39:53 -0700177 // A new sample rate passed to ProcessReverseStream should cause an init.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000178 SetFrameSampleRate(&frame, 16000);
Alex Luebs5b830fe2016-03-08 17:52:52 +0100179 EXPECT_CALL(mock, InitializeLocked()).Times(1);
aluebsb0319552016-03-17 20:39:53 -0700180 EXPECT_NOERR(mock.ProcessReverseStream(&frame));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000181}
182
Alessio Bazzicac054e782018-04-16 12:10:09 +0200183TEST(AudioProcessingImplTest, UpdateCapturePreGainRuntimeSetting) {
Alex Loikob5c9a792018-04-16 16:31:22 +0200184 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
185 webrtc::AudioProcessing::Config apm_config;
186 apm_config.pre_amplifier.enabled = true;
187 apm_config.pre_amplifier.fixed_gain_factor = 1.f;
188 apm->ApplyConfig(apm_config);
189
190 AudioFrame frame;
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200191 constexpr int16_t kAudioLevel = 10000;
192 constexpr size_t kSampleRateHz = 48000;
193 constexpr size_t kNumChannels = 2;
194 InitializeAudioFrame(kSampleRateHz, kNumChannels, &frame);
Alex Loikob5c9a792018-04-16 16:31:22 +0200195
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200196 FillFixedFrame(kAudioLevel, &frame);
Alex Loikob5c9a792018-04-16 16:31:22 +0200197 apm->ProcessStream(&frame);
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200198 EXPECT_EQ(frame.data()[100], kAudioLevel)
Alex Loikob5c9a792018-04-16 16:31:22 +0200199 << "With factor 1, frame shouldn't be modified.";
200
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200201 constexpr float kGainFactor = 2.f;
Alex Loikob5c9a792018-04-16 16:31:22 +0200202 apm->SetRuntimeSetting(
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200203 AudioProcessing::RuntimeSetting::CreateCapturePreGain(kGainFactor));
Alex Loikob5c9a792018-04-16 16:31:22 +0200204
205 // Process for two frames to have time to ramp up gain.
206 for (int i = 0; i < 2; ++i) {
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200207 FillFixedFrame(kAudioLevel, &frame);
Alex Loikob5c9a792018-04-16 16:31:22 +0200208 apm->ProcessStream(&frame);
209 }
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200210 EXPECT_EQ(frame.data()[100], kGainFactor * kAudioLevel)
Alex Loikob5c9a792018-04-16 16:31:22 +0200211 << "Frame should be amplified.";
Alessio Bazzicac054e782018-04-16 12:10:09 +0200212}
213
Alessio Bazzicae4498052018-12-17 09:44:06 +0100214TEST(AudioProcessingImplTest,
215 EchoControllerObservesPreAmplifierEchoPathGainChange) {
216 // Tests that the echo controller observes an echo path gain change when the
217 // pre-amplifier submodule changes the gain.
218 auto echo_control_factory = absl::make_unique<MockEchoControlFactory>();
219 const auto* echo_control_factory_ptr = echo_control_factory.get();
220
221 std::unique_ptr<AudioProcessing> apm(
222 AudioProcessingBuilder()
223 .SetEchoControlFactory(std::move(echo_control_factory))
224 .Create());
225 apm->gain_control()->Enable(false); // Disable AGC.
226 apm->gain_control()->set_mode(GainControl::Mode::kFixedDigital);
227 webrtc::AudioProcessing::Config apm_config;
228 apm_config.gain_controller2.enabled = false;
229 apm_config.pre_amplifier.enabled = true;
230 apm_config.pre_amplifier.fixed_gain_factor = 1.f;
231 apm->ApplyConfig(apm_config);
232
233 AudioFrame frame;
234 constexpr int16_t kAudioLevel = 10000;
235 constexpr size_t kSampleRateHz = 48000;
236 constexpr size_t kNumChannels = 2;
237 InitializeAudioFrame(kSampleRateHz, kNumChannels, &frame);
238 FillFixedFrame(kAudioLevel, &frame);
239
240 MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext();
241
242 EXPECT_CALL(*echo_control_mock, AnalyzeCapture(NotNull())).Times(1);
243 EXPECT_CALL(*echo_control_mock, ProcessCapture(NotNull(), false)).Times(1);
244 apm->ProcessStream(&frame);
245
246 EXPECT_CALL(*echo_control_mock, AnalyzeCapture(NotNull())).Times(1);
247 EXPECT_CALL(*echo_control_mock, ProcessCapture(NotNull(), true)).Times(1);
248 apm->SetRuntimeSetting(
249 AudioProcessing::RuntimeSetting::CreateCapturePreGain(2.f));
250 apm->ProcessStream(&frame);
251}
252
253TEST(AudioProcessingImplTest,
254 EchoControllerObservesAnalogAgc1EchoPathGainChange) {
255 // Tests that the echo controller observes an echo path gain change when the
256 // AGC1 analog adaptive submodule changes the analog gain.
257 auto echo_control_factory = absl::make_unique<MockEchoControlFactory>();
258 const auto* echo_control_factory_ptr = echo_control_factory.get();
259
260 std::unique_ptr<AudioProcessing> apm(
261 AudioProcessingBuilder()
262 .SetEchoControlFactory(std::move(echo_control_factory))
263 .Create());
264 apm->gain_control()->Enable(true); // Enable AGC.
265 apm->gain_control()->set_mode(GainControl::Mode::kAdaptiveAnalog);
266 webrtc::AudioProcessing::Config apm_config;
267 apm_config.gain_controller2.enabled = false;
268 apm_config.pre_amplifier.enabled = false;
269 apm->ApplyConfig(apm_config);
270
271 AudioFrame frame;
272 constexpr int16_t kAudioLevel = 1000;
273 constexpr size_t kSampleRateHz = 48000;
274 constexpr size_t kNumChannels = 2;
275 InitializeAudioFrame(kSampleRateHz, kNumChannels, &frame);
276 FillFixedFrame(kAudioLevel, &frame);
277
278 MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext();
279
280 const int initial_analog_gain = apm->gain_control()->stream_analog_level();
281 EXPECT_CALL(*echo_control_mock, AnalyzeCapture(NotNull())).Times(1);
282 EXPECT_CALL(*echo_control_mock, ProcessCapture(NotNull(), false)).Times(1);
283 apm->ProcessStream(&frame);
284
285 // Force an analog gain change if it did not happen.
286 if (initial_analog_gain == apm->gain_control()->stream_analog_level()) {
287 apm->gain_control()->set_stream_analog_level(initial_analog_gain + 1);
288 }
289
290 EXPECT_CALL(*echo_control_mock, AnalyzeCapture(NotNull())).Times(1);
291 EXPECT_CALL(*echo_control_mock, ProcessCapture(NotNull(), true)).Times(1);
292 apm->ProcessStream(&frame);
293}
294
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200295TEST(AudioProcessingImplTest, RenderPreProcessorBeforeEchoDetector) {
296 // Make sure that signal changes caused by a render pre-processing sub-module
297 // take place before any echo detector analysis.
298 rtc::scoped_refptr<TestEchoDetector> test_echo_detector(
299 new rtc::RefCountedObject<TestEchoDetector>());
300 std::unique_ptr<CustomProcessing> test_render_pre_processor(
301 new TestRenderPreProcessor());
302 // Create APM injecting the test echo detector and render pre-processor.
303 std::unique_ptr<AudioProcessing> apm(
304 AudioProcessingBuilder()
305 .SetEchoDetector(test_echo_detector)
306 .SetRenderPreProcessing(std::move(test_render_pre_processor))
307 .Create());
308 webrtc::AudioProcessing::Config apm_config;
309 apm_config.pre_amplifier.enabled = true;
310 apm_config.residual_echo_detector.enabled = true;
311 apm->ApplyConfig(apm_config);
312
313 constexpr int16_t kAudioLevel = 1000;
314 constexpr int kSampleRateHz = 16000;
315 constexpr size_t kNumChannels = 1;
316 AudioFrame frame;
317 InitializeAudioFrame(kSampleRateHz, kNumChannels, &frame);
318
319 constexpr float kAudioLevelFloat = static_cast<float>(kAudioLevel);
320 constexpr float kExpectedPreprocessedAudioLevel =
321 TestRenderPreProcessor::ProcessSample(kAudioLevelFloat);
322 ASSERT_NE(kAudioLevelFloat, kExpectedPreprocessedAudioLevel);
323
324 // Analyze a render stream frame.
325 FillFixedFrame(kAudioLevel, &frame);
326 ASSERT_EQ(AudioProcessing::Error::kNoError,
327 apm->ProcessReverseStream(&frame));
328 // Trigger a call to in EchoDetector::AnalyzeRenderAudio() via
329 // ProcessStream().
330 FillFixedFrame(kAudioLevel, &frame);
331 ASSERT_EQ(AudioProcessing::Error::kNoError, apm->ProcessStream(&frame));
332 // Regardless of how the call to in EchoDetector::AnalyzeRenderAudio() is
333 // triggered, the line below checks that the call has occurred. If not, the
334 // APM implementation may have changed and this test might need to be adapted.
335 ASSERT_TRUE(test_echo_detector->analyze_render_audio_called());
336 // Check that the data read in EchoDetector::AnalyzeRenderAudio() is that
337 // produced by the render pre-processor.
338 EXPECT_EQ(kExpectedPreprocessedAudioLevel,
339 test_echo_detector->last_render_audio_first_sample());
340}
341
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000342} // namespace webrtc