Injecting Clock in video receive.

Bug: webrtc:10365
Change-Id: Id20fca5b8ad13c133e05efa8972d8f5679507064
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/125192
Commit-Queue: Sebastian Jansson <srte@webrtc.org>
Reviewed-by: Åsa Persson <asapersson@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26958}
diff --git a/call/call.cc b/call/call.cc
index fc32205..35727b3 100644
--- a/call/call.cc
+++ b/call/call.cc
@@ -890,7 +890,7 @@
   VideoReceiveStream* receive_stream = new VideoReceiveStream(
       task_queue_factory_, &video_receiver_controller_, num_cpu_cores_,
       transport_send_ptr_->packet_router(), std::move(configuration),
-      module_process_thread_.get(), call_stats_.get());
+      module_process_thread_.get(), call_stats_.get(), clock_);
 
   const webrtc::VideoReceiveStream::Config& config = receive_stream->config();
   {
@@ -962,7 +962,7 @@
     // TODO(nisse): Fix constructor so that it can be moved outside of
     // this locked scope.
     receive_stream = new FlexfecReceiveStreamImpl(
-        &video_receiver_controller_, config, recovered_packet_receiver,
+        clock_, &video_receiver_controller_, config, recovered_packet_receiver,
         call_stats_.get(), module_process_thread_.get());
 
     RTC_DCHECK(receive_rtp_config_.find(config.remote_ssrc) ==
diff --git a/call/flexfec_receive_stream_impl.cc b/call/flexfec_receive_stream_impl.cc
index 8154715..645df78 100644
--- a/call/flexfec_receive_stream_impl.cc
+++ b/call/flexfec_receive_stream_impl.cc
@@ -80,6 +80,7 @@
 
 // TODO(brandtr): Update this function when we support multistream protection.
 std::unique_ptr<FlexfecReceiver> MaybeCreateFlexfecReceiver(
+    Clock* clock,
     const FlexfecReceiveStream::Config& config,
     RecoveredPacketReceiver* recovered_packet_receiver) {
   if (config.payload_type < 0) {
@@ -112,19 +113,20 @@
     return nullptr;
   }
   RTC_DCHECK_EQ(1U, config.protected_media_ssrcs.size());
-  return std::unique_ptr<FlexfecReceiver>(
-      new FlexfecReceiver(config.remote_ssrc, config.protected_media_ssrcs[0],
-                          recovered_packet_receiver));
+  return std::unique_ptr<FlexfecReceiver>(new FlexfecReceiver(
+      clock, config.remote_ssrc, config.protected_media_ssrcs[0],
+      recovered_packet_receiver));
 }
 
 std::unique_ptr<RtpRtcp> CreateRtpRtcpModule(
+    Clock* clock,
     ReceiveStatistics* receive_statistics,
     Transport* rtcp_send_transport,
     RtcpRttStats* rtt_stats) {
   RtpRtcp::Configuration configuration;
   configuration.audio = false;
   configuration.receiver_only = true;
-  configuration.clock = Clock::GetRealTimeClock();
+  configuration.clock = clock;
   configuration.receive_statistics = receive_statistics;
   configuration.outgoing_transport = rtcp_send_transport;
   configuration.rtt_stats = rtt_stats;
@@ -135,16 +137,19 @@
 }  // namespace
 
 FlexfecReceiveStreamImpl::FlexfecReceiveStreamImpl(
+    Clock* clock,
     RtpStreamReceiverControllerInterface* receiver_controller,
     const Config& config,
     RecoveredPacketReceiver* recovered_packet_receiver,
     RtcpRttStats* rtt_stats,
     ProcessThread* process_thread)
     : config_(config),
-      receiver_(MaybeCreateFlexfecReceiver(config_, recovered_packet_receiver)),
-      rtp_receive_statistics_(
-          ReceiveStatistics::Create(Clock::GetRealTimeClock())),
-      rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_.get(),
+      receiver_(MaybeCreateFlexfecReceiver(clock,
+                                           config_,
+                                           recovered_packet_receiver)),
+      rtp_receive_statistics_(ReceiveStatistics::Create(clock)),
+      rtp_rtcp_(CreateRtpRtcpModule(clock,
+                                    rtp_receive_statistics_.get(),
                                     config_.rtcp_send_transport,
                                     rtt_stats)),
       process_thread_(process_thread) {
diff --git a/call/flexfec_receive_stream_impl.h b/call/flexfec_receive_stream_impl.h
index 6bcbc7c..d4fdc74 100644
--- a/call/flexfec_receive_stream_impl.h
+++ b/call/flexfec_receive_stream_impl.h
@@ -15,6 +15,7 @@
 
 #include "call/flexfec_receive_stream.h"
 #include "call/rtp_packet_sink_interface.h"
+#include "system_wrappers/include/clock.h"
 
 namespace webrtc {
 
@@ -31,6 +32,7 @@
 class FlexfecReceiveStreamImpl : public FlexfecReceiveStream {
  public:
   FlexfecReceiveStreamImpl(
+      Clock* clock,
       RtpStreamReceiverControllerInterface* receiver_controller,
       const Config& config,
       RecoveredPacketReceiver* recovered_packet_receiver,
diff --git a/call/flexfec_receive_stream_unittest.cc b/call/flexfec_receive_stream_unittest.cc
index a1bacf9..4728c8f 100644
--- a/call/flexfec_receive_stream_unittest.cc
+++ b/call/flexfec_receive_stream_unittest.cc
@@ -89,8 +89,8 @@
       : config_(CreateDefaultConfig(&rtcp_send_transport_)) {
     EXPECT_CALL(process_thread_, RegisterModule(_, _)).Times(1);
     receive_stream_ = absl::make_unique<FlexfecReceiveStreamImpl>(
-        &rtp_stream_receiver_controller_, config_, &recovered_packet_receiver_,
-        &rtt_stats_, &process_thread_);
+        Clock::GetRealTimeClock(), &rtp_stream_receiver_controller_, config_,
+        &recovered_packet_receiver_, &rtt_stats_, &process_thread_);
   }
 
   ~FlexfecReceiveStreamTest() {
@@ -145,9 +145,9 @@
 
   testing::StrictMock<MockRecoveredPacketReceiver> recovered_packet_receiver;
   EXPECT_CALL(process_thread_, RegisterModule(_, _)).Times(1);
-  FlexfecReceiveStreamImpl receive_stream(&rtp_stream_receiver_controller_,
-                                          config_, &recovered_packet_receiver,
-                                          &rtt_stats_, &process_thread_);
+  FlexfecReceiveStreamImpl receive_stream(
+      Clock::GetRealTimeClock(), &rtp_stream_receiver_controller_, config_,
+      &recovered_packet_receiver, &rtt_stats_, &process_thread_);
 
   EXPECT_CALL(recovered_packet_receiver,
               OnRecoveredPacket(_, kRtpHeaderSize + kPayloadLength[1]));
diff --git a/modules/rtp_rtcp/include/flexfec_receiver.h b/modules/rtp_rtcp/include/flexfec_receiver.h
index f0ed576..2426559 100644
--- a/modules/rtp_rtcp/include/flexfec_receiver.h
+++ b/modules/rtp_rtcp/include/flexfec_receiver.h
@@ -30,6 +30,10 @@
   FlexfecReceiver(uint32_t ssrc,
                   uint32_t protected_media_ssrc,
                   RecoveredPacketReceiver* recovered_packet_receiver);
+  FlexfecReceiver(Clock* clock,
+                  uint32_t ssrc,
+                  uint32_t protected_media_ssrc,
+                  RecoveredPacketReceiver* recovered_packet_receiver);
   ~FlexfecReceiver();
 
   // Inserts a received packet (can be either media or FlexFEC) into the
diff --git a/modules/rtp_rtcp/source/flexfec_receiver.cc b/modules/rtp_rtcp/source/flexfec_receiver.cc
index 4cf58d1..1750927 100644
--- a/modules/rtp_rtcp/source/flexfec_receiver.cc
+++ b/modules/rtp_rtcp/source/flexfec_receiver.cc
@@ -36,12 +36,22 @@
     uint32_t ssrc,
     uint32_t protected_media_ssrc,
     RecoveredPacketReceiver* recovered_packet_receiver)
+    : FlexfecReceiver(Clock::GetRealTimeClock(),
+                      ssrc,
+                      protected_media_ssrc,
+                      recovered_packet_receiver) {}
+
+FlexfecReceiver::FlexfecReceiver(
+    Clock* clock,
+    uint32_t ssrc,
+    uint32_t protected_media_ssrc,
+    RecoveredPacketReceiver* recovered_packet_receiver)
     : ssrc_(ssrc),
       protected_media_ssrc_(protected_media_ssrc),
       erasure_code_(
           ForwardErrorCorrection::CreateFlexfec(ssrc, protected_media_ssrc)),
       recovered_packet_receiver_(recovered_packet_receiver),
-      clock_(Clock::GetRealTimeClock()),
+      clock_(clock),
       last_recovered_packet_ms_(-1) {
   // It's OK to create this object on a different thread/task queue than
   // the one used during main operation.
diff --git a/modules/rtp_rtcp/source/flexfec_receiver_unittest.cc b/modules/rtp_rtcp/source/flexfec_receiver_unittest.cc
index 378cf7d..d19e575 100644
--- a/modules/rtp_rtcp/source/flexfec_receiver_unittest.cc
+++ b/modules/rtp_rtcp/source/flexfec_receiver_unittest.cc
@@ -49,8 +49,10 @@
   FlexfecReceiverForTest(uint32_t ssrc,
                          uint32_t protected_media_ssrc,
                          RecoveredPacketReceiver* recovered_packet_receiver)
-      : FlexfecReceiver(ssrc, protected_media_ssrc, recovered_packet_receiver) {
-  }
+      : FlexfecReceiver(Clock::GetRealTimeClock(),
+                        ssrc,
+                        protected_media_ssrc,
+                        recovered_packet_receiver) {}
   // Expose methods for tests.
   using FlexfecReceiver::AddReceivedPacket;
   using FlexfecReceiver::ProcessReceivedPacket;
@@ -466,7 +468,7 @@
   } loopback_recovered_packet_receiver;
 
   // Feed recovered packets back into |receiver|.
-  FlexfecReceiver receiver(kFlexfecSsrc, kMediaSsrc,
+  FlexfecReceiver receiver(Clock::GetRealTimeClock(), kFlexfecSsrc, kMediaSsrc,
                            &loopback_recovered_packet_receiver);
   loopback_recovered_packet_receiver.SetReceiver(&receiver);
 
@@ -590,7 +592,7 @@
   } loopback_recovered_packet_receiver;
 
   // Feed recovered packets back into |receiver|.
-  FlexfecReceiver receiver(kFlexfecSsrc, kMediaSsrc,
+  FlexfecReceiver receiver(Clock::GetRealTimeClock(), kFlexfecSsrc, kMediaSsrc,
                            &loopback_recovered_packet_receiver);
   loopback_recovered_packet_receiver.SetReceiver(&receiver);
 
diff --git a/video/rtp_video_stream_receiver.cc b/video/rtp_video_stream_receiver.cc
index 5032f0e..65ec75d 100644
--- a/video/rtp_video_stream_receiver.cc
+++ b/video/rtp_video_stream_receiver.cc
@@ -52,12 +52,14 @@
 }  // namespace
 
 std::unique_ptr<RtpRtcp> CreateRtpRtcpModule(
+    Clock* clock,
     ReceiveStatistics* receive_statistics,
     Transport* outgoing_transport,
     RtcpRttStats* rtt_stats,
     RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer,
     TransportSequenceNumberAllocator* transport_sequence_number_allocator) {
   RtpRtcp::Configuration configuration;
+  configuration.clock = clock;
   configuration.audio = false;
   configuration.receiver_only = true;
   configuration.receive_statistics = receive_statistics;
@@ -83,6 +85,7 @@
 static const int kPacketLogIntervalMs = 10000;
 
 RtpVideoStreamReceiver::RtpVideoStreamReceiver(
+    Clock* clock,
     Transport* transport,
     RtcpRttStats* rtt_stats,
     PacketRouter* packet_router,
@@ -94,17 +97,18 @@
     KeyFrameRequestSender* keyframe_request_sender,
     video_coding::OnCompleteFrameCallback* complete_frame_callback,
     rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor)
-    : clock_(Clock::GetRealTimeClock()),
+    : clock_(clock),
       config_(*config),
       packet_router_(packet_router),
       process_thread_(process_thread),
-      ntp_estimator_(clock_),
+      ntp_estimator_(clock),
       rtp_header_extensions_(config_.rtp.extensions),
       rtp_receive_statistics_(rtp_receive_statistics),
       ulpfec_receiver_(UlpfecReceiver::Create(config->rtp.remote_ssrc, this)),
       receiving_(false),
       last_packet_log_ms_(-1),
-      rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_,
+      rtp_rtcp_(CreateRtpRtcpModule(clock,
+                                    rtp_receive_statistics_,
                                     transport,
                                     rtt_stats,
                                     receive_stats_proxy,
diff --git a/video/rtp_video_stream_receiver.h b/video/rtp_video_stream_receiver.h
index 9e50dd3..3824680 100644
--- a/video/rtp_video_stream_receiver.h
+++ b/video/rtp_video_stream_receiver.h
@@ -67,6 +67,7 @@
                                public OnDecryptionStatusChangeCallback {
  public:
   RtpVideoStreamReceiver(
+      Clock* clock,
       Transport* transport,
       RtcpRttStats* rtt_stats,
       PacketRouter* packet_router,
diff --git a/video/rtp_video_stream_receiver_unittest.cc b/video/rtp_video_stream_receiver_unittest.cc
index c6fb13d..8521732 100644
--- a/video/rtp_video_stream_receiver_unittest.cc
+++ b/video/rtp_video_stream_receiver_unittest.cc
@@ -130,8 +130,8 @@
     rtp_receive_statistics_ =
         absl::WrapUnique(ReceiveStatistics::Create(Clock::GetRealTimeClock()));
     rtp_video_stream_receiver_ = absl::make_unique<RtpVideoStreamReceiver>(
-        &mock_transport_, nullptr, &packet_router_, &config_,
-        rtp_receive_statistics_.get(), nullptr, process_thread_.get(),
+        Clock::GetRealTimeClock(), &mock_transport_, nullptr, &packet_router_,
+        &config_, rtp_receive_statistics_.get(), nullptr, process_thread_.get(),
         &mock_nack_sender_, &mock_key_frame_request_sender_,
         &mock_on_complete_frame_callback_, nullptr);
   }
diff --git a/video/video_receive_stream.cc b/video/video_receive_stream.cc
index 9d84fd3..904e0a5 100644
--- a/video/video_receive_stream.cc
+++ b/video/video_receive_stream.cc
@@ -192,7 +192,8 @@
                       timing_.get(),
                       this,   // NackSender
                       this),  // KeyFrameRequestSender
-      rtp_video_stream_receiver_(&transport_adapter_,
+      rtp_video_stream_receiver_(clock_,
+                                 &transport_adapter_,
                                  call_stats,
                                  packet_router,
                                  &config_,
@@ -259,7 +260,8 @@
     PacketRouter* packet_router,
     VideoReceiveStream::Config config,
     ProcessThread* process_thread,
-    CallStats* call_stats)
+    CallStats* call_stats,
+    Clock* clock)
     : VideoReceiveStream(task_queue_factory,
                          receiver_controller,
                          num_cpu_cores,
@@ -267,8 +269,8 @@
                          std::move(config),
                          process_thread,
                          call_stats,
-                         Clock::GetRealTimeClock(),
-                         new VCMTiming(Clock::GetRealTimeClock())) {}
+                         clock,
+                         new VCMTiming(clock)) {}
 
 VideoReceiveStream::~VideoReceiveStream() {
   RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
diff --git a/video/video_receive_stream.h b/video/video_receive_stream.h
index ada5b7b..3730505 100644
--- a/video/video_receive_stream.h
+++ b/video/video_receive_stream.h
@@ -68,7 +68,8 @@
                      PacketRouter* packet_router,
                      VideoReceiveStream::Config config,
                      ProcessThread* process_thread,
-                     CallStats* call_stats);
+                     CallStats* call_stats,
+                     Clock* clock);
   ~VideoReceiveStream() override;
 
   const Config& config() const { return config_; }