Bug 1729455 - Cherry-pick libwebrtc patches that makes SystemTimeNanos overrideable. r=bwc

This lets us define our own monotonic system clock for libwebrtc to use.

This patch corresponds to the libwebrtc part of chromium bug 516700
(https://crbug.com/516700).

It consists of the following libwebrtc patches squashed:
https://webrtc.googlesource.com/src/+/b73c9f0bc3c0331c177fcbd54224b350153aebcd
https://webrtc.googlesource.com/src/+/da20c739a8607e9f43e6ac04024ea622167ec694
https://webrtc.googlesource.com/src/+/373bb7bec4d2df7378d3dae965313548e6817246
https://webrtc.googlesource.com/src/+/bb52bdf09516ca548c4aff50526eda561f239bc0

Differential Revision: https://phabricator.services.mozilla.com/D127705
This commit is contained in:
Andreas Pehrson 2021-11-02 14:35:50 +00:00
Родитель 2c57bb60f4
Коммит 8dcf1ce947
8 изменённых файлов: 148 добавлений и 67 удалений

6
third_party/libwebrtc/rtc_base/BUILD.gn поставляемый
Просмотреть файл

@ -391,6 +391,8 @@ rtc_source_set("safe_conversions") {
rtc_library("timeutils") {
visibility = [ "*" ]
sources = [
"system_time.cc",
"system_time.h",
"time_utils.cc",
"time_utils.h",
]
@ -400,6 +402,10 @@ rtc_library("timeutils") {
":stringutils",
"system:rtc_export",
]
if (rtc_exclude_system_time) {
defines = [ "WEBRTC_EXCLUDE_SYSTEM_TIME" ]
}
libs = []
if (is_win) {
libs += [ "winmm.lib" ]

97
third_party/libwebrtc/rtc_base/system_time.cc поставляемый Normal file
Просмотреть файл

@ -0,0 +1,97 @@
/*
* Copyright 2021 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.
*/
// If WEBRTC_EXCLUDE_SYSTEM_TIME is set, an implementation of
// rtc::SystemTimeNanos() must be provided externally.
#ifndef WEBRTC_EXCLUDE_SYSTEM_TIME
#include <stdint.h>
#include <limits>
#if defined(WEBRTC_POSIX)
#include <sys/time.h>
#if defined(WEBRTC_MAC)
#include <mach/mach_time.h>
#endif
#endif
#if defined(WEBRTC_WIN)
// clang-format off
// clang formatting would put <windows.h> last,
// which leads to compilation failure.
#include <windows.h>
#include <mmsystem.h>
#include <sys/timeb.h>
// clang-format on
#endif
#include "rtc_base/checks.h"
#include "rtc_base/numerics/safe_conversions.h"
#include "rtc_base/system_time.h"
#include "rtc_base/time_utils.h"
namespace rtc {
int64_t SystemTimeNanos() {
int64_t ticks;
#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.
if (mach_timebase_info(&timebase) != KERN_SUCCESS) {
RTC_NOTREACHED();
}
}
// Use timebase to convert absolute time tick units into nanoseconds.
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;
#elif defined(WEBRTC_POSIX)
struct timespec ts;
// TODO(deadbeef): Do we need to handle the case when CLOCK_MONOTONIC is not
// supported?
clock_gettime(CLOCK_MONOTONIC, &ts);
ticks = kNumNanosecsPerSec * static_cast<int64_t>(ts.tv_sec) +
static_cast<int64_t>(ts.tv_nsec);
#elif defined(WINUWP)
ticks = WinUwpSystemTimeNanos();
#elif defined(WEBRTC_WIN)
static volatile LONG last_timegettime = 0;
static volatile int64_t num_wrap_timegettime = 0;
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) {
// If now is earlier than old, there may have been a race between threads.
// 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);
// TODO(deadbeef): Calculate with nanosecond precision. Otherwise, we're
// just wasting a multiply and divide when doing Time() on Windows.
ticks = ticks * kNumNanosecsPerMillisec;
#else
#error Unsupported platform.
#endif
return ticks;
}
} // namespace rtc
#endif // WEBRTC_EXCLUDE_SYSTEM_TIME

22
third_party/libwebrtc/rtc_base/system_time.h поставляемый Normal file
Просмотреть файл

@ -0,0 +1,22 @@
/*
* Copyright 2021 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.
*/
#ifndef RTC_BASE_SYSTEM_TIME_H_
#define RTC_BASE_SYSTEM_TIME_H_
namespace rtc {
// Returns the actual system time, even if a clock is set for testing.
// Useful for timeouts while using a test clock, or for logging.
int64_t SystemTimeNanos();
} // namespace rtc
#endif // RTC_BASE_SYSTEM_TIME_H_

67
third_party/libwebrtc/rtc_base/time_utils.cc поставляемый
Просмотреть файл

@ -12,23 +12,15 @@
#if defined(WEBRTC_POSIX)
#include <sys/time.h>
#if defined(WEBRTC_MAC)
#include <mach/mach_time.h>
#endif
#endif
#if defined(WEBRTC_WIN)
// clang-format off
// clang formatting would put <windows.h> last,
// which leads to compilation failure.
#include <windows.h>
#include <mmsystem.h>
#include <sys/timeb.h>
// clang-format on
#endif
#include "rtc_base/checks.h"
#include "rtc_base/numerics/safe_conversions.h"
#include "rtc_base/system_time.h"
#include "rtc_base/time_utils.h"
namespace rtc {
@ -141,61 +133,12 @@ void SyncWithNtp(int64_t time_from_ntp_server_ms) {
TimeHelper::SyncWithNtp(time_from_ntp_server_ms);
}
#endif // defined(WINUWP)
int64_t SystemTimeNanos() {
int64_t ticks;
#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.
if (mach_timebase_info(&timebase) != KERN_SUCCESS) {
RTC_NOTREACHED();
}
}
// Use timebase to convert absolute time tick units into nanoseconds.
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;
#elif defined(WEBRTC_POSIX)
struct timespec ts;
// TODO(deadbeef): Do we need to handle the case when CLOCK_MONOTONIC is not
// supported?
clock_gettime(CLOCK_MONOTONIC, &ts);
ticks = kNumNanosecsPerSec * static_cast<int64_t>(ts.tv_sec) +
static_cast<int64_t>(ts.tv_nsec);
#elif defined(WINUWP)
ticks = TimeHelper::TicksNs();
#elif defined(WEBRTC_WIN)
static volatile LONG last_timegettime = 0;
static volatile int64_t num_wrap_timegettime = 0;
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) {
// If now is earlier than old, there may have been a race between threads.
// 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);
// TODO(deadbeef): Calculate with nanosecond precision. Otherwise, we're
// just wasting a multiply and divide when doing Time() on Windows.
ticks = ticks * kNumNanosecsPerMillisec;
#else
#error Unsupported platform.
#endif
return ticks;
int64_t WinUwpSystemTimeNanos() {
return TimeHelper::TicksNs();
}
#endif // defined(WINUWP)
int64_t SystemTimeMillis() {
return static_cast<int64_t>(SystemTimeNanos() / kNumNanosecsPerMillisec);
}

8
third_party/libwebrtc/rtc_base/time_utils.h поставляемый
Просмотреть файл

@ -16,6 +16,7 @@
#include "rtc_base/checks.h"
#include "rtc_base/system/rtc_export.h"
#include "rtc_base/system_time.h"
namespace rtc {
@ -61,11 +62,16 @@ RTC_EXPORT ClockInterface* GetClockForTesting();
// Synchronizes the current clock based upon an NTP server's epoch in
// milliseconds.
void SyncWithNtp(int64_t time_from_ntp_server_ms);
// Returns the current time in nanoseconds. The clock is synchonized with the
// system wall clock time upon instatiation. It may also be synchronized using
// the SyncWithNtp() function above. Please note that the clock will most likely
// drift away from the system wall clock time as time goes by.
int64_t WinUwpSystemTimeNanos();
#endif // defined(WINUWP)
// Returns the actual system time, even if a clock is set for testing.
// Useful for timeouts while using a test clock, or for logging.
int64_t SystemTimeNanos();
int64_t SystemTimeMillis();
// Returns the current time in milliseconds in 32 bits.

Просмотреть файл

@ -91,10 +91,10 @@ class WinUwpRealTimeClock final : public RealTimeClock {
protected:
timeval CurrentTimeVal() override {
// The rtc::SystemTimeNanos() method is already time offset from a base
// epoch value and might as be synchronized against an NTP time server as
// an added bonus.
auto nanos = rtc::SystemTimeNanos();
// The rtc::WinUwpSystemTimeNanos() method is already time offset from a
// base epoch value and might as be synchronized against an NTP time server
// as an added bonus.
auto nanos = rtc::WinUwpSystemTimeNanos();
struct timeval tv;

Просмотреть файл

@ -23,6 +23,7 @@
#include "rtc_base/memory_usage.h"
#include "rtc_base/task_queue_for_test.h"
#include "rtc_base/task_utils/repeating_task.h"
#include "rtc_base/time_utils.h"
#include "system_wrappers/include/cpu_info.h"
#include "test/call_test.h"
#include "test/testsupport/file_utils.h"

6
third_party/libwebrtc/webrtc.gni поставляемый
Просмотреть файл

@ -64,6 +64,12 @@ declare_args() {
# provided.
rtc_exclude_metrics_default = build_with_chromium
# Setting this to true will define WEBRTC_EXCLUDE_SYSTEM_TIME which
# will tell the pre-processor to remove the default definition of the
# SystemTimeNanos() which is defined in rtc_base/system_time.cc. In
# that case a new implementation needs to be provided.
rtc_exclude_system_time = build_with_chromium
# Setting this to false will require the API user to pass in their own
# SSLCertificateVerifier to verify the certificates presented from a
# TLS-TURN server. In return disabling this saves around 100kb in the binary.