mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-27 04:35:34 +08:00
[feat] use an adapter layer to convert SystemClock into webrtc Clock
This commit is contained in:
@@ -43,25 +43,51 @@ NtpTime TimeMicrosToNtp(int64_t time_us) {
|
||||
|
||||
} // namespace
|
||||
|
||||
class RealTimeClock : public Clock {
|
||||
class WebrtcClock : public Clock {
|
||||
public:
|
||||
RealTimeClock() = default;
|
||||
WebrtcClock(std::shared_ptr<SystemClock> system_clock)
|
||||
: system_clock_(system_clock) {}
|
||||
WebrtcClock() = delete;
|
||||
|
||||
Timestamp CurrentTime() override {
|
||||
return Timestamp::Micros(rtc::TimeMicros());
|
||||
return Timestamp::Micros(system_clock_->CurrentTimeUs());
|
||||
}
|
||||
|
||||
NtpTime ConvertTimestampToNtpTime(Timestamp timestamp) override {
|
||||
return TimeMicrosToNtp(timestamp.us());
|
||||
int64_t time_us = timestamp.us();
|
||||
constexpr int64_t kNtpJan1970Sec = 2208988800;
|
||||
int64_t clock_time = system_clock_->CurrentTimeUs();
|
||||
int64_t utc_time = system_clock_->CurrentUtcTimeUs();
|
||||
static int64_t ntp_offset_us =
|
||||
utc_time - clock_time + kNtpJan1970Sec * rtc::kNumMicrosecsPerSec;
|
||||
|
||||
int64_t time_ntp_us = time_us + ntp_offset_us;
|
||||
|
||||
// Convert seconds to uint32 through uint64 for a well-defined cast.
|
||||
// A wrap around, which will happen in 2036, is expected for NTP time.
|
||||
uint32_t ntp_seconds =
|
||||
static_cast<uint64_t>(time_ntp_us / rtc::kNumMicrosecsPerSec);
|
||||
|
||||
// Scale fractions of the second to NTP resolution.
|
||||
constexpr int64_t kNtpFractionsInSecond = 1LL << 32;
|
||||
int64_t us_fractions = time_ntp_us % rtc::kNumMicrosecsPerSec;
|
||||
uint32_t ntp_fractions =
|
||||
us_fractions * kNtpFractionsInSecond / rtc::kNumMicrosecsPerSec;
|
||||
|
||||
return NtpTime(ntp_seconds, ntp_fractions);
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<SystemClock> system_clock_;
|
||||
};
|
||||
|
||||
Clock* Clock::GetRealTimeClock() {
|
||||
static Clock* const clock = new RealTimeClock();
|
||||
Clock* Clock::GetWebrtcClock(std::shared_ptr<SystemClock> system_clock) {
|
||||
static Clock* const clock = new WebrtcClock(system_clock);
|
||||
return clock;
|
||||
}
|
||||
|
||||
std::shared_ptr<Clock> Clock::GetRealTimeClockShared() {
|
||||
return std::make_shared<RealTimeClock>();
|
||||
std::shared_ptr<Clock> Clock::GetWebrtcClockShared(
|
||||
std::shared_ptr<SystemClock> system_clock) {
|
||||
return std::make_shared<WebrtcClock>(system_clock);
|
||||
}
|
||||
} // namespace webrtc
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
#include "api/ntp/ntp_time.h"
|
||||
#include "api/units/timestamp.h"
|
||||
#include "clock/system_clock.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
@@ -65,8 +66,9 @@ class Clock {
|
||||
}
|
||||
|
||||
// Returns an instance of the real-time system clock implementation.
|
||||
static Clock* GetRealTimeClock();
|
||||
static std::shared_ptr<Clock> GetRealTimeClockShared();
|
||||
static Clock* GetWebrtcClock(std::shared_ptr<SystemClock> system_clock);
|
||||
static std::shared_ptr<Clock> GetWebrtcClockShared(
|
||||
std::shared_ptr<SystemClock> system_clock);
|
||||
};
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
@@ -69,15 +69,15 @@ int64_t SystemClock::CurrentNtpTime() {
|
||||
}
|
||||
|
||||
int64_t SystemClock::CurrentNtpTimeMs() {
|
||||
return CurrentTimeMs() + kNtpEpochOffset * 1000LL;
|
||||
return CurrentUtcTimeMs() + kNtpEpochOffset * 1000LL;
|
||||
}
|
||||
|
||||
int64_t SystemClock::CurrentNtpTimeUs() {
|
||||
return CurrentTimeUs() + kNtpEpochOffset * 1000000LL;
|
||||
return CurrentUtcTimeUs() + kNtpEpochOffset * 1000000LL;
|
||||
}
|
||||
|
||||
int64_t SystemClock::CurrentNtpTimeNs() {
|
||||
return CurrentTimeNs() + kNtpEpochOffset * 1000000000LL;
|
||||
return CurrentUtcTimeNs() + kNtpEpochOffset * 1000000000LL;
|
||||
}
|
||||
|
||||
int64_t SystemClock::CurrentUtcTimeNs() {
|
||||
@@ -111,12 +111,3 @@ int64_t SystemClock::CurrentUtcTimeMs() {
|
||||
int64_t SystemClock::CurrentUtcTime() {
|
||||
return CurrentUtcTimeNs() / 1000000000LL;
|
||||
}
|
||||
|
||||
static SystemClock* GetSystemClock() {
|
||||
static SystemClock* const clock = new SystemClock();
|
||||
return clock;
|
||||
}
|
||||
|
||||
static std::shared_ptr<SystemClock> GetSystemClockShared() {
|
||||
return std::make_shared<SystemClock>();
|
||||
}
|
||||
@@ -33,7 +33,4 @@ class SystemClock {
|
||||
int64_t CurrentUtcTimeNs();
|
||||
};
|
||||
|
||||
static SystemClock* GetSystemClock();
|
||||
static std::shared_ptr<SystemClock> GetSystemClockShared();
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user