blob: 11b8aa62ee4c730a1bb3a668850e1dddbc78416e [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
Niels Möller7a669002022-06-27 09:47:02 +020014#include <atomic>
Gustaf Ullberge47433f2019-01-24 16:00:57 +010015#include <memory>
Yves Gerey988cc082018-10-23 12:03:01 +020016#include <utility>
Gustaf Ullberge47433f2019-01-24 16:00:57 +010017#include <vector>
Yves Gerey988cc082018-10-23 12:03:01 +020018
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020019#include "absl/types/optional.h"
Gustaf Ullberge47433f2019-01-24 16:00:57 +010020#include "api/audio/echo_canceller3_config.h"
21#include "api/audio/echo_control.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/audio_processing/aec3/aec3_common.h"
23#include "modules/audio_processing/aec3/block_processor_metrics.h"
Yves Gerey988cc082018-10-23 12:03:01 +020024#include "modules/audio_processing/aec3/delay_estimate.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_processing/aec3/echo_path_variability.h"
Gustaf Ullberge47433f2019-01-24 16:00:57 +010026#include "modules/audio_processing/aec3/echo_remover.h"
27#include "modules/audio_processing/aec3/render_delay_buffer.h"
28#include "modules/audio_processing/aec3/render_delay_controller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "modules/audio_processing/logging/apm_data_dumper.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
Gustaf Ullbergd3ead1a2022-05-23 10:39:53 +020052 void ProcessCapture(bool echo_path_gain_change,
53 bool capture_signal_saturation,
54 Block* linear_output,
55 Block* capture_block) override;
peahd0263542017-01-03 04:20:34 -080056
Gustaf Ullbergd3ead1a2022-05-23 10:39:53 +020057 void BufferRender(const Block& block) override;
peahd0263542017-01-03 04:20:34 -080058
peah69221db2017-01-27 03:28:19 -080059 void UpdateEchoLeakageStatus(bool leakage_detected) override;
peahd0263542017-01-03 04:20:34 -080060
Gustaf Ullberg332150d2017-11-22 14:17:39 +010061 void GetMetrics(EchoControl::Metrics* metrics) const override;
62
Gustaf Ullberg3cb61042019-10-24 15:52:10 +020063 void SetAudioBufferDelay(int delay_ms) override;
Per Åhgren8ee1ec82021-03-11 06:33:45 +000064 void SetCaptureOutputUsage(bool capture_output_used) override;
Per Åhgrend0fa8202018-04-18 09:35:13 +020065
peahd0263542017-01-03 04:20:34 -080066 private:
Niels Möller7a669002022-06-27 09:47:02 +020067 static std::atomic<int> instance_count_;
peahd0263542017-01-03 04:20:34 -080068 std::unique_ptr<ApmDataDumper> data_dumper_;
Per Åhgren8ba58612017-12-01 23:01:44 +010069 const EchoCanceller3Config config_;
70 bool capture_properly_started_ = false;
71 bool render_properly_started_ = false;
peah69221db2017-01-27 03:28:19 -080072 const size_t sample_rate_hz_;
73 std::unique_ptr<RenderDelayBuffer> render_buffer_;
74 std::unique_ptr<RenderDelayController> delay_controller_;
75 std::unique_ptr<EchoRemover> echo_remover_;
peahe985b3f2017-02-28 22:08:53 -080076 BlockProcessorMetrics metrics_;
Per Åhgren8ba58612017-12-01 23:01:44 +010077 RenderDelayBuffer::BufferingEvent render_event_;
Per Åhgrenc59a5762017-12-11 21:34:19 +010078 size_t capture_call_counter_ = 0;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020079 absl::optional<DelayEstimate> estimated_delay_;
peahd0263542017-01-03 04:20:34 -080080};
81
Niels Möller7a669002022-06-27 09:47:02 +020082std::atomic<int> BlockProcessorImpl::instance_count_(0);
peahd0263542017-01-03 04:20:34 -080083
peah69221db2017-01-27 03:28:19 -080084BlockProcessorImpl::BlockProcessorImpl(
Per Åhgren8ba58612017-12-01 23:01:44 +010085 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -080086 int sample_rate_hz,
Per Åhgrence202a02019-09-02 17:01:19 +020087 size_t num_render_channels,
88 size_t num_capture_channels,
peah69221db2017-01-27 03:28:19 -080089 std::unique_ptr<RenderDelayBuffer> render_buffer,
90 std::unique_ptr<RenderDelayController> delay_controller,
91 std::unique_ptr<EchoRemover> echo_remover)
Niels Möller7a669002022-06-27 09:47:02 +020092 : data_dumper_(new ApmDataDumper(instance_count_.fetch_add(1) + 1)),
Per Åhgren8ba58612017-12-01 23:01:44 +010093 config_(config),
peah69221db2017-01-27 03:28:19 -080094 sample_rate_hz_(sample_rate_hz),
95 render_buffer_(std::move(render_buffer)),
96 delay_controller_(std::move(delay_controller)),
Per Åhgren8ba58612017-12-01 23:01:44 +010097 echo_remover_(std::move(echo_remover)),
98 render_event_(RenderDelayBuffer::BufferingEvent::kNone) {
peah21920892017-02-08 05:08:56 -080099 RTC_DCHECK(ValidFullBandRate(sample_rate_hz_));
100}
peahd0263542017-01-03 04:20:34 -0800101
102BlockProcessorImpl::~BlockProcessorImpl() = default;
103
Gustaf Ullbergd3ead1a2022-05-23 10:39:53 +0200104void BlockProcessorImpl::ProcessCapture(bool echo_path_gain_change,
105 bool capture_signal_saturation,
106 Block* linear_output,
107 Block* capture_block) {
peahd0263542017-01-03 04:20:34 -0800108 RTC_DCHECK(capture_block);
Gustaf Ullbergd3ead1a2022-05-23 10:39:53 +0200109 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), capture_block->NumBands());
Per Åhgrenc59a5762017-12-11 21:34:19 +0100110
111 capture_call_counter_++;
112
peahcf02cf12017-04-05 14:18:07 -0700113 data_dumper_->DumpRaw("aec3_processblock_call_order",
114 static_cast<int>(BlockProcessorApiCall::kCapture));
Gustaf Ullbergd3ead1a2022-05-23 10:39:53 +0200115 data_dumper_->DumpWav("aec3_processblock_capture_input",
116 capture_block->View(/*band=*/0, /*channel=*/0), 16000,
117 1);
peah69221db2017-01-27 03:28:19 -0800118
Per Åhgren8ba58612017-12-01 23:01:44 +0100119 if (render_properly_started_) {
120 if (!capture_properly_started_) {
121 capture_properly_started_ = true;
122 render_buffer_->Reset();
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200123 if (delay_controller_)
124 delay_controller_->Reset(true);
Per Åhgren8ba58612017-12-01 23:01:44 +0100125 }
126 } else {
127 // If no render data has yet arrived, do not process the capture signal.
Sam Zackrissonff571c62020-07-27 15:45:30 +0200128 render_buffer_->HandleSkippedCaptureProcessing();
peahcf02cf12017-04-05 14:18:07 -0700129 return;
peah69221db2017-01-27 03:28:19 -0800130 }
131
Per Åhgren8ba58612017-12-01 23:01:44 +0100132 EchoPathVariability echo_path_variability(
133 echo_path_gain_change, EchoPathVariability::DelayAdjustment::kNone,
134 false);
135
136 if (render_event_ == RenderDelayBuffer::BufferingEvent::kRenderOverrun &&
137 render_properly_started_) {
138 echo_path_variability.delay_change =
139 EchoPathVariability::DelayAdjustment::kBufferFlush;
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200140 if (delay_controller_)
141 delay_controller_->Reset(true);
Per Åhgrenc59a5762017-12-11 21:34:19 +0100142 RTC_LOG(LS_WARNING) << "Reset due to render buffer overrun at block "
143 << capture_call_counter_;
Per Åhgren8ba58612017-12-01 23:01:44 +0100144 }
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100145 render_event_ = RenderDelayBuffer::BufferingEvent::kNone;
Per Åhgren8ba58612017-12-01 23:01:44 +0100146
147 // Update the render buffers with any newly arrived render blocks and prepare
148 // the render buffers for reading the render data corresponding to the current
149 // capture block.
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100150 RenderDelayBuffer::BufferingEvent buffer_event =
151 render_buffer_->PrepareCaptureProcessing();
152 // Reset the delay controller at render buffer underrun.
153 if (buffer_event == RenderDelayBuffer::BufferingEvent::kRenderUnderrun) {
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200154 if (delay_controller_)
155 delay_controller_->Reset(false);
Per Åhgren8ba58612017-12-01 23:01:44 +0100156 }
157
Gustaf Ullbergd3ead1a2022-05-23 10:39:53 +0200158 data_dumper_->DumpWav("aec3_processblock_capture_input2",
159 capture_block->View(/*band=*/0, /*channel=*/0), 16000,
160 1);
peahcf02cf12017-04-05 14:18:07 -0700161
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200162 bool has_delay_estimator = !config_.delay.use_external_delay_estimator;
163 if (has_delay_estimator) {
164 RTC_DCHECK(delay_controller_);
165 // Compute and apply the render delay required to achieve proper signal
166 // alignment.
167 estimated_delay_ = delay_controller_->GetDelay(
168 render_buffer_->GetDownsampledRenderBuffer(), render_buffer_->Delay(),
Gustaf Ullbergd3ead1a2022-05-23 10:39:53 +0200169 *capture_block);
peah4b572d02017-04-06 16:33:06 -0700170
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200171 if (estimated_delay_) {
172 bool delay_change =
173 render_buffer_->AlignFromDelay(estimated_delay_->delay);
174 if (delay_change) {
Sam Zackrissonffc84522019-10-15 13:43:02 +0200175 rtc::LoggingSeverity log_level =
176 config_.delay.log_warning_on_delay_changes ? rtc::LS_WARNING
177 : rtc::LS_INFO;
178 RTC_LOG_V(log_level) << "Delay changed to " << estimated_delay_->delay
179 << " at block " << capture_call_counter_;
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200180 echo_path_variability.delay_change =
181 EchoPathVariability::DelayAdjustment::kNewDetectedDelay;
182 }
Per Åhgrenc59a5762017-12-11 21:34:19 +0100183 }
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200184
185 echo_path_variability.clock_drift = delay_controller_->HasClockdrift();
186
187 } else {
188 render_buffer_->AlignFromExternalDelay();
peah96b951c2017-08-22 10:26:07 -0700189 }
peah4b572d02017-04-06 16:33:06 -0700190
191 // Remove the echo from the capture signal.
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200192 if (has_delay_estimator || render_buffer_->HasReceivedBufferDelay()) {
193 echo_remover_->ProcessCapture(
194 echo_path_variability, capture_signal_saturation, estimated_delay_,
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100195 render_buffer_->GetRenderBuffer(), linear_output, capture_block);
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200196 }
peah4b572d02017-04-06 16:33:06 -0700197
peahcf02cf12017-04-05 14:18:07 -0700198 // Update the metrics.
Per Åhgren8ba58612017-12-01 23:01:44 +0100199 metrics_.UpdateCapture(false);
peahd0263542017-01-03 04:20:34 -0800200}
201
Gustaf Ullbergd3ead1a2022-05-23 10:39:53 +0200202void BlockProcessorImpl::BufferRender(const Block& block) {
203 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), block.NumBands());
peahcf02cf12017-04-05 14:18:07 -0700204 data_dumper_->DumpRaw("aec3_processblock_call_order",
205 static_cast<int>(BlockProcessorApiCall::kRender));
Gustaf Ullbergd3ead1a2022-05-23 10:39:53 +0200206 data_dumper_->DumpWav("aec3_processblock_render_input",
207 block.View(/*band=*/0, /*channel=*/0), 16000, 1);
208 data_dumper_->DumpWav("aec3_processblock_render_input2",
209 block.View(/*band=*/0, /*channel=*/0), 16000, 1);
peahcf02cf12017-04-05 14:18:07 -0700210
Per Åhgren8ba58612017-12-01 23:01:44 +0100211 render_event_ = render_buffer_->Insert(block);
peahcf02cf12017-04-05 14:18:07 -0700212
Per Åhgren8ba58612017-12-01 23:01:44 +0100213 metrics_.UpdateRender(render_event_ !=
214 RenderDelayBuffer::BufferingEvent::kNone);
215
216 render_properly_started_ = true;
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200217 if (delay_controller_)
218 delay_controller_->LogRenderCall();
peahd0263542017-01-03 04:20:34 -0800219}
220
peah69221db2017-01-27 03:28:19 -0800221void BlockProcessorImpl::UpdateEchoLeakageStatus(bool leakage_detected) {
222 echo_remover_->UpdateEchoLeakageStatus(leakage_detected);
223}
peahd0263542017-01-03 04:20:34 -0800224
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100225void BlockProcessorImpl::GetMetrics(EchoControl::Metrics* metrics) const {
226 echo_remover_->GetMetrics(metrics);
Per Åhgrence202a02019-09-02 17:01:19 +0200227 constexpr int block_size_ms = 4;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200228 absl::optional<size_t> delay = render_buffer_->Delay();
Per Åhgrenc59a5762017-12-11 21:34:19 +0100229 metrics->delay_ms = delay ? static_cast<int>(*delay) * block_size_ms : 0;
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100230}
231
Gustaf Ullberg3cb61042019-10-24 15:52:10 +0200232void BlockProcessorImpl::SetAudioBufferDelay(int delay_ms) {
Per Åhgrend0fa8202018-04-18 09:35:13 +0200233 render_buffer_->SetAudioBufferDelay(delay_ms);
234}
235
Per Åhgren8ee1ec82021-03-11 06:33:45 +0000236void BlockProcessorImpl::SetCaptureOutputUsage(bool capture_output_used) {
237 echo_remover_->SetCaptureOutputUsage(capture_output_used);
238}
239
peahd0263542017-01-03 04:20:34 -0800240} // namespace
241
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200242BlockProcessor* BlockProcessor::Create(const EchoCanceller3Config& config,
Per Åhgrence202a02019-09-02 17:01:19 +0200243 int sample_rate_hz,
244 size_t num_render_channels,
245 size_t num_capture_channels) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100246 std::unique_ptr<RenderDelayBuffer> render_buffer(
Per Åhgrence202a02019-09-02 17:01:19 +0200247 RenderDelayBuffer::Create(config, sample_rate_hz, num_render_channels));
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200248 std::unique_ptr<RenderDelayController> delay_controller;
249 if (!config.delay.use_external_delay_estimator) {
Per Åhgren6a05bb12019-12-03 11:24:59 +0100250 delay_controller.reset(RenderDelayController::Create(config, sample_rate_hz,
251 num_capture_channels));
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200252 }
Per Åhgrence202a02019-09-02 17:01:19 +0200253 std::unique_ptr<EchoRemover> echo_remover(EchoRemover::Create(
254 config, sample_rate_hz, num_render_channels, num_capture_channels));
255 return Create(config, sample_rate_hz, num_render_channels,
256 num_capture_channels, 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,
Per Åhgrence202a02019-09-02 17:01:19 +0200263 size_t num_render_channels,
264 size_t num_capture_channels,
peah69221db2017-01-27 03:28:19 -0800265 std::unique_ptr<RenderDelayBuffer> render_buffer) {
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200266 std::unique_ptr<RenderDelayController> delay_controller;
267 if (!config.delay.use_external_delay_estimator) {
Per Åhgren6a05bb12019-12-03 11:24:59 +0100268 delay_controller.reset(RenderDelayController::Create(config, sample_rate_hz,
269 num_capture_channels));
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200270 }
Per Åhgrence202a02019-09-02 17:01:19 +0200271 std::unique_ptr<EchoRemover> echo_remover(EchoRemover::Create(
272 config, sample_rate_hz, num_render_channels, num_capture_channels));
273 return Create(config, sample_rate_hz, num_render_channels,
274 num_capture_channels, std::move(render_buffer),
peah69221db2017-01-27 03:28:19 -0800275 std::move(delay_controller), std::move(echo_remover));
276}
277
278BlockProcessor* BlockProcessor::Create(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200279 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -0800280 int sample_rate_hz,
Per Åhgrence202a02019-09-02 17:01:19 +0200281 size_t num_render_channels,
282 size_t num_capture_channels,
peah69221db2017-01-27 03:28:19 -0800283 std::unique_ptr<RenderDelayBuffer> render_buffer,
284 std::unique_ptr<RenderDelayController> delay_controller,
285 std::unique_ptr<EchoRemover> echo_remover) {
Per Åhgrence202a02019-09-02 17:01:19 +0200286 return new BlockProcessorImpl(config, sample_rate_hz, num_render_channels,
287 num_capture_channels, std::move(render_buffer),
288 std::move(delay_controller),
289 std::move(echo_remover));
peahd0263542017-01-03 04:20:34 -0800290}
291
292} // namespace webrtc