xref: /aosp_15_r20/external/webrtc/common_video/frame_rate_estimator_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2019 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 "common_video/frame_rate_estimator.h"
12 
13 #include "system_wrappers/include/clock.h"
14 #include "test/gmock.h"
15 #include "test/gtest.h"
16 
17 namespace webrtc {
18 namespace {
19 constexpr TimeDelta kDefaultWindow = TimeDelta::Millis(1000);
20 }
21 
22 class FrameRateEstimatorTest : public ::testing::Test {
23  public:
FrameRateEstimatorTest()24   FrameRateEstimatorTest() : clock_(123), estimator_(kDefaultWindow) {}
25 
26  protected:
27   SimulatedClock clock_;
28   FrameRateEstimator estimator_;
29 };
30 
TEST_F(FrameRateEstimatorTest,NoEstimateWithLessThanTwoFrames)31 TEST_F(FrameRateEstimatorTest, NoEstimateWithLessThanTwoFrames) {
32   EXPECT_FALSE(estimator_.GetAverageFps());
33   estimator_.OnFrame(clock_.CurrentTime());
34   EXPECT_FALSE(estimator_.GetAverageFps());
35   clock_.AdvanceTime(TimeDelta::Millis(33));
36   EXPECT_FALSE(estimator_.GetAverageFps());
37 }
38 
TEST_F(FrameRateEstimatorTest,NoEstimateWithZeroSpan)39 TEST_F(FrameRateEstimatorTest, NoEstimateWithZeroSpan) {
40   // Two frames, but they are spanning 0ms so can't estimate frame rate.
41   estimator_.OnFrame(clock_.CurrentTime());
42   estimator_.OnFrame(clock_.CurrentTime());
43   EXPECT_FALSE(estimator_.GetAverageFps());
44 }
45 
TEST_F(FrameRateEstimatorTest,SingleSpanFps)46 TEST_F(FrameRateEstimatorTest, SingleSpanFps) {
47   const double kExpectedFps = 30.0;
48   estimator_.OnFrame(clock_.CurrentTime());
49   clock_.AdvanceTime(TimeDelta::Seconds(1) / kExpectedFps);
50   estimator_.OnFrame(clock_.CurrentTime());
51   EXPECT_NEAR(*estimator_.GetAverageFps(), kExpectedFps, 0.001);
52 }
53 
TEST_F(FrameRateEstimatorTest,AverageFps)54 TEST_F(FrameRateEstimatorTest, AverageFps) {
55   // Insert frames a intervals corresponding to 10fps for half the window, then
56   // 40fps half the window. The average should be 20fps.
57   const double kLowFps = 10.0;
58   const double kHighFps = 30.0;
59   const double kExpectedFps = 20.0;
60 
61   const Timestamp start_time = clock_.CurrentTime();
62   while (clock_.CurrentTime() - start_time < kDefaultWindow / 2) {
63     estimator_.OnFrame(clock_.CurrentTime());
64     clock_.AdvanceTime(TimeDelta::Seconds(1) / kLowFps);
65   }
66   while (clock_.CurrentTime() - start_time < kDefaultWindow) {
67     estimator_.OnFrame(clock_.CurrentTime());
68     clock_.AdvanceTime(TimeDelta::Seconds(1) / kHighFps);
69   }
70 
71   EXPECT_NEAR(*estimator_.GetAverageFps(), kExpectedFps, 0.001);
72 }
73 
TEST_F(FrameRateEstimatorTest,CullsOldFramesFromAveragingWindow)74 TEST_F(FrameRateEstimatorTest, CullsOldFramesFromAveragingWindow) {
75   // Two frames, just on the border of the 1s window => 1 fps.
76   estimator_.OnFrame(clock_.CurrentTime());
77   clock_.AdvanceTime(kDefaultWindow);
78   estimator_.OnFrame(clock_.CurrentTime());
79   EXPECT_TRUE(estimator_.GetAverageFps());
80   EXPECT_NEAR(*estimator_.GetAverageFps(), 1.0, 0.001);
81 
82   // Oldest frame should just be pushed out the window, leaving a single frame
83   // => no estimate possible.
84   clock_.AdvanceTime(TimeDelta::Micros(1));
85   EXPECT_FALSE(estimator_.GetAverageFps(clock_.CurrentTime()));
86 }
87 
TEST_F(FrameRateEstimatorTest,Reset)88 TEST_F(FrameRateEstimatorTest, Reset) {
89   estimator_.OnFrame(clock_.CurrentTime());
90   clock_.AdvanceTime(TimeDelta::Seconds(1) / 30);
91   estimator_.OnFrame(clock_.CurrentTime());
92   EXPECT_TRUE(estimator_.GetAverageFps());
93 
94   // Clear estimator, no estimate should be possible even after inserting one
95   // new frame.
96   estimator_.Reset();
97   EXPECT_FALSE(estimator_.GetAverageFps());
98   clock_.AdvanceTime(TimeDelta::Seconds(1) / 30);
99   estimator_.OnFrame(clock_.CurrentTime());
100   EXPECT_FALSE(estimator_.GetAverageFps());
101 }
102 
103 }  // namespace webrtc
104