OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 // can be set. The unwrapped value is not allowed to wrap. | 84 // can be set. The unwrapped value is not allowed to wrap. |
85 template <typename T, T M = 0> | 85 template <typename T, T M = 0> |
86 class SeqNumUnwrapper { | 86 class SeqNumUnwrapper { |
87 // Use '<' instead of rtc::SafeLt to avoid crbug.com/753488 | 87 // Use '<' instead of rtc::SafeLt to avoid crbug.com/753488 |
88 static_assert( | 88 static_assert( |
89 std::is_unsigned<T>::value && | 89 std::is_unsigned<T>::value && |
90 std::numeric_limits<T>::max() < std::numeric_limits<uint64_t>::max(), | 90 std::numeric_limits<T>::max() < std::numeric_limits<uint64_t>::max(), |
91 "Type unwrapped must be an unsigned integer smaller than uint64_t."); | 91 "Type unwrapped must be an unsigned integer smaller than uint64_t."); |
92 | 92 |
93 public: | 93 public: |
94 SeqNumUnwrapper() : last_unwrapped_(0) {} | 94 // The reason we use this as the default start value is because it the |
95 // closest power of 10 to 2^63, and the reason we want to have a number | |
96 // that is a power of 10 is because it is more human readable. | |
stefan-webrtc
2017/09/01 07:25:09
Comment also on why we need a large start value, a
philipel
2017/09/01 08:55:46
Done.
| |
97 static constexpr uint64_t kDefaultStartValue = 10000000000000000000UL; | |
98 | |
99 SeqNumUnwrapper() : last_unwrapped_(kDefaultStartValue) {} | |
95 explicit SeqNumUnwrapper(uint64_t start_at) : last_unwrapped_(start_at) {} | 100 explicit SeqNumUnwrapper(uint64_t start_at) : last_unwrapped_(start_at) {} |
96 | 101 |
97 uint64_t Unwrap(T value) { | 102 uint64_t Unwrap(T value) { |
98 if (!last_value_) | 103 if (!last_value_) |
99 last_value_.emplace(value); | 104 last_value_.emplace(value); |
100 | 105 |
101 uint64_t unwrapped = 0; | 106 uint64_t unwrapped = 0; |
102 if (AheadOrAt<T, M>(value, *last_value_)) { | 107 if (AheadOrAt<T, M>(value, *last_value_)) { |
103 unwrapped = last_unwrapped_ + ForwardDiff<T, M>(*last_value_, value); | 108 unwrapped = last_unwrapped_ + ForwardDiff<T, M>(*last_value_, value); |
104 RTC_CHECK_GE(unwrapped, last_unwrapped_); | 109 RTC_CHECK_GE(unwrapped, last_unwrapped_); |
105 } else { | 110 } else { |
106 unwrapped = last_unwrapped_ - ReverseDiff<T, M>(*last_value_, value); | 111 unwrapped = last_unwrapped_ - ReverseDiff<T, M>(*last_value_, value); |
107 RTC_CHECK_LT(unwrapped, last_unwrapped_); | 112 RTC_CHECK_LT(unwrapped, last_unwrapped_); |
108 } | 113 } |
109 | 114 |
110 *last_value_ = value; | 115 *last_value_ = value; |
111 last_unwrapped_ = unwrapped; | 116 last_unwrapped_ = unwrapped; |
112 return last_unwrapped_; | 117 return last_unwrapped_; |
113 } | 118 } |
114 | 119 |
115 private: | 120 private: |
116 uint64_t last_unwrapped_; | 121 uint64_t last_unwrapped_; |
117 rtc::Optional<T> last_value_; | 122 rtc::Optional<T> last_value_; |
118 }; | 123 }; |
119 | 124 |
120 } // namespace webrtc | 125 } // namespace webrtc |
121 | 126 |
122 #endif // WEBRTC_MODULES_VIDEO_CODING_SEQUENCE_NUMBER_UTIL_H_ | 127 #endif // WEBRTC_MODULES_VIDEO_CODING_SEQUENCE_NUMBER_UTIL_H_ |
OLD | NEW |