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/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());
}