Use generated_noise_samples to count consecutive expands.

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‎ <jakobi@webrtc.org>
Reviewed-by: Henrik Lundin <henrik.lundin@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#39334}
diff --git a/modules/audio_coding/neteq/decision_logic.cc b/modules/audio_coding/neteq/decision_logic.cc
index 91b0252..a52c3e9 100644
--- a/modules/audio_coding/neteq/decision_logic.cc
+++ b/modules/audio_coding/neteq/decision_logic.cc
@@ -34,7 +34,7 @@
 
 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 @@
 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 @@
   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 @@
     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 @@
 
   // 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<size_t>(config_.reinit_after_expand_ms *
+                              sample_rate_khz_)) {
     *reset_decoder = true;
     return NetEq::Operation::kNormal;
   }
@@ -215,10 +212,6 @@
   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 @@
          TargetLevelMs() * sample_rate_khz_;
 }
 
-bool DecisionLogic::ReinitAfterExpands(uint32_t timestamp_leap) const {
-  return timestamp_leap >= static_cast<uint32_t>(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<uint32_t>(config_.reinit_after_expand_ms *
+                               sample_rate_khz_);
 }
 
-bool DecisionLogic::PacketTooEarly(uint32_t timestamp_leap) const {
-  return timestamp_leap >
-         static_cast<uint32_t>(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<size_t>(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 8d1ff4d..41c62c4 100644
--- a/modules/audio_coding/neteq/decision_logic.h
+++ b/modules/audio_coding/neteq/decision_logic.h
@@ -80,8 +80,6 @@
 
   void RegisterEmptyPacket() override {}
 
-  void NotifyMutedState() override;
-
   bool SetMaximumDelay(int delay_ms) override {
     return delay_manager_->SetMaximumDelay(delay_ms);
   }
@@ -145,19 +143,15 @@
   // 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 @@
     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<int> cng_timeout_ms;
@@ -192,7 +186,6 @@
   bool prev_time_scale_ = false;
   bool disallow_time_stretching_;
   std::unique_ptr<TickTimer::Countdown> timescale_countdown_;
-  int num_consecutive_expands_ = 0;
   int time_stretched_cn_samples_ = 0;
   bool buffer_flush_ = false;
   int last_playout_delay_ms_ = 0;