blob: c79c94b59e5bb83dde1c09517b09e83ac1b9ac2e [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"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "rtc_base/constructor_magic.h"
peah69221db2017-01-27 03:28:19 -080029
30namespace webrtc {
31
32namespace {
33
peah69221db2017-01-27 03:28:19 -080034class RenderDelayControllerImpl final : public RenderDelayController {
35 public:
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020036 RenderDelayControllerImpl(const EchoCanceller3Config& config,
37 int sample_rate_hz);
peah69221db2017-01-27 03:28:19 -080038 ~RenderDelayControllerImpl() override;
Per Åhgren8b7d2062018-10-30 23:44:40 +010039 void Reset(bool reset_delay_confidence) override;
Per Åhgren47127762018-02-13 12:59:33 +010040 void LogRenderCall() override;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020041 absl::optional<DelayEstimate> GetDelay(
Per Åhgrena76ef9d2018-01-25 07:01:34 +010042 const DownsampledRenderBuffer& render_buffer,
Per Åhgren5c532d32018-03-22 00:29:25 +010043 size_t render_delay_buffer_delay,
Gustaf Ullbergee84d392019-09-10 09:36:43 +020044 const std::vector<std::vector<float>>& capture) override;
Gustaf Ullberg777cf262018-11-22 16:02:34 +010045 bool HasClockdrift() const override;
peah69221db2017-01-27 03:28:19 -080046
47 private:
48 static int instance_count_;
49 std::unique_ptr<ApmDataDumper> data_dumper_;
Gustaf Ullberg9249fbf2019-03-14 11:24:54 +010050 const int hysteresis_limit_blocks_;
51 const int delay_headroom_samples_;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020052 absl::optional<DelayEstimate> delay_;
Per Åhgren8ba58612017-12-01 23:01:44 +010053 EchoPathDelayEstimator delay_estimator_;
peahe985b3f2017-02-28 22:08:53 -080054 RenderDelayControllerMetrics metrics_;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020055 absl::optional<DelayEstimate> delay_samples_;
Per Åhgrend8243fa2018-02-22 17:51:39 +010056 size_t capture_call_counter_ = 0;
Per Åhgren47127762018-02-13 12:59:33 +010057 int delay_change_counter_ = 0;
Per Åhgren8b7d2062018-10-30 23:44:40 +010058 DelayEstimate::Quality last_delay_estimate_quality_;
peah69221db2017-01-27 03:28:19 -080059 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RenderDelayControllerImpl);
60};
61
Per Åhgren47127762018-02-13 12:59:33 +010062DelayEstimate ComputeBufferDelay(
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020063 const absl::optional<DelayEstimate>& current_delay,
Gustaf Ullberg9249fbf2019-03-14 11:24:54 +010064 int hysteresis_limit_blocks,
65 int delay_headroom_samples,
Per Åhgrena76ef9d2018-01-25 07:01:34 +010066 DelayEstimate estimated_delay) {
Gustaf Ullberg9249fbf2019-03-14 11:24:54 +010067 // Subtract delay headroom.
68 const int delay_with_headroom_samples = std::max(
69 static_cast<int>(estimated_delay.delay) - delay_headroom_samples, 0);
peah69221db2017-01-27 03:28:19 -080070
71 // Compute the buffer delay increase required to achieve the desired latency.
Gustaf Ullberg9249fbf2019-03-14 11:24:54 +010072 size_t new_delay_blocks = delay_with_headroom_samples >> kBlockSizeLog2;
Per Åhgrena76ef9d2018-01-25 07:01:34 +010073
peah69221db2017-01-27 03:28:19 -080074 // Add hysteresis.
Per Åhgrenc59a5762017-12-11 21:34:19 +010075 if (current_delay) {
Per Åhgrena76ef9d2018-01-25 07:01:34 +010076 size_t current_delay_blocks = current_delay->delay;
Gustaf Ullberg9249fbf2019-03-14 11:24:54 +010077 if (new_delay_blocks > current_delay_blocks &&
78 new_delay_blocks <= current_delay_blocks + hysteresis_limit_blocks) {
79 new_delay_blocks = current_delay_blocks;
Per Åhgrenc59a5762017-12-11 21:34:19 +010080 }
peah69221db2017-01-27 03:28:19 -080081 }
82
Per Åhgren3ab308f2018-02-21 08:46:03 +010083 DelayEstimate new_delay = estimated_delay;
84 new_delay.delay = new_delay_blocks;
85 return new_delay;
peah69221db2017-01-27 03:28:19 -080086}
87
88int RenderDelayControllerImpl::instance_count_ = 0;
89
peah4fed3c02017-08-30 06:58:44 -070090RenderDelayControllerImpl::RenderDelayControllerImpl(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020091 const EchoCanceller3Config& config,
peah4fed3c02017-08-30 06:58:44 -070092 int sample_rate_hz)
peah69221db2017-01-27 03:28:19 -080093 : data_dumper_(
94 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Gustaf Ullberg9249fbf2019-03-14 11:24:54 +010095 hysteresis_limit_blocks_(
96 static_cast<int>(config.delay.hysteresis_limit_blocks)),
97 delay_headroom_samples_(config.delay.delay_headroom_samples),
Per Åhgren8ba58612017-12-01 23:01:44 +010098 delay_estimator_(data_dumper_.get(), config),
Per Åhgren8b7d2062018-10-30 23:44:40 +010099 last_delay_estimate_quality_(DelayEstimate::Quality::kCoarse) {
peah21920892017-02-08 05:08:56 -0800100 RTC_DCHECK(ValidFullBandRate(sample_rate_hz));
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100101 delay_estimator_.LogDelayEstimationProperties(sample_rate_hz, 0);
peah69221db2017-01-27 03:28:19 -0800102}
103
104RenderDelayControllerImpl::~RenderDelayControllerImpl() = default;
105
Per Åhgren8b7d2062018-10-30 23:44:40 +0100106void RenderDelayControllerImpl::Reset(bool reset_delay_confidence) {
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200107 delay_ = absl::nullopt;
108 delay_samples_ = absl::nullopt;
Per Åhgren8b7d2062018-10-30 23:44:40 +0100109 delay_estimator_.Reset(reset_delay_confidence);
Per Åhgren47127762018-02-13 12:59:33 +0100110 delay_change_counter_ = 0;
Per Åhgren8b7d2062018-10-30 23:44:40 +0100111 if (reset_delay_confidence) {
112 last_delay_estimate_quality_ = DelayEstimate::Quality::kCoarse;
113 }
Per Åhgren47127762018-02-13 12:59:33 +0100114}
115
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100116void RenderDelayControllerImpl::LogRenderCall() {}
peahcf02cf12017-04-05 14:18:07 -0700117
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200118absl::optional<DelayEstimate> RenderDelayControllerImpl::GetDelay(
peahcf02cf12017-04-05 14:18:07 -0700119 const DownsampledRenderBuffer& render_buffer,
Per Åhgren5c532d32018-03-22 00:29:25 +0100120 size_t render_delay_buffer_delay,
Gustaf Ullbergee84d392019-09-10 09:36:43 +0200121 const std::vector<std::vector<float>>& capture) {
122 RTC_DCHECK_EQ(kBlockSize, capture[0].size());
Per Åhgrend8243fa2018-02-22 17:51:39 +0100123 ++capture_call_counter_;
Per Åhgren930021d2017-09-15 07:32:53 +0200124
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100125 auto delay_samples = delay_estimator_.EstimateDelay(render_buffer, capture);
Per Åhgrenc59a5762017-12-11 21:34:19 +0100126
Per Åhgrenc59a5762017-12-11 21:34:19 +0100127 if (delay_samples) {
Per Åhgren47127762018-02-13 12:59:33 +0100128 if (!delay_samples_ || delay_samples->delay != delay_samples_->delay) {
129 delay_change_counter_ = 0;
130 }
Per Åhgren3ab308f2018-02-21 08:46:03 +0100131 if (delay_samples_) {
132 delay_samples_->blocks_since_last_change =
133 delay_samples_->delay == delay_samples->delay
134 ? delay_samples_->blocks_since_last_change + 1
135 : 0;
136 delay_samples_->blocks_since_last_update = 0;
137 delay_samples_->delay = delay_samples->delay;
138 delay_samples_->quality = delay_samples->quality;
139 } else {
140 delay_samples_ = delay_samples;
141 }
142 } else {
143 if (delay_samples_) {
144 ++delay_samples_->blocks_since_last_change;
145 ++delay_samples_->blocks_since_last_update;
146 }
Per Åhgren930021d2017-09-15 07:32:53 +0200147 }
peahe985b3f2017-02-28 22:08:53 -0800148
Per Åhgren47127762018-02-13 12:59:33 +0100149 if (delay_change_counter_ < 2 * kNumBlocksPerSecond) {
150 ++delay_change_counter_;
Per Åhgrend8243fa2018-02-22 17:51:39 +0100151 }
152
Per Åhgren47127762018-02-13 12:59:33 +0100153 if (delay_samples_) {
154 // Compute the render delay buffer delay.
Per Åhgren8b7d2062018-10-30 23:44:40 +0100155 const bool use_hysteresis =
156 last_delay_estimate_quality_ == DelayEstimate::Quality::kRefined &&
157 delay_samples_->quality == DelayEstimate::Quality::kRefined;
Gustaf Ullberg9249fbf2019-03-14 11:24:54 +0100158 delay_ = ComputeBufferDelay(delay_,
159 use_hysteresis ? hysteresis_limit_blocks_ : 0,
160 delay_headroom_samples_, *delay_samples_);
Per Åhgren8b7d2062018-10-30 23:44:40 +0100161 last_delay_estimate_quality_ = delay_samples_->quality;
Per Åhgren47127762018-02-13 12:59:33 +0100162 }
163
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200164 metrics_.Update(delay_samples_ ? absl::optional<size_t>(delay_samples_->delay)
165 : absl::nullopt,
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100166 delay_ ? delay_->delay : 0, 0, delay_estimator_.Clockdrift());
Per Åhgren47127762018-02-13 12:59:33 +0100167
Per Åhgrenc59a5762017-12-11 21:34:19 +0100168 data_dumper_->DumpRaw("aec3_render_delay_controller_delay",
Per Åhgrena76ef9d2018-01-25 07:01:34 +0100169 delay_samples ? delay_samples->delay : 0);
Per Åhgrenc59a5762017-12-11 21:34:19 +0100170 data_dumper_->DumpRaw("aec3_render_delay_controller_buffer_delay",
Per Åhgrena76ef9d2018-01-25 07:01:34 +0100171 delay_ ? delay_->delay : 0);
peah69221db2017-01-27 03:28:19 -0800172
173 return delay_;
174}
175
Gustaf Ullberg777cf262018-11-22 16:02:34 +0100176bool RenderDelayControllerImpl::HasClockdrift() const {
177 return delay_estimator_.Clockdrift() != ClockdriftDetector::Level::kNone;
178}
179
peah69221db2017-01-27 03:28:19 -0800180} // namespace
181
peah4fed3c02017-08-30 06:58:44 -0700182RenderDelayController* RenderDelayController::Create(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200183 const EchoCanceller3Config& config,
peah4fed3c02017-08-30 06:58:44 -0700184 int sample_rate_hz) {
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100185 return new RenderDelayControllerImpl(config, sample_rate_hz);
peah69221db2017-01-27 03:28:19 -0800186}
187
188} // namespace webrtc