blob: a441e2f2082ff6694443c97640683eb00ff5e13f [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
Per Åhgren2507f8c2020-03-19 12:33:29 +010013#include <array>
Alessio Bazzicae4498052018-12-17 09:44:06 +010014#include <memory>
15
Mirko Bonadeid9708072019-01-25 20:26:48 +010016#include "api/scoped_refptr.h"
Alessio Bazzicad2b97402018-08-09 14:23:11 +020017#include "modules/audio_processing/include/audio_processing.h"
Alessio Bazzicae4498052018-12-17 09:44:06 +010018#include "modules/audio_processing/test/echo_control_mock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_processing/test/test_utils.h"
Alessio Bazzicae4498052018-12-17 09:44:06 +010020#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/ref_counted_object.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:
Mirko Bonadei317a1f02019-09-17 17:06:18 +020054 MockEchoControlFactory() : next_mock_(std::make_unique<MockEchoControl>()) {}
Alessio Bazzicae4498052018-12-17 09:44:06 +010055 // Returns a pointer to the next MockEchoControl that this factory creates.
56 MockEchoControl* GetNext() const { return next_mock_.get(); }
Per Åhgren4e5c7092019-11-01 20:44:11 +010057 std::unique_ptr<EchoControl> Create(int sample_rate_hz,
58 int num_render_channels,
59 int num_capture_channels) override {
Alessio Bazzicae4498052018-12-17 09:44:06 +010060 std::unique_ptr<EchoControl> mock = std::move(next_mock_);
Mirko Bonadei317a1f02019-09-17 17:06:18 +020061 next_mock_ = std::make_unique<MockEchoControl>();
Alessio Bazzicae4498052018-12-17 09:44:06 +010062 return mock;
63 }
64
65 private:
66 std::unique_ptr<MockEchoControl> next_mock_;
67};
68
Alessio Bazzicad2b97402018-08-09 14:23:11 +020069// 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.
72class 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.
106class 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 Åhgrend47941e2019-08-22 11:51:13 +0200113 rtc::ArrayView<float> channel_view(audio->channels()[k],
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200114 audio->num_frames());
115 std::transform(channel_view.begin(), channel_view.end(),
116 channel_view.begin(), ProcessSample);
117 }
Mirko Bonadeic4dd7302019-02-25 09:12:02 +0100118 }
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200119 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
peaha9cc40b2017-06-29 08:32:09 -0700126} // namespace
127
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000128TEST(AudioProcessingImplTest, AudioParameterChangeTriggersInit) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100129 webrtc::Config webrtc_config;
130 MockInitialize mock(webrtc_config);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000131 ON_CALL(mock, InitializeLocked())
132 .WillByDefault(Invoke(&mock, &MockInitialize::RealInitializeLocked));
133
134 EXPECT_CALL(mock, InitializeLocked()).Times(1);
135 mock.Initialize();
136
Per Åhgren2507f8c2020-03-19 12:33:29 +0100137 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);
peah2ace3f92016-09-10 04:42:27 -0700142 // Call with the default parameters; there should be an init.
Per Åhgren4bdced52017-06-27 16:00:38 +0200143 EXPECT_CALL(mock, InitializeLocked()).Times(0);
Per Åhgrendc5522b2020-03-19 14:55:58 +0100144 EXPECT_NOERR(mock.ProcessStream(frame.data(), config, config, frame.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +0100145 EXPECT_NOERR(
146 mock.ProcessReverseStream(frame.data(), config, config, frame.data()));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000147
148 // New sample rate. (Only impacts ProcessStream).
Per Åhgren2507f8c2020-03-19 12:33:29 +0100149 config = StreamConfig(32000, 1, /*has_keyboard=*/false);
Yves Gerey665174f2018-06-19 15:03:05 +0200150 EXPECT_CALL(mock, InitializeLocked()).Times(1);
Per Åhgrendc5522b2020-03-19 14:55:58 +0100151 EXPECT_NOERR(mock.ProcessStream(frame.data(), config, config, frame.data()));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000152
153 // New number of channels.
peah2ace3f92016-09-10 04:42:27 -0700154 // TODO(peah): Investigate why this causes 2 inits.
Per Åhgren2507f8c2020-03-19 12:33:29 +0100155 config = StreamConfig(32000, 2, /*has_keyboard=*/false);
Yves Gerey665174f2018-06-19 15:03:05 +0200156 EXPECT_CALL(mock, InitializeLocked()).Times(2);
Per Åhgrendc5522b2020-03-19 14:55:58 +0100157 EXPECT_NOERR(mock.ProcessStream(frame.data(), config, config, frame.data()));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000158 // ProcessStream sets num_channels_ == num_output_channels.
Per Åhgren2507f8c2020-03-19 12:33:29 +0100159 EXPECT_NOERR(
160 mock.ProcessReverseStream(frame.data(), config, config, frame.data()));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000161
aluebsb0319552016-03-17 20:39:53 -0700162 // A new sample rate passed to ProcessReverseStream should cause an init.
Per Åhgren2507f8c2020-03-19 12:33:29 +0100163 config = StreamConfig(16000, 2, /*has_keyboard=*/false);
Alex Luebs5b830fe2016-03-08 17:52:52 +0100164 EXPECT_CALL(mock, InitializeLocked()).Times(1);
Per Åhgren2507f8c2020-03-19 12:33:29 +0100165 EXPECT_NOERR(
166 mock.ProcessReverseStream(frame.data(), config, config, frame.data()));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000167}
168
Alessio Bazzicac054e782018-04-16 12:10:09 +0200169TEST(AudioProcessingImplTest, UpdateCapturePreGainRuntimeSetting) {
Alex Loikob5c9a792018-04-16 16:31:22 +0200170 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
171 webrtc::AudioProcessing::Config apm_config;
172 apm_config.pre_amplifier.enabled = true;
173 apm_config.pre_amplifier.fixed_gain_factor = 1.f;
174 apm->ApplyConfig(apm_config);
175
Per Åhgren2507f8c2020-03-19 12:33:29 +0100176 constexpr int kSampleRateHz = 48000;
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200177 constexpr int16_t kAudioLevel = 10000;
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200178 constexpr size_t kNumChannels = 2;
Alex Loikob5c9a792018-04-16 16:31:22 +0200179
Per Åhgren2507f8c2020-03-19 12:33:29 +0100180 std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame;
181 StreamConfig config(kSampleRateHz, kNumChannels, /*has_keyboard=*/false);
182 frame.fill(kAudioLevel);
Per Åhgrendc5522b2020-03-19 14:55:58 +0100183 apm->ProcessStream(frame.data(), config, config, frame.data());
Per Åhgren2507f8c2020-03-19 12:33:29 +0100184 EXPECT_EQ(frame[100], kAudioLevel)
Alex Loikob5c9a792018-04-16 16:31:22 +0200185 << "With factor 1, frame shouldn't be modified.";
186
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200187 constexpr float kGainFactor = 2.f;
Alex Loikob5c9a792018-04-16 16:31:22 +0200188 apm->SetRuntimeSetting(
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200189 AudioProcessing::RuntimeSetting::CreateCapturePreGain(kGainFactor));
Alex Loikob5c9a792018-04-16 16:31:22 +0200190
191 // Process for two frames to have time to ramp up gain.
192 for (int i = 0; i < 2; ++i) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100193 frame.fill(kAudioLevel);
Per Åhgrendc5522b2020-03-19 14:55:58 +0100194 apm->ProcessStream(frame.data(), config, config, frame.data());
Alex Loikob5c9a792018-04-16 16:31:22 +0200195 }
Per Åhgren2507f8c2020-03-19 12:33:29 +0100196 EXPECT_EQ(frame[100], kGainFactor * kAudioLevel)
Alex Loikob5c9a792018-04-16 16:31:22 +0200197 << "Frame should be amplified.";
Alessio Bazzicac054e782018-04-16 12:10:09 +0200198}
199
Alessio Bazzicae4498052018-12-17 09:44:06 +0100200TEST(AudioProcessingImplTest,
201 EchoControllerObservesPreAmplifierEchoPathGainChange) {
202 // Tests that the echo controller observes an echo path gain change when the
203 // pre-amplifier submodule changes the gain.
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200204 auto echo_control_factory = std::make_unique<MockEchoControlFactory>();
Alessio Bazzicae4498052018-12-17 09:44:06 +0100205 const auto* echo_control_factory_ptr = echo_control_factory.get();
206
207 std::unique_ptr<AudioProcessing> apm(
208 AudioProcessingBuilder()
209 .SetEchoControlFactory(std::move(echo_control_factory))
210 .Create());
Sam Zackrisson41478c72019-10-15 10:10:26 +0200211 // Disable AGC.
Alessio Bazzicae4498052018-12-17 09:44:06 +0100212 webrtc::AudioProcessing::Config apm_config;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200213 apm_config.gain_controller1.enabled = false;
Alessio Bazzicae4498052018-12-17 09:44:06 +0100214 apm_config.gain_controller2.enabled = false;
215 apm_config.pre_amplifier.enabled = true;
216 apm_config.pre_amplifier.fixed_gain_factor = 1.f;
217 apm->ApplyConfig(apm_config);
218
Alessio Bazzicae4498052018-12-17 09:44:06 +0100219 constexpr int16_t kAudioLevel = 10000;
220 constexpr size_t kSampleRateHz = 48000;
221 constexpr size_t kNumChannels = 2;
Per Åhgren2507f8c2020-03-19 12:33:29 +0100222 std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame;
223 StreamConfig config(kSampleRateHz, kNumChannels, /*has_keyboard=*/false);
224 frame.fill(kAudioLevel);
Alessio Bazzicae4498052018-12-17 09:44:06 +0100225
226 MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext();
227
Per Åhgren0aefbf02019-08-23 21:29:17 +0200228 EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1);
Fredrik Hernqvistbf47f342019-05-09 10:50:31 +0200229 EXPECT_CALL(*echo_control_mock,
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100230 ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/false))
Fredrik Hernqvistbf47f342019-05-09 10:50:31 +0200231 .Times(1);
Per Åhgrendc5522b2020-03-19 14:55:58 +0100232 apm->ProcessStream(frame.data(), config, config, frame.data());
Alessio Bazzicae4498052018-12-17 09:44:06 +0100233
Per Åhgren0aefbf02019-08-23 21:29:17 +0200234 EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1);
Fredrik Hernqvistbf47f342019-05-09 10:50:31 +0200235 EXPECT_CALL(*echo_control_mock,
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100236 ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/true))
Fredrik Hernqvistbf47f342019-05-09 10:50:31 +0200237 .Times(1);
Alessio Bazzicae4498052018-12-17 09:44:06 +0100238 apm->SetRuntimeSetting(
239 AudioProcessing::RuntimeSetting::CreateCapturePreGain(2.f));
Per Åhgrendc5522b2020-03-19 14:55:58 +0100240 apm->ProcessStream(frame.data(), config, config, frame.data());
Alessio Bazzicae4498052018-12-17 09:44:06 +0100241}
242
243TEST(AudioProcessingImplTest,
244 EchoControllerObservesAnalogAgc1EchoPathGainChange) {
245 // Tests that the echo controller observes an echo path gain change when the
246 // AGC1 analog adaptive submodule changes the analog gain.
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200247 auto echo_control_factory = std::make_unique<MockEchoControlFactory>();
Alessio Bazzicae4498052018-12-17 09:44:06 +0100248 const auto* echo_control_factory_ptr = echo_control_factory.get();
249
250 std::unique_ptr<AudioProcessing> apm(
251 AudioProcessingBuilder()
252 .SetEchoControlFactory(std::move(echo_control_factory))
253 .Create());
Alessio Bazzicae4498052018-12-17 09:44:06 +0100254 webrtc::AudioProcessing::Config apm_config;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200255 // Enable AGC1.
256 apm_config.gain_controller1.enabled = true;
257 apm_config.gain_controller1.mode =
258 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
Alessio Bazzicae4498052018-12-17 09:44:06 +0100259 apm_config.gain_controller2.enabled = false;
260 apm_config.pre_amplifier.enabled = false;
261 apm->ApplyConfig(apm_config);
262
Alessio Bazzicae4498052018-12-17 09:44:06 +0100263 constexpr int16_t kAudioLevel = 1000;
264 constexpr size_t kSampleRateHz = 48000;
265 constexpr size_t kNumChannels = 2;
Per Åhgren2507f8c2020-03-19 12:33:29 +0100266 std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame;
267 StreamConfig stream_config(kSampleRateHz, kNumChannels,
268 /*has_keyboard=*/false);
269 frame.fill(kAudioLevel);
Alessio Bazzicae4498052018-12-17 09:44:06 +0100270
271 MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext();
272
Sam Zackrisson41478c72019-10-15 10:10:26 +0200273 const int initial_analog_gain = apm->recommended_stream_analog_level();
Per Åhgren0aefbf02019-08-23 21:29:17 +0200274 EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1);
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100275 EXPECT_CALL(*echo_control_mock, ProcessCapture(NotNull(), testing::_, false))
276 .Times(1);
Per Åhgrendc5522b2020-03-19 14:55:58 +0100277 apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data());
Alessio Bazzicae4498052018-12-17 09:44:06 +0100278
279 // Force an analog gain change if it did not happen.
Sam Zackrisson41478c72019-10-15 10:10:26 +0200280 if (initial_analog_gain == apm->recommended_stream_analog_level()) {
281 apm->set_stream_analog_level(initial_analog_gain + 1);
Alessio Bazzicae4498052018-12-17 09:44:06 +0100282 }
283
Per Åhgren0aefbf02019-08-23 21:29:17 +0200284 EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1);
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100285 EXPECT_CALL(*echo_control_mock, ProcessCapture(NotNull(), testing::_, true))
286 .Times(1);
Per Åhgrendc5522b2020-03-19 14:55:58 +0100287 apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data());
Alessio Bazzicae4498052018-12-17 09:44:06 +0100288}
289
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200290TEST(AudioProcessingImplTest, EchoControllerObservesPlayoutVolumeChange) {
291 // Tests that the echo controller observes an echo path gain change when a
292 // playout volume change is reported.
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200293 auto echo_control_factory = std::make_unique<MockEchoControlFactory>();
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200294 const auto* echo_control_factory_ptr = echo_control_factory.get();
295
296 std::unique_ptr<AudioProcessing> apm(
297 AudioProcessingBuilder()
298 .SetEchoControlFactory(std::move(echo_control_factory))
299 .Create());
Sam Zackrisson41478c72019-10-15 10:10:26 +0200300 // Disable AGC.
301 webrtc::AudioProcessing::Config apm_config;
302 apm_config.gain_controller1.enabled = false;
303 apm_config.gain_controller2.enabled = false;
304 apm->ApplyConfig(apm_config);
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200305
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200306 constexpr int16_t kAudioLevel = 10000;
307 constexpr size_t kSampleRateHz = 48000;
308 constexpr size_t kNumChannels = 2;
Per Åhgren2507f8c2020-03-19 12:33:29 +0100309 std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame;
310 StreamConfig stream_config(kSampleRateHz, kNumChannels,
311 /*has_keyboard=*/false);
312 frame.fill(kAudioLevel);
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200313
314 MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext();
315
Per Åhgren0aefbf02019-08-23 21:29:17 +0200316 EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1);
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200317 EXPECT_CALL(*echo_control_mock,
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100318 ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/false))
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200319 .Times(1);
Per Åhgrendc5522b2020-03-19 14:55:58 +0100320 apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data());
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200321
Per Åhgren0aefbf02019-08-23 21:29:17 +0200322 EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1);
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200323 EXPECT_CALL(*echo_control_mock,
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100324 ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/false))
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200325 .Times(1);
326 apm->SetRuntimeSetting(
327 AudioProcessing::RuntimeSetting::CreatePlayoutVolumeChange(50));
Per Åhgrendc5522b2020-03-19 14:55:58 +0100328 apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data());
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200329
Per Åhgren0aefbf02019-08-23 21:29:17 +0200330 EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1);
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200331 EXPECT_CALL(*echo_control_mock,
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100332 ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/false))
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200333 .Times(1);
334 apm->SetRuntimeSetting(
335 AudioProcessing::RuntimeSetting::CreatePlayoutVolumeChange(50));
Per Åhgrendc5522b2020-03-19 14:55:58 +0100336 apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data());
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200337
Per Åhgren0aefbf02019-08-23 21:29:17 +0200338 EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1);
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200339 EXPECT_CALL(*echo_control_mock,
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100340 ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/true))
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200341 .Times(1);
342 apm->SetRuntimeSetting(
343 AudioProcessing::RuntimeSetting::CreatePlayoutVolumeChange(100));
Per Åhgrendc5522b2020-03-19 14:55:58 +0100344 apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data());
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200345}
346
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200347TEST(AudioProcessingImplTest, RenderPreProcessorBeforeEchoDetector) {
348 // Make sure that signal changes caused by a render pre-processing sub-module
349 // take place before any echo detector analysis.
350 rtc::scoped_refptr<TestEchoDetector> test_echo_detector(
351 new rtc::RefCountedObject<TestEchoDetector>());
352 std::unique_ptr<CustomProcessing> test_render_pre_processor(
353 new TestRenderPreProcessor());
354 // Create APM injecting the test echo detector and render pre-processor.
355 std::unique_ptr<AudioProcessing> apm(
356 AudioProcessingBuilder()
357 .SetEchoDetector(test_echo_detector)
358 .SetRenderPreProcessing(std::move(test_render_pre_processor))
359 .Create());
360 webrtc::AudioProcessing::Config apm_config;
361 apm_config.pre_amplifier.enabled = true;
362 apm_config.residual_echo_detector.enabled = true;
363 apm->ApplyConfig(apm_config);
364
365 constexpr int16_t kAudioLevel = 1000;
366 constexpr int kSampleRateHz = 16000;
367 constexpr size_t kNumChannels = 1;
Sam Zackrisson12e319a2020-01-03 14:54:20 +0100368 // Explicitly initialize APM to ensure no render frames are discarded.
369 const ProcessingConfig processing_config = {{
370 {kSampleRateHz, kNumChannels, /*has_keyboard=*/false},
371 {kSampleRateHz, kNumChannels, /*has_keyboard=*/false},
372 {kSampleRateHz, kNumChannels, /*has_keyboard=*/false},
373 {kSampleRateHz, kNumChannels, /*has_keyboard=*/false},
374 }};
375 apm->Initialize(processing_config);
376
Per Åhgren2507f8c2020-03-19 12:33:29 +0100377 std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame;
378 StreamConfig stream_config(kSampleRateHz, kNumChannels,
379 /*has_keyboard=*/false);
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200380
381 constexpr float kAudioLevelFloat = static_cast<float>(kAudioLevel);
382 constexpr float kExpectedPreprocessedAudioLevel =
383 TestRenderPreProcessor::ProcessSample(kAudioLevelFloat);
384 ASSERT_NE(kAudioLevelFloat, kExpectedPreprocessedAudioLevel);
385
386 // Analyze a render stream frame.
Per Åhgren2507f8c2020-03-19 12:33:29 +0100387 frame.fill(kAudioLevel);
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200388 ASSERT_EQ(AudioProcessing::Error::kNoError,
Per Åhgren2507f8c2020-03-19 12:33:29 +0100389 apm->ProcessReverseStream(frame.data(), stream_config,
390 stream_config, frame.data()));
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200391 // Trigger a call to in EchoDetector::AnalyzeRenderAudio() via
392 // ProcessStream().
Per Åhgren2507f8c2020-03-19 12:33:29 +0100393 frame.fill(kAudioLevel);
394 ASSERT_EQ(AudioProcessing::Error::kNoError,
395 apm->ProcessStream(frame.data(), stream_config, stream_config,
Per Åhgrendc5522b2020-03-19 14:55:58 +0100396 frame.data()));
Alessio Bazzicad2b97402018-08-09 14:23:11 +0200397 // Regardless of how the call to in EchoDetector::AnalyzeRenderAudio() is
398 // triggered, the line below checks that the call has occurred. If not, the
399 // APM implementation may have changed and this test might need to be adapted.
400 ASSERT_TRUE(test_echo_detector->analyze_render_audio_called());
401 // Check that the data read in EchoDetector::AnalyzeRenderAudio() is that
402 // produced by the render pre-processor.
403 EXPECT_EQ(kExpectedPreprocessedAudioLevel,
404 test_echo_detector->last_render_audio_first_sample());
405}
406
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000407} // namespace webrtc