1 /*
2  *  Copyright (c) 2017 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 
11 #ifndef MODULES_INCLUDE_MODULE_COMMON_TYPES_PUBLIC_H_
12 #define MODULES_INCLUDE_MODULE_COMMON_TYPES_PUBLIC_H_
13 
14 #include <limits>
15 
16 #include "api/optional.h"
17 #include "typedefs.h"  // NOLINT(build/include)
18 
19 namespace webrtc {
20 
21 template <typename U>
IsNewer(U value,U prev_value)22 inline bool IsNewer(U value, U prev_value) {
23   static_assert(!std::numeric_limits<U>::is_signed, "U must be unsigned");
24   // kBreakpoint is the half-way mark for the type U. For instance, for a
25   // uint16_t it will be 0x8000, and for a uint32_t, it will be 0x8000000.
26   constexpr U kBreakpoint = (std::numeric_limits<U>::max() >> 1) + 1;
27   // Distinguish between elements that are exactly kBreakpoint apart.
28   // If t1>t2 and |t1-t2| = kBreakpoint: IsNewer(t1,t2)=true,
29   // IsNewer(t2,t1)=false
30   // rather than having IsNewer(t1,t2) = IsNewer(t2,t1) = false.
31   if (value - prev_value == kBreakpoint) {
32     return value > prev_value;
33   }
34   return value != prev_value &&
35          static_cast<U>(value - prev_value) < kBreakpoint;
36 }
37 
38 // Utility class to unwrap a number to a larger type. The numbers will never be
39 // unwrapped to a negative value.
40 template <typename U>
41 class Unwrapper {
42   static_assert(!std::numeric_limits<U>::is_signed, "U must be unsigned");
43   static_assert(std::numeric_limits<U>::max() <=
44                     std::numeric_limits<uint32_t>::max(),
45                 "U must not be wider than 32 bits");
46 
47  public:
48   // Get the unwrapped value, but don't update the internal state.
UnwrapWithoutUpdate(U value)49   int64_t UnwrapWithoutUpdate(U value) const {
50     if (!last_value_)
51       return value;
52 
53     constexpr int64_t kMaxPlusOne =
54         static_cast<int64_t>(std::numeric_limits<U>::max()) + 1;
55 
56     U cropped_last = static_cast<U>(*last_value_);
57     int64_t delta = value - cropped_last;
58     if (IsNewer(value, cropped_last)) {
59       if (delta < 0)
60         delta += kMaxPlusOne;  // Wrap forwards.
61     } else if (delta > 0 && (*last_value_ + delta - kMaxPlusOne) >= 0) {
62       // If value is older but delta is positive, this is a backwards
63       // wrap-around. However, don't wrap backwards past 0 (unwrapped).
64       delta -= kMaxPlusOne;
65     }
66 
67     return *last_value_ + delta;
68   }
69 
70   // Only update the internal state to the specified last (unwrapped) value.
UpdateLast(int64_t last_value)71   void UpdateLast(int64_t last_value) {
72     last_value_ = last_value;
73   }
74 
75   // Unwrap the value and update the internal state.
Unwrap(U value)76   int64_t Unwrap(U value) {
77     int64_t unwrapped = UnwrapWithoutUpdate(value);
78     UpdateLast(unwrapped);
79     return unwrapped;
80   }
81 
82  private:
83   rtc::Optional<int64_t> last_value_;
84 };
85 
86 using SequenceNumberUnwrapper = Unwrapper<uint16_t>;
87 using TimestampUnwrapper = Unwrapper<uint32_t>;
88 
IsNewerSequenceNumber(uint16_t sequence_number,uint16_t prev_sequence_number)89 inline bool IsNewerSequenceNumber(uint16_t sequence_number,
90                                   uint16_t prev_sequence_number) {
91   return IsNewer(sequence_number, prev_sequence_number);
92 }
93 
IsNewerTimestamp(uint32_t timestamp,uint32_t prev_timestamp)94 inline bool IsNewerTimestamp(uint32_t timestamp, uint32_t prev_timestamp) {
95   return IsNewer(timestamp, prev_timestamp);
96 }
97 
LatestSequenceNumber(uint16_t sequence_number1,uint16_t sequence_number2)98 inline uint16_t LatestSequenceNumber(uint16_t sequence_number1,
99                                      uint16_t sequence_number2) {
100   return IsNewerSequenceNumber(sequence_number1, sequence_number2)
101              ? sequence_number1
102              : sequence_number2;
103 }
104 
LatestTimestamp(uint32_t timestamp1,uint32_t timestamp2)105 inline uint32_t LatestTimestamp(uint32_t timestamp1, uint32_t timestamp2) {
106   return IsNewerTimestamp(timestamp1, timestamp2) ? timestamp1 : timestamp2;
107 }
108 
109 }  // namespace webrtc
110 #endif  // MODULES_INCLUDE_MODULE_COMMON_TYPES_PUBLIC_H_
111