Refactor NetEq delay manager logic.
- Removes dependence on sequence number for calculating target delay. - Changes target delay unit to milliseconds instead of number of packets. - Moves acceleration/preemptive expand thresholds to decision logic. Tests for this will be added in a follow up cl. Bug: webrtc:10333 Change-Id: If690aae4abf41ef1d9353f0ff01fb7d121cf8a26 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/186265 Commit-Queue: Jakob Ivarsson <jakobi@webrtc.org> Reviewed-by: Ivo Creusen <ivoc@webrtc.org> Cr-Commit-Position: refs/heads/master@{#32326}
This commit is contained in:
committed by
Commit Bot
parent
76d3e7a8d1
commit
f8e62fcb14
@ -27,17 +27,16 @@
|
||||
#include "rtc_base/numerics/safe_minmax.h"
|
||||
#include "system_wrappers/include/field_trial.h"
|
||||
|
||||
namespace webrtc {
|
||||
namespace {
|
||||
|
||||
constexpr int kMinBaseMinimumDelayMs = 0;
|
||||
constexpr int kMaxBaseMinimumDelayMs = 10000;
|
||||
constexpr int kMaxReorderedPackets =
|
||||
10; // Max number of consecutive reordered packets.
|
||||
constexpr int kMaxHistoryMs = 2000; // Oldest packet to include in history to
|
||||
// calculate relative packet arrival delay.
|
||||
constexpr int kDelayBuckets = 100;
|
||||
constexpr int kBucketSizeMs = 20;
|
||||
constexpr int kDecelerationTargetLevelOffsetMs = 85 << 8; // In Q8.
|
||||
constexpr int kStartDelayMs = 80;
|
||||
|
||||
int PercentileToQuantile(double percentile) {
|
||||
return static_cast<int>((1 << 30) * percentile / 100.0 + 0.5);
|
||||
@ -49,6 +48,7 @@ struct DelayHistogramConfig {
|
||||
absl::optional<double> start_forget_weight = 2;
|
||||
};
|
||||
|
||||
// TODO(jakobi): Remove legacy field trial.
|
||||
DelayHistogramConfig GetDelayHistogramConfig() {
|
||||
constexpr char kDelayHistogramFieldTrial[] =
|
||||
"WebRTC-Audio-NetEqDelayHistogram";
|
||||
@ -81,12 +81,9 @@ DelayHistogramConfig GetDelayHistogramConfig() {
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
DelayManager::DelayManager(size_t max_packets_in_buffer,
|
||||
DelayManager::DelayManager(int max_packets_in_buffer,
|
||||
int base_minimum_delay_ms,
|
||||
int histogram_quantile,
|
||||
bool enable_rtx_handling,
|
||||
const TickTimer* tick_timer,
|
||||
std::unique_ptr<Histogram> histogram)
|
||||
: first_packet_received_(false),
|
||||
@ -96,15 +93,10 @@ DelayManager::DelayManager(size_t max_packets_in_buffer,
|
||||
tick_timer_(tick_timer),
|
||||
base_minimum_delay_ms_(base_minimum_delay_ms),
|
||||
effective_minimum_delay_ms_(base_minimum_delay_ms),
|
||||
base_target_level_(4), // In Q0 domain.
|
||||
target_level_(base_target_level_ << 8), // In Q8 domain.
|
||||
packet_len_ms_(0),
|
||||
last_seq_no_(0),
|
||||
last_timestamp_(0),
|
||||
minimum_delay_ms_(0),
|
||||
maximum_delay_ms_(0),
|
||||
last_pack_cng_or_dtmf_(1),
|
||||
enable_rtx_handling_(enable_rtx_handling) {
|
||||
target_level_ms_(kStartDelayMs),
|
||||
last_timestamp_(0) {
|
||||
RTC_CHECK(histogram_);
|
||||
RTC_DCHECK_GE(base_minimum_delay_ms_, 0);
|
||||
|
||||
@ -112,102 +104,70 @@ DelayManager::DelayManager(size_t max_packets_in_buffer,
|
||||
}
|
||||
|
||||
std::unique_ptr<DelayManager> DelayManager::Create(
|
||||
size_t max_packets_in_buffer,
|
||||
int max_packets_in_buffer,
|
||||
int base_minimum_delay_ms,
|
||||
bool enable_rtx_handling,
|
||||
const TickTimer* tick_timer) {
|
||||
DelayHistogramConfig config = GetDelayHistogramConfig();
|
||||
const int quantile = config.quantile;
|
||||
auto config = GetDelayHistogramConfig();
|
||||
std::unique_ptr<Histogram> histogram = std::make_unique<Histogram>(
|
||||
kDelayBuckets, config.forget_factor, config.start_forget_weight);
|
||||
return std::make_unique<DelayManager>(
|
||||
max_packets_in_buffer, base_minimum_delay_ms, quantile,
|
||||
enable_rtx_handling, tick_timer, std::move(histogram));
|
||||
return std::make_unique<DelayManager>(max_packets_in_buffer,
|
||||
base_minimum_delay_ms, config.quantile,
|
||||
tick_timer, std::move(histogram));
|
||||
}
|
||||
|
||||
DelayManager::~DelayManager() {}
|
||||
|
||||
absl::optional<int> DelayManager::Update(uint16_t sequence_number,
|
||||
uint32_t timestamp,
|
||||
int sample_rate_hz) {
|
||||
absl::optional<int> DelayManager::Update(uint32_t timestamp,
|
||||
int sample_rate_hz,
|
||||
bool reset) {
|
||||
if (sample_rate_hz <= 0) {
|
||||
return absl::nullopt;
|
||||
}
|
||||
|
||||
if (!first_packet_received_) {
|
||||
// Prepare for next packet arrival.
|
||||
if (!first_packet_received_ || reset) {
|
||||
// Restart relative delay esimation from this packet.
|
||||
delay_history_.clear();
|
||||
packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch();
|
||||
last_seq_no_ = sequence_number;
|
||||
last_timestamp_ = timestamp;
|
||||
first_packet_received_ = true;
|
||||
return absl::nullopt;
|
||||
}
|
||||
|
||||
// Try calculating packet length from current and previous timestamps.
|
||||
int packet_len_ms;
|
||||
if (!IsNewerTimestamp(timestamp, last_timestamp_) ||
|
||||
!IsNewerSequenceNumber(sequence_number, last_seq_no_)) {
|
||||
// Wrong timestamp or sequence order; use stored value.
|
||||
packet_len_ms = packet_len_ms_;
|
||||
} else {
|
||||
// Calculate timestamps per packet and derive packet length in ms.
|
||||
int64_t packet_len_samp =
|
||||
static_cast<uint32_t>(timestamp - last_timestamp_) /
|
||||
static_cast<uint16_t>(sequence_number - last_seq_no_);
|
||||
packet_len_ms =
|
||||
rtc::saturated_cast<int>(1000 * packet_len_samp / sample_rate_hz);
|
||||
}
|
||||
|
||||
bool reordered = false;
|
||||
const int expected_iat_ms =
|
||||
1000 * static_cast<int32_t>(timestamp - last_timestamp_) / sample_rate_hz;
|
||||
const int iat_ms = packet_iat_stopwatch_->ElapsedMs();
|
||||
const int iat_delay_ms = iat_ms - expected_iat_ms;
|
||||
absl::optional<int> relative_delay;
|
||||
if (packet_len_ms > 0) {
|
||||
// Cannot update statistics unless |packet_len_ms| is valid.
|
||||
|
||||
// Inter-arrival time (IAT) in integer "packet times" (rounding down). This
|
||||
// is the value added to the inter-arrival time histogram.
|
||||
int iat_ms = packet_iat_stopwatch_->ElapsedMs();
|
||||
// Check for discontinuous packet sequence and re-ordering.
|
||||
if (IsNewerSequenceNumber(sequence_number, last_seq_no_ + 1)) {
|
||||
// Compensate for gap in the sequence numbers. Reduce IAT with the
|
||||
// expected extra time due to lost packets.
|
||||
int packet_offset =
|
||||
static_cast<uint16_t>(sequence_number - last_seq_no_ - 1);
|
||||
iat_ms -= packet_offset * packet_len_ms;
|
||||
} else if (!IsNewerSequenceNumber(sequence_number, last_seq_no_)) {
|
||||
int packet_offset =
|
||||
static_cast<uint16_t>(last_seq_no_ + 1 - sequence_number);
|
||||
iat_ms += packet_offset * packet_len_ms;
|
||||
reordered = true;
|
||||
}
|
||||
|
||||
int iat_delay = iat_ms - packet_len_ms;
|
||||
if (reordered) {
|
||||
relative_delay = std::max(iat_delay, 0);
|
||||
} else {
|
||||
UpdateDelayHistory(iat_delay, timestamp, sample_rate_hz);
|
||||
relative_delay = CalculateRelativePacketArrivalDelay();
|
||||
}
|
||||
|
||||
const int index = relative_delay.value() / kBucketSizeMs;
|
||||
if (index < histogram_->NumBuckets()) {
|
||||
// Maximum delay to register is 2000 ms.
|
||||
histogram_->Add(index);
|
||||
}
|
||||
// Calculate new |target_level_| based on updated statistics.
|
||||
target_level_ = CalculateTargetLevel();
|
||||
|
||||
LimitTargetLevel();
|
||||
} // End if (packet_len_ms > 0).
|
||||
|
||||
if (enable_rtx_handling_ && reordered &&
|
||||
num_reordered_packets_ < kMaxReorderedPackets) {
|
||||
++num_reordered_packets_;
|
||||
return relative_delay;
|
||||
if (!IsNewerTimestamp(timestamp, last_timestamp_)) {
|
||||
relative_delay = std::max(iat_delay_ms, 0);
|
||||
// Reset the history and restart delay estimation from this packet.
|
||||
delay_history_.clear();
|
||||
} else {
|
||||
UpdateDelayHistory(iat_delay_ms, timestamp, sample_rate_hz);
|
||||
relative_delay = CalculateRelativePacketArrivalDelay();
|
||||
}
|
||||
num_reordered_packets_ = 0;
|
||||
const int index = relative_delay.value() / kBucketSizeMs;
|
||||
if (index < histogram_->NumBuckets()) {
|
||||
// Maximum delay to register is 2000 ms.
|
||||
histogram_->Add(index);
|
||||
}
|
||||
// Calculate new |target_level_ms_| based on updated statistics.
|
||||
int bucket_index = histogram_->Quantile(histogram_quantile_);
|
||||
target_level_ms_ = (1 + bucket_index) * kBucketSizeMs;
|
||||
target_level_ms_ = std::max(target_level_ms_, effective_minimum_delay_ms_);
|
||||
if (maximum_delay_ms_ > 0) {
|
||||
target_level_ms_ = std::min(target_level_ms_, maximum_delay_ms_);
|
||||
}
|
||||
if (packet_len_ms_ > 0) {
|
||||
// Target level should be at least one packet.
|
||||
target_level_ms_ = std::max(target_level_ms_, packet_len_ms_);
|
||||
// Limit to 75% of maximum buffer size.
|
||||
target_level_ms_ = std::min(
|
||||
target_level_ms_, 3 * max_packets_in_buffer_ * packet_len_ms_ / 4);
|
||||
}
|
||||
|
||||
// Prepare for next packet arrival.
|
||||
packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch();
|
||||
last_seq_no_ = sequence_number;
|
||||
last_timestamp_ = timestamp;
|
||||
return relative_delay;
|
||||
}
|
||||
@ -238,128 +198,26 @@ int DelayManager::CalculateRelativePacketArrivalDelay() const {
|
||||
return relative_delay;
|
||||
}
|
||||
|
||||
// Enforces upper and lower limits for |target_level_|. The upper limit is
|
||||
// chosen to be minimum of i) 75% of |max_packets_in_buffer_|, to leave some
|
||||
// headroom for natural fluctuations around the target, and ii) equivalent of
|
||||
// |maximum_delay_ms_| in packets. Note that in practice, if no
|
||||
// |maximum_delay_ms_| is specified, this does not have any impact, since the
|
||||
// target level is far below the buffer capacity in all reasonable cases.
|
||||
// The lower limit is equivalent of |effective_minimum_delay_ms_| in packets.
|
||||
// We update |least_required_level_| while the above limits are applied.
|
||||
// TODO(hlundin): Move this check to the buffer logistics class.
|
||||
void DelayManager::LimitTargetLevel() {
|
||||
if (packet_len_ms_ > 0 && effective_minimum_delay_ms_ > 0) {
|
||||
int minimum_delay_packet_q8 =
|
||||
(effective_minimum_delay_ms_ << 8) / packet_len_ms_;
|
||||
target_level_ = std::max(target_level_, minimum_delay_packet_q8);
|
||||
}
|
||||
|
||||
if (maximum_delay_ms_ > 0 && packet_len_ms_ > 0) {
|
||||
int maximum_delay_packet_q8 = (maximum_delay_ms_ << 8) / packet_len_ms_;
|
||||
target_level_ = std::min(target_level_, maximum_delay_packet_q8);
|
||||
}
|
||||
|
||||
// Shift to Q8, then 75%.;
|
||||
int max_buffer_packets_q8 =
|
||||
static_cast<int>((3 * (max_packets_in_buffer_ << 8)) / 4);
|
||||
target_level_ = std::min(target_level_, max_buffer_packets_q8);
|
||||
|
||||
// Sanity check, at least 1 packet (in Q8).
|
||||
target_level_ = std::max(target_level_, 1 << 8);
|
||||
}
|
||||
|
||||
int DelayManager::CalculateTargetLevel() {
|
||||
int limit_probability = histogram_quantile_;
|
||||
|
||||
int bucket_index = histogram_->Quantile(limit_probability);
|
||||
int target_level = 1;
|
||||
if (packet_len_ms_ > 0) {
|
||||
target_level += bucket_index * kBucketSizeMs / packet_len_ms_;
|
||||
}
|
||||
base_target_level_ = target_level;
|
||||
|
||||
// Sanity check. |target_level| must be strictly positive.
|
||||
target_level = std::max(target_level, 1);
|
||||
// Scale to Q8 and assign to member variable.
|
||||
target_level_ = target_level << 8;
|
||||
return target_level_;
|
||||
}
|
||||
|
||||
int DelayManager::SetPacketAudioLength(int length_ms) {
|
||||
if (length_ms <= 0) {
|
||||
RTC_LOG_F(LS_ERROR) << "length_ms = " << length_ms;
|
||||
return -1;
|
||||
}
|
||||
|
||||
packet_len_ms_ = length_ms;
|
||||
packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch();
|
||||
last_pack_cng_or_dtmf_ = 1; // TODO(hlundin): Legacy. Remove?
|
||||
return 0;
|
||||
}
|
||||
|
||||
void DelayManager::Reset() {
|
||||
packet_len_ms_ = 0; // Packet size unknown.
|
||||
packet_len_ms_ = 0;
|
||||
histogram_->Reset();
|
||||
delay_history_.clear();
|
||||
base_target_level_ = 4;
|
||||
target_level_ = base_target_level_ << 8;
|
||||
target_level_ms_ = kStartDelayMs;
|
||||
packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch();
|
||||
last_pack_cng_or_dtmf_ = 1;
|
||||
first_packet_received_ = false;
|
||||
}
|
||||
|
||||
void DelayManager::ResetPacketIatCount() {
|
||||
packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch();
|
||||
}
|
||||
|
||||
void DelayManager::BufferLimits(int* lower_limit, int* higher_limit) const {
|
||||
BufferLimits(target_level_, lower_limit, higher_limit);
|
||||
}
|
||||
|
||||
// Note that |low_limit| and |higher_limit| are not assigned to
|
||||
// |minimum_delay_ms_| and |maximum_delay_ms_| defined by the client of this
|
||||
// class. They are computed from |target_level| in Q8 and used for decision
|
||||
// making.
|
||||
void DelayManager::BufferLimits(int target_level,
|
||||
int* lower_limit,
|
||||
int* higher_limit) const {
|
||||
if (!lower_limit || !higher_limit) {
|
||||
RTC_LOG_F(LS_ERROR) << "NULL pointers supplied as input";
|
||||
assert(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// |target_level| is in Q8 already.
|
||||
*lower_limit = (target_level * 3) / 4;
|
||||
|
||||
if (packet_len_ms_ > 0) {
|
||||
*lower_limit =
|
||||
std::max(*lower_limit, target_level - kDecelerationTargetLevelOffsetMs /
|
||||
packet_len_ms_);
|
||||
}
|
||||
|
||||
int window_20ms = 0x7FFF; // Default large value for legacy bit-exactness.
|
||||
if (packet_len_ms_ > 0) {
|
||||
window_20ms = (20 << 8) / packet_len_ms_;
|
||||
}
|
||||
// |higher_limit| is equal to |target_level|, but should at
|
||||
// least be 20 ms higher than |lower_limit|.
|
||||
*higher_limit = std::max(target_level, *lower_limit + window_20ms);
|
||||
}
|
||||
|
||||
int DelayManager::TargetLevel() const {
|
||||
return target_level_;
|
||||
}
|
||||
|
||||
void DelayManager::LastDecodedWasCngOrDtmf(bool it_was) {
|
||||
if (it_was) {
|
||||
last_pack_cng_or_dtmf_ = 1;
|
||||
} else if (last_pack_cng_or_dtmf_ != 0) {
|
||||
last_pack_cng_or_dtmf_ = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void DelayManager::RegisterEmptyPacket() {
|
||||
++last_seq_no_;
|
||||
int DelayManager::TargetDelayMs() const {
|
||||
return target_level_ms_;
|
||||
}
|
||||
|
||||
bool DelayManager::IsValidMinimumDelay(int delay_ms) const {
|
||||
@ -409,17 +267,6 @@ int DelayManager::GetBaseMinimumDelay() const {
|
||||
return base_minimum_delay_ms_;
|
||||
}
|
||||
|
||||
int DelayManager::base_target_level() const {
|
||||
return base_target_level_;
|
||||
}
|
||||
int DelayManager::last_pack_cng_or_dtmf() const {
|
||||
return last_pack_cng_or_dtmf_;
|
||||
}
|
||||
|
||||
void DelayManager::set_last_pack_cng_or_dtmf(int value) {
|
||||
last_pack_cng_or_dtmf_ = value;
|
||||
}
|
||||
|
||||
void DelayManager::UpdateEffectiveMinimumDelay() {
|
||||
// Clamp |base_minimum_delay_ms_| into the range which can be effectively
|
||||
// used.
|
||||
@ -432,16 +279,11 @@ void DelayManager::UpdateEffectiveMinimumDelay() {
|
||||
int DelayManager::MinimumDelayUpperBound() const {
|
||||
// Choose the lowest possible bound discarding 0 cases which mean the value
|
||||
// is not set and unconstrained.
|
||||
int q75 = MaxBufferTimeQ75();
|
||||
int q75 = max_packets_in_buffer_ * packet_len_ms_ * 3 / 4;
|
||||
q75 = q75 > 0 ? q75 : kMaxBaseMinimumDelayMs;
|
||||
const int maximum_delay_ms =
|
||||
maximum_delay_ms_ > 0 ? maximum_delay_ms_ : kMaxBaseMinimumDelayMs;
|
||||
return std::min(maximum_delay_ms, q75);
|
||||
}
|
||||
|
||||
int DelayManager::MaxBufferTimeQ75() const {
|
||||
const int max_buffer_time = max_packets_in_buffer_ * packet_len_ms_;
|
||||
return rtc::dchecked_cast<int>(3 * max_buffer_time / 4);
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
Reference in New Issue
Block a user