blob: abe43ae3a6592dd67c18191d28c52e50ad2af4fe [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 <string>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#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"
Jonas Olsson366a50c2018-09-06 13:41:30 +020024#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "test/gtest.h"
peah69221db2017-01-27 03:28:19 -080026
27namespace webrtc {
28namespace {
29
30std::string ProduceDebugText(int sample_rate_hz) {
Jonas Olsson366a50c2018-09-06 13:41:30 +020031 rtc::StringBuilder ss;
peah69221db2017-01-27 03:28:19 -080032 ss << "Sample rate: " << sample_rate_hz;
Jonas Olsson84df1c72018-09-14 16:59:32 +020033 return ss.Release();
peah69221db2017-01-27 03:28:19 -080034}
35
peah522d71b2017-02-23 05:16:26 -080036std::string ProduceDebugText(int sample_rate_hz, int delay) {
Jonas Olsson366a50c2018-09-06 13:41:30 +020037 rtc::StringBuilder ss(ProduceDebugText(sample_rate_hz));
peah522d71b2017-02-23 05:16:26 -080038 ss << ", Delay: " << delay;
Jonas Olsson84df1c72018-09-14 16:59:32 +020039 return ss.Release();
peah522d71b2017-02-23 05:16:26 -080040}
41
peah69221db2017-01-27 03:28:19 -080042} // namespace
43
44// Verifies the basic API call sequence
45TEST(EchoRemover, BasicApiCalls) {
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020046 absl::optional<DelayEstimate> delay_estimate;
Per Åhgrend112c752019-09-02 13:56:56 +000047 for (auto rate : {8000, 16000, 32000, 48000}) {
48 SCOPED_TRACE(ProduceDebugText(rate));
49 std::unique_ptr<EchoRemover> remover(
50 EchoRemover::Create(EchoCanceller3Config(), rate));
51 std::unique_ptr<RenderDelayBuffer> render_buffer(
52 RenderDelayBuffer::Create(EchoCanceller3Config(), rate));
peah69221db2017-01-27 03:28:19 -080053
Per Åhgrend112c752019-09-02 13:56:56 +000054 std::vector<std::vector<float>> render(NumBandsForRate(rate),
55 std::vector<float>(kBlockSize, 0.f));
56 std::vector<std::vector<float>> capture(
57 NumBandsForRate(rate), std::vector<float>(kBlockSize, 0.f));
58 for (size_t k = 0; k < 100; ++k) {
59 EchoPathVariability echo_path_variability(
60 k % 3 == 0 ? true : false,
61 k % 5 == 0 ? EchoPathVariability::DelayAdjustment::kNewDetectedDelay
62 : EchoPathVariability::DelayAdjustment::kNone,
63 false);
64 render_buffer->Insert(render);
65 render_buffer->PrepareCaptureProcessing();
Per Åhgrenc59a5762017-12-11 21:34:19 +010066
Per Åhgrend112c752019-09-02 13:56:56 +000067 remover->ProcessCapture(echo_path_variability, k % 2 == 0 ? true : false,
68 delay_estimate, render_buffer->GetRenderBuffer(),
69 &capture);
peah69221db2017-01-27 03:28:19 -080070 }
71 }
72}
73
74#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
75
76// Verifies the check for the samplerate.
77// TODO(peah): Re-enable the test once the issue with memory leaks during DEATH
78// tests on test bots has been fixed.
79TEST(EchoRemover, DISABLED_WrongSampleRate) {
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020080 EXPECT_DEATH(std::unique_ptr<EchoRemover>(
Per Åhgrend112c752019-09-02 13:56:56 +000081 EchoRemover::Create(EchoCanceller3Config(), 8001)),
peah697a5902017-06-30 07:06:10 -070082 "");
peah69221db2017-01-27 03:28:19 -080083}
84
peah69221db2017-01-27 03:28:19 -080085// Verifies the check for the capture block size.
86TEST(EchoRemover, WrongCaptureBlockSize) {
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020087 absl::optional<DelayEstimate> delay_estimate;
Per Åhgrend112c752019-09-02 13:56:56 +000088 for (auto rate : {8000, 16000, 32000, 48000}) {
peah522d71b2017-02-23 05:16:26 -080089 SCOPED_TRACE(ProduceDebugText(rate));
peah697a5902017-06-30 07:06:10 -070090 std::unique_ptr<EchoRemover> remover(
Per Åhgrend112c752019-09-02 13:56:56 +000091 EchoRemover::Create(EchoCanceller3Config(), rate));
Gustaf Ullbergcd277b82019-08-19 12:15:39 +020092 std::unique_ptr<RenderDelayBuffer> render_buffer(
Per Åhgrend112c752019-09-02 13:56:56 +000093 RenderDelayBuffer::Create(EchoCanceller3Config(), rate));
94 std::vector<std::vector<float>> capture(
95 NumBandsForRate(rate), std::vector<float>(kBlockSize - 1, 0.f));
Per Åhgren8ba58612017-12-01 23:01:44 +010096 EchoPathVariability echo_path_variability(
97 false, EchoPathVariability::DelayAdjustment::kNone, false);
Per Åhgrende22a172017-12-20 18:00:51 +010098 EXPECT_DEATH(
Per Åhgren3ab308f2018-02-21 08:46:03 +010099 remover->ProcessCapture(echo_path_variability, false, delay_estimate,
Per Åhgrende22a172017-12-20 18:00:51 +0100100 render_buffer->GetRenderBuffer(), &capture),
101 "");
peah69221db2017-01-27 03:28:19 -0800102 }
103}
104
105// Verifies the check for the number of capture bands.
peah522d71b2017-02-23 05:16:26 -0800106// TODO(peah): Re-enable the test once the issue with memory leaks during DEATH
107// tests on test bots has been fixed.c
108TEST(EchoRemover, DISABLED_WrongCaptureNumBands) {
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200109 absl::optional<DelayEstimate> delay_estimate;
peah69221db2017-01-27 03:28:19 -0800110 for (auto rate : {16000, 32000, 48000}) {
peah522d71b2017-02-23 05:16:26 -0800111 SCOPED_TRACE(ProduceDebugText(rate));
peah697a5902017-06-30 07:06:10 -0700112 std::unique_ptr<EchoRemover> remover(
Per Åhgrend112c752019-09-02 13:56:56 +0000113 EchoRemover::Create(EchoCanceller3Config(), rate));
Gustaf Ullbergcd277b82019-08-19 12:15:39 +0200114 std::unique_ptr<RenderDelayBuffer> render_buffer(
Per Åhgrend112c752019-09-02 13:56:56 +0000115 RenderDelayBuffer::Create(EchoCanceller3Config(), rate));
116 std::vector<std::vector<float>> capture(
peah69221db2017-01-27 03:28:19 -0800117 NumBandsForRate(rate == 48000 ? 16000 : rate + 16000),
Per Åhgrend112c752019-09-02 13:56:56 +0000118 std::vector<float>(kBlockSize, 0.f));
Per Åhgren8ba58612017-12-01 23:01:44 +0100119 EchoPathVariability echo_path_variability(
120 false, EchoPathVariability::DelayAdjustment::kNone, false);
Per Åhgrende22a172017-12-20 18:00:51 +0100121 EXPECT_DEATH(
Per Åhgren3ab308f2018-02-21 08:46:03 +0100122 remover->ProcessCapture(echo_path_variability, false, delay_estimate,
Per Åhgrende22a172017-12-20 18:00:51 +0100123 render_buffer->GetRenderBuffer(), &capture),
124 "");
peah69221db2017-01-27 03:28:19 -0800125 }
126}
127
128// Verifies the check for non-null capture block.
129TEST(EchoRemover, NullCapture) {
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200130 absl::optional<DelayEstimate> delay_estimate;
peah697a5902017-06-30 07:06:10 -0700131 std::unique_ptr<EchoRemover> remover(
Per Åhgrend112c752019-09-02 13:56:56 +0000132 EchoRemover::Create(EchoCanceller3Config(), 8000));
Per Åhgren8ba58612017-12-01 23:01:44 +0100133 std::unique_ptr<RenderDelayBuffer> render_buffer(
Per Åhgrend112c752019-09-02 13:56:56 +0000134 RenderDelayBuffer::Create(EchoCanceller3Config(), 8000));
Per Åhgren8ba58612017-12-01 23:01:44 +0100135 EchoPathVariability echo_path_variability(
136 false, EchoPathVariability::DelayAdjustment::kNone, false);
peah69221db2017-01-27 03:28:19 -0800137 EXPECT_DEATH(
Per Åhgren3ab308f2018-02-21 08:46:03 +0100138 remover->ProcessCapture(echo_path_variability, false, delay_estimate,
Per Åhgrende22a172017-12-20 18:00:51 +0100139 render_buffer->GetRenderBuffer(), nullptr),
peah69221db2017-01-27 03:28:19 -0800140 "");
141}
142
143#endif
144
peah522d71b2017-02-23 05:16:26 -0800145// Performs a sanity check that the echo_remover is able to properly
146// remove echoes.
147TEST(EchoRemover, BasicEchoRemoval) {
148 constexpr int kNumBlocksToProcess = 500;
149 Random random_generator(42U);
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200150 absl::optional<DelayEstimate> delay_estimate;
Per Åhgrend112c752019-09-02 13:56:56 +0000151 for (auto rate : {8000, 16000, 32000, 48000}) {
152 std::vector<std::vector<float>> x(NumBandsForRate(rate),
153 std::vector<float>(kBlockSize, 0.f));
154 std::vector<std::vector<float>> y(NumBandsForRate(rate),
155 std::vector<float>(kBlockSize, 0.f));
156 EchoPathVariability echo_path_variability(
157 false, EchoPathVariability::DelayAdjustment::kNone, false);
158 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
159 SCOPED_TRACE(ProduceDebugText(rate, delay_samples));
160 EchoCanceller3Config config;
161 std::unique_ptr<EchoRemover> remover(EchoRemover::Create(config, rate));
162 std::unique_ptr<RenderDelayBuffer> render_buffer(
163 RenderDelayBuffer::Create(config, rate));
164 render_buffer->AlignFromDelay(delay_samples / kBlockSize);
Per Åhgrenc59a5762017-12-11 21:34:19 +0100165
Per Åhgrend112c752019-09-02 13:56:56 +0000166 std::vector<std::unique_ptr<DelayBuffer<float>>> delay_buffers(x.size());
167 for (size_t j = 0; j < x.size(); ++j) {
168 delay_buffers[j].reset(new DelayBuffer<float>(delay_samples));
peah522d71b2017-02-23 05:16:26 -0800169 }
Per Åhgrend112c752019-09-02 13:56:56 +0000170
171 float input_energy = 0.f;
172 float output_energy = 0.f;
173 for (int k = 0; k < kNumBlocksToProcess; ++k) {
174 const bool silence = k < 100 || (k % 100 >= 10);
175
176 for (size_t j = 0; j < x.size(); ++j) {
177 if (silence) {
178 std::fill(x[j].begin(), x[j].end(), 0.f);
179 } else {
180 RandomizeSampleVector(&random_generator, x[j]);
181 }
182 delay_buffers[j]->Delay(x[j], y[j]);
183 }
184
185 if (k > kNumBlocksToProcess / 2) {
186 for (size_t j = 0; j < x.size(); ++j) {
187 input_energy = std::inner_product(y[j].begin(), y[j].end(),
188 y[j].begin(), input_energy);
189 }
190 }
191
192 render_buffer->Insert(x);
193 render_buffer->PrepareCaptureProcessing();
194
195 remover->ProcessCapture(echo_path_variability, false, delay_estimate,
196 render_buffer->GetRenderBuffer(), &y);
197
198 if (k > kNumBlocksToProcess / 2) {
199 for (size_t j = 0; j < x.size(); ++j) {
200 output_energy = std::inner_product(y[j].begin(), y[j].end(),
201 y[j].begin(), output_energy);
202 }
203 }
204 }
205 EXPECT_GT(input_energy, 10.f * output_energy);
peah522d71b2017-02-23 05:16:26 -0800206 }
207 }
208}
209
peah69221db2017-01-27 03:28:19 -0800210} // namespace webrtc