Use SimulatedClock in OveruseFrameDetectorTests

Also converts to TimeDeltas over ints.

Bug: webrtc:42223992, webrtc:42223979
Change-Id: I59a696bf47f631ccfaced47098b05751685fc4ab
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/407020
Reviewed-by: Henrik Boström <hbos@webrtc.org>
Auto-Submit: Evan Shrubsole <eshr@webrtc.org>
Commit-Queue: Henrik Boström <hbos@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#45528}
diff --git a/video/adaptation/BUILD.gn b/video/adaptation/BUILD.gn
index 389a11e..2c2b5d8 100644
--- a/video/adaptation/BUILD.gn
+++ b/video/adaptation/BUILD.gn
@@ -117,6 +117,8 @@
       "../../rtc_base:task_queue_for_test",
       "../../rtc_base:threading",
       "../../rtc_base:timeutils",
+      "../../system_wrappers",
+      "../../test:create_test_environment",
       "../../test:rtc_expect_death",
       "../../test:test_support",
       "../../test/time_controller",
diff --git a/video/adaptation/overuse_frame_detector_unittest.cc b/video/adaptation/overuse_frame_detector_unittest.cc
index 30fa879..d474e97 100644
--- a/video/adaptation/overuse_frame_detector_unittest.cc
+++ b/video/adaptation/overuse_frame_detector_unittest.cc
@@ -17,31 +17,29 @@
 
 #include "api/array_view.h"
 #include "api/environment/environment.h"
-#include "api/environment/environment_factory.h"
 #include "api/units/time_delta.h"
+#include "api/units/timestamp.h"
 #include "api/video/i420_buffer.h"
 #include "api/video/video_rotation.h"
 #include "rtc_base/event.h"
-#include "rtc_base/fake_clock.h"
 #include "rtc_base/random.h"
 #include "rtc_base/task_queue_for_test.h"
-#include "rtc_base/time_utils.h"
+#include "system_wrappers/include/clock.h"
+#include "test/create_test_environment.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
 #include "video/video_stream_encoder_observer.h"
 
 namespace webrtc {
+namespace {
 
-using ::testing::_;
 using ::testing::InvokeWithoutArgs;
 
-namespace {
 constexpr int kWidth = 640;
 constexpr int kHeight = 480;
 // Corresponds to load of 15%
-constexpr int kFrameIntervalUs = 33 * kNumMicrosecsPerMillisec;
-constexpr int kProcessTimeUs = 5 * kNumMicrosecsPerMillisec;
-}  // namespace
+constexpr TimeDelta kFrameInterval = TimeDelta::Millis(33);
+constexpr TimeDelta kProcessTime = TimeDelta::Millis(5);
 
 class MockCpuOveruseObserver : public OveruseFrameDetectorObserverInterface {
  public:
@@ -83,7 +81,7 @@
     observer_ = &mock_observer_;
     options_.min_process_count = 0;
     overuse_detector_ = std::make_unique<OveruseFrameDetectorUnderTest>(
-        CreateEnvironment(), this);
+        CreateTestEnvironment({.time = &clock_}), this);
     // Unfortunately, we can't call SetOptions here, since that would break
     // single-threading requirements in the RunOnTqNormalUsage test.
   }
@@ -101,10 +99,10 @@
   }
 
   virtual void InsertAndSendFramesWithInterval(int num_frames,
-                                               int interval_us,
+                                               TimeDelta interval_us,
                                                int width,
                                                int height,
-                                               int delay_us) {
+                                               TimeDelta delay_us) {
     VideoFrame frame =
         VideoFrame::Builder()
             .set_video_frame_buffer(I420Buffer::Create(width, height))
@@ -114,23 +112,23 @@
     uint32_t timestamp = 0;
     while (num_frames-- > 0) {
       frame.set_rtp_timestamp(timestamp);
-      int64_t capture_time_us = TimeMicros();
+      int64_t capture_time_us = clock_.TimeInMicroseconds();
       overuse_detector_->FrameCaptured(frame, capture_time_us);
-      clock_.AdvanceTime(TimeDelta::Micros(delay_us));
-      overuse_detector_->FrameSent(timestamp, TimeMicros(), capture_time_us,
-                                   delay_us);
-      clock_.AdvanceTime(TimeDelta::Micros(interval_us - delay_us));
-      timestamp += interval_us * 90 / 1000;
+      clock_.AdvanceTime(delay_us);
+      overuse_detector_->FrameSent(timestamp, clock_.TimeInMicroseconds(),
+                                   capture_time_us, delay_us.us());
+      clock_.AdvanceTime(interval_us - delay_us);
+      timestamp += interval_us.us() * 90 / 1000;
     }
   }
 
   virtual void InsertAndSendSimulcastFramesWithInterval(
       int num_frames,
-      int interval_us,
+      TimeDelta interval,
       int width,
       int height,
       // One element per layer
-      ArrayView<const int> delays_us) {
+      ArrayView<const TimeDelta> delays_us) {
     VideoFrame frame =
         VideoFrame::Builder()
             .set_video_frame_buffer(I420Buffer::Create(width, height))
@@ -140,30 +138,30 @@
     uint32_t timestamp = 0;
     while (num_frames-- > 0) {
       frame.set_rtp_timestamp(timestamp);
-      int64_t capture_time_us = TimeMicros();
+      int64_t capture_time_us = clock_.TimeInMicroseconds();
       overuse_detector_->FrameCaptured(frame, capture_time_us);
-      int max_delay_us = 0;
-      for (int delay_us : delays_us) {
-        if (delay_us > max_delay_us) {
-          clock_.AdvanceTime(TimeDelta::Micros(delay_us - max_delay_us));
-          max_delay_us = delay_us;
+      TimeDelta max_delay = TimeDelta::Zero();
+      for (TimeDelta delay : delays_us) {
+        if (delay > max_delay) {
+          clock_.AdvanceTime(delay - max_delay);
+          max_delay = delay;
         }
 
-        overuse_detector_->FrameSent(timestamp, TimeMicros(), capture_time_us,
-                                     delay_us);
+        overuse_detector_->FrameSent(timestamp, clock_.TimeInMicroseconds(),
+                                     capture_time_us, delay.us());
       }
       overuse_detector_->CheckForOveruse(observer_);
-      clock_.AdvanceTime(TimeDelta::Micros(interval_us - max_delay_us));
-      timestamp += interval_us * 90 / 1000;
+      clock_.AdvanceTime(interval - max_delay);
+      timestamp += interval.us() * 90 / 1000;
     }
   }
 
   virtual void InsertAndSendFramesWithRandomInterval(int num_frames,
-                                                     int min_interval_us,
-                                                     int max_interval_us,
+                                                     TimeDelta min_interval_us,
+                                                     TimeDelta max_interval_us,
                                                      int width,
                                                      int height,
-                                                     int delay_us) {
+                                                     TimeDelta delay_us) {
     Random random(17);
 
     VideoFrame frame =
@@ -175,19 +173,19 @@
     uint32_t timestamp = 0;
     while (num_frames-- > 0) {
       frame.set_rtp_timestamp(timestamp);
-      int interval_us = random.Rand(min_interval_us, max_interval_us);
-      int64_t capture_time_us = TimeMicros();
+      TimeDelta interval_us = TimeDelta::Micros(random.Rand(
+          min_interval_us.us<uint32_t>(), max_interval_us.us<uint32_t>()));
+      int64_t capture_time_us = clock_.TimeInMicroseconds();
       overuse_detector_->FrameCaptured(frame, capture_time_us);
-      clock_.AdvanceTime(TimeDelta::Micros(delay_us));
-      overuse_detector_->FrameSent(timestamp, TimeMicros(), capture_time_us,
-                                   std::optional<int>(delay_us));
+      clock_.AdvanceTime(delay_us);
+      overuse_detector_->FrameSent(timestamp, clock_.TimeInMicroseconds(),
+                                   capture_time_us, delay_us.us());
 
       overuse_detector_->CheckForOveruse(observer_);
       // Avoid turning clock backwards.
       if (interval_us > delay_us)
-        clock_.AdvanceTime(TimeDelta::Micros(interval_us - delay_us));
-
-      timestamp += interval_us * 90 / 1000;
+        clock_.AdvanceTime(interval_us - delay_us);
+      timestamp += interval_us.us() * 90 / 1000;
     }
   }
 
@@ -196,46 +194,46 @@
     // the usage. From the tests where these are used, adding another sample
     // doesn't affect the expected outcome (this is mainly to check initial
     // values and whether the overuse detector has been reset or not).
-    InsertAndSendFramesWithInterval(2, kNumMicrosecsPerSec, width, height,
-                                    kFrameIntervalUs);
+    InsertAndSendFramesWithInterval(2, TimeDelta::Seconds(1), width, height,
+                                    kFrameInterval);
   }
   void TriggerOveruse(int num_times) {
-    const int kDelayUs = 32 * kNumMicrosecsPerMillisec;
+    const TimeDelta kDelay = TimeDelta::Millis(32);
     for (int i = 0; i < num_times; ++i) {
-      InsertAndSendFramesWithInterval(1000, kFrameIntervalUs, kWidth, kHeight,
-                                      kDelayUs);
+      InsertAndSendFramesWithInterval(1000, kFrameInterval, kWidth, kHeight,
+                                      kDelay);
       overuse_detector_->CheckForOveruse(observer_);
     }
   }
 
   void TriggerUnderuse() {
-    const int kDelayUs1 = 5000;
-    const int kDelayUs2 = 6000;
-    InsertAndSendFramesWithInterval(1300, kFrameIntervalUs, kWidth, kHeight,
+    const TimeDelta kDelayUs1 = TimeDelta::Micros(5000);
+    const TimeDelta kDelayUs2 = TimeDelta::Micros(6000);
+    InsertAndSendFramesWithInterval(1300, kFrameInterval, kWidth, kHeight,
                                     kDelayUs1);
-    InsertAndSendFramesWithInterval(1, kFrameIntervalUs, kWidth, kHeight,
+    InsertAndSendFramesWithInterval(1, kFrameInterval, kWidth, kHeight,
                                     kDelayUs2);
     overuse_detector_->CheckForOveruse(observer_);
   }
 
   int UsagePercent() { return encode_usage_percent_; }
 
-  int64_t OveruseProcessingTimeLimitForFramerate(int fps) const {
-    int64_t frame_interval = kNumMicrosecsPerSec / fps;
-    int64_t max_processing_time_us =
+  TimeDelta OveruseProcessingTimeLimitForFramerate(int fps) const {
+    TimeDelta frame_interval = TimeDelta::Seconds(1) / fps;
+    TimeDelta max_processing_time_us =
         (frame_interval * options_.high_encode_usage_threshold_percent) / 100;
     return max_processing_time_us;
   }
 
-  int64_t UnderuseProcessingTimeLimitForFramerate(int fps) const {
-    int64_t frame_interval = kNumMicrosecsPerSec / fps;
-    int64_t max_processing_time_us =
+  TimeDelta UnderuseProcessingTimeLimitForFramerate(int fps) const {
+    TimeDelta frame_interval = TimeDelta::Seconds(1) / fps;
+    TimeDelta max_processing_time_us =
         (frame_interval * options_.low_encode_usage_threshold_percent) / 100;
     return max_processing_time_us;
   }
 
   CpuOveruseOptions options_;
-  ScopedFakeClock clock_;
+  SimulatedClock clock_{Timestamp::Millis(12345)};
   MockCpuOveruseObserver mock_observer_;
   OveruseFrameDetectorObserverInterface* observer_;
   std::unique_ptr<OveruseFrameDetectorUnderTest> overuse_detector_;
@@ -271,16 +269,16 @@
 }
 
 TEST_F(OveruseFrameDetectorTest, TriggerUnderuseWithMinProcessCount) {
-  const int kProcessIntervalUs = 5 * kNumMicrosecsPerSec;
+  const TimeDelta kProcessInterval = TimeDelta::Seconds(5);
   options_.min_process_count = 1;
   CpuOveruseObserverImpl overuse_observer;
   observer_ = nullptr;
   overuse_detector_->SetOptions(options_);
-  InsertAndSendFramesWithInterval(1200, kFrameIntervalUs, kWidth, kHeight,
-                                  kProcessTimeUs);
+  InsertAndSendFramesWithInterval(1200, kFrameInterval, kWidth, kHeight,
+                                  kProcessTime);
   overuse_detector_->CheckForOveruse(&overuse_observer);
   EXPECT_EQ(0, overuse_observer.normaluse_);
-  clock_.AdvanceTime(TimeDelta::Micros(kProcessIntervalUs));
+  clock_.AdvanceTime(kProcessInterval);
   overuse_detector_->CheckForOveruse(&overuse_observer);
   EXPECT_EQ(1, overuse_observer.normaluse_);
 }
@@ -312,17 +310,18 @@
 
 TEST_F(OveruseFrameDetectorTest, ProcessingUsage) {
   overuse_detector_->SetOptions(options_);
-  InsertAndSendFramesWithInterval(1000, kFrameIntervalUs, kWidth, kHeight,
-                                  kProcessTimeUs);
-  EXPECT_EQ(kProcessTimeUs * 100 / kFrameIntervalUs, UsagePercent());
+  InsertAndSendFramesWithInterval(1000, kFrameInterval, kWidth, kHeight,
+                                  kProcessTime);
+  EXPECT_EQ(static_cast<int>(kProcessTime * 100 / kFrameInterval),
+            UsagePercent());
 }
 
 TEST_F(OveruseFrameDetectorTest, ResetAfterResolutionChange) {
   overuse_detector_->SetOptions(options_);
   ForceUpdate(kWidth, kHeight);
   EXPECT_EQ(InitialUsage(), UsagePercent());
-  InsertAndSendFramesWithInterval(1000, kFrameIntervalUs, kWidth, kHeight,
-                                  kProcessTimeUs);
+  InsertAndSendFramesWithInterval(1000, kFrameInterval, kWidth, kHeight,
+                                  kProcessTime);
   EXPECT_NE(InitialUsage(), UsagePercent());
   // Verify reset (with new width/height).
   ForceUpdate(kWidth, kHeight + 1);
@@ -333,17 +332,17 @@
   overuse_detector_->SetOptions(options_);
   ForceUpdate(kWidth, kHeight);
   EXPECT_EQ(InitialUsage(), UsagePercent());
-  InsertAndSendFramesWithInterval(1000, kFrameIntervalUs, kWidth, kHeight,
-                                  kProcessTimeUs);
+  InsertAndSendFramesWithInterval(1000, kFrameInterval, kWidth, kHeight,
+                                  kProcessTime);
   EXPECT_NE(InitialUsage(), UsagePercent());
   InsertAndSendFramesWithInterval(
-      2, options_.frame_timeout_interval_ms * kNumMicrosecsPerMillisec, kWidth,
-      kHeight, kProcessTimeUs);
+      2, TimeDelta::Millis(options_.frame_timeout_interval_ms), kWidth, kHeight,
+      kProcessTime);
   EXPECT_NE(InitialUsage(), UsagePercent());
   // Verify reset.
   InsertAndSendFramesWithInterval(
-      2, (options_.frame_timeout_interval_ms + 1) * kNumMicrosecsPerMillisec,
-      kWidth, kHeight, kProcessTimeUs);
+      2, TimeDelta::Millis(options_.frame_timeout_interval_ms + 1), kWidth,
+      kHeight, kProcessTime);
   ForceUpdate(kWidth, kHeight);
   EXPECT_EQ(InitialUsage(), UsagePercent());
 }
@@ -351,20 +350,20 @@
 TEST_F(OveruseFrameDetectorTest, MinFrameSamplesBeforeUpdating) {
   options_.min_frame_samples = 40;
   overuse_detector_->SetOptions(options_);
-  InsertAndSendFramesWithInterval(40, kFrameIntervalUs, kWidth, kHeight,
-                                  kProcessTimeUs);
+  InsertAndSendFramesWithInterval(40, kFrameInterval, kWidth, kHeight,
+                                  kProcessTime);
   EXPECT_EQ(InitialUsage(), UsagePercent());
   // Pass time far enough to digest all previous samples.
   clock_.AdvanceTime(TimeDelta::Seconds(1));
-  InsertAndSendFramesWithInterval(1, kFrameIntervalUs, kWidth, kHeight,
-                                  kProcessTimeUs);
+  InsertAndSendFramesWithInterval(1, kFrameInterval, kWidth, kHeight,
+                                  kProcessTime);
   // The last sample has not been processed here.
   EXPECT_EQ(InitialUsage(), UsagePercent());
 
   // Pass time far enough to digest all previous samples, 41 in total.
   clock_.AdvanceTime(TimeDelta::Seconds(1));
-  InsertAndSendFramesWithInterval(1, kFrameIntervalUs, kWidth, kHeight,
-                                  kProcessTimeUs);
+  InsertAndSendFramesWithInterval(1, kFrameInterval, kWidth, kHeight,
+                                  kProcessTime);
   EXPECT_NE(InitialUsage(), UsagePercent());
 }
 
@@ -377,7 +376,7 @@
 TEST_F(OveruseFrameDetectorTest, MeasuresMultipleConcurrentSamples) {
   overuse_detector_->SetOptions(options_);
   EXPECT_CALL(mock_observer_, AdaptDown()).Times(::testing::AtLeast(1));
-  static const int kIntervalUs = 33 * kNumMicrosecsPerMillisec;
+  static TimeDelta kInterval = TimeDelta::Millis(33);
   static const size_t kNumFramesEncodingDelay = 3;
   VideoFrame frame =
       VideoFrame::Builder()
@@ -388,13 +387,13 @@
   for (size_t i = 0; i < 1000; ++i) {
     // Unique timestamps.
     frame.set_rtp_timestamp(static_cast<uint32_t>(i));
-    int64_t capture_time_us = TimeMicros();
+    int64_t capture_time_us = clock_.TimeInMicroseconds();
     overuse_detector_->FrameCaptured(frame, capture_time_us);
-    clock_.AdvanceTime(TimeDelta::Micros(kIntervalUs));
+    clock_.AdvanceTime(kInterval);
     if (i > kNumFramesEncodingDelay) {
       overuse_detector_->FrameSent(
-          static_cast<uint32_t>(i - kNumFramesEncodingDelay), TimeMicros(),
-          capture_time_us, kIntervalUs);
+          static_cast<uint32_t>(i - kNumFramesEncodingDelay),
+          clock_.TimeInMicroseconds(), capture_time_us, kInterval.us());
     }
     overuse_detector_->CheckForOveruse(observer_);
   }
@@ -404,8 +403,8 @@
   // >85% encoding time should trigger overuse.
   overuse_detector_->SetOptions(options_);
   EXPECT_CALL(mock_observer_, AdaptDown()).Times(::testing::AtLeast(1));
-  static const int kIntervalUs = 33 * kNumMicrosecsPerMillisec;
-  static const int kDelayUs = 30 * kNumMicrosecsPerMillisec;
+  static const TimeDelta kInterval = TimeDelta::Millis(33);
+  static const TimeDelta kDelay = TimeDelta::Millis(30);
   VideoFrame frame =
       VideoFrame::Builder()
           .set_video_frame_buffer(I420Buffer::Create(kWidth, kHeight))
@@ -415,18 +414,18 @@
   uint32_t timestamp = 0;
   for (size_t i = 0; i < 1000; ++i) {
     frame.set_rtp_timestamp(timestamp);
-    int64_t capture_time_us = TimeMicros();
+    int64_t capture_time_us = clock_.TimeInMicroseconds();
     overuse_detector_->FrameCaptured(frame, capture_time_us);
     // Encode and send first parts almost instantly.
     clock_.AdvanceTime(TimeDelta::Millis(1));
-    overuse_detector_->FrameSent(timestamp, TimeMicros(), capture_time_us,
-                                 kNumMicrosecsPerMillisec);
+    overuse_detector_->FrameSent(timestamp, clock_.TimeInMicroseconds(),
+                                 capture_time_us, TimeDelta::Millis(1).us());
     // Encode heavier part, resulting in >85% usage total.
-    clock_.AdvanceTime(TimeDelta::Micros(kDelayUs) - TimeDelta::Millis(1));
-    overuse_detector_->FrameSent(timestamp, TimeMicros(), capture_time_us,
-                                 kDelayUs);
-    clock_.AdvanceTime(TimeDelta::Micros(kIntervalUs - kDelayUs));
-    timestamp += kIntervalUs * 90 / 1000;
+    clock_.AdvanceTime(kDelay - TimeDelta::Millis(1));
+    overuse_detector_->FrameSent(timestamp, clock_.TimeInMicroseconds(),
+                                 capture_time_us, kDelay.us());
+    clock_.AdvanceTime(kInterval - kDelay);
+    timestamp += kInterval.us() * 90 / 1000;
     overuse_detector_->CheckForOveruse(observer_);
   }
 }
@@ -448,11 +447,11 @@
       }));
 
   queue.PostTask([this] {
-    const int kDelayUs1 = 5 * kNumMicrosecsPerMillisec;
-    const int kDelayUs2 = 6 * kNumMicrosecsPerMillisec;
-    InsertAndSendFramesWithInterval(1300, kFrameIntervalUs, kWidth, kHeight,
+    const TimeDelta kDelayUs1 = TimeDelta::Millis(5);
+    const TimeDelta kDelayUs2 = TimeDelta::Millis(6);
+    InsertAndSendFramesWithInterval(1300, kFrameInterval, kWidth, kHeight,
                                     kDelayUs1);
-    InsertAndSendFramesWithInterval(1, kFrameIntervalUs, kWidth, kHeight,
+    InsertAndSendFramesWithInterval(1, kFrameInterval, kWidth, kHeight,
                                     kDelayUs2);
   });
 
@@ -467,9 +466,9 @@
 
   overuse_detector_->SetOptions(options_);
   // Trigger overuse.
-  int64_t frame_interval_us = kNumMicrosecsPerSec / kCapturerMaxFrameRate;
+  TimeDelta frame_interval_us = TimeDelta::Seconds(1) / kCapturerMaxFrameRate;
   // Processing time just below over use limit given kEncodeMaxFrameRate.
-  int64_t processing_time_us =
+  TimeDelta processing_time_us =
       (98 * OveruseProcessingTimeLimitForFramerate(kEncodeMaxFrameRate)) / 100;
   EXPECT_CALL(mock_observer_, AdaptDown()).Times(1);
   for (int i = 0; i < options_.high_threshold_consecutive_count; ++i) {
@@ -479,7 +478,7 @@
   }
 
   // Simulate frame rate reduction and normal usage.
-  frame_interval_us = kNumMicrosecsPerSec / kEncodeMaxFrameRate;
+  frame_interval_us = TimeDelta::Seconds(1) / kEncodeMaxFrameRate;
   overuse_detector_->OnTargetFramerateUpdated(kEncodeMaxFrameRate);
   EXPECT_CALL(mock_observer_, AdaptDown()).Times(0);
   for (int i = 0; i < options_.high_threshold_consecutive_count; ++i) {
@@ -504,9 +503,9 @@
   overuse_detector_->OnTargetFramerateUpdated(kMinFrameRate);
 
   // Normal usage just at the limit.
-  int64_t frame_interval_us = kNumMicrosecsPerSec / kMinFrameRate;
+  TimeDelta frame_interval_us = TimeDelta::Seconds(1) / kMinFrameRate;
   // Processing time just below over use limit given kEncodeMaxFrameRate.
-  int64_t processing_time_us =
+  TimeDelta processing_time_us =
       (98 * OveruseProcessingTimeLimitForFramerate(kMinFrameRate)) / 100;
   EXPECT_CALL(mock_observer_, AdaptDown()).Times(0);
   for (int i = 0; i < options_.high_threshold_consecutive_count; ++i) {
@@ -539,17 +538,17 @@
   const int kMaxFrameRate = 20;
   overuse_detector_->SetOptions(options_);
   overuse_detector_->OnTargetFramerateUpdated(kMaxFrameRate);
-  int64_t frame_interval_us = kNumMicrosecsPerSec / kMaxFrameRate;
+  TimeDelta frame_interval_us = TimeDelta::Seconds(1) / kMaxFrameRate;
   // Maximum frame interval allowed is 35% above ideal.
-  int64_t max_frame_interval_us = (135 * frame_interval_us) / 100;
+  TimeDelta max_frame_interval_us = (135 * frame_interval_us) / 100;
   // Maximum processing time, without triggering overuse, allowed with the above
   // frame interval.
-  int64_t max_processing_time_us =
+  TimeDelta max_processing_time_us =
       (max_frame_interval_us * options_.high_encode_usage_threshold_percent) /
       100;
 
   // Processing time just below overuse limit given kMaxFrameRate.
-  int64_t processing_time_us = (98 * max_processing_time_us) / 100;
+  TimeDelta processing_time_us = (98 * max_processing_time_us) / 100;
   EXPECT_CALL(mock_observer_, AdaptDown()).Times(0);
   for (int i = 0; i < options_.high_threshold_consecutive_count; ++i) {
     InsertAndSendFramesWithInterval(1200, max_frame_interval_us, kWidth,
@@ -567,7 +566,7 @@
   }
 
   // Increase frame interval, should still trigger overuse.
-  max_frame_interval_us *= 2;
+  max_frame_interval_us = max_frame_interval_us * 2;
   EXPECT_CALL(mock_observer_, AdaptDown()).Times(1);
   for (int i = 0; i < options_.high_threshold_consecutive_count; ++i) {
     InsertAndSendFramesWithInterval(1200, max_frame_interval_us, kWidth,
@@ -589,18 +588,16 @@
   overuse_detector_->SetOptions(options_);
 
   const int kNumFrames = 500;
-  const int kEncodeTimeUs = 100 * kNumMicrosecsPerMillisec;
-
-  const int kMinIntervalUs = 30 * kNumMicrosecsPerMillisec;
-  const int kMaxIntervalUs = 1000 * kNumMicrosecsPerMillisec;
+  const TimeDelta kEncodeTime = TimeDelta::Micros(100);
+  const TimeDelta kMinInterval = TimeDelta::Micros(30);
+  const TimeDelta kMaxInterval = TimeDelta::Micros(1000);
 
   const int kTargetFramerate = 5;
 
   overuse_detector_->OnTargetFramerateUpdated(kTargetFramerate);
 
-  InsertAndSendFramesWithRandomInterval(kNumFrames, kMinIntervalUs,
-                                        kMaxIntervalUs, kWidth, kHeight,
-                                        kEncodeTimeUs);
+  InsertAndSendFramesWithRandomInterval(kNumFrames, kMinInterval, kMaxInterval,
+                                        kWidth, kHeight, kEncodeTime);
   // Average usage 19%. Check that estimate is in the right ball park.
   // EXPECT_NEAR(UsagePercent(), 20, 10);
   EXPECT_NEAR(UsagePercent(), 20, 35);
@@ -617,18 +614,16 @@
   //     .Times(::testing::AtLeast(1));
 
   const int kNumFrames = 500;
-  const int kEncodeTimeUs = 100 * kNumMicrosecsPerMillisec;
-
-  const int kMinIntervalUs = 30 * kNumMicrosecsPerMillisec;
-  const int kMaxIntervalUs = 3000 * kNumMicrosecsPerMillisec;
+  const TimeDelta kEncodeTime = TimeDelta::Millis(100);
+  const TimeDelta kMinInterval = TimeDelta::Millis(30);
+  const TimeDelta kMaxInterval = TimeDelta::Millis(3000);
 
   const int kTargetFramerate = 5;
 
   overuse_detector_->OnTargetFramerateUpdated(kTargetFramerate);
 
-  InsertAndSendFramesWithRandomInterval(kNumFrames, kMinIntervalUs,
-                                        kMaxIntervalUs, kWidth, kHeight,
-                                        kEncodeTimeUs);
+  InsertAndSendFramesWithRandomInterval(kNumFrames, kMinInterval, kMaxInterval,
+                                        kWidth, kHeight, kEncodeTime);
 
   // Average usage 6.6%, but since the frame_timeout_interval_ms is
   // only 1500 ms, we often reset the estimate to the initial value.
@@ -644,15 +639,15 @@
   EXPECT_CALL(mock_observer_, AdaptDown()).Times(0);
 
   constexpr int kNumFrames = 500;
-  constexpr int kEncodeTimesUs[] = {
-      10 * kNumMicrosecsPerMillisec,
-      8 * kNumMicrosecsPerMillisec,
-      12 * kNumMicrosecsPerMillisec,
+  constexpr TimeDelta kEncodeTimes[] = {
+      TimeDelta::Millis(10),
+      TimeDelta::Millis(8),
+      TimeDelta::Millis(12),
   };
-  constexpr int kIntervalUs = 30 * kNumMicrosecsPerMillisec;
+  constexpr TimeDelta kInterval = TimeDelta::Millis(30);
 
-  InsertAndSendSimulcastFramesWithInterval(kNumFrames, kIntervalUs, kWidth,
-                                           kHeight, kEncodeTimesUs);
+  InsertAndSendSimulcastFramesWithInterval(kNumFrames, kInterval, kWidth,
+                                           kHeight, kEncodeTimes);
 
   // Average usage 40%. 12 ms / 30 ms.
   EXPECT_GE(UsagePercent(), 35);
@@ -663,15 +658,15 @@
 class OveruseFrameDetectorTest2 : public OveruseFrameDetectorTest {
  protected:
   void SetUp() override {
-    options_.filter_time_ms = 5 * kNumMillisecsPerSec;
+    options_.filter_time_ms = TimeDelta::Seconds(5).ms();
     OveruseFrameDetectorTest::SetUp();
   }
 
   void InsertAndSendFramesWithInterval(int num_frames,
-                                       int interval_us,
+                                       TimeDelta interval_us,
                                        int width,
                                        int height,
-                                       int delay_us) override {
+                                       TimeDelta delay_us) override {
     VideoFrame frame =
         VideoFrame::Builder()
             .set_video_frame_buffer(I420Buffer::Create(width, height))
@@ -679,21 +674,21 @@
             .set_timestamp_us(0)
             .build();
     while (num_frames-- > 0) {
-      int64_t capture_time_us = TimeMicros();
+      int64_t capture_time_us = clock_.TimeInMicroseconds();
       overuse_detector_->FrameCaptured(frame, capture_time_us /* ignored */);
       overuse_detector_->FrameSent(0 /* ignored timestamp */,
                                    0 /* ignored send_time_us */,
-                                   capture_time_us, delay_us);
-      clock_.AdvanceTime(TimeDelta::Micros(interval_us));
+                                   capture_time_us, delay_us.us());
+      clock_.AdvanceTime(interval_us);
     }
   }
 
   void InsertAndSendFramesWithRandomInterval(int num_frames,
-                                             int min_interval_us,
-                                             int max_interval_us,
+                                             TimeDelta min_interval_us,
+                                             TimeDelta max_interval_us,
                                              int width,
                                              int height,
-                                             int delay_us) override {
+                                             TimeDelta delay_us) override {
     Random random(17);
 
     VideoFrame frame =
@@ -703,23 +698,24 @@
             .set_timestamp_us(0)
             .build();
     for (int i = 0; i < num_frames; i++) {
-      int interval_us = random.Rand(min_interval_us, max_interval_us);
-      int64_t capture_time_us = TimeMicros();
+      TimeDelta interval_us = TimeDelta::Micros(random.Rand(
+          min_interval_us.us<uint32_t>(), max_interval_us.us<uint32_t>()));
+      int64_t capture_time_us = clock_.TimeInMicroseconds();
       overuse_detector_->FrameCaptured(frame, capture_time_us);
       overuse_detector_->FrameSent(0 /* ignored timestamp */,
                                    0 /* ignored send_time_us */,
-                                   capture_time_us, delay_us);
+                                   capture_time_us, delay_us.us());
 
       overuse_detector_->CheckForOveruse(observer_);
-      clock_.AdvanceTime(TimeDelta::Micros(interval_us));
+      clock_.AdvanceTime(interval_us);
     }
   }
 
   void ForceUpdate(int width, int height) override {
     // This is mainly to check initial values and whether the overuse
     // detector has been reset or not.
-    InsertAndSendFramesWithInterval(1, kNumMicrosecsPerSec, width, height,
-                                    kFrameIntervalUs);
+    InsertAndSendFramesWithInterval(1, TimeDelta::Seconds(1), width, height,
+                                    kFrameInterval);
   }
 };
 
@@ -752,16 +748,16 @@
 }
 
 TEST_F(OveruseFrameDetectorTest2, TriggerUnderuseWithMinProcessCount) {
-  const int kProcessIntervalUs = 5 * kNumMicrosecsPerSec;
+  const TimeDelta kProcessInterval = TimeDelta::Seconds(5);
   options_.min_process_count = 1;
   CpuOveruseObserverImpl overuse_observer;
   observer_ = nullptr;
   overuse_detector_->SetOptions(options_);
-  InsertAndSendFramesWithInterval(1200, kFrameIntervalUs, kWidth, kHeight,
-                                  kProcessTimeUs);
+  InsertAndSendFramesWithInterval(1200, kFrameInterval, kWidth, kHeight,
+                                  kProcessTime);
   overuse_detector_->CheckForOveruse(&overuse_observer);
   EXPECT_EQ(0, overuse_observer.normaluse_);
-  clock_.AdvanceTime(TimeDelta::Micros(kProcessIntervalUs));
+  clock_.AdvanceTime(kProcessInterval);
   overuse_detector_->CheckForOveruse(&overuse_observer);
   EXPECT_EQ(1, overuse_observer.normaluse_);
 }
@@ -791,17 +787,18 @@
 
 TEST_F(OveruseFrameDetectorTest2, ProcessingUsage) {
   overuse_detector_->SetOptions(options_);
-  InsertAndSendFramesWithInterval(1000, kFrameIntervalUs, kWidth, kHeight,
-                                  kProcessTimeUs);
-  EXPECT_EQ(kProcessTimeUs * 100 / kFrameIntervalUs, UsagePercent());
+  InsertAndSendFramesWithInterval(1000, kFrameInterval, kWidth, kHeight,
+                                  kProcessTime);
+  EXPECT_EQ(static_cast<int>(kProcessTime * 100 / kFrameInterval),
+            UsagePercent());
 }
 
 TEST_F(OveruseFrameDetectorTest2, ResetAfterResolutionChange) {
   overuse_detector_->SetOptions(options_);
   ForceUpdate(kWidth, kHeight);
   EXPECT_EQ(InitialUsage(), UsagePercent());
-  InsertAndSendFramesWithInterval(1000, kFrameIntervalUs, kWidth, kHeight,
-                                  kProcessTimeUs);
+  InsertAndSendFramesWithInterval(1000, kFrameInterval, kWidth, kHeight,
+                                  kProcessTime);
   EXPECT_NE(InitialUsage(), UsagePercent());
   // Verify reset (with new width/height).
   ForceUpdate(kWidth, kHeight + 1);
@@ -812,40 +809,40 @@
   overuse_detector_->SetOptions(options_);
   ForceUpdate(kWidth, kHeight);
   EXPECT_EQ(InitialUsage(), UsagePercent());
-  InsertAndSendFramesWithInterval(1000, kFrameIntervalUs, kWidth, kHeight,
-                                  kProcessTimeUs);
+  InsertAndSendFramesWithInterval(1000, kFrameInterval, kWidth, kHeight,
+                                  kProcessTime);
   EXPECT_NE(InitialUsage(), UsagePercent());
   InsertAndSendFramesWithInterval(
-      2, options_.frame_timeout_interval_ms * kNumMicrosecsPerMillisec, kWidth,
-      kHeight, kProcessTimeUs);
+      2, TimeDelta::Millis(options_.frame_timeout_interval_ms), kWidth, kHeight,
+      kProcessTime);
   EXPECT_NE(InitialUsage(), UsagePercent());
   // Verify reset.
   InsertAndSendFramesWithInterval(
-      2, (options_.frame_timeout_interval_ms + 1) * kNumMicrosecsPerMillisec,
-      kWidth, kHeight, kProcessTimeUs);
+      2, TimeDelta::Millis(options_.frame_timeout_interval_ms + 1), kWidth,
+      kHeight, kProcessTime);
   ForceUpdate(kWidth, kHeight);
   EXPECT_EQ(InitialUsage(), UsagePercent());
 }
 
 TEST_F(OveruseFrameDetectorTest2, ConvergesSlowly) {
   overuse_detector_->SetOptions(options_);
-  InsertAndSendFramesWithInterval(1, kFrameIntervalUs, kWidth, kHeight,
-                                  kProcessTimeUs);
+  InsertAndSendFramesWithInterval(1, kFrameInterval, kWidth, kHeight,
+                                  kProcessTime);
   // No update for the first sample.
   EXPECT_EQ(InitialUsage(), UsagePercent());
 
   // Total time approximately 40 * 33ms = 1.3s, significantly less
   // than the 5s time constant.
-  InsertAndSendFramesWithInterval(40, kFrameIntervalUs, kWidth, kHeight,
-                                  kProcessTimeUs);
+  InsertAndSendFramesWithInterval(40, kFrameInterval, kWidth, kHeight,
+                                  kProcessTime);
 
   // Should have started to approach correct load of 15%, but not very far.
   EXPECT_LT(UsagePercent(), InitialUsage());
   EXPECT_GT(UsagePercent(), (InitialUsage() * 3 + 8) / 4);
 
   // Run for roughly 10s more, should now be closer.
-  InsertAndSendFramesWithInterval(300, kFrameIntervalUs, kWidth, kHeight,
-                                  kProcessTimeUs);
+  InsertAndSendFramesWithInterval(300, kFrameInterval, kWidth, kHeight,
+                                  kProcessTime);
   EXPECT_NEAR(UsagePercent(), 20, 5);
 }
 
@@ -858,7 +855,7 @@
 TEST_F(OveruseFrameDetectorTest2, MeasuresMultipleConcurrentSamples) {
   overuse_detector_->SetOptions(options_);
   EXPECT_CALL(mock_observer_, AdaptDown()).Times(::testing::AtLeast(1));
-  static const int kIntervalUs = 33 * kNumMicrosecsPerMillisec;
+  static const TimeDelta kInterval = TimeDelta::Millis(33);
   static const size_t kNumFramesEncodingDelay = 3;
   VideoFrame frame =
       VideoFrame::Builder()
@@ -869,13 +866,13 @@
   for (size_t i = 0; i < 1000; ++i) {
     // Unique timestamps.
     frame.set_rtp_timestamp(static_cast<uint32_t>(i));
-    int64_t capture_time_us = TimeMicros();
+    int64_t capture_time_us = clock_.TimeInMicroseconds();
     overuse_detector_->FrameCaptured(frame, capture_time_us);
-    clock_.AdvanceTime(TimeDelta::Micros(kIntervalUs));
+    clock_.AdvanceTime(kInterval);
     if (i > kNumFramesEncodingDelay) {
       overuse_detector_->FrameSent(
-          static_cast<uint32_t>(i - kNumFramesEncodingDelay), TimeMicros(),
-          capture_time_us, kIntervalUs);
+          static_cast<uint32_t>(i - kNumFramesEncodingDelay),
+          clock_.TimeInMicroseconds(), capture_time_us, kInterval.us());
     }
     overuse_detector_->CheckForOveruse(observer_);
   }
@@ -885,8 +882,8 @@
   // >85% encoding time should trigger overuse.
   overuse_detector_->SetOptions(options_);
   EXPECT_CALL(mock_observer_, AdaptDown()).Times(::testing::AtLeast(1));
-  static const int kIntervalUs = 33 * kNumMicrosecsPerMillisec;
-  static const int kDelayUs = 30 * kNumMicrosecsPerMillisec;
+  static const TimeDelta kInterval = TimeDelta::Millis(33);
+  static const TimeDelta kDelay = TimeDelta::Millis(30);
   VideoFrame frame =
       VideoFrame::Builder()
           .set_video_frame_buffer(I420Buffer::Create(kWidth, kHeight))
@@ -896,18 +893,18 @@
   uint32_t timestamp = 0;
   for (size_t i = 0; i < 1000; ++i) {
     frame.set_rtp_timestamp(timestamp);
-    int64_t capture_time_us = TimeMicros();
+    int64_t capture_time_us = clock_.TimeInMicroseconds();
     overuse_detector_->FrameCaptured(frame, capture_time_us);
     // Encode and send first parts almost instantly.
     clock_.AdvanceTime(TimeDelta::Millis(1));
-    overuse_detector_->FrameSent(timestamp, TimeMicros(), capture_time_us,
-                                 kNumMicrosecsPerMillisec);
+    overuse_detector_->FrameSent(timestamp, clock_.TimeInMicroseconds(),
+                                 capture_time_us, TimeDelta::Millis(1).us());
     // Encode heavier part, resulting in >85% usage total.
-    clock_.AdvanceTime(TimeDelta::Micros(kDelayUs) - TimeDelta::Millis(1));
-    overuse_detector_->FrameSent(timestamp, TimeMicros(), capture_time_us,
-                                 kDelayUs);
-    clock_.AdvanceTime(TimeDelta::Micros(kIntervalUs - kDelayUs));
-    timestamp += kIntervalUs * 90 / 1000;
+    clock_.AdvanceTime(kDelay - TimeDelta::Millis(1));
+    overuse_detector_->FrameSent(timestamp, clock_.TimeInMicroseconds(),
+                                 capture_time_us, kDelay.us());
+    clock_.AdvanceTime(kInterval - kDelay);
+    timestamp += kInterval.us() * 90 / 1000;
     overuse_detector_->CheckForOveruse(observer_);
   }
 }
@@ -929,11 +926,11 @@
       }));
 
   queue.PostTask([this] {
-    const int kDelayUs1 = 5 * kNumMicrosecsPerMillisec;
-    const int kDelayUs2 = 6 * kNumMicrosecsPerMillisec;
-    InsertAndSendFramesWithInterval(1300, kFrameIntervalUs, kWidth, kHeight,
+    const TimeDelta kDelayUs1 = TimeDelta::Millis(5);
+    const TimeDelta kDelayUs2 = TimeDelta::Millis(6);
+    InsertAndSendFramesWithInterval(1300, kFrameInterval, kWidth, kHeight,
                                     kDelayUs1);
-    InsertAndSendFramesWithInterval(1, kFrameIntervalUs, kWidth, kHeight,
+    InsertAndSendFramesWithInterval(1, kFrameInterval, kWidth, kHeight,
                                     kDelayUs2);
   });
 
@@ -948,14 +945,12 @@
   EXPECT_CALL(mock_observer_, AdaptUp()).Times(::testing::AtLeast(1));
 
   const int kNumFrames = 500;
-  const int kEncodeTimeUs = 100 * kNumMicrosecsPerMillisec;
+  const TimeDelta kEncodeTime = TimeDelta::Millis(100);
+  const TimeDelta kMinInterval = TimeDelta::Millis(30);
+  const TimeDelta kMaxInterval = TimeDelta::Millis(1000);
 
-  const int kMinIntervalUs = 30 * kNumMicrosecsPerMillisec;
-  const int kMaxIntervalUs = 1000 * kNumMicrosecsPerMillisec;
-
-  InsertAndSendFramesWithRandomInterval(kNumFrames, kMinIntervalUs,
-                                        kMaxIntervalUs, kWidth, kHeight,
-                                        kEncodeTimeUs);
+  InsertAndSendFramesWithRandomInterval(kNumFrames, kMinInterval, kMaxInterval,
+                                        kWidth, kHeight, kEncodeTime);
   // Average usage 19%. Check that estimate is in the right ball park.
   EXPECT_NEAR(UsagePercent(), 20, 10);
 }
@@ -968,14 +963,12 @@
   EXPECT_CALL(mock_observer_, AdaptUp()).Times(::testing::AtLeast(1));
 
   const int kNumFrames = 500;
-  const int kEncodeTimeUs = 100 * kNumMicrosecsPerMillisec;
+  const TimeDelta kEncodeTime = TimeDelta::Millis(100);
+  const TimeDelta kMinInterval = TimeDelta::Millis(30);
+  const TimeDelta kMaxInterval = TimeDelta::Millis(3000);
 
-  const int kMinIntervalUs = 30 * kNumMicrosecsPerMillisec;
-  const int kMaxIntervalUs = 3000 * kNumMicrosecsPerMillisec;
-
-  InsertAndSendFramesWithRandomInterval(kNumFrames, kMinIntervalUs,
-                                        kMaxIntervalUs, kWidth, kHeight,
-                                        kEncodeTimeUs);
+  InsertAndSendFramesWithRandomInterval(kNumFrames, kMinInterval, kMaxInterval,
+                                        kWidth, kHeight, kEncodeTime);
 
   // Average usage 6.6%, but since the frame_timeout_interval_ms is
   // only 1500 ms, we often reset the estimate to the initial value.
@@ -989,12 +982,13 @@
   // Represents a cpu utilization close to 100%. First input frame results in
   // three encoded frames, and the last of those isn't finished until after the
   // first encoded frame corresponding to the next input frame.
-  const int kEncodeTimeUs = 30 * kNumMicrosecsPerMillisec;
-  const int kCaptureTimesMs[] = {33, 33, 66, 33};
+  const TimeDelta kEncodeTime = TimeDelta::Millis(30);
+  const Timestamp kCaptureTimes[] = {
+      Timestamp::Millis(33), Timestamp::Millis(33), Timestamp::Millis(66),
+      Timestamp::Millis(33)};
 
-  for (int capture_time_ms : kCaptureTimesMs) {
-    overuse_detector_->FrameSent(
-        0, 0, capture_time_ms * kNumMicrosecsPerMillisec, kEncodeTimeUs);
+  for (Timestamp capture_time : kCaptureTimes) {
+    overuse_detector_->FrameSent(0, 0, capture_time.ms(), kEncodeTime.us());
   }
   EXPECT_GE(UsagePercent(), InitialUsage());
 }
@@ -1006,19 +1000,20 @@
   EXPECT_CALL(mock_observer_, AdaptDown()).Times(0);
 
   constexpr int kNumFrames = 500;
-  constexpr int kEncodeTimesUs[] = {
-      10 * kNumMicrosecsPerMillisec,
-      8 * kNumMicrosecsPerMillisec,
-      12 * kNumMicrosecsPerMillisec,
+  constexpr TimeDelta kEncodeTimes[] = {
+      TimeDelta::Millis(10),
+      TimeDelta::Millis(8),
+      TimeDelta::Millis(12),
   };
-  constexpr int kIntervalUs = 30 * kNumMicrosecsPerMillisec;
+  constexpr TimeDelta kInterval = TimeDelta::Millis(30);
 
-  InsertAndSendSimulcastFramesWithInterval(kNumFrames, kIntervalUs, kWidth,
-                                           kHeight, kEncodeTimesUs);
+  InsertAndSendSimulcastFramesWithInterval(kNumFrames, kInterval, kWidth,
+                                           kHeight, kEncodeTimes);
 
   // Average usage 40%. 12 ms / 30 ms.
   EXPECT_GE(UsagePercent(), 35);
   EXPECT_LE(UsagePercent(), 45);
 }
 
+}  // namespace
 }  // namespace webrtc