You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

277 lines
8.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  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. #include "system_wrappers/include/clock.h"
  11. #if defined(WEBRTC_WIN)
  12. // Windows needs to be included before mmsystem.h
  13. #include "rtc_base/win32.h"
  14. #include <mmsystem.h>
  15. #include "rtc_base/critical_section.h"
  16. #elif defined(WEBRTC_POSIX)
  17. #include <sys/time.h>
  18. #include <time.h>
  19. #endif // defined(WEBRTC_POSIX)
  20. #include "rtc_base/synchronization/rw_lock_wrapper.h"
  21. #include "rtc_base/time_utils.h"
  22. namespace webrtc {
  23. class RealTimeClock : public Clock {
  24. Timestamp CurrentTime() override { return Timestamp::us(rtc::TimeMicros()); }
  25. // Return a timestamp in milliseconds relative to some arbitrary source; the
  26. // source is fixed for this clock.
  27. int64_t TimeInMilliseconds() override { return rtc::TimeMillis(); }
  28. // Return a timestamp in microseconds relative to some arbitrary source; the
  29. // source is fixed for this clock.
  30. int64_t TimeInMicroseconds() override { return rtc::TimeMicros(); }
  31. // Retrieve an NTP absolute timestamp.
  32. NtpTime CurrentNtpTime() override {
  33. timeval tv = CurrentTimeVal();
  34. double microseconds_in_seconds;
  35. uint32_t seconds;
  36. Adjust(tv, &seconds, &microseconds_in_seconds);
  37. uint32_t fractions = static_cast<uint32_t>(
  38. microseconds_in_seconds * kMagicNtpFractionalUnit + 0.5);
  39. return NtpTime(seconds, fractions);
  40. }
  41. // Retrieve an NTP absolute timestamp in milliseconds.
  42. int64_t CurrentNtpInMilliseconds() override {
  43. timeval tv = CurrentTimeVal();
  44. uint32_t seconds;
  45. double microseconds_in_seconds;
  46. Adjust(tv, &seconds, &microseconds_in_seconds);
  47. return 1000 * static_cast<int64_t>(seconds) +
  48. static_cast<int64_t>(1000.0 * microseconds_in_seconds + 0.5);
  49. }
  50. protected:
  51. virtual timeval CurrentTimeVal() = 0;
  52. static void Adjust(const timeval& tv,
  53. uint32_t* adjusted_s,
  54. double* adjusted_us_in_s) {
  55. *adjusted_s = tv.tv_sec + kNtpJan1970;
  56. *adjusted_us_in_s = tv.tv_usec / 1e6;
  57. if (*adjusted_us_in_s >= 1) {
  58. *adjusted_us_in_s -= 1;
  59. ++*adjusted_s;
  60. } else if (*adjusted_us_in_s < -1) {
  61. *adjusted_us_in_s += 1;
  62. --*adjusted_s;
  63. }
  64. }
  65. };
  66. #if defined(WINUWP)
  67. class WinUwpRealTimeClock final : public RealTimeClock {
  68. public:
  69. WinUwpRealTimeClock() = default;
  70. ~WinUwpRealTimeClock() override {}
  71. protected:
  72. timeval CurrentTimeVal() override {
  73. // The rtc::SystemTimeNanos() method is already time offset from a base
  74. // epoch value and might as be synchronized against an NTP time server as
  75. // an added bonus.
  76. auto nanos = rtc::SystemTimeNanos();
  77. struct timeval tv;
  78. tv.tv_sec = rtc::dchecked_cast<long>(nanos / 1000000000);
  79. tv.tv_usec = rtc::dchecked_cast<long>(nanos / 1000);
  80. return tv;
  81. }
  82. };
  83. #elif defined(WEBRTC_WIN)
  84. // TODO(pbos): Consider modifying the implementation to synchronize itself
  85. // against system time (update ref_point_) periodically to
  86. // prevent clock drift.
  87. class WindowsRealTimeClock : public RealTimeClock {
  88. public:
  89. WindowsRealTimeClock()
  90. : last_time_ms_(0),
  91. num_timer_wraps_(0),
  92. ref_point_(GetSystemReferencePoint()) {}
  93. ~WindowsRealTimeClock() override {}
  94. protected:
  95. struct ReferencePoint {
  96. FILETIME file_time;
  97. LARGE_INTEGER counter_ms;
  98. };
  99. timeval CurrentTimeVal() override {
  100. const uint64_t FILETIME_1970 = 0x019db1ded53e8000;
  101. FILETIME StartTime;
  102. uint64_t Time;
  103. struct timeval tv;
  104. // We can't use query performance counter since they can change depending on
  105. // speed stepping.
  106. GetTime(&StartTime);
  107. Time = (((uint64_t)StartTime.dwHighDateTime) << 32) +
  108. (uint64_t)StartTime.dwLowDateTime;
  109. // Convert the hecto-nano second time to tv format.
  110. Time -= FILETIME_1970;
  111. tv.tv_sec = (uint32_t)(Time / (uint64_t)10000000);
  112. tv.tv_usec = (uint32_t)((Time % (uint64_t)10000000) / 10);
  113. return tv;
  114. }
  115. void GetTime(FILETIME* current_time) {
  116. DWORD t;
  117. LARGE_INTEGER elapsed_ms;
  118. {
  119. rtc::CritScope lock(&crit_);
  120. // time MUST be fetched inside the critical section to avoid non-monotonic
  121. // last_time_ms_ values that'll register as incorrect wraparounds due to
  122. // concurrent calls to GetTime.
  123. t = timeGetTime();
  124. if (t < last_time_ms_)
  125. num_timer_wraps_++;
  126. last_time_ms_ = t;
  127. elapsed_ms.HighPart = num_timer_wraps_;
  128. }
  129. elapsed_ms.LowPart = t;
  130. elapsed_ms.QuadPart = elapsed_ms.QuadPart - ref_point_.counter_ms.QuadPart;
  131. // Translate to 100-nanoseconds intervals (FILETIME resolution)
  132. // and add to reference FILETIME to get current FILETIME.
  133. ULARGE_INTEGER filetime_ref_as_ul;
  134. filetime_ref_as_ul.HighPart = ref_point_.file_time.dwHighDateTime;
  135. filetime_ref_as_ul.LowPart = ref_point_.file_time.dwLowDateTime;
  136. filetime_ref_as_ul.QuadPart +=
  137. static_cast<ULONGLONG>((elapsed_ms.QuadPart) * 1000 * 10);
  138. // Copy to result
  139. current_time->dwHighDateTime = filetime_ref_as_ul.HighPart;
  140. current_time->dwLowDateTime = filetime_ref_as_ul.LowPart;
  141. }
  142. static ReferencePoint GetSystemReferencePoint() {
  143. ReferencePoint ref = {};
  144. FILETIME ft0 = {};
  145. FILETIME ft1 = {};
  146. // Spin waiting for a change in system time. As soon as this change happens,
  147. // get the matching call for timeGetTime() as soon as possible. This is
  148. // assumed to be the most accurate offset that we can get between
  149. // timeGetTime() and system time.
  150. // Set timer accuracy to 1 ms.
  151. timeBeginPeriod(1);
  152. GetSystemTimeAsFileTime(&ft0);
  153. do {
  154. GetSystemTimeAsFileTime(&ft1);
  155. ref.counter_ms.QuadPart = timeGetTime();
  156. Sleep(0);
  157. } while ((ft0.dwHighDateTime == ft1.dwHighDateTime) &&
  158. (ft0.dwLowDateTime == ft1.dwLowDateTime));
  159. ref.file_time = ft1;
  160. timeEndPeriod(1);
  161. return ref;
  162. }
  163. rtc::CriticalSection crit_;
  164. DWORD last_time_ms_;
  165. LONG num_timer_wraps_;
  166. const ReferencePoint ref_point_;
  167. };
  168. #elif defined(WEBRTC_POSIX)
  169. class UnixRealTimeClock : public RealTimeClock {
  170. public:
  171. UnixRealTimeClock() {}
  172. ~UnixRealTimeClock() override {}
  173. protected:
  174. timeval CurrentTimeVal() override {
  175. struct timeval tv;
  176. struct timezone tz;
  177. tz.tz_minuteswest = 0;
  178. tz.tz_dsttime = 0;
  179. gettimeofday(&tv, &tz);
  180. return tv;
  181. }
  182. };
  183. #endif // defined(WEBRTC_POSIX)
  184. Clock* Clock::GetRealTimeClock() {
  185. #if defined(WINUWP)
  186. static Clock* const clock = new WinUwpRealTimeClock();
  187. #elif defined(WEBRTC_WIN)
  188. static Clock* const clock = new WindowsRealTimeClock();
  189. #elif defined(WEBRTC_POSIX)
  190. static Clock* const clock = new UnixRealTimeClock();
  191. #else
  192. static Clock* const clock = nullptr;
  193. #endif
  194. return clock;
  195. }
  196. SimulatedClock::SimulatedClock(int64_t initial_time_us)
  197. : SimulatedClock(Timestamp::us(initial_time_us)) {}
  198. SimulatedClock::SimulatedClock(Timestamp initial_time)
  199. : time_(initial_time), lock_(RWLockWrapper::CreateRWLock()) {}
  200. SimulatedClock::~SimulatedClock() {}
  201. Timestamp SimulatedClock::CurrentTime() {
  202. ReadLockScoped synchronize(*lock_);
  203. return time_;
  204. }
  205. NtpTime SimulatedClock::CurrentNtpTime() {
  206. int64_t now_ms = TimeInMilliseconds();
  207. uint32_t seconds = (now_ms / 1000) + kNtpJan1970;
  208. uint32_t fractions =
  209. static_cast<uint32_t>((now_ms % 1000) * kMagicNtpFractionalUnit / 1000);
  210. return NtpTime(seconds, fractions);
  211. }
  212. int64_t SimulatedClock::CurrentNtpInMilliseconds() {
  213. return TimeInMilliseconds() + 1000 * static_cast<int64_t>(kNtpJan1970);
  214. }
  215. void SimulatedClock::AdvanceTimeMilliseconds(int64_t milliseconds) {
  216. AdvanceTime(TimeDelta::ms(milliseconds));
  217. }
  218. void SimulatedClock::AdvanceTimeMicroseconds(int64_t microseconds) {
  219. AdvanceTime(TimeDelta::us(microseconds));
  220. }
  221. void SimulatedClock::AdvanceTime(TimeDelta delta) {
  222. WriteLockScoped synchronize(*lock_);
  223. time_ += delta;
  224. }
  225. } // namespace webrtc