cc: Remove tile and scale specific code from raster source
[chromium-blink-merge.git] / media / base / pipeline_unittest.cc
blob354b51e8efade50dadbd2ac21f90123d5bb4eb64
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.
5 #include <vector>
7 #include "base/bind.h"
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"
25 using ::testing::_;
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;
40 namespace media {
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 {
72 public:
73 // Used for setting expectations on pipeline callbacks. Using a StrictMock
74 // also lets us test for missing callbacks.
75 class CallbackHelper {
76 public:
77 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());
90 private:
91 DISALLOW_COPY_AND_ASSIGN(CallbackHelper);
94 PipelineTest()
95 : pipeline_(new Pipeline(message_loop_.message_loop_proxy(),
96 new MediaLog())),
97 demuxer_(new StrictMock<MockDemuxer>()),
98 scoped_renderer_(new StrictMock<MockRenderer>()),
99 renderer_(scoped_renderer_.get()) {
100 // SetDemuxerExpectations() adds overriding expectations for expected
101 // non-NULL streams.
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())
117 return;
119 ExpectDemuxerStop();
121 // The mock demuxer doesn't stop the fake text track stream,
122 // so just stop it manually.
123 if (text_stream_) {
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);
141 protected:
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() {
190 pipeline_->Start(
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));
220 StartPipeline();
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),
258 RunClosure<0>()));
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());
282 pipeline_.reset();
285 void ExpectDemuxerStop() {
286 if (demuxer_)
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());
306 // Fixture members.
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_;
326 private:
327 DISALLOW_COPY_AND_ASSIGN(PipelineTest);
330 // Test that playback controls methods no-op when the pipeline hasn't been
331 // started.
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
362 // never executed.
363 StartPipeline();
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();
375 pipeline_->Stop(
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(_));
386 StartPipeline();
388 // Expect a stop callback if we were started.
389 ExpectPipelineStopAndDestroyPipeline();
390 pipeline_->Stop(
391 base::Bind(&CallbackHelper::OnStop, base::Unretained(&callbacks_)));
392 message_loop_.RunUntilIdle();
395 TEST_F(PipelineTest, DemuxerErrorDuringStop) {
396 CreateAudioStream();
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();
409 pipeline_->Stop(
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) {
431 CreateAudioStream();
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) {
444 CreateVideoStream();
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) {
457 CreateAudioStream();
458 CreateVideoStream();
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) {
472 CreateVideoStream();
473 CreateTextStream();
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);
484 AddTextStream();
487 TEST_F(PipelineTest, VideoAudioTextStream) {
488 CreateVideoStream();
489 CreateAudioStream();
490 CreateTextStream();
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);
502 AddTextStream();
505 TEST_F(PipelineTest, Seek) {
506 CreateAudioStream();
507 CreateVideoStream();
508 CreateTextStream();
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);
522 DoSeek(expected);
525 TEST_F(PipelineTest, SeekAfterError) {
526 CreateAudioStream();
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();
543 pipeline_->Seek(
544 base::TimeDelta::FromMilliseconds(100),
545 base::Bind(&CallbackHelper::OnSeek, base::Unretained(&callbacks_)));
546 message_loop_.RunUntilIdle();
549 TEST_F(PipelineTest, SetVolume) {
550 CreateAudioStream();
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) {
567 CreateVideoStream();
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) {
582 CreateVideoStream();
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);
604 DoSeek(kSeekTime);
606 EXPECT_FALSE(pipeline_->DidLoadingProgress());
609 TEST_F(PipelineTest, EndedCallback) {
610 CreateAudioStream();
611 CreateVideoStream();
612 CreateTextStream();
613 MockDemuxerStreamVector streams;
614 streams.push_back(audio_stream());
615 streams.push_back(video_stream());
617 SetDemuxerExpectations(&streams);
618 SetRendererExpectations();
619 StartPipelineAndExpect(PIPELINE_OK);
621 AddTextStream();
623 // The ended callback shouldn't run until all renderers have ended.
624 ended_cb_.Run();
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) {
636 CreateAudioStream();
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),
655 RunClosure<0>()));
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 */) {
672 CHECK(pipeline);
673 CHECK(message_loop);
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) {
687 CreateAudioStream();
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),
707 RunClosure<0>()));
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) {
721 CreateAudioStream();
722 MockDemuxerStreamVector streams;
723 streams.push_back(audio_stream());
724 SetDemuxerExpectations(&streams);
725 SetRendererExpectations();
726 StartPipelineAndExpect(PIPELINE_OK);
728 ExpectDemuxerStop();
730 ExpectPipelineStopAndDestroyPipeline();
731 pipeline_->Stop(
732 base::Bind(&CallbackHelper::OnStop, base::Unretained(&callbacks_)));
733 message_loop_.RunUntilIdle();
736 TEST_F(PipelineTest, Underflow) {
737 CreateAudioStream();
738 CreateVideoStream();
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);
754 DoSeek(expected);
757 TEST_F(PipelineTest, PositiveStartTime) {
758 start_time_ = base::TimeDelta::FromSeconds(1);
759 EXPECT_CALL(*demuxer_, GetStartTime()).WillRepeatedly(Return(start_time_));
760 CreateAudioStream();
761 MockDemuxerStreamVector streams;
762 streams.push_back(audio_stream());
763 SetDemuxerExpectations(&streams);
764 SetRendererExpectations();
765 StartPipelineAndExpect(PIPELINE_OK);
766 ExpectDemuxerStop();
767 ExpectPipelineStopAndDestroyPipeline();
768 pipeline_->Stop(
769 base::Bind(&CallbackHelper::OnStop, base::Unretained(&callbacks_)));
770 message_loop_.RunUntilIdle();
773 class PipelineTeardownTest : public PipelineTest {
774 public:
775 enum TeardownState {
776 kInitDemuxer,
777 kInitRenderer,
778 kFlushing,
779 kSeeking,
780 kPlaying,
783 enum StopOrError {
784 kStop,
785 kError,
786 kErrorAndStop,
789 PipelineTeardownTest() {}
790 ~PipelineTeardownTest() override {}
792 void RunTest(TeardownState state, StopOrError stop_or_error) {
793 switch (state) {
794 case kInitDemuxer:
795 case kInitRenderer:
796 DoInitialize(state, stop_or_error);
797 break;
799 case kFlushing:
800 case kSeeking:
801 DoInitialize(state, stop_or_error);
802 DoSeek(state, stop_or_error);
803 break;
805 case kPlaying:
806 DoInitialize(state, stop_or_error);
807 DoStopOrError(stop_or_error);
808 break;
812 private:
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));
821 StartPipeline();
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();
837 } else {
838 status = DEMUXER_ERROR_COULD_NOT_OPEN;
839 EXPECT_CALL(*demuxer_, Initialize(_, _, _))
840 .WillOnce(PostCallback<1>(status));
843 EXPECT_CALL(*demuxer_, Stop());
844 return status;
847 CreateAudioStream();
848 CreateVideoStream();
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();
863 } else {
864 status = PIPELINE_ERROR_INITIALIZATION_FAILED;
865 EXPECT_CALL(*renderer_, Initialize(_, _, _, _, _, _, _))
866 .WillOnce(PostCallback<1>(status));
869 EXPECT_CALL(*demuxer_, Stop());
870 return status;
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));
889 return status;
892 void DoSeek(TeardownState state, StopOrError stop_or_error) {
893 InSequence s;
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),
920 RunClosure<0>()));
921 EXPECT_CALL(callbacks_, OnBufferingStateChange(BUFFERING_HAVE_NOTHING));
922 } else {
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),
928 RunClosure<0>()));
929 EXPECT_CALL(callbacks_, OnBufferingStateChange(BUFFERING_HAVE_NOTHING));
932 return status;
935 EXPECT_CALL(*renderer_, Flush(_))
936 .WillOnce(DoAll(SetBufferingState(&buffering_state_cb_,
937 BUFFERING_HAVE_NOTHING),
938 RunClosure<0>()));
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)));
946 } else {
947 status = PIPELINE_ERROR_READ;
948 EXPECT_CALL(*demuxer_, Seek(_, _))
949 .WillOnce(RunCallback<1>(status));
952 return status;
955 NOTREACHED() << "State not supported: " << state;
956 return status;
959 void DoStopOrError(StopOrError stop_or_error) {
960 InSequence s;
962 EXPECT_CALL(*demuxer_, Stop());
964 switch (stop_or_error) {
965 case kStop:
966 ExpectPipelineStopAndDestroyPipeline();
967 pipeline_->Stop(base::Bind(
968 &CallbackHelper::OnStop, base::Unretained(&callbacks_)));
969 break;
971 case kError:
972 EXPECT_CALL(callbacks_, OnError(PIPELINE_ERROR_READ));
973 pipeline_->SetErrorForTesting(PIPELINE_ERROR_READ);
974 break;
976 case kErrorAndStop:
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_)));
983 break;
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