[BackgroundSync] Clean up some tests
[chromium-blink-merge.git] / media / base / android / media_source_player_unittest.cc
blobca13a060cdea1a2bbbbb744926247b9f3440c63d
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.
5 #include <string>
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 "testing/gmock/include/gmock/gmock.h"
22 #include "ui/gl/android/surface_texture.h"
24 namespace media {
26 // Helper macro to skip the test if MediaCodecBridge isn't available.
27 #define SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE() \
28 do { \
29 if (!MediaCodecBridge::IsAvailable()) { \
30 VLOG(0) << "Could not run test - not supported on device."; \
31 return; \
32 } \
33 } while (0)
35 const base::TimeDelta kDefaultDuration =
36 base::TimeDelta::FromMilliseconds(10000);
38 // TODO(wolenetz/qinmin): Simplify tests with more effective mock usage, and
39 // fix flaky pointer-based MDJ inequality testing. See http://crbug.com/327839.
41 // Mock of MediaPlayerManager for testing purpose.
42 class MockMediaPlayerManager : public MediaPlayerManager {
43 public:
44 explicit MockMediaPlayerManager(base::MessageLoop* message_loop)
45 : message_loop_(message_loop),
46 playback_completed_(false),
47 num_resources_requested_(0),
48 num_metadata_changes_(0),
49 timestamp_updated_(false),
50 is_audible_(false),
51 is_delay_expired_(false),
52 allow_play_(true) {}
53 ~MockMediaPlayerManager() override {}
55 // MediaPlayerManager implementation.
56 MediaResourceGetter* GetMediaResourceGetter() override { return NULL; }
57 MediaUrlInterceptor* GetMediaUrlInterceptor() override { return NULL; }
58 void OnTimeUpdate(int player_id,
59 base::TimeDelta current_time,
60 base::TimeTicks current_time_ticks) override {
61 timestamp_updated_ = true;
63 void OnMediaMetadataChanged(int player_id,
64 base::TimeDelta duration,
65 int width,
66 int height,
67 bool success) override {
68 num_metadata_changes_++;
70 void OnPlaybackComplete(int player_id) override {
71 playback_completed_ = true;
72 if (message_loop_->is_running())
73 message_loop_->Quit();
75 void OnMediaInterrupted(int player_id) override {}
76 void OnBufferingUpdate(int player_id, int percentage) override {}
77 void OnSeekComplete(int player_id,
78 const base::TimeDelta& current_time) override {}
79 void OnError(int player_id, int error) override {}
80 void OnVideoSizeChanged(int player_id, int width, int height) override {}
81 void OnWaitingForDecryptionKey(int player_id) override {}
82 MediaPlayerAndroid* GetFullscreenPlayer() override { return NULL; }
83 MediaPlayerAndroid* GetPlayer(int player_id) override { return NULL; }
84 void RequestFullScreen(int player_id) override {}
86 bool RequestPlay(int player_id) override {
87 return allow_play_;
90 void OnAudibleStateChanged(int player_id, bool is_audible_now) override {
91 is_audible_ = is_audible_now;
94 bool playback_completed() const {
95 return playback_completed_;
98 int num_resources_requested() const {
99 return num_resources_requested_;
102 int num_metadata_changes() const {
103 return num_metadata_changes_;
106 void OnMediaResourcesRequested(int player_id) {
107 num_resources_requested_++;
110 bool timestamp_updated() const {
111 return timestamp_updated_;
114 void ResetTimestampUpdated() {
115 timestamp_updated_ = false;
118 bool is_audible() const {
119 return is_audible_;
122 bool is_delay_expired() const {
123 return is_delay_expired_;
126 void SetDelayExpired(bool value) {
127 is_delay_expired_ = value;
130 void set_allow_play(bool value) {
131 allow_play_ = value;
134 private:
135 base::MessageLoop* message_loop_;
136 bool playback_completed_;
137 // The number of resource requests this object has seen.
138 int num_resources_requested_;
139 // The number of metadata changes reported by the player.
140 int num_metadata_changes_;
141 // Playback timestamp was updated.
142 bool timestamp_updated_;
143 // Audible state of the pipeline
144 bool is_audible_;
145 // Helper flag to ensure delay for WaitForDelay().
146 bool is_delay_expired_;
147 // Whether the manager will allow players that request playing.
148 bool allow_play_;
150 DISALLOW_COPY_AND_ASSIGN(MockMediaPlayerManager);
153 class MockDemuxerAndroid : public DemuxerAndroid {
154 public:
155 explicit MockDemuxerAndroid(base::MessageLoop* message_loop)
156 : message_loop_(message_loop),
157 num_data_requests_(0),
158 num_seek_requests_(0),
159 num_browser_seek_requests_(0) {}
160 ~MockDemuxerAndroid() override {}
162 void Initialize(DemuxerAndroidClient* client) override {}
163 void RequestDemuxerData(DemuxerStream::Type type) override {
164 num_data_requests_++;
165 if (message_loop_->is_running())
166 message_loop_->Quit();
168 void RequestDemuxerSeek(const base::TimeDelta& time_to_seek,
169 bool is_browser_seek) override {
170 num_seek_requests_++;
171 if (is_browser_seek)
172 num_browser_seek_requests_++;
175 int num_data_requests() const { return num_data_requests_; }
176 int num_seek_requests() const { return num_seek_requests_; }
177 int num_browser_seek_requests() const { return num_browser_seek_requests_; }
179 private:
180 base::MessageLoop* message_loop_;
182 // The number of encoded data requests this object has seen.
183 int num_data_requests_;
185 // The number of regular and browser seek requests this object has seen.
186 int num_seek_requests_;
188 // The number of browser seek requests this object has seen.
189 int num_browser_seek_requests_;
191 DISALLOW_COPY_AND_ASSIGN(MockDemuxerAndroid);
194 class MediaSourcePlayerTest : public testing::Test {
195 public:
196 MediaSourcePlayerTest()
197 : manager_(&message_loop_),
198 demuxer_(new MockDemuxerAndroid(&message_loop_)),
199 player_(0, &manager_,
200 base::Bind(&MockMediaPlayerManager::OnMediaResourcesRequested,
201 base::Unretained(&manager_)),
202 scoped_ptr<DemuxerAndroid>(demuxer_),
203 GURL()),
204 decoder_callback_hook_executed_(false),
205 surface_texture_a_is_next_(true) {}
207 ~MediaSourcePlayerTest() override {}
209 protected:
210 // Get the decoder job from the MediaSourcePlayer. The return value must not
211 // be NULL.
212 MediaDecoderJob* GetMediaDecoderJob(bool is_audio) {
213 if (is_audio) {
214 return reinterpret_cast<MediaDecoderJob*>(
215 player_.audio_decoder_job_.get());
217 return reinterpret_cast<MediaDecoderJob*>(
218 player_.video_decoder_job_.get());
221 // Get the MediaCodecBridge from the decoder job. The return value could be
222 // NULL if the decoder is not yet created.
223 MediaCodecBridge* GetMediaCodecBridge(bool is_audio) {
224 if (is_audio)
225 return player_.audio_decoder_job_->media_codec_bridge_.get();
226 return player_.video_decoder_job_->media_codec_bridge_.get();
229 // Get the per-job prerolling status from the MediaSourcePlayer's job matching
230 // |is_audio|. Caller must guard against NPE if the player's job is NULL.
231 bool IsPrerolling(bool is_audio) {
232 return GetMediaDecoderJob(is_audio)->prerolling_;
235 // Get the preroll timestamp from the MediaSourcePlayer.
236 base::TimeDelta GetPrerollTimestamp() {
237 return player_.preroll_timestamp_;
240 // Simulate player has reached starvation timeout.
241 void TriggerPlayerStarvation() {
242 player_.decoder_starvation_callback_.Cancel();
243 player_.OnDecoderStarved();
246 // Release() the player.
247 void ReleasePlayer() {
248 EXPECT_TRUE(player_.IsPlaying());
249 player_.Release();
250 EXPECT_FALSE(player_.IsPlaying());
253 // Upon the next successful decode callback, post a task to call Release()
254 // on the |player_|. TEST_F's do not have access to the private player
255 // members, hence this helper method.
256 // Prevent usage creep of MSP::set_decode_callback_for_testing() by
257 // only using it for the ReleaseWithOnPrefetchDoneAlreadyPosted test.
258 void OnNextTestDecodeCallbackPostTaskToReleasePlayer() {
259 DCHECK_EQ(&message_loop_, base::MessageLoop::current());
260 player_.set_decode_callback_for_testing(media::BindToCurrentLoop(
261 base::Bind(
262 &MediaSourcePlayerTest::ReleaseWithPendingPrefetchDoneVerification,
263 base::Unretained(this))));
266 // Asynch test callback posted upon decode completion to verify that a pending
267 // prefetch done event is not cleared across |player_|'s Release(). This helps
268 // ensure the ReleaseWithOnPrefetchDoneAlreadyPosted test scenario is met.
269 void ReleaseWithPendingPrefetchDoneVerification() {
270 EXPECT_TRUE(player_.IsEventPending(player_.PREFETCH_DONE_EVENT_PENDING));
271 ReleasePlayer();
272 EXPECT_TRUE(player_.IsEventPending(player_.PREFETCH_DONE_EVENT_PENDING));
273 EXPECT_FALSE(decoder_callback_hook_executed_);
274 EXPECT_FALSE(GetMediaCodecBridge(true));
275 decoder_callback_hook_executed_ = true;
278 DemuxerConfigs CreateAudioDemuxerConfigs(AudioCodec audio_codec,
279 bool use_low_sample_rate) {
280 DemuxerConfigs configs;
281 configs.audio_codec = audio_codec;
282 configs.audio_channels = 2;
283 configs.is_audio_encrypted = false;
284 configs.duration = kDefaultDuration;
286 if (audio_codec == kCodecVorbis) {
287 configs.audio_sampling_rate = use_low_sample_rate ? 11025 : 44100;
288 scoped_refptr<DecoderBuffer> buffer = ReadTestDataFile(
289 "vorbis-extradata");
290 configs.audio_extra_data = std::vector<uint8>(
291 buffer->data(),
292 buffer->data() + buffer->data_size());
293 return configs;
296 // Other codecs are not yet supported by this helper.
297 EXPECT_EQ(audio_codec, kCodecAAC);
299 configs.audio_sampling_rate = 48000;
300 uint8 aac_extra_data[] = { 0x13, 0x10 };
301 configs.audio_extra_data = std::vector<uint8>(
302 aac_extra_data,
303 aac_extra_data + 2);
304 return configs;
307 DemuxerConfigs CreateVideoDemuxerConfigs(bool use_larger_size) {
308 DemuxerConfigs configs;
309 configs.video_codec = kCodecVP8;
310 configs.video_size =
311 use_larger_size ? gfx::Size(640, 240) : gfx::Size(320, 240);
312 configs.is_video_encrypted = false;
313 configs.duration = kDefaultDuration;
314 return configs;
317 DemuxerConfigs CreateAudioVideoDemuxerConfigs() {
318 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, false);
319 configs.video_codec = kCodecVP8;
320 configs.video_size = gfx::Size(320, 240);
321 configs.is_video_encrypted = false;
322 return configs;
325 DemuxerConfigs CreateDemuxerConfigs(bool have_audio, bool have_video) {
326 DCHECK(have_audio || have_video);
328 if (have_audio && !have_video)
329 return CreateAudioDemuxerConfigs(kCodecVorbis, false);
331 if (have_video && !have_audio)
332 return CreateVideoDemuxerConfigs(false);
334 return CreateAudioVideoDemuxerConfigs();
337 // Starts an audio decoder job.
338 void StartAudioDecoderJob() {
339 Start(CreateAudioDemuxerConfigs(kCodecVorbis, false));
342 // Starts a video decoder job.
343 void StartVideoDecoderJob() {
344 Start(CreateVideoDemuxerConfigs(false));
347 // Starts decoding the data.
348 void Start(const DemuxerConfigs& configs) {
349 EXPECT_EQ(demuxer_->num_data_requests(), 0);
350 player_.OnDemuxerConfigsAvailable(configs);
351 player_.Start();
353 EXPECT_TRUE(player_.IsPlaying());
354 int expected_num_requests = (player_.HasAudio() ? 1 : 0) +
355 (player_.HasVideo() ? 1 : 0);
356 EXPECT_EQ(expected_num_requests, demuxer_->num_data_requests());
359 // Resumes decoding the data. Verifies player behavior relative to
360 // |expect_player_requests_audio_data| and
361 // |expect_player_requests_video_data|.
362 void Resume(bool expect_player_requests_audio_data,
363 bool expect_player_requests_video_data) {
364 EXPECT_FALSE(player_.IsPlaying());
365 EXPECT_TRUE(player_.HasVideo() || player_.HasAudio());
366 int original_num_data_requests = demuxer_->num_data_requests();
367 int expected_request_delta =
368 (expect_player_requests_audio_data ? 1 : 0) +
369 (expect_player_requests_video_data ? 1 : 0);
371 player_.Start();
373 EXPECT_TRUE(player_.IsPlaying());
374 EXPECT_EQ(original_num_data_requests + expected_request_delta,
375 demuxer_->num_data_requests());
378 // Keeps decoding audio data until the decoder starts to output samples.
379 // Gives up if no audio output after decoding 10 frames.
380 void DecodeAudioDataUntilOutputBecomesAvailable() {
381 EXPECT_TRUE(player_.IsPlaying());
382 base::TimeDelta current_time = player_.GetCurrentTime();
383 base::TimeDelta start_timestamp = current_time;
384 for (int i = 0; i < 10; ++i) {
385 manager_.ResetTimestampUpdated();
386 player_.OnDemuxerDataAvailable(
387 CreateReadFromDemuxerAckForAudio(i > 3 ? 3 : i));
388 WaitForAudioDecodeDone();
389 base::TimeDelta new_current_time = player_.GetCurrentTime();
390 EXPECT_LE(current_time.InMilliseconds(),
391 new_current_time.InMilliseconds());
392 current_time = new_current_time;
393 if (manager_.timestamp_updated()) {
394 // TODO(qinmin): the current time is from the decoder thread and it does
395 // not take the delay from posting the task into consideration.
396 // http://crbug.com/421616.
397 EXPECT_LE(start_timestamp.InMillisecondsF(),
398 new_current_time.InMillisecondsF());
399 return;
402 EXPECT_TRUE(false);
405 AccessUnit CreateAccessUnitWithData(bool is_audio, int audio_packet_id,
406 bool use_large_size_video) {
407 AccessUnit unit;
409 unit.status = DemuxerStream::kOk;
410 scoped_refptr<DecoderBuffer> buffer;
411 if (is_audio) {
412 buffer = ReadTestDataFile(
413 base::StringPrintf("vorbis-packet-%d", audio_packet_id));
414 } else {
415 buffer = ReadTestDataFile(
416 use_large_size_video ? "vp8-I-frame-640x240" : "vp8-I-frame-320x240");
418 unit.data = std::vector<uint8>(
419 buffer->data(), buffer->data() + buffer->data_size());
421 if (is_audio) {
422 // Vorbis needs 4 extra bytes padding on Android to decode properly. Check
423 // NuMediaExtractor.cpp in Android source code.
424 uint8 padding[4] = { 0xff , 0xff , 0xff , 0xff };
425 unit.data.insert(unit.data.end(), padding, padding + 4);
428 return unit;
431 DemuxerData CreateReadFromDemuxerAckForAudio(int packet_id) {
432 DemuxerData data;
433 data.type = DemuxerStream::AUDIO;
434 data.access_units.resize(1);
435 data.access_units[0] = CreateAccessUnitWithData(true, packet_id, false);
437 return data;
440 DemuxerData CreateReadFromDemuxerAckForVideo(bool use_large_size) {
441 DemuxerData data;
442 data.type = DemuxerStream::VIDEO;
443 data.access_units.resize(1);
444 data.access_units[0] = CreateAccessUnitWithData(false, 0, use_large_size);
445 return data;
448 DemuxerData CreateEOSAck(bool is_audio) {
449 DemuxerData data;
450 data.type = is_audio ? DemuxerStream::AUDIO : DemuxerStream::VIDEO;
451 data.access_units.resize(1);
452 data.access_units[0].status = DemuxerStream::kOk;
453 data.access_units[0].is_end_of_stream = true;
454 return data;
457 DemuxerData CreateAbortedAck(bool is_audio) {
458 DemuxerData data;
459 data.type = is_audio ? DemuxerStream::AUDIO : DemuxerStream::VIDEO;
460 data.access_units.resize(1);
461 data.access_units[0].status = DemuxerStream::kAborted;
462 return data;
465 bool HasData(bool is_audio) {
466 return GetMediaDecoderJob(is_audio)->HasData();
469 // Helper method for use at test start. It starts an audio decoder job and
470 // immediately feeds it some data to decode. Then, without letting the decoder
471 // job complete a decode cycle, it also starts player SeekTo(). Upon return,
472 // the player should not yet have sent the DemuxerSeek IPC request, though
473 // seek event should be pending. The audio decoder job will also still be
474 // decoding.
475 void StartAudioDecoderJobAndSeekToWhileDecoding(
476 const base::TimeDelta& seek_time) {
477 EXPECT_FALSE(GetMediaCodecBridge(true));
478 EXPECT_FALSE(player_.IsPlaying());
479 EXPECT_EQ(0, demuxer_->num_data_requests());
480 EXPECT_EQ(0.0, GetPrerollTimestamp().InMillisecondsF());
481 EXPECT_EQ(player_.GetCurrentTime(), GetPrerollTimestamp());
482 StartAudioDecoderJob();
483 EXPECT_FALSE(GetMediaDecoderJob(true)->is_decoding());
484 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
485 EXPECT_EQ(2, demuxer_->num_data_requests());
486 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
487 player_.SeekTo(seek_time);
488 EXPECT_EQ(0.0, GetPrerollTimestamp().InMillisecondsF());
489 EXPECT_EQ(0, demuxer_->num_seek_requests());
490 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
493 // Seek, including simulated receipt of |kAborted| read between SeekTo() and
494 // OnDemuxerSeekDone(). Use this helper method only when the player already
495 // has created the media codec bridge. Exactly one request for more data is
496 // expected following the seek, so use this helper for players with only audio
497 // or only video.
498 void SeekPlayerWithAbort(bool is_audio, const base::TimeDelta& seek_time) {
499 int original_num_seeks = demuxer_->num_seek_requests();
500 int original_num_data_requests = demuxer_->num_data_requests();
502 // Initiate a seek. Skip the round-trip of requesting seek from renderer.
503 // Instead behave as if the renderer has asked us to seek.
504 player_.SeekTo(seek_time);
506 // Verify that the seek does not occur until previously outstanding data
507 // request is satisfied.
508 EXPECT_EQ(original_num_seeks, demuxer_->num_seek_requests());
510 // Simulate seeking causes the demuxer to abort the outstanding read
511 // caused by the seek.
512 player_.OnDemuxerDataAvailable(CreateAbortedAck(is_audio));
514 // Wait for the decode job to finish so we can process the seek request.
515 WaitForDecodeDone(is_audio, !is_audio);
517 // Verify that the seek is requested.
518 EXPECT_EQ(original_num_seeks + 1, demuxer_->num_seek_requests());
520 // Send back the seek done notification. This should trigger the player to
521 // call OnReadFromDemuxer() again.
522 EXPECT_EQ(original_num_data_requests, demuxer_->num_data_requests());
523 player_.OnDemuxerSeekDone(kNoTimestamp());
524 EXPECT_EQ(original_num_data_requests + 1, demuxer_->num_data_requests());
526 // No other seek should have been requested.
527 EXPECT_EQ(original_num_seeks + 1, demuxer_->num_seek_requests());
530 // Preroll the decoder job to |target_timestamp|. The first access unit
531 // to decode will have a timestamp equal to |start_timestamp|.
532 // |is_clock_manager| indicates whether the decoder serves as the clock
533 // manager for the player.
534 // TODO(qinmin): Add additional test cases for out-of-order decodes.
535 // See http://crbug.com/331421.
536 void PrerollDecoderToTime(bool is_audio,
537 const base::TimeDelta& start_timestamp,
538 const base::TimeDelta& target_timestamp,
539 bool is_clock_manager) {
540 // For streams with both audio and video, it is possible that audio rolls
541 // past the |target_timestamp|. As a result, the current time may be larger
542 // than the |target_timestamp| for video as it may not be the clock manager.
543 EXPECT_TRUE(!is_clock_manager ||
544 target_timestamp == player_.GetCurrentTime());
545 // |start_timestamp| must be smaller than |target_timestamp|.
546 EXPECT_LE(start_timestamp, target_timestamp);
547 DemuxerData data = is_audio ? CreateReadFromDemuxerAckForAudio(1) :
548 CreateReadFromDemuxerAckForVideo(false);
549 int current_timestamp = start_timestamp.InMilliseconds();
551 // Send some data with access unit timestamps before the |target_timestamp|,
552 // and continue sending the data until preroll finishes.
553 // This simulates the common condition that AUs received after browser
554 // seek begin with timestamps before the seek target, and don't
555 // immediately complete preroll.
556 while (IsPrerolling(is_audio)) {
557 data.access_units[0].timestamp =
558 base::TimeDelta::FromMilliseconds(current_timestamp);
559 player_.OnDemuxerDataAvailable(data);
560 EXPECT_TRUE(GetMediaDecoderJob(is_audio)->is_decoding());
561 EXPECT_TRUE(GetMediaCodecBridge(is_audio));
562 EXPECT_TRUE(!is_clock_manager ||
563 target_timestamp == player_.GetCurrentTime());
564 current_timestamp += 30;
565 WaitForDecodeDone(is_audio, !is_audio);
567 EXPECT_LE(target_timestamp, player_.GetCurrentTime());
570 void PlayAudioForTimeInterval(const base::TimeDelta& start_timestamp,
571 const base::TimeDelta& target_timestamp ) {
573 DemuxerData data = CreateReadFromDemuxerAckForAudio(1);
574 int current_timestamp = start_timestamp.InMilliseconds();
575 int stop_timestamp = target_timestamp.InMilliseconds();
576 while (current_timestamp < stop_timestamp) {
577 data.access_units[0].timestamp =
578 base::TimeDelta::FromMilliseconds(current_timestamp);
579 player_.OnDemuxerDataAvailable(data);
580 current_timestamp += 30;
581 WaitForAudioDecodeDone();
585 void WaitForDelay(const base::TimeDelta& delay) {
586 // Let the message_loop_ process events.
587 // We post delayed task and RunUnitilIdle() until it signals.
589 manager_.SetDelayExpired(false);
590 message_loop_.PostDelayedTask(
591 FROM_HERE,
592 base::Bind(&MockMediaPlayerManager::SetDelayExpired,
593 base::Unretained(&manager_),
594 true),
595 delay);
597 while (!manager_.is_delay_expired())
598 message_loop_.RunUntilIdle();
601 DemuxerData CreateReadFromDemuxerAckWithConfigChanged(
602 bool is_audio,
603 int config_unit_index,
604 const DemuxerConfigs& configs) {
605 DemuxerData data;
606 data.type = is_audio ? DemuxerStream::AUDIO : DemuxerStream::VIDEO;
607 data.access_units.resize(config_unit_index + 1);
609 for (int i = 0; i < config_unit_index; ++i)
610 data.access_units[i] = CreateAccessUnitWithData(is_audio, i, false);
612 data.access_units[config_unit_index].status = DemuxerStream::kConfigChanged;
613 data.demuxer_configs.resize(1);
614 data.demuxer_configs[0] = configs;
615 return data;
618 // Valid only for video-only player tests. If |trigger_with_release_start| is
619 // true, triggers the browser seek with a Release() + video data received +
620 // Start() with a new surface. If false, triggers the browser seek by
621 // setting a new video surface after beginning decode of received video data.
622 // Such data receipt causes possibility that an I-frame is not next, and
623 // browser seek results once decode completes and surface change processing
624 // begins.
625 void BrowserSeekPlayer(bool trigger_with_release_start) {
626 int expected_num_data_requests = demuxer_->num_data_requests() + 2;
627 int expected_num_seek_requests = demuxer_->num_seek_requests();
628 int expected_num_browser_seek_requests =
629 demuxer_->num_browser_seek_requests();
631 CreateNextTextureAndSetVideoSurface();
632 StartVideoDecoderJob();
633 if (trigger_with_release_start) {
634 // Consume the first frame, so that the next VideoDecoderJob will not
635 // inherit the I-frame from the previous decoder.
636 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
637 ReleasePlayer();
638 WaitForVideoDecodeDone();
640 // Simulate demuxer's response to the video data request. The data will be
641 // passed to the next MediaCodecBridge.
642 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
643 EXPECT_FALSE(GetMediaCodecBridge(false));
644 EXPECT_FALSE(player_.IsPlaying());
645 EXPECT_EQ(expected_num_seek_requests, demuxer_->num_seek_requests());
647 CreateNextTextureAndSetVideoSurface();
648 Resume(false, false);
649 EXPECT_FALSE(GetMediaCodecBridge(false));
651 // Run the message loop so that prefetch will complete.
652 while (expected_num_seek_requests == demuxer_->num_seek_requests())
653 message_loop_.RunUntilIdle();
654 } else {
655 // Simulate demuxer's response to the video data request.
656 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
658 // While the decoder is decoding, trigger a browser seek by changing
659 // surface. Demuxer does not know of browser seek in advance, so no
660 // |kAborted| data is required (though |kAborted| can certainly occur for
661 // any pending read in reality due to renderer preparing for a regular
662 // seek).
663 CreateNextTextureAndSetVideoSurface();
665 // Browser seek should not begin until decoding has completed.
666 EXPECT_TRUE(GetMediaCodecBridge(false));
667 EXPECT_EQ(expected_num_seek_requests, demuxer_->num_seek_requests());
669 // Wait for the media codec bridge to finish decoding and be reset pending
670 // the browser seek.
671 WaitForVideoDecodeDone();
672 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
675 // Only one browser seek should have been initiated, and no further data
676 // should have been requested.
677 expected_num_seek_requests++;
678 expected_num_browser_seek_requests++;
679 EXPECT_EQ(expected_num_seek_requests, demuxer_->num_seek_requests());
680 EXPECT_EQ(expected_num_browser_seek_requests,
681 demuxer_->num_browser_seek_requests());
682 EXPECT_EQ(expected_num_data_requests, demuxer_->num_data_requests());
685 // Creates a new media codec bridge and feeds it data ending with a
686 // |kConfigChanged| access unit. If |config_unit_in_prefetch| is true, sends
687 // feeds the config change AU in response to the job's first read request
688 // (prefetch). If false, regular data is fed and decoded prior to feeding the
689 // config change AU in response to the second data request (after prefetch
690 // completed). |config_unit_index| controls which access unit is
691 // |kConfigChanged|. If |enable_adaptive_playback| is true, config change will
692 // not cause the decoder to recreate the media codec bridge. Otherwise, the
693 // decoder has to drain all its data before recreating the new codec.
694 void SendConfigChangeToDecoder(bool is_audio,
695 bool config_unit_in_prefetch,
696 int config_unit_index,
697 bool enable_adaptive_playback) {
698 EXPECT_FALSE(GetMediaCodecBridge(is_audio));
699 if (is_audio) {
700 StartAudioDecoderJob();
701 } else {
702 CreateNextTextureAndSetVideoSurface();
703 StartVideoDecoderJob();
706 int expected_num_data_requests = demuxer_->num_data_requests();
707 // Feed and decode a standalone access unit so the player exits prefetch.
708 if (!config_unit_in_prefetch) {
709 if (is_audio) {
710 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
711 } else {
712 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
713 EnableAdaptiveVideoPlayback(enable_adaptive_playback);
716 WaitForDecodeDone(is_audio, !is_audio);
718 // We should have completed the prefetch phase at this point.
719 expected_num_data_requests++;
720 EXPECT_EQ(expected_num_data_requests, demuxer_->num_data_requests());
723 DemuxerConfigs configs = is_audio ?
724 CreateAudioDemuxerConfigs(kCodecVorbis, true) :
725 CreateVideoDemuxerConfigs(true);
726 // Feed and decode access units with data for any units prior to
727 // |config_unit_index|, and a |kConfigChanged| unit at that index.
728 // Player should prepare to reconfigure the decoder job, and should request
729 // new demuxer configs.
730 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckWithConfigChanged(
731 is_audio, config_unit_index, configs));
733 expected_num_data_requests++;
734 EXPECT_EQ(expected_num_data_requests, demuxer_->num_data_requests());
735 if (is_audio)
736 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
737 else
738 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(true));
740 // If the adaptive playback setting was not passed to the MediaCodecBridge
741 // earlier, do it here.
742 if (config_unit_in_prefetch && !is_audio)
743 EnableAdaptiveVideoPlayback(enable_adaptive_playback);
746 // Send a config change to the decoder job and drain the decoder so that the
747 // config change is processed.
748 void StartConfigChange(bool is_audio,
749 bool config_unit_in_prefetch,
750 int config_unit_index,
751 bool enable_adaptive_playback) {
752 SendConfigChangeToDecoder(is_audio, config_unit_in_prefetch,
753 config_unit_index, enable_adaptive_playback);
755 EXPECT_EQ(!config_unit_in_prefetch && !enable_adaptive_playback &&
756 config_unit_index == 0, IsDrainingDecoder(is_audio));
757 int expected_num_data_requests = demuxer_->num_data_requests();
758 // Run until decoder starts to request new data.
759 while (demuxer_->num_data_requests() == expected_num_data_requests)
760 message_loop_.RunUntilIdle();
761 EXPECT_FALSE(IsDrainingDecoder(is_audio));
764 void EnableAdaptiveVideoPlayback(bool enable) {
765 EXPECT_TRUE(GetMediaCodecBridge(false));
766 static_cast<VideoCodecBridge*>(GetMediaCodecBridge(false))->
767 set_adaptive_playback_supported_for_testing(
768 enable ? 1 : 0);
771 void CreateNextTextureAndSetVideoSurface() {
772 gfx::SurfaceTexture* surface_texture;
773 if (surface_texture_a_is_next_) {
774 surface_texture_a_ = gfx::SurfaceTexture::Create(next_texture_id_++);
775 surface_texture = surface_texture_a_.get();
776 } else {
777 surface_texture_b_ = gfx::SurfaceTexture::Create(next_texture_id_++);
778 surface_texture = surface_texture_b_.get();
781 surface_texture_a_is_next_ = !surface_texture_a_is_next_;
782 gfx::ScopedJavaSurface surface = gfx::ScopedJavaSurface(surface_texture);
783 player_.SetVideoSurface(surface.Pass());
786 // Wait for one or both of the jobs to complete decoding. Media codec bridges
787 // are assumed to exist for any stream whose decode completion is awaited.
788 void WaitForDecodeDone(bool wait_for_audio, bool wait_for_video) {
789 DCHECK(wait_for_audio || wait_for_video);
790 while ((wait_for_audio && GetMediaCodecBridge(true) &&
791 GetMediaDecoderJob(true)->HasData() &&
792 GetMediaDecoderJob(true)->is_decoding()) ||
793 (wait_for_video && GetMediaCodecBridge(false) &&
794 GetMediaDecoderJob(false)->HasData() &&
795 GetMediaDecoderJob(false)->is_decoding())) {
796 message_loop_.RunUntilIdle();
800 void WaitForAudioDecodeDone() {
801 WaitForDecodeDone(true, false);
804 void WaitForVideoDecodeDone() {
805 WaitForDecodeDone(false, true);
808 void WaitForAudioVideoDecodeDone() {
809 WaitForDecodeDone(true, true);
812 // If |send_eos| is true, generates EOS for the stream corresponding to
813 // |eos_for_audio|. Verifies that playback completes and no further data
814 // is requested.
815 // If |send_eos| is false, then it is assumed that caller previously arranged
816 // for player to receive EOS for each stream, but the player has not yet
817 // decoded all of them. In this case, |eos_for_audio| is ignored.
818 void VerifyPlaybackCompletesOnEOSDecode(bool send_eos, bool eos_for_audio) {
819 int original_num_data_requests = demuxer_->num_data_requests();
820 if (send_eos)
821 player_.OnDemuxerDataAvailable(CreateEOSAck(eos_for_audio));
822 EXPECT_FALSE(manager_.playback_completed());
823 message_loop_.Run();
824 EXPECT_TRUE(manager_.playback_completed());
825 EXPECT_EQ(original_num_data_requests, demuxer_->num_data_requests());
828 void VerifyCompletedPlaybackResumesOnSeekPlusStart(bool have_audio,
829 bool have_video) {
830 DCHECK(have_audio || have_video);
832 EXPECT_TRUE(manager_.playback_completed());
834 player_.SeekTo(base::TimeDelta());
835 player_.OnDemuxerSeekDone(kNoTimestamp());
836 Resume(have_audio, have_video);
839 // Starts the appropriate decoder jobs according to |have_audio| and
840 // |have_video|. Then starts seek during decode of EOS or non-EOS according to
841 // |eos_audio| and |eos_video|. Simulates seek completion and verifies that
842 // playback never completed. |eos_{audio,video}| is ignored if the
843 // corresponding |have_{audio,video}| is false.
844 void VerifySeekDuringEOSDecodePreventsPlaybackCompletion(bool have_audio,
845 bool have_video,
846 bool eos_audio,
847 bool eos_video) {
848 DCHECK(have_audio || have_video);
850 if (have_video)
851 CreateNextTextureAndSetVideoSurface();
853 Start(CreateDemuxerConfigs(have_audio, have_video));
855 if (have_audio)
856 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
858 if (have_video)
859 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
861 // Run until more data is requested a number of times equal to the number of
862 // media types configured. Since prefetching may be in progress, we cannot
863 // reliably expect Run() to complete until we have sent demuxer data for all
864 // configured media types, above.
865 WaitForDecodeDone(have_audio, have_video);
867 // Simulate seek while decoding EOS or non-EOS for the appropriate
868 // stream(s).
869 if (have_audio) {
870 if (eos_audio)
871 player_.OnDemuxerDataAvailable(CreateEOSAck(true));
872 else
873 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(1));
876 if (have_video) {
877 if (eos_video)
878 player_.OnDemuxerDataAvailable(CreateEOSAck(false));
879 else
880 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
883 player_.SeekTo(base::TimeDelta());
884 EXPECT_EQ(0, demuxer_->num_seek_requests());
885 WaitForDecodeDone(have_audio, have_video);
886 EXPECT_EQ(1, demuxer_->num_seek_requests());
888 player_.OnDemuxerSeekDone(kNoTimestamp());
889 EXPECT_FALSE(manager_.playback_completed());
892 base::TimeTicks StartTimeTicks() {
893 return player_.start_time_ticks_;
896 bool IsRequestingDemuxerData(bool is_audio) {
897 return GetMediaDecoderJob(is_audio)->is_requesting_demuxer_data_;
900 bool IsDrainingDecoder(bool is_audio) {
901 return GetMediaDecoderJob(is_audio)->drain_decoder_;
904 protected:
905 base::MessageLoop message_loop_;
906 MockMediaPlayerManager manager_;
907 MockDemuxerAndroid* demuxer_; // Owned by |player_|.
908 MediaSourcePlayer player_;
910 // Track whether a possibly async decoder callback test hook has run.
911 bool decoder_callback_hook_executed_;
913 // We need to keep the surface texture while the decoder is actively decoding.
914 // Otherwise, it may trigger unexpected crashes on some devices. To switch
915 // surfaces, tests need to create a new surface texture without releasing
916 // their previous one. In CreateNextTextureAndSetVideoSurface(), we toggle
917 // between two surface textures, only replacing the N-2 texture. Assumption is
918 // that no more than N-1 texture is in use by decoder when
919 // CreateNextTextureAndSetVideoSurface() is called.
920 scoped_refptr<gfx::SurfaceTexture> surface_texture_a_;
921 scoped_refptr<gfx::SurfaceTexture> surface_texture_b_;
922 bool surface_texture_a_is_next_;
923 int next_texture_id_;
925 bool verify_not_audible_is_called_;
927 DISALLOW_COPY_AND_ASSIGN(MediaSourcePlayerTest);
930 TEST_F(MediaSourcePlayerTest, StartAudioDecoderWithValidConfig) {
931 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
933 // Test audio codec will be created when valid configs and data are passed to
934 // the audio decoder job.
935 StartAudioDecoderJob();
936 EXPECT_EQ(0, demuxer_->num_seek_requests());
937 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
938 EXPECT_TRUE(GetMediaCodecBridge(true));
941 TEST_F(MediaSourcePlayerTest, StartAudioDecoderWithInvalidConfig) {
942 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
944 // Test audio decoder job will not be created when failed to start the codec.
945 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, false);
946 // Replace with invalid |audio_extra_data|
947 configs.audio_extra_data.clear();
948 uint8 invalid_codec_data[] = { 0x00, 0xff, 0xff, 0xff, 0xff };
949 configs.audio_extra_data.insert(configs.audio_extra_data.begin(),
950 invalid_codec_data, invalid_codec_data + 4);
951 Start(configs);
953 // Decoder is not created after data is received.
954 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
955 EXPECT_FALSE(GetMediaCodecBridge(true));
958 // timav
959 TEST_F(MediaSourcePlayerTest, AudioDecoderSetsAudibleState) {
960 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
962 // No data arrived yet
963 EXPECT_FALSE(manager_.is_audible());
965 // Initialize decoder
966 StartAudioDecoderJob();
967 player_.SetVolume(1.0);
969 // Process frames until prerolling is done.
970 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100));
971 EXPECT_TRUE(IsPrerolling(true));
972 PrerollDecoderToTime(
973 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), false);
974 EXPECT_TRUE(IsPrerolling(false));
976 // Send more packets
977 PlayAudioForTimeInterval(base::TimeDelta::FromMilliseconds(150),
978 base::TimeDelta::FromMilliseconds(220));
980 // The player should trigger audible status
981 EXPECT_TRUE(manager_.is_audible());
983 // The player release should report a non-audible state.
984 ReleasePlayer();
985 EXPECT_FALSE(manager_.is_audible());
988 TEST_F(MediaSourcePlayerTest, AudioDecoderRemovesAudibleStateWhenPaused) {
989 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
991 // No data arrived yet
992 EXPECT_FALSE(manager_.is_audible());
994 // Initialize decoder
995 StartAudioDecoderJob();
996 player_.SetVolume(1.0);
998 // Process frames until prerolling is done.
999 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100));
1000 EXPECT_TRUE(IsPrerolling(true));
1001 PrerollDecoderToTime(
1002 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), false);
1003 EXPECT_TRUE(IsPrerolling(false));
1005 // Send more packets
1006 PlayAudioForTimeInterval(base::TimeDelta::FromMilliseconds(150),
1007 base::TimeDelta::FromMilliseconds(220));
1009 // The player should trigger audible status
1010 EXPECT_TRUE(manager_.is_audible());
1012 // Pause the player
1013 player_.Pause(true);
1015 // Send more packets
1016 PlayAudioForTimeInterval(base::TimeDelta::FromMilliseconds(240),
1017 base::TimeDelta::FromMilliseconds(280));
1019 // The player should trigger audible status again
1020 EXPECT_FALSE(manager_.is_audible());
1022 player_.Release();
1025 TEST_F(MediaSourcePlayerTest, AudioDecoderRemovesAudibleStateWhenIdle) {
1026 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1028 // No data arrived yet
1029 EXPECT_FALSE(manager_.is_audible());
1031 // Initialize decoder
1032 StartAudioDecoderJob();
1033 player_.SetVolume(1.0);
1035 // Process frames until prerolling is done.
1036 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100));
1037 EXPECT_TRUE(IsPrerolling(true));
1038 PrerollDecoderToTime(
1039 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), false);
1040 EXPECT_TRUE(IsPrerolling(false));
1042 // Send more packets
1043 PlayAudioForTimeInterval(base::TimeDelta::FromMilliseconds(150),
1044 base::TimeDelta::FromMilliseconds(220));
1046 // The player should trigger audible status
1047 EXPECT_TRUE(manager_.is_audible());
1049 // Simulate the freeze on demuxer: wait for 300 ms
1050 WaitForDelay(base::TimeDelta::FromMilliseconds(300));
1052 // By this time the player should have reported
1053 // that there is no audio.
1054 EXPECT_FALSE(manager_.is_audible());
1056 ReleasePlayer();
1059 TEST_F(MediaSourcePlayerTest, StartVideoCodecWithValidSurface) {
1060 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1062 // Test video codec will not be created until data is received.
1063 StartVideoDecoderJob();
1065 // Set both an initial and a later video surface without receiving any
1066 // demuxed data yet.
1067 CreateNextTextureAndSetVideoSurface();
1068 EXPECT_FALSE(GetMediaCodecBridge(false));
1069 CreateNextTextureAndSetVideoSurface();
1070 EXPECT_FALSE(GetMediaCodecBridge(false));
1072 // No seeks, even on setting surface, should have occurred. (Browser seeks can
1073 // occur on setting surface, but only after previously receiving video data.)
1074 EXPECT_EQ(0, demuxer_->num_seek_requests());
1076 // Send the first input chunk and verify that decoder will be created.
1077 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1078 EXPECT_TRUE(GetMediaCodecBridge(false));
1079 WaitForVideoDecodeDone();
1082 TEST_F(MediaSourcePlayerTest, StartVideoCodecWithInvalidSurface) {
1083 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1085 // Test video codec will not be created when surface is invalid.
1086 scoped_refptr<gfx::SurfaceTexture> surface_texture(
1087 gfx::SurfaceTexture::Create(0));
1088 gfx::ScopedJavaSurface surface(surface_texture.get());
1089 StartVideoDecoderJob();
1091 // Release the surface texture.
1092 surface_texture = NULL;
1093 player_.SetVideoSurface(surface.Pass());
1095 // Player should not seek the demuxer on setting initial surface.
1096 EXPECT_EQ(0, demuxer_->num_seek_requests());
1097 EXPECT_EQ(1, demuxer_->num_data_requests());
1099 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1100 EXPECT_FALSE(GetMediaCodecBridge(false));
1103 TEST_F(MediaSourcePlayerTest, ReadFromDemuxerAfterSeek) {
1104 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1106 // Test decoder job will resend a ReadFromDemuxer request after seek.
1107 StartAudioDecoderJob();
1108 SeekPlayerWithAbort(true, base::TimeDelta());
1111 TEST_F(MediaSourcePlayerTest, SetSurfaceWhileSeeking) {
1112 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1114 // Test SetVideoSurface() will not cause an extra seek while the player is
1115 // waiting for demuxer to indicate seek is done.
1116 player_.OnDemuxerConfigsAvailable(
1117 CreateVideoDemuxerConfigs(false));
1119 // Initiate a seek. Skip requesting element seek of renderer.
1120 // Instead behave as if the renderer has asked us to seek.
1121 player_.SeekTo(base::TimeDelta());
1122 EXPECT_EQ(1, demuxer_->num_seek_requests());
1124 CreateNextTextureAndSetVideoSurface();
1125 EXPECT_EQ(1, demuxer_->num_seek_requests());
1126 player_.Start();
1128 // Send the seek done notification. The player should start requesting data.
1129 player_.OnDemuxerSeekDone(kNoTimestamp());
1130 EXPECT_FALSE(GetMediaCodecBridge(false));
1131 EXPECT_EQ(1, demuxer_->num_data_requests());
1132 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1133 EXPECT_TRUE(GetMediaCodecBridge(false));
1135 // Reconfirm exactly 1 seek request has been made of demuxer, and that it
1136 // was not a browser seek request.
1137 EXPECT_EQ(1, demuxer_->num_seek_requests());
1138 EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
1139 WaitForVideoDecodeDone();
1142 TEST_F(MediaSourcePlayerTest, ChangeMultipleSurfaceWhileDecoding) {
1143 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1145 // Test MediaSourcePlayer can switch multiple surfaces during decoding.
1146 CreateNextTextureAndSetVideoSurface();
1147 StartVideoDecoderJob();
1148 EXPECT_EQ(0, demuxer_->num_seek_requests());
1150 // Send the first input chunk.
1151 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1153 // While the decoder is decoding, change multiple surfaces. Pass an empty
1154 // surface first.
1155 gfx::ScopedJavaSurface empty_surface;
1156 player_.SetVideoSurface(empty_surface.Pass());
1157 // Next, pass a new non-empty surface.
1158 CreateNextTextureAndSetVideoSurface();
1160 // Wait for the media codec bridge to finish decoding and be reset pending a
1161 // browser seek.
1162 WaitForVideoDecodeDone();
1163 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1165 // Only one browser seek should have been initiated. No further data request
1166 // should have been processed on |message_loop_| before surface change event
1167 // became pending, above.
1168 EXPECT_EQ(1, demuxer_->num_browser_seek_requests());
1169 EXPECT_EQ(2, demuxer_->num_data_requests());
1171 // Simulate browser seek is done and confirm player requests more data for new
1172 // video codec.
1173 player_.OnDemuxerSeekDone(player_.GetCurrentTime());
1174 EXPECT_FALSE(GetMediaCodecBridge(false));
1175 EXPECT_EQ(3, demuxer_->num_data_requests());
1176 EXPECT_EQ(1, demuxer_->num_seek_requests());
1178 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1179 EXPECT_TRUE(GetMediaCodecBridge(false));
1180 WaitForVideoDecodeDone();
1183 TEST_F(MediaSourcePlayerTest, SetEmptySurfaceAndStarveWhileDecoding) {
1184 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1186 // Test player pauses if an empty surface is passed.
1187 CreateNextTextureAndSetVideoSurface();
1188 StartVideoDecoderJob();
1189 EXPECT_EQ(1, demuxer_->num_data_requests());
1191 // Send the first input chunk.
1192 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1194 // While the decoder is decoding, pass an empty surface.
1195 gfx::ScopedJavaSurface empty_surface;
1196 player_.SetVideoSurface(empty_surface.Pass());
1197 // Let the player starve. However, it should not issue any new data request in
1198 // this case.
1199 TriggerPlayerStarvation();
1200 // Wait for the media codec bridge to finish decoding and be reset.
1201 while (GetMediaDecoderJob(false)->is_decoding())
1202 message_loop_.RunUntilIdle();
1204 // No further seek or data requests should have been received since the
1205 // surface is empty.
1206 EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
1207 EXPECT_EQ(2, demuxer_->num_data_requests());
1208 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1210 // Playback resumes once a non-empty surface is passed.
1211 CreateNextTextureAndSetVideoSurface();
1212 EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
1213 while(demuxer_->num_browser_seek_requests() != 1)
1214 message_loop_.RunUntilIdle();
1215 WaitForVideoDecodeDone();
1218 TEST_F(MediaSourcePlayerTest, ReleaseVideoDecoderResourcesWhileDecoding) {
1219 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1221 // Test that if video decoder is released while decoding, the resources will
1222 // not be immediately released.
1223 CreateNextTextureAndSetVideoSurface();
1224 StartVideoDecoderJob();
1225 // No resource is requested since there is no data to decode.
1226 EXPECT_EQ(0, manager_.num_resources_requested());
1227 ReleasePlayer();
1228 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1230 // Recreate the video decoder.
1231 CreateNextTextureAndSetVideoSurface();
1232 player_.Start();
1233 while (!GetMediaDecoderJob(false)->is_decoding())
1234 message_loop_.RunUntilIdle();
1235 EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
1236 EXPECT_EQ(1, manager_.num_resources_requested());
1237 ReleasePlayer();
1238 // Wait for the media codec bridge to finish decoding and be reset.
1239 while (GetMediaDecoderJob(false)->is_decoding())
1240 message_loop_.RunUntilIdle();
1243 TEST_F(MediaSourcePlayerTest, AudioOnlyStartAfterSeekFinish) {
1244 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1246 // Test audio decoder job will not start until pending seek event is handled.
1247 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, false);
1248 player_.OnDemuxerConfigsAvailable(configs);
1250 // Initiate a seek. Skip requesting element seek of renderer.
1251 // Instead behave as if the renderer has asked us to seek.
1252 player_.SeekTo(base::TimeDelta());
1253 EXPECT_EQ(1, demuxer_->num_seek_requests());
1255 player_.Start();
1256 EXPECT_EQ(0, demuxer_->num_data_requests());
1258 // Sending back the seek done notification.
1259 player_.OnDemuxerSeekDone(kNoTimestamp());
1260 EXPECT_FALSE(GetMediaCodecBridge(true));
1261 EXPECT_EQ(1, demuxer_->num_data_requests());
1263 // Reconfirm exactly 1 seek request has been made of demuxer.
1264 EXPECT_EQ(1, demuxer_->num_seek_requests());
1266 // Decoder is created after data is received.
1267 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1268 EXPECT_TRUE(GetMediaCodecBridge(true));
1271 TEST_F(MediaSourcePlayerTest, VideoOnlyStartAfterSeekFinish) {
1272 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1274 // Test video decoder job will not start until pending seek event is handled.
1275 CreateNextTextureAndSetVideoSurface();
1276 DemuxerConfigs configs = CreateVideoDemuxerConfigs(false);
1277 player_.OnDemuxerConfigsAvailable(configs);
1279 // Initiate a seek. Skip requesting element seek of renderer.
1280 // Instead behave as if the renderer has asked us to seek.
1281 player_.SeekTo(base::TimeDelta());
1282 EXPECT_EQ(1, demuxer_->num_seek_requests());
1284 player_.Start();
1285 EXPECT_EQ(0, demuxer_->num_data_requests());
1287 // Sending back the seek done notification.
1288 player_.OnDemuxerSeekDone(kNoTimestamp());
1289 EXPECT_FALSE(GetMediaCodecBridge(false));
1290 EXPECT_EQ(1, demuxer_->num_data_requests());
1292 // Reconfirm exactly 1 seek request has been made of demuxer.
1293 EXPECT_EQ(1, demuxer_->num_seek_requests());
1295 // Decoder is created after data is received.
1296 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1297 EXPECT_TRUE(GetMediaCodecBridge(false));
1298 WaitForVideoDecodeDone();
1301 TEST_F(MediaSourcePlayerTest, StartImmediatelyAfterPause) {
1302 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1304 // Test that if the decoding job is not fully stopped after Pause(),
1305 // calling Start() will be a noop.
1306 StartAudioDecoderJob();
1308 MediaDecoderJob* decoder_job = GetMediaDecoderJob(true);
1309 EXPECT_FALSE(GetMediaDecoderJob(true)->is_decoding());
1311 // Sending data to player.
1312 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1313 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1314 EXPECT_EQ(2, demuxer_->num_data_requests());
1316 // Decoder job will not immediately stop after Pause() since it is
1317 // running on another thread.
1318 player_.Pause(true);
1319 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1321 // Nothing happens when calling Start() again.
1322 player_.Start();
1323 // Verify that Start() will not destroy and recreate the media codec bridge.
1324 EXPECT_EQ(decoder_job, GetMediaDecoderJob(true));
1326 while (GetMediaDecoderJob(true)->is_decoding())
1327 message_loop_.RunUntilIdle();
1328 // The decoder job should finish and wait for data.
1329 EXPECT_EQ(2, demuxer_->num_data_requests());
1330 EXPECT_TRUE(IsRequestingDemuxerData(true));
1333 TEST_F(MediaSourcePlayerTest, DecoderJobsCannotStartWithoutAudio) {
1334 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1336 // Test that when Start() is called, video decoder job will wait for audio
1337 // decoder job before start decoding the data.
1338 CreateNextTextureAndSetVideoSurface();
1339 Start(CreateAudioVideoDemuxerConfigs());
1340 MediaDecoderJob* audio_decoder_job = GetMediaDecoderJob(true);
1341 MediaDecoderJob* video_decoder_job = GetMediaDecoderJob(false);
1343 EXPECT_FALSE(audio_decoder_job->is_decoding());
1344 EXPECT_FALSE(video_decoder_job->is_decoding());
1346 // Sending video data to player, video decoder should not start.
1347 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1348 EXPECT_FALSE(video_decoder_job->is_decoding());
1350 // Sending audio data to player, both decoders should start now.
1351 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1352 EXPECT_TRUE(audio_decoder_job->is_decoding());
1353 EXPECT_TRUE(video_decoder_job->is_decoding());
1355 // No seeks should have occurred.
1356 EXPECT_EQ(0, demuxer_->num_seek_requests());
1357 WaitForVideoDecodeDone();
1360 TEST_F(MediaSourcePlayerTest, StartTimeTicksResetAfterDecoderUnderruns) {
1361 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1363 // Test start time ticks will reset after decoder job underruns.
1364 StartAudioDecoderJob();
1366 DecodeAudioDataUntilOutputBecomesAvailable();
1368 // The decoder job should finish prerolling and start prefetching.
1369 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(3));
1370 base::TimeTicks previous = StartTimeTicks();
1372 // Let the decoder starve.
1373 TriggerPlayerStarvation();
1374 WaitForAudioDecodeDone();
1375 EXPECT_TRUE(StartTimeTicks() == previous);
1377 // Send new data to the decoder so it can finish prefetching. This should
1378 // reset the start time ticks.
1379 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(3));
1380 EXPECT_TRUE(StartTimeTicks() != previous);
1382 base::TimeTicks current = StartTimeTicks();
1383 EXPECT_LE(0, (current - previous).InMillisecondsF());
1386 TEST_F(MediaSourcePlayerTest, V_SecondAccessUnitIsEOSAndResumePlayAfterSeek) {
1387 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1389 // Test MediaSourcePlayer can replay video after input EOS is reached.
1390 CreateNextTextureAndSetVideoSurface();
1391 StartVideoDecoderJob();
1393 // Send the first input chunk.
1394 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1395 WaitForVideoDecodeDone();
1397 VerifyPlaybackCompletesOnEOSDecode(true, false);
1398 VerifyCompletedPlaybackResumesOnSeekPlusStart(false, true);
1401 TEST_F(MediaSourcePlayerTest, A_FirstAccessUnitIsEOSAndResumePlayAfterSeek) {
1402 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1404 // Test decode of audio EOS buffer without any prior decode. See also
1405 // http://b/11696552.
1406 // Also tests that seeking+Start() after completing audio playback resumes
1407 // playback.
1408 Start(CreateAudioDemuxerConfigs(kCodecAAC, false));
1409 VerifyPlaybackCompletesOnEOSDecode(true, true);
1410 VerifyCompletedPlaybackResumesOnSeekPlusStart(true, false);
1413 TEST_F(MediaSourcePlayerTest, V_FirstAccessUnitAfterSeekIsEOS) {
1414 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1416 // Test decode of video EOS buffer, just after seeking, without any prior
1417 // decode (other than the simulated |kAborted| resulting from the seek
1418 // process.)
1419 CreateNextTextureAndSetVideoSurface();
1420 StartVideoDecoderJob();
1421 SeekPlayerWithAbort(false, base::TimeDelta());
1422 VerifyPlaybackCompletesOnEOSDecode(true, false);
1425 TEST_F(MediaSourcePlayerTest, A_FirstAccessUnitAfterSeekIsEOS) {
1426 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1428 // Test decode of audio EOS buffer, just after seeking, without any prior
1429 // decode (other than the simulated |kAborted| resulting from the seek
1430 // process.) See also http://b/11696552.
1431 Start(CreateAudioDemuxerConfigs(kCodecAAC, false));
1432 SeekPlayerWithAbort(true, base::TimeDelta());
1433 VerifyPlaybackCompletesOnEOSDecode(true, true);
1436 TEST_F(MediaSourcePlayerTest, AV_PlaybackCompletionAcrossConfigChange) {
1437 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1439 // Test that if one stream (audio) has completed decode of EOS and the other
1440 // stream (video) processes config change, that subsequent video EOS completes
1441 // A/V playback.
1442 // Also tests that seeking+Start() after completing playback resumes playback.
1443 CreateNextTextureAndSetVideoSurface();
1444 Start(CreateAudioVideoDemuxerConfigs());
1446 player_.OnDemuxerDataAvailable(CreateEOSAck(true)); // Audio EOS
1447 DemuxerConfigs configs = CreateVideoDemuxerConfigs(true);
1448 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckWithConfigChanged(
1449 false, 0, configs)); // Video |kConfigChanged| as first unit.
1451 WaitForAudioVideoDecodeDone();
1453 EXPECT_EQ(3, demuxer_->num_data_requests());
1455 // At no time after completing audio EOS decode, above, should the
1456 // audio decoder job resume decoding. Send and decode video EOS.
1457 VerifyPlaybackCompletesOnEOSDecode(true, false);
1458 VerifyCompletedPlaybackResumesOnSeekPlusStart(true, true);
1461 TEST_F(MediaSourcePlayerTest, VA_PlaybackCompletionAcrossConfigChange) {
1462 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1464 // Test that if one stream (video) has completed decode of EOS and the other
1465 // stream (audio) processes config change, that subsequent audio EOS completes
1466 // A/V playback.
1467 // Also tests that seeking+Start() after completing playback resumes playback.
1468 CreateNextTextureAndSetVideoSurface();
1469 Start(CreateAudioVideoDemuxerConfigs());
1471 player_.OnDemuxerDataAvailable(CreateEOSAck(false)); // Video EOS
1472 // Audio |kConfigChanged| as first unit.
1473 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckWithConfigChanged(
1474 true, 0, CreateAudioDemuxerConfigs(kCodecVorbis, false)));
1476 WaitForAudioVideoDecodeDone();
1478 EXPECT_EQ(3, demuxer_->num_data_requests());
1480 // At no time after completing video EOS decode, above, should the
1481 // video decoder job resume decoding. Send and decode audio EOS.
1482 VerifyPlaybackCompletesOnEOSDecode(true, true);
1483 VerifyCompletedPlaybackResumesOnSeekPlusStart(true, true);
1486 TEST_F(MediaSourcePlayerTest, AV_NoPrefetchForFinishedVideoOnAudioStarvation) {
1487 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1489 // Test that if one stream (video) has completed decode of EOS, prefetch
1490 // resulting from player starvation occurs only for the other stream (audio),
1491 // and responding to that prefetch with EOS completes A/V playback, even if
1492 // another starvation occurs during the latter EOS's decode.
1493 CreateNextTextureAndSetVideoSurface();
1494 Start(CreateAudioVideoDemuxerConfigs());
1496 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1497 player_.OnDemuxerDataAvailable(CreateEOSAck(false)); // Video EOS
1499 // Wait until video EOS is processed and more data (assumed to be audio) is
1500 // requested.
1501 WaitForAudioVideoDecodeDone();
1502 EXPECT_EQ(3, demuxer_->num_data_requests());
1504 // Simulate decoder underrun to trigger prefetch while still decoding audio.
1505 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(1));
1506 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding() &&
1507 !GetMediaDecoderJob(false)->is_decoding());
1508 TriggerPlayerStarvation();
1510 // Complete the audio decode that was in progress when simulated player
1511 // starvation was triggered.
1512 WaitForAudioDecodeDone();
1513 EXPECT_EQ(4, demuxer_->num_data_requests());
1514 player_.OnDemuxerDataAvailable(CreateEOSAck(true)); // Audio EOS
1515 EXPECT_FALSE(GetMediaDecoderJob(false)->is_decoding());
1516 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1518 // Simulate another decoder underrun to trigger prefetch while decoding EOS.
1519 TriggerPlayerStarvation();
1520 VerifyPlaybackCompletesOnEOSDecode(false, true /* ignored */);
1523 TEST_F(MediaSourcePlayerTest, V_StarvationDuringEOSDecode) {
1524 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1526 // Test that video-only playback completes without further data requested when
1527 // starvation occurs during EOS decode.
1528 CreateNextTextureAndSetVideoSurface();
1529 StartVideoDecoderJob();
1530 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1531 WaitForVideoDecodeDone();
1533 // Simulate decoder underrun to trigger prefetch while decoding EOS.
1534 player_.OnDemuxerDataAvailable(CreateEOSAck(false)); // Video EOS
1535 EXPECT_TRUE(GetMediaDecoderJob(false)->is_decoding());
1536 TriggerPlayerStarvation();
1537 VerifyPlaybackCompletesOnEOSDecode(false, false /* ignored */);
1540 TEST_F(MediaSourcePlayerTest, A_StarvationDuringEOSDecode) {
1541 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1543 // Test that audio-only playback completes without further data requested when
1544 // starvation occurs during EOS decode.
1545 StartAudioDecoderJob();
1546 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1547 WaitForAudioDecodeDone();
1549 // Simulate decoder underrun to trigger prefetch while decoding EOS.
1550 player_.OnDemuxerDataAvailable(CreateEOSAck(true)); // Audio EOS
1551 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1552 TriggerPlayerStarvation();
1553 VerifyPlaybackCompletesOnEOSDecode(false, true /* ignored */);
1556 TEST_F(MediaSourcePlayerTest, AV_SeekDuringEOSDecodePreventsCompletion) {
1557 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1559 // Test that seek supercedes audio+video playback completion on simultaneous
1560 // audio and video EOS decode, if SeekTo() occurs during these EOS decodes.
1561 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, true, true, true);
1564 TEST_F(MediaSourcePlayerTest, AV_SeekDuringAudioEOSDecodePreventsCompletion) {
1565 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1567 // Test that seek supercedes audio+video playback completion on simultaneous
1568 // audio EOS and video non-EOS decode, if SeekTo() occurs during these
1569 // decodes.
1570 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, true, true, false);
1573 TEST_F(MediaSourcePlayerTest, AV_SeekDuringVideoEOSDecodePreventsCompletion) {
1574 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1576 // Test that seek supercedes audio+video playback completion on simultaneous
1577 // audio non-EOS and video EOS decode, if SeekTo() occurs during these
1578 // decodes.
1579 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, true, false, true);
1582 TEST_F(MediaSourcePlayerTest, V_SeekDuringEOSDecodePreventsCompletion) {
1583 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1585 // Test that seek supercedes video-only playback completion on EOS decode, if
1586 // SeekTo() occurs during EOS decode.
1587 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(false, true, false, true);
1590 TEST_F(MediaSourcePlayerTest, A_SeekDuringEOSDecodePreventsCompletion) {
1591 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1593 // Test that seek supercedes audio-only playback completion on EOS decode, if
1594 // SeekTo() occurs during EOS decode.
1595 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, false, true, false);
1598 TEST_F(MediaSourcePlayerTest, NoRequestForDataAfterAbort) {
1599 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1601 // Test that the decoder will not request new data after receiving an aborted
1602 // access unit.
1603 StartAudioDecoderJob();
1605 // Send an aborted access unit.
1606 player_.OnDemuxerDataAvailable(CreateAbortedAck(true));
1607 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1608 WaitForAudioDecodeDone();
1610 // No request will be sent for new data.
1611 EXPECT_EQ(1, demuxer_->num_data_requests());
1613 // No seek requests should have occurred.
1614 EXPECT_EQ(0, demuxer_->num_seek_requests());
1617 TEST_F(MediaSourcePlayerTest, DemuxerDataArrivesAfterRelease) {
1618 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1620 // Test that the decoder should not crash if demuxer data arrives after
1621 // Release().
1622 StartAudioDecoderJob();
1624 ReleasePlayer();
1625 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1627 // The media codec bridge should have been released.
1628 EXPECT_FALSE(player_.IsPlaying());
1630 // No further data should have been requested.
1631 EXPECT_EQ(1, demuxer_->num_data_requests());
1633 // No seek requests should have occurred.
1634 EXPECT_EQ(0, demuxer_->num_seek_requests());
1637 TEST_F(MediaSourcePlayerTest, BrowserSeek_RegularSeekPendsBrowserSeekDone) {
1638 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1640 // Test that a browser seek, once started, delays a newly arrived regular
1641 // SeekTo() request's demuxer seek until the browser seek is done.
1642 BrowserSeekPlayer(false);
1644 // Simulate renderer requesting a regular seek while browser seek in progress.
1645 player_.SeekTo(base::TimeDelta());
1647 // Simulate browser seek is done. Confirm player requests the regular seek,
1648 // still has no video codec configured, and has not requested any
1649 // further data since the surface change event became pending in
1650 // BrowserSeekPlayer().
1651 EXPECT_EQ(1, demuxer_->num_seek_requests());
1652 player_.OnDemuxerSeekDone(base::TimeDelta());
1653 EXPECT_EQ(2, demuxer_->num_seek_requests());
1654 EXPECT_EQ(1, demuxer_->num_browser_seek_requests());
1656 // Simulate regular seek is done and confirm player requests more data for
1657 // new video codec.
1658 player_.OnDemuxerSeekDone(kNoTimestamp());
1659 EXPECT_FALSE(GetMediaCodecBridge(false));
1660 EXPECT_EQ(3, demuxer_->num_data_requests());
1661 EXPECT_EQ(2, demuxer_->num_seek_requests());
1662 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1663 EXPECT_TRUE(GetMediaCodecBridge(false));
1664 WaitForVideoDecodeDone();
1667 TEST_F(MediaSourcePlayerTest, BrowserSeek_InitialReleaseAndStart) {
1668 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1670 // Test that no browser seek is requested if player Release() + Start() occurs
1671 // prior to receiving any data.
1672 CreateNextTextureAndSetVideoSurface();
1673 StartVideoDecoderJob();
1674 ReleasePlayer();
1676 // Pass a new non-empty surface.
1677 CreateNextTextureAndSetVideoSurface();
1679 player_.Start();
1681 // No data request is issued since there is still one pending.
1682 EXPECT_EQ(1, demuxer_->num_data_requests());
1683 EXPECT_FALSE(GetMediaCodecBridge(false));
1685 // No browser seek is needed.
1686 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1687 EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
1688 EXPECT_EQ(2, demuxer_->num_data_requests());
1689 WaitForVideoDecodeDone();
1692 TEST_F(MediaSourcePlayerTest, BrowserSeek_MidStreamReleaseAndStart) {
1693 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1695 // Test that one browser seek is requested if player Release() + Start(), with
1696 // video data received between Release() and Start().
1697 BrowserSeekPlayer(true);
1699 // Simulate browser seek is done and confirm player requests more data.
1700 player_.OnDemuxerSeekDone(base::TimeDelta());
1701 EXPECT_EQ(3, demuxer_->num_data_requests());
1702 EXPECT_EQ(1, demuxer_->num_seek_requests());
1705 TEST_F(MediaSourcePlayerTest, NoBrowserSeekWithKeyFrameInCache) {
1706 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1708 // Test that browser seek is not needed if a key frame is found in data
1709 // cache.
1710 CreateNextTextureAndSetVideoSurface();
1711 StartVideoDecoderJob();
1712 DemuxerData data = CreateReadFromDemuxerAckForVideo(false);
1713 data.access_units[0].is_key_frame = true;
1715 // Simulate demuxer's response to the video data request.
1716 player_.OnDemuxerDataAvailable(data);
1718 // Trigger decoder recreation later by changing surfaces.
1719 CreateNextTextureAndSetVideoSurface();
1721 // Wait for the media codec bridge to finish decoding and be reset.
1722 WaitForVideoDecodeDone();
1723 EXPECT_FALSE(HasData(false));
1725 // Send a non key frame to decoder so that decoder can continue. This will
1726 // not trigger any browser seeks as the previous key frame is still in the
1727 // buffer.
1728 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1729 WaitForVideoDecodeDone();
1730 EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
1733 TEST_F(MediaSourcePlayerTest, PrerollAudioAfterSeek) {
1734 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1736 // Test decoder job will preroll the media to the seek position.
1737 StartAudioDecoderJob();
1739 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100));
1740 EXPECT_TRUE(IsPrerolling(true));
1741 PrerollDecoderToTime(
1742 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), true);
1745 TEST_F(MediaSourcePlayerTest, PrerollVideoAfterSeek) {
1746 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1748 // Test decoder job will preroll the media to the seek position.
1749 CreateNextTextureAndSetVideoSurface();
1750 StartVideoDecoderJob();
1752 SeekPlayerWithAbort(false, base::TimeDelta::FromMilliseconds(100));
1753 EXPECT_TRUE(IsPrerolling(false));
1754 PrerollDecoderToTime(
1755 false, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), true);
1758 TEST_F(MediaSourcePlayerTest, SeekingAfterCompletingPrerollRestartsPreroll) {
1759 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1761 // Test decoder job will begin prerolling upon seek, when it was not
1762 // prerolling prior to the seek.
1763 StartAudioDecoderJob();
1764 MediaDecoderJob* decoder_job = GetMediaDecoderJob(true);
1765 EXPECT_TRUE(IsPrerolling(true));
1767 // Complete the initial preroll by feeding data to the decoder.
1768 DecodeAudioDataUntilOutputBecomesAvailable();
1769 EXPECT_FALSE(IsPrerolling(true));
1771 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(500));
1773 // Prerolling should have begun again.
1774 EXPECT_TRUE(IsPrerolling(true));
1775 EXPECT_EQ(500.0, GetPrerollTimestamp().InMillisecondsF());
1777 // Send data at and after the seek position. Prerolling should complete.
1778 for (int i = 0; i < 4; ++i) {
1779 DemuxerData data = CreateReadFromDemuxerAckForAudio(i);
1780 data.access_units[0].timestamp = base::TimeDelta::FromMilliseconds(
1781 500 + 30 * (i - 1));
1782 player_.OnDemuxerDataAvailable(data);
1783 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1784 WaitForAudioDecodeDone();
1786 EXPECT_LT(500.0, player_.GetCurrentTime().InMillisecondsF());
1787 EXPECT_FALSE(IsPrerolling(true));
1789 // Throughout this test, we should have not re-created the media codec bridge,
1790 // so IsPrerolling() transition from false to true was not due to constructor
1791 // initialization. It was due to BeginPrerolling().
1792 EXPECT_EQ(decoder_job, GetMediaDecoderJob(true));
1795 TEST_F(MediaSourcePlayerTest, PrerollContinuesAcrossReleaseAndStart) {
1796 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1798 // Test decoder job will resume media prerolling if interrupted by Release()
1799 // and Start().
1800 StartAudioDecoderJob();
1802 base::TimeDelta target_timestamp = base::TimeDelta::FromMilliseconds(100);
1803 SeekPlayerWithAbort(true, target_timestamp);
1804 EXPECT_TRUE(IsPrerolling(true));
1805 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
1807 // Send some data before the seek position.
1808 // Test uses 'large' number of iterations because decoder job may not get
1809 // MEDIA_CODEC_OK output status until after a few dequeue output attempts.
1810 // This allows decoder status to stabilize prior to AU timestamp reaching
1811 // the preroll target.
1812 DemuxerData data;
1813 for (int i = 0; i < 10; ++i) {
1814 data = CreateReadFromDemuxerAckForAudio(3);
1815 data.access_units[0].timestamp = base::TimeDelta::FromMilliseconds(i * 10);
1816 if (i == 1) {
1817 // While still prerolling, Release() and Start() the player.
1818 ReleasePlayer();
1819 // The decoder is still decoding and will not be immediately released.
1820 EXPECT_TRUE(GetMediaCodecBridge(true));
1821 Resume(false, false);
1822 } else {
1823 player_.OnDemuxerDataAvailable(data);
1824 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1825 WaitForAudioDecodeDone();
1827 EXPECT_TRUE(IsPrerolling(true));
1829 EXPECT_EQ(100.0, player_.GetCurrentTime().InMillisecondsF());
1830 EXPECT_TRUE(IsPrerolling(true));
1832 // Send data after the seek position.
1833 PrerollDecoderToTime(true, target_timestamp, target_timestamp, true);
1836 // Flaky on Android: crbug.com/419122.
1837 #if defined(OS_ANDROID)
1838 #define MAYBE_PrerollContinuesAcrossConfigChange \
1839 DISABLED_PrerollContinuesAcrossConfigChange
1840 #else
1841 #define MAYBE_PrerollContinuesAcrossConfigChange \
1842 PrerollContinuesAcrossConfigChange
1843 #endif
1844 TEST_F(MediaSourcePlayerTest, MAYBE_PrerollContinuesAcrossConfigChange) {
1845 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1847 // Test decoder job will resume media prerolling if interrupted by
1848 // |kConfigChanged| and OnDemuxerConfigsAvailable().
1849 StartAudioDecoderJob();
1851 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100));
1852 EXPECT_TRUE(IsPrerolling(true));
1853 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
1855 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, true);
1857 // In response to data request, simulate that demuxer signals config change by
1858 // sending an AU with |kConfigChanged|.
1859 DemuxerData data = CreateReadFromDemuxerAckWithConfigChanged(
1860 true, 0, configs);
1861 player_.OnDemuxerDataAvailable(data);
1863 PrerollDecoderToTime(
1864 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), true);
1867 TEST_F(MediaSourcePlayerTest, PrerollContinuesAfterUnchangedConfigs) {
1868 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1870 // Test decoder job will resume media prerolling if interrupted by a config
1871 // change access unit with unchanged configs.
1872 StartAudioDecoderJob();
1874 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100));
1875 EXPECT_TRUE(IsPrerolling(true));
1876 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
1878 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, false);
1880 // In response to data request, simulate that demuxer signals config change by
1881 // sending an AU with |kConfigChanged|.
1882 DemuxerData data = CreateReadFromDemuxerAckWithConfigChanged(
1883 true, 0, configs);
1884 player_.OnDemuxerDataAvailable(data);
1885 PrerollDecoderToTime(
1886 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), true);
1889 TEST_F(MediaSourcePlayerTest, AudioPrerollFinishesBeforeVideo) {
1890 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1892 // Test that after audio finishes prerolling, it will wait for video to finish
1893 // prerolling before advancing together.
1894 CreateNextTextureAndSetVideoSurface();
1895 Start(CreateAudioVideoDemuxerConfigs());
1897 // Initiate a seek.
1898 base::TimeDelta seek_position = base::TimeDelta::FromMilliseconds(100);
1899 player_.SeekTo(seek_position);
1900 player_.OnDemuxerDataAvailable(CreateAbortedAck(true));
1901 player_.OnDemuxerDataAvailable(CreateAbortedAck(false));
1902 WaitForDecodeDone(true, true);
1904 // Verify that the seek is requested.
1905 EXPECT_EQ(1, demuxer_->num_seek_requests());
1906 player_.OnDemuxerSeekDone(kNoTimestamp());
1907 EXPECT_EQ(4, demuxer_->num_data_requests());
1908 EXPECT_EQ(player_.GetCurrentTime().InMillisecondsF(), 100.0);
1909 EXPECT_EQ(GetPrerollTimestamp().InMillisecondsF(), 100.0);
1911 // Send both audio and video data to finish prefetching.
1912 base::TimeDelta seek_ack_position = base::TimeDelta::FromMilliseconds(70);
1913 DemuxerData audio_data = CreateReadFromDemuxerAckForAudio(0);
1914 audio_data.access_units[0].timestamp = seek_ack_position;
1915 DemuxerData video_data = CreateReadFromDemuxerAckForVideo(false);
1916 video_data.access_units[0].timestamp = seek_ack_position;
1917 player_.OnDemuxerDataAvailable(audio_data);
1918 player_.OnDemuxerDataAvailable(video_data);
1919 WaitForAudioDecodeDone();
1920 WaitForVideoDecodeDone();
1922 // Send audio data at and after the seek position. Audio should finish
1923 // prerolling and stop decoding.
1924 EXPECT_EQ(6, demuxer_->num_data_requests());
1925 PrerollDecoderToTime(true, seek_position, seek_position, true);
1926 EXPECT_FALSE(GetMediaDecoderJob(true)->is_decoding());
1927 EXPECT_FALSE(IsPrerolling(true));
1928 EXPECT_TRUE(IsPrerolling(false));
1930 // Send video data to let video finish prerolling.
1931 PrerollDecoderToTime(false, seek_position, seek_position, false);
1932 EXPECT_FALSE(IsPrerolling(false));
1934 // Both audio and video decoders should start decoding again.
1935 player_.OnDemuxerDataAvailable(audio_data);
1936 player_.OnDemuxerDataAvailable(video_data);
1937 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1938 EXPECT_TRUE(GetMediaDecoderJob(false)->is_decoding());
1941 TEST_F(MediaSourcePlayerTest, SimultaneousAudioVideoConfigChange) {
1942 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1944 // Test that the player allows simultaneous audio and video config change,
1945 // such as might occur during OnPrefetchDone() if next access unit for both
1946 // audio and video jobs is |kConfigChanged|.
1947 CreateNextTextureAndSetVideoSurface();
1948 Start(CreateAudioVideoDemuxerConfigs());
1949 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1950 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1951 EXPECT_TRUE(GetMediaCodecBridge(true));
1952 EXPECT_TRUE(GetMediaCodecBridge(false));
1953 EnableAdaptiveVideoPlayback(false);
1954 WaitForAudioVideoDecodeDone();
1956 // If audio or video hasn't finished prerolling, let them finish it.
1957 if (IsPrerolling(true))
1958 PrerollDecoderToTime(true, base::TimeDelta(), base::TimeDelta(), true);
1959 if (IsPrerolling(false))
1960 PrerollDecoderToTime(false, base::TimeDelta(), base::TimeDelta(), false);
1961 int expected_num_data_requests = demuxer_->num_data_requests();
1963 // Simulate audio |kConfigChanged| prefetched as standalone access unit.
1964 DemuxerConfigs audio_configs = CreateAudioDemuxerConfigs(kCodecVorbis, true);
1965 player_.OnDemuxerDataAvailable(
1966 CreateReadFromDemuxerAckWithConfigChanged(true, 0, audio_configs));
1968 // Simulate video |kConfigChanged| prefetched as standalone access unit.
1969 player_.OnDemuxerDataAvailable(
1970 CreateReadFromDemuxerAckWithConfigChanged(
1971 false, 0, CreateVideoDemuxerConfigs(true)));
1972 EXPECT_EQ(expected_num_data_requests + 2, demuxer_->num_data_requests());
1973 EXPECT_TRUE(IsDrainingDecoder(true));
1974 EXPECT_TRUE(IsDrainingDecoder(false));
1976 // Waiting for decoder to finish draining.
1977 while (IsDrainingDecoder(true) || IsDrainingDecoder(false))
1978 message_loop_.RunUntilIdle();
1981 TEST_F(MediaSourcePlayerTest,
1982 SimultaneousAudioVideoConfigChangeWithAdaptivePlayback) {
1983 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1985 // Test that the player allows simultaneous audio and video config change with
1986 // adaptive video playback enabled.
1987 CreateNextTextureAndSetVideoSurface();
1988 Start(CreateAudioVideoDemuxerConfigs());
1989 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1990 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1991 EXPECT_EQ(4, demuxer_->num_data_requests());
1992 EXPECT_TRUE(GetMediaCodecBridge(true));
1993 EXPECT_TRUE(GetMediaCodecBridge(false));
1994 EnableAdaptiveVideoPlayback(true);
1995 WaitForAudioVideoDecodeDone();
1997 // If audio or video hasn't finished prerolling, let them finish it.
1998 if (IsPrerolling(true))
1999 PrerollDecoderToTime(true, base::TimeDelta(), base::TimeDelta(), true);
2000 if (IsPrerolling(false))
2001 PrerollDecoderToTime(false, base::TimeDelta(), base::TimeDelta(), false);
2002 int expected_num_data_requests = demuxer_->num_data_requests();
2004 // Simulate audio |kConfigChanged| prefetched as standalone access unit.
2005 DemuxerConfigs audio_configs = CreateAudioDemuxerConfigs(kCodecVorbis, true);
2006 player_.OnDemuxerDataAvailable(
2007 CreateReadFromDemuxerAckWithConfigChanged(true, 0, audio_configs));
2009 // Simulate video |kConfigChanged| prefetched as standalone access unit.
2010 player_.OnDemuxerDataAvailable(
2011 CreateReadFromDemuxerAckWithConfigChanged(
2012 false, 0, CreateVideoDemuxerConfigs(true)));
2013 EXPECT_EQ(expected_num_data_requests + 2, demuxer_->num_data_requests());
2014 EXPECT_TRUE(IsDrainingDecoder(true));
2015 EXPECT_FALSE(IsDrainingDecoder(false));
2017 // Waiting for audio decoder to finish draining.
2018 while (IsDrainingDecoder(true))
2019 message_loop_.RunUntilIdle();
2022 TEST_F(MediaSourcePlayerTest, DemuxerConfigRequestedIfInPrefetchUnit0) {
2023 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2025 // Test that the player detects need for and requests demuxer configs if
2026 // the |kConfigChanged| unit is the very first unit in the set of units
2027 // received in OnDemuxerDataAvailable() ostensibly while
2028 // |PREFETCH_DONE_EVENT_PENDING|.
2029 StartConfigChange(true, true, 0, false);
2030 WaitForAudioDecodeDone();
2033 TEST_F(MediaSourcePlayerTest, DemuxerConfigRequestedIfInPrefetchUnit1) {
2034 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2036 // Test that the player detects need for and requests demuxer configs if
2037 // the |kConfigChanged| unit is not the first unit in the set of units
2038 // received in OnDemuxerDataAvailable() ostensibly while
2039 // |PREFETCH_DONE_EVENT_PENDING|.
2040 StartConfigChange(true, true, 1, false);
2041 WaitForAudioDecodeDone();
2044 TEST_F(MediaSourcePlayerTest, DemuxerConfigRequestedIfInUnit0AfterPrefetch) {
2045 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2047 // Test that the player detects need for and requests demuxer configs if
2048 // the |kConfigChanged| unit is the very first unit in the set of units
2049 // received in OnDemuxerDataAvailable() from data requested ostensibly while
2050 // not prefetching.
2051 StartConfigChange(true, false, 0, false);
2052 WaitForAudioDecodeDone();
2055 TEST_F(MediaSourcePlayerTest, DemuxerConfigRequestedIfInUnit1AfterPrefetch) {
2056 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2058 // Test that the player detects need for and requests demuxer configs if
2059 // the |kConfigChanged| unit is not the first unit in the set of units
2060 // received in OnDemuxerDataAvailable() from data requested ostensibly while
2061 // not prefetching.
2062 StartConfigChange(true, false, 1, false);
2063 WaitForAudioDecodeDone();
2066 TEST_F(MediaSourcePlayerTest, BrowserSeek_PrerollAfterBrowserSeek) {
2067 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2069 // Test decoder job will preroll the media to the actual seek position
2070 // resulting from a browser seek.
2071 BrowserSeekPlayer(false);
2073 // Simulate browser seek is done, but to a later time than was requested.
2074 EXPECT_LT(player_.GetCurrentTime().InMillisecondsF(), 100);
2075 player_.OnDemuxerSeekDone(base::TimeDelta::FromMilliseconds(100));
2076 // Because next AU is not I-frame, MediaCodecBridge will not be recreated.
2077 EXPECT_FALSE(GetMediaCodecBridge(false));
2078 EXPECT_EQ(100.0, player_.GetCurrentTime().InMillisecondsF());
2079 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2080 EXPECT_EQ(3, demuxer_->num_data_requests());
2082 PrerollDecoderToTime(
2083 false, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), true);
2086 TEST_F(MediaSourcePlayerTest, VideoDemuxerConfigChange) {
2087 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2089 // Test that video config change notification results in creating a new
2090 // video codec without any browser seek.
2091 StartConfigChange(false, true, 1, false);
2093 // New video codec should have been created and configured, without any
2094 // browser seek.
2095 EXPECT_TRUE(GetMediaCodecBridge(false));
2096 EXPECT_EQ(3, demuxer_->num_data_requests());
2097 EXPECT_EQ(0, demuxer_->num_seek_requests());
2099 // 2 codecs should have been created, one before the config change, and one
2100 // after it.
2101 EXPECT_EQ(2, manager_.num_resources_requested());
2102 WaitForVideoDecodeDone();
2105 TEST_F(MediaSourcePlayerTest, VideoDemuxerConfigChangeWithAdaptivePlayback) {
2106 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2108 // Test that if codec supports adaptive playback, no new codec should be
2109 // created beyond the one used to decode the prefetch media data prior to
2110 // the kConfigChanged.
2111 StartConfigChange(false, true, 1, true);
2113 // No browser seek should be needed.
2114 EXPECT_TRUE(GetMediaCodecBridge(false));
2115 EXPECT_EQ(3, demuxer_->num_data_requests());
2116 EXPECT_EQ(0, demuxer_->num_seek_requests());
2118 // Only 1 codec should have been created so far.
2119 EXPECT_EQ(1, manager_.num_resources_requested());
2120 WaitForVideoDecodeDone();
2123 TEST_F(MediaSourcePlayerTest, DecoderDrainInterruptedBySeek) {
2124 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2126 // Test if a decoder is being drained while receiving a seek request, draining
2127 // is canceled.
2128 SendConfigChangeToDecoder(true, false, 0, false);
2129 EXPECT_TRUE(IsDrainingDecoder(true));
2131 player_.SeekTo(base::TimeDelta::FromMilliseconds(100));
2132 WaitForAudioDecodeDone();
2133 EXPECT_FALSE(IsDrainingDecoder(true));
2134 player_.OnDemuxerSeekDone(kNoTimestamp());
2136 EXPECT_EQ(1, demuxer_->num_seek_requests());
2137 EXPECT_EQ(4, demuxer_->num_data_requests());
2140 TEST_F(MediaSourcePlayerTest, DecoderDrainInterruptedByRelease) {
2141 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2143 // Test if a decoder is being drained while receiving a release request,
2144 // draining is canceled.
2145 SendConfigChangeToDecoder(true, false, 0, false);
2146 EXPECT_TRUE(IsDrainingDecoder(true));
2148 ReleasePlayer();
2149 WaitForAudioDecodeDone();
2150 EXPECT_EQ(3, demuxer_->num_data_requests());
2151 EXPECT_FALSE(IsDrainingDecoder(true));
2153 EXPECT_FALSE(GetMediaCodecBridge(true));
2154 EXPECT_FALSE(player_.IsPlaying());
2156 player_.Start();
2157 EXPECT_TRUE(player_.IsPlaying());
2158 EXPECT_EQ(3, demuxer_->num_data_requests());
2161 TEST_F(MediaSourcePlayerTest, DecoderDrainInterruptedBySurfaceChange) {
2162 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2164 // Test if a video decoder is being drained while surface changes, draining
2165 // is canceled.
2166 SendConfigChangeToDecoder(false, false, 0, false);
2167 EXPECT_TRUE(IsDrainingDecoder(false));
2169 CreateNextTextureAndSetVideoSurface();
2170 WaitForVideoDecodeDone();
2172 EXPECT_FALSE(IsDrainingDecoder(false));
2173 EXPECT_TRUE(player_.IsPlaying());
2175 // The frame after the config change should always be an iframe, so no browser
2176 // seek is needed when recreating the video decoder due to surface change.
2177 EXPECT_TRUE(GetMediaCodecBridge(false));
2178 EXPECT_EQ(4, demuxer_->num_data_requests());
2179 EXPECT_EQ(0, demuxer_->num_seek_requests());
2182 TEST_F(MediaSourcePlayerTest,
2183 BrowserSeek_DecoderStarvationWhilePendingSurfaceChange) {
2184 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2186 // Test video decoder starvation while handling a pending surface change
2187 // should not cause any crashes.
2188 CreateNextTextureAndSetVideoSurface();
2189 StartVideoDecoderJob();
2190 DemuxerData data = CreateReadFromDemuxerAckForVideo(false);
2191 player_.OnDemuxerDataAvailable(data);
2193 // Trigger a surface change and decoder starvation.
2194 CreateNextTextureAndSetVideoSurface();
2195 TriggerPlayerStarvation();
2196 WaitForVideoDecodeDone();
2197 EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
2199 // Surface change should trigger a seek.
2200 player_.OnDemuxerDataAvailable(data);
2201 EXPECT_EQ(1, demuxer_->num_browser_seek_requests());
2202 player_.OnDemuxerSeekDone(base::TimeDelta());
2203 // After seek is done, prefetch is handled first. MediaCodecBridge is not
2204 // created at this moment.
2205 EXPECT_FALSE(GetMediaCodecBridge(false));
2207 // A new data request should be sent.
2208 EXPECT_EQ(3, demuxer_->num_data_requests());
2211 TEST_F(MediaSourcePlayerTest, ReleaseWithOnPrefetchDoneAlreadyPosted) {
2212 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2214 // Test if OnPrefetchDone() had already been posted before and is executed
2215 // after Release(), then player does not DCHECK. This test is fragile to
2216 // change to MediaDecoderJob::Prefetch() implementation; it assumes task
2217 // is posted to run |prefetch_cb| if the job already HasData().
2218 // TODO(wolenetz): Remove MSP::set_decode_callback_for_testing() if this test
2219 // becomes obsolete. See http://crbug.com/304234.
2220 StartAudioDecoderJob();
2222 // Escape the original prefetch by decoding a single access unit.
2223 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
2224 WaitForAudioDecodeDone();
2226 // Prime the job with a few more access units, so that a later prefetch,
2227 // triggered by starvation to simulate decoder underrun, can trivially
2228 // post task to run OnPrefetchDone().
2229 player_.OnDemuxerDataAvailable(
2230 CreateReadFromDemuxerAckWithConfigChanged(
2231 true, 4, CreateAudioDemuxerConfigs(kCodecVorbis, false)));
2232 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
2234 // Simulate decoder underrun, so trivial prefetch starts while still decoding.
2235 // The prefetch and posting of OnPrefetchDone() will not occur until next
2236 // MediaDecoderCallBack() occurs.
2237 TriggerPlayerStarvation();
2239 // Upon the next successful decode callback, post a task to call Release() on
2240 // the |player_|, such that the trivial OnPrefetchDone() task posting also
2241 // occurs and should execute after the Release().
2242 OnNextTestDecodeCallbackPostTaskToReleasePlayer();
2244 WaitForAudioDecodeDone();
2245 EXPECT_TRUE(decoder_callback_hook_executed_);
2247 EXPECT_EQ(3, demuxer_->num_data_requests());
2249 // Player should not request any new data since the access units haven't
2250 // been fully decoded yet.
2251 Resume(false, false);
2254 TEST_F(MediaSourcePlayerTest, SeekToThenReleaseThenDemuxerSeekAndDone) {
2255 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2257 // Test if Release() occurs after SeekTo(), but the DemuxerSeek IPC request
2258 // has not yet been sent, then the seek request is sent after Release(). Also,
2259 // test if OnDemuxerSeekDone() occurs prior to next Start(), then the player
2260 // will resume correct post-seek preroll upon Start().
2261 StartAudioDecoderJobAndSeekToWhileDecoding(
2262 base::TimeDelta::FromMilliseconds(100));
2263 ReleasePlayer();
2264 EXPECT_EQ(0, demuxer_->num_seek_requests());
2265 WaitForAudioDecodeDone();
2266 EXPECT_EQ(1, demuxer_->num_seek_requests());
2268 player_.OnDemuxerSeekDone(kNoTimestamp());
2269 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2270 EXPECT_FALSE(GetMediaCodecBridge(true));
2271 EXPECT_FALSE(player_.IsPlaying());
2273 // Player should begin prefetch and resume preroll upon Start().
2274 EXPECT_EQ(2, demuxer_->num_data_requests());
2275 Resume(true, false);
2276 EXPECT_TRUE(IsPrerolling(true));
2277 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2279 // No further seek should have been requested since Release(), above.
2280 EXPECT_EQ(1, demuxer_->num_seek_requests());
2283 TEST_F(MediaSourcePlayerTest, SeekToThenReleaseThenDemuxerSeekThenStart) {
2284 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2286 // Test if Release() occurs after SeekTo(), but the DemuxerSeek IPC request
2287 // has not yet been sent, then the seek request is sent after Release(). Also,
2288 // test if OnDemuxerSeekDone() does not occur until after the next Start(),
2289 // then the player remains pending seek done until (and resumes correct
2290 // post-seek preroll after) OnDemuxerSeekDone().
2291 StartAudioDecoderJobAndSeekToWhileDecoding(
2292 base::TimeDelta::FromMilliseconds(100));
2293 ReleasePlayer();
2294 EXPECT_EQ(0, demuxer_->num_seek_requests());
2296 // Player should not prefetch upon Start() nor create the media codec bridge,
2297 // due to awaiting DemuxerSeekDone.
2298 EXPECT_EQ(2, demuxer_->num_data_requests());
2299 Resume(false, false);
2301 WaitForAudioDecodeDone();
2302 EXPECT_EQ(1, demuxer_->num_seek_requests());
2303 player_.OnDemuxerSeekDone(kNoTimestamp());
2304 EXPECT_TRUE(GetMediaDecoderJob(true));
2305 EXPECT_TRUE(IsPrerolling(true));
2306 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2307 EXPECT_EQ(3, demuxer_->num_data_requests());
2309 // No further seek should have been requested since Release(), above.
2310 EXPECT_EQ(1, demuxer_->num_seek_requests());
2313 TEST_F(MediaSourcePlayerTest, SeekToThenDemuxerSeekThenReleaseThenSeekDone) {
2314 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2316 // Test if Release() occurs after a SeekTo()'s subsequent DemuxerSeek IPC
2317 // request and OnDemuxerSeekDone() arrives prior to the next Start(), then the
2318 // player will resume correct post-seek preroll upon Start().
2319 StartAudioDecoderJobAndSeekToWhileDecoding(
2320 base::TimeDelta::FromMilliseconds(100));
2321 WaitForAudioDecodeDone();
2322 EXPECT_EQ(1, demuxer_->num_seek_requests());
2324 ReleasePlayer();
2325 player_.OnDemuxerSeekDone(kNoTimestamp());
2326 EXPECT_FALSE(player_.IsPlaying());
2327 EXPECT_FALSE(GetMediaCodecBridge(true));
2328 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2330 // Player should begin prefetch and resume preroll upon Start().
2331 EXPECT_EQ(2, demuxer_->num_data_requests());
2332 Resume(true, false);
2333 EXPECT_TRUE(IsPrerolling(true));
2334 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2336 // No further seek should have been requested since before Release(), above.
2337 EXPECT_EQ(1, demuxer_->num_seek_requests());
2340 TEST_F(MediaSourcePlayerTest, SeekToThenReleaseThenStart) {
2341 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2343 // Test if Release() occurs after a SeekTo()'s subsequent DemuxerSeeK IPC
2344 // request and OnDemuxerSeekDone() does not occur until after the next
2345 // Start(), then the player remains pending seek done until (and resumes
2346 // correct post-seek preroll after) OnDemuxerSeekDone().
2347 StartAudioDecoderJobAndSeekToWhileDecoding(
2348 base::TimeDelta::FromMilliseconds(100));
2349 WaitForAudioDecodeDone();
2350 EXPECT_EQ(1, demuxer_->num_seek_requests());
2352 ReleasePlayer();
2353 EXPECT_EQ(2, demuxer_->num_data_requests());
2354 Resume(false, false);
2356 player_.OnDemuxerSeekDone(kNoTimestamp());
2357 EXPECT_FALSE(GetMediaCodecBridge(true));
2358 EXPECT_TRUE(IsPrerolling(true));
2359 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2360 EXPECT_EQ(3, demuxer_->num_data_requests());
2362 // No further seek should have been requested since before Release(), above.
2363 EXPECT_EQ(1, demuxer_->num_seek_requests());
2366 TEST_F(MediaSourcePlayerTest, ConfigChangedThenReleaseThenStart) {
2367 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2369 // Test if Release() occurs after |kConfigChanged| is processed, new data
2370 // requested of demuxer, and the requested data arrive before the next
2371 // Start(), then the player starts to decode the new data without any seek.
2372 StartConfigChange(true, true, 0, false);
2373 ReleasePlayer();
2375 EXPECT_TRUE(GetMediaCodecBridge(true));
2376 EXPECT_FALSE(player_.IsPlaying());
2377 EXPECT_EQ(3, demuxer_->num_data_requests());
2378 player_.OnDemuxerDataAvailable(
2379 CreateReadFromDemuxerAckWithConfigChanged(
2380 true, 4, CreateAudioDemuxerConfigs(kCodecVorbis, false)));
2381 WaitForAudioDecodeDone();
2382 EXPECT_FALSE(GetMediaCodecBridge(true));
2384 // Player should resume upon Start(), even without further configs supplied.
2385 player_.Start();
2386 EXPECT_TRUE(player_.IsPlaying());
2387 EXPECT_EQ(3, demuxer_->num_data_requests());
2388 EXPECT_EQ(0, demuxer_->num_seek_requests());
2389 WaitForAudioDecodeDone();
2392 TEST_F(MediaSourcePlayerTest, BrowserSeek_ThenReleaseThenDemuxerSeekDone) {
2393 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2395 // Test that Release() after a browser seek's DemuxerSeek IPC request has been
2396 // sent behaves similar to a regular seek: if OnDemuxerSeekDone() occurs
2397 // before the next Start()+SetVideoSurface(), then the player will resume
2398 // correct post-seek preroll upon Start()+SetVideoSurface().
2399 BrowserSeekPlayer(false);
2400 base::TimeDelta expected_preroll_timestamp = player_.GetCurrentTime();
2401 ReleasePlayer();
2403 player_.OnDemuxerSeekDone(expected_preroll_timestamp);
2404 EXPECT_FALSE(player_.IsPlaying());
2405 EXPECT_FALSE(GetMediaCodecBridge(false));
2406 EXPECT_EQ(expected_preroll_timestamp, GetPrerollTimestamp());
2408 // Player should begin prefetch and resume preroll upon Start().
2409 EXPECT_EQ(2, demuxer_->num_data_requests());
2410 CreateNextTextureAndSetVideoSurface();
2411 Resume(false, true);
2412 EXPECT_TRUE(IsPrerolling(false));
2413 EXPECT_EQ(expected_preroll_timestamp, GetPrerollTimestamp());
2414 EXPECT_EQ(expected_preroll_timestamp, player_.GetCurrentTime());
2416 // No further seek should have been requested since BrowserSeekPlayer().
2417 EXPECT_EQ(1, demuxer_->num_seek_requests());
2420 TEST_F(MediaSourcePlayerTest, BrowserSeek_ThenReleaseThenStart) {
2421 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2423 // Test that Release() after a browser seek's DemuxerSeek IPC request has been
2424 // sent behaves similar to a regular seek: if OnDemuxerSeekDone() does not
2425 // occur until after the next Start()+SetVideoSurface(), then the player
2426 // remains pending seek done until (and resumes correct post-seek preroll
2427 // after) OnDemuxerSeekDone().
2428 BrowserSeekPlayer(false);
2429 base::TimeDelta expected_preroll_timestamp = player_.GetCurrentTime();
2430 ReleasePlayer();
2432 EXPECT_EQ(2, demuxer_->num_data_requests());
2433 CreateNextTextureAndSetVideoSurface();
2434 Resume(false, false);
2436 player_.OnDemuxerSeekDone(expected_preroll_timestamp);
2437 // Prefetch takes place first, and the decoder is not created yet.
2438 EXPECT_FALSE(GetMediaCodecBridge(false));
2439 EXPECT_TRUE(IsPrerolling(false));
2440 EXPECT_EQ(expected_preroll_timestamp, GetPrerollTimestamp());
2441 EXPECT_EQ(expected_preroll_timestamp, player_.GetCurrentTime());
2442 EXPECT_EQ(3, demuxer_->num_data_requests());
2444 // No further seek should have been requested since BrowserSeekPlayer().
2445 EXPECT_EQ(1, demuxer_->num_seek_requests());
2447 // Decoder will be created once data is received.
2448 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
2449 EXPECT_TRUE(GetMediaCodecBridge(false));
2450 WaitForVideoDecodeDone();
2453 // TODO(xhwang): Once we add tests to cover DrmBridge, update this test to
2454 // also verify that the job is successfully created if SetDrmBridge(), Start()
2455 // and eventually OnMediaCrypto() occur. This would increase test coverage of
2456 // http://crbug.com/313470 and allow us to remove inspection of internal player
2457 // pending event state. See http://crbug.com/313860.
2458 TEST_F(MediaSourcePlayerTest, SurfaceChangeClearedEvenIfMediaCryptoAbsent) {
2459 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2461 // Test that |SURFACE_CHANGE_EVENT_PENDING| is not pending after
2462 // SetVideoSurface() for a player configured for encrypted video, when the
2463 // player has not yet received media crypto.
2464 DemuxerConfigs configs = CreateVideoDemuxerConfigs(false);
2465 configs.is_video_encrypted = true;
2467 player_.OnDemuxerConfigsAvailable(configs);
2468 CreateNextTextureAndSetVideoSurface();
2469 EXPECT_FALSE(GetMediaCodecBridge(false));
2472 TEST_F(MediaSourcePlayerTest, CurrentTimeUpdatedWhileDecoderStarved) {
2473 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2475 // Test that current time is updated while decoder is starved.
2476 StartAudioDecoderJob();
2477 DecodeAudioDataUntilOutputBecomesAvailable();
2479 // Trigger starvation while the decoder is decoding.
2480 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(3));
2481 manager_.ResetTimestampUpdated();
2482 TriggerPlayerStarvation();
2483 WaitForAudioDecodeDone();
2485 // Current time should be updated.
2486 EXPECT_TRUE(manager_.timestamp_updated());
2489 TEST_F(MediaSourcePlayerTest, CurrentTimeKeepsIncreasingAfterConfigChange) {
2490 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2492 // Test current time keep on increasing after audio config change.
2493 // Test that current time is updated while decoder is starved.
2494 StartAudioDecoderJob();
2496 DecodeAudioDataUntilOutputBecomesAvailable();
2498 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, true);
2499 DemuxerData data = CreateReadFromDemuxerAckWithConfigChanged(
2500 true, 0, configs);
2501 player_.OnDemuxerDataAvailable(data);
2502 WaitForAudioDecodeDone();
2503 DecodeAudioDataUntilOutputBecomesAvailable();
2506 TEST_F(MediaSourcePlayerTest, VideoMetadataChangeAfterConfigChange) {
2507 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2509 // Test that after a config change, metadata change will be happen
2510 // after decoder is drained.
2511 StartConfigChange(false, true, 2, false);
2512 EXPECT_EQ(1, manager_.num_metadata_changes());
2513 EXPECT_FALSE(IsDrainingDecoder(false));
2515 // Create video data with new resolutions.
2516 DemuxerData data = CreateReadFromDemuxerAckForVideo(true);
2518 // Wait for the metadata change.
2519 while(manager_.num_metadata_changes() == 1) {
2520 player_.OnDemuxerDataAvailable(data);
2521 WaitForVideoDecodeDone();
2523 EXPECT_EQ(2, manager_.num_metadata_changes());
2524 WaitForVideoDecodeDone();
2527 TEST_F(MediaSourcePlayerTest, RequestPlayDeniedDontPlay_Audio) {
2528 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2530 EXPECT_EQ(demuxer_->num_data_requests(), 0);
2531 player_.OnDemuxerConfigsAvailable(CreateDemuxerConfigs(true, false));
2533 manager_.set_allow_play(false);
2534 player_.Start();
2535 EXPECT_FALSE(player_.IsPlaying());
2538 TEST_F(MediaSourcePlayerTest, RequestPlayDeniedDontPlay_Video) {
2539 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2541 EXPECT_EQ(demuxer_->num_data_requests(), 0);
2542 player_.OnDemuxerConfigsAvailable(CreateDemuxerConfigs(false, true));
2544 manager_.set_allow_play(false);
2545 player_.Start();
2546 EXPECT_FALSE(player_.IsPlaying());
2549 TEST_F(MediaSourcePlayerTest, RequestPlayDeniedDontPlay_AV) {
2550 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2552 EXPECT_EQ(demuxer_->num_data_requests(), 0);
2553 player_.OnDemuxerConfigsAvailable(CreateDemuxerConfigs(true, true));
2555 manager_.set_allow_play(false);
2556 player_.Start();
2557 EXPECT_FALSE(player_.IsPlaying());
2560 TEST_F(MediaSourcePlayerTest, RequestPlayGrantedPlays) {
2561 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2563 EXPECT_EQ(demuxer_->num_data_requests(), 0);
2564 player_.OnDemuxerConfigsAvailable(CreateDemuxerConfigs(true, true));
2566 manager_.set_allow_play(true);
2567 player_.Start();
2568 EXPECT_TRUE(player_.IsPlaying());
2571 } // namespace media