henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 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 <stdint.h> |
| 12 | |
| 13 | #if defined(WEBRTC_POSIX) |
| 14 | #include <sys/time.h> |
| 15 | #if defined(WEBRTC_MAC) |
| 16 | #include <mach/mach_time.h> |
| 17 | #endif |
| 18 | #endif |
| 19 | |
| 20 | #if defined(WEBRTC_WIN) |
andrew@webrtc.org | 6ae5a6d | 2014-09-16 01:03:29 +0000 | [diff] [blame] | 21 | #ifndef WIN32_LEAN_AND_MEAN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 22 | #define WIN32_LEAN_AND_MEAN |
andrew@webrtc.org | 6ae5a6d | 2014-09-16 01:03:29 +0000 | [diff] [blame] | 23 | #endif |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 24 | // clang-format off |
| 25 | // clang formatting would put <windows.h> last, |
| 26 | // which leads to compilation failure. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 27 | #include <windows.h> |
| 28 | #include <mmsystem.h> |
nisse | cdf37a9 | 2016-09-13 23:41:47 -0700 | [diff] [blame] | 29 | #include <sys/timeb.h> |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 30 | // clang-format on |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 31 | #endif |
| 32 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 33 | #include "rtc_base/checks.h" |
Karl Wiberg | e0269cd | 2018-02-26 23:44:19 +0100 | [diff] [blame] | 34 | #include "rtc_base/numerics/safe_conversions.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 35 | #include "rtc_base/timeutils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 36 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 37 | namespace rtc { |
| 38 | |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 39 | ClockInterface* g_clock = nullptr; |
| 40 | |
deadbeef | f5f03e8 | 2016-06-06 11:16:06 -0700 | [diff] [blame] | 41 | ClockInterface* SetClockForTesting(ClockInterface* clock) { |
| 42 | ClockInterface* prev = g_clock; |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 43 | g_clock = clock; |
deadbeef | f5f03e8 | 2016-06-06 11:16:06 -0700 | [diff] [blame] | 44 | return prev; |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 45 | } |
| 46 | |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 47 | ClockInterface* GetClockForTesting() { |
| 48 | return g_clock; |
| 49 | } |
| 50 | |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 51 | int64_t SystemTimeNanos() { |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 52 | int64_t ticks; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 53 | #if defined(WEBRTC_MAC) |
| 54 | static mach_timebase_info_data_t timebase; |
| 55 | if (timebase.denom == 0) { |
| 56 | // Get the timebase if this is the first time we run. |
| 57 | // Recommended by Apple's QA1398. |
andrew@webrtc.org | 6ae5a6d | 2014-09-16 01:03:29 +0000 | [diff] [blame] | 58 | if (mach_timebase_info(&timebase) != KERN_SUCCESS) { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 59 | RTC_NOTREACHED(); |
andrew@webrtc.org | 6ae5a6d | 2014-09-16 01:03:29 +0000 | [diff] [blame] | 60 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 61 | } |
| 62 | // Use timebase to convert absolute time tick units into nanoseconds. |
Karl Wiberg | e0269cd | 2018-02-26 23:44:19 +0100 | [diff] [blame] | 63 | const auto mul = [](uint64_t a, uint32_t b) -> int64_t { |
| 64 | RTC_DCHECK_NE(b, 0); |
| 65 | RTC_DCHECK_LE(a, std::numeric_limits<int64_t>::max() / b) |
| 66 | << "The multiplication " << a << " * " << b << " overflows"; |
| 67 | return rtc::dchecked_cast<int64_t>(a * b); |
| 68 | }; |
| 69 | ticks = mul(mach_absolute_time(), timebase.numer) / timebase.denom; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 70 | #elif defined(WEBRTC_POSIX) |
| 71 | struct timespec ts; |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 72 | // TODO(deadbeef): Do we need to handle the case when CLOCK_MONOTONIC is not |
| 73 | // supported? |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 74 | clock_gettime(CLOCK_MONOTONIC, &ts); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 75 | ticks = kNumNanosecsPerSec * static_cast<int64_t>(ts.tv_sec) + |
| 76 | static_cast<int64_t>(ts.tv_nsec); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 77 | #elif defined(WEBRTC_WIN) |
| 78 | static volatile LONG last_timegettime = 0; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 79 | static volatile int64_t num_wrap_timegettime = 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 80 | volatile LONG* last_timegettime_ptr = &last_timegettime; |
| 81 | DWORD now = timeGetTime(); |
| 82 | // Atomically update the last gotten time |
| 83 | DWORD old = InterlockedExchange(last_timegettime_ptr, now); |
| 84 | if (now < old) { |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 85 | // If now is earlier than old, there may have been a race between threads. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 86 | // 0x0fffffff ~3.1 days, the code will not take that long to execute |
| 87 | // so it must have been a wrap around. |
| 88 | if (old > 0xf0000000 && now < 0x0fffffff) { |
| 89 | num_wrap_timegettime++; |
| 90 | } |
| 91 | } |
| 92 | ticks = now + (num_wrap_timegettime << 32); |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 93 | // TODO(deadbeef): Calculate with nanosecond precision. Otherwise, we're |
| 94 | // just wasting a multiply and divide when doing Time() on Windows. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 95 | ticks = ticks * kNumNanosecsPerMillisec; |
Erik Språng | 1c39098 | 2016-01-27 12:55:33 +0100 | [diff] [blame] | 96 | #else |
| 97 | #error Unsupported platform. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 98 | #endif |
| 99 | return ticks; |
| 100 | } |
| 101 | |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 102 | int64_t SystemTimeMillis() { |
| 103 | return static_cast<int64_t>(SystemTimeNanos() / kNumNanosecsPerMillisec); |
| 104 | } |
| 105 | |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 106 | int64_t TimeNanos() { |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 107 | if (g_clock) { |
| 108 | return g_clock->TimeNanos(); |
| 109 | } |
| 110 | return SystemTimeNanos(); |
| 111 | } |
| 112 | |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 113 | uint32_t Time32() { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 114 | return static_cast<uint32_t>(TimeNanos() / kNumNanosecsPerMillisec); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 115 | } |
| 116 | |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 117 | int64_t TimeMillis() { |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 118 | return TimeNanos() / kNumNanosecsPerMillisec; |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 119 | } |
| 120 | |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 121 | int64_t TimeMicros() { |
| 122 | return TimeNanos() / kNumNanosecsPerMicrosec; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 125 | int64_t TimeAfter(int64_t elapsed) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 126 | RTC_DCHECK_GE(elapsed, 0); |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 127 | return TimeMillis() + elapsed; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 130 | int32_t TimeDiff32(uint32_t later, uint32_t earlier) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 131 | return later - earlier; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 134 | int64_t TimeDiff(int64_t later, int64_t earlier) { |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 135 | return later - earlier; |
| 136 | } |
| 137 | |
henrike@webrtc.org | 99b4162 | 2014-05-21 20:42:17 +0000 | [diff] [blame] | 138 | TimestampWrapAroundHandler::TimestampWrapAroundHandler() |
sprang | 1b3530b | 2016-03-10 01:32:53 -0800 | [diff] [blame] | 139 | : last_ts_(0), num_wrap_(-1) {} |
henrike@webrtc.org | 99b4162 | 2014-05-21 20:42:17 +0000 | [diff] [blame] | 140 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 141 | int64_t TimestampWrapAroundHandler::Unwrap(uint32_t ts) { |
sprang | 1b3530b | 2016-03-10 01:32:53 -0800 | [diff] [blame] | 142 | if (num_wrap_ == -1) { |
| 143 | last_ts_ = ts; |
| 144 | num_wrap_ = 0; |
| 145 | return ts; |
henrike@webrtc.org | 99b4162 | 2014-05-21 20:42:17 +0000 | [diff] [blame] | 146 | } |
sprang | 1b3530b | 2016-03-10 01:32:53 -0800 | [diff] [blame] | 147 | |
| 148 | if (ts < last_ts_) { |
| 149 | if (last_ts_ >= 0xf0000000 && ts < 0x0fffffff) |
| 150 | ++num_wrap_; |
| 151 | } else if ((ts - last_ts_) > 0xf0000000) { |
| 152 | // Backwards wrap. Unwrap with last wrap count and don't update last_ts_. |
| 153 | return ts + ((num_wrap_ - 1) << 32); |
| 154 | } |
| 155 | |
henrike@webrtc.org | 99b4162 | 2014-05-21 20:42:17 +0000 | [diff] [blame] | 156 | last_ts_ = ts; |
sprang | 1b3530b | 2016-03-10 01:32:53 -0800 | [diff] [blame] | 157 | return ts + (num_wrap_ << 32); |
henrike@webrtc.org | 99b4162 | 2014-05-21 20:42:17 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 160 | int64_t TmToSeconds(const std::tm& tm) { |
| 161 | static short int mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; |
| 162 | static short int cumul_mdays[12] = {0, 31, 59, 90, 120, 151, |
| 163 | 181, 212, 243, 273, 304, 334}; |
| 164 | int year = tm.tm_year + 1900; |
| 165 | int month = tm.tm_mon; |
| 166 | int day = tm.tm_mday - 1; // Make 0-based like the rest. |
| 167 | int hour = tm.tm_hour; |
| 168 | int min = tm.tm_min; |
| 169 | int sec = tm.tm_sec; |
| 170 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 171 | bool expiry_in_leap_year = |
| 172 | (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 173 | |
| 174 | if (year < 1970) |
| 175 | return -1; |
| 176 | if (month < 0 || month > 11) |
| 177 | return -1; |
| 178 | if (day < 0 || day >= mdays[month] + (expiry_in_leap_year && month == 2 - 1)) |
| 179 | return -1; |
| 180 | if (hour < 0 || hour > 23) |
| 181 | return -1; |
| 182 | if (min < 0 || min > 59) |
| 183 | return -1; |
| 184 | if (sec < 0 || sec > 59) |
| 185 | return -1; |
| 186 | |
| 187 | day += cumul_mdays[month]; |
| 188 | |
| 189 | // Add number of leap days between 1970 and the expiration year, inclusive. |
| 190 | day += ((year / 4 - 1970 / 4) - (year / 100 - 1970 / 100) + |
| 191 | (year / 400 - 1970 / 400)); |
| 192 | |
| 193 | // We will have added one day too much above if expiration is during a leap |
| 194 | // year, and expiration is in January or February. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 195 | if (expiry_in_leap_year && month <= 2 - 1) // |month| is zero based. |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 196 | day -= 1; |
| 197 | |
| 198 | // Combine all variables into seconds from 1970-01-01 00:00 (except |month| |
| 199 | // which was accumulated into |day| above). |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 200 | return (((static_cast<int64_t>(year - 1970) * 365 + day) * 24 + hour) * 60 + |
| 201 | min) * |
| 202 | 60 + |
| 203 | sec; |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 204 | } |
| 205 | |
nisse | cdf37a9 | 2016-09-13 23:41:47 -0700 | [diff] [blame] | 206 | int64_t TimeUTCMicros() { |
| 207 | #if defined(WEBRTC_POSIX) |
| 208 | struct timeval time; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 209 | gettimeofday(&time, nullptr); |
nisse | cdf37a9 | 2016-09-13 23:41:47 -0700 | [diff] [blame] | 210 | // Convert from second (1.0) and microsecond (1e-6). |
| 211 | return (static_cast<int64_t>(time.tv_sec) * rtc::kNumMicrosecsPerSec + |
| 212 | time.tv_usec); |
| 213 | |
| 214 | #elif defined(WEBRTC_WIN) |
| 215 | struct _timeb time; |
| 216 | _ftime(&time); |
| 217 | // Convert from second (1.0) and milliseconds (1e-3). |
| 218 | return (static_cast<int64_t>(time.time) * rtc::kNumMicrosecsPerSec + |
| 219 | static_cast<int64_t>(time.millitm) * rtc::kNumMicrosecsPerMillisec); |
| 220 | #endif |
| 221 | } |
| 222 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 223 | } // namespace rtc |