2017-01-27 03:28:19 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/audio_processing/aec3/render_delay_controller.h"
|
2017-01-27 03:28:19 -08:00
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/audio_processing/aec3/aec3_common.h"
|
|
|
|
|
#include "modules/audio_processing/aec3/echo_path_delay_estimator.h"
|
|
|
|
|
#include "modules/audio_processing/aec3/render_delay_controller_metrics.h"
|
|
|
|
|
#include "modules/audio_processing/include/audio_processing.h"
|
|
|
|
|
#include "rtc_base/atomicops.h"
|
|
|
|
|
#include "rtc_base/constructormagic.h"
|
2017-01-27 03:28:19 -08:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
class RenderDelayControllerImpl final : public RenderDelayController {
|
|
|
|
|
public:
|
2017-10-18 12:32:42 +02:00
|
|
|
RenderDelayControllerImpl(const EchoCanceller3Config& config,
|
|
|
|
|
int sample_rate_hz);
|
2017-01-27 03:28:19 -08:00
|
|
|
~RenderDelayControllerImpl() override;
|
2017-04-05 14:18:07 -07:00
|
|
|
void Reset() override;
|
|
|
|
|
void SetDelay(size_t render_delay) override;
|
|
|
|
|
size_t GetDelay(const DownsampledRenderBuffer& render_buffer,
|
|
|
|
|
rtc::ArrayView<const float> capture) override;
|
2017-01-27 03:28:19 -08:00
|
|
|
rtc::Optional<size_t> AlignmentHeadroomSamples() const override {
|
|
|
|
|
return headroom_samples_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
static int instance_count_;
|
|
|
|
|
std::unique_ptr<ApmDataDumper> data_dumper_;
|
2017-12-01 23:01:44 +01:00
|
|
|
const size_t min_echo_path_delay_;
|
2017-10-09 12:21:56 +02:00
|
|
|
const size_t default_delay_;
|
|
|
|
|
size_t delay_;
|
2017-12-01 23:01:44 +01:00
|
|
|
EchoPathDelayEstimator delay_estimator_;
|
2017-01-27 03:28:19 -08:00
|
|
|
size_t blocks_since_last_delay_estimate_ = 300000;
|
2017-10-09 12:21:56 +02:00
|
|
|
int echo_path_delay_samples_;
|
2017-01-27 03:28:19 -08:00
|
|
|
size_t align_call_counter_ = 0;
|
|
|
|
|
rtc::Optional<size_t> headroom_samples_;
|
2017-09-15 07:32:53 +02:00
|
|
|
std::vector<float> capture_delay_buffer_;
|
|
|
|
|
int capture_delay_buffer_index_ = 0;
|
2017-02-28 22:08:53 -08:00
|
|
|
RenderDelayControllerMetrics metrics_;
|
2017-01-27 03:28:19 -08:00
|
|
|
RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RenderDelayControllerImpl);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
size_t ComputeNewBufferDelay(size_t current_delay,
|
|
|
|
|
size_t echo_path_delay_samples) {
|
|
|
|
|
// The below division is not exact and the truncation is intended.
|
|
|
|
|
const int echo_path_delay_blocks = echo_path_delay_samples / kBlockSize;
|
|
|
|
|
constexpr int kDelayHeadroomBlocks = 1;
|
|
|
|
|
|
|
|
|
|
// Compute the buffer delay increase required to achieve the desired latency.
|
|
|
|
|
size_t new_delay = std::max(echo_path_delay_blocks - kDelayHeadroomBlocks, 0);
|
|
|
|
|
|
|
|
|
|
// Add hysteresis.
|
2017-08-22 10:26:07 -07:00
|
|
|
if (new_delay == current_delay + 1) {
|
2017-01-27 03:28:19 -08:00
|
|
|
new_delay = current_delay;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new_delay;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int RenderDelayControllerImpl::instance_count_ = 0;
|
|
|
|
|
|
2017-08-30 06:58:44 -07:00
|
|
|
RenderDelayControllerImpl::RenderDelayControllerImpl(
|
2017-10-18 12:32:42 +02:00
|
|
|
const EchoCanceller3Config& config,
|
2017-08-30 06:58:44 -07:00
|
|
|
int sample_rate_hz)
|
2017-01-27 03:28:19 -08:00
|
|
|
: data_dumper_(
|
|
|
|
|
new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
|
2017-12-01 23:01:44 +01:00
|
|
|
min_echo_path_delay_(config.delay.min_echo_path_delay_blocks),
|
2017-10-09 12:21:56 +02:00
|
|
|
default_delay_(
|
2017-12-01 23:01:44 +01:00
|
|
|
std::max(config.delay.default_delay, min_echo_path_delay_)),
|
2017-10-09 12:21:56 +02:00
|
|
|
delay_(default_delay_),
|
2017-12-01 23:01:44 +01:00
|
|
|
delay_estimator_(data_dumper_.get(), config),
|
2017-10-09 12:21:56 +02:00
|
|
|
echo_path_delay_samples_(default_delay_ * kBlockSize),
|
2017-12-01 23:01:44 +01:00
|
|
|
capture_delay_buffer_(
|
|
|
|
|
kBlockSize * (config.delay.api_call_jitter_blocks + 2),
|
|
|
|
|
0.f) {
|
2017-02-08 05:08:56 -08:00
|
|
|
RTC_DCHECK(ValidFullBandRate(sample_rate_hz));
|
2017-11-17 14:54:28 +01:00
|
|
|
delay_estimator_.LogDelayEstimationProperties(sample_rate_hz,
|
|
|
|
|
capture_delay_buffer_.size());
|
2017-01-27 03:28:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RenderDelayControllerImpl::~RenderDelayControllerImpl() = default;
|
|
|
|
|
|
2017-04-05 14:18:07 -07:00
|
|
|
void RenderDelayControllerImpl::Reset() {
|
2017-10-09 12:21:56 +02:00
|
|
|
delay_ = default_delay_;
|
2017-04-05 14:18:07 -07:00
|
|
|
blocks_since_last_delay_estimate_ = 300000;
|
2017-09-11 06:46:07 -07:00
|
|
|
echo_path_delay_samples_ = delay_ * kBlockSize;
|
2017-04-05 14:18:07 -07:00
|
|
|
align_call_counter_ = 0;
|
2017-11-17 14:34:48 +01:00
|
|
|
headroom_samples_ = rtc::nullopt;
|
2017-09-15 07:32:53 +02:00
|
|
|
std::fill(capture_delay_buffer_.begin(), capture_delay_buffer_.end(), 0.f);
|
2017-04-05 14:18:07 -07:00
|
|
|
delay_estimator_.Reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderDelayControllerImpl::SetDelay(size_t render_delay) {
|
|
|
|
|
if (delay_ != render_delay) {
|
|
|
|
|
// If a the delay set does not match the actual delay, reset the delay
|
|
|
|
|
// controller.
|
|
|
|
|
Reset();
|
|
|
|
|
delay_ = render_delay;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 03:28:19 -08:00
|
|
|
size_t RenderDelayControllerImpl::GetDelay(
|
2017-04-05 14:18:07 -07:00
|
|
|
const DownsampledRenderBuffer& render_buffer,
|
2017-01-27 03:28:19 -08:00
|
|
|
rtc::ArrayView<const float> capture) {
|
|
|
|
|
RTC_DCHECK_EQ(kBlockSize, capture.size());
|
|
|
|
|
|
|
|
|
|
++align_call_counter_;
|
2017-09-15 07:32:53 +02:00
|
|
|
|
|
|
|
|
// Estimate the delay with a delayed capture signal in order to catch
|
|
|
|
|
// noncausal delays.
|
|
|
|
|
RTC_DCHECK_LT(capture_delay_buffer_index_ + kBlockSize - 1,
|
|
|
|
|
capture_delay_buffer_.size());
|
|
|
|
|
const rtc::Optional<size_t> echo_path_delay_samples_shifted =
|
|
|
|
|
delay_estimator_.EstimateDelay(
|
|
|
|
|
render_buffer,
|
|
|
|
|
rtc::ArrayView<const float>(
|
|
|
|
|
&capture_delay_buffer_[capture_delay_buffer_index_], kBlockSize));
|
|
|
|
|
std::copy(capture.begin(), capture.end(),
|
|
|
|
|
capture_delay_buffer_.begin() + capture_delay_buffer_index_);
|
|
|
|
|
capture_delay_buffer_index_ =
|
|
|
|
|
(capture_delay_buffer_index_ + kBlockSize) % capture_delay_buffer_.size();
|
|
|
|
|
|
|
|
|
|
if (echo_path_delay_samples_shifted) {
|
2017-06-27 11:44:27 +02:00
|
|
|
blocks_since_last_delay_estimate_ = 0;
|
2017-09-15 07:32:53 +02:00
|
|
|
|
|
|
|
|
// Correct for the capture signal delay.
|
|
|
|
|
const int echo_path_delay_samples_corrected =
|
|
|
|
|
static_cast<int>(*echo_path_delay_samples_shifted) -
|
|
|
|
|
static_cast<int>(capture_delay_buffer_.size());
|
|
|
|
|
echo_path_delay_samples_ = std::max(0, echo_path_delay_samples_corrected);
|
2017-01-27 03:28:19 -08:00
|
|
|
|
|
|
|
|
// Compute and set new render delay buffer delay.
|
|
|
|
|
const size_t new_delay =
|
2017-04-05 14:18:07 -07:00
|
|
|
ComputeNewBufferDelay(delay_, echo_path_delay_samples_);
|
2017-06-27 11:44:27 +02:00
|
|
|
if (align_call_counter_ > kNumBlocksPerSecond) {
|
2017-01-27 03:28:19 -08:00
|
|
|
delay_ = new_delay;
|
|
|
|
|
|
2017-06-27 11:44:27 +02:00
|
|
|
// Update render delay buffer headroom.
|
2017-09-15 07:32:53 +02:00
|
|
|
if (echo_path_delay_samples_corrected >= 0) {
|
|
|
|
|
const int headroom = echo_path_delay_samples_ - delay_ * kBlockSize;
|
|
|
|
|
RTC_DCHECK_LE(0, headroom);
|
2017-11-17 14:34:48 +01:00
|
|
|
headroom_samples_ = headroom;
|
2017-09-15 07:32:53 +02:00
|
|
|
} else {
|
2017-11-17 14:34:48 +01:00
|
|
|
headroom_samples_ = rtc::nullopt;
|
2017-09-15 07:32:53 +02:00
|
|
|
}
|
2017-06-27 11:44:27 +02:00
|
|
|
}
|
2017-01-27 03:28:19 -08:00
|
|
|
|
2017-11-17 14:34:48 +01:00
|
|
|
metrics_.Update(echo_path_delay_samples_, delay_);
|
2017-09-15 07:32:53 +02:00
|
|
|
} else {
|
2017-11-17 14:34:48 +01:00
|
|
|
metrics_.Update(rtc::nullopt, delay_);
|
2017-09-15 07:32:53 +02:00
|
|
|
}
|
2017-02-28 22:08:53 -08:00
|
|
|
|
2017-01-27 03:28:19 -08:00
|
|
|
data_dumper_->DumpRaw("aec3_render_delay_controller_delay", 1,
|
|
|
|
|
&echo_path_delay_samples_);
|
|
|
|
|
data_dumper_->DumpRaw("aec3_render_delay_controller_buffer_delay", delay_);
|
|
|
|
|
|
|
|
|
|
return delay_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2017-08-30 06:58:44 -07:00
|
|
|
RenderDelayController* RenderDelayController::Create(
|
2017-10-18 12:32:42 +02:00
|
|
|
const EchoCanceller3Config& config,
|
2017-08-30 06:58:44 -07:00
|
|
|
int sample_rate_hz) {
|
|
|
|
|
return new RenderDelayControllerImpl(config, sample_rate_hz);
|
2017-01-27 03:28:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|