Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 1 | /* |
| 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 Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef RTC_BASE_FAKE_CLOCK_H_ |
| 12 | #define RTC_BASE_FAKE_CLOCK_H_ |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <stdint.h> |
| 15 | |
Sebastian Jansson | 5f83cf0 | 2018-05-08 14:52:22 +0200 | [diff] [blame] | 16 | #include "api/units/time_delta.h" |
Sebastian Jansson | d624c39 | 2019-04-17 10:36:03 +0200 | [diff] [blame^] | 17 | #include "api/units/timestamp.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "rtc_base/critical_section.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 19 | #include "rtc_base/thread_annotations.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "rtc_base/time_utils.h" |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 21 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 22 | namespace rtc { |
| 23 | |
| 24 | // Fake clock for use with unit tests, which does not tick on its own. |
| 25 | // Starts at time 0. |
| 26 | // |
| 27 | // TODO(deadbeef): Unify with webrtc::SimulatedClock. |
| 28 | class FakeClock : public ClockInterface { |
| 29 | public: |
Sebastian Jansson | d624c39 | 2019-04-17 10:36:03 +0200 | [diff] [blame^] | 30 | FakeClock() = default; |
| 31 | FakeClock(const FakeClock&) = delete; |
| 32 | FakeClock& operator=(const FakeClock&) = delete; |
| 33 | ~FakeClock() override = default; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 34 | |
| 35 | // ClockInterface implementation. |
| 36 | int64_t TimeNanos() const override; |
| 37 | |
| 38 | // Methods that can be used by the test to control the time. |
| 39 | |
| 40 | // Should only be used to set a time in the future. |
Sebastian Jansson | d624c39 | 2019-04-17 10:36:03 +0200 | [diff] [blame^] | 41 | void SetTime(webrtc::Timestamp new_time); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 42 | |
Sebastian Jansson | 5f83cf0 | 2018-05-08 14:52:22 +0200 | [diff] [blame] | 43 | void AdvanceTime(webrtc::TimeDelta delta); |
Sebastian Jansson | d624c39 | 2019-04-17 10:36:03 +0200 | [diff] [blame^] | 44 | |
| 45 | private: |
| 46 | CriticalSection lock_; |
| 47 | int64_t time_ns_ RTC_GUARDED_BY(lock_) = 0; |
| 48 | }; |
| 49 | |
| 50 | class ThreadProcessingFakeClock : public ClockInterface { |
| 51 | public: |
| 52 | int64_t TimeNanos() const override { return clock_.TimeNanos(); } |
| 53 | void SetTime(webrtc::Timestamp time); |
| 54 | void SetTimeMicros(int64_t micros) { |
| 55 | SetTime(webrtc::Timestamp ::us(micros)); |
| 56 | } |
| 57 | void AdvanceTime(webrtc::TimeDelta delta); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 58 | void AdvanceTimeMicros(int64_t micros) { |
Sebastian Jansson | 5f83cf0 | 2018-05-08 14:52:22 +0200 | [diff] [blame] | 59 | AdvanceTime(webrtc::TimeDelta::us(micros)); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 60 | } |
| 61 | private: |
Sebastian Jansson | d624c39 | 2019-04-17 10:36:03 +0200 | [diff] [blame^] | 62 | FakeClock clock_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | // Helper class that sets itself as the global clock in its constructor and |
| 66 | // unsets it in its destructor. |
Sebastian Jansson | d624c39 | 2019-04-17 10:36:03 +0200 | [diff] [blame^] | 67 | class ScopedBaseFakeClock : public FakeClock { |
| 68 | public: |
| 69 | ScopedBaseFakeClock(); |
| 70 | ~ScopedBaseFakeClock() override; |
| 71 | |
| 72 | private: |
| 73 | ClockInterface* prev_clock_; |
| 74 | }; |
| 75 | |
| 76 | // TODO(srte): Rename this to reflect that it also does thread processing. |
| 77 | class ScopedFakeClock : public ThreadProcessingFakeClock { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 78 | public: |
| 79 | ScopedFakeClock(); |
| 80 | ~ScopedFakeClock() override; |
| 81 | |
| 82 | private: |
| 83 | ClockInterface* prev_clock_; |
| 84 | }; |
| 85 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 86 | } // namespace rtc |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 87 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 88 | #endif // RTC_BASE_FAKE_CLOCK_H_ |