blob: 9b400ff11194eae525bc030219deb9f33e0ce843 [file] [log] [blame]
peah69221db2017-01-27 03:28:19 -08001/*
2 * Copyright (c) 2017 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 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020010#include "modules/audio_processing/aec3/render_delay_controller.h"
peah69221db2017-01-27 03:28:19 -080011
12#include <algorithm>
13#include <memory>
14#include <string>
15#include <vector>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/audio_processing/aec3/aec3_common.h"
18#include "modules/audio_processing/aec3/echo_path_delay_estimator.h"
19#include "modules/audio_processing/aec3/render_delay_controller_metrics.h"
20#include "modules/audio_processing/include/audio_processing.h"
21#include "rtc_base/atomicops.h"
22#include "rtc_base/constructormagic.h"
peah69221db2017-01-27 03:28:19 -080023
24namespace webrtc {
25
26namespace {
27
peah69221db2017-01-27 03:28:19 -080028class RenderDelayControllerImpl final : public RenderDelayController {
29 public:
peah4fed3c02017-08-30 06:58:44 -070030 RenderDelayControllerImpl(
31 const AudioProcessing::Config::EchoCanceller3& config,
32 int sample_rate_hz);
peah69221db2017-01-27 03:28:19 -080033 ~RenderDelayControllerImpl() override;
peahcf02cf12017-04-05 14:18:07 -070034 void Reset() override;
35 void SetDelay(size_t render_delay) override;
36 size_t GetDelay(const DownsampledRenderBuffer& render_buffer,
37 rtc::ArrayView<const float> capture) override;
peah69221db2017-01-27 03:28:19 -080038 rtc::Optional<size_t> AlignmentHeadroomSamples() const override {
39 return headroom_samples_;
40 }
41
42 private:
43 static int instance_count_;
44 std::unique_ptr<ApmDataDumper> data_dumper_;
peahc6b10412017-09-11 06:46:07 -070045 size_t delay_ = kMinEchoPathDelayBlocks;
peah69221db2017-01-27 03:28:19 -080046 EchoPathDelayEstimator delay_estimator_;
47 size_t blocks_since_last_delay_estimate_ = 300000;
peahc6b10412017-09-11 06:46:07 -070048 int echo_path_delay_samples_ = kMinEchoPathDelayBlocks * kBlockSize;
peah69221db2017-01-27 03:28:19 -080049 size_t align_call_counter_ = 0;
50 rtc::Optional<size_t> headroom_samples_;
Per Åhgren930021d2017-09-15 07:32:53 +020051 std::vector<float> capture_delay_buffer_;
52 int capture_delay_buffer_index_ = 0;
peahe985b3f2017-02-28 22:08:53 -080053 RenderDelayControllerMetrics metrics_;
peah69221db2017-01-27 03:28:19 -080054 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RenderDelayControllerImpl);
55};
56
57size_t ComputeNewBufferDelay(size_t current_delay,
peah69221db2017-01-27 03:28:19 -080058 size_t echo_path_delay_samples) {
59 // The below division is not exact and the truncation is intended.
60 const int echo_path_delay_blocks = echo_path_delay_samples / kBlockSize;
61 constexpr int kDelayHeadroomBlocks = 1;
62
63 // Compute the buffer delay increase required to achieve the desired latency.
64 size_t new_delay = std::max(echo_path_delay_blocks - kDelayHeadroomBlocks, 0);
65
66 // Add hysteresis.
peah96b951c2017-08-22 10:26:07 -070067 if (new_delay == current_delay + 1) {
peah69221db2017-01-27 03:28:19 -080068 new_delay = current_delay;
69 }
70
peah69221db2017-01-27 03:28:19 -080071 return new_delay;
72}
73
74int RenderDelayControllerImpl::instance_count_ = 0;
75
peah4fed3c02017-08-30 06:58:44 -070076RenderDelayControllerImpl::RenderDelayControllerImpl(
77 const AudioProcessing::Config::EchoCanceller3& config,
78 int sample_rate_hz)
peah69221db2017-01-27 03:28:19 -080079 : data_dumper_(
80 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Per Åhgren930021d2017-09-15 07:32:53 +020081 delay_estimator_(data_dumper_.get(), config),
82 capture_delay_buffer_(kBlockSize * (kMaxApiCallsJitterBlocks + 2), 0.f) {
peah21920892017-02-08 05:08:56 -080083 RTC_DCHECK(ValidFullBandRate(sample_rate_hz));
peah69221db2017-01-27 03:28:19 -080084}
85
86RenderDelayControllerImpl::~RenderDelayControllerImpl() = default;
87
peahcf02cf12017-04-05 14:18:07 -070088void RenderDelayControllerImpl::Reset() {
peah96b951c2017-08-22 10:26:07 -070089 delay_ = kMinEchoPathDelayBlocks;
peahcf02cf12017-04-05 14:18:07 -070090 blocks_since_last_delay_estimate_ = 300000;
peahc6b10412017-09-11 06:46:07 -070091 echo_path_delay_samples_ = delay_ * kBlockSize;
peahcf02cf12017-04-05 14:18:07 -070092 align_call_counter_ = 0;
93 headroom_samples_ = rtc::Optional<size_t>();
Per Åhgren930021d2017-09-15 07:32:53 +020094 std::fill(capture_delay_buffer_.begin(), capture_delay_buffer_.end(), 0.f);
peahcf02cf12017-04-05 14:18:07 -070095 delay_estimator_.Reset();
96}
97
98void RenderDelayControllerImpl::SetDelay(size_t render_delay) {
99 if (delay_ != render_delay) {
100 // If a the delay set does not match the actual delay, reset the delay
101 // controller.
102 Reset();
103 delay_ = render_delay;
104 }
105}
106
peah69221db2017-01-27 03:28:19 -0800107size_t RenderDelayControllerImpl::GetDelay(
peahcf02cf12017-04-05 14:18:07 -0700108 const DownsampledRenderBuffer& render_buffer,
peah69221db2017-01-27 03:28:19 -0800109 rtc::ArrayView<const float> capture) {
110 RTC_DCHECK_EQ(kBlockSize, capture.size());
peah69221db2017-01-27 03:28:19 -0800111
112 ++align_call_counter_;
Per Åhgren930021d2017-09-15 07:32:53 +0200113
114 // Estimate the delay with a delayed capture signal in order to catch
115 // noncausal delays.
116 RTC_DCHECK_LT(capture_delay_buffer_index_ + kBlockSize - 1,
117 capture_delay_buffer_.size());
118 const rtc::Optional<size_t> echo_path_delay_samples_shifted =
119 delay_estimator_.EstimateDelay(
120 render_buffer,
121 rtc::ArrayView<const float>(
122 &capture_delay_buffer_[capture_delay_buffer_index_], kBlockSize));
123 std::copy(capture.begin(), capture.end(),
124 capture_delay_buffer_.begin() + capture_delay_buffer_index_);
125 capture_delay_buffer_index_ =
126 (capture_delay_buffer_index_ + kBlockSize) % capture_delay_buffer_.size();
127
128 if (echo_path_delay_samples_shifted) {
Per Åhgrenf0a6fb12017-06-27 11:44:27 +0200129 blocks_since_last_delay_estimate_ = 0;
Per Åhgren930021d2017-09-15 07:32:53 +0200130
131 // Correct for the capture signal delay.
132 const int echo_path_delay_samples_corrected =
133 static_cast<int>(*echo_path_delay_samples_shifted) -
134 static_cast<int>(capture_delay_buffer_.size());
135 echo_path_delay_samples_ = std::max(0, echo_path_delay_samples_corrected);
peah69221db2017-01-27 03:28:19 -0800136
137 // Compute and set new render delay buffer delay.
138 const size_t new_delay =
peahcf02cf12017-04-05 14:18:07 -0700139 ComputeNewBufferDelay(delay_, echo_path_delay_samples_);
Per Åhgrenf0a6fb12017-06-27 11:44:27 +0200140 if (align_call_counter_ > kNumBlocksPerSecond) {
peah69221db2017-01-27 03:28:19 -0800141 delay_ = new_delay;
peah69221db2017-01-27 03:28:19 -0800142
Per Åhgrenf0a6fb12017-06-27 11:44:27 +0200143 // Update render delay buffer headroom.
Per Åhgren930021d2017-09-15 07:32:53 +0200144 if (echo_path_delay_samples_corrected >= 0) {
145 const int headroom = echo_path_delay_samples_ - delay_ * kBlockSize;
146 RTC_DCHECK_LE(0, headroom);
147 headroom_samples_ = rtc::Optional<size_t>(headroom);
148 } else {
149 headroom_samples_ = rtc::Optional<size_t>();
150 }
Per Åhgrenf0a6fb12017-06-27 11:44:27 +0200151 }
peah69221db2017-01-27 03:28:19 -0800152
Per Åhgren930021d2017-09-15 07:32:53 +0200153 metrics_.Update(rtc::Optional<size_t>(echo_path_delay_samples_), delay_);
154 } else {
155 metrics_.Update(rtc::Optional<size_t>(), delay_);
156 }
peahe985b3f2017-02-28 22:08:53 -0800157
peah69221db2017-01-27 03:28:19 -0800158 data_dumper_->DumpRaw("aec3_render_delay_controller_delay", 1,
159 &echo_path_delay_samples_);
160 data_dumper_->DumpRaw("aec3_render_delay_controller_buffer_delay", delay_);
161
162 return delay_;
163}
164
peah69221db2017-01-27 03:28:19 -0800165} // namespace
166
peah4fed3c02017-08-30 06:58:44 -0700167RenderDelayController* RenderDelayController::Create(
168 const AudioProcessing::Config::EchoCanceller3& config,
169 int sample_rate_hz) {
170 return new RenderDelayControllerImpl(config, sample_rate_hz);
peah69221db2017-01-27 03:28:19 -0800171}
172
173} // namespace webrtc