1 // Copyright (c) 2012 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 "base/bind.h" 6 #include "base/callback_helpers.h" 7 #include "base/gtest_prod_util.h" 8 #include "base/memory/scoped_vector.h" 9 #include "base/message_loop/message_loop.h" 10 #include "base/stl_util.h" 11 #include "base/strings/stringprintf.h" 12 #include "media/base/audio_buffer.h" 13 #include "media/base/audio_timestamp_helper.h" 14 #include "media/base/fake_audio_renderer_sink.h" 15 #include "media/base/gmock_callback_support.h" 16 #include "media/base/mock_filters.h" 17 #include "media/base/test_helpers.h" 18 #include "media/filters/audio_renderer_impl.h" 19 #include "testing/gtest/include/gtest/gtest.h" 20 21 using ::base::Time; 22 using ::base::TimeTicks; 23 using ::base::TimeDelta; 24 using ::testing::_; 25 using ::testing::AnyNumber; 26 using ::testing::Invoke; 27 using ::testing::Return; 28 29 namespace media { 30 31 // Constants to specify the type of audio data used. 32 static AudioCodec kCodec = kCodecVorbis; 33 static SampleFormat kSampleFormat = kSampleFormatPlanarF32; 34 static ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO; 35 static int kChannels = ChannelLayoutToChannelCount(kChannelLayout); 36 static int kSamplesPerSecond = 44100; 37 38 // Constants for distinguishing between muted audio and playing audio when using 39 // ConsumeBufferedData(). Must match the type needed by kSampleFormat. 40 static float kMutedAudio = 0.0f; 41 static float kPlayingAudio = 0.5f; 42 43 static const int kDataSize = 1024; 44 45 class AudioRendererImplTest : public ::testing::Test { 46 public: 47 // Give the decoder some non-garbage media properties. 48 AudioRendererImplTest() 49 : demuxer_stream_(DemuxerStream::AUDIO), 50 decoder_(new MockAudioDecoder()) { 51 AudioDecoderConfig audio_config(kCodec, 52 kSampleFormat, 53 kChannelLayout, 54 kSamplesPerSecond, 55 NULL, 56 0, 57 false); 58 demuxer_stream_.set_audio_decoder_config(audio_config); 59 60 // Used to save callbacks and run them at a later time. 61 EXPECT_CALL(*decoder_, Read(_)) 62 .WillRepeatedly(Invoke(this, &AudioRendererImplTest::ReadDecoder)); 63 64 EXPECT_CALL(*decoder_, Reset(_)) 65 .WillRepeatedly(Invoke(this, &AudioRendererImplTest::ResetDecoder)); 66 67 // Set up audio properties. 68 EXPECT_CALL(*decoder_, bits_per_channel()) 69 .WillRepeatedly(Return(audio_config.bits_per_channel())); 70 EXPECT_CALL(*decoder_, channel_layout()) 71 .WillRepeatedly(Return(audio_config.channel_layout())); 72 EXPECT_CALL(*decoder_, samples_per_second()) 73 .WillRepeatedly(Return(audio_config.samples_per_second())); 74 75 ScopedVector<AudioDecoder> decoders; 76 decoders.push_back(decoder_); 77 sink_ = new FakeAudioRendererSink(); 78 renderer_.reset(new AudioRendererImpl( 79 message_loop_.message_loop_proxy(), 80 sink_, 81 decoders.Pass(), 82 SetDecryptorReadyCB())); 83 84 // Stub out time. 85 renderer_->set_now_cb_for_testing(base::Bind( 86 &AudioRendererImplTest::GetTime, base::Unretained(this))); 87 } 88 89 virtual ~AudioRendererImplTest() { 90 SCOPED_TRACE("~AudioRendererImplTest()"); 91 WaitableMessageLoopEvent event; 92 renderer_->Stop(event.GetClosure()); 93 event.RunAndWait(); 94 } 95 96 void ExpectUnsupportedAudioDecoder() { 97 EXPECT_CALL(*decoder_, Initialize(_, _, _)) 98 .WillOnce(RunCallback<1>(DECODER_ERROR_NOT_SUPPORTED)); 99 } 100 101 void ExpectUnsupportedAudioDecoderConfig() { 102 EXPECT_CALL(*decoder_, bits_per_channel()) 103 .WillRepeatedly(Return(3)); 104 EXPECT_CALL(*decoder_, channel_layout()) 105 .WillRepeatedly(Return(CHANNEL_LAYOUT_UNSUPPORTED)); 106 EXPECT_CALL(*decoder_, samples_per_second()) 107 .WillRepeatedly(Return(0)); 108 EXPECT_CALL(*decoder_, Initialize(_, _, _)) 109 .WillOnce(RunCallback<1>(PIPELINE_OK)); 110 } 111 112 MOCK_METHOD1(OnStatistics, void(const PipelineStatistics&)); 113 MOCK_METHOD0(OnUnderflow, void()); 114 MOCK_METHOD0(OnDisabled, void()); 115 MOCK_METHOD1(OnError, void(PipelineStatus)); 116 117 void OnAudioTimeCallback(TimeDelta current_time, TimeDelta max_time) { 118 CHECK(current_time <= max_time); 119 } 120 121 void Initialize() { 122 EXPECT_CALL(*decoder_, Initialize(_, _, _)) 123 .WillOnce(RunCallback<1>(PIPELINE_OK)); 124 InitializeWithStatus(PIPELINE_OK); 125 126 next_timestamp_.reset( 127 new AudioTimestampHelper(decoder_->samples_per_second())); 128 } 129 130 void InitializeWithStatus(PipelineStatus expected) { 131 SCOPED_TRACE(base::StringPrintf("InitializeWithStatus(%d)", expected)); 132 133 WaitableMessageLoopEvent event; 134 renderer_->Initialize( 135 &demuxer_stream_, 136 event.GetPipelineStatusCB(), 137 base::Bind(&AudioRendererImplTest::OnStatistics, 138 base::Unretained(this)), 139 base::Bind(&AudioRendererImplTest::OnUnderflow, 140 base::Unretained(this)), 141 base::Bind(&AudioRendererImplTest::OnAudioTimeCallback, 142 base::Unretained(this)), 143 ended_event_.GetClosure(), 144 base::Bind(&AudioRendererImplTest::OnDisabled, 145 base::Unretained(this)), 146 base::Bind(&AudioRendererImplTest::OnError, 147 base::Unretained(this))); 148 event.RunAndWaitForStatus(expected); 149 150 // We should have no reads. 151 EXPECT_TRUE(read_cb_.is_null()); 152 } 153 154 void Flush() { 155 WaitableMessageLoopEvent flush_event; 156 renderer_->Flush(flush_event.GetClosure()); 157 flush_event.RunAndWait(); 158 159 EXPECT_FALSE(IsReadPending()); 160 } 161 162 void Preroll() { 163 Preroll(0, PIPELINE_OK); 164 } 165 166 void Preroll(int timestamp_ms, PipelineStatus expected) { 167 SCOPED_TRACE(base::StringPrintf("Preroll(%d, %d)", timestamp_ms, expected)); 168 169 TimeDelta timestamp = TimeDelta::FromMilliseconds(timestamp_ms); 170 next_timestamp_->SetBaseTimestamp(timestamp); 171 172 // Fill entire buffer to complete prerolling. 173 WaitableMessageLoopEvent event; 174 renderer_->Preroll(timestamp, event.GetPipelineStatusCB()); 175 WaitForPendingRead(); 176 DeliverRemainingAudio(); 177 event.RunAndWaitForStatus(PIPELINE_OK); 178 179 // We should have no reads. 180 EXPECT_TRUE(read_cb_.is_null()); 181 } 182 183 void Play() { 184 SCOPED_TRACE("Play()"); 185 WaitableMessageLoopEvent event; 186 renderer_->Play(event.GetClosure()); 187 renderer_->SetPlaybackRate(1.0f); 188 event.RunAndWait(); 189 } 190 191 void Pause() { 192 WaitableMessageLoopEvent pause_event; 193 renderer_->Pause(pause_event.GetClosure()); 194 pause_event.RunAndWait(); 195 } 196 197 void Seek() { 198 Pause(); 199 200 Flush(); 201 202 Preroll(); 203 } 204 205 void WaitForEnded() { 206 SCOPED_TRACE("WaitForEnded()"); 207 ended_event_.RunAndWait(); 208 } 209 210 bool IsReadPending() const { 211 return !read_cb_.is_null(); 212 } 213 214 void WaitForPendingRead() { 215 SCOPED_TRACE("WaitForPendingRead()"); 216 if (!read_cb_.is_null()) 217 return; 218 219 DCHECK(wait_for_pending_read_cb_.is_null()); 220 221 WaitableMessageLoopEvent event; 222 wait_for_pending_read_cb_ = event.GetClosure(); 223 event.RunAndWait(); 224 225 DCHECK(!read_cb_.is_null()); 226 DCHECK(wait_for_pending_read_cb_.is_null()); 227 } 228 229 // Delivers |size| frames with value kPlayingAudio to |renderer_|. 230 void SatisfyPendingRead(int size) { 231 CHECK_GT(size, 0); 232 CHECK(!read_cb_.is_null()); 233 234 scoped_refptr<AudioBuffer> buffer = 235 MakePlanarAudioBuffer<float>(kSampleFormat, 236 kChannels, 237 kPlayingAudio, 238 0.0f, 239 size, 240 next_timestamp_->GetTimestamp(), 241 next_timestamp_->GetFrameDuration(size)); 242 next_timestamp_->AddFrames(size); 243 244 DeliverBuffer(AudioDecoder::kOk, buffer); 245 } 246 247 void AbortPendingRead() { 248 DeliverBuffer(AudioDecoder::kAborted, NULL); 249 } 250 251 void DeliverEndOfStream() { 252 DeliverBuffer(AudioDecoder::kOk, AudioBuffer::CreateEOSBuffer()); 253 } 254 255 // Delivers frames until |renderer_|'s internal buffer is full and no longer 256 // has pending reads. 257 void DeliverRemainingAudio() { 258 SatisfyPendingRead(frames_remaining_in_buffer()); 259 } 260 261 // Attempts to consume |requested_frames| frames from |renderer_|'s internal 262 // buffer, returning true if all |requested_frames| frames were consumed, 263 // false if less than |requested_frames| frames were consumed. 264 // 265 // |muted| is optional and if passed will get set if the value of 266 // the consumed data is muted audio. 267 bool ConsumeBufferedData(int requested_frames, bool* muted) { 268 scoped_ptr<AudioBus> bus = 269 AudioBus::Create(kChannels, std::max(requested_frames, 1)); 270 int frames_read; 271 if (!sink_->Render(bus.get(), 0, &frames_read)) { 272 if (muted) 273 *muted = true; 274 return false; 275 } 276 277 if (muted) 278 *muted = frames_read < 1 || bus->channel(0)[0] == kMutedAudio; 279 return frames_read == requested_frames; 280 } 281 282 // Attempts to consume all data available from the renderer. Returns the 283 // number of frames read. Since time is frozen, the audio delay will increase 284 // as frames come in. 285 int ConsumeAllBufferedData() { 286 renderer_->DisableUnderflowForTesting(); 287 288 int frames_read = 0; 289 int total_frames_read = 0; 290 291 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, 1024); 292 293 do { 294 TimeDelta audio_delay = TimeDelta::FromMicroseconds( 295 total_frames_read * Time::kMicrosecondsPerSecond / 296 static_cast<float>(decoder_->samples_per_second())); 297 298 frames_read = renderer_->Render( 299 bus.get(), audio_delay.InMilliseconds()); 300 total_frames_read += frames_read; 301 } while (frames_read > 0); 302 303 return total_frames_read; 304 } 305 306 int frames_buffered() { 307 return renderer_->algorithm_->frames_buffered(); 308 } 309 310 int buffer_capacity() { 311 return renderer_->algorithm_->QueueCapacity(); 312 } 313 314 int frames_remaining_in_buffer() { 315 // This can happen if too much data was delivered, in which case the buffer 316 // will accept the data but not increase capacity. 317 if (frames_buffered() > buffer_capacity()) { 318 return 0; 319 } 320 return buffer_capacity() - frames_buffered(); 321 } 322 323 void CallResumeAfterUnderflow() { 324 renderer_->ResumeAfterUnderflow(); 325 } 326 327 TimeDelta CalculatePlayTime(int frames_filled) { 328 return TimeDelta::FromMicroseconds( 329 frames_filled * Time::kMicrosecondsPerSecond / 330 renderer_->audio_parameters_.sample_rate()); 331 } 332 333 void EndOfStreamTest(float playback_rate) { 334 Initialize(); 335 Preroll(); 336 Play(); 337 renderer_->SetPlaybackRate(playback_rate); 338 339 // Drain internal buffer, we should have a pending read. 340 int total_frames = frames_buffered(); 341 int frames_filled = ConsumeAllBufferedData(); 342 WaitForPendingRead(); 343 344 // Due to how the cross-fade algorithm works we won't get an exact match 345 // between the ideal and expected number of frames consumed. In the faster 346 // than normal playback case, more frames are created than should exist and 347 // vice versa in the slower than normal playback case. 348 const float kEpsilon = 0.20 * (total_frames / playback_rate); 349 EXPECT_NEAR(frames_filled, total_frames / playback_rate, kEpsilon); 350 351 // Figure out how long until the ended event should fire. 352 TimeDelta audio_play_time = CalculatePlayTime(frames_filled); 353 DVLOG(1) << "audio_play_time = " << audio_play_time.InSecondsF(); 354 355 // Fulfill the read with an end-of-stream packet. We shouldn't report ended 356 // nor have a read until we drain the internal buffer. 357 DeliverEndOfStream(); 358 359 // Advance time half way without an ended expectation. 360 AdvanceTime(audio_play_time / 2); 361 ConsumeBufferedData(frames_buffered(), NULL); 362 363 // Advance time by other half and expect the ended event. 364 AdvanceTime(audio_play_time / 2); 365 ConsumeBufferedData(frames_buffered(), NULL); 366 WaitForEnded(); 367 } 368 369 void AdvanceTime(TimeDelta time) { 370 base::AutoLock auto_lock(lock_); 371 time_ += time; 372 } 373 374 // Fixture members. 375 base::MessageLoop message_loop_; 376 scoped_ptr<AudioRendererImpl> renderer_; 377 scoped_refptr<FakeAudioRendererSink> sink_; 378 379 private: 380 TimeTicks GetTime() { 381 base::AutoLock auto_lock(lock_); 382 return time_; 383 } 384 385 void ReadDecoder(const AudioDecoder::ReadCB& read_cb) { 386 // TODO(scherkus): Make this a DCHECK after threading semantics are fixed. 387 if (base::MessageLoop::current() != &message_loop_) { 388 message_loop_.PostTask(FROM_HERE, base::Bind( 389 &AudioRendererImplTest::ReadDecoder, 390 base::Unretained(this), read_cb)); 391 return; 392 } 393 394 CHECK(read_cb_.is_null()) << "Overlapping reads are not permitted"; 395 read_cb_ = read_cb; 396 397 // Wake up WaitForPendingRead() if needed. 398 if (!wait_for_pending_read_cb_.is_null()) 399 base::ResetAndReturn(&wait_for_pending_read_cb_).Run(); 400 } 401 402 void ResetDecoder(const base::Closure& reset_cb) { 403 CHECK(read_cb_.is_null()) 404 << "Reset overlapping with reads is not permitted"; 405 406 message_loop_.PostTask(FROM_HERE, reset_cb); 407 } 408 409 void DeliverBuffer(AudioDecoder::Status status, 410 const scoped_refptr<AudioBuffer>& buffer) { 411 CHECK(!read_cb_.is_null()); 412 base::ResetAndReturn(&read_cb_).Run(status, buffer); 413 } 414 415 MockDemuxerStream demuxer_stream_; 416 MockAudioDecoder* decoder_; 417 418 // Used for stubbing out time in the audio callback thread. 419 base::Lock lock_; 420 TimeTicks time_; 421 422 // Used for satisfying reads. 423 AudioDecoder::ReadCB read_cb_; 424 scoped_ptr<AudioTimestampHelper> next_timestamp_; 425 426 WaitableMessageLoopEvent ended_event_; 427 428 // Run during ReadDecoder() to unblock WaitForPendingRead(). 429 base::Closure wait_for_pending_read_cb_; 430 431 DISALLOW_COPY_AND_ASSIGN(AudioRendererImplTest); 432 }; 433 434 TEST_F(AudioRendererImplTest, Initialize_Failed) { 435 ExpectUnsupportedAudioDecoderConfig(); 436 InitializeWithStatus(PIPELINE_ERROR_INITIALIZATION_FAILED); 437 } 438 439 TEST_F(AudioRendererImplTest, Initialize_Successful) { 440 Initialize(); 441 } 442 443 TEST_F(AudioRendererImplTest, Initialize_DecoderInitFailure) { 444 ExpectUnsupportedAudioDecoder(); 445 InitializeWithStatus(DECODER_ERROR_NOT_SUPPORTED); 446 } 447 448 TEST_F(AudioRendererImplTest, Preroll) { 449 Initialize(); 450 Preroll(); 451 } 452 453 TEST_F(AudioRendererImplTest, Play) { 454 Initialize(); 455 Preroll(); 456 Play(); 457 458 // Drain internal buffer, we should have a pending read. 459 EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL)); 460 WaitForPendingRead(); 461 } 462 463 TEST_F(AudioRendererImplTest, EndOfStream) { 464 EndOfStreamTest(1.0); 465 } 466 467 TEST_F(AudioRendererImplTest, EndOfStream_FasterPlaybackSpeed) { 468 EndOfStreamTest(2.0); 469 } 470 471 TEST_F(AudioRendererImplTest, EndOfStream_SlowerPlaybackSpeed) { 472 EndOfStreamTest(0.5); 473 } 474 475 TEST_F(AudioRendererImplTest, Underflow) { 476 Initialize(); 477 Preroll(); 478 479 int initial_capacity = buffer_capacity(); 480 481 Play(); 482 483 // Drain internal buffer, we should have a pending read. 484 EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL)); 485 WaitForPendingRead(); 486 487 // Verify the next FillBuffer() call triggers the underflow callback 488 // since the decoder hasn't delivered any data after it was drained. 489 EXPECT_CALL(*this, OnUnderflow()); 490 EXPECT_FALSE(ConsumeBufferedData(kDataSize, NULL)); 491 492 renderer_->ResumeAfterUnderflow(); 493 494 // Verify after resuming that we're still not getting data. 495 bool muted = false; 496 EXPECT_EQ(0, frames_buffered()); 497 EXPECT_FALSE(ConsumeBufferedData(kDataSize, &muted)); 498 EXPECT_TRUE(muted); 499 500 // Verify that the buffer capacity increased as a result of the underflow. 501 EXPECT_GT(buffer_capacity(), initial_capacity); 502 503 // Deliver data, we should get non-muted audio. 504 DeliverRemainingAudio(); 505 EXPECT_TRUE(ConsumeBufferedData(kDataSize, &muted)); 506 EXPECT_FALSE(muted); 507 } 508 509 TEST_F(AudioRendererImplTest, Underflow_FollowedByFlush) { 510 Initialize(); 511 Preroll(); 512 513 int initial_capacity = buffer_capacity(); 514 515 Play(); 516 517 // Drain internal buffer, we should have a pending read. 518 EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL)); 519 WaitForPendingRead(); 520 521 // Verify the next FillBuffer() call triggers the underflow callback 522 // since the decoder hasn't delivered any data after it was drained. 523 EXPECT_CALL(*this, OnUnderflow()); 524 EXPECT_FALSE(ConsumeBufferedData(kDataSize, NULL)); 525 526 renderer_->ResumeAfterUnderflow(); 527 528 // Verify that the buffer capacity increased as a result of the underflow. 529 EXPECT_GT(buffer_capacity(), initial_capacity); 530 531 // Deliver data to get the renderer out of the underflow/rebuffer state. 532 DeliverRemainingAudio(); 533 534 Seek(); 535 536 // Verify that the buffer capacity is restored to the |initial_capacity|. 537 EXPECT_EQ(buffer_capacity(), initial_capacity); 538 } 539 540 TEST_F(AudioRendererImplTest, Underflow_EndOfStream) { 541 Initialize(); 542 Preroll(); 543 Play(); 544 545 // Figure out how long until the ended event should fire. Since 546 // ConsumeBufferedData() doesn't provide audio delay information, the time 547 // until the ended event fires is equivalent to the longest buffered section, 548 // which is the initial frames_buffered() read. 549 TimeDelta time_until_ended = CalculatePlayTime(frames_buffered()); 550 551 // Drain internal buffer, we should have a pending read. 552 EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL)); 553 WaitForPendingRead(); 554 555 // Verify the next FillBuffer() call triggers the underflow callback 556 // since the decoder hasn't delivered any data after it was drained. 557 EXPECT_CALL(*this, OnUnderflow()); 558 EXPECT_FALSE(ConsumeBufferedData(kDataSize, NULL)); 559 560 // Deliver a little bit of data. 561 SatisfyPendingRead(kDataSize); 562 WaitForPendingRead(); 563 564 // Verify we're getting muted audio during underflow. 565 bool muted = false; 566 EXPECT_EQ(kDataSize, frames_buffered()); 567 EXPECT_FALSE(ConsumeBufferedData(kDataSize, &muted)); 568 EXPECT_TRUE(muted); 569 570 // Now deliver end of stream, we should get our little bit of data back. 571 DeliverEndOfStream(); 572 EXPECT_EQ(kDataSize, frames_buffered()); 573 EXPECT_TRUE(ConsumeBufferedData(kDataSize, &muted)); 574 EXPECT_FALSE(muted); 575 576 // Attempt to read to make sure we're truly at the end of stream. 577 AdvanceTime(time_until_ended); 578 EXPECT_FALSE(ConsumeBufferedData(kDataSize, &muted)); 579 EXPECT_TRUE(muted); 580 WaitForEnded(); 581 } 582 583 TEST_F(AudioRendererImplTest, Underflow_ResumeFromCallback) { 584 Initialize(); 585 Preroll(); 586 Play(); 587 588 // Drain internal buffer, we should have a pending read. 589 EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL)); 590 WaitForPendingRead(); 591 592 // Verify the next FillBuffer() call triggers the underflow callback 593 // since the decoder hasn't delivered any data after it was drained. 594 EXPECT_CALL(*this, OnUnderflow()) 595 .WillOnce(Invoke(this, &AudioRendererImplTest::CallResumeAfterUnderflow)); 596 EXPECT_FALSE(ConsumeBufferedData(kDataSize, NULL)); 597 598 // Verify after resuming that we're still not getting data. 599 bool muted = false; 600 EXPECT_EQ(0, frames_buffered()); 601 EXPECT_FALSE(ConsumeBufferedData(kDataSize, &muted)); 602 EXPECT_TRUE(muted); 603 604 // Deliver data, we should get non-muted audio. 605 DeliverRemainingAudio(); 606 EXPECT_TRUE(ConsumeBufferedData(kDataSize, &muted)); 607 EXPECT_FALSE(muted); 608 } 609 610 TEST_F(AudioRendererImplTest, Underflow_SetPlaybackRate) { 611 Initialize(); 612 Preroll(); 613 Play(); 614 615 // Drain internal buffer, we should have a pending read. 616 EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL)); 617 WaitForPendingRead(); 618 619 EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state()); 620 621 // Verify the next FillBuffer() call triggers the underflow callback 622 // since the decoder hasn't delivered any data after it was drained. 623 EXPECT_CALL(*this, OnUnderflow()) 624 .WillOnce(Invoke(this, &AudioRendererImplTest::CallResumeAfterUnderflow)); 625 EXPECT_FALSE(ConsumeBufferedData(kDataSize, NULL)); 626 EXPECT_EQ(0, frames_buffered()); 627 628 EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state()); 629 630 // Simulate playback being paused. 631 renderer_->SetPlaybackRate(0); 632 633 EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state()); 634 635 // Deliver data to resolve the underflow. 636 DeliverRemainingAudio(); 637 638 EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state()); 639 640 // Simulate playback being resumed. 641 renderer_->SetPlaybackRate(1); 642 643 EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state()); 644 } 645 646 TEST_F(AudioRendererImplTest, Underflow_PausePlay) { 647 Initialize(); 648 Preroll(); 649 Play(); 650 651 // Drain internal buffer, we should have a pending read. 652 EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL)); 653 WaitForPendingRead(); 654 655 EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state()); 656 657 // Verify the next FillBuffer() call triggers the underflow callback 658 // since the decoder hasn't delivered any data after it was drained. 659 EXPECT_CALL(*this, OnUnderflow()) 660 .WillOnce(Invoke(this, &AudioRendererImplTest::CallResumeAfterUnderflow)); 661 EXPECT_FALSE(ConsumeBufferedData(kDataSize, NULL)); 662 EXPECT_EQ(0, frames_buffered()); 663 664 EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state()); 665 666 // Simulate playback being paused, and then played again. 667 renderer_->SetPlaybackRate(0.0); 668 renderer_->SetPlaybackRate(1.0); 669 670 // Deliver data to resolve the underflow. 671 DeliverRemainingAudio(); 672 673 // We should have resumed playing now. 674 EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state()); 675 } 676 677 TEST_F(AudioRendererImplTest, AbortPendingRead_Preroll) { 678 Initialize(); 679 680 // Start prerolling and wait for a read. 681 WaitableMessageLoopEvent event; 682 renderer_->Preroll(TimeDelta(), event.GetPipelineStatusCB()); 683 WaitForPendingRead(); 684 685 // Simulate the decoder aborting the pending read. 686 AbortPendingRead(); 687 event.RunAndWaitForStatus(PIPELINE_OK); 688 689 Flush(); 690 691 // Preroll again to a different timestamp and verify it completed normally. 692 Preroll(1000, PIPELINE_OK); 693 } 694 695 TEST_F(AudioRendererImplTest, AbortPendingRead_Pause) { 696 Initialize(); 697 698 Preroll(); 699 Play(); 700 701 // Partially drain internal buffer so we get a pending read. 702 EXPECT_TRUE(ConsumeBufferedData(frames_buffered() / 2, NULL)); 703 WaitForPendingRead(); 704 705 // Start pausing. 706 WaitableMessageLoopEvent event; 707 renderer_->Pause(event.GetClosure()); 708 709 // Simulate the decoder aborting the pending read. 710 AbortPendingRead(); 711 event.RunAndWait(); 712 713 Flush(); 714 715 // Preroll again to a different timestamp and verify it completed normally. 716 Preroll(1000, PIPELINE_OK); 717 } 718 719 720 TEST_F(AudioRendererImplTest, AbortPendingRead_Flush) { 721 Initialize(); 722 723 Preroll(); 724 Play(); 725 726 // Partially drain internal buffer so we get a pending read. 727 EXPECT_TRUE(ConsumeBufferedData(frames_buffered() / 2, NULL)); 728 WaitForPendingRead(); 729 730 Pause(); 731 732 EXPECT_TRUE(IsReadPending()); 733 734 // Start flushing. 735 WaitableMessageLoopEvent flush_event; 736 renderer_->Flush(flush_event.GetClosure()); 737 738 // Simulate the decoder aborting the pending read. 739 AbortPendingRead(); 740 flush_event.RunAndWait(); 741 742 EXPECT_FALSE(IsReadPending()); 743 744 // Preroll again to a different timestamp and verify it completed normally. 745 Preroll(1000, PIPELINE_OK); 746 } 747 748 TEST_F(AudioRendererImplTest, PendingRead_Pause) { 749 Initialize(); 750 751 Preroll(); 752 Play(); 753 754 // Partially drain internal buffer so we get a pending read. 755 EXPECT_TRUE(ConsumeBufferedData(frames_buffered() / 2, NULL)); 756 WaitForPendingRead(); 757 758 // Start pausing. 759 WaitableMessageLoopEvent event; 760 renderer_->Pause(event.GetClosure()); 761 762 SatisfyPendingRead(kDataSize); 763 764 event.RunAndWait(); 765 766 Flush(); 767 768 // Preroll again to a different timestamp and verify it completed normally. 769 Preroll(1000, PIPELINE_OK); 770 } 771 772 773 TEST_F(AudioRendererImplTest, PendingRead_Flush) { 774 Initialize(); 775 776 Preroll(); 777 Play(); 778 779 // Partially drain internal buffer so we get a pending read. 780 EXPECT_TRUE(ConsumeBufferedData(frames_buffered() / 2, NULL)); 781 WaitForPendingRead(); 782 783 Pause(); 784 785 EXPECT_TRUE(IsReadPending()); 786 787 // Start flushing. 788 WaitableMessageLoopEvent flush_event; 789 renderer_->Flush(flush_event.GetClosure()); 790 791 SatisfyPendingRead(kDataSize); 792 793 flush_event.RunAndWait(); 794 795 EXPECT_FALSE(IsReadPending()); 796 797 // Preroll again to a different timestamp and verify it completed normally. 798 Preroll(1000, PIPELINE_OK); 799 } 800 801 TEST_F(AudioRendererImplTest, StopDuringFlush) { 802 Initialize(); 803 804 Preroll(); 805 Play(); 806 807 // Partially drain internal buffer so we get a pending read. 808 EXPECT_TRUE(ConsumeBufferedData(frames_buffered() / 2, NULL)); 809 WaitForPendingRead(); 810 811 Pause(); 812 813 EXPECT_TRUE(IsReadPending()); 814 815 // Start flushing. 816 WaitableMessageLoopEvent flush_event; 817 renderer_->Flush(flush_event.GetClosure()); 818 819 SatisfyPendingRead(kDataSize); 820 821 // Request a Stop() before the flush completes. 822 WaitableMessageLoopEvent stop_event; 823 renderer_->Stop(stop_event.GetClosure()); 824 stop_event.RunAndWait(); 825 } 826 827 } // namespace media 828