blob: d11a7d414098537cf70d238d8cbe06dc6b2c78d1 [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 {
Danil Chapovalov0c626af2020-02-10 11:16:00 +010019constexpr TimeDelta kDefaultWindow = TimeDelta::Millis(1000);
Erik Språng7daf5502019-08-14 11:04:40 +020020}
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());
Danil Chapovalov0c626af2020-02-10 11:16:00 +010035 clock_.AdvanceTime(TimeDelta::Millis(33));
Erik Språng7daf5502019-08-14 11:04:40 +020036 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());
Danil Chapovalov0c626af2020-02-10 11:16:00 +010049 clock_.AdvanceTime(TimeDelta::Seconds(1) / kExpectedFps);
Erik Språng7daf5502019-08-14 11:04:40 +020050 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());
Danil Chapovalov0c626af2020-02-10 11:16:00 +010064 clock_.AdvanceTime(TimeDelta::Seconds(1) / kLowFps);
Erik Språng7daf5502019-08-14 11:04:40 +020065 }
66 while (clock_.CurrentTime() - start_time < kDefaultWindow) {
67 estimator_.OnFrame(clock_.CurrentTime());
Danil Chapovalov0c626af2020-02-10 11:16:00 +010068 clock_.AdvanceTime(TimeDelta::Seconds(1) / kHighFps);
Erik Språng7daf5502019-08-14 11:04:40 +020069 }
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.
Danil Chapovalov0c626af2020-02-10 11:16:00 +010084 clock_.AdvanceTime(TimeDelta::Micros(1));
Erik Språng7daf5502019-08-14 11:04:40 +020085 EXPECT_FALSE(estimator_.GetAverageFps(clock_.CurrentTime()));
86}
87
88TEST_F(FrameRateEstimatorTest, Reset) {
89 estimator_.OnFrame(clock_.CurrentTime());
Danil Chapovalov0c626af2020-02-10 11:16:00 +010090 clock_.AdvanceTime(TimeDelta::Seconds(1) / 30);
Erik Språng7daf5502019-08-14 11:04:40 +020091 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());
Danil Chapovalov0c626af2020-02-10 11:16:00 +010098 clock_.AdvanceTime(TimeDelta::Seconds(1) / 30);
Erik Språng7daf5502019-08-14 11:04:40 +020099 estimator_.OnFrame(clock_.CurrentTime());
100 EXPECT_FALSE(estimator_.GetAverageFps());
101}
102
103} // namespace webrtc