mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-12 21:30:45 +01:00
[Unwrap] Delete webrtc::Unwrapper
Bug: webrtc:13982 Change-Id: I501261b09a05080ec681ae120648938e350a05de Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/290890 Commit-Queue: Harald Alvestrand <hta@webrtc.org> Reviewed-by: Harald Alvestrand <hta@webrtc.org> Auto-Submit: Evan Shrubsole <eshr@webrtc.org> Cr-Commit-Position: refs/heads/main@{#39088}
This commit is contained in:
parent
d8b6b06e70
commit
b613d62285
2 changed files with 0 additions and 157 deletions
|
@ -34,55 +34,6 @@ inline bool IsNewer(U value, U prev_value) {
|
|||
static_cast<U>(value - prev_value) < kBreakpoint;
|
||||
}
|
||||
|
||||
// Utility class to unwrap a number to a larger type. The numbers will never be
|
||||
// unwrapped to a negative value.
|
||||
template <typename U>
|
||||
class Unwrapper {
|
||||
static_assert(!std::numeric_limits<U>::is_signed, "U must be unsigned");
|
||||
static_assert(std::numeric_limits<U>::max() <=
|
||||
std::numeric_limits<uint32_t>::max(),
|
||||
"U must not be wider than 32 bits");
|
||||
|
||||
public:
|
||||
// Get the unwrapped value, but don't update the internal state.
|
||||
int64_t UnwrapWithoutUpdate(U value) const {
|
||||
if (!last_value_)
|
||||
return value;
|
||||
|
||||
constexpr int64_t kMaxPlusOne =
|
||||
static_cast<int64_t>(std::numeric_limits<U>::max()) + 1;
|
||||
|
||||
U cropped_last = static_cast<U>(*last_value_);
|
||||
int64_t delta = value - cropped_last;
|
||||
if (IsNewer(value, cropped_last)) {
|
||||
if (delta < 0)
|
||||
delta += kMaxPlusOne; // Wrap forwards.
|
||||
} else if (delta > 0 && (*last_value_ + delta - kMaxPlusOne) >= 0) {
|
||||
// If value is older but delta is positive, this is a backwards
|
||||
// wrap-around. However, don't wrap backwards past 0 (unwrapped).
|
||||
delta -= kMaxPlusOne;
|
||||
}
|
||||
|
||||
return *last_value_ + delta;
|
||||
}
|
||||
|
||||
// Only update the internal state to the specified last (unwrapped) value.
|
||||
void UpdateLast(int64_t last_value) { last_value_ = last_value; }
|
||||
|
||||
// Unwrap the value and update the internal state.
|
||||
int64_t Unwrap(U value) {
|
||||
int64_t unwrapped = UnwrapWithoutUpdate(value);
|
||||
UpdateLast(unwrapped);
|
||||
return unwrapped;
|
||||
}
|
||||
|
||||
private:
|
||||
absl::optional<int64_t> last_value_;
|
||||
};
|
||||
|
||||
using SequenceNumberUnwrapper = Unwrapper<uint16_t>;
|
||||
using TimestampUnwrapper = Unwrapper<uint32_t>;
|
||||
|
||||
// NB: Doesn't fulfill strict weak ordering requirements.
|
||||
// Mustn't be used as std::map Compare function.
|
||||
inline bool IsNewerSequenceNumber(uint16_t sequence_number,
|
||||
|
|
|
@ -113,112 +113,4 @@ TEST(LatestTimestamp, Wrap) {
|
|||
EXPECT_EQ(0x0000FFFFu, LatestTimestamp(0xFFFF0000, 0x0000FFFF));
|
||||
}
|
||||
|
||||
TEST(SequenceNumberUnwrapper, Limits) {
|
||||
SequenceNumberUnwrapper unwrapper;
|
||||
|
||||
EXPECT_EQ(0, unwrapper.Unwrap(0));
|
||||
EXPECT_EQ(0x8000, unwrapper.Unwrap(0x8000));
|
||||
// Delta is exactly 0x8000 but current is lower than input, wrap backwards.
|
||||
EXPECT_EQ(0, unwrapper.Unwrap(0));
|
||||
|
||||
EXPECT_EQ(0x8000, unwrapper.Unwrap(0x8000));
|
||||
EXPECT_EQ(0xFFFF, unwrapper.Unwrap(0xFFFF));
|
||||
EXPECT_EQ(0x10000, unwrapper.Unwrap(0));
|
||||
EXPECT_EQ(0xFFFF, unwrapper.Unwrap(0xFFFF));
|
||||
EXPECT_EQ(0x8000, unwrapper.Unwrap(0x8000));
|
||||
EXPECT_EQ(0, unwrapper.Unwrap(0));
|
||||
|
||||
// Don't allow negative values.
|
||||
EXPECT_EQ(0xFFFF, unwrapper.Unwrap(0xFFFF));
|
||||
}
|
||||
|
||||
TEST(SequenceNumberUnwrapper, ForwardWraps) {
|
||||
int64_t seq = 0;
|
||||
SequenceNumberUnwrapper unwrapper;
|
||||
|
||||
const int kMaxIncrease = 0x8000 - 1;
|
||||
const int kNumWraps = 4;
|
||||
for (int i = 0; i < kNumWraps * 2; ++i) {
|
||||
int64_t unwrapped = unwrapper.Unwrap(static_cast<uint16_t>(seq & 0xFFFF));
|
||||
EXPECT_EQ(seq, unwrapped);
|
||||
seq += kMaxIncrease;
|
||||
}
|
||||
|
||||
unwrapper.UpdateLast(0);
|
||||
for (int seq = 0; seq < kNumWraps * 0xFFFF; ++seq) {
|
||||
int64_t unwrapped = unwrapper.Unwrap(static_cast<uint16_t>(seq & 0xFFFF));
|
||||
EXPECT_EQ(seq, unwrapped);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(SequenceNumberUnwrapper, BackwardWraps) {
|
||||
SequenceNumberUnwrapper unwrapper;
|
||||
|
||||
const int kMaxDecrease = 0x8000 - 1;
|
||||
const int kNumWraps = 4;
|
||||
int64_t seq = kNumWraps * 2 * kMaxDecrease;
|
||||
unwrapper.UpdateLast(seq);
|
||||
for (int i = kNumWraps * 2; i >= 0; --i) {
|
||||
int64_t unwrapped = unwrapper.Unwrap(static_cast<uint16_t>(seq & 0xFFFF));
|
||||
EXPECT_EQ(seq, unwrapped);
|
||||
seq -= kMaxDecrease;
|
||||
}
|
||||
|
||||
seq = kNumWraps * 0xFFFF;
|
||||
unwrapper.UpdateLast(seq);
|
||||
for (; seq >= 0; --seq) {
|
||||
int64_t unwrapped = unwrapper.Unwrap(static_cast<uint16_t>(seq & 0xFFFF));
|
||||
EXPECT_EQ(seq, unwrapped);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(TimestampUnwrapper, Limits) {
|
||||
TimestampUnwrapper unwrapper;
|
||||
|
||||
EXPECT_EQ(0, unwrapper.Unwrap(0));
|
||||
EXPECT_EQ(0x80000000, unwrapper.Unwrap(0x80000000));
|
||||
// Delta is exactly 0x80000000 but current is lower than input, wrap
|
||||
// backwards.
|
||||
EXPECT_EQ(0, unwrapper.Unwrap(0));
|
||||
|
||||
EXPECT_EQ(0x80000000, unwrapper.Unwrap(0x80000000));
|
||||
EXPECT_EQ(0xFFFFFFFF, unwrapper.Unwrap(0xFFFFFFFF));
|
||||
EXPECT_EQ(0x100000000, unwrapper.Unwrap(0x00000000));
|
||||
EXPECT_EQ(0xFFFFFFFF, unwrapper.Unwrap(0xFFFFFFFF));
|
||||
EXPECT_EQ(0x80000000, unwrapper.Unwrap(0x80000000));
|
||||
EXPECT_EQ(0, unwrapper.Unwrap(0));
|
||||
|
||||
// Don't allow negative values.
|
||||
EXPECT_EQ(0xFFFFFFFF, unwrapper.Unwrap(0xFFFFFFFF));
|
||||
}
|
||||
|
||||
TEST(TimestampUnwrapper, ForwardWraps) {
|
||||
int64_t ts = 0;
|
||||
TimestampUnwrapper unwrapper;
|
||||
|
||||
const int64_t kMaxIncrease = 0x80000000 - 1;
|
||||
const int kNumWraps = 4;
|
||||
for (int i = 0; i < kNumWraps * 2; ++i) {
|
||||
int64_t unwrapped =
|
||||
unwrapper.Unwrap(static_cast<uint32_t>(ts & 0xFFFFFFFF));
|
||||
EXPECT_EQ(ts, unwrapped);
|
||||
ts += kMaxIncrease;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(TimestampUnwrapper, BackwardWraps) {
|
||||
TimestampUnwrapper unwrapper;
|
||||
|
||||
const int64_t kMaxDecrease = 0x80000000 - 1;
|
||||
const int kNumWraps = 4;
|
||||
int64_t ts = kNumWraps * 2 * kMaxDecrease;
|
||||
unwrapper.UpdateLast(ts);
|
||||
for (int i = 0; i <= kNumWraps * 2; ++i) {
|
||||
int64_t unwrapped =
|
||||
unwrapper.Unwrap(static_cast<uint32_t>(ts & 0xFFFFFFFF));
|
||||
EXPECT_EQ(ts, unwrapped);
|
||||
ts -= kMaxDecrease;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
|
|
Loading…
Reference in a new issue