blob: 0997b1a8d7cf454c8eac2a2ff9901f4e8fbf21b9 [file] [log] [blame]
peahd0263542017-01-03 04:20:34 -08001/*
Gustaf Ullberge47433f2019-01-24 16:00:57 +01002 * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
peahd0263542017-01-03 04:20:34 -08003 *
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 */
Gustaf Ullberge47433f2019-01-24 16:00:57 +010010#include <stddef.h>
11#include <memory>
Yves Gerey988cc082018-10-23 12:03:01 +020012#include <utility>
Gustaf Ullberge47433f2019-01-24 16:00:57 +010013#include <vector>
Yves Gerey988cc082018-10-23 12:03:01 +020014
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020015#include "absl/types/optional.h"
Gustaf Ullberge47433f2019-01-24 16:00:57 +010016#include "api/audio/echo_canceller3_config.h"
17#include "api/audio/echo_control.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_processing/aec3/aec3_common.h"
Gustaf Ullberge47433f2019-01-24 16:00:57 +010019#include "modules/audio_processing/aec3/block_processor.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/audio_processing/aec3/block_processor_metrics.h"
Yves Gerey988cc082018-10-23 12:03:01 +020021#include "modules/audio_processing/aec3/delay_estimate.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/audio_processing/aec3/echo_path_variability.h"
Gustaf Ullberge47433f2019-01-24 16:00:57 +010023#include "modules/audio_processing/aec3/echo_remover.h"
24#include "modules/audio_processing/aec3/render_delay_buffer.h"
25#include "modules/audio_processing/aec3/render_delay_controller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "modules/audio_processing/logging/apm_data_dumper.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "rtc_base/atomic_ops.h"
Yves Gerey988cc082018-10-23 12:03:01 +020028#include "rtc_base/checks.h"
Per Åhgrenfe9f2222017-09-26 23:33:26 +020029#include "rtc_base/logging.h"
peahd0263542017-01-03 04:20:34 -080030
31namespace webrtc {
32namespace {
33
peahcf02cf12017-04-05 14:18:07 -070034enum class BlockProcessorApiCall { kCapture, kRender };
35
peahd0263542017-01-03 04:20:34 -080036class BlockProcessorImpl final : public BlockProcessor {
37 public:
Per Åhgren8ba58612017-12-01 23:01:44 +010038 BlockProcessorImpl(const EchoCanceller3Config& config,
39 int sample_rate_hz,
peah69221db2017-01-27 03:28:19 -080040 std::unique_ptr<RenderDelayBuffer> render_buffer,
41 std::unique_ptr<RenderDelayController> delay_controller,
42 std::unique_ptr<EchoRemover> echo_remover);
43
Gustaf Ullberge47433f2019-01-24 16:00:57 +010044 BlockProcessorImpl() = delete;
45
peahd0263542017-01-03 04:20:34 -080046 ~BlockProcessorImpl() override;
47
peah69221db2017-01-27 03:28:19 -080048 void ProcessCapture(bool echo_path_gain_change,
49 bool capture_signal_saturation,
peahd0263542017-01-03 04:20:34 -080050 std::vector<std::vector<float>>* capture_block) override;
51
peahcf02cf12017-04-05 14:18:07 -070052 void BufferRender(const std::vector<std::vector<float>>& block) override;
peahd0263542017-01-03 04:20:34 -080053
peah69221db2017-01-27 03:28:19 -080054 void UpdateEchoLeakageStatus(bool leakage_detected) override;
peahd0263542017-01-03 04:20:34 -080055
Gustaf Ullberg332150d2017-11-22 14:17:39 +010056 void GetMetrics(EchoControl::Metrics* metrics) const override;
57
Per Åhgrend0fa8202018-04-18 09:35:13 +020058 void SetAudioBufferDelay(size_t delay_ms) override;
59
peahd0263542017-01-03 04:20:34 -080060 private:
peahd0263542017-01-03 04:20:34 -080061 static int instance_count_;
62 std::unique_ptr<ApmDataDumper> data_dumper_;
Per Åhgren8ba58612017-12-01 23:01:44 +010063 const EchoCanceller3Config config_;
64 bool capture_properly_started_ = false;
65 bool render_properly_started_ = false;
peah69221db2017-01-27 03:28:19 -080066 const size_t sample_rate_hz_;
67 std::unique_ptr<RenderDelayBuffer> render_buffer_;
68 std::unique_ptr<RenderDelayController> delay_controller_;
69 std::unique_ptr<EchoRemover> echo_remover_;
peahe985b3f2017-02-28 22:08:53 -080070 BlockProcessorMetrics metrics_;
Per Åhgren8ba58612017-12-01 23:01:44 +010071 RenderDelayBuffer::BufferingEvent render_event_;
Per Åhgrenc59a5762017-12-11 21:34:19 +010072 size_t capture_call_counter_ = 0;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020073 absl::optional<DelayEstimate> estimated_delay_;
peahd0263542017-01-03 04:20:34 -080074};
75
76int BlockProcessorImpl::instance_count_ = 0;
77
peah69221db2017-01-27 03:28:19 -080078BlockProcessorImpl::BlockProcessorImpl(
Per Åhgren8ba58612017-12-01 23:01:44 +010079 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -080080 int sample_rate_hz,
81 std::unique_ptr<RenderDelayBuffer> render_buffer,
82 std::unique_ptr<RenderDelayController> delay_controller,
83 std::unique_ptr<EchoRemover> echo_remover)
84 : data_dumper_(
85 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Per Åhgren8ba58612017-12-01 23:01:44 +010086 config_(config),
peah69221db2017-01-27 03:28:19 -080087 sample_rate_hz_(sample_rate_hz),
88 render_buffer_(std::move(render_buffer)),
89 delay_controller_(std::move(delay_controller)),
Per Åhgren8ba58612017-12-01 23:01:44 +010090 echo_remover_(std::move(echo_remover)),
91 render_event_(RenderDelayBuffer::BufferingEvent::kNone) {
peah21920892017-02-08 05:08:56 -080092 RTC_DCHECK(ValidFullBandRate(sample_rate_hz_));
93}
peahd0263542017-01-03 04:20:34 -080094
95BlockProcessorImpl::~BlockProcessorImpl() = default;
96
97void BlockProcessorImpl::ProcessCapture(
peah69221db2017-01-27 03:28:19 -080098 bool echo_path_gain_change,
99 bool capture_signal_saturation,
peahd0263542017-01-03 04:20:34 -0800100 std::vector<std::vector<float>>* capture_block) {
101 RTC_DCHECK(capture_block);
102 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), capture_block->size());
103 RTC_DCHECK_EQ(kBlockSize, (*capture_block)[0].size());
Per Åhgrenc59a5762017-12-11 21:34:19 +0100104
105 capture_call_counter_++;
106
peahcf02cf12017-04-05 14:18:07 -0700107 data_dumper_->DumpRaw("aec3_processblock_call_order",
108 static_cast<int>(BlockProcessorApiCall::kCapture));
109 data_dumper_->DumpWav("aec3_processblock_capture_input", kBlockSize,
110 &(*capture_block)[0][0],
111 LowestBandRate(sample_rate_hz_), 1);
peah69221db2017-01-27 03:28:19 -0800112
Per Åhgren8ba58612017-12-01 23:01:44 +0100113 if (render_properly_started_) {
114 if (!capture_properly_started_) {
115 capture_properly_started_ = true;
116 render_buffer_->Reset();
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200117 if (delay_controller_)
118 delay_controller_->Reset(true);
Per Åhgren8ba58612017-12-01 23:01:44 +0100119 }
120 } else {
121 // If no render data has yet arrived, do not process the capture signal.
peahcf02cf12017-04-05 14:18:07 -0700122 return;
peah69221db2017-01-27 03:28:19 -0800123 }
124
Per Åhgren8ba58612017-12-01 23:01:44 +0100125 EchoPathVariability echo_path_variability(
126 echo_path_gain_change, EchoPathVariability::DelayAdjustment::kNone,
127 false);
128
129 if (render_event_ == RenderDelayBuffer::BufferingEvent::kRenderOverrun &&
130 render_properly_started_) {
131 echo_path_variability.delay_change =
132 EchoPathVariability::DelayAdjustment::kBufferFlush;
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200133 if (delay_controller_)
134 delay_controller_->Reset(true);
Per Åhgrenc59a5762017-12-11 21:34:19 +0100135 RTC_LOG(LS_WARNING) << "Reset due to render buffer overrun at block "
136 << capture_call_counter_;
Per Åhgren8ba58612017-12-01 23:01:44 +0100137 }
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100138 render_event_ = RenderDelayBuffer::BufferingEvent::kNone;
Per Åhgren8ba58612017-12-01 23:01:44 +0100139
140 // Update the render buffers with any newly arrived render blocks and prepare
141 // the render buffers for reading the render data corresponding to the current
142 // capture block.
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100143 RenderDelayBuffer::BufferingEvent buffer_event =
144 render_buffer_->PrepareCaptureProcessing();
145 // Reset the delay controller at render buffer underrun.
146 if (buffer_event == RenderDelayBuffer::BufferingEvent::kRenderUnderrun) {
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200147 if (delay_controller_)
148 delay_controller_->Reset(false);
Per Åhgren8ba58612017-12-01 23:01:44 +0100149 }
150
peahcf02cf12017-04-05 14:18:07 -0700151 data_dumper_->DumpWav("aec3_processblock_capture_input2", kBlockSize,
152 &(*capture_block)[0][0],
153 LowestBandRate(sample_rate_hz_), 1);
154
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200155 bool has_delay_estimator = !config_.delay.use_external_delay_estimator;
156 if (has_delay_estimator) {
157 RTC_DCHECK(delay_controller_);
158 // Compute and apply the render delay required to achieve proper signal
159 // alignment.
160 estimated_delay_ = delay_controller_->GetDelay(
161 render_buffer_->GetDownsampledRenderBuffer(), render_buffer_->Delay(),
162 (*capture_block)[0]);
peah4b572d02017-04-06 16:33:06 -0700163
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200164 if (estimated_delay_) {
165 bool delay_change =
166 render_buffer_->AlignFromDelay(estimated_delay_->delay);
167 if (delay_change) {
168 RTC_LOG(LS_WARNING) << "Delay changed to " << estimated_delay_->delay
169 << " at block " << capture_call_counter_;
170 echo_path_variability.delay_change =
171 EchoPathVariability::DelayAdjustment::kNewDetectedDelay;
172 }
Per Åhgrenc59a5762017-12-11 21:34:19 +0100173 }
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200174
175 echo_path_variability.clock_drift = delay_controller_->HasClockdrift();
176
177 } else {
178 render_buffer_->AlignFromExternalDelay();
peah96b951c2017-08-22 10:26:07 -0700179 }
peah4b572d02017-04-06 16:33:06 -0700180
181 // Remove the echo from the capture signal.
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200182 if (has_delay_estimator || render_buffer_->HasReceivedBufferDelay()) {
183 echo_remover_->ProcessCapture(
184 echo_path_variability, capture_signal_saturation, estimated_delay_,
185 render_buffer_->GetRenderBuffer(), capture_block);
186 }
peah4b572d02017-04-06 16:33:06 -0700187
peahcf02cf12017-04-05 14:18:07 -0700188 // Update the metrics.
Per Åhgren8ba58612017-12-01 23:01:44 +0100189 metrics_.UpdateCapture(false);
peahd0263542017-01-03 04:20:34 -0800190}
191
peahcf02cf12017-04-05 14:18:07 -0700192void BlockProcessorImpl::BufferRender(
193 const std::vector<std::vector<float>>& block) {
194 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), block.size());
195 RTC_DCHECK_EQ(kBlockSize, block[0].size());
196 data_dumper_->DumpRaw("aec3_processblock_call_order",
197 static_cast<int>(BlockProcessorApiCall::kRender));
198 data_dumper_->DumpWav("aec3_processblock_render_input", kBlockSize,
199 &block[0][0], LowestBandRate(sample_rate_hz_), 1);
peahcf02cf12017-04-05 14:18:07 -0700200 data_dumper_->DumpWav("aec3_processblock_render_input2", kBlockSize,
201 &block[0][0], LowestBandRate(sample_rate_hz_), 1);
202
Per Åhgren8ba58612017-12-01 23:01:44 +0100203 render_event_ = render_buffer_->Insert(block);
peahcf02cf12017-04-05 14:18:07 -0700204
Per Åhgren8ba58612017-12-01 23:01:44 +0100205 metrics_.UpdateRender(render_event_ !=
206 RenderDelayBuffer::BufferingEvent::kNone);
207
208 render_properly_started_ = true;
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200209 if (delay_controller_)
210 delay_controller_->LogRenderCall();
peahd0263542017-01-03 04:20:34 -0800211}
212
peah69221db2017-01-27 03:28:19 -0800213void BlockProcessorImpl::UpdateEchoLeakageStatus(bool leakage_detected) {
214 echo_remover_->UpdateEchoLeakageStatus(leakage_detected);
215}
peahd0263542017-01-03 04:20:34 -0800216
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100217void BlockProcessorImpl::GetMetrics(EchoControl::Metrics* metrics) const {
218 echo_remover_->GetMetrics(metrics);
Per Åhgren83c4a022017-11-27 12:07:09 +0100219 const int block_size_ms = sample_rate_hz_ == 8000 ? 8 : 4;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200220 absl::optional<size_t> delay = render_buffer_->Delay();
Per Åhgrenc59a5762017-12-11 21:34:19 +0100221 metrics->delay_ms = delay ? static_cast<int>(*delay) * block_size_ms : 0;
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100222}
223
Per Åhgrend0fa8202018-04-18 09:35:13 +0200224void BlockProcessorImpl::SetAudioBufferDelay(size_t delay_ms) {
225 render_buffer_->SetAudioBufferDelay(delay_ms);
226}
227
peahd0263542017-01-03 04:20:34 -0800228} // namespace
229
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200230BlockProcessor* BlockProcessor::Create(const EchoCanceller3Config& config,
231 int sample_rate_hz) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100232 std::unique_ptr<RenderDelayBuffer> render_buffer(
233 RenderDelayBuffer::Create(config, NumBandsForRate(sample_rate_hz)));
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200234 std::unique_ptr<RenderDelayController> delay_controller;
235 if (!config.delay.use_external_delay_estimator) {
236 delay_controller.reset(
237 RenderDelayController::Create(config, sample_rate_hz));
238 }
peah69221db2017-01-27 03:28:19 -0800239 std::unique_ptr<EchoRemover> echo_remover(
peah697a5902017-06-30 07:06:10 -0700240 EchoRemover::Create(config, sample_rate_hz));
241 return Create(config, sample_rate_hz, std::move(render_buffer),
peah69221db2017-01-27 03:28:19 -0800242 std::move(delay_controller), std::move(echo_remover));
243}
244
245BlockProcessor* BlockProcessor::Create(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200246 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -0800247 int sample_rate_hz,
248 std::unique_ptr<RenderDelayBuffer> render_buffer) {
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200249 std::unique_ptr<RenderDelayController> delay_controller;
250 if (!config.delay.use_external_delay_estimator) {
251 delay_controller.reset(
252 RenderDelayController::Create(config, sample_rate_hz));
253 }
peah69221db2017-01-27 03:28:19 -0800254 std::unique_ptr<EchoRemover> echo_remover(
peah697a5902017-06-30 07:06:10 -0700255 EchoRemover::Create(config, sample_rate_hz));
256 return Create(config, sample_rate_hz, std::move(render_buffer),
peah69221db2017-01-27 03:28:19 -0800257 std::move(delay_controller), std::move(echo_remover));
258}
259
260BlockProcessor* BlockProcessor::Create(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200261 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -0800262 int sample_rate_hz,
263 std::unique_ptr<RenderDelayBuffer> render_buffer,
264 std::unique_ptr<RenderDelayController> delay_controller,
265 std::unique_ptr<EchoRemover> echo_remover) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100266 return new BlockProcessorImpl(
267 config, sample_rate_hz, std::move(render_buffer),
268 std::move(delay_controller), std::move(echo_remover));
peahd0263542017-01-03 04:20:34 -0800269}
270
271} // namespace webrtc