2020-10-16

web rtc 时间

1. webrtc\base\timeutils.cc

uint64_t SystemTimeNanos()
源码:

uint64_t SystemTimeNanos() {
  int64_t ticks;
#if defined(WEBRTC_MAC)
  static mach_timebase_info_data_t timebase;
  if (timebase.denom == 0) {
    // Get the timebase if this is the first time we run.
    // Recommended by Apple's QA1398.
    if (mach_timebase_info(&timebase) != KERN_SUCCESS) {
      RTC_DCHECK(false);
    }
  }
  // Use timebase to convert absolute time tick units into nanoseconds.
  ticks = mach_absolute_time() * timebase.numer / timebase.denom;
#elif defined(WEBRTC_POSIX)
  struct timespec ts;
  // TODO(deadbeef): Do we need to handle the case when CLOCK_MONOTONIC is not
  // supported?
  clock_gettime(CLOCK_MONOTONIC, &ts);
  ticks = kNumNanosecsPerSec * static_cast<int64_t>(ts.tv_sec) +
          static_cast<int64_t>(ts.tv_nsec);
#elif defined(WEBRTC_WIN)
  static volatile LONG last_timegettime = 0;
  static volatile int64_t num_wrap_timegettime = 0;
  volatile LONG* last_timegettime_ptr = &last_timegettime;
  DWORD now = timeGetTime();
  // Atomically update the last gotten time
  DWORD old = InterlockedExchange(last_timegettime_ptr, now);
  if (now < old) {
    // If now is earlier than old, there may have been a race between threads.
    // 0x0fffffff ~3.1 days, the code will not take that long to execute
    // so it must have been a wrap around.
    if (old > 0xf0000000 && now < 0x0fffffff) {
      num_wrap_timegettime++;
    }
  }
  ticks = now + (num_wrap_timegettime << 32);
  // TODO(deadbeef): Calculate with nanosecond precision. Otherwise, we're
  // just wasting a multiply and divide when doing Time() on Windows.
  ticks = ticks * kNumNanosecsPerMillisec;
#else
#error Unsupported platform.
#endif
  return ticks;
}

关注 linux 情况的

clock_gettime(CLOCK_MONOTONIC, &ts);
从系统启动这一刻起开始计时,不受系统时间被用户改变的影响
https://www.cnblogs.com/suyunhong/p/4920011.html
ticks = kNumNanosecsPerSec * static_cast<int64_t>(ts.tv_sec) + static_cast<int64_t>(ts.tv_nsec);
转换为纳秒单位

int64_t SystemTimeMillis()
获取当前毫秒时间 64位表示

uint32_t Time32()
获取当前毫秒时间 32位表示

int64_t TimeMillis()
获取当前毫秒时间 64位表示

uint64_t TimeMicros()
获取当前微秒时间

uint64_t TimeNanos()
获取当前纳秒时间

int64_t TmToSeconds(const std::tm& tm)
1900-01-01 00:00起的记录的时间转换为1970-01-01 00:00起记录的时间,单位为秒
Convert from std::tm, which is relative to 1900-01-01 00:00 to number of seconds from 1970-01-01 00:00 ("epoch"). Don't return time_t since that is still 32 bits on many systems.

1. \webrtc\system_wrappers\source\clock.cc

Clock.jpg

UnixRealTimeClock 是 linux Clock接口的linux的linux实现获取时间
timeval UnixRealTimeClock::CurrentTimeVal()
1970年1月1日0时0分0秒(格林威治时间)

int64_t CurrentNtpInMilliseconds()
格林时间(UTC)1900年1月1日以来的毫秒数

int64_t RealTimeClock::TimeInMilliseconds()
调用全局方法TimeMillis() 获得操作系统启动后的毫秒数

  1. 其他
    VideoCaptureInput 里面的delta_ntp_internal_ms_ 算出来后表示操作系统启动的格林时间的好描述
    delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - clock_->TimeInMilliseconds()),
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。