blob: 2ee32b82dc6da1b585f2cbf81420620e518c6dde [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 */
Jonas Olssona4d87372019-07-05 19:08:33 +020010#include "modules/audio_processing/aec3/block_processor.h"
11
Gustaf Ullberge47433f2019-01-24 16:00:57 +010012#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020013
Gustaf Ullberge47433f2019-01-24 16:00:57 +010014#include <memory>
Yves Gerey988cc082018-10-23 12:03:01 +020015#include <utility>
Gustaf Ullberge47433f2019-01-24 16:00:57 +010016#include <vector>
Yves Gerey988cc082018-10-23 12:03:01 +020017
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020018#include "absl/types/optional.h"
Gustaf Ullberge47433f2019-01-24 16:00:57 +010019#include "api/audio/echo_canceller3_config.h"
20#include "api/audio/echo_control.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/audio_processing/aec3/aec3_common.h"
22#include "modules/audio_processing/aec3/block_processor_metrics.h"
Yves Gerey988cc082018-10-23 12:03:01 +020023#include "modules/audio_processing/aec3/delay_estimate.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/audio_processing/aec3/echo_path_variability.h"
Gustaf Ullberge47433f2019-01-24 16:00:57 +010025#include "modules/audio_processing/aec3/echo_remover.h"
26#include "modules/audio_processing/aec3/render_delay_buffer.h"
27#include "modules/audio_processing/aec3/render_delay_controller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/audio_processing/logging/apm_data_dumper.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "rtc_base/atomic_ops.h"
Yves Gerey988cc082018-10-23 12:03:01 +020030#include "rtc_base/checks.h"
Per Åhgrenfe9f2222017-09-26 23:33:26 +020031#include "rtc_base/logging.h"
peahd0263542017-01-03 04:20:34 -080032
33namespace webrtc {
34namespace {
35
peahcf02cf12017-04-05 14:18:07 -070036enum class BlockProcessorApiCall { kCapture, kRender };
37
peahd0263542017-01-03 04:20:34 -080038class BlockProcessorImpl final : public BlockProcessor {
39 public:
Per Åhgren8ba58612017-12-01 23:01:44 +010040 BlockProcessorImpl(const EchoCanceller3Config& config,
41 int sample_rate_hz,
Per Åhgrence202a02019-09-02 17:01:19 +020042 size_t num_render_channels,
43 size_t num_capture_channels,
peah69221db2017-01-27 03:28:19 -080044 std::unique_ptr<RenderDelayBuffer> render_buffer,
45 std::unique_ptr<RenderDelayController> delay_controller,
46 std::unique_ptr<EchoRemover> echo_remover);
47
Gustaf Ullberge47433f2019-01-24 16:00:57 +010048 BlockProcessorImpl() = delete;
49
peahd0263542017-01-03 04:20:34 -080050 ~BlockProcessorImpl() override;
51
Per Åhgrence202a02019-09-02 17:01:19 +020052 void ProcessCapture(
53 bool echo_path_gain_change,
54 bool capture_signal_saturation,
Per Åhgrenc20a19c2019-11-13 11:12:29 +010055 std::vector<std::vector<std::vector<float>>>* linear_output,
Per Åhgrence202a02019-09-02 17:01:19 +020056 std::vector<std::vector<std::vector<float>>>* capture_block) override;
peahd0263542017-01-03 04:20:34 -080057
Per Åhgrence202a02019-09-02 17:01:19 +020058 void BufferRender(
59 const std::vector<std::vector<std::vector<float>>>& block) override;
peahd0263542017-01-03 04:20:34 -080060
peah69221db2017-01-27 03:28:19 -080061 void UpdateEchoLeakageStatus(bool leakage_detected) override;
peahd0263542017-01-03 04:20:34 -080062
Gustaf Ullberg332150d2017-11-22 14:17:39 +010063 void GetMetrics(EchoControl::Metrics* metrics) const override;
64
Gustaf Ullberg3cb61042019-10-24 15:52:10 +020065 void SetAudioBufferDelay(int delay_ms) override;
Per Åhgren8ee1ec82021-03-11 06:33:45 +000066 void SetCaptureOutputUsage(bool capture_output_used) override;
Per Åhgrend0fa8202018-04-18 09:35:13 +020067
peahd0263542017-01-03 04:20:34 -080068 private:
peahd0263542017-01-03 04:20:34 -080069 static int instance_count_;
70 std::unique_ptr<ApmDataDumper> data_dumper_;
Per Åhgren8ba58612017-12-01 23:01:44 +010071 const EchoCanceller3Config config_;
72 bool capture_properly_started_ = false;
73 bool render_properly_started_ = false;
peah69221db2017-01-27 03:28:19 -080074 const size_t sample_rate_hz_;
75 std::unique_ptr<RenderDelayBuffer> render_buffer_;
76 std::unique_ptr<RenderDelayController> delay_controller_;
77 std::unique_ptr<EchoRemover> echo_remover_;
peahe985b3f2017-02-28 22:08:53 -080078 BlockProcessorMetrics metrics_;
Per Åhgren8ba58612017-12-01 23:01:44 +010079 RenderDelayBuffer::BufferingEvent render_event_;
Per Åhgrenc59a5762017-12-11 21:34:19 +010080 size_t capture_call_counter_ = 0;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020081 absl::optional<DelayEstimate> estimated_delay_;
peahd0263542017-01-03 04:20:34 -080082};
83
84int BlockProcessorImpl::instance_count_ = 0;
85
peah69221db2017-01-27 03:28:19 -080086BlockProcessorImpl::BlockProcessorImpl(
Per Åhgren8ba58612017-12-01 23:01:44 +010087 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -080088 int sample_rate_hz,
Per Åhgrence202a02019-09-02 17:01:19 +020089 size_t num_render_channels,
90 size_t num_capture_channels,
peah69221db2017-01-27 03:28:19 -080091 std::unique_ptr<RenderDelayBuffer> render_buffer,
92 std::unique_ptr<RenderDelayController> delay_controller,
93 std::unique_ptr<EchoRemover> echo_remover)
94 : data_dumper_(
95 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Per Åhgren8ba58612017-12-01 23:01:44 +010096 config_(config),
peah69221db2017-01-27 03:28:19 -080097 sample_rate_hz_(sample_rate_hz),
98 render_buffer_(std::move(render_buffer)),
99 delay_controller_(std::move(delay_controller)),
Per Åhgren8ba58612017-12-01 23:01:44 +0100100 echo_remover_(std::move(echo_remover)),
101 render_event_(RenderDelayBuffer::BufferingEvent::kNone) {
peah21920892017-02-08 05:08:56 -0800102 RTC_DCHECK(ValidFullBandRate(sample_rate_hz_));
103}
peahd0263542017-01-03 04:20:34 -0800104
105BlockProcessorImpl::~BlockProcessorImpl() = default;
106
107void BlockProcessorImpl::ProcessCapture(
peah69221db2017-01-27 03:28:19 -0800108 bool echo_path_gain_change,
109 bool capture_signal_saturation,
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100110 std::vector<std::vector<std::vector<float>>>* linear_output,
Per Åhgrence202a02019-09-02 17:01:19 +0200111 std::vector<std::vector<std::vector<float>>>* capture_block) {
peahd0263542017-01-03 04:20:34 -0800112 RTC_DCHECK(capture_block);
113 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), capture_block->size());
Per Åhgrence202a02019-09-02 17:01:19 +0200114 RTC_DCHECK_EQ(kBlockSize, (*capture_block)[0][0].size());
Per Åhgrenc59a5762017-12-11 21:34:19 +0100115
116 capture_call_counter_++;
117
peahcf02cf12017-04-05 14:18:07 -0700118 data_dumper_->DumpRaw("aec3_processblock_call_order",
119 static_cast<int>(BlockProcessorApiCall::kCapture));
120 data_dumper_->DumpWav("aec3_processblock_capture_input", kBlockSize,
Per Åhgrence202a02019-09-02 17:01:19 +0200121 &(*capture_block)[0][0][0], 16000, 1);
peah69221db2017-01-27 03:28:19 -0800122
Per Åhgren8ba58612017-12-01 23:01:44 +0100123 if (render_properly_started_) {
124 if (!capture_properly_started_) {
125 capture_properly_started_ = true;
126 render_buffer_->Reset();
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200127 if (delay_controller_)
128 delay_controller_->Reset(true);
Per Åhgren8ba58612017-12-01 23:01:44 +0100129 }
130 } else {
131 // If no render data has yet arrived, do not process the capture signal.
Sam Zackrissonff571c62020-07-27 15:45:30 +0200132 render_buffer_->HandleSkippedCaptureProcessing();
peahcf02cf12017-04-05 14:18:07 -0700133 return;
peah69221db2017-01-27 03:28:19 -0800134 }
135
Per Åhgren8ba58612017-12-01 23:01:44 +0100136 EchoPathVariability echo_path_variability(
137 echo_path_gain_change, EchoPathVariability::DelayAdjustment::kNone,
138 false);
139
140 if (render_event_ == RenderDelayBuffer::BufferingEvent::kRenderOverrun &&
141 render_properly_started_) {
142 echo_path_variability.delay_change =
143 EchoPathVariability::DelayAdjustment::kBufferFlush;
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200144 if (delay_controller_)
145 delay_controller_->Reset(true);
Per Åhgrenc59a5762017-12-11 21:34:19 +0100146 RTC_LOG(LS_WARNING) << "Reset due to render buffer overrun at block "
147 << capture_call_counter_;
Per Åhgren8ba58612017-12-01 23:01:44 +0100148 }
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100149 render_event_ = RenderDelayBuffer::BufferingEvent::kNone;
Per Åhgren8ba58612017-12-01 23:01:44 +0100150
151 // Update the render buffers with any newly arrived render blocks and prepare
152 // the render buffers for reading the render data corresponding to the current
153 // capture block.
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100154 RenderDelayBuffer::BufferingEvent buffer_event =
155 render_buffer_->PrepareCaptureProcessing();
156 // Reset the delay controller at render buffer underrun.
157 if (buffer_event == RenderDelayBuffer::BufferingEvent::kRenderUnderrun) {
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200158 if (delay_controller_)
159 delay_controller_->Reset(false);
Per Åhgren8ba58612017-12-01 23:01:44 +0100160 }
161
peahcf02cf12017-04-05 14:18:07 -0700162 data_dumper_->DumpWav("aec3_processblock_capture_input2", kBlockSize,
Per Åhgrence202a02019-09-02 17:01:19 +0200163 &(*capture_block)[0][0][0], 16000, 1);
peahcf02cf12017-04-05 14:18:07 -0700164
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200165 bool has_delay_estimator = !config_.delay.use_external_delay_estimator;
166 if (has_delay_estimator) {
167 RTC_DCHECK(delay_controller_);
168 // Compute and apply the render delay required to achieve proper signal
169 // alignment.
170 estimated_delay_ = delay_controller_->GetDelay(
171 render_buffer_->GetDownsampledRenderBuffer(), render_buffer_->Delay(),
Gustaf Ullbergee84d392019-09-10 09:36:43 +0200172 (*capture_block)[0]);
peah4b572d02017-04-06 16:33:06 -0700173
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200174 if (estimated_delay_) {
175 bool delay_change =
176 render_buffer_->AlignFromDelay(estimated_delay_->delay);
177 if (delay_change) {
Sam Zackrissonffc84522019-10-15 13:43:02 +0200178 rtc::LoggingSeverity log_level =
179 config_.delay.log_warning_on_delay_changes ? rtc::LS_WARNING
180 : rtc::LS_INFO;
181 RTC_LOG_V(log_level) << "Delay changed to " << estimated_delay_->delay
182 << " at block " << capture_call_counter_;
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200183 echo_path_variability.delay_change =
184 EchoPathVariability::DelayAdjustment::kNewDetectedDelay;
185 }
Per Åhgrenc59a5762017-12-11 21:34:19 +0100186 }
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200187
188 echo_path_variability.clock_drift = delay_controller_->HasClockdrift();
189
190 } else {
191 render_buffer_->AlignFromExternalDelay();
peah96b951c2017-08-22 10:26:07 -0700192 }
peah4b572d02017-04-06 16:33:06 -0700193
194 // Remove the echo from the capture signal.
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200195 if (has_delay_estimator || render_buffer_->HasReceivedBufferDelay()) {
196 echo_remover_->ProcessCapture(
197 echo_path_variability, capture_signal_saturation, estimated_delay_,
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100198 render_buffer_->GetRenderBuffer(), linear_output, capture_block);
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200199 }
peah4b572d02017-04-06 16:33:06 -0700200
peahcf02cf12017-04-05 14:18:07 -0700201 // Update the metrics.
Per Åhgren8ba58612017-12-01 23:01:44 +0100202 metrics_.UpdateCapture(false);
peahd0263542017-01-03 04:20:34 -0800203}
204
peahcf02cf12017-04-05 14:18:07 -0700205void BlockProcessorImpl::BufferRender(
Per Åhgrence202a02019-09-02 17:01:19 +0200206 const std::vector<std::vector<std::vector<float>>>& block) {
peahcf02cf12017-04-05 14:18:07 -0700207 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), block.size());
Per Åhgrence202a02019-09-02 17:01:19 +0200208 RTC_DCHECK_EQ(kBlockSize, block[0][0].size());
peahcf02cf12017-04-05 14:18:07 -0700209 data_dumper_->DumpRaw("aec3_processblock_call_order",
210 static_cast<int>(BlockProcessorApiCall::kRender));
211 data_dumper_->DumpWav("aec3_processblock_render_input", kBlockSize,
Per Åhgrence202a02019-09-02 17:01:19 +0200212 &block[0][0][0], 16000, 1);
peahcf02cf12017-04-05 14:18:07 -0700213 data_dumper_->DumpWav("aec3_processblock_render_input2", kBlockSize,
Per Åhgrence202a02019-09-02 17:01:19 +0200214 &block[0][0][0], 16000, 1);
peahcf02cf12017-04-05 14:18:07 -0700215
Per Åhgren8ba58612017-12-01 23:01:44 +0100216 render_event_ = render_buffer_->Insert(block);
peahcf02cf12017-04-05 14:18:07 -0700217
Per Åhgren8ba58612017-12-01 23:01:44 +0100218 metrics_.UpdateRender(render_event_ !=
219 RenderDelayBuffer::BufferingEvent::kNone);
220
221 render_properly_started_ = true;
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200222 if (delay_controller_)
223 delay_controller_->LogRenderCall();
peahd0263542017-01-03 04:20:34 -0800224}
225
peah69221db2017-01-27 03:28:19 -0800226void BlockProcessorImpl::UpdateEchoLeakageStatus(bool leakage_detected) {
227 echo_remover_->UpdateEchoLeakageStatus(leakage_detected);
228}
peahd0263542017-01-03 04:20:34 -0800229
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100230void BlockProcessorImpl::GetMetrics(EchoControl::Metrics* metrics) const {
231 echo_remover_->GetMetrics(metrics);
Per Åhgrence202a02019-09-02 17:01:19 +0200232 constexpr int block_size_ms = 4;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200233 absl::optional<size_t> delay = render_buffer_->Delay();
Per Åhgrenc59a5762017-12-11 21:34:19 +0100234 metrics->delay_ms = delay ? static_cast<int>(*delay) * block_size_ms : 0;
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100235}
236
Gustaf Ullberg3cb61042019-10-24 15:52:10 +0200237void BlockProcessorImpl::SetAudioBufferDelay(int delay_ms) {
Per Åhgrend0fa8202018-04-18 09:35:13 +0200238 render_buffer_->SetAudioBufferDelay(delay_ms);
239}
240
Per Åhgren8ee1ec82021-03-11 06:33:45 +0000241void BlockProcessorImpl::SetCaptureOutputUsage(bool capture_output_used) {
242 echo_remover_->SetCaptureOutputUsage(capture_output_used);
243}
244
peahd0263542017-01-03 04:20:34 -0800245} // namespace
246
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200247BlockProcessor* BlockProcessor::Create(const EchoCanceller3Config& config,
Per Åhgrence202a02019-09-02 17:01:19 +0200248 int sample_rate_hz,
249 size_t num_render_channels,
250 size_t num_capture_channels) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100251 std::unique_ptr<RenderDelayBuffer> render_buffer(
Per Åhgrence202a02019-09-02 17:01:19 +0200252 RenderDelayBuffer::Create(config, sample_rate_hz, num_render_channels));
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200253 std::unique_ptr<RenderDelayController> delay_controller;
254 if (!config.delay.use_external_delay_estimator) {
Per Åhgren6a05bb12019-12-03 11:24:59 +0100255 delay_controller.reset(RenderDelayController::Create(config, sample_rate_hz,
256 num_capture_channels));
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200257 }
Per Åhgrence202a02019-09-02 17:01:19 +0200258 std::unique_ptr<EchoRemover> echo_remover(EchoRemover::Create(
259 config, sample_rate_hz, num_render_channels, num_capture_channels));
260 return Create(config, sample_rate_hz, num_render_channels,
261 num_capture_channels, std::move(render_buffer),
peah69221db2017-01-27 03:28:19 -0800262 std::move(delay_controller), std::move(echo_remover));
263}
264
265BlockProcessor* BlockProcessor::Create(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200266 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -0800267 int sample_rate_hz,
Per Åhgrence202a02019-09-02 17:01:19 +0200268 size_t num_render_channels,
269 size_t num_capture_channels,
peah69221db2017-01-27 03:28:19 -0800270 std::unique_ptr<RenderDelayBuffer> render_buffer) {
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200271 std::unique_ptr<RenderDelayController> delay_controller;
272 if (!config.delay.use_external_delay_estimator) {
Per Åhgren6a05bb12019-12-03 11:24:59 +0100273 delay_controller.reset(RenderDelayController::Create(config, sample_rate_hz,
274 num_capture_channels));
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200275 }
Per Åhgrence202a02019-09-02 17:01:19 +0200276 std::unique_ptr<EchoRemover> echo_remover(EchoRemover::Create(
277 config, sample_rate_hz, num_render_channels, num_capture_channels));
278 return Create(config, sample_rate_hz, num_render_channels,
279 num_capture_channels, std::move(render_buffer),
peah69221db2017-01-27 03:28:19 -0800280 std::move(delay_controller), std::move(echo_remover));
281}
282
283BlockProcessor* BlockProcessor::Create(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200284 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -0800285 int sample_rate_hz,
Per Åhgrence202a02019-09-02 17:01:19 +0200286 size_t num_render_channels,
287 size_t num_capture_channels,
peah69221db2017-01-27 03:28:19 -0800288 std::unique_ptr<RenderDelayBuffer> render_buffer,
289 std::unique_ptr<RenderDelayController> delay_controller,
290 std::unique_ptr<EchoRemover> echo_remover) {
Per Åhgrence202a02019-09-02 17:01:19 +0200291 return new BlockProcessorImpl(config, sample_rate_hz, num_render_channels,
292 num_capture_channels, std::move(render_buffer),
293 std::move(delay_controller),
294 std::move(echo_remover));
peahd0263542017-01-03 04:20:34 -0800295}
296
297} // namespace webrtc