blob: 565bf43f0b44e364a0be3f673c6cbe4f8a3cc898 [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 */
10#include "webrtc/modules/audio_processing/aec3/render_delay_controller.h"
11
12#include <algorithm>
13#include <memory>
14#include <string>
15#include <vector>
16
peah522d71b2017-02-23 05:16:26 -080017#include "webrtc/modules/audio_processing/aec3/aec3_common.h"
peah69221db2017-01-27 03:28:19 -080018#include "webrtc/modules/audio_processing/aec3/echo_path_delay_estimator.h"
peahe985b3f2017-02-28 22:08:53 -080019#include "webrtc/modules/audio_processing/aec3/render_delay_controller_metrics.h"
peah4fed3c02017-08-30 06:58:44 -070020#include "webrtc/modules/audio_processing/include/audio_processing.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020021#include "webrtc/rtc_base/atomicops.h"
22#include "webrtc/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_;
peahe985b3f2017-02-28 22:08:53 -080051 RenderDelayControllerMetrics metrics_;
peah69221db2017-01-27 03:28:19 -080052 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RenderDelayControllerImpl);
53};
54
55size_t ComputeNewBufferDelay(size_t current_delay,
peah69221db2017-01-27 03:28:19 -080056 size_t echo_path_delay_samples) {
57 // The below division is not exact and the truncation is intended.
58 const int echo_path_delay_blocks = echo_path_delay_samples / kBlockSize;
59 constexpr int kDelayHeadroomBlocks = 1;
60
61 // Compute the buffer delay increase required to achieve the desired latency.
62 size_t new_delay = std::max(echo_path_delay_blocks - kDelayHeadroomBlocks, 0);
63
64 // Add hysteresis.
peah96b951c2017-08-22 10:26:07 -070065 if (new_delay == current_delay + 1) {
peah69221db2017-01-27 03:28:19 -080066 new_delay = current_delay;
67 }
68
peah69221db2017-01-27 03:28:19 -080069 return new_delay;
70}
71
72int RenderDelayControllerImpl::instance_count_ = 0;
73
peah4fed3c02017-08-30 06:58:44 -070074RenderDelayControllerImpl::RenderDelayControllerImpl(
75 const AudioProcessing::Config::EchoCanceller3& config,
76 int sample_rate_hz)
peah69221db2017-01-27 03:28:19 -080077 : data_dumper_(
78 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
peah4fed3c02017-08-30 06:58:44 -070079 delay_estimator_(data_dumper_.get(), config) {
peah21920892017-02-08 05:08:56 -080080 RTC_DCHECK(ValidFullBandRate(sample_rate_hz));
peah69221db2017-01-27 03:28:19 -080081}
82
83RenderDelayControllerImpl::~RenderDelayControllerImpl() = default;
84
peahcf02cf12017-04-05 14:18:07 -070085void RenderDelayControllerImpl::Reset() {
peah96b951c2017-08-22 10:26:07 -070086 delay_ = kMinEchoPathDelayBlocks;
peahcf02cf12017-04-05 14:18:07 -070087 blocks_since_last_delay_estimate_ = 300000;
peahc6b10412017-09-11 06:46:07 -070088 echo_path_delay_samples_ = delay_ * kBlockSize;
peahcf02cf12017-04-05 14:18:07 -070089 align_call_counter_ = 0;
90 headroom_samples_ = rtc::Optional<size_t>();
91
92 delay_estimator_.Reset();
93}
94
95void RenderDelayControllerImpl::SetDelay(size_t render_delay) {
96 if (delay_ != render_delay) {
97 // If a the delay set does not match the actual delay, reset the delay
98 // controller.
99 Reset();
100 delay_ = render_delay;
101 }
102}
103
peah69221db2017-01-27 03:28:19 -0800104size_t RenderDelayControllerImpl::GetDelay(
peahcf02cf12017-04-05 14:18:07 -0700105 const DownsampledRenderBuffer& render_buffer,
peah69221db2017-01-27 03:28:19 -0800106 rtc::ArrayView<const float> capture) {
107 RTC_DCHECK_EQ(kBlockSize, capture.size());
peah69221db2017-01-27 03:28:19 -0800108
109 ++align_call_counter_;
peah69221db2017-01-27 03:28:19 -0800110 rtc::Optional<size_t> echo_path_delay_samples =
peahcf02cf12017-04-05 14:18:07 -0700111 delay_estimator_.EstimateDelay(render_buffer, capture);
peah69221db2017-01-27 03:28:19 -0800112 if (echo_path_delay_samples) {
Per Åhgrenf0a6fb12017-06-27 11:44:27 +0200113 blocks_since_last_delay_estimate_ = 0;
peah69221db2017-01-27 03:28:19 -0800114 echo_path_delay_samples_ = *echo_path_delay_samples;
115
116 // Compute and set new render delay buffer delay.
117 const size_t new_delay =
peahcf02cf12017-04-05 14:18:07 -0700118 ComputeNewBufferDelay(delay_, echo_path_delay_samples_);
Per Åhgrenf0a6fb12017-06-27 11:44:27 +0200119 if (align_call_counter_ > kNumBlocksPerSecond) {
peah69221db2017-01-27 03:28:19 -0800120 delay_ = new_delay;
peah69221db2017-01-27 03:28:19 -0800121
Per Åhgrenf0a6fb12017-06-27 11:44:27 +0200122 // Update render delay buffer headroom.
123 const int headroom = echo_path_delay_samples_ - delay_ * kBlockSize;
124 RTC_DCHECK_LE(0, headroom);
125 headroom_samples_ = rtc::Optional<size_t>(headroom);
126 }
peah69221db2017-01-27 03:28:19 -0800127 }
128
peahe985b3f2017-02-28 22:08:53 -0800129 metrics_.Update(echo_path_delay_samples, delay_);
130
peah69221db2017-01-27 03:28:19 -0800131 data_dumper_->DumpRaw("aec3_render_delay_controller_delay", 1,
132 &echo_path_delay_samples_);
133 data_dumper_->DumpRaw("aec3_render_delay_controller_buffer_delay", delay_);
134
135 return delay_;
136}
137
peah69221db2017-01-27 03:28:19 -0800138} // namespace
139
peah4fed3c02017-08-30 06:58:44 -0700140RenderDelayController* RenderDelayController::Create(
141 const AudioProcessing::Config::EchoCanceller3& config,
142 int sample_rate_hz) {
143 return new RenderDelayControllerImpl(config, sample_rate_hz);
peah69221db2017-01-27 03:28:19 -0800144}
145
146} // namespace webrtc