Roll src/third_party/WebKit d10c917:a1123a1 (svn 198729:198730)
[chromium-blink-merge.git] / media / base / android / media_source_player_unittest.cc
blobbe6db141b87cb7c2f353d1341a8282fd8c057fc9
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; }
85 bool RequestPlay(int player_id) override {
86 return allow_play_;
89 void OnAudibleStateChanged(int player_id, bool is_audible_now) override {
90 is_audible_ = is_audible_now;
93 bool playback_completed() const {
94 return playback_completed_;
97 int num_resources_requested() const {
98 return num_resources_requested_;
101 int num_metadata_changes() const {
102 return num_metadata_changes_;
105 void OnMediaResourcesRequested(int player_id) {
106 num_resources_requested_++;
109 bool timestamp_updated() const {
110 return timestamp_updated_;
113 void ResetTimestampUpdated() {
114 timestamp_updated_ = false;
117 bool is_audible() const {
118 return is_audible_;
121 bool is_delay_expired() const {
122 return is_delay_expired_;
125 void SetDelayExpired(bool value) {
126 is_delay_expired_ = value;
129 void set_allow_play(bool value) {
130 allow_play_ = value;
133 private:
134 base::MessageLoop* message_loop_;
135 bool playback_completed_;
136 // The number of resource requests this object has seen.
137 int num_resources_requested_;
138 // The number of metadata changes reported by the player.
139 int num_metadata_changes_;
140 // Playback timestamp was updated.
141 bool timestamp_updated_;
142 // Audible state of the pipeline
143 bool is_audible_;
144 // Helper flag to ensure delay for WaitForDelay().
145 bool is_delay_expired_;
146 // Whether the manager will allow players that request playing.
147 bool allow_play_;
149 DISALLOW_COPY_AND_ASSIGN(MockMediaPlayerManager);
152 class MockDemuxerAndroid : public DemuxerAndroid {
153 public:
154 explicit MockDemuxerAndroid(base::MessageLoop* message_loop)
155 : message_loop_(message_loop),
156 num_data_requests_(0),
157 num_seek_requests_(0),
158 num_browser_seek_requests_(0) {}
159 ~MockDemuxerAndroid() override {}
161 void Initialize(DemuxerAndroidClient* client) override {}
162 void RequestDemuxerData(DemuxerStream::Type type) override {
163 num_data_requests_++;
164 if (message_loop_->is_running())
165 message_loop_->Quit();
167 void RequestDemuxerSeek(const base::TimeDelta& time_to_seek,
168 bool is_browser_seek) override {
169 num_seek_requests_++;
170 if (is_browser_seek)
171 num_browser_seek_requests_++;
174 int num_data_requests() const { return num_data_requests_; }
175 int num_seek_requests() const { return num_seek_requests_; }
176 int num_browser_seek_requests() const { return num_browser_seek_requests_; }
178 private:
179 base::MessageLoop* message_loop_;
181 // The number of encoded data requests this object has seen.
182 int num_data_requests_;
184 // The number of regular and browser seek requests this object has seen.
185 int num_seek_requests_;
187 // The number of browser seek requests this object has seen.
188 int num_browser_seek_requests_;
190 DISALLOW_COPY_AND_ASSIGN(MockDemuxerAndroid);
193 class MediaSourcePlayerTest : public testing::Test {
194 public:
195 MediaSourcePlayerTest()
196 : manager_(&message_loop_),
197 demuxer_(new MockDemuxerAndroid(&message_loop_)),
198 player_(0, &manager_,
199 base::Bind(&MockMediaPlayerManager::OnMediaResourcesRequested,
200 base::Unretained(&manager_)),
201 scoped_ptr<DemuxerAndroid>(demuxer_),
202 GURL()),
203 decoder_callback_hook_executed_(false),
204 surface_texture_a_is_next_(true) {}
206 ~MediaSourcePlayerTest() override {}
208 protected:
209 // Get the decoder job from the MediaSourcePlayer. The return value must not
210 // be NULL.
211 MediaDecoderJob* GetMediaDecoderJob(bool is_audio) {
212 if (is_audio) {
213 return reinterpret_cast<MediaDecoderJob*>(
214 player_.audio_decoder_job_.get());
216 return reinterpret_cast<MediaDecoderJob*>(
217 player_.video_decoder_job_.get());
220 // Get the MediaCodecBridge from the decoder job. The return value could be
221 // NULL if the decoder is not yet created.
222 MediaCodecBridge* GetMediaCodecBridge(bool is_audio) {
223 if (is_audio)
224 return player_.audio_decoder_job_->media_codec_bridge_.get();
225 return player_.video_decoder_job_->media_codec_bridge_.get();
228 // Get the per-job prerolling status from the MediaSourcePlayer's job matching
229 // |is_audio|. Caller must guard against NPE if the player's job is NULL.
230 bool IsPrerolling(bool is_audio) {
231 return GetMediaDecoderJob(is_audio)->prerolling_;
234 // Get the preroll timestamp from the MediaSourcePlayer.
235 base::TimeDelta GetPrerollTimestamp() {
236 return player_.preroll_timestamp_;
239 // Simulate player has reached starvation timeout.
240 void TriggerPlayerStarvation() {
241 player_.decoder_starvation_callback_.Cancel();
242 player_.OnDecoderStarved();
245 // Release() the player.
246 void ReleasePlayer() {
247 EXPECT_TRUE(player_.IsPlaying());
248 player_.Release();
249 EXPECT_FALSE(player_.IsPlaying());
252 // Upon the next successful decode callback, post a task to call Release()
253 // on the |player_|. TEST_F's do not have access to the private player
254 // members, hence this helper method.
255 // Prevent usage creep of MSP::set_decode_callback_for_testing() by
256 // only using it for the ReleaseWithOnPrefetchDoneAlreadyPosted test.
257 void OnNextTestDecodeCallbackPostTaskToReleasePlayer() {
258 DCHECK_EQ(&message_loop_, base::MessageLoop::current());
259 player_.set_decode_callback_for_testing(media::BindToCurrentLoop(
260 base::Bind(
261 &MediaSourcePlayerTest::ReleaseWithPendingPrefetchDoneVerification,
262 base::Unretained(this))));
265 // Asynch test callback posted upon decode completion to verify that a pending
266 // prefetch done event is not cleared across |player_|'s Release(). This helps
267 // ensure the ReleaseWithOnPrefetchDoneAlreadyPosted test scenario is met.
268 void ReleaseWithPendingPrefetchDoneVerification() {
269 EXPECT_TRUE(player_.IsEventPending(player_.PREFETCH_DONE_EVENT_PENDING));
270 ReleasePlayer();
271 EXPECT_TRUE(player_.IsEventPending(player_.PREFETCH_DONE_EVENT_PENDING));
272 EXPECT_FALSE(decoder_callback_hook_executed_);
273 EXPECT_FALSE(GetMediaCodecBridge(true));
274 decoder_callback_hook_executed_ = true;
277 DemuxerConfigs CreateAudioDemuxerConfigs(AudioCodec audio_codec,
278 bool use_low_sample_rate) {
279 DemuxerConfigs configs;
280 configs.audio_codec = audio_codec;
281 configs.audio_channels = 2;
282 configs.is_audio_encrypted = false;
283 configs.duration = kDefaultDuration;
285 if (audio_codec == kCodecVorbis) {
286 configs.audio_sampling_rate = use_low_sample_rate ? 11025 : 44100;
287 scoped_refptr<DecoderBuffer> buffer = ReadTestDataFile(
288 "vorbis-extradata");
289 configs.audio_extra_data = std::vector<uint8>(
290 buffer->data(),
291 buffer->data() + buffer->data_size());
292 return configs;
295 // Other codecs are not yet supported by this helper.
296 EXPECT_EQ(audio_codec, kCodecAAC);
298 configs.audio_sampling_rate = 48000;
299 uint8 aac_extra_data[] = { 0x13, 0x10 };
300 configs.audio_extra_data = std::vector<uint8>(
301 aac_extra_data,
302 aac_extra_data + 2);
303 return configs;
306 DemuxerConfigs CreateVideoDemuxerConfigs(bool use_larger_size) {
307 DemuxerConfigs configs;
308 configs.video_codec = kCodecVP8;
309 configs.video_size =
310 use_larger_size ? gfx::Size(640, 240) : gfx::Size(320, 240);
311 configs.is_video_encrypted = false;
312 configs.duration = kDefaultDuration;
313 return configs;
316 DemuxerConfigs CreateAudioVideoDemuxerConfigs() {
317 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, false);
318 configs.video_codec = kCodecVP8;
319 configs.video_size = gfx::Size(320, 240);
320 configs.is_video_encrypted = false;
321 return configs;
324 DemuxerConfigs CreateDemuxerConfigs(bool have_audio, bool have_video) {
325 DCHECK(have_audio || have_video);
327 if (have_audio && !have_video)
328 return CreateAudioDemuxerConfigs(kCodecVorbis, false);
330 if (have_video && !have_audio)
331 return CreateVideoDemuxerConfigs(false);
333 return CreateAudioVideoDemuxerConfigs();
336 // Starts an audio decoder job.
337 void StartAudioDecoderJob() {
338 Start(CreateAudioDemuxerConfigs(kCodecVorbis, false));
341 // Starts a video decoder job.
342 void StartVideoDecoderJob() {
343 Start(CreateVideoDemuxerConfigs(false));
346 // Starts decoding the data.
347 void Start(const DemuxerConfigs& configs) {
348 EXPECT_EQ(demuxer_->num_data_requests(), 0);
349 player_.OnDemuxerConfigsAvailable(configs);
350 player_.Start();
352 EXPECT_TRUE(player_.IsPlaying());
353 int expected_num_requests = (player_.HasAudio() ? 1 : 0) +
354 (player_.HasVideo() ? 1 : 0);
355 EXPECT_EQ(expected_num_requests, demuxer_->num_data_requests());
358 // Resumes decoding the data. Verifies player behavior relative to
359 // |expect_player_requests_audio_data| and
360 // |expect_player_requests_video_data|.
361 void Resume(bool expect_player_requests_audio_data,
362 bool expect_player_requests_video_data) {
363 EXPECT_FALSE(player_.IsPlaying());
364 EXPECT_TRUE(player_.HasVideo() || player_.HasAudio());
365 int original_num_data_requests = demuxer_->num_data_requests();
366 int expected_request_delta =
367 (expect_player_requests_audio_data ? 1 : 0) +
368 (expect_player_requests_video_data ? 1 : 0);
370 player_.Start();
372 EXPECT_TRUE(player_.IsPlaying());
373 EXPECT_EQ(original_num_data_requests + expected_request_delta,
374 demuxer_->num_data_requests());
377 // Keeps decoding audio data until the decoder starts to output samples.
378 // Gives up if no audio output after decoding 10 frames.
379 void DecodeAudioDataUntilOutputBecomesAvailable() {
380 EXPECT_TRUE(player_.IsPlaying());
381 base::TimeDelta current_time = player_.GetCurrentTime();
382 base::TimeDelta start_timestamp = current_time;
383 for (int i = 0; i < 10; ++i) {
384 manager_.ResetTimestampUpdated();
385 player_.OnDemuxerDataAvailable(
386 CreateReadFromDemuxerAckForAudio(i > 3 ? 3 : i));
387 WaitForAudioDecodeDone();
388 base::TimeDelta new_current_time = player_.GetCurrentTime();
389 EXPECT_LE(current_time.InMilliseconds(),
390 new_current_time.InMilliseconds());
391 current_time = new_current_time;
392 if (manager_.timestamp_updated()) {
393 // TODO(qinmin): the current time is from the decoder thread and it does
394 // not take the delay from posting the task into consideration.
395 // http://crbug.com/421616.
396 EXPECT_LE(start_timestamp.InMillisecondsF(),
397 new_current_time.InMillisecondsF());
398 return;
401 EXPECT_TRUE(false);
404 AccessUnit CreateAccessUnitWithData(bool is_audio, int audio_packet_id,
405 bool use_large_size_video) {
406 AccessUnit unit;
408 unit.status = DemuxerStream::kOk;
409 scoped_refptr<DecoderBuffer> buffer;
410 if (is_audio) {
411 buffer = ReadTestDataFile(
412 base::StringPrintf("vorbis-packet-%d", audio_packet_id));
413 } else {
414 buffer = ReadTestDataFile(
415 use_large_size_video ? "vp8-I-frame-640x240" : "vp8-I-frame-320x240");
417 unit.data = std::vector<uint8>(
418 buffer->data(), buffer->data() + buffer->data_size());
420 if (is_audio) {
421 // Vorbis needs 4 extra bytes padding on Android to decode properly. Check
422 // NuMediaExtractor.cpp in Android source code.
423 uint8 padding[4] = { 0xff , 0xff , 0xff , 0xff };
424 unit.data.insert(unit.data.end(), padding, padding + 4);
427 return unit;
430 DemuxerData CreateReadFromDemuxerAckForAudio(int packet_id) {
431 DemuxerData data;
432 data.type = DemuxerStream::AUDIO;
433 data.access_units.resize(1);
434 data.access_units[0] = CreateAccessUnitWithData(true, packet_id, false);
436 return data;
439 DemuxerData CreateReadFromDemuxerAckForVideo(bool use_large_size) {
440 DemuxerData data;
441 data.type = DemuxerStream::VIDEO;
442 data.access_units.resize(1);
443 data.access_units[0] = CreateAccessUnitWithData(false, 0, use_large_size);
444 return data;
447 DemuxerData CreateEOSAck(bool is_audio) {
448 DemuxerData data;
449 data.type = is_audio ? DemuxerStream::AUDIO : DemuxerStream::VIDEO;
450 data.access_units.resize(1);
451 data.access_units[0].status = DemuxerStream::kOk;
452 data.access_units[0].is_end_of_stream = true;
453 return data;
456 DemuxerData CreateAbortedAck(bool is_audio) {
457 DemuxerData data;
458 data.type = is_audio ? DemuxerStream::AUDIO : DemuxerStream::VIDEO;
459 data.access_units.resize(1);
460 data.access_units[0].status = DemuxerStream::kAborted;
461 return data;
464 bool HasData(bool is_audio) {
465 return GetMediaDecoderJob(is_audio)->HasData();
468 // Helper method for use at test start. It starts an audio decoder job and
469 // immediately feeds it some data to decode. Then, without letting the decoder
470 // job complete a decode cycle, it also starts player SeekTo(). Upon return,
471 // the player should not yet have sent the DemuxerSeek IPC request, though
472 // seek event should be pending. The audio decoder job will also still be
473 // decoding.
474 void StartAudioDecoderJobAndSeekToWhileDecoding(
475 const base::TimeDelta& seek_time) {
476 EXPECT_FALSE(GetMediaCodecBridge(true));
477 EXPECT_FALSE(player_.IsPlaying());
478 EXPECT_EQ(0, demuxer_->num_data_requests());
479 EXPECT_EQ(0.0, GetPrerollTimestamp().InMillisecondsF());
480 EXPECT_EQ(player_.GetCurrentTime(), GetPrerollTimestamp());
481 StartAudioDecoderJob();
482 EXPECT_FALSE(GetMediaDecoderJob(true)->is_decoding());
483 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
484 EXPECT_EQ(2, demuxer_->num_data_requests());
485 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
486 player_.SeekTo(seek_time);
487 EXPECT_EQ(0.0, GetPrerollTimestamp().InMillisecondsF());
488 EXPECT_EQ(0, demuxer_->num_seek_requests());
489 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
492 // Seek, including simulated receipt of |kAborted| read between SeekTo() and
493 // OnDemuxerSeekDone(). Use this helper method only when the player already
494 // has created the media codec bridge. Exactly one request for more data is
495 // expected following the seek, so use this helper for players with only audio
496 // or only video.
497 void SeekPlayerWithAbort(bool is_audio, const base::TimeDelta& seek_time) {
498 int original_num_seeks = demuxer_->num_seek_requests();
499 int original_num_data_requests = demuxer_->num_data_requests();
501 // Initiate a seek. Skip the round-trip of requesting seek from renderer.
502 // Instead behave as if the renderer has asked us to seek.
503 player_.SeekTo(seek_time);
505 // Verify that the seek does not occur until previously outstanding data
506 // request is satisfied.
507 EXPECT_EQ(original_num_seeks, demuxer_->num_seek_requests());
509 // Simulate seeking causes the demuxer to abort the outstanding read
510 // caused by the seek.
511 player_.OnDemuxerDataAvailable(CreateAbortedAck(is_audio));
513 // Wait for the decode job to finish so we can process the seek request.
514 WaitForDecodeDone(is_audio, !is_audio);
516 // Verify that the seek is requested.
517 EXPECT_EQ(original_num_seeks + 1, demuxer_->num_seek_requests());
519 // Send back the seek done notification. This should trigger the player to
520 // call OnReadFromDemuxer() again.
521 EXPECT_EQ(original_num_data_requests, demuxer_->num_data_requests());
522 player_.OnDemuxerSeekDone(kNoTimestamp());
523 EXPECT_EQ(original_num_data_requests + 1, demuxer_->num_data_requests());
525 // No other seek should have been requested.
526 EXPECT_EQ(original_num_seeks + 1, demuxer_->num_seek_requests());
529 // Preroll the decoder job to |target_timestamp|. The first access unit
530 // to decode will have a timestamp equal to |start_timestamp|.
531 // |is_clock_manager| indicates whether the decoder serves as the clock
532 // manager for the player.
533 // TODO(qinmin): Add additional test cases for out-of-order decodes.
534 // See http://crbug.com/331421.
535 void PrerollDecoderToTime(bool is_audio,
536 const base::TimeDelta& start_timestamp,
537 const base::TimeDelta& target_timestamp,
538 bool is_clock_manager) {
539 // For streams with both audio and video, it is possible that audio rolls
540 // past the |target_timestamp|. As a result, the current time may be larger
541 // than the |target_timestamp| for video as it may not be the clock manager.
542 EXPECT_TRUE(!is_clock_manager ||
543 target_timestamp == player_.GetCurrentTime());
544 // |start_timestamp| must be smaller than |target_timestamp|.
545 EXPECT_LE(start_timestamp, target_timestamp);
546 DemuxerData data = is_audio ? CreateReadFromDemuxerAckForAudio(1) :
547 CreateReadFromDemuxerAckForVideo(false);
548 int current_timestamp = start_timestamp.InMilliseconds();
550 // Send some data with access unit timestamps before the |target_timestamp|,
551 // and continue sending the data until preroll finishes.
552 // This simulates the common condition that AUs received after browser
553 // seek begin with timestamps before the seek target, and don't
554 // immediately complete preroll.
555 while (IsPrerolling(is_audio)) {
556 data.access_units[0].timestamp =
557 base::TimeDelta::FromMilliseconds(current_timestamp);
558 player_.OnDemuxerDataAvailable(data);
559 EXPECT_TRUE(GetMediaDecoderJob(is_audio)->is_decoding());
560 EXPECT_TRUE(GetMediaCodecBridge(is_audio));
561 EXPECT_TRUE(!is_clock_manager ||
562 target_timestamp == player_.GetCurrentTime());
563 current_timestamp += 30;
564 WaitForDecodeDone(is_audio, !is_audio);
566 EXPECT_LE(target_timestamp, player_.GetCurrentTime());
569 void PlayAudioForTimeInterval(const base::TimeDelta& start_timestamp,
570 const base::TimeDelta& target_timestamp ) {
572 DemuxerData data = CreateReadFromDemuxerAckForAudio(1);
573 int current_timestamp = start_timestamp.InMilliseconds();
574 int stop_timestamp = target_timestamp.InMilliseconds();
575 while (current_timestamp < stop_timestamp) {
576 data.access_units[0].timestamp =
577 base::TimeDelta::FromMilliseconds(current_timestamp);
578 player_.OnDemuxerDataAvailable(data);
579 current_timestamp += 30;
580 WaitForAudioDecodeDone();
584 void WaitForDelay(const base::TimeDelta& delay) {
585 // Let the message_loop_ process events.
586 // We post delayed task and RunUnitilIdle() until it signals.
588 manager_.SetDelayExpired(false);
589 message_loop_.PostDelayedTask(
590 FROM_HERE,
591 base::Bind(&MockMediaPlayerManager::SetDelayExpired,
592 base::Unretained(&manager_),
593 true),
594 delay);
596 while (!manager_.is_delay_expired())
597 message_loop_.RunUntilIdle();
600 DemuxerData CreateReadFromDemuxerAckWithConfigChanged(
601 bool is_audio,
602 int config_unit_index,
603 const DemuxerConfigs& configs) {
604 DemuxerData data;
605 data.type = is_audio ? DemuxerStream::AUDIO : DemuxerStream::VIDEO;
606 data.access_units.resize(config_unit_index + 1);
608 for (int i = 0; i < config_unit_index; ++i)
609 data.access_units[i] = CreateAccessUnitWithData(is_audio, i, false);
611 data.access_units[config_unit_index].status = DemuxerStream::kConfigChanged;
612 data.demuxer_configs.resize(1);
613 data.demuxer_configs[0] = configs;
614 return data;
617 // Valid only for video-only player tests. If |trigger_with_release_start| is
618 // true, triggers the browser seek with a Release() + video data received +
619 // Start() with a new surface. If false, triggers the browser seek by
620 // setting a new video surface after beginning decode of received video data.
621 // Such data receipt causes possibility that an I-frame is not next, and
622 // browser seek results once decode completes and surface change processing
623 // begins.
624 void BrowserSeekPlayer(bool trigger_with_release_start) {
625 int expected_num_data_requests = demuxer_->num_data_requests() + 2;
626 int expected_num_seek_requests = demuxer_->num_seek_requests();
627 int expected_num_browser_seek_requests =
628 demuxer_->num_browser_seek_requests();
630 CreateNextTextureAndSetVideoSurface();
631 StartVideoDecoderJob();
632 if (trigger_with_release_start) {
633 // Consume the first frame, so that the next VideoDecoderJob will not
634 // inherit the I-frame from the previous decoder.
635 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
636 ReleasePlayer();
637 WaitForVideoDecodeDone();
639 // Simulate demuxer's response to the video data request. The data will be
640 // passed to the next MediaCodecBridge.
641 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
642 EXPECT_FALSE(GetMediaCodecBridge(false));
643 EXPECT_FALSE(player_.IsPlaying());
644 EXPECT_EQ(expected_num_seek_requests, demuxer_->num_seek_requests());
646 CreateNextTextureAndSetVideoSurface();
647 Resume(false, false);
648 EXPECT_FALSE(GetMediaCodecBridge(false));
650 // Run the message loop so that prefetch will complete.
651 while (expected_num_seek_requests == demuxer_->num_seek_requests())
652 message_loop_.RunUntilIdle();
653 } else {
654 // Simulate demuxer's response to the video data request.
655 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
657 // While the decoder is decoding, trigger a browser seek by changing
658 // surface. Demuxer does not know of browser seek in advance, so no
659 // |kAborted| data is required (though |kAborted| can certainly occur for
660 // any pending read in reality due to renderer preparing for a regular
661 // seek).
662 CreateNextTextureAndSetVideoSurface();
664 // Browser seek should not begin until decoding has completed.
665 EXPECT_TRUE(GetMediaCodecBridge(false));
666 EXPECT_EQ(expected_num_seek_requests, demuxer_->num_seek_requests());
668 // Wait for the media codec bridge to finish decoding and be reset pending
669 // the browser seek.
670 WaitForVideoDecodeDone();
671 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
674 // Only one browser seek should have been initiated, and no further data
675 // should have been requested.
676 expected_num_seek_requests++;
677 expected_num_browser_seek_requests++;
678 EXPECT_EQ(expected_num_seek_requests, demuxer_->num_seek_requests());
679 EXPECT_EQ(expected_num_browser_seek_requests,
680 demuxer_->num_browser_seek_requests());
681 EXPECT_EQ(expected_num_data_requests, demuxer_->num_data_requests());
684 // Creates a new media codec bridge and feeds it data ending with a
685 // |kConfigChanged| access unit. If |config_unit_in_prefetch| is true, sends
686 // feeds the config change AU in response to the job's first read request
687 // (prefetch). If false, regular data is fed and decoded prior to feeding the
688 // config change AU in response to the second data request (after prefetch
689 // completed). |config_unit_index| controls which access unit is
690 // |kConfigChanged|. If |enable_adaptive_playback| is true, config change will
691 // not cause the decoder to recreate the media codec bridge. Otherwise, the
692 // decoder has to drain all its data before recreating the new codec.
693 void SendConfigChangeToDecoder(bool is_audio,
694 bool config_unit_in_prefetch,
695 int config_unit_index,
696 bool enable_adaptive_playback) {
697 EXPECT_FALSE(GetMediaCodecBridge(is_audio));
698 if (is_audio) {
699 StartAudioDecoderJob();
700 } else {
701 CreateNextTextureAndSetVideoSurface();
702 StartVideoDecoderJob();
705 int expected_num_data_requests = demuxer_->num_data_requests();
706 // Feed and decode a standalone access unit so the player exits prefetch.
707 if (!config_unit_in_prefetch) {
708 if (is_audio) {
709 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
710 } else {
711 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
712 EnableAdaptiveVideoPlayback(enable_adaptive_playback);
715 WaitForDecodeDone(is_audio, !is_audio);
717 // We should have completed the prefetch phase at this point.
718 expected_num_data_requests++;
719 EXPECT_EQ(expected_num_data_requests, demuxer_->num_data_requests());
722 DemuxerConfigs configs = is_audio ?
723 CreateAudioDemuxerConfigs(kCodecVorbis, true) :
724 CreateVideoDemuxerConfigs(true);
725 // Feed and decode access units with data for any units prior to
726 // |config_unit_index|, and a |kConfigChanged| unit at that index.
727 // Player should prepare to reconfigure the decoder job, and should request
728 // new demuxer configs.
729 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckWithConfigChanged(
730 is_audio, config_unit_index, configs));
732 expected_num_data_requests++;
733 EXPECT_EQ(expected_num_data_requests, demuxer_->num_data_requests());
734 if (is_audio)
735 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
736 else
737 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(true));
739 // If the adaptive playback setting was not passed to the MediaCodecBridge
740 // earlier, do it here.
741 if (config_unit_in_prefetch && !is_audio)
742 EnableAdaptiveVideoPlayback(enable_adaptive_playback);
745 // Send a config change to the decoder job and drain the decoder so that the
746 // config change is processed.
747 void StartConfigChange(bool is_audio,
748 bool config_unit_in_prefetch,
749 int config_unit_index,
750 bool enable_adaptive_playback) {
751 SendConfigChangeToDecoder(is_audio, config_unit_in_prefetch,
752 config_unit_index, enable_adaptive_playback);
754 EXPECT_EQ(!config_unit_in_prefetch && !enable_adaptive_playback &&
755 config_unit_index == 0, IsDrainingDecoder(is_audio));
756 int expected_num_data_requests = demuxer_->num_data_requests();
757 // Run until decoder starts to request new data.
758 while (demuxer_->num_data_requests() == expected_num_data_requests)
759 message_loop_.RunUntilIdle();
760 EXPECT_FALSE(IsDrainingDecoder(is_audio));
763 void EnableAdaptiveVideoPlayback(bool enable) {
764 EXPECT_TRUE(GetMediaCodecBridge(false));
765 static_cast<VideoCodecBridge*>(GetMediaCodecBridge(false))->
766 set_adaptive_playback_supported_for_testing(
767 enable ? 1 : 0);
770 void CreateNextTextureAndSetVideoSurface() {
771 gfx::SurfaceTexture* surface_texture;
772 if (surface_texture_a_is_next_) {
773 surface_texture_a_ = gfx::SurfaceTexture::Create(next_texture_id_++);
774 surface_texture = surface_texture_a_.get();
775 } else {
776 surface_texture_b_ = gfx::SurfaceTexture::Create(next_texture_id_++);
777 surface_texture = surface_texture_b_.get();
780 surface_texture_a_is_next_ = !surface_texture_a_is_next_;
781 gfx::ScopedJavaSurface surface = gfx::ScopedJavaSurface(surface_texture);
782 player_.SetVideoSurface(surface.Pass());
785 // Wait for one or both of the jobs to complete decoding. Media codec bridges
786 // are assumed to exist for any stream whose decode completion is awaited.
787 void WaitForDecodeDone(bool wait_for_audio, bool wait_for_video) {
788 DCHECK(wait_for_audio || wait_for_video);
789 while ((wait_for_audio && GetMediaCodecBridge(true) &&
790 GetMediaDecoderJob(true)->HasData() &&
791 GetMediaDecoderJob(true)->is_decoding()) ||
792 (wait_for_video && GetMediaCodecBridge(false) &&
793 GetMediaDecoderJob(false)->HasData() &&
794 GetMediaDecoderJob(false)->is_decoding())) {
795 message_loop_.RunUntilIdle();
799 void WaitForAudioDecodeDone() {
800 WaitForDecodeDone(true, false);
803 void WaitForVideoDecodeDone() {
804 WaitForDecodeDone(false, true);
807 void WaitForAudioVideoDecodeDone() {
808 WaitForDecodeDone(true, true);
811 // If |send_eos| is true, generates EOS for the stream corresponding to
812 // |eos_for_audio|. Verifies that playback completes and no further data
813 // is requested.
814 // If |send_eos| is false, then it is assumed that caller previously arranged
815 // for player to receive EOS for each stream, but the player has not yet
816 // decoded all of them. In this case, |eos_for_audio| is ignored.
817 void VerifyPlaybackCompletesOnEOSDecode(bool send_eos, bool eos_for_audio) {
818 int original_num_data_requests = demuxer_->num_data_requests();
819 if (send_eos)
820 player_.OnDemuxerDataAvailable(CreateEOSAck(eos_for_audio));
821 EXPECT_FALSE(manager_.playback_completed());
822 message_loop_.Run();
823 EXPECT_TRUE(manager_.playback_completed());
824 EXPECT_EQ(original_num_data_requests, demuxer_->num_data_requests());
827 void VerifyCompletedPlaybackResumesOnSeekPlusStart(bool have_audio,
828 bool have_video) {
829 DCHECK(have_audio || have_video);
831 EXPECT_TRUE(manager_.playback_completed());
833 player_.SeekTo(base::TimeDelta());
834 player_.OnDemuxerSeekDone(kNoTimestamp());
835 Resume(have_audio, have_video);
838 // Starts the appropriate decoder jobs according to |have_audio| and
839 // |have_video|. Then starts seek during decode of EOS or non-EOS according to
840 // |eos_audio| and |eos_video|. Simulates seek completion and verifies that
841 // playback never completed. |eos_{audio,video}| is ignored if the
842 // corresponding |have_{audio,video}| is false.
843 void VerifySeekDuringEOSDecodePreventsPlaybackCompletion(bool have_audio,
844 bool have_video,
845 bool eos_audio,
846 bool eos_video) {
847 DCHECK(have_audio || have_video);
849 if (have_video)
850 CreateNextTextureAndSetVideoSurface();
852 Start(CreateDemuxerConfigs(have_audio, have_video));
854 if (have_audio)
855 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
857 if (have_video)
858 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
860 // Run until more data is requested a number of times equal to the number of
861 // media types configured. Since prefetching may be in progress, we cannot
862 // reliably expect Run() to complete until we have sent demuxer data for all
863 // configured media types, above.
864 WaitForDecodeDone(have_audio, have_video);
866 // Simulate seek while decoding EOS or non-EOS for the appropriate
867 // stream(s).
868 if (have_audio) {
869 if (eos_audio)
870 player_.OnDemuxerDataAvailable(CreateEOSAck(true));
871 else
872 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(1));
875 if (have_video) {
876 if (eos_video)
877 player_.OnDemuxerDataAvailable(CreateEOSAck(false));
878 else
879 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
882 player_.SeekTo(base::TimeDelta());
883 EXPECT_EQ(0, demuxer_->num_seek_requests());
884 WaitForDecodeDone(have_audio, have_video);
885 EXPECT_EQ(1, demuxer_->num_seek_requests());
887 player_.OnDemuxerSeekDone(kNoTimestamp());
888 EXPECT_FALSE(manager_.playback_completed());
891 base::TimeTicks StartTimeTicks() {
892 return player_.start_time_ticks_;
895 bool IsRequestingDemuxerData(bool is_audio) {
896 return GetMediaDecoderJob(is_audio)->is_requesting_demuxer_data_;
899 bool IsDrainingDecoder(bool is_audio) {
900 return GetMediaDecoderJob(is_audio)->drain_decoder_;
903 protected:
904 base::MessageLoop message_loop_;
905 MockMediaPlayerManager manager_;
906 MockDemuxerAndroid* demuxer_; // Owned by |player_|.
907 MediaSourcePlayer player_;
909 // Track whether a possibly async decoder callback test hook has run.
910 bool decoder_callback_hook_executed_;
912 // We need to keep the surface texture while the decoder is actively decoding.
913 // Otherwise, it may trigger unexpected crashes on some devices. To switch
914 // surfaces, tests need to create a new surface texture without releasing
915 // their previous one. In CreateNextTextureAndSetVideoSurface(), we toggle
916 // between two surface textures, only replacing the N-2 texture. Assumption is
917 // that no more than N-1 texture is in use by decoder when
918 // CreateNextTextureAndSetVideoSurface() is called.
919 scoped_refptr<gfx::SurfaceTexture> surface_texture_a_;
920 scoped_refptr<gfx::SurfaceTexture> surface_texture_b_;
921 bool surface_texture_a_is_next_;
922 int next_texture_id_;
924 bool verify_not_audible_is_called_;
926 DISALLOW_COPY_AND_ASSIGN(MediaSourcePlayerTest);
929 TEST_F(MediaSourcePlayerTest, StartAudioDecoderWithValidConfig) {
930 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
932 // Test audio codec will be created when valid configs and data are passed to
933 // the audio decoder job.
934 StartAudioDecoderJob();
935 EXPECT_EQ(0, demuxer_->num_seek_requests());
936 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
937 EXPECT_TRUE(GetMediaCodecBridge(true));
940 TEST_F(MediaSourcePlayerTest, StartAudioDecoderWithInvalidConfig) {
941 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
943 // Test audio decoder job will not be created when failed to start the codec.
944 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, false);
945 // Replace with invalid |audio_extra_data|
946 configs.audio_extra_data.clear();
947 uint8 invalid_codec_data[] = { 0x00, 0xff, 0xff, 0xff, 0xff };
948 configs.audio_extra_data.insert(configs.audio_extra_data.begin(),
949 invalid_codec_data, invalid_codec_data + 4);
950 Start(configs);
952 // Decoder is not created after data is received.
953 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
954 EXPECT_FALSE(GetMediaCodecBridge(true));
957 // timav
958 TEST_F(MediaSourcePlayerTest, AudioDecoderSetsAudibleState) {
959 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
961 // No data arrived yet
962 EXPECT_FALSE(manager_.is_audible());
964 // Initialize decoder
965 StartAudioDecoderJob();
966 player_.SetVolume(1.0);
968 // Process frames until prerolling is done.
969 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100));
970 EXPECT_TRUE(IsPrerolling(true));
971 PrerollDecoderToTime(
972 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), false);
973 EXPECT_TRUE(IsPrerolling(false));
975 // Send more packets
976 PlayAudioForTimeInterval(base::TimeDelta::FromMilliseconds(150),
977 base::TimeDelta::FromMilliseconds(220));
979 // The player should trigger audible status
980 EXPECT_TRUE(manager_.is_audible());
982 // The player release should report a non-audible state.
983 ReleasePlayer();
984 EXPECT_FALSE(manager_.is_audible());
987 TEST_F(MediaSourcePlayerTest, AudioDecoderRemovesAudibleStateWhenPaused) {
988 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
990 // No data arrived yet
991 EXPECT_FALSE(manager_.is_audible());
993 // Initialize decoder
994 StartAudioDecoderJob();
995 player_.SetVolume(1.0);
997 // Process frames until prerolling is done.
998 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100));
999 EXPECT_TRUE(IsPrerolling(true));
1000 PrerollDecoderToTime(
1001 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), false);
1002 EXPECT_TRUE(IsPrerolling(false));
1004 // Send more packets
1005 PlayAudioForTimeInterval(base::TimeDelta::FromMilliseconds(150),
1006 base::TimeDelta::FromMilliseconds(220));
1008 // The player should trigger audible status
1009 EXPECT_TRUE(manager_.is_audible());
1011 // Pause the player
1012 player_.Pause(true);
1014 // Send more packets
1015 PlayAudioForTimeInterval(base::TimeDelta::FromMilliseconds(240),
1016 base::TimeDelta::FromMilliseconds(280));
1018 // The player should trigger audible status again
1019 EXPECT_FALSE(manager_.is_audible());
1021 player_.Release();
1024 TEST_F(MediaSourcePlayerTest, AudioDecoderRemovesAudibleStateWhenIdle) {
1025 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1027 // No data arrived yet
1028 EXPECT_FALSE(manager_.is_audible());
1030 // Initialize decoder
1031 StartAudioDecoderJob();
1032 player_.SetVolume(1.0);
1034 // Process frames until prerolling is done.
1035 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100));
1036 EXPECT_TRUE(IsPrerolling(true));
1037 PrerollDecoderToTime(
1038 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), false);
1039 EXPECT_TRUE(IsPrerolling(false));
1041 // Send more packets
1042 PlayAudioForTimeInterval(base::TimeDelta::FromMilliseconds(150),
1043 base::TimeDelta::FromMilliseconds(220));
1045 // The player should trigger audible status
1046 EXPECT_TRUE(manager_.is_audible());
1048 // Simulate the freeze on demuxer: wait for 300 ms
1049 WaitForDelay(base::TimeDelta::FromMilliseconds(300));
1051 // By this time the player should have reported
1052 // that there is no audio.
1053 EXPECT_FALSE(manager_.is_audible());
1055 ReleasePlayer();
1058 TEST_F(MediaSourcePlayerTest, StartVideoCodecWithValidSurface) {
1059 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1061 // Test video codec will not be created until data is received.
1062 StartVideoDecoderJob();
1064 // Set both an initial and a later video surface without receiving any
1065 // demuxed data yet.
1066 CreateNextTextureAndSetVideoSurface();
1067 EXPECT_FALSE(GetMediaCodecBridge(false));
1068 CreateNextTextureAndSetVideoSurface();
1069 EXPECT_FALSE(GetMediaCodecBridge(false));
1071 // No seeks, even on setting surface, should have occurred. (Browser seeks can
1072 // occur on setting surface, but only after previously receiving video data.)
1073 EXPECT_EQ(0, demuxer_->num_seek_requests());
1075 // Send the first input chunk and verify that decoder will be created.
1076 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1077 EXPECT_TRUE(GetMediaCodecBridge(false));
1078 WaitForVideoDecodeDone();
1081 TEST_F(MediaSourcePlayerTest, StartVideoCodecWithInvalidSurface) {
1082 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1084 // Test video codec will not be created when surface is invalid.
1085 scoped_refptr<gfx::SurfaceTexture> surface_texture(
1086 gfx::SurfaceTexture::Create(0));
1087 gfx::ScopedJavaSurface surface(surface_texture.get());
1088 StartVideoDecoderJob();
1090 // Release the surface texture.
1091 surface_texture = NULL;
1092 player_.SetVideoSurface(surface.Pass());
1094 // Player should not seek the demuxer on setting initial surface.
1095 EXPECT_EQ(0, demuxer_->num_seek_requests());
1096 EXPECT_EQ(1, demuxer_->num_data_requests());
1098 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1099 EXPECT_FALSE(GetMediaCodecBridge(false));
1102 TEST_F(MediaSourcePlayerTest, ReadFromDemuxerAfterSeek) {
1103 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1105 // Test decoder job will resend a ReadFromDemuxer request after seek.
1106 StartAudioDecoderJob();
1107 SeekPlayerWithAbort(true, base::TimeDelta());
1110 TEST_F(MediaSourcePlayerTest, SetSurfaceWhileSeeking) {
1111 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1113 // Test SetVideoSurface() will not cause an extra seek while the player is
1114 // waiting for demuxer to indicate seek is done.
1115 player_.OnDemuxerConfigsAvailable(
1116 CreateVideoDemuxerConfigs(false));
1118 // Initiate a seek. Skip requesting element seek of renderer.
1119 // Instead behave as if the renderer has asked us to seek.
1120 player_.SeekTo(base::TimeDelta());
1121 EXPECT_EQ(1, demuxer_->num_seek_requests());
1123 CreateNextTextureAndSetVideoSurface();
1124 EXPECT_EQ(1, demuxer_->num_seek_requests());
1125 player_.Start();
1127 // Send the seek done notification. The player should start requesting data.
1128 player_.OnDemuxerSeekDone(kNoTimestamp());
1129 EXPECT_FALSE(GetMediaCodecBridge(false));
1130 EXPECT_EQ(1, demuxer_->num_data_requests());
1131 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1132 EXPECT_TRUE(GetMediaCodecBridge(false));
1134 // Reconfirm exactly 1 seek request has been made of demuxer, and that it
1135 // was not a browser seek request.
1136 EXPECT_EQ(1, demuxer_->num_seek_requests());
1137 EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
1138 WaitForVideoDecodeDone();
1141 TEST_F(MediaSourcePlayerTest, ChangeMultipleSurfaceWhileDecoding) {
1142 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1144 // Test MediaSourcePlayer can switch multiple surfaces during decoding.
1145 CreateNextTextureAndSetVideoSurface();
1146 StartVideoDecoderJob();
1147 EXPECT_EQ(0, demuxer_->num_seek_requests());
1149 // Send the first input chunk.
1150 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1152 // While the decoder is decoding, change multiple surfaces. Pass an empty
1153 // surface first.
1154 gfx::ScopedJavaSurface empty_surface;
1155 player_.SetVideoSurface(empty_surface.Pass());
1156 // Next, pass a new non-empty surface.
1157 CreateNextTextureAndSetVideoSurface();
1159 // Wait for the media codec bridge to finish decoding and be reset pending a
1160 // browser seek.
1161 WaitForVideoDecodeDone();
1162 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1164 // Only one browser seek should have been initiated. No further data request
1165 // should have been processed on |message_loop_| before surface change event
1166 // became pending, above.
1167 EXPECT_EQ(1, demuxer_->num_browser_seek_requests());
1168 EXPECT_EQ(2, demuxer_->num_data_requests());
1170 // Simulate browser seek is done and confirm player requests more data for new
1171 // video codec.
1172 player_.OnDemuxerSeekDone(player_.GetCurrentTime());
1173 EXPECT_FALSE(GetMediaCodecBridge(false));
1174 EXPECT_EQ(3, demuxer_->num_data_requests());
1175 EXPECT_EQ(1, demuxer_->num_seek_requests());
1177 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1178 EXPECT_TRUE(GetMediaCodecBridge(false));
1179 WaitForVideoDecodeDone();
1182 TEST_F(MediaSourcePlayerTest, SetEmptySurfaceAndStarveWhileDecoding) {
1183 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1185 // Test player pauses if an empty surface is passed.
1186 CreateNextTextureAndSetVideoSurface();
1187 StartVideoDecoderJob();
1188 EXPECT_EQ(1, demuxer_->num_data_requests());
1190 // Send the first input chunk.
1191 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1193 // While the decoder is decoding, pass an empty surface.
1194 gfx::ScopedJavaSurface empty_surface;
1195 player_.SetVideoSurface(empty_surface.Pass());
1196 // Let the player starve. However, it should not issue any new data request in
1197 // this case.
1198 TriggerPlayerStarvation();
1199 // Wait for the media codec bridge to finish decoding and be reset.
1200 while (GetMediaDecoderJob(false)->is_decoding())
1201 message_loop_.RunUntilIdle();
1203 // No further seek or data requests should have been received since the
1204 // surface is empty.
1205 EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
1206 EXPECT_EQ(2, demuxer_->num_data_requests());
1207 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1209 // Playback resumes once a non-empty surface is passed.
1210 CreateNextTextureAndSetVideoSurface();
1211 EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
1212 while(demuxer_->num_browser_seek_requests() != 1)
1213 message_loop_.RunUntilIdle();
1214 WaitForVideoDecodeDone();
1217 TEST_F(MediaSourcePlayerTest, ReleaseVideoDecoderResourcesWhileDecoding) {
1218 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1220 // Test that if video decoder is released while decoding, the resources will
1221 // not be immediately released.
1222 CreateNextTextureAndSetVideoSurface();
1223 StartVideoDecoderJob();
1224 // No resource is requested since there is no data to decode.
1225 EXPECT_EQ(0, manager_.num_resources_requested());
1226 ReleasePlayer();
1227 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1229 // Recreate the video decoder.
1230 CreateNextTextureAndSetVideoSurface();
1231 player_.Start();
1232 while (!GetMediaDecoderJob(false)->is_decoding())
1233 message_loop_.RunUntilIdle();
1234 EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
1235 EXPECT_EQ(1, manager_.num_resources_requested());
1236 ReleasePlayer();
1237 // Wait for the media codec bridge to finish decoding and be reset.
1238 while (GetMediaDecoderJob(false)->is_decoding())
1239 message_loop_.RunUntilIdle();
1242 TEST_F(MediaSourcePlayerTest, AudioOnlyStartAfterSeekFinish) {
1243 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1245 // Test audio decoder job will not start until pending seek event is handled.
1246 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, false);
1247 player_.OnDemuxerConfigsAvailable(configs);
1249 // Initiate a seek. Skip requesting element seek of renderer.
1250 // Instead behave as if the renderer has asked us to seek.
1251 player_.SeekTo(base::TimeDelta());
1252 EXPECT_EQ(1, demuxer_->num_seek_requests());
1254 player_.Start();
1255 EXPECT_EQ(0, demuxer_->num_data_requests());
1257 // Sending back the seek done notification.
1258 player_.OnDemuxerSeekDone(kNoTimestamp());
1259 EXPECT_FALSE(GetMediaCodecBridge(true));
1260 EXPECT_EQ(1, demuxer_->num_data_requests());
1262 // Reconfirm exactly 1 seek request has been made of demuxer.
1263 EXPECT_EQ(1, demuxer_->num_seek_requests());
1265 // Decoder is created after data is received.
1266 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1267 EXPECT_TRUE(GetMediaCodecBridge(true));
1270 TEST_F(MediaSourcePlayerTest, VideoOnlyStartAfterSeekFinish) {
1271 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1273 // Test video decoder job will not start until pending seek event is handled.
1274 CreateNextTextureAndSetVideoSurface();
1275 DemuxerConfigs configs = CreateVideoDemuxerConfigs(false);
1276 player_.OnDemuxerConfigsAvailable(configs);
1278 // Initiate a seek. Skip requesting element seek of renderer.
1279 // Instead behave as if the renderer has asked us to seek.
1280 player_.SeekTo(base::TimeDelta());
1281 EXPECT_EQ(1, demuxer_->num_seek_requests());
1283 player_.Start();
1284 EXPECT_EQ(0, demuxer_->num_data_requests());
1286 // Sending back the seek done notification.
1287 player_.OnDemuxerSeekDone(kNoTimestamp());
1288 EXPECT_FALSE(GetMediaCodecBridge(false));
1289 EXPECT_EQ(1, demuxer_->num_data_requests());
1291 // Reconfirm exactly 1 seek request has been made of demuxer.
1292 EXPECT_EQ(1, demuxer_->num_seek_requests());
1294 // Decoder is created after data is received.
1295 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1296 EXPECT_TRUE(GetMediaCodecBridge(false));
1297 WaitForVideoDecodeDone();
1300 TEST_F(MediaSourcePlayerTest, StartImmediatelyAfterPause) {
1301 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1303 // Test that if the decoding job is not fully stopped after Pause(),
1304 // calling Start() will be a noop.
1305 StartAudioDecoderJob();
1307 MediaDecoderJob* decoder_job = GetMediaDecoderJob(true);
1308 EXPECT_FALSE(GetMediaDecoderJob(true)->is_decoding());
1310 // Sending data to player.
1311 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1312 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1313 EXPECT_EQ(2, demuxer_->num_data_requests());
1315 // Decoder job will not immediately stop after Pause() since it is
1316 // running on another thread.
1317 player_.Pause(true);
1318 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1320 // Nothing happens when calling Start() again.
1321 player_.Start();
1322 // Verify that Start() will not destroy and recreate the media codec bridge.
1323 EXPECT_EQ(decoder_job, GetMediaDecoderJob(true));
1325 while (GetMediaDecoderJob(true)->is_decoding())
1326 message_loop_.RunUntilIdle();
1327 // The decoder job should finish and wait for data.
1328 EXPECT_EQ(2, demuxer_->num_data_requests());
1329 EXPECT_TRUE(IsRequestingDemuxerData(true));
1332 TEST_F(MediaSourcePlayerTest, DecoderJobsCannotStartWithoutAudio) {
1333 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1335 // Test that when Start() is called, video decoder job will wait for audio
1336 // decoder job before start decoding the data.
1337 CreateNextTextureAndSetVideoSurface();
1338 Start(CreateAudioVideoDemuxerConfigs());
1339 MediaDecoderJob* audio_decoder_job = GetMediaDecoderJob(true);
1340 MediaDecoderJob* video_decoder_job = GetMediaDecoderJob(false);
1342 EXPECT_FALSE(audio_decoder_job->is_decoding());
1343 EXPECT_FALSE(video_decoder_job->is_decoding());
1345 // Sending video data to player, video decoder should not start.
1346 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1347 EXPECT_FALSE(video_decoder_job->is_decoding());
1349 // Sending audio data to player, both decoders should start now.
1350 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1351 EXPECT_TRUE(audio_decoder_job->is_decoding());
1352 EXPECT_TRUE(video_decoder_job->is_decoding());
1354 // No seeks should have occurred.
1355 EXPECT_EQ(0, demuxer_->num_seek_requests());
1356 WaitForVideoDecodeDone();
1359 TEST_F(MediaSourcePlayerTest, StartTimeTicksResetAfterDecoderUnderruns) {
1360 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1362 // Test start time ticks will reset after decoder job underruns.
1363 StartAudioDecoderJob();
1365 DecodeAudioDataUntilOutputBecomesAvailable();
1367 // The decoder job should finish prerolling and start prefetching.
1368 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(3));
1369 base::TimeTicks previous = StartTimeTicks();
1371 // Let the decoder starve.
1372 TriggerPlayerStarvation();
1373 WaitForAudioDecodeDone();
1374 EXPECT_TRUE(StartTimeTicks() == previous);
1376 // Send new data to the decoder so it can finish prefetching. This should
1377 // reset the start time ticks.
1378 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(3));
1379 EXPECT_TRUE(StartTimeTicks() != previous);
1381 base::TimeTicks current = StartTimeTicks();
1382 EXPECT_LE(0, (current - previous).InMillisecondsF());
1385 TEST_F(MediaSourcePlayerTest, V_SecondAccessUnitIsEOSAndResumePlayAfterSeek) {
1386 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1388 // Test MediaSourcePlayer can replay video after input EOS is reached.
1389 CreateNextTextureAndSetVideoSurface();
1390 StartVideoDecoderJob();
1392 // Send the first input chunk.
1393 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1394 WaitForVideoDecodeDone();
1396 VerifyPlaybackCompletesOnEOSDecode(true, false);
1397 VerifyCompletedPlaybackResumesOnSeekPlusStart(false, true);
1400 TEST_F(MediaSourcePlayerTest, A_FirstAccessUnitIsEOSAndResumePlayAfterSeek) {
1401 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1403 // Test decode of audio EOS buffer without any prior decode. See also
1404 // http://b/11696552.
1405 // Also tests that seeking+Start() after completing audio playback resumes
1406 // playback.
1407 Start(CreateAudioDemuxerConfigs(kCodecAAC, false));
1408 VerifyPlaybackCompletesOnEOSDecode(true, true);
1409 VerifyCompletedPlaybackResumesOnSeekPlusStart(true, false);
1412 TEST_F(MediaSourcePlayerTest, V_FirstAccessUnitAfterSeekIsEOS) {
1413 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1415 // Test decode of video EOS buffer, just after seeking, without any prior
1416 // decode (other than the simulated |kAborted| resulting from the seek
1417 // process.)
1418 CreateNextTextureAndSetVideoSurface();
1419 StartVideoDecoderJob();
1420 SeekPlayerWithAbort(false, base::TimeDelta());
1421 VerifyPlaybackCompletesOnEOSDecode(true, false);
1424 TEST_F(MediaSourcePlayerTest, A_FirstAccessUnitAfterSeekIsEOS) {
1425 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1427 // Test decode of audio EOS buffer, just after seeking, without any prior
1428 // decode (other than the simulated |kAborted| resulting from the seek
1429 // process.) See also http://b/11696552.
1430 Start(CreateAudioDemuxerConfigs(kCodecAAC, false));
1431 SeekPlayerWithAbort(true, base::TimeDelta());
1432 VerifyPlaybackCompletesOnEOSDecode(true, true);
1435 TEST_F(MediaSourcePlayerTest, AV_PlaybackCompletionAcrossConfigChange) {
1436 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1438 // Test that if one stream (audio) has completed decode of EOS and the other
1439 // stream (video) processes config change, that subsequent video EOS completes
1440 // A/V playback.
1441 // Also tests that seeking+Start() after completing playback resumes playback.
1442 CreateNextTextureAndSetVideoSurface();
1443 Start(CreateAudioVideoDemuxerConfigs());
1445 player_.OnDemuxerDataAvailable(CreateEOSAck(true)); // Audio EOS
1446 DemuxerConfigs configs = CreateVideoDemuxerConfigs(true);
1447 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckWithConfigChanged(
1448 false, 0, configs)); // Video |kConfigChanged| as first unit.
1450 WaitForAudioVideoDecodeDone();
1452 EXPECT_EQ(3, demuxer_->num_data_requests());
1454 // At no time after completing audio EOS decode, above, should the
1455 // audio decoder job resume decoding. Send and decode video EOS.
1456 VerifyPlaybackCompletesOnEOSDecode(true, false);
1457 VerifyCompletedPlaybackResumesOnSeekPlusStart(true, true);
1460 TEST_F(MediaSourcePlayerTest, VA_PlaybackCompletionAcrossConfigChange) {
1461 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1463 // Test that if one stream (video) has completed decode of EOS and the other
1464 // stream (audio) processes config change, that subsequent audio EOS completes
1465 // A/V playback.
1466 // Also tests that seeking+Start() after completing playback resumes playback.
1467 CreateNextTextureAndSetVideoSurface();
1468 Start(CreateAudioVideoDemuxerConfigs());
1470 player_.OnDemuxerDataAvailable(CreateEOSAck(false)); // Video EOS
1471 // Audio |kConfigChanged| as first unit.
1472 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckWithConfigChanged(
1473 true, 0, CreateAudioDemuxerConfigs(kCodecVorbis, false)));
1475 WaitForAudioVideoDecodeDone();
1477 EXPECT_EQ(3, demuxer_->num_data_requests());
1479 // At no time after completing video EOS decode, above, should the
1480 // video decoder job resume decoding. Send and decode audio EOS.
1481 VerifyPlaybackCompletesOnEOSDecode(true, true);
1482 VerifyCompletedPlaybackResumesOnSeekPlusStart(true, true);
1485 TEST_F(MediaSourcePlayerTest, AV_NoPrefetchForFinishedVideoOnAudioStarvation) {
1486 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1488 // Test that if one stream (video) has completed decode of EOS, prefetch
1489 // resulting from player starvation occurs only for the other stream (audio),
1490 // and responding to that prefetch with EOS completes A/V playback, even if
1491 // another starvation occurs during the latter EOS's decode.
1492 CreateNextTextureAndSetVideoSurface();
1493 Start(CreateAudioVideoDemuxerConfigs());
1495 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1496 player_.OnDemuxerDataAvailable(CreateEOSAck(false)); // Video EOS
1498 // Wait until video EOS is processed and more data (assumed to be audio) is
1499 // requested.
1500 WaitForAudioVideoDecodeDone();
1501 EXPECT_EQ(3, demuxer_->num_data_requests());
1503 // Simulate decoder underrun to trigger prefetch while still decoding audio.
1504 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(1));
1505 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding() &&
1506 !GetMediaDecoderJob(false)->is_decoding());
1507 TriggerPlayerStarvation();
1509 // Complete the audio decode that was in progress when simulated player
1510 // starvation was triggered.
1511 WaitForAudioDecodeDone();
1512 EXPECT_EQ(4, demuxer_->num_data_requests());
1513 player_.OnDemuxerDataAvailable(CreateEOSAck(true)); // Audio EOS
1514 EXPECT_FALSE(GetMediaDecoderJob(false)->is_decoding());
1515 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1517 // Simulate another decoder underrun to trigger prefetch while decoding EOS.
1518 TriggerPlayerStarvation();
1519 VerifyPlaybackCompletesOnEOSDecode(false, true /* ignored */);
1522 TEST_F(MediaSourcePlayerTest, V_StarvationDuringEOSDecode) {
1523 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1525 // Test that video-only playback completes without further data requested when
1526 // starvation occurs during EOS decode.
1527 CreateNextTextureAndSetVideoSurface();
1528 StartVideoDecoderJob();
1529 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1530 WaitForVideoDecodeDone();
1532 // Simulate decoder underrun to trigger prefetch while decoding EOS.
1533 player_.OnDemuxerDataAvailable(CreateEOSAck(false)); // Video EOS
1534 EXPECT_TRUE(GetMediaDecoderJob(false)->is_decoding());
1535 TriggerPlayerStarvation();
1536 VerifyPlaybackCompletesOnEOSDecode(false, false /* ignored */);
1539 TEST_F(MediaSourcePlayerTest, A_StarvationDuringEOSDecode) {
1540 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1542 // Test that audio-only playback completes without further data requested when
1543 // starvation occurs during EOS decode.
1544 StartAudioDecoderJob();
1545 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1546 WaitForAudioDecodeDone();
1548 // Simulate decoder underrun to trigger prefetch while decoding EOS.
1549 player_.OnDemuxerDataAvailable(CreateEOSAck(true)); // Audio EOS
1550 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1551 TriggerPlayerStarvation();
1552 VerifyPlaybackCompletesOnEOSDecode(false, true /* ignored */);
1555 TEST_F(MediaSourcePlayerTest, AV_SeekDuringEOSDecodePreventsCompletion) {
1556 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1558 // Test that seek supercedes audio+video playback completion on simultaneous
1559 // audio and video EOS decode, if SeekTo() occurs during these EOS decodes.
1560 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, true, true, true);
1563 TEST_F(MediaSourcePlayerTest, AV_SeekDuringAudioEOSDecodePreventsCompletion) {
1564 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1566 // Test that seek supercedes audio+video playback completion on simultaneous
1567 // audio EOS and video non-EOS decode, if SeekTo() occurs during these
1568 // decodes.
1569 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, true, true, false);
1572 TEST_F(MediaSourcePlayerTest, AV_SeekDuringVideoEOSDecodePreventsCompletion) {
1573 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1575 // Test that seek supercedes audio+video playback completion on simultaneous
1576 // audio non-EOS and video EOS decode, if SeekTo() occurs during these
1577 // decodes.
1578 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, true, false, true);
1581 TEST_F(MediaSourcePlayerTest, V_SeekDuringEOSDecodePreventsCompletion) {
1582 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1584 // Test that seek supercedes video-only playback completion on EOS decode, if
1585 // SeekTo() occurs during EOS decode.
1586 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(false, true, false, true);
1589 TEST_F(MediaSourcePlayerTest, A_SeekDuringEOSDecodePreventsCompletion) {
1590 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1592 // Test that seek supercedes audio-only playback completion on EOS decode, if
1593 // SeekTo() occurs during EOS decode.
1594 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, false, true, false);
1597 TEST_F(MediaSourcePlayerTest, NoRequestForDataAfterAbort) {
1598 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1600 // Test that the decoder will not request new data after receiving an aborted
1601 // access unit.
1602 StartAudioDecoderJob();
1604 // Send an aborted access unit.
1605 player_.OnDemuxerDataAvailable(CreateAbortedAck(true));
1606 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1607 WaitForAudioDecodeDone();
1609 // No request will be sent for new data.
1610 EXPECT_EQ(1, demuxer_->num_data_requests());
1612 // No seek requests should have occurred.
1613 EXPECT_EQ(0, demuxer_->num_seek_requests());
1616 TEST_F(MediaSourcePlayerTest, DemuxerDataArrivesAfterRelease) {
1617 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1619 // Test that the decoder should not crash if demuxer data arrives after
1620 // Release().
1621 StartAudioDecoderJob();
1623 ReleasePlayer();
1624 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1626 // The media codec bridge should have been released.
1627 EXPECT_FALSE(player_.IsPlaying());
1629 // No further data should have been requested.
1630 EXPECT_EQ(1, demuxer_->num_data_requests());
1632 // No seek requests should have occurred.
1633 EXPECT_EQ(0, demuxer_->num_seek_requests());
1636 TEST_F(MediaSourcePlayerTest, BrowserSeek_RegularSeekPendsBrowserSeekDone) {
1637 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1639 // Test that a browser seek, once started, delays a newly arrived regular
1640 // SeekTo() request's demuxer seek until the browser seek is done.
1641 BrowserSeekPlayer(false);
1643 // Simulate renderer requesting a regular seek while browser seek in progress.
1644 player_.SeekTo(base::TimeDelta());
1646 // Simulate browser seek is done. Confirm player requests the regular seek,
1647 // still has no video codec configured, and has not requested any
1648 // further data since the surface change event became pending in
1649 // BrowserSeekPlayer().
1650 EXPECT_EQ(1, demuxer_->num_seek_requests());
1651 player_.OnDemuxerSeekDone(base::TimeDelta());
1652 EXPECT_EQ(2, demuxer_->num_seek_requests());
1653 EXPECT_EQ(1, demuxer_->num_browser_seek_requests());
1655 // Simulate regular seek is done and confirm player requests more data for
1656 // new video codec.
1657 player_.OnDemuxerSeekDone(kNoTimestamp());
1658 EXPECT_FALSE(GetMediaCodecBridge(false));
1659 EXPECT_EQ(3, demuxer_->num_data_requests());
1660 EXPECT_EQ(2, demuxer_->num_seek_requests());
1661 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1662 EXPECT_TRUE(GetMediaCodecBridge(false));
1663 WaitForVideoDecodeDone();
1666 TEST_F(MediaSourcePlayerTest, BrowserSeek_InitialReleaseAndStart) {
1667 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1669 // Test that no browser seek is requested if player Release() + Start() occurs
1670 // prior to receiving any data.
1671 CreateNextTextureAndSetVideoSurface();
1672 StartVideoDecoderJob();
1673 ReleasePlayer();
1675 // Pass a new non-empty surface.
1676 CreateNextTextureAndSetVideoSurface();
1678 player_.Start();
1680 // No data request is issued since there is still one pending.
1681 EXPECT_EQ(1, demuxer_->num_data_requests());
1682 EXPECT_FALSE(GetMediaCodecBridge(false));
1684 // No browser seek is needed.
1685 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1686 EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
1687 EXPECT_EQ(2, demuxer_->num_data_requests());
1688 WaitForVideoDecodeDone();
1691 TEST_F(MediaSourcePlayerTest, BrowserSeek_MidStreamReleaseAndStart) {
1692 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1694 // Test that one browser seek is requested if player Release() + Start(), with
1695 // video data received between Release() and Start().
1696 BrowserSeekPlayer(true);
1698 // Simulate browser seek is done and confirm player requests more data.
1699 player_.OnDemuxerSeekDone(base::TimeDelta());
1700 EXPECT_EQ(3, demuxer_->num_data_requests());
1701 EXPECT_EQ(1, demuxer_->num_seek_requests());
1704 TEST_F(MediaSourcePlayerTest, NoBrowserSeekWithKeyFrameInCache) {
1705 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1707 // Test that browser seek is not needed if a key frame is found in data
1708 // cache.
1709 CreateNextTextureAndSetVideoSurface();
1710 StartVideoDecoderJob();
1711 DemuxerData data = CreateReadFromDemuxerAckForVideo(false);
1712 data.access_units[0].is_key_frame = true;
1714 // Simulate demuxer's response to the video data request.
1715 player_.OnDemuxerDataAvailable(data);
1717 // Trigger decoder recreation later by changing surfaces.
1718 CreateNextTextureAndSetVideoSurface();
1720 // Wait for the media codec bridge to finish decoding and be reset.
1721 WaitForVideoDecodeDone();
1722 EXPECT_FALSE(HasData(false));
1724 // Send a non key frame to decoder so that decoder can continue. This will
1725 // not trigger any browser seeks as the previous key frame is still in the
1726 // buffer.
1727 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1728 WaitForVideoDecodeDone();
1729 EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
1732 TEST_F(MediaSourcePlayerTest, PrerollAudioAfterSeek) {
1733 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1735 // Test decoder job will preroll the media to the seek position.
1736 StartAudioDecoderJob();
1738 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100));
1739 EXPECT_TRUE(IsPrerolling(true));
1740 PrerollDecoderToTime(
1741 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), true);
1744 TEST_F(MediaSourcePlayerTest, PrerollVideoAfterSeek) {
1745 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1747 // Test decoder job will preroll the media to the seek position.
1748 CreateNextTextureAndSetVideoSurface();
1749 StartVideoDecoderJob();
1751 SeekPlayerWithAbort(false, base::TimeDelta::FromMilliseconds(100));
1752 EXPECT_TRUE(IsPrerolling(false));
1753 PrerollDecoderToTime(
1754 false, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), true);
1757 TEST_F(MediaSourcePlayerTest, SeekingAfterCompletingPrerollRestartsPreroll) {
1758 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1760 // Test decoder job will begin prerolling upon seek, when it was not
1761 // prerolling prior to the seek.
1762 StartAudioDecoderJob();
1763 MediaDecoderJob* decoder_job = GetMediaDecoderJob(true);
1764 EXPECT_TRUE(IsPrerolling(true));
1766 // Complete the initial preroll by feeding data to the decoder.
1767 DecodeAudioDataUntilOutputBecomesAvailable();
1768 EXPECT_FALSE(IsPrerolling(true));
1770 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(500));
1772 // Prerolling should have begun again.
1773 EXPECT_TRUE(IsPrerolling(true));
1774 EXPECT_EQ(500.0, GetPrerollTimestamp().InMillisecondsF());
1776 // Send data at and after the seek position. Prerolling should complete.
1777 for (int i = 0; i < 4; ++i) {
1778 DemuxerData data = CreateReadFromDemuxerAckForAudio(i);
1779 data.access_units[0].timestamp = base::TimeDelta::FromMilliseconds(
1780 500 + 30 * (i - 1));
1781 player_.OnDemuxerDataAvailable(data);
1782 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1783 WaitForAudioDecodeDone();
1785 EXPECT_LT(500.0, player_.GetCurrentTime().InMillisecondsF());
1786 EXPECT_FALSE(IsPrerolling(true));
1788 // Throughout this test, we should have not re-created the media codec bridge,
1789 // so IsPrerolling() transition from false to true was not due to constructor
1790 // initialization. It was due to BeginPrerolling().
1791 EXPECT_EQ(decoder_job, GetMediaDecoderJob(true));
1794 TEST_F(MediaSourcePlayerTest, PrerollContinuesAcrossReleaseAndStart) {
1795 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1797 // Test decoder job will resume media prerolling if interrupted by Release()
1798 // and Start().
1799 StartAudioDecoderJob();
1801 base::TimeDelta target_timestamp = base::TimeDelta::FromMilliseconds(100);
1802 SeekPlayerWithAbort(true, target_timestamp);
1803 EXPECT_TRUE(IsPrerolling(true));
1804 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
1806 // Send some data before the seek position.
1807 // Test uses 'large' number of iterations because decoder job may not get
1808 // MEDIA_CODEC_OK output status until after a few dequeue output attempts.
1809 // This allows decoder status to stabilize prior to AU timestamp reaching
1810 // the preroll target.
1811 DemuxerData data;
1812 for (int i = 0; i < 10; ++i) {
1813 data = CreateReadFromDemuxerAckForAudio(3);
1814 data.access_units[0].timestamp = base::TimeDelta::FromMilliseconds(i * 10);
1815 if (i == 1) {
1816 // While still prerolling, Release() and Start() the player.
1817 ReleasePlayer();
1818 // The decoder is still decoding and will not be immediately released.
1819 EXPECT_TRUE(GetMediaCodecBridge(true));
1820 Resume(false, false);
1821 } else {
1822 player_.OnDemuxerDataAvailable(data);
1823 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1824 WaitForAudioDecodeDone();
1826 EXPECT_TRUE(IsPrerolling(true));
1828 EXPECT_EQ(100.0, player_.GetCurrentTime().InMillisecondsF());
1829 EXPECT_TRUE(IsPrerolling(true));
1831 // Send data after the seek position.
1832 PrerollDecoderToTime(true, target_timestamp, target_timestamp, true);
1835 // Flaky on Android: crbug.com/419122.
1836 #if defined(OS_ANDROID)
1837 #define MAYBE_PrerollContinuesAcrossConfigChange \
1838 DISABLED_PrerollContinuesAcrossConfigChange
1839 #else
1840 #define MAYBE_PrerollContinuesAcrossConfigChange \
1841 PrerollContinuesAcrossConfigChange
1842 #endif
1843 TEST_F(MediaSourcePlayerTest, MAYBE_PrerollContinuesAcrossConfigChange) {
1844 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1846 // Test decoder job will resume media prerolling if interrupted by
1847 // |kConfigChanged| and OnDemuxerConfigsAvailable().
1848 StartAudioDecoderJob();
1850 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100));
1851 EXPECT_TRUE(IsPrerolling(true));
1852 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
1854 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, true);
1856 // In response to data request, simulate that demuxer signals config change by
1857 // sending an AU with |kConfigChanged|.
1858 DemuxerData data = CreateReadFromDemuxerAckWithConfigChanged(
1859 true, 0, configs);
1860 player_.OnDemuxerDataAvailable(data);
1862 PrerollDecoderToTime(
1863 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), true);
1866 TEST_F(MediaSourcePlayerTest, PrerollContinuesAfterUnchangedConfigs) {
1867 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1869 // Test decoder job will resume media prerolling if interrupted by a config
1870 // change access unit with unchanged configs.
1871 StartAudioDecoderJob();
1873 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100));
1874 EXPECT_TRUE(IsPrerolling(true));
1875 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
1877 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, false);
1879 // In response to data request, simulate that demuxer signals config change by
1880 // sending an AU with |kConfigChanged|.
1881 DemuxerData data = CreateReadFromDemuxerAckWithConfigChanged(
1882 true, 0, configs);
1883 player_.OnDemuxerDataAvailable(data);
1884 PrerollDecoderToTime(
1885 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), true);
1888 TEST_F(MediaSourcePlayerTest, AudioPrerollFinishesBeforeVideo) {
1889 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1891 // Test that after audio finishes prerolling, it will wait for video to finish
1892 // prerolling before advancing together.
1893 CreateNextTextureAndSetVideoSurface();
1894 Start(CreateAudioVideoDemuxerConfigs());
1896 // Initiate a seek.
1897 base::TimeDelta seek_position = base::TimeDelta::FromMilliseconds(100);
1898 player_.SeekTo(seek_position);
1899 player_.OnDemuxerDataAvailable(CreateAbortedAck(true));
1900 player_.OnDemuxerDataAvailable(CreateAbortedAck(false));
1901 WaitForDecodeDone(true, true);
1903 // Verify that the seek is requested.
1904 EXPECT_EQ(1, demuxer_->num_seek_requests());
1905 player_.OnDemuxerSeekDone(kNoTimestamp());
1906 EXPECT_EQ(4, demuxer_->num_data_requests());
1907 EXPECT_EQ(player_.GetCurrentTime().InMillisecondsF(), 100.0);
1908 EXPECT_EQ(GetPrerollTimestamp().InMillisecondsF(), 100.0);
1910 // Send both audio and video data to finish prefetching.
1911 base::TimeDelta seek_ack_position = base::TimeDelta::FromMilliseconds(70);
1912 DemuxerData audio_data = CreateReadFromDemuxerAckForAudio(0);
1913 audio_data.access_units[0].timestamp = seek_ack_position;
1914 DemuxerData video_data = CreateReadFromDemuxerAckForVideo(false);
1915 video_data.access_units[0].timestamp = seek_ack_position;
1916 player_.OnDemuxerDataAvailable(audio_data);
1917 player_.OnDemuxerDataAvailable(video_data);
1918 WaitForAudioDecodeDone();
1919 WaitForVideoDecodeDone();
1921 // Send audio data at and after the seek position. Audio should finish
1922 // prerolling and stop decoding.
1923 EXPECT_EQ(6, demuxer_->num_data_requests());
1924 PrerollDecoderToTime(true, seek_position, seek_position, true);
1925 EXPECT_FALSE(GetMediaDecoderJob(true)->is_decoding());
1926 EXPECT_FALSE(IsPrerolling(true));
1927 EXPECT_TRUE(IsPrerolling(false));
1929 // Send video data to let video finish prerolling.
1930 PrerollDecoderToTime(false, seek_position, seek_position, false);
1931 EXPECT_FALSE(IsPrerolling(false));
1933 // Both audio and video decoders should start decoding again.
1934 player_.OnDemuxerDataAvailable(audio_data);
1935 player_.OnDemuxerDataAvailable(video_data);
1936 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1937 EXPECT_TRUE(GetMediaDecoderJob(false)->is_decoding());
1940 TEST_F(MediaSourcePlayerTest, SimultaneousAudioVideoConfigChange) {
1941 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1943 // Test that the player allows simultaneous audio and video config change,
1944 // such as might occur during OnPrefetchDone() if next access unit for both
1945 // audio and video jobs is |kConfigChanged|.
1946 CreateNextTextureAndSetVideoSurface();
1947 Start(CreateAudioVideoDemuxerConfigs());
1948 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1949 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1950 EXPECT_TRUE(GetMediaCodecBridge(true));
1951 EXPECT_TRUE(GetMediaCodecBridge(false));
1952 EnableAdaptiveVideoPlayback(false);
1953 WaitForAudioVideoDecodeDone();
1955 // If audio or video hasn't finished prerolling, let them finish it.
1956 if (IsPrerolling(true))
1957 PrerollDecoderToTime(true, base::TimeDelta(), base::TimeDelta(), true);
1958 if (IsPrerolling(false))
1959 PrerollDecoderToTime(false, base::TimeDelta(), base::TimeDelta(), false);
1960 int expected_num_data_requests = demuxer_->num_data_requests();
1962 // Simulate audio |kConfigChanged| prefetched as standalone access unit.
1963 DemuxerConfigs audio_configs = CreateAudioDemuxerConfigs(kCodecVorbis, true);
1964 player_.OnDemuxerDataAvailable(
1965 CreateReadFromDemuxerAckWithConfigChanged(true, 0, audio_configs));
1967 // Simulate video |kConfigChanged| prefetched as standalone access unit.
1968 player_.OnDemuxerDataAvailable(
1969 CreateReadFromDemuxerAckWithConfigChanged(
1970 false, 0, CreateVideoDemuxerConfigs(true)));
1971 EXPECT_EQ(expected_num_data_requests + 2, demuxer_->num_data_requests());
1972 EXPECT_TRUE(IsDrainingDecoder(true));
1973 EXPECT_TRUE(IsDrainingDecoder(false));
1975 // Waiting for decoder to finish draining.
1976 while (IsDrainingDecoder(true) || IsDrainingDecoder(false))
1977 message_loop_.RunUntilIdle();
1980 TEST_F(MediaSourcePlayerTest,
1981 SimultaneousAudioVideoConfigChangeWithAdaptivePlayback) {
1982 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1984 // Test that the player allows simultaneous audio and video config change with
1985 // adaptive video playback enabled.
1986 CreateNextTextureAndSetVideoSurface();
1987 Start(CreateAudioVideoDemuxerConfigs());
1988 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1989 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1990 EXPECT_EQ(4, demuxer_->num_data_requests());
1991 EXPECT_TRUE(GetMediaCodecBridge(true));
1992 EXPECT_TRUE(GetMediaCodecBridge(false));
1993 EnableAdaptiveVideoPlayback(true);
1994 WaitForAudioVideoDecodeDone();
1996 // If audio or video hasn't finished prerolling, let them finish it.
1997 if (IsPrerolling(true))
1998 PrerollDecoderToTime(true, base::TimeDelta(), base::TimeDelta(), true);
1999 if (IsPrerolling(false))
2000 PrerollDecoderToTime(false, base::TimeDelta(), base::TimeDelta(), false);
2001 int expected_num_data_requests = demuxer_->num_data_requests();
2003 // Simulate audio |kConfigChanged| prefetched as standalone access unit.
2004 DemuxerConfigs audio_configs = CreateAudioDemuxerConfigs(kCodecVorbis, true);
2005 player_.OnDemuxerDataAvailable(
2006 CreateReadFromDemuxerAckWithConfigChanged(true, 0, audio_configs));
2008 // Simulate video |kConfigChanged| prefetched as standalone access unit.
2009 player_.OnDemuxerDataAvailable(
2010 CreateReadFromDemuxerAckWithConfigChanged(
2011 false, 0, CreateVideoDemuxerConfigs(true)));
2012 EXPECT_EQ(expected_num_data_requests + 2, demuxer_->num_data_requests());
2013 EXPECT_TRUE(IsDrainingDecoder(true));
2014 EXPECT_FALSE(IsDrainingDecoder(false));
2016 // Waiting for audio decoder to finish draining.
2017 while (IsDrainingDecoder(true))
2018 message_loop_.RunUntilIdle();
2021 TEST_F(MediaSourcePlayerTest, DemuxerConfigRequestedIfInPrefetchUnit0) {
2022 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2024 // Test that the player detects need for and requests demuxer configs if
2025 // the |kConfigChanged| unit is the very first unit in the set of units
2026 // received in OnDemuxerDataAvailable() ostensibly while
2027 // |PREFETCH_DONE_EVENT_PENDING|.
2028 StartConfigChange(true, true, 0, false);
2029 WaitForAudioDecodeDone();
2032 TEST_F(MediaSourcePlayerTest, DemuxerConfigRequestedIfInPrefetchUnit1) {
2033 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2035 // Test that the player detects need for and requests demuxer configs if
2036 // the |kConfigChanged| unit is not the first unit in the set of units
2037 // received in OnDemuxerDataAvailable() ostensibly while
2038 // |PREFETCH_DONE_EVENT_PENDING|.
2039 StartConfigChange(true, true, 1, false);
2040 WaitForAudioDecodeDone();
2043 TEST_F(MediaSourcePlayerTest, DemuxerConfigRequestedIfInUnit0AfterPrefetch) {
2044 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2046 // Test that the player detects need for and requests demuxer configs if
2047 // the |kConfigChanged| unit is the very first unit in the set of units
2048 // received in OnDemuxerDataAvailable() from data requested ostensibly while
2049 // not prefetching.
2050 StartConfigChange(true, false, 0, false);
2051 WaitForAudioDecodeDone();
2054 TEST_F(MediaSourcePlayerTest, DemuxerConfigRequestedIfInUnit1AfterPrefetch) {
2055 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2057 // Test that the player detects need for and requests demuxer configs if
2058 // the |kConfigChanged| unit is not the first unit in the set of units
2059 // received in OnDemuxerDataAvailable() from data requested ostensibly while
2060 // not prefetching.
2061 StartConfigChange(true, false, 1, false);
2062 WaitForAudioDecodeDone();
2065 TEST_F(MediaSourcePlayerTest, BrowserSeek_PrerollAfterBrowserSeek) {
2066 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2068 // Test decoder job will preroll the media to the actual seek position
2069 // resulting from a browser seek.
2070 BrowserSeekPlayer(false);
2072 // Simulate browser seek is done, but to a later time than was requested.
2073 EXPECT_LT(player_.GetCurrentTime().InMillisecondsF(), 100);
2074 player_.OnDemuxerSeekDone(base::TimeDelta::FromMilliseconds(100));
2075 // Because next AU is not I-frame, MediaCodecBridge will not be recreated.
2076 EXPECT_FALSE(GetMediaCodecBridge(false));
2077 EXPECT_EQ(100.0, player_.GetCurrentTime().InMillisecondsF());
2078 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2079 EXPECT_EQ(3, demuxer_->num_data_requests());
2081 PrerollDecoderToTime(
2082 false, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), true);
2085 TEST_F(MediaSourcePlayerTest, VideoDemuxerConfigChange) {
2086 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2088 // Test that video config change notification results in creating a new
2089 // video codec without any browser seek.
2090 StartConfigChange(false, true, 1, false);
2092 // New video codec should have been created and configured, without any
2093 // browser seek.
2094 EXPECT_TRUE(GetMediaCodecBridge(false));
2095 EXPECT_EQ(3, demuxer_->num_data_requests());
2096 EXPECT_EQ(0, demuxer_->num_seek_requests());
2098 // 2 codecs should have been created, one before the config change, and one
2099 // after it.
2100 EXPECT_EQ(2, manager_.num_resources_requested());
2101 WaitForVideoDecodeDone();
2104 TEST_F(MediaSourcePlayerTest, VideoDemuxerConfigChangeWithAdaptivePlayback) {
2105 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2107 // Test that if codec supports adaptive playback, no new codec should be
2108 // created beyond the one used to decode the prefetch media data prior to
2109 // the kConfigChanged.
2110 StartConfigChange(false, true, 1, true);
2112 // No browser seek should be needed.
2113 EXPECT_TRUE(GetMediaCodecBridge(false));
2114 EXPECT_EQ(3, demuxer_->num_data_requests());
2115 EXPECT_EQ(0, demuxer_->num_seek_requests());
2117 // Only 1 codec should have been created so far.
2118 EXPECT_EQ(1, manager_.num_resources_requested());
2119 WaitForVideoDecodeDone();
2122 TEST_F(MediaSourcePlayerTest, DecoderDrainInterruptedBySeek) {
2123 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2125 // Test if a decoder is being drained while receiving a seek request, draining
2126 // is canceled.
2127 SendConfigChangeToDecoder(true, false, 0, false);
2128 EXPECT_TRUE(IsDrainingDecoder(true));
2130 player_.SeekTo(base::TimeDelta::FromMilliseconds(100));
2131 WaitForAudioDecodeDone();
2132 EXPECT_FALSE(IsDrainingDecoder(true));
2133 player_.OnDemuxerSeekDone(kNoTimestamp());
2135 EXPECT_EQ(1, demuxer_->num_seek_requests());
2136 EXPECT_EQ(4, demuxer_->num_data_requests());
2139 TEST_F(MediaSourcePlayerTest, DecoderDrainInterruptedByRelease) {
2140 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2142 // Test if a decoder is being drained while receiving a release request,
2143 // draining is canceled.
2144 SendConfigChangeToDecoder(true, false, 0, false);
2145 EXPECT_TRUE(IsDrainingDecoder(true));
2147 ReleasePlayer();
2148 WaitForAudioDecodeDone();
2149 EXPECT_EQ(3, demuxer_->num_data_requests());
2150 EXPECT_FALSE(IsDrainingDecoder(true));
2152 EXPECT_FALSE(GetMediaCodecBridge(true));
2153 EXPECT_FALSE(player_.IsPlaying());
2155 player_.Start();
2156 EXPECT_TRUE(player_.IsPlaying());
2157 EXPECT_EQ(3, demuxer_->num_data_requests());
2160 TEST_F(MediaSourcePlayerTest, DecoderDrainInterruptedBySurfaceChange) {
2161 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2163 // Test if a video decoder is being drained while surface changes, draining
2164 // is canceled.
2165 SendConfigChangeToDecoder(false, false, 0, false);
2166 EXPECT_TRUE(IsDrainingDecoder(false));
2168 CreateNextTextureAndSetVideoSurface();
2169 WaitForVideoDecodeDone();
2171 EXPECT_FALSE(IsDrainingDecoder(false));
2172 EXPECT_TRUE(player_.IsPlaying());
2174 // The frame after the config change should always be an iframe, so no browser
2175 // seek is needed when recreating the video decoder due to surface change.
2176 EXPECT_TRUE(GetMediaCodecBridge(false));
2177 EXPECT_EQ(4, demuxer_->num_data_requests());
2178 EXPECT_EQ(0, demuxer_->num_seek_requests());
2181 TEST_F(MediaSourcePlayerTest,
2182 BrowserSeek_DecoderStarvationWhilePendingSurfaceChange) {
2183 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2185 // Test video decoder starvation while handling a pending surface change
2186 // should not cause any crashes.
2187 CreateNextTextureAndSetVideoSurface();
2188 StartVideoDecoderJob();
2189 DemuxerData data = CreateReadFromDemuxerAckForVideo(false);
2190 player_.OnDemuxerDataAvailable(data);
2192 // Trigger a surface change and decoder starvation.
2193 CreateNextTextureAndSetVideoSurface();
2194 TriggerPlayerStarvation();
2195 WaitForVideoDecodeDone();
2196 EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
2198 // Surface change should trigger a seek.
2199 player_.OnDemuxerDataAvailable(data);
2200 EXPECT_EQ(1, demuxer_->num_browser_seek_requests());
2201 player_.OnDemuxerSeekDone(base::TimeDelta());
2202 // After seek is done, prefetch is handled first. MediaCodecBridge is not
2203 // created at this moment.
2204 EXPECT_FALSE(GetMediaCodecBridge(false));
2206 // A new data request should be sent.
2207 EXPECT_EQ(3, demuxer_->num_data_requests());
2210 TEST_F(MediaSourcePlayerTest, ReleaseWithOnPrefetchDoneAlreadyPosted) {
2211 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2213 // Test if OnPrefetchDone() had already been posted before and is executed
2214 // after Release(), then player does not DCHECK. This test is fragile to
2215 // change to MediaDecoderJob::Prefetch() implementation; it assumes task
2216 // is posted to run |prefetch_cb| if the job already HasData().
2217 // TODO(wolenetz): Remove MSP::set_decode_callback_for_testing() if this test
2218 // becomes obsolete. See http://crbug.com/304234.
2219 StartAudioDecoderJob();
2221 // Escape the original prefetch by decoding a single access unit.
2222 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
2223 WaitForAudioDecodeDone();
2225 // Prime the job with a few more access units, so that a later prefetch,
2226 // triggered by starvation to simulate decoder underrun, can trivially
2227 // post task to run OnPrefetchDone().
2228 player_.OnDemuxerDataAvailable(
2229 CreateReadFromDemuxerAckWithConfigChanged(
2230 true, 4, CreateAudioDemuxerConfigs(kCodecVorbis, false)));
2231 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
2233 // Simulate decoder underrun, so trivial prefetch starts while still decoding.
2234 // The prefetch and posting of OnPrefetchDone() will not occur until next
2235 // MediaDecoderCallBack() occurs.
2236 TriggerPlayerStarvation();
2238 // Upon the next successful decode callback, post a task to call Release() on
2239 // the |player_|, such that the trivial OnPrefetchDone() task posting also
2240 // occurs and should execute after the Release().
2241 OnNextTestDecodeCallbackPostTaskToReleasePlayer();
2243 WaitForAudioDecodeDone();
2244 EXPECT_TRUE(decoder_callback_hook_executed_);
2246 EXPECT_EQ(3, demuxer_->num_data_requests());
2248 // Player should not request any new data since the access units haven't
2249 // been fully decoded yet.
2250 Resume(false, false);
2253 TEST_F(MediaSourcePlayerTest, SeekToThenReleaseThenDemuxerSeekAndDone) {
2254 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2256 // Test if Release() occurs after SeekTo(), but the DemuxerSeek IPC request
2257 // has not yet been sent, then the seek request is sent after Release(). Also,
2258 // test if OnDemuxerSeekDone() occurs prior to next Start(), then the player
2259 // will resume correct post-seek preroll upon Start().
2260 StartAudioDecoderJobAndSeekToWhileDecoding(
2261 base::TimeDelta::FromMilliseconds(100));
2262 ReleasePlayer();
2263 EXPECT_EQ(0, demuxer_->num_seek_requests());
2264 WaitForAudioDecodeDone();
2265 EXPECT_EQ(1, demuxer_->num_seek_requests());
2267 player_.OnDemuxerSeekDone(kNoTimestamp());
2268 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2269 EXPECT_FALSE(GetMediaCodecBridge(true));
2270 EXPECT_FALSE(player_.IsPlaying());
2272 // Player should begin prefetch and resume preroll upon Start().
2273 EXPECT_EQ(2, demuxer_->num_data_requests());
2274 Resume(true, false);
2275 EXPECT_TRUE(IsPrerolling(true));
2276 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2278 // No further seek should have been requested since Release(), above.
2279 EXPECT_EQ(1, demuxer_->num_seek_requests());
2282 TEST_F(MediaSourcePlayerTest, SeekToThenReleaseThenDemuxerSeekThenStart) {
2283 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2285 // Test if Release() occurs after SeekTo(), but the DemuxerSeek IPC request
2286 // has not yet been sent, then the seek request is sent after Release(). Also,
2287 // test if OnDemuxerSeekDone() does not occur until after the next Start(),
2288 // then the player remains pending seek done until (and resumes correct
2289 // post-seek preroll after) OnDemuxerSeekDone().
2290 StartAudioDecoderJobAndSeekToWhileDecoding(
2291 base::TimeDelta::FromMilliseconds(100));
2292 ReleasePlayer();
2293 EXPECT_EQ(0, demuxer_->num_seek_requests());
2295 // Player should not prefetch upon Start() nor create the media codec bridge,
2296 // due to awaiting DemuxerSeekDone.
2297 EXPECT_EQ(2, demuxer_->num_data_requests());
2298 Resume(false, false);
2300 WaitForAudioDecodeDone();
2301 EXPECT_EQ(1, demuxer_->num_seek_requests());
2302 player_.OnDemuxerSeekDone(kNoTimestamp());
2303 EXPECT_TRUE(GetMediaDecoderJob(true));
2304 EXPECT_TRUE(IsPrerolling(true));
2305 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2306 EXPECT_EQ(3, demuxer_->num_data_requests());
2308 // No further seek should have been requested since Release(), above.
2309 EXPECT_EQ(1, demuxer_->num_seek_requests());
2312 TEST_F(MediaSourcePlayerTest, SeekToThenDemuxerSeekThenReleaseThenSeekDone) {
2313 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2315 // Test if Release() occurs after a SeekTo()'s subsequent DemuxerSeek IPC
2316 // request and OnDemuxerSeekDone() arrives prior to the next Start(), then the
2317 // player will resume correct post-seek preroll upon Start().
2318 StartAudioDecoderJobAndSeekToWhileDecoding(
2319 base::TimeDelta::FromMilliseconds(100));
2320 WaitForAudioDecodeDone();
2321 EXPECT_EQ(1, demuxer_->num_seek_requests());
2323 ReleasePlayer();
2324 player_.OnDemuxerSeekDone(kNoTimestamp());
2325 EXPECT_FALSE(player_.IsPlaying());
2326 EXPECT_FALSE(GetMediaCodecBridge(true));
2327 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2329 // Player should begin prefetch and resume preroll upon Start().
2330 EXPECT_EQ(2, demuxer_->num_data_requests());
2331 Resume(true, false);
2332 EXPECT_TRUE(IsPrerolling(true));
2333 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2335 // No further seek should have been requested since before Release(), above.
2336 EXPECT_EQ(1, demuxer_->num_seek_requests());
2339 TEST_F(MediaSourcePlayerTest, SeekToThenReleaseThenStart) {
2340 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2342 // Test if Release() occurs after a SeekTo()'s subsequent DemuxerSeeK IPC
2343 // request and OnDemuxerSeekDone() does not occur until after the next
2344 // Start(), then the player remains pending seek done until (and resumes
2345 // correct post-seek preroll after) OnDemuxerSeekDone().
2346 StartAudioDecoderJobAndSeekToWhileDecoding(
2347 base::TimeDelta::FromMilliseconds(100));
2348 WaitForAudioDecodeDone();
2349 EXPECT_EQ(1, demuxer_->num_seek_requests());
2351 ReleasePlayer();
2352 EXPECT_EQ(2, demuxer_->num_data_requests());
2353 Resume(false, false);
2355 player_.OnDemuxerSeekDone(kNoTimestamp());
2356 EXPECT_FALSE(GetMediaCodecBridge(true));
2357 EXPECT_TRUE(IsPrerolling(true));
2358 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2359 EXPECT_EQ(3, demuxer_->num_data_requests());
2361 // No further seek should have been requested since before Release(), above.
2362 EXPECT_EQ(1, demuxer_->num_seek_requests());
2365 TEST_F(MediaSourcePlayerTest, ConfigChangedThenReleaseThenStart) {
2366 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2368 // Test if Release() occurs after |kConfigChanged| is processed, new data
2369 // requested of demuxer, and the requested data arrive before the next
2370 // Start(), then the player starts to decode the new data without any seek.
2371 StartConfigChange(true, true, 0, false);
2372 ReleasePlayer();
2374 EXPECT_TRUE(GetMediaCodecBridge(true));
2375 EXPECT_FALSE(player_.IsPlaying());
2376 EXPECT_EQ(3, demuxer_->num_data_requests());
2377 player_.OnDemuxerDataAvailable(
2378 CreateReadFromDemuxerAckWithConfigChanged(
2379 true, 4, CreateAudioDemuxerConfigs(kCodecVorbis, false)));
2380 WaitForAudioDecodeDone();
2381 EXPECT_FALSE(GetMediaCodecBridge(true));
2383 // Player should resume upon Start(), even without further configs supplied.
2384 player_.Start();
2385 EXPECT_TRUE(player_.IsPlaying());
2386 EXPECT_EQ(3, demuxer_->num_data_requests());
2387 EXPECT_EQ(0, demuxer_->num_seek_requests());
2388 WaitForAudioDecodeDone();
2391 TEST_F(MediaSourcePlayerTest, BrowserSeek_ThenReleaseThenDemuxerSeekDone) {
2392 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2394 // Test that Release() after a browser seek's DemuxerSeek IPC request has been
2395 // sent behaves similar to a regular seek: if OnDemuxerSeekDone() occurs
2396 // before the next Start()+SetVideoSurface(), then the player will resume
2397 // correct post-seek preroll upon Start()+SetVideoSurface().
2398 BrowserSeekPlayer(false);
2399 base::TimeDelta expected_preroll_timestamp = player_.GetCurrentTime();
2400 ReleasePlayer();
2402 player_.OnDemuxerSeekDone(expected_preroll_timestamp);
2403 EXPECT_FALSE(player_.IsPlaying());
2404 EXPECT_FALSE(GetMediaCodecBridge(false));
2405 EXPECT_EQ(expected_preroll_timestamp, GetPrerollTimestamp());
2407 // Player should begin prefetch and resume preroll upon Start().
2408 EXPECT_EQ(2, demuxer_->num_data_requests());
2409 CreateNextTextureAndSetVideoSurface();
2410 Resume(false, true);
2411 EXPECT_TRUE(IsPrerolling(false));
2412 EXPECT_EQ(expected_preroll_timestamp, GetPrerollTimestamp());
2413 EXPECT_EQ(expected_preroll_timestamp, player_.GetCurrentTime());
2415 // No further seek should have been requested since BrowserSeekPlayer().
2416 EXPECT_EQ(1, demuxer_->num_seek_requests());
2419 TEST_F(MediaSourcePlayerTest, BrowserSeek_ThenReleaseThenStart) {
2420 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2422 // Test that Release() after a browser seek's DemuxerSeek IPC request has been
2423 // sent behaves similar to a regular seek: if OnDemuxerSeekDone() does not
2424 // occur until after the next Start()+SetVideoSurface(), then the player
2425 // remains pending seek done until (and resumes correct post-seek preroll
2426 // after) OnDemuxerSeekDone().
2427 BrowserSeekPlayer(false);
2428 base::TimeDelta expected_preroll_timestamp = player_.GetCurrentTime();
2429 ReleasePlayer();
2431 EXPECT_EQ(2, demuxer_->num_data_requests());
2432 CreateNextTextureAndSetVideoSurface();
2433 Resume(false, false);
2435 player_.OnDemuxerSeekDone(expected_preroll_timestamp);
2436 // Prefetch takes place first, and the decoder is not created yet.
2437 EXPECT_FALSE(GetMediaCodecBridge(false));
2438 EXPECT_TRUE(IsPrerolling(false));
2439 EXPECT_EQ(expected_preroll_timestamp, GetPrerollTimestamp());
2440 EXPECT_EQ(expected_preroll_timestamp, player_.GetCurrentTime());
2441 EXPECT_EQ(3, demuxer_->num_data_requests());
2443 // No further seek should have been requested since BrowserSeekPlayer().
2444 EXPECT_EQ(1, demuxer_->num_seek_requests());
2446 // Decoder will be created once data is received.
2447 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
2448 EXPECT_TRUE(GetMediaCodecBridge(false));
2449 WaitForVideoDecodeDone();
2452 // TODO(xhwang): Once we add tests to cover DrmBridge, update this test to
2453 // also verify that the job is successfully created if SetDrmBridge(), Start()
2454 // and eventually OnMediaCrypto() occur. This would increase test coverage of
2455 // http://crbug.com/313470 and allow us to remove inspection of internal player
2456 // pending event state. See http://crbug.com/313860.
2457 TEST_F(MediaSourcePlayerTest, SurfaceChangeClearedEvenIfMediaCryptoAbsent) {
2458 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2460 // Test that |SURFACE_CHANGE_EVENT_PENDING| is not pending after
2461 // SetVideoSurface() for a player configured for encrypted video, when the
2462 // player has not yet received media crypto.
2463 DemuxerConfigs configs = CreateVideoDemuxerConfigs(false);
2464 configs.is_video_encrypted = true;
2466 player_.OnDemuxerConfigsAvailable(configs);
2467 CreateNextTextureAndSetVideoSurface();
2468 EXPECT_FALSE(GetMediaCodecBridge(false));
2471 TEST_F(MediaSourcePlayerTest, CurrentTimeUpdatedWhileDecoderStarved) {
2472 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2474 // Test that current time is updated while decoder is starved.
2475 StartAudioDecoderJob();
2476 DecodeAudioDataUntilOutputBecomesAvailable();
2478 // Trigger starvation while the decoder is decoding.
2479 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(3));
2480 manager_.ResetTimestampUpdated();
2481 TriggerPlayerStarvation();
2482 WaitForAudioDecodeDone();
2484 // Current time should be updated.
2485 EXPECT_TRUE(manager_.timestamp_updated());
2488 TEST_F(MediaSourcePlayerTest, CurrentTimeKeepsIncreasingAfterConfigChange) {
2489 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2491 // Test current time keep on increasing after audio config change.
2492 // Test that current time is updated while decoder is starved.
2493 StartAudioDecoderJob();
2495 DecodeAudioDataUntilOutputBecomesAvailable();
2497 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, true);
2498 DemuxerData data = CreateReadFromDemuxerAckWithConfigChanged(
2499 true, 0, configs);
2500 player_.OnDemuxerDataAvailable(data);
2501 WaitForAudioDecodeDone();
2502 DecodeAudioDataUntilOutputBecomesAvailable();
2505 TEST_F(MediaSourcePlayerTest, VideoMetadataChangeAfterConfigChange) {
2506 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2508 // Test that after a config change, metadata change will be happen
2509 // after decoder is drained.
2510 StartConfigChange(false, true, 2, false);
2511 EXPECT_EQ(1, manager_.num_metadata_changes());
2512 EXPECT_FALSE(IsDrainingDecoder(false));
2514 // Create video data with new resolutions.
2515 DemuxerData data = CreateReadFromDemuxerAckForVideo(true);
2517 // Wait for the metadata change.
2518 while(manager_.num_metadata_changes() == 1) {
2519 player_.OnDemuxerDataAvailable(data);
2520 WaitForVideoDecodeDone();
2522 EXPECT_EQ(2, manager_.num_metadata_changes());
2523 WaitForVideoDecodeDone();
2526 TEST_F(MediaSourcePlayerTest, RequestPlayDeniedDontPlay_Audio) {
2527 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2529 EXPECT_EQ(demuxer_->num_data_requests(), 0);
2530 player_.OnDemuxerConfigsAvailable(CreateDemuxerConfigs(true, false));
2532 manager_.set_allow_play(false);
2533 player_.Start();
2534 EXPECT_FALSE(player_.IsPlaying());
2537 TEST_F(MediaSourcePlayerTest, RequestPlayDeniedDontPlay_Video) {
2538 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2540 EXPECT_EQ(demuxer_->num_data_requests(), 0);
2541 player_.OnDemuxerConfigsAvailable(CreateDemuxerConfigs(false, true));
2543 manager_.set_allow_play(false);
2544 player_.Start();
2545 EXPECT_FALSE(player_.IsPlaying());
2548 TEST_F(MediaSourcePlayerTest, RequestPlayDeniedDontPlay_AV) {
2549 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2551 EXPECT_EQ(demuxer_->num_data_requests(), 0);
2552 player_.OnDemuxerConfigsAvailable(CreateDemuxerConfigs(true, true));
2554 manager_.set_allow_play(false);
2555 player_.Start();
2556 EXPECT_FALSE(player_.IsPlaying());
2559 TEST_F(MediaSourcePlayerTest, RequestPlayGrantedPlays) {
2560 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2562 EXPECT_EQ(demuxer_->num_data_requests(), 0);
2563 player_.OnDemuxerConfigsAvailable(CreateDemuxerConfigs(true, true));
2565 manager_.set_allow_play(true);
2566 player_.Start();
2567 EXPECT_TRUE(player_.IsPlaying());
2570 } // namespace media