xref: /aosp_15_r20/external/webrtc/modules/audio_processing/aec3/render_delay_controller.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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  */
10 #include "modules/audio_processing/aec3/render_delay_controller.h"
11 
12 #include <stddef.h>
13 
14 #include <algorithm>
15 #include <atomic>
16 #include <memory>
17 
18 #include "absl/types/optional.h"
19 #include "api/array_view.h"
20 #include "api/audio/echo_canceller3_config.h"
21 #include "modules/audio_processing/aec3/aec3_common.h"
22 #include "modules/audio_processing/aec3/delay_estimate.h"
23 #include "modules/audio_processing/aec3/downsampled_render_buffer.h"
24 #include "modules/audio_processing/aec3/echo_path_delay_estimator.h"
25 #include "modules/audio_processing/aec3/render_delay_controller_metrics.h"
26 #include "modules/audio_processing/logging/apm_data_dumper.h"
27 #include "rtc_base/checks.h"
28 
29 namespace webrtc {
30 
31 namespace {
32 
33 class RenderDelayControllerImpl final : public RenderDelayController {
34  public:
35   RenderDelayControllerImpl(const EchoCanceller3Config& config,
36                             int sample_rate_hz,
37                             size_t num_capture_channels);
38 
39   RenderDelayControllerImpl() = delete;
40   RenderDelayControllerImpl(const RenderDelayControllerImpl&) = delete;
41   RenderDelayControllerImpl& operator=(const RenderDelayControllerImpl&) =
42       delete;
43 
44   ~RenderDelayControllerImpl() override;
45   void Reset(bool reset_delay_confidence) override;
46   void LogRenderCall() override;
47   absl::optional<DelayEstimate> GetDelay(
48       const DownsampledRenderBuffer& render_buffer,
49       size_t render_delay_buffer_delay,
50       const Block& capture) override;
51   bool HasClockdrift() const override;
52 
53  private:
54   static std::atomic<int> instance_count_;
55   std::unique_ptr<ApmDataDumper> data_dumper_;
56   const int hysteresis_limit_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;
63   DelayEstimate::Quality last_delay_estimate_quality_;
64 };
65 
ComputeBufferDelay(const absl::optional<DelayEstimate> & current_delay,int hysteresis_limit_blocks,DelayEstimate estimated_delay)66 DelayEstimate ComputeBufferDelay(
67     const absl::optional<DelayEstimate>& current_delay,
68     int hysteresis_limit_blocks,
69     DelayEstimate estimated_delay) {
70   // Compute the buffer delay increase required to achieve the desired latency.
71   size_t new_delay_blocks = estimated_delay.delay >> kBlockSizeLog2;
72   // Add hysteresis.
73   if (current_delay) {
74     size_t current_delay_blocks = current_delay->delay;
75     if (new_delay_blocks > current_delay_blocks &&
76         new_delay_blocks <= current_delay_blocks + hysteresis_limit_blocks) {
77       new_delay_blocks = current_delay_blocks;
78     }
79   }
80   DelayEstimate new_delay = estimated_delay;
81   new_delay.delay = new_delay_blocks;
82   return new_delay;
83 }
84 
85 std::atomic<int> RenderDelayControllerImpl::instance_count_(0);
86 
RenderDelayControllerImpl(const EchoCanceller3Config & config,int sample_rate_hz,size_t num_capture_channels)87 RenderDelayControllerImpl::RenderDelayControllerImpl(
88     const EchoCanceller3Config& config,
89     int sample_rate_hz,
90     size_t num_capture_channels)
91     : data_dumper_(new ApmDataDumper(instance_count_.fetch_add(1) + 1)),
92       hysteresis_limit_blocks_(
93           static_cast<int>(config.delay.hysteresis_limit_blocks)),
94       delay_estimator_(data_dumper_.get(), config, num_capture_channels),
95       last_delay_estimate_quality_(DelayEstimate::Quality::kCoarse) {
96   RTC_DCHECK(ValidFullBandRate(sample_rate_hz));
97   delay_estimator_.LogDelayEstimationProperties(sample_rate_hz, 0);
98 }
99 
100 RenderDelayControllerImpl::~RenderDelayControllerImpl() = default;
101 
Reset(bool reset_delay_confidence)102 void RenderDelayControllerImpl::Reset(bool reset_delay_confidence) {
103   delay_ = absl::nullopt;
104   delay_samples_ = absl::nullopt;
105   delay_estimator_.Reset(reset_delay_confidence);
106   delay_change_counter_ = 0;
107   if (reset_delay_confidence) {
108     last_delay_estimate_quality_ = DelayEstimate::Quality::kCoarse;
109   }
110 }
111 
LogRenderCall()112 void RenderDelayControllerImpl::LogRenderCall() {}
113 
GetDelay(const DownsampledRenderBuffer & render_buffer,size_t render_delay_buffer_delay,const Block & capture)114 absl::optional<DelayEstimate> RenderDelayControllerImpl::GetDelay(
115     const DownsampledRenderBuffer& render_buffer,
116     size_t render_delay_buffer_delay,
117     const Block& capture) {
118   ++capture_call_counter_;
119 
120   auto delay_samples = delay_estimator_.EstimateDelay(render_buffer, capture);
121 
122   if (delay_samples) {
123     if (!delay_samples_ || delay_samples->delay != delay_samples_->delay) {
124       delay_change_counter_ = 0;
125     }
126     if (delay_samples_) {
127       delay_samples_->blocks_since_last_change =
128           delay_samples_->delay == delay_samples->delay
129               ? delay_samples_->blocks_since_last_change + 1
130               : 0;
131       delay_samples_->blocks_since_last_update = 0;
132       delay_samples_->delay = delay_samples->delay;
133       delay_samples_->quality = delay_samples->quality;
134     } else {
135       delay_samples_ = delay_samples;
136     }
137   } else {
138     if (delay_samples_) {
139       ++delay_samples_->blocks_since_last_change;
140       ++delay_samples_->blocks_since_last_update;
141     }
142   }
143 
144   if (delay_change_counter_ < 2 * kNumBlocksPerSecond) {
145     ++delay_change_counter_;
146   }
147 
148   if (delay_samples_) {
149     // Compute the render delay buffer delay.
150     const bool use_hysteresis =
151         last_delay_estimate_quality_ == DelayEstimate::Quality::kRefined &&
152         delay_samples_->quality == DelayEstimate::Quality::kRefined;
153     delay_ = ComputeBufferDelay(
154         delay_, use_hysteresis ? hysteresis_limit_blocks_ : 0, *delay_samples_);
155     last_delay_estimate_quality_ = delay_samples_->quality;
156   }
157 
158   metrics_.Update(
159       delay_samples_ ? absl::optional<size_t>(delay_samples_->delay)
160                      : absl::nullopt,
161       delay_ ? absl::optional<size_t>(delay_->delay) : absl::nullopt,
162       delay_estimator_.Clockdrift());
163 
164   data_dumper_->DumpRaw("aec3_render_delay_controller_delay",
165                         delay_samples ? delay_samples->delay : 0);
166   data_dumper_->DumpRaw("aec3_render_delay_controller_buffer_delay",
167                         delay_ ? delay_->delay : 0);
168 
169   return delay_;
170 }
171 
HasClockdrift() const172 bool RenderDelayControllerImpl::HasClockdrift() const {
173   return delay_estimator_.Clockdrift() != ClockdriftDetector::Level::kNone;
174 }
175 
176 }  // namespace
177 
Create(const EchoCanceller3Config & config,int sample_rate_hz,size_t num_capture_channels)178 RenderDelayController* RenderDelayController::Create(
179     const EchoCanceller3Config& config,
180     int sample_rate_hz,
181     size_t num_capture_channels) {
182   return new RenderDelayControllerImpl(config, sample_rate_hz,
183                                        num_capture_channels);
184 }
185 
186 }  // namespace webrtc
187