2017-01-27 03:28:19 -08:00
|
|
|
/*
|
2019-01-24 16:00:57 +01:00
|
|
|
* Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
|
2017-01-27 03:28:19 -08:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2019-01-24 16:00:57 +01:00
|
|
|
#include "modules/audio_processing/aec3/render_delay_controller.h"
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2019-01-24 16:00:57 +01:00
|
|
|
#include <stddef.h>
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2017-01-27 03:28:19 -08:00
|
|
|
#include <algorithm>
|
2022-07-07 20:44:37 +02:00
|
|
|
#include <atomic>
|
2017-01-27 03:28:19 -08:00
|
|
|
#include <memory>
|
|
|
|
|
|
2019-01-24 16:00:57 +01:00
|
|
|
#include "absl/types/optional.h"
|
|
|
|
|
#include "api/array_view.h"
|
2018-02-14 15:19:04 +01:00
|
|
|
#include "api/audio/echo_canceller3_config.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/audio_processing/aec3/aec3_common.h"
|
2019-01-24 16:00:57 +01:00
|
|
|
#include "modules/audio_processing/aec3/delay_estimate.h"
|
|
|
|
|
#include "modules/audio_processing/aec3/downsampled_render_buffer.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/audio_processing/aec3/echo_path_delay_estimator.h"
|
|
|
|
|
#include "modules/audio_processing/aec3/render_delay_controller_metrics.h"
|
2018-10-23 12:03:01 +02:00
|
|
|
#include "modules/audio_processing/logging/apm_data_dumper.h"
|
|
|
|
|
#include "rtc_base/checks.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,
|
2019-12-03 11:24:59 +01:00
|
|
|
int sample_rate_hz,
|
|
|
|
|
size_t num_capture_channels);
|
2020-09-29 09:46:21 +02:00
|
|
|
|
|
|
|
|
RenderDelayControllerImpl() = delete;
|
|
|
|
|
RenderDelayControllerImpl(const RenderDelayControllerImpl&) = delete;
|
|
|
|
|
RenderDelayControllerImpl& operator=(const RenderDelayControllerImpl&) =
|
|
|
|
|
delete;
|
|
|
|
|
|
2017-01-27 03:28:19 -08:00
|
|
|
~RenderDelayControllerImpl() override;
|
2018-10-30 23:44:40 +01:00
|
|
|
void Reset(bool reset_delay_confidence) override;
|
2018-02-13 12:59:33 +01:00
|
|
|
void LogRenderCall() override;
|
2018-06-19 10:50:11 +02:00
|
|
|
absl::optional<DelayEstimate> GetDelay(
|
2018-01-25 07:01:34 +01:00
|
|
|
const DownsampledRenderBuffer& render_buffer,
|
2018-03-22 00:29:25 +01:00
|
|
|
size_t render_delay_buffer_delay,
|
2022-05-23 10:39:53 +02:00
|
|
|
const Block& capture) override;
|
AEC3: Clockdrift detection
This change introduces a clockdrift detector operating on the estimated
delay of the echo path delay estimator. Each time the delay estimate
changes it is compared to previous estimates. If the estimates are
slowly increasing or decreasing, clockdrift is detected.
Four different patterns are considered clockdrift:
- k, k+1, k+2, k+3
- k, k+2, k+1, k+3
- k, k-1, k-2, k-3
- k, k-2, k-1, k-3
A delay estimate history matching the three last elements in one of the
patterns is considered probable clockdrift. Matching all four elements
is considered verified clockdrift.
If the delay is constant for some time after clockdrift is detected the
clockdrift detector will revert to no detected clockdrift.
The level of clockdrift is reported via an UMA histogram.
Bug: webrtc:10014
Change-Id: I1cce4d593e101a8b3fa99df6935e59b4243cb97a
Reviewed-on: https://webrtc-review.googlesource.com/c/111381
Commit-Queue: Gustaf Ullberg <gustaf@webrtc.org>
Reviewed-by: Per Åhgren <peah@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25758}
2018-11-22 16:02:34 +01:00
|
|
|
bool HasClockdrift() const override;
|
2017-01-27 03:28:19 -08:00
|
|
|
|
|
|
|
|
private:
|
2022-06-27 09:47:02 +02:00
|
|
|
static std::atomic<int> instance_count_;
|
2017-01-27 03:28:19 -08:00
|
|
|
std::unique_ptr<ApmDataDumper> data_dumper_;
|
2019-03-14 11:24:54 +01:00
|
|
|
const int hysteresis_limit_blocks_;
|
2018-06-19 10:50:11 +02:00
|
|
|
absl::optional<DelayEstimate> delay_;
|
2017-12-01 23:01:44 +01:00
|
|
|
EchoPathDelayEstimator delay_estimator_;
|
2017-02-28 22:08:53 -08:00
|
|
|
RenderDelayControllerMetrics metrics_;
|
2018-06-19 10:50:11 +02:00
|
|
|
absl::optional<DelayEstimate> delay_samples_;
|
2018-02-22 17:51:39 +01:00
|
|
|
size_t capture_call_counter_ = 0;
|
2018-02-13 12:59:33 +01:00
|
|
|
int delay_change_counter_ = 0;
|
2018-10-30 23:44:40 +01:00
|
|
|
DelayEstimate::Quality last_delay_estimate_quality_;
|
2017-01-27 03:28:19 -08:00
|
|
|
};
|
|
|
|
|
|
2018-02-13 12:59:33 +01:00
|
|
|
DelayEstimate ComputeBufferDelay(
|
2018-06-19 10:50:11 +02:00
|
|
|
const absl::optional<DelayEstimate>& current_delay,
|
2019-03-14 11:24:54 +01:00
|
|
|
int hysteresis_limit_blocks,
|
2018-01-25 07:01:34 +01:00
|
|
|
DelayEstimate estimated_delay) {
|
2017-01-27 03:28:19 -08:00
|
|
|
// Compute the buffer delay increase required to achieve the desired latency.
|
2022-06-28 15:37:13 +02:00
|
|
|
size_t new_delay_blocks = estimated_delay.delay >> kBlockSizeLog2;
|
2017-01-27 03:28:19 -08:00
|
|
|
// Add hysteresis.
|
2017-12-11 21:34:19 +01:00
|
|
|
if (current_delay) {
|
2018-01-25 07:01:34 +01:00
|
|
|
size_t current_delay_blocks = current_delay->delay;
|
2019-03-14 11:24:54 +01:00
|
|
|
if (new_delay_blocks > current_delay_blocks &&
|
|
|
|
|
new_delay_blocks <= current_delay_blocks + hysteresis_limit_blocks) {
|
|
|
|
|
new_delay_blocks = current_delay_blocks;
|
2017-12-11 21:34:19 +01:00
|
|
|
}
|
2017-01-27 03:28:19 -08:00
|
|
|
}
|
2018-02-21 08:46:03 +01:00
|
|
|
DelayEstimate new_delay = estimated_delay;
|
|
|
|
|
new_delay.delay = new_delay_blocks;
|
|
|
|
|
return new_delay;
|
2017-01-27 03:28:19 -08:00
|
|
|
}
|
|
|
|
|
|
2022-06-27 09:47:02 +02:00
|
|
|
std::atomic<int> RenderDelayControllerImpl::instance_count_(0);
|
2017-01-27 03:28:19 -08:00
|
|
|
|
2017-08-30 06:58:44 -07:00
|
|
|
RenderDelayControllerImpl::RenderDelayControllerImpl(
|
2017-10-18 12:32:42 +02:00
|
|
|
const EchoCanceller3Config& config,
|
2019-12-03 11:24:59 +01:00
|
|
|
int sample_rate_hz,
|
|
|
|
|
size_t num_capture_channels)
|
2022-06-27 09:47:02 +02:00
|
|
|
: data_dumper_(new ApmDataDumper(instance_count_.fetch_add(1) + 1)),
|
2019-03-14 11:24:54 +01:00
|
|
|
hysteresis_limit_blocks_(
|
|
|
|
|
static_cast<int>(config.delay.hysteresis_limit_blocks)),
|
2019-12-03 11:24:59 +01:00
|
|
|
delay_estimator_(data_dumper_.get(), config, num_capture_channels),
|
2018-10-30 23:44:40 +01:00
|
|
|
last_delay_estimate_quality_(DelayEstimate::Quality::kCoarse) {
|
2017-02-08 05:08:56 -08:00
|
|
|
RTC_DCHECK(ValidFullBandRate(sample_rate_hz));
|
2019-01-24 16:00:57 +01:00
|
|
|
delay_estimator_.LogDelayEstimationProperties(sample_rate_hz, 0);
|
2017-01-27 03:28:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RenderDelayControllerImpl::~RenderDelayControllerImpl() = default;
|
|
|
|
|
|
2018-10-30 23:44:40 +01:00
|
|
|
void RenderDelayControllerImpl::Reset(bool reset_delay_confidence) {
|
2018-06-19 10:50:11 +02:00
|
|
|
delay_ = absl::nullopt;
|
|
|
|
|
delay_samples_ = absl::nullopt;
|
2018-10-30 23:44:40 +01:00
|
|
|
delay_estimator_.Reset(reset_delay_confidence);
|
2018-02-13 12:59:33 +01:00
|
|
|
delay_change_counter_ = 0;
|
2018-10-30 23:44:40 +01:00
|
|
|
if (reset_delay_confidence) {
|
|
|
|
|
last_delay_estimate_quality_ = DelayEstimate::Quality::kCoarse;
|
|
|
|
|
}
|
2018-02-13 12:59:33 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-24 16:00:57 +01:00
|
|
|
void RenderDelayControllerImpl::LogRenderCall() {}
|
2017-04-05 14:18:07 -07:00
|
|
|
|
2018-06-19 10:50:11 +02:00
|
|
|
absl::optional<DelayEstimate> RenderDelayControllerImpl::GetDelay(
|
2017-04-05 14:18:07 -07:00
|
|
|
const DownsampledRenderBuffer& render_buffer,
|
2018-03-22 00:29:25 +01:00
|
|
|
size_t render_delay_buffer_delay,
|
2022-05-23 10:39:53 +02:00
|
|
|
const Block& capture) {
|
2018-02-22 17:51:39 +01:00
|
|
|
++capture_call_counter_;
|
2017-09-15 07:32:53 +02:00
|
|
|
|
2019-01-24 16:00:57 +01:00
|
|
|
auto delay_samples = delay_estimator_.EstimateDelay(render_buffer, capture);
|
2017-09-15 07:32:53 +02:00
|
|
|
|
2018-02-13 12:59:33 +01:00
|
|
|
if (delay_samples) {
|
|
|
|
|
if (!delay_samples_ || delay_samples->delay != delay_samples_->delay) {
|
|
|
|
|
delay_change_counter_ = 0;
|
|
|
|
|
}
|
2018-02-21 08:46:03 +01:00
|
|
|
if (delay_samples_) {
|
|
|
|
|
delay_samples_->blocks_since_last_change =
|
|
|
|
|
delay_samples_->delay == delay_samples->delay
|
|
|
|
|
? delay_samples_->blocks_since_last_change + 1
|
|
|
|
|
: 0;
|
|
|
|
|
delay_samples_->blocks_since_last_update = 0;
|
|
|
|
|
delay_samples_->delay = delay_samples->delay;
|
|
|
|
|
delay_samples_->quality = delay_samples->quality;
|
|
|
|
|
} else {
|
|
|
|
|
delay_samples_ = delay_samples;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (delay_samples_) {
|
|
|
|
|
++delay_samples_->blocks_since_last_change;
|
|
|
|
|
++delay_samples_->blocks_since_last_update;
|
|
|
|
|
}
|
2018-02-13 12:59:33 +01:00
|
|
|
}
|
2018-01-25 07:01:34 +01:00
|
|
|
|
2018-02-13 12:59:33 +01:00
|
|
|
if (delay_change_counter_ < 2 * kNumBlocksPerSecond) {
|
|
|
|
|
++delay_change_counter_;
|
2018-02-22 17:51:39 +01:00
|
|
|
}
|
|
|
|
|
|
2018-02-13 12:59:33 +01:00
|
|
|
if (delay_samples_) {
|
|
|
|
|
// Compute the render delay buffer delay.
|
2018-10-30 23:44:40 +01:00
|
|
|
const bool use_hysteresis =
|
|
|
|
|
last_delay_estimate_quality_ == DelayEstimate::Quality::kRefined &&
|
|
|
|
|
delay_samples_->quality == DelayEstimate::Quality::kRefined;
|
2022-06-28 15:37:13 +02:00
|
|
|
delay_ = ComputeBufferDelay(
|
|
|
|
|
delay_, use_hysteresis ? hysteresis_limit_blocks_ : 0, *delay_samples_);
|
2018-10-30 23:44:40 +01:00
|
|
|
last_delay_estimate_quality_ = delay_samples_->quality;
|
2018-02-13 12:59:33 +01:00
|
|
|
}
|
|
|
|
|
|
2022-10-12 17:14:31 +02:00
|
|
|
metrics_.Update(
|
|
|
|
|
delay_samples_ ? absl::optional<size_t>(delay_samples_->delay)
|
|
|
|
|
: absl::nullopt,
|
|
|
|
|
delay_ ? absl::optional<size_t>(delay_->delay) : absl::nullopt,
|
|
|
|
|
delay_estimator_.Clockdrift());
|
2018-02-13 12:59:33 +01:00
|
|
|
|
2017-12-11 21:34:19 +01:00
|
|
|
data_dumper_->DumpRaw("aec3_render_delay_controller_delay",
|
2018-01-25 07:01:34 +01:00
|
|
|
delay_samples ? delay_samples->delay : 0);
|
2017-12-11 21:34:19 +01:00
|
|
|
data_dumper_->DumpRaw("aec3_render_delay_controller_buffer_delay",
|
2018-01-25 07:01:34 +01:00
|
|
|
delay_ ? delay_->delay : 0);
|
2017-01-27 03:28:19 -08:00
|
|
|
|
|
|
|
|
return delay_;
|
|
|
|
|
}
|
|
|
|
|
|
AEC3: Clockdrift detection
This change introduces a clockdrift detector operating on the estimated
delay of the echo path delay estimator. Each time the delay estimate
changes it is compared to previous estimates. If the estimates are
slowly increasing or decreasing, clockdrift is detected.
Four different patterns are considered clockdrift:
- k, k+1, k+2, k+3
- k, k+2, k+1, k+3
- k, k-1, k-2, k-3
- k, k-2, k-1, k-3
A delay estimate history matching the three last elements in one of the
patterns is considered probable clockdrift. Matching all four elements
is considered verified clockdrift.
If the delay is constant for some time after clockdrift is detected the
clockdrift detector will revert to no detected clockdrift.
The level of clockdrift is reported via an UMA histogram.
Bug: webrtc:10014
Change-Id: I1cce4d593e101a8b3fa99df6935e59b4243cb97a
Reviewed-on: https://webrtc-review.googlesource.com/c/111381
Commit-Queue: Gustaf Ullberg <gustaf@webrtc.org>
Reviewed-by: Per Åhgren <peah@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25758}
2018-11-22 16:02:34 +01:00
|
|
|
bool RenderDelayControllerImpl::HasClockdrift() const {
|
|
|
|
|
return delay_estimator_.Clockdrift() != ClockdriftDetector::Level::kNone;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 03:28:19 -08:00
|
|
|
} // namespace
|
|
|
|
|
|
2017-08-30 06:58:44 -07:00
|
|
|
RenderDelayController* RenderDelayController::Create(
|
2017-10-18 12:32:42 +02:00
|
|
|
const EchoCanceller3Config& config,
|
2019-12-03 11:24:59 +01:00
|
|
|
int sample_rate_hz,
|
|
|
|
|
size_t num_capture_channels) {
|
|
|
|
|
return new RenderDelayControllerImpl(config, sample_rate_hz,
|
|
|
|
|
num_capture_channels);
|
2017-01-27 03:28:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|