blob: f12e7eed3c8259407202f6363ae8ad28db074b97 [file] [log] [blame]
peahd0263542017-01-03 04:20:34 -08001/*
2 * Copyright (c) 2016 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 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020010#include "modules/audio_processing/aec3/block_processor.h"
peahd0263542017-01-03 04:20:34 -080011
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020012#include "api/optional.h"
13#include "modules/audio_processing/aec3/aec3_common.h"
14#include "modules/audio_processing/aec3/block_processor_metrics.h"
15#include "modules/audio_processing/aec3/echo_path_variability.h"
16#include "modules/audio_processing/logging/apm_data_dumper.h"
17#include "rtc_base/atomicops.h"
18#include "rtc_base/constructormagic.h"
Per Åhgrenfe9f2222017-09-26 23:33:26 +020019#include "rtc_base/logging.h"
peahd0263542017-01-03 04:20:34 -080020
21namespace webrtc {
22namespace {
23
peahcf02cf12017-04-05 14:18:07 -070024enum class BlockProcessorApiCall { kCapture, kRender };
25
peahd0263542017-01-03 04:20:34 -080026class BlockProcessorImpl final : public BlockProcessor {
27 public:
Per Åhgren8ba58612017-12-01 23:01:44 +010028 BlockProcessorImpl(const EchoCanceller3Config& config,
29 int sample_rate_hz,
peah69221db2017-01-27 03:28:19 -080030 std::unique_ptr<RenderDelayBuffer> render_buffer,
31 std::unique_ptr<RenderDelayController> delay_controller,
32 std::unique_ptr<EchoRemover> echo_remover);
33
peahd0263542017-01-03 04:20:34 -080034 ~BlockProcessorImpl() override;
35
peah69221db2017-01-27 03:28:19 -080036 void ProcessCapture(bool echo_path_gain_change,
37 bool capture_signal_saturation,
peahd0263542017-01-03 04:20:34 -080038 std::vector<std::vector<float>>* capture_block) override;
39
peahcf02cf12017-04-05 14:18:07 -070040 void BufferRender(const std::vector<std::vector<float>>& block) override;
peahd0263542017-01-03 04:20:34 -080041
peah69221db2017-01-27 03:28:19 -080042 void UpdateEchoLeakageStatus(bool leakage_detected) override;
peahd0263542017-01-03 04:20:34 -080043
Gustaf Ullberg332150d2017-11-22 14:17:39 +010044 void GetMetrics(EchoControl::Metrics* metrics) const override;
45
peahd0263542017-01-03 04:20:34 -080046 private:
peahd0263542017-01-03 04:20:34 -080047 static int instance_count_;
48 std::unique_ptr<ApmDataDumper> data_dumper_;
Per Åhgren8ba58612017-12-01 23:01:44 +010049 const EchoCanceller3Config config_;
50 bool capture_properly_started_ = false;
51 bool render_properly_started_ = false;
peah69221db2017-01-27 03:28:19 -080052 const size_t sample_rate_hz_;
53 std::unique_ptr<RenderDelayBuffer> render_buffer_;
54 std::unique_ptr<RenderDelayController> delay_controller_;
55 std::unique_ptr<EchoRemover> echo_remover_;
peahe985b3f2017-02-28 22:08:53 -080056 BlockProcessorMetrics metrics_;
Per Åhgren8ba58612017-12-01 23:01:44 +010057 RenderDelayBuffer::BufferingEvent render_event_;
peahd0263542017-01-03 04:20:34 -080058 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(BlockProcessorImpl);
59};
60
61int BlockProcessorImpl::instance_count_ = 0;
62
peah69221db2017-01-27 03:28:19 -080063BlockProcessorImpl::BlockProcessorImpl(
Per Åhgren8ba58612017-12-01 23:01:44 +010064 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -080065 int sample_rate_hz,
66 std::unique_ptr<RenderDelayBuffer> render_buffer,
67 std::unique_ptr<RenderDelayController> delay_controller,
68 std::unique_ptr<EchoRemover> echo_remover)
69 : data_dumper_(
70 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Per Åhgren8ba58612017-12-01 23:01:44 +010071 config_(config),
peah69221db2017-01-27 03:28:19 -080072 sample_rate_hz_(sample_rate_hz),
73 render_buffer_(std::move(render_buffer)),
74 delay_controller_(std::move(delay_controller)),
Per Åhgren8ba58612017-12-01 23:01:44 +010075 echo_remover_(std::move(echo_remover)),
76 render_event_(RenderDelayBuffer::BufferingEvent::kNone) {
peah21920892017-02-08 05:08:56 -080077 RTC_DCHECK(ValidFullBandRate(sample_rate_hz_));
78}
peahd0263542017-01-03 04:20:34 -080079
80BlockProcessorImpl::~BlockProcessorImpl() = default;
81
82void BlockProcessorImpl::ProcessCapture(
peah69221db2017-01-27 03:28:19 -080083 bool echo_path_gain_change,
84 bool capture_signal_saturation,
peahd0263542017-01-03 04:20:34 -080085 std::vector<std::vector<float>>* capture_block) {
86 RTC_DCHECK(capture_block);
87 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), capture_block->size());
88 RTC_DCHECK_EQ(kBlockSize, (*capture_block)[0].size());
peahcf02cf12017-04-05 14:18:07 -070089 data_dumper_->DumpRaw("aec3_processblock_call_order",
90 static_cast<int>(BlockProcessorApiCall::kCapture));
91 data_dumper_->DumpWav("aec3_processblock_capture_input", kBlockSize,
92 &(*capture_block)[0][0],
93 LowestBandRate(sample_rate_hz_), 1);
peah69221db2017-01-27 03:28:19 -080094
Per Åhgren8ba58612017-12-01 23:01:44 +010095 if (render_properly_started_) {
96 if (!capture_properly_started_) {
97 capture_properly_started_ = true;
98 render_buffer_->Reset();
99 }
100 } else {
101 // If no render data has yet arrived, do not process the capture signal.
peahcf02cf12017-04-05 14:18:07 -0700102 return;
peah69221db2017-01-27 03:28:19 -0800103 }
104
Per Åhgren8ba58612017-12-01 23:01:44 +0100105 EchoPathVariability echo_path_variability(
106 echo_path_gain_change, EchoPathVariability::DelayAdjustment::kNone,
107 false);
108
109 if (render_event_ == RenderDelayBuffer::BufferingEvent::kRenderOverrun &&
110 render_properly_started_) {
111 echo_path_variability.delay_change =
112 EchoPathVariability::DelayAdjustment::kBufferFlush;
113 delay_controller_->Reset();
114 render_buffer_->Reset();
115 RTC_LOG(LS_WARNING) << "Reset due to render buffer overrun.";
116 }
117
118 // Update the render buffers with any newly arrived render blocks and prepare
119 // the render buffers for reading the render data corresponding to the current
120 // capture block.
121 render_event_ = render_buffer_->PrepareCaptureCall();
122 RTC_DCHECK(RenderDelayBuffer::BufferingEvent::kRenderOverrun !=
123 render_event_);
124 if (render_event_ == RenderDelayBuffer::BufferingEvent::kRenderUnderrun) {
125 echo_path_variability.delay_change =
126 EchoPathVariability::DelayAdjustment::kBufferReadjustment;
127 delay_controller_->Reset();
128 render_buffer_->Reset();
129 } else if (render_event_ == RenderDelayBuffer::BufferingEvent::kApiCallSkew) {
130 // There have been too many render calls in a row. Reset to avoid noncausal
131 // echo.
132 echo_path_variability.delay_change =
133 EchoPathVariability::DelayAdjustment::kDelayReset;
134 delay_controller_->Reset();
135 render_buffer_->Reset();
136 capture_properly_started_ = false;
137 render_properly_started_ = false;
138 }
139
peahcf02cf12017-04-05 14:18:07 -0700140 data_dumper_->DumpWav("aec3_processblock_capture_input2", kBlockSize,
141 &(*capture_block)[0][0],
142 LowestBandRate(sample_rate_hz_), 1);
143
peah4b572d02017-04-06 16:33:06 -0700144 // Compute and and apply the render delay required to achieve proper signal
145 // alignment.
Per Åhgren8ba58612017-12-01 23:01:44 +0100146 const size_t estimated_delay = delay_controller_->GetDelay(
peah4b572d02017-04-06 16:33:06 -0700147 render_buffer_->GetDownsampledRenderBuffer(), (*capture_block)[0]);
Per Åhgren8ba58612017-12-01 23:01:44 +0100148 const size_t new_delay =
149 std::min(render_buffer_->MaxDelay(), estimated_delay);
peah4b572d02017-04-06 16:33:06 -0700150
Per Åhgren8ba58612017-12-01 23:01:44 +0100151 bool delay_change = render_buffer_->Delay() != new_delay;
152 if (delay_change && new_delay >= config_.delay.min_echo_path_delay_blocks) {
153 echo_path_variability.delay_change =
154 EchoPathVariability::DelayAdjustment::kNewDetectedDelay;
peah96b951c2017-08-22 10:26:07 -0700155 render_buffer_->SetDelay(new_delay);
Per Åhgren8ba58612017-12-01 23:01:44 +0100156 RTC_DCHECK_EQ(render_buffer_->Delay(), new_delay);
157 delay_controller_->SetDelay(new_delay);
158 } else if (delay_change &&
159 new_delay < config_.delay.min_echo_path_delay_blocks) {
160 // A noncausal delay has been detected. This can only happen if there is
161 // clockdrift, an audio pipeline issue has occurred or the specified minimum
162 // delay is too short. Perform a full reset.
163 echo_path_variability.delay_change =
164 EchoPathVariability::DelayAdjustment::kDelayReset;
peah96b951c2017-08-22 10:26:07 -0700165 delay_controller_->Reset();
166 render_buffer_->Reset();
Per Åhgren8ba58612017-12-01 23:01:44 +0100167 capture_properly_started_ = false;
168 render_properly_started_ = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100169 RTC_LOG(LS_WARNING) << "Reset due to noncausal delay.";
peah96b951c2017-08-22 10:26:07 -0700170 }
peah4b572d02017-04-06 16:33:06 -0700171
172 // Remove the echo from the capture signal.
173 echo_remover_->ProcessCapture(
Per Åhgren8ba58612017-12-01 23:01:44 +0100174 delay_controller_->AlignmentHeadroomSamples(), echo_path_variability,
peah4b572d02017-04-06 16:33:06 -0700175 capture_signal_saturation, render_buffer_->GetRenderBuffer(),
176 capture_block);
177
peahcf02cf12017-04-05 14:18:07 -0700178 // Update the metrics.
Per Åhgren8ba58612017-12-01 23:01:44 +0100179 metrics_.UpdateCapture(false);
peahcf02cf12017-04-05 14:18:07 -0700180
Per Åhgren8ba58612017-12-01 23:01:44 +0100181 render_event_ = RenderDelayBuffer::BufferingEvent::kNone;
peahd0263542017-01-03 04:20:34 -0800182}
183
peahcf02cf12017-04-05 14:18:07 -0700184void BlockProcessorImpl::BufferRender(
185 const std::vector<std::vector<float>>& block) {
186 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), block.size());
187 RTC_DCHECK_EQ(kBlockSize, block[0].size());
188 data_dumper_->DumpRaw("aec3_processblock_call_order",
189 static_cast<int>(BlockProcessorApiCall::kRender));
190 data_dumper_->DumpWav("aec3_processblock_render_input", kBlockSize,
191 &block[0][0], LowestBandRate(sample_rate_hz_), 1);
peahcf02cf12017-04-05 14:18:07 -0700192 data_dumper_->DumpWav("aec3_processblock_render_input2", kBlockSize,
193 &block[0][0], LowestBandRate(sample_rate_hz_), 1);
194
Per Åhgren8ba58612017-12-01 23:01:44 +0100195 render_event_ = render_buffer_->Insert(block);
peahcf02cf12017-04-05 14:18:07 -0700196
Per Åhgren8ba58612017-12-01 23:01:44 +0100197 metrics_.UpdateRender(render_event_ !=
198 RenderDelayBuffer::BufferingEvent::kNone);
199
200 render_properly_started_ = true;
peahd0263542017-01-03 04:20:34 -0800201}
202
peah69221db2017-01-27 03:28:19 -0800203void BlockProcessorImpl::UpdateEchoLeakageStatus(bool leakage_detected) {
204 echo_remover_->UpdateEchoLeakageStatus(leakage_detected);
205}
peahd0263542017-01-03 04:20:34 -0800206
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100207void BlockProcessorImpl::GetMetrics(EchoControl::Metrics* metrics) const {
208 echo_remover_->GetMetrics(metrics);
Per Åhgren83c4a022017-11-27 12:07:09 +0100209 const int block_size_ms = sample_rate_hz_ == 8000 ? 8 : 4;
210 metrics->delay_ms = static_cast<int>(render_buffer_->Delay()) * block_size_ms;
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100211}
212
peahd0263542017-01-03 04:20:34 -0800213} // namespace
214
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200215BlockProcessor* BlockProcessor::Create(const EchoCanceller3Config& config,
216 int sample_rate_hz) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100217 std::unique_ptr<RenderDelayBuffer> render_buffer(
218 RenderDelayBuffer::Create(config, NumBandsForRate(sample_rate_hz)));
peah69221db2017-01-27 03:28:19 -0800219 std::unique_ptr<RenderDelayController> delay_controller(
peah4fed3c02017-08-30 06:58:44 -0700220 RenderDelayController::Create(config, sample_rate_hz));
peah69221db2017-01-27 03:28:19 -0800221 std::unique_ptr<EchoRemover> echo_remover(
peah697a5902017-06-30 07:06:10 -0700222 EchoRemover::Create(config, sample_rate_hz));
223 return Create(config, sample_rate_hz, std::move(render_buffer),
peah69221db2017-01-27 03:28:19 -0800224 std::move(delay_controller), std::move(echo_remover));
225}
226
227BlockProcessor* BlockProcessor::Create(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200228 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -0800229 int sample_rate_hz,
230 std::unique_ptr<RenderDelayBuffer> render_buffer) {
231 std::unique_ptr<RenderDelayController> delay_controller(
peah4fed3c02017-08-30 06:58:44 -0700232 RenderDelayController::Create(config, sample_rate_hz));
peah69221db2017-01-27 03:28:19 -0800233 std::unique_ptr<EchoRemover> echo_remover(
peah697a5902017-06-30 07:06:10 -0700234 EchoRemover::Create(config, sample_rate_hz));
235 return Create(config, sample_rate_hz, std::move(render_buffer),
peah69221db2017-01-27 03:28:19 -0800236 std::move(delay_controller), std::move(echo_remover));
237}
238
239BlockProcessor* BlockProcessor::Create(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200240 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -0800241 int sample_rate_hz,
242 std::unique_ptr<RenderDelayBuffer> render_buffer,
243 std::unique_ptr<RenderDelayController> delay_controller,
244 std::unique_ptr<EchoRemover> echo_remover) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100245 return new BlockProcessorImpl(
246 config, sample_rate_hz, std::move(render_buffer),
247 std::move(delay_controller), std::move(echo_remover));
peahd0263542017-01-03 04:20:34 -0800248}
249
250} // namespace webrtc