blob: 5e90786b959f9d1bf0ef2af8cc1f2d56aa3d4440 [file] [log] [blame]
Johannes Kron82c48212021-04-09 14:03:221/*
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
16namespace webrtc {
17namespace video_coding {
18namespace {
19constexpr int kTimestampMapSize = 6;
20constexpr int kTimestamp1 = 1;
21constexpr int kTimestamp2 = 2;
22constexpr int kNoExistingTimestamp3 = 3;
23constexpr int kTimestamp4 = 4;
24constexpr int kTimestamp5 = 5;
25constexpr int kTimestamp6 = 6;
26constexpr int kTimestamp7 = 7;
27constexpr int64_t kRenderTime1 = 1000;
28constexpr int64_t kRenderTime2 = 2000;
29constexpr int64_t kRenderTime4 = 4000;
30constexpr int64_t kRenderTime5 = 5000;
31constexpr int64_t kRenderTime6 = 6000;
32constexpr int64_t kRenderTime7 = 7000;
33} // namespace
34
35class 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
48TEST_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
61TEST_F(VcmTimestampMapTest, PopNonexistingClearsOlderFrameInfos) {
62 auto frameInfo = _timestampMap.Pop(kNoExistingTimestamp3);
63 EXPECT_FALSE(frameInfo);
64 EXPECT_EQ(_timestampMap.Size(), 1u);
65}
66
67TEST_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
75TEST_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
87TEST_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
96TEST_F(VcmTimestampMapTest, PopLastAddedClearsMap) {
97 EXPECT_EQ(_timestampMap.Size(), 3u);
98 EXPECT_TRUE(_timestampMap.Pop(kTimestamp4));
99 EXPECT_EQ(_timestampMap.Size(), 0u);
100}
101
102TEST_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