blob: b9f0ee95e5358c6e9be3f12eb68c6972782cc390 [file] [log] [blame]
Taylor Brandstetterb3c68102016-05-27 14:15:43 -07001/*
2 * Copyright 2016 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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "rtc_base/fake_clock.h"
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080014#include "rtc_base/message_queue.h"
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070015
16namespace rtc {
17
nissedeb95f32016-11-28 01:54:54 -080018int64_t FakeClock::TimeNanos() const {
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070019 CritScope cs(&lock_);
Sebastian Janssond624c392019-04-17 10:36:03 +020020 return time_ns_;
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070021}
22
Sebastian Janssond624c392019-04-17 10:36:03 +020023void FakeClock::SetTime(webrtc::Timestamp new_time) {
24 CritScope cs(&lock_);
25 RTC_DCHECK(new_time.us() * 1000 >= time_ns_);
26 time_ns_ = new_time.us() * 1000;
27}
28
29void FakeClock::AdvanceTime(webrtc::TimeDelta delta) {
30 CritScope cs(&lock_);
31 time_ns_ += delta.ns();
32}
33
34void ThreadProcessingFakeClock::SetTime(webrtc::Timestamp time) {
35 clock_.SetTime(time);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070036 // If message queues are waiting in a socket select() with a timeout provided
deadbeeff5f03e82016-06-06 11:16:06 -070037 // by the OS, they should wake up and dispatch all messages that are ready.
Niels Möller8909a632018-09-06 08:42:44 +020038 MessageQueueManager::ProcessAllMessageQueuesForTesting();
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070039}
40
Sebastian Janssond624c392019-04-17 10:36:03 +020041void ThreadProcessingFakeClock::AdvanceTime(webrtc::TimeDelta delta) {
42 clock_.AdvanceTime(delta);
Niels Möller8909a632018-09-06 08:42:44 +020043 MessageQueueManager::ProcessAllMessageQueuesForTesting();
deadbeeff5f03e82016-06-06 11:16:06 -070044}
45
Sebastian Janssond624c392019-04-17 10:36:03 +020046ScopedBaseFakeClock::ScopedBaseFakeClock() {
47 prev_clock_ = SetClockForTesting(this);
48}
49
50ScopedBaseFakeClock::~ScopedBaseFakeClock() {
51 SetClockForTesting(prev_clock_);
52}
53
deadbeeff5f03e82016-06-06 11:16:06 -070054ScopedFakeClock::ScopedFakeClock() {
55 prev_clock_ = SetClockForTesting(this);
56}
57
58ScopedFakeClock::~ScopedFakeClock() {
59 SetClockForTesting(prev_clock_);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070060}
61
62} // namespace rtc