blob: c9fad802b132c321c863eb7cbfc9d89035cea68a [file] [log] [blame]
peah69221db2017-01-27 03:28:19 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/aec3/echo_remover.h"
peah69221db2017-01-27 03:28:19 -080012
peah522d71b2017-02-23 05:16:26 -080013#include <algorithm>
peah69221db2017-01-27 03:28:19 -080014#include <memory>
peah522d71b2017-02-23 05:16:26 -080015#include <numeric>
peah69221db2017-01-27 03:28:19 -080016#include <sstream>
17#include <string>
18
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_processing/aec3/aec3_common.h"
20#include "modules/audio_processing/aec3/render_buffer.h"
21#include "modules/audio_processing/aec3/render_delay_buffer.h"
22#include "modules/audio_processing/logging/apm_data_dumper.h"
23#include "modules/audio_processing/test/echo_canceller_test_tools.h"
24#include "rtc_base/random.h"
25#include "test/gtest.h"
peah69221db2017-01-27 03:28:19 -080026
27namespace webrtc {
28namespace {
29
30std::string ProduceDebugText(int sample_rate_hz) {
31 std::ostringstream ss;
32 ss << "Sample rate: " << sample_rate_hz;
33 return ss.str();
34}
35
peah522d71b2017-02-23 05:16:26 -080036std::string ProduceDebugText(int sample_rate_hz, int delay) {
37 std::ostringstream ss(ProduceDebugText(sample_rate_hz));
38 ss << ", Delay: " << delay;
39 return ss.str();
40}
41
peah69221db2017-01-27 03:28:19 -080042} // namespace
43
44// Verifies the basic API call sequence
45TEST(EchoRemover, BasicApiCalls) {
46 for (auto rate : {8000, 16000, 32000, 48000}) {
peah522d71b2017-02-23 05:16:26 -080047 SCOPED_TRACE(ProduceDebugText(rate));
peah697a5902017-06-30 07:06:10 -070048 std::unique_ptr<EchoRemover> remover(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020049 EchoRemover::Create(EchoCanceller3Config(), rate));
Per Åhgren38e2d952017-11-17 14:54:28 +010050 std::unique_ptr<RenderDelayBuffer> render_buffer(RenderDelayBuffer::Create(
Per Åhgren8ba58612017-12-01 23:01:44 +010051 EchoCanceller3Config(), NumBandsForRate(rate)));
peah69221db2017-01-27 03:28:19 -080052
53 std::vector<std::vector<float>> render(NumBandsForRate(rate),
54 std::vector<float>(kBlockSize, 0.f));
55 std::vector<std::vector<float>> capture(
56 NumBandsForRate(rate), std::vector<float>(kBlockSize, 0.f));
57 for (size_t k = 0; k < 100; ++k) {
Per Åhgren8ba58612017-12-01 23:01:44 +010058 EchoPathVariability echo_path_variability(
59 k % 3 == 0 ? true : false,
60 k % 5 == 0 ? EchoPathVariability::DelayAdjustment::kNewDetectedDelay
61 : EchoPathVariability::DelayAdjustment::kNone,
62 false);
peahcf02cf12017-04-05 14:18:07 -070063 render_buffer->Insert(render);
Per Åhgrenc59a5762017-12-11 21:34:19 +010064 render_buffer->PrepareCaptureProcessing();
65
Per Åhgrende22a172017-12-20 18:00:51 +010066 remover->ProcessCapture(echo_path_variability, k % 2 == 0 ? true : false,
peahcf02cf12017-04-05 14:18:07 -070067 render_buffer->GetRenderBuffer(), &capture);
peah69221db2017-01-27 03:28:19 -080068 }
69 }
70}
71
72#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
73
74// Verifies the check for the samplerate.
75// TODO(peah): Re-enable the test once the issue with memory leaks during DEATH
76// tests on test bots has been fixed.
77TEST(EchoRemover, DISABLED_WrongSampleRate) {
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020078 EXPECT_DEATH(std::unique_ptr<EchoRemover>(
79 EchoRemover::Create(EchoCanceller3Config(), 8001)),
peah697a5902017-06-30 07:06:10 -070080 "");
peah69221db2017-01-27 03:28:19 -080081}
82
peah69221db2017-01-27 03:28:19 -080083// Verifies the check for the capture block size.
84TEST(EchoRemover, WrongCaptureBlockSize) {
85 for (auto rate : {8000, 16000, 32000, 48000}) {
peah522d71b2017-02-23 05:16:26 -080086 SCOPED_TRACE(ProduceDebugText(rate));
peah697a5902017-06-30 07:06:10 -070087 std::unique_ptr<EchoRemover> remover(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020088 EchoRemover::Create(EchoCanceller3Config(), rate));
Per Åhgren38e2d952017-11-17 14:54:28 +010089 std::unique_ptr<RenderDelayBuffer> render_buffer(RenderDelayBuffer::Create(
Per Åhgren8ba58612017-12-01 23:01:44 +010090 EchoCanceller3Config(), NumBandsForRate(rate)));
peah69221db2017-01-27 03:28:19 -080091 std::vector<std::vector<float>> capture(
92 NumBandsForRate(rate), std::vector<float>(kBlockSize - 1, 0.f));
Per Åhgren8ba58612017-12-01 23:01:44 +010093 EchoPathVariability echo_path_variability(
94 false, EchoPathVariability::DelayAdjustment::kNone, false);
Per Åhgrende22a172017-12-20 18:00:51 +010095 EXPECT_DEATH(
96 remover->ProcessCapture(echo_path_variability, false,
97 render_buffer->GetRenderBuffer(), &capture),
98 "");
peah69221db2017-01-27 03:28:19 -080099 }
100}
101
102// Verifies the check for the number of capture bands.
peah522d71b2017-02-23 05:16:26 -0800103// TODO(peah): Re-enable the test once the issue with memory leaks during DEATH
104// tests on test bots has been fixed.c
105TEST(EchoRemover, DISABLED_WrongCaptureNumBands) {
peah69221db2017-01-27 03:28:19 -0800106 for (auto rate : {16000, 32000, 48000}) {
peah522d71b2017-02-23 05:16:26 -0800107 SCOPED_TRACE(ProduceDebugText(rate));
peah697a5902017-06-30 07:06:10 -0700108 std::unique_ptr<EchoRemover> remover(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200109 EchoRemover::Create(EchoCanceller3Config(), rate));
Per Åhgren38e2d952017-11-17 14:54:28 +0100110 std::unique_ptr<RenderDelayBuffer> render_buffer(RenderDelayBuffer::Create(
Per Åhgren8ba58612017-12-01 23:01:44 +0100111 EchoCanceller3Config(), NumBandsForRate(rate)));
peah69221db2017-01-27 03:28:19 -0800112 std::vector<std::vector<float>> capture(
113 NumBandsForRate(rate == 48000 ? 16000 : rate + 16000),
114 std::vector<float>(kBlockSize, 0.f));
Per Åhgren8ba58612017-12-01 23:01:44 +0100115 EchoPathVariability echo_path_variability(
116 false, EchoPathVariability::DelayAdjustment::kNone, false);
Per Åhgrende22a172017-12-20 18:00:51 +0100117 EXPECT_DEATH(
118 remover->ProcessCapture(echo_path_variability, false,
119 render_buffer->GetRenderBuffer(), &capture),
120 "");
peah69221db2017-01-27 03:28:19 -0800121 }
122}
123
124// Verifies the check for non-null capture block.
125TEST(EchoRemover, NullCapture) {
peah697a5902017-06-30 07:06:10 -0700126 std::unique_ptr<EchoRemover> remover(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200127 EchoRemover::Create(EchoCanceller3Config(), 8000));
Per Åhgren8ba58612017-12-01 23:01:44 +0100128 std::unique_ptr<RenderDelayBuffer> render_buffer(
129 RenderDelayBuffer::Create(EchoCanceller3Config(), 3));
130 EchoPathVariability echo_path_variability(
131 false, EchoPathVariability::DelayAdjustment::kNone, false);
peah69221db2017-01-27 03:28:19 -0800132 EXPECT_DEATH(
Per Åhgrende22a172017-12-20 18:00:51 +0100133 remover->ProcessCapture(echo_path_variability, false,
134 render_buffer->GetRenderBuffer(), nullptr),
peah69221db2017-01-27 03:28:19 -0800135 "");
136}
137
138#endif
139
peah522d71b2017-02-23 05:16:26 -0800140// Performs a sanity check that the echo_remover is able to properly
141// remove echoes.
142TEST(EchoRemover, BasicEchoRemoval) {
143 constexpr int kNumBlocksToProcess = 500;
144 Random random_generator(42U);
145 for (auto rate : {8000, 16000, 32000, 48000}) {
146 std::vector<std::vector<float>> x(NumBandsForRate(rate),
147 std::vector<float>(kBlockSize, 0.f));
148 std::vector<std::vector<float>> y(NumBandsForRate(rate),
149 std::vector<float>(kBlockSize, 0.f));
Per Åhgren8ba58612017-12-01 23:01:44 +0100150 EchoPathVariability echo_path_variability(
151 false, EchoPathVariability::DelayAdjustment::kNone, false);
peah522d71b2017-02-23 05:16:26 -0800152 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
153 SCOPED_TRACE(ProduceDebugText(rate, delay_samples));
Per Åhgren8ba58612017-12-01 23:01:44 +0100154 EchoCanceller3Config config;
155 config.delay.min_echo_path_delay_blocks = 0;
156 std::unique_ptr<EchoRemover> remover(EchoRemover::Create(config, rate));
peahcf02cf12017-04-05 14:18:07 -0700157 std::unique_ptr<RenderDelayBuffer> render_buffer(
Per Åhgren8ba58612017-12-01 23:01:44 +0100158 RenderDelayBuffer::Create(config, NumBandsForRate(rate)));
Per Åhgrenc59a5762017-12-11 21:34:19 +0100159 render_buffer->SetDelay(delay_samples / kBlockSize);
160
peah522d71b2017-02-23 05:16:26 -0800161 std::vector<std::unique_ptr<DelayBuffer<float>>> delay_buffers(x.size());
162 for (size_t j = 0; j < x.size(); ++j) {
163 delay_buffers[j].reset(new DelayBuffer<float>(delay_samples));
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 (size_t j = 0; j < x.size(); ++j) {
172 if (silence) {
173 std::fill(x[j].begin(), x[j].end(), 0.f);
174 } else {
175 RandomizeSampleVector(&random_generator, x[j]);
176 }
177 delay_buffers[j]->Delay(x[j], y[j]);
178 }
179
180 if (k > kNumBlocksToProcess / 2) {
181 for (size_t j = 0; j < x.size(); ++j) {
182 input_energy = std::inner_product(y[j].begin(), y[j].end(),
183 y[j].begin(), input_energy);
184 }
185 }
186
peahcf02cf12017-04-05 14:18:07 -0700187 render_buffer->Insert(x);
Per Åhgrenc59a5762017-12-11 21:34:19 +0100188 render_buffer->PrepareCaptureProcessing();
peahcf02cf12017-04-05 14:18:07 -0700189
Per Åhgrende22a172017-12-20 18:00:51 +0100190 remover->ProcessCapture(echo_path_variability, false,
peahcf02cf12017-04-05 14:18:07 -0700191 render_buffer->GetRenderBuffer(), &y);
peah522d71b2017-02-23 05:16:26 -0800192
193 if (k > kNumBlocksToProcess / 2) {
194 for (size_t j = 0; j < x.size(); ++j) {
195 output_energy = std::inner_product(y[j].begin(), y[j].end(),
196 y[j].begin(), output_energy);
197 }
198 }
199 }
200 EXPECT_GT(input_energy, 10.f * output_energy);
201 }
202 }
203}
204
peah69221db2017-01-27 03:28:19 -0800205} // namespace webrtc