Adds unit based interface to Clock class.

Bug: webrtc:9883
Change-Id: Ibebf3ba3918139c34ff2ab83ea4a06a26fae2507
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/140947
Commit-Queue: Sebastian Jansson <srte@webrtc.org>
Reviewed-by: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#28229}
This commit is contained in:
Sebastian Jansson 2019-06-11 08:52:11 +02:00 committed by Commit Bot
parent fd5166c305
commit 4de3115e8f
5 changed files with 37 additions and 43 deletions

View File

@ -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",

View File

@ -14,6 +14,7 @@
#include <stdint.h>
#include <memory>
#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<RWLockWrapper> lock_;
};

View File

@ -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

View File

@ -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<uint64_t>(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

View File

@ -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