forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_renderer_impl_unittest.cc
1514 lines (1277 loc) · 53.2 KB
/
video_renderer_impl_unittest.cc
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
// Copyright 2013 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 <stdint.h>
#include <memory>
#include <utility>
#include "base/bind.h"
#include "base/callback.h"
#include "base/callback_helpers.h"
#include "base/debug/stack_trace.h"
#include "base/macros.h"
#include "base/memory/memory_pressure_listener.h"
#include "base/memory/memory_pressure_monitor.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/lock.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_tick_clock.h"
#include "media/base/data_buffer.h"
#include "media/base/gmock_callback_support.h"
#include "media/base/limits.h"
#include "media/base/media_switches.h"
#include "media/base/mock_filters.h"
#include "media/base/null_video_sink.h"
#include "media/base/test_helpers.h"
#include "media/base/video_frame.h"
#include "media/base/wall_clock_time_source.h"
#include "media/renderers/mock_gpu_memory_buffer_video_frame_pool.h"
#include "media/renderers/video_renderer_impl.h"
#include "testing/gmock_mutant.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using ::testing::AnyNumber;
using ::testing::CreateFunctor;
using ::testing::Invoke;
using ::testing::Mock;
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::SaveArg;
using ::testing::StrictMock;
namespace media {
MATCHER_P(HasTimestampMatcher, ms, "") {
*result_listener << "has timestamp " << arg->timestamp().InMilliseconds();
return arg->timestamp().InMilliseconds() == ms;
}
class VideoRendererImplTest : public testing::Test {
public:
std::vector<std::unique_ptr<VideoDecoder>> CreateVideoDecodersForTest() {
decoder_ = new NiceMock<MockVideoDecoder>();
std::vector<std::unique_ptr<VideoDecoder>> decoders;
decoders.push_back(base::WrapUnique(decoder_));
ON_CALL(*decoder_, Initialize(_, _, _, _, _))
.WillByDefault(DoAll(SaveArg<4>(&output_cb_),
RunCallback<3>(expect_init_success_)));
// Monitor decodes from the decoder.
ON_CALL(*decoder_, Decode(_, _))
.WillByDefault(Invoke(this, &VideoRendererImplTest::DecodeRequested));
ON_CALL(*decoder_, Reset(_))
.WillByDefault(Invoke(this, &VideoRendererImplTest::FlushRequested));
return decoders;
}
VideoRendererImplTest()
: tick_clock_(new base::SimpleTestTickClock()),
decoder_(nullptr),
demuxer_stream_(DemuxerStream::VIDEO),
simulate_decode_delay_(false),
expect_init_success_(true) {
null_video_sink_.reset(new NullVideoSink(
false, base::TimeDelta::FromSecondsD(1.0 / 60),
base::Bind(&MockCB::FrameReceived, base::Unretained(&mock_cb_)),
message_loop_.task_runner()));
// Complexity based buffering does not affect any tests not specifically
// written to test it, so enable it always.
scoped_feature_list_.InitAndEnableFeature(kComplexityBasedVideoBuffering);
renderer_.reset(new VideoRendererImpl(
message_loop_.task_runner(), message_loop_.task_runner().get(),
null_video_sink_.get(),
base::Bind(&VideoRendererImplTest::CreateVideoDecodersForTest,
base::Unretained(this)),
true,
nullptr, // gpu_factories
&media_log_));
renderer_->SetTickClockForTesting(
std::unique_ptr<base::TickClock>(tick_clock_));
null_video_sink_->set_tick_clock_for_testing(tick_clock_);
time_source_.set_tick_clock_for_testing(tick_clock_);
// Start wallclock time at a non-zero value.
AdvanceWallclockTimeInMs(12345);
demuxer_stream_.set_video_decoder_config(TestVideoConfig::Normal());
// We expect these to be called but we don't care how/when.
EXPECT_CALL(demuxer_stream_, Read(_)).WillRepeatedly(
RunCallback<0>(DemuxerStream::kOk,
scoped_refptr<DecoderBuffer>(new DecoderBuffer(0))));
}
virtual ~VideoRendererImplTest() {}
void Initialize() {
InitializeWithLowDelay(false);
}
void InitializeWithLowDelay(bool low_delay) {
// Initialize, we shouldn't have any reads.
InitializeRenderer(&demuxer_stream_, low_delay, true);
}
void InitializeRenderer(MockDemuxerStream* demuxer_stream,
bool low_delay,
bool expect_success) {
SCOPED_TRACE(base::StringPrintf("InitializeRenderer(%d)", expect_success));
expect_init_success_ = expect_success;
WaitableMessageLoopEvent event;
CallInitialize(demuxer_stream, event.GetPipelineStatusCB(), low_delay,
expect_success);
event.RunAndWaitForStatus(expect_success ? PIPELINE_OK
: DECODER_ERROR_NOT_SUPPORTED);
}
void CallInitialize(MockDemuxerStream* demuxer_stream,
const PipelineStatusCB& status_cb,
bool low_delay,
bool expect_success) {
if (low_delay)
demuxer_stream->set_liveness(DemuxerStream::LIVENESS_LIVE);
EXPECT_CALL(mock_cb_, OnWaitingForDecryptionKey()).Times(0);
EXPECT_CALL(mock_cb_, OnAudioConfigChange(_)).Times(0);
renderer_->Initialize(demuxer_stream, nullptr, &mock_cb_,
base::Bind(&WallClockTimeSource::GetWallClockTimes,
base::Unretained(&time_source_)),
status_cb);
}
void StartPlayingFrom(int milliseconds) {
SCOPED_TRACE(base::StringPrintf("StartPlayingFrom(%d)", milliseconds));
const base::TimeDelta media_time =
base::TimeDelta::FromMilliseconds(milliseconds);
time_source_.SetMediaTime(media_time);
renderer_->StartPlayingFrom(media_time);
base::RunLoop().RunUntilIdle();
}
void Flush() {
SCOPED_TRACE("Flush()");
WaitableMessageLoopEvent event;
renderer_->Flush(event.GetClosure());
event.RunAndWait();
}
void Destroy() {
SCOPED_TRACE("Destroy()");
renderer_.reset();
base::RunLoop().RunUntilIdle();
}
// Parses a string representation of video frames and generates corresponding
// VideoFrame objects in |decode_results_|.
//
// Syntax:
// nn - Queue a decoder buffer with timestamp nn * 1000us
// abort - Queue an aborted read
// error - Queue a decoder error
//
// Examples:
// A clip that is four frames long: "0 10 20 30"
// A clip that has a decode error: "60 70 error"
void QueueFrames(const std::string& str) {
for (const std::string& token :
base::SplitString(str, " ", base::TRIM_WHITESPACE,
base::SPLIT_WANT_ALL)) {
if (token == "abort") {
scoped_refptr<VideoFrame> null_frame;
QueueFrame(DecodeStatus::ABORTED, null_frame);
continue;
}
if (token == "error") {
scoped_refptr<VideoFrame> null_frame;
QueueFrame(DecodeStatus::DECODE_ERROR, null_frame);
continue;
}
int timestamp_in_ms = 0;
if (base::StringToInt(token, ×tamp_in_ms)) {
gfx::Size natural_size = TestVideoConfig::NormalCodedSize();
scoped_refptr<VideoFrame> frame = VideoFrame::CreateFrame(
PIXEL_FORMAT_YV12, natural_size, gfx::Rect(natural_size),
natural_size, base::TimeDelta::FromMilliseconds(timestamp_in_ms));
QueueFrame(DecodeStatus::OK, frame);
continue;
}
CHECK(false) << "Unrecognized decoder buffer token: " << token;
}
}
// Queues video frames to be served by the decoder during rendering.
void QueueFrame(DecodeStatus status, scoped_refptr<VideoFrame> frame) {
decode_results_.push_back(std::make_pair(status, frame));
}
bool IsReadPending() {
return !decode_cb_.is_null();
}
void WaitForError(PipelineStatus expected) {
SCOPED_TRACE(base::StringPrintf("WaitForError(%d)", expected));
WaitableMessageLoopEvent event;
PipelineStatusCB error_cb = event.GetPipelineStatusCB();
EXPECT_CALL(mock_cb_, OnError(_)).WillOnce(Invoke(CreateFunctor(error_cb)));
event.RunAndWaitForStatus(expected);
}
void WaitForEnded() {
SCOPED_TRACE("WaitForEnded()");
WaitableMessageLoopEvent event;
EXPECT_CALL(mock_cb_, OnEnded()).WillOnce(RunClosure(event.GetClosure()));
event.RunAndWait();
}
void WaitForPendingDecode() {
SCOPED_TRACE("WaitForPendingDecode()");
if (!decode_cb_.is_null())
return;
DCHECK(wait_for_pending_decode_cb_.is_null());
WaitableMessageLoopEvent event;
wait_for_pending_decode_cb_ = event.GetClosure();
event.RunAndWait();
DCHECK(!decode_cb_.is_null());
DCHECK(wait_for_pending_decode_cb_.is_null());
}
void SatisfyPendingDecode() {
CHECK(!decode_cb_.is_null());
CHECK(!decode_results_.empty());
// Post tasks for OutputCB and DecodeCB.
scoped_refptr<VideoFrame> frame = decode_results_.front().second;
if (frame.get())
message_loop_.task_runner()->PostTask(FROM_HERE,
base::Bind(output_cb_, frame));
message_loop_.task_runner()->PostTask(
FROM_HERE, base::Bind(base::ResetAndReturn(&decode_cb_),
decode_results_.front().first));
decode_results_.pop_front();
}
void SatisfyPendingDecodeWithEndOfStream() {
DCHECK(!decode_cb_.is_null());
// Return EOS buffer to trigger EOS frame.
EXPECT_CALL(demuxer_stream_, Read(_))
.WillOnce(RunCallback<0>(DemuxerStream::kOk,
DecoderBuffer::CreateEOSBuffer()));
// Satify pending |decode_cb_| to trigger a new DemuxerStream::Read().
message_loop_.task_runner()->PostTask(
FROM_HERE,
base::Bind(base::ResetAndReturn(&decode_cb_), DecodeStatus::OK));
WaitForPendingDecode();
message_loop_.task_runner()->PostTask(
FROM_HERE,
base::Bind(base::ResetAndReturn(&decode_cb_), DecodeStatus::OK));
}
void AdvanceWallclockTimeInMs(int time_ms) {
DCHECK_EQ(&message_loop_, base::MessageLoop::current());
base::AutoLock l(lock_);
tick_clock_->Advance(base::TimeDelta::FromMilliseconds(time_ms));
}
void AdvanceTimeInMs(int time_ms) {
DCHECK_EQ(&message_loop_, base::MessageLoop::current());
base::AutoLock l(lock_);
time_ += base::TimeDelta::FromMilliseconds(time_ms);
time_source_.StopTicking();
time_source_.SetMediaTime(time_);
time_source_.StartTicking();
}
enum class UnderflowTestType {
NORMAL,
LOW_DELAY,
CANT_READ_WITHOUT_STALLING
};
void BasicUnderflowTest(UnderflowTestType type) {
InitializeWithLowDelay(type == UnderflowTestType::LOW_DELAY);
if (type == UnderflowTestType::CANT_READ_WITHOUT_STALLING)
ON_CALL(*decoder_, CanReadWithoutStalling()).WillByDefault(Return(false));
QueueFrames("0 30 60 90");
{
WaitableMessageLoopEvent event;
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(0)));
EXPECT_CALL(mock_cb_, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH))
.WillOnce(RunClosure(event.GetClosure()));
EXPECT_CALL(mock_cb_, OnStatisticsUpdate(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnVideoNaturalSizeChange(_)).Times(1);
EXPECT_CALL(mock_cb_, OnVideoOpacityChange(_)).Times(1);
StartPlayingFrom(0);
event.RunAndWait();
Mock::VerifyAndClearExpectations(&mock_cb_);
}
renderer_->OnTimeProgressing();
// Advance time slightly, but enough to exceed the duration of the last
// frame.
// Frames should be dropped and we should NOT signal having nothing.
{
SCOPED_TRACE("Waiting for frame drops");
WaitableMessageLoopEvent event;
// Note: Starting the TimeSource will cause the old VideoRendererImpl to
// start rendering frames on its own thread, so the first frame may be
// received.
time_source_.StartTicking();
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(30))).Times(0);
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(60))).Times(0);
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(90)))
.WillOnce(RunClosure(event.GetClosure()));
EXPECT_CALL(mock_cb_, OnStatisticsUpdate(_)).Times(AnyNumber());
AdvanceTimeInMs(91);
event.RunAndWait();
Mock::VerifyAndClearExpectations(&mock_cb_);
}
// Advance time more. Now we should signal having nothing. And put
// the last frame up for display.
{
SCOPED_TRACE("Waiting for BUFFERING_HAVE_NOTHING");
WaitableMessageLoopEvent event;
EXPECT_CALL(mock_cb_, OnBufferingStateChange(BUFFERING_HAVE_NOTHING))
.WillOnce(RunClosure(event.GetClosure()));
AdvanceTimeInMs(30);
event.RunAndWait();
Mock::VerifyAndClearExpectations(&mock_cb_);
}
// Simulate delayed buffering state callbacks.
renderer_->OnTimeStopped();
renderer_->OnTimeProgressing();
// Receiving end of stream should signal having enough.
{
SCOPED_TRACE("Waiting for BUFFERING_HAVE_ENOUGH");
WaitableMessageLoopEvent event;
EXPECT_CALL(mock_cb_, OnStatisticsUpdate(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH))
.WillOnce(RunClosure(event.GetClosure()));
EXPECT_CALL(mock_cb_, OnEnded());
SatisfyPendingDecodeWithEndOfStream();
event.RunAndWait();
}
Destroy();
}
void UnderflowRecoveryTest(UnderflowTestType type) {
InitializeWithLowDelay(type == UnderflowTestType::LOW_DELAY);
if (type == UnderflowTestType::CANT_READ_WITHOUT_STALLING)
ON_CALL(*decoder_, CanReadWithoutStalling()).WillByDefault(Return(false));
QueueFrames("0 20 40 60");
{
WaitableMessageLoopEvent event;
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(0)));
EXPECT_CALL(mock_cb_, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH))
.WillOnce(RunClosure(event.GetClosure()));
EXPECT_CALL(mock_cb_, OnStatisticsUpdate(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnVideoNaturalSizeChange(_)).Times(1);
EXPECT_CALL(mock_cb_, OnVideoOpacityChange(_)).Times(1);
StartPlayingFrom(0);
event.RunAndWait();
Mock::VerifyAndClearExpectations(&mock_cb_);
}
renderer_->OnTimeProgressing();
time_source_.StartTicking();
// Advance time, this should cause have nothing to be signaled.
{
SCOPED_TRACE("Waiting for BUFFERING_HAVE_NOTHING");
WaitableMessageLoopEvent event;
EXPECT_CALL(mock_cb_, OnBufferingStateChange(BUFFERING_HAVE_NOTHING))
.WillOnce(RunClosure(event.GetClosure()));
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(20))).Times(1);
EXPECT_CALL(mock_cb_, OnStatisticsUpdate(_)).Times(AnyNumber());
AdvanceTimeInMs(20);
event.RunAndWait();
Mock::VerifyAndClearExpectations(&mock_cb_);
}
AdvanceTimeInMs(59);
EXPECT_EQ(3u, renderer_->frames_queued_for_testing());
time_source_.StopTicking();
renderer_->OnTimeStopped();
EXPECT_EQ(0u, renderer_->frames_queued_for_testing());
ASSERT_TRUE(IsReadPending());
// Queue some frames, satisfy reads, and make sure expired frames are gone
// when the renderer paints the first frame.
{
SCOPED_TRACE("Waiting for BUFFERING_HAVE_ENOUGH");
WaitableMessageLoopEvent event;
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(80))).Times(1);
EXPECT_CALL(mock_cb_, OnStatisticsUpdate(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH))
.WillOnce(RunClosure(event.GetClosure()));
// Note: In the normal underflow case we queue 5 frames here instead of
// four since the underflow increases the number of required frames to
// reach the have enough state.
if (type == UnderflowTestType::NORMAL)
QueueFrames("80 100 120 140 160");
else
QueueFrames("40 60 80 90");
SatisfyPendingDecode();
event.RunAndWait();
}
Destroy();
}
MOCK_METHOD0(OnSimulateDecodeDelay, base::TimeDelta(void));
protected:
base::MessageLoop message_loop_;
MediaLog media_log_;
// Fixture members.
base::test::ScopedFeatureList scoped_feature_list_;
std::unique_ptr<VideoRendererImpl> renderer_;
base::SimpleTestTickClock* tick_clock_; // Owned by |renderer_|.
NiceMock<MockVideoDecoder>* decoder_; // Owned by |renderer_|.
NiceMock<MockDemuxerStream> demuxer_stream_;
bool simulate_decode_delay_;
bool expect_init_success_;
// Use StrictMock<T> to catch missing/extra callbacks.
class MockCB : public MockRendererClient {
public:
MOCK_METHOD1(FrameReceived, void(const scoped_refptr<VideoFrame>&));
};
StrictMock<MockCB> mock_cb_;
// Must be destroyed before |renderer_| since they share |tick_clock_|.
std::unique_ptr<NullVideoSink> null_video_sink_;
WallClockTimeSource time_source_;
private:
void DecodeRequested(const scoped_refptr<DecoderBuffer>& buffer,
const VideoDecoder::DecodeCB& decode_cb) {
DCHECK_EQ(&message_loop_, base::MessageLoop::current());
CHECK(decode_cb_.is_null());
decode_cb_ = decode_cb;
// Wake up WaitForPendingDecode() if needed.
if (!wait_for_pending_decode_cb_.is_null())
base::ResetAndReturn(&wait_for_pending_decode_cb_).Run();
if (decode_results_.empty())
return;
if (simulate_decode_delay_)
tick_clock_->Advance(OnSimulateDecodeDelay());
SatisfyPendingDecode();
}
void FlushRequested(const base::Closure& callback) {
DCHECK_EQ(&message_loop_, base::MessageLoop::current());
decode_results_.clear();
if (!decode_cb_.is_null()) {
QueueFrames("abort");
SatisfyPendingDecode();
}
message_loop_.task_runner()->PostTask(FROM_HERE, callback);
}
// Used to protect |time_|.
base::Lock lock_;
base::TimeDelta time_;
// Used for satisfying reads.
VideoDecoder::OutputCB output_cb_;
VideoDecoder::DecodeCB decode_cb_;
base::TimeDelta next_frame_timestamp_;
// Run during DecodeRequested() to unblock WaitForPendingDecode().
base::Closure wait_for_pending_decode_cb_;
std::deque<std::pair<DecodeStatus, scoped_refptr<VideoFrame>>>
decode_results_;
DISALLOW_COPY_AND_ASSIGN(VideoRendererImplTest);
};
TEST_F(VideoRendererImplTest, DoNothing) {
// Test that creation and deletion doesn't depend on calls to Initialize()
// and/or Destroy().
}
TEST_F(VideoRendererImplTest, DestroyWithoutInitialize) {
Destroy();
}
TEST_F(VideoRendererImplTest, Initialize) {
Initialize();
Destroy();
}
TEST_F(VideoRendererImplTest, InitializeAndStartPlayingFrom) {
Initialize();
QueueFrames("0 10 20 30");
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(0)));
EXPECT_CALL(mock_cb_, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH));
EXPECT_CALL(mock_cb_, OnStatisticsUpdate(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnVideoNaturalSizeChange(_)).Times(1);
EXPECT_CALL(mock_cb_, OnVideoOpacityChange(_)).Times(1);
StartPlayingFrom(0);
Destroy();
}
TEST_F(VideoRendererImplTest, InitializeAndEndOfStream) {
Initialize();
StartPlayingFrom(0);
WaitForPendingDecode();
{
SCOPED_TRACE("Waiting for BUFFERING_HAVE_ENOUGH");
WaitableMessageLoopEvent event;
EXPECT_CALL(mock_cb_, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH))
.WillOnce(RunClosure(event.GetClosure()));
EXPECT_CALL(mock_cb_, OnEnded());
SatisfyPendingDecodeWithEndOfStream();
event.RunAndWait();
}
// Firing a time state changed to true should be ignored...
renderer_->OnTimeProgressing();
EXPECT_FALSE(null_video_sink_->is_started());
Destroy();
}
TEST_F(VideoRendererImplTest, ReinitializeForAnotherStream) {
Initialize();
StartPlayingFrom(0);
Flush();
NiceMock<MockDemuxerStream> new_stream(DemuxerStream::VIDEO);
new_stream.set_video_decoder_config(TestVideoConfig::Normal());
InitializeRenderer(&new_stream, false, true);
}
TEST_F(VideoRendererImplTest, DestroyWhileInitializing) {
CallInitialize(&demuxer_stream_, NewExpectedStatusCB(PIPELINE_ERROR_ABORT),
false, PIPELINE_OK);
Destroy();
}
TEST_F(VideoRendererImplTest, DestroyWhileFlushing) {
Initialize();
QueueFrames("0 10 20 30");
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(0)));
EXPECT_CALL(mock_cb_, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH));
EXPECT_CALL(mock_cb_, OnStatisticsUpdate(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnVideoNaturalSizeChange(_)).Times(1);
EXPECT_CALL(mock_cb_, OnVideoOpacityChange(_)).Times(1);
StartPlayingFrom(0);
renderer_->Flush(NewExpectedClosure());
Destroy();
}
TEST_F(VideoRendererImplTest, Play) {
Initialize();
QueueFrames("0 10 20 30");
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(0)));
EXPECT_CALL(mock_cb_, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH));
EXPECT_CALL(mock_cb_, OnStatisticsUpdate(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnVideoNaturalSizeChange(_)).Times(1);
EXPECT_CALL(mock_cb_, OnVideoOpacityChange(_)).Times(1);
StartPlayingFrom(0);
Destroy();
}
TEST_F(VideoRendererImplTest, FlushWithNothingBuffered) {
Initialize();
StartPlayingFrom(0);
// We shouldn't expect a buffering state change since we never reached
// BUFFERING_HAVE_ENOUGH.
Flush();
Destroy();
}
// Verify that the flush callback is invoked outside of VideoRenderer lock, so
// we should be able to call other renderer methods from the Flush callback.
static void VideoRendererImplTest_FlushDoneCB(VideoRendererImplTest* test,
VideoRenderer* renderer,
const base::Closure& success_cb) {
test->QueueFrames("0 10 20 30");
renderer->StartPlayingFrom(base::TimeDelta::FromSeconds(0));
success_cb.Run();
}
TEST_F(VideoRendererImplTest, FlushCallbackNoLock) {
Initialize();
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(0)));
EXPECT_CALL(mock_cb_, OnStatisticsUpdate(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnVideoNaturalSizeChange(_)).Times(1);
EXPECT_CALL(mock_cb_, OnVideoOpacityChange(_)).Times(1);
StartPlayingFrom(0);
WaitableMessageLoopEvent event;
renderer_->Flush(
base::Bind(&VideoRendererImplTest_FlushDoneCB, base::Unretained(this),
base::Unretained(renderer_.get()), event.GetClosure()));
event.RunAndWait();
Destroy();
}
TEST_F(VideoRendererImplTest, DecodeError_Playing) {
Initialize();
QueueFrames("0 10 20 30");
EXPECT_CALL(mock_cb_, FrameReceived(_)).Times(testing::AtLeast(1));
// Consider the case that rendering is faster than we setup the test event.
// In that case, when we run out of the frames, BUFFERING_HAVE_NOTHING will
// be called.
EXPECT_CALL(mock_cb_, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH));
EXPECT_CALL(mock_cb_, OnBufferingStateChange(BUFFERING_HAVE_NOTHING))
.Times(testing::AtMost(1));
EXPECT_CALL(mock_cb_, OnStatisticsUpdate(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnVideoNaturalSizeChange(_)).Times(1);
EXPECT_CALL(mock_cb_, OnVideoOpacityChange(_)).Times(1);
StartPlayingFrom(0);
renderer_->OnTimeProgressing();
time_source_.StartTicking();
AdvanceTimeInMs(10);
QueueFrames("error");
SatisfyPendingDecode();
WaitForError(PIPELINE_ERROR_DECODE);
Destroy();
}
TEST_F(VideoRendererImplTest, DecodeError_DuringStartPlayingFrom) {
Initialize();
QueueFrames("error");
EXPECT_CALL(mock_cb_, OnError(PIPELINE_ERROR_DECODE));
StartPlayingFrom(0);
Destroy();
}
TEST_F(VideoRendererImplTest, StartPlayingFrom_Exact) {
Initialize();
QueueFrames("50 60 70 80 90");
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(60)));
EXPECT_CALL(mock_cb_, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH));
EXPECT_CALL(mock_cb_, OnStatisticsUpdate(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnVideoNaturalSizeChange(_)).Times(1);
EXPECT_CALL(mock_cb_, OnVideoOpacityChange(_)).Times(1);
StartPlayingFrom(60);
Destroy();
}
TEST_F(VideoRendererImplTest, StartPlayingFrom_RightBefore) {
Initialize();
QueueFrames("50 60 70 80 90");
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(50)));
EXPECT_CALL(mock_cb_, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH));
EXPECT_CALL(mock_cb_, OnStatisticsUpdate(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnVideoNaturalSizeChange(_)).Times(1);
EXPECT_CALL(mock_cb_, OnVideoOpacityChange(_)).Times(1);
StartPlayingFrom(59);
Destroy();
}
TEST_F(VideoRendererImplTest, StartPlayingFrom_RightAfter) {
Initialize();
QueueFrames("50 60 70 80 90");
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(60)));
EXPECT_CALL(mock_cb_, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH));
EXPECT_CALL(mock_cb_, OnStatisticsUpdate(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnVideoNaturalSizeChange(_)).Times(1);
EXPECT_CALL(mock_cb_, OnVideoOpacityChange(_)).Times(1);
StartPlayingFrom(61);
Destroy();
}
TEST_F(VideoRendererImplTest, StartPlayingFrom_LowDelay) {
// In low-delay mode only one frame is required to finish preroll. But frames
// prior to the start time will not be used.
InitializeWithLowDelay(true);
QueueFrames("0 10");
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(10)));
// Expect some amount of have enough/nothing due to only requiring one frame.
EXPECT_CALL(mock_cb_, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH))
.Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnBufferingStateChange(BUFFERING_HAVE_NOTHING))
.Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnStatisticsUpdate(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnVideoNaturalSizeChange(_)).Times(1);
EXPECT_CALL(mock_cb_, OnVideoOpacityChange(_)).Times(1);
StartPlayingFrom(10);
QueueFrames("20");
SatisfyPendingDecode();
renderer_->OnTimeProgressing();
time_source_.StartTicking();
WaitableMessageLoopEvent event;
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(20)))
.WillOnce(RunClosure(event.GetClosure()));
AdvanceTimeInMs(20);
event.RunAndWait();
Destroy();
}
// Ensures that we don't waste memory trying to keep up with decoders that are
// too slow to playback video in real time.
TEST_F(VideoRendererImplTest, ComplexityBasedBufferingRealtimeIncapable) {
Initialize();
QueueFrames("0 1000 2000 3000 4000 5000 6000 7000 8000");
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(0)));
EXPECT_CALL(mock_cb_, OnBufferingStateChange(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnStatisticsUpdate(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnVideoNaturalSizeChange(_)).Times(1);
EXPECT_CALL(mock_cb_, OnVideoOpacityChange(_)).Times(1);
simulate_decode_delay_ = true;
null_video_sink_->set_clockless(true);
// Set a decode delay of 4s per 1s frame; too slow for realtime playback.
EXPECT_CALL(*this, OnSimulateDecodeDelay())
.WillRepeatedly(Return(base::TimeDelta::FromSeconds(4)));
StartPlayingFrom(0);
EXPECT_EQ(limits::kMaxVideoFrames,
renderer_->min_buffered_frames_for_testing());
EXPECT_EQ(limits::kMaxVideoFrames,
renderer_->max_buffered_frames_for_testing());
renderer_->OnTimeProgressing();
time_source_.StartTicking();
WaitableMessageLoopEvent event;
EXPECT_CALL(mock_cb_, FrameReceived(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(8000)))
.WillOnce(RunClosure(event.GetClosure()));
event.RunAndWait();
// No buffering should have been triggered due to speed.
EXPECT_EQ(limits::kMaxVideoFrames,
renderer_->min_buffered_frames_for_testing());
EXPECT_EQ(limits::kMaxVideoFrames,
renderer_->max_buffered_frames_for_testing());
Destroy();
}
class TestMemoryPressureMonitor : public base::MemoryPressureMonitor {
public:
TestMemoryPressureMonitor() {}
~TestMemoryPressureMonitor() override {}
MemoryPressureLevel GetCurrentPressureLevel() override {
return base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE;
}
void SetDispatchCallback(const DispatchCallback& callback) override {
NOTREACHED();
}
private:
DISALLOW_COPY_AND_ASSIGN(TestMemoryPressureMonitor);
};
// Ensures that we don't waste memory during low memory situations.
TEST_F(VideoRendererImplTest, ComplexityBasedBufferingMemoryPressure) {
Initialize();
QueueFrames("0 1000 2000 3000 4000 5000 6000 7000 8000");
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(0)));
EXPECT_CALL(mock_cb_, OnBufferingStateChange(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnStatisticsUpdate(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnVideoNaturalSizeChange(_)).Times(1);
EXPECT_CALL(mock_cb_, OnVideoOpacityChange(_)).Times(1);
simulate_decode_delay_ = true;
null_video_sink_->set_clockless(true);
TestMemoryPressureMonitor test_monitor;
EXPECT_CALL(*this, OnSimulateDecodeDelay())
.WillOnce(Return(base::TimeDelta::FromSeconds(4)))
.WillRepeatedly(Return(base::TimeDelta::FromMilliseconds(100)));
StartPlayingFrom(0);
EXPECT_EQ(limits::kMaxVideoFrames,
renderer_->min_buffered_frames_for_testing());
EXPECT_EQ(limits::kMaxVideoFrames,
renderer_->max_buffered_frames_for_testing());
renderer_->OnTimeProgressing();
time_source_.StartTicking();
WaitableMessageLoopEvent event;
EXPECT_CALL(mock_cb_, FrameReceived(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(8000)))
.WillOnce(RunClosure(event.GetClosure()));
event.RunAndWait();
// No buffering should have been triggered due to memory pressure.
EXPECT_EQ(limits::kMaxVideoFrames,
renderer_->min_buffered_frames_for_testing());
EXPECT_EQ(limits::kMaxVideoFrames,
renderer_->max_buffered_frames_for_testing());
Destroy();
}
// Ensures that we don't waste memory during low memory situations.
TEST_F(VideoRendererImplTest, ComplexityBasedBufferingBackgroundRendering) {
Initialize();
QueueFrames("0 1000 2000 3000 4000 5000 6000 7000 8000");
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(0)));
EXPECT_CALL(mock_cb_, OnBufferingStateChange(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnStatisticsUpdate(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnVideoNaturalSizeChange(_)).Times(1);
EXPECT_CALL(mock_cb_, OnVideoOpacityChange(_)).Times(1);
simulate_decode_delay_ = true;
null_video_sink_->set_clockless(true);
null_video_sink_->set_background_render(true);
TestMemoryPressureMonitor test_monitor;
EXPECT_CALL(*this, OnSimulateDecodeDelay())
.WillOnce(Return(base::TimeDelta::FromSeconds(4)))
.WillRepeatedly(Return(base::TimeDelta::FromMilliseconds(100)));
StartPlayingFrom(0);
EXPECT_EQ(limits::kMaxVideoFrames,
renderer_->min_buffered_frames_for_testing());
EXPECT_EQ(limits::kMaxVideoFrames,
renderer_->max_buffered_frames_for_testing());
renderer_->OnTimeProgressing();
time_source_.StartTicking();
WaitableMessageLoopEvent event;
EXPECT_CALL(mock_cb_, FrameReceived(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(8000)))
.WillOnce(RunClosure(event.GetClosure()));
event.RunAndWait();
// No buffering should have been triggered due to background rendering.
EXPECT_EQ(limits::kMaxVideoFrames,
renderer_->min_buffered_frames_for_testing());
EXPECT_EQ(limits::kMaxVideoFrames,
renderer_->max_buffered_frames_for_testing());
Destroy();
}
TEST_F(VideoRendererImplTest, ComplexityBasedBuffering) {
Initialize();
QueueFrames("0 1000 2000 3000 4000 5000 6000 7000 8000");
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(0)));
EXPECT_CALL(mock_cb_, OnBufferingStateChange(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnStatisticsUpdate(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnVideoNaturalSizeChange(_)).Times(1);
EXPECT_CALL(mock_cb_, OnVideoOpacityChange(_)).Times(1);
simulate_decode_delay_ = true;
null_video_sink_->set_clockless(true);
EXPECT_CALL(*this, OnSimulateDecodeDelay())
.WillOnce(Return(base::TimeDelta::FromSeconds(4)))
.WillRepeatedly(Return(base::TimeDelta::FromMilliseconds(100)));
StartPlayingFrom(0);
// Prior to playback start no extended buffering should be triggered.
EXPECT_EQ(limits::kMaxVideoFrames,
renderer_->min_buffered_frames_for_testing());
EXPECT_EQ(limits::kMaxVideoFrames,
renderer_->max_buffered_frames_for_testing());
renderer_->OnTimeProgressing();
time_source_.StartTicking();
{
// Not enough frames have been played to trigger extended buffering yet;
// start from frame 1s, since 0s is painted as the poster image.
WaitableMessageLoopEvent event;
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(1000)))
.WillOnce(RunClosure(event.GetClosure()));
event.RunAndWait();
EXPECT_EQ(limits::kMaxVideoFrames,
renderer_->min_buffered_frames_for_testing());
EXPECT_EQ(limits::kMaxVideoFrames,
renderer_->max_buffered_frames_for_testing());
}
WaitableMessageLoopEvent event;
EXPECT_CALL(mock_cb_, FrameReceived(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(8000)))
.WillOnce(RunClosure(event.GetClosure()));
event.RunAndWait();
// 4 frames * (4s - 1s) / 1s
EXPECT_EQ(12u, renderer_->max_buffered_frames_for_testing());
time_source_.StopTicking();
renderer_->OnTimeStopped();
Flush();
// Ensure min/max buffered frames is reset.
EXPECT_EQ(limits::kMaxVideoFrames,
renderer_->min_buffered_frames_for_testing());
EXPECT_EQ(limits::kMaxVideoFrames,
renderer_->max_buffered_frames_for_testing());
Destroy();
}
TEST_F(VideoRendererImplTest, ComplexityBasedBufferingUnderflow) {
Initialize();
QueueFrames("0 1000 2000 3000 4000 5000 6000 7000 8000");
EXPECT_CALL(mock_cb_, FrameReceived(HasTimestampMatcher(0)));
EXPECT_CALL(mock_cb_, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH));
EXPECT_CALL(mock_cb_, OnStatisticsUpdate(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnVideoNaturalSizeChange(_)).Times(1);
EXPECT_CALL(mock_cb_, OnVideoOpacityChange(_)).Times(1);
simulate_decode_delay_ = true;
null_video_sink_->set_clockless(true);
EXPECT_CALL(*this, OnSimulateDecodeDelay())
.WillOnce(Return(base::TimeDelta::FromSeconds(4)))
.WillRepeatedly(Return(base::TimeDelta::FromMilliseconds(100)));
StartPlayingFrom(0);
// Prior to playback start no extended buffering should be triggered.
EXPECT_EQ(limits::kMaxVideoFrames,
renderer_->max_buffered_frames_for_testing());
renderer_->OnTimeProgressing();
time_source_.StartTicking();
WaitableMessageLoopEvent event;
EXPECT_CALL(mock_cb_, FrameReceived(_)).Times(AnyNumber());
EXPECT_CALL(mock_cb_, OnBufferingStateChange(BUFFERING_HAVE_NOTHING))
.WillOnce(RunClosure(event.GetClosure()));
event.RunAndWait();
// 4 frames * (4s - 1s) / 1s
const size_t kExpectedFrames = 12u;
EXPECT_EQ(kExpectedFrames, renderer_->max_buffered_frames_for_testing());
time_source_.StopTicking();
renderer_->OnTimeStopped();
// Since we stopped in an underflow situation, ensure max buffered frames is
// promoted to the new recommended minimum.
EXPECT_EQ(kExpectedFrames, renderer_->min_buffered_frames_for_testing());
EXPECT_EQ(kExpectedFrames, renderer_->max_buffered_frames_for_testing());