Taylor Brandstetter | b3c6810 | 2016-05-27 21:15:43 | [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 17:11:00 | [diff] [blame] | 11 | #ifndef RTC_BASE_FAKE_CLOCK_H_ |
| 12 | #define RTC_BASE_FAKE_CLOCK_H_ |
Taylor Brandstetter | b3c6810 | 2016-05-27 21:15:43 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 14 | #include <stdint.h> |
| 15 | |
Sebastian Jansson | 5f83cf0 | 2018-05-08 12:52:22 | [diff] [blame] | 16 | #include "api/units/time_delta.h" |
Sebastian Jansson | d624c39 | 2019-04-17 08:36:03 | [diff] [blame] | 17 | #include "api/units/timestamp.h" |
Markus Handell | 18523c3 | 2020-07-08 15:55:58 | [diff] [blame] | 18 | #include "rtc_base/synchronization/mutex.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 19 | #include "rtc_base/thread_annotations.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 20 | #include "rtc_base/time_utils.h" |
Taylor Brandstetter | b3c6810 | 2016-05-27 21:15:43 | [diff] [blame] | 21 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [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 08:36:03 | [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 05:52:50 | [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 08:36:03 | [diff] [blame] | 41 | void SetTime(webrtc::Timestamp new_time); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 42 | |
Sebastian Jansson | 5f83cf0 | 2018-05-08 12:52:22 | [diff] [blame] | 43 | void AdvanceTime(webrtc::TimeDelta delta); |
Sebastian Jansson | d624c39 | 2019-04-17 08:36:03 | [diff] [blame] | 44 | |
| 45 | private: |
Markus Handell | 18523c3 | 2020-07-08 15:55:58 | [diff] [blame] | 46 | mutable webrtc::Mutex lock_; |
Sebastian Jansson | d624c39 | 2019-04-17 08:36:03 | [diff] [blame] | 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); |
Sebastian Jansson | d624c39 | 2019-04-17 08:36:03 | [diff] [blame] | 54 | void AdvanceTime(webrtc::TimeDelta delta); |
Sebastian Jansson | 40889f3 | 2019-04-17 10:11:20 | [diff] [blame] | 55 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 56 | private: |
Sebastian Jansson | d624c39 | 2019-04-17 08:36:03 | [diff] [blame] | 57 | FakeClock clock_; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | // Helper class that sets itself as the global clock in its constructor and |
| 61 | // unsets it in its destructor. |
Sebastian Jansson | d624c39 | 2019-04-17 08:36:03 | [diff] [blame] | 62 | class ScopedBaseFakeClock : public FakeClock { |
| 63 | public: |
| 64 | ScopedBaseFakeClock(); |
| 65 | ~ScopedBaseFakeClock() override; |
| 66 | |
| 67 | private: |
| 68 | ClockInterface* prev_clock_; |
| 69 | }; |
| 70 | |
| 71 | // TODO(srte): Rename this to reflect that it also does thread processing. |
| 72 | class ScopedFakeClock : public ThreadProcessingFakeClock { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 73 | public: |
| 74 | ScopedFakeClock(); |
| 75 | ~ScopedFakeClock() override; |
| 76 | |
| 77 | private: |
| 78 | ClockInterface* prev_clock_; |
| 79 | }; |
| 80 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 81 | } // namespace rtc |
Taylor Brandstetter | b3c6810 | 2016-05-27 21:15:43 | [diff] [blame] | 82 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 83 | #endif // RTC_BASE_FAKE_CLOCK_H_ |