blob: 5a2391b510d1b30cb1ecf098f32e765bab53318e [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
Mirko Bonadeid9708072019-01-25 20:26:48 +010015#include "api/scoped_refptr.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"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/ref_counted_object.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "test/gmock.h"
22#include "test/gtest.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000023
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000024namespace webrtc {
peaha9cc40b2017-06-29 08:32:09 -070025namespace {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000026
Alessio Bazzicae4498052018-12-17 09:44:06 +010027using ::testing::Invoke;
28using ::testing::NotNull;
29
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000030class MockInitialize : public AudioProcessingImpl {
31 public:
peah88ac8532016-09-12 16:47:25 -070032 explicit MockInitialize(const webrtc::Config& config)
33 : AudioProcessingImpl(config) {}
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000034
andrew@webrtc.orge84978f2014-01-25 02:09:06 +000035 MOCK_METHOD0(InitializeLocked, int());
danilchap56359be2017-09-07 07:53:45 -070036 int RealInitializeLocked() RTC_NO_THREAD_SAFETY_ANALYSIS {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000037 return AudioProcessingImpl::InitializeLocked();
38 }
peaha9cc40b2017-06-29 08:32:09 -070039
Niels Möller6f72f562017-10-19 13:15:17 +020040 MOCK_CONST_METHOD0(AddRef, void());
41 MOCK_CONST_METHOD0(Release, rtc::RefCountReleaseStatus());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000042};
43
Alessio Bazzicae4498052018-12-17 09:44:06 +010044// Creates MockEchoControl instances and provides a raw pointer access to
45// the next created one. The raw pointer is meant to be used with gmock.
46// Returning a pointer of the next created MockEchoControl instance is necessary
47// for the following reasons: (i) gmock expectations must be set before any call
48// occurs, (ii) APM is initialized the first time that
49// AudioProcessingImpl::ProcessStream() is called and the initialization leads
50// to the creation of a new EchoControl object.
51class MockEchoControlFactory : public EchoControlFactory {
52 public:
Mirko Bonadei317a1f02019-09-17 17:06:18 +020053 MockEchoControlFactory() : next_mock_(std::make_unique<MockEchoControl>()) {}
Alessio Bazzicae4498052018-12-17 09:44:06 +010054 // Returns a pointer to the next MockEchoControl that this factory creates.
55 MockEchoControl* GetNext() const { return next_mock_.get(); }
56 std::unique_ptr<EchoControl> Create(int sample_rate_hz) override {
Per Åhgren4e5c7092019-11-01 20:44:11 +010057 RTC_NOTREACHED();
58 return nullptr;
59 }
60 std::unique_ptr<EchoControl> Create(int sample_rate_hz,
61 int num_render_channels,
62 int num_capture_channels) override {
Alessio Bazzicae4498052018-12-17 09:44:06 +010063 std::unique_ptr<EchoControl> mock = std::move(next_mock_);
Mirko Bonadei317a1f02019-09-17 17:06:18 +020064 next_mock_ = std::make_unique<MockEchoControl>();
Alessio Bazzicae4498052018-12-17 09:44:06 +010065 return mock;
66 }
67
68 private:
69 std::unique_ptr<MockEchoControl> next_mock_;
70};
71
Alessio Bazzicad2b97402018-08-09 14:23:11 +020072void InitializeAudioFrame(size_t input_rate,
73 size_t num_channels,
74 AudioFrame* frame) {
Alex Loikob5c9a792018-04-16 16:31:22 +020075 const size_t samples_per_input_channel = rtc::CheckedDivExact(
76 input_rate, static_cast<size_t>(rtc::CheckedDivExact(
77 1000, AudioProcessing::kChunkSizeMs)));
Alex Loikob5c9a792018-04-16 16:31:22 +020078 RTC_DCHECK_LE(samples_per_input_channel * num_channels,
79 AudioFrame::kMaxDataSizeSamples);
Alessio Bazzicad2b97402018-08-09 14:23:11 +020080 frame->samples_per_channel_ = samples_per_input_channel;
81 frame->sample_rate_hz_ = input_rate;
82 frame->num_channels_ = num_channels;
83}
84
85void FillFixedFrame(int16_t audio_level, AudioFrame* frame) {
86 const size_t num_samples = frame->samples_per_channel_ * frame->num_channels_;
87 for (size_t i = 0; i < num_samples; ++i) {
88 frame->mutable_data()[i] = audio_level;
Alex Loikob5c9a792018-04-16 16:31:22 +020089 }
90}
91
Alessio Bazzicad2b97402018-08-09 14:23:11 +020092// Mocks EchoDetector and records the first samples of the last analyzed render
93// stream frame. Used to check what data is read by an EchoDetector
94// implementation injected into an APM.
95class TestEchoDetector : public EchoDetector {
96 public:
97 TestEchoDetector()
98 : analyze_render_audio_called_(false),
99 last_render_audio_first_sample_(0.f) {}
100 ~TestEchoDetector() override = default;
101 void AnalyzeRenderAudio(rtc::ArrayView<const float> render_audio) override {
102 last_render_audio_first_sample_ = render_audio[0];
103 analyze_render_audio_called_ = true;
104 }
105 void AnalyzeCaptureAudio(rtc::ArrayView<const float> capture_audio) override {
106 }
107 void Initialize(int capture_sample_rate_hz,
108 int num_capture_channels,
109 int render_sample_rate_hz,
110 int num_render_channels) override {}
111 EchoDetector::Metrics GetMetrics() const override { return {}; }
112 // Returns true if AnalyzeRenderAudio() has been called at least once.
113 bool analyze_render_audio_called() const {
114 return analyze_render_audio_called_;
115 }
116 // Returns the first sample of the last analyzed render frame.
117 float last_render_audio_first_sample() const {
118 return last_render_audio_first_sample_;
119 }
120
121 private:
122 bool analyze_render_audio_called_;
123 float last_render_audio_first_sample_;
124};
125
126// Mocks CustomProcessing and applies ProcessSample() to all the samples.
127// Meant to be injected into an APM to modify samples in a known and detectable
128// way.
129class TestRenderPreProcessor : public CustomProcessing {
130 public:
131 TestRenderPreProcessor() = default;
132 ~TestRenderPreProcessor() = default;
133 void Initialize(int sample_rate_hz, int num_channels) override {}
134 void Process(AudioBuffer* audio) override {
135 for (size_t k = 0; k < audio->num_channels(); ++k) {
Per Åhgrend47941e2019-08-22 11:51:13 +0200136 rtc::ArrayView<float> channel_view(audio->channels()[k],
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200137 audio->num_frames());
138 std::transform(channel_view.begin(), channel_view.end(),
139 channel_view.begin(), ProcessSample);
140 }
Mirko Bonadeic4dd7302019-02-25 09:12:02 +0100141 }
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200142 std::string ToString() const override { return "TestRenderPreProcessor"; }
143 void SetRuntimeSetting(AudioProcessing::RuntimeSetting setting) override {}
144 // Modifies a sample. This member is used in Process() to modify a frame and
145 // it is publicly visible to enable tests.
146 static constexpr float ProcessSample(float x) { return 2.f * x; }
147};
148
peaha9cc40b2017-06-29 08:32:09 -0700149} // namespace
150
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000151TEST(AudioProcessingImplTest, AudioParameterChangeTriggersInit) {
peah88ac8532016-09-12 16:47:25 -0700152 webrtc::Config config;
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000153 MockInitialize mock(config);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000154 ON_CALL(mock, InitializeLocked())
155 .WillByDefault(Invoke(&mock, &MockInitialize::RealInitializeLocked));
156
157 EXPECT_CALL(mock, InitializeLocked()).Times(1);
158 mock.Initialize();
159
160 AudioFrame frame;
peah2ace3f92016-09-10 04:42:27 -0700161 // Call with the default parameters; there should be an init.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000162 frame.num_channels_ = 1;
163 SetFrameSampleRate(&frame, 16000);
Per Åhgren4bdced52017-06-27 16:00:38 +0200164 EXPECT_CALL(mock, InitializeLocked()).Times(0);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000165 EXPECT_NOERR(mock.ProcessStream(&frame));
aluebsb0319552016-03-17 20:39:53 -0700166 EXPECT_NOERR(mock.ProcessReverseStream(&frame));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000167
168 // New sample rate. (Only impacts ProcessStream).
169 SetFrameSampleRate(&frame, 32000);
Yves Gerey665174f2018-06-19 15:03:05 +0200170 EXPECT_CALL(mock, InitializeLocked()).Times(1);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000171 EXPECT_NOERR(mock.ProcessStream(&frame));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000172
173 // New number of channels.
peah2ace3f92016-09-10 04:42:27 -0700174 // TODO(peah): Investigate why this causes 2 inits.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000175 frame.num_channels_ = 2;
Yves Gerey665174f2018-06-19 15:03:05 +0200176 EXPECT_CALL(mock, InitializeLocked()).Times(2);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000177 EXPECT_NOERR(mock.ProcessStream(&frame));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000178 // ProcessStream sets num_channels_ == num_output_channels.
179 frame.num_channels_ = 2;
aluebsb0319552016-03-17 20:39:53 -0700180 EXPECT_NOERR(mock.ProcessReverseStream(&frame));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000181
aluebsb0319552016-03-17 20:39:53 -0700182 // A new sample rate passed to ProcessReverseStream should cause an init.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000183 SetFrameSampleRate(&frame, 16000);
Alex Luebs5b830fe2016-03-08 17:52:52 +0100184 EXPECT_CALL(mock, InitializeLocked()).Times(1);
aluebsb0319552016-03-17 20:39:53 -0700185 EXPECT_NOERR(mock.ProcessReverseStream(&frame));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000186}
187
Alessio Bazzicac054e782018-04-16 12:10:09 +0200188TEST(AudioProcessingImplTest, UpdateCapturePreGainRuntimeSetting) {
Alex Loikob5c9a792018-04-16 16:31:22 +0200189 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
190 webrtc::AudioProcessing::Config apm_config;
191 apm_config.pre_amplifier.enabled = true;
192 apm_config.pre_amplifier.fixed_gain_factor = 1.f;
193 apm->ApplyConfig(apm_config);
194
195 AudioFrame frame;
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200196 constexpr int16_t kAudioLevel = 10000;
197 constexpr size_t kSampleRateHz = 48000;
198 constexpr size_t kNumChannels = 2;
199 InitializeAudioFrame(kSampleRateHz, kNumChannels, &frame);
Alex Loikob5c9a792018-04-16 16:31:22 +0200200
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200201 FillFixedFrame(kAudioLevel, &frame);
Alex Loikob5c9a792018-04-16 16:31:22 +0200202 apm->ProcessStream(&frame);
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200203 EXPECT_EQ(frame.data()[100], kAudioLevel)
Alex Loikob5c9a792018-04-16 16:31:22 +0200204 << "With factor 1, frame shouldn't be modified.";
205
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200206 constexpr float kGainFactor = 2.f;
Alex Loikob5c9a792018-04-16 16:31:22 +0200207 apm->SetRuntimeSetting(
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200208 AudioProcessing::RuntimeSetting::CreateCapturePreGain(kGainFactor));
Alex Loikob5c9a792018-04-16 16:31:22 +0200209
210 // Process for two frames to have time to ramp up gain.
211 for (int i = 0; i < 2; ++i) {
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200212 FillFixedFrame(kAudioLevel, &frame);
Alex Loikob5c9a792018-04-16 16:31:22 +0200213 apm->ProcessStream(&frame);
214 }
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200215 EXPECT_EQ(frame.data()[100], kGainFactor * kAudioLevel)
Alex Loikob5c9a792018-04-16 16:31:22 +0200216 << "Frame should be amplified.";
Alessio Bazzicac054e782018-04-16 12:10:09 +0200217}
218
Alessio Bazzicae4498052018-12-17 09:44:06 +0100219TEST(AudioProcessingImplTest,
220 EchoControllerObservesPreAmplifierEchoPathGainChange) {
221 // Tests that the echo controller observes an echo path gain change when the
222 // pre-amplifier submodule changes the gain.
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200223 auto echo_control_factory = std::make_unique<MockEchoControlFactory>();
Alessio Bazzicae4498052018-12-17 09:44:06 +0100224 const auto* echo_control_factory_ptr = echo_control_factory.get();
225
226 std::unique_ptr<AudioProcessing> apm(
227 AudioProcessingBuilder()
228 .SetEchoControlFactory(std::move(echo_control_factory))
229 .Create());
Sam Zackrisson41478c72019-10-15 10:10:26 +0200230 // Disable AGC.
Alessio Bazzicae4498052018-12-17 09:44:06 +0100231 webrtc::AudioProcessing::Config apm_config;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200232 apm_config.gain_controller1.enabled = false;
Alessio Bazzicae4498052018-12-17 09:44:06 +0100233 apm_config.gain_controller2.enabled = false;
234 apm_config.pre_amplifier.enabled = true;
235 apm_config.pre_amplifier.fixed_gain_factor = 1.f;
236 apm->ApplyConfig(apm_config);
237
238 AudioFrame frame;
239 constexpr int16_t kAudioLevel = 10000;
240 constexpr size_t kSampleRateHz = 48000;
241 constexpr size_t kNumChannels = 2;
242 InitializeAudioFrame(kSampleRateHz, kNumChannels, &frame);
243 FillFixedFrame(kAudioLevel, &frame);
244
245 MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext();
246
Per Åhgren0aefbf02019-08-23 21:29:17 +0200247 EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1);
Fredrik Hernqvistbf47f342019-05-09 10:50:31 +0200248 EXPECT_CALL(*echo_control_mock,
249 ProcessCapture(NotNull(), /*echo_path_change=*/false))
250 .Times(1);
Alessio Bazzicae4498052018-12-17 09:44:06 +0100251 apm->ProcessStream(&frame);
252
Per Åhgren0aefbf02019-08-23 21:29:17 +0200253 EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1);
Fredrik Hernqvistbf47f342019-05-09 10:50:31 +0200254 EXPECT_CALL(*echo_control_mock,
255 ProcessCapture(NotNull(), /*echo_path_change=*/true))
256 .Times(1);
Alessio Bazzicae4498052018-12-17 09:44:06 +0100257 apm->SetRuntimeSetting(
258 AudioProcessing::RuntimeSetting::CreateCapturePreGain(2.f));
259 apm->ProcessStream(&frame);
260}
261
262TEST(AudioProcessingImplTest,
263 EchoControllerObservesAnalogAgc1EchoPathGainChange) {
264 // Tests that the echo controller observes an echo path gain change when the
265 // AGC1 analog adaptive submodule changes the analog gain.
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200266 auto echo_control_factory = std::make_unique<MockEchoControlFactory>();
Alessio Bazzicae4498052018-12-17 09:44:06 +0100267 const auto* echo_control_factory_ptr = echo_control_factory.get();
268
269 std::unique_ptr<AudioProcessing> apm(
270 AudioProcessingBuilder()
271 .SetEchoControlFactory(std::move(echo_control_factory))
272 .Create());
Alessio Bazzicae4498052018-12-17 09:44:06 +0100273 webrtc::AudioProcessing::Config apm_config;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200274 // Enable AGC1.
275 apm_config.gain_controller1.enabled = true;
276 apm_config.gain_controller1.mode =
277 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
Alessio Bazzicae4498052018-12-17 09:44:06 +0100278 apm_config.gain_controller2.enabled = false;
279 apm_config.pre_amplifier.enabled = false;
280 apm->ApplyConfig(apm_config);
281
282 AudioFrame frame;
283 constexpr int16_t kAudioLevel = 1000;
284 constexpr size_t kSampleRateHz = 48000;
285 constexpr size_t kNumChannels = 2;
286 InitializeAudioFrame(kSampleRateHz, kNumChannels, &frame);
287 FillFixedFrame(kAudioLevel, &frame);
288
289 MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext();
290
Sam Zackrisson41478c72019-10-15 10:10:26 +0200291 const int initial_analog_gain = apm->recommended_stream_analog_level();
Per Åhgren0aefbf02019-08-23 21:29:17 +0200292 EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1);
Alessio Bazzicae4498052018-12-17 09:44:06 +0100293 EXPECT_CALL(*echo_control_mock, ProcessCapture(NotNull(), false)).Times(1);
294 apm->ProcessStream(&frame);
295
296 // Force an analog gain change if it did not happen.
Sam Zackrisson41478c72019-10-15 10:10:26 +0200297 if (initial_analog_gain == apm->recommended_stream_analog_level()) {
298 apm->set_stream_analog_level(initial_analog_gain + 1);
Alessio Bazzicae4498052018-12-17 09:44:06 +0100299 }
300
Per Åhgren0aefbf02019-08-23 21:29:17 +0200301 EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1);
Alessio Bazzicae4498052018-12-17 09:44:06 +0100302 EXPECT_CALL(*echo_control_mock, ProcessCapture(NotNull(), true)).Times(1);
303 apm->ProcessStream(&frame);
304}
305
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200306TEST(AudioProcessingImplTest, EchoControllerObservesPlayoutVolumeChange) {
307 // Tests that the echo controller observes an echo path gain change when a
308 // playout volume change is reported.
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200309 auto echo_control_factory = std::make_unique<MockEchoControlFactory>();
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200310 const auto* echo_control_factory_ptr = echo_control_factory.get();
311
312 std::unique_ptr<AudioProcessing> apm(
313 AudioProcessingBuilder()
314 .SetEchoControlFactory(std::move(echo_control_factory))
315 .Create());
Sam Zackrisson41478c72019-10-15 10:10:26 +0200316 // Disable AGC.
317 webrtc::AudioProcessing::Config apm_config;
318 apm_config.gain_controller1.enabled = false;
319 apm_config.gain_controller2.enabled = false;
320 apm->ApplyConfig(apm_config);
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200321
322 AudioFrame frame;
323 constexpr int16_t kAudioLevel = 10000;
324 constexpr size_t kSampleRateHz = 48000;
325 constexpr size_t kNumChannels = 2;
326 InitializeAudioFrame(kSampleRateHz, kNumChannels, &frame);
327 FillFixedFrame(kAudioLevel, &frame);
328
329 MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext();
330
Per Åhgren0aefbf02019-08-23 21:29:17 +0200331 EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1);
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200332 EXPECT_CALL(*echo_control_mock,
333 ProcessCapture(NotNull(), /*echo_path_change=*/false))
334 .Times(1);
335 apm->ProcessStream(&frame);
336
Per Åhgren0aefbf02019-08-23 21:29:17 +0200337 EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1);
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200338 EXPECT_CALL(*echo_control_mock,
339 ProcessCapture(NotNull(), /*echo_path_change=*/false))
340 .Times(1);
341 apm->SetRuntimeSetting(
342 AudioProcessing::RuntimeSetting::CreatePlayoutVolumeChange(50));
343 apm->ProcessStream(&frame);
344
Per Åhgren0aefbf02019-08-23 21:29:17 +0200345 EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1);
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200346 EXPECT_CALL(*echo_control_mock,
347 ProcessCapture(NotNull(), /*echo_path_change=*/false))
348 .Times(1);
349 apm->SetRuntimeSetting(
350 AudioProcessing::RuntimeSetting::CreatePlayoutVolumeChange(50));
351 apm->ProcessStream(&frame);
352
Per Åhgren0aefbf02019-08-23 21:29:17 +0200353 EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1);
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200354 EXPECT_CALL(*echo_control_mock,
355 ProcessCapture(NotNull(), /*echo_path_change=*/true))
356 .Times(1);
357 apm->SetRuntimeSetting(
358 AudioProcessing::RuntimeSetting::CreatePlayoutVolumeChange(100));
359 apm->ProcessStream(&frame);
360}
361
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200362TEST(AudioProcessingImplTest, RenderPreProcessorBeforeEchoDetector) {
363 // Make sure that signal changes caused by a render pre-processing sub-module
364 // take place before any echo detector analysis.
365 rtc::scoped_refptr<TestEchoDetector> test_echo_detector(
366 new rtc::RefCountedObject<TestEchoDetector>());
367 std::unique_ptr<CustomProcessing> test_render_pre_processor(
368 new TestRenderPreProcessor());
369 // Create APM injecting the test echo detector and render pre-processor.
370 std::unique_ptr<AudioProcessing> apm(
371 AudioProcessingBuilder()
372 .SetEchoDetector(test_echo_detector)
373 .SetRenderPreProcessing(std::move(test_render_pre_processor))
374 .Create());
375 webrtc::AudioProcessing::Config apm_config;
376 apm_config.pre_amplifier.enabled = true;
377 apm_config.residual_echo_detector.enabled = true;
378 apm->ApplyConfig(apm_config);
379
380 constexpr int16_t kAudioLevel = 1000;
381 constexpr int kSampleRateHz = 16000;
382 constexpr size_t kNumChannels = 1;
383 AudioFrame frame;
384 InitializeAudioFrame(kSampleRateHz, kNumChannels, &frame);
385
386 constexpr float kAudioLevelFloat = static_cast<float>(kAudioLevel);
387 constexpr float kExpectedPreprocessedAudioLevel =
388 TestRenderPreProcessor::ProcessSample(kAudioLevelFloat);
389 ASSERT_NE(kAudioLevelFloat, kExpectedPreprocessedAudioLevel);
390
391 // Analyze a render stream frame.
392 FillFixedFrame(kAudioLevel, &frame);
393 ASSERT_EQ(AudioProcessing::Error::kNoError,
394 apm->ProcessReverseStream(&frame));
395 // Trigger a call to in EchoDetector::AnalyzeRenderAudio() via
396 // ProcessStream().
397 FillFixedFrame(kAudioLevel, &frame);
398 ASSERT_EQ(AudioProcessing::Error::kNoError, apm->ProcessStream(&frame));
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.org60730cf2014-01-07 17:45:09 +0000409} // namespace webrtc