1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "FrameDropper_test"
19 #include <utils/Log.h>
20 
21 #include <gtest/gtest.h>
22 
23 #include "FrameDropper.h"
24 #include <media/stagefright/foundation/ADebug.h>
25 
26 namespace android {
27 
28 struct TestFrame {
29   int64_t timeUs;
30   bool shouldDrop;
31 };
32 
33 static const TestFrame testFrames20Fps[] = {
34     {1000000, false}, {1050000, false}, {1100000, false}, {1150000, false},
35     {1200000, false}, {1250000, false}, {1300000, false}, {1350000, false},
36     {1400000, false}, {1450000, false}, {1500000, false}, {1550000, false},
37     {1600000, false}, {1650000, false}, {1700000, false}, {1750000, false},
38     {1800000, false}, {1850000, false}, {1900000, false}, {1950000, false},
39 };
40 
41 static const TestFrame testFrames30Fps[] = {
42     {1000000, false}, {1033333, false}, {1066667, false}, {1100000, false},
43     {1133333, false}, {1166667, false}, {1200000, false}, {1233333, false},
44     {1266667, false}, {1300000, false}, {1333333, false}, {1366667, false},
45     {1400000, false}, {1433333, false}, {1466667, false}, {1500000, false},
46     {1533333, false}, {1566667, false}, {1600000, false}, {1633333, false},
47 };
48 
49 static const TestFrame testFrames40Fps[] = {
50     {1000000, false}, {1025000, true}, {1050000, false}, {1075000, false},
51     {1100000, false}, {1125000, true}, {1150000, false}, {1175000, false},
52     {1200000, false}, {1225000, true}, {1250000, false}, {1275000, false},
53     {1300000, false}, {1325000, true}, {1350000, false}, {1375000, false},
54     {1400000, false}, {1425000, true}, {1450000, false}, {1475000, false},
55 };
56 
57 static const TestFrame testFrames60Fps[] = {
58     {1000000, false}, {1016667, true}, {1033333, false}, {1050000, true},
59     {1066667, false}, {1083333, true}, {1100000, false}, {1116667, true},
60     {1133333, false}, {1150000, true}, {1166667, false}, {1183333, true},
61     {1200000, false}, {1216667, true}, {1233333, false}, {1250000, true},
62     {1266667, false}, {1283333, true}, {1300000, false}, {1316667, true},
63 };
64 
65 static const TestFrame testFramesVariableFps[] = {
66     // 40fps
67     {1000000, false}, {1025000, true}, {1050000, false}, {1075000, false},
68     {1100000, false}, {1125000, true}, {1150000, false}, {1175000, false},
69     {1200000, false}, {1225000, true}, {1250000, false}, {1275000, false},
70     {1300000, false}, {1325000, true}, {1350000, false}, {1375000, false},
71     {1400000, false}, {1425000, true}, {1450000, false}, {1475000, false},
72     // a timestamp jump plus switch to 20fps
73     {2000000, false}, {2050000, false}, {2100000, false}, {2150000, false},
74     {2200000, false}, {2250000, false}, {2300000, false}, {2350000, false},
75     {2400000, false}, {2450000, false}, {2500000, false}, {2550000, false},
76     {2600000, false}, {2650000, false}, {2700000, false}, {2750000, false},
77     {2800000, false}, {2850000, false}, {2900000, false}, {2950000, false},
78     // 60fps
79     {2966667, false}, {2983333, true}, {3000000, false}, {3016667, true},
80     {3033333, false}, {3050000, true}, {3066667, false}, {3083333, true},
81     {3100000, false}, {3116667, true}, {3133333, false}, {3150000, true},
82     {3166667, false}, {3183333, true}, {3200000, false}, {3216667, true},
83     {3233333, false}, {3250000, true}, {3266667, false}, {3283333, true},
84 };
85 
86 static const int kMaxTestJitterUs = 2000;
87 // return one of 1000, 0, -1000 as jitter.
GetJitter(size_t i)88 static int GetJitter(size_t i) {
89     return (1 - (i % 3)) * (kMaxTestJitterUs / 2);
90 }
91 
92 class FrameDropperTest : public ::testing::Test {
93 public:
FrameDropperTest()94     FrameDropperTest() : mFrameDropper(new FrameDropper()) {
95         EXPECT_EQ(OK, mFrameDropper->setMaxFrameRate(30.0));
96     }
97 
98 protected:
RunTest(const TestFrame * frames,size_t size)99     void RunTest(const TestFrame* frames, size_t size) {
100         for (size_t i = 0; i < size; ++i) {
101             int jitter = GetJitter(i);
102             int64_t testTimeUs = frames[i].timeUs + jitter;
103             printf("time %lld, testTime %lld, jitter %d\n",
104                     (long long)frames[i].timeUs, (long long)testTimeUs, jitter);
105             EXPECT_EQ(frames[i].shouldDrop, mFrameDropper->shouldDrop(testTimeUs));
106         }
107     }
108 
109     sp<FrameDropper> mFrameDropper;
110 };
111 
TEST_F(FrameDropperTest,TestInvalidMaxFrameRate)112 TEST_F(FrameDropperTest, TestInvalidMaxFrameRate) {
113     EXPECT_NE(OK, mFrameDropper->setMaxFrameRate(-1.0));
114     EXPECT_NE(OK, mFrameDropper->setMaxFrameRate(0));
115 }
116 
TEST_F(FrameDropperTest,Test20Fps)117 TEST_F(FrameDropperTest, Test20Fps) {
118     RunTest(testFrames20Fps, ARRAY_SIZE(testFrames20Fps));
119 }
120 
TEST_F(FrameDropperTest,Test30Fps)121 TEST_F(FrameDropperTest, Test30Fps) {
122     RunTest(testFrames30Fps, ARRAY_SIZE(testFrames30Fps));
123 }
124 
TEST_F(FrameDropperTest,Test40Fps)125 TEST_F(FrameDropperTest, Test40Fps) {
126     RunTest(testFrames40Fps, ARRAY_SIZE(testFrames40Fps));
127 }
128 
TEST_F(FrameDropperTest,Test60Fps)129 TEST_F(FrameDropperTest, Test60Fps) {
130     RunTest(testFrames60Fps, ARRAY_SIZE(testFrames60Fps));
131 }
132 
TEST_F(FrameDropperTest,TestVariableFps)133 TEST_F(FrameDropperTest, TestVariableFps) {
134     RunTest(testFramesVariableFps, ARRAY_SIZE(testFramesVariableFps));
135 }
136 
137 } // namespace android
138