blob: b341a5f0613bd01d8ce9226740347c6ef3be0df0 [file] [log] [blame]
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +00001/*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "webrtc/system_wrappers/interface/clock.h"
12
13#if defined(_WIN32)
pbos@webrtc.orgacaf3a12013-05-27 15:07:45 +000014// Windows needs to be included before mmsystem.h
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000015#include <Windows.h>
16#include <WinSock.h>
17#include <MMSystem.h>
18#elif ((defined WEBRTC_LINUX) || (defined WEBRTC_MAC))
19#include <sys/time.h>
20#include <time.h>
21#endif
22
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +000023#include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000024#include "webrtc/system_wrappers/interface/tick_util.h"
25
26namespace webrtc {
27
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000028const double kNtpFracPerMs = 4.294967296E6;
29
30int64_t Clock::NtpToMs(uint32_t ntp_secs, uint32_t ntp_frac) {
31 const double ntp_frac_ms = static_cast<double>(ntp_frac) / kNtpFracPerMs;
32 return 1000 * static_cast<int64_t>(ntp_secs) +
33 static_cast<int64_t>(ntp_frac_ms + 0.5);
34}
35
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000036class RealTimeClock : public Clock {
37 // Return a timestamp in milliseconds relative to some arbitrary source; the
38 // source is fixed for this clock.
pbos@webrtc.orga2a27182013-08-01 17:26:15 +000039 virtual int64_t TimeInMilliseconds() OVERRIDE {
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000040 return TickTime::MillisecondTimestamp();
41 }
42
43 // Return a timestamp in microseconds relative to some arbitrary source; the
44 // source is fixed for this clock.
pbos@webrtc.orga2a27182013-08-01 17:26:15 +000045 virtual int64_t TimeInMicroseconds() OVERRIDE {
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000046 return TickTime::MicrosecondTimestamp();
47 }
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000048
49 // Retrieve an NTP absolute timestamp in seconds and fractions of a second.
pbos@webrtc.orga2a27182013-08-01 17:26:15 +000050 virtual void CurrentNtp(uint32_t& seconds, uint32_t& fractions) OVERRIDE {
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000051 timeval tv = CurrentTimeVal();
52 double microseconds_in_seconds;
53 Adjust(tv, &seconds, &microseconds_in_seconds);
54 fractions = static_cast<uint32_t>(
55 microseconds_in_seconds * kMagicNtpFractionalUnit + 0.5);
56 }
57
58 // Retrieve an NTP absolute timestamp in milliseconds.
pbos@webrtc.orga2a27182013-08-01 17:26:15 +000059 virtual int64_t CurrentNtpInMilliseconds() OVERRIDE {
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000060 timeval tv = CurrentTimeVal();
61 uint32_t seconds;
62 double microseconds_in_seconds;
63 Adjust(tv, &seconds, &microseconds_in_seconds);
64 return 1000 * static_cast<int64_t>(seconds) +
65 static_cast<int64_t>(1000.0 * microseconds_in_seconds + 0.5);
66 }
67
68 protected:
69 virtual timeval CurrentTimeVal() const = 0;
70
71 static void Adjust(const timeval& tv, uint32_t* adjusted_s,
72 double* adjusted_us_in_s) {
73 *adjusted_s = tv.tv_sec + kNtpJan1970;
74 *adjusted_us_in_s = tv.tv_usec / 1e6;
75
76 if (*adjusted_us_in_s >= 1) {
77 *adjusted_us_in_s -= 1;
78 ++*adjusted_s;
79 } else if (*adjusted_us_in_s < -1) {
80 *adjusted_us_in_s += 1;
81 --*adjusted_s;
82 }
83 }
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000084};
85
86#if defined(_WIN32)
87class WindowsRealTimeClock : public RealTimeClock {
88 public:
wu@webrtc.org75718cf2014-05-15 23:54:14 +000089 WindowsRealTimeClock() {}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000090
91 virtual ~WindowsRealTimeClock() {}
92
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000093 protected:
pbos@webrtc.orga2a27182013-08-01 17:26:15 +000094 virtual timeval CurrentTimeVal() const OVERRIDE {
pbos@webrtc.org046deb92013-04-09 09:06:11 +000095 const uint64_t FILETIME_1970 = 0x019db1ded53e8000;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000096
97 FILETIME StartTime;
pbos@webrtc.org046deb92013-04-09 09:06:11 +000098 uint64_t Time;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000099 struct timeval tv;
100
wu@webrtc.org75718cf2014-05-15 23:54:14 +0000101 GetSystemTimeAsFileTime(&StartTime);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000102
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000103 Time = (((uint64_t) StartTime.dwHighDateTime) << 32) +
104 (uint64_t) StartTime.dwLowDateTime;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000105
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000106 // Convert the hecto-nano second time to tv format.
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000107 Time -= FILETIME_1970;
108
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000109 tv.tv_sec = (uint32_t)(Time / (uint64_t)10000000);
110 tv.tv_usec = (uint32_t)((Time % (uint64_t)10000000) / 10);
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000111 return tv;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000112 }
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000113};
114
115#elif ((defined WEBRTC_LINUX) || (defined WEBRTC_MAC))
116class UnixRealTimeClock : public RealTimeClock {
117 public:
118 UnixRealTimeClock() {}
119
120 virtual ~UnixRealTimeClock() {}
121
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000122 protected:
pbos@webrtc.orga2a27182013-08-01 17:26:15 +0000123 virtual timeval CurrentTimeVal() const OVERRIDE {
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000124 struct timeval tv;
125 struct timezone tz;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000126 tz.tz_minuteswest = 0;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000127 tz.tz_dsttime = 0;
128 gettimeofday(&tv, &tz);
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000129 return tv;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000130 }
131};
132#endif
133
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000134Clock* Clock::GetRealTimeClock() {
135#if defined(_WIN32)
wu@webrtc.org75718cf2014-05-15 23:54:14 +0000136 static WindowsRealTimeClock clock;
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000137 return &clock;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000138#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000139 static UnixRealTimeClock clock;
140 return &clock;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000141#else
142 return NULL;
143#endif
144}
145
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000146SimulatedClock::SimulatedClock(int64_t initial_time_us)
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000147 : time_us_(initial_time_us), lock_(RWLockWrapper::CreateRWLock()) {
148}
149
150SimulatedClock::~SimulatedClock() {
151}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000152
153int64_t SimulatedClock::TimeInMilliseconds() {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000154 ReadLockScoped synchronize(*lock_);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000155 return (time_us_ + 500) / 1000;
156}
157
158int64_t SimulatedClock::TimeInMicroseconds() {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000159 ReadLockScoped synchronize(*lock_);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000160 return time_us_;
161}
162
163void SimulatedClock::CurrentNtp(uint32_t& seconds, uint32_t& fractions) {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000164 int64_t now_ms = TimeInMilliseconds();
165 seconds = (now_ms / 1000) + kNtpJan1970;
166 fractions =
167 static_cast<uint32_t>((now_ms % 1000) * kMagicNtpFractionalUnit / 1000);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000168}
169
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000170int64_t SimulatedClock::CurrentNtpInMilliseconds() {
171 return TimeInMilliseconds() + 1000 * static_cast<int64_t>(kNtpJan1970);
172}
173
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000174void SimulatedClock::AdvanceTimeMilliseconds(int64_t milliseconds) {
175 AdvanceTimeMicroseconds(1000 * milliseconds);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000176}
177
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000178void SimulatedClock::AdvanceTimeMicroseconds(int64_t microseconds) {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000179 WriteLockScoped synchronize(*lock_);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000180 time_us_ += microseconds;
181}
182
183}; // namespace webrtc