diff --git a/system_wrappers/BUILD.gn b/system_wrappers/BUILD.gn index 7059fac740..1f17a94209 100644 --- a/system_wrappers/BUILD.gn +++ b/system_wrappers/BUILD.gn @@ -32,6 +32,7 @@ rtc_static_library("system_wrappers") { deps = [ ":cpu_features_api", "../api:array_view", + "../api/units:timestamp", "../modules:module_api_public", "../rtc_base:checks", "../rtc_base/synchronization:rw_lock_wrapper", diff --git a/system_wrappers/include/clock.h b/system_wrappers/include/clock.h index b2c6c5fddb..09d2785179 100644 --- a/system_wrappers/include/clock.h +++ b/system_wrappers/include/clock.h @@ -14,6 +14,7 @@ #include #include +#include "api/units/timestamp.h" #include "rtc_base/synchronization/rw_lock_wrapper.h" #include "system_wrappers/include/ntp_time.h" @@ -29,14 +30,12 @@ const double kMagicNtpFractionalUnit = 4.294967296E+9; class Clock { public: virtual ~Clock() {} - - // Return a timestamp in milliseconds relative to some arbitrary source; the - // source is fixed for this clock. - virtual int64_t TimeInMilliseconds() = 0; - - // Return a timestamp in microseconds relative to some arbitrary source; the - // source is fixed for this clock. - virtual int64_t TimeInMicroseconds() = 0; + // Return a timestamp relative to an unspecified epoch. + virtual Timestamp CurrentTime() { + return Timestamp::us(TimeInMicroseconds()); + } + virtual int64_t TimeInMilliseconds() { return CurrentTime().ms(); } + virtual int64_t TimeInMicroseconds() { return CurrentTime().us(); } // Retrieve an NTP absolute timestamp. virtual NtpTime CurrentNtpTime() = 0; @@ -56,16 +55,13 @@ class Clock { class SimulatedClock : public Clock { public: explicit SimulatedClock(int64_t initial_time_us); + explicit SimulatedClock(Timestamp initial_time); ~SimulatedClock() override; - // Return a timestamp in milliseconds relative to some arbitrary source; the - // source is fixed for this clock. - int64_t TimeInMilliseconds() override; - - // Return a timestamp in microseconds relative to some arbitrary source; the - // source is fixed for this clock. - int64_t TimeInMicroseconds() override; + // Return a timestamp relative to some arbitrary source; the source is fixed + // for this clock. + Timestamp CurrentTime() override; // Retrieve an NTP absolute timestamp. NtpTime CurrentNtpTime() override; @@ -77,9 +73,10 @@ class SimulatedClock : public Clock { // microseconds. void AdvanceTimeMilliseconds(int64_t milliseconds); void AdvanceTimeMicroseconds(int64_t microseconds); + void AdvanceTime(TimeDelta delta); private: - int64_t time_us_; + Timestamp time_; std::unique_ptr lock_; }; diff --git a/system_wrappers/source/clock.cc b/system_wrappers/source/clock.cc index f69d13c1db..3eea155711 100644 --- a/system_wrappers/source/clock.cc +++ b/system_wrappers/source/clock.cc @@ -32,6 +32,7 @@ namespace webrtc { class RealTimeClock : public Clock { + Timestamp CurrentTime() override { return Timestamp::us(rtc::TimeMicros()); } // Return a timestamp in milliseconds relative to some arbitrary source; the // source is fixed for this clock. int64_t TimeInMilliseconds() override { return rtc::TimeMillis(); } @@ -236,18 +237,16 @@ Clock* Clock::GetRealTimeClock() { } SimulatedClock::SimulatedClock(int64_t initial_time_us) - : time_us_(initial_time_us), lock_(RWLockWrapper::CreateRWLock()) {} + : SimulatedClock(Timestamp::us(initial_time_us)) {} + +SimulatedClock::SimulatedClock(Timestamp initial_time) + : time_(initial_time), lock_(RWLockWrapper::CreateRWLock()) {} SimulatedClock::~SimulatedClock() {} -int64_t SimulatedClock::TimeInMilliseconds() { +Timestamp SimulatedClock::CurrentTime() { ReadLockScoped synchronize(*lock_); - return (time_us_ + 500) / 1000; -} - -int64_t SimulatedClock::TimeInMicroseconds() { - ReadLockScoped synchronize(*lock_); - return time_us_; + return time_; } NtpTime SimulatedClock::CurrentNtpTime() { @@ -263,12 +262,16 @@ int64_t SimulatedClock::CurrentNtpInMilliseconds() { } void SimulatedClock::AdvanceTimeMilliseconds(int64_t milliseconds) { - AdvanceTimeMicroseconds(1000 * milliseconds); + AdvanceTime(TimeDelta::ms(milliseconds)); } void SimulatedClock::AdvanceTimeMicroseconds(int64_t microseconds) { + AdvanceTime(TimeDelta::us(microseconds)); +} + +void SimulatedClock::AdvanceTime(TimeDelta delta) { WriteLockScoped synchronize(*lock_); - time_us_ += microseconds; + time_ += delta; } } // namespace webrtc diff --git a/test/drifting_clock.cc b/test/drifting_clock.cc index 41ab2cf59e..2907ad0ec2 100644 --- a/test/drifting_clock.cc +++ b/test/drifting_clock.cc @@ -18,25 +18,19 @@ const float DriftingClock::kNoDrift = 1.0f; const float DriftingClock::kHalfSpeed = 0.5f; DriftingClock::DriftingClock(Clock* clock, float speed) - : clock_(clock), - drift_(speed - 1.0f), - start_time_(clock_->TimeInMicroseconds()) { + : clock_(clock), drift_(speed - 1.0f), start_time_(clock_->CurrentTime()) { RTC_CHECK(clock); RTC_CHECK_GT(speed, 0.0f); } -float DriftingClock::Drift() const { - int64_t now = clock_->TimeInMicroseconds(); +TimeDelta DriftingClock::Drift() const { + auto now = clock_->CurrentTime(); RTC_DCHECK_GE(now, start_time_); return (now - start_time_) * drift_; } -int64_t DriftingClock::TimeInMilliseconds() { - return clock_->TimeInMilliseconds() + Drift() / 1000.; -} - -int64_t DriftingClock::TimeInMicroseconds() { - return clock_->TimeInMicroseconds() + Drift(); +Timestamp DriftingClock::CurrentTime() { + return clock_->CurrentTime() + Drift() / 1000.; } NtpTime DriftingClock::CurrentNtpTime() { @@ -45,12 +39,12 @@ NtpTime DriftingClock::CurrentNtpTime() { NtpTime ntp = clock_->CurrentNtpTime(); uint64_t total_fractions = static_cast(ntp); - total_fractions += Drift() * kNtpFracPerMicroSecond; + total_fractions += Drift().us() * kNtpFracPerMicroSecond; return NtpTime(total_fractions); } int64_t DriftingClock::CurrentNtpInMilliseconds() { - return clock_->CurrentNtpInMilliseconds() + Drift() / 1000.; + return clock_->CurrentNtpInMilliseconds() + Drift().ms(); } } // namespace test } // namespace webrtc diff --git a/test/drifting_clock.h b/test/drifting_clock.h index 46e3c1b7a5..9ed3923588 100644 --- a/test/drifting_clock.h +++ b/test/drifting_clock.h @@ -30,17 +30,16 @@ class DriftingClock : public Clock { static float PercentsFaster(float percent) { return 1.0f + percent / 100.0f; } static float PercentsSlower(float percent) { return 1.0f - percent / 100.0f; } - int64_t TimeInMilliseconds() override; - int64_t TimeInMicroseconds() override; + Timestamp CurrentTime() override; NtpTime CurrentNtpTime() override; int64_t CurrentNtpInMilliseconds() override; private: - float Drift() const; + TimeDelta Drift() const; Clock* const clock_; const float drift_; - const int64_t start_time_; + const Timestamp start_time_; }; } // namespace test } // namespace webrtc