blob: 184248fc23dcb01ca35d7ee3c90305655803c9cd [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,
peah69221db2017-01-27 03:28:19 -080042 std::unique_ptr<RenderDelayBuffer> render_buffer,
43 std::unique_ptr<RenderDelayController> delay_controller,
44 std::unique_ptr<EchoRemover> echo_remover);
45
Gustaf Ullberge47433f2019-01-24 16:00:57 +010046 BlockProcessorImpl() = delete;
47
peahd0263542017-01-03 04:20:34 -080048 ~BlockProcessorImpl() override;
49
Per Åhgrend112c752019-09-02 13:56:56 +000050 void ProcessCapture(bool echo_path_gain_change,
51 bool capture_signal_saturation,
52 std::vector<std::vector<float>>* capture_block) override;
peahd0263542017-01-03 04:20:34 -080053
Per Åhgrend112c752019-09-02 13:56:56 +000054 void BufferRender(const std::vector<std::vector<float>>& block) override;
peahd0263542017-01-03 04:20:34 -080055
peah69221db2017-01-27 03:28:19 -080056 void UpdateEchoLeakageStatus(bool leakage_detected) override;
peahd0263542017-01-03 04:20:34 -080057
Gustaf Ullberg332150d2017-11-22 14:17:39 +010058 void GetMetrics(EchoControl::Metrics* metrics) const override;
59
Per Åhgrend0fa8202018-04-18 09:35:13 +020060 void SetAudioBufferDelay(size_t delay_ms) override;
61
peahd0263542017-01-03 04:20:34 -080062 private:
peahd0263542017-01-03 04:20:34 -080063 static int instance_count_;
64 std::unique_ptr<ApmDataDumper> data_dumper_;
Per Åhgren8ba58612017-12-01 23:01:44 +010065 const EchoCanceller3Config config_;
66 bool capture_properly_started_ = false;
67 bool render_properly_started_ = false;
peah69221db2017-01-27 03:28:19 -080068 const size_t sample_rate_hz_;
69 std::unique_ptr<RenderDelayBuffer> render_buffer_;
70 std::unique_ptr<RenderDelayController> delay_controller_;
71 std::unique_ptr<EchoRemover> echo_remover_;
peahe985b3f2017-02-28 22:08:53 -080072 BlockProcessorMetrics metrics_;
Per Åhgren8ba58612017-12-01 23:01:44 +010073 RenderDelayBuffer::BufferingEvent render_event_;
Per Åhgrenc59a5762017-12-11 21:34:19 +010074 size_t capture_call_counter_ = 0;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020075 absl::optional<DelayEstimate> estimated_delay_;
peahd0263542017-01-03 04:20:34 -080076};
77
78int BlockProcessorImpl::instance_count_ = 0;
79
peah69221db2017-01-27 03:28:19 -080080BlockProcessorImpl::BlockProcessorImpl(
Per Åhgren8ba58612017-12-01 23:01:44 +010081 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -080082 int sample_rate_hz,
83 std::unique_ptr<RenderDelayBuffer> render_buffer,
84 std::unique_ptr<RenderDelayController> delay_controller,
85 std::unique_ptr<EchoRemover> echo_remover)
86 : data_dumper_(
87 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Per Åhgren8ba58612017-12-01 23:01:44 +010088 config_(config),
peah69221db2017-01-27 03:28:19 -080089 sample_rate_hz_(sample_rate_hz),
90 render_buffer_(std::move(render_buffer)),
91 delay_controller_(std::move(delay_controller)),
Per Åhgren8ba58612017-12-01 23:01:44 +010092 echo_remover_(std::move(echo_remover)),
93 render_event_(RenderDelayBuffer::BufferingEvent::kNone) {
peah21920892017-02-08 05:08:56 -080094 RTC_DCHECK(ValidFullBandRate(sample_rate_hz_));
95}
peahd0263542017-01-03 04:20:34 -080096
97BlockProcessorImpl::~BlockProcessorImpl() = default;
98
99void BlockProcessorImpl::ProcessCapture(
peah69221db2017-01-27 03:28:19 -0800100 bool echo_path_gain_change,
101 bool capture_signal_saturation,
Per Åhgrend112c752019-09-02 13:56:56 +0000102 std::vector<std::vector<float>>* capture_block) {
peahd0263542017-01-03 04:20:34 -0800103 RTC_DCHECK(capture_block);
104 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), capture_block->size());
Per Åhgrend112c752019-09-02 13:56:56 +0000105 RTC_DCHECK_EQ(kBlockSize, (*capture_block)[0].size());
Per Åhgrenc59a5762017-12-11 21:34:19 +0100106
107 capture_call_counter_++;
108
peahcf02cf12017-04-05 14:18:07 -0700109 data_dumper_->DumpRaw("aec3_processblock_call_order",
110 static_cast<int>(BlockProcessorApiCall::kCapture));
111 data_dumper_->DumpWav("aec3_processblock_capture_input", kBlockSize,
Per Åhgrend112c752019-09-02 13:56:56 +0000112 &(*capture_block)[0][0],
113 LowestBandRate(sample_rate_hz_), 1);
peah69221db2017-01-27 03:28:19 -0800114
Per Åhgren8ba58612017-12-01 23:01:44 +0100115 if (render_properly_started_) {
116 if (!capture_properly_started_) {
117 capture_properly_started_ = true;
118 render_buffer_->Reset();
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200119 if (delay_controller_)
120 delay_controller_->Reset(true);
Per Åhgren8ba58612017-12-01 23:01:44 +0100121 }
122 } else {
123 // If no render data has yet arrived, do not process the capture signal.
peahcf02cf12017-04-05 14:18:07 -0700124 return;
peah69221db2017-01-27 03:28:19 -0800125 }
126
Per Åhgren8ba58612017-12-01 23:01:44 +0100127 EchoPathVariability echo_path_variability(
128 echo_path_gain_change, EchoPathVariability::DelayAdjustment::kNone,
129 false);
130
131 if (render_event_ == RenderDelayBuffer::BufferingEvent::kRenderOverrun &&
132 render_properly_started_) {
133 echo_path_variability.delay_change =
134 EchoPathVariability::DelayAdjustment::kBufferFlush;
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200135 if (delay_controller_)
136 delay_controller_->Reset(true);
Per Åhgrenc59a5762017-12-11 21:34:19 +0100137 RTC_LOG(LS_WARNING) << "Reset due to render buffer overrun at block "
138 << capture_call_counter_;
Per Åhgren8ba58612017-12-01 23:01:44 +0100139 }
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100140 render_event_ = RenderDelayBuffer::BufferingEvent::kNone;
Per Åhgren8ba58612017-12-01 23:01:44 +0100141
142 // Update the render buffers with any newly arrived render blocks and prepare
143 // the render buffers for reading the render data corresponding to the current
144 // capture block.
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100145 RenderDelayBuffer::BufferingEvent buffer_event =
146 render_buffer_->PrepareCaptureProcessing();
147 // Reset the delay controller at render buffer underrun.
148 if (buffer_event == RenderDelayBuffer::BufferingEvent::kRenderUnderrun) {
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200149 if (delay_controller_)
150 delay_controller_->Reset(false);
Per Åhgren8ba58612017-12-01 23:01:44 +0100151 }
152
peahcf02cf12017-04-05 14:18:07 -0700153 data_dumper_->DumpWav("aec3_processblock_capture_input2", kBlockSize,
Per Åhgrend112c752019-09-02 13:56:56 +0000154 &(*capture_block)[0][0],
155 LowestBandRate(sample_rate_hz_), 1);
peahcf02cf12017-04-05 14:18:07 -0700156
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200157 bool has_delay_estimator = !config_.delay.use_external_delay_estimator;
158 if (has_delay_estimator) {
159 RTC_DCHECK(delay_controller_);
160 // Compute and apply the render delay required to achieve proper signal
161 // alignment.
162 estimated_delay_ = delay_controller_->GetDelay(
163 render_buffer_->GetDownsampledRenderBuffer(), render_buffer_->Delay(),
Per Åhgrend112c752019-09-02 13:56:56 +0000164 (*capture_block)[0]);
peah4b572d02017-04-06 16:33:06 -0700165
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200166 if (estimated_delay_) {
167 bool delay_change =
168 render_buffer_->AlignFromDelay(estimated_delay_->delay);
169 if (delay_change) {
Gustaf Ullberg940c2b52019-08-08 15:04:41 +0200170 RTC_LOG(LS_INFO) << "Delay changed to " << estimated_delay_->delay
171 << " at block " << capture_call_counter_;
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200172 echo_path_variability.delay_change =
173 EchoPathVariability::DelayAdjustment::kNewDetectedDelay;
174 }
Per Åhgrenc59a5762017-12-11 21:34:19 +0100175 }
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200176
177 echo_path_variability.clock_drift = delay_controller_->HasClockdrift();
178
179 } else {
180 render_buffer_->AlignFromExternalDelay();
peah96b951c2017-08-22 10:26:07 -0700181 }
peah4b572d02017-04-06 16:33:06 -0700182
183 // Remove the echo from the capture signal.
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200184 if (has_delay_estimator || render_buffer_->HasReceivedBufferDelay()) {
185 echo_remover_->ProcessCapture(
186 echo_path_variability, capture_signal_saturation, estimated_delay_,
187 render_buffer_->GetRenderBuffer(), capture_block);
188 }
peah4b572d02017-04-06 16:33:06 -0700189
peahcf02cf12017-04-05 14:18:07 -0700190 // Update the metrics.
Per Åhgren8ba58612017-12-01 23:01:44 +0100191 metrics_.UpdateCapture(false);
peahd0263542017-01-03 04:20:34 -0800192}
193
peahcf02cf12017-04-05 14:18:07 -0700194void BlockProcessorImpl::BufferRender(
Per Åhgrend112c752019-09-02 13:56:56 +0000195 const std::vector<std::vector<float>>& block) {
peahcf02cf12017-04-05 14:18:07 -0700196 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), block.size());
Per Åhgrend112c752019-09-02 13:56:56 +0000197 RTC_DCHECK_EQ(kBlockSize, block[0].size());
peahcf02cf12017-04-05 14:18:07 -0700198 data_dumper_->DumpRaw("aec3_processblock_call_order",
199 static_cast<int>(BlockProcessorApiCall::kRender));
200 data_dumper_->DumpWav("aec3_processblock_render_input", kBlockSize,
Per Åhgrend112c752019-09-02 13:56:56 +0000201 &block[0][0], LowestBandRate(sample_rate_hz_), 1);
peahcf02cf12017-04-05 14:18:07 -0700202 data_dumper_->DumpWav("aec3_processblock_render_input2", kBlockSize,
Per Åhgrend112c752019-09-02 13:56:56 +0000203 &block[0][0], LowestBandRate(sample_rate_hz_), 1);
peahcf02cf12017-04-05 14:18:07 -0700204
Per Åhgren8ba58612017-12-01 23:01:44 +0100205 render_event_ = render_buffer_->Insert(block);
peahcf02cf12017-04-05 14:18:07 -0700206
Per Åhgren8ba58612017-12-01 23:01:44 +0100207 metrics_.UpdateRender(render_event_ !=
208 RenderDelayBuffer::BufferingEvent::kNone);
209
210 render_properly_started_ = true;
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200211 if (delay_controller_)
212 delay_controller_->LogRenderCall();
peahd0263542017-01-03 04:20:34 -0800213}
214
peah69221db2017-01-27 03:28:19 -0800215void BlockProcessorImpl::UpdateEchoLeakageStatus(bool leakage_detected) {
216 echo_remover_->UpdateEchoLeakageStatus(leakage_detected);
217}
peahd0263542017-01-03 04:20:34 -0800218
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100219void BlockProcessorImpl::GetMetrics(EchoControl::Metrics* metrics) const {
220 echo_remover_->GetMetrics(metrics);
Per Åhgrend112c752019-09-02 13:56:56 +0000221 const int block_size_ms = sample_rate_hz_ == 8000 ? 8 : 4;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200222 absl::optional<size_t> delay = render_buffer_->Delay();
Per Åhgrenc59a5762017-12-11 21:34:19 +0100223 metrics->delay_ms = delay ? static_cast<int>(*delay) * block_size_ms : 0;
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100224}
225
Per Åhgrend0fa8202018-04-18 09:35:13 +0200226void BlockProcessorImpl::SetAudioBufferDelay(size_t delay_ms) {
227 render_buffer_->SetAudioBufferDelay(delay_ms);
228}
229
peahd0263542017-01-03 04:20:34 -0800230} // namespace
231
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200232BlockProcessor* BlockProcessor::Create(const EchoCanceller3Config& config,
Per Åhgrend112c752019-09-02 13:56:56 +0000233 int sample_rate_hz) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100234 std::unique_ptr<RenderDelayBuffer> render_buffer(
Per Åhgrend112c752019-09-02 13:56:56 +0000235 RenderDelayBuffer::Create(config, sample_rate_hz));
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200236 std::unique_ptr<RenderDelayController> delay_controller;
237 if (!config.delay.use_external_delay_estimator) {
238 delay_controller.reset(
239 RenderDelayController::Create(config, sample_rate_hz));
240 }
Per Åhgrend112c752019-09-02 13:56:56 +0000241 std::unique_ptr<EchoRemover> echo_remover(
242 EchoRemover::Create(config, sample_rate_hz));
243 return Create(config, sample_rate_hz, std::move(render_buffer),
peah69221db2017-01-27 03:28:19 -0800244 std::move(delay_controller), std::move(echo_remover));
245}
246
247BlockProcessor* BlockProcessor::Create(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200248 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -0800249 int sample_rate_hz,
250 std::unique_ptr<RenderDelayBuffer> render_buffer) {
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200251 std::unique_ptr<RenderDelayController> delay_controller;
252 if (!config.delay.use_external_delay_estimator) {
253 delay_controller.reset(
254 RenderDelayController::Create(config, sample_rate_hz));
255 }
Per Åhgrend112c752019-09-02 13:56:56 +0000256 std::unique_ptr<EchoRemover> echo_remover(
257 EchoRemover::Create(config, sample_rate_hz));
258 return Create(config, sample_rate_hz, std::move(render_buffer),
peah69221db2017-01-27 03:28:19 -0800259 std::move(delay_controller), std::move(echo_remover));
260}
261
262BlockProcessor* BlockProcessor::Create(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200263 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -0800264 int sample_rate_hz,
265 std::unique_ptr<RenderDelayBuffer> render_buffer,
266 std::unique_ptr<RenderDelayController> delay_controller,
267 std::unique_ptr<EchoRemover> echo_remover) {
Per Åhgrend112c752019-09-02 13:56:56 +0000268 return new BlockProcessorImpl(
269 config, sample_rate_hz, std::move(render_buffer),
270 std::move(delay_controller), std::move(echo_remover));
peahd0263542017-01-03 04:20:34 -0800271}
272
273} // namespace webrtc