Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 11 | #ifndef API_UNITS_TIMESTAMP_H_ |
| 12 | #define API_UNITS_TIMESTAMP_H_ |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 13 | |
Andrey Logvin | b95d90b | 2020-12-09 12:49:39 +0000 | [diff] [blame] | 14 | #ifdef WEBRTC_UNIT_TEST |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame] | 15 | #include <ostream> // no-presubmit-check TODO(webrtc:8982) |
Andrey Logvin | b95d90b | 2020-12-09 12:49:39 +0000 | [diff] [blame] | 16 | #endif // WEBRTC_UNIT_TEST |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame] | 17 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 18 | #include <string> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 19 | #include <type_traits> |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 20 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 21 | #include "api/units/time_delta.h" |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 22 | #include "rtc_base/checks.h" |
Danil Chapovalov | f6e48bf | 2022-10-31 15:16:50 +0100 | [diff] [blame] | 23 | #include "rtc_base/units/unit_base.h" // IWYU pragma: export |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 26 | // Timestamp represents the time that has passed since some unspecified epoch. |
| 27 | // The epoch is assumed to be before any represented timestamps, this means that |
| 28 | // negative values are not valid. The most notable feature is that the |
| 29 | // difference of two Timestamps results in a TimeDelta. |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 30 | class Timestamp final : public rtc_units_impl::UnitBase<Timestamp> { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 31 | public: |
Danil Chapovalov | 8d94dc2 | 2020-01-28 17:54:47 +0100 | [diff] [blame] | 32 | template <typename T> |
| 33 | static constexpr Timestamp Seconds(T value) { |
| 34 | static_assert(std::is_arithmetic<T>::value, ""); |
| 35 | return FromFraction(1'000'000, value); |
| 36 | } |
| 37 | template <typename T> |
| 38 | static constexpr Timestamp Millis(T value) { |
| 39 | static_assert(std::is_arithmetic<T>::value, ""); |
| 40 | return FromFraction(1'000, value); |
| 41 | } |
| 42 | template <typename T> |
| 43 | static constexpr Timestamp Micros(T value) { |
| 44 | static_assert(std::is_arithmetic<T>::value, ""); |
| 45 | return FromValue(value); |
| 46 | } |
| 47 | |
Sebastian Jansson | 3b69b19 | 2018-05-07 13:51:51 +0200 | [diff] [blame] | 48 | Timestamp() = delete; |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 49 | |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 50 | template <typename T = int64_t> |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 51 | constexpr T seconds() const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 52 | return ToFraction<1000000, T>(); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 53 | } |
| 54 | template <typename T = int64_t> |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 55 | constexpr T ms() const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 56 | return ToFraction<1000, T>(); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 57 | } |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 58 | template <typename T = int64_t> |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 59 | constexpr T us() const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 60 | return ToValue<T>(); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | constexpr int64_t seconds_or(int64_t fallback_value) const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 64 | return ToFractionOr<1000000>(fallback_value); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 65 | } |
| 66 | constexpr int64_t ms_or(int64_t fallback_value) const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 67 | return ToFractionOr<1000>(fallback_value); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 68 | } |
| 69 | constexpr int64_t us_or(int64_t fallback_value) const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 70 | return ToValueOr(fallback_value); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 71 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 72 | |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 73 | constexpr Timestamp operator+(const TimeDelta delta) const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 74 | if (IsPlusInfinity() || delta.IsPlusInfinity()) { |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 75 | RTC_DCHECK(!IsMinusInfinity()); |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 76 | RTC_DCHECK(!delta.IsMinusInfinity()); |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 77 | return PlusInfinity(); |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 78 | } else if (IsMinusInfinity() || delta.IsMinusInfinity()) { |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 79 | RTC_DCHECK(!IsPlusInfinity()); |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 80 | RTC_DCHECK(!delta.IsPlusInfinity()); |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 81 | return MinusInfinity(); |
| 82 | } |
Danil Chapovalov | 0c626af | 2020-02-10 11:16:00 +0100 | [diff] [blame] | 83 | return Timestamp::Micros(us() + delta.us()); |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 84 | } |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 85 | constexpr Timestamp operator-(const TimeDelta delta) const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 86 | if (IsPlusInfinity() || delta.IsMinusInfinity()) { |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 87 | RTC_DCHECK(!IsMinusInfinity()); |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 88 | RTC_DCHECK(!delta.IsPlusInfinity()); |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 89 | return PlusInfinity(); |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 90 | } else if (IsMinusInfinity() || delta.IsPlusInfinity()) { |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 91 | RTC_DCHECK(!IsPlusInfinity()); |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 92 | RTC_DCHECK(!delta.IsMinusInfinity()); |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 93 | return MinusInfinity(); |
| 94 | } |
Danil Chapovalov | 0c626af | 2020-02-10 11:16:00 +0100 | [diff] [blame] | 95 | return Timestamp::Micros(us() - delta.us()); |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 96 | } |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 97 | constexpr TimeDelta operator-(const Timestamp other) const { |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 98 | if (IsPlusInfinity() || other.IsMinusInfinity()) { |
| 99 | RTC_DCHECK(!IsMinusInfinity()); |
| 100 | RTC_DCHECK(!other.IsPlusInfinity()); |
| 101 | return TimeDelta::PlusInfinity(); |
| 102 | } else if (IsMinusInfinity() || other.IsPlusInfinity()) { |
| 103 | RTC_DCHECK(!IsPlusInfinity()); |
| 104 | RTC_DCHECK(!other.IsMinusInfinity()); |
| 105 | return TimeDelta::MinusInfinity(); |
| 106 | } |
Danil Chapovalov | 0c626af | 2020-02-10 11:16:00 +0100 | [diff] [blame] | 107 | return TimeDelta::Micros(us() - other.us()); |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 108 | } |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 109 | constexpr Timestamp& operator-=(const TimeDelta delta) { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 110 | *this = *this - delta; |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 111 | return *this; |
| 112 | } |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 113 | constexpr Timestamp& operator+=(const TimeDelta delta) { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 114 | *this = *this + delta; |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 115 | return *this; |
| 116 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 117 | |
| 118 | private: |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 119 | friend class rtc_units_impl::UnitBase<Timestamp>; |
| 120 | using UnitBase::UnitBase; |
| 121 | static constexpr bool one_sided = true; |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 122 | }; |
| 123 | |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 124 | std::string ToString(Timestamp value); |
Sebastian Jansson | b113862 | 2019-04-11 16:48:15 +0200 | [diff] [blame] | 125 | inline std::string ToLogString(Timestamp value) { |
| 126 | return ToString(value); |
| 127 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 128 | |
Andrey Logvin | b95d90b | 2020-12-09 12:49:39 +0000 | [diff] [blame] | 129 | #ifdef WEBRTC_UNIT_TEST |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame] | 130 | inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) |
| 131 | std::ostream& stream, // no-presubmit-check TODO(webrtc:8982) |
| 132 | Timestamp value) { |
| 133 | return stream << ToString(value); |
| 134 | } |
Andrey Logvin | b95d90b | 2020-12-09 12:49:39 +0000 | [diff] [blame] | 135 | #endif // WEBRTC_UNIT_TEST |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame] | 136 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 137 | } // namespace webrtc |
| 138 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 139 | #endif // API_UNITS_TIMESTAMP_H_ |