2014-05-13 18:00:26 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
#if defined(WEBRTC_POSIX)
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#if defined(WEBRTC_MAC)
|
|
|
|
|
#include <mach/mach_time.h>
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(WEBRTC_WIN)
|
2018-06-19 15:03:05 +02:00
|
|
|
// clang-format off
|
|
|
|
|
// clang formatting would put <windows.h> last,
|
|
|
|
|
// which leads to compilation failure.
|
2014-05-13 18:00:26 +00:00
|
|
|
#include <windows.h>
|
|
|
|
|
#include <mmsystem.h>
|
2016-09-13 23:41:47 -07:00
|
|
|
#include <sys/timeb.h>
|
2018-06-19 15:03:05 +02:00
|
|
|
// clang-format on
|
2014-05-13 18:00:26 +00:00
|
|
|
#endif
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/checks.h"
|
2018-02-26 23:44:19 +01:00
|
|
|
#include "rtc_base/numerics/safe_conversions.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/timeutils.h"
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
namespace rtc {
|
|
|
|
|
|
2016-05-27 14:15:43 -07:00
|
|
|
ClockInterface* g_clock = nullptr;
|
|
|
|
|
|
2016-06-06 11:16:06 -07:00
|
|
|
ClockInterface* SetClockForTesting(ClockInterface* clock) {
|
|
|
|
|
ClockInterface* prev = g_clock;
|
2016-05-27 14:15:43 -07:00
|
|
|
g_clock = clock;
|
2016-06-06 11:16:06 -07:00
|
|
|
return prev;
|
2016-05-27 14:15:43 -07:00
|
|
|
}
|
|
|
|
|
|
Update VirtualSocketServerTest to use a fake clock.
Since this is a test for a fake network, it's only natural that it uses
a fake clock as well. This makes the tests much faster, less flaky, and
lets them be moved out of "webrtc_nonparallel_tests", since they no
longer have a dependency on any "real" thing (sockets, or time) and
can be run in parallel as easily as any other tests.
As part of this CL, added the fake clock as an argument to
VirtualSocketServer's and TestClient's constructors, since these classes
have methods that wait synchronously for something to occur, and if the
test is using a fake clock, they need to advance it in order to make
progress.
Lastly, added a DCHECK in Thread::ProcessMessages. If called with a
nonzero time while a fake clock is used, it will get stuck in an
infinite loop; a DCHECK is easier to notice than an infinite loop.
BUG=webrtc:7727, webrtc:2409
Review-Url: https://codereview.webrtc.org/2927413002
Cr-Commit-Position: refs/heads/master@{#18544}
2017-06-12 14:30:28 -07:00
|
|
|
ClockInterface* GetClockForTesting() {
|
|
|
|
|
return g_clock;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-28 01:54:54 -08:00
|
|
|
int64_t SystemTimeNanos() {
|
2016-05-27 14:15:43 -07:00
|
|
|
int64_t ticks;
|
2014-05-13 18:00:26 +00:00
|
|
|
#if defined(WEBRTC_MAC)
|
|
|
|
|
static mach_timebase_info_data_t timebase;
|
|
|
|
|
if (timebase.denom == 0) {
|
|
|
|
|
// Get the timebase if this is the first time we run.
|
|
|
|
|
// Recommended by Apple's QA1398.
|
2014-09-16 01:03:29 +00:00
|
|
|
if (mach_timebase_info(&timebase) != KERN_SUCCESS) {
|
2017-01-12 02:24:27 -08:00
|
|
|
RTC_NOTREACHED();
|
2014-09-16 01:03:29 +00:00
|
|
|
}
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
// Use timebase to convert absolute time tick units into nanoseconds.
|
2018-02-26 23:44:19 +01:00
|
|
|
const auto mul = [](uint64_t a, uint32_t b) -> int64_t {
|
|
|
|
|
RTC_DCHECK_NE(b, 0);
|
|
|
|
|
RTC_DCHECK_LE(a, std::numeric_limits<int64_t>::max() / b)
|
|
|
|
|
<< "The multiplication " << a << " * " << b << " overflows";
|
|
|
|
|
return rtc::dchecked_cast<int64_t>(a * b);
|
|
|
|
|
};
|
|
|
|
|
ticks = mul(mach_absolute_time(), timebase.numer) / timebase.denom;
|
2014-05-13 18:00:26 +00:00
|
|
|
#elif defined(WEBRTC_POSIX)
|
|
|
|
|
struct timespec ts;
|
2016-05-27 14:15:43 -07:00
|
|
|
// TODO(deadbeef): Do we need to handle the case when CLOCK_MONOTONIC is not
|
|
|
|
|
// supported?
|
2014-05-13 18:00:26 +00:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
ticks = kNumNanosecsPerSec * static_cast<int64_t>(ts.tv_sec) +
|
|
|
|
|
static_cast<int64_t>(ts.tv_nsec);
|
2014-05-13 18:00:26 +00:00
|
|
|
#elif defined(WEBRTC_WIN)
|
|
|
|
|
static volatile LONG last_timegettime = 0;
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
static volatile int64_t num_wrap_timegettime = 0;
|
2014-05-13 18:00:26 +00:00
|
|
|
volatile LONG* last_timegettime_ptr = &last_timegettime;
|
|
|
|
|
DWORD now = timeGetTime();
|
|
|
|
|
// Atomically update the last gotten time
|
|
|
|
|
DWORD old = InterlockedExchange(last_timegettime_ptr, now);
|
|
|
|
|
if (now < old) {
|
2016-05-27 14:15:43 -07:00
|
|
|
// If now is earlier than old, there may have been a race between threads.
|
2014-05-13 18:00:26 +00:00
|
|
|
// 0x0fffffff ~3.1 days, the code will not take that long to execute
|
|
|
|
|
// so it must have been a wrap around.
|
|
|
|
|
if (old > 0xf0000000 && now < 0x0fffffff) {
|
|
|
|
|
num_wrap_timegettime++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ticks = now + (num_wrap_timegettime << 32);
|
2016-05-27 14:15:43 -07:00
|
|
|
// TODO(deadbeef): Calculate with nanosecond precision. Otherwise, we're
|
|
|
|
|
// just wasting a multiply and divide when doing Time() on Windows.
|
2014-05-13 18:00:26 +00:00
|
|
|
ticks = ticks * kNumNanosecsPerMillisec;
|
2016-01-27 12:55:33 +01:00
|
|
|
#else
|
|
|
|
|
#error Unsupported platform.
|
2014-05-13 18:00:26 +00:00
|
|
|
#endif
|
|
|
|
|
return ticks;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-15 17:15:23 -07:00
|
|
|
int64_t SystemTimeMillis() {
|
|
|
|
|
return static_cast<int64_t>(SystemTimeNanos() / kNumNanosecsPerMillisec);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-28 01:54:54 -08:00
|
|
|
int64_t TimeNanos() {
|
2016-06-15 17:15:23 -07:00
|
|
|
if (g_clock) {
|
|
|
|
|
return g_clock->TimeNanos();
|
|
|
|
|
}
|
|
|
|
|
return SystemTimeNanos();
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-16 08:55:44 -07:00
|
|
|
uint32_t Time32() {
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
return static_cast<uint32_t>(TimeNanos() / kNumNanosecsPerMillisec);
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-02 08:18:55 -07:00
|
|
|
int64_t TimeMillis() {
|
2016-11-28 01:54:54 -08:00
|
|
|
return TimeNanos() / kNumNanosecsPerMillisec;
|
2016-03-16 08:55:44 -07:00
|
|
|
}
|
|
|
|
|
|
2016-11-28 01:54:54 -08:00
|
|
|
int64_t TimeMicros() {
|
|
|
|
|
return TimeNanos() / kNumNanosecsPerMicrosec;
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-06 11:29:15 -07:00
|
|
|
int64_t TimeAfter(int64_t elapsed) {
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_DCHECK_GE(elapsed, 0);
|
2016-05-06 11:29:15 -07:00
|
|
|
return TimeMillis() + elapsed;
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-06 11:29:15 -07:00
|
|
|
int32_t TimeDiff32(uint32_t later, uint32_t earlier) {
|
2014-05-13 18:00:26 +00:00
|
|
|
return later - earlier;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-06 11:29:15 -07:00
|
|
|
int64_t TimeDiff(int64_t later, int64_t earlier) {
|
2016-03-16 08:55:44 -07:00
|
|
|
return later - earlier;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-21 20:42:17 +00:00
|
|
|
TimestampWrapAroundHandler::TimestampWrapAroundHandler()
|
2016-03-10 01:32:53 -08:00
|
|
|
: last_ts_(0), num_wrap_(-1) {}
|
2014-05-21 20:42:17 +00:00
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
int64_t TimestampWrapAroundHandler::Unwrap(uint32_t ts) {
|
2016-03-10 01:32:53 -08:00
|
|
|
if (num_wrap_ == -1) {
|
|
|
|
|
last_ts_ = ts;
|
|
|
|
|
num_wrap_ = 0;
|
|
|
|
|
return ts;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-21 20:42:17 +00:00
|
|
|
if (ts < last_ts_) {
|
2016-03-10 01:32:53 -08:00
|
|
|
if (last_ts_ >= 0xf0000000 && ts < 0x0fffffff)
|
2014-05-21 20:42:17 +00:00
|
|
|
++num_wrap_;
|
2016-03-10 01:32:53 -08:00
|
|
|
} else if ((ts - last_ts_) > 0xf0000000) {
|
|
|
|
|
// Backwards wrap. Unwrap with last wrap count and don't update last_ts_.
|
|
|
|
|
return ts + ((num_wrap_ - 1) << 32);
|
2014-05-21 20:42:17 +00:00
|
|
|
}
|
2016-03-10 01:32:53 -08:00
|
|
|
|
2014-05-21 20:42:17 +00:00
|
|
|
last_ts_ = ts;
|
2016-03-10 01:32:53 -08:00
|
|
|
return ts + (num_wrap_ << 32);
|
2014-05-21 20:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-01 13:06:34 +01:00
|
|
|
int64_t TmToSeconds(const std::tm& tm) {
|
|
|
|
|
static short int mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
|
|
|
|
static short int cumul_mdays[12] = {0, 31, 59, 90, 120, 151,
|
|
|
|
|
181, 212, 243, 273, 304, 334};
|
|
|
|
|
int year = tm.tm_year + 1900;
|
|
|
|
|
int month = tm.tm_mon;
|
|
|
|
|
int day = tm.tm_mday - 1; // Make 0-based like the rest.
|
|
|
|
|
int hour = tm.tm_hour;
|
|
|
|
|
int min = tm.tm_min;
|
|
|
|
|
int sec = tm.tm_sec;
|
|
|
|
|
|
2018-06-19 15:03:05 +02:00
|
|
|
bool expiry_in_leap_year =
|
|
|
|
|
(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
|
2015-12-01 13:06:34 +01:00
|
|
|
|
|
|
|
|
if (year < 1970)
|
|
|
|
|
return -1;
|
|
|
|
|
if (month < 0 || month > 11)
|
|
|
|
|
return -1;
|
|
|
|
|
if (day < 0 || day >= mdays[month] + (expiry_in_leap_year && month == 2 - 1))
|
|
|
|
|
return -1;
|
|
|
|
|
if (hour < 0 || hour > 23)
|
|
|
|
|
return -1;
|
|
|
|
|
if (min < 0 || min > 59)
|
|
|
|
|
return -1;
|
|
|
|
|
if (sec < 0 || sec > 59)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
day += cumul_mdays[month];
|
|
|
|
|
|
|
|
|
|
// Add number of leap days between 1970 and the expiration year, inclusive.
|
|
|
|
|
day += ((year / 4 - 1970 / 4) - (year / 100 - 1970 / 100) +
|
|
|
|
|
(year / 400 - 1970 / 400));
|
|
|
|
|
|
|
|
|
|
// We will have added one day too much above if expiration is during a leap
|
|
|
|
|
// year, and expiration is in January or February.
|
2018-06-19 15:03:05 +02:00
|
|
|
if (expiry_in_leap_year && month <= 2 - 1) // |month| is zero based.
|
2015-12-01 13:06:34 +01:00
|
|
|
day -= 1;
|
|
|
|
|
|
|
|
|
|
// Combine all variables into seconds from 1970-01-01 00:00 (except |month|
|
|
|
|
|
// which was accumulated into |day| above).
|
2018-06-19 15:03:05 +02:00
|
|
|
return (((static_cast<int64_t>(year - 1970) * 365 + day) * 24 + hour) * 60 +
|
|
|
|
|
min) *
|
|
|
|
|
60 +
|
|
|
|
|
sec;
|
2015-12-01 13:06:34 +01:00
|
|
|
}
|
|
|
|
|
|
2016-09-13 23:41:47 -07:00
|
|
|
int64_t TimeUTCMicros() {
|
2018-08-10 15:38:52 +02:00
|
|
|
if (g_clock) {
|
|
|
|
|
return g_clock->TimeNanos() / kNumNanosecsPerMicrosec;
|
|
|
|
|
}
|
2016-09-13 23:41:47 -07:00
|
|
|
#if defined(WEBRTC_POSIX)
|
|
|
|
|
struct timeval time;
|
2017-02-27 14:06:41 -08:00
|
|
|
gettimeofday(&time, nullptr);
|
2016-09-13 23:41:47 -07:00
|
|
|
// Convert from second (1.0) and microsecond (1e-6).
|
|
|
|
|
return (static_cast<int64_t>(time.tv_sec) * rtc::kNumMicrosecsPerSec +
|
|
|
|
|
time.tv_usec);
|
|
|
|
|
|
|
|
|
|
#elif defined(WEBRTC_WIN)
|
|
|
|
|
struct _timeb time;
|
|
|
|
|
_ftime(&time);
|
|
|
|
|
// Convert from second (1.0) and milliseconds (1e-3).
|
|
|
|
|
return (static_cast<int64_t>(time.time) * rtc::kNumMicrosecsPerSec +
|
|
|
|
|
static_cast<int64_t>(time.millitm) * rtc::kNumMicrosecsPerMillisec);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-10 15:38:52 +02:00
|
|
|
int64_t TimeUTCMillis() {
|
|
|
|
|
return TimeUTCMicros() / kNumMicrosecsPerMillisec;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-19 15:03:05 +02:00
|
|
|
} // namespace rtc
|