Gustaf Ullberg | 11539f0 | 2018-10-15 13:40:29 +0200 | [diff] [blame] | 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 | */ |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 10 | #include <stddef.h> |
Gustaf Ullberg | 11539f0 | 2018-10-15 13:40:29 +0200 | [diff] [blame] | 11 | #include <algorithm> |
| 12 | #include <memory> |
Gustaf Ullberg | 11539f0 | 2018-10-15 13:40:29 +0200 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include "absl/types/optional.h" |
| 15 | #include "api/array_view.h" |
Gustaf Ullberg | 11539f0 | 2018-10-15 13:40:29 +0200 | [diff] [blame] | 16 | #include "api/audio/echo_canceller3_config.h" |
| 17 | #include "modules/audio_processing/aec3/aec3_common.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 18 | #include "modules/audio_processing/aec3/delay_estimate.h" |
| 19 | #include "modules/audio_processing/aec3/downsampled_render_buffer.h" |
Gustaf Ullberg | 11539f0 | 2018-10-15 13:40:29 +0200 | [diff] [blame] | 20 | #include "modules/audio_processing/aec3/echo_path_delay_estimator.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 21 | #include "modules/audio_processing/aec3/render_delay_controller.h" |
Gustaf Ullberg | 11539f0 | 2018-10-15 13:40:29 +0200 | [diff] [blame] | 22 | #include "modules/audio_processing/aec3/render_delay_controller_metrics.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 23 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
Gustaf Ullberg | 11539f0 | 2018-10-15 13:40:29 +0200 | [diff] [blame] | 24 | #include "rtc_base/atomicops.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 25 | #include "rtc_base/checks.h" |
Gustaf Ullberg | 11539f0 | 2018-10-15 13:40:29 +0200 | [diff] [blame] | 26 | #include "rtc_base/constructormagic.h" |
Per Åhgren | 8b7d206 | 2018-10-30 23:44:40 +0100 | [diff] [blame^] | 27 | #include "system_wrappers/include/field_trial.h" |
Gustaf Ullberg | 11539f0 | 2018-10-15 13:40:29 +0200 | [diff] [blame] | 28 | |
| 29 | namespace webrtc { |
| 30 | |
| 31 | namespace { |
| 32 | |
Per Åhgren | 8b7d206 | 2018-10-30 23:44:40 +0100 | [diff] [blame^] | 33 | bool UseEarlyDelayDetection() { |
| 34 | return !field_trial::IsEnabled("WebRTC-Aec3EarlyDelayDetectionKillSwitch"); |
| 35 | } |
| 36 | |
Gustaf Ullberg | 11539f0 | 2018-10-15 13:40:29 +0200 | [diff] [blame] | 37 | class RenderDelayControllerImpl2 final : public RenderDelayController { |
| 38 | public: |
| 39 | RenderDelayControllerImpl2(const EchoCanceller3Config& config, |
| 40 | int sample_rate_hz); |
| 41 | ~RenderDelayControllerImpl2() override; |
Per Åhgren | 8b7d206 | 2018-10-30 23:44:40 +0100 | [diff] [blame^] | 42 | void Reset(bool reset_delay_confidence) override; |
Gustaf Ullberg | 11539f0 | 2018-10-15 13:40:29 +0200 | [diff] [blame] | 43 | 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 Åhgren | 8b7d206 | 2018-10-30 23:44:40 +0100 | [diff] [blame^] | 53 | const bool use_early_delay_detection_; |
Gustaf Ullberg | 11539f0 | 2018-10-15 13:40:29 +0200 | [diff] [blame] | 54 | 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 Åhgren | 8b7d206 | 2018-10-30 23:44:40 +0100 | [diff] [blame^] | 63 | DelayEstimate::Quality last_delay_estimate_quality_; |
Gustaf Ullberg | 11539f0 | 2018-10-15 13:40:29 +0200 | [diff] [blame] | 64 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RenderDelayControllerImpl2); |
| 65 | }; |
| 66 | |
| 67 | DelayEstimate 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 | |
| 103 | int RenderDelayControllerImpl2::instance_count_ = 0; |
| 104 | |
| 105 | RenderDelayControllerImpl2::RenderDelayControllerImpl2( |
| 106 | const EchoCanceller3Config& config, |
| 107 | int sample_rate_hz) |
| 108 | : data_dumper_( |
| 109 | new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))), |
Per Åhgren | 8b7d206 | 2018-10-30 23:44:40 +0100 | [diff] [blame^] | 110 | use_early_delay_detection_(UseEarlyDelayDetection()), |
Gustaf Ullberg | 11539f0 | 2018-10-15 13:40:29 +0200 | [diff] [blame] | 111 | 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 Åhgren | 8b7d206 | 2018-10-30 23:44:40 +0100 | [diff] [blame^] | 117 | delay_estimator_(data_dumper_.get(), config), |
| 118 | last_delay_estimate_quality_(DelayEstimate::Quality::kCoarse) { |
Gustaf Ullberg | 11539f0 | 2018-10-15 13:40:29 +0200 | [diff] [blame] | 119 | RTC_DCHECK(ValidFullBandRate(sample_rate_hz)); |
| 120 | delay_estimator_.LogDelayEstimationProperties(sample_rate_hz, 0); |
| 121 | } |
| 122 | |
| 123 | RenderDelayControllerImpl2::~RenderDelayControllerImpl2() = default; |
| 124 | |
Per Åhgren | 8b7d206 | 2018-10-30 23:44:40 +0100 | [diff] [blame^] | 125 | void RenderDelayControllerImpl2::Reset(bool reset_delay_confidence) { |
Gustaf Ullberg | 11539f0 | 2018-10-15 13:40:29 +0200 | [diff] [blame] | 126 | delay_ = absl::nullopt; |
| 127 | delay_samples_ = absl::nullopt; |
Per Åhgren | 8b7d206 | 2018-10-30 23:44:40 +0100 | [diff] [blame^] | 128 | delay_estimator_.Reset(reset_delay_confidence); |
Gustaf Ullberg | 11539f0 | 2018-10-15 13:40:29 +0200 | [diff] [blame] | 129 | delay_change_counter_ = 0; |
Per Åhgren | 8b7d206 | 2018-10-30 23:44:40 +0100 | [diff] [blame^] | 130 | if (reset_delay_confidence || true) { |
| 131 | last_delay_estimate_quality_ = DelayEstimate::Quality::kCoarse; |
| 132 | } |
Gustaf Ullberg | 11539f0 | 2018-10-15 13:40:29 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void RenderDelayControllerImpl2::LogRenderCall() {} |
| 136 | |
| 137 | absl::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 Ullberg | 11539f0 | 2018-10-15 13:40:29 +0200 | [diff] [blame] | 156 | 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 Åhgren | 8b7d206 | 2018-10-30 23:44:40 +0100 | [diff] [blame^] | 183 | const bool use_hysteresis = |
| 184 | last_delay_estimate_quality_ == DelayEstimate::Quality::kRefined && |
| 185 | delay_samples_->quality == DelayEstimate::Quality::kRefined; |
Gustaf Ullberg | 11539f0 | 2018-10-15 13:40:29 +0200 | [diff] [blame] | 186 | delay_ = ComputeBufferDelay(delay_, delay_headroom_blocks_, |
Per Åhgren | 8b7d206 | 2018-10-30 23:44:40 +0100 | [diff] [blame^] | 187 | 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 Ullberg | 11539f0 | 2018-10-15 13:40:29 +0200 | [diff] [blame] | 191 | } |
| 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 | |
| 207 | RenderDelayController* RenderDelayController::Create2( |
| 208 | const EchoCanceller3Config& config, |
| 209 | int sample_rate_hz) { |
| 210 | return new RenderDelayControllerImpl2(config, sample_rate_hz); |
| 211 | } |
| 212 | |
| 213 | } // namespace webrtc |