From b613d6228515037230748f4b5914925f6110fb0b Mon Sep 17 00:00:00 2001 From: Evan Shrubsole Date: Thu, 12 Jan 2023 12:28:01 +0000 Subject: [PATCH] [Unwrap] Delete webrtc::Unwrapper Bug: webrtc:13982 Change-Id: I501261b09a05080ec681ae120648938e350a05de Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/290890 Commit-Queue: Harald Alvestrand Reviewed-by: Harald Alvestrand Auto-Submit: Evan Shrubsole Cr-Commit-Position: refs/heads/main@{#39088} --- modules/include/module_common_types_public.h | 49 --------- modules/module_common_types_unittest.cc | 108 ------------------- 2 files changed, 157 deletions(-) diff --git a/modules/include/module_common_types_public.h b/modules/include/module_common_types_public.h index 345e45ce12..5a6f634df7 100644 --- a/modules/include/module_common_types_public.h +++ b/modules/include/module_common_types_public.h @@ -34,55 +34,6 @@ inline bool IsNewer(U value, U prev_value) { static_cast(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 -class Unwrapper { - static_assert(!std::numeric_limits::is_signed, "U must be unsigned"); - static_assert(std::numeric_limits::max() <= - std::numeric_limits::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(std::numeric_limits::max()) + 1; - - U cropped_last = static_cast(*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 last_value_; -}; - -using SequenceNumberUnwrapper = Unwrapper; -using TimestampUnwrapper = Unwrapper; - // NB: Doesn't fulfill strict weak ordering requirements. // Mustn't be used as std::map Compare function. inline bool IsNewerSequenceNumber(uint16_t sequence_number, diff --git a/modules/module_common_types_unittest.cc b/modules/module_common_types_unittest.cc index 0979af38e6..bec3b5da22 100644 --- a/modules/module_common_types_unittest.cc +++ b/modules/module_common_types_unittest.cc @@ -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(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(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(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(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(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(ts & 0xFFFFFFFF)); - EXPECT_EQ(ts, unwrapped); - ts -= kMaxDecrease; - } -} - } // namespace webrtc