peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 1 | /* |
Gustaf Ullberg | e47433f | 2019-01-24 16:00:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 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 | */ |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 10 | #include "modules/audio_processing/aec3/render_delay_controller.h" |
| 11 | |
Gustaf Ullberg | e47433f | 2019-01-24 16:00:57 +0100 | [diff] [blame] | 12 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 13 | |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 14 | #include <algorithm> |
| 15 | #include <memory> |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 16 | |
Gustaf Ullberg | e47433f | 2019-01-24 16:00:57 +0100 | [diff] [blame] | 17 | #include "absl/types/optional.h" |
| 18 | #include "api/array_view.h" |
Gustaf Ullberg | 3646f97 | 2018-02-14 15:19:04 +0100 | [diff] [blame] | 19 | #include "api/audio/echo_canceller3_config.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "modules/audio_processing/aec3/aec3_common.h" |
Gustaf Ullberg | e47433f | 2019-01-24 16:00:57 +0100 | [diff] [blame] | 21 | #include "modules/audio_processing/aec3/delay_estimate.h" |
| 22 | #include "modules/audio_processing/aec3/downsampled_render_buffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "modules/audio_processing/aec3/echo_path_delay_estimator.h" |
| 24 | #include "modules/audio_processing/aec3/render_delay_controller_metrics.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 25 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 26 | #include "rtc_base/atomic_ops.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 27 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 28 | #include "rtc_base/constructor_magic.h" |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 29 | |
| 30 | namespace webrtc { |
| 31 | |
| 32 | namespace { |
| 33 | |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 34 | class RenderDelayControllerImpl final : public RenderDelayController { |
| 35 | public: |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 36 | RenderDelayControllerImpl(const EchoCanceller3Config& config, |
Per Åhgren | 6a05bb1 | 2019-12-03 11:24:59 +0100 | [diff] [blame^] | 37 | int sample_rate_hz, |
| 38 | size_t num_capture_channels); |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 39 | ~RenderDelayControllerImpl() override; |
Per Åhgren | 8b7d206 | 2018-10-30 23:44:40 +0100 | [diff] [blame] | 40 | void Reset(bool reset_delay_confidence) override; |
Per Åhgren | 4712776 | 2018-02-13 12:59:33 +0100 | [diff] [blame] | 41 | void LogRenderCall() override; |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 42 | absl::optional<DelayEstimate> GetDelay( |
Per Åhgren | a76ef9d | 2018-01-25 07:01:34 +0100 | [diff] [blame] | 43 | const DownsampledRenderBuffer& render_buffer, |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 44 | size_t render_delay_buffer_delay, |
Gustaf Ullberg | ee84d39 | 2019-09-10 09:36:43 +0200 | [diff] [blame] | 45 | const std::vector<std::vector<float>>& capture) override; |
Gustaf Ullberg | 777cf26 | 2018-11-22 16:02:34 +0100 | [diff] [blame] | 46 | bool HasClockdrift() const override; |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 47 | |
| 48 | private: |
| 49 | static int instance_count_; |
| 50 | std::unique_ptr<ApmDataDumper> data_dumper_; |
Gustaf Ullberg | 9249fbf | 2019-03-14 11:24:54 +0100 | [diff] [blame] | 51 | const int hysteresis_limit_blocks_; |
| 52 | const int delay_headroom_samples_; |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 53 | absl::optional<DelayEstimate> delay_; |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 54 | EchoPathDelayEstimator delay_estimator_; |
peah | e985b3f | 2017-02-28 22:08:53 -0800 | [diff] [blame] | 55 | RenderDelayControllerMetrics metrics_; |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 56 | absl::optional<DelayEstimate> delay_samples_; |
Per Åhgren | d8243fa | 2018-02-22 17:51:39 +0100 | [diff] [blame] | 57 | size_t capture_call_counter_ = 0; |
Per Åhgren | 4712776 | 2018-02-13 12:59:33 +0100 | [diff] [blame] | 58 | int delay_change_counter_ = 0; |
Per Åhgren | 8b7d206 | 2018-10-30 23:44:40 +0100 | [diff] [blame] | 59 | DelayEstimate::Quality last_delay_estimate_quality_; |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 60 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RenderDelayControllerImpl); |
| 61 | }; |
| 62 | |
Per Åhgren | 4712776 | 2018-02-13 12:59:33 +0100 | [diff] [blame] | 63 | DelayEstimate ComputeBufferDelay( |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 64 | const absl::optional<DelayEstimate>& current_delay, |
Gustaf Ullberg | 9249fbf | 2019-03-14 11:24:54 +0100 | [diff] [blame] | 65 | int hysteresis_limit_blocks, |
| 66 | int delay_headroom_samples, |
Per Åhgren | a76ef9d | 2018-01-25 07:01:34 +0100 | [diff] [blame] | 67 | DelayEstimate estimated_delay) { |
Gustaf Ullberg | 9249fbf | 2019-03-14 11:24:54 +0100 | [diff] [blame] | 68 | // Subtract delay headroom. |
| 69 | const int delay_with_headroom_samples = std::max( |
| 70 | static_cast<int>(estimated_delay.delay) - delay_headroom_samples, 0); |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 71 | |
| 72 | // Compute the buffer delay increase required to achieve the desired latency. |
Gustaf Ullberg | 9249fbf | 2019-03-14 11:24:54 +0100 | [diff] [blame] | 73 | size_t new_delay_blocks = delay_with_headroom_samples >> kBlockSizeLog2; |
Per Åhgren | a76ef9d | 2018-01-25 07:01:34 +0100 | [diff] [blame] | 74 | |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 75 | // Add hysteresis. |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 76 | if (current_delay) { |
Per Åhgren | a76ef9d | 2018-01-25 07:01:34 +0100 | [diff] [blame] | 77 | size_t current_delay_blocks = current_delay->delay; |
Gustaf Ullberg | 9249fbf | 2019-03-14 11:24:54 +0100 | [diff] [blame] | 78 | if (new_delay_blocks > current_delay_blocks && |
| 79 | new_delay_blocks <= current_delay_blocks + hysteresis_limit_blocks) { |
| 80 | new_delay_blocks = current_delay_blocks; |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 81 | } |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Per Åhgren | 3ab308f | 2018-02-21 08:46:03 +0100 | [diff] [blame] | 84 | DelayEstimate new_delay = estimated_delay; |
| 85 | new_delay.delay = new_delay_blocks; |
| 86 | return new_delay; |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | int RenderDelayControllerImpl::instance_count_ = 0; |
| 90 | |
peah | 4fed3c0 | 2017-08-30 06:58:44 -0700 | [diff] [blame] | 91 | RenderDelayControllerImpl::RenderDelayControllerImpl( |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 92 | const EchoCanceller3Config& config, |
Per Åhgren | 6a05bb1 | 2019-12-03 11:24:59 +0100 | [diff] [blame^] | 93 | int sample_rate_hz, |
| 94 | size_t num_capture_channels) |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 95 | : data_dumper_( |
| 96 | new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))), |
Gustaf Ullberg | 9249fbf | 2019-03-14 11:24:54 +0100 | [diff] [blame] | 97 | hysteresis_limit_blocks_( |
| 98 | static_cast<int>(config.delay.hysteresis_limit_blocks)), |
| 99 | delay_headroom_samples_(config.delay.delay_headroom_samples), |
Per Åhgren | 6a05bb1 | 2019-12-03 11:24:59 +0100 | [diff] [blame^] | 100 | delay_estimator_(data_dumper_.get(), config, num_capture_channels), |
Per Åhgren | 8b7d206 | 2018-10-30 23:44:40 +0100 | [diff] [blame] | 101 | last_delay_estimate_quality_(DelayEstimate::Quality::kCoarse) { |
peah | 2192089 | 2017-02-08 05:08:56 -0800 | [diff] [blame] | 102 | RTC_DCHECK(ValidFullBandRate(sample_rate_hz)); |
Gustaf Ullberg | e47433f | 2019-01-24 16:00:57 +0100 | [diff] [blame] | 103 | delay_estimator_.LogDelayEstimationProperties(sample_rate_hz, 0); |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | RenderDelayControllerImpl::~RenderDelayControllerImpl() = default; |
| 107 | |
Per Åhgren | 8b7d206 | 2018-10-30 23:44:40 +0100 | [diff] [blame] | 108 | void RenderDelayControllerImpl::Reset(bool reset_delay_confidence) { |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 109 | delay_ = absl::nullopt; |
| 110 | delay_samples_ = absl::nullopt; |
Per Åhgren | 8b7d206 | 2018-10-30 23:44:40 +0100 | [diff] [blame] | 111 | delay_estimator_.Reset(reset_delay_confidence); |
Per Åhgren | 4712776 | 2018-02-13 12:59:33 +0100 | [diff] [blame] | 112 | delay_change_counter_ = 0; |
Per Åhgren | 8b7d206 | 2018-10-30 23:44:40 +0100 | [diff] [blame] | 113 | if (reset_delay_confidence) { |
| 114 | last_delay_estimate_quality_ = DelayEstimate::Quality::kCoarse; |
| 115 | } |
Per Åhgren | 4712776 | 2018-02-13 12:59:33 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Gustaf Ullberg | e47433f | 2019-01-24 16:00:57 +0100 | [diff] [blame] | 118 | void RenderDelayControllerImpl::LogRenderCall() {} |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 119 | |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 120 | absl::optional<DelayEstimate> RenderDelayControllerImpl::GetDelay( |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 121 | const DownsampledRenderBuffer& render_buffer, |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 122 | size_t render_delay_buffer_delay, |
Gustaf Ullberg | ee84d39 | 2019-09-10 09:36:43 +0200 | [diff] [blame] | 123 | const std::vector<std::vector<float>>& capture) { |
| 124 | RTC_DCHECK_EQ(kBlockSize, capture[0].size()); |
Per Åhgren | d8243fa | 2018-02-22 17:51:39 +0100 | [diff] [blame] | 125 | ++capture_call_counter_; |
Per Åhgren | 930021d | 2017-09-15 07:32:53 +0200 | [diff] [blame] | 126 | |
Gustaf Ullberg | e47433f | 2019-01-24 16:00:57 +0100 | [diff] [blame] | 127 | auto delay_samples = delay_estimator_.EstimateDelay(render_buffer, capture); |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 128 | |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 129 | if (delay_samples) { |
Per Åhgren | 4712776 | 2018-02-13 12:59:33 +0100 | [diff] [blame] | 130 | if (!delay_samples_ || delay_samples->delay != delay_samples_->delay) { |
| 131 | delay_change_counter_ = 0; |
| 132 | } |
Per Åhgren | 3ab308f | 2018-02-21 08:46:03 +0100 | [diff] [blame] | 133 | if (delay_samples_) { |
| 134 | delay_samples_->blocks_since_last_change = |
| 135 | delay_samples_->delay == delay_samples->delay |
| 136 | ? delay_samples_->blocks_since_last_change + 1 |
| 137 | : 0; |
| 138 | delay_samples_->blocks_since_last_update = 0; |
| 139 | delay_samples_->delay = delay_samples->delay; |
| 140 | delay_samples_->quality = delay_samples->quality; |
| 141 | } else { |
| 142 | delay_samples_ = delay_samples; |
| 143 | } |
| 144 | } else { |
| 145 | if (delay_samples_) { |
| 146 | ++delay_samples_->blocks_since_last_change; |
| 147 | ++delay_samples_->blocks_since_last_update; |
| 148 | } |
Per Åhgren | 930021d | 2017-09-15 07:32:53 +0200 | [diff] [blame] | 149 | } |
peah | e985b3f | 2017-02-28 22:08:53 -0800 | [diff] [blame] | 150 | |
Per Åhgren | 4712776 | 2018-02-13 12:59:33 +0100 | [diff] [blame] | 151 | if (delay_change_counter_ < 2 * kNumBlocksPerSecond) { |
| 152 | ++delay_change_counter_; |
Per Åhgren | d8243fa | 2018-02-22 17:51:39 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Per Åhgren | 4712776 | 2018-02-13 12:59:33 +0100 | [diff] [blame] | 155 | if (delay_samples_) { |
| 156 | // Compute the render delay buffer delay. |
Per Åhgren | 8b7d206 | 2018-10-30 23:44:40 +0100 | [diff] [blame] | 157 | const bool use_hysteresis = |
| 158 | last_delay_estimate_quality_ == DelayEstimate::Quality::kRefined && |
| 159 | delay_samples_->quality == DelayEstimate::Quality::kRefined; |
Gustaf Ullberg | 9249fbf | 2019-03-14 11:24:54 +0100 | [diff] [blame] | 160 | delay_ = ComputeBufferDelay(delay_, |
| 161 | use_hysteresis ? hysteresis_limit_blocks_ : 0, |
| 162 | delay_headroom_samples_, *delay_samples_); |
Per Åhgren | 8b7d206 | 2018-10-30 23:44:40 +0100 | [diff] [blame] | 163 | last_delay_estimate_quality_ = delay_samples_->quality; |
Per Åhgren | 4712776 | 2018-02-13 12:59:33 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 166 | metrics_.Update(delay_samples_ ? absl::optional<size_t>(delay_samples_->delay) |
| 167 | : absl::nullopt, |
Gustaf Ullberg | e47433f | 2019-01-24 16:00:57 +0100 | [diff] [blame] | 168 | delay_ ? delay_->delay : 0, 0, delay_estimator_.Clockdrift()); |
Per Åhgren | 4712776 | 2018-02-13 12:59:33 +0100 | [diff] [blame] | 169 | |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 170 | data_dumper_->DumpRaw("aec3_render_delay_controller_delay", |
Per Åhgren | a76ef9d | 2018-01-25 07:01:34 +0100 | [diff] [blame] | 171 | delay_samples ? delay_samples->delay : 0); |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 172 | data_dumper_->DumpRaw("aec3_render_delay_controller_buffer_delay", |
Per Åhgren | a76ef9d | 2018-01-25 07:01:34 +0100 | [diff] [blame] | 173 | delay_ ? delay_->delay : 0); |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 174 | |
| 175 | return delay_; |
| 176 | } |
| 177 | |
Gustaf Ullberg | 777cf26 | 2018-11-22 16:02:34 +0100 | [diff] [blame] | 178 | bool RenderDelayControllerImpl::HasClockdrift() const { |
| 179 | return delay_estimator_.Clockdrift() != ClockdriftDetector::Level::kNone; |
| 180 | } |
| 181 | |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 182 | } // namespace |
| 183 | |
peah | 4fed3c0 | 2017-08-30 06:58:44 -0700 | [diff] [blame] | 184 | RenderDelayController* RenderDelayController::Create( |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 185 | const EchoCanceller3Config& config, |
Per Åhgren | 6a05bb1 | 2019-12-03 11:24:59 +0100 | [diff] [blame^] | 186 | int sample_rate_hz, |
| 187 | size_t num_capture_channels) { |
| 188 | return new RenderDelayControllerImpl(config, sample_rate_hz, |
| 189 | num_capture_channels); |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | } // namespace webrtc |