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