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