OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/test/simple_test_tick_clock.h" | 10 #include "base/test/simple_test_tick_clock.h" |
11 #include "media/base/gmock_callback_support.h" | 11 #include "media/base/gmock_callback_support.h" |
12 #include "media/base/mock_filters.h" | 12 #include "media/base/mock_filters.h" |
13 #include "media/base/test_helpers.h" | 13 #include "media/base/test_helpers.h" |
| 14 #include "media/base/time_delta_interpolator.h" |
14 #include "media/filters/renderer_impl.h" | 15 #include "media/filters/renderer_impl.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 using ::testing::_; | 18 using ::testing::_; |
18 using ::testing::DoAll; | 19 using ::testing::DoAll; |
19 using ::testing::InSequence; | 20 using ::testing::InSequence; |
20 using ::testing::Mock; | 21 using ::testing::Mock; |
21 using ::testing::Return; | 22 using ::testing::Return; |
22 using ::testing::SaveArg; | 23 using ::testing::SaveArg; |
23 using ::testing::StrictMock; | 24 using ::testing::StrictMock; |
24 | 25 |
25 namespace media { | 26 namespace media { |
26 | 27 |
27 const int64 kStartPlayingTimeInMs = 100; | 28 const int64 kStartPlayingTimeInMs = 100; |
| 29 const int64 kDurationInMs = 3000; |
| 30 const int64 kAudioUpdateTimeMs = 150; |
| 31 const int64 kAudioUpdateMaxTimeMs = 1000; |
28 | 32 |
29 ACTION_P2(SetBufferingState, cb, buffering_state) { | 33 ACTION_P2(SetBufferingState, cb, buffering_state) { |
30 cb->Run(buffering_state); | 34 cb->Run(buffering_state); |
31 } | 35 } |
32 | 36 |
| 37 ACTION_P3(UpdateAudioTime, cb, time, max_time) { |
| 38 cb->Run(base::TimeDelta::FromMilliseconds(time), |
| 39 base::TimeDelta::FromMilliseconds(max_time)); |
| 40 } |
| 41 |
33 ACTION_P2(AudioError, cb, error) { | 42 ACTION_P2(AudioError, cb, error) { |
34 cb->Run(error); | 43 cb->Run(error); |
35 } | 44 } |
36 | 45 |
| 46 static base::TimeDelta GetDuration() { |
| 47 return base::TimeDelta::FromMilliseconds(kDurationInMs); |
| 48 } |
| 49 |
37 class RendererImplTest : public ::testing::Test { | 50 class RendererImplTest : public ::testing::Test { |
38 public: | 51 public: |
39 // Used for setting expectations on pipeline callbacks. Using a StrictMock | 52 // Used for setting expectations on pipeline callbacks. Using a StrictMock |
40 // also lets us test for missing callbacks. | 53 // also lets us test for missing callbacks. |
41 class CallbackHelper { | 54 class CallbackHelper { |
42 public: | 55 public: |
43 CallbackHelper() {} | 56 CallbackHelper() {} |
44 virtual ~CallbackHelper() {} | 57 virtual ~CallbackHelper() {} |
45 | 58 |
46 MOCK_METHOD0(OnInitialize, void()); | 59 MOCK_METHOD0(OnInitialize, void()); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 scoped_ptr<StrictMock<MockDemuxerStream> > CreateStream( | 96 scoped_ptr<StrictMock<MockDemuxerStream> > CreateStream( |
84 DemuxerStream::Type type) { | 97 DemuxerStream::Type type) { |
85 scoped_ptr<StrictMock<MockDemuxerStream> > stream( | 98 scoped_ptr<StrictMock<MockDemuxerStream> > stream( |
86 new StrictMock<MockDemuxerStream>(type)); | 99 new StrictMock<MockDemuxerStream>(type)); |
87 return stream.Pass(); | 100 return stream.Pass(); |
88 } | 101 } |
89 | 102 |
90 // Sets up expectations to allow the audio renderer to initialize. | 103 // Sets up expectations to allow the audio renderer to initialize. |
91 void SetAudioRendererInitializeExpectations(PipelineStatus status) { | 104 void SetAudioRendererInitializeExpectations(PipelineStatus status) { |
92 EXPECT_CALL(*audio_renderer_, | 105 EXPECT_CALL(*audio_renderer_, |
93 Initialize(audio_stream_.get(), _, _, _, _, _)) | 106 Initialize(audio_stream_.get(), _, _, _, _, _, _)) |
94 .WillOnce(DoAll(SaveArg<3>(&audio_buffering_state_cb_), | 107 .WillOnce(DoAll(SaveArg<3>(&audio_time_cb_), |
95 SaveArg<4>(&audio_ended_cb_), | 108 SaveArg<4>(&audio_buffering_state_cb_), |
96 SaveArg<5>(&audio_error_cb_), | 109 SaveArg<5>(&audio_ended_cb_), |
| 110 SaveArg<6>(&audio_error_cb_), |
97 RunCallback<1>(status))); | 111 RunCallback<1>(status))); |
| 112 if (status == PIPELINE_OK) { |
| 113 EXPECT_CALL(*audio_renderer_, GetTimeSource()) |
| 114 .WillOnce(Return(&time_source_)); |
| 115 } |
98 } | 116 } |
99 | 117 |
100 // Sets up expectations to allow the video renderer to initialize. | 118 // Sets up expectations to allow the video renderer to initialize. |
101 void SetVideoRendererInitializeExpectations(PipelineStatus status) { | 119 void SetVideoRendererInitializeExpectations(PipelineStatus status) { |
102 EXPECT_CALL(*video_renderer_, | 120 EXPECT_CALL(*video_renderer_, |
103 Initialize(video_stream_.get(), _, _, _, _, _, _, _)) | 121 Initialize(video_stream_.get(), _, _, _, _, _, _, _, _, _)) |
104 .WillOnce(DoAll(SaveArg<4>(&video_buffering_state_cb_), | 122 .WillOnce(DoAll(SaveArg<5>(&video_buffering_state_cb_), |
105 SaveArg<5>(&video_ended_cb_), | 123 SaveArg<6>(&video_ended_cb_), |
106 RunCallback<2>(status))); | 124 RunCallback<2>(status))); |
107 } | 125 } |
108 | 126 |
109 void InitializeAndExpect(PipelineStatus start_status) { | 127 void InitializeAndExpect(PipelineStatus start_status) { |
110 if (start_status != PIPELINE_OK) | 128 if (start_status != PIPELINE_OK) |
111 EXPECT_CALL(callbacks_, OnError(start_status)); | 129 EXPECT_CALL(callbacks_, OnError(start_status)); |
112 | 130 |
113 EXPECT_CALL(callbacks_, OnInitialize()); | 131 EXPECT_CALL(callbacks_, OnInitialize()); |
114 | 132 |
115 if (start_status == PIPELINE_OK && audio_stream_) { | |
116 EXPECT_CALL(*audio_renderer_, GetTimeSource()) | |
117 .WillOnce(Return(&time_source_)); | |
118 } | |
119 | |
120 renderer_impl_->Initialize( | 133 renderer_impl_->Initialize( |
121 base::Bind(&CallbackHelper::OnInitialize, | 134 base::Bind(&CallbackHelper::OnInitialize, |
122 base::Unretained(&callbacks_)), | 135 base::Unretained(&callbacks_)), |
123 base::Bind(&CallbackHelper::OnUpdateStatistics, | 136 base::Bind(&CallbackHelper::OnUpdateStatistics, |
124 base::Unretained(&callbacks_)), | 137 base::Unretained(&callbacks_)), |
125 base::Bind(&CallbackHelper::OnEnded, base::Unretained(&callbacks_)), | 138 base::Bind(&CallbackHelper::OnEnded, base::Unretained(&callbacks_)), |
126 base::Bind(&CallbackHelper::OnError, base::Unretained(&callbacks_)), | 139 base::Bind(&CallbackHelper::OnError, base::Unretained(&callbacks_)), |
127 base::Bind(&CallbackHelper::OnBufferingStateChange, | 140 base::Bind(&CallbackHelper::OnBufferingStateChange, |
128 base::Unretained(&callbacks_))); | 141 base::Unretained(&callbacks_)), |
| 142 base::Bind(&GetDuration)); |
129 base::RunLoop().RunUntilIdle(); | 143 base::RunLoop().RunUntilIdle(); |
130 } | 144 } |
131 | 145 |
132 void CreateAudioStream() { | 146 void CreateAudioStream() { |
133 audio_stream_ = CreateStream(DemuxerStream::AUDIO); | 147 audio_stream_ = CreateStream(DemuxerStream::AUDIO); |
134 streams_.push_back(audio_stream_.get()); | 148 streams_.push_back(audio_stream_.get()); |
135 EXPECT_CALL(*demuxer_, GetStream(DemuxerStream::AUDIO)) | 149 EXPECT_CALL(*demuxer_, GetStream(DemuxerStream::AUDIO)) |
136 .WillRepeatedly(Return(audio_stream_.get())); | 150 .WillRepeatedly(Return(audio_stream_.get())); |
137 } | 151 } |
138 | 152 |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 | 266 |
253 scoped_ptr<StrictMock<MockDemuxer> > demuxer_; | 267 scoped_ptr<StrictMock<MockDemuxer> > demuxer_; |
254 StrictMock<MockVideoRenderer>* video_renderer_; | 268 StrictMock<MockVideoRenderer>* video_renderer_; |
255 StrictMock<MockAudioRenderer>* audio_renderer_; | 269 StrictMock<MockAudioRenderer>* audio_renderer_; |
256 scoped_ptr<RendererImpl> renderer_impl_; | 270 scoped_ptr<RendererImpl> renderer_impl_; |
257 | 271 |
258 StrictMock<MockTimeSource> time_source_; | 272 StrictMock<MockTimeSource> time_source_; |
259 scoped_ptr<StrictMock<MockDemuxerStream> > audio_stream_; | 273 scoped_ptr<StrictMock<MockDemuxerStream> > audio_stream_; |
260 scoped_ptr<StrictMock<MockDemuxerStream> > video_stream_; | 274 scoped_ptr<StrictMock<MockDemuxerStream> > video_stream_; |
261 MockDemuxerStreamVector streams_; | 275 MockDemuxerStreamVector streams_; |
| 276 AudioRenderer::TimeCB audio_time_cb_; |
262 BufferingStateCB audio_buffering_state_cb_; | 277 BufferingStateCB audio_buffering_state_cb_; |
263 BufferingStateCB video_buffering_state_cb_; | 278 BufferingStateCB video_buffering_state_cb_; |
264 base::Closure audio_ended_cb_; | 279 base::Closure audio_ended_cb_; |
265 base::Closure video_ended_cb_; | 280 base::Closure video_ended_cb_; |
266 PipelineStatusCB audio_error_cb_; | 281 PipelineStatusCB audio_error_cb_; |
267 VideoDecoderConfig video_decoder_config_; | 282 VideoDecoderConfig video_decoder_config_; |
268 | 283 |
269 private: | 284 private: |
270 DISALLOW_COPY_AND_ASSIGN(RendererImplTest); | 285 DISALLOW_COPY_AND_ASSIGN(RendererImplTest); |
271 }; | 286 }; |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 SetPlaybackRate(1.0f); | 360 SetPlaybackRate(1.0f); |
346 SetPlaybackRate(2.0f); | 361 SetPlaybackRate(2.0f); |
347 } | 362 } |
348 | 363 |
349 TEST_F(RendererImplTest, SetVolume) { | 364 TEST_F(RendererImplTest, SetVolume) { |
350 InitializeWithAudioAndVideo(); | 365 InitializeWithAudioAndVideo(); |
351 EXPECT_CALL(*audio_renderer_, SetVolume(2.0f)); | 366 EXPECT_CALL(*audio_renderer_, SetVolume(2.0f)); |
352 renderer_impl_->SetVolume(2.0f); | 367 renderer_impl_->SetVolume(2.0f); |
353 } | 368 } |
354 | 369 |
| 370 TEST_F(RendererImplTest, GetMediaTime) { |
| 371 // Replace what's used for interpolating to simulate wall clock time. |
| 372 renderer_impl_->SetTimeDeltaInterpolatorForTesting( |
| 373 new TimeDeltaInterpolator(&test_tick_clock_)); |
| 374 |
| 375 InitializeWithAudioAndVideo(); |
| 376 Play(); |
| 377 |
| 378 EXPECT_EQ(kStartPlayingTimeInMs, GetMediaTimeMs()); |
| 379 |
| 380 // Verify that the clock doesn't advance since it hasn't been started by |
| 381 // a time update from the audio stream. |
| 382 EXPECT_FALSE(IsMediaTimeAdvancing()); |
| 383 |
| 384 // Provide an initial time update so that the pipeline transitions out of the |
| 385 // "waiting for time update" state. |
| 386 audio_time_cb_.Run(base::TimeDelta::FromMilliseconds(kAudioUpdateTimeMs), |
| 387 base::TimeDelta::FromMilliseconds(kAudioUpdateMaxTimeMs)); |
| 388 EXPECT_EQ(kAudioUpdateTimeMs, GetMediaTimeMs()); |
| 389 |
| 390 // Advance the clock so that GetMediaTime() also advances. This also verifies |
| 391 // that the default playback rate is 1. |
| 392 EXPECT_TRUE(IsMediaTimeAdvancing()); |
| 393 |
| 394 // Verify that playback rate affects the rate GetMediaTime() advances. |
| 395 SetPlaybackRate(2.0f); |
| 396 EXPECT_TRUE(IsMediaTimeAdvancing(2.0f)); |
| 397 |
| 398 // Verify that GetMediaTime() is bounded by audio max time. |
| 399 DCHECK_GT(GetMediaTimeMs() + 2000, kAudioUpdateMaxTimeMs); |
| 400 test_tick_clock_.Advance(base::TimeDelta::FromMilliseconds(2000)); |
| 401 EXPECT_EQ(kAudioUpdateMaxTimeMs, GetMediaTimeMs()); |
| 402 } |
| 403 |
| 404 TEST_F(RendererImplTest, AudioTimeUpdateDuringFlush) { |
| 405 // Replace what's used for interpolating to simulate wall clock time. |
| 406 renderer_impl_->SetTimeDeltaInterpolatorForTesting( |
| 407 new TimeDeltaInterpolator(&test_tick_clock_)); |
| 408 |
| 409 InitializeWithAudio(); |
| 410 Play(); |
| 411 |
| 412 // Provide an initial time update so that the pipeline transitions out of the |
| 413 // "waiting for time update" state. |
| 414 audio_time_cb_.Run(base::TimeDelta::FromMilliseconds(kAudioUpdateTimeMs), |
| 415 base::TimeDelta::FromMilliseconds(kAudioUpdateMaxTimeMs)); |
| 416 EXPECT_EQ(kAudioUpdateTimeMs, GetMediaTimeMs()); |
| 417 |
| 418 int64 start_time = GetMediaTimeMs(); |
| 419 |
| 420 EXPECT_CALL(*audio_renderer_, Flush(_)).WillOnce(DoAll( |
| 421 UpdateAudioTime( |
| 422 &audio_time_cb_, kAudioUpdateTimeMs + 100, kAudioUpdateMaxTimeMs), |
| 423 SetBufferingState(&audio_buffering_state_cb_, BUFFERING_HAVE_NOTHING), |
| 424 RunClosure<0>())); |
| 425 EXPECT_CALL(time_source_, StopTicking()); |
| 426 EXPECT_CALL(callbacks_, OnFlushed()); |
| 427 renderer_impl_->Flush( |
| 428 base::Bind(&CallbackHelper::OnFlushed, base::Unretained(&callbacks_))); |
| 429 |
| 430 // Audio time update during Flush() has no effect. |
| 431 EXPECT_EQ(start_time, GetMediaTimeMs()); |
| 432 |
| 433 // Verify that the clock doesn't advance since it hasn't been started by |
| 434 // a time update from the audio stream. |
| 435 EXPECT_FALSE(IsMediaTimeAdvancing()); |
| 436 } |
| 437 |
| 438 TEST_F(RendererImplTest, PostTimeUpdateDuringDestroy) { |
| 439 InitializeWithAudioAndVideo(); |
| 440 |
| 441 // Simulate the case where TimeCB is posted during ~AudioRenderer(), which is |
| 442 // triggered in ~Renderer(). |
| 443 base::TimeDelta time = base::TimeDelta::FromMilliseconds(100); |
| 444 message_loop_.PostTask(FROM_HERE, base::Bind(audio_time_cb_, time, time)); |
| 445 |
| 446 renderer_impl_.reset(); |
| 447 message_loop_.RunUntilIdle(); |
| 448 } |
| 449 |
355 TEST_F(RendererImplTest, AudioStreamEnded) { | 450 TEST_F(RendererImplTest, AudioStreamEnded) { |
356 InitializeWithAudio(); | 451 InitializeWithAudio(); |
357 Play(); | 452 Play(); |
358 | 453 |
359 EXPECT_CALL(time_source_, StopTicking()); | 454 EXPECT_CALL(time_source_, StopTicking()); |
360 EXPECT_CALL(callbacks_, OnEnded()); | 455 EXPECT_CALL(callbacks_, OnEnded()); |
361 | 456 |
362 audio_ended_cb_.Run(); | 457 audio_ended_cb_.Run(); |
363 base::RunLoop().RunUntilIdle(); | 458 base::RunLoop().RunUntilIdle(); |
364 } | 459 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 InitializeWithAudio(); | 520 InitializeWithAudio(); |
426 Play(); | 521 Play(); |
427 Flush(false); | 522 Flush(false); |
428 | 523 |
429 EXPECT_CALL(callbacks_, OnError(PIPELINE_ERROR_DECODE)); | 524 EXPECT_CALL(callbacks_, OnError(PIPELINE_ERROR_DECODE)); |
430 audio_error_cb_.Run(PIPELINE_ERROR_DECODE); | 525 audio_error_cb_.Run(PIPELINE_ERROR_DECODE); |
431 base::RunLoop().RunUntilIdle(); | 526 base::RunLoop().RunUntilIdle(); |
432 } | 527 } |
433 | 528 |
434 } // namespace media | 529 } // namespace media |
OLD | NEW |