blob: fa9c7b3fb38a65a21e42c8f969814d46221af78d [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,
55 std::vector<std::vector<std::vector<float>>>* capture_block) override;
peahd0263542017-01-03 04:20:34 -080056
Per Åhgrence202a02019-09-02 17:01:19 +020057 void BufferRender(
58 const std::vector<std::vector<std::vector<float>>>& block) override;
peahd0263542017-01-03 04:20:34 -080059
peah69221db2017-01-27 03:28:19 -080060 void UpdateEchoLeakageStatus(bool leakage_detected) override;
peahd0263542017-01-03 04:20:34 -080061
Gustaf Ullberg332150d2017-11-22 14:17:39 +010062 void GetMetrics(EchoControl::Metrics* metrics) const override;
63
Per Åhgrend0fa8202018-04-18 09:35:13 +020064 void SetAudioBufferDelay(size_t delay_ms) override;
65
peahd0263542017-01-03 04:20:34 -080066 private:
peahd0263542017-01-03 04:20:34 -080067 static int instance_count_;
68 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
82int BlockProcessorImpl::instance_count_ = 0;
83
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)
92 : data_dumper_(
93 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Per Åhgren8ba58612017-12-01 23:01:44 +010094 config_(config),
peah69221db2017-01-27 03:28:19 -080095 sample_rate_hz_(sample_rate_hz),
96 render_buffer_(std::move(render_buffer)),
97 delay_controller_(std::move(delay_controller)),
Per Åhgren8ba58612017-12-01 23:01:44 +010098 echo_remover_(std::move(echo_remover)),
99 render_event_(RenderDelayBuffer::BufferingEvent::kNone) {
peah21920892017-02-08 05:08:56 -0800100 RTC_DCHECK(ValidFullBandRate(sample_rate_hz_));
101}
peahd0263542017-01-03 04:20:34 -0800102
103BlockProcessorImpl::~BlockProcessorImpl() = default;
104
105void BlockProcessorImpl::ProcessCapture(
peah69221db2017-01-27 03:28:19 -0800106 bool echo_path_gain_change,
107 bool capture_signal_saturation,
Per Åhgrence202a02019-09-02 17:01:19 +0200108 std::vector<std::vector<std::vector<float>>>* capture_block) {
peahd0263542017-01-03 04:20:34 -0800109 RTC_DCHECK(capture_block);
110 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), capture_block->size());
Per Åhgrence202a02019-09-02 17:01:19 +0200111 RTC_DCHECK_EQ(kBlockSize, (*capture_block)[0][0].size());
Per Åhgrenc59a5762017-12-11 21:34:19 +0100112
113 capture_call_counter_++;
114
peahcf02cf12017-04-05 14:18:07 -0700115 data_dumper_->DumpRaw("aec3_processblock_call_order",
116 static_cast<int>(BlockProcessorApiCall::kCapture));
117 data_dumper_->DumpWav("aec3_processblock_capture_input", kBlockSize,
Per Åhgrence202a02019-09-02 17:01:19 +0200118 &(*capture_block)[0][0][0], 16000, 1);
peah69221db2017-01-27 03:28:19 -0800119
Per Åhgren8ba58612017-12-01 23:01:44 +0100120 if (render_properly_started_) {
121 if (!capture_properly_started_) {
122 capture_properly_started_ = true;
123 render_buffer_->Reset();
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200124 if (delay_controller_)
125 delay_controller_->Reset(true);
Per Åhgren8ba58612017-12-01 23:01:44 +0100126 }
127 } else {
128 // If no render data has yet arrived, do not process the capture signal.
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
peahcf02cf12017-04-05 14:18:07 -0700158 data_dumper_->DumpWav("aec3_processblock_capture_input2", kBlockSize,
Per Åhgrence202a02019-09-02 17:01:19 +0200159 &(*capture_block)[0][0][0], 16000, 1);
peahcf02cf12017-04-05 14:18:07 -0700160
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200161 bool has_delay_estimator = !config_.delay.use_external_delay_estimator;
162 if (has_delay_estimator) {
163 RTC_DCHECK(delay_controller_);
164 // Compute and apply the render delay required to achieve proper signal
165 // alignment.
166 estimated_delay_ = delay_controller_->GetDelay(
167 render_buffer_->GetDownsampledRenderBuffer(), render_buffer_->Delay(),
Gustaf Ullbergee84d392019-09-10 09:36:43 +0200168 (*capture_block)[0]);
peah4b572d02017-04-06 16:33:06 -0700169
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200170 if (estimated_delay_) {
171 bool delay_change =
172 render_buffer_->AlignFromDelay(estimated_delay_->delay);
173 if (delay_change) {
Sam Zackrissonffc84522019-10-15 13:43:02 +0200174 rtc::LoggingSeverity log_level =
175 config_.delay.log_warning_on_delay_changes ? rtc::LS_WARNING
176 : rtc::LS_INFO;
177 RTC_LOG_V(log_level) << "Delay changed to " << estimated_delay_->delay
178 << " at block " << capture_call_counter_;
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200179 echo_path_variability.delay_change =
180 EchoPathVariability::DelayAdjustment::kNewDetectedDelay;
181 }
Per Åhgrenc59a5762017-12-11 21:34:19 +0100182 }
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200183
184 echo_path_variability.clock_drift = delay_controller_->HasClockdrift();
185
186 } else {
187 render_buffer_->AlignFromExternalDelay();
peah96b951c2017-08-22 10:26:07 -0700188 }
peah4b572d02017-04-06 16:33:06 -0700189
190 // Remove the echo from the capture signal.
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200191 if (has_delay_estimator || render_buffer_->HasReceivedBufferDelay()) {
192 echo_remover_->ProcessCapture(
193 echo_path_variability, capture_signal_saturation, estimated_delay_,
194 render_buffer_->GetRenderBuffer(), capture_block);
195 }
peah4b572d02017-04-06 16:33:06 -0700196
peahcf02cf12017-04-05 14:18:07 -0700197 // Update the metrics.
Per Åhgren8ba58612017-12-01 23:01:44 +0100198 metrics_.UpdateCapture(false);
peahd0263542017-01-03 04:20:34 -0800199}
200
peahcf02cf12017-04-05 14:18:07 -0700201void BlockProcessorImpl::BufferRender(
Per Åhgrence202a02019-09-02 17:01:19 +0200202 const std::vector<std::vector<std::vector<float>>>& block) {
peahcf02cf12017-04-05 14:18:07 -0700203 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), block.size());
Per Åhgrence202a02019-09-02 17:01:19 +0200204 RTC_DCHECK_EQ(kBlockSize, block[0][0].size());
peahcf02cf12017-04-05 14:18:07 -0700205 data_dumper_->DumpRaw("aec3_processblock_call_order",
206 static_cast<int>(BlockProcessorApiCall::kRender));
207 data_dumper_->DumpWav("aec3_processblock_render_input", kBlockSize,
Per Åhgrence202a02019-09-02 17:01:19 +0200208 &block[0][0][0], 16000, 1);
peahcf02cf12017-04-05 14:18:07 -0700209 data_dumper_->DumpWav("aec3_processblock_render_input2", kBlockSize,
Per Åhgrence202a02019-09-02 17:01:19 +0200210 &block[0][0][0], 16000, 1);
peahcf02cf12017-04-05 14:18:07 -0700211
Per Åhgren8ba58612017-12-01 23:01:44 +0100212 render_event_ = render_buffer_->Insert(block);
peahcf02cf12017-04-05 14:18:07 -0700213
Per Åhgren8ba58612017-12-01 23:01:44 +0100214 metrics_.UpdateRender(render_event_ !=
215 RenderDelayBuffer::BufferingEvent::kNone);
216
217 render_properly_started_ = true;
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200218 if (delay_controller_)
219 delay_controller_->LogRenderCall();
peahd0263542017-01-03 04:20:34 -0800220}
221
peah69221db2017-01-27 03:28:19 -0800222void BlockProcessorImpl::UpdateEchoLeakageStatus(bool leakage_detected) {
223 echo_remover_->UpdateEchoLeakageStatus(leakage_detected);
224}
peahd0263542017-01-03 04:20:34 -0800225
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100226void BlockProcessorImpl::GetMetrics(EchoControl::Metrics* metrics) const {
227 echo_remover_->GetMetrics(metrics);
Per Åhgrence202a02019-09-02 17:01:19 +0200228 constexpr int block_size_ms = 4;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200229 absl::optional<size_t> delay = render_buffer_->Delay();
Per Åhgrenc59a5762017-12-11 21:34:19 +0100230 metrics->delay_ms = delay ? static_cast<int>(*delay) * block_size_ms : 0;
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100231}
232
Per Åhgrend0fa8202018-04-18 09:35:13 +0200233void BlockProcessorImpl::SetAudioBufferDelay(size_t delay_ms) {
234 render_buffer_->SetAudioBufferDelay(delay_ms);
235}
236
peahd0263542017-01-03 04:20:34 -0800237} // namespace
238
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200239BlockProcessor* BlockProcessor::Create(const EchoCanceller3Config& config,
Per Åhgrence202a02019-09-02 17:01:19 +0200240 int sample_rate_hz,
241 size_t num_render_channels,
242 size_t num_capture_channels) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100243 std::unique_ptr<RenderDelayBuffer> render_buffer(
Per Åhgrence202a02019-09-02 17:01:19 +0200244 RenderDelayBuffer::Create(config, sample_rate_hz, num_render_channels));
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200245 std::unique_ptr<RenderDelayController> delay_controller;
246 if (!config.delay.use_external_delay_estimator) {
247 delay_controller.reset(
248 RenderDelayController::Create(config, sample_rate_hz));
249 }
Per Åhgrence202a02019-09-02 17:01:19 +0200250 std::unique_ptr<EchoRemover> echo_remover(EchoRemover::Create(
251 config, sample_rate_hz, num_render_channels, num_capture_channels));
252 return Create(config, sample_rate_hz, num_render_channels,
253 num_capture_channels, std::move(render_buffer),
peah69221db2017-01-27 03:28:19 -0800254 std::move(delay_controller), std::move(echo_remover));
255}
256
257BlockProcessor* BlockProcessor::Create(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200258 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -0800259 int sample_rate_hz,
Per Åhgrence202a02019-09-02 17:01:19 +0200260 size_t num_render_channels,
261 size_t num_capture_channels,
peah69221db2017-01-27 03:28:19 -0800262 std::unique_ptr<RenderDelayBuffer> render_buffer) {
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200263 std::unique_ptr<RenderDelayController> delay_controller;
264 if (!config.delay.use_external_delay_estimator) {
265 delay_controller.reset(
266 RenderDelayController::Create(config, sample_rate_hz));
267 }
Per Åhgrence202a02019-09-02 17:01:19 +0200268 std::unique_ptr<EchoRemover> echo_remover(EchoRemover::Create(
269 config, sample_rate_hz, num_render_channels, num_capture_channels));
270 return Create(config, sample_rate_hz, num_render_channels,
271 num_capture_channels, std::move(render_buffer),
peah69221db2017-01-27 03:28:19 -0800272 std::move(delay_controller), std::move(echo_remover));
273}
274
275BlockProcessor* BlockProcessor::Create(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200276 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -0800277 int sample_rate_hz,
Per Åhgrence202a02019-09-02 17:01:19 +0200278 size_t num_render_channels,
279 size_t num_capture_channels,
peah69221db2017-01-27 03:28:19 -0800280 std::unique_ptr<RenderDelayBuffer> render_buffer,
281 std::unique_ptr<RenderDelayController> delay_controller,
282 std::unique_ptr<EchoRemover> echo_remover) {
Per Åhgrence202a02019-09-02 17:01:19 +0200283 return new BlockProcessorImpl(config, sample_rate_hz, num_render_channels,
284 num_capture_channels, std::move(render_buffer),
285 std::move(delay_controller),
286 std::move(echo_remover));
peahd0263542017-01-03 04:20:34 -0800287}
288
289} // namespace webrtc