Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / media / base / android / media_source_player_unittest.cc
blobf37cf205a54b09e413551d88c7552dc5ad27d040
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 ~MockMediaPlayerManager() override {}
54 // MediaPlayerManager implementation.
55 MediaResourceGetter* GetMediaResourceGetter() override { return NULL; }
56 MediaUrlInterceptor* GetMediaUrlInterceptor() override { return NULL; }
57 void OnTimeUpdate(int player_id,
58 base::TimeDelta current_time,
59 base::TimeTicks current_time_ticks) override {
60 timestamp_updated_ = true;
62 void OnMediaMetadataChanged(int player_id,
63 base::TimeDelta duration,
64 int width,
65 int height,
66 bool success) override {
67 num_metadata_changes_++;
69 void OnPlaybackComplete(int player_id) override {
70 playback_completed_ = true;
71 if (message_loop_->is_running())
72 message_loop_->Quit();
74 void OnMediaInterrupted(int player_id) override {}
75 void OnBufferingUpdate(int player_id, int percentage) override {}
76 void OnSeekComplete(int player_id,
77 const base::TimeDelta& current_time) override {}
78 void OnError(int player_id, int error) override {}
79 void OnVideoSizeChanged(int player_id, int width, int height) override {}
80 void OnWaitingForDecryptionKey(int player_id) override {}
81 MediaPlayerAndroid* GetFullscreenPlayer() override { return NULL; }
82 MediaPlayerAndroid* GetPlayer(int player_id) override { return NULL; }
83 void RequestFullScreen(int player_id) override {}
85 void OnAudibleStateChanged(int player_id, bool is_audible_now) override {
86 is_audible_ = is_audible_now;
89 bool playback_completed() const {
90 return playback_completed_;
93 int num_resources_requested() const {
94 return num_resources_requested_;
97 int num_metadata_changes() const {
98 return num_metadata_changes_;
101 void OnMediaResourcesRequested(int player_id) {
102 num_resources_requested_++;
105 bool timestamp_updated() const {
106 return timestamp_updated_;
109 void ResetTimestampUpdated() {
110 timestamp_updated_ = false;
113 bool is_audible() const {
114 return is_audible_;
117 bool is_delay_expired() const {
118 return is_delay_expired_;
121 void SetDelayExpired(bool value) {
122 is_delay_expired_ = value;
125 private:
126 base::MessageLoop* message_loop_;
127 bool playback_completed_;
128 // The number of resource requests this object has seen.
129 int num_resources_requested_;
130 // The number of metadata changes reported by the player.
131 int num_metadata_changes_;
132 // Playback timestamp was updated.
133 bool timestamp_updated_;
134 // Audible state of the pipeline
135 bool is_audible_;
136 // Helper flag to ensure delay for WaitForDelay().
137 bool is_delay_expired_;
139 DISALLOW_COPY_AND_ASSIGN(MockMediaPlayerManager);
142 class MockDemuxerAndroid : public DemuxerAndroid {
143 public:
144 explicit MockDemuxerAndroid(base::MessageLoop* message_loop)
145 : message_loop_(message_loop),
146 num_data_requests_(0),
147 num_seek_requests_(0),
148 num_browser_seek_requests_(0) {}
149 ~MockDemuxerAndroid() override {}
151 void Initialize(DemuxerAndroidClient* client) override {}
152 void RequestDemuxerData(DemuxerStream::Type type) override {
153 num_data_requests_++;
154 if (message_loop_->is_running())
155 message_loop_->Quit();
157 void RequestDemuxerSeek(const base::TimeDelta& time_to_seek,
158 bool is_browser_seek) override {
159 num_seek_requests_++;
160 if (is_browser_seek)
161 num_browser_seek_requests_++;
164 int num_data_requests() const { return num_data_requests_; }
165 int num_seek_requests() const { return num_seek_requests_; }
166 int num_browser_seek_requests() const { return num_browser_seek_requests_; }
168 private:
169 base::MessageLoop* message_loop_;
171 // The number of encoded data requests this object has seen.
172 int num_data_requests_;
174 // The number of regular and browser seek requests this object has seen.
175 int num_seek_requests_;
177 // The number of browser seek requests this object has seen.
178 int num_browser_seek_requests_;
180 DISALLOW_COPY_AND_ASSIGN(MockDemuxerAndroid);
183 class MediaSourcePlayerTest : public testing::Test {
184 public:
185 MediaSourcePlayerTest()
186 : manager_(&message_loop_),
187 demuxer_(new MockDemuxerAndroid(&message_loop_)),
188 player_(0, &manager_,
189 base::Bind(&MockMediaPlayerManager::OnMediaResourcesRequested,
190 base::Unretained(&manager_)),
191 scoped_ptr<DemuxerAndroid>(demuxer_),
192 GURL()),
193 decoder_callback_hook_executed_(false),
194 surface_texture_a_is_next_(true) {}
196 ~MediaSourcePlayerTest() override {}
198 protected:
199 // Get the decoder job from the MediaSourcePlayer. The return value must not
200 // be NULL.
201 MediaDecoderJob* GetMediaDecoderJob(bool is_audio) {
202 if (is_audio) {
203 return reinterpret_cast<MediaDecoderJob*>(
204 player_.audio_decoder_job_.get());
206 return reinterpret_cast<MediaDecoderJob*>(
207 player_.video_decoder_job_.get());
210 // Get the MediaCodecBridge from the decoder job. The return value could be
211 // NULL if the decoder is not yet created.
212 MediaCodecBridge* GetMediaCodecBridge(bool is_audio) {
213 if (is_audio)
214 return player_.audio_decoder_job_->media_codec_bridge_.get();
215 return player_.video_decoder_job_->media_codec_bridge_.get();
218 // Get the per-job prerolling status from the MediaSourcePlayer's job matching
219 // |is_audio|. Caller must guard against NPE if the player's job is NULL.
220 bool IsPrerolling(bool is_audio) {
221 return GetMediaDecoderJob(is_audio)->prerolling_;
224 // Get the preroll timestamp from the MediaSourcePlayer.
225 base::TimeDelta GetPrerollTimestamp() {
226 return player_.preroll_timestamp_;
229 // Simulate player has reached starvation timeout.
230 void TriggerPlayerStarvation() {
231 player_.decoder_starvation_callback_.Cancel();
232 player_.OnDecoderStarved();
235 // Release() the player.
236 void ReleasePlayer() {
237 EXPECT_TRUE(player_.IsPlaying());
238 player_.Release();
239 EXPECT_FALSE(player_.IsPlaying());
242 // Upon the next successful decode callback, post a task to call Release()
243 // on the |player_|. TEST_F's do not have access to the private player
244 // members, hence this helper method.
245 // Prevent usage creep of MSP::set_decode_callback_for_testing() by
246 // only using it for the ReleaseWithOnPrefetchDoneAlreadyPosted test.
247 void OnNextTestDecodeCallbackPostTaskToReleasePlayer() {
248 DCHECK_EQ(&message_loop_, base::MessageLoop::current());
249 player_.set_decode_callback_for_testing(media::BindToCurrentLoop(
250 base::Bind(
251 &MediaSourcePlayerTest::ReleaseWithPendingPrefetchDoneVerification,
252 base::Unretained(this))));
255 // Asynch test callback posted upon decode completion to verify that a pending
256 // prefetch done event is not cleared across |player_|'s Release(). This helps
257 // ensure the ReleaseWithOnPrefetchDoneAlreadyPosted test scenario is met.
258 void ReleaseWithPendingPrefetchDoneVerification() {
259 EXPECT_TRUE(player_.IsEventPending(player_.PREFETCH_DONE_EVENT_PENDING));
260 ReleasePlayer();
261 EXPECT_TRUE(player_.IsEventPending(player_.PREFETCH_DONE_EVENT_PENDING));
262 EXPECT_FALSE(decoder_callback_hook_executed_);
263 EXPECT_FALSE(GetMediaCodecBridge(true));
264 decoder_callback_hook_executed_ = true;
267 DemuxerConfigs CreateAudioDemuxerConfigs(AudioCodec audio_codec,
268 bool use_low_sample_rate) {
269 DemuxerConfigs configs;
270 configs.audio_codec = audio_codec;
271 configs.audio_channels = 2;
272 configs.is_audio_encrypted = false;
273 configs.duration = kDefaultDuration;
275 if (audio_codec == kCodecVorbis) {
276 configs.audio_sampling_rate = use_low_sample_rate ? 11025 : 44100;
277 scoped_refptr<DecoderBuffer> buffer = ReadTestDataFile(
278 "vorbis-extradata");
279 configs.audio_extra_data = std::vector<uint8>(
280 buffer->data(),
281 buffer->data() + buffer->data_size());
282 return configs;
285 // Other codecs are not yet supported by this helper.
286 EXPECT_EQ(audio_codec, kCodecAAC);
288 configs.audio_sampling_rate = 48000;
289 uint8 aac_extra_data[] = { 0x13, 0x10 };
290 configs.audio_extra_data = std::vector<uint8>(
291 aac_extra_data,
292 aac_extra_data + 2);
293 return configs;
296 DemuxerConfigs CreateVideoDemuxerConfigs(bool use_larger_size) {
297 DemuxerConfigs configs;
298 configs.video_codec = kCodecVP8;
299 configs.video_size =
300 use_larger_size ? gfx::Size(640, 240) : gfx::Size(320, 240);
301 configs.is_video_encrypted = false;
302 configs.duration = kDefaultDuration;
303 return configs;
306 DemuxerConfigs CreateAudioVideoDemuxerConfigs() {
307 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, false);
308 configs.video_codec = kCodecVP8;
309 configs.video_size = gfx::Size(320, 240);
310 configs.is_video_encrypted = false;
311 return configs;
314 DemuxerConfigs CreateDemuxerConfigs(bool have_audio, bool have_video) {
315 DCHECK(have_audio || have_video);
317 if (have_audio && !have_video)
318 return CreateAudioDemuxerConfigs(kCodecVorbis, false);
320 if (have_video && !have_audio)
321 return CreateVideoDemuxerConfigs(false);
323 return CreateAudioVideoDemuxerConfigs();
326 // Starts an audio decoder job.
327 void StartAudioDecoderJob() {
328 Start(CreateAudioDemuxerConfigs(kCodecVorbis, false));
331 // Starts a video decoder job.
332 void StartVideoDecoderJob() {
333 Start(CreateVideoDemuxerConfigs(false));
336 // Starts decoding the data.
337 void Start(const DemuxerConfigs& configs) {
338 EXPECT_EQ(demuxer_->num_data_requests(), 0);
339 player_.OnDemuxerConfigsAvailable(configs);
340 player_.Start();
342 EXPECT_TRUE(player_.IsPlaying());
343 int expected_num_requests = (player_.HasAudio() ? 1 : 0) +
344 (player_.HasVideo() ? 1 : 0);
345 EXPECT_EQ(expected_num_requests, demuxer_->num_data_requests());
348 // Resumes decoding the data. Verifies player behavior relative to
349 // |expect_player_requests_audio_data| and
350 // |expect_player_requests_video_data|.
351 void Resume(bool expect_player_requests_audio_data,
352 bool expect_player_requests_video_data) {
353 EXPECT_FALSE(player_.IsPlaying());
354 EXPECT_TRUE(player_.HasVideo() || player_.HasAudio());
355 int original_num_data_requests = demuxer_->num_data_requests();
356 int expected_request_delta =
357 (expect_player_requests_audio_data ? 1 : 0) +
358 (expect_player_requests_video_data ? 1 : 0);
360 player_.Start();
362 EXPECT_TRUE(player_.IsPlaying());
363 EXPECT_EQ(original_num_data_requests + expected_request_delta,
364 demuxer_->num_data_requests());
367 // Keeps decoding audio data until the decoder starts to output samples.
368 // Gives up if no audio output after decoding 10 frames.
369 void DecodeAudioDataUntilOutputBecomesAvailable() {
370 EXPECT_TRUE(player_.IsPlaying());
371 base::TimeDelta current_time = player_.GetCurrentTime();
372 base::TimeDelta start_timestamp = current_time;
373 for (int i = 0; i < 10; ++i) {
374 manager_.ResetTimestampUpdated();
375 player_.OnDemuxerDataAvailable(
376 CreateReadFromDemuxerAckForAudio(i > 3 ? 3 : i));
377 WaitForAudioDecodeDone();
378 base::TimeDelta new_current_time = player_.GetCurrentTime();
379 EXPECT_LE(current_time.InMilliseconds(),
380 new_current_time.InMilliseconds());
381 current_time = new_current_time;
382 if (manager_.timestamp_updated()) {
383 // TODO(qinmin): the current time is from the decoder thread and it does
384 // not take the delay from posting the task into consideration.
385 // http://crbug.com/421616.
386 EXPECT_LE(start_timestamp.InMillisecondsF(),
387 new_current_time.InMillisecondsF());
388 return;
391 EXPECT_TRUE(false);
394 AccessUnit CreateAccessUnitWithData(bool is_audio, int audio_packet_id,
395 bool use_large_size_video) {
396 AccessUnit unit;
398 unit.status = DemuxerStream::kOk;
399 scoped_refptr<DecoderBuffer> buffer;
400 if (is_audio) {
401 buffer = ReadTestDataFile(
402 base::StringPrintf("vorbis-packet-%d", audio_packet_id));
403 } else {
404 buffer = ReadTestDataFile(
405 use_large_size_video ? "vp8-I-frame-640x240" : "vp8-I-frame-320x240");
407 unit.data = std::vector<uint8>(
408 buffer->data(), buffer->data() + buffer->data_size());
410 if (is_audio) {
411 // Vorbis needs 4 extra bytes padding on Android to decode properly. Check
412 // NuMediaExtractor.cpp in Android source code.
413 uint8 padding[4] = { 0xff , 0xff , 0xff , 0xff };
414 unit.data.insert(unit.data.end(), padding, padding + 4);
417 return unit;
420 DemuxerData CreateReadFromDemuxerAckForAudio(int packet_id) {
421 DemuxerData data;
422 data.type = DemuxerStream::AUDIO;
423 data.access_units.resize(1);
424 data.access_units[0] = CreateAccessUnitWithData(true, packet_id, false);
426 return data;
429 DemuxerData CreateReadFromDemuxerAckForVideo(bool use_large_size) {
430 DemuxerData data;
431 data.type = DemuxerStream::VIDEO;
432 data.access_units.resize(1);
433 data.access_units[0] = CreateAccessUnitWithData(false, 0, use_large_size);
434 return data;
437 DemuxerData CreateEOSAck(bool is_audio) {
438 DemuxerData data;
439 data.type = is_audio ? DemuxerStream::AUDIO : DemuxerStream::VIDEO;
440 data.access_units.resize(1);
441 data.access_units[0].status = DemuxerStream::kOk;
442 data.access_units[0].is_end_of_stream = true;
443 return data;
446 DemuxerData CreateAbortedAck(bool is_audio) {
447 DemuxerData data;
448 data.type = is_audio ? DemuxerStream::AUDIO : DemuxerStream::VIDEO;
449 data.access_units.resize(1);
450 data.access_units[0].status = DemuxerStream::kAborted;
451 return data;
454 bool HasData(bool is_audio) {
455 return GetMediaDecoderJob(is_audio)->HasData();
458 // Helper method for use at test start. It starts an audio decoder job and
459 // immediately feeds it some data to decode. Then, without letting the decoder
460 // job complete a decode cycle, it also starts player SeekTo(). Upon return,
461 // the player should not yet have sent the DemuxerSeek IPC request, though
462 // seek event should be pending. The audio decoder job will also still be
463 // decoding.
464 void StartAudioDecoderJobAndSeekToWhileDecoding(
465 const base::TimeDelta& seek_time) {
466 EXPECT_FALSE(GetMediaCodecBridge(true));
467 EXPECT_FALSE(player_.IsPlaying());
468 EXPECT_EQ(0, demuxer_->num_data_requests());
469 EXPECT_EQ(0.0, GetPrerollTimestamp().InMillisecondsF());
470 EXPECT_EQ(player_.GetCurrentTime(), GetPrerollTimestamp());
471 StartAudioDecoderJob();
472 EXPECT_FALSE(GetMediaDecoderJob(true)->is_decoding());
473 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
474 EXPECT_EQ(2, demuxer_->num_data_requests());
475 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
476 player_.SeekTo(seek_time);
477 EXPECT_EQ(0.0, GetPrerollTimestamp().InMillisecondsF());
478 EXPECT_EQ(0, demuxer_->num_seek_requests());
479 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
482 // Seek, including simulated receipt of |kAborted| read between SeekTo() and
483 // OnDemuxerSeekDone(). Use this helper method only when the player already
484 // has created the media codec bridge. Exactly one request for more data is
485 // expected following the seek, so use this helper for players with only audio
486 // or only video.
487 void SeekPlayerWithAbort(bool is_audio, const base::TimeDelta& seek_time) {
488 int original_num_seeks = demuxer_->num_seek_requests();
489 int original_num_data_requests = demuxer_->num_data_requests();
491 // Initiate a seek. Skip the round-trip of requesting seek from renderer.
492 // Instead behave as if the renderer has asked us to seek.
493 player_.SeekTo(seek_time);
495 // Verify that the seek does not occur until previously outstanding data
496 // request is satisfied.
497 EXPECT_EQ(original_num_seeks, demuxer_->num_seek_requests());
499 // Simulate seeking causes the demuxer to abort the outstanding read
500 // caused by the seek.
501 player_.OnDemuxerDataAvailable(CreateAbortedAck(is_audio));
503 // Wait for the decode job to finish so we can process the seek request.
504 WaitForDecodeDone(is_audio, !is_audio);
506 // Verify that the seek is requested.
507 EXPECT_EQ(original_num_seeks + 1, demuxer_->num_seek_requests());
509 // Send back the seek done notification. This should trigger the player to
510 // call OnReadFromDemuxer() again.
511 EXPECT_EQ(original_num_data_requests, demuxer_->num_data_requests());
512 player_.OnDemuxerSeekDone(kNoTimestamp());
513 EXPECT_EQ(original_num_data_requests + 1, demuxer_->num_data_requests());
515 // No other seek should have been requested.
516 EXPECT_EQ(original_num_seeks + 1, demuxer_->num_seek_requests());
519 // Preroll the decoder job to |target_timestamp|. The first access unit
520 // to decode will have a timestamp equal to |start_timestamp|.
521 // |is_clock_manager| indicates whether the decoder serves as the clock
522 // manager for the player.
523 // TODO(qinmin): Add additional test cases for out-of-order decodes.
524 // See http://crbug.com/331421.
525 void PrerollDecoderToTime(bool is_audio,
526 const base::TimeDelta& start_timestamp,
527 const base::TimeDelta& target_timestamp,
528 bool is_clock_manager) {
529 // For streams with both audio and video, it is possible that audio rolls
530 // past the |target_timestamp|. As a result, the current time may be larger
531 // than the |target_timestamp| for video as it may not be the clock manager.
532 EXPECT_TRUE(!is_clock_manager ||
533 target_timestamp == player_.GetCurrentTime());
534 // |start_timestamp| must be smaller than |target_timestamp|.
535 EXPECT_LE(start_timestamp, target_timestamp);
536 DemuxerData data = is_audio ? CreateReadFromDemuxerAckForAudio(1) :
537 CreateReadFromDemuxerAckForVideo(false);
538 int current_timestamp = start_timestamp.InMilliseconds();
540 // Send some data with access unit timestamps before the |target_timestamp|,
541 // and continue sending the data until preroll finishes.
542 // This simulates the common condition that AUs received after browser
543 // seek begin with timestamps before the seek target, and don't
544 // immediately complete preroll.
545 while (IsPrerolling(is_audio)) {
546 data.access_units[0].timestamp =
547 base::TimeDelta::FromMilliseconds(current_timestamp);
548 player_.OnDemuxerDataAvailable(data);
549 EXPECT_TRUE(GetMediaDecoderJob(is_audio)->is_decoding());
550 EXPECT_TRUE(GetMediaCodecBridge(is_audio));
551 EXPECT_TRUE(!is_clock_manager ||
552 target_timestamp == player_.GetCurrentTime());
553 current_timestamp += 30;
554 WaitForDecodeDone(is_audio, !is_audio);
556 EXPECT_LE(target_timestamp, player_.GetCurrentTime());
559 void PlayAudioForTimeInterval(const base::TimeDelta& start_timestamp,
560 const base::TimeDelta& target_timestamp ) {
562 DemuxerData data = CreateReadFromDemuxerAckForAudio(1);
563 int current_timestamp = start_timestamp.InMilliseconds();
564 int stop_timestamp = target_timestamp.InMilliseconds();
565 while (current_timestamp < stop_timestamp) {
566 data.access_units[0].timestamp =
567 base::TimeDelta::FromMilliseconds(current_timestamp);
568 player_.OnDemuxerDataAvailable(data);
569 current_timestamp += 30;
570 WaitForAudioDecodeDone();
574 void WaitForDelay(const base::TimeDelta& delay) {
575 // Let the message_loop_ process events.
576 // We post delayed task and RunUnitilIdle() until it signals.
578 manager_.SetDelayExpired(false);
579 message_loop_.PostDelayedTask(
580 FROM_HERE,
581 base::Bind(&MockMediaPlayerManager::SetDelayExpired,
582 base::Unretained(&manager_),
583 true),
584 delay);
586 while (!manager_.is_delay_expired())
587 message_loop_.RunUntilIdle();
590 DemuxerData CreateReadFromDemuxerAckWithConfigChanged(
591 bool is_audio,
592 int config_unit_index,
593 const DemuxerConfigs& configs) {
594 DemuxerData data;
595 data.type = is_audio ? DemuxerStream::AUDIO : DemuxerStream::VIDEO;
596 data.access_units.resize(config_unit_index + 1);
598 for (int i = 0; i < config_unit_index; ++i)
599 data.access_units[i] = CreateAccessUnitWithData(is_audio, i, false);
601 data.access_units[config_unit_index].status = DemuxerStream::kConfigChanged;
602 data.demuxer_configs.resize(1);
603 data.demuxer_configs[0] = configs;
604 return data;
607 // Valid only for video-only player tests. If |trigger_with_release_start| is
608 // true, triggers the browser seek with a Release() + video data received +
609 // Start() with a new surface. If false, triggers the browser seek by
610 // setting a new video surface after beginning decode of received video data.
611 // Such data receipt causes possibility that an I-frame is not next, and
612 // browser seek results once decode completes and surface change processing
613 // begins.
614 void BrowserSeekPlayer(bool trigger_with_release_start) {
615 int expected_num_data_requests = demuxer_->num_data_requests() + 2;
616 int expected_num_seek_requests = demuxer_->num_seek_requests();
617 int expected_num_browser_seek_requests =
618 demuxer_->num_browser_seek_requests();
620 CreateNextTextureAndSetVideoSurface();
621 StartVideoDecoderJob();
622 if (trigger_with_release_start) {
623 // Consume the first frame, so that the next VideoDecoderJob will not
624 // inherit the I-frame from the previous decoder.
625 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
626 ReleasePlayer();
627 WaitForVideoDecodeDone();
629 // Simulate demuxer's response to the video data request. The data will be
630 // passed to the next MediaCodecBridge.
631 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
632 EXPECT_FALSE(GetMediaCodecBridge(false));
633 EXPECT_FALSE(player_.IsPlaying());
634 EXPECT_EQ(expected_num_seek_requests, demuxer_->num_seek_requests());
636 CreateNextTextureAndSetVideoSurface();
637 Resume(false, false);
638 EXPECT_FALSE(GetMediaCodecBridge(false));
640 // Run the message loop so that prefetch will complete.
641 while (expected_num_seek_requests == demuxer_->num_seek_requests())
642 message_loop_.RunUntilIdle();
643 } else {
644 // Simulate demuxer's response to the video data request.
645 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
647 // While the decoder is decoding, trigger a browser seek by changing
648 // surface. Demuxer does not know of browser seek in advance, so no
649 // |kAborted| data is required (though |kAborted| can certainly occur for
650 // any pending read in reality due to renderer preparing for a regular
651 // seek).
652 CreateNextTextureAndSetVideoSurface();
654 // Browser seek should not begin until decoding has completed.
655 EXPECT_TRUE(GetMediaCodecBridge(false));
656 EXPECT_EQ(expected_num_seek_requests, demuxer_->num_seek_requests());
658 // Wait for the media codec bridge to finish decoding and be reset pending
659 // the browser seek.
660 WaitForVideoDecodeDone();
661 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
664 // Only one browser seek should have been initiated, and no further data
665 // should have been requested.
666 expected_num_seek_requests++;
667 expected_num_browser_seek_requests++;
668 EXPECT_EQ(expected_num_seek_requests, demuxer_->num_seek_requests());
669 EXPECT_EQ(expected_num_browser_seek_requests,
670 demuxer_->num_browser_seek_requests());
671 EXPECT_EQ(expected_num_data_requests, demuxer_->num_data_requests());
674 // Creates a new media codec bridge and feeds it data ending with a
675 // |kConfigChanged| access unit. If |config_unit_in_prefetch| is true, sends
676 // feeds the config change AU in response to the job's first read request
677 // (prefetch). If false, regular data is fed and decoded prior to feeding the
678 // config change AU in response to the second data request (after prefetch
679 // completed). |config_unit_index| controls which access unit is
680 // |kConfigChanged|. If |enable_adaptive_playback| is true, config change will
681 // not cause the decoder to recreate the media codec bridge. Otherwise, the
682 // decoder has to drain all its data before recreating the new codec.
683 void SendConfigChangeToDecoder(bool is_audio,
684 bool config_unit_in_prefetch,
685 int config_unit_index,
686 bool enable_adaptive_playback) {
687 EXPECT_FALSE(GetMediaCodecBridge(is_audio));
688 if (is_audio) {
689 StartAudioDecoderJob();
690 } else {
691 CreateNextTextureAndSetVideoSurface();
692 StartVideoDecoderJob();
695 int expected_num_data_requests = demuxer_->num_data_requests();
696 // Feed and decode a standalone access unit so the player exits prefetch.
697 if (!config_unit_in_prefetch) {
698 if (is_audio) {
699 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
700 } else {
701 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
702 EnableAdaptiveVideoPlayback(enable_adaptive_playback);
705 WaitForDecodeDone(is_audio, !is_audio);
707 // We should have completed the prefetch phase at this point.
708 expected_num_data_requests++;
709 EXPECT_EQ(expected_num_data_requests, demuxer_->num_data_requests());
712 DemuxerConfigs configs = is_audio ?
713 CreateAudioDemuxerConfigs(kCodecVorbis, true) :
714 CreateVideoDemuxerConfigs(true);
715 // Feed and decode access units with data for any units prior to
716 // |config_unit_index|, and a |kConfigChanged| unit at that index.
717 // Player should prepare to reconfigure the decoder job, and should request
718 // new demuxer configs.
719 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckWithConfigChanged(
720 is_audio, config_unit_index, configs));
722 expected_num_data_requests++;
723 EXPECT_EQ(expected_num_data_requests, demuxer_->num_data_requests());
724 if (is_audio)
725 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
726 else
727 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(true));
729 // If the adaptive playback setting was not passed to the MediaCodecBridge
730 // earlier, do it here.
731 if (config_unit_in_prefetch && !is_audio)
732 EnableAdaptiveVideoPlayback(enable_adaptive_playback);
735 // Send a config change to the decoder job and drain the decoder so that the
736 // config change is processed.
737 void StartConfigChange(bool is_audio,
738 bool config_unit_in_prefetch,
739 int config_unit_index,
740 bool enable_adaptive_playback) {
741 SendConfigChangeToDecoder(is_audio, config_unit_in_prefetch,
742 config_unit_index, enable_adaptive_playback);
744 EXPECT_EQ(!config_unit_in_prefetch && !enable_adaptive_playback &&
745 config_unit_index == 0, IsDrainingDecoder(is_audio));
746 int expected_num_data_requests = demuxer_->num_data_requests();
747 // Run until decoder starts to request new data.
748 while (demuxer_->num_data_requests() == expected_num_data_requests)
749 message_loop_.RunUntilIdle();
750 EXPECT_FALSE(IsDrainingDecoder(is_audio));
753 void EnableAdaptiveVideoPlayback(bool enable) {
754 EXPECT_TRUE(GetMediaCodecBridge(false));
755 static_cast<VideoCodecBridge*>(GetMediaCodecBridge(false))->
756 set_adaptive_playback_supported_for_testing(
757 enable ? 1 : 0);
760 void CreateNextTextureAndSetVideoSurface() {
761 gfx::SurfaceTexture* surface_texture;
762 if (surface_texture_a_is_next_) {
763 surface_texture_a_ = gfx::SurfaceTexture::Create(next_texture_id_++);
764 surface_texture = surface_texture_a_.get();
765 } else {
766 surface_texture_b_ = gfx::SurfaceTexture::Create(next_texture_id_++);
767 surface_texture = surface_texture_b_.get();
770 surface_texture_a_is_next_ = !surface_texture_a_is_next_;
771 gfx::ScopedJavaSurface surface = gfx::ScopedJavaSurface(surface_texture);
772 player_.SetVideoSurface(surface.Pass());
775 // Wait for one or both of the jobs to complete decoding. Media codec bridges
776 // are assumed to exist for any stream whose decode completion is awaited.
777 void WaitForDecodeDone(bool wait_for_audio, bool wait_for_video) {
778 DCHECK(wait_for_audio || wait_for_video);
779 while ((wait_for_audio && GetMediaCodecBridge(true) &&
780 GetMediaDecoderJob(true)->HasData() &&
781 GetMediaDecoderJob(true)->is_decoding()) ||
782 (wait_for_video && GetMediaCodecBridge(false) &&
783 GetMediaDecoderJob(false)->HasData() &&
784 GetMediaDecoderJob(false)->is_decoding())) {
785 message_loop_.RunUntilIdle();
789 void WaitForAudioDecodeDone() {
790 WaitForDecodeDone(true, false);
793 void WaitForVideoDecodeDone() {
794 WaitForDecodeDone(false, true);
797 void WaitForAudioVideoDecodeDone() {
798 WaitForDecodeDone(true, true);
801 // If |send_eos| is true, generates EOS for the stream corresponding to
802 // |eos_for_audio|. Verifies that playback completes and no further data
803 // is requested.
804 // If |send_eos| is false, then it is assumed that caller previously arranged
805 // for player to receive EOS for each stream, but the player has not yet
806 // decoded all of them. In this case, |eos_for_audio| is ignored.
807 void VerifyPlaybackCompletesOnEOSDecode(bool send_eos, bool eos_for_audio) {
808 int original_num_data_requests = demuxer_->num_data_requests();
809 if (send_eos)
810 player_.OnDemuxerDataAvailable(CreateEOSAck(eos_for_audio));
811 EXPECT_FALSE(manager_.playback_completed());
812 message_loop_.Run();
813 EXPECT_TRUE(manager_.playback_completed());
814 EXPECT_EQ(original_num_data_requests, demuxer_->num_data_requests());
817 void VerifyCompletedPlaybackResumesOnSeekPlusStart(bool have_audio,
818 bool have_video) {
819 DCHECK(have_audio || have_video);
821 EXPECT_TRUE(manager_.playback_completed());
823 player_.SeekTo(base::TimeDelta());
824 player_.OnDemuxerSeekDone(kNoTimestamp());
825 Resume(have_audio, have_video);
828 // Starts the appropriate decoder jobs according to |have_audio| and
829 // |have_video|. Then starts seek during decode of EOS or non-EOS according to
830 // |eos_audio| and |eos_video|. Simulates seek completion and verifies that
831 // playback never completed. |eos_{audio,video}| is ignored if the
832 // corresponding |have_{audio,video}| is false.
833 void VerifySeekDuringEOSDecodePreventsPlaybackCompletion(bool have_audio,
834 bool have_video,
835 bool eos_audio,
836 bool eos_video) {
837 DCHECK(have_audio || have_video);
839 if (have_video)
840 CreateNextTextureAndSetVideoSurface();
842 Start(CreateDemuxerConfigs(have_audio, have_video));
844 if (have_audio)
845 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
847 if (have_video)
848 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
850 // Run until more data is requested a number of times equal to the number of
851 // media types configured. Since prefetching may be in progress, we cannot
852 // reliably expect Run() to complete until we have sent demuxer data for all
853 // configured media types, above.
854 WaitForDecodeDone(have_audio, have_video);
856 // Simulate seek while decoding EOS or non-EOS for the appropriate
857 // stream(s).
858 if (have_audio) {
859 if (eos_audio)
860 player_.OnDemuxerDataAvailable(CreateEOSAck(true));
861 else
862 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(1));
865 if (have_video) {
866 if (eos_video)
867 player_.OnDemuxerDataAvailable(CreateEOSAck(false));
868 else
869 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
872 player_.SeekTo(base::TimeDelta());
873 EXPECT_EQ(0, demuxer_->num_seek_requests());
874 WaitForDecodeDone(have_audio, have_video);
875 EXPECT_EQ(1, demuxer_->num_seek_requests());
877 player_.OnDemuxerSeekDone(kNoTimestamp());
878 EXPECT_FALSE(manager_.playback_completed());
881 base::TimeTicks StartTimeTicks() {
882 return player_.start_time_ticks_;
885 bool IsRequestingDemuxerData(bool is_audio) {
886 return GetMediaDecoderJob(is_audio)->is_requesting_demuxer_data_;
889 bool IsDrainingDecoder(bool is_audio) {
890 return GetMediaDecoderJob(is_audio)->drain_decoder_;
893 protected:
894 base::MessageLoop message_loop_;
895 MockMediaPlayerManager manager_;
896 MockDemuxerAndroid* demuxer_; // Owned by |player_|.
897 MediaSourcePlayer player_;
899 // Track whether a possibly async decoder callback test hook has run.
900 bool decoder_callback_hook_executed_;
902 // We need to keep the surface texture while the decoder is actively decoding.
903 // Otherwise, it may trigger unexpected crashes on some devices. To switch
904 // surfaces, tests need to create a new surface texture without releasing
905 // their previous one. In CreateNextTextureAndSetVideoSurface(), we toggle
906 // between two surface textures, only replacing the N-2 texture. Assumption is
907 // that no more than N-1 texture is in use by decoder when
908 // CreateNextTextureAndSetVideoSurface() is called.
909 scoped_refptr<gfx::SurfaceTexture> surface_texture_a_;
910 scoped_refptr<gfx::SurfaceTexture> surface_texture_b_;
911 bool surface_texture_a_is_next_;
912 int next_texture_id_;
914 bool verify_not_audible_is_called_;
916 DISALLOW_COPY_AND_ASSIGN(MediaSourcePlayerTest);
919 TEST_F(MediaSourcePlayerTest, StartAudioDecoderWithValidConfig) {
920 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
922 // Test audio codec will be created when valid configs and data are passed to
923 // the audio decoder job.
924 StartAudioDecoderJob();
925 EXPECT_EQ(0, demuxer_->num_seek_requests());
926 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
927 EXPECT_TRUE(GetMediaCodecBridge(true));
930 TEST_F(MediaSourcePlayerTest, StartAudioDecoderWithInvalidConfig) {
931 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
933 // Test audio decoder job will not be created when failed to start the codec.
934 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, false);
935 // Replace with invalid |audio_extra_data|
936 configs.audio_extra_data.clear();
937 uint8 invalid_codec_data[] = { 0x00, 0xff, 0xff, 0xff, 0xff };
938 configs.audio_extra_data.insert(configs.audio_extra_data.begin(),
939 invalid_codec_data, invalid_codec_data + 4);
940 Start(configs);
942 // Decoder is not created after data is received.
943 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
944 EXPECT_FALSE(GetMediaCodecBridge(true));
947 // timav
948 TEST_F(MediaSourcePlayerTest, AudioDecoderSetsAudibleState) {
949 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
951 // No data arrived yet
952 EXPECT_FALSE(manager_.is_audible());
954 // Initialize decoder
955 StartAudioDecoderJob();
956 player_.SetVolume(1.0);
958 // Process frames until prerolling is done.
959 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100));
960 EXPECT_TRUE(IsPrerolling(true));
961 PrerollDecoderToTime(
962 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), false);
963 EXPECT_TRUE(IsPrerolling(false));
965 // Send more packets
966 PlayAudioForTimeInterval(base::TimeDelta::FromMilliseconds(150),
967 base::TimeDelta::FromMilliseconds(220));
969 // The player should trigger audible status
970 EXPECT_TRUE(manager_.is_audible());
972 // The player release should report a non-audible state.
973 ReleasePlayer();
974 EXPECT_FALSE(manager_.is_audible());
977 TEST_F(MediaSourcePlayerTest, AudioDecoderRemovesAudibleStateWhenPaused) {
978 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
980 // No data arrived yet
981 EXPECT_FALSE(manager_.is_audible());
983 // Initialize decoder
984 StartAudioDecoderJob();
985 player_.SetVolume(1.0);
987 // Process frames until prerolling is done.
988 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100));
989 EXPECT_TRUE(IsPrerolling(true));
990 PrerollDecoderToTime(
991 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), false);
992 EXPECT_TRUE(IsPrerolling(false));
994 // Send more packets
995 PlayAudioForTimeInterval(base::TimeDelta::FromMilliseconds(150),
996 base::TimeDelta::FromMilliseconds(220));
998 // The player should trigger audible status
999 EXPECT_TRUE(manager_.is_audible());
1001 // Pause the player
1002 player_.Pause(true);
1004 // Send more packets
1005 PlayAudioForTimeInterval(base::TimeDelta::FromMilliseconds(240),
1006 base::TimeDelta::FromMilliseconds(280));
1008 // The player should trigger audible status again
1009 EXPECT_FALSE(manager_.is_audible());
1011 player_.Release();
1014 TEST_F(MediaSourcePlayerTest, AudioDecoderRemovesAudibleStateWhenIdle) {
1015 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1017 // No data arrived yet
1018 EXPECT_FALSE(manager_.is_audible());
1020 // Initialize decoder
1021 StartAudioDecoderJob();
1022 player_.SetVolume(1.0);
1024 // Process frames until prerolling is done.
1025 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100));
1026 EXPECT_TRUE(IsPrerolling(true));
1027 PrerollDecoderToTime(
1028 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), false);
1029 EXPECT_TRUE(IsPrerolling(false));
1031 // Send more packets
1032 PlayAudioForTimeInterval(base::TimeDelta::FromMilliseconds(150),
1033 base::TimeDelta::FromMilliseconds(220));
1035 // The player should trigger audible status
1036 EXPECT_TRUE(manager_.is_audible());
1038 // Simulate the freeze on demuxer: wait for 300 ms
1039 WaitForDelay(base::TimeDelta::FromMilliseconds(300));
1041 // By this time the player should have reported
1042 // that there is no audio.
1043 EXPECT_FALSE(manager_.is_audible());
1045 ReleasePlayer();
1048 TEST_F(MediaSourcePlayerTest, StartVideoCodecWithValidSurface) {
1049 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1051 // Test video codec will not be created until data is received.
1052 StartVideoDecoderJob();
1054 // Set both an initial and a later video surface without receiving any
1055 // demuxed data yet.
1056 CreateNextTextureAndSetVideoSurface();
1057 EXPECT_FALSE(GetMediaCodecBridge(false));
1058 CreateNextTextureAndSetVideoSurface();
1059 EXPECT_FALSE(GetMediaCodecBridge(false));
1061 // No seeks, even on setting surface, should have occurred. (Browser seeks can
1062 // occur on setting surface, but only after previously receiving video data.)
1063 EXPECT_EQ(0, demuxer_->num_seek_requests());
1065 // Send the first input chunk and verify that decoder will be created.
1066 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1067 EXPECT_TRUE(GetMediaCodecBridge(false));
1068 WaitForVideoDecodeDone();
1071 TEST_F(MediaSourcePlayerTest, StartVideoCodecWithInvalidSurface) {
1072 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1074 // Test video codec will not be created when surface is invalid.
1075 scoped_refptr<gfx::SurfaceTexture> surface_texture(
1076 gfx::SurfaceTexture::Create(0));
1077 gfx::ScopedJavaSurface surface(surface_texture.get());
1078 StartVideoDecoderJob();
1080 // Release the surface texture.
1081 surface_texture = NULL;
1082 player_.SetVideoSurface(surface.Pass());
1084 // Player should not seek the demuxer on setting initial surface.
1085 EXPECT_EQ(0, demuxer_->num_seek_requests());
1086 EXPECT_EQ(1, demuxer_->num_data_requests());
1088 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1089 EXPECT_FALSE(GetMediaCodecBridge(false));
1092 TEST_F(MediaSourcePlayerTest, ReadFromDemuxerAfterSeek) {
1093 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1095 // Test decoder job will resend a ReadFromDemuxer request after seek.
1096 StartAudioDecoderJob();
1097 SeekPlayerWithAbort(true, base::TimeDelta());
1100 TEST_F(MediaSourcePlayerTest, SetSurfaceWhileSeeking) {
1101 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1103 // Test SetVideoSurface() will not cause an extra seek while the player is
1104 // waiting for demuxer to indicate seek is done.
1105 player_.OnDemuxerConfigsAvailable(
1106 CreateVideoDemuxerConfigs(false));
1108 // Initiate a seek. Skip requesting element seek of renderer.
1109 // Instead behave as if the renderer has asked us to seek.
1110 player_.SeekTo(base::TimeDelta());
1111 EXPECT_EQ(1, demuxer_->num_seek_requests());
1113 CreateNextTextureAndSetVideoSurface();
1114 EXPECT_EQ(1, demuxer_->num_seek_requests());
1115 player_.Start();
1117 // Send the seek done notification. The player should start requesting data.
1118 player_.OnDemuxerSeekDone(kNoTimestamp());
1119 EXPECT_FALSE(GetMediaCodecBridge(false));
1120 EXPECT_EQ(1, demuxer_->num_data_requests());
1121 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1122 EXPECT_TRUE(GetMediaCodecBridge(false));
1124 // Reconfirm exactly 1 seek request has been made of demuxer, and that it
1125 // was not a browser seek request.
1126 EXPECT_EQ(1, demuxer_->num_seek_requests());
1127 EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
1128 WaitForVideoDecodeDone();
1131 TEST_F(MediaSourcePlayerTest, ChangeMultipleSurfaceWhileDecoding) {
1132 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1134 // Test MediaSourcePlayer can switch multiple surfaces during decoding.
1135 CreateNextTextureAndSetVideoSurface();
1136 StartVideoDecoderJob();
1137 EXPECT_EQ(0, demuxer_->num_seek_requests());
1139 // Send the first input chunk.
1140 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1142 // While the decoder is decoding, change multiple surfaces. Pass an empty
1143 // surface first.
1144 gfx::ScopedJavaSurface empty_surface;
1145 player_.SetVideoSurface(empty_surface.Pass());
1146 // Next, pass a new non-empty surface.
1147 CreateNextTextureAndSetVideoSurface();
1149 // Wait for the media codec bridge to finish decoding and be reset pending a
1150 // browser seek.
1151 WaitForVideoDecodeDone();
1152 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1154 // Only one browser seek should have been initiated. No further data request
1155 // should have been processed on |message_loop_| before surface change event
1156 // became pending, above.
1157 EXPECT_EQ(1, demuxer_->num_browser_seek_requests());
1158 EXPECT_EQ(2, demuxer_->num_data_requests());
1160 // Simulate browser seek is done and confirm player requests more data for new
1161 // video codec.
1162 player_.OnDemuxerSeekDone(player_.GetCurrentTime());
1163 EXPECT_FALSE(GetMediaCodecBridge(false));
1164 EXPECT_EQ(3, demuxer_->num_data_requests());
1165 EXPECT_EQ(1, demuxer_->num_seek_requests());
1167 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1168 EXPECT_TRUE(GetMediaCodecBridge(false));
1169 WaitForVideoDecodeDone();
1172 TEST_F(MediaSourcePlayerTest, SetEmptySurfaceAndStarveWhileDecoding) {
1173 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1175 // Test player pauses if an empty surface is passed.
1176 CreateNextTextureAndSetVideoSurface();
1177 StartVideoDecoderJob();
1178 EXPECT_EQ(1, demuxer_->num_data_requests());
1180 // Send the first input chunk.
1181 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1183 // While the decoder is decoding, pass an empty surface.
1184 gfx::ScopedJavaSurface empty_surface;
1185 player_.SetVideoSurface(empty_surface.Pass());
1186 // Let the player starve. However, it should not issue any new data request in
1187 // this case.
1188 TriggerPlayerStarvation();
1189 // Wait for the media codec bridge to finish decoding and be reset.
1190 while (GetMediaDecoderJob(false)->is_decoding())
1191 message_loop_.RunUntilIdle();
1193 // No further seek or data requests should have been received since the
1194 // surface is empty.
1195 EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
1196 EXPECT_EQ(2, demuxer_->num_data_requests());
1197 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1199 // Playback resumes once a non-empty surface is passed.
1200 CreateNextTextureAndSetVideoSurface();
1201 EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
1202 while(demuxer_->num_browser_seek_requests() != 1)
1203 message_loop_.RunUntilIdle();
1204 WaitForVideoDecodeDone();
1207 TEST_F(MediaSourcePlayerTest, ReleaseVideoDecoderResourcesWhileDecoding) {
1208 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1210 // Test that if video decoder is released while decoding, the resources will
1211 // not be immediately released.
1212 CreateNextTextureAndSetVideoSurface();
1213 StartVideoDecoderJob();
1214 // No resource is requested since there is no data to decode.
1215 EXPECT_EQ(0, manager_.num_resources_requested());
1216 ReleasePlayer();
1217 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1219 // Recreate the video decoder.
1220 CreateNextTextureAndSetVideoSurface();
1221 player_.Start();
1222 while (!GetMediaDecoderJob(false)->is_decoding())
1223 message_loop_.RunUntilIdle();
1224 EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
1225 EXPECT_EQ(1, manager_.num_resources_requested());
1226 ReleasePlayer();
1227 // Wait for the media codec bridge to finish decoding and be reset.
1228 while (GetMediaDecoderJob(false)->is_decoding())
1229 message_loop_.RunUntilIdle();
1232 TEST_F(MediaSourcePlayerTest, AudioOnlyStartAfterSeekFinish) {
1233 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1235 // Test audio decoder job will not start until pending seek event is handled.
1236 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, false);
1237 player_.OnDemuxerConfigsAvailable(configs);
1239 // Initiate a seek. Skip requesting element seek of renderer.
1240 // Instead behave as if the renderer has asked us to seek.
1241 player_.SeekTo(base::TimeDelta());
1242 EXPECT_EQ(1, demuxer_->num_seek_requests());
1244 player_.Start();
1245 EXPECT_EQ(0, demuxer_->num_data_requests());
1247 // Sending back the seek done notification.
1248 player_.OnDemuxerSeekDone(kNoTimestamp());
1249 EXPECT_FALSE(GetMediaCodecBridge(true));
1250 EXPECT_EQ(1, demuxer_->num_data_requests());
1252 // Reconfirm exactly 1 seek request has been made of demuxer.
1253 EXPECT_EQ(1, demuxer_->num_seek_requests());
1255 // Decoder is created after data is received.
1256 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1257 EXPECT_TRUE(GetMediaCodecBridge(true));
1260 TEST_F(MediaSourcePlayerTest, VideoOnlyStartAfterSeekFinish) {
1261 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1263 // Test video decoder job will not start until pending seek event is handled.
1264 CreateNextTextureAndSetVideoSurface();
1265 DemuxerConfigs configs = CreateVideoDemuxerConfigs(false);
1266 player_.OnDemuxerConfigsAvailable(configs);
1268 // Initiate a seek. Skip requesting element seek of renderer.
1269 // Instead behave as if the renderer has asked us to seek.
1270 player_.SeekTo(base::TimeDelta());
1271 EXPECT_EQ(1, demuxer_->num_seek_requests());
1273 player_.Start();
1274 EXPECT_EQ(0, demuxer_->num_data_requests());
1276 // Sending back the seek done notification.
1277 player_.OnDemuxerSeekDone(kNoTimestamp());
1278 EXPECT_FALSE(GetMediaCodecBridge(false));
1279 EXPECT_EQ(1, demuxer_->num_data_requests());
1281 // Reconfirm exactly 1 seek request has been made of demuxer.
1282 EXPECT_EQ(1, demuxer_->num_seek_requests());
1284 // Decoder is created after data is received.
1285 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1286 EXPECT_TRUE(GetMediaCodecBridge(false));
1287 WaitForVideoDecodeDone();
1290 TEST_F(MediaSourcePlayerTest, StartImmediatelyAfterPause) {
1291 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1293 // Test that if the decoding job is not fully stopped after Pause(),
1294 // calling Start() will be a noop.
1295 StartAudioDecoderJob();
1297 MediaDecoderJob* decoder_job = GetMediaDecoderJob(true);
1298 EXPECT_FALSE(GetMediaDecoderJob(true)->is_decoding());
1300 // Sending data to player.
1301 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1302 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1303 EXPECT_EQ(2, demuxer_->num_data_requests());
1305 // Decoder job will not immediately stop after Pause() since it is
1306 // running on another thread.
1307 player_.Pause(true);
1308 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1310 // Nothing happens when calling Start() again.
1311 player_.Start();
1312 // Verify that Start() will not destroy and recreate the media codec bridge.
1313 EXPECT_EQ(decoder_job, GetMediaDecoderJob(true));
1315 while (GetMediaDecoderJob(true)->is_decoding())
1316 message_loop_.RunUntilIdle();
1317 // The decoder job should finish and wait for data.
1318 EXPECT_EQ(2, demuxer_->num_data_requests());
1319 EXPECT_TRUE(IsRequestingDemuxerData(true));
1322 TEST_F(MediaSourcePlayerTest, DecoderJobsCannotStartWithoutAudio) {
1323 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1325 // Test that when Start() is called, video decoder job will wait for audio
1326 // decoder job before start decoding the data.
1327 CreateNextTextureAndSetVideoSurface();
1328 Start(CreateAudioVideoDemuxerConfigs());
1329 MediaDecoderJob* audio_decoder_job = GetMediaDecoderJob(true);
1330 MediaDecoderJob* video_decoder_job = GetMediaDecoderJob(false);
1332 EXPECT_FALSE(audio_decoder_job->is_decoding());
1333 EXPECT_FALSE(video_decoder_job->is_decoding());
1335 // Sending video data to player, video decoder should not start.
1336 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1337 EXPECT_FALSE(video_decoder_job->is_decoding());
1339 // Sending audio data to player, both decoders should start now.
1340 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1341 EXPECT_TRUE(audio_decoder_job->is_decoding());
1342 EXPECT_TRUE(video_decoder_job->is_decoding());
1344 // No seeks should have occurred.
1345 EXPECT_EQ(0, demuxer_->num_seek_requests());
1346 WaitForVideoDecodeDone();
1349 TEST_F(MediaSourcePlayerTest, StartTimeTicksResetAfterDecoderUnderruns) {
1350 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1352 // Test start time ticks will reset after decoder job underruns.
1353 StartAudioDecoderJob();
1355 DecodeAudioDataUntilOutputBecomesAvailable();
1357 // The decoder job should finish prerolling and start prefetching.
1358 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(3));
1359 base::TimeTicks previous = StartTimeTicks();
1361 // Let the decoder starve.
1362 TriggerPlayerStarvation();
1363 WaitForAudioDecodeDone();
1364 EXPECT_TRUE(StartTimeTicks() == previous);
1366 // Send new data to the decoder so it can finish prefetching. This should
1367 // reset the start time ticks.
1368 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(3));
1369 EXPECT_TRUE(StartTimeTicks() != previous);
1371 base::TimeTicks current = StartTimeTicks();
1372 EXPECT_LE(0, (current - previous).InMillisecondsF());
1375 TEST_F(MediaSourcePlayerTest, V_SecondAccessUnitIsEOSAndResumePlayAfterSeek) {
1376 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1378 // Test MediaSourcePlayer can replay video after input EOS is reached.
1379 CreateNextTextureAndSetVideoSurface();
1380 StartVideoDecoderJob();
1382 // Send the first input chunk.
1383 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1384 WaitForVideoDecodeDone();
1386 VerifyPlaybackCompletesOnEOSDecode(true, false);
1387 VerifyCompletedPlaybackResumesOnSeekPlusStart(false, true);
1390 TEST_F(MediaSourcePlayerTest, A_FirstAccessUnitIsEOSAndResumePlayAfterSeek) {
1391 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1393 // Test decode of audio EOS buffer without any prior decode. See also
1394 // http://b/11696552.
1395 // Also tests that seeking+Start() after completing audio playback resumes
1396 // playback.
1397 Start(CreateAudioDemuxerConfigs(kCodecAAC, false));
1398 VerifyPlaybackCompletesOnEOSDecode(true, true);
1399 VerifyCompletedPlaybackResumesOnSeekPlusStart(true, false);
1402 TEST_F(MediaSourcePlayerTest, V_FirstAccessUnitAfterSeekIsEOS) {
1403 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1405 // Test decode of video EOS buffer, just after seeking, without any prior
1406 // decode (other than the simulated |kAborted| resulting from the seek
1407 // process.)
1408 CreateNextTextureAndSetVideoSurface();
1409 StartVideoDecoderJob();
1410 SeekPlayerWithAbort(false, base::TimeDelta());
1411 VerifyPlaybackCompletesOnEOSDecode(true, false);
1414 TEST_F(MediaSourcePlayerTest, A_FirstAccessUnitAfterSeekIsEOS) {
1415 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1417 // Test decode of audio EOS buffer, just after seeking, without any prior
1418 // decode (other than the simulated |kAborted| resulting from the seek
1419 // process.) See also http://b/11696552.
1420 Start(CreateAudioDemuxerConfigs(kCodecAAC, false));
1421 SeekPlayerWithAbort(true, base::TimeDelta());
1422 VerifyPlaybackCompletesOnEOSDecode(true, true);
1425 TEST_F(MediaSourcePlayerTest, AV_PlaybackCompletionAcrossConfigChange) {
1426 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1428 // Test that if one stream (audio) has completed decode of EOS and the other
1429 // stream (video) processes config change, that subsequent video EOS completes
1430 // A/V playback.
1431 // Also tests that seeking+Start() after completing playback resumes playback.
1432 CreateNextTextureAndSetVideoSurface();
1433 Start(CreateAudioVideoDemuxerConfigs());
1435 player_.OnDemuxerDataAvailable(CreateEOSAck(true)); // Audio EOS
1436 DemuxerConfigs configs = CreateVideoDemuxerConfigs(true);
1437 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckWithConfigChanged(
1438 false, 0, configs)); // Video |kConfigChanged| as first unit.
1440 WaitForAudioVideoDecodeDone();
1442 EXPECT_EQ(3, demuxer_->num_data_requests());
1444 // At no time after completing audio EOS decode, above, should the
1445 // audio decoder job resume decoding. Send and decode video EOS.
1446 VerifyPlaybackCompletesOnEOSDecode(true, false);
1447 VerifyCompletedPlaybackResumesOnSeekPlusStart(true, true);
1450 TEST_F(MediaSourcePlayerTest, VA_PlaybackCompletionAcrossConfigChange) {
1451 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1453 // Test that if one stream (video) has completed decode of EOS and the other
1454 // stream (audio) processes config change, that subsequent audio EOS completes
1455 // A/V playback.
1456 // Also tests that seeking+Start() after completing playback resumes playback.
1457 CreateNextTextureAndSetVideoSurface();
1458 Start(CreateAudioVideoDemuxerConfigs());
1460 player_.OnDemuxerDataAvailable(CreateEOSAck(false)); // Video EOS
1461 // Audio |kConfigChanged| as first unit.
1462 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckWithConfigChanged(
1463 true, 0, CreateAudioDemuxerConfigs(kCodecVorbis, false)));
1465 WaitForAudioVideoDecodeDone();
1467 EXPECT_EQ(3, demuxer_->num_data_requests());
1469 // At no time after completing video EOS decode, above, should the
1470 // video decoder job resume decoding. Send and decode audio EOS.
1471 VerifyPlaybackCompletesOnEOSDecode(true, true);
1472 VerifyCompletedPlaybackResumesOnSeekPlusStart(true, true);
1475 TEST_F(MediaSourcePlayerTest, AV_NoPrefetchForFinishedVideoOnAudioStarvation) {
1476 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1478 // Test that if one stream (video) has completed decode of EOS, prefetch
1479 // resulting from player starvation occurs only for the other stream (audio),
1480 // and responding to that prefetch with EOS completes A/V playback, even if
1481 // another starvation occurs during the latter EOS's decode.
1482 CreateNextTextureAndSetVideoSurface();
1483 Start(CreateAudioVideoDemuxerConfigs());
1485 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1486 player_.OnDemuxerDataAvailable(CreateEOSAck(false)); // Video EOS
1488 // Wait until video EOS is processed and more data (assumed to be audio) is
1489 // requested.
1490 WaitForAudioVideoDecodeDone();
1491 EXPECT_EQ(3, demuxer_->num_data_requests());
1493 // Simulate decoder underrun to trigger prefetch while still decoding audio.
1494 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(1));
1495 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding() &&
1496 !GetMediaDecoderJob(false)->is_decoding());
1497 TriggerPlayerStarvation();
1499 // Complete the audio decode that was in progress when simulated player
1500 // starvation was triggered.
1501 WaitForAudioDecodeDone();
1502 EXPECT_EQ(4, demuxer_->num_data_requests());
1503 player_.OnDemuxerDataAvailable(CreateEOSAck(true)); // Audio EOS
1504 EXPECT_FALSE(GetMediaDecoderJob(false)->is_decoding());
1505 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1507 // Simulate another decoder underrun to trigger prefetch while decoding EOS.
1508 TriggerPlayerStarvation();
1509 VerifyPlaybackCompletesOnEOSDecode(false, true /* ignored */);
1512 TEST_F(MediaSourcePlayerTest, V_StarvationDuringEOSDecode) {
1513 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1515 // Test that video-only playback completes without further data requested when
1516 // starvation occurs during EOS decode.
1517 CreateNextTextureAndSetVideoSurface();
1518 StartVideoDecoderJob();
1519 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1520 WaitForVideoDecodeDone();
1522 // Simulate decoder underrun to trigger prefetch while decoding EOS.
1523 player_.OnDemuxerDataAvailable(CreateEOSAck(false)); // Video EOS
1524 EXPECT_TRUE(GetMediaDecoderJob(false)->is_decoding());
1525 TriggerPlayerStarvation();
1526 VerifyPlaybackCompletesOnEOSDecode(false, false /* ignored */);
1529 TEST_F(MediaSourcePlayerTest, A_StarvationDuringEOSDecode) {
1530 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1532 // Test that audio-only playback completes without further data requested when
1533 // starvation occurs during EOS decode.
1534 StartAudioDecoderJob();
1535 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1536 WaitForAudioDecodeDone();
1538 // Simulate decoder underrun to trigger prefetch while decoding EOS.
1539 player_.OnDemuxerDataAvailable(CreateEOSAck(true)); // Audio EOS
1540 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1541 TriggerPlayerStarvation();
1542 VerifyPlaybackCompletesOnEOSDecode(false, true /* ignored */);
1545 TEST_F(MediaSourcePlayerTest, AV_SeekDuringEOSDecodePreventsCompletion) {
1546 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1548 // Test that seek supercedes audio+video playback completion on simultaneous
1549 // audio and video EOS decode, if SeekTo() occurs during these EOS decodes.
1550 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, true, true, true);
1553 TEST_F(MediaSourcePlayerTest, AV_SeekDuringAudioEOSDecodePreventsCompletion) {
1554 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1556 // Test that seek supercedes audio+video playback completion on simultaneous
1557 // audio EOS and video non-EOS decode, if SeekTo() occurs during these
1558 // decodes.
1559 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, true, true, false);
1562 TEST_F(MediaSourcePlayerTest, AV_SeekDuringVideoEOSDecodePreventsCompletion) {
1563 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1565 // Test that seek supercedes audio+video playback completion on simultaneous
1566 // audio non-EOS and video EOS decode, if SeekTo() occurs during these
1567 // decodes.
1568 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, true, false, true);
1571 TEST_F(MediaSourcePlayerTest, V_SeekDuringEOSDecodePreventsCompletion) {
1572 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1574 // Test that seek supercedes video-only playback completion on EOS decode, if
1575 // SeekTo() occurs during EOS decode.
1576 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(false, true, false, true);
1579 TEST_F(MediaSourcePlayerTest, A_SeekDuringEOSDecodePreventsCompletion) {
1580 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1582 // Test that seek supercedes audio-only playback completion on EOS decode, if
1583 // SeekTo() occurs during EOS decode.
1584 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, false, true, false);
1587 TEST_F(MediaSourcePlayerTest, NoRequestForDataAfterAbort) {
1588 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1590 // Test that the decoder will not request new data after receiving an aborted
1591 // access unit.
1592 StartAudioDecoderJob();
1594 // Send an aborted access unit.
1595 player_.OnDemuxerDataAvailable(CreateAbortedAck(true));
1596 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1597 WaitForAudioDecodeDone();
1599 // No request will be sent for new data.
1600 EXPECT_EQ(1, demuxer_->num_data_requests());
1602 // No seek requests should have occurred.
1603 EXPECT_EQ(0, demuxer_->num_seek_requests());
1606 TEST_F(MediaSourcePlayerTest, DemuxerDataArrivesAfterRelease) {
1607 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1609 // Test that the decoder should not crash if demuxer data arrives after
1610 // Release().
1611 StartAudioDecoderJob();
1613 ReleasePlayer();
1614 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1616 // The media codec bridge should have been released.
1617 EXPECT_FALSE(player_.IsPlaying());
1619 // No further data should have been requested.
1620 EXPECT_EQ(1, demuxer_->num_data_requests());
1622 // No seek requests should have occurred.
1623 EXPECT_EQ(0, demuxer_->num_seek_requests());
1626 TEST_F(MediaSourcePlayerTest, BrowserSeek_RegularSeekPendsBrowserSeekDone) {
1627 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1629 // Test that a browser seek, once started, delays a newly arrived regular
1630 // SeekTo() request's demuxer seek until the browser seek is done.
1631 BrowserSeekPlayer(false);
1633 // Simulate renderer requesting a regular seek while browser seek in progress.
1634 player_.SeekTo(base::TimeDelta());
1636 // Simulate browser seek is done. Confirm player requests the regular seek,
1637 // still has no video codec configured, and has not requested any
1638 // further data since the surface change event became pending in
1639 // BrowserSeekPlayer().
1640 EXPECT_EQ(1, demuxer_->num_seek_requests());
1641 player_.OnDemuxerSeekDone(base::TimeDelta());
1642 EXPECT_EQ(2, demuxer_->num_seek_requests());
1643 EXPECT_EQ(1, demuxer_->num_browser_seek_requests());
1645 // Simulate regular seek is done and confirm player requests more data for
1646 // new video codec.
1647 player_.OnDemuxerSeekDone(kNoTimestamp());
1648 EXPECT_FALSE(GetMediaCodecBridge(false));
1649 EXPECT_EQ(3, demuxer_->num_data_requests());
1650 EXPECT_EQ(2, demuxer_->num_seek_requests());
1651 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1652 EXPECT_TRUE(GetMediaCodecBridge(false));
1653 WaitForVideoDecodeDone();
1656 TEST_F(MediaSourcePlayerTest, BrowserSeek_InitialReleaseAndStart) {
1657 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1659 // Test that no browser seek is requested if player Release() + Start() occurs
1660 // prior to receiving any data.
1661 CreateNextTextureAndSetVideoSurface();
1662 StartVideoDecoderJob();
1663 ReleasePlayer();
1665 // Pass a new non-empty surface.
1666 CreateNextTextureAndSetVideoSurface();
1668 player_.Start();
1670 // No data request is issued since there is still one pending.
1671 EXPECT_EQ(1, demuxer_->num_data_requests());
1672 EXPECT_FALSE(GetMediaCodecBridge(false));
1674 // No browser seek is needed.
1675 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1676 EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
1677 EXPECT_EQ(2, demuxer_->num_data_requests());
1678 WaitForVideoDecodeDone();
1681 TEST_F(MediaSourcePlayerTest, BrowserSeek_MidStreamReleaseAndStart) {
1682 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1684 // Test that one browser seek is requested if player Release() + Start(), with
1685 // video data received between Release() and Start().
1686 BrowserSeekPlayer(true);
1688 // Simulate browser seek is done and confirm player requests more data.
1689 player_.OnDemuxerSeekDone(base::TimeDelta());
1690 EXPECT_EQ(3, demuxer_->num_data_requests());
1691 EXPECT_EQ(1, demuxer_->num_seek_requests());
1694 TEST_F(MediaSourcePlayerTest, NoBrowserSeekWithKeyFrameInCache) {
1695 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1697 // Test that browser seek is not needed if a key frame is found in data
1698 // cache.
1699 CreateNextTextureAndSetVideoSurface();
1700 StartVideoDecoderJob();
1701 DemuxerData data = CreateReadFromDemuxerAckForVideo(false);
1702 data.access_units[0].is_key_frame = true;
1704 // Simulate demuxer's response to the video data request.
1705 player_.OnDemuxerDataAvailable(data);
1707 // Trigger decoder recreation later by changing surfaces.
1708 CreateNextTextureAndSetVideoSurface();
1710 // Wait for the media codec bridge to finish decoding and be reset.
1711 WaitForVideoDecodeDone();
1712 EXPECT_FALSE(HasData(false));
1714 // Send a non key frame to decoder so that decoder can continue. This will
1715 // not trigger any browser seeks as the previous key frame is still in the
1716 // buffer.
1717 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1718 WaitForVideoDecodeDone();
1719 EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
1722 TEST_F(MediaSourcePlayerTest, PrerollAudioAfterSeek) {
1723 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1725 // Test decoder job will preroll the media to the seek position.
1726 StartAudioDecoderJob();
1728 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100));
1729 EXPECT_TRUE(IsPrerolling(true));
1730 PrerollDecoderToTime(
1731 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), true);
1734 TEST_F(MediaSourcePlayerTest, PrerollVideoAfterSeek) {
1735 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1737 // Test decoder job will preroll the media to the seek position.
1738 CreateNextTextureAndSetVideoSurface();
1739 StartVideoDecoderJob();
1741 SeekPlayerWithAbort(false, base::TimeDelta::FromMilliseconds(100));
1742 EXPECT_TRUE(IsPrerolling(false));
1743 PrerollDecoderToTime(
1744 false, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), true);
1747 TEST_F(MediaSourcePlayerTest, SeekingAfterCompletingPrerollRestartsPreroll) {
1748 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1750 // Test decoder job will begin prerolling upon seek, when it was not
1751 // prerolling prior to the seek.
1752 StartAudioDecoderJob();
1753 MediaDecoderJob* decoder_job = GetMediaDecoderJob(true);
1754 EXPECT_TRUE(IsPrerolling(true));
1756 // Complete the initial preroll by feeding data to the decoder.
1757 DecodeAudioDataUntilOutputBecomesAvailable();
1758 EXPECT_FALSE(IsPrerolling(true));
1760 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(500));
1762 // Prerolling should have begun again.
1763 EXPECT_TRUE(IsPrerolling(true));
1764 EXPECT_EQ(500.0, GetPrerollTimestamp().InMillisecondsF());
1766 // Send data at and after the seek position. Prerolling should complete.
1767 for (int i = 0; i < 4; ++i) {
1768 DemuxerData data = CreateReadFromDemuxerAckForAudio(i);
1769 data.access_units[0].timestamp = base::TimeDelta::FromMilliseconds(
1770 500 + 30 * (i - 1));
1771 player_.OnDemuxerDataAvailable(data);
1772 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1773 WaitForAudioDecodeDone();
1775 EXPECT_LT(500.0, player_.GetCurrentTime().InMillisecondsF());
1776 EXPECT_FALSE(IsPrerolling(true));
1778 // Throughout this test, we should have not re-created the media codec bridge,
1779 // so IsPrerolling() transition from false to true was not due to constructor
1780 // initialization. It was due to BeginPrerolling().
1781 EXPECT_EQ(decoder_job, GetMediaDecoderJob(true));
1784 TEST_F(MediaSourcePlayerTest, PrerollContinuesAcrossReleaseAndStart) {
1785 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1787 // Test decoder job will resume media prerolling if interrupted by Release()
1788 // and Start().
1789 StartAudioDecoderJob();
1791 base::TimeDelta target_timestamp = base::TimeDelta::FromMilliseconds(100);
1792 SeekPlayerWithAbort(true, target_timestamp);
1793 EXPECT_TRUE(IsPrerolling(true));
1794 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
1796 // Send some data before the seek position.
1797 // Test uses 'large' number of iterations because decoder job may not get
1798 // MEDIA_CODEC_OK output status until after a few dequeue output attempts.
1799 // This allows decoder status to stabilize prior to AU timestamp reaching
1800 // the preroll target.
1801 DemuxerData data;
1802 for (int i = 0; i < 10; ++i) {
1803 data = CreateReadFromDemuxerAckForAudio(3);
1804 data.access_units[0].timestamp = base::TimeDelta::FromMilliseconds(i * 10);
1805 if (i == 1) {
1806 // While still prerolling, Release() and Start() the player.
1807 ReleasePlayer();
1808 // The decoder is still decoding and will not be immediately released.
1809 EXPECT_TRUE(GetMediaCodecBridge(true));
1810 Resume(false, false);
1811 } else {
1812 player_.OnDemuxerDataAvailable(data);
1813 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1814 WaitForAudioDecodeDone();
1816 EXPECT_TRUE(IsPrerolling(true));
1818 EXPECT_EQ(100.0, player_.GetCurrentTime().InMillisecondsF());
1819 EXPECT_TRUE(IsPrerolling(true));
1821 // Send data after the seek position.
1822 PrerollDecoderToTime(true, target_timestamp, target_timestamp, true);
1825 // Flaky on Android: crbug.com/419122.
1826 #if defined(OS_ANDROID)
1827 #define MAYBE_PrerollContinuesAcrossConfigChange \
1828 DISABLED_PrerollContinuesAcrossConfigChange
1829 #else
1830 #define MAYBE_PrerollContinuesAcrossConfigChange \
1831 PrerollContinuesAcrossConfigChange
1832 #endif
1833 TEST_F(MediaSourcePlayerTest, MAYBE_PrerollContinuesAcrossConfigChange) {
1834 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1836 // Test decoder job will resume media prerolling if interrupted by
1837 // |kConfigChanged| and OnDemuxerConfigsAvailable().
1838 StartAudioDecoderJob();
1840 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100));
1841 EXPECT_TRUE(IsPrerolling(true));
1842 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
1844 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, true);
1846 // In response to data request, simulate that demuxer signals config change by
1847 // sending an AU with |kConfigChanged|.
1848 DemuxerData data = CreateReadFromDemuxerAckWithConfigChanged(
1849 true, 0, configs);
1850 player_.OnDemuxerDataAvailable(data);
1852 PrerollDecoderToTime(
1853 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), true);
1856 TEST_F(MediaSourcePlayerTest, PrerollContinuesAfterUnchangedConfigs) {
1857 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1859 // Test decoder job will resume media prerolling if interrupted by a config
1860 // change access unit with unchanged configs.
1861 StartAudioDecoderJob();
1863 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100));
1864 EXPECT_TRUE(IsPrerolling(true));
1865 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
1867 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, false);
1869 // In response to data request, simulate that demuxer signals config change by
1870 // sending an AU with |kConfigChanged|.
1871 DemuxerData data = CreateReadFromDemuxerAckWithConfigChanged(
1872 true, 0, configs);
1873 player_.OnDemuxerDataAvailable(data);
1874 PrerollDecoderToTime(
1875 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), true);
1878 TEST_F(MediaSourcePlayerTest, AudioPrerollFinishesBeforeVideo) {
1879 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1881 // Test that after audio finishes prerolling, it will wait for video to finish
1882 // prerolling before advancing together.
1883 CreateNextTextureAndSetVideoSurface();
1884 Start(CreateAudioVideoDemuxerConfigs());
1886 // Initiate a seek.
1887 base::TimeDelta seek_position = base::TimeDelta::FromMilliseconds(100);
1888 player_.SeekTo(seek_position);
1889 player_.OnDemuxerDataAvailable(CreateAbortedAck(true));
1890 player_.OnDemuxerDataAvailable(CreateAbortedAck(false));
1891 WaitForDecodeDone(true, true);
1893 // Verify that the seek is requested.
1894 EXPECT_EQ(1, demuxer_->num_seek_requests());
1895 player_.OnDemuxerSeekDone(kNoTimestamp());
1896 EXPECT_EQ(4, demuxer_->num_data_requests());
1897 EXPECT_EQ(player_.GetCurrentTime().InMillisecondsF(), 100.0);
1898 EXPECT_EQ(GetPrerollTimestamp().InMillisecondsF(), 100.0);
1900 // Send both audio and video data to finish prefetching.
1901 base::TimeDelta seek_ack_position = base::TimeDelta::FromMilliseconds(70);
1902 DemuxerData audio_data = CreateReadFromDemuxerAckForAudio(0);
1903 audio_data.access_units[0].timestamp = seek_ack_position;
1904 DemuxerData video_data = CreateReadFromDemuxerAckForVideo(false);
1905 video_data.access_units[0].timestamp = seek_ack_position;
1906 player_.OnDemuxerDataAvailable(audio_data);
1907 player_.OnDemuxerDataAvailable(video_data);
1908 WaitForAudioDecodeDone();
1909 WaitForVideoDecodeDone();
1911 // Send audio data at and after the seek position. Audio should finish
1912 // prerolling and stop decoding.
1913 EXPECT_EQ(6, demuxer_->num_data_requests());
1914 PrerollDecoderToTime(true, seek_position, seek_position, true);
1915 EXPECT_FALSE(GetMediaDecoderJob(true)->is_decoding());
1916 EXPECT_FALSE(IsPrerolling(true));
1917 EXPECT_TRUE(IsPrerolling(false));
1919 // Send video data to let video finish prerolling.
1920 PrerollDecoderToTime(false, seek_position, seek_position, false);
1921 EXPECT_FALSE(IsPrerolling(false));
1923 // Both audio and video decoders should start decoding again.
1924 player_.OnDemuxerDataAvailable(audio_data);
1925 player_.OnDemuxerDataAvailable(video_data);
1926 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1927 EXPECT_TRUE(GetMediaDecoderJob(false)->is_decoding());
1930 TEST_F(MediaSourcePlayerTest, SimultaneousAudioVideoConfigChange) {
1931 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1933 // Test that the player allows simultaneous audio and video config change,
1934 // such as might occur during OnPrefetchDone() if next access unit for both
1935 // audio and video jobs is |kConfigChanged|.
1936 CreateNextTextureAndSetVideoSurface();
1937 Start(CreateAudioVideoDemuxerConfigs());
1938 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1939 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1940 EXPECT_TRUE(GetMediaCodecBridge(true));
1941 EXPECT_TRUE(GetMediaCodecBridge(false));
1942 EnableAdaptiveVideoPlayback(false);
1943 WaitForAudioVideoDecodeDone();
1945 // If audio or video hasn't finished prerolling, let them finish it.
1946 if (IsPrerolling(true))
1947 PrerollDecoderToTime(true, base::TimeDelta(), base::TimeDelta(), true);
1948 if (IsPrerolling(false))
1949 PrerollDecoderToTime(false, base::TimeDelta(), base::TimeDelta(), false);
1950 int expected_num_data_requests = demuxer_->num_data_requests();
1952 // Simulate audio |kConfigChanged| prefetched as standalone access unit.
1953 DemuxerConfigs audio_configs = CreateAudioDemuxerConfigs(kCodecVorbis, true);
1954 player_.OnDemuxerDataAvailable(
1955 CreateReadFromDemuxerAckWithConfigChanged(true, 0, audio_configs));
1957 // Simulate video |kConfigChanged| prefetched as standalone access unit.
1958 player_.OnDemuxerDataAvailable(
1959 CreateReadFromDemuxerAckWithConfigChanged(
1960 false, 0, CreateVideoDemuxerConfigs(true)));
1961 EXPECT_EQ(expected_num_data_requests + 2, demuxer_->num_data_requests());
1962 EXPECT_TRUE(IsDrainingDecoder(true));
1963 EXPECT_TRUE(IsDrainingDecoder(false));
1965 // Waiting for decoder to finish draining.
1966 while (IsDrainingDecoder(true) || IsDrainingDecoder(false))
1967 message_loop_.RunUntilIdle();
1970 TEST_F(MediaSourcePlayerTest,
1971 SimultaneousAudioVideoConfigChangeWithAdaptivePlayback) {
1972 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1974 // Test that the player allows simultaneous audio and video config change with
1975 // adaptive video playback enabled.
1976 CreateNextTextureAndSetVideoSurface();
1977 Start(CreateAudioVideoDemuxerConfigs());
1978 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1979 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
1980 EXPECT_EQ(4, demuxer_->num_data_requests());
1981 EXPECT_TRUE(GetMediaCodecBridge(true));
1982 EXPECT_TRUE(GetMediaCodecBridge(false));
1983 EnableAdaptiveVideoPlayback(true);
1984 WaitForAudioVideoDecodeDone();
1986 // If audio or video hasn't finished prerolling, let them finish it.
1987 if (IsPrerolling(true))
1988 PrerollDecoderToTime(true, base::TimeDelta(), base::TimeDelta(), true);
1989 if (IsPrerolling(false))
1990 PrerollDecoderToTime(false, base::TimeDelta(), base::TimeDelta(), false);
1991 int expected_num_data_requests = demuxer_->num_data_requests();
1993 // Simulate audio |kConfigChanged| prefetched as standalone access unit.
1994 DemuxerConfigs audio_configs = CreateAudioDemuxerConfigs(kCodecVorbis, true);
1995 player_.OnDemuxerDataAvailable(
1996 CreateReadFromDemuxerAckWithConfigChanged(true, 0, audio_configs));
1998 // Simulate video |kConfigChanged| prefetched as standalone access unit.
1999 player_.OnDemuxerDataAvailable(
2000 CreateReadFromDemuxerAckWithConfigChanged(
2001 false, 0, CreateVideoDemuxerConfigs(true)));
2002 EXPECT_EQ(expected_num_data_requests + 2, demuxer_->num_data_requests());
2003 EXPECT_TRUE(IsDrainingDecoder(true));
2004 EXPECT_FALSE(IsDrainingDecoder(false));
2006 // Waiting for audio decoder to finish draining.
2007 while (IsDrainingDecoder(true))
2008 message_loop_.RunUntilIdle();
2011 TEST_F(MediaSourcePlayerTest, DemuxerConfigRequestedIfInPrefetchUnit0) {
2012 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2014 // Test that the player detects need for and requests demuxer configs if
2015 // the |kConfigChanged| unit is the very first unit in the set of units
2016 // received in OnDemuxerDataAvailable() ostensibly while
2017 // |PREFETCH_DONE_EVENT_PENDING|.
2018 StartConfigChange(true, true, 0, false);
2019 WaitForAudioDecodeDone();
2022 TEST_F(MediaSourcePlayerTest, DemuxerConfigRequestedIfInPrefetchUnit1) {
2023 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2025 // Test that the player detects need for and requests demuxer configs if
2026 // the |kConfigChanged| unit is not the first unit in the set of units
2027 // received in OnDemuxerDataAvailable() ostensibly while
2028 // |PREFETCH_DONE_EVENT_PENDING|.
2029 StartConfigChange(true, true, 1, false);
2030 WaitForAudioDecodeDone();
2033 TEST_F(MediaSourcePlayerTest, DemuxerConfigRequestedIfInUnit0AfterPrefetch) {
2034 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2036 // Test that the player detects need for and requests demuxer configs if
2037 // the |kConfigChanged| unit is the very first unit in the set of units
2038 // received in OnDemuxerDataAvailable() from data requested ostensibly while
2039 // not prefetching.
2040 StartConfigChange(true, false, 0, false);
2041 WaitForAudioDecodeDone();
2044 TEST_F(MediaSourcePlayerTest, DemuxerConfigRequestedIfInUnit1AfterPrefetch) {
2045 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2047 // Test that the player detects need for and requests demuxer configs if
2048 // the |kConfigChanged| unit is not the first unit in the set of units
2049 // received in OnDemuxerDataAvailable() from data requested ostensibly while
2050 // not prefetching.
2051 StartConfigChange(true, false, 1, false);
2052 WaitForAudioDecodeDone();
2055 TEST_F(MediaSourcePlayerTest, BrowserSeek_PrerollAfterBrowserSeek) {
2056 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2058 // Test decoder job will preroll the media to the actual seek position
2059 // resulting from a browser seek.
2060 BrowserSeekPlayer(false);
2062 // Simulate browser seek is done, but to a later time than was requested.
2063 EXPECT_LT(player_.GetCurrentTime().InMillisecondsF(), 100);
2064 player_.OnDemuxerSeekDone(base::TimeDelta::FromMilliseconds(100));
2065 // Because next AU is not I-frame, MediaCodecBridge will not be recreated.
2066 EXPECT_FALSE(GetMediaCodecBridge(false));
2067 EXPECT_EQ(100.0, player_.GetCurrentTime().InMillisecondsF());
2068 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2069 EXPECT_EQ(3, demuxer_->num_data_requests());
2071 PrerollDecoderToTime(
2072 false, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100), true);
2075 TEST_F(MediaSourcePlayerTest, VideoDemuxerConfigChange) {
2076 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2078 // Test that video config change notification results in creating a new
2079 // video codec without any browser seek.
2080 StartConfigChange(false, true, 1, false);
2082 // New video codec should have been created and configured, without any
2083 // browser seek.
2084 EXPECT_TRUE(GetMediaCodecBridge(false));
2085 EXPECT_EQ(3, demuxer_->num_data_requests());
2086 EXPECT_EQ(0, demuxer_->num_seek_requests());
2088 // 2 codecs should have been created, one before the config change, and one
2089 // after it.
2090 EXPECT_EQ(2, manager_.num_resources_requested());
2091 WaitForVideoDecodeDone();
2094 TEST_F(MediaSourcePlayerTest, VideoDemuxerConfigChangeWithAdaptivePlayback) {
2095 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2097 // Test that if codec supports adaptive playback, no new codec should be
2098 // created beyond the one used to decode the prefetch media data prior to
2099 // the kConfigChanged.
2100 StartConfigChange(false, true, 1, true);
2102 // No browser seek should be needed.
2103 EXPECT_TRUE(GetMediaCodecBridge(false));
2104 EXPECT_EQ(3, demuxer_->num_data_requests());
2105 EXPECT_EQ(0, demuxer_->num_seek_requests());
2107 // Only 1 codec should have been created so far.
2108 EXPECT_EQ(1, manager_.num_resources_requested());
2109 WaitForVideoDecodeDone();
2112 TEST_F(MediaSourcePlayerTest, DecoderDrainInterruptedBySeek) {
2113 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2115 // Test if a decoder is being drained while receiving a seek request, draining
2116 // is canceled.
2117 SendConfigChangeToDecoder(true, false, 0, false);
2118 EXPECT_TRUE(IsDrainingDecoder(true));
2120 player_.SeekTo(base::TimeDelta::FromMilliseconds(100));
2121 WaitForAudioDecodeDone();
2122 EXPECT_FALSE(IsDrainingDecoder(true));
2123 player_.OnDemuxerSeekDone(kNoTimestamp());
2125 EXPECT_EQ(1, demuxer_->num_seek_requests());
2126 EXPECT_EQ(4, demuxer_->num_data_requests());
2129 TEST_F(MediaSourcePlayerTest, DecoderDrainInterruptedByRelease) {
2130 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2132 // Test if a decoder is being drained while receiving a release request,
2133 // draining is canceled.
2134 SendConfigChangeToDecoder(true, false, 0, false);
2135 EXPECT_TRUE(IsDrainingDecoder(true));
2137 ReleasePlayer();
2138 WaitForAudioDecodeDone();
2139 EXPECT_EQ(3, demuxer_->num_data_requests());
2140 EXPECT_FALSE(IsDrainingDecoder(true));
2142 EXPECT_FALSE(GetMediaCodecBridge(true));
2143 EXPECT_FALSE(player_.IsPlaying());
2145 player_.Start();
2146 EXPECT_TRUE(player_.IsPlaying());
2147 EXPECT_EQ(3, demuxer_->num_data_requests());
2150 TEST_F(MediaSourcePlayerTest, DecoderDrainInterruptedBySurfaceChange) {
2151 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2153 // Test if a video decoder is being drained while surface changes, draining
2154 // is canceled.
2155 SendConfigChangeToDecoder(false, false, 0, false);
2156 EXPECT_TRUE(IsDrainingDecoder(false));
2158 CreateNextTextureAndSetVideoSurface();
2159 WaitForVideoDecodeDone();
2161 EXPECT_FALSE(IsDrainingDecoder(false));
2162 EXPECT_TRUE(player_.IsPlaying());
2164 // The frame after the config change should always be an iframe, so no browser
2165 // seek is needed when recreating the video decoder due to surface change.
2166 EXPECT_TRUE(GetMediaCodecBridge(false));
2167 EXPECT_EQ(4, demuxer_->num_data_requests());
2168 EXPECT_EQ(0, demuxer_->num_seek_requests());
2171 TEST_F(MediaSourcePlayerTest,
2172 BrowserSeek_DecoderStarvationWhilePendingSurfaceChange) {
2173 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2175 // Test video decoder starvation while handling a pending surface change
2176 // should not cause any crashes.
2177 CreateNextTextureAndSetVideoSurface();
2178 StartVideoDecoderJob();
2179 DemuxerData data = CreateReadFromDemuxerAckForVideo(false);
2180 player_.OnDemuxerDataAvailable(data);
2182 // Trigger a surface change and decoder starvation.
2183 CreateNextTextureAndSetVideoSurface();
2184 TriggerPlayerStarvation();
2185 WaitForVideoDecodeDone();
2186 EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
2188 // Surface change should trigger a seek.
2189 player_.OnDemuxerDataAvailable(data);
2190 EXPECT_EQ(1, demuxer_->num_browser_seek_requests());
2191 player_.OnDemuxerSeekDone(base::TimeDelta());
2192 // After seek is done, prefetch is handled first. MediaCodecBridge is not
2193 // created at this moment.
2194 EXPECT_FALSE(GetMediaCodecBridge(false));
2196 // A new data request should be sent.
2197 EXPECT_EQ(3, demuxer_->num_data_requests());
2200 TEST_F(MediaSourcePlayerTest, ReleaseWithOnPrefetchDoneAlreadyPosted) {
2201 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2203 // Test if OnPrefetchDone() had already been posted before and is executed
2204 // after Release(), then player does not DCHECK. This test is fragile to
2205 // change to MediaDecoderJob::Prefetch() implementation; it assumes task
2206 // is posted to run |prefetch_cb| if the job already HasData().
2207 // TODO(wolenetz): Remove MSP::set_decode_callback_for_testing() if this test
2208 // becomes obsolete. See http://crbug.com/304234.
2209 StartAudioDecoderJob();
2211 // Escape the original prefetch by decoding a single access unit.
2212 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
2213 WaitForAudioDecodeDone();
2215 // Prime the job with a few more access units, so that a later prefetch,
2216 // triggered by starvation to simulate decoder underrun, can trivially
2217 // post task to run OnPrefetchDone().
2218 player_.OnDemuxerDataAvailable(
2219 CreateReadFromDemuxerAckWithConfigChanged(
2220 true, 4, CreateAudioDemuxerConfigs(kCodecVorbis, false)));
2221 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
2223 // Simulate decoder underrun, so trivial prefetch starts while still decoding.
2224 // The prefetch and posting of OnPrefetchDone() will not occur until next
2225 // MediaDecoderCallBack() occurs.
2226 TriggerPlayerStarvation();
2228 // Upon the next successful decode callback, post a task to call Release() on
2229 // the |player_|, such that the trivial OnPrefetchDone() task posting also
2230 // occurs and should execute after the Release().
2231 OnNextTestDecodeCallbackPostTaskToReleasePlayer();
2233 WaitForAudioDecodeDone();
2234 EXPECT_TRUE(decoder_callback_hook_executed_);
2236 EXPECT_EQ(3, demuxer_->num_data_requests());
2238 // Player should not request any new data since the access units haven't
2239 // been fully decoded yet.
2240 Resume(false, false);
2243 TEST_F(MediaSourcePlayerTest, SeekToThenReleaseThenDemuxerSeekAndDone) {
2244 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2246 // Test if Release() occurs after SeekTo(), but the DemuxerSeek IPC request
2247 // has not yet been sent, then the seek request is sent after Release(). Also,
2248 // test if OnDemuxerSeekDone() occurs prior to next Start(), then the player
2249 // will resume correct post-seek preroll upon Start().
2250 StartAudioDecoderJobAndSeekToWhileDecoding(
2251 base::TimeDelta::FromMilliseconds(100));
2252 ReleasePlayer();
2253 EXPECT_EQ(0, demuxer_->num_seek_requests());
2254 WaitForAudioDecodeDone();
2255 EXPECT_EQ(1, demuxer_->num_seek_requests());
2257 player_.OnDemuxerSeekDone(kNoTimestamp());
2258 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2259 EXPECT_FALSE(GetMediaCodecBridge(true));
2260 EXPECT_FALSE(player_.IsPlaying());
2262 // Player should begin prefetch and resume preroll upon Start().
2263 EXPECT_EQ(2, demuxer_->num_data_requests());
2264 Resume(true, false);
2265 EXPECT_TRUE(IsPrerolling(true));
2266 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2268 // No further seek should have been requested since Release(), above.
2269 EXPECT_EQ(1, demuxer_->num_seek_requests());
2272 TEST_F(MediaSourcePlayerTest, SeekToThenReleaseThenDemuxerSeekThenStart) {
2273 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2275 // Test if Release() occurs after SeekTo(), but the DemuxerSeek IPC request
2276 // has not yet been sent, then the seek request is sent after Release(). Also,
2277 // test if OnDemuxerSeekDone() does not occur until after the next Start(),
2278 // then the player remains pending seek done until (and resumes correct
2279 // post-seek preroll after) OnDemuxerSeekDone().
2280 StartAudioDecoderJobAndSeekToWhileDecoding(
2281 base::TimeDelta::FromMilliseconds(100));
2282 ReleasePlayer();
2283 EXPECT_EQ(0, demuxer_->num_seek_requests());
2285 // Player should not prefetch upon Start() nor create the media codec bridge,
2286 // due to awaiting DemuxerSeekDone.
2287 EXPECT_EQ(2, demuxer_->num_data_requests());
2288 Resume(false, false);
2290 WaitForAudioDecodeDone();
2291 EXPECT_EQ(1, demuxer_->num_seek_requests());
2292 player_.OnDemuxerSeekDone(kNoTimestamp());
2293 EXPECT_TRUE(GetMediaDecoderJob(true));
2294 EXPECT_TRUE(IsPrerolling(true));
2295 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2296 EXPECT_EQ(3, demuxer_->num_data_requests());
2298 // No further seek should have been requested since Release(), above.
2299 EXPECT_EQ(1, demuxer_->num_seek_requests());
2302 TEST_F(MediaSourcePlayerTest, SeekToThenDemuxerSeekThenReleaseThenSeekDone) {
2303 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2305 // Test if Release() occurs after a SeekTo()'s subsequent DemuxerSeek IPC
2306 // request and OnDemuxerSeekDone() arrives prior to the next Start(), then the
2307 // player will resume correct post-seek preroll upon Start().
2308 StartAudioDecoderJobAndSeekToWhileDecoding(
2309 base::TimeDelta::FromMilliseconds(100));
2310 WaitForAudioDecodeDone();
2311 EXPECT_EQ(1, demuxer_->num_seek_requests());
2313 ReleasePlayer();
2314 player_.OnDemuxerSeekDone(kNoTimestamp());
2315 EXPECT_FALSE(player_.IsPlaying());
2316 EXPECT_FALSE(GetMediaCodecBridge(true));
2317 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2319 // Player should begin prefetch and resume preroll upon Start().
2320 EXPECT_EQ(2, demuxer_->num_data_requests());
2321 Resume(true, false);
2322 EXPECT_TRUE(IsPrerolling(true));
2323 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2325 // No further seek should have been requested since before Release(), above.
2326 EXPECT_EQ(1, demuxer_->num_seek_requests());
2329 TEST_F(MediaSourcePlayerTest, SeekToThenReleaseThenStart) {
2330 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2332 // Test if Release() occurs after a SeekTo()'s subsequent DemuxerSeeK IPC
2333 // request and OnDemuxerSeekDone() does not occur until after the next
2334 // Start(), then the player remains pending seek done until (and resumes
2335 // correct post-seek preroll after) OnDemuxerSeekDone().
2336 StartAudioDecoderJobAndSeekToWhileDecoding(
2337 base::TimeDelta::FromMilliseconds(100));
2338 WaitForAudioDecodeDone();
2339 EXPECT_EQ(1, demuxer_->num_seek_requests());
2341 ReleasePlayer();
2342 EXPECT_EQ(2, demuxer_->num_data_requests());
2343 Resume(false, false);
2345 player_.OnDemuxerSeekDone(kNoTimestamp());
2346 EXPECT_FALSE(GetMediaCodecBridge(true));
2347 EXPECT_TRUE(IsPrerolling(true));
2348 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
2349 EXPECT_EQ(3, demuxer_->num_data_requests());
2351 // No further seek should have been requested since before Release(), above.
2352 EXPECT_EQ(1, demuxer_->num_seek_requests());
2355 TEST_F(MediaSourcePlayerTest, ConfigChangedThenReleaseThenStart) {
2356 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2358 // Test if Release() occurs after |kConfigChanged| is processed, new data
2359 // requested of demuxer, and the requested data arrive before the next
2360 // Start(), then the player starts to decode the new data without any seek.
2361 StartConfigChange(true, true, 0, false);
2362 ReleasePlayer();
2364 EXPECT_TRUE(GetMediaCodecBridge(true));
2365 EXPECT_FALSE(player_.IsPlaying());
2366 EXPECT_EQ(3, demuxer_->num_data_requests());
2367 player_.OnDemuxerDataAvailable(
2368 CreateReadFromDemuxerAckWithConfigChanged(
2369 true, 4, CreateAudioDemuxerConfigs(kCodecVorbis, false)));
2370 WaitForAudioDecodeDone();
2371 EXPECT_FALSE(GetMediaCodecBridge(true));
2373 // Player should resume upon Start(), even without further configs supplied.
2374 player_.Start();
2375 EXPECT_TRUE(player_.IsPlaying());
2376 EXPECT_EQ(3, demuxer_->num_data_requests());
2377 EXPECT_EQ(0, demuxer_->num_seek_requests());
2378 WaitForAudioDecodeDone();
2381 TEST_F(MediaSourcePlayerTest, BrowserSeek_ThenReleaseThenDemuxerSeekDone) {
2382 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2384 // Test that Release() after a browser seek's DemuxerSeek IPC request has been
2385 // sent behaves similar to a regular seek: if OnDemuxerSeekDone() occurs
2386 // before the next Start()+SetVideoSurface(), then the player will resume
2387 // correct post-seek preroll upon Start()+SetVideoSurface().
2388 BrowserSeekPlayer(false);
2389 base::TimeDelta expected_preroll_timestamp = player_.GetCurrentTime();
2390 ReleasePlayer();
2392 player_.OnDemuxerSeekDone(expected_preroll_timestamp);
2393 EXPECT_FALSE(player_.IsPlaying());
2394 EXPECT_FALSE(GetMediaCodecBridge(false));
2395 EXPECT_EQ(expected_preroll_timestamp, GetPrerollTimestamp());
2397 // Player should begin prefetch and resume preroll upon Start().
2398 EXPECT_EQ(2, demuxer_->num_data_requests());
2399 CreateNextTextureAndSetVideoSurface();
2400 Resume(false, true);
2401 EXPECT_TRUE(IsPrerolling(false));
2402 EXPECT_EQ(expected_preroll_timestamp, GetPrerollTimestamp());
2403 EXPECT_EQ(expected_preroll_timestamp, player_.GetCurrentTime());
2405 // No further seek should have been requested since BrowserSeekPlayer().
2406 EXPECT_EQ(1, demuxer_->num_seek_requests());
2409 TEST_F(MediaSourcePlayerTest, BrowserSeek_ThenReleaseThenStart) {
2410 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2412 // Test that Release() after a browser seek's DemuxerSeek IPC request has been
2413 // sent behaves similar to a regular seek: if OnDemuxerSeekDone() does not
2414 // occur until after the next Start()+SetVideoSurface(), then the player
2415 // remains pending seek done until (and resumes correct post-seek preroll
2416 // after) OnDemuxerSeekDone().
2417 BrowserSeekPlayer(false);
2418 base::TimeDelta expected_preroll_timestamp = player_.GetCurrentTime();
2419 ReleasePlayer();
2421 EXPECT_EQ(2, demuxer_->num_data_requests());
2422 CreateNextTextureAndSetVideoSurface();
2423 Resume(false, false);
2425 player_.OnDemuxerSeekDone(expected_preroll_timestamp);
2426 // Prefetch takes place first, and the decoder is not created yet.
2427 EXPECT_FALSE(GetMediaCodecBridge(false));
2428 EXPECT_TRUE(IsPrerolling(false));
2429 EXPECT_EQ(expected_preroll_timestamp, GetPrerollTimestamp());
2430 EXPECT_EQ(expected_preroll_timestamp, player_.GetCurrentTime());
2431 EXPECT_EQ(3, demuxer_->num_data_requests());
2433 // No further seek should have been requested since BrowserSeekPlayer().
2434 EXPECT_EQ(1, demuxer_->num_seek_requests());
2436 // Decoder will be created once data is received.
2437 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo(false));
2438 EXPECT_TRUE(GetMediaCodecBridge(false));
2439 WaitForVideoDecodeDone();
2442 // TODO(xhwang): Once we add tests to cover DrmBridge, update this test to
2443 // also verify that the job is successfully created if SetDrmBridge(), Start()
2444 // and eventually OnMediaCrypto() occur. This would increase test coverage of
2445 // http://crbug.com/313470 and allow us to remove inspection of internal player
2446 // pending event state. See http://crbug.com/313860.
2447 TEST_F(MediaSourcePlayerTest, SurfaceChangeClearedEvenIfMediaCryptoAbsent) {
2448 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2450 // Test that |SURFACE_CHANGE_EVENT_PENDING| is not pending after
2451 // SetVideoSurface() for a player configured for encrypted video, when the
2452 // player has not yet received media crypto.
2453 DemuxerConfigs configs = CreateVideoDemuxerConfigs(false);
2454 configs.is_video_encrypted = true;
2456 player_.OnDemuxerConfigsAvailable(configs);
2457 CreateNextTextureAndSetVideoSurface();
2458 EXPECT_FALSE(GetMediaCodecBridge(false));
2461 TEST_F(MediaSourcePlayerTest, CurrentTimeUpdatedWhileDecoderStarved) {
2462 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2464 // Test that current time is updated while decoder is starved.
2465 StartAudioDecoderJob();
2466 DecodeAudioDataUntilOutputBecomesAvailable();
2468 // Trigger starvation while the decoder is decoding.
2469 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(3));
2470 manager_.ResetTimestampUpdated();
2471 TriggerPlayerStarvation();
2472 WaitForAudioDecodeDone();
2474 // Current time should be updated.
2475 EXPECT_TRUE(manager_.timestamp_updated());
2478 TEST_F(MediaSourcePlayerTest, CurrentTimeKeepsIncreasingAfterConfigChange) {
2479 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2481 // Test current time keep on increasing after audio config change.
2482 // Test that current time is updated while decoder is starved.
2483 StartAudioDecoderJob();
2485 DecodeAudioDataUntilOutputBecomesAvailable();
2487 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, true);
2488 DemuxerData data = CreateReadFromDemuxerAckWithConfigChanged(
2489 true, 0, configs);
2490 player_.OnDemuxerDataAvailable(data);
2491 WaitForAudioDecodeDone();
2492 DecodeAudioDataUntilOutputBecomesAvailable();
2495 TEST_F(MediaSourcePlayerTest, VideoMetadataChangeAfterConfigChange) {
2496 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2498 // Test that after a config change, metadata change will be happen
2499 // after decoder is drained.
2500 StartConfigChange(false, true, 2, false);
2501 EXPECT_EQ(1, manager_.num_metadata_changes());
2502 EXPECT_FALSE(IsDrainingDecoder(false));
2504 // Create video data with new resolutions.
2505 DemuxerData data = CreateReadFromDemuxerAckForVideo(true);
2507 // Wait for the metadata change.
2508 while(manager_.num_metadata_changes() == 1) {
2509 player_.OnDemuxerDataAvailable(data);
2510 WaitForVideoDecodeDone();
2512 EXPECT_EQ(2, manager_.num_metadata_changes());
2513 WaitForVideoDecodeDone();
2516 } // namespace media