blob: 3677085d8101fa1b6d134012a401d14759a7b358 [file] [log] [blame]
peah69221db2017-01-27 03:28:19 -08001/*
Gustaf Ullberge47433f2019-01-24 16:00:57 +01002 * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
peah69221db2017-01-27 03:28:19 -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 */
Jonas Olssona4d87372019-07-05 19:08:33 +020010#include "modules/audio_processing/aec3/render_delay_controller.h"
11
Gustaf Ullberge47433f2019-01-24 16:00:57 +010012#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020013
peah69221db2017-01-27 03:28:19 -080014#include <algorithm>
15#include <memory>
peah69221db2017-01-27 03:28:19 -080016
Gustaf Ullberge47433f2019-01-24 16:00:57 +010017#include "absl/types/optional.h"
18#include "api/array_view.h"
Gustaf Ullberg3646f972018-02-14 15:19:04 +010019#include "api/audio/echo_canceller3_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/audio_processing/aec3/aec3_common.h"
Gustaf Ullberge47433f2019-01-24 16:00:57 +010021#include "modules/audio_processing/aec3/delay_estimate.h"
22#include "modules/audio_processing/aec3/downsampled_render_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/audio_processing/aec3/echo_path_delay_estimator.h"
24#include "modules/audio_processing/aec3/render_delay_controller_metrics.h"
Yves Gerey988cc082018-10-23 12:03:01 +020025#include "modules/audio_processing/logging/apm_data_dumper.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "rtc_base/atomic_ops.h"
Yves Gerey988cc082018-10-23 12:03:01 +020027#include "rtc_base/checks.h"
peah69221db2017-01-27 03:28:19 -080028
29namespace webrtc {
30
31namespace {
32
peah69221db2017-01-27 03:28:19 -080033class RenderDelayControllerImpl final : public RenderDelayController {
34 public:
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020035 RenderDelayControllerImpl(const EchoCanceller3Config& config,
Per Åhgren6a05bb12019-12-03 11:24:59 +010036 int sample_rate_hz,
37 size_t num_capture_channels);
Niels Möllerde953292020-09-29 09:46:21 +020038
39 RenderDelayControllerImpl() = delete;
40 RenderDelayControllerImpl(const RenderDelayControllerImpl&) = delete;
41 RenderDelayControllerImpl& operator=(const RenderDelayControllerImpl&) =
42 delete;
43
peah69221db2017-01-27 03:28:19 -080044 ~RenderDelayControllerImpl() override;
Per Åhgren8b7d2062018-10-30 23:44:40 +010045 void Reset(bool reset_delay_confidence) override;
Per Åhgren47127762018-02-13 12:59:33 +010046 void LogRenderCall() override;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020047 absl::optional<DelayEstimate> GetDelay(
Per Åhgrena76ef9d2018-01-25 07:01:34 +010048 const DownsampledRenderBuffer& render_buffer,
Per Åhgren5c532d32018-03-22 00:29:25 +010049 size_t render_delay_buffer_delay,
Gustaf Ullbergee84d392019-09-10 09:36:43 +020050 const std::vector<std::vector<float>>& capture) override;
Gustaf Ullberg777cf262018-11-22 16:02:34 +010051 bool HasClockdrift() const override;
peah69221db2017-01-27 03:28:19 -080052
53 private:
54 static int instance_count_;
55 std::unique_ptr<ApmDataDumper> data_dumper_;
Gustaf Ullberg9249fbf2019-03-14 11:24:54 +010056 const int hysteresis_limit_blocks_;
57 const int delay_headroom_samples_;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020058 absl::optional<DelayEstimate> delay_;
Per Åhgren8ba58612017-12-01 23:01:44 +010059 EchoPathDelayEstimator delay_estimator_;
peahe985b3f2017-02-28 22:08:53 -080060 RenderDelayControllerMetrics metrics_;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020061 absl::optional<DelayEstimate> delay_samples_;
Per Åhgrend8243fa2018-02-22 17:51:39 +010062 size_t capture_call_counter_ = 0;
Per Åhgren47127762018-02-13 12:59:33 +010063 int delay_change_counter_ = 0;
Per Åhgren8b7d2062018-10-30 23:44:40 +010064 DelayEstimate::Quality last_delay_estimate_quality_;
peah69221db2017-01-27 03:28:19 -080065};
66
Per Åhgren47127762018-02-13 12:59:33 +010067DelayEstimate ComputeBufferDelay(
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020068 const absl::optional<DelayEstimate>& current_delay,
Gustaf Ullberg9249fbf2019-03-14 11:24:54 +010069 int hysteresis_limit_blocks,
70 int delay_headroom_samples,
Per Åhgrena76ef9d2018-01-25 07:01:34 +010071 DelayEstimate estimated_delay) {
Gustaf Ullberg9249fbf2019-03-14 11:24:54 +010072 // Subtract delay headroom.
73 const int delay_with_headroom_samples = std::max(
74 static_cast<int>(estimated_delay.delay) - delay_headroom_samples, 0);
peah69221db2017-01-27 03:28:19 -080075
76 // Compute the buffer delay increase required to achieve the desired latency.
Gustaf Ullberg9249fbf2019-03-14 11:24:54 +010077 size_t new_delay_blocks = delay_with_headroom_samples >> kBlockSizeLog2;
Per Åhgrena76ef9d2018-01-25 07:01:34 +010078
peah69221db2017-01-27 03:28:19 -080079 // Add hysteresis.
Per Åhgrenc59a5762017-12-11 21:34:19 +010080 if (current_delay) {
Per Åhgrena76ef9d2018-01-25 07:01:34 +010081 size_t current_delay_blocks = current_delay->delay;
Gustaf Ullberg9249fbf2019-03-14 11:24:54 +010082 if (new_delay_blocks > current_delay_blocks &&
83 new_delay_blocks <= current_delay_blocks + hysteresis_limit_blocks) {
84 new_delay_blocks = current_delay_blocks;
Per Åhgrenc59a5762017-12-11 21:34:19 +010085 }
peah69221db2017-01-27 03:28:19 -080086 }
87
Per Åhgren3ab308f2018-02-21 08:46:03 +010088 DelayEstimate new_delay = estimated_delay;
89 new_delay.delay = new_delay_blocks;
90 return new_delay;
peah69221db2017-01-27 03:28:19 -080091}
92
93int RenderDelayControllerImpl::instance_count_ = 0;
94
peah4fed3c02017-08-30 06:58:44 -070095RenderDelayControllerImpl::RenderDelayControllerImpl(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020096 const EchoCanceller3Config& config,
Per Åhgren6a05bb12019-12-03 11:24:59 +010097 int sample_rate_hz,
98 size_t num_capture_channels)
peah69221db2017-01-27 03:28:19 -080099 : data_dumper_(
100 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Gustaf Ullberg9249fbf2019-03-14 11:24:54 +0100101 hysteresis_limit_blocks_(
102 static_cast<int>(config.delay.hysteresis_limit_blocks)),
103 delay_headroom_samples_(config.delay.delay_headroom_samples),
Per Åhgren6a05bb12019-12-03 11:24:59 +0100104 delay_estimator_(data_dumper_.get(), config, num_capture_channels),
Per Åhgren8b7d2062018-10-30 23:44:40 +0100105 last_delay_estimate_quality_(DelayEstimate::Quality::kCoarse) {
peah21920892017-02-08 05:08:56 -0800106 RTC_DCHECK(ValidFullBandRate(sample_rate_hz));
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100107 delay_estimator_.LogDelayEstimationProperties(sample_rate_hz, 0);
peah69221db2017-01-27 03:28:19 -0800108}
109
110RenderDelayControllerImpl::~RenderDelayControllerImpl() = default;
111
Per Åhgren8b7d2062018-10-30 23:44:40 +0100112void RenderDelayControllerImpl::Reset(bool reset_delay_confidence) {
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200113 delay_ = absl::nullopt;
114 delay_samples_ = absl::nullopt;
Per Åhgren8b7d2062018-10-30 23:44:40 +0100115 delay_estimator_.Reset(reset_delay_confidence);
Per Åhgren47127762018-02-13 12:59:33 +0100116 delay_change_counter_ = 0;
Per Åhgren8b7d2062018-10-30 23:44:40 +0100117 if (reset_delay_confidence) {
118 last_delay_estimate_quality_ = DelayEstimate::Quality::kCoarse;
119 }
Per Åhgren47127762018-02-13 12:59:33 +0100120}
121
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100122void RenderDelayControllerImpl::LogRenderCall() {}
peahcf02cf12017-04-05 14:18:07 -0700123
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200124absl::optional<DelayEstimate> RenderDelayControllerImpl::GetDelay(
peahcf02cf12017-04-05 14:18:07 -0700125 const DownsampledRenderBuffer& render_buffer,
Per Åhgren5c532d32018-03-22 00:29:25 +0100126 size_t render_delay_buffer_delay,
Gustaf Ullbergee84d392019-09-10 09:36:43 +0200127 const std::vector<std::vector<float>>& capture) {
128 RTC_DCHECK_EQ(kBlockSize, capture[0].size());
Per Åhgrend8243fa2018-02-22 17:51:39 +0100129 ++capture_call_counter_;
Per Åhgren930021d2017-09-15 07:32:53 +0200130
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100131 auto delay_samples = delay_estimator_.EstimateDelay(render_buffer, capture);
Per Åhgrenc59a5762017-12-11 21:34:19 +0100132
Per Åhgrenc59a5762017-12-11 21:34:19 +0100133 if (delay_samples) {
Per Åhgren47127762018-02-13 12:59:33 +0100134 if (!delay_samples_ || delay_samples->delay != delay_samples_->delay) {
135 delay_change_counter_ = 0;
136 }
Per Åhgren3ab308f2018-02-21 08:46:03 +0100137 if (delay_samples_) {
138 delay_samples_->blocks_since_last_change =
139 delay_samples_->delay == delay_samples->delay
140 ? delay_samples_->blocks_since_last_change + 1
141 : 0;
142 delay_samples_->blocks_since_last_update = 0;
143 delay_samples_->delay = delay_samples->delay;
144 delay_samples_->quality = delay_samples->quality;
145 } else {
146 delay_samples_ = delay_samples;
147 }
148 } else {
149 if (delay_samples_) {
150 ++delay_samples_->blocks_since_last_change;
151 ++delay_samples_->blocks_since_last_update;
152 }
Per Åhgren930021d2017-09-15 07:32:53 +0200153 }
peahe985b3f2017-02-28 22:08:53 -0800154
Per Åhgren47127762018-02-13 12:59:33 +0100155 if (delay_change_counter_ < 2 * kNumBlocksPerSecond) {
156 ++delay_change_counter_;
Per Åhgrend8243fa2018-02-22 17:51:39 +0100157 }
158
Per Åhgren47127762018-02-13 12:59:33 +0100159 if (delay_samples_) {
160 // Compute the render delay buffer delay.
Per Åhgren8b7d2062018-10-30 23:44:40 +0100161 const bool use_hysteresis =
162 last_delay_estimate_quality_ == DelayEstimate::Quality::kRefined &&
163 delay_samples_->quality == DelayEstimate::Quality::kRefined;
Gustaf Ullberg9249fbf2019-03-14 11:24:54 +0100164 delay_ = ComputeBufferDelay(delay_,
165 use_hysteresis ? hysteresis_limit_blocks_ : 0,
166 delay_headroom_samples_, *delay_samples_);
Per Åhgren8b7d2062018-10-30 23:44:40 +0100167 last_delay_estimate_quality_ = delay_samples_->quality;
Per Åhgren47127762018-02-13 12:59:33 +0100168 }
169
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200170 metrics_.Update(delay_samples_ ? absl::optional<size_t>(delay_samples_->delay)
171 : absl::nullopt,
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100172 delay_ ? delay_->delay : 0, 0, delay_estimator_.Clockdrift());
Per Åhgren47127762018-02-13 12:59:33 +0100173
Per Åhgrenc59a5762017-12-11 21:34:19 +0100174 data_dumper_->DumpRaw("aec3_render_delay_controller_delay",
Per Åhgrena76ef9d2018-01-25 07:01:34 +0100175 delay_samples ? delay_samples->delay : 0);
Per Åhgrenc59a5762017-12-11 21:34:19 +0100176 data_dumper_->DumpRaw("aec3_render_delay_controller_buffer_delay",
Per Åhgrena76ef9d2018-01-25 07:01:34 +0100177 delay_ ? delay_->delay : 0);
peah69221db2017-01-27 03:28:19 -0800178
179 return delay_;
180}
181
Gustaf Ullberg777cf262018-11-22 16:02:34 +0100182bool RenderDelayControllerImpl::HasClockdrift() const {
183 return delay_estimator_.Clockdrift() != ClockdriftDetector::Level::kNone;
184}
185
peah69221db2017-01-27 03:28:19 -0800186} // namespace
187
peah4fed3c02017-08-30 06:58:44 -0700188RenderDelayController* RenderDelayController::Create(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200189 const EchoCanceller3Config& config,
Per Åhgren6a05bb12019-12-03 11:24:59 +0100190 int sample_rate_hz,
191 size_t num_capture_channels) {
192 return new RenderDelayControllerImpl(config, sample_rate_hz,
193 num_capture_channels);
peah69221db2017-01-27 03:28:19 -0800194}
195
196} // namespace webrtc