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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <cmath>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/strings/stringprintf.h"
#include "base/test/simple_test_tick_clock.h"
#include "media/base/timestamp_constants.h"
#include "media/base/video_frame_pool.h"
#include "media/base/wall_clock_time_source.h"
#include "media/filters/video_renderer_algorithm.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
// 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;
}
// Helper class for generating TimeTicks in a sequence according to a frequency.
class TickGenerator {
public:
TickGenerator(base::TimeTicks base_timestamp, double hertz)
: tick_count_(0),
hertz_(hertz),
microseconds_per_tick_(base::Time::kMicrosecondsPerSecond / hertz),
base_time_(base_timestamp) {}
base::TimeDelta interval(int tick_count) const {
return base::TimeDelta::FromMicroseconds(tick_count *
microseconds_per_tick_);
}
base::TimeTicks current() const { return base_time_ + interval(tick_count_); }
base::TimeTicks step() { return step(1); }
base::TimeTicks step(int n) {
tick_count_ += n;
return current();
}
double hertz() const { return hertz_; }
void Reset(base::TimeTicks base_timestamp) {
base_time_ = base_timestamp;
tick_count_ = 0;
}
private:
// Track a tick count and seconds per tick value to ensure we don't drift too
// far due to accumulated errors during testing.
int64_t tick_count_;
const double hertz_;
const double microseconds_per_tick_;
base::TimeTicks base_time_;
DISALLOW_COPY_AND_ASSIGN(TickGenerator);
};
class VideoRendererAlgorithmTest : public testing::Test {
public:
VideoRendererAlgorithmTest()
: tick_clock_(new base::SimpleTestTickClock()),
algorithm_(base::Bind(&WallClockTimeSource::GetWallClockTimes,
base::Unretained(&time_source_))) {
// Always start the TickClock at a non-zero value since null values have
// special connotations.
tick_clock_->Advance(base::TimeDelta::FromMicroseconds(10000));
time_source_.set_tick_clock_for_testing(tick_clock_.get());
}
~VideoRendererAlgorithmTest() override {}
scoped_refptr<VideoFrame> CreateFrame(base::TimeDelta timestamp) {
const gfx::Size natural_size(8, 8);
return frame_pool_.CreateFrame(PIXEL_FORMAT_YV12, natural_size,
gfx::Rect(natural_size), natural_size,
timestamp);
}
base::TimeDelta minimum_glitch_time() const {
return base::TimeDelta::FromSeconds(
VideoRendererAlgorithm::kMinimumAcceptableTimeBetweenGlitchesSecs);
}
base::TimeDelta max_acceptable_drift() const {
return algorithm_.max_acceptable_drift_;
}
void disable_cadence_hysteresis() {
algorithm_.cadence_estimator_.set_cadence_hysteresis_threshold_for_testing(
base::TimeDelta());
}
bool last_render_had_glitch() const {
return algorithm_.last_render_had_glitch_;
}
bool is_using_cadence() const {
return algorithm_.cadence_estimator_.has_cadence();
}
bool IsCadenceBelowOne() const {
if (!is_using_cadence())
return false;
size_t size = algorithm_.cadence_estimator_.cadence_size_for_testing();
for (size_t i = 0; i < size; ++i) {
if (!algorithm_.cadence_estimator_.GetCadenceForFrame(i))
return true;
}
return false;
}
double CadenceValue() const {
int num_render_intervals = 0;
size_t size = algorithm_.cadence_estimator_.cadence_size_for_testing();
for (size_t i = 0; i < size; ++i) {
num_render_intervals +=
algorithm_.cadence_estimator_.GetCadenceForFrame(i);
}
return (num_render_intervals + 0.0) / size;
}
size_t frames_queued() const { return algorithm_.frame_queue_.size(); }
std::string GetCadence(double frame_rate, double display_rate) {
TickGenerator display_tg(tick_clock_->NowTicks(), display_rate);
TickGenerator frame_tg(base::TimeTicks(), frame_rate);
time_source_.StartTicking();
// Enqueue enough frames for cadence detection.
size_t frames_dropped = 0;
disable_cadence_hysteresis();
algorithm_.EnqueueFrame(CreateFrame(frame_tg.interval(0)));
algorithm_.EnqueueFrame(CreateFrame(frame_tg.interval(1)));
EXPECT_TRUE(RenderAndStep(&display_tg, &frames_dropped));
// Store cadence before reseting the algorithm.
const std::string cadence =
algorithm_.cadence_estimator_.GetCadenceForTesting();
time_source_.StopTicking();
algorithm_.Reset();
return cadence;
}
base::TimeDelta CalculateAbsoluteDriftForFrame(base::TimeTicks deadline_min,
int frame_index) {
return algorithm_.CalculateAbsoluteDriftForFrame(deadline_min, frame_index);
}
bool DriftOfLastRenderWasWithinTolerance(base::TimeTicks deadline_min) {
return CalculateAbsoluteDriftForFrame(deadline_min, 0) <=
algorithm_.max_acceptable_drift_;
}
scoped_refptr<VideoFrame> RenderAndStep(TickGenerator* tg,
size_t* frames_dropped) {
const base::TimeTicks start = tg->current();
const base::TimeTicks end = tg->step();
return algorithm_.Render(start, end, frames_dropped);
}
// Allows tests to run a Render() loop with sufficient frames for the various
// rendering modes. Upon each Render() |render_test_func| will be called with
// the rendered frame and the number of frames dropped.
template <typename OnRenderCallback>
void RunFramePumpTest(bool reset,
TickGenerator* frame_tg,
TickGenerator* display_tg,
OnRenderCallback render_test_func) {
SCOPED_TRACE(base::StringPrintf("Rendering %.03f fps into %0.03f",
frame_tg->hertz(), display_tg->hertz()));
tick_clock_->Advance(display_tg->current() - tick_clock_->NowTicks());
time_source_.StartTicking();
const bool fresh_algorithm = !algorithm_.have_rendered_frames_;
base::TimeDelta last_start_timestamp = kNoTimestamp();
bool should_use_cadence = false;
int glitch_count = 0;
const base::TimeTicks start_time = tick_clock_->NowTicks();
while (tick_clock_->NowTicks() - start_time < minimum_glitch_time()) {
while (algorithm_.EffectiveFramesQueued() < 3 ||
frame_tg->current() - time_source_.CurrentMediaTime() <
base::TimeTicks()) {
algorithm_.EnqueueFrame(
CreateFrame(frame_tg->current() - base::TimeTicks()));
frame_tg->step();
}
size_t frames_dropped = 0;
const base::TimeTicks deadline_min = display_tg->current();
const base::TimeTicks deadline_max = display_tg->step();
scoped_refptr<VideoFrame> frame =
algorithm_.Render(deadline_min, deadline_max, &frames_dropped);
render_test_func(frame, frames_dropped);
tick_clock_->Advance(display_tg->current() - tick_clock_->NowTicks());
if (HasFatalFailure())
return;
// Render() should always return a frame within drift tolerances.
ASSERT_TRUE(DriftOfLastRenderWasWithinTolerance(deadline_min));
// If we have a frame, the timestamps should always be monotonically
// increasing.
if (frame) {
if (last_start_timestamp != kNoTimestamp())
ASSERT_LE(last_start_timestamp, frame->timestamp());
else
last_start_timestamp = frame->timestamp();
}
// Only verify certain properties for fresh instances.
if (fresh_algorithm) {
ASSERT_NEAR(frame_tg->interval(1).InMicroseconds(),
algorithm_.average_frame_duration().InMicroseconds(), 1);
if (is_using_cadence() && last_render_had_glitch())
++glitch_count;
// Once cadence starts, it should never stop for the current set of
// tests.
if (is_using_cadence())
should_use_cadence = true;
ASSERT_EQ(is_using_cadence(), should_use_cadence);
}
// When there are no frames, we're not using cadence based selection, or a
// frame is under cadence the two queue size reports should be equal to
// the number of usable frames; i.e. those frames whose end time was not
// within the last render interval.
if (!is_using_cadence() || !frames_queued() ||
GetCurrentFrameDisplayCount() < GetCurrentFrameIdealDisplayCount()) {
ASSERT_NEAR(GetUsableFrameCount(deadline_max),
algorithm_.EffectiveFramesQueued(),
fresh_algorithm ? 0 : 1);
} else if (is_using_cadence() && !IsCadenceBelowOne()) {
// If there was no glitch in the last render, the two queue sizes should
// be off by exactly one frame; i.e., the current frame doesn't count.
if (!last_render_had_glitch() && fresh_algorithm)
ASSERT_EQ(frames_queued() - 1, algorithm_.EffectiveFramesQueued());
} else if (IsCadenceBelowOne()) {
// The frame estimate should be off by at most one frame.
const size_t estimated_frames_queued =
std::floor(frames_queued() * CadenceValue());
ASSERT_NEAR(algorithm_.EffectiveFramesQueued(), estimated_frames_queued,
1);
}
}
// When using cadence, the glitch count should be at most one for when
// rendering for the less than minimum_glitch_time().
if (fresh_algorithm && is_using_cadence())
ASSERT_LE(glitch_count, 1);
time_source_.StopTicking();
if (reset) {
algorithm_.Reset();
time_source_.SetMediaTime(base::TimeDelta());
}
}
int FindBestFrameByCoverage(base::TimeTicks deadline_min,
base::TimeTicks deadline_max,
int* second_best) {
return algorithm_.FindBestFrameByCoverage(deadline_min, deadline_max,
second_best);
}
int FindBestFrameByDrift(base::TimeTicks deadline_min,
base::TimeDelta* selected_frame_drift) {
return algorithm_.FindBestFrameByDrift(deadline_min, selected_frame_drift);
}
int GetCurrentFrameDropCount() const {
DCHECK_GT(frames_queued(), 0u);
return algorithm_.frame_queue_[algorithm_.last_frame_index_].drop_count;
}
int GetCurrentFrameDisplayCount() const {
DCHECK_GT(frames_queued(), 0u);
return algorithm_.frame_queue_[algorithm_.last_frame_index_].render_count;
}
int GetCurrentFrameIdealDisplayCount() const {
DCHECK_GT(frames_queued(), 0u);
return algorithm_.frame_queue_[algorithm_.last_frame_index_]
.ideal_render_count;
}
int AccountForMissedIntervalsAndStep(TickGenerator* tg) {
const base::TimeTicks start = tg->current();
const base::TimeTicks end = tg->step();
return AccountForMissedIntervals(start, end);
}
int AccountForMissedIntervals(base::TimeTicks deadline_min,
base::TimeTicks deadline_max) {
algorithm_.AccountForMissedIntervals(deadline_min, deadline_max);
return frames_queued() ? GetCurrentFrameDisplayCount() : -1;
}
size_t GetUsableFrameCount(base::TimeTicks deadline_max) {
if (is_using_cadence())
return frames_queued();
for (size_t i = 0; i < frames_queued(); ++i)
if (algorithm_.frame_queue_[i].end_time > deadline_max)
return frames_queued() - i;
return 0;
}
protected:
VideoFramePool frame_pool_;
scoped_ptr<base::SimpleTestTickClock> tick_clock_;
WallClockTimeSource time_source_;
VideoRendererAlgorithm algorithm_;
private:
DISALLOW_COPY_AND_ASSIGN(VideoRendererAlgorithmTest);
};
TEST_F(VideoRendererAlgorithmTest, Empty) {
TickGenerator tg(tick_clock_->NowTicks(), 50);
size_t frames_dropped = 0;
EXPECT_EQ(0u, frames_queued());
EXPECT_FALSE(RenderAndStep(&tg, &frames_dropped));
EXPECT_EQ(0u, frames_dropped);
EXPECT_EQ(0u, frames_queued());
EXPECT_NE(base::TimeDelta(), max_acceptable_drift());
}
TEST_F(VideoRendererAlgorithmTest, Reset) {
TickGenerator tg(tick_clock_->NowTicks(), 50);
algorithm_.EnqueueFrame(CreateFrame(tg.interval(0)));
EXPECT_EQ(1u, frames_queued());
EXPECT_NE(base::TimeDelta(), max_acceptable_drift());
algorithm_.Reset();
EXPECT_EQ(0u, frames_queued());
EXPECT_NE(base::TimeDelta(), max_acceptable_drift());
}
TEST_F(VideoRendererAlgorithmTest, AccountForMissingIntervals) {
TickGenerator tg(tick_clock_->NowTicks(), 50);
time_source_.StartTicking();
// Disable hysteresis since AccountForMissingIntervals() only affects cadence
// based rendering.
disable_cadence_hysteresis();
// Simulate Render() called before any frames are present.
EXPECT_EQ(-1, AccountForMissedIntervalsAndStep(&tg));
algorithm_.EnqueueFrame(CreateFrame(tg.interval(0)));
algorithm_.EnqueueFrame(CreateFrame(tg.interval(1)));
algorithm_.EnqueueFrame(CreateFrame(tg.interval(2)));
algorithm_.EnqueueFrame(CreateFrame(tg.interval(3)));
// Simulate Render() called before any frames have been rendered.
EXPECT_EQ(0, AccountForMissedIntervalsAndStep(&tg));
// Render one frame (several are in the past and will be dropped).
base::TimeTicks deadline_min = tg.current();
base::TimeTicks deadline_max = tg.step();
size_t frames_dropped = 0;
scoped_refptr<VideoFrame> frame =
algorithm_.Render(deadline_min, deadline_max, &frames_dropped);
ASSERT_TRUE(frame);
EXPECT_EQ(tg.interval(2), frame->timestamp());
EXPECT_EQ(2u, frames_dropped);
ASSERT_EQ(1, GetCurrentFrameDisplayCount());
// Now calling AccountForMissingIntervals with an interval which overlaps the
// previous should do nothing.
deadline_min += tg.interval(1) / 2;
deadline_max += tg.interval(1) / 2;
EXPECT_EQ(1, AccountForMissedIntervals(deadline_min, deadline_max));
// Steping by 1.5 intervals, is not enough to increase the count.
deadline_min += tg.interval(1);
deadline_max += tg.interval(1);
EXPECT_EQ(1, AccountForMissedIntervals(deadline_min, deadline_max));
// Calling it after a full skipped interval should increase the count by 1 for
// each skipped interval.
tg.step();
EXPECT_EQ(2, AccountForMissedIntervalsAndStep(&tg));
// 4 because [tg.current(), tg.step()] now represents 2 additional intervals.
EXPECT_EQ(4, AccountForMissedIntervalsAndStep(&tg));
// Frame should be way over cadence and no good frames remain, so last frame
// should be returned.
frame = RenderAndStep(&tg, &frames_dropped);
ASSERT_TRUE(frame);
EXPECT_EQ(tg.interval(3), frame->timestamp());
EXPECT_EQ(0u, frames_dropped);
EXPECT_EQ(1, GetCurrentFrameDisplayCount());
// Stop the time source and verify AccountForMissedIntervals() doesn't try to
// account for intervals from pause behavior.
time_source_.StopTicking();
frame = RenderAndStep(&tg, &frames_dropped);
ASSERT_TRUE(frame);
EXPECT_EQ(tg.interval(3), frame->timestamp());
EXPECT_EQ(0u, frames_dropped);
EXPECT_EQ(1, GetCurrentFrameDisplayCount());
tg.step(100);
frame = RenderAndStep(&tg, &frames_dropped);
ASSERT_TRUE(frame);
EXPECT_EQ(tg.interval(3), frame->timestamp());
EXPECT_EQ(0u, frames_dropped);
EXPECT_EQ(1, GetCurrentFrameDisplayCount());
time_source_.StartTicking();
// Now run the same test using set_time_stopped();
frame = RenderAndStep(&tg, &frames_dropped);
ASSERT_TRUE(frame);
EXPECT_EQ(tg.interval(3), frame->timestamp());
EXPECT_EQ(0u, frames_dropped);
EXPECT_EQ(2, GetCurrentFrameDisplayCount());
algorithm_.set_time_stopped();
tg.step(100);
frame = RenderAndStep(&tg, &frames_dropped);
ASSERT_TRUE(frame);
EXPECT_EQ(tg.interval(3), frame->timestamp());
EXPECT_EQ(0u, frames_dropped);
EXPECT_EQ(3, GetCurrentFrameDisplayCount());
}
TEST_F(VideoRendererAlgorithmTest, OnLastFrameDropped) {
TickGenerator frame_tg(base::TimeTicks(), 25);
TickGenerator display_tg(tick_clock_->NowTicks(), 50);
time_source_.StartTicking();
// Disable hysteresis since OnLastFrameDropped() only affects cadence based
// rendering.
disable_cadence_hysteresis();
algorithm_.EnqueueFrame(CreateFrame(frame_tg.interval(0)));
algorithm_.EnqueueFrame(CreateFrame(frame_tg.interval(1)));
algorithm_.EnqueueFrame(CreateFrame(frame_tg.interval(2)));
// Render one frame (several are in the past and will be dropped).
size_t frames_dropped = 0;
scoped_refptr<VideoFrame> frame = RenderAndStep(&display_tg, &frames_dropped);
ASSERT_TRUE(frame);
EXPECT_EQ(frame_tg.interval(0), frame->timestamp());
EXPECT_EQ(0u, frames_dropped);
// The frame should have its display count decremented once it's reported as
// dropped.
ASSERT_EQ(1, GetCurrentFrameDisplayCount());
ASSERT_EQ(0, GetCurrentFrameDropCount());
algorithm_.OnLastFrameDropped();
ASSERT_EQ(1, GetCurrentFrameDisplayCount());
ASSERT_EQ(1, GetCurrentFrameDropCount());
// Render the frame again and then force another drop.
frame = RenderAndStep(&display_tg, &frames_dropped);
ASSERT_TRUE(frame);
EXPECT_EQ(frame_tg.interval(0), frame->timestamp());
EXPECT_EQ(0u, frames_dropped);
ASSERT_EQ(2, GetCurrentFrameDisplayCount());
ASSERT_EQ(1, GetCurrentFrameDropCount());
algorithm_.OnLastFrameDropped();
ASSERT_EQ(2, GetCurrentFrameDisplayCount());
ASSERT_EQ(2, GetCurrentFrameDropCount());
// The next Render() call should now count this frame as dropped.
frame = RenderAndStep(&display_tg, &frames_dropped);
ASSERT_TRUE(frame);
EXPECT_EQ(frame_tg.interval(1), frame->timestamp());
EXPECT_EQ(1u, frames_dropped);
ASSERT_EQ(1, GetCurrentFrameDisplayCount());
ASSERT_EQ(0, GetCurrentFrameDropCount());
// Rendering again should result in the same frame being displayed.
frame = RenderAndStep(&display_tg, &frames_dropped);
ASSERT_TRUE(frame);
EXPECT_EQ(frame_tg.interval(1), frame->timestamp());
EXPECT_EQ(0u, frames_dropped);
// In this case, the drop count is less than the display count, so the frame
// should not be counted as dropped.
ASSERT_EQ(2, GetCurrentFrameDisplayCount());
ASSERT_EQ(0, GetCurrentFrameDropCount());
algorithm_.OnLastFrameDropped();
ASSERT_EQ(2, GetCurrentFrameDisplayCount());
ASSERT_EQ(1, GetCurrentFrameDropCount());
// The third frame should be rendered correctly now and the previous frame not
// counted as having been dropped.
frame = RenderAndStep(&display_tg, &frames_dropped);
ASSERT_TRUE(frame);
EXPECT_EQ(frame_tg.interval(2), frame->timestamp());
EXPECT_EQ(0u, frames_dropped);
}
TEST_F(VideoRendererAlgorithmTest, EffectiveFramesQueued) {
TickGenerator frame_tg(base::TimeTicks(), 50);
TickGenerator display_tg(tick_clock_->NowTicks(), 25);
// Disable hysteresis since EffectiveFramesQueued() is tested as part of the
// normal frame pump tests when cadence is not present.
disable_cadence_hysteresis();
EXPECT_EQ(0u, algorithm_.EffectiveFramesQueued());
time_source_.StartTicking();
algorithm_.EnqueueFrame(CreateFrame(frame_tg.interval(0)));
EXPECT_EQ(1u, algorithm_.EffectiveFramesQueued());
algorithm_.EnqueueFrame(CreateFrame(frame_tg.interval(1)));
EXPECT_EQ(2u, algorithm_.EffectiveFramesQueued());
algorithm_.EnqueueFrame(CreateFrame(frame_tg.interval(2)));
EXPECT_EQ(3u, algorithm_.EffectiveFramesQueued());
algorithm_.EnqueueFrame(CreateFrame(frame_tg.interval(3)));
EXPECT_EQ(4u, algorithm_.EffectiveFramesQueued());
EXPECT_EQ(4u, frames_queued());
// Render one frame which will detect cadence...
size_t frames_dropped = 0;
scoped_refptr<VideoFrame> frame = RenderAndStep(&display_tg, &frames_dropped);
ASSERT_TRUE(frame);
EXPECT_EQ(frame_tg.interval(0), frame->timestamp());
EXPECT_EQ(0u, frames_dropped);
// Fractional cadence should be detected and the count will decrease.
ASSERT_TRUE(is_using_cadence());
EXPECT_EQ(1u, algorithm_.EffectiveFramesQueued());
EXPECT_EQ(4u, frames_queued());
// Dropping the last rendered frame should do nothing, since the last frame
// is already excluded from the count if it has a display count of 1.
algorithm_.OnLastFrameDropped();
EXPECT_EQ(1u, algorithm_.EffectiveFramesQueued());
}
TEST_F(VideoRendererAlgorithmTest, EffectiveFramesQueuedWithoutCadence) {
TickGenerator tg(tick_clock_->NowTicks(), 60);
EXPECT_EQ(0u, algorithm_.EffectiveFramesQueued());
time_source_.StartTicking();
algorithm_.EnqueueFrame(CreateFrame(tg.interval(0)));
EXPECT_EQ(1u, algorithm_.EffectiveFramesQueued());
algorithm_.EnqueueFrame(CreateFrame(tg.interval(1)));
EXPECT_EQ(2u, algorithm_.EffectiveFramesQueued());
algorithm_.EnqueueFrame(CreateFrame(tg.interval(2)));
EXPECT_EQ(3u, algorithm_.EffectiveFramesQueued());
algorithm_.EnqueueFrame(CreateFrame(tg.interval(3)));
EXPECT_EQ(4u, algorithm_.EffectiveFramesQueued());
EXPECT_EQ(4u, frames_queued());
// Issue a render call that should drop the first two frames and mark the 3rd
// as consumed.
tg.step(2);
size_t frames_dropped = 0;
scoped_refptr<VideoFrame> frame = RenderAndStep(&tg, &frames_dropped);
ASSERT_FALSE(is_using_cadence());
ASSERT_TRUE(frame);
EXPECT_EQ(2u, frames_dropped);
EXPECT_EQ(tg.interval(2), frame->timestamp());
EXPECT_EQ(1u, algorithm_.EffectiveFramesQueued());
EXPECT_EQ(2u, frames_queued());
// Rendering one more frame should return 0 effective frames queued.
frame = RenderAndStep(&tg, &frames_dropped);
ASSERT_FALSE(is_using_cadence());
ASSERT_TRUE(frame);
EXPECT_EQ(0u, frames_dropped);
EXPECT_EQ(tg.interval(3), frame->timestamp());
EXPECT_EQ(0u, algorithm_.EffectiveFramesQueued());
EXPECT_EQ(1u, frames_queued());
}
// The maximum acceptable drift should be updated once we have two frames.
TEST_F(VideoRendererAlgorithmTest, AcceptableDriftUpdated) {
TickGenerator tg(tick_clock_->NowTicks(), 50);
size_t frames_dropped = 0;
const base::TimeDelta original_drift = max_acceptable_drift();
algorithm_.EnqueueFrame(CreateFrame(tg.interval(0)));
EXPECT_EQ(1u, frames_queued());
EXPECT_TRUE(RenderAndStep(&tg, &frames_dropped));
EXPECT_EQ(original_drift, max_acceptable_drift());
// Time must be ticking to get wall clock times for frames.
time_source_.StartTicking();
algorithm_.EnqueueFrame(CreateFrame(tg.interval(1)));
EXPECT_EQ(2u, frames_queued());
EXPECT_TRUE(RenderAndStep(&tg, &frames_dropped));
EXPECT_NE(original_drift, max_acceptable_drift());
}
// Verifies behavior when time stops.
TEST_F(VideoRendererAlgorithmTest, TimeIsStopped) {
TickGenerator tg(tick_clock_->NowTicks(), 50);
// Prior to rendering the first frame, the algorithm should always return the
// first available frame.
size_t frames_dropped = 0;
algorithm_.EnqueueFrame(CreateFrame(tg.interval(0)));
EXPECT_EQ(1u, frames_queued());
scoped_refptr<VideoFrame> frame = RenderAndStep(&tg, &frames_dropped);
ASSERT_TRUE(frame);
EXPECT_EQ(tg.interval(0), frame->timestamp());
EXPECT_EQ(0u, frames_dropped);
EXPECT_EQ(1u, frames_queued());
EXPECT_EQ(1u, algorithm_.EffectiveFramesQueued());
// The same timestamp should be returned after time starts.
tick_clock_->Advance(tg.interval(1));
time_source_.StartTicking();
frame = RenderAndStep(&tg, &frames_dropped);
ASSERT_TRUE(frame);
EXPECT_EQ(tg.interval(0), frame->timestamp());
EXPECT_EQ(0u, frames_dropped);
EXPECT_EQ(1u, frames_queued());
EXPECT_EQ(1u, algorithm_.EffectiveFramesQueued());
// Ensure the next suitable frame is vended as time advances.
algorithm_.EnqueueFrame(CreateFrame(tg.interval(1)));
EXPECT_EQ(2u, frames_queued());
EXPECT_EQ(2u, algorithm_.EffectiveFramesQueued());
frame = RenderAndStep(&tg, &frames_dropped);
ASSERT_TRUE(frame);
EXPECT_EQ(tg.interval(1), frame->timestamp());
EXPECT_EQ(0u, frames_dropped);
EXPECT_EQ(1u, frames_queued());
EXPECT_EQ(0u, algorithm_.EffectiveFramesQueued());
// Once time stops ticking, any further frames shouldn't be returned, even if
// the interval requested more closely matches.
algorithm_.EnqueueFrame(CreateFrame(tg.interval(2)));
time_source_.StopTicking();
frame = RenderAndStep(&tg, &frames_dropped);
ASSERT_TRUE(frame);
EXPECT_EQ(tg.interval(1), frame->timestamp());
EXPECT_EQ(0u, frames_dropped);
EXPECT_EQ(2u, frames_queued());
EXPECT_EQ(1u, algorithm_.EffectiveFramesQueued());
}
// Verify frames inserted out of order end up in the right spot and are rendered
// according to the API contract.
TEST_F(VideoRendererAlgorithmTest, SortedFrameQueue) {
TickGenerator tg(tick_clock_->NowTicks(), 50);
// Ensure frames handed in out of order before time starts ticking are sorted
// and returned in the correct order upon Render().
algorithm_.EnqueueFrame(CreateFrame(tg.interval(3)));
algorithm_.EnqueueFrame(CreateFrame(tg.interval(2)));
EXPECT_EQ(2u, frames_queued());
EXPECT_EQ(2u, algorithm_.EffectiveFramesQueued());
time_source_.StartTicking();
// The first call should return the earliest frame appended.
size_t frames_dropped = 0;
scoped_refptr<VideoFrame> frame = RenderAndStep(&tg, &frames_dropped);
EXPECT_EQ(0u, frames_dropped);
EXPECT_EQ(tg.interval(2), frame->timestamp());
EXPECT_EQ(2u, frames_queued());
EXPECT_EQ(2u, algorithm_.EffectiveFramesQueued());
// Since a frame has already been rendered, queuing this frame and calling
// Render() should result in it being dropped; even though it's a better
// candidate for the desired interval. The frame is dropped during enqueue so
// it won't show up in frames_queued().
algorithm_.EnqueueFrame(CreateFrame(tg.interval(1)));
EXPECT_EQ(2u, frames_queued());
EXPECT_EQ(2u, algorithm_.EffectiveFramesQueued());
frame = RenderAndStep(&tg, &frames_dropped);
EXPECT_EQ(1u, frames_dropped);
EXPECT_EQ(tg.interval(2), frame->timestamp());
EXPECT_EQ(2u, frames_queued());
EXPECT_EQ(2u, algorithm_.EffectiveFramesQueued());
}
// Run through integer cadence selection for 1, 2, 3, and 4.
TEST_F(VideoRendererAlgorithmTest, BestFrameByCadence) {
const double kTestRates[][2] = {{60, 60}, {30, 60}, {25, 75}, {25, 100}};
for (const auto& test_rate : kTestRates) {
disable_cadence_hysteresis();
TickGenerator frame_tg(base::TimeTicks(), test_rate[0]);
TickGenerator display_tg(tick_clock_->NowTicks(), test_rate[1]);
int actual_frame_pattern = 0;
const int desired_frame_pattern = test_rate[1] / test_rate[0];
scoped_refptr<VideoFrame> current_frame;
RunFramePumpTest(
true, &frame_tg, &display_tg,
[¤t_frame, &actual_frame_pattern, desired_frame_pattern, this](
const scoped_refptr<VideoFrame>& frame, size_t frames_dropped) {
ASSERT_TRUE(frame);
ASSERT_EQ(0u, frames_dropped);
// Each frame should display for exactly it's desired cadence pattern.
if (!current_frame || current_frame == frame) {
actual_frame_pattern++;
} else {
ASSERT_EQ(actual_frame_pattern, desired_frame_pattern);
actual_frame_pattern = 1;
}
current_frame = frame;
ASSERT_TRUE(is_using_cadence());
});
if (HasFatalFailure())
return;
}
}
TEST_F(VideoRendererAlgorithmTest, BestFrameByCadenceOverdisplayed) {
TickGenerator frame_tg(base::TimeTicks(), 25);
TickGenerator display_tg(tick_clock_->NowTicks(), 50);
time_source_.StartTicking();
disable_cadence_hysteresis();
algorithm_.EnqueueFrame(CreateFrame(frame_tg.interval(0)));
algorithm_.EnqueueFrame(CreateFrame(frame_tg.interval(1)));
// Render frames until we've exhausted available frames and the last frame is
// forced to be over displayed.
for (int i = 0; i < 5; ++i) {
size_t frames_dropped = 0;
scoped_refptr<VideoFrame> frame =
RenderAndStep(&display_tg, &frames_dropped);
ASSERT_TRUE(frame);
EXPECT_EQ(frame_tg.interval(i < 4 ? i / 2 : 1), frame->timestamp());
EXPECT_EQ(0u, frames_dropped);
ASSERT_EQ(2, GetCurrentFrameIdealDisplayCount());
}
// Verify last frame is above cadence (2 in this case)
ASSERT_EQ(GetCurrentFrameIdealDisplayCount() + 1,
GetCurrentFrameDisplayCount());
algorithm_.EnqueueFrame(CreateFrame(frame_tg.interval(2)));
algorithm_.EnqueueFrame(CreateFrame(frame_tg.interval(3)));
// The next frame should only be displayed once, since the previous one was
// over displayed by one frame.
size_t frames_dropped = 0;
scoped_refptr<VideoFrame> frame = RenderAndStep(&display_tg, &frames_dropped);
ASSERT_TRUE(frame);
EXPECT_EQ(frame_tg.interval(2), frame->timestamp());
EXPECT_EQ(0u, frames_dropped);
// Enqueuing a new frame should keep the correct cadence values.
algorithm_.EnqueueFrame(CreateFrame(frame_tg.interval(4)));
ASSERT_EQ(2, GetCurrentFrameDisplayCount());
ASSERT_EQ(1, GetCurrentFrameDropCount());
ASSERT_EQ(2, GetCurrentFrameIdealDisplayCount());
frame = RenderAndStep(&display_tg, &frames_dropped);
ASSERT_TRUE(frame);
EXPECT_EQ(frame_tg.interval(3), frame->timestamp());
EXPECT_EQ(0u, frames_dropped);
ASSERT_EQ(1, GetCurrentFrameDisplayCount());
ASSERT_EQ(0, GetCurrentFrameDropCount());
ASSERT_EQ(2, GetCurrentFrameIdealDisplayCount());
}
TEST_F(VideoRendererAlgorithmTest, BestFrameByCadenceOverdisplayedForDrift) {
// Use 24.94 to ensure drift expires pretty rapidly (8.36s in this case).
TickGenerator frame_tg(base::TimeTicks(), 24.94);
TickGenerator display_tg(tick_clock_->NowTicks(), 50);
time_source_.StartTicking();
disable_cadence_hysteresis();
scoped_refptr<VideoFrame> last_frame;
bool have_overdisplayed_frame = false;
while (!have_overdisplayed_frame) {
while (algorithm_.EffectiveFramesQueued() < 2) {
algorithm_.EnqueueFrame(
CreateFrame(frame_tg.current() - base::TimeTicks()));
frame_tg.step();
}
size_t frames_dropped = 0;
last_frame = RenderAndStep(&display_tg, &frames_dropped);
ASSERT_TRUE(last_frame);
ASSERT_TRUE(is_using_cadence());
ASSERT_EQ(0u, frames_dropped);
ASSERT_EQ(2, GetCurrentFrameIdealDisplayCount());
have_overdisplayed_frame = GetCurrentFrameDisplayCount() > 2;
}
ASSERT_TRUE(last_render_had_glitch());
// We've reached the point where the current frame is over displayed due to
// drift, the next frame should resume cadence without accounting for the
// overdisplayed frame.
size_t frames_dropped = 0;
scoped_refptr<VideoFrame> next_frame =
RenderAndStep(&display_tg, &frames_dropped);
ASSERT_EQ(0u, frames_dropped);
ASSERT_NE(last_frame, next_frame);
ASSERT_TRUE(is_using_cadence());
ASSERT_EQ(2, GetCurrentFrameIdealDisplayCount());
ASSERT_EQ(1, GetCurrentFrameDisplayCount());
last_frame = next_frame;
next_frame = RenderAndStep(&display_tg, &frames_dropped);
ASSERT_EQ(0u, frames_dropped);
ASSERT_EQ(last_frame, next_frame);
ASSERT_TRUE(is_using_cadence());
ASSERT_EQ(2, GetCurrentFrameIdealDisplayCount());
ASSERT_EQ(2, GetCurrentFrameDisplayCount());
}
TEST_F(VideoRendererAlgorithmTest, BestFrameByCoverage) {
TickGenerator tg(tick_clock_->NowTicks(), 50);
time_source_.StartTicking();
algorithm_.EnqueueFrame(CreateFrame(tg.interval(0)));
algorithm_.EnqueueFrame(CreateFrame(tg.interval(1)));
algorithm_.EnqueueFrame(CreateFrame(tg.interval(2)));
base::TimeTicks deadline_min = tg.current();
base::TimeTicks deadline_max = deadline_min + tg.interval(1);
size_t frames_dropped = 0;
scoped_refptr<VideoFrame> frame =
algorithm_.Render(deadline_min, deadline_max, &frames_dropped);
ASSERT_TRUE(frame);
EXPECT_EQ(tg.interval(0), frame->timestamp());
EXPECT_EQ(0u, frames_dropped);
int second_best = 0;
// Coverage should be 1 for if the frame overlaps the interval entirely, no
// second best should be found.
EXPECT_EQ(0,
FindBestFrameByCoverage(deadline_min, deadline_max, &second_best));
EXPECT_EQ(-1, second_best);
// 49/51 coverage for frame 0 and frame 1 should be within tolerance such that
// the earlier frame should still be chosen.
deadline_min = tg.current() + tg.interval(1) / 2 +
base::TimeDelta::FromMicroseconds(250);
deadline_max = deadline_min + tg.interval(1);
EXPECT_EQ(0,
FindBestFrameByCoverage(deadline_min, deadline_max, &second_best));
EXPECT_EQ(1, second_best);
// 48/52 coverage should result in the second frame being chosen.
deadline_min = tg.current() + tg.interval(1) / 2 +
base::TimeDelta::FromMicroseconds(500);
deadline_max = deadline_min + tg.interval(1);
EXPECT_EQ(1,
FindBestFrameByCoverage(deadline_min, deadline_max, &second_best));
EXPECT_EQ(0, second_best);
// Overlapping three frames should choose the one with the most coverage and
// the second best should be the earliest frame.
deadline_min = tg.current() + tg.interval(1) / 2;
deadline_max = deadline_min + tg.interval(2);
EXPECT_EQ(1,
FindBestFrameByCoverage(deadline_min, deadline_max, &second_best));
EXPECT_EQ(0, second_best);
// Requesting coverage outside of all known frames should return -1 for both
// best indices.
deadline_min = tg.current() + tg.interval(frames_queued());
deadline_max = deadline_min + tg.interval(1);
EXPECT_EQ(-1,
FindBestFrameByCoverage(deadline_min, deadline_max, &second_best));
EXPECT_EQ(-1, second_best);
}
TEST_F(VideoRendererAlgorithmTest, BestFrameByDriftAndDriftCalculations) {
TickGenerator tg(tick_clock_->NowTicks(), 50);
time_source_.StartTicking();
algorithm_.EnqueueFrame(CreateFrame(tg.interval(0)));
algorithm_.EnqueueFrame(CreateFrame(tg.interval(1)));
size_t frames_dropped = 0;
scoped_refptr<VideoFrame> frame = algorithm_.Render(
tg.current(), tg.current() + tg.interval(1), &frames_dropped);
ASSERT_TRUE(frame);
EXPECT_EQ(tg.interval(0), frame->timestamp());
EXPECT_EQ(0u, frames_dropped);
base::TimeDelta zero_drift, half_drift = tg.interval(1) / 2;
base::TimeDelta detected_drift;
// Frame_0 overlaps the deadline, Frame_1 is a full interval away.
base::TimeTicks deadline = tg.current();
EXPECT_EQ(zero_drift, CalculateAbsoluteDriftForFrame(deadline, 0));
EXPECT_EQ(tg.interval(1), CalculateAbsoluteDriftForFrame(deadline, 1));
EXPECT_EQ(0, FindBestFrameByDrift(deadline, &detected_drift));
EXPECT_EQ(zero_drift, detected_drift);
// Frame_0 overlaps the deadline, Frame_1 is a half interval away.
deadline += half_drift;
EXPECT_EQ(zero_drift, CalculateAbsoluteDriftForFrame(deadline, 0));
EXPECT_EQ(half_drift, CalculateAbsoluteDriftForFrame(deadline, 1));
EXPECT_EQ(0, FindBestFrameByDrift(deadline, &detected_drift));
EXPECT_EQ(zero_drift, detected_drift);
// Both frames overlap the deadline.
deadline += half_drift;
EXPECT_EQ(zero_drift, CalculateAbsoluteDriftForFrame(deadline, 0));
EXPECT_EQ(zero_drift, CalculateAbsoluteDriftForFrame(deadline, 1));
EXPECT_EQ(1, FindBestFrameByDrift(deadline, &detected_drift));
EXPECT_EQ(zero_drift, detected_drift);
// Frame_0 is half an interval away, Frame_1 overlaps the deadline.
deadline += half_drift;
EXPECT_EQ(half_drift, CalculateAbsoluteDriftForFrame(deadline, 0));
EXPECT_EQ(zero_drift, CalculateAbsoluteDriftForFrame(deadline, 1));
EXPECT_EQ(1, FindBestFrameByDrift(deadline, &detected_drift));
EXPECT_EQ(zero_drift, detected_drift);
// Frame_0 is a full interval away, Frame_1 overlaps the deadline.
deadline += half_drift;
EXPECT_EQ(tg.interval(1), CalculateAbsoluteDriftForFrame(deadline, 0));
EXPECT_EQ(zero_drift, CalculateAbsoluteDriftForFrame(deadline, 1));
EXPECT_EQ(1, FindBestFrameByDrift(deadline, &detected_drift));
EXPECT_EQ(zero_drift, detected_drift);
// Both frames are entirely before the deadline.
deadline += half_drift;
EXPECT_EQ(tg.interval(1) + half_drift,
CalculateAbsoluteDriftForFrame(deadline, 0));
EXPECT_EQ(half_drift, CalculateAbsoluteDriftForFrame(deadline, 1));
EXPECT_EQ(1, FindBestFrameByDrift(deadline, &detected_drift));
EXPECT_EQ(half_drift, detected_drift);
}
// Run through fractional cadence selection for 1/2, 1/3, and 1/4.
TEST_F(VideoRendererAlgorithmTest, BestFrameByFractionalCadence) {
const double kTestRates[][2] = {{120, 60}, {72, 24}, {100, 25}};
for (const auto& test_rate : kTestRates) {
disable_cadence_hysteresis();
TickGenerator frame_tg(base::TimeTicks(), test_rate[0]);
TickGenerator display_tg(tick_clock_->NowTicks(), test_rate[1]);
const size_t desired_drop_pattern = test_rate[0] / test_rate[1] - 1;
scoped_refptr<VideoFrame> current_frame;
RunFramePumpTest(
true, &frame_tg, &display_tg,
[¤t_frame, desired_drop_pattern, this](
const scoped_refptr<VideoFrame>& frame, size_t frames_dropped) {
ASSERT_TRUE(frame);
// The first frame should have zero dropped frames, but each Render()
// call after should drop the same number of frames based on the
// fractional cadence.
if (!current_frame)
ASSERT_EQ(0u, frames_dropped);
else
ASSERT_EQ(desired_drop_pattern, frames_dropped);
ASSERT_NE(current_frame, frame);
ASSERT_TRUE(is_using_cadence());
current_frame = frame;
});
if (HasFatalFailure())
return;
}
}
// Verify a 3:2 frame pattern for 23.974fps and 24fps in 60Hz.
TEST_F(VideoRendererAlgorithmTest, FilmCadence) {
const double kTestRates[] = {NTSC(24), 24};
disable_cadence_hysteresis();
for (double frame_rate : kTestRates) {
scoped_refptr<VideoFrame> current_frame;
int actual_frame_pattern = 0, desired_frame_pattern = 3;
TickGenerator frame_tg(base::TimeTicks(), frame_rate);
TickGenerator display_tg(tick_clock_->NowTicks(), 60);
RunFramePumpTest(
true, &frame_tg, &display_tg,
[¤t_frame, &actual_frame_pattern, &desired_frame_pattern, this](
const scoped_refptr<VideoFrame>& frame, size_t frames_dropped) {
ASSERT_TRUE(frame);
ASSERT_EQ(0u, frames_dropped);
if (!current_frame || current_frame == frame) {
actual_frame_pattern++;
} else {
ASSERT_EQ(actual_frame_pattern, desired_frame_pattern);
actual_frame_pattern = 1;
desired_frame_pattern = (desired_frame_pattern == 3 ? 2 : 3);
}
current_frame = frame;
ASSERT_TRUE(is_using_cadence());
});
if (HasFatalFailure())
return;
}
}
// Spot check common display and frame rate pairs for correctness.
TEST_F(VideoRendererAlgorithmTest, CadenceCalculations) {
ASSERT_EQ("[3:2]", GetCadence(24, 60));
ASSERT_EQ("[3:2]", GetCadence(NTSC(24), 60));
ASSERT_EQ("[2:3:2:3:2]", GetCadence(25, 60));
ASSERT_EQ("[2]", GetCadence(NTSC(30), 60));
ASSERT_EQ("[2]", GetCadence(30, 60));
ASSERT_EQ("[1:1:2:1:1]", GetCadence(50, 60));
ASSERT_EQ("[1]", GetCadence(NTSC(60), 60));
ASSERT_EQ("[1:0]", GetCadence(120, 60));
// 50Hz is common in the EU.
ASSERT_EQ("[]", GetCadence(NTSC(24), 50));
ASSERT_EQ("[]", GetCadence(24, 50));
ASSERT_EQ("[2]", GetCadence(NTSC(25), 50));
ASSERT_EQ("[2]", GetCadence(25, 50));
ASSERT_EQ("[2:1:2]", GetCadence(NTSC(30), 50));
ASSERT_EQ("[2:1:2]", GetCadence(30, 50));
ASSERT_EQ("[]", GetCadence(NTSC(60), 50));
ASSERT_EQ("[]", GetCadence(60, 50));
ASSERT_EQ("[2:3:2:3:2]", GetCadence(25, NTSC(60)));
ASSERT_EQ("[1:0]", GetCadence(120, NTSC(60)));
ASSERT_EQ("[60]", GetCadence(1, NTSC(60)));
}
TEST_F(VideoRendererAlgorithmTest, RemoveExpiredFrames) {
TickGenerator tg(tick_clock_->NowTicks(), 50);
// Removing expired frames before anything is enqueued should do nothing.
ASSERT_EQ(0u, algorithm_.RemoveExpiredFrames(tg.current()));
algorithm_.EnqueueFrame(CreateFrame(tg.interval(0)));
ASSERT_EQ(0u, algorithm_.RemoveExpiredFrames(tg.current()));
EXPECT_EQ(1u, algorithm_.EffectiveFramesQueued());
time_source_.StartTicking();
size_t frames_dropped = 0;
scoped_refptr<VideoFrame> frame = RenderAndStep(&tg, &frames_dropped);
ASSERT_TRUE(frame);
EXPECT_EQ(tg.interval(0), frame->timestamp());
EXPECT_EQ(0u, frames_dropped);
algorithm_.EnqueueFrame(CreateFrame(tg.interval(1)));
algorithm_.EnqueueFrame(CreateFrame(tg.interval(2)));
algorithm_.EnqueueFrame(CreateFrame(tg.interval(3)));
algorithm_.EnqueueFrame(CreateFrame(tg.interval(4)));
EXPECT_EQ(5u, algorithm_.EffectiveFramesQueued());
tg.step(2);
ASSERT_EQ(2u, algorithm_.RemoveExpiredFrames(tg.current()));
frame = RenderAndStep(&tg, &frames_dropped);
EXPECT_EQ(1u, frames_dropped);
EXPECT_EQ(2u, frames_queued());
EXPECT_EQ(1u, algorithm_.EffectiveFramesQueued());
ASSERT_TRUE(frame);
EXPECT_EQ(tg.interval(3), frame->timestamp());
// Advance expiry enough that one frame is removed, but one remains and is
// still counted as effective.
ASSERT_EQ(
1u, algorithm_.RemoveExpiredFrames(tg.current() + tg.interval(1) * 0.9));
EXPECT_EQ(1u, frames_queued());
EXPECT_EQ(1u, algorithm_.EffectiveFramesQueued());
// Advancing expiry once more should mark the frame as ineffective.
tg.step();
ASSERT_EQ(0u, algorithm_.RemoveExpiredFrames(tg.current()));
EXPECT_EQ(1u, frames_queued());
EXPECT_EQ(0u, algorithm_.EffectiveFramesQueued());
}
TEST_F(VideoRendererAlgorithmTest, RemoveExpiredFramesCadence) {
TickGenerator tg(tick_clock_->NowTicks(), 50);
disable_cadence_hysteresis();
algorithm_.EnqueueFrame(CreateFrame(tg.interval(0)));
algorithm_.EnqueueFrame(CreateFrame(tg.interval(1)));
algorithm_.EnqueueFrame(CreateFrame(tg.interval(2)));
ASSERT_EQ(0u, algorithm_.RemoveExpiredFrames(tg.current()));
EXPECT_EQ(3u, algorithm_.EffectiveFramesQueued());
time_source_.StartTicking();
size_t frames_dropped = 0;
scoped_refptr<VideoFrame> frame = RenderAndStep(&tg, &frames_dropped);
ASSERT_TRUE(frame);
EXPECT_EQ(tg.interval(0), frame->timestamp());
EXPECT_EQ(0u, frames_dropped);
ASSERT_TRUE(is_using_cadence());
EXPECT_EQ(2u, algorithm_.EffectiveFramesQueued());
// Advance expiry enough that some frames are removed, but one remains and is
// still counted as effective.
ASSERT_EQ(2u, algorithm_.RemoveExpiredFrames(tg.current() + tg.interval(1) +
max_acceptable_drift() * 1.25));
EXPECT_EQ(1u, frames_queued());
EXPECT_EQ(1u, algorithm_.EffectiveFramesQueued());
// Advancing expiry once more should mark the frame as ineffective.
tg.step(3);
ASSERT_EQ(0u, algorithm_.RemoveExpiredFrames(tg.current()));
EXPECT_EQ(1u, frames_queued());
EXPECT_EQ(0u, algorithm_.EffectiveFramesQueued());
}
TEST_F(VideoRendererAlgorithmTest, CadenceBasedTest) {
// Common display rates.
const double kDisplayRates[] = {
NTSC(24),
24,
NTSC(25),
25,
NTSC(30),
30,
48,
NTSC(50),
50,
NTSC(60),
60,
75,
120,
144,
};
// List of common frame rate values. Values pulled from local test media,
// videostack test matrix, and Wikipedia.
const double kTestRates[] = {
1, 10, 12.5, 15, NTSC(24), 24, NTSC(25), 25,
NTSC(30), 30, 30.12, 48, NTSC(50), 50, 58.74, NTSC(60),
60, 72, 90, 100, 120, 144, 240, 300,
};
for (double display_rate : kDisplayRates) {
for (double frame_rate : kTestRates) {
TickGenerator frame_tg(base::TimeTicks(), frame_rate);
TickGenerator display_tg(tick_clock_->NowTicks(), display_rate);
RunFramePumpTest(
true, &frame_tg, &display_tg,
[](const scoped_refptr<VideoFrame>& frame, size_t frames_dropped) {});
if (HasFatalFailure())
return;
}
}
}
// Rotate through various playback rates and ensure algorithm adapts correctly.
TEST_F(VideoRendererAlgorithmTest, VariableFrameRateCadence) {
TickGenerator frame_tg(base::TimeTicks(), NTSC(30));
TickGenerator display_tg(tick_clock_->NowTicks(), 60);
const double kTestRates[] = {1.0, 2, 0.215, 0.5, 1.0, 3.15};
const bool kTestRateHasCadence[arraysize(kTestRates)] = {true, true, true,
true, true, false};
for (size_t i = 0; i < arraysize(kTestRates); ++i) {
const double playback_rate = kTestRates[i];
SCOPED_TRACE(base::StringPrintf("Playback Rate: %.03f", playback_rate));
time_source_.SetPlaybackRate(playback_rate);
RunFramePumpTest(false, &frame_tg, &display_tg,
[this](const scoped_refptr<VideoFrame>& frame,
size_t frames_dropped) {});
if (HasFatalFailure())
return;
ASSERT_EQ(kTestRateHasCadence[i], is_using_cadence());
}
// TODO(dalecurtis): Is there more we can test here?
}
// Ensures media which only expresses timestamps in milliseconds, gets the right
// cadence detection.
TEST_F(VideoRendererAlgorithmTest, UglyTimestampsHaveCadence) {
TickGenerator display_tg(tick_clock_->NowTicks(), 60);
time_source_.StartTicking();
// 59.94fps, timestamp deltas from https://youtu.be/byoLvAo9qjs
const int kBadTimestampsMs[] = {
17, 16, 17, 17, 16, 17, 17, 16, 17, 17, 17, 16, 17, 17, 16, 17, 17, 16,
17, 17, 16, 17, 17, 16, 17, 17, 16, 17, 17, 17, 16, 17, 17, 16, 17, 17,
16, 17, 17, 16, 17, 17, 16, 17, 17, 16, 17, 17, 16, 17, 17, 17};
// Run throught ~1.6 seconds worth of frames.
bool cadence_detected = false;
base::TimeDelta timestamp;
for (size_t i = 0; i < arraysize(kBadTimestampsMs) * 2; ++i) {
while (algorithm_.EffectiveFramesQueued() < 3) {
algorithm_.EnqueueFrame(CreateFrame(timestamp));
timestamp += base::TimeDelta::FromMilliseconds(
kBadTimestampsMs[i % arraysize(kBadTimestampsMs)]);
}
size_t frames_dropped = 0;
RenderAndStep(&display_tg, &frames_dropped);
ASSERT_EQ(0u, frames_dropped);
// Cadence won't be detected immediately on this clip, but it will after
// enough frames are encountered; after which it should not drop out of
// cadence.
if (is_using_cadence())
cadence_detected = true;
if (cadence_detected)
ASSERT_TRUE(is_using_cadence());
}
}
TEST_F(VideoRendererAlgorithmTest, EnqueueFrames) {
TickGenerator tg(base::TimeTicks(), 50);
time_source_.StartTicking();
EXPECT_EQ(0u, frames_queued());
scoped_refptr<VideoFrame> frame_1 = CreateFrame(tg.interval(0));
algorithm_.EnqueueFrame(frame_1);
EXPECT_EQ(1u, frames_queued());
// Enqueuing a frame with the same timestamp should always be dropped.
scoped_refptr<VideoFrame> frame_2 = CreateFrame(tg.interval(0));
algorithm_.EnqueueFrame(frame_2);
EXPECT_EQ(1u, frames_queued());
size_t frames_dropped = 0;
scoped_refptr<VideoFrame> rendered_frame =
RenderAndStep(&tg, &frames_dropped);
EXPECT_EQ(1u, frames_queued());
EXPECT_EQ(frame_1, rendered_frame);
EXPECT_EQ(1, GetCurrentFrameDisplayCount());
// The replaced frame should count as dropped.
EXPECT_EQ(1u, frames_dropped);
// Trying to replace frame_1 with frame_2 should do nothing.
algorithm_.EnqueueFrame(frame_2);
EXPECT_EQ(1u, frames_queued());
rendered_frame = RenderAndStep(&tg, &frames_dropped);
EXPECT_EQ(1u, frames_queued());
EXPECT_EQ(frame_1, rendered_frame);
EXPECT_EQ(1u, frames_dropped);
EXPECT_EQ(2, GetCurrentFrameDisplayCount());
// Trying to add a frame < 1 ms after the last frame should drop the frame.
algorithm_.EnqueueFrame(CreateFrame(base::TimeDelta::FromMicroseconds(999)));
rendered_frame = RenderAndStep(&tg, &frames_dropped);
EXPECT_EQ(1u, frames_queued());
EXPECT_EQ(frame_1, rendered_frame);
EXPECT_EQ(1u, frames_dropped);
EXPECT_EQ(3, GetCurrentFrameDisplayCount());
scoped_refptr<VideoFrame> frame_3 = CreateFrame(tg.interval(1));
algorithm_.EnqueueFrame(frame_3);
EXPECT_EQ(2u, frames_queued());
// Trying to add a frame < 1 ms before the last frame should drop the frame.
algorithm_.EnqueueFrame(
CreateFrame(tg.interval(1) - base::TimeDelta::FromMicroseconds(999)));
rendered_frame = RenderAndStep(&tg, &frames_dropped);
EXPECT_EQ(1u, frames_queued());
EXPECT_EQ(frame_3, rendered_frame);
EXPECT_EQ(1u, frames_dropped);
EXPECT_EQ(1, GetCurrentFrameDisplayCount());
}
TEST_F(VideoRendererAlgorithmTest, CadenceForFutureFrames) {
TickGenerator tg(base::TimeTicks(), 50);
time_source_.StartTicking();
disable_cadence_hysteresis();
algorithm_.EnqueueFrame(CreateFrame(tg.interval(10)));
algorithm_.EnqueueFrame(CreateFrame(tg.interval(11)));
algorithm_.EnqueueFrame(CreateFrame(tg.interval(12)));
EXPECT_EQ(3u, frames_queued());
// Call Render() a few times to increment the render count.
for (int i = 0; i < 10; ++i) {
size_t frames_dropped = 0;
scoped_refptr<VideoFrame> rendered_frame =
RenderAndStep(&tg, &frames_dropped);
EXPECT_EQ(3u, frames_queued());
EXPECT_EQ(tg.interval(10), rendered_frame->timestamp());
ASSERT_TRUE(is_using_cadence());
}
// Add some noise to the tick generator so it our first frame
// doesn't line up evenly on a deadline.
tg.Reset(tg.current() + base::TimeDelta::FromMilliseconds(5));
// We're now at the first frame, cadence should be one, so
// it should only be displayed once.
size_t frames_dropped = 0;
scoped_refptr<VideoFrame> rendered_frame =
RenderAndStep(&tg, &frames_dropped);
EXPECT_EQ(3u, frames_queued());
EXPECT_EQ(tg.interval(10), rendered_frame->timestamp());
ASSERT_TRUE(is_using_cadence());
// Then the next frame should be displayed.
rendered_frame = RenderAndStep(&tg, &frames_dropped);
EXPECT_EQ(2u, frames_queued());
EXPECT_EQ(tg.interval(11), rendered_frame->timestamp());
ASSERT_TRUE(is_using_cadence());
// Finally the last frame.
rendered_frame = RenderAndStep(&tg, &frames_dropped);
EXPECT_EQ(1u, frames_queued());
EXPECT_EQ(tg.interval(12), rendered_frame->timestamp());
ASSERT_TRUE(is_using_cadence());
}
} // namespace media
|