NetEq: Use TickTimer in DelayManager

This change replaces packet_iat_count_ms_ and max_timer_ms_, two
time-counting member variables in DelayManager, with Stopwatch objects
obtained from a TickTimer.

BUG=webrtc:5608

Review-Url: https://codereview.webrtc.org/1929863002
Cr-Commit-Position: refs/heads/master@{#12554}
diff --git a/webrtc/modules/audio_coding/neteq/decision_logic.cc b/webrtc/modules/audio_coding/neteq/decision_logic.cc
index 39bb466..b702e6d 100644
--- a/webrtc/modules/audio_coding/neteq/decision_logic.cc
+++ b/webrtc/modules/audio_coding/neteq/decision_logic.cc
@@ -152,10 +152,6 @@
 
 void DecisionLogic::FilterBufferLevel(size_t buffer_size_samples,
                                       Modes prev_mode) {
-  const int elapsed_time_ms =
-      static_cast<int>(output_size_samples_ / (8 * fs_mult_));
-  delay_manager_->UpdateCounters(elapsed_time_ms);
-
   // Do not update buffer history if currently playing CNG since it will bias
   // the filtered buffer level.
   if ((prev_mode != kModeRfc3389Cng) && (prev_mode != kModeCodecInternalCng)) {
diff --git a/webrtc/modules/audio_coding/neteq/decision_logic_unittest.cc b/webrtc/modules/audio_coding/neteq/decision_logic_unittest.cc
index 350821c..7165d93 100644
--- a/webrtc/modules/audio_coding/neteq/decision_logic_unittest.cc
+++ b/webrtc/modules/audio_coding/neteq/decision_logic_unittest.cc
@@ -28,7 +28,7 @@
   TickTimer tick_timer;
   PacketBuffer packet_buffer(10, &tick_timer);
   DelayPeakDetector delay_peak_detector(&tick_timer);
-  DelayManager delay_manager(240, &delay_peak_detector);
+  DelayManager delay_manager(240, &delay_peak_detector, &tick_timer);
   BufferLevelFilter buffer_level_filter;
   DecisionLogic* logic = DecisionLogic::Create(fs_hz, output_size_samples,
                                                kPlayoutOn, &decoder_database,
diff --git a/webrtc/modules/audio_coding/neteq/delay_manager.cc b/webrtc/modules/audio_coding/neteq/delay_manager.cc
index e955f17..84bda7c 100644
--- a/webrtc/modules/audio_coding/neteq/delay_manager.cc
+++ b/webrtc/modules/audio_coding/neteq/delay_manager.cc
@@ -24,12 +24,13 @@
 namespace webrtc {
 
 DelayManager::DelayManager(size_t max_packets_in_buffer,
-                           DelayPeakDetector* peak_detector)
+                           DelayPeakDetector* peak_detector,
+                           const TickTimer* tick_timer)
     : first_packet_received_(false),
       max_packets_in_buffer_(max_packets_in_buffer),
       iat_vector_(kMaxIat + 1, 0),
       iat_factor_(0),
-      packet_iat_count_ms_(0),
+      tick_timer_(tick_timer),
       base_target_level_(4),  // In Q0 domain.
       target_level_(base_target_level_ << 8),  // In Q8 domain.
       packet_len_ms_(0),
@@ -41,7 +42,6 @@
       maximum_delay_ms_(target_level_),
       iat_cumulative_sum_(0),
       max_iat_cumulative_sum_(0),
-      max_timer_ms_(0),
       peak_detector_(*peak_detector),
       last_pack_cng_or_dtmf_(1) {
   assert(peak_detector);  // Should never be NULL.
@@ -79,7 +79,7 @@
 
   if (!first_packet_received_) {
     // Prepare for next packet arrival.
-    packet_iat_count_ms_ = 0;
+    packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch();
     last_seq_no_ = sequence_number;
     last_timestamp_ = timestamp;
     first_packet_received_ = true;
@@ -106,7 +106,7 @@
     // Calculate inter-arrival time (IAT) in integer "packet times"
     // (rounding down). This is the value used as index to the histogram
     // vector |iat_vector_|.
-    int iat_packets = packet_iat_count_ms_ / packet_len_ms;
+    int iat_packets = packet_iat_stopwatch_->ElapsedMs() / packet_len_ms;
 
     if (streaming_mode_) {
       UpdateCumulativeSums(packet_len_ms, sequence_number);
@@ -137,7 +137,7 @@
   }  // End if (packet_len_ms > 0).
 
   // Prepare for next packet arrival.
-  packet_iat_count_ms_ = 0;
+  packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch();
   last_seq_no_ = sequence_number;
   last_timestamp_ = timestamp;
   return 0;
@@ -147,7 +147,8 @@
                                         uint16_t sequence_number) {
   // Calculate IAT in Q8, including fractions of a packet (i.e., more
   // accurate than |iat_packets|.
-  int iat_packets_q8 = (packet_iat_count_ms_ << 8) / packet_len_ms;
+  int iat_packets_q8 =
+      (packet_iat_stopwatch_->ElapsedMs() << 8) / packet_len_ms;
   // Calculate cumulative sum IAT with sequence number compensation. The sum
   // is zero if there is no clock-drift.
   iat_cumulative_sum_ += (iat_packets_q8 -
@@ -159,9 +160,9 @@
   if (iat_cumulative_sum_ > max_iat_cumulative_sum_) {
     // Found a new maximum.
     max_iat_cumulative_sum_ = iat_cumulative_sum_;
-    max_timer_ms_ = 0;
+    max_iat_stopwatch_ = tick_timer_->GetNewStopwatch();
   }
-  if (max_timer_ms_ > kMaxStreamingPeakPeriodMs) {
+  if (max_iat_stopwatch_->ElapsedMs() > kMaxStreamingPeakPeriodMs) {
     // Too long since the last maximum was observed; decrease max value.
     max_iat_cumulative_sum_ -= kCumulativeSumDrift;
   }
@@ -299,7 +300,7 @@
   }
   packet_len_ms_ = length_ms;
   peak_detector_.SetPacketAudioLength(packet_len_ms_);
-  packet_iat_count_ms_ = 0;
+  packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch();
   last_pack_cng_or_dtmf_ = 1;  // TODO(hlundin): Legacy. Remove?
   return 0;
 }
@@ -311,8 +312,8 @@
   peak_detector_.Reset();
   ResetHistogram();  // Resets target levels too.
   iat_factor_ = 0;  // Adapt the histogram faster for the first few packets.
-  packet_iat_count_ms_ = 0;
-  max_timer_ms_ = 0;
+  packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch();
+  max_iat_stopwatch_ = tick_timer_->GetNewStopwatch();
   iat_cumulative_sum_ = 0;
   max_iat_cumulative_sum_ = 0;
   last_pack_cng_or_dtmf_ = 1;
@@ -340,13 +341,10 @@
   return peak_detector_.peak_found();
 }
 
-void DelayManager::UpdateCounters(int elapsed_time_ms) {
-  packet_iat_count_ms_ += elapsed_time_ms;
-  max_timer_ms_ += elapsed_time_ms;
+void DelayManager::ResetPacketIatCount() {
+  packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch();
 }
 
-void DelayManager::ResetPacketIatCount() { packet_iat_count_ms_ = 0; }
-
 // 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_| and used for decision making.
diff --git a/webrtc/modules/audio_coding/neteq/delay_manager.h b/webrtc/modules/audio_coding/neteq/delay_manager.h
index 785fced..6f3c14a 100644
--- a/webrtc/modules/audio_coding/neteq/delay_manager.h
+++ b/webrtc/modules/audio_coding/neteq/delay_manager.h
@@ -13,10 +13,12 @@
 
 #include <string.h>  // Provide access to size_t.
 
+#include <memory>
 #include <vector>
 
 #include "webrtc/base/constructormagic.h"
 #include "webrtc/modules/audio_coding/neteq/audio_decoder_impl.h"
+#include "webrtc/modules/audio_coding/neteq/tick_timer.h"
 #include "webrtc/typedefs.h"
 
 namespace webrtc {
@@ -32,7 +34,9 @@
   // buffer can hold no more than |max_packets_in_buffer| packets (i.e., this
   // is the number of packet slots in the buffer). Supply a PeakDetector
   // object to the DelayManager.
-  DelayManager(size_t max_packets_in_buffer, DelayPeakDetector* peak_detector);
+  DelayManager(size_t max_packets_in_buffer,
+               DelayPeakDetector* peak_detector,
+               const TickTimer* tick_timer);
 
   virtual ~DelayManager();
 
@@ -75,10 +79,6 @@
   // DelayPeakDetector object.
   virtual bool PeakFound() const;
 
-  // Notifies the counters in DelayManager and DelayPeakDetector that
-  // |elapsed_time_ms| have elapsed.
-  virtual void UpdateCounters(int elapsed_time_ms);
-
   // Reset the inter-arrival time counter to 0.
   virtual void ResetPacketIatCount();
 
@@ -135,7 +135,9 @@
   const size_t max_packets_in_buffer_;  // Capacity of the packet buffer.
   IATVector iat_vector_;  // Histogram of inter-arrival times.
   int iat_factor_;  // Forgetting factor for updating the IAT histogram (Q15).
-  int packet_iat_count_ms_;  // Milliseconds elapsed since last packet.
+  const TickTimer* tick_timer_;
+  // Time elapsed since last packet.
+  std::unique_ptr<TickTimer::Stopwatch> packet_iat_stopwatch_;
   int base_target_level_;   // Currently preferred buffer level before peak
                             // detection and streaming mode (Q0).
   // TODO(turajs) change the comment according to the implementation of
@@ -153,7 +155,8 @@
   int maximum_delay_ms_;  // Externally set maximum allowed delay.
   int iat_cumulative_sum_;  // Cumulative sum of delta inter-arrival times.
   int max_iat_cumulative_sum_;  // Max of |iat_cumulative_sum_|.
-  int max_timer_ms_;  // Time elapsed since maximum was observed.
+  // Time elapsed since maximum was observed.
+  std::unique_ptr<TickTimer::Stopwatch> max_iat_stopwatch_;
   DelayPeakDetector& peak_detector_;
   int last_pack_cng_or_dtmf_;
 
diff --git a/webrtc/modules/audio_coding/neteq/delay_manager_unittest.cc b/webrtc/modules/audio_coding/neteq/delay_manager_unittest.cc
index 05d6f3e..3290e9c 100644
--- a/webrtc/modules/audio_coding/neteq/delay_manager_unittest.cc
+++ b/webrtc/modules/audio_coding/neteq/delay_manager_unittest.cc
@@ -51,7 +51,7 @@
 void DelayManagerTest::SetUp() {
   EXPECT_CALL(detector_, Reset())
             .Times(1);
-  dm_ = new DelayManager(kMaxNumberOfPackets, &detector_);
+  dm_ = new DelayManager(kMaxNumberOfPackets, &detector_, &tick_timer_);
 }
 
 void DelayManagerTest::SetPacketAudioLength(int lengt_ms) {
@@ -67,7 +67,6 @@
 
 void DelayManagerTest::IncreaseTime(int inc_ms) {
   for (int t = 0; t < inc_ms; t += kTimeStepMs) {
-    dm_->UpdateCounters(kTimeStepMs);
     tick_timer_.Increment();
   }
 }
diff --git a/webrtc/modules/audio_coding/neteq/mock/mock_delay_manager.h b/webrtc/modules/audio_coding/neteq/mock/mock_delay_manager.h
index 6fb8585..7ceea70 100644
--- a/webrtc/modules/audio_coding/neteq/mock/mock_delay_manager.h
+++ b/webrtc/modules/audio_coding/neteq/mock/mock_delay_manager.h
@@ -20,8 +20,9 @@
 class MockDelayManager : public DelayManager {
  public:
   MockDelayManager(size_t max_packets_in_buffer,
-                   DelayPeakDetector* peak_detector)
-      : DelayManager(max_packets_in_buffer, peak_detector) {}
+                   DelayPeakDetector* peak_detector,
+                   const TickTimer* tick_timer)
+      : DelayManager(max_packets_in_buffer, peak_detector, tick_timer) {}
   virtual ~MockDelayManager() { Die(); }
   MOCK_METHOD0(Die, void());
   MOCK_CONST_METHOD0(iat_vector,
diff --git a/webrtc/modules/audio_coding/neteq/neteq_impl.cc b/webrtc/modules/audio_coding/neteq/neteq_impl.cc
index 7bdb23c..ef1e6cb 100644
--- a/webrtc/modules/audio_coding/neteq/neteq_impl.cc
+++ b/webrtc/modules/audio_coding/neteq/neteq_impl.cc
@@ -60,7 +60,8 @@
       decoder_database(new DecoderDatabase),
       delay_peak_detector(new DelayPeakDetector(tick_timer.get())),
       delay_manager(new DelayManager(config.max_packets_in_buffer,
-                                     delay_peak_detector.get())),
+                                     delay_peak_detector.get(),
+                                     tick_timer.get())),
       dtmf_buffer(new DtmfBuffer(config.sample_rate_hz)),
       dtmf_tone_generator(new DtmfToneGenerator),
       packet_buffer(
diff --git a/webrtc/modules/audio_coding/neteq/neteq_impl_unittest.cc b/webrtc/modules/audio_coding/neteq/neteq_impl_unittest.cc
index ed6dc20..42f2c1e 100644
--- a/webrtc/modules/audio_coding/neteq/neteq_impl_unittest.cc
+++ b/webrtc/modules/audio_coding/neteq/neteq_impl_unittest.cc
@@ -91,7 +91,7 @@
 
     if (use_mock_delay_manager_) {
       std::unique_ptr<MockDelayManager> mock(new MockDelayManager(
-          config_.max_packets_in_buffer, delay_peak_detector_));
+          config_.max_packets_in_buffer, delay_peak_detector_, tick_timer_));
       mock_delay_manager_ = mock.get();
       EXPECT_CALL(*mock_delay_manager_, set_streaming_mode(false)).Times(1);
       deps.delay_manager = std::move(mock);