blob: 1b7c18d3e97df8d85e47328f6b2b4cf332c24735 [file] [log] [blame]
Gustaf Ullberg11539f02018-10-15 13:40:29 +02001/*
2 * Copyright (c) 2018 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 */
Yves Gerey988cc082018-10-23 12:03:01 +020010#include <stddef.h>
Gustaf Ullberg11539f02018-10-15 13:40:29 +020011#include <algorithm>
12#include <memory>
Gustaf Ullberg11539f02018-10-15 13:40:29 +020013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include "absl/types/optional.h"
15#include "api/array_view.h"
Gustaf Ullberg11539f02018-10-15 13:40:29 +020016#include "api/audio/echo_canceller3_config.h"
17#include "modules/audio_processing/aec3/aec3_common.h"
Yves Gerey988cc082018-10-23 12:03:01 +020018#include "modules/audio_processing/aec3/delay_estimate.h"
19#include "modules/audio_processing/aec3/downsampled_render_buffer.h"
Gustaf Ullberg11539f02018-10-15 13:40:29 +020020#include "modules/audio_processing/aec3/echo_path_delay_estimator.h"
Yves Gerey988cc082018-10-23 12:03:01 +020021#include "modules/audio_processing/aec3/render_delay_controller.h"
Gustaf Ullberg11539f02018-10-15 13:40:29 +020022#include "modules/audio_processing/aec3/render_delay_controller_metrics.h"
Yves Gerey988cc082018-10-23 12:03:01 +020023#include "modules/audio_processing/logging/apm_data_dumper.h"
Gustaf Ullberg11539f02018-10-15 13:40:29 +020024#include "rtc_base/atomicops.h"
Yves Gerey988cc082018-10-23 12:03:01 +020025#include "rtc_base/checks.h"
Gustaf Ullberg11539f02018-10-15 13:40:29 +020026#include "rtc_base/constructormagic.h"
Per Åhgren8b7d2062018-10-30 23:44:40 +010027#include "system_wrappers/include/field_trial.h"
Gustaf Ullberg11539f02018-10-15 13:40:29 +020028
29namespace webrtc {
30
31namespace {
32
Per Åhgren8b7d2062018-10-30 23:44:40 +010033bool UseEarlyDelayDetection() {
34 return !field_trial::IsEnabled("WebRTC-Aec3EarlyDelayDetectionKillSwitch");
35}
36
Gustaf Ullberg11539f02018-10-15 13:40:29 +020037class RenderDelayControllerImpl2 final : public RenderDelayController {
38 public:
39 RenderDelayControllerImpl2(const EchoCanceller3Config& config,
40 int sample_rate_hz);
41 ~RenderDelayControllerImpl2() override;
Per Åhgren8b7d2062018-10-30 23:44:40 +010042 void Reset(bool reset_delay_confidence) override;
Gustaf Ullberg11539f02018-10-15 13:40:29 +020043 void LogRenderCall() override;
44 absl::optional<DelayEstimate> GetDelay(
45 const DownsampledRenderBuffer& render_buffer,
46 size_t render_delay_buffer_delay,
47 const absl::optional<int>& echo_remover_delay,
48 rtc::ArrayView<const float> capture) override;
49
50 private:
51 static int instance_count_;
52 std::unique_ptr<ApmDataDumper> data_dumper_;
Per Åhgren8b7d2062018-10-30 23:44:40 +010053 const bool use_early_delay_detection_;
Gustaf Ullberg11539f02018-10-15 13:40:29 +020054 const int delay_headroom_blocks_;
55 const int hysteresis_limit_1_blocks_;
56 const int hysteresis_limit_2_blocks_;
57 absl::optional<DelayEstimate> delay_;
58 EchoPathDelayEstimator delay_estimator_;
59 RenderDelayControllerMetrics metrics_;
60 absl::optional<DelayEstimate> delay_samples_;
61 size_t capture_call_counter_ = 0;
62 int delay_change_counter_ = 0;
Per Åhgren8b7d2062018-10-30 23:44:40 +010063 DelayEstimate::Quality last_delay_estimate_quality_;
Gustaf Ullberg11539f02018-10-15 13:40:29 +020064 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RenderDelayControllerImpl2);
65};
66
67DelayEstimate ComputeBufferDelay(
68 const absl::optional<DelayEstimate>& current_delay,
69 int delay_headroom_blocks,
70 int hysteresis_limit_1_blocks,
71 int hysteresis_limit_2_blocks,
72 DelayEstimate estimated_delay) {
73 // The below division is not exact and the truncation is intended.
74 const int echo_path_delay_blocks = estimated_delay.delay >> kBlockSizeLog2;
75
76 // Compute the buffer delay increase required to achieve the desired latency.
77 size_t new_delay_blocks =
78 std::max(echo_path_delay_blocks - delay_headroom_blocks, 0);
79
80 // Add hysteresis.
81 if (current_delay) {
82 size_t current_delay_blocks = current_delay->delay;
83 if (new_delay_blocks > current_delay_blocks) {
84 if (new_delay_blocks <=
85 current_delay_blocks + hysteresis_limit_1_blocks) {
86 new_delay_blocks = current_delay_blocks;
87 }
88 } else if (new_delay_blocks < current_delay_blocks) {
89 size_t hysteresis_limit = std::max(
90 static_cast<int>(current_delay_blocks) - hysteresis_limit_2_blocks,
91 0);
92 if (new_delay_blocks >= hysteresis_limit) {
93 new_delay_blocks = current_delay_blocks;
94 }
95 }
96 }
97
98 DelayEstimate new_delay = estimated_delay;
99 new_delay.delay = new_delay_blocks;
100 return new_delay;
101}
102
103int RenderDelayControllerImpl2::instance_count_ = 0;
104
105RenderDelayControllerImpl2::RenderDelayControllerImpl2(
106 const EchoCanceller3Config& config,
107 int sample_rate_hz)
108 : data_dumper_(
109 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Per Åhgren8b7d2062018-10-30 23:44:40 +0100110 use_early_delay_detection_(UseEarlyDelayDetection()),
Gustaf Ullberg11539f02018-10-15 13:40:29 +0200111 delay_headroom_blocks_(
112 static_cast<int>(config.delay.delay_headroom_blocks)),
113 hysteresis_limit_1_blocks_(
114 static_cast<int>(config.delay.hysteresis_limit_1_blocks)),
115 hysteresis_limit_2_blocks_(
116 static_cast<int>(config.delay.hysteresis_limit_2_blocks)),
Per Åhgren8b7d2062018-10-30 23:44:40 +0100117 delay_estimator_(data_dumper_.get(), config),
118 last_delay_estimate_quality_(DelayEstimate::Quality::kCoarse) {
Gustaf Ullberg11539f02018-10-15 13:40:29 +0200119 RTC_DCHECK(ValidFullBandRate(sample_rate_hz));
120 delay_estimator_.LogDelayEstimationProperties(sample_rate_hz, 0);
121}
122
123RenderDelayControllerImpl2::~RenderDelayControllerImpl2() = default;
124
Per Åhgren8b7d2062018-10-30 23:44:40 +0100125void RenderDelayControllerImpl2::Reset(bool reset_delay_confidence) {
Gustaf Ullberg11539f02018-10-15 13:40:29 +0200126 delay_ = absl::nullopt;
127 delay_samples_ = absl::nullopt;
Per Åhgren8b7d2062018-10-30 23:44:40 +0100128 delay_estimator_.Reset(reset_delay_confidence);
Gustaf Ullberg11539f02018-10-15 13:40:29 +0200129 delay_change_counter_ = 0;
Per Åhgren8b7d2062018-10-30 23:44:40 +0100130 if (reset_delay_confidence || true) {
131 last_delay_estimate_quality_ = DelayEstimate::Quality::kCoarse;
132 }
Gustaf Ullberg11539f02018-10-15 13:40:29 +0200133}
134
135void RenderDelayControllerImpl2::LogRenderCall() {}
136
137absl::optional<DelayEstimate> RenderDelayControllerImpl2::GetDelay(
138 const DownsampledRenderBuffer& render_buffer,
139 size_t render_delay_buffer_delay,
140 const absl::optional<int>& echo_remover_delay,
141 rtc::ArrayView<const float> capture) {
142 RTC_DCHECK_EQ(kBlockSize, capture.size());
143 ++capture_call_counter_;
144
145 auto delay_samples = delay_estimator_.EstimateDelay(render_buffer, capture);
146
147 // Overrule the delay estimator delay if the echo remover reports a delay.
148 if (echo_remover_delay) {
149 int total_echo_remover_delay_samples =
150 (render_delay_buffer_delay + *echo_remover_delay) * kBlockSize;
151 delay_samples = DelayEstimate(DelayEstimate::Quality::kRefined,
152 total_echo_remover_delay_samples);
153 }
154
155 if (delay_samples) {
Gustaf Ullberg11539f02018-10-15 13:40:29 +0200156 if (!delay_samples_ || delay_samples->delay != delay_samples_->delay) {
157 delay_change_counter_ = 0;
158 }
159 if (delay_samples_) {
160 delay_samples_->blocks_since_last_change =
161 delay_samples_->delay == delay_samples->delay
162 ? delay_samples_->blocks_since_last_change + 1
163 : 0;
164 delay_samples_->blocks_since_last_update = 0;
165 delay_samples_->delay = delay_samples->delay;
166 delay_samples_->quality = delay_samples->quality;
167 } else {
168 delay_samples_ = delay_samples;
169 }
170 } else {
171 if (delay_samples_) {
172 ++delay_samples_->blocks_since_last_change;
173 ++delay_samples_->blocks_since_last_update;
174 }
175 }
176
177 if (delay_change_counter_ < 2 * kNumBlocksPerSecond) {
178 ++delay_change_counter_;
179 }
180
181 if (delay_samples_) {
182 // Compute the render delay buffer delay.
Per Åhgren8b7d2062018-10-30 23:44:40 +0100183 const bool use_hysteresis =
184 last_delay_estimate_quality_ == DelayEstimate::Quality::kRefined &&
185 delay_samples_->quality == DelayEstimate::Quality::kRefined;
Gustaf Ullberg11539f02018-10-15 13:40:29 +0200186 delay_ = ComputeBufferDelay(delay_, delay_headroom_blocks_,
Per Åhgren8b7d2062018-10-30 23:44:40 +0100187 use_hysteresis ? hysteresis_limit_1_blocks_ : 0,
188 use_hysteresis ? hysteresis_limit_2_blocks_ : 0,
189 *delay_samples_);
190 last_delay_estimate_quality_ = delay_samples_->quality;
Gustaf Ullberg11539f02018-10-15 13:40:29 +0200191 }
192
193 metrics_.Update(delay_samples_ ? absl::optional<size_t>(delay_samples_->delay)
194 : absl::nullopt,
195 delay_ ? delay_->delay : 0, 0);
196
197 data_dumper_->DumpRaw("aec3_render_delay_controller_delay",
198 delay_samples ? delay_samples->delay : 0);
199 data_dumper_->DumpRaw("aec3_render_delay_controller_buffer_delay",
200 delay_ ? delay_->delay : 0);
201
202 return delay_;
203}
204
205} // namespace
206
207RenderDelayController* RenderDelayController::Create2(
208 const EchoCanceller3Config& config,
209 int sample_rate_hz) {
210 return new RenderDelayControllerImpl2(config, sample_rate_hz);
211}
212
213} // namespace webrtc