danilchap | 9c6a0c7 | 2016-02-10 18:54:47 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 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 | */ |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 10 | #ifndef TEST_DRIFTING_CLOCK_H_ |
| 11 | #define TEST_DRIFTING_CLOCK_H_ |
danilchap | 9c6a0c7 | 2016-02-10 18:54:47 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 13 | #include <stdint.h> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 15 | #include "system_wrappers/include/clock.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 16 | #include "system_wrappers/include/ntp_time.h" |
danilchap | 9c6a0c7 | 2016-02-10 18:54:47 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | namespace test { |
| 20 | class DriftingClock : public Clock { |
| 21 | public: |
Danil Chapovalov | 01b7e92 | 2019-09-10 11:28:58 | [diff] [blame] | 22 | static constexpr float kNoDrift = 1.0f; |
danilchap | 9c6a0c7 | 2016-02-10 18:54:47 | [diff] [blame] | 23 | |
| 24 | DriftingClock(Clock* clock, float speed); |
| 25 | |
Danil Chapovalov | 01b7e92 | 2019-09-10 11:28:58 | [diff] [blame] | 26 | static constexpr float PercentsFaster(float percent) { |
| 27 | return 1.0f + percent / 100.0f; |
| 28 | } |
| 29 | static constexpr float PercentsSlower(float percent) { |
| 30 | return 1.0f - percent / 100.0f; |
| 31 | } |
danilchap | 9c6a0c7 | 2016-02-10 18:54:47 | [diff] [blame] | 32 | |
Paul Hallak | b59e904 | 2021-05-20 15:21:49 | [diff] [blame] | 33 | Timestamp CurrentTime() override { return Drift(clock_->CurrentTime()); } |
Paul Hallak | b59e904 | 2021-05-20 15:21:49 | [diff] [blame] | 34 | NtpTime ConvertTimestampToNtpTime(Timestamp timestamp) override { |
| 35 | return Drift(clock_->ConvertTimestampToNtpTime(timestamp)); |
| 36 | } |
danilchap | 9c6a0c7 | 2016-02-10 18:54:47 | [diff] [blame] | 37 | |
| 38 | private: |
Sebastian Jansson | 4de3115 | 2019-06-11 06:52:11 | [diff] [blame] | 39 | TimeDelta Drift() const; |
Paul Hallak | b59e904 | 2021-05-20 15:21:49 | [diff] [blame] | 40 | Timestamp Drift(Timestamp timestamp) const; |
| 41 | NtpTime Drift(NtpTime ntp_time) const; |
danilchap | 9c6a0c7 | 2016-02-10 18:54:47 | [diff] [blame] | 42 | |
| 43 | Clock* const clock_; |
| 44 | const float drift_; |
Sebastian Jansson | 4de3115 | 2019-06-11 06:52:11 | [diff] [blame] | 45 | const Timestamp start_time_; |
danilchap | 9c6a0c7 | 2016-02-10 18:54:47 | [diff] [blame] | 46 | }; |
| 47 | } // namespace test |
| 48 | } // namespace webrtc |
| 49 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 50 | #endif // TEST_DRIFTING_CLOCK_H_ |