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