1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 #include "base/message_loop/message_loop.h"
9 #include "base/stl_util.h"
10 #include "base/test/simple_test_tick_clock.h"
11 #include "base/threading/simple_thread.h"
12 #include "base/time/clock.h"
13 #include "media/base/fake_text_track_stream.h"
14 #include "media/base/gmock_callback_support.h"
15 #include "media/base/media_log.h"
16 #include "media/base/mock_filters.h"
17 #include "media/base/pipeline.h"
18 #include "media/base/test_helpers.h"
19 #include "media/base/text_renderer.h"
20 #include "media/base/text_track_config.h"
21 #include "media/base/time_delta_interpolator.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "ui/gfx/geometry/size.h"
26 using ::testing::AnyNumber
;
27 using ::testing::DeleteArg
;
28 using ::testing::DoAll
;
29 // TODO(scherkus): Remove InSequence after refactoring Pipeline.
30 using ::testing::InSequence
;
31 using ::testing::Invoke
;
32 using ::testing::InvokeWithoutArgs
;
33 using ::testing::Mock
;
34 using ::testing::NotNull
;
35 using ::testing::Return
;
36 using ::testing::SaveArg
;
37 using ::testing::StrictMock
;
38 using ::testing::WithArg
;
42 ACTION_P(SetDemuxerProperties
, duration
) {
43 arg0
->SetDuration(duration
);
46 ACTION_P2(Stop
, pipeline
, stop_cb
) {
47 pipeline
->Stop(stop_cb
);
50 ACTION_P2(SetError
, pipeline
, status
) {
51 pipeline
->SetErrorForTesting(status
);
54 ACTION_P2(SetBufferingState
, cb
, buffering_state
) {
55 cb
->Run(buffering_state
);
58 ACTION_TEMPLATE(PostCallback
,
59 HAS_1_TEMPLATE_PARAMS(int, k
),
60 AND_1_VALUE_PARAMS(p0
)) {
61 return base::MessageLoop::current()->PostTask(
62 FROM_HERE
, base::Bind(::std::tr1::get
<k
>(args
), p0
));
65 // TODO(scherkus): even though some filters are initialized on separate
66 // threads these test aren't flaky... why? It's because filters' Initialize()
67 // is executed on |message_loop_| and the mock filters instantly call
68 // InitializationComplete(), which keeps the pipeline humming along. If
69 // either filters don't call InitializationComplete() immediately or filter
70 // initialization is moved to a separate thread this test will become flaky.
71 class PipelineTest
: public ::testing::Test
{
73 // Used for setting expectations on pipeline callbacks. Using a StrictMock
74 // also lets us test for missing callbacks.
75 class CallbackHelper
{
78 virtual ~CallbackHelper() {}
80 MOCK_METHOD1(OnStart
, void(PipelineStatus
));
81 MOCK_METHOD1(OnSeek
, void(PipelineStatus
));
82 MOCK_METHOD0(OnStop
, void());
83 MOCK_METHOD0(OnEnded
, void());
84 MOCK_METHOD1(OnError
, void(PipelineStatus
));
85 MOCK_METHOD1(OnMetadata
, void(PipelineMetadata
));
86 MOCK_METHOD1(OnBufferingStateChange
, void(BufferingState
));
87 MOCK_METHOD1(OnVideoFramePaint
, void(const scoped_refptr
<VideoFrame
>&));
88 MOCK_METHOD0(OnDurationChange
, void());
91 DISALLOW_COPY_AND_ASSIGN(CallbackHelper
);
95 : pipeline_(new Pipeline(message_loop_
.message_loop_proxy(),
97 demuxer_(new StrictMock
<MockDemuxer
>()),
98 scoped_renderer_(new StrictMock
<MockRenderer
>()),
99 renderer_(scoped_renderer_
.get()) {
100 // SetDemuxerExpectations() adds overriding expectations for expected
102 DemuxerStream
* null_pointer
= NULL
;
103 EXPECT_CALL(*demuxer_
, GetStream(_
))
104 .WillRepeatedly(Return(null_pointer
));
106 EXPECT_CALL(*demuxer_
, GetTimelineOffset())
107 .WillRepeatedly(Return(base::Time()));
109 EXPECT_CALL(*renderer_
, GetMediaTime())
110 .WillRepeatedly(Return(base::TimeDelta()));
112 EXPECT_CALL(*demuxer_
, GetStartTime()).WillRepeatedly(Return(start_time_
));
115 virtual ~PipelineTest() {
116 if (!pipeline_
|| !pipeline_
->IsRunning())
121 // The mock demuxer doesn't stop the fake text track stream,
122 // so just stop it manually.
124 text_stream_
->Stop();
125 message_loop_
.RunUntilIdle();
128 // Expect a stop callback if we were started.
129 ExpectPipelineStopAndDestroyPipeline();
130 pipeline_
->Stop(base::Bind(&CallbackHelper::OnStop
,
131 base::Unretained(&callbacks_
)));
132 message_loop_
.RunUntilIdle();
135 void OnDemuxerError() {
136 // Cast because OnDemuxerError is private in Pipeline.
137 static_cast<DemuxerHost
*>(pipeline_
.get())
138 ->OnDemuxerError(PIPELINE_ERROR_ABORT
);
142 // Sets up expectations to allow the demuxer to initialize.
143 typedef std::vector
<MockDemuxerStream
*> MockDemuxerStreamVector
;
144 void SetDemuxerExpectations(MockDemuxerStreamVector
* streams
,
145 const base::TimeDelta
& duration
) {
146 EXPECT_CALL(callbacks_
, OnDurationChange());
147 EXPECT_CALL(*demuxer_
, Initialize(_
, _
, _
))
148 .WillOnce(DoAll(SetDemuxerProperties(duration
),
149 PostCallback
<1>(PIPELINE_OK
)));
151 // Configure the demuxer to return the streams.
152 for (size_t i
= 0; i
< streams
->size(); ++i
) {
153 DemuxerStream
* stream
= (*streams
)[i
];
154 EXPECT_CALL(*demuxer_
, GetStream(stream
->type()))
155 .WillRepeatedly(Return(stream
));
159 void SetDemuxerExpectations(MockDemuxerStreamVector
* streams
) {
160 // Initialize with a default non-zero duration.
161 SetDemuxerExpectations(streams
, base::TimeDelta::FromSeconds(10));
164 scoped_ptr
<StrictMock
<MockDemuxerStream
> > CreateStream(
165 DemuxerStream::Type type
) {
166 scoped_ptr
<StrictMock
<MockDemuxerStream
> > stream(
167 new StrictMock
<MockDemuxerStream
>(type
));
168 return stream
.Pass();
171 // Sets up expectations to allow the video renderer to initialize.
172 void SetRendererExpectations() {
173 EXPECT_CALL(*renderer_
, Initialize(_
, _
, _
, _
, _
, _
, _
))
174 .WillOnce(DoAll(SaveArg
<3>(&buffering_state_cb_
),
175 SaveArg
<5>(&ended_cb_
),
176 PostCallback
<1>(PIPELINE_OK
)));
177 EXPECT_CALL(*renderer_
, HasAudio()).WillRepeatedly(Return(audio_stream()));
178 EXPECT_CALL(*renderer_
, HasVideo()).WillRepeatedly(Return(video_stream()));
181 void AddTextStream() {
182 EXPECT_CALL(*this, OnAddTextTrack(_
,_
))
183 .WillOnce(Invoke(this, &PipelineTest::DoOnAddTextTrack
));
184 static_cast<DemuxerHost
*>(pipeline_
.get())->AddTextStream(text_stream(),
185 TextTrackConfig(kTextSubtitles
, "", "", ""));
186 message_loop_
.RunUntilIdle();
189 void StartPipeline() {
191 demuxer_
.get(), scoped_renderer_
.Pass(),
192 base::Bind(&CallbackHelper::OnEnded
, base::Unretained(&callbacks_
)),
193 base::Bind(&CallbackHelper::OnError
, base::Unretained(&callbacks_
)),
194 base::Bind(&CallbackHelper::OnStart
, base::Unretained(&callbacks_
)),
195 base::Bind(&CallbackHelper::OnMetadata
, base::Unretained(&callbacks_
)),
196 base::Bind(&CallbackHelper::OnBufferingStateChange
,
197 base::Unretained(&callbacks_
)),
198 base::Bind(&CallbackHelper::OnVideoFramePaint
,
199 base::Unretained(&callbacks_
)),
200 base::Bind(&CallbackHelper::OnDurationChange
,
201 base::Unretained(&callbacks_
)),
202 base::Bind(&PipelineTest::OnAddTextTrack
, base::Unretained(this)));
205 // Sets up expectations on the callback and initializes the pipeline. Called
206 // after tests have set expectations any filters they wish to use.
207 void StartPipelineAndExpect(PipelineStatus start_status
) {
208 EXPECT_CALL(callbacks_
, OnStart(start_status
));
210 if (start_status
== PIPELINE_OK
) {
211 EXPECT_CALL(callbacks_
, OnMetadata(_
)).WillOnce(SaveArg
<0>(&metadata_
));
212 EXPECT_CALL(*renderer_
, SetPlaybackRate(0.0f
));
213 EXPECT_CALL(*renderer_
, SetVolume(1.0f
));
214 EXPECT_CALL(*renderer_
, StartPlayingFrom(start_time_
))
215 .WillOnce(SetBufferingState(&buffering_state_cb_
,
216 BUFFERING_HAVE_ENOUGH
));
217 EXPECT_CALL(callbacks_
, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH
));
221 message_loop_
.RunUntilIdle();
224 void CreateAudioStream() {
225 audio_stream_
= CreateStream(DemuxerStream::AUDIO
);
228 void CreateVideoStream() {
229 video_stream_
= CreateStream(DemuxerStream::VIDEO
);
230 video_stream_
->set_video_decoder_config(video_decoder_config_
);
233 void CreateTextStream() {
234 scoped_ptr
<FakeTextTrackStream
> text_stream(new FakeTextTrackStream());
235 EXPECT_CALL(*text_stream
, OnRead()).Times(AnyNumber());
236 text_stream_
= text_stream
.Pass();
239 MockDemuxerStream
* audio_stream() {
240 return audio_stream_
.get();
243 MockDemuxerStream
* video_stream() {
244 return video_stream_
.get();
247 FakeTextTrackStream
* text_stream() {
248 return text_stream_
.get();
251 void ExpectSeek(const base::TimeDelta
& seek_time
, bool underflowed
) {
252 EXPECT_CALL(*demuxer_
, Seek(seek_time
, _
))
253 .WillOnce(RunCallback
<1>(PIPELINE_OK
));
255 EXPECT_CALL(*renderer_
, Flush(_
))
256 .WillOnce(DoAll(SetBufferingState(&buffering_state_cb_
,
257 BUFFERING_HAVE_NOTHING
),
259 EXPECT_CALL(*renderer_
, SetPlaybackRate(_
));
260 EXPECT_CALL(*renderer_
, SetVolume(_
));
261 EXPECT_CALL(*renderer_
, StartPlayingFrom(seek_time
))
262 .WillOnce(SetBufferingState(&buffering_state_cb_
,
263 BUFFERING_HAVE_ENOUGH
));
264 EXPECT_CALL(callbacks_
, OnBufferingStateChange(BUFFERING_HAVE_NOTHING
));
266 // We expect a successful seek callback followed by a buffering update.
267 EXPECT_CALL(callbacks_
, OnSeek(PIPELINE_OK
));
268 EXPECT_CALL(callbacks_
, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH
));
271 void DoSeek(const base::TimeDelta
& seek_time
) {
272 pipeline_
->Seek(seek_time
,
273 base::Bind(&CallbackHelper::OnSeek
,
274 base::Unretained(&callbacks_
)));
275 message_loop_
.RunUntilIdle();
278 void DestroyPipeline() {
279 // In real code Pipeline could be destroyed on a different thread. All weak
280 // pointers must have been invalidated before the stop callback returns.
281 DCHECK(!pipeline_
->HasWeakPtrsForTesting());
285 void ExpectDemuxerStop() {
287 EXPECT_CALL(*demuxer_
, Stop());
290 void ExpectPipelineStopAndDestroyPipeline() {
291 // After the Pipeline is stopped, it could be destroyed any time. Always
292 // destroy the pipeline immediately after OnStop() to test this.
293 EXPECT_CALL(callbacks_
, OnStop())
294 .WillOnce(Invoke(this, &PipelineTest::DestroyPipeline
));
297 MOCK_METHOD2(OnAddTextTrack
, void(const TextTrackConfig
&,
298 const AddTextTrackDoneCB
&));
300 void DoOnAddTextTrack(const TextTrackConfig
& config
,
301 const AddTextTrackDoneCB
& done_cb
) {
302 scoped_ptr
<TextTrack
> text_track(new MockTextTrack
);
303 done_cb
.Run(text_track
.Pass());
307 StrictMock
<CallbackHelper
> callbacks_
;
308 base::SimpleTestTickClock test_tick_clock_
;
309 base::MessageLoop message_loop_
;
310 scoped_ptr
<Pipeline
> pipeline_
;
312 scoped_ptr
<StrictMock
<MockDemuxer
> > demuxer_
;
313 scoped_ptr
<StrictMock
<MockRenderer
> > scoped_renderer_
;
314 StrictMock
<MockRenderer
>* renderer_
;
315 StrictMock
<CallbackHelper
> text_renderer_callbacks_
;
316 TextRenderer
* text_renderer_
;
317 scoped_ptr
<StrictMock
<MockDemuxerStream
> > audio_stream_
;
318 scoped_ptr
<StrictMock
<MockDemuxerStream
> > video_stream_
;
319 scoped_ptr
<FakeTextTrackStream
> text_stream_
;
320 BufferingStateCB buffering_state_cb_
;
321 base::Closure ended_cb_
;
322 VideoDecoderConfig video_decoder_config_
;
323 PipelineMetadata metadata_
;
324 base::TimeDelta start_time_
;
327 DISALLOW_COPY_AND_ASSIGN(PipelineTest
);
330 // Test that playback controls methods no-op when the pipeline hasn't been
332 TEST_F(PipelineTest
, NotStarted
) {
333 const base::TimeDelta kZero
;
335 EXPECT_FALSE(pipeline_
->IsRunning());
337 // Setting should still work.
338 EXPECT_EQ(0.0f
, pipeline_
->GetPlaybackRate());
339 pipeline_
->SetPlaybackRate(-1.0f
);
340 EXPECT_EQ(0.0f
, pipeline_
->GetPlaybackRate());
341 pipeline_
->SetPlaybackRate(1.0f
);
342 EXPECT_EQ(1.0f
, pipeline_
->GetPlaybackRate());
344 // Setting should still work.
345 EXPECT_EQ(1.0f
, pipeline_
->GetVolume());
346 pipeline_
->SetVolume(-1.0f
);
347 EXPECT_EQ(1.0f
, pipeline_
->GetVolume());
348 pipeline_
->SetVolume(0.0f
);
349 EXPECT_EQ(0.0f
, pipeline_
->GetVolume());
351 EXPECT_TRUE(kZero
== pipeline_
->GetMediaTime());
352 EXPECT_EQ(0u, pipeline_
->GetBufferedTimeRanges().size());
353 EXPECT_TRUE(kZero
== pipeline_
->GetMediaDuration());
356 TEST_F(PipelineTest
, NeverInitializes
) {
357 // Don't execute the callback passed into Initialize().
358 EXPECT_CALL(*demuxer_
, Initialize(_
, _
, _
));
360 // This test hangs during initialization by never calling
361 // InitializationComplete(). StrictMock<> will ensure that the callback is
364 message_loop_
.RunUntilIdle();
366 // Because our callback will get executed when the test tears down, we'll
367 // verify that nothing has been called, then set our expectation for the call
368 // made during tear down.
369 Mock::VerifyAndClear(&callbacks_
);
370 EXPECT_CALL(callbacks_
, OnStart(PIPELINE_OK
));
373 TEST_F(PipelineTest
, StopWithoutStart
) {
374 ExpectPipelineStopAndDestroyPipeline();
376 base::Bind(&CallbackHelper::OnStop
, base::Unretained(&callbacks_
)));
377 message_loop_
.RunUntilIdle();
380 TEST_F(PipelineTest
, StartThenStopImmediately
) {
381 EXPECT_CALL(*demuxer_
, Initialize(_
, _
, _
))
382 .WillOnce(PostCallback
<1>(PIPELINE_OK
));
383 EXPECT_CALL(*demuxer_
, Stop());
385 EXPECT_CALL(callbacks_
, OnStart(_
));
388 // Expect a stop callback if we were started.
389 ExpectPipelineStopAndDestroyPipeline();
391 base::Bind(&CallbackHelper::OnStop
, base::Unretained(&callbacks_
)));
392 message_loop_
.RunUntilIdle();
395 TEST_F(PipelineTest
, DemuxerErrorDuringStop
) {
397 MockDemuxerStreamVector streams
;
398 streams
.push_back(audio_stream());
400 SetDemuxerExpectations(&streams
);
401 SetRendererExpectations();
403 StartPipelineAndExpect(PIPELINE_OK
);
405 EXPECT_CALL(*demuxer_
, Stop())
406 .WillOnce(InvokeWithoutArgs(this, &PipelineTest::OnDemuxerError
));
407 ExpectPipelineStopAndDestroyPipeline();
410 base::Bind(&CallbackHelper::OnStop
, base::Unretained(&callbacks_
)));
411 message_loop_
.RunUntilIdle();
414 TEST_F(PipelineTest
, URLNotFound
) {
415 EXPECT_CALL(*demuxer_
, Initialize(_
, _
, _
))
416 .WillOnce(PostCallback
<1>(PIPELINE_ERROR_URL_NOT_FOUND
));
417 EXPECT_CALL(*demuxer_
, Stop());
419 StartPipelineAndExpect(PIPELINE_ERROR_URL_NOT_FOUND
);
422 TEST_F(PipelineTest
, NoStreams
) {
423 EXPECT_CALL(*demuxer_
, Initialize(_
, _
, _
))
424 .WillOnce(PostCallback
<1>(PIPELINE_OK
));
425 EXPECT_CALL(*demuxer_
, Stop());
427 StartPipelineAndExpect(PIPELINE_ERROR_COULD_NOT_RENDER
);
430 TEST_F(PipelineTest
, AudioStream
) {
432 MockDemuxerStreamVector streams
;
433 streams
.push_back(audio_stream());
435 SetDemuxerExpectations(&streams
);
436 SetRendererExpectations();
438 StartPipelineAndExpect(PIPELINE_OK
);
439 EXPECT_TRUE(metadata_
.has_audio
);
440 EXPECT_FALSE(metadata_
.has_video
);
443 TEST_F(PipelineTest
, VideoStream
) {
445 MockDemuxerStreamVector streams
;
446 streams
.push_back(video_stream());
448 SetDemuxerExpectations(&streams
);
449 SetRendererExpectations();
451 StartPipelineAndExpect(PIPELINE_OK
);
452 EXPECT_FALSE(metadata_
.has_audio
);
453 EXPECT_TRUE(metadata_
.has_video
);
456 TEST_F(PipelineTest
, AudioVideoStream
) {
459 MockDemuxerStreamVector streams
;
460 streams
.push_back(audio_stream());
461 streams
.push_back(video_stream());
463 SetDemuxerExpectations(&streams
);
464 SetRendererExpectations();
466 StartPipelineAndExpect(PIPELINE_OK
);
467 EXPECT_TRUE(metadata_
.has_audio
);
468 EXPECT_TRUE(metadata_
.has_video
);
471 TEST_F(PipelineTest
, VideoTextStream
) {
474 MockDemuxerStreamVector streams
;
475 streams
.push_back(video_stream());
477 SetDemuxerExpectations(&streams
);
478 SetRendererExpectations();
480 StartPipelineAndExpect(PIPELINE_OK
);
481 EXPECT_FALSE(metadata_
.has_audio
);
482 EXPECT_TRUE(metadata_
.has_video
);
487 TEST_F(PipelineTest
, VideoAudioTextStream
) {
491 MockDemuxerStreamVector streams
;
492 streams
.push_back(video_stream());
493 streams
.push_back(audio_stream());
495 SetDemuxerExpectations(&streams
);
496 SetRendererExpectations();
498 StartPipelineAndExpect(PIPELINE_OK
);
499 EXPECT_TRUE(metadata_
.has_audio
);
500 EXPECT_TRUE(metadata_
.has_video
);
505 TEST_F(PipelineTest
, Seek
) {
509 MockDemuxerStreamVector streams
;
510 streams
.push_back(audio_stream());
511 streams
.push_back(video_stream());
513 SetDemuxerExpectations(&streams
, base::TimeDelta::FromSeconds(3000));
514 SetRendererExpectations();
516 // Initialize then seek!
517 StartPipelineAndExpect(PIPELINE_OK
);
519 // Every filter should receive a call to Seek().
520 base::TimeDelta expected
= base::TimeDelta::FromSeconds(2000);
521 ExpectSeek(expected
, false);
525 TEST_F(PipelineTest
, SeekAfterError
) {
527 MockDemuxerStreamVector streams
;
528 streams
.push_back(audio_stream());
530 SetDemuxerExpectations(&streams
, base::TimeDelta::FromSeconds(3000));
531 SetRendererExpectations();
533 // Initialize then seek!
534 StartPipelineAndExpect(PIPELINE_OK
);
536 EXPECT_CALL(*demuxer_
, Stop());
537 EXPECT_CALL(callbacks_
, OnError(_
));
539 static_cast<DemuxerHost
*>(pipeline_
.get())
540 ->OnDemuxerError(PIPELINE_ERROR_ABORT
);
541 message_loop_
.RunUntilIdle();
544 base::TimeDelta::FromMilliseconds(100),
545 base::Bind(&CallbackHelper::OnSeek
, base::Unretained(&callbacks_
)));
546 message_loop_
.RunUntilIdle();
549 TEST_F(PipelineTest
, SetVolume
) {
551 MockDemuxerStreamVector streams
;
552 streams
.push_back(audio_stream());
554 SetDemuxerExpectations(&streams
);
555 SetRendererExpectations();
557 // The audio renderer should receive a call to SetVolume().
558 float expected
= 0.5f
;
559 EXPECT_CALL(*renderer_
, SetVolume(expected
));
561 // Initialize then set volume!
562 StartPipelineAndExpect(PIPELINE_OK
);
563 pipeline_
->SetVolume(expected
);
566 TEST_F(PipelineTest
, Properties
) {
568 MockDemuxerStreamVector streams
;
569 streams
.push_back(video_stream());
571 const base::TimeDelta kDuration
= base::TimeDelta::FromSeconds(100);
572 SetDemuxerExpectations(&streams
, kDuration
);
573 SetRendererExpectations();
575 StartPipelineAndExpect(PIPELINE_OK
);
576 EXPECT_EQ(kDuration
.ToInternalValue(),
577 pipeline_
->GetMediaDuration().ToInternalValue());
578 EXPECT_FALSE(pipeline_
->DidLoadingProgress());
581 TEST_F(PipelineTest
, GetBufferedTimeRanges
) {
583 MockDemuxerStreamVector streams
;
584 streams
.push_back(video_stream());
586 const base::TimeDelta kDuration
= base::TimeDelta::FromSeconds(100);
587 SetDemuxerExpectations(&streams
, kDuration
);
588 SetRendererExpectations();
590 StartPipelineAndExpect(PIPELINE_OK
);
592 EXPECT_EQ(0u, pipeline_
->GetBufferedTimeRanges().size());
594 EXPECT_FALSE(pipeline_
->DidLoadingProgress());
595 pipeline_
->AddBufferedTimeRange(base::TimeDelta(), kDuration
/ 8);
596 EXPECT_TRUE(pipeline_
->DidLoadingProgress());
597 EXPECT_FALSE(pipeline_
->DidLoadingProgress());
598 EXPECT_EQ(1u, pipeline_
->GetBufferedTimeRanges().size());
599 EXPECT_EQ(base::TimeDelta(), pipeline_
->GetBufferedTimeRanges().start(0));
600 EXPECT_EQ(kDuration
/ 8, pipeline_
->GetBufferedTimeRanges().end(0));
602 base::TimeDelta kSeekTime
= kDuration
/ 2;
603 ExpectSeek(kSeekTime
, false);
606 EXPECT_FALSE(pipeline_
->DidLoadingProgress());
609 TEST_F(PipelineTest
, EndedCallback
) {
613 MockDemuxerStreamVector streams
;
614 streams
.push_back(audio_stream());
615 streams
.push_back(video_stream());
617 SetDemuxerExpectations(&streams
);
618 SetRendererExpectations();
619 StartPipelineAndExpect(PIPELINE_OK
);
623 // The ended callback shouldn't run until all renderers have ended.
625 message_loop_
.RunUntilIdle();
627 EXPECT_CALL(callbacks_
, OnEnded());
628 // Since the |ended_cb_| is manually invoked above, the duration does not
629 // match the expected duration and is updated upon ended.
630 EXPECT_CALL(callbacks_
, OnDurationChange());
631 text_stream()->SendEosNotification();
632 message_loop_
.RunUntilIdle();
635 TEST_F(PipelineTest
, ErrorDuringSeek
) {
637 MockDemuxerStreamVector streams
;
638 streams
.push_back(audio_stream());
640 SetDemuxerExpectations(&streams
);
641 SetRendererExpectations();
642 StartPipelineAndExpect(PIPELINE_OK
);
644 float playback_rate
= 1.0f
;
645 EXPECT_CALL(*renderer_
, SetPlaybackRate(playback_rate
));
646 pipeline_
->SetPlaybackRate(playback_rate
);
647 message_loop_
.RunUntilIdle();
649 base::TimeDelta seek_time
= base::TimeDelta::FromSeconds(5);
651 EXPECT_CALL(callbacks_
, OnBufferingStateChange(BUFFERING_HAVE_NOTHING
));
652 EXPECT_CALL(*renderer_
, Flush(_
))
653 .WillOnce(DoAll(SetBufferingState(&buffering_state_cb_
,
654 BUFFERING_HAVE_NOTHING
),
657 EXPECT_CALL(*demuxer_
, Seek(seek_time
, _
))
658 .WillOnce(RunCallback
<1>(PIPELINE_ERROR_READ
));
659 EXPECT_CALL(*demuxer_
, Stop());
661 pipeline_
->Seek(seek_time
, base::Bind(&CallbackHelper::OnSeek
,
662 base::Unretained(&callbacks_
)));
663 EXPECT_CALL(callbacks_
, OnSeek(PIPELINE_ERROR_READ
));
664 message_loop_
.RunUntilIdle();
667 // Invoked function OnError. This asserts that the pipeline does not enqueue
668 // non-teardown related tasks while tearing down.
669 static void TestNoCallsAfterError(
670 Pipeline
* pipeline
, base::MessageLoop
* message_loop
,
671 PipelineStatus
/* status */) {
675 // When we get to this stage, the message loop should be empty.
676 EXPECT_TRUE(message_loop
->IsIdleForTesting());
678 // Make calls on pipeline after error has occurred.
679 pipeline
->SetPlaybackRate(0.5f
);
680 pipeline
->SetVolume(0.5f
);
682 // No additional tasks should be queued as a result of these calls.
683 EXPECT_TRUE(message_loop
->IsIdleForTesting());
686 TEST_F(PipelineTest
, NoMessageDuringTearDownFromError
) {
688 MockDemuxerStreamVector streams
;
689 streams
.push_back(audio_stream());
691 SetDemuxerExpectations(&streams
);
692 SetRendererExpectations();
693 StartPipelineAndExpect(PIPELINE_OK
);
695 // Trigger additional requests on the pipeline during tear down from error.
696 base::Callback
<void(PipelineStatus
)> cb
= base::Bind(
697 &TestNoCallsAfterError
, pipeline_
.get(), &message_loop_
);
698 ON_CALL(callbacks_
, OnError(_
))
699 .WillByDefault(Invoke(&cb
, &base::Callback
<void(PipelineStatus
)>::Run
));
701 base::TimeDelta seek_time
= base::TimeDelta::FromSeconds(5);
703 // Seek() isn't called as the demuxer errors out first.
704 EXPECT_CALL(*renderer_
, Flush(_
))
705 .WillOnce(DoAll(SetBufferingState(&buffering_state_cb_
,
706 BUFFERING_HAVE_NOTHING
),
708 EXPECT_CALL(callbacks_
, OnBufferingStateChange(BUFFERING_HAVE_NOTHING
));
710 EXPECT_CALL(*demuxer_
, Seek(seek_time
, _
))
711 .WillOnce(RunCallback
<1>(PIPELINE_ERROR_READ
));
712 EXPECT_CALL(*demuxer_
, Stop());
714 pipeline_
->Seek(seek_time
, base::Bind(&CallbackHelper::OnSeek
,
715 base::Unretained(&callbacks_
)));
716 EXPECT_CALL(callbacks_
, OnSeek(PIPELINE_ERROR_READ
));
717 message_loop_
.RunUntilIdle();
720 TEST_F(PipelineTest
, DestroyAfterStop
) {
722 MockDemuxerStreamVector streams
;
723 streams
.push_back(audio_stream());
724 SetDemuxerExpectations(&streams
);
725 SetRendererExpectations();
726 StartPipelineAndExpect(PIPELINE_OK
);
730 ExpectPipelineStopAndDestroyPipeline();
732 base::Bind(&CallbackHelper::OnStop
, base::Unretained(&callbacks_
)));
733 message_loop_
.RunUntilIdle();
736 TEST_F(PipelineTest
, Underflow
) {
739 MockDemuxerStreamVector streams
;
740 streams
.push_back(audio_stream());
741 streams
.push_back(video_stream());
743 SetDemuxerExpectations(&streams
);
744 SetRendererExpectations();
745 StartPipelineAndExpect(PIPELINE_OK
);
747 // Simulate underflow.
748 EXPECT_CALL(callbacks_
, OnBufferingStateChange(BUFFERING_HAVE_NOTHING
));
749 buffering_state_cb_
.Run(BUFFERING_HAVE_NOTHING
);
751 // Seek while underflowed.
752 base::TimeDelta expected
= base::TimeDelta::FromSeconds(5);
753 ExpectSeek(expected
, true);
757 TEST_F(PipelineTest
, PositiveStartTime
) {
758 start_time_
= base::TimeDelta::FromSeconds(1);
759 EXPECT_CALL(*demuxer_
, GetStartTime()).WillRepeatedly(Return(start_time_
));
761 MockDemuxerStreamVector streams
;
762 streams
.push_back(audio_stream());
763 SetDemuxerExpectations(&streams
);
764 SetRendererExpectations();
765 StartPipelineAndExpect(PIPELINE_OK
);
767 ExpectPipelineStopAndDestroyPipeline();
769 base::Bind(&CallbackHelper::OnStop
, base::Unretained(&callbacks_
)));
770 message_loop_
.RunUntilIdle();
773 class PipelineTeardownTest
: public PipelineTest
{
789 PipelineTeardownTest() {}
790 ~PipelineTeardownTest() override
{}
792 void RunTest(TeardownState state
, StopOrError stop_or_error
) {
796 DoInitialize(state
, stop_or_error
);
801 DoInitialize(state
, stop_or_error
);
802 DoSeek(state
, stop_or_error
);
806 DoInitialize(state
, stop_or_error
);
807 DoStopOrError(stop_or_error
);
813 // TODO(scherkus): We do radically different things whether teardown is
814 // invoked via stop vs error. The teardown path should be the same,
815 // see http://crbug.com/110228
816 void DoInitialize(TeardownState state
, StopOrError stop_or_error
) {
817 PipelineStatus expected_status
=
818 SetInitializeExpectations(state
, stop_or_error
);
820 EXPECT_CALL(callbacks_
, OnStart(expected_status
));
822 message_loop_
.RunUntilIdle();
825 PipelineStatus
SetInitializeExpectations(TeardownState state
,
826 StopOrError stop_or_error
) {
827 PipelineStatus status
= PIPELINE_OK
;
828 base::Closure stop_cb
= base::Bind(
829 &CallbackHelper::OnStop
, base::Unretained(&callbacks_
));
831 if (state
== kInitDemuxer
) {
832 if (stop_or_error
== kStop
) {
833 EXPECT_CALL(*demuxer_
, Initialize(_
, _
, _
))
834 .WillOnce(DoAll(Stop(pipeline_
.get(), stop_cb
),
835 PostCallback
<1>(PIPELINE_OK
)));
836 ExpectPipelineStopAndDestroyPipeline();
838 status
= DEMUXER_ERROR_COULD_NOT_OPEN
;
839 EXPECT_CALL(*demuxer_
, Initialize(_
, _
, _
))
840 .WillOnce(PostCallback
<1>(status
));
843 EXPECT_CALL(*demuxer_
, Stop());
849 MockDemuxerStreamVector streams
;
850 streams
.push_back(audio_stream());
851 streams
.push_back(video_stream());
852 SetDemuxerExpectations(&streams
, base::TimeDelta::FromSeconds(3000));
854 EXPECT_CALL(*renderer_
, HasAudio()).WillRepeatedly(Return(true));
855 EXPECT_CALL(*renderer_
, HasVideo()).WillRepeatedly(Return(true));
857 if (state
== kInitRenderer
) {
858 if (stop_or_error
== kStop
) {
859 EXPECT_CALL(*renderer_
, Initialize(_
, _
, _
, _
, _
, _
, _
))
860 .WillOnce(DoAll(Stop(pipeline_
.get(), stop_cb
),
861 PostCallback
<1>(PIPELINE_OK
)));
862 ExpectPipelineStopAndDestroyPipeline();
864 status
= PIPELINE_ERROR_INITIALIZATION_FAILED
;
865 EXPECT_CALL(*renderer_
, Initialize(_
, _
, _
, _
, _
, _
, _
))
866 .WillOnce(PostCallback
<1>(status
));
869 EXPECT_CALL(*demuxer_
, Stop());
873 EXPECT_CALL(*renderer_
, Initialize(_
, _
, _
, _
, _
, _
, _
))
874 .WillOnce(DoAll(SaveArg
<3>(&buffering_state_cb_
),
875 PostCallback
<1>(PIPELINE_OK
)));
877 EXPECT_CALL(callbacks_
, OnMetadata(_
));
879 // If we get here it's a successful initialization.
880 EXPECT_CALL(*renderer_
, SetPlaybackRate(0.0f
));
881 EXPECT_CALL(*renderer_
, SetVolume(1.0f
));
882 EXPECT_CALL(*renderer_
, StartPlayingFrom(base::TimeDelta()))
883 .WillOnce(SetBufferingState(&buffering_state_cb_
,
884 BUFFERING_HAVE_ENOUGH
));
886 if (status
== PIPELINE_OK
)
887 EXPECT_CALL(callbacks_
, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH
));
892 void DoSeek(TeardownState state
, StopOrError stop_or_error
) {
894 PipelineStatus status
= SetSeekExpectations(state
, stop_or_error
);
896 EXPECT_CALL(*demuxer_
, Stop());
897 EXPECT_CALL(callbacks_
, OnSeek(status
));
899 if (status
== PIPELINE_OK
) {
900 ExpectPipelineStopAndDestroyPipeline();
903 pipeline_
->Seek(base::TimeDelta::FromSeconds(10), base::Bind(
904 &CallbackHelper::OnSeek
, base::Unretained(&callbacks_
)));
905 message_loop_
.RunUntilIdle();
908 PipelineStatus
SetSeekExpectations(TeardownState state
,
909 StopOrError stop_or_error
) {
910 PipelineStatus status
= PIPELINE_OK
;
911 base::Closure stop_cb
= base::Bind(
912 &CallbackHelper::OnStop
, base::Unretained(&callbacks_
));
914 if (state
== kFlushing
) {
915 if (stop_or_error
== kStop
) {
916 EXPECT_CALL(*renderer_
, Flush(_
))
917 .WillOnce(DoAll(Stop(pipeline_
.get(), stop_cb
),
918 SetBufferingState(&buffering_state_cb_
,
919 BUFFERING_HAVE_NOTHING
),
921 EXPECT_CALL(callbacks_
, OnBufferingStateChange(BUFFERING_HAVE_NOTHING
));
923 status
= PIPELINE_ERROR_READ
;
924 EXPECT_CALL(*renderer_
, Flush(_
))
925 .WillOnce(DoAll(SetError(pipeline_
.get(), status
),
926 SetBufferingState(&buffering_state_cb_
,
927 BUFFERING_HAVE_NOTHING
),
929 EXPECT_CALL(callbacks_
, OnBufferingStateChange(BUFFERING_HAVE_NOTHING
));
935 EXPECT_CALL(*renderer_
, Flush(_
))
936 .WillOnce(DoAll(SetBufferingState(&buffering_state_cb_
,
937 BUFFERING_HAVE_NOTHING
),
939 EXPECT_CALL(callbacks_
, OnBufferingStateChange(BUFFERING_HAVE_NOTHING
));
941 if (state
== kSeeking
) {
942 if (stop_or_error
== kStop
) {
943 EXPECT_CALL(*demuxer_
, Seek(_
, _
))
944 .WillOnce(DoAll(Stop(pipeline_
.get(), stop_cb
),
945 RunCallback
<1>(PIPELINE_OK
)));
947 status
= PIPELINE_ERROR_READ
;
948 EXPECT_CALL(*demuxer_
, Seek(_
, _
))
949 .WillOnce(RunCallback
<1>(status
));
955 NOTREACHED() << "State not supported: " << state
;
959 void DoStopOrError(StopOrError stop_or_error
) {
962 EXPECT_CALL(*demuxer_
, Stop());
964 switch (stop_or_error
) {
966 ExpectPipelineStopAndDestroyPipeline();
967 pipeline_
->Stop(base::Bind(
968 &CallbackHelper::OnStop
, base::Unretained(&callbacks_
)));
972 EXPECT_CALL(callbacks_
, OnError(PIPELINE_ERROR_READ
));
973 pipeline_
->SetErrorForTesting(PIPELINE_ERROR_READ
);
977 EXPECT_CALL(callbacks_
, OnError(PIPELINE_ERROR_READ
));
978 ExpectPipelineStopAndDestroyPipeline();
979 pipeline_
->SetErrorForTesting(PIPELINE_ERROR_READ
);
980 message_loop_
.RunUntilIdle();
981 pipeline_
->Stop(base::Bind(
982 &CallbackHelper::OnStop
, base::Unretained(&callbacks_
)));
986 message_loop_
.RunUntilIdle();
989 DISALLOW_COPY_AND_ASSIGN(PipelineTeardownTest
);
992 #define INSTANTIATE_TEARDOWN_TEST(stop_or_error, state) \
993 TEST_F(PipelineTeardownTest, stop_or_error##_##state) { \
994 RunTest(k##state, k##stop_or_error); \
997 INSTANTIATE_TEARDOWN_TEST(Stop
, InitDemuxer
);
998 INSTANTIATE_TEARDOWN_TEST(Stop
, InitRenderer
);
999 INSTANTIATE_TEARDOWN_TEST(Stop
, Flushing
);
1000 INSTANTIATE_TEARDOWN_TEST(Stop
, Seeking
);
1001 INSTANTIATE_TEARDOWN_TEST(Stop
, Playing
);
1003 INSTANTIATE_TEARDOWN_TEST(Error
, InitDemuxer
);
1004 INSTANTIATE_TEARDOWN_TEST(Error
, InitRenderer
);
1005 INSTANTIATE_TEARDOWN_TEST(Error
, Flushing
);
1006 INSTANTIATE_TEARDOWN_TEST(Error
, Seeking
);
1007 INSTANTIATE_TEARDOWN_TEST(Error
, Playing
);
1009 INSTANTIATE_TEARDOWN_TEST(ErrorAndStop
, Playing
);
1011 } // namespace media