blob: f0b963087c27503c1d69b6c6284bf06550915adb [file] [log] [blame]
peahd0263542017-01-03 04:20:34 -08001/*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 *
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 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020010#include "modules/audio_processing/aec3/block_processor.h"
peahd0263542017-01-03 04:20:34 -080011
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020012#include "api/optional.h"
13#include "modules/audio_processing/aec3/aec3_common.h"
14#include "modules/audio_processing/aec3/block_processor_metrics.h"
15#include "modules/audio_processing/aec3/echo_path_variability.h"
16#include "modules/audio_processing/logging/apm_data_dumper.h"
17#include "rtc_base/atomicops.h"
18#include "rtc_base/constructormagic.h"
Per Åhgrenfe9f2222017-09-26 23:33:26 +020019#include "rtc_base/logging.h"
peahd0263542017-01-03 04:20:34 -080020
21namespace webrtc {
22namespace {
23
peahcf02cf12017-04-05 14:18:07 -070024enum class BlockProcessorApiCall { kCapture, kRender };
25
peahd0263542017-01-03 04:20:34 -080026class BlockProcessorImpl final : public BlockProcessor {
27 public:
peah69221db2017-01-27 03:28:19 -080028 BlockProcessorImpl(int sample_rate_hz,
29 std::unique_ptr<RenderDelayBuffer> render_buffer,
30 std::unique_ptr<RenderDelayController> delay_controller,
31 std::unique_ptr<EchoRemover> echo_remover);
32
peahd0263542017-01-03 04:20:34 -080033 ~BlockProcessorImpl() override;
34
peah69221db2017-01-27 03:28:19 -080035 void ProcessCapture(bool echo_path_gain_change,
36 bool capture_signal_saturation,
peahd0263542017-01-03 04:20:34 -080037 std::vector<std::vector<float>>* capture_block) override;
38
peahcf02cf12017-04-05 14:18:07 -070039 void BufferRender(const std::vector<std::vector<float>>& block) override;
peahd0263542017-01-03 04:20:34 -080040
peah69221db2017-01-27 03:28:19 -080041 void UpdateEchoLeakageStatus(bool leakage_detected) override;
peahd0263542017-01-03 04:20:34 -080042
Gustaf Ullberg332150d2017-11-22 14:17:39 +010043 void GetMetrics(EchoControl::Metrics* metrics) const override;
44
peahd0263542017-01-03 04:20:34 -080045 private:
peahd0263542017-01-03 04:20:34 -080046 static int instance_count_;
peahcf02cf12017-04-05 14:18:07 -070047 bool no_capture_data_received_ = true;
48 bool no_render_data_received_ = true;
peahd0263542017-01-03 04:20:34 -080049 std::unique_ptr<ApmDataDumper> data_dumper_;
peah69221db2017-01-27 03:28:19 -080050 const size_t sample_rate_hz_;
51 std::unique_ptr<RenderDelayBuffer> render_buffer_;
52 std::unique_ptr<RenderDelayController> delay_controller_;
53 std::unique_ptr<EchoRemover> echo_remover_;
peahe985b3f2017-02-28 22:08:53 -080054 BlockProcessorMetrics metrics_;
peahcf02cf12017-04-05 14:18:07 -070055 bool render_buffer_overrun_occurred_ = false;
peahd0263542017-01-03 04:20:34 -080056 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(BlockProcessorImpl);
57};
58
59int BlockProcessorImpl::instance_count_ = 0;
60
peah69221db2017-01-27 03:28:19 -080061BlockProcessorImpl::BlockProcessorImpl(
62 int sample_rate_hz,
63 std::unique_ptr<RenderDelayBuffer> render_buffer,
64 std::unique_ptr<RenderDelayController> delay_controller,
65 std::unique_ptr<EchoRemover> echo_remover)
66 : data_dumper_(
67 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
68 sample_rate_hz_(sample_rate_hz),
69 render_buffer_(std::move(render_buffer)),
70 delay_controller_(std::move(delay_controller)),
peah21920892017-02-08 05:08:56 -080071 echo_remover_(std::move(echo_remover)) {
72 RTC_DCHECK(ValidFullBandRate(sample_rate_hz_));
73}
peahd0263542017-01-03 04:20:34 -080074
75BlockProcessorImpl::~BlockProcessorImpl() = default;
76
77void BlockProcessorImpl::ProcessCapture(
peah69221db2017-01-27 03:28:19 -080078 bool echo_path_gain_change,
79 bool capture_signal_saturation,
peahd0263542017-01-03 04:20:34 -080080 std::vector<std::vector<float>>* capture_block) {
81 RTC_DCHECK(capture_block);
82 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), capture_block->size());
83 RTC_DCHECK_EQ(kBlockSize, (*capture_block)[0].size());
peahcf02cf12017-04-05 14:18:07 -070084 data_dumper_->DumpRaw("aec3_processblock_call_order",
85 static_cast<int>(BlockProcessorApiCall::kCapture));
86 data_dumper_->DumpWav("aec3_processblock_capture_input", kBlockSize,
87 &(*capture_block)[0][0],
88 LowestBandRate(sample_rate_hz_), 1);
peah69221db2017-01-27 03:28:19 -080089
peahcf02cf12017-04-05 14:18:07 -070090 // Do not start processing until render data has been buffered as that will
91 // cause the buffers to be wrongly aligned.
92 no_capture_data_received_ = false;
93 if (no_render_data_received_) {
94 return;
peah69221db2017-01-27 03:28:19 -080095 }
96
peahcf02cf12017-04-05 14:18:07 -070097 data_dumper_->DumpWav("aec3_processblock_capture_input2", kBlockSize,
98 &(*capture_block)[0][0],
99 LowestBandRate(sample_rate_hz_), 1);
100
101 bool render_buffer_underrun = false;
102 if (render_buffer_overrun_occurred_) {
103 // Reset the render buffers and the alignment functionality when there has
104 // been a render buffer overrun as the buffer alignment may be noncausal.
105 delay_controller_->Reset();
106 render_buffer_->Reset();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100107 RTC_LOG(LS_WARNING) << "Reset due to detected render buffer overrun.";
peah69221db2017-01-27 03:28:19 -0800108 }
peahcf02cf12017-04-05 14:18:07 -0700109
peah4b572d02017-04-06 16:33:06 -0700110 // Update the render buffers with new render data, filling the buffers with
111 // empty blocks when there is no render data available.
112 render_buffer_underrun = !render_buffer_->UpdateBuffers();
Per Åhgrenfe9f2222017-09-26 23:33:26 +0200113 if (render_buffer_underrun) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100114 RTC_LOG(LS_WARNING) << "Render API jitter buffer underrun.";
Per Åhgrenfe9f2222017-09-26 23:33:26 +0200115 }
peah4b572d02017-04-06 16:33:06 -0700116
117 // Compute and and apply the render delay required to achieve proper signal
118 // alignment.
119 const size_t old_delay = render_buffer_->Delay();
120 const size_t new_delay = delay_controller_->GetDelay(
121 render_buffer_->GetDownsampledRenderBuffer(), (*capture_block)[0]);
peah4b572d02017-04-06 16:33:06 -0700122
peah96b951c2017-08-22 10:26:07 -0700123 bool delay_change;
124 if (new_delay >= kMinEchoPathDelayBlocks) {
125 render_buffer_->SetDelay(new_delay);
126 const size_t achieved_delay = render_buffer_->Delay();
127 delay_change = old_delay != achieved_delay || old_delay != new_delay ||
128 render_buffer_overrun_occurred_;
129
130 // Inform the delay controller of the actually set delay to allow it to
131 // properly react to a non-feasible delay.
132 delay_controller_->SetDelay(achieved_delay);
133 } else {
134 delay_controller_->Reset();
135 render_buffer_->Reset();
136 delay_change = true;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100137 RTC_LOG(LS_WARNING) << "Reset due to noncausal delay.";
peah96b951c2017-08-22 10:26:07 -0700138 }
peah4b572d02017-04-06 16:33:06 -0700139
140 // Remove the echo from the capture signal.
141 echo_remover_->ProcessCapture(
142 delay_controller_->AlignmentHeadroomSamples(),
peah96b951c2017-08-22 10:26:07 -0700143 EchoPathVariability(echo_path_gain_change, delay_change),
peah4b572d02017-04-06 16:33:06 -0700144 capture_signal_saturation, render_buffer_->GetRenderBuffer(),
145 capture_block);
146
peahcf02cf12017-04-05 14:18:07 -0700147 // Update the metrics.
148 metrics_.UpdateCapture(render_buffer_underrun);
149
150 render_buffer_overrun_occurred_ = false;
peahd0263542017-01-03 04:20:34 -0800151}
152
peahcf02cf12017-04-05 14:18:07 -0700153void BlockProcessorImpl::BufferRender(
154 const std::vector<std::vector<float>>& block) {
155 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), block.size());
156 RTC_DCHECK_EQ(kBlockSize, block[0].size());
157 data_dumper_->DumpRaw("aec3_processblock_call_order",
158 static_cast<int>(BlockProcessorApiCall::kRender));
159 data_dumper_->DumpWav("aec3_processblock_render_input", kBlockSize,
160 &block[0][0], LowestBandRate(sample_rate_hz_), 1);
peah69221db2017-01-27 03:28:19 -0800161
peahcf02cf12017-04-05 14:18:07 -0700162 no_render_data_received_ = false;
163
164 // Do not start buffer render data until capture data has been received as
165 // that data may give a false alignment.
166 if (no_capture_data_received_) {
167 return;
peah69221db2017-01-27 03:28:19 -0800168 }
peahcf02cf12017-04-05 14:18:07 -0700169
170 data_dumper_->DumpWav("aec3_processblock_render_input2", kBlockSize,
171 &block[0][0], LowestBandRate(sample_rate_hz_), 1);
172
173 // Buffer the render data.
174 render_buffer_overrun_occurred_ = !render_buffer_->Insert(block);
175
176 // Update the metrics.
177 metrics_.UpdateRender(render_buffer_overrun_occurred_);
peahd0263542017-01-03 04:20:34 -0800178}
179
peah69221db2017-01-27 03:28:19 -0800180void BlockProcessorImpl::UpdateEchoLeakageStatus(bool leakage_detected) {
181 echo_remover_->UpdateEchoLeakageStatus(leakage_detected);
182}
peahd0263542017-01-03 04:20:34 -0800183
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100184void BlockProcessorImpl::GetMetrics(EchoControl::Metrics* metrics) const {
185 echo_remover_->GetMetrics(metrics);
Per Åhgren83c4a022017-11-27 12:07:09 +0100186 const int block_size_ms = sample_rate_hz_ == 8000 ? 8 : 4;
187 metrics->delay_ms = static_cast<int>(render_buffer_->Delay()) * block_size_ms;
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100188}
189
peahd0263542017-01-03 04:20:34 -0800190} // namespace
191
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200192BlockProcessor* BlockProcessor::Create(const EchoCanceller3Config& config,
193 int sample_rate_hz) {
Per Åhgren38e2d952017-11-17 14:54:28 +0100194 std::unique_ptr<RenderDelayBuffer> render_buffer(RenderDelayBuffer::Create(
195 NumBandsForRate(sample_rate_hz), config.delay.down_sampling_factor,
196 GetDownSampledBufferSize(config.delay.down_sampling_factor,
197 config.delay.num_filters),
198 GetRenderDelayBufferSize(config.delay.down_sampling_factor,
199 config.delay.num_filters)));
peah69221db2017-01-27 03:28:19 -0800200 std::unique_ptr<RenderDelayController> delay_controller(
peah4fed3c02017-08-30 06:58:44 -0700201 RenderDelayController::Create(config, sample_rate_hz));
peah69221db2017-01-27 03:28:19 -0800202 std::unique_ptr<EchoRemover> echo_remover(
peah697a5902017-06-30 07:06:10 -0700203 EchoRemover::Create(config, sample_rate_hz));
204 return Create(config, sample_rate_hz, std::move(render_buffer),
peah69221db2017-01-27 03:28:19 -0800205 std::move(delay_controller), std::move(echo_remover));
206}
207
208BlockProcessor* BlockProcessor::Create(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200209 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -0800210 int sample_rate_hz,
211 std::unique_ptr<RenderDelayBuffer> render_buffer) {
212 std::unique_ptr<RenderDelayController> delay_controller(
peah4fed3c02017-08-30 06:58:44 -0700213 RenderDelayController::Create(config, sample_rate_hz));
peah69221db2017-01-27 03:28:19 -0800214 std::unique_ptr<EchoRemover> echo_remover(
peah697a5902017-06-30 07:06:10 -0700215 EchoRemover::Create(config, sample_rate_hz));
216 return Create(config, sample_rate_hz, std::move(render_buffer),
peah69221db2017-01-27 03:28:19 -0800217 std::move(delay_controller), std::move(echo_remover));
218}
219
220BlockProcessor* BlockProcessor::Create(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200221 const EchoCanceller3Config& config,
peah69221db2017-01-27 03:28:19 -0800222 int sample_rate_hz,
223 std::unique_ptr<RenderDelayBuffer> render_buffer,
224 std::unique_ptr<RenderDelayController> delay_controller,
225 std::unique_ptr<EchoRemover> echo_remover) {
226 return new BlockProcessorImpl(sample_rate_hz, std::move(render_buffer),
227 std::move(delay_controller),
228 std::move(echo_remover));
peahd0263542017-01-03 04:20:34 -0800229}
230
231} // namespace webrtc