blob: b3a15c0957b0c5ffa4692ee394d734659fe76648 [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"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "rtc_base/atomic_ops.h"
Yves Gerey988cc082018-10-23 12:03:01 +020025#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "rtc_base/constructor_magic.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;
Gustaf Ullberg777cf262018-11-22 16:02:34 +010049 bool HasClockdrift() const override;
Gustaf Ullberg11539f02018-10-15 13:40:29 +020050
51 private:
52 static int instance_count_;
53 std::unique_ptr<ApmDataDumper> data_dumper_;
Per Åhgren8b7d2062018-10-30 23:44:40 +010054 const bool use_early_delay_detection_;
Gustaf Ullberg11539f02018-10-15 13:40:29 +020055 const int delay_headroom_blocks_;
56 const int hysteresis_limit_1_blocks_;
57 const int hysteresis_limit_2_blocks_;
58 absl::optional<DelayEstimate> delay_;
59 EchoPathDelayEstimator delay_estimator_;
60 RenderDelayControllerMetrics metrics_;
61 absl::optional<DelayEstimate> delay_samples_;
62 size_t capture_call_counter_ = 0;
63 int delay_change_counter_ = 0;
Per Åhgren8b7d2062018-10-30 23:44:40 +010064 DelayEstimate::Quality last_delay_estimate_quality_;
Gustaf Ullberg11539f02018-10-15 13:40:29 +020065 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RenderDelayControllerImpl2);
66};
67
68DelayEstimate ComputeBufferDelay(
69 const absl::optional<DelayEstimate>& current_delay,
70 int delay_headroom_blocks,
71 int hysteresis_limit_1_blocks,
72 int hysteresis_limit_2_blocks,
73 DelayEstimate estimated_delay) {
74 // The below division is not exact and the truncation is intended.
75 const int echo_path_delay_blocks = estimated_delay.delay >> kBlockSizeLog2;
76
77 // Compute the buffer delay increase required to achieve the desired latency.
78 size_t new_delay_blocks =
79 std::max(echo_path_delay_blocks - delay_headroom_blocks, 0);
80
81 // Add hysteresis.
82 if (current_delay) {
83 size_t current_delay_blocks = current_delay->delay;
84 if (new_delay_blocks > current_delay_blocks) {
85 if (new_delay_blocks <=
86 current_delay_blocks + hysteresis_limit_1_blocks) {
87 new_delay_blocks = current_delay_blocks;
88 }
89 } else if (new_delay_blocks < current_delay_blocks) {
90 size_t hysteresis_limit = std::max(
91 static_cast<int>(current_delay_blocks) - hysteresis_limit_2_blocks,
92 0);
93 if (new_delay_blocks >= hysteresis_limit) {
94 new_delay_blocks = current_delay_blocks;
95 }
96 }
97 }
98
99 DelayEstimate new_delay = estimated_delay;
100 new_delay.delay = new_delay_blocks;
101 return new_delay;
102}
103
104int RenderDelayControllerImpl2::instance_count_ = 0;
105
106RenderDelayControllerImpl2::RenderDelayControllerImpl2(
107 const EchoCanceller3Config& config,
108 int sample_rate_hz)
109 : data_dumper_(
110 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Per Åhgren8b7d2062018-10-30 23:44:40 +0100111 use_early_delay_detection_(UseEarlyDelayDetection()),
Gustaf Ullberg11539f02018-10-15 13:40:29 +0200112 delay_headroom_blocks_(
113 static_cast<int>(config.delay.delay_headroom_blocks)),
114 hysteresis_limit_1_blocks_(
115 static_cast<int>(config.delay.hysteresis_limit_1_blocks)),
116 hysteresis_limit_2_blocks_(
117 static_cast<int>(config.delay.hysteresis_limit_2_blocks)),
Per Åhgren8b7d2062018-10-30 23:44:40 +0100118 delay_estimator_(data_dumper_.get(), config),
119 last_delay_estimate_quality_(DelayEstimate::Quality::kCoarse) {
Gustaf Ullberg11539f02018-10-15 13:40:29 +0200120 RTC_DCHECK(ValidFullBandRate(sample_rate_hz));
121 delay_estimator_.LogDelayEstimationProperties(sample_rate_hz, 0);
122}
123
124RenderDelayControllerImpl2::~RenderDelayControllerImpl2() = default;
125
Per Åhgren8b7d2062018-10-30 23:44:40 +0100126void RenderDelayControllerImpl2::Reset(bool reset_delay_confidence) {
Gustaf Ullberg11539f02018-10-15 13:40:29 +0200127 delay_ = absl::nullopt;
128 delay_samples_ = absl::nullopt;
Per Åhgren8b7d2062018-10-30 23:44:40 +0100129 delay_estimator_.Reset(reset_delay_confidence);
Gustaf Ullberg11539f02018-10-15 13:40:29 +0200130 delay_change_counter_ = 0;
Per Åhgrena33c8952018-11-13 11:20:28 +0100131 if (reset_delay_confidence) {
Per Åhgren8b7d2062018-10-30 23:44:40 +0100132 last_delay_estimate_quality_ = DelayEstimate::Quality::kCoarse;
133 }
Gustaf Ullberg11539f02018-10-15 13:40:29 +0200134}
135
136void RenderDelayControllerImpl2::LogRenderCall() {}
137
138absl::optional<DelayEstimate> RenderDelayControllerImpl2::GetDelay(
139 const DownsampledRenderBuffer& render_buffer,
140 size_t render_delay_buffer_delay,
141 const absl::optional<int>& echo_remover_delay,
142 rtc::ArrayView<const float> capture) {
143 RTC_DCHECK_EQ(kBlockSize, capture.size());
144 ++capture_call_counter_;
145
146 auto delay_samples = delay_estimator_.EstimateDelay(render_buffer, capture);
147
148 // Overrule the delay estimator delay if the echo remover reports a delay.
149 if (echo_remover_delay) {
150 int total_echo_remover_delay_samples =
151 (render_delay_buffer_delay + *echo_remover_delay) * kBlockSize;
152 delay_samples = DelayEstimate(DelayEstimate::Quality::kRefined,
153 total_echo_remover_delay_samples);
154 }
155
156 if (delay_samples) {
Gustaf Ullberg11539f02018-10-15 13:40:29 +0200157 if (!delay_samples_ || delay_samples->delay != delay_samples_->delay) {
158 delay_change_counter_ = 0;
159 }
160 if (delay_samples_) {
161 delay_samples_->blocks_since_last_change =
162 delay_samples_->delay == delay_samples->delay
163 ? delay_samples_->blocks_since_last_change + 1
164 : 0;
165 delay_samples_->blocks_since_last_update = 0;
166 delay_samples_->delay = delay_samples->delay;
167 delay_samples_->quality = delay_samples->quality;
168 } else {
169 delay_samples_ = delay_samples;
170 }
171 } else {
172 if (delay_samples_) {
173 ++delay_samples_->blocks_since_last_change;
174 ++delay_samples_->blocks_since_last_update;
175 }
176 }
177
178 if (delay_change_counter_ < 2 * kNumBlocksPerSecond) {
179 ++delay_change_counter_;
180 }
181
182 if (delay_samples_) {
183 // Compute the render delay buffer delay.
Per Åhgren8b7d2062018-10-30 23:44:40 +0100184 const bool use_hysteresis =
185 last_delay_estimate_quality_ == DelayEstimate::Quality::kRefined &&
186 delay_samples_->quality == DelayEstimate::Quality::kRefined;
Gustaf Ullberg11539f02018-10-15 13:40:29 +0200187 delay_ = ComputeBufferDelay(delay_, delay_headroom_blocks_,
Per Åhgren8b7d2062018-10-30 23:44:40 +0100188 use_hysteresis ? hysteresis_limit_1_blocks_ : 0,
189 use_hysteresis ? hysteresis_limit_2_blocks_ : 0,
190 *delay_samples_);
191 last_delay_estimate_quality_ = delay_samples_->quality;
Gustaf Ullberg11539f02018-10-15 13:40:29 +0200192 }
193
194 metrics_.Update(delay_samples_ ? absl::optional<size_t>(delay_samples_->delay)
195 : absl::nullopt,
Gustaf Ullberg777cf262018-11-22 16:02:34 +0100196 delay_ ? delay_->delay : 0, 0, delay_estimator_.Clockdrift());
Gustaf Ullberg11539f02018-10-15 13:40:29 +0200197
198 data_dumper_->DumpRaw("aec3_render_delay_controller_delay",
199 delay_samples ? delay_samples->delay : 0);
200 data_dumper_->DumpRaw("aec3_render_delay_controller_buffer_delay",
201 delay_ ? delay_->delay : 0);
202
203 return delay_;
204}
205
Gustaf Ullberg777cf262018-11-22 16:02:34 +0100206bool RenderDelayControllerImpl2::HasClockdrift() const {
207 return delay_estimator_.Clockdrift() != ClockdriftDetector::Level::kNone;
208}
209
Gustaf Ullberg11539f02018-10-15 13:40:29 +0200210} // namespace
211
212RenderDelayController* RenderDelayController::Create2(
213 const EchoCanceller3Config& config,
214 int sample_rate_hz) {
215 return new RenderDelayControllerImpl2(config, sample_rate_hz);
216}
217
218} // namespace webrtc