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. 4 5 #include <string> 6 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" 23 24 namespace media { 25 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) 34 35 const base::TimeDelta kDefaultDuration = 36 base::TimeDelta::FromMilliseconds(10000); 37 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. 40 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_resources_released_(0), 49 timestamp_updated_(false) {} 50 virtual ~MockMediaPlayerManager() {} 51 52 // MediaPlayerManager implementation. 53 virtual MediaResourceGetter* GetMediaResourceGetter() OVERRIDE { 54 return NULL; 55 } 56 virtual MediaUrlInterceptor* GetMediaUrlInterceptor() OVERRIDE { 57 return NULL; 58 } 59 virtual void OnTimeUpdate(int player_id, 60 base::TimeDelta current_time) OVERRIDE { 61 timestamp_updated_ = true; 62 } 63 virtual void OnMediaMetadataChanged( 64 int player_id, base::TimeDelta duration, int width, int height, 65 bool success) OVERRIDE {} 66 virtual void OnPlaybackComplete(int player_id) OVERRIDE { 67 playback_completed_ = true; 68 if (message_loop_->is_running()) 69 message_loop_->Quit(); 70 } 71 virtual void OnMediaInterrupted(int player_id) OVERRIDE {} 72 virtual void OnBufferingUpdate(int player_id, int percentage) OVERRIDE {} 73 virtual void OnSeekComplete(int player_id, 74 const base::TimeDelta& current_time) OVERRIDE {} 75 virtual void OnError(int player_id, int error) OVERRIDE {} 76 virtual void OnVideoSizeChanged(int player_id, int width, 77 int height) OVERRIDE {} 78 virtual MediaPlayerAndroid* GetFullscreenPlayer() OVERRIDE { return NULL; } 79 virtual MediaPlayerAndroid* GetPlayer(int player_id) OVERRIDE { return NULL; } 80 virtual void RequestFullScreen(int player_id) OVERRIDE {} 81 #if defined(VIDEO_HOLE) 82 virtual bool ShouldUseVideoOverlayForEmbeddedEncryptedVideo() OVERRIDE { 83 return false; 84 } 85 #endif // defined(VIDEO_HOLE) 86 87 bool playback_completed() const { 88 return playback_completed_; 89 } 90 91 int num_resources_requested() const { 92 return num_resources_requested_; 93 } 94 95 int num_resources_released() const { 96 return num_resources_released_; 97 } 98 99 void OnMediaResourcesRequested(int player_id) { 100 num_resources_requested_++; 101 } 102 103 void OnMediaResourcesReleased(int player_id) { 104 num_resources_released_++; 105 } 106 107 bool timestamp_updated() const { 108 return timestamp_updated_; 109 } 110 111 void ResetTimestampUpdated() { 112 timestamp_updated_ = false; 113 } 114 115 private: 116 base::MessageLoop* message_loop_; 117 bool playback_completed_; 118 // The number of resource requests this object has seen. 119 int num_resources_requested_; 120 // The number of released resources. 121 int num_resources_released_; 122 // Playback timestamp was updated. 123 bool timestamp_updated_; 124 125 DISALLOW_COPY_AND_ASSIGN(MockMediaPlayerManager); 126 }; 127 128 class MockDemuxerAndroid : public DemuxerAndroid { 129 public: 130 explicit MockDemuxerAndroid(base::MessageLoop* message_loop) 131 : message_loop_(message_loop), 132 num_data_requests_(0), 133 num_seek_requests_(0), 134 num_browser_seek_requests_(0) {} 135 virtual ~MockDemuxerAndroid() {} 136 137 virtual void Initialize(DemuxerAndroidClient* client) OVERRIDE {} 138 virtual void RequestDemuxerData(DemuxerStream::Type type) OVERRIDE { 139 num_data_requests_++; 140 if (message_loop_->is_running()) 141 message_loop_->Quit(); 142 } 143 virtual void RequestDemuxerSeek(const base::TimeDelta& time_to_seek, 144 bool is_browser_seek) OVERRIDE { 145 num_seek_requests_++; 146 if (is_browser_seek) 147 num_browser_seek_requests_++; 148 } 149 150 int num_data_requests() const { return num_data_requests_; } 151 int num_seek_requests() const { return num_seek_requests_; } 152 int num_browser_seek_requests() const { return num_browser_seek_requests_; } 153 154 private: 155 base::MessageLoop* message_loop_; 156 157 // The number of encoded data requests this object has seen. 158 int num_data_requests_; 159 160 // The number of regular and browser seek requests this object has seen. 161 int num_seek_requests_; 162 163 // The number of browser seek requests this object has seen. 164 int num_browser_seek_requests_; 165 166 DISALLOW_COPY_AND_ASSIGN(MockDemuxerAndroid); 167 }; 168 169 class MediaSourcePlayerTest : public testing::Test { 170 public: 171 MediaSourcePlayerTest() 172 : manager_(&message_loop_), 173 demuxer_(new MockDemuxerAndroid(&message_loop_)), 174 player_(0, &manager_, 175 base::Bind(&MockMediaPlayerManager::OnMediaResourcesRequested, 176 base::Unretained(&manager_)), 177 base::Bind(&MockMediaPlayerManager::OnMediaResourcesReleased, 178 base::Unretained(&manager_)), 179 scoped_ptr<DemuxerAndroid>(demuxer_), 180 GURL()), 181 decoder_callback_hook_executed_(false), 182 surface_texture_a_is_next_(true) {} 183 virtual ~MediaSourcePlayerTest() {} 184 185 protected: 186 // Get the decoder job from the MediaSourcePlayer. The return value must not 187 // be NULL. 188 MediaDecoderJob* GetMediaDecoderJob(bool is_audio) { 189 if (is_audio) { 190 return reinterpret_cast<MediaDecoderJob*>( 191 player_.audio_decoder_job_.get()); 192 } 193 return reinterpret_cast<MediaDecoderJob*>( 194 player_.video_decoder_job_.get()); 195 } 196 197 // Get the MediaCodecBridge from the decoder job. The return value could be 198 // NULL if the decoder is not yet created. 199 MediaCodecBridge* GetMediaCodecBridge(bool is_audio) { 200 if (is_audio) 201 return player_.audio_decoder_job_->media_codec_bridge_.get(); 202 return player_.video_decoder_job_->media_codec_bridge_.get(); 203 } 204 205 // Get the per-job prerolling status from the MediaSourcePlayer's job matching 206 // |is_audio|. Caller must guard against NPE if the player's job is NULL. 207 bool IsPrerolling(bool is_audio) { 208 return GetMediaDecoderJob(is_audio)->prerolling_; 209 } 210 211 // Get the preroll timestamp from the MediaSourcePlayer. 212 base::TimeDelta GetPrerollTimestamp() { 213 return player_.preroll_timestamp_; 214 } 215 216 // Simulate player has reached starvation timeout. 217 void TriggerPlayerStarvation() { 218 player_.decoder_starvation_callback_.Cancel(); 219 player_.OnDecoderStarved(); 220 } 221 222 // Release() the player. 223 void ReleasePlayer() { 224 EXPECT_TRUE(player_.IsPlaying()); 225 player_.Release(); 226 EXPECT_FALSE(player_.IsPlaying()); 227 } 228 229 // Upon the next successful decode callback, post a task to call Release() 230 // on the |player_|. TEST_F's do not have access to the private player 231 // members, hence this helper method. 232 // Prevent usage creep of MSP::set_decode_callback_for_testing() by 233 // only using it for the ReleaseWithOnPrefetchDoneAlreadyPosted test. 234 void OnNextTestDecodeCallbackPostTaskToReleasePlayer() { 235 DCHECK_EQ(&message_loop_, base::MessageLoop::current()); 236 player_.set_decode_callback_for_testing(media::BindToCurrentLoop( 237 base::Bind( 238 &MediaSourcePlayerTest::ReleaseWithPendingPrefetchDoneVerification, 239 base::Unretained(this)))); 240 } 241 242 // Asynch test callback posted upon decode completion to verify that a pending 243 // prefetch done event is not cleared across |player_|'s Release(). This helps 244 // ensure the ReleaseWithOnPrefetchDoneAlreadyPosted test scenario is met. 245 void ReleaseWithPendingPrefetchDoneVerification() { 246 EXPECT_TRUE(player_.IsEventPending(player_.PREFETCH_DONE_EVENT_PENDING)); 247 ReleasePlayer(); 248 EXPECT_TRUE(player_.IsEventPending(player_.PREFETCH_DONE_EVENT_PENDING)); 249 EXPECT_FALSE(decoder_callback_hook_executed_); 250 EXPECT_FALSE(GetMediaCodecBridge(true)); 251 decoder_callback_hook_executed_ = true; 252 } 253 254 DemuxerConfigs CreateAudioDemuxerConfigs(AudioCodec audio_codec, 255 bool use_low_sample_rate) { 256 DemuxerConfigs configs; 257 configs.audio_codec = audio_codec; 258 configs.audio_channels = 2; 259 configs.is_audio_encrypted = false; 260 configs.duration = kDefaultDuration; 261 262 if (audio_codec == kCodecVorbis) { 263 configs.audio_sampling_rate = use_low_sample_rate ? 11025 : 44100; 264 scoped_refptr<DecoderBuffer> buffer = ReadTestDataFile( 265 "vorbis-extradata"); 266 configs.audio_extra_data = std::vector<uint8>( 267 buffer->data(), 268 buffer->data() + buffer->data_size()); 269 return configs; 270 } 271 272 // Other codecs are not yet supported by this helper. 273 EXPECT_EQ(audio_codec, kCodecAAC); 274 275 configs.audio_sampling_rate = 48000; 276 uint8 aac_extra_data[] = { 0x13, 0x10 }; 277 configs.audio_extra_data = std::vector<uint8>( 278 aac_extra_data, 279 aac_extra_data + 2); 280 return configs; 281 } 282 283 DemuxerConfigs CreateVideoDemuxerConfigs(bool use_larger_size) { 284 DemuxerConfigs configs; 285 configs.video_codec = kCodecVP8; 286 configs.video_size = 287 use_larger_size ? gfx::Size(640, 480) : gfx::Size(320, 240); 288 configs.is_video_encrypted = false; 289 configs.duration = kDefaultDuration; 290 return configs; 291 } 292 293 DemuxerConfigs CreateAudioVideoDemuxerConfigs() { 294 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, false); 295 configs.video_codec = kCodecVP8; 296 configs.video_size = gfx::Size(320, 240); 297 configs.is_video_encrypted = false; 298 return configs; 299 } 300 301 DemuxerConfigs CreateDemuxerConfigs(bool have_audio, bool have_video) { 302 DCHECK(have_audio || have_video); 303 304 if (have_audio && !have_video) 305 return CreateAudioDemuxerConfigs(kCodecVorbis, false); 306 307 if (have_video && !have_audio) 308 return CreateVideoDemuxerConfigs(false); 309 310 return CreateAudioVideoDemuxerConfigs(); 311 } 312 313 // Starts an audio decoder job. 314 void StartAudioDecoderJob() { 315 Start(CreateAudioDemuxerConfigs(kCodecVorbis, false)); 316 } 317 318 // Starts a video decoder job. 319 void StartVideoDecoderJob() { 320 Start(CreateVideoDemuxerConfigs(false)); 321 } 322 323 // Starts decoding the data. 324 void Start(const DemuxerConfigs& configs) { 325 EXPECT_EQ(demuxer_->num_data_requests(), 0); 326 player_.OnDemuxerConfigsAvailable(configs); 327 player_.Start(); 328 329 EXPECT_TRUE(player_.IsPlaying()); 330 int expected_num_requests = (player_.HasAudio() ? 1 : 0) + 331 (player_.HasVideo() ? 1 : 0); 332 EXPECT_EQ(expected_num_requests, demuxer_->num_data_requests()); 333 } 334 335 // Resumes decoding the data. Verifies player behavior relative to 336 // |expect_player_requests_audio_data| and 337 // |expect_player_requests_video_data|. 338 void Resume(bool expect_player_requests_audio_data, 339 bool expect_player_requests_video_data) { 340 EXPECT_FALSE(player_.IsPlaying()); 341 EXPECT_TRUE(player_.HasVideo() || player_.HasAudio()); 342 int original_num_data_requests = demuxer_->num_data_requests(); 343 int expected_request_delta = 344 (expect_player_requests_audio_data ? 1 : 0) + 345 (expect_player_requests_video_data ? 1 : 0); 346 347 player_.Start(); 348 349 EXPECT_TRUE(player_.IsPlaying()); 350 EXPECT_EQ(original_num_data_requests + expected_request_delta, 351 demuxer_->num_data_requests()); 352 } 353 354 // Keeps decoding audio data until the decoder starts to output samples. 355 // Gives up if no audio output after decoding 10 frames. 356 void DecodeAudioDataUntilOutputBecomesAvailable() { 357 EXPECT_TRUE(player_.IsPlaying()); 358 base::TimeDelta current_time = player_.GetCurrentTime(); 359 base::TimeDelta start_timestamp = current_time; 360 for (int i = 0; i < 10; ++i) { 361 manager_.ResetTimestampUpdated(); 362 player_.OnDemuxerDataAvailable( 363 CreateReadFromDemuxerAckForAudio(i > 3 ? 3 : i)); 364 WaitForAudioDecodeDone(); 365 base::TimeDelta new_current_time = player_.GetCurrentTime(); 366 EXPECT_LE(current_time.InMilliseconds(), 367 new_current_time.InMilliseconds()); 368 current_time = new_current_time; 369 if (manager_.timestamp_updated()) { 370 EXPECT_LT(start_timestamp.InMillisecondsF(), 371 new_current_time.InMillisecondsF()); 372 return; 373 } 374 } 375 EXPECT_TRUE(false); 376 } 377 378 AccessUnit CreateAccessUnitWithData(bool is_audio, int audio_packet_id) { 379 AccessUnit unit; 380 381 unit.status = DemuxerStream::kOk; 382 scoped_refptr<DecoderBuffer> buffer = ReadTestDataFile( 383 is_audio ? base::StringPrintf("vorbis-packet-%d", audio_packet_id) 384 : "vp8-I-frame-320x240"); 385 unit.data = std::vector<uint8>( 386 buffer->data(), buffer->data() + buffer->data_size()); 387 388 if (is_audio) { 389 // Vorbis needs 4 extra bytes padding on Android to decode properly. Check 390 // NuMediaExtractor.cpp in Android source code. 391 uint8 padding[4] = { 0xff , 0xff , 0xff , 0xff }; 392 unit.data.insert(unit.data.end(), padding, padding + 4); 393 } 394 395 return unit; 396 } 397 398 DemuxerData CreateReadFromDemuxerAckForAudio(int packet_id) { 399 DemuxerData data; 400 data.type = DemuxerStream::AUDIO; 401 data.access_units.resize(1); 402 data.access_units[0] = CreateAccessUnitWithData(true, packet_id); 403 return data; 404 } 405 406 DemuxerData CreateReadFromDemuxerAckForVideo() { 407 DemuxerData data; 408 data.type = DemuxerStream::VIDEO; 409 data.access_units.resize(1); 410 data.access_units[0] = CreateAccessUnitWithData(false, 0); 411 return data; 412 } 413 414 DemuxerData CreateEOSAck(bool is_audio) { 415 DemuxerData data; 416 data.type = is_audio ? DemuxerStream::AUDIO : DemuxerStream::VIDEO; 417 data.access_units.resize(1); 418 data.access_units[0].status = DemuxerStream::kOk; 419 data.access_units[0].end_of_stream = true; 420 return data; 421 } 422 423 DemuxerData CreateAbortedAck(bool is_audio) { 424 DemuxerData data; 425 data.type = is_audio ? DemuxerStream::AUDIO : DemuxerStream::VIDEO; 426 data.access_units.resize(1); 427 data.access_units[0].status = DemuxerStream::kAborted; 428 return data; 429 } 430 431 // Helper method for use at test start. It starts an audio decoder job and 432 // immediately feeds it some data to decode. Then, without letting the decoder 433 // job complete a decode cycle, it also starts player SeekTo(). Upon return, 434 // the player should not yet have sent the DemuxerSeek IPC request, though 435 // seek event should be pending. The audio decoder job will also still be 436 // decoding. 437 void StartAudioDecoderJobAndSeekToWhileDecoding( 438 const base::TimeDelta& seek_time) { 439 EXPECT_FALSE(GetMediaCodecBridge(true)); 440 EXPECT_FALSE(player_.IsPlaying()); 441 EXPECT_EQ(0, demuxer_->num_data_requests()); 442 EXPECT_EQ(0.0, GetPrerollTimestamp().InMillisecondsF()); 443 EXPECT_EQ(player_.GetCurrentTime(), GetPrerollTimestamp()); 444 StartAudioDecoderJob(); 445 EXPECT_FALSE(GetMediaDecoderJob(true)->is_decoding()); 446 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0)); 447 EXPECT_EQ(2, demuxer_->num_data_requests()); 448 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding()); 449 player_.SeekTo(seek_time); 450 EXPECT_EQ(0.0, GetPrerollTimestamp().InMillisecondsF()); 451 EXPECT_EQ(0, demuxer_->num_seek_requests()); 452 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0)); 453 } 454 455 // Seek, including simulated receipt of |kAborted| read between SeekTo() and 456 // OnDemuxerSeekDone(). Use this helper method only when the player already 457 // has created the media codec bridge. Exactly one request for more data is 458 // expected following the seek, so use this helper for players with only audio 459 // or only video. 460 void SeekPlayerWithAbort(bool is_audio, const base::TimeDelta& seek_time) { 461 int original_num_seeks = demuxer_->num_seek_requests(); 462 int original_num_data_requests = demuxer_->num_data_requests(); 463 464 // Initiate a seek. Skip the round-trip of requesting seek from renderer. 465 // Instead behave as if the renderer has asked us to seek. 466 player_.SeekTo(seek_time); 467 468 // Verify that the seek does not occur until previously outstanding data 469 // request is satisfied. 470 EXPECT_EQ(original_num_seeks, demuxer_->num_seek_requests()); 471 472 // Simulate seeking causes the demuxer to abort the outstanding read 473 // caused by the seek. 474 player_.OnDemuxerDataAvailable(CreateAbortedAck(is_audio)); 475 476 // Wait for the decode job to finish so we can process the seek request. 477 WaitForDecodeDone(is_audio, !is_audio); 478 479 // Verify that the seek is requested. 480 EXPECT_EQ(original_num_seeks + 1, demuxer_->num_seek_requests()); 481 482 // Send back the seek done notification. This should trigger the player to 483 // call OnReadFromDemuxer() again. 484 EXPECT_EQ(original_num_data_requests, demuxer_->num_data_requests()); 485 player_.OnDemuxerSeekDone(kNoTimestamp()); 486 EXPECT_EQ(original_num_data_requests + 1, demuxer_->num_data_requests()); 487 488 // No other seek should have been requested. 489 EXPECT_EQ(original_num_seeks + 1, demuxer_->num_seek_requests()); 490 } 491 492 // Preroll the decoder job to |target_timestamp|. The first access unit 493 // to decode will have a timestamp equal to |start_timestamp|. 494 // TODO(qinmin): Add additional test cases for out-of-order decodes. 495 // See http://crbug.com/331421. 496 void PrerollDecoderToTime(bool is_audio, 497 const base::TimeDelta& start_timestamp, 498 const base::TimeDelta& target_timestamp) { 499 EXPECT_EQ(target_timestamp, player_.GetCurrentTime()); 500 // |start_timestamp| must be smaller than |target_timestamp|. 501 EXPECT_LE(start_timestamp, target_timestamp); 502 DemuxerData data = is_audio ? CreateReadFromDemuxerAckForAudio(1) : 503 CreateReadFromDemuxerAckForVideo(); 504 int current_timestamp = start_timestamp.InMilliseconds(); 505 506 // Send some data with access unit timestamps before the |target_timestamp|, 507 // and continue sending the data until preroll finishes. 508 // This simulates the common condition that AUs received after browser 509 // seek begin with timestamps before the seek target, and don't 510 // immediately complete preroll. 511 while (IsPrerolling(is_audio)) { 512 data.access_units[0].timestamp = 513 base::TimeDelta::FromMilliseconds(current_timestamp); 514 player_.OnDemuxerDataAvailable(data); 515 EXPECT_TRUE(GetMediaDecoderJob(is_audio)->is_decoding()); 516 EXPECT_TRUE(GetMediaCodecBridge(is_audio)); 517 EXPECT_EQ(target_timestamp, player_.GetCurrentTime()); 518 current_timestamp += 30; 519 WaitForDecodeDone(is_audio, !is_audio); 520 } 521 EXPECT_LE(target_timestamp, player_.GetCurrentTime()); 522 } 523 524 DemuxerData CreateReadFromDemuxerAckWithConfigChanged( 525 bool is_audio, 526 int config_unit_index, 527 const DemuxerConfigs& configs) { 528 DemuxerData data; 529 data.type = is_audio ? DemuxerStream::AUDIO : DemuxerStream::VIDEO; 530 data.access_units.resize(config_unit_index + 1); 531 532 for (int i = 0; i < config_unit_index; ++i) 533 data.access_units[i] = CreateAccessUnitWithData(is_audio, i); 534 535 data.access_units[config_unit_index].status = DemuxerStream::kConfigChanged; 536 data.demuxer_configs.resize(1); 537 data.demuxer_configs[0] = configs; 538 return data; 539 } 540 541 // Valid only for video-only player tests. If |trigger_with_release_start| is 542 // true, triggers the browser seek with a Release() + video data received + 543 // Start() with a new surface. If false, triggers the browser seek by 544 // setting a new video surface after beginning decode of received video data. 545 // Such data receipt causes possibility that an I-frame is not next, and 546 // browser seek results once decode completes and surface change processing 547 // begins. 548 void BrowserSeekPlayer(bool trigger_with_release_start) { 549 int expected_num_data_requests = demuxer_->num_data_requests() + 2; 550 int expected_num_seek_requests = demuxer_->num_seek_requests(); 551 int expected_num_browser_seek_requests = 552 demuxer_->num_browser_seek_requests(); 553 554 CreateNextTextureAndSetVideoSurface(); 555 StartVideoDecoderJob(); 556 if (trigger_with_release_start) { 557 // Consume the first frame, so that the next VideoDecoderJob will not 558 // inherit the I-frame from the previous decoder. 559 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 560 ReleasePlayer(); 561 WaitForVideoDecodeDone(); 562 563 // Simulate demuxer's response to the video data request. The data will be 564 // passed to the next MediaCodecBridge. 565 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 566 EXPECT_FALSE(GetMediaCodecBridge(false)); 567 EXPECT_FALSE(player_.IsPlaying()); 568 EXPECT_EQ(expected_num_seek_requests, demuxer_->num_seek_requests()); 569 570 CreateNextTextureAndSetVideoSurface(); 571 Resume(false, false); 572 EXPECT_FALSE(GetMediaCodecBridge(false)); 573 574 // Run the message loop so that prefetch will complete. 575 while (expected_num_seek_requests == demuxer_->num_seek_requests()) 576 message_loop_.RunUntilIdle(); 577 } else { 578 // Simulate demuxer's response to the video data request. 579 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 580 581 // While the decoder is decoding, trigger a browser seek by changing 582 // surface. Demuxer does not know of browser seek in advance, so no 583 // |kAborted| data is required (though |kAborted| can certainly occur for 584 // any pending read in reality due to renderer preparing for a regular 585 // seek). 586 CreateNextTextureAndSetVideoSurface(); 587 588 // Browser seek should not begin until decoding has completed. 589 EXPECT_TRUE(GetMediaCodecBridge(false)); 590 EXPECT_EQ(expected_num_seek_requests, demuxer_->num_seek_requests()); 591 592 // Wait for the media codec bridge to finish decoding and be reset pending 593 // the browser seek. 594 WaitForVideoDecodeDone(); 595 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 596 } 597 598 // Only one browser seek should have been initiated, and no further data 599 // should have been requested. 600 expected_num_seek_requests++; 601 expected_num_browser_seek_requests++; 602 EXPECT_EQ(expected_num_seek_requests, demuxer_->num_seek_requests()); 603 EXPECT_EQ(expected_num_browser_seek_requests, 604 demuxer_->num_browser_seek_requests()); 605 EXPECT_EQ(expected_num_data_requests, demuxer_->num_data_requests()); 606 } 607 608 // Creates a new media codec bridge and feeds it data ending with a 609 // |kConfigChanged| access unit. If |config_unit_in_prefetch| is true, sends 610 // feeds the config change AU in response to the job's first read request 611 // (prefetch). If false, regular data is fed and decoded prior to feeding the 612 // config change AU in response to the second data request (after prefetch 613 // completed). |config_unit_index| controls which access unit is 614 // |kConfigChanged|. If |enable_adaptive_playback| is true, config change will 615 // not cause the decoder to recreate the media codec bridge. Otherwise, the 616 // decoder has to drain all its data before recreating the new codec. 617 void SendConfigChangeToDecoder(bool is_audio, 618 bool config_unit_in_prefetch, 619 int config_unit_index, 620 bool enable_adaptive_playback) { 621 EXPECT_FALSE(GetMediaCodecBridge(is_audio)); 622 if (is_audio) { 623 StartAudioDecoderJob(); 624 } else { 625 CreateNextTextureAndSetVideoSurface(); 626 StartVideoDecoderJob(); 627 } 628 629 int expected_num_data_requests = demuxer_->num_data_requests(); 630 // Feed and decode a standalone access unit so the player exits prefetch. 631 if (!config_unit_in_prefetch) { 632 if (is_audio) { 633 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0)); 634 } else { 635 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 636 EnableAdaptiveVideoPlayback(enable_adaptive_playback); 637 } 638 639 WaitForDecodeDone(is_audio, !is_audio); 640 641 // We should have completed the prefetch phase at this point. 642 expected_num_data_requests++; 643 EXPECT_EQ(expected_num_data_requests, demuxer_->num_data_requests()); 644 } 645 646 DemuxerConfigs configs = is_audio ? 647 CreateAudioDemuxerConfigs(kCodecAAC, false) : 648 CreateVideoDemuxerConfigs(true); 649 // Feed and decode access units with data for any units prior to 650 // |config_unit_index|, and a |kConfigChanged| unit at that index. 651 // Player should prepare to reconfigure the decoder job, and should request 652 // new demuxer configs. 653 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckWithConfigChanged( 654 is_audio, config_unit_index, configs)); 655 656 expected_num_data_requests++; 657 EXPECT_EQ(expected_num_data_requests, demuxer_->num_data_requests()); 658 if (is_audio) 659 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0)); 660 else 661 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 662 663 // If the adaptive playback setting was not passed to the MediaCodecBridge 664 // earlier, do it here. 665 if (config_unit_in_prefetch && !is_audio) 666 EnableAdaptiveVideoPlayback(enable_adaptive_playback); 667 } 668 669 // Send a config change to the decoder job and drain the decoder so that the 670 // config change is processed. 671 void StartConfigChange(bool is_audio, 672 bool config_unit_in_prefetch, 673 int config_unit_index, 674 bool enable_adaptive_playback) { 675 SendConfigChangeToDecoder(is_audio, config_unit_in_prefetch, 676 config_unit_index, enable_adaptive_playback); 677 678 EXPECT_EQ(!config_unit_in_prefetch && !enable_adaptive_playback && 679 config_unit_index == 0, IsDrainingDecoder(is_audio)); 680 int expected_num_data_requests = demuxer_->num_data_requests(); 681 // Run until decoder starts to request new data. 682 while (demuxer_->num_data_requests() == expected_num_data_requests) 683 message_loop_.RunUntilIdle(); 684 EXPECT_FALSE(IsDrainingDecoder(is_audio)); 685 } 686 687 void EnableAdaptiveVideoPlayback(bool enable) { 688 EXPECT_TRUE(GetMediaCodecBridge(false)); 689 static_cast<VideoCodecBridge*>(GetMediaCodecBridge(false))-> 690 set_adaptive_playback_supported_for_testing( 691 enable ? 1 : 0); 692 } 693 694 void CreateNextTextureAndSetVideoSurface() { 695 gfx::SurfaceTexture* surface_texture; 696 if (surface_texture_a_is_next_) { 697 surface_texture_a_ = gfx::SurfaceTexture::Create(next_texture_id_++); 698 surface_texture = surface_texture_a_.get(); 699 } else { 700 surface_texture_b_ = gfx::SurfaceTexture::Create(next_texture_id_++); 701 surface_texture = surface_texture_b_.get(); 702 } 703 704 surface_texture_a_is_next_ = !surface_texture_a_is_next_; 705 gfx::ScopedJavaSurface surface = gfx::ScopedJavaSurface(surface_texture); 706 player_.SetVideoSurface(surface.Pass()); 707 } 708 709 // Wait for one or both of the jobs to complete decoding. Media codec bridges 710 // are assumed to exist for any stream whose decode completion is awaited. 711 void WaitForDecodeDone(bool wait_for_audio, bool wait_for_video) { 712 DCHECK(wait_for_audio || wait_for_video); 713 while ((wait_for_audio && GetMediaCodecBridge(true) && 714 GetMediaDecoderJob(true)->HasData() && 715 GetMediaDecoderJob(true)->is_decoding()) || 716 (wait_for_video && GetMediaCodecBridge(false) && 717 GetMediaDecoderJob(false)->HasData() && 718 GetMediaDecoderJob(false)->is_decoding())) { 719 message_loop_.RunUntilIdle(); 720 } 721 } 722 723 void WaitForAudioDecodeDone() { 724 WaitForDecodeDone(true, false); 725 } 726 727 void WaitForVideoDecodeDone() { 728 WaitForDecodeDone(false, true); 729 } 730 731 void WaitForAudioVideoDecodeDone() { 732 WaitForDecodeDone(true, true); 733 } 734 735 // If |send_eos| is true, generates EOS for the stream corresponding to 736 // |eos_for_audio|. Verifies that playback completes and no further data 737 // is requested. 738 // If |send_eos| is false, then it is assumed that caller previously arranged 739 // for player to receive EOS for each stream, but the player has not yet 740 // decoded all of them. In this case, |eos_for_audio| is ignored. 741 void VerifyPlaybackCompletesOnEOSDecode(bool send_eos, bool eos_for_audio) { 742 int original_num_data_requests = demuxer_->num_data_requests(); 743 if (send_eos) 744 player_.OnDemuxerDataAvailable(CreateEOSAck(eos_for_audio)); 745 EXPECT_FALSE(manager_.playback_completed()); 746 message_loop_.Run(); 747 EXPECT_TRUE(manager_.playback_completed()); 748 EXPECT_EQ(original_num_data_requests, demuxer_->num_data_requests()); 749 } 750 751 void VerifyCompletedPlaybackResumesOnSeekPlusStart(bool have_audio, 752 bool have_video) { 753 DCHECK(have_audio || have_video); 754 755 EXPECT_TRUE(manager_.playback_completed()); 756 757 player_.SeekTo(base::TimeDelta()); 758 player_.OnDemuxerSeekDone(kNoTimestamp()); 759 Resume(have_audio, have_video); 760 } 761 762 // Starts the appropriate decoder jobs according to |have_audio| and 763 // |have_video|. Then starts seek during decode of EOS or non-EOS according to 764 // |eos_audio| and |eos_video|. Simulates seek completion and verifies that 765 // playback never completed. |eos_{audio,video}| is ignored if the 766 // corresponding |have_{audio,video}| is false. 767 void VerifySeekDuringEOSDecodePreventsPlaybackCompletion(bool have_audio, 768 bool have_video, 769 bool eos_audio, 770 bool eos_video) { 771 DCHECK(have_audio || have_video); 772 773 if (have_video) 774 CreateNextTextureAndSetVideoSurface(); 775 776 Start(CreateDemuxerConfigs(have_audio, have_video)); 777 778 if (have_audio) 779 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0)); 780 781 if (have_video) 782 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 783 784 // Run until more data is requested a number of times equal to the number of 785 // media types configured. Since prefetching may be in progress, we cannot 786 // reliably expect Run() to complete until we have sent demuxer data for all 787 // configured media types, above. 788 WaitForDecodeDone(have_audio, have_video); 789 790 // Simulate seek while decoding EOS or non-EOS for the appropriate 791 // stream(s). 792 if (have_audio) { 793 if (eos_audio) 794 player_.OnDemuxerDataAvailable(CreateEOSAck(true)); 795 else 796 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(1)); 797 } 798 799 if (have_video) { 800 if (eos_video) 801 player_.OnDemuxerDataAvailable(CreateEOSAck(false)); 802 else 803 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 804 } 805 806 player_.SeekTo(base::TimeDelta()); 807 EXPECT_EQ(0, demuxer_->num_seek_requests()); 808 WaitForDecodeDone(have_audio, have_video); 809 EXPECT_EQ(1, demuxer_->num_seek_requests()); 810 811 player_.OnDemuxerSeekDone(kNoTimestamp()); 812 EXPECT_FALSE(manager_.playback_completed()); 813 } 814 815 base::TimeTicks StartTimeTicks() { 816 return player_.start_time_ticks_; 817 } 818 819 bool IsRequestingDemuxerData(bool is_audio) { 820 return GetMediaDecoderJob(is_audio)->is_requesting_demuxer_data_; 821 } 822 823 bool IsDrainingDecoder(bool is_audio) { 824 return GetMediaDecoderJob(is_audio)->drain_decoder_; 825 } 826 827 base::MessageLoop message_loop_; 828 MockMediaPlayerManager manager_; 829 MockDemuxerAndroid* demuxer_; // Owned by |player_|. 830 MediaSourcePlayer player_; 831 832 // Track whether a possibly async decoder callback test hook has run. 833 bool decoder_callback_hook_executed_; 834 835 // We need to keep the surface texture while the decoder is actively decoding. 836 // Otherwise, it may trigger unexpected crashes on some devices. To switch 837 // surfaces, tests need to create a new surface texture without releasing 838 // their previous one. In CreateNextTextureAndSetVideoSurface(), we toggle 839 // between two surface textures, only replacing the N-2 texture. Assumption is 840 // that no more than N-1 texture is in use by decoder when 841 // CreateNextTextureAndSetVideoSurface() is called. 842 scoped_refptr<gfx::SurfaceTexture> surface_texture_a_; 843 scoped_refptr<gfx::SurfaceTexture> surface_texture_b_; 844 bool surface_texture_a_is_next_; 845 int next_texture_id_; 846 847 DISALLOW_COPY_AND_ASSIGN(MediaSourcePlayerTest); 848 }; 849 850 TEST_F(MediaSourcePlayerTest, StartAudioDecoderWithValidConfig) { 851 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 852 853 // Test audio codec will be created when valid configs and data are passed to 854 // the audio decoder job. 855 StartAudioDecoderJob(); 856 EXPECT_EQ(0, demuxer_->num_seek_requests()); 857 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0)); 858 EXPECT_TRUE(GetMediaCodecBridge(true)); 859 } 860 861 TEST_F(MediaSourcePlayerTest, StartAudioDecoderWithInvalidConfig) { 862 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 863 864 // Test audio decoder job will not be created when failed to start the codec. 865 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, false); 866 // Replace with invalid |audio_extra_data| 867 configs.audio_extra_data.clear(); 868 uint8 invalid_codec_data[] = { 0x00, 0xff, 0xff, 0xff, 0xff }; 869 configs.audio_extra_data.insert(configs.audio_extra_data.begin(), 870 invalid_codec_data, invalid_codec_data + 4); 871 Start(configs); 872 873 // Decoder is not created after data is received. 874 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0)); 875 EXPECT_FALSE(GetMediaCodecBridge(true)); 876 } 877 878 TEST_F(MediaSourcePlayerTest, StartVideoCodecWithValidSurface) { 879 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 880 881 // Test video codec will not be created until data is received. 882 StartVideoDecoderJob(); 883 884 // Set both an initial and a later video surface without receiving any 885 // demuxed data yet. 886 CreateNextTextureAndSetVideoSurface(); 887 EXPECT_FALSE(GetMediaCodecBridge(false)); 888 CreateNextTextureAndSetVideoSurface(); 889 EXPECT_FALSE(GetMediaCodecBridge(false)); 890 891 // No seeks, even on setting surface, should have occurred. (Browser seeks can 892 // occur on setting surface, but only after previously receiving video data.) 893 EXPECT_EQ(0, demuxer_->num_seek_requests()); 894 895 // Send the first input chunk and verify that decoder will be created. 896 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 897 EXPECT_TRUE(GetMediaCodecBridge(false)); 898 } 899 900 TEST_F(MediaSourcePlayerTest, StartVideoCodecWithInvalidSurface) { 901 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 902 903 // Test video codec will not be created when surface is invalid. 904 scoped_refptr<gfx::SurfaceTexture> surface_texture( 905 gfx::SurfaceTexture::Create(0)); 906 gfx::ScopedJavaSurface surface(surface_texture.get()); 907 StartVideoDecoderJob(); 908 909 // Release the surface texture. 910 surface_texture = NULL; 911 player_.SetVideoSurface(surface.Pass()); 912 913 // Player should not seek the demuxer on setting initial surface. 914 EXPECT_EQ(0, demuxer_->num_seek_requests()); 915 EXPECT_EQ(1, demuxer_->num_data_requests()); 916 917 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 918 EXPECT_FALSE(GetMediaCodecBridge(false)); 919 } 920 921 TEST_F(MediaSourcePlayerTest, ReadFromDemuxerAfterSeek) { 922 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 923 924 // Test decoder job will resend a ReadFromDemuxer request after seek. 925 StartAudioDecoderJob(); 926 SeekPlayerWithAbort(true, base::TimeDelta()); 927 } 928 929 TEST_F(MediaSourcePlayerTest, SetSurfaceWhileSeeking) { 930 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 931 932 // Test SetVideoSurface() will not cause an extra seek while the player is 933 // waiting for demuxer to indicate seek is done. 934 player_.OnDemuxerConfigsAvailable( 935 CreateVideoDemuxerConfigs(false)); 936 937 // Initiate a seek. Skip requesting element seek of renderer. 938 // Instead behave as if the renderer has asked us to seek. 939 player_.SeekTo(base::TimeDelta()); 940 EXPECT_EQ(1, demuxer_->num_seek_requests()); 941 942 CreateNextTextureAndSetVideoSurface(); 943 EXPECT_EQ(1, demuxer_->num_seek_requests()); 944 player_.Start(); 945 946 // Send the seek done notification. The player should start requesting data. 947 player_.OnDemuxerSeekDone(kNoTimestamp()); 948 EXPECT_FALSE(GetMediaCodecBridge(false)); 949 EXPECT_EQ(1, demuxer_->num_data_requests()); 950 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 951 EXPECT_TRUE(GetMediaCodecBridge(false)); 952 953 // Reconfirm exactly 1 seek request has been made of demuxer, and that it 954 // was not a browser seek request. 955 EXPECT_EQ(1, demuxer_->num_seek_requests()); 956 EXPECT_EQ(0, demuxer_->num_browser_seek_requests()); 957 } 958 959 TEST_F(MediaSourcePlayerTest, ChangeMultipleSurfaceWhileDecoding) { 960 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 961 962 // Test MediaSourcePlayer can switch multiple surfaces during decoding. 963 CreateNextTextureAndSetVideoSurface(); 964 StartVideoDecoderJob(); 965 EXPECT_EQ(0, demuxer_->num_seek_requests()); 966 967 // Send the first input chunk. 968 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 969 970 // While the decoder is decoding, change multiple surfaces. Pass an empty 971 // surface first. 972 gfx::ScopedJavaSurface empty_surface; 973 player_.SetVideoSurface(empty_surface.Pass()); 974 // Next, pass a new non-empty surface. 975 CreateNextTextureAndSetVideoSurface(); 976 977 // Wait for the media codec bridge to finish decoding and be reset pending a 978 // browser seek. 979 WaitForVideoDecodeDone(); 980 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 981 982 // Only one browser seek should have been initiated. No further data request 983 // should have been processed on |message_loop_| before surface change event 984 // became pending, above. 985 EXPECT_EQ(1, demuxer_->num_browser_seek_requests()); 986 EXPECT_EQ(2, demuxer_->num_data_requests()); 987 988 // Simulate browser seek is done and confirm player requests more data for new 989 // video codec. 990 player_.OnDemuxerSeekDone(player_.GetCurrentTime()); 991 EXPECT_FALSE(GetMediaCodecBridge(false)); 992 EXPECT_EQ(3, demuxer_->num_data_requests()); 993 EXPECT_EQ(1, demuxer_->num_seek_requests()); 994 995 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 996 EXPECT_TRUE(GetMediaCodecBridge(false)); 997 } 998 999 TEST_F(MediaSourcePlayerTest, SetEmptySurfaceAndStarveWhileDecoding) { 1000 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1001 1002 // Test player pauses if an empty surface is passed. 1003 CreateNextTextureAndSetVideoSurface(); 1004 StartVideoDecoderJob(); 1005 EXPECT_EQ(1, demuxer_->num_data_requests()); 1006 1007 // Send the first input chunk. 1008 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 1009 1010 // While the decoder is decoding, pass an empty surface. 1011 gfx::ScopedJavaSurface empty_surface; 1012 player_.SetVideoSurface(empty_surface.Pass()); 1013 // Let the player starve. However, it should not issue any new data request in 1014 // this case. 1015 TriggerPlayerStarvation(); 1016 // Wait for the media codec bridge to finish decoding and be reset. 1017 while (GetMediaDecoderJob(false)->is_decoding()) 1018 message_loop_.RunUntilIdle(); 1019 1020 // No further seek or data requests should have been received since the 1021 // surface is empty. 1022 EXPECT_EQ(0, demuxer_->num_browser_seek_requests()); 1023 EXPECT_EQ(2, demuxer_->num_data_requests()); 1024 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 1025 1026 // Playback resumes once a non-empty surface is passed. 1027 CreateNextTextureAndSetVideoSurface(); 1028 EXPECT_EQ(1, demuxer_->num_browser_seek_requests()); 1029 } 1030 1031 TEST_F(MediaSourcePlayerTest, ReleaseVideoDecoderResourcesWhileDecoding) { 1032 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1033 1034 // Test that if video decoder is released while decoding, the resources will 1035 // not be immediately released. 1036 CreateNextTextureAndSetVideoSurface(); 1037 StartVideoDecoderJob(); 1038 // No resource is requested since there is no data to decode. 1039 EXPECT_EQ(0, manager_.num_resources_requested()); 1040 ReleasePlayer(); 1041 EXPECT_EQ(0, manager_.num_resources_released()); 1042 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 1043 1044 // Recreate the video decoder. 1045 CreateNextTextureAndSetVideoSurface(); 1046 player_.Start(); 1047 while (!GetMediaDecoderJob(false)->is_decoding()) 1048 message_loop_.RunUntilIdle(); 1049 EXPECT_EQ(0, demuxer_->num_browser_seek_requests()); 1050 EXPECT_EQ(1, manager_.num_resources_requested()); 1051 ReleasePlayer(); 1052 // The resource is still held by the video decoder until it finishes decoding. 1053 EXPECT_EQ(0, manager_.num_resources_released()); 1054 // Wait for the media codec bridge to finish decoding and be reset. 1055 while (manager_.num_resources_released() != 1) 1056 message_loop_.RunUntilIdle(); 1057 } 1058 1059 TEST_F(MediaSourcePlayerTest, AudioOnlyStartAfterSeekFinish) { 1060 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1061 1062 // Test audio decoder job will not start until pending seek event is handled. 1063 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, false); 1064 player_.OnDemuxerConfigsAvailable(configs); 1065 1066 // Initiate a seek. Skip requesting element seek of renderer. 1067 // Instead behave as if the renderer has asked us to seek. 1068 player_.SeekTo(base::TimeDelta()); 1069 EXPECT_EQ(1, demuxer_->num_seek_requests()); 1070 1071 player_.Start(); 1072 EXPECT_EQ(0, demuxer_->num_data_requests()); 1073 1074 // Sending back the seek done notification. 1075 player_.OnDemuxerSeekDone(kNoTimestamp()); 1076 EXPECT_FALSE(GetMediaCodecBridge(true)); 1077 EXPECT_EQ(1, demuxer_->num_data_requests()); 1078 1079 // Reconfirm exactly 1 seek request has been made of demuxer. 1080 EXPECT_EQ(1, demuxer_->num_seek_requests()); 1081 1082 // Decoder is created after data is received. 1083 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0)); 1084 EXPECT_TRUE(GetMediaCodecBridge(true)); 1085 } 1086 1087 TEST_F(MediaSourcePlayerTest, VideoOnlyStartAfterSeekFinish) { 1088 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1089 1090 // Test video decoder job will not start until pending seek event is handled. 1091 CreateNextTextureAndSetVideoSurface(); 1092 DemuxerConfigs configs = CreateVideoDemuxerConfigs(false); 1093 player_.OnDemuxerConfigsAvailable(configs); 1094 1095 // Initiate a seek. Skip requesting element seek of renderer. 1096 // Instead behave as if the renderer has asked us to seek. 1097 player_.SeekTo(base::TimeDelta()); 1098 EXPECT_EQ(1, demuxer_->num_seek_requests()); 1099 1100 player_.Start(); 1101 EXPECT_EQ(0, demuxer_->num_data_requests()); 1102 1103 // Sending back the seek done notification. 1104 player_.OnDemuxerSeekDone(kNoTimestamp()); 1105 EXPECT_FALSE(GetMediaCodecBridge(false)); 1106 EXPECT_EQ(1, demuxer_->num_data_requests()); 1107 1108 // Reconfirm exactly 1 seek request has been made of demuxer. 1109 EXPECT_EQ(1, demuxer_->num_seek_requests()); 1110 1111 // Decoder is created after data is received. 1112 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 1113 EXPECT_TRUE(GetMediaCodecBridge(false)); 1114 } 1115 1116 TEST_F(MediaSourcePlayerTest, StartImmediatelyAfterPause) { 1117 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1118 1119 // Test that if the decoding job is not fully stopped after Pause(), 1120 // calling Start() will be a noop. 1121 StartAudioDecoderJob(); 1122 1123 MediaDecoderJob* decoder_job = GetMediaDecoderJob(true); 1124 EXPECT_FALSE(GetMediaDecoderJob(true)->is_decoding()); 1125 1126 // Sending data to player. 1127 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0)); 1128 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding()); 1129 EXPECT_EQ(2, demuxer_->num_data_requests()); 1130 1131 // Decoder job will not immediately stop after Pause() since it is 1132 // running on another thread. 1133 player_.Pause(true); 1134 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding()); 1135 1136 // Nothing happens when calling Start() again. 1137 player_.Start(); 1138 // Verify that Start() will not destroy and recreate the media codec bridge. 1139 EXPECT_EQ(decoder_job, GetMediaDecoderJob(true)); 1140 1141 while (GetMediaDecoderJob(true)->is_decoding()) 1142 message_loop_.RunUntilIdle(); 1143 // The decoder job should finish and wait for data. 1144 EXPECT_EQ(2, demuxer_->num_data_requests()); 1145 EXPECT_TRUE(IsRequestingDemuxerData(true)); 1146 } 1147 1148 TEST_F(MediaSourcePlayerTest, DecoderJobsCannotStartWithoutAudio) { 1149 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1150 1151 // Test that when Start() is called, video decoder job will wait for audio 1152 // decoder job before start decoding the data. 1153 CreateNextTextureAndSetVideoSurface(); 1154 Start(CreateAudioVideoDemuxerConfigs()); 1155 MediaDecoderJob* audio_decoder_job = GetMediaDecoderJob(true); 1156 MediaDecoderJob* video_decoder_job = GetMediaDecoderJob(false); 1157 1158 EXPECT_FALSE(audio_decoder_job->is_decoding()); 1159 EXPECT_FALSE(video_decoder_job->is_decoding()); 1160 1161 // Sending video data to player, video decoder should not start. 1162 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 1163 EXPECT_FALSE(video_decoder_job->is_decoding()); 1164 1165 // Sending audio data to player, both decoders should start now. 1166 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0)); 1167 EXPECT_TRUE(audio_decoder_job->is_decoding()); 1168 EXPECT_TRUE(video_decoder_job->is_decoding()); 1169 1170 // No seeks should have occurred. 1171 EXPECT_EQ(0, demuxer_->num_seek_requests()); 1172 } 1173 1174 TEST_F(MediaSourcePlayerTest, StartTimeTicksResetAfterDecoderUnderruns) { 1175 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1176 1177 // Test start time ticks will reset after decoder job underruns. 1178 StartAudioDecoderJob(); 1179 1180 DecodeAudioDataUntilOutputBecomesAvailable(); 1181 1182 // The decoder job should finish and a new request will be sent. 1183 base::TimeTicks previous = StartTimeTicks(); 1184 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(3)); 1185 1186 // Let the decoder starve. 1187 TriggerPlayerStarvation(); 1188 WaitForAudioDecodeDone(); 1189 EXPECT_TRUE(StartTimeTicks() == previous); 1190 1191 // Send new data to the decoder so it can finish prefetching. This should 1192 // reset the start time ticks. 1193 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(3)); 1194 EXPECT_TRUE(StartTimeTicks() != previous); 1195 1196 base::TimeTicks current = StartTimeTicks(); 1197 EXPECT_LE(0, (current - previous).InMillisecondsF()); 1198 } 1199 1200 TEST_F(MediaSourcePlayerTest, V_SecondAccessUnitIsEOSAndResumePlayAfterSeek) { 1201 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1202 1203 // Test MediaSourcePlayer can replay video after input EOS is reached. 1204 CreateNextTextureAndSetVideoSurface(); 1205 StartVideoDecoderJob(); 1206 1207 // Send the first input chunk. 1208 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 1209 WaitForVideoDecodeDone(); 1210 1211 VerifyPlaybackCompletesOnEOSDecode(true, false); 1212 VerifyCompletedPlaybackResumesOnSeekPlusStart(false, true); 1213 } 1214 1215 TEST_F(MediaSourcePlayerTest, A_FirstAccessUnitIsEOSAndResumePlayAfterSeek) { 1216 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1217 1218 // Test decode of audio EOS buffer without any prior decode. See also 1219 // http://b/11696552. 1220 // Also tests that seeking+Start() after completing audio playback resumes 1221 // playback. 1222 Start(CreateAudioDemuxerConfigs(kCodecAAC, false)); 1223 VerifyPlaybackCompletesOnEOSDecode(true, true); 1224 VerifyCompletedPlaybackResumesOnSeekPlusStart(true, false); 1225 } 1226 1227 TEST_F(MediaSourcePlayerTest, V_FirstAccessUnitAfterSeekIsEOS) { 1228 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1229 1230 // Test decode of video EOS buffer, just after seeking, without any prior 1231 // decode (other than the simulated |kAborted| resulting from the seek 1232 // process.) 1233 CreateNextTextureAndSetVideoSurface(); 1234 StartVideoDecoderJob(); 1235 SeekPlayerWithAbort(false, base::TimeDelta()); 1236 VerifyPlaybackCompletesOnEOSDecode(true, false); 1237 } 1238 1239 TEST_F(MediaSourcePlayerTest, A_FirstAccessUnitAfterSeekIsEOS) { 1240 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1241 1242 // Test decode of audio EOS buffer, just after seeking, without any prior 1243 // decode (other than the simulated |kAborted| resulting from the seek 1244 // process.) See also http://b/11696552. 1245 Start(CreateAudioDemuxerConfigs(kCodecAAC, false)); 1246 SeekPlayerWithAbort(true, base::TimeDelta()); 1247 VerifyPlaybackCompletesOnEOSDecode(true, true); 1248 } 1249 1250 TEST_F(MediaSourcePlayerTest, AV_PlaybackCompletionAcrossConfigChange) { 1251 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1252 1253 // Test that if one stream (audio) has completed decode of EOS and the other 1254 // stream (video) processes config change, that subsequent video EOS completes 1255 // A/V playback. 1256 // Also tests that seeking+Start() after completing playback resumes playback. 1257 CreateNextTextureAndSetVideoSurface(); 1258 Start(CreateAudioVideoDemuxerConfigs()); 1259 1260 player_.OnDemuxerDataAvailable(CreateEOSAck(true)); // Audio EOS 1261 DemuxerConfigs configs = CreateVideoDemuxerConfigs(true); 1262 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckWithConfigChanged( 1263 false, 0, configs)); // Video |kConfigChanged| as first unit. 1264 1265 WaitForAudioVideoDecodeDone(); 1266 1267 EXPECT_EQ(3, demuxer_->num_data_requests()); 1268 1269 // At no time after completing audio EOS decode, above, should the 1270 // audio decoder job resume decoding. Send and decode video EOS. 1271 VerifyPlaybackCompletesOnEOSDecode(true, false); 1272 VerifyCompletedPlaybackResumesOnSeekPlusStart(true, true); 1273 } 1274 1275 TEST_F(MediaSourcePlayerTest, VA_PlaybackCompletionAcrossConfigChange) { 1276 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1277 1278 // Test that if one stream (video) has completed decode of EOS and the other 1279 // stream (audio) processes config change, that subsequent audio EOS completes 1280 // A/V playback. 1281 // Also tests that seeking+Start() after completing playback resumes playback. 1282 CreateNextTextureAndSetVideoSurface(); 1283 Start(CreateAudioVideoDemuxerConfigs()); 1284 1285 player_.OnDemuxerDataAvailable(CreateEOSAck(false)); // Video EOS 1286 // Audio |kConfigChanged| as first unit. 1287 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckWithConfigChanged( 1288 true, 0, CreateAudioDemuxerConfigs(kCodecVorbis, false))); 1289 1290 WaitForAudioVideoDecodeDone(); 1291 1292 EXPECT_EQ(3, demuxer_->num_data_requests()); 1293 1294 // At no time after completing video EOS decode, above, should the 1295 // video decoder job resume decoding. Send and decode audio EOS. 1296 VerifyPlaybackCompletesOnEOSDecode(true, true); 1297 VerifyCompletedPlaybackResumesOnSeekPlusStart(true, true); 1298 } 1299 1300 TEST_F(MediaSourcePlayerTest, AV_NoPrefetchForFinishedVideoOnAudioStarvation) { 1301 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1302 1303 // Test that if one stream (video) has completed decode of EOS, prefetch 1304 // resulting from player starvation occurs only for the other stream (audio), 1305 // and responding to that prefetch with EOS completes A/V playback, even if 1306 // another starvation occurs during the latter EOS's decode. 1307 CreateNextTextureAndSetVideoSurface(); 1308 Start(CreateAudioVideoDemuxerConfigs()); 1309 1310 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0)); 1311 player_.OnDemuxerDataAvailable(CreateEOSAck(false)); // Video EOS 1312 1313 // Wait until video EOS is processed and more data (assumed to be audio) is 1314 // requested. 1315 WaitForAudioVideoDecodeDone(); 1316 EXPECT_EQ(3, demuxer_->num_data_requests()); 1317 1318 // Simulate decoder underrun to trigger prefetch while still decoding audio. 1319 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(1)); 1320 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding() && 1321 !GetMediaDecoderJob(false)->is_decoding()); 1322 TriggerPlayerStarvation(); 1323 1324 // Complete the audio decode that was in progress when simulated player 1325 // starvation was triggered. 1326 WaitForAudioDecodeDone(); 1327 EXPECT_EQ(4, demuxer_->num_data_requests()); 1328 player_.OnDemuxerDataAvailable(CreateEOSAck(true)); // Audio EOS 1329 EXPECT_FALSE(GetMediaDecoderJob(false)->is_decoding()); 1330 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding()); 1331 1332 // Simulate another decoder underrun to trigger prefetch while decoding EOS. 1333 TriggerPlayerStarvation(); 1334 VerifyPlaybackCompletesOnEOSDecode(false, true /* ignored */); 1335 } 1336 1337 TEST_F(MediaSourcePlayerTest, V_StarvationDuringEOSDecode) { 1338 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1339 1340 // Test that video-only playback completes without further data requested when 1341 // starvation occurs during EOS decode. 1342 CreateNextTextureAndSetVideoSurface(); 1343 StartVideoDecoderJob(); 1344 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 1345 WaitForVideoDecodeDone(); 1346 1347 // Simulate decoder underrun to trigger prefetch while decoding EOS. 1348 player_.OnDemuxerDataAvailable(CreateEOSAck(false)); // Video EOS 1349 EXPECT_TRUE(GetMediaDecoderJob(false)->is_decoding()); 1350 TriggerPlayerStarvation(); 1351 VerifyPlaybackCompletesOnEOSDecode(false, false /* ignored */); 1352 } 1353 1354 TEST_F(MediaSourcePlayerTest, A_StarvationDuringEOSDecode) { 1355 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1356 1357 // Test that audio-only playback completes without further data requested when 1358 // starvation occurs during EOS decode. 1359 StartAudioDecoderJob(); 1360 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0)); 1361 WaitForAudioDecodeDone(); 1362 1363 // Simulate decoder underrun to trigger prefetch while decoding EOS. 1364 player_.OnDemuxerDataAvailable(CreateEOSAck(true)); // Audio EOS 1365 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding()); 1366 TriggerPlayerStarvation(); 1367 VerifyPlaybackCompletesOnEOSDecode(false, true /* ignored */); 1368 } 1369 1370 TEST_F(MediaSourcePlayerTest, AV_SeekDuringEOSDecodePreventsCompletion) { 1371 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1372 1373 // Test that seek supercedes audio+video playback completion on simultaneous 1374 // audio and video EOS decode, if SeekTo() occurs during these EOS decodes. 1375 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, true, true, true); 1376 } 1377 1378 TEST_F(MediaSourcePlayerTest, AV_SeekDuringAudioEOSDecodePreventsCompletion) { 1379 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1380 1381 // Test that seek supercedes audio+video playback completion on simultaneous 1382 // audio EOS and video non-EOS decode, if SeekTo() occurs during these 1383 // decodes. 1384 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, true, true, false); 1385 } 1386 1387 TEST_F(MediaSourcePlayerTest, AV_SeekDuringVideoEOSDecodePreventsCompletion) { 1388 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1389 1390 // Test that seek supercedes audio+video playback completion on simultaneous 1391 // audio non-EOS and video EOS decode, if SeekTo() occurs during these 1392 // decodes. 1393 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, true, false, true); 1394 } 1395 1396 TEST_F(MediaSourcePlayerTest, V_SeekDuringEOSDecodePreventsCompletion) { 1397 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1398 1399 // Test that seek supercedes video-only playback completion on EOS decode, if 1400 // SeekTo() occurs during EOS decode. 1401 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(false, true, false, true); 1402 } 1403 1404 TEST_F(MediaSourcePlayerTest, A_SeekDuringEOSDecodePreventsCompletion) { 1405 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1406 1407 // Test that seek supercedes audio-only playback completion on EOS decode, if 1408 // SeekTo() occurs during EOS decode. 1409 VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, false, true, false); 1410 } 1411 1412 TEST_F(MediaSourcePlayerTest, NoRequestForDataAfterAbort) { 1413 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1414 1415 // Test that the decoder will not request new data after receiving an aborted 1416 // access unit. 1417 StartAudioDecoderJob(); 1418 1419 // Send an aborted access unit. 1420 player_.OnDemuxerDataAvailable(CreateAbortedAck(true)); 1421 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding()); 1422 WaitForAudioDecodeDone(); 1423 1424 // No request will be sent for new data. 1425 EXPECT_EQ(1, demuxer_->num_data_requests()); 1426 1427 // No seek requests should have occurred. 1428 EXPECT_EQ(0, demuxer_->num_seek_requests()); 1429 } 1430 1431 TEST_F(MediaSourcePlayerTest, DemuxerDataArrivesAfterRelease) { 1432 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1433 1434 // Test that the decoder should not crash if demuxer data arrives after 1435 // Release(). 1436 StartAudioDecoderJob(); 1437 1438 ReleasePlayer(); 1439 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0)); 1440 1441 // The media codec bridge should have been released. 1442 EXPECT_FALSE(player_.IsPlaying()); 1443 1444 // No further data should have been requested. 1445 EXPECT_EQ(1, demuxer_->num_data_requests()); 1446 1447 // No seek requests should have occurred. 1448 EXPECT_EQ(0, demuxer_->num_seek_requests()); 1449 } 1450 1451 TEST_F(MediaSourcePlayerTest, BrowserSeek_RegularSeekPendsBrowserSeekDone) { 1452 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1453 1454 // Test that a browser seek, once started, delays a newly arrived regular 1455 // SeekTo() request's demuxer seek until the browser seek is done. 1456 BrowserSeekPlayer(false); 1457 1458 // Simulate renderer requesting a regular seek while browser seek in progress. 1459 player_.SeekTo(base::TimeDelta()); 1460 1461 // Simulate browser seek is done. Confirm player requests the regular seek, 1462 // still has no video codec configured, and has not requested any 1463 // further data since the surface change event became pending in 1464 // BrowserSeekPlayer(). 1465 EXPECT_EQ(1, demuxer_->num_seek_requests()); 1466 player_.OnDemuxerSeekDone(base::TimeDelta()); 1467 EXPECT_EQ(2, demuxer_->num_seek_requests()); 1468 EXPECT_EQ(1, demuxer_->num_browser_seek_requests()); 1469 1470 // Simulate regular seek is done and confirm player requests more data for 1471 // new video codec. 1472 player_.OnDemuxerSeekDone(kNoTimestamp()); 1473 EXPECT_FALSE(GetMediaCodecBridge(false)); 1474 EXPECT_EQ(3, demuxer_->num_data_requests()); 1475 EXPECT_EQ(2, demuxer_->num_seek_requests()); 1476 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 1477 EXPECT_TRUE(GetMediaCodecBridge(false)); 1478 } 1479 1480 TEST_F(MediaSourcePlayerTest, BrowserSeek_InitialReleaseAndStart) { 1481 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1482 1483 // Test that no browser seek is requested if player Release() + Start() occurs 1484 // prior to receiving any data. 1485 CreateNextTextureAndSetVideoSurface(); 1486 StartVideoDecoderJob(); 1487 ReleasePlayer(); 1488 1489 // Pass a new non-empty surface. 1490 CreateNextTextureAndSetVideoSurface(); 1491 1492 player_.Start(); 1493 1494 // No data request is issued since there is still one pending. 1495 EXPECT_EQ(1, demuxer_->num_data_requests()); 1496 EXPECT_FALSE(GetMediaCodecBridge(false)); 1497 1498 // No browser seek is needed. 1499 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 1500 EXPECT_EQ(0, demuxer_->num_browser_seek_requests()); 1501 EXPECT_EQ(2, demuxer_->num_data_requests()); 1502 } 1503 1504 TEST_F(MediaSourcePlayerTest, BrowserSeek_MidStreamReleaseAndStart) { 1505 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1506 1507 // Test that one browser seek is requested if player Release() + Start(), with 1508 // video data received between Release() and Start(). 1509 BrowserSeekPlayer(true); 1510 1511 // Simulate browser seek is done and confirm player requests more data. 1512 player_.OnDemuxerSeekDone(base::TimeDelta()); 1513 EXPECT_EQ(3, demuxer_->num_data_requests()); 1514 EXPECT_EQ(1, demuxer_->num_seek_requests()); 1515 } 1516 1517 TEST_F(MediaSourcePlayerTest, PrerollAudioAfterSeek) { 1518 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1519 1520 // Test decoder job will preroll the media to the seek position. 1521 StartAudioDecoderJob(); 1522 1523 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100)); 1524 EXPECT_TRUE(IsPrerolling(true)); 1525 PrerollDecoderToTime( 1526 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100)); 1527 } 1528 1529 TEST_F(MediaSourcePlayerTest, PrerollVideoAfterSeek) { 1530 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1531 1532 // Test decoder job will preroll the media to the seek position. 1533 CreateNextTextureAndSetVideoSurface(); 1534 StartVideoDecoderJob(); 1535 1536 SeekPlayerWithAbort(false, base::TimeDelta::FromMilliseconds(100)); 1537 EXPECT_TRUE(IsPrerolling(false)); 1538 PrerollDecoderToTime( 1539 false, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100)); 1540 } 1541 1542 TEST_F(MediaSourcePlayerTest, SeekingAfterCompletingPrerollRestartsPreroll) { 1543 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1544 1545 // Test decoder job will begin prerolling upon seek, when it was not 1546 // prerolling prior to the seek. 1547 StartAudioDecoderJob(); 1548 MediaDecoderJob* decoder_job = GetMediaDecoderJob(true); 1549 EXPECT_TRUE(IsPrerolling(true)); 1550 1551 // Complete the initial preroll by feeding data to the decoder. 1552 DecodeAudioDataUntilOutputBecomesAvailable(); 1553 EXPECT_FALSE(IsPrerolling(true)); 1554 1555 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(500)); 1556 1557 // Prerolling should have begun again. 1558 EXPECT_TRUE(IsPrerolling(true)); 1559 EXPECT_EQ(500.0, GetPrerollTimestamp().InMillisecondsF()); 1560 1561 // Send data at and after the seek position. Prerolling should complete. 1562 for (int i = 0; i < 4; ++i) { 1563 DemuxerData data = CreateReadFromDemuxerAckForAudio(i); 1564 data.access_units[0].timestamp = base::TimeDelta::FromMilliseconds( 1565 500 + 30 * (i - 1)); 1566 player_.OnDemuxerDataAvailable(data); 1567 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding()); 1568 WaitForAudioDecodeDone(); 1569 } 1570 EXPECT_LT(500.0, player_.GetCurrentTime().InMillisecondsF()); 1571 EXPECT_FALSE(IsPrerolling(true)); 1572 1573 // Throughout this test, we should have not re-created the media codec bridge, 1574 // so IsPrerolling() transition from false to true was not due to constructor 1575 // initialization. It was due to BeginPrerolling(). 1576 EXPECT_EQ(decoder_job, GetMediaDecoderJob(true)); 1577 } 1578 1579 TEST_F(MediaSourcePlayerTest, PrerollContinuesAcrossReleaseAndStart) { 1580 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1581 1582 // Test decoder job will resume media prerolling if interrupted by Release() 1583 // and Start(). 1584 StartAudioDecoderJob(); 1585 1586 base::TimeDelta target_timestamp = base::TimeDelta::FromMilliseconds(100); 1587 SeekPlayerWithAbort(true, target_timestamp); 1588 EXPECT_TRUE(IsPrerolling(true)); 1589 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF()); 1590 1591 // Send some data before the seek position. 1592 // Test uses 'large' number of iterations because decoder job may not get 1593 // MEDIA_CODEC_OK output status until after a few dequeue output attempts. 1594 // This allows decoder status to stabilize prior to AU timestamp reaching 1595 // the preroll target. 1596 DemuxerData data; 1597 for (int i = 0; i < 10; ++i) { 1598 data = CreateReadFromDemuxerAckForAudio(3); 1599 data.access_units[0].timestamp = base::TimeDelta::FromMilliseconds(i * 10); 1600 if (i == 1) { 1601 // While still prerolling, Release() and Start() the player. 1602 ReleasePlayer(); 1603 // The decoder is still decoding and will not be immediately released. 1604 EXPECT_TRUE(GetMediaCodecBridge(true)); 1605 Resume(false, false); 1606 } else { 1607 player_.OnDemuxerDataAvailable(data); 1608 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding()); 1609 WaitForAudioDecodeDone(); 1610 } 1611 EXPECT_TRUE(IsPrerolling(true)); 1612 } 1613 EXPECT_EQ(100.0, player_.GetCurrentTime().InMillisecondsF()); 1614 EXPECT_TRUE(IsPrerolling(true)); 1615 1616 // Send data after the seek position. 1617 PrerollDecoderToTime(true, target_timestamp, target_timestamp); 1618 } 1619 1620 TEST_F(MediaSourcePlayerTest, PrerollContinuesAcrossConfigChange) { 1621 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1622 1623 // Test decoder job will resume media prerolling if interrupted by 1624 // |kConfigChanged| and OnDemuxerConfigsAvailable(). 1625 StartAudioDecoderJob(); 1626 1627 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100)); 1628 EXPECT_TRUE(IsPrerolling(true)); 1629 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF()); 1630 1631 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, true); 1632 1633 // In response to data request, simulate that demuxer signals config change by 1634 // sending an AU with |kConfigChanged|. 1635 DemuxerData data = CreateReadFromDemuxerAckWithConfigChanged( 1636 true, 0, configs); 1637 player_.OnDemuxerDataAvailable(data); 1638 PrerollDecoderToTime( 1639 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100)); 1640 } 1641 1642 TEST_F(MediaSourcePlayerTest, PrerollContinuesAfterUnchangedConfigs) { 1643 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1644 1645 // Test decoder job will resume media prerolling if interrupted by a config 1646 // change access unit with unchanged configs. 1647 StartAudioDecoderJob(); 1648 1649 SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100)); 1650 EXPECT_TRUE(IsPrerolling(true)); 1651 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF()); 1652 1653 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, false); 1654 1655 // In response to data request, simulate that demuxer signals config change by 1656 // sending an AU with |kConfigChanged|. 1657 DemuxerData data = CreateReadFromDemuxerAckWithConfigChanged( 1658 true, 0, configs); 1659 player_.OnDemuxerDataAvailable(data); 1660 PrerollDecoderToTime( 1661 true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100)); 1662 } 1663 1664 TEST_F(MediaSourcePlayerTest, SimultaneousAudioVideoConfigChange) { 1665 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1666 1667 // Test that the player allows simultaneous audio and video config change, 1668 // such as might occur during OnPrefetchDone() if next access unit for both 1669 // audio and video jobs is |kConfigChanged|. 1670 CreateNextTextureAndSetVideoSurface(); 1671 Start(CreateAudioVideoDemuxerConfigs()); 1672 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0)); 1673 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 1674 EXPECT_TRUE(GetMediaCodecBridge(true)); 1675 EXPECT_TRUE(GetMediaCodecBridge(false)); 1676 EnableAdaptiveVideoPlayback(false); 1677 WaitForAudioVideoDecodeDone(); 1678 1679 // Simulate audio |kConfigChanged| prefetched as standalone access unit. 1680 DemuxerConfigs audio_configs = CreateAudioDemuxerConfigs(kCodecVorbis, true); 1681 player_.OnDemuxerDataAvailable( 1682 CreateReadFromDemuxerAckWithConfigChanged(true, 0, audio_configs)); 1683 1684 // Simulate video |kConfigChanged| prefetched as standalone access unit. 1685 player_.OnDemuxerDataAvailable( 1686 CreateReadFromDemuxerAckWithConfigChanged( 1687 false, 0, CreateVideoDemuxerConfigs(true))); 1688 EXPECT_EQ(6, demuxer_->num_data_requests()); 1689 EXPECT_TRUE(IsDrainingDecoder(true)); 1690 EXPECT_TRUE(IsDrainingDecoder(false)); 1691 1692 // Waiting for decoder to finish draining. 1693 while (IsDrainingDecoder(true) || IsDrainingDecoder(false)) 1694 message_loop_.RunUntilIdle(); 1695 } 1696 1697 TEST_F(MediaSourcePlayerTest, 1698 SimultaneousAudioVideoConfigChangeWithAdaptivePlayback) { 1699 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1700 1701 // Test that the player allows simultaneous audio and video config change with 1702 // adaptive video playback enabled. 1703 CreateNextTextureAndSetVideoSurface(); 1704 Start(CreateAudioVideoDemuxerConfigs()); 1705 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0)); 1706 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 1707 EXPECT_TRUE(GetMediaCodecBridge(true)); 1708 EXPECT_TRUE(GetMediaCodecBridge(false)); 1709 EnableAdaptiveVideoPlayback(true); 1710 WaitForAudioVideoDecodeDone(); 1711 1712 // Simulate audio |kConfigChanged| prefetched as standalone access unit. 1713 DemuxerConfigs audio_configs = CreateAudioDemuxerConfigs(kCodecVorbis, true); 1714 player_.OnDemuxerDataAvailable( 1715 CreateReadFromDemuxerAckWithConfigChanged(true, 0, audio_configs)); 1716 1717 // Simulate video |kConfigChanged| prefetched as standalone access unit. 1718 player_.OnDemuxerDataAvailable( 1719 CreateReadFromDemuxerAckWithConfigChanged( 1720 false, 0, CreateVideoDemuxerConfigs(true))); 1721 EXPECT_EQ(6, demuxer_->num_data_requests()); 1722 EXPECT_TRUE(IsDrainingDecoder(true)); 1723 EXPECT_FALSE(IsDrainingDecoder(false)); 1724 1725 // Waiting for audio decoder to finish draining. 1726 while (IsDrainingDecoder(true)) 1727 message_loop_.RunUntilIdle(); 1728 } 1729 1730 TEST_F(MediaSourcePlayerTest, DemuxerConfigRequestedIfInPrefetchUnit0) { 1731 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1732 1733 // Test that the player detects need for and requests demuxer configs if 1734 // the |kConfigChanged| unit is the very first unit in the set of units 1735 // received in OnDemuxerDataAvailable() ostensibly while 1736 // |PREFETCH_DONE_EVENT_PENDING|. 1737 StartConfigChange(true, true, 0, false); 1738 } 1739 1740 TEST_F(MediaSourcePlayerTest, DemuxerConfigRequestedIfInPrefetchUnit1) { 1741 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1742 1743 // Test that the player detects need for and requests demuxer configs if 1744 // the |kConfigChanged| unit is not the first unit in the set of units 1745 // received in OnDemuxerDataAvailable() ostensibly while 1746 // |PREFETCH_DONE_EVENT_PENDING|. 1747 StartConfigChange(true, true, 1, false); 1748 } 1749 1750 TEST_F(MediaSourcePlayerTest, DemuxerConfigRequestedIfInUnit0AfterPrefetch) { 1751 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1752 1753 // Test that the player detects need for and requests demuxer configs if 1754 // the |kConfigChanged| unit is the very first unit in the set of units 1755 // received in OnDemuxerDataAvailable() from data requested ostensibly while 1756 // not prefetching. 1757 StartConfigChange(true, false, 0, false); 1758 } 1759 1760 TEST_F(MediaSourcePlayerTest, DemuxerConfigRequestedIfInUnit1AfterPrefetch) { 1761 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1762 1763 // Test that the player detects need for and requests demuxer configs if 1764 // the |kConfigChanged| unit is not the first unit in the set of units 1765 // received in OnDemuxerDataAvailable() from data requested ostensibly while 1766 // not prefetching. 1767 StartConfigChange(true, false, 1, false); 1768 } 1769 1770 TEST_F(MediaSourcePlayerTest, BrowserSeek_PrerollAfterBrowserSeek) { 1771 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1772 1773 // Test decoder job will preroll the media to the actual seek position 1774 // resulting from a browser seek. 1775 BrowserSeekPlayer(false); 1776 1777 // Simulate browser seek is done, but to a later time than was requested. 1778 EXPECT_LT(player_.GetCurrentTime().InMillisecondsF(), 100); 1779 player_.OnDemuxerSeekDone(base::TimeDelta::FromMilliseconds(100)); 1780 // Because next AU is not I-frame, MediaCodecBridge will not be recreated. 1781 EXPECT_FALSE(GetMediaCodecBridge(false)); 1782 EXPECT_EQ(100.0, player_.GetCurrentTime().InMillisecondsF()); 1783 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF()); 1784 EXPECT_EQ(3, demuxer_->num_data_requests()); 1785 1786 PrerollDecoderToTime( 1787 false, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100)); 1788 } 1789 1790 TEST_F(MediaSourcePlayerTest, VideoDemuxerConfigChange) { 1791 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1792 1793 // Test that video config change notification results in creating a new 1794 // video codec without any browser seek. 1795 StartConfigChange(false, true, 1, false); 1796 1797 // New video codec should have been created and configured, without any 1798 // browser seek. 1799 EXPECT_TRUE(GetMediaCodecBridge(false)); 1800 EXPECT_EQ(3, demuxer_->num_data_requests()); 1801 EXPECT_EQ(0, demuxer_->num_seek_requests()); 1802 1803 // 2 codecs should have been created, one before the config change, and one 1804 // after it. 1805 EXPECT_EQ(2, manager_.num_resources_requested()); 1806 EXPECT_EQ(1, manager_.num_resources_released()); 1807 } 1808 1809 TEST_F(MediaSourcePlayerTest, VideoDemuxerConfigChangeWithAdaptivePlayback) { 1810 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1811 1812 // Test that if codec supports adaptive playback, no new codec should be 1813 // created beyond the one used to decode the prefetch media data prior to 1814 // the kConfigChanged. 1815 StartConfigChange(false, true, 1, true); 1816 1817 // No browser seek should be needed. 1818 EXPECT_TRUE(GetMediaCodecBridge(false)); 1819 EXPECT_EQ(3, demuxer_->num_data_requests()); 1820 EXPECT_EQ(0, demuxer_->num_seek_requests()); 1821 1822 // Only 1 codec should have been created so far. 1823 EXPECT_EQ(1, manager_.num_resources_requested()); 1824 } 1825 1826 TEST_F(MediaSourcePlayerTest, DecoderDrainInterruptedBySeek) { 1827 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1828 1829 // Test if a decoder is being drained while receiving a seek request, draining 1830 // is canceled. 1831 SendConfigChangeToDecoder(true, false, 0, false); 1832 EXPECT_TRUE(IsDrainingDecoder(true)); 1833 1834 player_.SeekTo(base::TimeDelta::FromMilliseconds(100)); 1835 WaitForAudioDecodeDone(); 1836 EXPECT_FALSE(IsDrainingDecoder(true)); 1837 player_.OnDemuxerSeekDone(kNoTimestamp()); 1838 1839 EXPECT_EQ(1, demuxer_->num_seek_requests()); 1840 EXPECT_EQ(4, demuxer_->num_data_requests()); 1841 } 1842 1843 TEST_F(MediaSourcePlayerTest, DecoderDrainInterruptedByRelease) { 1844 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1845 1846 // Test if a decoder is being drained while receiving a release request, 1847 // draining is canceled. 1848 SendConfigChangeToDecoder(true, false, 0, false); 1849 EXPECT_TRUE(IsDrainingDecoder(true)); 1850 1851 ReleasePlayer(); 1852 WaitForAudioDecodeDone(); 1853 EXPECT_EQ(3, demuxer_->num_data_requests()); 1854 EXPECT_FALSE(IsDrainingDecoder(true)); 1855 1856 EXPECT_FALSE(GetMediaCodecBridge(true)); 1857 EXPECT_FALSE(player_.IsPlaying()); 1858 1859 player_.Start(); 1860 EXPECT_TRUE(player_.IsPlaying()); 1861 EXPECT_EQ(3, demuxer_->num_data_requests()); 1862 } 1863 1864 TEST_F(MediaSourcePlayerTest, DecoderDrainInterruptedBySurfaceChange) { 1865 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1866 1867 // Test if a video decoder is being drained while surface changes, draining 1868 // is canceled. 1869 SendConfigChangeToDecoder(false, false, 0, false); 1870 EXPECT_TRUE(IsDrainingDecoder(false)); 1871 1872 CreateNextTextureAndSetVideoSurface(); 1873 WaitForVideoDecodeDone(); 1874 1875 EXPECT_FALSE(IsDrainingDecoder(false)); 1876 EXPECT_FALSE(GetMediaCodecBridge(false)); 1877 EXPECT_TRUE(player_.IsPlaying()); 1878 EXPECT_EQ(3, demuxer_->num_data_requests()); 1879 1880 // Finish the browser seek introduced by surface change. 1881 player_.OnDemuxerSeekDone(base::TimeDelta()); 1882 EXPECT_EQ(4, demuxer_->num_data_requests()); 1883 } 1884 1885 TEST_F(MediaSourcePlayerTest, 1886 BrowserSeek_DecoderStarvationWhilePendingSurfaceChange) { 1887 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1888 1889 // Test video decoder starvation while handling a pending surface change 1890 // should not cause any crashes. 1891 CreateNextTextureAndSetVideoSurface(); 1892 StartVideoDecoderJob(); 1893 DemuxerData data = CreateReadFromDemuxerAckForVideo(); 1894 player_.OnDemuxerDataAvailable(data); 1895 1896 // Trigger a surface change and decoder starvation. 1897 CreateNextTextureAndSetVideoSurface(); 1898 TriggerPlayerStarvation(); 1899 WaitForVideoDecodeDone(); 1900 EXPECT_EQ(0, demuxer_->num_browser_seek_requests()); 1901 1902 // Surface change should trigger a seek. 1903 player_.OnDemuxerDataAvailable(data); 1904 EXPECT_EQ(1, demuxer_->num_browser_seek_requests()); 1905 player_.OnDemuxerSeekDone(base::TimeDelta()); 1906 // After seek is done, prefetch is handled first. MediaCodecBridge is not 1907 // created at this moment. 1908 EXPECT_FALSE(GetMediaCodecBridge(false)); 1909 1910 // A new data request should be sent. 1911 EXPECT_EQ(3, demuxer_->num_data_requests()); 1912 } 1913 1914 TEST_F(MediaSourcePlayerTest, ReleaseWithOnPrefetchDoneAlreadyPosted) { 1915 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1916 1917 // Test if OnPrefetchDone() had already been posted before and is executed 1918 // after Release(), then player does not DCHECK. This test is fragile to 1919 // change to MediaDecoderJob::Prefetch() implementation; it assumes task 1920 // is posted to run |prefetch_cb| if the job already HasData(). 1921 // TODO(wolenetz): Remove MSP::set_decode_callback_for_testing() if this test 1922 // becomes obsolete. See http://crbug.com/304234. 1923 StartAudioDecoderJob(); 1924 1925 // Escape the original prefetch by decoding a single access unit. 1926 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0)); 1927 WaitForAudioDecodeDone(); 1928 1929 // Prime the job with a few more access units, so that a later prefetch, 1930 // triggered by starvation to simulate decoder underrun, can trivially 1931 // post task to run OnPrefetchDone(). 1932 player_.OnDemuxerDataAvailable( 1933 CreateReadFromDemuxerAckWithConfigChanged( 1934 true, 4, CreateAudioDemuxerConfigs(kCodecVorbis, false))); 1935 EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding()); 1936 1937 // Simulate decoder underrun, so trivial prefetch starts while still decoding. 1938 // The prefetch and posting of OnPrefetchDone() will not occur until next 1939 // MediaDecoderCallBack() occurs. 1940 TriggerPlayerStarvation(); 1941 1942 // Upon the next successful decode callback, post a task to call Release() on 1943 // the |player_|, such that the trivial OnPrefetchDone() task posting also 1944 // occurs and should execute after the Release(). 1945 OnNextTestDecodeCallbackPostTaskToReleasePlayer(); 1946 1947 WaitForAudioDecodeDone(); 1948 EXPECT_TRUE(decoder_callback_hook_executed_); 1949 1950 EXPECT_EQ(3, demuxer_->num_data_requests()); 1951 1952 // Player should not request any new data since the access units haven't 1953 // been fully decoded yet. 1954 Resume(false, false); 1955 } 1956 1957 TEST_F(MediaSourcePlayerTest, SeekToThenReleaseThenDemuxerSeekAndDone) { 1958 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1959 1960 // Test if Release() occurs after SeekTo(), but the DemuxerSeek IPC request 1961 // has not yet been sent, then the seek request is sent after Release(). Also, 1962 // test if OnDemuxerSeekDone() occurs prior to next Start(), then the player 1963 // will resume correct post-seek preroll upon Start(). 1964 StartAudioDecoderJobAndSeekToWhileDecoding( 1965 base::TimeDelta::FromMilliseconds(100)); 1966 ReleasePlayer(); 1967 EXPECT_EQ(0, demuxer_->num_seek_requests()); 1968 WaitForAudioDecodeDone(); 1969 EXPECT_EQ(1, demuxer_->num_seek_requests()); 1970 1971 player_.OnDemuxerSeekDone(kNoTimestamp()); 1972 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF()); 1973 EXPECT_FALSE(GetMediaCodecBridge(true)); 1974 EXPECT_FALSE(player_.IsPlaying()); 1975 1976 // Player should begin prefetch and resume preroll upon Start(). 1977 EXPECT_EQ(2, demuxer_->num_data_requests()); 1978 Resume(true, false); 1979 EXPECT_TRUE(IsPrerolling(true)); 1980 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF()); 1981 1982 // No further seek should have been requested since Release(), above. 1983 EXPECT_EQ(1, demuxer_->num_seek_requests()); 1984 } 1985 1986 TEST_F(MediaSourcePlayerTest, SeekToThenReleaseThenDemuxerSeekThenStart) { 1987 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 1988 1989 // Test if Release() occurs after SeekTo(), but the DemuxerSeek IPC request 1990 // has not yet been sent, then the seek request is sent after Release(). Also, 1991 // test if OnDemuxerSeekDone() does not occur until after the next Start(), 1992 // then the player remains pending seek done until (and resumes correct 1993 // post-seek preroll after) OnDemuxerSeekDone(). 1994 StartAudioDecoderJobAndSeekToWhileDecoding( 1995 base::TimeDelta::FromMilliseconds(100)); 1996 ReleasePlayer(); 1997 EXPECT_EQ(0, demuxer_->num_seek_requests()); 1998 1999 // Player should not prefetch upon Start() nor create the media codec bridge, 2000 // due to awaiting DemuxerSeekDone. 2001 EXPECT_EQ(2, demuxer_->num_data_requests()); 2002 Resume(false, false); 2003 2004 WaitForAudioDecodeDone(); 2005 EXPECT_EQ(1, demuxer_->num_seek_requests()); 2006 player_.OnDemuxerSeekDone(kNoTimestamp()); 2007 EXPECT_TRUE(GetMediaDecoderJob(true)); 2008 EXPECT_TRUE(IsPrerolling(true)); 2009 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF()); 2010 EXPECT_EQ(3, demuxer_->num_data_requests()); 2011 2012 // No further seek should have been requested since Release(), above. 2013 EXPECT_EQ(1, demuxer_->num_seek_requests()); 2014 } 2015 2016 TEST_F(MediaSourcePlayerTest, SeekToThenDemuxerSeekThenReleaseThenSeekDone) { 2017 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 2018 2019 // Test if Release() occurs after a SeekTo()'s subsequent DemuxerSeek IPC 2020 // request and OnDemuxerSeekDone() arrives prior to the next Start(), then the 2021 // player will resume correct post-seek preroll upon Start(). 2022 StartAudioDecoderJobAndSeekToWhileDecoding( 2023 base::TimeDelta::FromMilliseconds(100)); 2024 WaitForAudioDecodeDone(); 2025 EXPECT_EQ(1, demuxer_->num_seek_requests()); 2026 2027 ReleasePlayer(); 2028 player_.OnDemuxerSeekDone(kNoTimestamp()); 2029 EXPECT_FALSE(player_.IsPlaying()); 2030 EXPECT_FALSE(GetMediaCodecBridge(true)); 2031 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF()); 2032 2033 // Player should begin prefetch and resume preroll upon Start(). 2034 EXPECT_EQ(2, demuxer_->num_data_requests()); 2035 Resume(true, false); 2036 EXPECT_TRUE(IsPrerolling(true)); 2037 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF()); 2038 2039 // No further seek should have been requested since before Release(), above. 2040 EXPECT_EQ(1, demuxer_->num_seek_requests()); 2041 } 2042 2043 TEST_F(MediaSourcePlayerTest, SeekToThenReleaseThenStart) { 2044 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 2045 2046 // Test if Release() occurs after a SeekTo()'s subsequent DemuxerSeeK IPC 2047 // request and OnDemuxerSeekDone() does not occur until after the next 2048 // Start(), then the player remains pending seek done until (and resumes 2049 // correct post-seek preroll after) OnDemuxerSeekDone(). 2050 StartAudioDecoderJobAndSeekToWhileDecoding( 2051 base::TimeDelta::FromMilliseconds(100)); 2052 WaitForAudioDecodeDone(); 2053 EXPECT_EQ(1, demuxer_->num_seek_requests()); 2054 2055 ReleasePlayer(); 2056 EXPECT_EQ(2, demuxer_->num_data_requests()); 2057 Resume(false, false); 2058 2059 player_.OnDemuxerSeekDone(kNoTimestamp()); 2060 EXPECT_FALSE(GetMediaCodecBridge(true)); 2061 EXPECT_TRUE(IsPrerolling(true)); 2062 EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF()); 2063 EXPECT_EQ(3, demuxer_->num_data_requests()); 2064 2065 // No further seek should have been requested since before Release(), above. 2066 EXPECT_EQ(1, demuxer_->num_seek_requests()); 2067 } 2068 2069 TEST_F(MediaSourcePlayerTest, ConfigChangedThenReleaseThenStart) { 2070 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 2071 2072 // Test if Release() occurs after |kConfigChanged| is processed, new data 2073 // requested of demuxer, and the requested data arrive before the next 2074 // Start(), then the player starts to decode the new data without any seek. 2075 StartConfigChange(true, true, 0, false); 2076 ReleasePlayer(); 2077 2078 EXPECT_TRUE(GetMediaCodecBridge(true)); 2079 EXPECT_FALSE(player_.IsPlaying()); 2080 EXPECT_EQ(3, demuxer_->num_data_requests()); 2081 player_.OnDemuxerDataAvailable( 2082 CreateReadFromDemuxerAckWithConfigChanged( 2083 true, 4, CreateAudioDemuxerConfigs(kCodecVorbis, false))); 2084 WaitForAudioDecodeDone(); 2085 EXPECT_FALSE(GetMediaCodecBridge(true)); 2086 2087 // Player should resume upon Start(), even without further configs supplied. 2088 player_.Start(); 2089 EXPECT_TRUE(player_.IsPlaying()); 2090 EXPECT_EQ(3, demuxer_->num_data_requests()); 2091 EXPECT_EQ(0, demuxer_->num_seek_requests()); 2092 } 2093 2094 TEST_F(MediaSourcePlayerTest, BrowserSeek_ThenReleaseThenDemuxerSeekDone) { 2095 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 2096 2097 // Test that Release() after a browser seek's DemuxerSeek IPC request has been 2098 // sent behaves similar to a regular seek: if OnDemuxerSeekDone() occurs 2099 // before the next Start()+SetVideoSurface(), then the player will resume 2100 // correct post-seek preroll upon Start()+SetVideoSurface(). 2101 BrowserSeekPlayer(false); 2102 base::TimeDelta expected_preroll_timestamp = player_.GetCurrentTime(); 2103 ReleasePlayer(); 2104 2105 player_.OnDemuxerSeekDone(expected_preroll_timestamp); 2106 EXPECT_FALSE(player_.IsPlaying()); 2107 EXPECT_FALSE(GetMediaCodecBridge(false)); 2108 EXPECT_EQ(expected_preroll_timestamp, GetPrerollTimestamp()); 2109 2110 // Player should begin prefetch and resume preroll upon Start(). 2111 EXPECT_EQ(2, demuxer_->num_data_requests()); 2112 CreateNextTextureAndSetVideoSurface(); 2113 Resume(false, true); 2114 EXPECT_TRUE(IsPrerolling(false)); 2115 EXPECT_EQ(expected_preroll_timestamp, GetPrerollTimestamp()); 2116 EXPECT_EQ(expected_preroll_timestamp, player_.GetCurrentTime()); 2117 2118 // No further seek should have been requested since BrowserSeekPlayer(). 2119 EXPECT_EQ(1, demuxer_->num_seek_requests()); 2120 } 2121 2122 TEST_F(MediaSourcePlayerTest, BrowserSeek_ThenReleaseThenStart) { 2123 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 2124 2125 // Test that Release() after a browser seek's DemuxerSeek IPC request has been 2126 // sent behaves similar to a regular seek: if OnDemuxerSeekDone() does not 2127 // occur until after the next Start()+SetVideoSurface(), then the player 2128 // remains pending seek done until (and resumes correct post-seek preroll 2129 // after) OnDemuxerSeekDone(). 2130 BrowserSeekPlayer(false); 2131 base::TimeDelta expected_preroll_timestamp = player_.GetCurrentTime(); 2132 ReleasePlayer(); 2133 2134 EXPECT_EQ(2, demuxer_->num_data_requests()); 2135 CreateNextTextureAndSetVideoSurface(); 2136 Resume(false, false); 2137 2138 player_.OnDemuxerSeekDone(expected_preroll_timestamp); 2139 // Prefetch takes place first, and the decoder is not created yet. 2140 EXPECT_FALSE(GetMediaCodecBridge(false)); 2141 EXPECT_TRUE(IsPrerolling(false)); 2142 EXPECT_EQ(expected_preroll_timestamp, GetPrerollTimestamp()); 2143 EXPECT_EQ(expected_preroll_timestamp, player_.GetCurrentTime()); 2144 EXPECT_EQ(3, demuxer_->num_data_requests()); 2145 2146 // No further seek should have been requested since BrowserSeekPlayer(). 2147 EXPECT_EQ(1, demuxer_->num_seek_requests()); 2148 2149 // Decoder will be created once data is received. 2150 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo()); 2151 while (!GetMediaCodecBridge(false)) 2152 message_loop_.RunUntilIdle(); 2153 } 2154 2155 // TODO(xhwang): Once we add tests to cover DrmBridge, update this test to 2156 // also verify that the job is successfully created if SetDrmBridge(), Start() 2157 // and eventually OnMediaCrypto() occur. This would increase test coverage of 2158 // http://crbug.com/313470 and allow us to remove inspection of internal player 2159 // pending event state. See http://crbug.com/313860. 2160 TEST_F(MediaSourcePlayerTest, SurfaceChangeClearedEvenIfMediaCryptoAbsent) { 2161 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 2162 2163 // Test that |SURFACE_CHANGE_EVENT_PENDING| is not pending after 2164 // SetVideoSurface() for a player configured for encrypted video, when the 2165 // player has not yet received media crypto. 2166 DemuxerConfigs configs = CreateVideoDemuxerConfigs(false); 2167 configs.is_video_encrypted = true; 2168 2169 player_.OnDemuxerConfigsAvailable(configs); 2170 CreateNextTextureAndSetVideoSurface(); 2171 EXPECT_FALSE(GetMediaCodecBridge(false)); 2172 } 2173 2174 TEST_F(MediaSourcePlayerTest, CurrentTimeUpdatedWhileDecoderStarved) { 2175 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 2176 2177 // Test that current time is updated while decoder is starved. 2178 StartAudioDecoderJob(); 2179 DecodeAudioDataUntilOutputBecomesAvailable(); 2180 2181 // Trigger starvation while the decoder is decoding. 2182 player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(3)); 2183 manager_.ResetTimestampUpdated(); 2184 TriggerPlayerStarvation(); 2185 WaitForAudioDecodeDone(); 2186 2187 // Current time should be updated. 2188 EXPECT_TRUE(manager_.timestamp_updated()); 2189 } 2190 2191 TEST_F(MediaSourcePlayerTest, CurrentTimeKeepsIncreasingAfterConfigChange) { 2192 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); 2193 2194 // Test current time keep on increasing after audio config change. 2195 // Test that current time is updated while decoder is starved. 2196 StartAudioDecoderJob(); 2197 2198 DecodeAudioDataUntilOutputBecomesAvailable(); 2199 2200 DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis, true); 2201 DemuxerData data = CreateReadFromDemuxerAckWithConfigChanged( 2202 true, 0, configs); 2203 player_.OnDemuxerDataAvailable(data); 2204 WaitForAudioDecodeDone(); 2205 DecodeAudioDataUntilOutputBecomesAvailable(); 2206 } 2207 2208 } // namespace media 2209