Johannes Kron | 82c4821 | 2021-04-09 14:03:22 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 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 | #include "modules/video_coding/timestamp_map.h" |
| 12 | |
| 13 | #include "test/gmock.h" |
| 14 | #include "test/gtest.h" |
| 15 | |
| 16 | namespace webrtc { |
| 17 | namespace video_coding { |
| 18 | namespace { |
| 19 | constexpr int kTimestampMapSize = 6; |
| 20 | constexpr int kTimestamp1 = 1; |
| 21 | constexpr int kTimestamp2 = 2; |
| 22 | constexpr int kNoExistingTimestamp3 = 3; |
| 23 | constexpr int kTimestamp4 = 4; |
| 24 | constexpr int kTimestamp5 = 5; |
| 25 | constexpr int kTimestamp6 = 6; |
| 26 | constexpr int kTimestamp7 = 7; |
| 27 | constexpr int64_t kRenderTime1 = 1000; |
| 28 | constexpr int64_t kRenderTime2 = 2000; |
| 29 | constexpr int64_t kRenderTime4 = 4000; |
| 30 | constexpr int64_t kRenderTime5 = 5000; |
| 31 | constexpr int64_t kRenderTime6 = 6000; |
| 32 | constexpr int64_t kRenderTime7 = 7000; |
| 33 | } // namespace |
| 34 | |
| 35 | class VcmTimestampMapTest : public ::testing::Test { |
| 36 | protected: |
| 37 | VcmTimestampMapTest() : _timestampMap(kTimestampMapSize) {} |
| 38 | |
| 39 | void SetUp() override { |
| 40 | _timestampMap.Add(kTimestamp1, VCMFrameInformation({kRenderTime1})); |
| 41 | _timestampMap.Add(kTimestamp2, VCMFrameInformation({kRenderTime2})); |
| 42 | _timestampMap.Add(kTimestamp4, VCMFrameInformation({kRenderTime4})); |
| 43 | } |
| 44 | |
| 45 | VCMTimestampMap _timestampMap; |
| 46 | }; |
| 47 | |
| 48 | TEST_F(VcmTimestampMapTest, PopExistingFrameInfo) { |
| 49 | EXPECT_EQ(_timestampMap.Size(), 3u); |
| 50 | auto frameInfo = _timestampMap.Pop(kTimestamp1); |
| 51 | ASSERT_TRUE(frameInfo); |
| 52 | EXPECT_EQ(frameInfo->renderTimeMs, kRenderTime1); |
| 53 | frameInfo = _timestampMap.Pop(kTimestamp2); |
| 54 | ASSERT_TRUE(frameInfo); |
| 55 | EXPECT_EQ(frameInfo->renderTimeMs, kRenderTime2); |
| 56 | frameInfo = _timestampMap.Pop(kTimestamp4); |
| 57 | ASSERT_TRUE(frameInfo); |
| 58 | EXPECT_EQ(frameInfo->renderTimeMs, kRenderTime4); |
| 59 | } |
| 60 | |
| 61 | TEST_F(VcmTimestampMapTest, PopNonexistingClearsOlderFrameInfos) { |
| 62 | auto frameInfo = _timestampMap.Pop(kNoExistingTimestamp3); |
| 63 | EXPECT_FALSE(frameInfo); |
| 64 | EXPECT_EQ(_timestampMap.Size(), 1u); |
| 65 | } |
| 66 | |
| 67 | TEST_F(VcmTimestampMapTest, SizeIsIncrementedWhenAddingNewFrameInfo) { |
| 68 | EXPECT_EQ(_timestampMap.Size(), 3u); |
| 69 | _timestampMap.Add(kTimestamp5, VCMFrameInformation({kRenderTime5})); |
| 70 | EXPECT_EQ(_timestampMap.Size(), 4u); |
| 71 | _timestampMap.Add(kTimestamp6, VCMFrameInformation({kRenderTime6})); |
| 72 | EXPECT_EQ(_timestampMap.Size(), 5u); |
| 73 | } |
| 74 | |
| 75 | TEST_F(VcmTimestampMapTest, SizeIsDecreasedWhenPoppingFrameInfo) { |
| 76 | EXPECT_EQ(_timestampMap.Size(), 3u); |
| 77 | EXPECT_TRUE(_timestampMap.Pop(kTimestamp1)); |
| 78 | EXPECT_EQ(_timestampMap.Size(), 2u); |
| 79 | EXPECT_TRUE(_timestampMap.Pop(kTimestamp2)); |
| 80 | EXPECT_EQ(_timestampMap.Size(), 1u); |
| 81 | EXPECT_FALSE(_timestampMap.Pop(kNoExistingTimestamp3)); |
| 82 | EXPECT_EQ(_timestampMap.Size(), 1u); |
| 83 | EXPECT_TRUE(_timestampMap.Pop(kTimestamp4)); |
| 84 | EXPECT_EQ(_timestampMap.Size(), 0u); |
| 85 | } |
| 86 | |
| 87 | TEST_F(VcmTimestampMapTest, ClearEmptiesMap) { |
| 88 | EXPECT_EQ(_timestampMap.Size(), 3u); |
| 89 | _timestampMap.Clear(); |
| 90 | EXPECT_EQ(_timestampMap.Size(), 0u); |
| 91 | // Clear empty map does nothing. |
| 92 | _timestampMap.Clear(); |
| 93 | EXPECT_EQ(_timestampMap.Size(), 0u); |
| 94 | } |
| 95 | |
| 96 | TEST_F(VcmTimestampMapTest, PopLastAddedClearsMap) { |
| 97 | EXPECT_EQ(_timestampMap.Size(), 3u); |
| 98 | EXPECT_TRUE(_timestampMap.Pop(kTimestamp4)); |
| 99 | EXPECT_EQ(_timestampMap.Size(), 0u); |
| 100 | } |
| 101 | |
| 102 | TEST_F(VcmTimestampMapTest, LastAddedIsDiscardedIfMapGetsFull) { |
| 103 | EXPECT_EQ(_timestampMap.Size(), 3u); |
| 104 | _timestampMap.Add(kTimestamp5, VCMFrameInformation({kRenderTime5})); |
| 105 | EXPECT_EQ(_timestampMap.Size(), 4u); |
| 106 | _timestampMap.Add(kTimestamp6, VCMFrameInformation({kRenderTime6})); |
| 107 | EXPECT_EQ(_timestampMap.Size(), 5u); |
| 108 | _timestampMap.Add(kTimestamp7, VCMFrameInformation({kRenderTime7})); |
| 109 | // Size is not incremented since the oldest element is discarded. |
| 110 | EXPECT_EQ(_timestampMap.Size(), 5u); |
| 111 | EXPECT_FALSE(_timestampMap.Pop(kTimestamp1)); |
| 112 | EXPECT_TRUE(_timestampMap.Pop(kTimestamp2)); |
| 113 | EXPECT_TRUE(_timestampMap.Pop(kTimestamp4)); |
| 114 | EXPECT_TRUE(_timestampMap.Pop(kTimestamp5)); |
| 115 | EXPECT_TRUE(_timestampMap.Pop(kTimestamp6)); |
| 116 | EXPECT_TRUE(_timestampMap.Pop(kTimestamp7)); |
| 117 | EXPECT_EQ(_timestampMap.Size(), 0u); |
| 118 | } |
| 119 | |
| 120 | } // namespace video_coding |
| 121 | } // namespace webrtc |