blob: c02994b966ded2673334ae99555d1d73e4d45f2d [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) {
Gustaf Ullberg940c2b52019-08-08 15:04:41 +0200174 RTC_LOG(LS_INFO) << "Delay changed to " << estimated_delay_->delay
175 << " at block " << capture_call_counter_;
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200176 echo_path_variability.delay_change =
177 EchoPathVariability::DelayAdjustment::kNewDetectedDelay;
178 }
Per Åhgrenc59a5762017-12-11 21:34:19 +0100179 }
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200180
181 echo_path_variability.clock_drift = delay_controller_->HasClockdrift();
182
183 } else {
184 render_buffer_->AlignFromExternalDelay();
peah96b951c2017-08-22 10:26:07 -0700185 }
peah4b572d02017-04-06 16:33:06 -0700186
187 // Remove the echo from the capture signal.
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200188 if (has_delay_estimator || render_buffer_->HasReceivedBufferDelay()) {
189 echo_remover_->ProcessCapture(
190 echo_path_variability, capture_signal_saturation, estimated_delay_,
191 render_buffer_->GetRenderBuffer(), capture_block);
192 }
peah4b572d02017-04-06 16:33:06 -0700193
peahcf02cf12017-04-05 14:18:07 -0700194 // Update the metrics.
Per Åhgren8ba58612017-12-01 23:01:44 +0100195 metrics_.UpdateCapture(false);
peahd0263542017-01-03 04:20:34 -0800196}
197
peahcf02cf12017-04-05 14:18:07 -0700198void BlockProcessorImpl::BufferRender(
Per Åhgrence202a02019-09-02 17:01:19 +0200199 const std::vector<std::vector<std::vector<float>>>& block) {
peahcf02cf12017-04-05 14:18:07 -0700200 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), block.size());
Per Åhgrence202a02019-09-02 17:01:19 +0200201 RTC_DCHECK_EQ(kBlockSize, block[0][0].size());
peahcf02cf12017-04-05 14:18:07 -0700202 data_dumper_->DumpRaw("aec3_processblock_call_order",
203 static_cast<int>(BlockProcessorApiCall::kRender));
204 data_dumper_->DumpWav("aec3_processblock_render_input", kBlockSize,
Per Åhgrence202a02019-09-02 17:01:19 +0200205 &block[0][0][0], 16000, 1);
peahcf02cf12017-04-05 14:18:07 -0700206 data_dumper_->DumpWav("aec3_processblock_render_input2", kBlockSize,
Per Åhgrence202a02019-09-02 17:01:19 +0200207 &block[0][0][0], 16000, 1);
peahcf02cf12017-04-05 14:18:07 -0700208
Per Åhgren8ba58612017-12-01 23:01:44 +0100209 render_event_ = render_buffer_->Insert(block);
peahcf02cf12017-04-05 14:18:07 -0700210
Per Åhgren8ba58612017-12-01 23:01:44 +0100211 metrics_.UpdateRender(render_event_ !=
212 RenderDelayBuffer::BufferingEvent::kNone);
213
214 render_properly_started_ = true;
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200215 if (delay_controller_)
216 delay_controller_->LogRenderCall();
peahd0263542017-01-03 04:20:34 -0800217}
218
peah69221db2017-01-27 03:28:19 -0800219void BlockProcessorImpl::UpdateEchoLeakageStatus(bool leakage_detected) {
220 echo_remover_->UpdateEchoLeakageStatus(leakage_detected);
221}
peahd0263542017-01-03 04:20:34 -0800222
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100223void BlockProcessorImpl::GetMetrics(EchoControl::Metrics* metrics) const {
224 echo_remover_->GetMetrics(metrics);
Per Åhgrence202a02019-09-02 17:01:19 +0200225 constexpr int block_size_ms = 4;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200226 absl::optional<size_t> delay = render_buffer_->Delay();
Per Åhgrenc59a5762017-12-11 21:34:19 +0100227 metrics->delay_ms = delay ? static_cast<int>(*delay) * block_size_ms : 0;
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100228}
229
Per Åhgrend0fa8202018-04-18 09:35:13 +0200230void BlockProcessorImpl::SetAudioBufferDelay(size_t delay_ms) {
231 render_buffer_->SetAudioBufferDelay(delay_ms);
232}
233
peahd0263542017-01-03 04:20:34 -0800234} // namespace
235
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200236BlockProcessor* BlockProcessor::Create(const EchoCanceller3Config& config,
Per Åhgrence202a02019-09-02 17:01:19 +0200237 int sample_rate_hz,
238 size_t num_render_channels,
239 size_t num_capture_channels) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100240 std::unique_ptr<RenderDelayBuffer> render_buffer(
Per Åhgrence202a02019-09-02 17:01:19 +0200241 RenderDelayBuffer::Create(config, sample_rate_hz, num_render_channels));
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200242 std::unique_ptr<RenderDelayController> delay_controller;
243 if (!config.delay.use_external_delay_estimator) {
244 delay_controller.reset(
245 RenderDelayController::Create(config, sample_rate_hz));
246 }
Per Åhgrence202a02019-09-02 17:01:19 +0200247 std::unique_ptr<EchoRemover> echo_remover(EchoRemover::Create(
248 config, sample_rate_hz, num_render_channels, num_capture_channels));
249 return Create(config, sample_rate_hz, num_render_channels,
250 num_capture_channels, std::move(render_buffer),
peah69221db2017-01-27 03:28:19 -0800251 std::move(delay_controller), std::move(echo_remover));
252}
253
254BlockProcessor* BlockProcessor::Create(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200255 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -0800256 int sample_rate_hz,
Per Åhgrence202a02019-09-02 17:01:19 +0200257 size_t num_render_channels,
258 size_t num_capture_channels,
peah69221db2017-01-27 03:28:19 -0800259 std::unique_ptr<RenderDelayBuffer> render_buffer) {
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200260 std::unique_ptr<RenderDelayController> delay_controller;
261 if (!config.delay.use_external_delay_estimator) {
262 delay_controller.reset(
263 RenderDelayController::Create(config, sample_rate_hz));
264 }
Per Åhgrence202a02019-09-02 17:01:19 +0200265 std::unique_ptr<EchoRemover> echo_remover(EchoRemover::Create(
266 config, sample_rate_hz, num_render_channels, num_capture_channels));
267 return Create(config, sample_rate_hz, num_render_channels,
268 num_capture_channels, std::move(render_buffer),
peah69221db2017-01-27 03:28:19 -0800269 std::move(delay_controller), std::move(echo_remover));
270}
271
272BlockProcessor* BlockProcessor::Create(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200273 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -0800274 int sample_rate_hz,
Per Åhgrence202a02019-09-02 17:01:19 +0200275 size_t num_render_channels,
276 size_t num_capture_channels,
peah69221db2017-01-27 03:28:19 -0800277 std::unique_ptr<RenderDelayBuffer> render_buffer,
278 std::unique_ptr<RenderDelayController> delay_controller,
279 std::unique_ptr<EchoRemover> echo_remover) {
Per Åhgrence202a02019-09-02 17:01:19 +0200280 return new BlockProcessorImpl(config, sample_rate_hz, num_render_channels,
281 num_capture_channels, std::move(render_buffer),
282 std::move(delay_controller),
283 std::move(echo_remover));
peahd0263542017-01-03 04:20:34 -0800284}
285
286} // namespace webrtc