blob: 9058bac4140bc881d8af64b838ca472848d30831 [file] [log] [blame]
Erik Språng7daf5502019-08-14 11:04:40 +02001/*
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
17namespace webrtc {
18namespace {
19constexpr TimeDelta kDefaultWindow = TimeDelta::Millis<1000>();
20}
21
22class FrameRateEstimatorTest : public ::testing::Test {
23 public:
24 FrameRateEstimatorTest() : clock_(123), estimator_(kDefaultWindow) {}
25
26 protected:
27 SimulatedClock clock_;
28 FrameRateEstimator estimator_;
29};
30
31TEST_F(FrameRateEstimatorTest, NoEstimateWithLessThanTwoFrames) {
32 EXPECT_FALSE(estimator_.GetAverageFps());
33 estimator_.OnFrame(clock_.CurrentTime());
34 EXPECT_FALSE(estimator_.GetAverageFps());
35 clock_.AdvanceTime(TimeDelta::ms(33));
36 EXPECT_FALSE(estimator_.GetAverageFps());
37}
38
39TEST_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
46TEST_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
54TEST_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
74TEST_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::us(1));
85 EXPECT_FALSE(estimator_.GetAverageFps(clock_.CurrentTime()));
86}
87
88TEST_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