Refactor DelayManager into separate Histogram class and make it injectable for testing purposes.

Change-Id: I98aa3f992169e598fc1a3dd850400183395fe1fe
Bug: webrtc:10333
Reviewed-on: https://webrtc-review.googlesource.com/c/123445
Commit-Queue: Jakob Ivarsson‎ <jakobi@webrtc.org>
Reviewed-by: Minyue Li <minyue@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26797}
diff --git a/modules/audio_coding/neteq/delay_manager_unittest.cc b/modules/audio_coding/neteq/delay_manager_unittest.cc
index 19b99af..b3797e2 100644
--- a/modules/audio_coding/neteq/delay_manager_unittest.cc
+++ b/modules/audio_coding/neteq/delay_manager_unittest.cc
@@ -14,7 +14,10 @@
 
 #include <math.h>
 
+#include "absl/memory/memory.h"
+#include "modules/audio_coding/neteq/histogram.h"
 #include "modules/audio_coding/neteq/mock/mock_delay_peak_detector.h"
+#include "modules/audio_coding/neteq/mock/mock_histogram.h"
 #include "rtc_base/checks.h"
 #include "test/field_trial.h"
 #include "test/gmock.h"
@@ -46,6 +49,8 @@
   std::unique_ptr<DelayManager> dm_;
   TickTimer tick_timer_;
   MockDelayPeakDetector detector_;
+  bool use_mock_histogram_ = false;
+  MockHistogram* mock_histogram_;
   uint16_t seq_no_;
   uint32_t ts_;
   bool enable_rtx_handling_ = false;
@@ -63,8 +68,18 @@
 
 void DelayManagerTest::RecreateDelayManager() {
   EXPECT_CALL(detector_, Reset()).Times(1);
+  std::unique_ptr<Histogram> histogram;
+  static const int kMaxIat = 64;
+  static const int kForgetFactor = 32745;
+  if (use_mock_histogram_) {
+    mock_histogram_ = new MockHistogram(kMaxIat, kForgetFactor);
+    histogram.reset(mock_histogram_);
+  } else {
+    histogram = absl::make_unique<Histogram>(kMaxIat, kForgetFactor);
+  }
   dm_.reset(new DelayManager(kMaxNumberOfPackets, kMinDelayMs,
-                             enable_rtx_handling_, &detector_, &tick_timer_));
+                             enable_rtx_handling_, &detector_, &tick_timer_,
+                             std::move(histogram)));
 }
 
 void DelayManagerTest::SetPacketAudioLength(int lengt_ms) {
@@ -92,17 +107,6 @@
   // object.
 }
 
-TEST_F(DelayManagerTest, VectorInitialization) {
-  const DelayManager::IATVector& vec = dm_->iat_vector();
-  double sum = 0.0;
-  for (size_t i = 0; i < vec.size(); i++) {
-    EXPECT_NEAR(ldexp(pow(0.5, static_cast<int>(i + 1)), 30), vec[i], 65537);
-    // Tolerance 65537 in Q30 corresponds to a delta of approximately 0.00006.
-    sum += vec[i];
-  }
-  EXPECT_EQ(1 << 30, static_cast<int>(sum));  // Should be 1 in Q30.
-}
-
 TEST_F(DelayManagerTest, SetPacketAudioLength) {
   const int kLengthMs = 30;
   // Expect DelayManager to pass on the new length to the detector object.
@@ -477,26 +481,26 @@
 
 TEST_F(DelayManagerTest, EnableRtxHandling) {
   enable_rtx_handling_ = true;
+  use_mock_histogram_ = true;
   RecreateDelayManager();
+  EXPECT_TRUE(mock_histogram_);
 
   // Insert first packet.
   SetPacketAudioLength(kFrameSizeMs);
   InsertNextPacket();
 
   // Insert reordered packet.
-  // TODO(jakobi): Test estimated inter-arrival time by mocking the histogram
-  // instead of checking the call to the peak detector.
-  EXPECT_CALL(detector_, Update(3, true, _));
+  EXPECT_CALL(*mock_histogram_, Add(3));
   EXPECT_EQ(0, dm_->Update(seq_no_ - 3, ts_ - 3 * kFrameSizeMs, kFs));
 
   // Insert another reordered packet.
-  EXPECT_CALL(detector_, Update(2, true, _));
+  EXPECT_CALL(*mock_histogram_, Add(2));
   EXPECT_EQ(0, dm_->Update(seq_no_ - 2, ts_ - 2 * kFrameSizeMs, kFs));
 
   // Insert the next packet in order and verify that the inter-arrival time is
   // estimated correctly.
   IncreaseTime(kFrameSizeMs);
-  EXPECT_CALL(detector_, Update(1, false, _));
+  EXPECT_CALL(*mock_histogram_, Add(1));
   InsertNextPacket();
 }
 
@@ -573,7 +577,7 @@
   test::ScopedFieldTrials field_trial(
       "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-0/");
   RecreateDelayManager();
-  EXPECT_EQ(absl::make_optional<int>(1 << 30),
+  EXPECT_EQ(absl::make_optional<int>(0),
             dm_->forced_limit_probability_for_test());
 
   SetPacketAudioLength(kFrameSizeMs);
@@ -595,14 +599,14 @@
     test::ScopedFieldTrials field_trial(
         "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-95/");
     RecreateDelayManager();
-    EXPECT_EQ(absl::make_optional<int>(53687091),
+    EXPECT_EQ(absl::make_optional<int>(1020054733),
               dm_->forced_limit_probability_for_test());  // 1/20 in Q30
   }
   {
     test::ScopedFieldTrials field_trial(
         "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-99.95/");
     RecreateDelayManager();
-    EXPECT_EQ(absl::make_optional<int>(536871),
+    EXPECT_EQ(absl::make_optional<int>(1073204953),
               dm_->forced_limit_probability_for_test());  // 1/2000 in Q30
   }
   {
@@ -624,116 +628,4 @@
   }
 }
 
-// Test if the histogram is stretched correctly if the packet size is decreased.
-TEST(DelayManagerIATScalingTest, StretchTest) {
-  using IATVector = DelayManager::IATVector;
-  // Test a straightforward 60ms to 20ms change.
-  IATVector iat = {12, 0, 0, 0, 0, 0};
-  IATVector expected_result = {4, 4, 4, 0, 0, 0};
-  IATVector stretched_iat = DelayManager::ScaleHistogram(iat, 60, 20);
-  EXPECT_EQ(stretched_iat, expected_result);
-
-  // Test an example where the last bin in the stretched histogram should
-  // contain the sum of the elements that don't fit into the new histogram.
-  iat = {18, 15, 12, 9, 6, 3, 0};
-  expected_result = {6, 6, 6, 5, 5, 5, 30};
-  stretched_iat = DelayManager::ScaleHistogram(iat, 60, 20);
-  EXPECT_EQ(stretched_iat, expected_result);
-
-  // Test a 120ms to 60ms change.
-  iat = {18, 16, 14, 4, 0};
-  expected_result = {9, 9, 8, 8, 18};
-  stretched_iat = DelayManager::ScaleHistogram(iat, 120, 60);
-  EXPECT_EQ(stretched_iat, expected_result);
-
-  // Test a 120ms to 20ms change.
-  iat = {19, 12, 0, 0, 0, 0, 0, 0};
-  expected_result = {3, 3, 3, 3, 3, 3, 2, 11};
-  stretched_iat = DelayManager::ScaleHistogram(iat, 120, 20);
-  EXPECT_EQ(stretched_iat, expected_result);
-
-  // Test a 70ms to 40ms change.
-  iat = {13, 7, 5, 3, 1, 5, 12, 11, 3, 0, 0, 0};
-  expected_result = {7, 5, 5, 3, 3, 2, 2, 1, 2, 2, 6, 22};
-  stretched_iat = DelayManager::ScaleHistogram(iat, 70, 40);
-  EXPECT_EQ(stretched_iat, expected_result);
-
-  // Test a 30ms to 20ms change.
-  iat = {13, 7, 5, 3, 1, 5, 12, 11, 3, 0, 0, 0};
-  expected_result = {8, 6, 6, 3, 2, 2, 1, 3, 3, 8, 7, 11};
-  stretched_iat = DelayManager::ScaleHistogram(iat, 30, 20);
-  EXPECT_EQ(stretched_iat, expected_result);
-}
-
-// Test if the histogram is compressed correctly if the packet size is
-// increased.
-TEST(DelayManagerIATScalingTest, CompressionTest) {
-  using IATVector = DelayManager::IATVector;
-  // Test a 20 to 60 ms change.
-  IATVector iat = {12, 11, 10, 3, 2, 1};
-  IATVector expected_result = {33, 6, 0, 0, 0, 0};
-  IATVector compressed_iat = DelayManager::ScaleHistogram(iat, 20, 60);
-  EXPECT_EQ(compressed_iat, expected_result);
-
-  // Test a 60ms to 120ms change.
-  iat = {18, 16, 14, 4, 1};
-  expected_result = {34, 18, 1, 0, 0};
-  compressed_iat = DelayManager::ScaleHistogram(iat, 60, 120);
-  EXPECT_EQ(compressed_iat, expected_result);
-
-  // Test a 20ms to 120ms change.
-  iat = {18, 12, 5, 4, 4, 3, 5, 1};
-  expected_result = {46, 6, 0, 0, 0, 0, 0, 0};
-  compressed_iat = DelayManager::ScaleHistogram(iat, 20, 120);
-  EXPECT_EQ(compressed_iat, expected_result);
-
-  // Test a 70ms to 80ms change.
-  iat = {13, 7, 5, 3, 1, 5, 12, 11, 3};
-  expected_result = {11, 8, 6, 2, 5, 12, 13, 3, 0};
-  compressed_iat = DelayManager::ScaleHistogram(iat, 70, 80);
-  EXPECT_EQ(compressed_iat, expected_result);
-
-  // Test a 50ms to 110ms change.
-  iat = {13, 7, 5, 3, 1, 5, 12, 11, 3};
-  expected_result = {18, 8, 16, 16, 2, 0, 0, 0, 0};
-  compressed_iat = DelayManager::ScaleHistogram(iat, 50, 110);
-  EXPECT_EQ(compressed_iat, expected_result);
-}
-
-// Test if the histogram scaling function handles overflows correctly.
-TEST(DelayManagerIATScalingTest, OverflowTest) {
-  using IATVector = DelayManager::IATVector;
-  // Test a compression operation that can cause overflow.
-  IATVector iat = {733544448, 0, 0, 0, 0, 0, 0, 340197376, 0, 0, 0, 0, 0, 0};
-  IATVector expected_result = {733544448, 340197376, 0, 0, 0, 0, 0,
-                               0,         0,         0, 0, 0, 0, 0};
-  IATVector scaled_iat = DelayManager::ScaleHistogram(iat, 10, 60);
-  EXPECT_EQ(scaled_iat, expected_result);
-
-  iat = {655591163, 39962288, 360736736, 1930514, 4003853, 1782764,
-         114119,    2072996,  0,         2149354, 0};
-  expected_result = {1056290187, 7717131, 2187115, 2149354, 0, 0,
-                     0,          0,       0,       0,       0};
-  scaled_iat = DelayManager::ScaleHistogram(iat, 20, 60);
-  EXPECT_EQ(scaled_iat, expected_result);
-
-  // In this test case we will not be able to add everything to the final bin in
-  // the scaled histogram. Check that the last bin doesn't overflow.
-  iat = {2000000000, 2000000000, 2000000000,
-         2000000000, 2000000000, 2000000000};
-  expected_result = {666666666, 666666666, 666666666,
-                     666666667, 666666667, 2147483647};
-  scaled_iat = DelayManager::ScaleHistogram(iat, 60, 20);
-  EXPECT_EQ(scaled_iat, expected_result);
-
-  // In this test case we will not be able to add enough to each of the bins,
-  // so the values should be smeared out past the end of the normal range.
-  iat = {2000000000, 2000000000, 2000000000,
-         2000000000, 2000000000, 2000000000};
-  expected_result = {2147483647, 2147483647, 2147483647,
-                     2147483647, 2147483647, 1262581765};
-  scaled_iat = DelayManager::ScaleHistogram(iat, 20, 60);
-  EXPECT_EQ(scaled_iat, expected_result);
-}
-
 }  // namespace webrtc