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" |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 18 | #include "modules/audio_processing/optionally_built_submodule_creators.h" |
Per Åhgren | cc73ed3 | 2020-04-26 23:56:17 +0200 | [diff] [blame] | 19 | #include "modules/audio_processing/test/audio_processing_builder_for_testing.h" |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 20 | #include "modules/audio_processing/test/echo_canceller_test_tools.h" |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 21 | #include "modules/audio_processing/test/echo_control_mock.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "modules/audio_processing/test/test_utils.h" |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 23 | #include "rtc_base/checks.h" |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 24 | #include "rtc_base/random.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 25 | #include "rtc_base/ref_counted_object.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "test/gmock.h" |
| 27 | #include "test/gtest.h" |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 28 | |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 29 | namespace webrtc { |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 30 | namespace { |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 31 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 32 | using ::testing::Invoke; |
| 33 | using ::testing::NotNull; |
| 34 | |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 35 | class MockInitialize : public AudioProcessingImpl { |
| 36 | public: |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 37 | explicit MockInitialize(const webrtc::Config& config) |
| 38 | : AudioProcessingImpl(config) {} |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 39 | |
Danil Chapovalov | 704fb55 | 2020-05-18 15:10:15 +0200 | [diff] [blame^] | 40 | MOCK_METHOD(int, InitializeLocked, (), (override)); |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 41 | int RealInitializeLocked() RTC_NO_THREAD_SAFETY_ANALYSIS { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 42 | return AudioProcessingImpl::InitializeLocked(); |
| 43 | } |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 44 | |
Danil Chapovalov | 704fb55 | 2020-05-18 15:10:15 +0200 | [diff] [blame^] | 45 | MOCK_METHOD(void, AddRef, (), (const, override)); |
| 46 | MOCK_METHOD(rtc::RefCountReleaseStatus, Release, (), (const, override)); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 49 | // Creates MockEchoControl instances and provides a raw pointer access to |
| 50 | // the next created one. The raw pointer is meant to be used with gmock. |
| 51 | // Returning a pointer of the next created MockEchoControl instance is necessary |
| 52 | // for the following reasons: (i) gmock expectations must be set before any call |
| 53 | // occurs, (ii) APM is initialized the first time that |
| 54 | // AudioProcessingImpl::ProcessStream() is called and the initialization leads |
| 55 | // to the creation of a new EchoControl object. |
| 56 | class MockEchoControlFactory : public EchoControlFactory { |
| 57 | public: |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 58 | MockEchoControlFactory() : next_mock_(std::make_unique<MockEchoControl>()) {} |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 59 | // Returns a pointer to the next MockEchoControl that this factory creates. |
| 60 | MockEchoControl* GetNext() const { return next_mock_.get(); } |
Per Åhgren | 4e5c709 | 2019-11-01 20:44:11 +0100 | [diff] [blame] | 61 | std::unique_ptr<EchoControl> Create(int sample_rate_hz, |
| 62 | int num_render_channels, |
| 63 | int num_capture_channels) override { |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 64 | std::unique_ptr<EchoControl> mock = std::move(next_mock_); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 65 | next_mock_ = std::make_unique<MockEchoControl>(); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 66 | return mock; |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | std::unique_ptr<MockEchoControl> next_mock_; |
| 71 | }; |
| 72 | |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 73 | // Mocks EchoDetector and records the first samples of the last analyzed render |
| 74 | // stream frame. Used to check what data is read by an EchoDetector |
| 75 | // implementation injected into an APM. |
| 76 | class TestEchoDetector : public EchoDetector { |
| 77 | public: |
| 78 | TestEchoDetector() |
| 79 | : analyze_render_audio_called_(false), |
| 80 | last_render_audio_first_sample_(0.f) {} |
| 81 | ~TestEchoDetector() override = default; |
| 82 | void AnalyzeRenderAudio(rtc::ArrayView<const float> render_audio) override { |
| 83 | last_render_audio_first_sample_ = render_audio[0]; |
| 84 | analyze_render_audio_called_ = true; |
| 85 | } |
| 86 | void AnalyzeCaptureAudio(rtc::ArrayView<const float> capture_audio) override { |
| 87 | } |
| 88 | void Initialize(int capture_sample_rate_hz, |
| 89 | int num_capture_channels, |
| 90 | int render_sample_rate_hz, |
| 91 | int num_render_channels) override {} |
| 92 | EchoDetector::Metrics GetMetrics() const override { return {}; } |
| 93 | // Returns true if AnalyzeRenderAudio() has been called at least once. |
| 94 | bool analyze_render_audio_called() const { |
| 95 | return analyze_render_audio_called_; |
| 96 | } |
| 97 | // Returns the first sample of the last analyzed render frame. |
| 98 | float last_render_audio_first_sample() const { |
| 99 | return last_render_audio_first_sample_; |
| 100 | } |
| 101 | |
| 102 | private: |
| 103 | bool analyze_render_audio_called_; |
| 104 | float last_render_audio_first_sample_; |
| 105 | }; |
| 106 | |
| 107 | // Mocks CustomProcessing and applies ProcessSample() to all the samples. |
| 108 | // Meant to be injected into an APM to modify samples in a known and detectable |
| 109 | // way. |
| 110 | class TestRenderPreProcessor : public CustomProcessing { |
| 111 | public: |
| 112 | TestRenderPreProcessor() = default; |
| 113 | ~TestRenderPreProcessor() = default; |
| 114 | void Initialize(int sample_rate_hz, int num_channels) override {} |
| 115 | void Process(AudioBuffer* audio) override { |
| 116 | for (size_t k = 0; k < audio->num_channels(); ++k) { |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 117 | rtc::ArrayView<float> channel_view(audio->channels()[k], |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 118 | audio->num_frames()); |
| 119 | std::transform(channel_view.begin(), channel_view.end(), |
| 120 | channel_view.begin(), ProcessSample); |
| 121 | } |
Mirko Bonadei | c4dd730 | 2019-02-25 09:12:02 +0100 | [diff] [blame] | 122 | } |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 123 | std::string ToString() const override { return "TestRenderPreProcessor"; } |
| 124 | void SetRuntimeSetting(AudioProcessing::RuntimeSetting setting) override {} |
| 125 | // Modifies a sample. This member is used in Process() to modify a frame and |
| 126 | // it is publicly visible to enable tests. |
| 127 | static constexpr float ProcessSample(float x) { return 2.f * x; } |
| 128 | }; |
| 129 | |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 130 | } // namespace |
| 131 | |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 132 | TEST(AudioProcessingImplTest, AudioParameterChangeTriggersInit) { |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 133 | webrtc::Config webrtc_config; |
| 134 | MockInitialize mock(webrtc_config); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 135 | ON_CALL(mock, InitializeLocked()) |
| 136 | .WillByDefault(Invoke(&mock, &MockInitialize::RealInitializeLocked)); |
| 137 | |
| 138 | EXPECT_CALL(mock, InitializeLocked()).Times(1); |
| 139 | mock.Initialize(); |
| 140 | |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 141 | constexpr size_t kMaxSampleRateHz = 32000; |
| 142 | constexpr size_t kMaxNumChannels = 2; |
| 143 | std::array<int16_t, kMaxNumChannels * kMaxSampleRateHz / 100> frame; |
| 144 | frame.fill(0); |
| 145 | StreamConfig config(16000, 1, /*has_keyboard=*/false); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 146 | // Call with the default parameters; there should be an init. |
Per Åhgren | 4bdced5 | 2017-06-27 16:00:38 +0200 | [diff] [blame] | 147 | EXPECT_CALL(mock, InitializeLocked()).Times(0); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 148 | EXPECT_NOERR(mock.ProcessStream(frame.data(), config, config, frame.data())); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 149 | EXPECT_NOERR( |
| 150 | mock.ProcessReverseStream(frame.data(), config, config, frame.data())); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 151 | |
| 152 | // New sample rate. (Only impacts ProcessStream). |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 153 | config = StreamConfig(32000, 1, /*has_keyboard=*/false); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 154 | EXPECT_CALL(mock, InitializeLocked()).Times(1); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 155 | EXPECT_NOERR(mock.ProcessStream(frame.data(), config, config, frame.data())); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 156 | |
| 157 | // New number of channels. |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 158 | // TODO(peah): Investigate why this causes 2 inits. |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 159 | config = StreamConfig(32000, 2, /*has_keyboard=*/false); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 160 | EXPECT_CALL(mock, InitializeLocked()).Times(2); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 161 | EXPECT_NOERR(mock.ProcessStream(frame.data(), config, config, frame.data())); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 162 | // ProcessStream sets num_channels_ == num_output_channels. |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 163 | EXPECT_NOERR( |
| 164 | mock.ProcessReverseStream(frame.data(), config, config, frame.data())); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 165 | |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 166 | // A new sample rate passed to ProcessReverseStream should cause an init. |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 167 | config = StreamConfig(16000, 2, /*has_keyboard=*/false); |
Alex Luebs | 5b830fe | 2016-03-08 17:52:52 +0100 | [diff] [blame] | 168 | EXPECT_CALL(mock, InitializeLocked()).Times(1); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 169 | EXPECT_NOERR( |
| 170 | mock.ProcessReverseStream(frame.data(), config, config, frame.data())); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 173 | TEST(AudioProcessingImplTest, UpdateCapturePreGainRuntimeSetting) { |
Per Åhgren | cc73ed3 | 2020-04-26 23:56:17 +0200 | [diff] [blame] | 174 | std::unique_ptr<AudioProcessing> apm( |
| 175 | AudioProcessingBuilderForTesting().Create()); |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 176 | webrtc::AudioProcessing::Config apm_config; |
| 177 | apm_config.pre_amplifier.enabled = true; |
| 178 | apm_config.pre_amplifier.fixed_gain_factor = 1.f; |
| 179 | apm->ApplyConfig(apm_config); |
| 180 | |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 181 | constexpr int kSampleRateHz = 48000; |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 182 | constexpr int16_t kAudioLevel = 10000; |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 183 | constexpr size_t kNumChannels = 2; |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 184 | |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 185 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
| 186 | StreamConfig config(kSampleRateHz, kNumChannels, /*has_keyboard=*/false); |
| 187 | frame.fill(kAudioLevel); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 188 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 189 | EXPECT_EQ(frame[100], kAudioLevel) |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 190 | << "With factor 1, frame shouldn't be modified."; |
| 191 | |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 192 | constexpr float kGainFactor = 2.f; |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 193 | apm->SetRuntimeSetting( |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 194 | AudioProcessing::RuntimeSetting::CreateCapturePreGain(kGainFactor)); |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 195 | |
| 196 | // Process for two frames to have time to ramp up gain. |
| 197 | for (int i = 0; i < 2; ++i) { |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 198 | frame.fill(kAudioLevel); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 199 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 200 | } |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 201 | EXPECT_EQ(frame[100], kGainFactor * kAudioLevel) |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 202 | << "Frame should be amplified."; |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 205 | TEST(AudioProcessingImplTest, |
| 206 | EchoControllerObservesPreAmplifierEchoPathGainChange) { |
| 207 | // Tests that the echo controller observes an echo path gain change when the |
| 208 | // pre-amplifier submodule changes the gain. |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 209 | auto echo_control_factory = std::make_unique<MockEchoControlFactory>(); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 210 | const auto* echo_control_factory_ptr = echo_control_factory.get(); |
| 211 | |
| 212 | std::unique_ptr<AudioProcessing> apm( |
Per Åhgren | cc73ed3 | 2020-04-26 23:56:17 +0200 | [diff] [blame] | 213 | AudioProcessingBuilderForTesting() |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 214 | .SetEchoControlFactory(std::move(echo_control_factory)) |
| 215 | .Create()); |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 216 | // Disable AGC. |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 217 | webrtc::AudioProcessing::Config apm_config; |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 218 | apm_config.gain_controller1.enabled = false; |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 219 | apm_config.gain_controller2.enabled = false; |
| 220 | apm_config.pre_amplifier.enabled = true; |
| 221 | apm_config.pre_amplifier.fixed_gain_factor = 1.f; |
| 222 | apm->ApplyConfig(apm_config); |
| 223 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 224 | constexpr int16_t kAudioLevel = 10000; |
| 225 | constexpr size_t kSampleRateHz = 48000; |
| 226 | constexpr size_t kNumChannels = 2; |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 227 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
| 228 | StreamConfig config(kSampleRateHz, kNumChannels, /*has_keyboard=*/false); |
| 229 | frame.fill(kAudioLevel); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 230 | |
| 231 | MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext(); |
| 232 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 233 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | bf47f34 | 2019-05-09 10:50:31 +0200 | [diff] [blame] | 234 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 235 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/false)) |
Fredrik Hernqvist | bf47f34 | 2019-05-09 10:50:31 +0200 | [diff] [blame] | 236 | .Times(1); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 237 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 238 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 239 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | bf47f34 | 2019-05-09 10:50:31 +0200 | [diff] [blame] | 240 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 241 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/true)) |
Fredrik Hernqvist | bf47f34 | 2019-05-09 10:50:31 +0200 | [diff] [blame] | 242 | .Times(1); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 243 | apm->SetRuntimeSetting( |
| 244 | AudioProcessing::RuntimeSetting::CreateCapturePreGain(2.f)); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 245 | apm->ProcessStream(frame.data(), config, config, frame.data()); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | TEST(AudioProcessingImplTest, |
| 249 | EchoControllerObservesAnalogAgc1EchoPathGainChange) { |
| 250 | // Tests that the echo controller observes an echo path gain change when the |
| 251 | // AGC1 analog adaptive submodule changes the analog gain. |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 252 | auto echo_control_factory = std::make_unique<MockEchoControlFactory>(); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 253 | const auto* echo_control_factory_ptr = echo_control_factory.get(); |
| 254 | |
| 255 | std::unique_ptr<AudioProcessing> apm( |
Per Åhgren | cc73ed3 | 2020-04-26 23:56:17 +0200 | [diff] [blame] | 256 | AudioProcessingBuilderForTesting() |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 257 | .SetEchoControlFactory(std::move(echo_control_factory)) |
| 258 | .Create()); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 259 | webrtc::AudioProcessing::Config apm_config; |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 260 | // Enable AGC1. |
| 261 | apm_config.gain_controller1.enabled = true; |
| 262 | apm_config.gain_controller1.mode = |
| 263 | AudioProcessing::Config::GainController1::kAdaptiveAnalog; |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 264 | apm_config.gain_controller2.enabled = false; |
| 265 | apm_config.pre_amplifier.enabled = false; |
| 266 | apm->ApplyConfig(apm_config); |
| 267 | |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 268 | constexpr int16_t kAudioLevel = 1000; |
| 269 | constexpr size_t kSampleRateHz = 48000; |
| 270 | constexpr size_t kNumChannels = 2; |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 271 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
| 272 | StreamConfig stream_config(kSampleRateHz, kNumChannels, |
| 273 | /*has_keyboard=*/false); |
| 274 | frame.fill(kAudioLevel); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 275 | |
| 276 | MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext(); |
| 277 | |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 278 | const int initial_analog_gain = apm->recommended_stream_analog_level(); |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 279 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 280 | EXPECT_CALL(*echo_control_mock, ProcessCapture(NotNull(), testing::_, false)) |
| 281 | .Times(1); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 282 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data()); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 283 | |
| 284 | // Force an analog gain change if it did not happen. |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 285 | if (initial_analog_gain == apm->recommended_stream_analog_level()) { |
| 286 | apm->set_stream_analog_level(initial_analog_gain + 1); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 287 | } |
| 288 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 289 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 290 | EXPECT_CALL(*echo_control_mock, ProcessCapture(NotNull(), testing::_, true)) |
| 291 | .Times(1); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 292 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data()); |
Alessio Bazzica | e449805 | 2018-12-17 09:44:06 +0100 | [diff] [blame] | 293 | } |
| 294 | |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 295 | TEST(AudioProcessingImplTest, EchoControllerObservesPlayoutVolumeChange) { |
| 296 | // Tests that the echo controller observes an echo path gain change when a |
| 297 | // playout volume change is reported. |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 298 | auto echo_control_factory = std::make_unique<MockEchoControlFactory>(); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 299 | const auto* echo_control_factory_ptr = echo_control_factory.get(); |
| 300 | |
| 301 | std::unique_ptr<AudioProcessing> apm( |
Per Åhgren | cc73ed3 | 2020-04-26 23:56:17 +0200 | [diff] [blame] | 302 | AudioProcessingBuilderForTesting() |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 303 | .SetEchoControlFactory(std::move(echo_control_factory)) |
| 304 | .Create()); |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 305 | // Disable AGC. |
| 306 | webrtc::AudioProcessing::Config apm_config; |
| 307 | apm_config.gain_controller1.enabled = false; |
| 308 | apm_config.gain_controller2.enabled = false; |
| 309 | apm->ApplyConfig(apm_config); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 310 | |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 311 | constexpr int16_t kAudioLevel = 10000; |
| 312 | constexpr size_t kSampleRateHz = 48000; |
| 313 | constexpr size_t kNumChannels = 2; |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 314 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
| 315 | StreamConfig stream_config(kSampleRateHz, kNumChannels, |
| 316 | /*has_keyboard=*/false); |
| 317 | frame.fill(kAudioLevel); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 318 | |
| 319 | MockEchoControl* echo_control_mock = echo_control_factory_ptr->GetNext(); |
| 320 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 321 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 322 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 323 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/false)) |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 324 | .Times(1); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 325 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data()); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 326 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 327 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 328 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 329 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/false)) |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 330 | .Times(1); |
| 331 | apm->SetRuntimeSetting( |
| 332 | AudioProcessing::RuntimeSetting::CreatePlayoutVolumeChange(50)); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 333 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data()); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 334 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 335 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 336 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 337 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/false)) |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 338 | .Times(1); |
| 339 | apm->SetRuntimeSetting( |
| 340 | AudioProcessing::RuntimeSetting::CreatePlayoutVolumeChange(50)); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 341 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data()); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 342 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 343 | EXPECT_CALL(*echo_control_mock, AnalyzeCapture(testing::_)).Times(1); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 344 | EXPECT_CALL(*echo_control_mock, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 345 | ProcessCapture(NotNull(), testing::_, /*echo_path_change=*/true)) |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 346 | .Times(1); |
| 347 | apm->SetRuntimeSetting( |
| 348 | AudioProcessing::RuntimeSetting::CreatePlayoutVolumeChange(100)); |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 349 | apm->ProcessStream(frame.data(), stream_config, stream_config, frame.data()); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 350 | } |
| 351 | |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 352 | TEST(AudioProcessingImplTest, RenderPreProcessorBeforeEchoDetector) { |
| 353 | // Make sure that signal changes caused by a render pre-processing sub-module |
| 354 | // take place before any echo detector analysis. |
| 355 | rtc::scoped_refptr<TestEchoDetector> test_echo_detector( |
| 356 | new rtc::RefCountedObject<TestEchoDetector>()); |
| 357 | std::unique_ptr<CustomProcessing> test_render_pre_processor( |
| 358 | new TestRenderPreProcessor()); |
| 359 | // Create APM injecting the test echo detector and render pre-processor. |
| 360 | std::unique_ptr<AudioProcessing> apm( |
Per Åhgren | cc73ed3 | 2020-04-26 23:56:17 +0200 | [diff] [blame] | 361 | AudioProcessingBuilderForTesting() |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 362 | .SetEchoDetector(test_echo_detector) |
| 363 | .SetRenderPreProcessing(std::move(test_render_pre_processor)) |
| 364 | .Create()); |
| 365 | webrtc::AudioProcessing::Config apm_config; |
| 366 | apm_config.pre_amplifier.enabled = true; |
| 367 | apm_config.residual_echo_detector.enabled = true; |
| 368 | apm->ApplyConfig(apm_config); |
| 369 | |
| 370 | constexpr int16_t kAudioLevel = 1000; |
| 371 | constexpr int kSampleRateHz = 16000; |
| 372 | constexpr size_t kNumChannels = 1; |
Sam Zackrisson | 12e319a | 2020-01-03 14:54:20 +0100 | [diff] [blame] | 373 | // Explicitly initialize APM to ensure no render frames are discarded. |
| 374 | const ProcessingConfig processing_config = {{ |
| 375 | {kSampleRateHz, kNumChannels, /*has_keyboard=*/false}, |
| 376 | {kSampleRateHz, kNumChannels, /*has_keyboard=*/false}, |
| 377 | {kSampleRateHz, kNumChannels, /*has_keyboard=*/false}, |
| 378 | {kSampleRateHz, kNumChannels, /*has_keyboard=*/false}, |
| 379 | }}; |
| 380 | apm->Initialize(processing_config); |
| 381 | |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 382 | std::array<int16_t, kNumChannels * kSampleRateHz / 100> frame; |
| 383 | StreamConfig stream_config(kSampleRateHz, kNumChannels, |
| 384 | /*has_keyboard=*/false); |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 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. |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 392 | frame.fill(kAudioLevel); |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 393 | ASSERT_EQ(AudioProcessing::Error::kNoError, |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 394 | apm->ProcessReverseStream(frame.data(), stream_config, |
| 395 | stream_config, frame.data())); |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 396 | // Trigger a call to in EchoDetector::AnalyzeRenderAudio() via |
| 397 | // ProcessStream(). |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 398 | frame.fill(kAudioLevel); |
| 399 | ASSERT_EQ(AudioProcessing::Error::kNoError, |
| 400 | apm->ProcessStream(frame.data(), stream_config, stream_config, |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 401 | frame.data())); |
Alessio Bazzica | d2b9740 | 2018-08-09 14:23:11 +0200 | [diff] [blame] | 402 | // Regardless of how the call to in EchoDetector::AnalyzeRenderAudio() is |
| 403 | // triggered, the line below checks that the call has occurred. If not, the |
| 404 | // APM implementation may have changed and this test might need to be adapted. |
| 405 | ASSERT_TRUE(test_echo_detector->analyze_render_audio_called()); |
| 406 | // Check that the data read in EchoDetector::AnalyzeRenderAudio() is that |
| 407 | // produced by the render pre-processor. |
| 408 | EXPECT_EQ(kExpectedPreprocessedAudioLevel, |
| 409 | test_echo_detector->last_render_audio_first_sample()); |
| 410 | } |
| 411 | |
Sam Zackrisson | b37e59d | 2020-04-27 08:39:33 +0200 | [diff] [blame] | 412 | // Disabling build-optional submodules and trying to enable them via the APM |
| 413 | // config should be bit-exact with running APM with said submodules disabled. |
| 414 | // This mainly tests that SetCreateOptionalSubmodulesForTesting has an effect. |
| 415 | TEST(ApmWithSubmodulesExcludedTest, BitexactWithDisabledModules) { |
| 416 | rtc::scoped_refptr<AudioProcessingImpl> apm = |
| 417 | new rtc::RefCountedObject<AudioProcessingImpl>(webrtc::Config()); |
| 418 | ASSERT_EQ(apm->Initialize(), AudioProcessing::kNoError); |
| 419 | |
| 420 | ApmSubmoduleCreationOverrides overrides; |
| 421 | overrides.transient_suppression = true; |
| 422 | apm->OverrideSubmoduleCreationForTesting(overrides); |
| 423 | |
| 424 | AudioProcessing::Config apm_config = apm->GetConfig(); |
| 425 | apm_config.transient_suppression.enabled = true; |
| 426 | apm->ApplyConfig(apm_config); |
| 427 | |
| 428 | rtc::scoped_refptr<AudioProcessing> apm_reference = |
| 429 | AudioProcessingBuilder().Create(); |
| 430 | apm_config = apm_reference->GetConfig(); |
| 431 | apm_config.transient_suppression.enabled = false; |
| 432 | apm_reference->ApplyConfig(apm_config); |
| 433 | |
| 434 | constexpr int kSampleRateHz = 16000; |
| 435 | constexpr int kNumChannels = 1; |
| 436 | std::array<float, kSampleRateHz / 100> buffer; |
| 437 | std::array<float, kSampleRateHz / 100> buffer_reference; |
| 438 | float* channel_pointers[] = {buffer.data()}; |
| 439 | float* channel_pointers_reference[] = {buffer_reference.data()}; |
| 440 | StreamConfig stream_config(/*sample_rate_hz=*/kSampleRateHz, |
| 441 | /*num_channels=*/kNumChannels, |
| 442 | /*has_keyboard=*/false); |
| 443 | Random random_generator(2341U); |
| 444 | constexpr int kFramesToProcessPerConfiguration = 10; |
| 445 | |
| 446 | for (int i = 0; i < kFramesToProcessPerConfiguration; ++i) { |
| 447 | RandomizeSampleVector(&random_generator, buffer); |
| 448 | std::copy(buffer.begin(), buffer.end(), buffer_reference.begin()); |
| 449 | ASSERT_EQ(apm->ProcessStream(channel_pointers, stream_config, stream_config, |
| 450 | channel_pointers), |
| 451 | kNoErr); |
| 452 | ASSERT_EQ( |
| 453 | apm_reference->ProcessStream(channel_pointers_reference, stream_config, |
| 454 | stream_config, channel_pointers_reference), |
| 455 | kNoErr); |
| 456 | for (int j = 0; j < kSampleRateHz / 100; ++j) { |
| 457 | EXPECT_EQ(buffer[j], buffer_reference[j]); |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | // Disable transient suppressor creation and run APM in ways that should trigger |
| 463 | // calls to the transient suppressor API. |
| 464 | TEST(ApmWithSubmodulesExcludedTest, ReinitializeTransientSuppressor) { |
| 465 | rtc::scoped_refptr<AudioProcessingImpl> apm = |
| 466 | new rtc::RefCountedObject<AudioProcessingImpl>(webrtc::Config()); |
| 467 | ASSERT_EQ(apm->Initialize(), kNoErr); |
| 468 | |
| 469 | ApmSubmoduleCreationOverrides overrides; |
| 470 | overrides.transient_suppression = true; |
| 471 | apm->OverrideSubmoduleCreationForTesting(overrides); |
| 472 | |
| 473 | AudioProcessing::Config config = apm->GetConfig(); |
| 474 | config.transient_suppression.enabled = true; |
| 475 | apm->ApplyConfig(config); |
| 476 | // 960 samples per frame: 10 ms of <= 48 kHz audio with <= 2 channels. |
| 477 | float buffer[960]; |
| 478 | float* channel_pointers[] = {&buffer[0], &buffer[480]}; |
| 479 | Random random_generator(2341U); |
| 480 | constexpr int kFramesToProcessPerConfiguration = 3; |
| 481 | |
| 482 | StreamConfig initial_stream_config(/*sample_rate_hz=*/16000, |
| 483 | /*num_channels=*/1, |
| 484 | /*has_keyboard=*/false); |
| 485 | for (int i = 0; i < kFramesToProcessPerConfiguration; ++i) { |
| 486 | RandomizeSampleVector(&random_generator, buffer); |
| 487 | EXPECT_EQ(apm->ProcessStream(channel_pointers, initial_stream_config, |
| 488 | initial_stream_config, channel_pointers), |
| 489 | kNoErr); |
| 490 | } |
| 491 | |
| 492 | StreamConfig stereo_stream_config(/*sample_rate_hz=*/16000, |
| 493 | /*num_channels=*/2, |
| 494 | /*has_keyboard=*/false); |
| 495 | for (int i = 0; i < kFramesToProcessPerConfiguration; ++i) { |
| 496 | RandomizeSampleVector(&random_generator, buffer); |
| 497 | EXPECT_EQ(apm->ProcessStream(channel_pointers, stereo_stream_config, |
| 498 | stereo_stream_config, channel_pointers), |
| 499 | kNoErr); |
| 500 | } |
| 501 | |
| 502 | StreamConfig high_sample_rate_stream_config(/*sample_rate_hz=*/48000, |
| 503 | /*num_channels=*/1, |
| 504 | /*has_keyboard=*/false); |
| 505 | for (int i = 0; i < kFramesToProcessPerConfiguration; ++i) { |
| 506 | RandomizeSampleVector(&random_generator, buffer); |
| 507 | EXPECT_EQ( |
| 508 | apm->ProcessStream(channel_pointers, high_sample_rate_stream_config, |
| 509 | high_sample_rate_stream_config, channel_pointers), |
| 510 | kNoErr); |
| 511 | } |
| 512 | |
| 513 | StreamConfig keyboard_stream_config(/*sample_rate_hz=*/16000, |
| 514 | /*num_channels=*/1, |
| 515 | /*has_keyboard=*/true); |
| 516 | for (int i = 0; i < kFramesToProcessPerConfiguration; ++i) { |
| 517 | RandomizeSampleVector(&random_generator, buffer); |
| 518 | EXPECT_EQ(apm->ProcessStream(channel_pointers, keyboard_stream_config, |
| 519 | keyboard_stream_config, channel_pointers), |
| 520 | kNoErr); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | // Disable transient suppressor creation and run APM in ways that should trigger |
| 525 | // calls to the transient suppressor API. |
| 526 | TEST(ApmWithSubmodulesExcludedTest, ToggleTransientSuppressor) { |
| 527 | rtc::scoped_refptr<AudioProcessingImpl> apm = |
| 528 | new rtc::RefCountedObject<AudioProcessingImpl>(webrtc::Config()); |
| 529 | ASSERT_EQ(apm->Initialize(), AudioProcessing::kNoError); |
| 530 | |
| 531 | ApmSubmoduleCreationOverrides overrides; |
| 532 | overrides.transient_suppression = true; |
| 533 | apm->OverrideSubmoduleCreationForTesting(overrides); |
| 534 | |
| 535 | // 960 samples per frame: 10 ms of <= 48 kHz audio with <= 2 channels. |
| 536 | float buffer[960]; |
| 537 | float* channel_pointers[] = {&buffer[0], &buffer[480]}; |
| 538 | Random random_generator(2341U); |
| 539 | constexpr int kFramesToProcessPerConfiguration = 3; |
| 540 | StreamConfig stream_config(/*sample_rate_hz=*/16000, |
| 541 | /*num_channels=*/1, |
| 542 | /*has_keyboard=*/false); |
| 543 | |
| 544 | AudioProcessing::Config config = apm->GetConfig(); |
| 545 | config.transient_suppression.enabled = true; |
| 546 | apm->ApplyConfig(config); |
| 547 | for (int i = 0; i < kFramesToProcessPerConfiguration; ++i) { |
| 548 | RandomizeSampleVector(&random_generator, buffer); |
| 549 | EXPECT_EQ(apm->ProcessStream(channel_pointers, stream_config, stream_config, |
| 550 | channel_pointers), |
| 551 | kNoErr); |
| 552 | } |
| 553 | |
| 554 | config = apm->GetConfig(); |
| 555 | config.transient_suppression.enabled = false; |
| 556 | apm->ApplyConfig(config); |
| 557 | for (int i = 0; i < kFramesToProcessPerConfiguration; ++i) { |
| 558 | RandomizeSampleVector(&random_generator, buffer); |
| 559 | EXPECT_EQ(apm->ProcessStream(channel_pointers, stream_config, stream_config, |
| 560 | channel_pointers), |
| 561 | kNoErr); |
| 562 | } |
| 563 | |
| 564 | config = apm->GetConfig(); |
| 565 | config.transient_suppression.enabled = true; |
| 566 | apm->ApplyConfig(config); |
| 567 | for (int i = 0; i < kFramesToProcessPerConfiguration; ++i) { |
| 568 | RandomizeSampleVector(&random_generator, buffer); |
| 569 | EXPECT_EQ(apm->ProcessStream(channel_pointers, stream_config, stream_config, |
| 570 | channel_pointers), |
| 571 | kNoErr); |
| 572 | } |
| 573 | } |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 574 | } // namespace webrtc |