blob: b08be68c22a84ef663bfac989c6faf8ebed2b857 [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 */
Gustaf Ullberge47433f2019-01-24 16:00:57 +010010#include <stddef.h>
11#include <memory>
Yves Gerey988cc082018-10-23 12:03:01 +020012#include <utility>
Gustaf Ullberge47433f2019-01-24 16:00:57 +010013#include <vector>
Yves Gerey988cc082018-10-23 12:03:01 +020014
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020015#include "absl/types/optional.h"
Gustaf Ullberge47433f2019-01-24 16:00:57 +010016#include "api/audio/echo_canceller3_config.h"
17#include "api/audio/echo_control.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_processing/aec3/aec3_common.h"
Gustaf Ullberge47433f2019-01-24 16:00:57 +010019#include "modules/audio_processing/aec3/block_processor.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/audio_processing/aec3/block_processor_metrics.h"
Yves Gerey988cc082018-10-23 12:03:01 +020021#include "modules/audio_processing/aec3/delay_estimate.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/audio_processing/aec3/echo_path_variability.h"
Gustaf Ullberge47433f2019-01-24 16:00:57 +010023#include "modules/audio_processing/aec3/echo_remover.h"
24#include "modules/audio_processing/aec3/render_delay_buffer.h"
25#include "modules/audio_processing/aec3/render_delay_controller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "modules/audio_processing/logging/apm_data_dumper.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "rtc_base/atomic_ops.h"
Yves Gerey988cc082018-10-23 12:03:01 +020028#include "rtc_base/checks.h"
Per Åhgrenfe9f2222017-09-26 23:33:26 +020029#include "rtc_base/logging.h"
peahd0263542017-01-03 04:20:34 -080030
31namespace webrtc {
32namespace {
33
peahcf02cf12017-04-05 14:18:07 -070034enum class BlockProcessorApiCall { kCapture, kRender };
35
peahd0263542017-01-03 04:20:34 -080036class BlockProcessorImpl final : public BlockProcessor {
37 public:
Per Åhgren8ba58612017-12-01 23:01:44 +010038 BlockProcessorImpl(const EchoCanceller3Config& config,
39 int sample_rate_hz,
peah69221db2017-01-27 03:28:19 -080040 std::unique_ptr<RenderDelayBuffer> render_buffer,
41 std::unique_ptr<RenderDelayController> delay_controller,
42 std::unique_ptr<EchoRemover> echo_remover);
43
Gustaf Ullberge47433f2019-01-24 16:00:57 +010044 BlockProcessorImpl() = delete;
45
peahd0263542017-01-03 04:20:34 -080046 ~BlockProcessorImpl() override;
47
peah69221db2017-01-27 03:28:19 -080048 void ProcessCapture(bool echo_path_gain_change,
49 bool capture_signal_saturation,
peahd0263542017-01-03 04:20:34 -080050 std::vector<std::vector<float>>* capture_block) override;
51
peahcf02cf12017-04-05 14:18:07 -070052 void BufferRender(const std::vector<std::vector<float>>& block) override;
peahd0263542017-01-03 04:20:34 -080053
peah69221db2017-01-27 03:28:19 -080054 void UpdateEchoLeakageStatus(bool leakage_detected) override;
peahd0263542017-01-03 04:20:34 -080055
Gustaf Ullberg332150d2017-11-22 14:17:39 +010056 void GetMetrics(EchoControl::Metrics* metrics) const override;
57
Per Åhgrend0fa8202018-04-18 09:35:13 +020058 void SetAudioBufferDelay(size_t delay_ms) override;
59
peahd0263542017-01-03 04:20:34 -080060 private:
peahd0263542017-01-03 04:20:34 -080061 static int instance_count_;
62 std::unique_ptr<ApmDataDumper> data_dumper_;
Per Åhgren8ba58612017-12-01 23:01:44 +010063 const EchoCanceller3Config config_;
64 bool capture_properly_started_ = false;
65 bool render_properly_started_ = false;
peah69221db2017-01-27 03:28:19 -080066 const size_t sample_rate_hz_;
67 std::unique_ptr<RenderDelayBuffer> render_buffer_;
68 std::unique_ptr<RenderDelayController> delay_controller_;
69 std::unique_ptr<EchoRemover> echo_remover_;
peahe985b3f2017-02-28 22:08:53 -080070 BlockProcessorMetrics metrics_;
Per Åhgren8ba58612017-12-01 23:01:44 +010071 RenderDelayBuffer::BufferingEvent render_event_;
Per Åhgrenc59a5762017-12-11 21:34:19 +010072 size_t capture_call_counter_ = 0;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020073 absl::optional<DelayEstimate> estimated_delay_;
74 absl::optional<int> echo_remover_delay_;
peahd0263542017-01-03 04:20:34 -080075};
76
77int BlockProcessorImpl::instance_count_ = 0;
78
peah69221db2017-01-27 03:28:19 -080079BlockProcessorImpl::BlockProcessorImpl(
Per Åhgren8ba58612017-12-01 23:01:44 +010080 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -080081 int sample_rate_hz,
82 std::unique_ptr<RenderDelayBuffer> render_buffer,
83 std::unique_ptr<RenderDelayController> delay_controller,
84 std::unique_ptr<EchoRemover> echo_remover)
85 : data_dumper_(
86 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Per Åhgren8ba58612017-12-01 23:01:44 +010087 config_(config),
peah69221db2017-01-27 03:28:19 -080088 sample_rate_hz_(sample_rate_hz),
89 render_buffer_(std::move(render_buffer)),
90 delay_controller_(std::move(delay_controller)),
Per Åhgren8ba58612017-12-01 23:01:44 +010091 echo_remover_(std::move(echo_remover)),
92 render_event_(RenderDelayBuffer::BufferingEvent::kNone) {
peah21920892017-02-08 05:08:56 -080093 RTC_DCHECK(ValidFullBandRate(sample_rate_hz_));
94}
peahd0263542017-01-03 04:20:34 -080095
96BlockProcessorImpl::~BlockProcessorImpl() = default;
97
98void BlockProcessorImpl::ProcessCapture(
peah69221db2017-01-27 03:28:19 -080099 bool echo_path_gain_change,
100 bool capture_signal_saturation,
peahd0263542017-01-03 04:20:34 -0800101 std::vector<std::vector<float>>* capture_block) {
102 RTC_DCHECK(capture_block);
103 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), capture_block->size());
104 RTC_DCHECK_EQ(kBlockSize, (*capture_block)[0].size());
Per Åhgrenc59a5762017-12-11 21:34:19 +0100105
106 capture_call_counter_++;
107
peahcf02cf12017-04-05 14:18:07 -0700108 data_dumper_->DumpRaw("aec3_processblock_call_order",
109 static_cast<int>(BlockProcessorApiCall::kCapture));
110 data_dumper_->DumpWav("aec3_processblock_capture_input", kBlockSize,
111 &(*capture_block)[0][0],
112 LowestBandRate(sample_rate_hz_), 1);
peah69221db2017-01-27 03:28:19 -0800113
Per Åhgren8ba58612017-12-01 23:01:44 +0100114 if (render_properly_started_) {
115 if (!capture_properly_started_) {
116 capture_properly_started_ = true;
117 render_buffer_->Reset();
Per Åhgren8b7d2062018-10-30 23:44:40 +0100118 delay_controller_->Reset(true);
Per Åhgren8ba58612017-12-01 23:01:44 +0100119 }
120 } else {
121 // If no render data has yet arrived, do not process the capture signal.
peahcf02cf12017-04-05 14:18:07 -0700122 return;
peah69221db2017-01-27 03:28:19 -0800123 }
124
Per Åhgren8ba58612017-12-01 23:01:44 +0100125 EchoPathVariability echo_path_variability(
126 echo_path_gain_change, EchoPathVariability::DelayAdjustment::kNone,
127 false);
128
129 if (render_event_ == RenderDelayBuffer::BufferingEvent::kRenderOverrun &&
130 render_properly_started_) {
131 echo_path_variability.delay_change =
132 EchoPathVariability::DelayAdjustment::kBufferFlush;
Per Åhgren8b7d2062018-10-30 23:44:40 +0100133 delay_controller_->Reset(true);
Per Åhgrenc59a5762017-12-11 21:34:19 +0100134 RTC_LOG(LS_WARNING) << "Reset due to render buffer overrun at block "
135 << capture_call_counter_;
Per Åhgren8ba58612017-12-01 23:01:44 +0100136 }
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100137 render_event_ = RenderDelayBuffer::BufferingEvent::kNone;
Per Åhgren8ba58612017-12-01 23:01:44 +0100138
139 // Update the render buffers with any newly arrived render blocks and prepare
140 // the render buffers for reading the render data corresponding to the current
141 // capture block.
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100142 RenderDelayBuffer::BufferingEvent buffer_event =
143 render_buffer_->PrepareCaptureProcessing();
144 // Reset the delay controller at render buffer underrun.
145 if (buffer_event == RenderDelayBuffer::BufferingEvent::kRenderUnderrun) {
146 delay_controller_->Reset(false);
Per Åhgren8ba58612017-12-01 23:01:44 +0100147 }
148
peahcf02cf12017-04-05 14:18:07 -0700149 data_dumper_->DumpWav("aec3_processblock_capture_input2", kBlockSize,
150 &(*capture_block)[0][0],
151 LowestBandRate(sample_rate_hz_), 1);
152
peah4b572d02017-04-06 16:33:06 -0700153 // Compute and and apply the render delay required to achieve proper signal
154 // alignment.
Per Åhgrena76ef9d2018-01-25 07:01:34 +0100155 estimated_delay_ = delay_controller_->GetDelay(
Per Åhgren5c532d32018-03-22 00:29:25 +0100156 render_buffer_->GetDownsampledRenderBuffer(), render_buffer_->Delay(),
157 echo_remover_delay_, (*capture_block)[0]);
peah4b572d02017-04-06 16:33:06 -0700158
Per Åhgrena76ef9d2018-01-25 07:01:34 +0100159 if (estimated_delay_) {
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100160 bool delay_change = render_buffer_->SetDelay(estimated_delay_->delay);
161 if (delay_change) {
162 RTC_LOG(LS_WARNING) << "Delay changed to " << estimated_delay_->delay
163 << " at block " << capture_call_counter_;
164 echo_path_variability.delay_change =
165 EchoPathVariability::DelayAdjustment::kNewDetectedDelay;
Per Åhgrenc59a5762017-12-11 21:34:19 +0100166 }
peah96b951c2017-08-22 10:26:07 -0700167 }
peah4b572d02017-04-06 16:33:06 -0700168
Gustaf Ullberg777cf262018-11-22 16:02:34 +0100169 echo_path_variability.clock_drift = delay_controller_->HasClockdrift();
170
peah4b572d02017-04-06 16:33:06 -0700171 // Remove the echo from the capture signal.
172 echo_remover_->ProcessCapture(
Per Åhgren3ab308f2018-02-21 08:46:03 +0100173 echo_path_variability, capture_signal_saturation, estimated_delay_,
Per Åhgrende22a172017-12-20 18:00:51 +0100174 render_buffer_->GetRenderBuffer(), capture_block);
peah4b572d02017-04-06 16:33:06 -0700175
Per Åhgren5c532d32018-03-22 00:29:25 +0100176 // Check to see if a refined delay estimate has been obtained from the echo
177 // remover.
178 echo_remover_delay_ = echo_remover_->Delay();
179
peahcf02cf12017-04-05 14:18:07 -0700180 // Update the metrics.
Per Åhgren8ba58612017-12-01 23:01:44 +0100181 metrics_.UpdateCapture(false);
peahd0263542017-01-03 04:20:34 -0800182}
183
peahcf02cf12017-04-05 14:18:07 -0700184void BlockProcessorImpl::BufferRender(
185 const std::vector<std::vector<float>>& block) {
186 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), block.size());
187 RTC_DCHECK_EQ(kBlockSize, block[0].size());
188 data_dumper_->DumpRaw("aec3_processblock_call_order",
189 static_cast<int>(BlockProcessorApiCall::kRender));
190 data_dumper_->DumpWav("aec3_processblock_render_input", kBlockSize,
191 &block[0][0], LowestBandRate(sample_rate_hz_), 1);
peahcf02cf12017-04-05 14:18:07 -0700192 data_dumper_->DumpWav("aec3_processblock_render_input2", kBlockSize,
193 &block[0][0], LowestBandRate(sample_rate_hz_), 1);
194
Per Åhgren8ba58612017-12-01 23:01:44 +0100195 render_event_ = render_buffer_->Insert(block);
peahcf02cf12017-04-05 14:18:07 -0700196
Per Åhgren8ba58612017-12-01 23:01:44 +0100197 metrics_.UpdateRender(render_event_ !=
198 RenderDelayBuffer::BufferingEvent::kNone);
199
200 render_properly_started_ = true;
Per Åhgren47127762018-02-13 12:59:33 +0100201 delay_controller_->LogRenderCall();
peahd0263542017-01-03 04:20:34 -0800202}
203
peah69221db2017-01-27 03:28:19 -0800204void BlockProcessorImpl::UpdateEchoLeakageStatus(bool leakage_detected) {
205 echo_remover_->UpdateEchoLeakageStatus(leakage_detected);
206}
peahd0263542017-01-03 04:20:34 -0800207
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100208void BlockProcessorImpl::GetMetrics(EchoControl::Metrics* metrics) const {
209 echo_remover_->GetMetrics(metrics);
Per Åhgren83c4a022017-11-27 12:07:09 +0100210 const int block_size_ms = sample_rate_hz_ == 8000 ? 8 : 4;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200211 absl::optional<size_t> delay = render_buffer_->Delay();
Per Åhgrenc59a5762017-12-11 21:34:19 +0100212 metrics->delay_ms = delay ? static_cast<int>(*delay) * block_size_ms : 0;
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100213}
214
Per Åhgrend0fa8202018-04-18 09:35:13 +0200215void BlockProcessorImpl::SetAudioBufferDelay(size_t delay_ms) {
216 render_buffer_->SetAudioBufferDelay(delay_ms);
217}
218
peahd0263542017-01-03 04:20:34 -0800219} // namespace
220
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200221BlockProcessor* BlockProcessor::Create(const EchoCanceller3Config& config,
222 int sample_rate_hz) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100223 std::unique_ptr<RenderDelayBuffer> render_buffer(
224 RenderDelayBuffer::Create(config, NumBandsForRate(sample_rate_hz)));
peah69221db2017-01-27 03:28:19 -0800225 std::unique_ptr<RenderDelayController> delay_controller(
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100226 RenderDelayController::Create(config, sample_rate_hz));
peah69221db2017-01-27 03:28:19 -0800227 std::unique_ptr<EchoRemover> echo_remover(
peah697a5902017-06-30 07:06:10 -0700228 EchoRemover::Create(config, sample_rate_hz));
229 return Create(config, sample_rate_hz, std::move(render_buffer),
peah69221db2017-01-27 03:28:19 -0800230 std::move(delay_controller), std::move(echo_remover));
231}
232
233BlockProcessor* BlockProcessor::Create(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200234 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -0800235 int sample_rate_hz,
236 std::unique_ptr<RenderDelayBuffer> render_buffer) {
237 std::unique_ptr<RenderDelayController> delay_controller(
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100238 RenderDelayController::Create(config, sample_rate_hz));
peah69221db2017-01-27 03:28:19 -0800239 std::unique_ptr<EchoRemover> echo_remover(
peah697a5902017-06-30 07:06:10 -0700240 EchoRemover::Create(config, sample_rate_hz));
241 return Create(config, sample_rate_hz, std::move(render_buffer),
peah69221db2017-01-27 03:28:19 -0800242 std::move(delay_controller), std::move(echo_remover));
243}
244
245BlockProcessor* BlockProcessor::Create(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200246 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -0800247 int sample_rate_hz,
248 std::unique_ptr<RenderDelayBuffer> render_buffer,
249 std::unique_ptr<RenderDelayController> delay_controller,
250 std::unique_ptr<EchoRemover> echo_remover) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100251 return new BlockProcessorImpl(
252 config, sample_rate_hz, std::move(render_buffer),
253 std::move(delay_controller), std::move(echo_remover));
peahd0263542017-01-03 04:20:34 -0800254}
255
256} // namespace webrtc