Use injected clock in LegacyStatsCollector

Bug: webrtc:42223992
Change-Id: I83b51e2efbf5131f0d8d53b44f45cbe67500a124
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/406241
Commit-Queue: Henrik Boström <hbos@webrtc.org>
Reviewed-by: Henrik Boström <hbos@webrtc.org>
Auto-Submit: Evan Shrubsole <eshr@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#45435}
diff --git a/pc/BUILD.gn b/pc/BUILD.gn
index 47e5c4d..94314bb 100644
--- a/pc/BUILD.gn
+++ b/pc/BUILD.gn
@@ -1375,6 +1375,7 @@
     "../rtc_base:stringutils",
     "../rtc_base:threading",
     "../rtc_base:timeutils",
+    "../system_wrappers",
     "//third_party/abseil-cpp/absl/strings",
     "//third_party/abseil-cpp/absl/strings:string_view",
   ]
diff --git a/pc/legacy_stats_collector.cc b/pc/legacy_stats_collector.cc
index 377bb60..9badcb7 100644
--- a/pc/legacy_stats_collector.cc
+++ b/pc/legacy_stats_collector.cc
@@ -63,6 +63,7 @@
 #include "rtc_base/thread.h"
 #include "rtc_base/time_utils.h"
 #include "rtc_base/trace_event.h"
+#include "system_wrappers/include/clock.h"
 
 namespace webrtc {
 namespace {
@@ -554,8 +555,10 @@
   }
 }
 
-LegacyStatsCollector::LegacyStatsCollector(PeerConnectionInternal* pc)
+LegacyStatsCollector::LegacyStatsCollector(PeerConnectionInternal* pc,
+                                           Clock& clock)
     : pc_(pc),
+      clock_(clock),
       stats_gathering_started_(0),
       use_standard_bytes_stats_(
           pc->trials().IsEnabled(kUseStandardBytesStats)) {
@@ -677,7 +680,7 @@
   // will be ignored. Using a monotonic clock specifically for this, while using
   // a UTC clock for the reports themselves.
   const int64_t kMinGatherStatsPeriodMs = 50;
-  int64_t cache_now_ms = TimeMillis();
+  int64_t cache_now_ms = clock_.TimeInMilliseconds();
   if (cache_timestamp_ms_ != 0 &&
       cache_timestamp_ms_ + kMinGatherStatsPeriodMs > cache_now_ms) {
     return;
diff --git a/pc/legacy_stats_collector.h b/pc/legacy_stats_collector.h
index e2786b1..791fb2c 100644
--- a/pc/legacy_stats_collector.h
+++ b/pc/legacy_stats_collector.h
@@ -38,6 +38,7 @@
 #include "rtc_base/network_constants.h"
 #include "rtc_base/ssl_certificate.h"
 #include "rtc_base/thread_annotations.h"
+#include "system_wrappers/include/clock.h"
 
 namespace webrtc {
 
@@ -57,7 +58,7 @@
  public:
   // The caller is responsible for ensuring that the pc outlives the
   // LegacyStatsCollector instance.
-  explicit LegacyStatsCollector(PeerConnectionInternal* pc);
+  LegacyStatsCollector(PeerConnectionInternal* pc, Clock& clock);
   virtual ~LegacyStatsCollector();
 
   // Adds a MediaStream with tracks that can be used as a `selector` in a call
@@ -199,6 +200,7 @@
   TrackIdMap track_ids_;
   // Raw pointer to the peer connection the statistics are gathered from.
   PeerConnectionInternal* const pc_;
+  Clock& clock_;
   int64_t cache_timestamp_ms_ RTC_GUARDED_BY(pc_->signaling_thread()) = 0;
   double stats_gathering_started_;
   const bool use_standard_bytes_stats_;
diff --git a/pc/legacy_stats_collector_unittest.cc b/pc/legacy_stats_collector_unittest.cc
index 8ec0c42..0f62cb0 100644
--- a/pc/legacy_stats_collector_unittest.cc
+++ b/pc/legacy_stats_collector_unittest.cc
@@ -32,6 +32,7 @@
 #include "api/peer_connection_interface.h"
 #include "api/rtp_sender_interface.h"
 #include "api/scoped_refptr.h"
+#include "api/units/timestamp.h"
 #include "call/call.h"
 #include "media/base/media_channel.h"
 #include "p2p/base/connection_info.h"
@@ -57,6 +58,7 @@
 #include "rtc_base/ssl_identity.h"
 #include "rtc_base/ssl_stream_adapter.h"
 #include "rtc_base/thread.h"
+#include "system_wrappers/include/clock.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
 
@@ -580,8 +582,8 @@
 
 class LegacyStatsCollectorForTest : public LegacyStatsCollector {
  public:
-  explicit LegacyStatsCollectorForTest(PeerConnectionInternal* pc)
-      : LegacyStatsCollector(pc), time_now_(19477) {}
+  explicit LegacyStatsCollectorForTest(PeerConnectionInternal* pc, Clock& clock)
+      : LegacyStatsCollector(pc, clock), time_now_(19477) {}
 
   double GetTimeNow() override { return time_now_; }
 
@@ -597,7 +599,7 @@
 
   std::unique_ptr<LegacyStatsCollectorForTest> CreateStatsCollector(
       PeerConnectionInternal* pc) {
-    return std::make_unique<LegacyStatsCollectorForTest>(pc);
+    return std::make_unique<LegacyStatsCollectorForTest>(pc, clock_);
   }
 
   void VerifyAudioTrackStats(FakeAudioTrack* audio_track,
@@ -723,6 +725,7 @@
   }
 
  private:
+  SimulatedClock clock_{Timestamp::Millis(1337)};
   AutoThread main_thread_;
 };
 
diff --git a/pc/peer_connection.cc b/pc/peer_connection.cc
index 7c32a90..81e3137 100644
--- a/pc/peer_connection.cc
+++ b/pc/peer_connection.cc
@@ -539,7 +539,7 @@
           /*alive=*/call_ != nullptr,
           worker_thread())),
       call_ptr_(call_.get()),
-      legacy_stats_(std::make_unique<LegacyStatsCollector>(this)),
+      legacy_stats_(std::make_unique<LegacyStatsCollector>(this, env_.clock())),
       stats_collector_(RTCStatsCollector::Create(this, env_)),
       // RFC 3264: The numeric value of the session id and version in the
       // o line MUST be representable with a "64 bit signed integer".