Use fake clock for replay fuzzing

This speed up fuzzing because no more SleepMs in real time.

Bug: chromium:959836, chromium:1009073
Change-Id: Ib00a2ff8d6ca2e0bfc706ee7469e0a9c7fb10758
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/156362
Commit-Queue: Benjamin Wright <benwright@webrtc.org>
Reviewed-by: Benjamin Wright <benwright@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29439}
diff --git a/test/fuzzers/utils/BUILD.gn b/test/fuzzers/utils/BUILD.gn
index e821e2f..5f9ea4c 100644
--- a/test/fuzzers/utils/BUILD.gn
+++ b/test/fuzzers/utils/BUILD.gn
@@ -25,6 +25,7 @@
     "../../../media:rtc_internal_video_codecs",
     "../../../rtc_base:checks",
     "../../../rtc_base:rtc_base_approved",
+    "../../../rtc_base:rtc_base_tests_utils",
     "../../../rtc_base:rtc_json",
     "../../../system_wrappers",
     "../../../test:call_config_utils",
diff --git a/test/fuzzers/utils/rtp_replayer.cc b/test/fuzzers/utils/rtp_replayer.cc
index 4a9712a..af03be2 100644
--- a/test/fuzzers/utils/rtp_replayer.cc
+++ b/test/fuzzers/utils/rtp_replayer.cc
@@ -18,7 +18,6 @@
 #include "api/task_queue/default_task_queue_factory.h"
 #include "rtc_base/strings/json.h"
 #include "system_wrappers/include/clock.h"
-#include "system_wrappers/include/sleep.h"
 #include "test/call_config_utils.h"
 #include "test/encoder_settings.h"
 #include "test/fake_decoder.h"
@@ -43,6 +42,13 @@
     std::vector<VideoReceiveStream::Config> receive_stream_configs,
     const uint8_t* rtp_dump_data,
     size_t rtp_dump_size) {
+  rtc::ScopedBaseFakeClock fake_clock;
+
+  // Work around: webrtc calls webrtc::Random(clock.TimeInMicroseconds())
+  // everywhere and Random expects non-zero seed. Let's set the clock non-zero
+  // to make them happy.
+  fake_clock.SetTime(webrtc::Timestamp::ms(1));
+
   // Attempt to create an RtpReader from the input file.
   auto rtp_reader = CreateRtpReader(rtp_dump_data, rtp_dump_size);
   if (rtp_reader == nullptr) {
@@ -64,7 +70,7 @@
     receive_stream->Start();
   }
 
-  ReplayPackets(call.get(), rtp_reader.get());
+  ReplayPackets(&fake_clock, call.get(), rtp_reader.get());
 
   for (const auto& receive_stream : stream_state->receive_streams) {
     call->DestroyVideoReceiveStream(receive_stream);
@@ -127,7 +133,9 @@
   return rtp_reader;
 }
 
-void RtpReplayer::ReplayPackets(Call* call, test::RtpFileReader* rtp_reader) {
+void RtpReplayer::ReplayPackets(rtc::FakeClock* clock,
+                                Call* call,
+                                test::RtpFileReader* rtp_reader) {
   int64_t replay_start_ms = -1;
   int num_packets = 0;
   std::map<uint32_t, int> unknown_packets;
@@ -145,8 +153,10 @@
 
     int64_t deliver_in_ms = replay_start_ms + packet.time_ms - now_ms;
     if (deliver_in_ms > 0) {
-      // Set an upper limit on sleep to prevent timing out.
-      SleepMs(std::min(deliver_in_ms, static_cast<int64_t>(100)));
+      // StatsCounter::ReportMetricToAggregatedCounter is O(elapsed time).
+      // Set an upper limit to prevent waste time.
+      clock->AdvanceTime(webrtc::TimeDelta::ms(
+          std::min(deliver_in_ms, static_cast<int64_t>(100))));
     }
 
     ++num_packets;
diff --git a/test/fuzzers/utils/rtp_replayer.h b/test/fuzzers/utils/rtp_replayer.h
index 3217fc3..c79a17e 100644
--- a/test/fuzzers/utils/rtp_replayer.h
+++ b/test/fuzzers/utils/rtp_replayer.h
@@ -23,6 +23,7 @@
 #include "api/video_codecs/video_decoder.h"
 #include "call/call.h"
 #include "media/engine/internal_decoder_factory.h"
+#include "rtc_base/fake_clock.h"
 #include "rtc_base/time_utils.h"
 #include "test/null_transport.h"
 #include "test/rtp_file_reader.h"
@@ -78,7 +79,9 @@
       size_t rtp_dump_size);
 
   // Replays each packet to from the RtpDump.
-  static void ReplayPackets(Call* call, test::RtpFileReader* rtp_reader);
+  static void ReplayPackets(rtc::FakeClock* clock,
+                            Call* call,
+                            test::RtpFileReader* rtp_reader);
 };  // class RtpReplayer
 
 }  // namespace test