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