OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/audio_processing/aec3/echo_remover.h" | 11 #include "webrtc/modules/audio_processing/aec3/echo_remover.h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include <memory> | 14 #include <memory> |
15 #include <numeric> | 15 #include <numeric> |
16 #include <sstream> | 16 #include <sstream> |
17 #include <string> | 17 #include <string> |
18 | 18 |
19 #include "webrtc/base/random.h" | 19 #include "webrtc/base/random.h" |
20 #include "webrtc/modules/audio_processing/aec3/aec3_common.h" | 20 #include "webrtc/modules/audio_processing/aec3/aec3_common.h" |
| 21 #include "webrtc/modules/audio_processing/aec3/render_buffer.h" |
| 22 #include "webrtc/modules/audio_processing/aec3/render_delay_buffer.h" |
21 #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h" | 23 #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h" |
22 #include "webrtc/modules/audio_processing/test/echo_canceller_test_tools.h" | 24 #include "webrtc/modules/audio_processing/test/echo_canceller_test_tools.h" |
23 #include "webrtc/test/gtest.h" | 25 #include "webrtc/test/gtest.h" |
24 | 26 |
25 namespace webrtc { | 27 namespace webrtc { |
26 namespace { | 28 namespace { |
27 | 29 |
28 std::string ProduceDebugText(int sample_rate_hz) { | 30 std::string ProduceDebugText(int sample_rate_hz) { |
29 std::ostringstream ss; | 31 std::ostringstream ss; |
30 ss << "Sample rate: " << sample_rate_hz; | 32 ss << "Sample rate: " << sample_rate_hz; |
31 return ss.str(); | 33 return ss.str(); |
32 } | 34 } |
33 | 35 |
34 std::string ProduceDebugText(int sample_rate_hz, int delay) { | 36 std::string ProduceDebugText(int sample_rate_hz, int delay) { |
35 std::ostringstream ss(ProduceDebugText(sample_rate_hz)); | 37 std::ostringstream ss(ProduceDebugText(sample_rate_hz)); |
36 ss << ", Delay: " << delay; | 38 ss << ", Delay: " << delay; |
37 return ss.str(); | 39 return ss.str(); |
38 } | 40 } |
39 | 41 |
40 } // namespace | 42 } // namespace |
41 | 43 |
42 // Verifies the basic API call sequence | 44 // Verifies the basic API call sequence |
43 TEST(EchoRemover, BasicApiCalls) { | 45 TEST(EchoRemover, BasicApiCalls) { |
44 for (auto rate : {8000, 16000, 32000, 48000}) { | 46 for (auto rate : {8000, 16000, 32000, 48000}) { |
45 SCOPED_TRACE(ProduceDebugText(rate)); | 47 SCOPED_TRACE(ProduceDebugText(rate)); |
46 std::unique_ptr<EchoRemover> remover(EchoRemover::Create(rate)); | 48 std::unique_ptr<EchoRemover> remover(EchoRemover::Create(rate)); |
| 49 std::unique_ptr<RenderDelayBuffer> render_buffer( |
| 50 RenderDelayBuffer::Create(NumBandsForRate(rate))); |
47 | 51 |
48 std::vector<std::vector<float>> render(NumBandsForRate(rate), | 52 std::vector<std::vector<float>> render(NumBandsForRate(rate), |
49 std::vector<float>(kBlockSize, 0.f)); | 53 std::vector<float>(kBlockSize, 0.f)); |
50 std::vector<std::vector<float>> capture( | 54 std::vector<std::vector<float>> capture( |
51 NumBandsForRate(rate), std::vector<float>(kBlockSize, 0.f)); | 55 NumBandsForRate(rate), std::vector<float>(kBlockSize, 0.f)); |
52 for (size_t k = 0; k < 100; ++k) { | 56 for (size_t k = 0; k < 100; ++k) { |
53 EchoPathVariability echo_path_variability(k % 3 == 0 ? true : false, | 57 EchoPathVariability echo_path_variability(k % 3 == 0 ? true : false, |
54 k % 5 == 0 ? true : false); | 58 k % 5 == 0 ? true : false); |
55 rtc::Optional<size_t> echo_path_delay_samples = | 59 rtc::Optional<size_t> echo_path_delay_samples = |
56 (k % 6 == 0 ? rtc::Optional<size_t>(k * 10) | 60 (k % 6 == 0 ? rtc::Optional<size_t>(k * 10) |
57 : rtc::Optional<size_t>()); | 61 : rtc::Optional<size_t>()); |
58 remover->ProcessBlock(echo_path_delay_samples, echo_path_variability, | 62 render_buffer->Insert(&render); |
59 k % 2 == 0 ? true : false, render, &capture); | 63 render_buffer->UpdateBuffers(); |
60 remover->UpdateEchoLeakageStatus(k % 7 == 0 ? true : false); | 64 remover->ProcessCapture(echo_path_delay_samples, echo_path_variability, |
| 65 k % 2 == 0 ? true : false, |
| 66 render_buffer->GetRenderBuffer(), &capture); |
61 } | 67 } |
62 } | 68 } |
63 } | 69 } |
64 | 70 |
65 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) | 71 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
66 | 72 |
67 // Verifies the check for the samplerate. | 73 // Verifies the check for the samplerate. |
68 // TODO(peah): Re-enable the test once the issue with memory leaks during DEATH | 74 // TODO(peah): Re-enable the test once the issue with memory leaks during DEATH |
69 // tests on test bots has been fixed. | 75 // tests on test bots has been fixed. |
70 TEST(EchoRemover, DISABLED_WrongSampleRate) { | 76 TEST(EchoRemover, DISABLED_WrongSampleRate) { |
71 EXPECT_DEATH(std::unique_ptr<EchoRemover>(EchoRemover::Create(8001)), ""); | 77 EXPECT_DEATH(std::unique_ptr<EchoRemover>(EchoRemover::Create(8001)), ""); |
72 } | 78 } |
73 | 79 |
74 // Verifies the check for the render block size. | |
75 TEST(EchoRemover, WrongRenderBlockSize) { | |
76 for (auto rate : {8000, 16000, 32000, 48000}) { | |
77 SCOPED_TRACE(ProduceDebugText(rate)); | |
78 std::unique_ptr<EchoRemover> remover(EchoRemover::Create(rate)); | |
79 | |
80 std::vector<std::vector<float>> render( | |
81 NumBandsForRate(rate), std::vector<float>(kBlockSize - 1, 0.f)); | |
82 std::vector<std::vector<float>> capture( | |
83 NumBandsForRate(rate), std::vector<float>(kBlockSize, 0.f)); | |
84 EchoPathVariability echo_path_variability(false, false); | |
85 rtc::Optional<size_t> echo_path_delay_samples; | |
86 EXPECT_DEATH( | |
87 remover->ProcessBlock(echo_path_delay_samples, echo_path_variability, | |
88 false, render, &capture), | |
89 ""); | |
90 } | |
91 } | |
92 | |
93 // Verifies the check for the capture block size. | 80 // Verifies the check for the capture block size. |
94 TEST(EchoRemover, WrongCaptureBlockSize) { | 81 TEST(EchoRemover, WrongCaptureBlockSize) { |
95 for (auto rate : {8000, 16000, 32000, 48000}) { | 82 for (auto rate : {8000, 16000, 32000, 48000}) { |
96 SCOPED_TRACE(ProduceDebugText(rate)); | 83 SCOPED_TRACE(ProduceDebugText(rate)); |
97 std::unique_ptr<EchoRemover> remover(EchoRemover::Create(rate)); | 84 std::unique_ptr<EchoRemover> remover(EchoRemover::Create(rate)); |
98 | 85 std::unique_ptr<RenderDelayBuffer> render_buffer( |
99 std::vector<std::vector<float>> render(NumBandsForRate(rate), | 86 RenderDelayBuffer::Create(NumBandsForRate(rate))); |
100 std::vector<float>(kBlockSize, 0.f)); | |
101 std::vector<std::vector<float>> capture( | 87 std::vector<std::vector<float>> capture( |
102 NumBandsForRate(rate), std::vector<float>(kBlockSize - 1, 0.f)); | 88 NumBandsForRate(rate), std::vector<float>(kBlockSize - 1, 0.f)); |
103 EchoPathVariability echo_path_variability(false, false); | 89 EchoPathVariability echo_path_variability(false, false); |
104 rtc::Optional<size_t> echo_path_delay_samples; | 90 rtc::Optional<size_t> echo_path_delay_samples; |
105 EXPECT_DEATH( | 91 EXPECT_DEATH(remover->ProcessCapture( |
106 remover->ProcessBlock(echo_path_delay_samples, echo_path_variability, | 92 echo_path_delay_samples, echo_path_variability, false, |
107 false, render, &capture), | 93 render_buffer->GetRenderBuffer(), &capture), |
108 ""); | 94 ""); |
109 } | 95 } |
110 } | 96 } |
111 | 97 |
112 // Verifies the check for the number of render bands. | |
113 TEST(EchoRemover, WrongRenderNumBands) { | |
114 for (auto rate : {16000, 32000, 48000}) { | |
115 SCOPED_TRACE(ProduceDebugText(rate)); | |
116 std::unique_ptr<EchoRemover> remover(EchoRemover::Create(rate)); | |
117 | |
118 std::vector<std::vector<float>> render( | |
119 NumBandsForRate(rate == 48000 ? 16000 : rate + 16000), | |
120 std::vector<float>(kBlockSize, 0.f)); | |
121 std::vector<std::vector<float>> capture( | |
122 NumBandsForRate(rate), std::vector<float>(kBlockSize, 0.f)); | |
123 EchoPathVariability echo_path_variability(false, false); | |
124 rtc::Optional<size_t> echo_path_delay_samples; | |
125 EXPECT_DEATH( | |
126 remover->ProcessBlock(echo_path_delay_samples, echo_path_variability, | |
127 false, render, &capture), | |
128 ""); | |
129 } | |
130 } | |
131 | |
132 // Verifies the check for the number of capture bands. | 98 // Verifies the check for the number of capture bands. |
133 // TODO(peah): Re-enable the test once the issue with memory leaks during DEATH | 99 // TODO(peah): Re-enable the test once the issue with memory leaks during DEATH |
134 // tests on test bots has been fixed.c | 100 // tests on test bots has been fixed.c |
135 TEST(EchoRemover, DISABLED_WrongCaptureNumBands) { | 101 TEST(EchoRemover, DISABLED_WrongCaptureNumBands) { |
136 for (auto rate : {16000, 32000, 48000}) { | 102 for (auto rate : {16000, 32000, 48000}) { |
137 SCOPED_TRACE(ProduceDebugText(rate)); | 103 SCOPED_TRACE(ProduceDebugText(rate)); |
138 std::unique_ptr<EchoRemover> remover(EchoRemover::Create(rate)); | 104 std::unique_ptr<EchoRemover> remover(EchoRemover::Create(rate)); |
139 | 105 std::unique_ptr<RenderDelayBuffer> render_buffer( |
140 std::vector<std::vector<float>> render(NumBandsForRate(rate), | 106 RenderDelayBuffer::Create(NumBandsForRate(rate))); |
141 std::vector<float>(kBlockSize, 0.f)); | |
142 std::vector<std::vector<float>> capture( | 107 std::vector<std::vector<float>> capture( |
143 NumBandsForRate(rate == 48000 ? 16000 : rate + 16000), | 108 NumBandsForRate(rate == 48000 ? 16000 : rate + 16000), |
144 std::vector<float>(kBlockSize, 0.f)); | 109 std::vector<float>(kBlockSize, 0.f)); |
145 EchoPathVariability echo_path_variability(false, false); | 110 EchoPathVariability echo_path_variability(false, false); |
146 rtc::Optional<size_t> echo_path_delay_samples; | 111 rtc::Optional<size_t> echo_path_delay_samples; |
147 EXPECT_DEATH( | 112 EXPECT_DEATH(remover->ProcessCapture( |
148 remover->ProcessBlock(echo_path_delay_samples, echo_path_variability, | 113 echo_path_delay_samples, echo_path_variability, false, |
149 false, render, &capture), | 114 render_buffer->GetRenderBuffer(), &capture), |
150 ""); | 115 ""); |
151 } | 116 } |
152 } | 117 } |
153 | 118 |
154 // Verifies the check for non-null capture block. | 119 // Verifies the check for non-null capture block. |
155 TEST(EchoRemover, NullCapture) { | 120 TEST(EchoRemover, NullCapture) { |
156 std::unique_ptr<EchoRemover> remover(EchoRemover::Create(8000)); | 121 std::unique_ptr<EchoRemover> remover(EchoRemover::Create(8000)); |
157 | 122 std::unique_ptr<RenderDelayBuffer> render_buffer( |
158 std::vector<std::vector<float>> render(NumBandsForRate(8000), | 123 RenderDelayBuffer::Create(3)); |
159 std::vector<float>(kBlockSize, 0.f)); | |
160 EchoPathVariability echo_path_variability(false, false); | 124 EchoPathVariability echo_path_variability(false, false); |
161 rtc::Optional<size_t> echo_path_delay_samples; | 125 rtc::Optional<size_t> echo_path_delay_samples; |
162 EXPECT_DEATH( | 126 EXPECT_DEATH( |
163 remover->ProcessBlock(echo_path_delay_samples, echo_path_variability, | 127 remover->ProcessCapture(echo_path_delay_samples, echo_path_variability, |
164 false, render, nullptr), | 128 false, render_buffer->GetRenderBuffer(), nullptr), |
165 ""); | 129 ""); |
166 } | 130 } |
167 | 131 |
168 #endif | 132 #endif |
169 | 133 |
170 // Performs a sanity check that the echo_remover is able to properly | 134 // Performs a sanity check that the echo_remover is able to properly |
171 // remove echoes. | 135 // remove echoes. |
172 TEST(EchoRemover, BasicEchoRemoval) { | 136 TEST(EchoRemover, BasicEchoRemoval) { |
173 constexpr int kNumBlocksToProcess = 500; | 137 constexpr int kNumBlocksToProcess = 500; |
174 Random random_generator(42U); | 138 Random random_generator(42U); |
175 for (auto rate : {8000, 16000, 32000, 48000}) { | 139 for (auto rate : {8000, 16000, 32000, 48000}) { |
176 std::vector<std::vector<float>> x(NumBandsForRate(rate), | 140 std::vector<std::vector<float>> x(NumBandsForRate(rate), |
177 std::vector<float>(kBlockSize, 0.f)); | 141 std::vector<float>(kBlockSize, 0.f)); |
178 std::vector<std::vector<float>> y(NumBandsForRate(rate), | 142 std::vector<std::vector<float>> y(NumBandsForRate(rate), |
179 std::vector<float>(kBlockSize, 0.f)); | 143 std::vector<float>(kBlockSize, 0.f)); |
180 EchoPathVariability echo_path_variability(false, false); | 144 EchoPathVariability echo_path_variability(false, false); |
181 for (size_t delay_samples : {0, 64, 150, 200, 301}) { | 145 for (size_t delay_samples : {0, 64, 150, 200, 301}) { |
182 SCOPED_TRACE(ProduceDebugText(rate, delay_samples)); | 146 SCOPED_TRACE(ProduceDebugText(rate, delay_samples)); |
183 std::unique_ptr<EchoRemover> remover(EchoRemover::Create(rate)); | 147 std::unique_ptr<EchoRemover> remover(EchoRemover::Create(rate)); |
| 148 std::unique_ptr<RenderDelayBuffer> render_buffer( |
| 149 RenderDelayBuffer::Create(NumBandsForRate(rate))); |
184 std::vector<std::unique_ptr<DelayBuffer<float>>> delay_buffers(x.size()); | 150 std::vector<std::unique_ptr<DelayBuffer<float>>> delay_buffers(x.size()); |
185 for (size_t j = 0; j < x.size(); ++j) { | 151 for (size_t j = 0; j < x.size(); ++j) { |
186 delay_buffers[j].reset(new DelayBuffer<float>(delay_samples)); | 152 delay_buffers[j].reset(new DelayBuffer<float>(delay_samples)); |
187 } | 153 } |
188 | 154 |
189 float input_energy = 0.f; | 155 float input_energy = 0.f; |
190 float output_energy = 0.f; | 156 float output_energy = 0.f; |
191 for (int k = 0; k < kNumBlocksToProcess; ++k) { | 157 for (int k = 0; k < kNumBlocksToProcess; ++k) { |
192 const bool silence = k < 100 || (k % 100 >= 10); | 158 const bool silence = k < 100 || (k % 100 >= 10); |
193 | 159 |
194 for (size_t j = 0; j < x.size(); ++j) { | 160 for (size_t j = 0; j < x.size(); ++j) { |
195 if (silence) { | 161 if (silence) { |
196 std::fill(x[j].begin(), x[j].end(), 0.f); | 162 std::fill(x[j].begin(), x[j].end(), 0.f); |
197 } else { | 163 } else { |
198 RandomizeSampleVector(&random_generator, x[j]); | 164 RandomizeSampleVector(&random_generator, x[j]); |
199 } | 165 } |
200 delay_buffers[j]->Delay(x[j], y[j]); | 166 delay_buffers[j]->Delay(x[j], y[j]); |
201 } | 167 } |
202 | 168 |
203 if (k > kNumBlocksToProcess / 2) { | 169 if (k > kNumBlocksToProcess / 2) { |
204 for (size_t j = 0; j < x.size(); ++j) { | 170 for (size_t j = 0; j < x.size(); ++j) { |
205 input_energy = std::inner_product(y[j].begin(), y[j].end(), | 171 input_energy = std::inner_product(y[j].begin(), y[j].end(), |
206 y[j].begin(), input_energy); | 172 y[j].begin(), input_energy); |
207 } | 173 } |
208 } | 174 } |
209 | 175 |
210 remover->ProcessBlock(rtc::Optional<size_t>(delay_samples), | 176 render_buffer->Insert(&x); |
211 echo_path_variability, false, x, &y); | 177 render_buffer->UpdateBuffers(); |
| 178 |
| 179 remover->ProcessCapture(rtc::Optional<size_t>(delay_samples), |
| 180 echo_path_variability, false, |
| 181 render_buffer->GetRenderBuffer(), &y); |
212 | 182 |
213 if (k > kNumBlocksToProcess / 2) { | 183 if (k > kNumBlocksToProcess / 2) { |
214 for (size_t j = 0; j < x.size(); ++j) { | 184 for (size_t j = 0; j < x.size(); ++j) { |
215 output_energy = std::inner_product(y[j].begin(), y[j].end(), | 185 output_energy = std::inner_product(y[j].begin(), y[j].end(), |
216 y[j].begin(), output_energy); | 186 y[j].begin(), output_energy); |
217 } | 187 } |
218 } | 188 } |
219 } | 189 } |
220 EXPECT_GT(input_energy, 10.f * output_energy); | 190 EXPECT_GT(input_energy, 10.f * output_energy); |
221 } | 191 } |
222 } | 192 } |
223 } | 193 } |
224 | 194 |
225 } // namespace webrtc | 195 } // namespace webrtc |
OLD | NEW |