tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

SystemTime.cpp (2178B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "SystemTime.h"
      8 
      9 #include "TimeUnits.h"
     10 
     11 namespace mozilla {
     12 
     13 // webrtc::Timestamp may not be negative. `now-base` for the first call to
     14 // WebrtcSystemTime() is always 0, which makes it impossible for libwebrtc
     15 // code to calculate a timestamp older than the first one returned. This
     16 // offset makes sure the clock starts at a value equivalent to roughly 4.5h.
     17 static constexpr webrtc::TimeDelta kWebrtcTimeOffset =
     18    webrtc::TimeDelta::Micros(0x10000000);
     19 
     20 RTCStatsTimestampMakerRealtimeClock::RTCStatsTimestampMakerRealtimeClock(
     21    const dom::RTCStatsTimestampMaker& aTimestampMaker)
     22    : mTimestampMaker(aTimestampMaker) {}
     23 
     24 webrtc::Timestamp RTCStatsTimestampMakerRealtimeClock::CurrentTime() {
     25  return mTimestampMaker.GetNow().ToRealtime();
     26 }
     27 
     28 webrtc::NtpTime RTCStatsTimestampMakerRealtimeClock::ConvertTimestampToNtpTime(
     29    webrtc::Timestamp aRealtime) {
     30  return CreateNtp(
     31      dom::RTCStatsTimestamp::FromRealtime(mTimestampMaker, aRealtime).ToNtp());
     32 }
     33 
     34 TimeStamp WebrtcSystemTimeBase() {
     35  static TimeStamp now = TimeStamp::Now();
     36  return now;
     37 }
     38 
     39 webrtc::Timestamp WebrtcSystemTime() {
     40  const TimeStamp base = WebrtcSystemTimeBase();
     41  const TimeStamp now = TimeStamp::Now();
     42  return webrtc::Timestamp::Micros((now - base).ToMicroseconds()) +
     43         kWebrtcTimeOffset;
     44 }
     45 
     46 webrtc::NtpTime CreateNtp(webrtc::Timestamp aTime) {
     47  const int64_t timeNtpUs = aTime.us();
     48  const uint32_t seconds = static_cast<uint32_t>(timeNtpUs / USECS_PER_S);
     49 
     50  constexpr int64_t fractionsPerSec = 1LL << 32;
     51  const int64_t fractionsUs = timeNtpUs % USECS_PER_S;
     52  const uint32_t fractions = (fractionsUs * fractionsPerSec) / USECS_PER_S;
     53 
     54  return webrtc::NtpTime(seconds, fractions);
     55 }
     56 }  // namespace mozilla
     57 
     58 namespace webrtc {
     59 int64_t SystemTimeNanos() { return mozilla::WebrtcSystemTime().us() * 1000; }
     60 }  // namespace webrtc