replace NtpTime->Clock with Clock->NtpTime dependency

BUG=None

Review-Url: https://codereview.webrtc.org/2393723004
Cr-Commit-Position: refs/heads/master@{#16519}
diff --git a/webrtc/modules/rtp_rtcp/source/receive_statistics_impl.cc b/webrtc/modules/rtp_rtcp/source/receive_statistics_impl.cc
index b3e88fc..6d64304 100644
--- a/webrtc/modules/rtp_rtcp/source/receive_statistics_impl.cc
+++ b/webrtc/modules/rtp_rtcp/source/receive_statistics_impl.cc
@@ -17,6 +17,7 @@
 #include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_logging.h"
 #include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_config.h"
 #include "webrtc/modules/rtp_rtcp/source/time_util.h"
+#include "webrtc/system_wrappers/include/clock.h"
 
 namespace webrtc {
 
@@ -78,7 +79,7 @@
   // are received, 4 will be ignored.
   if (in_order) {
     // Current time in samples.
-    NtpTime receive_time(*clock_);
+    NtpTime receive_time = clock_->CurrentNtpTime();
 
     // Wrong if we use RetransmitOfOldPacket.
     if (receive_counters_.transmitted.packets > 1 &&
diff --git a/webrtc/modules/rtp_rtcp/source/rtcp_receiver.cc b/webrtc/modules/rtp_rtcp/source/rtcp_receiver.cc
index e8d2ff2..b19ce38 100644
--- a/webrtc/modules/rtp_rtcp/source/rtcp_receiver.cc
+++ b/webrtc/modules/rtp_rtcp/source/rtcp_receiver.cc
@@ -260,7 +260,7 @@
 
   // Get the delay since last received report (RFC 3611).
   uint32_t receive_time_ntp = CompactNtp(last_received_xr_ntp_);
-  uint32_t now_ntp = CompactNtp(NtpTime(*clock_));
+  uint32_t now_ntp = CompactNtp(clock_->CurrentNtpTime());
 
   info->delay_since_last_rr = now_ntp - receive_time_ntp;
   return true;
@@ -423,7 +423,7 @@
     remote_sender_info_.sendPacketCount = sender_report.sender_packet_count();
     remote_sender_info_.sendOctetCount = sender_report.sender_octet_count();
 
-    last_received_sr_ntp_.SetCurrent(*clock_);
+    last_received_sr_ntp_ = clock_->CurrentNtpTime();
   } else {
     // We will only store the send report from one source, but
     // we will store all the receive blocks.
@@ -504,7 +504,7 @@
   if (!receiver_only_ && send_time_ntp != 0) {
     uint32_t delay_ntp = report_block.delay_since_last_sr();
     // Local NTP time.
-    uint32_t receive_time_ntp = CompactNtp(NtpTime(*clock_));
+    uint32_t receive_time_ntp = CompactNtp(clock_->CurrentNtpTime());
 
     // RTT in 1/(2^16) seconds.
     uint32_t rtt_ntp = receive_time_ntp - delay_ntp - send_time_ntp;
@@ -709,7 +709,7 @@
                                                 const rtcp::Rrtr& rrtr) {
   remote_time_info_.ssrc = sender_ssrc;
   remote_time_info_.last_rr = CompactNtp(rrtr.ntp());
-  last_received_xr_ntp_.SetCurrent(*clock_);
+  last_received_xr_ntp_ = clock_->CurrentNtpTime();
 }
 
 void RTCPReceiver::HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti) {
@@ -728,7 +728,7 @@
     return;
 
   uint32_t delay_ntp = rti.delay_since_last_rr;
-  uint32_t now_ntp = CompactNtp(NtpTime(*clock_));
+  uint32_t now_ntp = CompactNtp(clock_->CurrentNtpTime());
 
   uint32_t rtt_ntp = now_ntp - delay_ntp - send_time_ntp;
   xr_rr_rtt_ms_ = CompactNtpRttToMs(rtt_ntp);
diff --git a/webrtc/modules/rtp_rtcp/source/rtcp_receiver_unittest.cc b/webrtc/modules/rtp_rtcp/source/rtcp_receiver_unittest.cc
index 1e3a7a2..bc3aca0 100644
--- a/webrtc/modules/rtp_rtcp/source/rtcp_receiver_unittest.cc
+++ b/webrtc/modules/rtp_rtcp/source/rtcp_receiver_unittest.cc
@@ -271,7 +271,7 @@
   EXPECT_EQ(
       -1, rtcp_receiver_.RTT(kSenderSsrc, &rtt_ms, nullptr, nullptr, nullptr));
 
-  uint32_t sent_ntp = CompactNtp(NtpTime(system_clock_));
+  uint32_t sent_ntp = CompactNtp(system_clock_.CurrentNtpTime());
   system_clock_.AdvanceTimeMilliseconds(kRttMs + kDelayMs);
 
   rtcp::SenderReport sr;
@@ -301,7 +301,7 @@
   EXPECT_EQ(
       -1, rtcp_receiver_.RTT(kSenderSsrc, &rtt_ms, nullptr, nullptr, nullptr));
 
-  uint32_t sent_ntp = CompactNtp(NtpTime(system_clock_));
+  uint32_t sent_ntp = CompactNtp(system_clock_.CurrentNtpTime());
   system_clock_.AdvanceTimeMilliseconds(kRttMs + kDelayMs);
 
   rtcp::SenderReport sr;
@@ -774,7 +774,7 @@
 
   InjectRtcpPacket(xr);
 
-  uint32_t compact_ntp_now = CompactNtp(NtpTime(system_clock_));
+  uint32_t compact_ntp_now = CompactNtp(system_clock_.CurrentNtpTime());
   EXPECT_TRUE(rtcp_receiver_.GetAndResetXrRrRtt(&rtt_ms));
   uint32_t rtt_ntp = compact_ntp_now - kDelay - kLastRR;
   EXPECT_NEAR(CompactNtpRttToMs(rtt_ntp), rtt_ms, 1);
@@ -793,7 +793,7 @@
 
   InjectRtcpPacket(xr);
 
-  uint32_t compact_ntp_now = CompactNtp(NtpTime(system_clock_));
+  uint32_t compact_ntp_now = CompactNtp(system_clock_.CurrentNtpTime());
   int64_t rtt_ms = 0;
   EXPECT_TRUE(rtcp_receiver_.GetAndResetXrRrRtt(&rtt_ms));
   uint32_t rtt_ntp = compact_ntp_now - kDelay - kLastRR;
@@ -859,7 +859,7 @@
   const uint32_t kDelayNtp = rand.Rand(0, 0x7fffffff);
   const int64_t kDelayMs = CompactNtpRttToMs(kDelayNtp);
   rtcp_receiver_.SetRtcpXrRrtrStatus(true);
-  NtpTime now(system_clock_);
+  NtpTime now = system_clock_.CurrentNtpTime();
   uint32_t sent_ntp = CompactNtp(now);
   system_clock_.AdvanceTimeMilliseconds(kRttMs + kDelayMs);
 
@@ -879,7 +879,7 @@
   const int64_t kRttMs = rand.Rand(-3600 * 1000, -1);
   const uint32_t kDelayNtp = rand.Rand(0, 0x7fffffff);
   const int64_t kDelayMs = CompactNtpRttToMs(kDelayNtp);
-  NtpTime now(system_clock_);
+  NtpTime now = system_clock_.CurrentNtpTime();
   uint32_t sent_ntp = CompactNtp(now);
   system_clock_.AdvanceTimeMilliseconds(kRttMs + kDelayMs);
   rtcp_receiver_.SetRtcpXrRrtrStatus(true);
diff --git a/webrtc/modules/rtp_rtcp/source/rtcp_sender.cc b/webrtc/modules/rtp_rtcp/source/rtcp_sender.cc
index 423694f..48a016d 100644
--- a/webrtc/modules/rtp_rtcp/source/rtcp_sender.cc
+++ b/webrtc/modules/rtp_rtcp/source/rtcp_sender.cc
@@ -779,7 +779,7 @@
 
     // We need to send our NTP even if we haven't received any reports.
     RtcpContext context(feedback_state, nack_size, nack_list, pictureID,
-                        NtpTime(*clock_));
+                        clock_->CurrentNtpTime());
 
     PrepareReport(feedback_state);
 
diff --git a/webrtc/modules/rtp_rtcp/source/time_util.h b/webrtc/modules/rtp_rtcp/source/time_util.h
index 7bac526..69bbb0a 100644
--- a/webrtc/modules/rtp_rtcp/source/time_util.h
+++ b/webrtc/modules/rtp_rtcp/source/time_util.h
@@ -22,11 +22,6 @@
   uint32_t tmp = (static_cast<uint64_t>(ntp.fractions()) * freq) >> 32;
   return ntp.seconds() * freq + tmp;
 }
-// Return the current RTP timestamp from the NTP timestamp
-// returned by the specified clock.
-inline uint32_t CurrentRtp(const Clock& clock, uint32_t freq) {
-  return NtpToRtp(NtpTime(clock), freq);
-}
 
 // Helper function for compact ntp representation:
 // RFC 3550, Section 4. Time Format.
diff --git a/webrtc/system_wrappers/include/clock.h b/webrtc/system_wrappers/include/clock.h
index a209770..5066844 100644
--- a/webrtc/system_wrappers/include/clock.h
+++ b/webrtc/system_wrappers/include/clock.h
@@ -13,6 +13,7 @@
 
 #include <memory>
 
+#include "webrtc/system_wrappers/include/ntp_time.h"
 #include "webrtc/system_wrappers/include/rw_lock_wrapper.h"
 #include "webrtc/typedefs.h"
 
@@ -43,8 +44,14 @@
   // Retrieve an NTP absolute timestamp in milliseconds.
   virtual int64_t CurrentNtpInMilliseconds() const = 0;
 
+  // TODO(danilchap): Make pure virtual once implemented in derived classed
+  // replacing CurrentNtp function.
+  virtual NtpTime CurrentNtpTime() const;
+
   // Converts an NTP timestamp to a millisecond timestamp.
-  static int64_t NtpToMs(uint32_t seconds, uint32_t fractions);
+  static int64_t NtpToMs(uint32_t seconds, uint32_t fractions) {
+    return NtpTime(seconds, fractions).ToMs();
+  }
 
   // Returns an instance of the real-time system clock implementation.
   static Clock* GetRealTimeClock();
diff --git a/webrtc/system_wrappers/include/ntp_time.h b/webrtc/system_wrappers/include/ntp_time.h
index 9c55419..ecb303a 100644
--- a/webrtc/system_wrappers/include/ntp_time.h
+++ b/webrtc/system_wrappers/include/ntp_time.h
@@ -12,25 +12,17 @@
 
 #include <stdint.h>
 
-#include "webrtc/system_wrappers/include/clock.h"
-
 namespace webrtc {
 
 class NtpTime {
  public:
   NtpTime() : seconds_(0), fractions_(0) {}
-  explicit NtpTime(const Clock& clock) {
-    clock.CurrentNtp(seconds_, fractions_);
-  }
   NtpTime(uint32_t seconds, uint32_t fractions)
       : seconds_(seconds), fractions_(fractions) {}
 
   NtpTime(const NtpTime&) = default;
   NtpTime& operator=(const NtpTime&) = default;
 
-  void SetCurrent(const Clock& clock) {
-    clock.CurrentNtp(seconds_, fractions_);
-  }
   void Set(uint32_t seconds, uint32_t fractions) {
     seconds_ = seconds;
     fractions_ = fractions;
@@ -40,8 +32,12 @@
     fractions_ = 0;
   }
 
-  int64_t ToMs() const { return Clock::NtpToMs(seconds_, fractions_); }
-
+  int64_t ToMs() const {
+    static constexpr double kNtpFracPerMs = 4.294967296E6;  // 2^32 / 1000.
+    const double frac_ms = static_cast<double>(fractions_) / kNtpFracPerMs;
+    return 1000 * static_cast<int64_t>(seconds_) +
+           static_cast<int64_t>(frac_ms + 0.5);
+  }
   // NTP standard (RFC1305, section 3.1) explicitly state value 0/0 is invalid.
   bool Valid() const { return !(seconds_ == 0 && fractions_ == 0); }
 
diff --git a/webrtc/system_wrappers/source/clock.cc b/webrtc/system_wrappers/source/clock.cc
index 05dabd8..f31556f 100644
--- a/webrtc/system_wrappers/source/clock.cc
+++ b/webrtc/system_wrappers/source/clock.cc
@@ -25,12 +25,11 @@
 
 namespace webrtc {
 
-const double kNtpFracPerMs = 4.294967296E6;
-
-int64_t Clock::NtpToMs(uint32_t ntp_secs, uint32_t ntp_frac) {
-  const double ntp_frac_ms = static_cast<double>(ntp_frac) / kNtpFracPerMs;
-  return 1000 * static_cast<int64_t>(ntp_secs) +
-      static_cast<int64_t>(ntp_frac_ms + 0.5);
+NtpTime Clock::CurrentNtpTime() const {
+  uint32_t seconds;
+  uint32_t fractions;
+  CurrentNtp(seconds, fractions);
+  return NtpTime(seconds, fractions);
 }
 
 class RealTimeClock : public Clock {
diff --git a/webrtc/system_wrappers/source/ntp_time_unittest.cc b/webrtc/system_wrappers/source/ntp_time_unittest.cc
index a505789..fe04471 100644
--- a/webrtc/system_wrappers/source/ntp_time_unittest.cc
+++ b/webrtc/system_wrappers/source/ntp_time_unittest.cc
@@ -8,6 +8,7 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
+#include "webrtc/system_wrappers/include/clock.h"
 #include "webrtc/system_wrappers/include/ntp_time.h"
 #include "webrtc/test/gtest.h"
 
@@ -45,21 +46,10 @@
   EXPECT_EQ(ntp1, ntp2);
 }
 
-TEST(NtpTimeTest, SetCurrentIsSameAs1ParameterConstructor) {
-  SimulatedClock clock(0x0123456789abcdef);
-
-  NtpTime ntp1(clock);
-  NtpTime ntp2;
-  EXPECT_NE(ntp1, ntp2);
-
-  ntp2.SetCurrent(clock);
-  EXPECT_EQ(ntp1, ntp2);
-}
-
 TEST(NtpTimeTest, ToMsMeansToNtpMilliseconds) {
   SimulatedClock clock(0x123456789abc);
 
-  NtpTime ntp(clock);
+  NtpTime ntp = clock.CurrentNtpTime();
   EXPECT_EQ(ntp.ToMs(), Clock::NtpToMs(ntp.seconds(), ntp.fractions()));
   EXPECT_EQ(ntp.ToMs(), clock.CurrentNtpInMilliseconds());
 }