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 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
| 15 | #include "absl/memory/memory.h" |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 16 | #include "api/scoped_refptr.h" |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 17 | #include "modules/audio_processing/include/audio_processing.h" |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 18 | #include "modules/audio_processing/test/echo_control_mock.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "modules/audio_processing/test/test_utils.h" |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 20 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "rtc_base/ref_counted_object.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "test/gmock.h" |
| 23 | #include "test/gtest.h" |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 24 | |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 25 | namespace webrtc { |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 26 | namespace { |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 27 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 28 | using ::testing::Invoke; |
| 29 | using ::testing::NotNull; |
| 30 | |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 31 | class MockInitialize : public AudioProcessingImpl { |
| 32 | public: |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 33 | explicit MockInitialize(const webrtc::Config& config) |
| 34 | : AudioProcessingImpl(config) {} |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 35 | |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 36 | MOCK_METHOD0(InitializeLocked, int()); |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 37 | int RealInitializeLocked() RTC_NO_THREAD_SAFETY_ANALYSIS { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 38 | return AudioProcessingImpl::InitializeLocked(); |
| 39 | } |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 40 | |
Niels Möller | 6f72f56 | 2017-10-19 13:15:17 +0200 | [diff] [blame] | 41 | MOCK_CONST_METHOD0(AddRef, void()); |
| 42 | MOCK_CONST_METHOD0(Release, rtc::RefCountReleaseStatus()); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 45 | // Creates MockEchoControl instances and provides a raw pointer access to |
| 46 | // the next created one. The raw pointer is meant to be used with gmock. |
| 47 | // Returning a pointer of the next created MockEchoControl instance is necessary |
| 48 | // for the following reasons: (i) gmock expectations must be set before any call |
| 49 | // occurs, (ii) APM is initialized the first time that |
| 50 | // AudioProcessingImpl::ProcessStream() is called and the initialization leads |
| 51 | // to the creation of a new EchoControl object. |
| 52 | class MockEchoControlFactory : public EchoControlFactory { |
| 53 | public: |
| 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 Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 67 | void InitializeAudioFrame(size_t input_rate, |
| 68 | size_t num_channels, |
| 69 | AudioFrame* frame) { |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 70 | const size_t samples_per_input_channel = rtc::CheckedDivExact( |
| 71 | input_rate, static_cast<size_t>(rtc::CheckedDivExact( |
| 72 | 1000, AudioProcessing::kChunkSizeMs))); |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 73 | RTC_DCHECK_LE(samples_per_input_channel * num_channels, |
| 74 | AudioFrame::kMaxDataSizeSamples); |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 75 | frame->samples_per_channel_ = samples_per_input_channel; |
| 76 | frame->sample_rate_hz_ = input_rate; |
| 77 | frame->num_channels_ = num_channels; |
| 78 | } |
| 79 | |
| 80 | void 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 Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 87 | // 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. |
| 90 | class 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. |
| 124 | class 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 | } |
Mirko Bonadei | c4dd730 | 2019-02-25 09:12:02 +0100 | [diff] [blame^] | 136 | } |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 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 | |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 144 | } // namespace |
| 145 | |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 146 | TEST(AudioProcessingImplTest, AudioParameterChangeTriggersInit) { |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 147 | webrtc::Config config; |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 148 | MockInitialize mock(config); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 149 | 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; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 156 | // Call with the default parameters; there should be an init. |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 157 | frame.num_channels_ = 1; |
| 158 | SetFrameSampleRate(&frame, 16000); |
Per Ã…hgren | 4bdced5 | 2017-06-27 16:00:38 +0200 | [diff] [blame] | 159 | EXPECT_CALL(mock, InitializeLocked()).Times(0); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 160 | EXPECT_NOERR(mock.ProcessStream(&frame)); |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 161 | EXPECT_NOERR(mock.ProcessReverseStream(&frame)); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 162 | |
| 163 | // New sample rate. (Only impacts ProcessStream). |
| 164 | SetFrameSampleRate(&frame, 32000); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 165 | EXPECT_CALL(mock, InitializeLocked()).Times(1); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 166 | EXPECT_NOERR(mock.ProcessStream(&frame)); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 167 | |
| 168 | // New number of channels. |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 169 | // TODO(peah): Investigate why this causes 2 inits. |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 170 | frame.num_channels_ = 2; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 171 | EXPECT_CALL(mock, InitializeLocked()).Times(2); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 172 | EXPECT_NOERR(mock.ProcessStream(&frame)); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 173 | // ProcessStream sets num_channels_ == num_output_channels. |
| 174 | frame.num_channels_ = 2; |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 175 | EXPECT_NOERR(mock.ProcessReverseStream(&frame)); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 176 | |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 177 | // A new sample rate passed to ProcessReverseStream should cause an init. |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 178 | SetFrameSampleRate(&frame, 16000); |
Alex Luebs | 5b830fe | 2016-03-08 17:52:52 +0100 | [diff] [blame] | 179 | EXPECT_CALL(mock, InitializeLocked()).Times(1); |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 180 | EXPECT_NOERR(mock.ProcessReverseStream(&frame)); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 183 | TEST(AudioProcessingImplTest, UpdateCapturePreGainRuntimeSetting) { |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 184 | 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 Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 191 | constexpr int16_t kAudioLevel = 10000; |
| 192 | constexpr size_t kSampleRateHz = 48000; |
| 193 | constexpr size_t kNumChannels = 2; |
| 194 | InitializeAudioFrame(kSampleRateHz, kNumChannels, &frame); |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 195 | |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 196 | FillFixedFrame(kAudioLevel, &frame); |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 197 | apm->ProcessStream(&frame); |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 198 | EXPECT_EQ(frame.data()[100], kAudioLevel) |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 199 | << "With factor 1, frame shouldn't be modified."; |
| 200 | |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 201 | constexpr float kGainFactor = 2.f; |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 202 | apm->SetRuntimeSetting( |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 203 | AudioProcessing::RuntimeSetting::CreateCapturePreGain(kGainFactor)); |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 204 | |
| 205 | // Process for two frames to have time to ramp up gain. |
| 206 | for (int i = 0; i < 2; ++i) { |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 207 | FillFixedFrame(kAudioLevel, &frame); |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 208 | apm->ProcessStream(&frame); |
| 209 | } |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 210 | EXPECT_EQ(frame.data()[100], kGainFactor * kAudioLevel) |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 211 | << "Frame should be amplified."; |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 214 | TEST(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 | |
| 253 | TEST(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 Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 295 | TEST(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.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 342 | } // namespace webrtc |