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