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