blob: 2674827dde8362965edb39b6022c6a92b0e6e22b [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_;
peahd0263542017-01-03 04:20:34 -080074};
75
76int BlockProcessorImpl::instance_count_ = 0;
77
peah69221db2017-01-27 03:28:19 -080078BlockProcessorImpl::BlockProcessorImpl(
Per Åhgren8ba58612017-12-01 23:01:44 +010079 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -080080 int sample_rate_hz,
81 std::unique_ptr<RenderDelayBuffer> render_buffer,
82 std::unique_ptr<RenderDelayController> delay_controller,
83 std::unique_ptr<EchoRemover> echo_remover)
84 : data_dumper_(
85 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Per Åhgren8ba58612017-12-01 23:01:44 +010086 config_(config),
peah69221db2017-01-27 03:28:19 -080087 sample_rate_hz_(sample_rate_hz),
88 render_buffer_(std::move(render_buffer)),
89 delay_controller_(std::move(delay_controller)),
Per Åhgren8ba58612017-12-01 23:01:44 +010090 echo_remover_(std::move(echo_remover)),
91 render_event_(RenderDelayBuffer::BufferingEvent::kNone) {
peah21920892017-02-08 05:08:56 -080092 RTC_DCHECK(ValidFullBandRate(sample_rate_hz_));
93}
peahd0263542017-01-03 04:20:34 -080094
95BlockProcessorImpl::~BlockProcessorImpl() = default;
96
97void BlockProcessorImpl::ProcessCapture(
peah69221db2017-01-27 03:28:19 -080098 bool echo_path_gain_change,
99 bool capture_signal_saturation,
peahd0263542017-01-03 04:20:34 -0800100 std::vector<std::vector<float>>* capture_block) {
101 RTC_DCHECK(capture_block);
102 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), capture_block->size());
103 RTC_DCHECK_EQ(kBlockSize, (*capture_block)[0].size());
Per Åhgrenc59a5762017-12-11 21:34:19 +0100104
105 capture_call_counter_++;
106
peahcf02cf12017-04-05 14:18:07 -0700107 data_dumper_->DumpRaw("aec3_processblock_call_order",
108 static_cast<int>(BlockProcessorApiCall::kCapture));
109 data_dumper_->DumpWav("aec3_processblock_capture_input", kBlockSize,
110 &(*capture_block)[0][0],
111 LowestBandRate(sample_rate_hz_), 1);
peah69221db2017-01-27 03:28:19 -0800112
Per Åhgren8ba58612017-12-01 23:01:44 +0100113 if (render_properly_started_) {
114 if (!capture_properly_started_) {
115 capture_properly_started_ = true;
116 render_buffer_->Reset();
Per Åhgren8b7d2062018-10-30 23:44:40 +0100117 delay_controller_->Reset(true);
Per Åhgren8ba58612017-12-01 23:01:44 +0100118 }
119 } else {
120 // If no render data has yet arrived, do not process the capture signal.
peahcf02cf12017-04-05 14:18:07 -0700121 return;
peah69221db2017-01-27 03:28:19 -0800122 }
123
Per Åhgren8ba58612017-12-01 23:01:44 +0100124 EchoPathVariability echo_path_variability(
125 echo_path_gain_change, EchoPathVariability::DelayAdjustment::kNone,
126 false);
127
128 if (render_event_ == RenderDelayBuffer::BufferingEvent::kRenderOverrun &&
129 render_properly_started_) {
130 echo_path_variability.delay_change =
131 EchoPathVariability::DelayAdjustment::kBufferFlush;
Per Åhgren8b7d2062018-10-30 23:44:40 +0100132 delay_controller_->Reset(true);
Per Åhgrenc59a5762017-12-11 21:34:19 +0100133 RTC_LOG(LS_WARNING) << "Reset due to render buffer overrun at block "
134 << capture_call_counter_;
Per Åhgren8ba58612017-12-01 23:01:44 +0100135 }
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100136 render_event_ = RenderDelayBuffer::BufferingEvent::kNone;
Per Åhgren8ba58612017-12-01 23:01:44 +0100137
138 // Update the render buffers with any newly arrived render blocks and prepare
139 // the render buffers for reading the render data corresponding to the current
140 // capture block.
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100141 RenderDelayBuffer::BufferingEvent buffer_event =
142 render_buffer_->PrepareCaptureProcessing();
143 // Reset the delay controller at render buffer underrun.
144 if (buffer_event == RenderDelayBuffer::BufferingEvent::kRenderUnderrun) {
145 delay_controller_->Reset(false);
Per Åhgren8ba58612017-12-01 23:01:44 +0100146 }
147
peahcf02cf12017-04-05 14:18:07 -0700148 data_dumper_->DumpWav("aec3_processblock_capture_input2", kBlockSize,
149 &(*capture_block)[0][0],
150 LowestBandRate(sample_rate_hz_), 1);
151
Gustaf Ullberg1107cab2019-04-02 14:00:46 +0200152 // Compute and apply the render delay required to achieve proper signal
peah4b572d02017-04-06 16:33:06 -0700153 // alignment.
Gustaf Ullberg1107cab2019-04-02 14:00:46 +0200154 estimated_delay_ =
155 delay_controller_->GetDelay(render_buffer_->GetDownsampledRenderBuffer(),
156 render_buffer_->Delay(), (*capture_block)[0]);
peah4b572d02017-04-06 16:33:06 -0700157
Per Åhgrena76ef9d2018-01-25 07:01:34 +0100158 if (estimated_delay_) {
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100159 bool delay_change = render_buffer_->SetDelay(estimated_delay_->delay);
160 if (delay_change) {
161 RTC_LOG(LS_WARNING) << "Delay changed to " << estimated_delay_->delay
162 << " at block " << capture_call_counter_;
163 echo_path_variability.delay_change =
164 EchoPathVariability::DelayAdjustment::kNewDetectedDelay;
Per Åhgrenc59a5762017-12-11 21:34:19 +0100165 }
peah96b951c2017-08-22 10:26:07 -0700166 }
peah4b572d02017-04-06 16:33:06 -0700167
Gustaf Ullberg777cf262018-11-22 16:02:34 +0100168 echo_path_variability.clock_drift = delay_controller_->HasClockdrift();
169
peah4b572d02017-04-06 16:33:06 -0700170 // Remove the echo from the capture signal.
171 echo_remover_->ProcessCapture(
Per Åhgren3ab308f2018-02-21 08:46:03 +0100172 echo_path_variability, capture_signal_saturation, estimated_delay_,
Per Åhgrende22a172017-12-20 18:00:51 +0100173 render_buffer_->GetRenderBuffer(), capture_block);
peah4b572d02017-04-06 16:33:06 -0700174
peahcf02cf12017-04-05 14:18:07 -0700175 // Update the metrics.
Per Åhgren8ba58612017-12-01 23:01:44 +0100176 metrics_.UpdateCapture(false);
peahd0263542017-01-03 04:20:34 -0800177}
178
peahcf02cf12017-04-05 14:18:07 -0700179void BlockProcessorImpl::BufferRender(
180 const std::vector<std::vector<float>>& block) {
181 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), block.size());
182 RTC_DCHECK_EQ(kBlockSize, block[0].size());
183 data_dumper_->DumpRaw("aec3_processblock_call_order",
184 static_cast<int>(BlockProcessorApiCall::kRender));
185 data_dumper_->DumpWav("aec3_processblock_render_input", kBlockSize,
186 &block[0][0], LowestBandRate(sample_rate_hz_), 1);
peahcf02cf12017-04-05 14:18:07 -0700187 data_dumper_->DumpWav("aec3_processblock_render_input2", kBlockSize,
188 &block[0][0], LowestBandRate(sample_rate_hz_), 1);
189
Per Åhgren8ba58612017-12-01 23:01:44 +0100190 render_event_ = render_buffer_->Insert(block);
peahcf02cf12017-04-05 14:18:07 -0700191
Per Åhgren8ba58612017-12-01 23:01:44 +0100192 metrics_.UpdateRender(render_event_ !=
193 RenderDelayBuffer::BufferingEvent::kNone);
194
195 render_properly_started_ = true;
Per Åhgren47127762018-02-13 12:59:33 +0100196 delay_controller_->LogRenderCall();
peahd0263542017-01-03 04:20:34 -0800197}
198
peah69221db2017-01-27 03:28:19 -0800199void BlockProcessorImpl::UpdateEchoLeakageStatus(bool leakage_detected) {
200 echo_remover_->UpdateEchoLeakageStatus(leakage_detected);
201}
peahd0263542017-01-03 04:20:34 -0800202
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100203void BlockProcessorImpl::GetMetrics(EchoControl::Metrics* metrics) const {
204 echo_remover_->GetMetrics(metrics);
Per Åhgren83c4a022017-11-27 12:07:09 +0100205 const int block_size_ms = sample_rate_hz_ == 8000 ? 8 : 4;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200206 absl::optional<size_t> delay = render_buffer_->Delay();
Per Åhgrenc59a5762017-12-11 21:34:19 +0100207 metrics->delay_ms = delay ? static_cast<int>(*delay) * block_size_ms : 0;
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100208}
209
Per Åhgrend0fa8202018-04-18 09:35:13 +0200210void BlockProcessorImpl::SetAudioBufferDelay(size_t delay_ms) {
211 render_buffer_->SetAudioBufferDelay(delay_ms);
212}
213
peahd0263542017-01-03 04:20:34 -0800214} // namespace
215
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200216BlockProcessor* BlockProcessor::Create(const EchoCanceller3Config& config,
217 int sample_rate_hz) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100218 std::unique_ptr<RenderDelayBuffer> render_buffer(
219 RenderDelayBuffer::Create(config, NumBandsForRate(sample_rate_hz)));
peah69221db2017-01-27 03:28:19 -0800220 std::unique_ptr<RenderDelayController> delay_controller(
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100221 RenderDelayController::Create(config, sample_rate_hz));
peah69221db2017-01-27 03:28:19 -0800222 std::unique_ptr<EchoRemover> echo_remover(
peah697a5902017-06-30 07:06:10 -0700223 EchoRemover::Create(config, sample_rate_hz));
224 return Create(config, sample_rate_hz, std::move(render_buffer),
peah69221db2017-01-27 03:28:19 -0800225 std::move(delay_controller), std::move(echo_remover));
226}
227
228BlockProcessor* BlockProcessor::Create(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200229 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -0800230 int sample_rate_hz,
231 std::unique_ptr<RenderDelayBuffer> render_buffer) {
232 std::unique_ptr<RenderDelayController> delay_controller(
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100233 RenderDelayController::Create(config, sample_rate_hz));
peah69221db2017-01-27 03:28:19 -0800234 std::unique_ptr<EchoRemover> echo_remover(
peah697a5902017-06-30 07:06:10 -0700235 EchoRemover::Create(config, sample_rate_hz));
236 return Create(config, sample_rate_hz, std::move(render_buffer),
peah69221db2017-01-27 03:28:19 -0800237 std::move(delay_controller), std::move(echo_remover));
238}
239
240BlockProcessor* BlockProcessor::Create(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200241 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -0800242 int sample_rate_hz,
243 std::unique_ptr<RenderDelayBuffer> render_buffer,
244 std::unique_ptr<RenderDelayController> delay_controller,
245 std::unique_ptr<EchoRemover> echo_remover) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100246 return new BlockProcessorImpl(
247 config, sample_rate_hz, std::move(render_buffer),
248 std::move(delay_controller), std::move(echo_remover));
peahd0263542017-01-03 04:20:34 -0800249}
250
251} // namespace webrtc