1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
media / filters / video_cadence_estimator_unittest.cc [blame]
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/filters/video_cadence_estimator.h"
#include <math.h>
#include <stddef.h>
#include <memory>
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "media/base/media_switches.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
// See VideoCadenceEstimator header for more details.
constexpr auto kMinimumAcceptableTimeBetweenGlitches = base::Seconds(8);
// Slows down the given |fps| according to NTSC field reduction standards; see
// http://en.wikipedia.org/wiki/Frame_rate#Digital_video_and_television
static double NTSC(double fps) {
return fps / 1.001;
}
static base::TimeDelta Interval(double hertz) {
return base::Seconds(1.0 / hertz);
}
std::vector<int> CreateCadenceFromString(const std::string& cadence) {
CHECK_EQ('[', cadence.front());
CHECK_EQ(']', cadence.back());
std::vector<int> result;
for (const std::string& token :
base::SplitString(cadence.substr(1, cadence.length() - 2),
":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
int cadence_value = 0;
CHECK(base::StringToInt(token, &cadence_value)) << token;
result.push_back(cadence_value);
}
return result;
}
static void VerifyCadenceVectorWithCustomDeviationAndDrift(
VideoCadenceEstimator* estimator,
double frame_hertz,
double render_hertz,
base::TimeDelta deviation,
base::TimeDelta acceptable_drift,
const std::string& expected_cadence) {
SCOPED_TRACE(base::StringPrintf("Checking %.03f fps into %0.03f", frame_hertz,
render_hertz));
const std::vector<int> expected_cadence_vector =
CreateCadenceFromString(expected_cadence);
estimator->Reset();
const bool cadence_changed = estimator->UpdateCadenceEstimate(
Interval(render_hertz), Interval(frame_hertz), deviation,
acceptable_drift);
EXPECT_EQ(cadence_changed, estimator->has_cadence());
EXPECT_EQ(expected_cadence_vector.empty(), !estimator->has_cadence());
// Nothing further to test.
if (expected_cadence_vector.empty() || !estimator->has_cadence())
return;
EXPECT_EQ(expected_cadence_vector.size(),
estimator->cadence_size_for_testing());
// Spot two cycles of the cadence.
for (size_t i = 0; i < expected_cadence_vector.size() * 2; ++i) {
ASSERT_EQ(expected_cadence_vector[i % expected_cadence_vector.size()],
estimator->GetCadenceForFrame(i));
}
}
static void VerifyCadenceVectorWithCustomDrift(
VideoCadenceEstimator* estimator,
double frame_hertz,
double render_hertz,
base::TimeDelta acceptable_drift,
const std::string& expected_cadence) {
VerifyCadenceVectorWithCustomDeviationAndDrift(
estimator, frame_hertz, render_hertz, base::TimeDelta(), acceptable_drift,
expected_cadence);
}
static void VerifyCadenceVectorWithCustomDeviation(
VideoCadenceEstimator* estimator,
double frame_hertz,
double render_hertz,
base::TimeDelta deviation,
const std::string& expected_cadence) {
const base::TimeDelta acceptable_drift =
std::max(Interval(frame_hertz) / 2, Interval(render_hertz));
VerifyCadenceVectorWithCustomDeviationAndDrift(
estimator, frame_hertz, render_hertz, deviation, acceptable_drift,
expected_cadence);
}
static void VerifyCadenceVector(VideoCadenceEstimator* estimator,
double frame_hertz,
double render_hertz,
const std::string& expected_cadence) {
const base::TimeDelta acceptable_drift =
std::max(Interval(frame_hertz) / 2, Interval(render_hertz));
VerifyCadenceVectorWithCustomDeviationAndDrift(
estimator, frame_hertz, render_hertz, base::TimeDelta(), acceptable_drift,
expected_cadence);
}
// Spot check common display and frame rate pairs for correctness.
TEST(VideoCadenceEstimatorTest, CadenceCalculations) {
VideoCadenceEstimator estimator(kMinimumAcceptableTimeBetweenGlitches);
estimator.set_cadence_hysteresis_threshold_for_testing(base::TimeDelta());
const std::string kEmptyCadence = "[]";
VerifyCadenceVector(&estimator, 1, NTSC(60), "[60]");
VerifyCadenceVector(&estimator, 24, 60, "[3:2]");
VerifyCadenceVector(&estimator, NTSC(24), 60, "[3:2]");
VerifyCadenceVector(&estimator, 24, NTSC(60), "[3:2]");
VerifyCadenceVector(&estimator, 25, 60, "[2:3:2:3:2]");
VerifyCadenceVector(&estimator, NTSC(25), 60, "[2:3:2:3:2]");
VerifyCadenceVector(&estimator, 25, NTSC(60), "[2:3:2:3:2]");
VerifyCadenceVector(&estimator, 30, 60, "[2]");
VerifyCadenceVector(&estimator, NTSC(30), 60, "[2]");
VerifyCadenceVector(&estimator, 29.5, 60, kEmptyCadence);
VerifyCadenceVector(&estimator, 50, 60, "[1:1:2:1:1]");
VerifyCadenceVector(&estimator, NTSC(50), 60, "[1:1:2:1:1]");
VerifyCadenceVector(&estimator, 50, NTSC(60), "[1:1:2:1:1]");
VerifyCadenceVector(&estimator, NTSC(60), 60, "[1]");
VerifyCadenceVector(&estimator, 60, NTSC(60), "[1]");
VerifyCadenceVector(&estimator, 120, 60, "[1:0]");
VerifyCadenceVector(&estimator, NTSC(120), 60, "[1:0]");
VerifyCadenceVector(&estimator, 120, NTSC(60), "[1:0]");
// Test cases for cadence below 1.
VerifyCadenceVector(&estimator, 120, 24, "[1:0:0:0:0]");
VerifyCadenceVector(&estimator, 120, 48, "[1:0:0:1:0]");
VerifyCadenceVector(&estimator, 120, 72, "[1:0:1:0:1]");
VerifyCadenceVector(&estimator, 90, 60, "[1:0:1]");
// 50Hz is common in the EU.
VerifyCadenceVector(&estimator, NTSC(24), 50, kEmptyCadence);
VerifyCadenceVector(&estimator, 24, 50, kEmptyCadence);
VerifyCadenceVector(&estimator, NTSC(25), 50, "[2]");
VerifyCadenceVector(&estimator, 25, 50, "[2]");
VerifyCadenceVector(&estimator, NTSC(30), 50, "[2:1:2]");
VerifyCadenceVector(&estimator, 30, 50, "[2:1:2]");
VerifyCadenceVector(&estimator, NTSC(60), 50, kEmptyCadence);
VerifyCadenceVector(&estimator, 60, 50, kEmptyCadence);
}
// Check the extreme case that max_acceptable_drift is larger than
// minimum_time_until_max_drift.
TEST(VideoCadenceEstimatorTest, CadenceCalculationWithLargeDrift) {
VideoCadenceEstimator estimator(kMinimumAcceptableTimeBetweenGlitches);
estimator.set_cadence_hysteresis_threshold_for_testing(base::TimeDelta());
base::TimeDelta drift = base::Hours(1);
VerifyCadenceVectorWithCustomDrift(&estimator, 1, NTSC(60), drift, "[60]");
VerifyCadenceVectorWithCustomDrift(&estimator, 30, 60, drift, "[2]");
VerifyCadenceVectorWithCustomDrift(&estimator, NTSC(30), 60, drift, "[2]");
VerifyCadenceVectorWithCustomDrift(&estimator, 30, NTSC(60), drift, "[2]");
VerifyCadenceVectorWithCustomDrift(&estimator, 25, 60, drift, "[2]");
VerifyCadenceVectorWithCustomDrift(&estimator, NTSC(25), 60, drift, "[2]");
VerifyCadenceVectorWithCustomDrift(&estimator, 25, NTSC(60), drift, "[2]");
// Test cases for cadence below 1.
VerifyCadenceVectorWithCustomDrift(&estimator, 120, 24, drift, "[1]");
VerifyCadenceVectorWithCustomDrift(&estimator, 120, 48, drift, "[1]");
VerifyCadenceVectorWithCustomDrift(&estimator, 120, 72, drift, "[1]");
VerifyCadenceVectorWithCustomDrift(&estimator, 90, 60, drift, "[1]");
}
TEST(VideoCadenceEstimatorTest, SimpleCadenceTest) {
bool simple_cadence = VideoCadenceEstimator::HasSimpleCadence(
Interval(60), Interval(30), kMinimumAcceptableTimeBetweenGlitches);
// 60 Hz screen with 30 FPS video should be considered a simple cadence.
EXPECT_TRUE(simple_cadence);
simple_cadence = VideoCadenceEstimator::HasSimpleCadence(
Interval(60), Interval(24), kMinimumAcceptableTimeBetweenGlitches);
EXPECT_FALSE(simple_cadence);
}
// Check the case that the estimator excludes variable FPS case from Cadence.
TEST(VideoCadenceEstimatorTest, CadenceCalculationWithLargeDeviation) {
VideoCadenceEstimator estimator(kMinimumAcceptableTimeBetweenGlitches);
estimator.set_cadence_hysteresis_threshold_for_testing(base::TimeDelta());
const base::TimeDelta deviation = base::Milliseconds(30);
VerifyCadenceVectorWithCustomDeviation(&estimator, 1, 60, deviation, "[]");
VerifyCadenceVectorWithCustomDeviation(&estimator, 30, 60, deviation, "[]");
VerifyCadenceVectorWithCustomDeviation(&estimator, 25, 60, deviation, "[]");
// Test cases for cadence with low refresh rate.
VerifyCadenceVectorWithCustomDeviation(&estimator, 60, 12, deviation,
"[1:0:0:0:0]");
}
TEST(VideoCadenceEstimatorTest, CadenceVariesWithAcceptableDrift) {
VideoCadenceEstimator estimator(kMinimumAcceptableTimeBetweenGlitches);
estimator.set_cadence_hysteresis_threshold_for_testing(base::TimeDelta());
const base::TimeDelta render_interval = Interval(NTSC(60));
const base::TimeDelta frame_interval = Interval(120);
base::TimeDelta acceptable_drift = frame_interval / 2;
EXPECT_FALSE(estimator.UpdateCadenceEstimate(
render_interval, frame_interval, base::TimeDelta(), acceptable_drift));
EXPECT_FALSE(estimator.has_cadence());
// Increasing the acceptable drift should be result in more permissive
// detection of cadence.
acceptable_drift = render_interval;
EXPECT_TRUE(estimator.UpdateCadenceEstimate(
render_interval, frame_interval, base::TimeDelta(), acceptable_drift));
EXPECT_TRUE(estimator.has_cadence());
EXPECT_EQ("[1:0]", estimator.GetCadenceForTesting());
}
TEST(VideoCadenceEstimatorTest, CadenceVariesWithAcceptableGlitchTime) {
VideoCadenceEstimator estimator(kMinimumAcceptableTimeBetweenGlitches);
estimator.set_cadence_hysteresis_threshold_for_testing(base::TimeDelta());
const base::TimeDelta render_interval = Interval(NTSC(60));
const base::TimeDelta frame_interval = Interval(120);
const base::TimeDelta acceptable_drift = frame_interval / 2;
EXPECT_FALSE(estimator.UpdateCadenceEstimate(
render_interval, frame_interval, base::TimeDelta(), acceptable_drift));
EXPECT_FALSE(estimator.has_cadence());
// Decreasing the acceptable glitch time should be result in more permissive
// detection of cadence.
VideoCadenceEstimator permissive_estimator(
kMinimumAcceptableTimeBetweenGlitches / 2);
permissive_estimator.set_cadence_hysteresis_threshold_for_testing(
base::TimeDelta());
EXPECT_TRUE(permissive_estimator.UpdateCadenceEstimate(
render_interval, frame_interval, base::TimeDelta(), acceptable_drift));
EXPECT_TRUE(permissive_estimator.has_cadence());
EXPECT_EQ("[1:0]", permissive_estimator.GetCadenceForTesting());
}
TEST(VideoCadenceEstimatorTest, CadenceHystersisPreventsOscillation) {
VideoCadenceEstimator estimator(kMinimumAcceptableTimeBetweenGlitches);
const base::TimeDelta render_interval = Interval(30);
const base::TimeDelta frame_interval = Interval(60);
const base::TimeDelta acceptable_drift = frame_interval / 2;
estimator.set_cadence_hysteresis_threshold_for_testing(render_interval * 2);
// Cadence hysteresis should prevent the cadence from taking effect yet.
EXPECT_FALSE(estimator.UpdateCadenceEstimate(
render_interval, frame_interval, base::TimeDelta(), acceptable_drift));
EXPECT_FALSE(estimator.has_cadence());
// A second call should exceed cadence hysteresis and take into effect.
EXPECT_TRUE(estimator.UpdateCadenceEstimate(
render_interval, frame_interval, base::TimeDelta(), acceptable_drift));
EXPECT_TRUE(estimator.has_cadence());
// One bad interval shouldn't cause cadence to drop
EXPECT_FALSE(
estimator.UpdateCadenceEstimate(render_interval, frame_interval * 0.75,
base::TimeDelta(), acceptable_drift));
EXPECT_TRUE(estimator.has_cadence());
// Resumption of cadence should clear bad interval count.
EXPECT_FALSE(estimator.UpdateCadenceEstimate(
render_interval, frame_interval, base::TimeDelta(), acceptable_drift));
EXPECT_TRUE(estimator.has_cadence());
// So one more bad interval shouldn't cause cadence to drop
EXPECT_FALSE(
estimator.UpdateCadenceEstimate(render_interval, frame_interval * 0.75,
base::TimeDelta(), acceptable_drift));
EXPECT_TRUE(estimator.has_cadence());
// Two bad intervals should.
EXPECT_TRUE(
estimator.UpdateCadenceEstimate(render_interval, frame_interval * 0.75,
base::TimeDelta(), acceptable_drift));
EXPECT_FALSE(estimator.has_cadence());
}
TEST(VideoCadenceEstimatorTest, RenderIntervalChangingSkipsHystersis) {
VideoCadenceEstimator estimator(kMinimumAcceptableTimeBetweenGlitches);
const base::TimeDelta render_interval = Interval(60);
const base::TimeDelta frame_interval = Interval(30);
const base::TimeDelta acceptable_drift = frame_interval / 2;
estimator.set_cadence_hysteresis_threshold_for_testing(render_interval * 4);
// Wait for cadence to be detected.
int it_count = 0;
while (!estimator.has_cadence()) {
estimator.UpdateCadenceEstimate(render_interval, frame_interval,
base::TimeDelta(), acceptable_drift);
it_count++;
EXPECT_LE(it_count, 4);
}
// If |render_interval| changes, the hysteresis should be skipped and the
// candence should be updated immediately.
EXPECT_TRUE(estimator.UpdateCadenceEstimate(render_interval * 2,
frame_interval, base::TimeDelta(),
acceptable_drift));
EXPECT_TRUE(estimator.has_cadence());
// Minor changes (+/-10%) on |render_interval| should not trigger hyseteresis
// skipping.
EXPECT_FALSE(estimator.UpdateCadenceEstimate(
render_interval * 2 * 0.91, frame_interval, base::TimeDelta(),
acceptable_drift));
EXPECT_TRUE(estimator.has_cadence());
}
void VerifyCadenceSequence(VideoCadenceEstimator* estimator,
double frame_rate,
double display_rate,
std::vector<int> expected_cadence) {
SCOPED_TRACE(base::StringPrintf("Checking %.03f fps into %0.03f", frame_rate,
display_rate));
const base::TimeDelta render_interval = Interval(display_rate);
const base::TimeDelta frame_interval = Interval(frame_rate);
const base::TimeDelta acceptable_drift =
frame_interval < render_interval ? render_interval : frame_interval;
const base::TimeDelta test_runtime = base::Seconds(10 * 60);
const int test_frames = base::ClampFloor(test_runtime / frame_interval);
estimator->Reset();
EXPECT_TRUE(estimator->UpdateCadenceEstimate(
render_interval, frame_interval, base::TimeDelta(), acceptable_drift));
EXPECT_TRUE(estimator->has_cadence());
for (auto i = 0u; i < expected_cadence.size(); i++) {
ASSERT_EQ(expected_cadence[i], estimator->GetCadenceForFrame(i))
<< " i=" << i;
}
int total_display_cycles = 0;
for (int i = 0; i < test_frames; i++) {
total_display_cycles += estimator->GetCadenceForFrame(i);
base::TimeDelta drift =
(total_display_cycles * render_interval) - ((i + 1) * frame_interval);
EXPECT_LE(drift.magnitude(), acceptable_drift)
<< " i=" << i << " time=" << (total_display_cycles * render_interval);
if (drift.magnitude() > acceptable_drift)
break;
}
}
} // namespace media