blob: bda25893957b47b6a1addc000ca926a4f830422f [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 Åhgrend0fa8202018-04-18 09:35:13 +020066
peahd0263542017-01-03 04:20:34 -080067 private:
peahd0263542017-01-03 04:20:34 -080068 static int instance_count_;
69 std::unique_ptr<ApmDataDumper> data_dumper_;
Per Åhgren8ba58612017-12-01 23:01:44 +010070 const EchoCanceller3Config config_;
71 bool capture_properly_started_ = false;
72 bool render_properly_started_ = false;
peah69221db2017-01-27 03:28:19 -080073 const size_t sample_rate_hz_;
74 std::unique_ptr<RenderDelayBuffer> render_buffer_;
75 std::unique_ptr<RenderDelayController> delay_controller_;
76 std::unique_ptr<EchoRemover> echo_remover_;
peahe985b3f2017-02-28 22:08:53 -080077 BlockProcessorMetrics metrics_;
Per Åhgren8ba58612017-12-01 23:01:44 +010078 RenderDelayBuffer::BufferingEvent render_event_;
Per Åhgrenc59a5762017-12-11 21:34:19 +010079 size_t capture_call_counter_ = 0;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020080 absl::optional<DelayEstimate> estimated_delay_;
peahd0263542017-01-03 04:20:34 -080081};
82
83int BlockProcessorImpl::instance_count_ = 0;
84
peah69221db2017-01-27 03:28:19 -080085BlockProcessorImpl::BlockProcessorImpl(
Per Åhgren8ba58612017-12-01 23:01:44 +010086 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -080087 int sample_rate_hz,
Per Åhgrence202a02019-09-02 17:01:19 +020088 size_t num_render_channels,
89 size_t num_capture_channels,
peah69221db2017-01-27 03:28:19 -080090 std::unique_ptr<RenderDelayBuffer> render_buffer,
91 std::unique_ptr<RenderDelayController> delay_controller,
92 std::unique_ptr<EchoRemover> echo_remover)
93 : data_dumper_(
94 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Per Åhgren8ba58612017-12-01 23:01:44 +010095 config_(config),
peah69221db2017-01-27 03:28:19 -080096 sample_rate_hz_(sample_rate_hz),
97 render_buffer_(std::move(render_buffer)),
98 delay_controller_(std::move(delay_controller)),
Per Åhgren8ba58612017-12-01 23:01:44 +010099 echo_remover_(std::move(echo_remover)),
100 render_event_(RenderDelayBuffer::BufferingEvent::kNone) {
peah21920892017-02-08 05:08:56 -0800101 RTC_DCHECK(ValidFullBandRate(sample_rate_hz_));
102}
peahd0263542017-01-03 04:20:34 -0800103
104BlockProcessorImpl::~BlockProcessorImpl() = default;
105
106void BlockProcessorImpl::ProcessCapture(
peah69221db2017-01-27 03:28:19 -0800107 bool echo_path_gain_change,
108 bool capture_signal_saturation,
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100109 std::vector<std::vector<std::vector<float>>>* linear_output,
Per Åhgrence202a02019-09-02 17:01:19 +0200110 std::vector<std::vector<std::vector<float>>>* capture_block) {
peahd0263542017-01-03 04:20:34 -0800111 RTC_DCHECK(capture_block);
112 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), capture_block->size());
Per Åhgrence202a02019-09-02 17:01:19 +0200113 RTC_DCHECK_EQ(kBlockSize, (*capture_block)[0][0].size());
Per Åhgrenc59a5762017-12-11 21:34:19 +0100114
115 capture_call_counter_++;
116
peahcf02cf12017-04-05 14:18:07 -0700117 data_dumper_->DumpRaw("aec3_processblock_call_order",
118 static_cast<int>(BlockProcessorApiCall::kCapture));
119 data_dumper_->DumpWav("aec3_processblock_capture_input", kBlockSize,
Per Åhgrence202a02019-09-02 17:01:19 +0200120 &(*capture_block)[0][0][0], 16000, 1);
peah69221db2017-01-27 03:28:19 -0800121
Per Åhgren8ba58612017-12-01 23:01:44 +0100122 if (render_properly_started_) {
123 if (!capture_properly_started_) {
124 capture_properly_started_ = true;
125 render_buffer_->Reset();
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200126 if (delay_controller_)
127 delay_controller_->Reset(true);
Per Åhgren8ba58612017-12-01 23:01:44 +0100128 }
129 } else {
130 // If no render data has yet arrived, do not process the capture signal.
peahcf02cf12017-04-05 14:18:07 -0700131 return;
peah69221db2017-01-27 03:28:19 -0800132 }
133
Per Åhgren8ba58612017-12-01 23:01:44 +0100134 EchoPathVariability echo_path_variability(
135 echo_path_gain_change, EchoPathVariability::DelayAdjustment::kNone,
136 false);
137
138 if (render_event_ == RenderDelayBuffer::BufferingEvent::kRenderOverrun &&
139 render_properly_started_) {
140 echo_path_variability.delay_change =
141 EchoPathVariability::DelayAdjustment::kBufferFlush;
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200142 if (delay_controller_)
143 delay_controller_->Reset(true);
Per Åhgrenc59a5762017-12-11 21:34:19 +0100144 RTC_LOG(LS_WARNING) << "Reset due to render buffer overrun at block "
145 << capture_call_counter_;
Per Åhgren8ba58612017-12-01 23:01:44 +0100146 }
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100147 render_event_ = RenderDelayBuffer::BufferingEvent::kNone;
Per Åhgren8ba58612017-12-01 23:01:44 +0100148
149 // Update the render buffers with any newly arrived render blocks and prepare
150 // the render buffers for reading the render data corresponding to the current
151 // capture block.
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100152 RenderDelayBuffer::BufferingEvent buffer_event =
153 render_buffer_->PrepareCaptureProcessing();
154 // Reset the delay controller at render buffer underrun.
155 if (buffer_event == RenderDelayBuffer::BufferingEvent::kRenderUnderrun) {
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200156 if (delay_controller_)
157 delay_controller_->Reset(false);
Per Åhgren8ba58612017-12-01 23:01:44 +0100158 }
159
peahcf02cf12017-04-05 14:18:07 -0700160 data_dumper_->DumpWav("aec3_processblock_capture_input2", kBlockSize,
Per Åhgrence202a02019-09-02 17:01:19 +0200161 &(*capture_block)[0][0][0], 16000, 1);
peahcf02cf12017-04-05 14:18:07 -0700162
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200163 bool has_delay_estimator = !config_.delay.use_external_delay_estimator;
164 if (has_delay_estimator) {
165 RTC_DCHECK(delay_controller_);
166 // Compute and apply the render delay required to achieve proper signal
167 // alignment.
168 estimated_delay_ = delay_controller_->GetDelay(
169 render_buffer_->GetDownsampledRenderBuffer(), render_buffer_->Delay(),
Gustaf Ullbergee84d392019-09-10 09:36:43 +0200170 (*capture_block)[0]);
peah4b572d02017-04-06 16:33:06 -0700171
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200172 if (estimated_delay_) {
173 bool delay_change =
174 render_buffer_->AlignFromDelay(estimated_delay_->delay);
175 if (delay_change) {
Sam Zackrissonffc84522019-10-15 13:43:02 +0200176 rtc::LoggingSeverity log_level =
177 config_.delay.log_warning_on_delay_changes ? rtc::LS_WARNING
178 : rtc::LS_INFO;
179 RTC_LOG_V(log_level) << "Delay changed to " << estimated_delay_->delay
180 << " at block " << capture_call_counter_;
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200181 echo_path_variability.delay_change =
182 EchoPathVariability::DelayAdjustment::kNewDetectedDelay;
183 }
Per Åhgrenc59a5762017-12-11 21:34:19 +0100184 }
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200185
186 echo_path_variability.clock_drift = delay_controller_->HasClockdrift();
187
188 } else {
189 render_buffer_->AlignFromExternalDelay();
peah96b951c2017-08-22 10:26:07 -0700190 }
peah4b572d02017-04-06 16:33:06 -0700191
192 // Remove the echo from the capture signal.
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200193 if (has_delay_estimator || render_buffer_->HasReceivedBufferDelay()) {
194 echo_remover_->ProcessCapture(
195 echo_path_variability, capture_signal_saturation, estimated_delay_,
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100196 render_buffer_->GetRenderBuffer(), linear_output, capture_block);
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200197 }
peah4b572d02017-04-06 16:33:06 -0700198
peahcf02cf12017-04-05 14:18:07 -0700199 // Update the metrics.
Per Åhgren8ba58612017-12-01 23:01:44 +0100200 metrics_.UpdateCapture(false);
peahd0263542017-01-03 04:20:34 -0800201}
202
peahcf02cf12017-04-05 14:18:07 -0700203void BlockProcessorImpl::BufferRender(
Per Åhgrence202a02019-09-02 17:01:19 +0200204 const std::vector<std::vector<std::vector<float>>>& block) {
peahcf02cf12017-04-05 14:18:07 -0700205 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), block.size());
Per Åhgrence202a02019-09-02 17:01:19 +0200206 RTC_DCHECK_EQ(kBlockSize, block[0][0].size());
peahcf02cf12017-04-05 14:18:07 -0700207 data_dumper_->DumpRaw("aec3_processblock_call_order",
208 static_cast<int>(BlockProcessorApiCall::kRender));
209 data_dumper_->DumpWav("aec3_processblock_render_input", kBlockSize,
Per Åhgrence202a02019-09-02 17:01:19 +0200210 &block[0][0][0], 16000, 1);
peahcf02cf12017-04-05 14:18:07 -0700211 data_dumper_->DumpWav("aec3_processblock_render_input2", kBlockSize,
Per Åhgrence202a02019-09-02 17:01:19 +0200212 &block[0][0][0], 16000, 1);
peahcf02cf12017-04-05 14:18:07 -0700213
Per Åhgren8ba58612017-12-01 23:01:44 +0100214 render_event_ = render_buffer_->Insert(block);
peahcf02cf12017-04-05 14:18:07 -0700215
Per Åhgren8ba58612017-12-01 23:01:44 +0100216 metrics_.UpdateRender(render_event_ !=
217 RenderDelayBuffer::BufferingEvent::kNone);
218
219 render_properly_started_ = true;
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200220 if (delay_controller_)
221 delay_controller_->LogRenderCall();
peahd0263542017-01-03 04:20:34 -0800222}
223
peah69221db2017-01-27 03:28:19 -0800224void BlockProcessorImpl::UpdateEchoLeakageStatus(bool leakage_detected) {
225 echo_remover_->UpdateEchoLeakageStatus(leakage_detected);
226}
peahd0263542017-01-03 04:20:34 -0800227
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100228void BlockProcessorImpl::GetMetrics(EchoControl::Metrics* metrics) const {
229 echo_remover_->GetMetrics(metrics);
Per Åhgrence202a02019-09-02 17:01:19 +0200230 constexpr int block_size_ms = 4;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200231 absl::optional<size_t> delay = render_buffer_->Delay();
Per Åhgrenc59a5762017-12-11 21:34:19 +0100232 metrics->delay_ms = delay ? static_cast<int>(*delay) * block_size_ms : 0;
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100233}
234
Gustaf Ullberg3cb61042019-10-24 15:52:10 +0200235void BlockProcessorImpl::SetAudioBufferDelay(int delay_ms) {
Per Åhgrend0fa8202018-04-18 09:35:13 +0200236 render_buffer_->SetAudioBufferDelay(delay_ms);
237}
238
peahd0263542017-01-03 04:20:34 -0800239} // namespace
240
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200241BlockProcessor* BlockProcessor::Create(const EchoCanceller3Config& config,
Per Åhgrence202a02019-09-02 17:01:19 +0200242 int sample_rate_hz,
243 size_t num_render_channels,
244 size_t num_capture_channels) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100245 std::unique_ptr<RenderDelayBuffer> render_buffer(
Per Åhgrence202a02019-09-02 17:01:19 +0200246 RenderDelayBuffer::Create(config, sample_rate_hz, num_render_channels));
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200247 std::unique_ptr<RenderDelayController> delay_controller;
248 if (!config.delay.use_external_delay_estimator) {
249 delay_controller.reset(
250 RenderDelayController::Create(config, sample_rate_hz));
251 }
Per Åhgrence202a02019-09-02 17:01:19 +0200252 std::unique_ptr<EchoRemover> echo_remover(EchoRemover::Create(
253 config, sample_rate_hz, num_render_channels, num_capture_channels));
254 return Create(config, sample_rate_hz, num_render_channels,
255 num_capture_channels, std::move(render_buffer),
peah69221db2017-01-27 03:28:19 -0800256 std::move(delay_controller), std::move(echo_remover));
257}
258
259BlockProcessor* BlockProcessor::Create(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200260 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -0800261 int sample_rate_hz,
Per Åhgrence202a02019-09-02 17:01:19 +0200262 size_t num_render_channels,
263 size_t num_capture_channels,
peah69221db2017-01-27 03:28:19 -0800264 std::unique_ptr<RenderDelayBuffer> render_buffer) {
Gustaf Ullberg8f32b6c2019-04-05 16:23:50 +0200265 std::unique_ptr<RenderDelayController> delay_controller;
266 if (!config.delay.use_external_delay_estimator) {
267 delay_controller.reset(
268 RenderDelayController::Create(config, sample_rate_hz));
269 }
Per Åhgrence202a02019-09-02 17:01:19 +0200270 std::unique_ptr<EchoRemover> echo_remover(EchoRemover::Create(
271 config, sample_rate_hz, num_render_channels, num_capture_channels));
272 return Create(config, sample_rate_hz, num_render_channels,
273 num_capture_channels, std::move(render_buffer),
peah69221db2017-01-27 03:28:19 -0800274 std::move(delay_controller), std::move(echo_remover));
275}
276
277BlockProcessor* BlockProcessor::Create(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200278 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -0800279 int sample_rate_hz,
Per Åhgrence202a02019-09-02 17:01:19 +0200280 size_t num_render_channels,
281 size_t num_capture_channels,
peah69221db2017-01-27 03:28:19 -0800282 std::unique_ptr<RenderDelayBuffer> render_buffer,
283 std::unique_ptr<RenderDelayController> delay_controller,
284 std::unique_ptr<EchoRemover> echo_remover) {
Per Åhgrence202a02019-09-02 17:01:19 +0200285 return new BlockProcessorImpl(config, sample_rate_hz, num_render_channels,
286 num_capture_channels, std::move(render_buffer),
287 std::move(delay_controller),
288 std::move(echo_remover));
peahd0263542017-01-03 04:20:34 -0800289}
290
291} // namespace webrtc