From 91e6cd2fb3f291444b4433ff2efdaefd718f5bb4 Mon Sep 17 00:00:00 2001 From: Jakob Ivarsson Date: Fri, 17 Feb 2023 15:45:59 +0100 Subject: [PATCH] Use generated_noise_samples to count consecutive expands. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a pure noop refactor that removes duplicated state. It also correctly keeps track of generated samples when transitioning from CNG to expand mode when CNG timeout is used. Bug: webrtc:12790 Change-Id: Ieca559bd771c42566e5d4f8837235cb25b1420bf Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/293862 Commit-Queue: Jakob Ivarsson‎ Reviewed-by: Henrik Lundin Cr-Commit-Position: refs/heads/main@{#39334} --- modules/audio_coding/neteq/decision_logic.cc | 52 ++++++++++---------- modules/audio_coding/neteq/decision_logic.h | 21 +++----- 2 files changed, 32 insertions(+), 41 deletions(-) diff --git a/modules/audio_coding/neteq/decision_logic.cc b/modules/audio_coding/neteq/decision_logic.cc index 91b0252d2c..a52c3e9599 100644 --- a/modules/audio_coding/neteq/decision_logic.cc +++ b/modules/audio_coding/neteq/decision_logic.cc @@ -34,7 +34,7 @@ namespace { constexpr int kPostponeDecodingLevel = 50; constexpr int kTargetLevelWindowMs = 100; -constexpr int kMaxWaitForPacketTicks = 10; +constexpr int kMaxWaitForPacketMs = 100; // The granularity of delay adjustments (accelerate/preemptive expand) is 15ms, // but round up since the clock has a granularity of 10ms. constexpr int kDelayAdjustmentGranularityMs = 20; @@ -69,7 +69,7 @@ bool IsExpand(NetEq::Mode mode) { DecisionLogic::Config::Config() { StructParametersParser::Create( "enable_stable_playout_delay", &enable_stable_playout_delay, // - "reinit_after_expands", &reinit_after_expands, // + "reinit_after_expand_ms", &reinit_after_expand_ms, // "packet_history_size_ms", &packet_history_size_ms, // "cng_timeout_ms", &cng_timeout_ms, // "deceleration_target_level_offset_ms", @@ -79,7 +79,7 @@ DecisionLogic::Config::Config() { RTC_LOG(LS_INFO) << "NetEq decision logic config:" << " enable_stable_playout_delay=" << enable_stable_playout_delay - << " reinit_after_expands=" << reinit_after_expands + << " reinit_after_expand_ms=" << reinit_after_expand_ms << " packet_history_size_ms=" << packet_history_size_ms << " cng_timeout_ms=" << cng_timeout_ms.value_or(-1) << " deceleration_target_level_offset_ms=" @@ -138,12 +138,6 @@ NetEq::Operation DecisionLogic::GetDecision(const NetEqStatus& status, cng_state_ = kCngInternalOn; } - if (IsExpand(status.last_mode)) { - ++num_consecutive_expands_; - } else { - num_consecutive_expands_ = 0; - } - if (!IsExpand(status.last_mode) && !IsCng(status.last_mode)) { last_playout_delay_ms_ = GetPlayoutDelayMs(status); } @@ -177,7 +171,10 @@ NetEq::Operation DecisionLogic::GetDecision(const NetEqStatus& status, // If the expand period was very long, reset NetEQ since it is likely that the // sender was restarted. - if (num_consecutive_expands_ > config_.reinit_after_expands) { + if (IsExpand(status.last_mode) && + status.generated_noise_samples > + static_cast(config_.reinit_after_expand_ms * + sample_rate_khz_)) { *reset_decoder = true; return NetEq::Operation::kNormal; } @@ -215,10 +212,6 @@ NetEq::Operation DecisionLogic::GetDecision(const NetEqStatus& status, return NetEq::Operation::kUndefined; } -void DecisionLogic::NotifyMutedState() { - ++num_consecutive_expands_; -} - int DecisionLogic::TargetLevelMs() const { int target_delay_ms = delay_manager_->TargetDelayMs(); if (!config_.enable_stable_playout_delay) { @@ -438,30 +431,35 @@ bool DecisionLogic::UnderTargetLevel() const { TargetLevelMs() * sample_rate_khz_; } -bool DecisionLogic::ReinitAfterExpands(uint32_t timestamp_leap) const { - return timestamp_leap >= static_cast(output_size_samples_ * - config_.reinit_after_expands); +bool DecisionLogic::ReinitAfterExpands( + NetEqController::NetEqStatus status) const { + const uint32_t timestamp_leap = + status.next_packet->timestamp - status.target_timestamp; + return timestamp_leap >= + static_cast(config_.reinit_after_expand_ms * + sample_rate_khz_); } -bool DecisionLogic::PacketTooEarly(uint32_t timestamp_leap) const { - return timestamp_leap > - static_cast(output_size_samples_ * num_consecutive_expands_); +bool DecisionLogic::PacketTooEarly(NetEqController::NetEqStatus status) const { + const uint32_t timestamp_leap = + status.next_packet->timestamp - status.target_timestamp; + return timestamp_leap > status.generated_noise_samples; } -bool DecisionLogic::MaxWaitForPacket() const { - return num_consecutive_expands_ >= kMaxWaitForPacketTicks; +bool DecisionLogic::MaxWaitForPacket( + NetEqController::NetEqStatus status) const { + return status.generated_noise_samples >= + static_cast(kMaxWaitForPacketMs * sample_rate_khz_); } bool DecisionLogic::ShouldContinueExpand( NetEqController::NetEqStatus status) const { - uint32_t timestamp_leap = - status.next_packet->timestamp - status.target_timestamp; if (config_.enable_stable_playout_delay) { return GetNextPacketDelayMs(status) < HighThreshold() && - PacketTooEarly(timestamp_leap); + PacketTooEarly(status); } - return !ReinitAfterExpands(timestamp_leap) && !MaxWaitForPacket() && - PacketTooEarly(timestamp_leap) && UnderTargetLevel(); + return !ReinitAfterExpands(status) && !MaxWaitForPacket(status) && + PacketTooEarly(status) && UnderTargetLevel(); } int DecisionLogic::GetNextPacketDelayMs( diff --git a/modules/audio_coding/neteq/decision_logic.h b/modules/audio_coding/neteq/decision_logic.h index 8d1ff4d622..41c62c4dde 100644 --- a/modules/audio_coding/neteq/decision_logic.h +++ b/modules/audio_coding/neteq/decision_logic.h @@ -80,8 +80,6 @@ class DecisionLogic : public NetEqController { void RegisterEmptyPacket() override {} - void NotifyMutedState() override; - bool SetMaximumDelay(int delay_ms) override { return delay_manager_->SetMaximumDelay(delay_ms); } @@ -145,19 +143,15 @@ class DecisionLogic : public NetEqController { // Checks if the current (filtered) buffer level is under the target level. bool UnderTargetLevel() const; - // Checks if `timestamp_leap` is so long into the future that a reset due - // to exceeding kReinitAfterExpands will be done. - bool ReinitAfterExpands(uint32_t timestamp_leap) const; + // Checks if the timestamp leap is so long into the future that a reset due + // to exceeding `reinit_after_expand_ms` will be done. + bool ReinitAfterExpands(NetEqController::NetEqStatus status) const; // Checks if we still have not done enough expands to cover the distance from - // the last decoded packet to the next available packet, the distance beeing - // conveyed in `timestamp_leap`. - bool PacketTooEarly(uint32_t timestamp_leap) const; - - bool MaxWaitForPacket() const; - + // the last decoded packet to the next available packet. + bool PacketTooEarly(NetEqController::NetEqStatus status) const; + bool MaxWaitForPacket(NetEqController::NetEqStatus status) const; bool ShouldContinueExpand(NetEqController::NetEqStatus status) const; - int GetNextPacketDelayMs(NetEqController::NetEqStatus status) const; int GetPlayoutDelayMs(NetEqController::NetEqStatus status) const; @@ -172,7 +166,7 @@ class DecisionLogic : public NetEqController { Config(); bool enable_stable_playout_delay = false; - int reinit_after_expands = 100; + int reinit_after_expand_ms = 1000; int deceleration_target_level_offset_ms = 85; int packet_history_size_ms = 2000; absl::optional cng_timeout_ms; @@ -192,7 +186,6 @@ class DecisionLogic : public NetEqController { bool prev_time_scale_ = false; bool disallow_time_stretching_; std::unique_ptr timescale_countdown_; - int num_consecutive_expands_ = 0; int time_stretched_cn_samples_ = 0; bool buffer_flush_ = false; int last_playout_delay_ms_ = 0;