xref: /aosp_15_r20/external/webrtc/modules/video_coding/codecs/test/videocodec_test_stats_impl_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2011 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/codecs/test/videocodec_test_stats_impl.h"
12 
13 #include <vector>
14 
15 #include "test/gmock.h"
16 #include "test/gtest.h"
17 
18 namespace webrtc {
19 namespace test {
20 
21 using FrameStatistics = VideoCodecTestStatsImpl::FrameStatistics;
22 
23 namespace {
24 
25 const size_t kTimestamp = 12345;
26 
27 using ::testing::AllOf;
28 using ::testing::Contains;
29 using ::testing::Field;
30 
31 }  // namespace
32 
TEST(StatsTest,AddAndGetFrame)33 TEST(StatsTest, AddAndGetFrame) {
34   VideoCodecTestStatsImpl stats;
35   stats.AddFrame(FrameStatistics(0, kTimestamp, 0));
36   FrameStatistics* frame_stat = stats.GetFrame(0u, 0);
37   EXPECT_EQ(0u, frame_stat->frame_number);
38   EXPECT_EQ(kTimestamp, frame_stat->rtp_timestamp);
39 }
40 
TEST(StatsTest,AddAndGetFrames)41 TEST(StatsTest, AddAndGetFrames) {
42   VideoCodecTestStatsImpl stats;
43   const size_t kNumFrames = 1000;
44   for (size_t i = 0; i < kNumFrames; ++i) {
45     stats.AddFrame(FrameStatistics(i, kTimestamp + i, 0));
46     FrameStatistics* frame_stat = stats.GetFrame(i, 0);
47     EXPECT_EQ(i, frame_stat->frame_number);
48     EXPECT_EQ(kTimestamp + i, frame_stat->rtp_timestamp);
49   }
50   EXPECT_EQ(kNumFrames, stats.Size(0));
51   // Get frame.
52   size_t i = 22;
53   FrameStatistics* frame_stat = stats.GetFrameWithTimestamp(kTimestamp + i, 0);
54   EXPECT_EQ(i, frame_stat->frame_number);
55   EXPECT_EQ(kTimestamp + i, frame_stat->rtp_timestamp);
56 }
57 
TEST(StatsTest,AddFrameLayering)58 TEST(StatsTest, AddFrameLayering) {
59   VideoCodecTestStatsImpl stats;
60   for (size_t i = 0; i < 3; ++i) {
61     stats.AddFrame(FrameStatistics(0, kTimestamp + i, i));
62     FrameStatistics* frame_stat = stats.GetFrame(0u, i);
63     EXPECT_EQ(0u, frame_stat->frame_number);
64     EXPECT_EQ(kTimestamp, frame_stat->rtp_timestamp - i);
65     EXPECT_EQ(1u, stats.Size(i));
66   }
67 }
68 
TEST(StatsTest,GetFrameStatistics)69 TEST(StatsTest, GetFrameStatistics) {
70   VideoCodecTestStatsImpl stats;
71 
72   stats.AddFrame(FrameStatistics(0, kTimestamp, 0));
73   stats.AddFrame(FrameStatistics(0, kTimestamp, 1));
74   stats.AddFrame(FrameStatistics(1, kTimestamp + 3000, 0));
75   stats.AddFrame(FrameStatistics(1, kTimestamp + 3000, 1));
76 
77   const std::vector<FrameStatistics> frame_stats = stats.GetFrameStatistics();
78 
79   auto field_matcher = [](size_t frame_number, size_t spatial_idx) {
80     return AllOf(Field(&FrameStatistics::frame_number, frame_number),
81                  Field(&FrameStatistics::spatial_idx, spatial_idx));
82   };
83   EXPECT_THAT(frame_stats, Contains(field_matcher(0, 0)));
84   EXPECT_THAT(frame_stats, Contains(field_matcher(0, 1)));
85   EXPECT_THAT(frame_stats, Contains(field_matcher(1, 0)));
86   EXPECT_THAT(frame_stats, Contains(field_matcher(1, 1)));
87 }
88 
89 }  // namespace test
90 }  // namespace webrtc
91