1 // Copyright 2013 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.
7 #include "base/basictypes.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/stringprintf.h"
11 #include "media/base/android/audio_decoder_job.h"
12 #include "media/base/android/media_codec_bridge.h"
13 #include "media/base/android/media_drm_bridge.h"
14 #include "media/base/android/media_player_manager.h"
15 #include "media/base/android/media_source_player.h"
16 #include "media/base/android/media_url_interceptor.h"
17 #include "media/base/android/video_decoder_job.h"
18 #include "media/base/bind_to_current_loop.h"
19 #include "media/base/decoder_buffer.h"
20 #include "media/base/test_data_util.h"
21 #include "media/base/timestamp_constants.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "ui/gl/android/surface_texture.h"
27 // Helper macro to skip the test if MediaCodecBridge isn't available.
28 #define SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE() \
30 if (!MediaCodecBridge::IsAvailable()) { \
31 VLOG(0) << "Could not run test - not supported on device."; \
36 const base::TimeDelta kDefaultDuration
=
37 base::TimeDelta::FromMilliseconds(10000);
39 // TODO(wolenetz/qinmin): Simplify tests with more effective mock usage, and
40 // fix flaky pointer-based MDJ inequality testing. See http://crbug.com/327839.
42 // Mock of MediaPlayerManager for testing purpose.
43 class MockMediaPlayerManager
: public MediaPlayerManager
{
45 explicit MockMediaPlayerManager(base::MessageLoop
* message_loop
)
46 : message_loop_(message_loop
),
47 playback_completed_(false),
48 num_resources_requested_(0),
49 num_metadata_changes_(0),
50 timestamp_updated_(false),
52 ~MockMediaPlayerManager() override
{}
54 // MediaPlayerManager implementation.
55 MediaResourceGetter
* GetMediaResourceGetter() override
{ return NULL
; }
56 MediaUrlInterceptor
* GetMediaUrlInterceptor() override
{ return NULL
; }
57 void OnTimeUpdate(int player_id
,
58 base::TimeDelta current_time
,
59 base::TimeTicks current_time_ticks
) override
{
60 timestamp_updated_
= true;
62 void OnMediaMetadataChanged(int player_id
,
63 base::TimeDelta duration
,
66 bool success
) override
{
67 num_metadata_changes_
++;
69 void OnPlaybackComplete(int player_id
) override
{
70 playback_completed_
= true;
71 if (message_loop_
->is_running())
72 message_loop_
->Quit();
74 void OnMediaInterrupted(int player_id
) override
{}
75 void OnBufferingUpdate(int player_id
, int percentage
) override
{}
76 void OnSeekComplete(int player_id
,
77 const base::TimeDelta
& current_time
) override
{}
78 void OnError(int player_id
, int error
) override
{}
79 void OnVideoSizeChanged(int player_id
, int width
, int height
) override
{}
80 void OnWaitingForDecryptionKey(int player_id
) override
{}
81 MediaPlayerAndroid
* GetFullscreenPlayer() override
{ return NULL
; }
82 MediaPlayerAndroid
* GetPlayer(int player_id
) override
{ return NULL
; }
84 bool RequestPlay(int player_id
, base::TimeDelta duration
) override
{
88 bool playback_completed() const {
89 return playback_completed_
;
92 int num_resources_requested() const {
93 return num_resources_requested_
;
96 int num_metadata_changes() const {
97 return num_metadata_changes_
;
100 void OnMediaResourcesRequested(int player_id
) {
101 num_resources_requested_
++;
104 bool timestamp_updated() const {
105 return timestamp_updated_
;
108 void ResetTimestampUpdated() {
109 timestamp_updated_
= false;
112 void set_allow_play(bool value
) {
117 base::MessageLoop
* message_loop_
;
118 bool playback_completed_
;
119 // The number of resource requests this object has seen.
120 int num_resources_requested_
;
121 // The number of metadata changes reported by the player.
122 int num_metadata_changes_
;
123 // Playback timestamp was updated.
124 bool timestamp_updated_
;
125 // Whether the manager will allow players that request playing.
128 DISALLOW_COPY_AND_ASSIGN(MockMediaPlayerManager
);
131 class MockDemuxerAndroid
: public DemuxerAndroid
{
133 explicit MockDemuxerAndroid(base::MessageLoop
* message_loop
)
134 : message_loop_(message_loop
),
135 num_data_requests_(0),
136 num_seek_requests_(0),
137 num_browser_seek_requests_(0) {}
138 ~MockDemuxerAndroid() override
{}
140 void Initialize(DemuxerAndroidClient
* client
) override
{}
141 void RequestDemuxerData(DemuxerStream::Type type
) override
{
142 num_data_requests_
++;
143 if (message_loop_
->is_running())
144 message_loop_
->Quit();
146 void RequestDemuxerSeek(const base::TimeDelta
& time_to_seek
,
147 bool is_browser_seek
) override
{
148 num_seek_requests_
++;
150 num_browser_seek_requests_
++;
153 int num_data_requests() const { return num_data_requests_
; }
154 int num_seek_requests() const { return num_seek_requests_
; }
155 int num_browser_seek_requests() const { return num_browser_seek_requests_
; }
158 base::MessageLoop
* message_loop_
;
160 // The number of encoded data requests this object has seen.
161 int num_data_requests_
;
163 // The number of regular and browser seek requests this object has seen.
164 int num_seek_requests_
;
166 // The number of browser seek requests this object has seen.
167 int num_browser_seek_requests_
;
169 DISALLOW_COPY_AND_ASSIGN(MockDemuxerAndroid
);
172 class MediaSourcePlayerTest
: public testing::Test
{
174 MediaSourcePlayerTest()
175 : manager_(&message_loop_
),
176 demuxer_(new MockDemuxerAndroid(&message_loop_
)),
177 player_(0, &manager_
,
178 base::Bind(&MockMediaPlayerManager::OnMediaResourcesRequested
,
179 base::Unretained(&manager_
)),
180 scoped_ptr
<DemuxerAndroid
>(demuxer_
),
182 decoder_callback_hook_executed_(false),
183 surface_texture_a_is_next_(true) {}
185 ~MediaSourcePlayerTest() override
{}
188 // Get the decoder job from the MediaSourcePlayer. The return value must not
190 MediaDecoderJob
* GetMediaDecoderJob(bool is_audio
) {
192 return reinterpret_cast<MediaDecoderJob
*>(
193 player_
.audio_decoder_job_
.get());
195 return reinterpret_cast<MediaDecoderJob
*>(
196 player_
.video_decoder_job_
.get());
199 // Get the MediaCodecBridge from the decoder job. The return value could be
200 // NULL if the decoder is not yet created.
201 MediaCodecBridge
* GetMediaCodecBridge(bool is_audio
) {
203 return player_
.audio_decoder_job_
->media_codec_bridge_
.get();
204 return player_
.video_decoder_job_
->media_codec_bridge_
.get();
207 // Get the per-job prerolling status from the MediaSourcePlayer's job matching
208 // |is_audio|. Caller must guard against NPE if the player's job is NULL.
209 bool IsPrerolling(bool is_audio
) {
210 return GetMediaDecoderJob(is_audio
)->prerolling_
;
213 // Get the preroll timestamp from the MediaSourcePlayer.
214 base::TimeDelta
GetPrerollTimestamp() {
215 return player_
.preroll_timestamp_
;
218 // Simulate player has reached starvation timeout.
219 void TriggerPlayerStarvation() {
220 player_
.decoder_starvation_callback_
.Cancel();
221 player_
.OnDecoderStarved();
224 // Release() the player.
225 void ReleasePlayer() {
226 EXPECT_TRUE(player_
.IsPlaying());
228 EXPECT_FALSE(player_
.IsPlaying());
231 // Upon the next successful decode callback, post a task to call Release()
232 // on the |player_|. TEST_F's do not have access to the private player
233 // members, hence this helper method.
234 // Prevent usage creep of MSP::set_decode_callback_for_testing() by
235 // only using it for the ReleaseWithOnPrefetchDoneAlreadyPosted test.
236 void OnNextTestDecodeCallbackPostTaskToReleasePlayer() {
237 DCHECK_EQ(&message_loop_
, base::MessageLoop::current());
238 player_
.set_decode_callback_for_testing(media::BindToCurrentLoop(
240 &MediaSourcePlayerTest::ReleaseWithPendingPrefetchDoneVerification
,
241 base::Unretained(this))));
244 // Asynch test callback posted upon decode completion to verify that a pending
245 // prefetch done event is not cleared across |player_|'s Release(). This helps
246 // ensure the ReleaseWithOnPrefetchDoneAlreadyPosted test scenario is met.
247 void ReleaseWithPendingPrefetchDoneVerification() {
248 EXPECT_TRUE(player_
.IsEventPending(player_
.PREFETCH_DONE_EVENT_PENDING
));
250 EXPECT_TRUE(player_
.IsEventPending(player_
.PREFETCH_DONE_EVENT_PENDING
));
251 EXPECT_FALSE(decoder_callback_hook_executed_
);
252 EXPECT_FALSE(GetMediaCodecBridge(true));
253 decoder_callback_hook_executed_
= true;
256 DemuxerConfigs
CreateAudioDemuxerConfigs(AudioCodec audio_codec
,
257 bool use_low_sample_rate
) {
258 DemuxerConfigs configs
;
259 configs
.audio_codec
= audio_codec
;
260 configs
.audio_channels
= 2;
261 configs
.is_audio_encrypted
= false;
262 configs
.duration
= kDefaultDuration
;
264 if (audio_codec
== kCodecVorbis
) {
265 configs
.audio_sampling_rate
= use_low_sample_rate
? 11025 : 44100;
266 scoped_refptr
<DecoderBuffer
> buffer
= ReadTestDataFile(
268 configs
.audio_extra_data
= std::vector
<uint8
>(
270 buffer
->data() + buffer
->data_size());
274 // Other codecs are not yet supported by this helper.
275 EXPECT_EQ(audio_codec
, kCodecAAC
);
277 configs
.audio_sampling_rate
= 48000;
278 uint8 aac_extra_data
[] = { 0x13, 0x10 };
279 configs
.audio_extra_data
= std::vector
<uint8
>(
285 DemuxerConfigs
CreateVideoDemuxerConfigs(bool use_larger_size
) {
286 DemuxerConfigs configs
;
287 configs
.video_codec
= kCodecVP8
;
289 use_larger_size
? gfx::Size(640, 240) : gfx::Size(320, 240);
290 configs
.is_video_encrypted
= false;
291 configs
.duration
= kDefaultDuration
;
295 DemuxerConfigs
CreateAudioVideoDemuxerConfigs() {
296 DemuxerConfigs configs
= CreateAudioDemuxerConfigs(kCodecVorbis
, false);
297 configs
.video_codec
= kCodecVP8
;
298 configs
.video_size
= gfx::Size(320, 240);
299 configs
.is_video_encrypted
= false;
303 DemuxerConfigs
CreateDemuxerConfigs(bool have_audio
, bool have_video
) {
304 DCHECK(have_audio
|| have_video
);
306 if (have_audio
&& !have_video
)
307 return CreateAudioDemuxerConfigs(kCodecVorbis
, false);
309 if (have_video
&& !have_audio
)
310 return CreateVideoDemuxerConfigs(false);
312 return CreateAudioVideoDemuxerConfigs();
315 // Starts an audio decoder job.
316 void StartAudioDecoderJob() {
317 Start(CreateAudioDemuxerConfigs(kCodecVorbis
, false));
320 // Starts a video decoder job.
321 void StartVideoDecoderJob() {
322 Start(CreateVideoDemuxerConfigs(false));
325 // Starts decoding the data.
326 void Start(const DemuxerConfigs
& configs
) {
327 EXPECT_EQ(demuxer_
->num_data_requests(), 0);
328 player_
.OnDemuxerConfigsAvailable(configs
);
331 EXPECT_TRUE(player_
.IsPlaying());
332 int expected_num_requests
= (player_
.HasAudio() ? 1 : 0) +
333 (player_
.HasVideo() ? 1 : 0);
334 EXPECT_EQ(expected_num_requests
, demuxer_
->num_data_requests());
337 // Resumes decoding the data. Verifies player behavior relative to
338 // |expect_player_requests_audio_data| and
339 // |expect_player_requests_video_data|.
340 void Resume(bool expect_player_requests_audio_data
,
341 bool expect_player_requests_video_data
) {
342 EXPECT_FALSE(player_
.IsPlaying());
343 EXPECT_TRUE(player_
.HasVideo() || player_
.HasAudio());
344 int original_num_data_requests
= demuxer_
->num_data_requests();
345 int expected_request_delta
=
346 (expect_player_requests_audio_data
? 1 : 0) +
347 (expect_player_requests_video_data
? 1 : 0);
351 EXPECT_TRUE(player_
.IsPlaying());
352 EXPECT_EQ(original_num_data_requests
+ expected_request_delta
,
353 demuxer_
->num_data_requests());
356 // Keeps decoding audio data until the decoder starts to output samples.
357 // Gives up if no audio output after decoding 10 frames.
358 void DecodeAudioDataUntilOutputBecomesAvailable() {
359 EXPECT_TRUE(player_
.IsPlaying());
360 base::TimeDelta current_time
= player_
.GetCurrentTime();
361 base::TimeDelta start_timestamp
= current_time
;
362 for (int i
= 0; i
< 10; ++i
) {
363 manager_
.ResetTimestampUpdated();
364 player_
.OnDemuxerDataAvailable(
365 CreateReadFromDemuxerAckForAudio(i
> 3 ? 3 : i
));
366 WaitForAudioDecodeDone();
367 base::TimeDelta new_current_time
= player_
.GetCurrentTime();
368 EXPECT_LE(current_time
.InMilliseconds(),
369 new_current_time
.InMilliseconds());
370 current_time
= new_current_time
;
371 if (manager_
.timestamp_updated()) {
372 // TODO(qinmin): the current time is from the decoder thread and it does
373 // not take the delay from posting the task into consideration.
374 // http://crbug.com/421616.
375 EXPECT_LE(start_timestamp
.InMillisecondsF(),
376 new_current_time
.InMillisecondsF());
383 AccessUnit
CreateAccessUnitWithData(bool is_audio
, int audio_packet_id
,
384 bool use_large_size_video
) {
387 unit
.status
= DemuxerStream::kOk
;
388 scoped_refptr
<DecoderBuffer
> buffer
;
390 buffer
= ReadTestDataFile(
391 base::StringPrintf("vorbis-packet-%d", audio_packet_id
));
393 buffer
= ReadTestDataFile(
394 use_large_size_video
? "vp8-I-frame-640x240" : "vp8-I-frame-320x240");
396 unit
.data
= std::vector
<uint8
>(
397 buffer
->data(), buffer
->data() + buffer
->data_size());
400 // Vorbis needs 4 extra bytes padding on Android to decode properly. Check
401 // NuMediaExtractor.cpp in Android source code.
402 uint8 padding
[4] = { 0xff , 0xff , 0xff , 0xff };
403 unit
.data
.insert(unit
.data
.end(), padding
, padding
+ 4);
409 DemuxerData
CreateReadFromDemuxerAckForAudio(int packet_id
) {
411 data
.type
= DemuxerStream::AUDIO
;
412 data
.access_units
.resize(1);
413 data
.access_units
[0] = CreateAccessUnitWithData(true, packet_id
, false);
418 DemuxerData
CreateReadFromDemuxerAckForVideo(bool use_large_size
) {
420 data
.type
= DemuxerStream::VIDEO
;
421 data
.access_units
.resize(1);
422 data
.access_units
[0] = CreateAccessUnitWithData(false, 0, use_large_size
);
426 DemuxerData
CreateEOSAck(bool is_audio
) {
428 data
.type
= is_audio
? DemuxerStream::AUDIO
: DemuxerStream::VIDEO
;
429 data
.access_units
.resize(1);
430 data
.access_units
[0].status
= DemuxerStream::kOk
;
431 data
.access_units
[0].is_end_of_stream
= true;
435 DemuxerData
CreateAbortedAck(bool is_audio
) {
437 data
.type
= is_audio
? DemuxerStream::AUDIO
: DemuxerStream::VIDEO
;
438 data
.access_units
.resize(1);
439 data
.access_units
[0].status
= DemuxerStream::kAborted
;
443 bool HasData(bool is_audio
) {
444 return GetMediaDecoderJob(is_audio
)->HasData();
447 // Helper method for use at test start. It starts an audio decoder job and
448 // immediately feeds it some data to decode. Then, without letting the decoder
449 // job complete a decode cycle, it also starts player SeekTo(). Upon return,
450 // the player should not yet have sent the DemuxerSeek IPC request, though
451 // seek event should be pending. The audio decoder job will also still be
453 void StartAudioDecoderJobAndSeekToWhileDecoding(
454 const base::TimeDelta
& seek_time
) {
455 EXPECT_FALSE(GetMediaCodecBridge(true));
456 EXPECT_FALSE(player_
.IsPlaying());
457 EXPECT_EQ(0, demuxer_
->num_data_requests());
458 EXPECT_EQ(0.0, GetPrerollTimestamp().InMillisecondsF());
459 EXPECT_EQ(player_
.GetCurrentTime(), GetPrerollTimestamp());
460 StartAudioDecoderJob();
461 EXPECT_FALSE(GetMediaDecoderJob(true)->is_decoding());
462 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
463 EXPECT_EQ(2, demuxer_
->num_data_requests());
464 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
465 player_
.SeekTo(seek_time
);
466 EXPECT_EQ(0.0, GetPrerollTimestamp().InMillisecondsF());
467 EXPECT_EQ(0, demuxer_
->num_seek_requests());
468 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
471 // Seek, including simulated receipt of |kAborted| read between SeekTo() and
472 // OnDemuxerSeekDone(). Use this helper method only when the player already
473 // has created the media codec bridge. Exactly one request for more data is
474 // expected following the seek, so use this helper for players with only audio
476 void SeekPlayerWithAbort(bool is_audio
, const base::TimeDelta
& seek_time
) {
477 int original_num_seeks
= demuxer_
->num_seek_requests();
478 int original_num_data_requests
= demuxer_
->num_data_requests();
480 // Initiate a seek. Skip the round-trip of requesting seek from renderer.
481 // Instead behave as if the renderer has asked us to seek.
482 player_
.SeekTo(seek_time
);
484 // Verify that the seek does not occur until previously outstanding data
485 // request is satisfied.
486 EXPECT_EQ(original_num_seeks
, demuxer_
->num_seek_requests());
488 // Simulate seeking causes the demuxer to abort the outstanding read
489 // caused by the seek.
490 player_
.OnDemuxerDataAvailable(CreateAbortedAck(is_audio
));
492 // Wait for the decode job to finish so we can process the seek request.
493 WaitForDecodeDone(is_audio
, !is_audio
);
495 // Verify that the seek is requested.
496 EXPECT_EQ(original_num_seeks
+ 1, demuxer_
->num_seek_requests());
498 // Send back the seek done notification. This should trigger the player to
499 // call OnReadFromDemuxer() again.
500 EXPECT_EQ(original_num_data_requests
, demuxer_
->num_data_requests());
501 player_
.OnDemuxerSeekDone(kNoTimestamp());
502 EXPECT_EQ(original_num_data_requests
+ 1, demuxer_
->num_data_requests());
504 // No other seek should have been requested.
505 EXPECT_EQ(original_num_seeks
+ 1, demuxer_
->num_seek_requests());
508 // Preroll the decoder job to |target_timestamp|. The first access unit
509 // to decode will have a timestamp equal to |start_timestamp|.
510 // |is_clock_manager| indicates whether the decoder serves as the clock
511 // manager for the player.
512 // TODO(qinmin): Add additional test cases for out-of-order decodes.
513 // See http://crbug.com/331421.
514 void PrerollDecoderToTime(bool is_audio
,
515 const base::TimeDelta
& start_timestamp
,
516 const base::TimeDelta
& target_timestamp
,
517 bool is_clock_manager
) {
518 // For streams with both audio and video, it is possible that audio rolls
519 // past the |target_timestamp|. As a result, the current time may be larger
520 // than the |target_timestamp| for video as it may not be the clock manager.
521 EXPECT_TRUE(!is_clock_manager
||
522 target_timestamp
== player_
.GetCurrentTime());
523 // |start_timestamp| must be smaller than |target_timestamp|.
524 EXPECT_LE(start_timestamp
, target_timestamp
);
525 DemuxerData data
= is_audio
? CreateReadFromDemuxerAckForAudio(1) :
526 CreateReadFromDemuxerAckForVideo(false);
527 int current_timestamp
= start_timestamp
.InMilliseconds();
529 // Send some data with access unit timestamps before the |target_timestamp|,
530 // and continue sending the data until preroll finishes.
531 // This simulates the common condition that AUs received after browser
532 // seek begin with timestamps before the seek target, and don't
533 // immediately complete preroll.
534 while (IsPrerolling(is_audio
)) {
535 data
.access_units
[0].timestamp
=
536 base::TimeDelta::FromMilliseconds(current_timestamp
);
537 player_
.OnDemuxerDataAvailable(data
);
538 EXPECT_TRUE(GetMediaDecoderJob(is_audio
)->is_decoding());
539 EXPECT_TRUE(GetMediaCodecBridge(is_audio
));
540 EXPECT_TRUE(!is_clock_manager
||
541 target_timestamp
== player_
.GetCurrentTime());
542 current_timestamp
+= 30;
543 WaitForDecodeDone(is_audio
, !is_audio
);
545 EXPECT_LE(target_timestamp
, player_
.GetCurrentTime());
548 DemuxerData
CreateReadFromDemuxerAckWithConfigChanged(
550 int config_unit_index
,
551 const DemuxerConfigs
& configs
) {
553 data
.type
= is_audio
? DemuxerStream::AUDIO
: DemuxerStream::VIDEO
;
554 data
.access_units
.resize(config_unit_index
+ 1);
556 for (int i
= 0; i
< config_unit_index
; ++i
)
557 data
.access_units
[i
] = CreateAccessUnitWithData(is_audio
, i
, false);
559 data
.access_units
[config_unit_index
].status
= DemuxerStream::kConfigChanged
;
560 data
.demuxer_configs
.resize(1);
561 data
.demuxer_configs
[0] = configs
;
565 // Valid only for video-only player tests. If |trigger_with_release_start| is
566 // true, triggers the browser seek with a Release() + video data received +
567 // Start() with a new surface. If false, triggers the browser seek by
568 // setting a new video surface after beginning decode of received video data.
569 // Such data receipt causes possibility that an I-frame is not next, and
570 // browser seek results once decode completes and surface change processing
572 void BrowserSeekPlayer(bool trigger_with_release_start
) {
573 int expected_num_data_requests
= demuxer_
->num_data_requests() + 2;
574 int expected_num_seek_requests
= demuxer_
->num_seek_requests();
575 int expected_num_browser_seek_requests
=
576 demuxer_
->num_browser_seek_requests();
578 CreateNextTextureAndSetVideoSurface();
579 StartVideoDecoderJob();
580 if (trigger_with_release_start
) {
581 // Consume the first frame, so that the next VideoDecoderJob will not
582 // inherit the I-frame from the previous decoder.
583 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
585 WaitForVideoDecodeDone();
587 // Simulate demuxer's response to the video data request. The data will be
588 // passed to the next MediaCodecBridge.
589 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
590 EXPECT_FALSE(GetMediaCodecBridge(false));
591 EXPECT_FALSE(player_
.IsPlaying());
592 EXPECT_EQ(expected_num_seek_requests
, demuxer_
->num_seek_requests());
594 CreateNextTextureAndSetVideoSurface();
595 Resume(false, false);
596 EXPECT_FALSE(GetMediaCodecBridge(false));
598 // Run the message loop so that prefetch will complete.
599 while (expected_num_seek_requests
== demuxer_
->num_seek_requests())
600 message_loop_
.RunUntilIdle();
602 // Simulate demuxer's response to the video data request.
603 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
605 // While the decoder is decoding, trigger a browser seek by changing
606 // surface. Demuxer does not know of browser seek in advance, so no
607 // |kAborted| data is required (though |kAborted| can certainly occur for
608 // any pending read in reality due to renderer preparing for a regular
610 CreateNextTextureAndSetVideoSurface();
612 // Browser seek should not begin until decoding has completed.
613 EXPECT_TRUE(GetMediaCodecBridge(false));
614 EXPECT_EQ(expected_num_seek_requests
, demuxer_
->num_seek_requests());
616 // Wait for the media codec bridge to finish decoding and be reset pending
618 WaitForVideoDecodeDone();
619 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
622 // Only one browser seek should have been initiated, and no further data
623 // should have been requested.
624 expected_num_seek_requests
++;
625 expected_num_browser_seek_requests
++;
626 EXPECT_EQ(expected_num_seek_requests
, demuxer_
->num_seek_requests());
627 EXPECT_EQ(expected_num_browser_seek_requests
,
628 demuxer_
->num_browser_seek_requests());
629 EXPECT_EQ(expected_num_data_requests
, demuxer_
->num_data_requests());
632 // Creates a new media codec bridge and feeds it data ending with a
633 // |kConfigChanged| access unit. If |config_unit_in_prefetch| is true, sends
634 // feeds the config change AU in response to the job's first read request
635 // (prefetch). If false, regular data is fed and decoded prior to feeding the
636 // config change AU in response to the second data request (after prefetch
637 // completed). |config_unit_index| controls which access unit is
638 // |kConfigChanged|. If |enable_adaptive_playback| is true, config change will
639 // not cause the decoder to recreate the media codec bridge. Otherwise, the
640 // decoder has to drain all its data before recreating the new codec.
641 void SendConfigChangeToDecoder(bool is_audio
,
642 bool config_unit_in_prefetch
,
643 int config_unit_index
,
644 bool enable_adaptive_playback
) {
645 EXPECT_FALSE(GetMediaCodecBridge(is_audio
));
647 StartAudioDecoderJob();
649 CreateNextTextureAndSetVideoSurface();
650 StartVideoDecoderJob();
653 int expected_num_data_requests
= demuxer_
->num_data_requests();
654 // Feed and decode a standalone access unit so the player exits prefetch.
655 if (!config_unit_in_prefetch
) {
657 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
659 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
660 EnableAdaptiveVideoPlayback(enable_adaptive_playback
);
663 WaitForDecodeDone(is_audio
, !is_audio
);
665 // We should have completed the prefetch phase at this point.
666 expected_num_data_requests
++;
667 EXPECT_EQ(expected_num_data_requests
, demuxer_
->num_data_requests());
670 DemuxerConfigs configs
= is_audio
?
671 CreateAudioDemuxerConfigs(kCodecVorbis
, true) :
672 CreateVideoDemuxerConfigs(true);
673 // Feed and decode access units with data for any units prior to
674 // |config_unit_index|, and a |kConfigChanged| unit at that index.
675 // Player should prepare to reconfigure the decoder job, and should request
676 // new demuxer configs.
677 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckWithConfigChanged(
678 is_audio
, config_unit_index
, configs
));
680 expected_num_data_requests
++;
681 EXPECT_EQ(expected_num_data_requests
, demuxer_
->num_data_requests());
683 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
685 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(true));
687 // If the adaptive playback setting was not passed to the MediaCodecBridge
688 // earlier, do it here.
689 if (config_unit_in_prefetch
&& !is_audio
)
690 EnableAdaptiveVideoPlayback(enable_adaptive_playback
);
693 // Send a config change to the decoder job and drain the decoder so that the
694 // config change is processed.
695 void StartConfigChange(bool is_audio
,
696 bool config_unit_in_prefetch
,
697 int config_unit_index
,
698 bool enable_adaptive_playback
) {
699 SendConfigChangeToDecoder(is_audio
, config_unit_in_prefetch
,
700 config_unit_index
, enable_adaptive_playback
);
702 EXPECT_EQ(!config_unit_in_prefetch
&& !enable_adaptive_playback
&&
703 config_unit_index
== 0, IsDrainingDecoder(is_audio
));
704 int expected_num_data_requests
= demuxer_
->num_data_requests();
705 // Run until decoder starts to request new data.
706 while (demuxer_
->num_data_requests() == expected_num_data_requests
)
707 message_loop_
.RunUntilIdle();
708 EXPECT_FALSE(IsDrainingDecoder(is_audio
));
711 void EnableAdaptiveVideoPlayback(bool enable
) {
712 EXPECT_TRUE(GetMediaCodecBridge(false));
713 static_cast<VideoCodecBridge
*>(GetMediaCodecBridge(false))->
714 set_adaptive_playback_supported_for_testing(
718 void CreateNextTextureAndSetVideoSurface() {
719 gfx::SurfaceTexture
* surface_texture
;
720 if (surface_texture_a_is_next_
) {
721 surface_texture_a_
= gfx::SurfaceTexture::Create(next_texture_id_
++);
722 surface_texture
= surface_texture_a_
.get();
724 surface_texture_b_
= gfx::SurfaceTexture::Create(next_texture_id_
++);
725 surface_texture
= surface_texture_b_
.get();
728 surface_texture_a_is_next_
= !surface_texture_a_is_next_
;
729 gfx::ScopedJavaSurface surface
= gfx::ScopedJavaSurface(surface_texture
);
730 player_
.SetVideoSurface(surface
.Pass());
733 // Wait for one or both of the jobs to complete decoding. Media codec bridges
734 // are assumed to exist for any stream whose decode completion is awaited.
735 void WaitForDecodeDone(bool wait_for_audio
, bool wait_for_video
) {
736 DCHECK(wait_for_audio
|| wait_for_video
);
737 while ((wait_for_audio
&& GetMediaCodecBridge(true) &&
738 GetMediaDecoderJob(true)->HasData() &&
739 GetMediaDecoderJob(true)->is_decoding()) ||
740 (wait_for_video
&& GetMediaCodecBridge(false) &&
741 GetMediaDecoderJob(false)->HasData() &&
742 GetMediaDecoderJob(false)->is_decoding())) {
743 message_loop_
.RunUntilIdle();
747 void WaitForAudioDecodeDone() {
748 WaitForDecodeDone(true, false);
751 void WaitForVideoDecodeDone() {
752 WaitForDecodeDone(false, true);
755 void WaitForAudioVideoDecodeDone() {
756 WaitForDecodeDone(true, true);
759 // If |send_eos| is true, generates EOS for the stream corresponding to
760 // |eos_for_audio|. Verifies that playback completes and no further data
762 // If |send_eos| is false, then it is assumed that caller previously arranged
763 // for player to receive EOS for each stream, but the player has not yet
764 // decoded all of them. In this case, |eos_for_audio| is ignored.
765 void VerifyPlaybackCompletesOnEOSDecode(bool send_eos
, bool eos_for_audio
) {
766 int original_num_data_requests
= demuxer_
->num_data_requests();
768 player_
.OnDemuxerDataAvailable(CreateEOSAck(eos_for_audio
));
769 EXPECT_FALSE(manager_
.playback_completed());
771 EXPECT_TRUE(manager_
.playback_completed());
772 EXPECT_EQ(original_num_data_requests
, demuxer_
->num_data_requests());
775 void VerifyCompletedPlaybackResumesOnSeekPlusStart(bool have_audio
,
777 DCHECK(have_audio
|| have_video
);
779 EXPECT_TRUE(manager_
.playback_completed());
781 player_
.SeekTo(base::TimeDelta());
782 player_
.OnDemuxerSeekDone(kNoTimestamp());
783 Resume(have_audio
, have_video
);
786 // Starts the appropriate decoder jobs according to |have_audio| and
787 // |have_video|. Then starts seek during decode of EOS or non-EOS according to
788 // |eos_audio| and |eos_video|. Simulates seek completion and verifies that
789 // playback never completed. |eos_{audio,video}| is ignored if the
790 // corresponding |have_{audio,video}| is false.
791 void VerifySeekDuringEOSDecodePreventsPlaybackCompletion(bool have_audio
,
795 DCHECK(have_audio
|| have_video
);
798 CreateNextTextureAndSetVideoSurface();
800 Start(CreateDemuxerConfigs(have_audio
, have_video
));
803 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
806 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
808 // Run until more data is requested a number of times equal to the number of
809 // media types configured. Since prefetching may be in progress, we cannot
810 // reliably expect Run() to complete until we have sent demuxer data for all
811 // configured media types, above.
812 WaitForDecodeDone(have_audio
, have_video
);
814 // Simulate seek while decoding EOS or non-EOS for the appropriate
818 player_
.OnDemuxerDataAvailable(CreateEOSAck(true));
820 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(1));
825 player_
.OnDemuxerDataAvailable(CreateEOSAck(false));
827 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
830 player_
.SeekTo(base::TimeDelta());
831 EXPECT_EQ(0, demuxer_
->num_seek_requests());
832 WaitForDecodeDone(have_audio
, have_video
);
833 EXPECT_EQ(1, demuxer_
->num_seek_requests());
835 player_
.OnDemuxerSeekDone(kNoTimestamp());
836 EXPECT_FALSE(manager_
.playback_completed());
839 base::TimeTicks
StartTimeTicks() {
840 return player_
.start_time_ticks_
;
843 bool IsRequestingDemuxerData(bool is_audio
) {
844 return GetMediaDecoderJob(is_audio
)->is_requesting_demuxer_data_
;
847 bool IsDrainingDecoder(bool is_audio
) {
848 return GetMediaDecoderJob(is_audio
)->drain_decoder_
;
852 base::MessageLoop message_loop_
;
853 MockMediaPlayerManager manager_
;
854 MockDemuxerAndroid
* demuxer_
; // Owned by |player_|.
855 MediaSourcePlayer player_
;
857 // Track whether a possibly async decoder callback test hook has run.
858 bool decoder_callback_hook_executed_
;
860 // We need to keep the surface texture while the decoder is actively decoding.
861 // Otherwise, it may trigger unexpected crashes on some devices. To switch
862 // surfaces, tests need to create a new surface texture without releasing
863 // their previous one. In CreateNextTextureAndSetVideoSurface(), we toggle
864 // between two surface textures, only replacing the N-2 texture. Assumption is
865 // that no more than N-1 texture is in use by decoder when
866 // CreateNextTextureAndSetVideoSurface() is called.
867 scoped_refptr
<gfx::SurfaceTexture
> surface_texture_a_
;
868 scoped_refptr
<gfx::SurfaceTexture
> surface_texture_b_
;
869 bool surface_texture_a_is_next_
;
870 int next_texture_id_
;
872 DISALLOW_COPY_AND_ASSIGN(MediaSourcePlayerTest
);
875 TEST_F(MediaSourcePlayerTest
, StartAudioDecoderWithValidConfig
) {
876 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
878 // Test audio codec will be created when valid configs and data are passed to
879 // the audio decoder job.
880 StartAudioDecoderJob();
881 EXPECT_EQ(0, demuxer_
->num_seek_requests());
882 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
883 EXPECT_TRUE(GetMediaCodecBridge(true));
886 TEST_F(MediaSourcePlayerTest
, StartAudioDecoderWithInvalidConfig
) {
887 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
889 // Test audio decoder job will not be created when failed to start the codec.
890 DemuxerConfigs configs
= CreateAudioDemuxerConfigs(kCodecVorbis
, false);
891 // Replace with invalid |audio_extra_data|
892 configs
.audio_extra_data
.clear();
893 uint8 invalid_codec_data
[] = { 0x00, 0xff, 0xff, 0xff, 0xff };
894 configs
.audio_extra_data
.insert(configs
.audio_extra_data
.begin(),
895 invalid_codec_data
, invalid_codec_data
+ 4);
898 // Decoder is not created after data is received.
899 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
900 EXPECT_FALSE(GetMediaCodecBridge(true));
903 TEST_F(MediaSourcePlayerTest
, StartVideoCodecWithValidSurface
) {
904 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
906 // Test video codec will not be created until data is received.
907 StartVideoDecoderJob();
909 // Set both an initial and a later video surface without receiving any
911 CreateNextTextureAndSetVideoSurface();
912 EXPECT_FALSE(GetMediaCodecBridge(false));
913 CreateNextTextureAndSetVideoSurface();
914 EXPECT_FALSE(GetMediaCodecBridge(false));
916 // No seeks, even on setting surface, should have occurred. (Browser seeks can
917 // occur on setting surface, but only after previously receiving video data.)
918 EXPECT_EQ(0, demuxer_
->num_seek_requests());
920 // Send the first input chunk and verify that decoder will be created.
921 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
922 EXPECT_TRUE(GetMediaCodecBridge(false));
923 WaitForVideoDecodeDone();
926 TEST_F(MediaSourcePlayerTest
, StartVideoCodecWithInvalidSurface
) {
927 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
929 // Test video codec will not be created when surface is invalid.
930 scoped_refptr
<gfx::SurfaceTexture
> surface_texture(
931 gfx::SurfaceTexture::Create(0));
932 gfx::ScopedJavaSurface
surface(surface_texture
.get());
933 StartVideoDecoderJob();
935 // Release the surface texture.
936 surface_texture
= NULL
;
937 player_
.SetVideoSurface(surface
.Pass());
939 // Player should not seek the demuxer on setting initial surface.
940 EXPECT_EQ(0, demuxer_
->num_seek_requests());
941 EXPECT_EQ(1, demuxer_
->num_data_requests());
943 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
944 EXPECT_FALSE(GetMediaCodecBridge(false));
947 TEST_F(MediaSourcePlayerTest
, ReadFromDemuxerAfterSeek
) {
948 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
950 // Test decoder job will resend a ReadFromDemuxer request after seek.
951 StartAudioDecoderJob();
952 SeekPlayerWithAbort(true, base::TimeDelta());
955 TEST_F(MediaSourcePlayerTest
, SetSurfaceWhileSeeking
) {
956 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
958 // Test SetVideoSurface() will not cause an extra seek while the player is
959 // waiting for demuxer to indicate seek is done.
960 player_
.OnDemuxerConfigsAvailable(
961 CreateVideoDemuxerConfigs(false));
963 // Initiate a seek. Skip requesting element seek of renderer.
964 // Instead behave as if the renderer has asked us to seek.
965 player_
.SeekTo(base::TimeDelta());
966 EXPECT_EQ(1, demuxer_
->num_seek_requests());
968 CreateNextTextureAndSetVideoSurface();
969 EXPECT_EQ(1, demuxer_
->num_seek_requests());
972 // Send the seek done notification. The player should start requesting data.
973 player_
.OnDemuxerSeekDone(kNoTimestamp());
974 EXPECT_FALSE(GetMediaCodecBridge(false));
975 EXPECT_EQ(1, demuxer_
->num_data_requests());
976 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
977 EXPECT_TRUE(GetMediaCodecBridge(false));
979 // Reconfirm exactly 1 seek request has been made of demuxer, and that it
980 // was not a browser seek request.
981 EXPECT_EQ(1, demuxer_
->num_seek_requests());
982 EXPECT_EQ(0, demuxer_
->num_browser_seek_requests());
983 WaitForVideoDecodeDone();
986 TEST_F(MediaSourcePlayerTest
, ChangeMultipleSurfaceWhileDecoding
) {
987 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
989 // Test MediaSourcePlayer can switch multiple surfaces during decoding.
990 CreateNextTextureAndSetVideoSurface();
991 StartVideoDecoderJob();
992 EXPECT_EQ(0, demuxer_
->num_seek_requests());
994 // Send the first input chunk.
995 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
997 // While the decoder is decoding, change multiple surfaces. Pass an empty
999 gfx::ScopedJavaSurface empty_surface
;
1000 player_
.SetVideoSurface(empty_surface
.Pass());
1001 // Next, pass a new non-empty surface.
1002 CreateNextTextureAndSetVideoSurface();
1004 // Wait for the media codec bridge to finish decoding and be reset pending a
1006 WaitForVideoDecodeDone();
1007 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1009 // Only one browser seek should have been initiated. No further data request
1010 // should have been processed on |message_loop_| before surface change event
1011 // became pending, above.
1012 EXPECT_EQ(1, demuxer_
->num_browser_seek_requests());
1013 EXPECT_EQ(2, demuxer_
->num_data_requests());
1015 // Simulate browser seek is done and confirm player requests more data for new
1017 player_
.OnDemuxerSeekDone(player_
.GetCurrentTime());
1018 EXPECT_FALSE(GetMediaCodecBridge(false));
1019 EXPECT_EQ(3, demuxer_
->num_data_requests());
1020 EXPECT_EQ(1, demuxer_
->num_seek_requests());
1022 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1023 EXPECT_TRUE(GetMediaCodecBridge(false));
1024 WaitForVideoDecodeDone();
1027 TEST_F(MediaSourcePlayerTest
, SetEmptySurfaceAndStarveWhileDecoding
) {
1028 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1030 // Test player pauses if an empty surface is passed.
1031 CreateNextTextureAndSetVideoSurface();
1032 StartVideoDecoderJob();
1033 EXPECT_EQ(1, demuxer_
->num_data_requests());
1035 // Send the first input chunk.
1036 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1038 // While the decoder is decoding, pass an empty surface.
1039 gfx::ScopedJavaSurface empty_surface
;
1040 player_
.SetVideoSurface(empty_surface
.Pass());
1041 // Let the player starve. However, it should not issue any new data request in
1043 TriggerPlayerStarvation();
1044 // Wait for the media codec bridge to finish decoding and be reset.
1045 while (GetMediaDecoderJob(false)->is_decoding())
1046 message_loop_
.RunUntilIdle();
1048 // No further seek or data requests should have been received since the
1049 // surface is empty.
1050 EXPECT_EQ(0, demuxer_
->num_browser_seek_requests());
1051 EXPECT_EQ(2, demuxer_
->num_data_requests());
1052 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1054 // Playback resumes once a non-empty surface is passed.
1055 CreateNextTextureAndSetVideoSurface();
1056 EXPECT_EQ(0, demuxer_
->num_browser_seek_requests());
1057 while(demuxer_
->num_browser_seek_requests() != 1)
1058 message_loop_
.RunUntilIdle();
1059 WaitForVideoDecodeDone();
1062 TEST_F(MediaSourcePlayerTest
, ReleaseVideoDecoderResourcesWhileDecoding
) {
1063 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1065 // Test that if video decoder is released while decoding, the resources will
1066 // not be immediately released.
1067 CreateNextTextureAndSetVideoSurface();
1068 StartVideoDecoderJob();
1069 // No resource is requested since there is no data to decode.
1070 EXPECT_EQ(0, manager_
.num_resources_requested());
1072 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1074 // Recreate the video decoder.
1075 CreateNextTextureAndSetVideoSurface();
1077 while (!GetMediaDecoderJob(false)->is_decoding())
1078 message_loop_
.RunUntilIdle();
1079 EXPECT_EQ(0, demuxer_
->num_browser_seek_requests());
1080 EXPECT_EQ(1, manager_
.num_resources_requested());
1082 // Wait for the media codec bridge to finish decoding and be reset.
1083 while (GetMediaDecoderJob(false)->is_decoding())
1084 message_loop_
.RunUntilIdle();
1087 TEST_F(MediaSourcePlayerTest
, AudioOnlyStartAfterSeekFinish
) {
1088 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1090 // Test audio decoder job will not start until pending seek event is handled.
1091 DemuxerConfigs configs
= CreateAudioDemuxerConfigs(kCodecVorbis
, false);
1092 player_
.OnDemuxerConfigsAvailable(configs
);
1094 // Initiate a seek. Skip requesting element seek of renderer.
1095 // Instead behave as if the renderer has asked us to seek.
1096 player_
.SeekTo(base::TimeDelta());
1097 EXPECT_EQ(1, demuxer_
->num_seek_requests());
1100 EXPECT_EQ(0, demuxer_
->num_data_requests());
1102 // Sending back the seek done notification.
1103 player_
.OnDemuxerSeekDone(kNoTimestamp());
1104 EXPECT_FALSE(GetMediaCodecBridge(true));
1105 EXPECT_EQ(1, demuxer_
->num_data_requests());
1107 // Reconfirm exactly 1 seek request has been made of demuxer.
1108 EXPECT_EQ(1, demuxer_
->num_seek_requests());
1110 // Decoder is created after data is received.
1111 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1112 EXPECT_TRUE(GetMediaCodecBridge(true));
1115 TEST_F(MediaSourcePlayerTest
, VideoOnlyStartAfterSeekFinish
) {
1116 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1118 // Test video decoder job will not start until pending seek event is handled.
1119 CreateNextTextureAndSetVideoSurface();
1120 DemuxerConfigs configs
= CreateVideoDemuxerConfigs(false);
1121 player_
.OnDemuxerConfigsAvailable(configs
);
1123 // Initiate a seek. Skip requesting element seek of renderer.
1124 // Instead behave as if the renderer has asked us to seek.
1125 player_
.SeekTo(base::TimeDelta());
1126 EXPECT_EQ(1, demuxer_
->num_seek_requests());
1129 EXPECT_EQ(0, demuxer_
->num_data_requests());
1131 // Sending back the seek done notification.
1132 player_
.OnDemuxerSeekDone(kNoTimestamp());
1133 EXPECT_FALSE(GetMediaCodecBridge(false));
1134 EXPECT_EQ(1, demuxer_
->num_data_requests());
1136 // Reconfirm exactly 1 seek request has been made of demuxer.
1137 EXPECT_EQ(1, demuxer_
->num_seek_requests());
1139 // Decoder is created after data is received.
1140 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1141 EXPECT_TRUE(GetMediaCodecBridge(false));
1142 WaitForVideoDecodeDone();
1145 TEST_F(MediaSourcePlayerTest
, StartImmediatelyAfterPause
) {
1146 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1148 // Test that if the decoding job is not fully stopped after Pause(),
1149 // calling Start() will be a noop.
1150 StartAudioDecoderJob();
1152 MediaDecoderJob
* decoder_job
= GetMediaDecoderJob(true);
1153 EXPECT_FALSE(GetMediaDecoderJob(true)->is_decoding());
1155 // Sending data to player.
1156 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1157 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1158 EXPECT_EQ(2, demuxer_
->num_data_requests());
1160 // Decoder job will not immediately stop after Pause() since it is
1161 // running on another thread.
1162 player_
.Pause(true);
1163 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1165 // Nothing happens when calling Start() again.
1167 // Verify that Start() will not destroy and recreate the media codec bridge.
1168 EXPECT_EQ(decoder_job
, GetMediaDecoderJob(true));
1170 while (GetMediaDecoderJob(true)->is_decoding())
1171 message_loop_
.RunUntilIdle();
1172 // The decoder job should finish and wait for data.
1173 EXPECT_EQ(2, demuxer_
->num_data_requests());
1174 EXPECT_TRUE(IsRequestingDemuxerData(true));
1177 TEST_F(MediaSourcePlayerTest
, DecoderJobsCannotStartWithoutAudio
) {
1178 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1180 // Test that when Start() is called, video decoder job will wait for audio
1181 // decoder job before start decoding the data.
1182 CreateNextTextureAndSetVideoSurface();
1183 Start(CreateAudioVideoDemuxerConfigs());
1184 MediaDecoderJob
* audio_decoder_job
= GetMediaDecoderJob(true);
1185 MediaDecoderJob
* video_decoder_job
= GetMediaDecoderJob(false);
1187 EXPECT_FALSE(audio_decoder_job
->is_decoding());
1188 EXPECT_FALSE(video_decoder_job
->is_decoding());
1190 // Sending video data to player, video decoder should not start.
1191 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1192 EXPECT_FALSE(video_decoder_job
->is_decoding());
1194 // Sending audio data to player, both decoders should start now.
1195 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1196 EXPECT_TRUE(audio_decoder_job
->is_decoding());
1197 EXPECT_TRUE(video_decoder_job
->is_decoding());
1199 // No seeks should have occurred.
1200 EXPECT_EQ(0, demuxer_
->num_seek_requests());
1201 WaitForVideoDecodeDone();
1204 TEST_F(MediaSourcePlayerTest
, StartTimeTicksResetAfterDecoderUnderruns
) {
1205 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1207 // Test start time ticks will reset after decoder job underruns.
1208 StartAudioDecoderJob();
1210 DecodeAudioDataUntilOutputBecomesAvailable();
1212 // The decoder job should finish prerolling and start prefetching.
1213 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(3));
1214 base::TimeTicks previous
= StartTimeTicks();
1216 // Let the decoder starve.
1217 TriggerPlayerStarvation();
1218 WaitForAudioDecodeDone();
1219 EXPECT_TRUE(StartTimeTicks() == previous
);
1221 // Send new data to the decoder so it can finish prefetching. This should
1222 // reset the start time ticks.
1223 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(3));
1224 EXPECT_TRUE(StartTimeTicks() != previous
);
1226 base::TimeTicks current
= StartTimeTicks();
1227 EXPECT_LE(0, (current
- previous
).InMillisecondsF());
1230 TEST_F(MediaSourcePlayerTest
, V_SecondAccessUnitIsEOSAndResumePlayAfterSeek
) {
1231 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1233 // Test MediaSourcePlayer can replay video after input EOS is reached.
1234 CreateNextTextureAndSetVideoSurface();
1235 StartVideoDecoderJob();
1237 // Send the first input chunk.
1238 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1239 WaitForVideoDecodeDone();
1241 VerifyPlaybackCompletesOnEOSDecode(true, false);
1242 VerifyCompletedPlaybackResumesOnSeekPlusStart(false, true);
1245 TEST_F(MediaSourcePlayerTest
, A_FirstAccessUnitIsEOSAndResumePlayAfterSeek
) {
1246 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1248 // Test decode of audio EOS buffer without any prior decode. See also
1249 // http://b/11696552.
1250 // Also tests that seeking+Start() after completing audio playback resumes
1252 Start(CreateAudioDemuxerConfigs(kCodecAAC
, false));
1253 VerifyPlaybackCompletesOnEOSDecode(true, true);
1254 VerifyCompletedPlaybackResumesOnSeekPlusStart(true, false);
1257 TEST_F(MediaSourcePlayerTest
, V_FirstAccessUnitAfterSeekIsEOS
) {
1258 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1260 // Test decode of video EOS buffer, just after seeking, without any prior
1261 // decode (other than the simulated |kAborted| resulting from the seek
1263 CreateNextTextureAndSetVideoSurface();
1264 StartVideoDecoderJob();
1265 SeekPlayerWithAbort(false, base::TimeDelta());
1266 VerifyPlaybackCompletesOnEOSDecode(true, false);
1269 TEST_F(MediaSourcePlayerTest
, A_FirstAccessUnitAfterSeekIsEOS
) {
1270 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1272 // Test decode of audio EOS buffer, just after seeking, without any prior
1273 // decode (other than the simulated |kAborted| resulting from the seek
1274 // process.) See also http://b/11696552.
1275 Start(CreateAudioDemuxerConfigs(kCodecAAC
, false));
1276 SeekPlayerWithAbort(true, base::TimeDelta());
1277 VerifyPlaybackCompletesOnEOSDecode(true, true);
1280 TEST_F(MediaSourcePlayerTest
, AV_PlaybackCompletionAcrossConfigChange
) {
1281 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1283 // Test that if one stream (audio) has completed decode of EOS and the other
1284 // stream (video) processes config change, that subsequent video EOS completes
1286 // Also tests that seeking+Start() after completing playback resumes playback.
1287 CreateNextTextureAndSetVideoSurface();
1288 Start(CreateAudioVideoDemuxerConfigs());
1290 player_
.OnDemuxerDataAvailable(CreateEOSAck(true)); // Audio EOS
1291 DemuxerConfigs configs
= CreateVideoDemuxerConfigs(true);
1292 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckWithConfigChanged(
1293 false, 0, configs
)); // Video |kConfigChanged| as first unit.
1295 WaitForAudioVideoDecodeDone();
1297 EXPECT_EQ(3, demuxer_
->num_data_requests());
1299 // At no time after completing audio EOS decode, above, should the
1300 // audio decoder job resume decoding. Send and decode video EOS.
1301 VerifyPlaybackCompletesOnEOSDecode(true, false);
1302 VerifyCompletedPlaybackResumesOnSeekPlusStart(true, true);
1305 TEST_F(MediaSourcePlayerTest
, VA_PlaybackCompletionAcrossConfigChange
) {
1306 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1308 // Test that if one stream (video) has completed decode of EOS and the other
1309 // stream (audio) processes config change, that subsequent audio EOS completes
1311 // Also tests that seeking+Start() after completing playback resumes playback.
1312 CreateNextTextureAndSetVideoSurface();
1313 Start(CreateAudioVideoDemuxerConfigs());
1315 player_
.OnDemuxerDataAvailable(CreateEOSAck(false)); // Video EOS
1316 // Audio |kConfigChanged| as first unit.
1317 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckWithConfigChanged(
1318 true, 0, CreateAudioDemuxerConfigs(kCodecVorbis
, false)));
1320 WaitForAudioVideoDecodeDone();
1322 EXPECT_EQ(3, demuxer_
->num_data_requests());
1324 // At no time after completing video EOS decode, above, should the
1325 // video decoder job resume decoding. Send and decode audio EOS.
1326 VerifyPlaybackCompletesOnEOSDecode(true, true);
1327 VerifyCompletedPlaybackResumesOnSeekPlusStart(true, true);
1330 TEST_F(MediaSourcePlayerTest
, AV_NoPrefetchForFinishedVideoOnAudioStarvation
) {
1331 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1333 // Test that if one stream (video) has completed decode of EOS, prefetch
1334 // resulting from player starvation occurs only for the other stream (audio),
1335 // and responding to that prefetch with EOS completes A/V playback, even if
1336 // another starvation occurs during the latter EOS's decode.
1337 CreateNextTextureAndSetVideoSurface();
1338 Start(CreateAudioVideoDemuxerConfigs());
1340 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1341 player_
.OnDemuxerDataAvailable(CreateEOSAck(false)); // Video EOS
1343 // Wait until video EOS is processed and more data (assumed to be audio) is
1345 WaitForAudioVideoDecodeDone();
1346 EXPECT_EQ(3, demuxer_
->num_data_requests());
1348 // Simulate decoder underrun to trigger prefetch while still decoding audio.
1349 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(1));
1350 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding() &&
1351 !GetMediaDecoderJob(false)->is_decoding());
1352 TriggerPlayerStarvation();
1354 // Complete the audio decode that was in progress when simulated player
1355 // starvation was triggered.
1356 WaitForAudioDecodeDone();
1357 EXPECT_EQ(4, demuxer_
->num_data_requests());
1358 player_
.OnDemuxerDataAvailable(CreateEOSAck(true)); // Audio EOS
1359 EXPECT_FALSE(GetMediaDecoderJob(false)->is_decoding());
1360 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1362 // Simulate another decoder underrun to trigger prefetch while decoding EOS.
1363 TriggerPlayerStarvation();
1364 VerifyPlaybackCompletesOnEOSDecode(false, true /* ignored */);
1367 TEST_F(MediaSourcePlayerTest
, V_StarvationDuringEOSDecode
) {
1368 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1370 // Test that video-only playback completes without further data requested when
1371 // starvation occurs during EOS decode.
1372 CreateNextTextureAndSetVideoSurface();
1373 StartVideoDecoderJob();
1374 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1375 WaitForVideoDecodeDone();
1377 // Simulate decoder underrun to trigger prefetch while decoding EOS.
1378 player_
.OnDemuxerDataAvailable(CreateEOSAck(false)); // Video EOS
1379 EXPECT_TRUE(GetMediaDecoderJob(false)->is_decoding());
1380 TriggerPlayerStarvation();
1381 VerifyPlaybackCompletesOnEOSDecode(false, false /* ignored */);
1384 TEST_F(MediaSourcePlayerTest
, A_StarvationDuringEOSDecode
) {
1385 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1387 // Test that audio-only playback completes without further data requested when
1388 // starvation occurs during EOS decode.
1389 StartAudioDecoderJob();
1390 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1391 WaitForAudioDecodeDone();
1393 // Simulate decoder underrun to trigger prefetch while decoding EOS.
1394 player_
.OnDemuxerDataAvailable(CreateEOSAck(true)); // Audio EOS
1395 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1396 TriggerPlayerStarvation();
1397 VerifyPlaybackCompletesOnEOSDecode(false, true /* ignored */);
1400 TEST_F(MediaSourcePlayerTest
, AV_SeekDuringEOSDecodePreventsCompletion
) {
1401 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1403 // Test that seek supercedes audio+video playback completion on simultaneous
1404 // audio and video EOS decode, if SeekTo() occurs during these EOS decodes.
1405 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, true, true, true);
1408 TEST_F(MediaSourcePlayerTest
, AV_SeekDuringAudioEOSDecodePreventsCompletion
) {
1409 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1411 // Test that seek supercedes audio+video playback completion on simultaneous
1412 // audio EOS and video non-EOS decode, if SeekTo() occurs during these
1414 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, true, true, false);
1417 TEST_F(MediaSourcePlayerTest
, AV_SeekDuringVideoEOSDecodePreventsCompletion
) {
1418 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1420 // Test that seek supercedes audio+video playback completion on simultaneous
1421 // audio non-EOS and video EOS decode, if SeekTo() occurs during these
1423 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, true, false, true);
1426 TEST_F(MediaSourcePlayerTest
, V_SeekDuringEOSDecodePreventsCompletion
) {
1427 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1429 // Test that seek supercedes video-only playback completion on EOS decode, if
1430 // SeekTo() occurs during EOS decode.
1431 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(false, true, false, true);
1434 TEST_F(MediaSourcePlayerTest
, A_SeekDuringEOSDecodePreventsCompletion
) {
1435 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1437 // Test that seek supercedes audio-only playback completion on EOS decode, if
1438 // SeekTo() occurs during EOS decode.
1439 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, false, true, false);
1442 TEST_F(MediaSourcePlayerTest
, NoRequestForDataAfterAbort
) {
1443 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1445 // Test that the decoder will not request new data after receiving an aborted
1447 StartAudioDecoderJob();
1449 // Send an aborted access unit.
1450 player_
.OnDemuxerDataAvailable(CreateAbortedAck(true));
1451 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1452 WaitForAudioDecodeDone();
1454 // No request will be sent for new data.
1455 EXPECT_EQ(1, demuxer_
->num_data_requests());
1457 // No seek requests should have occurred.
1458 EXPECT_EQ(0, demuxer_
->num_seek_requests());
1461 TEST_F(MediaSourcePlayerTest
, DemuxerDataArrivesAfterRelease
) {
1462 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1464 // Test that the decoder should not crash if demuxer data arrives after
1466 StartAudioDecoderJob();
1469 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1471 // The media codec bridge should have been released.
1472 EXPECT_FALSE(player_
.IsPlaying());
1474 // No further data should have been requested.
1475 EXPECT_EQ(1, demuxer_
->num_data_requests());
1477 // No seek requests should have occurred.
1478 EXPECT_EQ(0, demuxer_
->num_seek_requests());
1481 TEST_F(MediaSourcePlayerTest
, BrowserSeek_RegularSeekPendsBrowserSeekDone
) {
1482 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1484 // Test that a browser seek, once started, delays a newly arrived regular
1485 // SeekTo() request's demuxer seek until the browser seek is done.
1486 BrowserSeekPlayer(false);
1488 // Simulate renderer requesting a regular seek while browser seek in progress.
1489 player_
.SeekTo(base::TimeDelta());
1491 // Simulate browser seek is done. Confirm player requests the regular seek,
1492 // still has no video codec configured, and has not requested any
1493 // further data since the surface change event became pending in
1494 // BrowserSeekPlayer().
1495 EXPECT_EQ(1, demuxer_
->num_seek_requests());
1496 player_
.OnDemuxerSeekDone(base::TimeDelta());
1497 EXPECT_EQ(2, demuxer_
->num_seek_requests());
1498 EXPECT_EQ(1, demuxer_
->num_browser_seek_requests());
1500 // Simulate regular seek is done and confirm player requests more data for
1502 player_
.OnDemuxerSeekDone(kNoTimestamp());
1503 EXPECT_FALSE(GetMediaCodecBridge(false));
1504 EXPECT_EQ(3, demuxer_
->num_data_requests());
1505 EXPECT_EQ(2, demuxer_
->num_seek_requests());
1506 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1507 EXPECT_TRUE(GetMediaCodecBridge(false));
1508 WaitForVideoDecodeDone();
1511 TEST_F(MediaSourcePlayerTest
, BrowserSeek_InitialReleaseAndStart
) {
1512 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1514 // Test that no browser seek is requested if player Release() + Start() occurs
1515 // prior to receiving any data.
1516 CreateNextTextureAndSetVideoSurface();
1517 StartVideoDecoderJob();
1520 // Pass a new non-empty surface.
1521 CreateNextTextureAndSetVideoSurface();
1525 // No data request is issued since there is still one pending.
1526 EXPECT_EQ(1, demuxer_
->num_data_requests());
1527 EXPECT_FALSE(GetMediaCodecBridge(false));
1529 // No browser seek is needed.
1530 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1531 EXPECT_EQ(0, demuxer_
->num_browser_seek_requests());
1532 EXPECT_EQ(2, demuxer_
->num_data_requests());
1533 WaitForVideoDecodeDone();
1536 TEST_F(MediaSourcePlayerTest
, BrowserSeek_MidStreamReleaseAndStart
) {
1537 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1539 // Test that one browser seek is requested if player Release() + Start(), with
1540 // video data received between Release() and Start().
1541 BrowserSeekPlayer(true);
1543 // Simulate browser seek is done and confirm player requests more data.
1544 player_
.OnDemuxerSeekDone(base::TimeDelta());
1545 EXPECT_EQ(3, demuxer_
->num_data_requests());
1546 EXPECT_EQ(1, demuxer_
->num_seek_requests());
1549 TEST_F(MediaSourcePlayerTest
, NoBrowserSeekWithKeyFrameInCache
) {
1550 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1552 // Test that browser seek is not needed if a key frame is found in data
1554 CreateNextTextureAndSetVideoSurface();
1555 StartVideoDecoderJob();
1556 DemuxerData data
= CreateReadFromDemuxerAckForVideo(false);
1557 data
.access_units
[0].is_key_frame
= true;
1559 // Simulate demuxer's response to the video data request.
1560 player_
.OnDemuxerDataAvailable(data
);
1562 // Trigger decoder recreation later by changing surfaces.
1563 CreateNextTextureAndSetVideoSurface();
1565 // Wait for the media codec bridge to finish decoding and be reset.
1566 WaitForVideoDecodeDone();
1567 EXPECT_FALSE(HasData(false));
1569 // Send a non key frame to decoder so that decoder can continue. This will
1570 // not trigger any browser seeks as the previous key frame is still in the
1572 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1573 WaitForVideoDecodeDone();
1574 EXPECT_EQ(0, demuxer_
->num_browser_seek_requests());
1577 TEST_F(MediaSourcePlayerTest
, PrerollAudioAfterSeek
) {
1578 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1580 // Test decoder job will preroll the media to the seek position.
1581 StartAudioDecoderJob();
1583 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100));
1584 EXPECT_TRUE(IsPrerolling(true));
1585 PrerollDecoderToTime(
1586 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), true);
1589 TEST_F(MediaSourcePlayerTest
, PrerollVideoAfterSeek
) {
1590 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1592 // Test decoder job will preroll the media to the seek position.
1593 CreateNextTextureAndSetVideoSurface();
1594 StartVideoDecoderJob();
1596 SeekPlayerWithAbort(false, base::TimeDelta::FromMilliseconds(100));
1597 EXPECT_TRUE(IsPrerolling(false));
1598 PrerollDecoderToTime(
1599 false, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), true);
1602 TEST_F(MediaSourcePlayerTest
, SeekingAfterCompletingPrerollRestartsPreroll
) {
1603 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1605 // Test decoder job will begin prerolling upon seek, when it was not
1606 // prerolling prior to the seek.
1607 StartAudioDecoderJob();
1608 MediaDecoderJob
* decoder_job
= GetMediaDecoderJob(true);
1609 EXPECT_TRUE(IsPrerolling(true));
1611 // Complete the initial preroll by feeding data to the decoder.
1612 DecodeAudioDataUntilOutputBecomesAvailable();
1613 EXPECT_FALSE(IsPrerolling(true));
1615 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(500));
1617 // Prerolling should have begun again.
1618 EXPECT_TRUE(IsPrerolling(true));
1619 EXPECT_EQ(500.0, GetPrerollTimestamp().InMillisecondsF());
1621 // Send data at and after the seek position. Prerolling should complete.
1622 for (int i
= 0; i
< 4; ++i
) {
1623 DemuxerData data
= CreateReadFromDemuxerAckForAudio(i
);
1624 data
.access_units
[0].timestamp
= base::TimeDelta::FromMilliseconds(
1625 500 + 30 * (i
- 1));
1626 player_
.OnDemuxerDataAvailable(data
);
1627 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1628 WaitForAudioDecodeDone();
1630 EXPECT_LT(500.0, player_
.GetCurrentTime().InMillisecondsF());
1631 EXPECT_FALSE(IsPrerolling(true));
1633 // Throughout this test, we should have not re-created the media codec bridge,
1634 // so IsPrerolling() transition from false to true was not due to constructor
1635 // initialization. It was due to BeginPrerolling().
1636 EXPECT_EQ(decoder_job
, GetMediaDecoderJob(true));
1639 TEST_F(MediaSourcePlayerTest
, PrerollContinuesAcrossReleaseAndStart
) {
1640 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1642 // Test decoder job will resume media prerolling if interrupted by Release()
1644 StartAudioDecoderJob();
1646 base::TimeDelta target_timestamp
= base::TimeDelta::FromMilliseconds(100);
1647 SeekPlayerWithAbort(true, target_timestamp
);
1648 EXPECT_TRUE(IsPrerolling(true));
1649 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
1651 // Send some data before the seek position.
1652 // Test uses 'large' number of iterations because decoder job may not get
1653 // MEDIA_CODEC_OK output status until after a few dequeue output attempts.
1654 // This allows decoder status to stabilize prior to AU timestamp reaching
1655 // the preroll target.
1657 for (int i
= 0; i
< 10; ++i
) {
1658 data
= CreateReadFromDemuxerAckForAudio(3);
1659 data
.access_units
[0].timestamp
= base::TimeDelta::FromMilliseconds(i
* 10);
1661 // While still prerolling, Release() and Start() the player.
1663 // The decoder is still decoding and will not be immediately released.
1664 EXPECT_TRUE(GetMediaCodecBridge(true));
1665 Resume(false, false);
1667 player_
.OnDemuxerDataAvailable(data
);
1668 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1669 WaitForAudioDecodeDone();
1671 EXPECT_TRUE(IsPrerolling(true));
1673 EXPECT_EQ(100.0, player_
.GetCurrentTime().InMillisecondsF());
1674 EXPECT_TRUE(IsPrerolling(true));
1676 // Send data after the seek position.
1677 PrerollDecoderToTime(true, target_timestamp
, target_timestamp
, true);
1680 // Flaky on Android: crbug.com/419122.
1681 #if defined(OS_ANDROID)
1682 #define MAYBE_PrerollContinuesAcrossConfigChange \
1683 DISABLED_PrerollContinuesAcrossConfigChange
1685 #define MAYBE_PrerollContinuesAcrossConfigChange \
1686 PrerollContinuesAcrossConfigChange
1688 TEST_F(MediaSourcePlayerTest
, MAYBE_PrerollContinuesAcrossConfigChange
) {
1689 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1691 // Test decoder job will resume media prerolling if interrupted by
1692 // |kConfigChanged| and OnDemuxerConfigsAvailable().
1693 StartAudioDecoderJob();
1695 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100));
1696 EXPECT_TRUE(IsPrerolling(true));
1697 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
1699 DemuxerConfigs configs
= CreateAudioDemuxerConfigs(kCodecVorbis
, true);
1701 // In response to data request, simulate that demuxer signals config change by
1702 // sending an AU with |kConfigChanged|.
1703 DemuxerData data
= CreateReadFromDemuxerAckWithConfigChanged(
1705 player_
.OnDemuxerDataAvailable(data
);
1707 PrerollDecoderToTime(
1708 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), true);
1711 TEST_F(MediaSourcePlayerTest
, PrerollContinuesAfterUnchangedConfigs
) {
1712 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1714 // Test decoder job will resume media prerolling if interrupted by a config
1715 // change access unit with unchanged configs.
1716 StartAudioDecoderJob();
1718 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100));
1719 EXPECT_TRUE(IsPrerolling(true));
1720 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
1722 DemuxerConfigs configs
= CreateAudioDemuxerConfigs(kCodecVorbis
, false);
1724 // In response to data request, simulate that demuxer signals config change by
1725 // sending an AU with |kConfigChanged|.
1726 DemuxerData data
= CreateReadFromDemuxerAckWithConfigChanged(
1728 player_
.OnDemuxerDataAvailable(data
);
1729 PrerollDecoderToTime(
1730 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), true);
1733 TEST_F(MediaSourcePlayerTest
, AudioPrerollFinishesBeforeVideo
) {
1734 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1736 // Test that after audio finishes prerolling, it will wait for video to finish
1737 // prerolling before advancing together.
1738 CreateNextTextureAndSetVideoSurface();
1739 Start(CreateAudioVideoDemuxerConfigs());
1742 base::TimeDelta seek_position
= base::TimeDelta::FromMilliseconds(100);
1743 player_
.SeekTo(seek_position
);
1744 player_
.OnDemuxerDataAvailable(CreateAbortedAck(true));
1745 player_
.OnDemuxerDataAvailable(CreateAbortedAck(false));
1746 WaitForDecodeDone(true, true);
1748 // Verify that the seek is requested.
1749 EXPECT_EQ(1, demuxer_
->num_seek_requests());
1750 player_
.OnDemuxerSeekDone(kNoTimestamp());
1751 EXPECT_EQ(4, demuxer_
->num_data_requests());
1752 EXPECT_EQ(player_
.GetCurrentTime().InMillisecondsF(), 100.0);
1753 EXPECT_EQ(GetPrerollTimestamp().InMillisecondsF(), 100.0);
1755 // Send both audio and video data to finish prefetching.
1756 base::TimeDelta seek_ack_position
= base::TimeDelta::FromMilliseconds(70);
1757 DemuxerData audio_data
= CreateReadFromDemuxerAckForAudio(0);
1758 audio_data
.access_units
[0].timestamp
= seek_ack_position
;
1759 DemuxerData video_data
= CreateReadFromDemuxerAckForVideo(false);
1760 video_data
.access_units
[0].timestamp
= seek_ack_position
;
1761 player_
.OnDemuxerDataAvailable(audio_data
);
1762 player_
.OnDemuxerDataAvailable(video_data
);
1763 WaitForAudioDecodeDone();
1764 WaitForVideoDecodeDone();
1766 // Send audio data at and after the seek position. Audio should finish
1767 // prerolling and stop decoding.
1768 EXPECT_EQ(6, demuxer_
->num_data_requests());
1769 PrerollDecoderToTime(true, seek_position
, seek_position
, true);
1770 EXPECT_FALSE(GetMediaDecoderJob(true)->is_decoding());
1771 EXPECT_FALSE(IsPrerolling(true));
1772 EXPECT_TRUE(IsPrerolling(false));
1774 // Send video data to let video finish prerolling.
1775 PrerollDecoderToTime(false, seek_position
, seek_position
, false);
1776 EXPECT_FALSE(IsPrerolling(false));
1778 // Both audio and video decoders should start decoding again.
1779 player_
.OnDemuxerDataAvailable(audio_data
);
1780 player_
.OnDemuxerDataAvailable(video_data
);
1781 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1782 EXPECT_TRUE(GetMediaDecoderJob(false)->is_decoding());
1785 TEST_F(MediaSourcePlayerTest
, SimultaneousAudioVideoConfigChange
) {
1786 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1788 // Test that the player allows simultaneous audio and video config change,
1789 // such as might occur during OnPrefetchDone() if next access unit for both
1790 // audio and video jobs is |kConfigChanged|.
1791 CreateNextTextureAndSetVideoSurface();
1792 Start(CreateAudioVideoDemuxerConfigs());
1793 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1794 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1795 EXPECT_TRUE(GetMediaCodecBridge(true));
1796 EXPECT_TRUE(GetMediaCodecBridge(false));
1797 EnableAdaptiveVideoPlayback(false);
1798 WaitForAudioVideoDecodeDone();
1800 // If audio or video hasn't finished prerolling, let them finish it.
1801 if (IsPrerolling(true))
1802 PrerollDecoderToTime(true, base::TimeDelta(), base::TimeDelta(), true);
1803 if (IsPrerolling(false))
1804 PrerollDecoderToTime(false, base::TimeDelta(), base::TimeDelta(), false);
1805 int expected_num_data_requests
= demuxer_
->num_data_requests();
1807 // Simulate audio |kConfigChanged| prefetched as standalone access unit.
1808 DemuxerConfigs audio_configs
= CreateAudioDemuxerConfigs(kCodecVorbis
, true);
1809 player_
.OnDemuxerDataAvailable(
1810 CreateReadFromDemuxerAckWithConfigChanged(true, 0, audio_configs
));
1812 // Simulate video |kConfigChanged| prefetched as standalone access unit.
1813 player_
.OnDemuxerDataAvailable(
1814 CreateReadFromDemuxerAckWithConfigChanged(
1815 false, 0, CreateVideoDemuxerConfigs(true)));
1816 EXPECT_EQ(expected_num_data_requests
+ 2, demuxer_
->num_data_requests());
1817 EXPECT_TRUE(IsDrainingDecoder(true));
1818 EXPECT_TRUE(IsDrainingDecoder(false));
1820 // Waiting for decoder to finish draining.
1821 while (IsDrainingDecoder(true) || IsDrainingDecoder(false))
1822 message_loop_
.RunUntilIdle();
1825 TEST_F(MediaSourcePlayerTest
,
1826 SimultaneousAudioVideoConfigChangeWithAdaptivePlayback
) {
1827 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1829 // Test that the player allows simultaneous audio and video config change with
1830 // adaptive video playback enabled.
1831 CreateNextTextureAndSetVideoSurface();
1832 Start(CreateAudioVideoDemuxerConfigs());
1833 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1834 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1835 EXPECT_EQ(4, demuxer_
->num_data_requests());
1836 EXPECT_TRUE(GetMediaCodecBridge(true));
1837 EXPECT_TRUE(GetMediaCodecBridge(false));
1838 EnableAdaptiveVideoPlayback(true);
1839 WaitForAudioVideoDecodeDone();
1841 // If audio or video hasn't finished prerolling, let them finish it.
1842 if (IsPrerolling(true))
1843 PrerollDecoderToTime(true, base::TimeDelta(), base::TimeDelta(), true);
1844 if (IsPrerolling(false))
1845 PrerollDecoderToTime(false, base::TimeDelta(), base::TimeDelta(), false);
1846 int expected_num_data_requests
= demuxer_
->num_data_requests();
1848 // Simulate audio |kConfigChanged| prefetched as standalone access unit.
1849 DemuxerConfigs audio_configs
= CreateAudioDemuxerConfigs(kCodecVorbis
, true);
1850 player_
.OnDemuxerDataAvailable(
1851 CreateReadFromDemuxerAckWithConfigChanged(true, 0, audio_configs
));
1853 // Simulate video |kConfigChanged| prefetched as standalone access unit.
1854 player_
.OnDemuxerDataAvailable(
1855 CreateReadFromDemuxerAckWithConfigChanged(
1856 false, 0, CreateVideoDemuxerConfigs(true)));
1857 EXPECT_EQ(expected_num_data_requests
+ 2, demuxer_
->num_data_requests());
1858 EXPECT_TRUE(IsDrainingDecoder(true));
1859 EXPECT_FALSE(IsDrainingDecoder(false));
1861 // Waiting for audio decoder to finish draining.
1862 while (IsDrainingDecoder(true))
1863 message_loop_
.RunUntilIdle();
1866 TEST_F(MediaSourcePlayerTest
, DemuxerConfigRequestedIfInPrefetchUnit0
) {
1867 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1869 // Test that the player detects need for and requests demuxer configs if
1870 // the |kConfigChanged| unit is the very first unit in the set of units
1871 // received in OnDemuxerDataAvailable() ostensibly while
1872 // |PREFETCH_DONE_EVENT_PENDING|.
1873 StartConfigChange(true, true, 0, false);
1874 WaitForAudioDecodeDone();
1877 TEST_F(MediaSourcePlayerTest
, DemuxerConfigRequestedIfInPrefetchUnit1
) {
1878 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1880 // Test that the player detects need for and requests demuxer configs if
1881 // the |kConfigChanged| unit is not the first unit in the set of units
1882 // received in OnDemuxerDataAvailable() ostensibly while
1883 // |PREFETCH_DONE_EVENT_PENDING|.
1884 StartConfigChange(true, true, 1, false);
1885 WaitForAudioDecodeDone();
1888 TEST_F(MediaSourcePlayerTest
, DemuxerConfigRequestedIfInUnit0AfterPrefetch
) {
1889 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1891 // Test that the player detects need for and requests demuxer configs if
1892 // the |kConfigChanged| unit is the very first unit in the set of units
1893 // received in OnDemuxerDataAvailable() from data requested ostensibly while
1895 StartConfigChange(true, false, 0, false);
1896 WaitForAudioDecodeDone();
1899 TEST_F(MediaSourcePlayerTest
, DemuxerConfigRequestedIfInUnit1AfterPrefetch
) {
1900 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1902 // Test that the player detects need for and requests demuxer configs if
1903 // the |kConfigChanged| unit is not the first unit in the set of units
1904 // received in OnDemuxerDataAvailable() from data requested ostensibly while
1906 StartConfigChange(true, false, 1, false);
1907 WaitForAudioDecodeDone();
1910 TEST_F(MediaSourcePlayerTest
, BrowserSeek_PrerollAfterBrowserSeek
) {
1911 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1913 // Test decoder job will preroll the media to the actual seek position
1914 // resulting from a browser seek.
1915 BrowserSeekPlayer(false);
1917 // Simulate browser seek is done, but to a later time than was requested.
1918 EXPECT_LT(player_
.GetCurrentTime().InMillisecondsF(), 100);
1919 player_
.OnDemuxerSeekDone(base::TimeDelta::FromMilliseconds(100));
1920 // Because next AU is not I-frame, MediaCodecBridge will not be recreated.
1921 EXPECT_FALSE(GetMediaCodecBridge(false));
1922 EXPECT_EQ(100.0, player_
.GetCurrentTime().InMillisecondsF());
1923 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
1924 EXPECT_EQ(3, demuxer_
->num_data_requests());
1926 PrerollDecoderToTime(
1927 false, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), true);
1930 TEST_F(MediaSourcePlayerTest
, VideoDemuxerConfigChange
) {
1931 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1933 // Test that video config change notification results in creating a new
1934 // video codec without any browser seek.
1935 StartConfigChange(false, true, 1, false);
1937 // New video codec should have been created and configured, without any
1939 EXPECT_TRUE(GetMediaCodecBridge(false));
1940 EXPECT_EQ(3, demuxer_
->num_data_requests());
1941 EXPECT_EQ(0, demuxer_
->num_seek_requests());
1943 // 2 codecs should have been created, one before the config change, and one
1945 EXPECT_EQ(2, manager_
.num_resources_requested());
1946 WaitForVideoDecodeDone();
1949 TEST_F(MediaSourcePlayerTest
, VideoDemuxerConfigChangeWithAdaptivePlayback
) {
1950 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1952 // Test that if codec supports adaptive playback, no new codec should be
1953 // created beyond the one used to decode the prefetch media data prior to
1954 // the kConfigChanged.
1955 StartConfigChange(false, true, 1, true);
1957 // No browser seek should be needed.
1958 EXPECT_TRUE(GetMediaCodecBridge(false));
1959 EXPECT_EQ(3, demuxer_
->num_data_requests());
1960 EXPECT_EQ(0, demuxer_
->num_seek_requests());
1962 // Only 1 codec should have been created so far.
1963 EXPECT_EQ(1, manager_
.num_resources_requested());
1964 WaitForVideoDecodeDone();
1967 TEST_F(MediaSourcePlayerTest
, DecoderDrainInterruptedBySeek
) {
1968 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1970 // Test if a decoder is being drained while receiving a seek request, draining
1972 SendConfigChangeToDecoder(true, false, 0, false);
1973 EXPECT_TRUE(IsDrainingDecoder(true));
1975 player_
.SeekTo(base::TimeDelta::FromMilliseconds(100));
1976 WaitForAudioDecodeDone();
1977 EXPECT_FALSE(IsDrainingDecoder(true));
1978 player_
.OnDemuxerSeekDone(kNoTimestamp());
1980 EXPECT_EQ(1, demuxer_
->num_seek_requests());
1981 EXPECT_EQ(4, demuxer_
->num_data_requests());
1984 TEST_F(MediaSourcePlayerTest
, DecoderDrainInterruptedByRelease
) {
1985 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1987 // Test if a decoder is being drained while receiving a release request,
1988 // draining is canceled.
1989 SendConfigChangeToDecoder(true, false, 0, false);
1990 EXPECT_TRUE(IsDrainingDecoder(true));
1993 WaitForAudioDecodeDone();
1994 EXPECT_EQ(3, demuxer_
->num_data_requests());
1995 EXPECT_FALSE(IsDrainingDecoder(true));
1997 EXPECT_FALSE(GetMediaCodecBridge(true));
1998 EXPECT_FALSE(player_
.IsPlaying());
2001 EXPECT_TRUE(player_
.IsPlaying());
2002 EXPECT_EQ(3, demuxer_
->num_data_requests());
2005 TEST_F(MediaSourcePlayerTest
, DecoderDrainInterruptedBySurfaceChange
) {
2006 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2008 // Test if a video decoder is being drained while surface changes, draining
2010 SendConfigChangeToDecoder(false, false, 0, false);
2011 EXPECT_TRUE(IsDrainingDecoder(false));
2013 CreateNextTextureAndSetVideoSurface();
2014 WaitForVideoDecodeDone();
2016 EXPECT_FALSE(IsDrainingDecoder(false));
2017 EXPECT_TRUE(player_
.IsPlaying());
2019 // The frame after the config change should always be an iframe, so no browser
2020 // seek is needed when recreating the video decoder due to surface change.
2021 EXPECT_TRUE(GetMediaCodecBridge(false));
2022 EXPECT_EQ(4, demuxer_
->num_data_requests());
2023 EXPECT_EQ(0, demuxer_
->num_seek_requests());
2026 TEST_F(MediaSourcePlayerTest
,
2027 BrowserSeek_DecoderStarvationWhilePendingSurfaceChange
) {
2028 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2030 // Test video decoder starvation while handling a pending surface change
2031 // should not cause any crashes.
2032 CreateNextTextureAndSetVideoSurface();
2033 StartVideoDecoderJob();
2034 DemuxerData data
= CreateReadFromDemuxerAckForVideo(false);
2035 player_
.OnDemuxerDataAvailable(data
);
2037 // Trigger a surface change and decoder starvation.
2038 CreateNextTextureAndSetVideoSurface();
2039 TriggerPlayerStarvation();
2040 WaitForVideoDecodeDone();
2041 EXPECT_EQ(0, demuxer_
->num_browser_seek_requests());
2043 // Surface change should trigger a seek.
2044 player_
.OnDemuxerDataAvailable(data
);
2045 EXPECT_EQ(1, demuxer_
->num_browser_seek_requests());
2046 player_
.OnDemuxerSeekDone(base::TimeDelta());
2047 // After seek is done, prefetch is handled first. MediaCodecBridge is not
2048 // created at this moment.
2049 EXPECT_FALSE(GetMediaCodecBridge(false));
2051 // A new data request should be sent.
2052 EXPECT_EQ(3, demuxer_
->num_data_requests());
2055 TEST_F(MediaSourcePlayerTest
, ReleaseWithOnPrefetchDoneAlreadyPosted
) {
2056 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2058 // Test if OnPrefetchDone() had already been posted before and is executed
2059 // after Release(), then player does not DCHECK. This test is fragile to
2060 // change to MediaDecoderJob::Prefetch() implementation; it assumes task
2061 // is posted to run |prefetch_cb| if the job already HasData().
2062 // TODO(wolenetz): Remove MSP::set_decode_callback_for_testing() if this test
2063 // becomes obsolete. See http://crbug.com/304234.
2064 StartAudioDecoderJob();
2066 // Escape the original prefetch by decoding a single access unit.
2067 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
2068 WaitForAudioDecodeDone();
2070 // Prime the job with a few more access units, so that a later prefetch,
2071 // triggered by starvation to simulate decoder underrun, can trivially
2072 // post task to run OnPrefetchDone().
2073 player_
.OnDemuxerDataAvailable(
2074 CreateReadFromDemuxerAckWithConfigChanged(
2075 true, 4, CreateAudioDemuxerConfigs(kCodecVorbis
, false)));
2076 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
2078 // Simulate decoder underrun, so trivial prefetch starts while still decoding.
2079 // The prefetch and posting of OnPrefetchDone() will not occur until next
2080 // MediaDecoderCallBack() occurs.
2081 TriggerPlayerStarvation();
2083 // Upon the next successful decode callback, post a task to call Release() on
2084 // the |player_|, such that the trivial OnPrefetchDone() task posting also
2085 // occurs and should execute after the Release().
2086 OnNextTestDecodeCallbackPostTaskToReleasePlayer();
2088 WaitForAudioDecodeDone();
2089 EXPECT_TRUE(decoder_callback_hook_executed_
);
2091 EXPECT_EQ(3, demuxer_
->num_data_requests());
2093 // Player should not request any new data since the access units haven't
2094 // been fully decoded yet.
2095 Resume(false, false);
2098 TEST_F(MediaSourcePlayerTest
, SeekToThenReleaseThenDemuxerSeekAndDone
) {
2099 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2101 // Test if Release() occurs after SeekTo(), but the DemuxerSeek IPC request
2102 // has not yet been sent, then the seek request is sent after Release(). Also,
2103 // test if OnDemuxerSeekDone() occurs prior to next Start(), then the player
2104 // will resume correct post-seek preroll upon Start().
2105 StartAudioDecoderJobAndSeekToWhileDecoding(
2106 base::TimeDelta::FromMilliseconds(100));
2108 EXPECT_EQ(0, demuxer_
->num_seek_requests());
2109 WaitForAudioDecodeDone();
2110 EXPECT_EQ(1, demuxer_
->num_seek_requests());
2112 player_
.OnDemuxerSeekDone(kNoTimestamp());
2113 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2114 EXPECT_FALSE(GetMediaCodecBridge(true));
2115 EXPECT_FALSE(player_
.IsPlaying());
2117 // Player should begin prefetch and resume preroll upon Start().
2118 EXPECT_EQ(2, demuxer_
->num_data_requests());
2119 Resume(true, false);
2120 EXPECT_TRUE(IsPrerolling(true));
2121 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2123 // No further seek should have been requested since Release(), above.
2124 EXPECT_EQ(1, demuxer_
->num_seek_requests());
2127 TEST_F(MediaSourcePlayerTest
, SeekToThenReleaseThenDemuxerSeekThenStart
) {
2128 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2130 // Test if Release() occurs after SeekTo(), but the DemuxerSeek IPC request
2131 // has not yet been sent, then the seek request is sent after Release(). Also,
2132 // test if OnDemuxerSeekDone() does not occur until after the next Start(),
2133 // then the player remains pending seek done until (and resumes correct
2134 // post-seek preroll after) OnDemuxerSeekDone().
2135 StartAudioDecoderJobAndSeekToWhileDecoding(
2136 base::TimeDelta::FromMilliseconds(100));
2138 EXPECT_EQ(0, demuxer_
->num_seek_requests());
2140 // Player should not prefetch upon Start() nor create the media codec bridge,
2141 // due to awaiting DemuxerSeekDone.
2142 EXPECT_EQ(2, demuxer_
->num_data_requests());
2143 Resume(false, false);
2145 WaitForAudioDecodeDone();
2146 EXPECT_EQ(1, demuxer_
->num_seek_requests());
2147 player_
.OnDemuxerSeekDone(kNoTimestamp());
2148 EXPECT_TRUE(GetMediaDecoderJob(true));
2149 EXPECT_TRUE(IsPrerolling(true));
2150 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2151 EXPECT_EQ(3, demuxer_
->num_data_requests());
2153 // No further seek should have been requested since Release(), above.
2154 EXPECT_EQ(1, demuxer_
->num_seek_requests());
2157 TEST_F(MediaSourcePlayerTest
, SeekToThenDemuxerSeekThenReleaseThenSeekDone
) {
2158 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2160 // Test if Release() occurs after a SeekTo()'s subsequent DemuxerSeek IPC
2161 // request and OnDemuxerSeekDone() arrives prior to the next Start(), then the
2162 // player will resume correct post-seek preroll upon Start().
2163 StartAudioDecoderJobAndSeekToWhileDecoding(
2164 base::TimeDelta::FromMilliseconds(100));
2165 WaitForAudioDecodeDone();
2166 EXPECT_EQ(1, demuxer_
->num_seek_requests());
2169 player_
.OnDemuxerSeekDone(kNoTimestamp());
2170 EXPECT_FALSE(player_
.IsPlaying());
2171 EXPECT_FALSE(GetMediaCodecBridge(true));
2172 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2174 // Player should begin prefetch and resume preroll upon Start().
2175 EXPECT_EQ(2, demuxer_
->num_data_requests());
2176 Resume(true, false);
2177 EXPECT_TRUE(IsPrerolling(true));
2178 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2180 // No further seek should have been requested since before Release(), above.
2181 EXPECT_EQ(1, demuxer_
->num_seek_requests());
2184 TEST_F(MediaSourcePlayerTest
, SeekToThenReleaseThenStart
) {
2185 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2187 // Test if Release() occurs after a SeekTo()'s subsequent DemuxerSeeK IPC
2188 // request and OnDemuxerSeekDone() does not occur until after the next
2189 // Start(), then the player remains pending seek done until (and resumes
2190 // correct post-seek preroll after) OnDemuxerSeekDone().
2191 StartAudioDecoderJobAndSeekToWhileDecoding(
2192 base::TimeDelta::FromMilliseconds(100));
2193 WaitForAudioDecodeDone();
2194 EXPECT_EQ(1, demuxer_
->num_seek_requests());
2197 EXPECT_EQ(2, demuxer_
->num_data_requests());
2198 Resume(false, false);
2200 player_
.OnDemuxerSeekDone(kNoTimestamp());
2201 EXPECT_FALSE(GetMediaCodecBridge(true));
2202 EXPECT_TRUE(IsPrerolling(true));
2203 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2204 EXPECT_EQ(3, demuxer_
->num_data_requests());
2206 // No further seek should have been requested since before Release(), above.
2207 EXPECT_EQ(1, demuxer_
->num_seek_requests());
2210 TEST_F(MediaSourcePlayerTest
, ConfigChangedThenReleaseThenStart
) {
2211 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2213 // Test if Release() occurs after |kConfigChanged| is processed, new data
2214 // requested of demuxer, and the requested data arrive before the next
2215 // Start(), then the player starts to decode the new data without any seek.
2216 StartConfigChange(true, true, 0, false);
2219 EXPECT_TRUE(GetMediaCodecBridge(true));
2220 EXPECT_FALSE(player_
.IsPlaying());
2221 EXPECT_EQ(3, demuxer_
->num_data_requests());
2222 player_
.OnDemuxerDataAvailable(
2223 CreateReadFromDemuxerAckWithConfigChanged(
2224 true, 4, CreateAudioDemuxerConfigs(kCodecVorbis
, false)));
2225 WaitForAudioDecodeDone();
2226 EXPECT_FALSE(GetMediaCodecBridge(true));
2228 // Player should resume upon Start(), even without further configs supplied.
2230 EXPECT_TRUE(player_
.IsPlaying());
2231 EXPECT_EQ(3, demuxer_
->num_data_requests());
2232 EXPECT_EQ(0, demuxer_
->num_seek_requests());
2233 WaitForAudioDecodeDone();
2236 TEST_F(MediaSourcePlayerTest
, BrowserSeek_ThenReleaseThenDemuxerSeekDone
) {
2237 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2239 // Test that Release() after a browser seek's DemuxerSeek IPC request has been
2240 // sent behaves similar to a regular seek: if OnDemuxerSeekDone() occurs
2241 // before the next Start()+SetVideoSurface(), then the player will resume
2242 // correct post-seek preroll upon Start()+SetVideoSurface().
2243 BrowserSeekPlayer(false);
2244 base::TimeDelta expected_preroll_timestamp
= player_
.GetCurrentTime();
2247 player_
.OnDemuxerSeekDone(expected_preroll_timestamp
);
2248 EXPECT_FALSE(player_
.IsPlaying());
2249 EXPECT_FALSE(GetMediaCodecBridge(false));
2250 EXPECT_EQ(expected_preroll_timestamp
, GetPrerollTimestamp());
2252 // Player should begin prefetch and resume preroll upon Start().
2253 EXPECT_EQ(2, demuxer_
->num_data_requests());
2254 CreateNextTextureAndSetVideoSurface();
2255 Resume(false, true);
2256 EXPECT_TRUE(IsPrerolling(false));
2257 EXPECT_EQ(expected_preroll_timestamp
, GetPrerollTimestamp());
2258 EXPECT_EQ(expected_preroll_timestamp
, player_
.GetCurrentTime());
2260 // No further seek should have been requested since BrowserSeekPlayer().
2261 EXPECT_EQ(1, demuxer_
->num_seek_requests());
2264 TEST_F(MediaSourcePlayerTest
, BrowserSeek_ThenReleaseThenStart
) {
2265 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2267 // Test that Release() after a browser seek's DemuxerSeek IPC request has been
2268 // sent behaves similar to a regular seek: if OnDemuxerSeekDone() does not
2269 // occur until after the next Start()+SetVideoSurface(), then the player
2270 // remains pending seek done until (and resumes correct post-seek preroll
2271 // after) OnDemuxerSeekDone().
2272 BrowserSeekPlayer(false);
2273 base::TimeDelta expected_preroll_timestamp
= player_
.GetCurrentTime();
2276 EXPECT_EQ(2, demuxer_
->num_data_requests());
2277 CreateNextTextureAndSetVideoSurface();
2278 Resume(false, false);
2280 player_
.OnDemuxerSeekDone(expected_preroll_timestamp
);
2281 // Prefetch takes place first, and the decoder is not created yet.
2282 EXPECT_FALSE(GetMediaCodecBridge(false));
2283 EXPECT_TRUE(IsPrerolling(false));
2284 EXPECT_EQ(expected_preroll_timestamp
, GetPrerollTimestamp());
2285 EXPECT_EQ(expected_preroll_timestamp
, player_
.GetCurrentTime());
2286 EXPECT_EQ(3, demuxer_
->num_data_requests());
2288 // No further seek should have been requested since BrowserSeekPlayer().
2289 EXPECT_EQ(1, demuxer_
->num_seek_requests());
2291 // Decoder will be created once data is received.
2292 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
2293 EXPECT_TRUE(GetMediaCodecBridge(false));
2294 WaitForVideoDecodeDone();
2297 // TODO(xhwang): Once we add tests to cover DrmBridge, update this test to
2298 // also verify that the job is successfully created if SetDrmBridge(), Start()
2299 // and eventually OnMediaCrypto() occur. This would increase test coverage of
2300 // http://crbug.com/313470 and allow us to remove inspection of internal player
2301 // pending event state. See http://crbug.com/313860.
2302 TEST_F(MediaSourcePlayerTest
, SurfaceChangeClearedEvenIfMediaCryptoAbsent
) {
2303 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2305 // Test that |SURFACE_CHANGE_EVENT_PENDING| is not pending after
2306 // SetVideoSurface() for a player configured for encrypted video, when the
2307 // player has not yet received media crypto.
2308 DemuxerConfigs configs
= CreateVideoDemuxerConfigs(false);
2309 configs
.is_video_encrypted
= true;
2311 player_
.OnDemuxerConfigsAvailable(configs
);
2312 CreateNextTextureAndSetVideoSurface();
2313 EXPECT_FALSE(GetMediaCodecBridge(false));
2316 TEST_F(MediaSourcePlayerTest
, CurrentTimeUpdatedWhileDecoderStarved
) {
2317 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2319 // Test that current time is updated while decoder is starved.
2320 StartAudioDecoderJob();
2321 DecodeAudioDataUntilOutputBecomesAvailable();
2323 // Trigger starvation while the decoder is decoding.
2324 player_
.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(3));
2325 manager_
.ResetTimestampUpdated();
2326 TriggerPlayerStarvation();
2327 WaitForAudioDecodeDone();
2329 // Current time should be updated.
2330 EXPECT_TRUE(manager_
.timestamp_updated());
2333 TEST_F(MediaSourcePlayerTest
, CurrentTimeKeepsIncreasingAfterConfigChange
) {
2334 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2336 // Test current time keep on increasing after audio config change.
2337 // Test that current time is updated while decoder is starved.
2338 StartAudioDecoderJob();
2340 DecodeAudioDataUntilOutputBecomesAvailable();
2342 DemuxerConfigs configs
= CreateAudioDemuxerConfigs(kCodecVorbis
, true);
2343 DemuxerData data
= CreateReadFromDemuxerAckWithConfigChanged(
2345 player_
.OnDemuxerDataAvailable(data
);
2346 WaitForAudioDecodeDone();
2347 DecodeAudioDataUntilOutputBecomesAvailable();
2350 TEST_F(MediaSourcePlayerTest
, VideoMetadataChangeAfterConfigChange
) {
2351 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2353 // Test that after a config change, metadata change will be happen
2354 // after decoder is drained.
2355 StartConfigChange(false, true, 2, false);
2356 EXPECT_EQ(1, manager_
.num_metadata_changes());
2357 EXPECT_FALSE(IsDrainingDecoder(false));
2359 // Create video data with new resolutions.
2360 DemuxerData data
= CreateReadFromDemuxerAckForVideo(true);
2362 // Wait for the metadata change.
2363 while(manager_
.num_metadata_changes() == 1) {
2364 player_
.OnDemuxerDataAvailable(data
);
2365 WaitForVideoDecodeDone();
2367 EXPECT_EQ(2, manager_
.num_metadata_changes());
2368 WaitForVideoDecodeDone();
2371 TEST_F(MediaSourcePlayerTest
, RequestPlayDeniedDontPlay_Audio
) {
2372 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2374 EXPECT_EQ(demuxer_
->num_data_requests(), 0);
2375 player_
.OnDemuxerConfigsAvailable(CreateDemuxerConfigs(true, false));
2377 manager_
.set_allow_play(false);
2379 EXPECT_FALSE(player_
.IsPlaying());
2382 TEST_F(MediaSourcePlayerTest
, RequestPlayDeniedDontPlay_Video
) {
2383 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2385 EXPECT_EQ(demuxer_
->num_data_requests(), 0);
2386 player_
.OnDemuxerConfigsAvailable(CreateDemuxerConfigs(false, true));
2388 manager_
.set_allow_play(false);
2390 EXPECT_FALSE(player_
.IsPlaying());
2393 TEST_F(MediaSourcePlayerTest
, RequestPlayDeniedDontPlay_AV
) {
2394 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2396 EXPECT_EQ(demuxer_
->num_data_requests(), 0);
2397 player_
.OnDemuxerConfigsAvailable(CreateDemuxerConfigs(true, true));
2399 manager_
.set_allow_play(false);
2401 EXPECT_FALSE(player_
.IsPlaying());
2404 TEST_F(MediaSourcePlayerTest
, RequestPlayGrantedPlays
) {
2405 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2407 EXPECT_EQ(demuxer_
->num_data_requests(), 0);
2408 player_
.OnDemuxerConfigsAvailable(CreateDemuxerConfigs(true, true));
2410 manager_
.set_allow_play(true);
2412 EXPECT_TRUE(player_
.IsPlaying());
2415 } // namespace media