Stop using ScopedFakeClock in audio_coding tests

Bug: webrtc:42223992
Change-Id: If681b96cb2237e9610e8b942454f40bd6a6a6964
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/462521
Auto-Submit: Evan Shrubsole <eshr@webrtc.org>
Reviewed-by: Jakob Ivarsson‎ <jakobi@webrtc.org>
Commit-Queue: Jakob Ivarsson‎ <jakobi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#47370}
diff --git a/modules/audio_coding/BUILD.gn b/modules/audio_coding/BUILD.gn
index 25748a0..337f9b1 100644
--- a/modules/audio_coding/BUILD.gn
+++ b/modules/audio_coding/BUILD.gn
@@ -1424,6 +1424,7 @@
         "../../api:rtp_packet_info",
         "../../api:rtp_parameters",
         "../../api:scoped_refptr",
+        "../../api:time_controller",
         "../../api/audio:audio_frame_api",
         "../../api/audio_codecs:audio_codecs_api",
         "../../api/audio_codecs:builtin_audio_decoder_factory",
@@ -1466,6 +1467,7 @@
         "../../test:fileutils",
         "../../test:rtc_expect_death",
         "../../test:test_support",
+        "../../test/time_controller",
         "../rtp_rtcp:rtp_rtcp_format",
         "codecs/opus/test",
         "codecs/opus/test:test_unittest",
diff --git a/modules/audio_coding/audio_network_adaptor/audio_network_adaptor_impl_unittest.cc b/modules/audio_coding/audio_network_adaptor/audio_network_adaptor_impl_unittest.cc
index 8388cf4..838c8f5 100644
--- a/modules/audio_coding/audio_network_adaptor/audio_network_adaptor_impl_unittest.cc
+++ b/modules/audio_coding/audio_network_adaptor/audio_network_adaptor_impl_unittest.cc
@@ -17,17 +17,19 @@
 #include <vector>
 
 #include "api/rtc_event_log/rtc_event.h"
+#include "api/test/time_controller.h"
 #include "api/units/time_delta.h"
+#include "api/units/timestamp.h"
 #include "logging/rtc_event_log/events/rtc_event_audio_network_adaptation.h"
 #include "logging/rtc_event_log/mock/mock_rtc_event_log.h"
 #include "modules/audio_coding/audio_network_adaptor/controller.h"
 #include "modules/audio_coding/audio_network_adaptor/mock/mock_controller.h"
 #include "modules/audio_coding/audio_network_adaptor/mock/mock_controller_manager.h"
 #include "modules/audio_coding/audio_network_adaptor/mock/mock_debug_dump_writer.h"
-#include "rtc_base/fake_clock.h"
 #include "test/create_test_environment.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
+#include "test/time_controller/simulated_time_controller.h"
 
 namespace webrtc {
 
@@ -81,7 +83,8 @@
   MockDebugDumpWriter* mock_debug_dump_writer;
 };
 
-AudioNetworkAdaptorStates CreateAudioNetworkAdaptor() {
+AudioNetworkAdaptorStates CreateAudioNetworkAdaptor(
+    TimeController* time_controller = nullptr) {
   AudioNetworkAdaptorStates states;
   std::vector<Controller*> controllers;
   for (size_t i = 0; i < kNumControllers; ++i) {
@@ -108,10 +111,16 @@
   EXPECT_CALL(*debug_dump_writer, Die());
   states.mock_debug_dump_writer = debug_dump_writer.get();
 
+  CreateTestEnvironmentOptions options;
+  options.event_log = states.event_log.get();
+  if (time_controller) {
+    options.time = time_controller;
+  }
+
   // AudioNetworkAdaptorImpl governs the lifetime of controller manager.
   states.audio_network_adaptor = std::make_unique<AudioNetworkAdaptorImpl>(
-      CreateTestEnvironment({.event_log = states.event_log.get()}),
-      std::move(controller_manager), std::move(debug_dump_writer));
+      CreateTestEnvironment(std::move(options)), std::move(controller_manager),
+      std::move(debug_dump_writer));
 
   return states;
 }
@@ -185,9 +194,9 @@
 
 TEST(AudioNetworkAdaptorImplTest,
      DumpEncoderRuntimeConfigIsCalledOnGetEncoderRuntimeConfig) {
-  ScopedFakeClock fake_clock;
-  fake_clock.AdvanceTime(TimeDelta::Millis(kClockInitialTimeMs));
-  auto states = CreateAudioNetworkAdaptor();
+  GlobalSimulatedTimeController time_controller(
+      Timestamp::Millis(kClockInitialTimeMs));
+  auto states = CreateAudioNetworkAdaptor(&time_controller);
   AudioEncoderRuntimeConfig config;
   config.bitrate_bps = 32000;
   config.enable_fec = true;
@@ -203,10 +212,10 @@
 
 TEST(AudioNetworkAdaptorImplTest,
      DumpNetworkMetricsIsCalledOnSetNetworkMetrics) {
-  ScopedFakeClock fake_clock;
-  fake_clock.AdvanceTime(TimeDelta::Millis(kClockInitialTimeMs));
+  GlobalSimulatedTimeController time_controller(
+      Timestamp::Millis(kClockInitialTimeMs));
 
-  auto states = CreateAudioNetworkAdaptor();
+  auto states = CreateAudioNetworkAdaptor(&time_controller);
 
   constexpr int kBandwidth = 16000;
   constexpr float kPacketLoss = 0.7f;
@@ -222,31 +231,31 @@
               DumpNetworkMetrics(NetworkMetricsIs(check), timestamp_check));
   states.audio_network_adaptor->SetUplinkBandwidth(kBandwidth);
 
-  fake_clock.AdvanceTime(TimeDelta::Millis(100));
+  time_controller.AdvanceTime(TimeDelta::Millis(100));
   timestamp_check += 100;
   check.uplink_packet_loss_fraction = kPacketLoss;
   EXPECT_CALL(*states.mock_debug_dump_writer,
               DumpNetworkMetrics(NetworkMetricsIs(check), timestamp_check));
   states.audio_network_adaptor->SetUplinkPacketLossFraction(kPacketLoss);
 
-  fake_clock.AdvanceTime(TimeDelta::Millis(50));
+  time_controller.AdvanceTime(TimeDelta::Millis(50));
   timestamp_check += 50;
 
-  fake_clock.AdvanceTime(TimeDelta::Millis(200));
+  time_controller.AdvanceTime(TimeDelta::Millis(200));
   timestamp_check += 200;
   check.rtt_ms = kRtt;
   EXPECT_CALL(*states.mock_debug_dump_writer,
               DumpNetworkMetrics(NetworkMetricsIs(check), timestamp_check));
   states.audio_network_adaptor->SetRtt(kRtt);
 
-  fake_clock.AdvanceTime(TimeDelta::Millis(150));
+  time_controller.AdvanceTime(TimeDelta::Millis(150));
   timestamp_check += 150;
   check.target_audio_bitrate_bps = kTargetAudioBitrate;
   EXPECT_CALL(*states.mock_debug_dump_writer,
               DumpNetworkMetrics(NetworkMetricsIs(check), timestamp_check));
   states.audio_network_adaptor->SetTargetAudioBitrate(kTargetAudioBitrate);
 
-  fake_clock.AdvanceTime(TimeDelta::Millis(50));
+  time_controller.AdvanceTime(TimeDelta::Millis(50));
   timestamp_check += 50;
   check.overhead_bytes_per_packet = kOverhead;
   EXPECT_CALL(*states.mock_debug_dump_writer,
diff --git a/modules/audio_coding/codecs/opus/audio_encoder_opus_unittest.cc b/modules/audio_coding/codecs/opus/audio_encoder_opus_unittest.cc
index f9ead46..687a4e0 100644
--- a/modules/audio_coding/codecs/opus/audio_encoder_opus_unittest.cc
+++ b/modules/audio_coding/codecs/opus/audio_encoder_opus_unittest.cc
@@ -39,11 +39,12 @@
 #include "modules/audio_coding/neteq/tools/audio_loop.h"
 #include "rtc_base/buffer.h"
 #include "rtc_base/checks.h"
-#include "rtc_base/fake_clock.h"
+#include "test/create_test_environment.h"
 #include "test/create_test_field_trials.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
 #include "test/testsupport/file_utils.h"
+#include "test/time_controller/simulated_time_controller.h"
 
 namespace webrtc {
 namespace {
@@ -65,8 +66,9 @@
 struct AudioEncoderOpusStates {
   MockAudioNetworkAdaptor* mock_audio_network_adaptor;
   MockSmoothingFilter* mock_bitrate_smoother;
+  GlobalSimulatedTimeController time_controller{
+      Timestamp::Micros(kInitialTimeUs)};
   std::unique_ptr<AudioEncoderOpusImpl> encoder;
-  std::unique_ptr<ScopedFakeClock> fake_clock;
   int uplink_bandwidth_update_interval_ms = 0;
 };
 
@@ -77,8 +79,6 @@
   std::unique_ptr<AudioEncoderOpusStates> states =
       std::make_unique<AudioEncoderOpusStates>();
   states->mock_audio_network_adaptor = nullptr;
-  states->fake_clock.reset(new ScopedFakeClock());
-  states->fake_clock->SetTime(Timestamp::Micros(kInitialTimeUs));
 
   MockAudioNetworkAdaptor** mock_ptr = &states->mock_audio_network_adaptor;
   AudioEncoderOpusImpl::AudioNetworkAdaptorCreator creator =
@@ -106,8 +106,10 @@
   states->mock_bitrate_smoother = bitrate_smoother.get();
 
   states->encoder = AudioEncoderOpusImpl::CreateForTesting(
-      CreateEnvironment(field_trials), std::move(config),
-      kDefaultOpusPayloadType, creator, std::move(bitrate_smoother));
+      CreateTestEnvironment(
+          {.field_trials = field_trials, .time = &states->time_controller}),
+      std::move(config), kDefaultOpusPayloadType, creator,
+      std::move(bitrate_smoother));
   return states;
 }
 
@@ -374,7 +376,7 @@
   states->encoder->OnReceivedUplinkPacketLossFraction(kPacketLossFraction_1);
   EXPECT_FLOAT_EQ(0.02f, states->encoder->packet_loss_rate());
 
-  states->fake_clock->AdvanceTime(TimeDelta::Millis(kSecondSampleTimeMs));
+  states->time_controller.AdvanceTime(TimeDelta::Millis(kSecondSampleTimeMs));
   states->encoder->OnReceivedUplinkPacketLossFraction(kPacketLossFraction_2);
 
   // Now the output of packet loss fraction smoother should be
@@ -517,7 +519,7 @@
   // Repeat update uplink bandwidth tests.
   for (int i = 0; i < 5; i++) {
     // Don't update till it is time to update again.
-    states->fake_clock->AdvanceTime(
+    states->time_controller.AdvanceTime(
         TimeDelta::Millis(states->uplink_bandwidth_update_interval_ms - 1));
     states->encoder->Encode(
         0, std::span<const int16_t>(audio.data(), audio.size()), &encoded);
@@ -526,7 +528,7 @@
     EXPECT_CALL(*states->mock_bitrate_smoother, GetAverage)
         .WillOnce(Return(40000));
     EXPECT_CALL(*states->mock_audio_network_adaptor, SetUplinkBandwidth(40000));
-    states->fake_clock->AdvanceTime(TimeDelta::Millis(1));
+    states->time_controller.AdvanceTime(TimeDelta::Millis(1));
     states->encoder->Encode(
         0, std::span<const int16_t>(audio.data(), audio.size()), &encoded);
   }