1 /*
2 * Copyright (c) 2017 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
11 #include "modules/audio_processing/aec3/echo_remover.h"
12
13 #include <algorithm>
14 #include <memory>
15 #include <numeric>
16 #include <string>
17
18 #include "modules/audio_processing/aec3/aec3_common.h"
19 #include "modules/audio_processing/aec3/render_buffer.h"
20 #include "modules/audio_processing/aec3/render_delay_buffer.h"
21 #include "modules/audio_processing/logging/apm_data_dumper.h"
22 #include "modules/audio_processing/test/echo_canceller_test_tools.h"
23 #include "rtc_base/random.h"
24 #include "rtc_base/strings/string_builder.h"
25 #include "test/gtest.h"
26
27 namespace webrtc {
28 namespace {
ProduceDebugText(int sample_rate_hz)29 std::string ProduceDebugText(int sample_rate_hz) {
30 rtc::StringBuilder ss;
31 ss << "Sample rate: " << sample_rate_hz;
32 return ss.Release();
33 }
34
ProduceDebugText(int sample_rate_hz,int delay)35 std::string ProduceDebugText(int sample_rate_hz, int delay) {
36 rtc::StringBuilder ss(ProduceDebugText(sample_rate_hz));
37 ss << ", Delay: " << delay;
38 return ss.Release();
39 }
40
41 } // namespace
42
43 class EchoRemoverMultiChannel
44 : public ::testing::Test,
45 public ::testing::WithParamInterface<std::tuple<size_t, size_t>> {};
46
47 INSTANTIATE_TEST_SUITE_P(MultiChannel,
48 EchoRemoverMultiChannel,
49 ::testing::Combine(::testing::Values(1, 2, 8),
50 ::testing::Values(1, 2, 8)));
51
52 // Verifies the basic API call sequence
TEST_P(EchoRemoverMultiChannel,BasicApiCalls)53 TEST_P(EchoRemoverMultiChannel, BasicApiCalls) {
54 const size_t num_render_channels = std::get<0>(GetParam());
55 const size_t num_capture_channels = std::get<1>(GetParam());
56 absl::optional<DelayEstimate> delay_estimate;
57 for (auto rate : {16000, 32000, 48000}) {
58 SCOPED_TRACE(ProduceDebugText(rate));
59 std::unique_ptr<EchoRemover> remover(
60 EchoRemover::Create(EchoCanceller3Config(), rate, num_render_channels,
61 num_capture_channels));
62 std::unique_ptr<RenderDelayBuffer> render_buffer(RenderDelayBuffer::Create(
63 EchoCanceller3Config(), rate, num_render_channels));
64
65 Block render(NumBandsForRate(rate), num_render_channels);
66 Block capture(NumBandsForRate(rate), num_capture_channels);
67 for (size_t k = 0; k < 100; ++k) {
68 EchoPathVariability echo_path_variability(
69 k % 3 == 0 ? true : false,
70 k % 5 == 0 ? EchoPathVariability::DelayAdjustment::kNewDetectedDelay
71 : EchoPathVariability::DelayAdjustment::kNone,
72 false);
73 render_buffer->Insert(render);
74 render_buffer->PrepareCaptureProcessing();
75
76 remover->ProcessCapture(echo_path_variability, k % 2 == 0 ? true : false,
77 delay_estimate, render_buffer->GetRenderBuffer(),
78 nullptr, &capture);
79 }
80 }
81 }
82
83 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
84
85 // Verifies the check for the samplerate.
86 // TODO(peah): Re-enable the test once the issue with memory leaks during DEATH
87 // tests on test bots has been fixed.
TEST(EchoRemoverDeathTest,DISABLED_WrongSampleRate)88 TEST(EchoRemoverDeathTest, DISABLED_WrongSampleRate) {
89 EXPECT_DEATH(std::unique_ptr<EchoRemover>(
90 EchoRemover::Create(EchoCanceller3Config(), 8001, 1, 1)),
91 "");
92 }
93
94 // Verifies the check for the number of capture bands.
95 // TODO(peah): Re-enable the test once the issue with memory leaks during DEATH
96 // tests on test bots has been fixed.c
TEST(EchoRemoverDeathTest,DISABLED_WrongCaptureNumBands)97 TEST(EchoRemoverDeathTest, DISABLED_WrongCaptureNumBands) {
98 absl::optional<DelayEstimate> delay_estimate;
99 for (auto rate : {16000, 32000, 48000}) {
100 SCOPED_TRACE(ProduceDebugText(rate));
101 std::unique_ptr<EchoRemover> remover(
102 EchoRemover::Create(EchoCanceller3Config(), rate, 1, 1));
103 std::unique_ptr<RenderDelayBuffer> render_buffer(
104 RenderDelayBuffer::Create(EchoCanceller3Config(), rate, 1));
105 Block capture(NumBandsForRate(rate == 48000 ? 16000 : rate + 16000), 1);
106 EchoPathVariability echo_path_variability(
107 false, EchoPathVariability::DelayAdjustment::kNone, false);
108 EXPECT_DEATH(remover->ProcessCapture(
109 echo_path_variability, false, delay_estimate,
110 render_buffer->GetRenderBuffer(), nullptr, &capture),
111 "");
112 }
113 }
114
115 // Verifies the check for non-null capture block.
TEST(EchoRemoverDeathTest,NullCapture)116 TEST(EchoRemoverDeathTest, NullCapture) {
117 absl::optional<DelayEstimate> delay_estimate;
118 std::unique_ptr<EchoRemover> remover(
119 EchoRemover::Create(EchoCanceller3Config(), 16000, 1, 1));
120 std::unique_ptr<RenderDelayBuffer> render_buffer(
121 RenderDelayBuffer::Create(EchoCanceller3Config(), 16000, 1));
122 EchoPathVariability echo_path_variability(
123 false, EchoPathVariability::DelayAdjustment::kNone, false);
124 EXPECT_DEATH(remover->ProcessCapture(
125 echo_path_variability, false, delay_estimate,
126 render_buffer->GetRenderBuffer(), nullptr, nullptr),
127 "");
128 }
129
130 #endif
131
132 // Performs a sanity check that the echo_remover is able to properly
133 // remove echoes.
TEST(EchoRemover,BasicEchoRemoval)134 TEST(EchoRemover, BasicEchoRemoval) {
135 constexpr int kNumBlocksToProcess = 500;
136 Random random_generator(42U);
137 absl::optional<DelayEstimate> delay_estimate;
138 for (size_t num_channels : {1, 2, 4}) {
139 for (auto rate : {16000, 32000, 48000}) {
140 Block x(NumBandsForRate(rate), num_channels);
141 Block y(NumBandsForRate(rate), num_channels);
142 EchoPathVariability echo_path_variability(
143 false, EchoPathVariability::DelayAdjustment::kNone, false);
144 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
145 SCOPED_TRACE(ProduceDebugText(rate, delay_samples));
146 EchoCanceller3Config config;
147 std::unique_ptr<EchoRemover> remover(
148 EchoRemover::Create(config, rate, num_channels, num_channels));
149 std::unique_ptr<RenderDelayBuffer> render_buffer(
150 RenderDelayBuffer::Create(config, rate, num_channels));
151 render_buffer->AlignFromDelay(delay_samples / kBlockSize);
152
153 std::vector<std::vector<std::unique_ptr<DelayBuffer<float>>>>
154 delay_buffers(x.NumBands());
155 for (size_t band = 0; band < delay_buffers.size(); ++band) {
156 delay_buffers[band].resize(x.NumChannels());
157 }
158
159 for (int band = 0; band < x.NumBands(); ++band) {
160 for (int channel = 0; channel < x.NumChannels(); ++channel) {
161 delay_buffers[band][channel].reset(
162 new DelayBuffer<float>(delay_samples));
163 }
164 }
165
166 float input_energy = 0.f;
167 float output_energy = 0.f;
168 for (int k = 0; k < kNumBlocksToProcess; ++k) {
169 const bool silence = k < 100 || (k % 100 >= 10);
170
171 for (int band = 0; band < x.NumBands(); ++band) {
172 for (int channel = 0; channel < x.NumChannels(); ++channel) {
173 if (silence) {
174 std::fill(x.begin(band, channel), x.end(band, channel), 0.f);
175 } else {
176 RandomizeSampleVector(&random_generator, x.View(band, channel));
177 }
178 delay_buffers[band][channel]->Delay(x.View(band, channel),
179 y.View(band, channel));
180 }
181 }
182
183 if (k > kNumBlocksToProcess / 2) {
184 input_energy = std::inner_product(
185 y.begin(/*band=*/0, /*channel=*/0),
186 y.end(/*band=*/0, /*channel=*/0),
187 y.begin(/*band=*/0, /*channel=*/0), input_energy);
188 }
189
190 render_buffer->Insert(x);
191 render_buffer->PrepareCaptureProcessing();
192
193 remover->ProcessCapture(echo_path_variability, false, delay_estimate,
194 render_buffer->GetRenderBuffer(), nullptr,
195 &y);
196
197 if (k > kNumBlocksToProcess / 2) {
198 output_energy = std::inner_product(
199 y.begin(/*band=*/0, /*channel=*/0),
200 y.end(/*band=*/0, /*channel=*/0),
201 y.begin(/*band=*/0, /*channel=*/0), output_energy);
202 }
203 }
204 EXPECT_GT(input_energy, 10.f * output_energy);
205 }
206 }
207 }
208 }
209
210 } // namespace webrtc
211