Home | History | Annotate | Download | only in filters
      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/run_loop.h"
     11 #include "base/stl_util.h"
     12 #include "base/strings/stringprintf.h"
     13 #include "media/base/audio_buffer.h"
     14 #include "media/base/audio_buffer_converter.h"
     15 #include "media/base/audio_hardware_config.h"
     16 #include "media/base/audio_splicer.h"
     17 #include "media/base/audio_timestamp_helper.h"
     18 #include "media/base/fake_audio_renderer_sink.h"
     19 #include "media/base/gmock_callback_support.h"
     20 #include "media/base/mock_filters.h"
     21 #include "media/base/test_helpers.h"
     22 #include "media/filters/audio_renderer_impl.h"
     23 #include "testing/gtest/include/gtest/gtest.h"
     24 
     25 using ::base::Time;
     26 using ::base::TimeTicks;
     27 using ::base::TimeDelta;
     28 using ::testing::_;
     29 using ::testing::AnyNumber;
     30 using ::testing::Invoke;
     31 using ::testing::Return;
     32 using ::testing::SaveArg;
     33 
     34 namespace media {
     35 
     36 // Constants to specify the type of audio data used.
     37 static AudioCodec kCodec = kCodecVorbis;
     38 static SampleFormat kSampleFormat = kSampleFormatPlanarF32;
     39 static ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO;
     40 static int kChannelCount = 2;
     41 static int kChannels = ChannelLayoutToChannelCount(kChannelLayout);
     42 static int kSamplesPerSecond = 44100;
     43 // Use a different output sample rate so the AudioBufferConverter is invoked.
     44 static int kOutputSamplesPerSecond = 48000;
     45 
     46 // Constants for distinguishing between muted audio and playing audio when using
     47 // ConsumeBufferedData(). Must match the type needed by kSampleFormat.
     48 static float kMutedAudio = 0.0f;
     49 static float kPlayingAudio = 0.5f;
     50 
     51 static const int kDataSize = 1024;
     52 
     53 ACTION_P(EnterPendingDecoderInitStateAction, test) {
     54   test->EnterPendingDecoderInitState(arg1);
     55 }
     56 
     57 class AudioRendererImplTest : public ::testing::Test {
     58  public:
     59   // Give the decoder some non-garbage media properties.
     60   AudioRendererImplTest()
     61       : hardware_config_(AudioParameters(), AudioParameters()),
     62         needs_stop_(true),
     63         demuxer_stream_(DemuxerStream::AUDIO),
     64         decoder_(new MockAudioDecoder()),
     65         last_time_update_(kNoTimestamp()) {
     66     AudioDecoderConfig audio_config(kCodec,
     67                                     kSampleFormat,
     68                                     kChannelLayout,
     69                                     kSamplesPerSecond,
     70                                     NULL,
     71                                     0,
     72                                     false);
     73     demuxer_stream_.set_audio_decoder_config(audio_config);
     74 
     75     // Used to save callbacks and run them at a later time.
     76     EXPECT_CALL(*decoder_, Decode(_, _))
     77         .WillRepeatedly(Invoke(this, &AudioRendererImplTest::DecodeDecoder));
     78     EXPECT_CALL(*decoder_, Reset(_))
     79         .WillRepeatedly(Invoke(this, &AudioRendererImplTest::ResetDecoder));
     80 
     81     // Mock out demuxer reads.
     82     EXPECT_CALL(demuxer_stream_, Read(_)).WillRepeatedly(
     83         RunCallback<0>(DemuxerStream::kOk,
     84                        scoped_refptr<DecoderBuffer>(new DecoderBuffer(0))));
     85     EXPECT_CALL(demuxer_stream_, SupportsConfigChanges())
     86         .WillRepeatedly(Return(true));
     87     AudioParameters out_params(AudioParameters::AUDIO_PCM_LOW_LATENCY,
     88                                kChannelLayout,
     89                                kOutputSamplesPerSecond,
     90                                SampleFormatToBytesPerChannel(kSampleFormat) * 8,
     91                                512);
     92     hardware_config_.UpdateOutputConfig(out_params);
     93     ScopedVector<AudioDecoder> decoders;
     94     decoders.push_back(decoder_);
     95     sink_ = new FakeAudioRendererSink();
     96     renderer_.reset(new AudioRendererImpl(message_loop_.message_loop_proxy(),
     97                                           sink_,
     98                                           decoders.Pass(),
     99                                           SetDecryptorReadyCB(),
    100                                           &hardware_config_));
    101 
    102     // Stub out time.
    103     renderer_->set_now_cb_for_testing(base::Bind(
    104         &AudioRendererImplTest::GetTime, base::Unretained(this)));
    105   }
    106 
    107   virtual ~AudioRendererImplTest() {
    108     SCOPED_TRACE("~AudioRendererImplTest()");
    109     if (needs_stop_) {
    110       WaitableMessageLoopEvent event;
    111       renderer_->Stop(event.GetClosure());
    112       event.RunAndWait();
    113     }
    114   }
    115 
    116   void ExpectUnsupportedAudioDecoder() {
    117     EXPECT_CALL(*decoder_, Initialize(_, _, _))
    118         .WillOnce(DoAll(SaveArg<2>(&output_cb_),
    119                         RunCallback<1>(DECODER_ERROR_NOT_SUPPORTED)));
    120   }
    121 
    122   MOCK_METHOD1(OnStatistics, void(const PipelineStatistics&));
    123   MOCK_METHOD0(OnUnderflow, void());
    124   MOCK_METHOD1(OnError, void(PipelineStatus));
    125 
    126   void OnAudioTimeCallback(TimeDelta current_time, TimeDelta max_time) {
    127     CHECK(current_time <= max_time);
    128     last_time_update_ = current_time;
    129   }
    130 
    131   void InitializeRenderer(const PipelineStatusCB& pipeline_status_cb) {
    132     renderer_->Initialize(
    133         &demuxer_stream_,
    134         pipeline_status_cb,
    135         base::Bind(&AudioRendererImplTest::OnStatistics,
    136                    base::Unretained(this)),
    137         base::Bind(&AudioRendererImplTest::OnUnderflow,
    138                    base::Unretained(this)),
    139         base::Bind(&AudioRendererImplTest::OnAudioTimeCallback,
    140                    base::Unretained(this)),
    141         ended_event_.GetClosure(),
    142         base::Bind(&AudioRendererImplTest::OnError,
    143                    base::Unretained(this)));
    144   }
    145 
    146   void Initialize() {
    147     EXPECT_CALL(*decoder_, Initialize(_, _, _))
    148         .WillOnce(DoAll(SaveArg<2>(&output_cb_),
    149                         RunCallback<1>(PIPELINE_OK)));
    150     EXPECT_CALL(*decoder_, Stop());
    151     InitializeWithStatus(PIPELINE_OK);
    152 
    153     next_timestamp_.reset(new AudioTimestampHelper(
    154         hardware_config_.GetOutputConfig().sample_rate()));
    155   }
    156 
    157   void InitializeWithStatus(PipelineStatus expected) {
    158     SCOPED_TRACE(base::StringPrintf("InitializeWithStatus(%d)", expected));
    159 
    160     WaitableMessageLoopEvent event;
    161     InitializeRenderer(event.GetPipelineStatusCB());
    162     event.RunAndWaitForStatus(expected);
    163 
    164     // We should have no reads.
    165     EXPECT_TRUE(decode_cb_.is_null());
    166   }
    167 
    168   void InitializeAndStop() {
    169     EXPECT_CALL(*decoder_, Initialize(_, _, _))
    170         .WillOnce(DoAll(SaveArg<2>(&output_cb_),
    171                         RunCallback<1>(PIPELINE_OK)));
    172     EXPECT_CALL(*decoder_, Stop());
    173 
    174     WaitableMessageLoopEvent event;
    175     InitializeRenderer(event.GetPipelineStatusCB());
    176 
    177     // Stop before we let the MessageLoop run, this simulates an interleaving
    178     // in which we end up calling Stop() while the OnDecoderSelected callback
    179     // is in flight.
    180     renderer_->Stop(NewExpectedClosure());
    181     event.RunAndWaitForStatus(PIPELINE_ERROR_ABORT);
    182     EXPECT_EQ(renderer_->state_, AudioRendererImpl::kStopped);
    183   }
    184 
    185   void InitializeAndStopDuringDecoderInit() {
    186     EXPECT_CALL(*decoder_, Initialize(_, _, _))
    187         .WillOnce(DoAll(SaveArg<2>(&output_cb_),
    188                         EnterPendingDecoderInitStateAction(this)));
    189     EXPECT_CALL(*decoder_, Stop());
    190 
    191     WaitableMessageLoopEvent event;
    192     InitializeRenderer(event.GetPipelineStatusCB());
    193 
    194     base::RunLoop().RunUntilIdle();
    195     DCHECK(!init_decoder_cb_.is_null());
    196 
    197     renderer_->Stop(NewExpectedClosure());
    198     base::ResetAndReturn(&init_decoder_cb_).Run(PIPELINE_OK);
    199 
    200     event.RunAndWaitForStatus(PIPELINE_ERROR_ABORT);
    201     EXPECT_EQ(renderer_->state_, AudioRendererImpl::kStopped);
    202   }
    203 
    204   void EnterPendingDecoderInitState(PipelineStatusCB cb) {
    205     init_decoder_cb_ = cb;
    206   }
    207 
    208   void Flush() {
    209     WaitableMessageLoopEvent flush_event;
    210     renderer_->Flush(flush_event.GetClosure());
    211     flush_event.RunAndWait();
    212 
    213     EXPECT_FALSE(IsReadPending());
    214   }
    215 
    216   void Preroll() {
    217     Preroll(0, PIPELINE_OK);
    218   }
    219 
    220   void Preroll(int timestamp_ms, PipelineStatus expected) {
    221     SCOPED_TRACE(base::StringPrintf("Preroll(%d, %d)", timestamp_ms, expected));
    222 
    223     TimeDelta timestamp = TimeDelta::FromMilliseconds(timestamp_ms);
    224     next_timestamp_->SetBaseTimestamp(timestamp);
    225 
    226     // Fill entire buffer to complete prerolling.
    227     WaitableMessageLoopEvent event;
    228     renderer_->Preroll(timestamp, event.GetPipelineStatusCB());
    229     WaitForPendingRead();
    230     DeliverRemainingAudio();
    231     event.RunAndWaitForStatus(PIPELINE_OK);
    232   }
    233 
    234   void StartRendering() {
    235     renderer_->StartRendering();
    236     renderer_->SetPlaybackRate(1.0f);
    237   }
    238 
    239   void StopRendering() {
    240     renderer_->StopRendering();
    241   }
    242 
    243   void Seek() {
    244     StopRendering();
    245     Flush();
    246     Preroll();
    247   }
    248 
    249   void WaitForEnded() {
    250     SCOPED_TRACE("WaitForEnded()");
    251     ended_event_.RunAndWait();
    252   }
    253 
    254   bool IsReadPending() const {
    255     return !decode_cb_.is_null();
    256   }
    257 
    258   void WaitForPendingRead() {
    259     SCOPED_TRACE("WaitForPendingRead()");
    260     if (!decode_cb_.is_null())
    261       return;
    262 
    263     DCHECK(wait_for_pending_decode_cb_.is_null());
    264 
    265     WaitableMessageLoopEvent event;
    266     wait_for_pending_decode_cb_ = event.GetClosure();
    267     event.RunAndWait();
    268 
    269     DCHECK(!decode_cb_.is_null());
    270     DCHECK(wait_for_pending_decode_cb_.is_null());
    271   }
    272 
    273   // Delivers |size| frames with value kPlayingAudio to |renderer_|.
    274   void SatisfyPendingRead(int size) {
    275     CHECK_GT(size, 0);
    276     CHECK(!decode_cb_.is_null());
    277 
    278     scoped_refptr<AudioBuffer> buffer =
    279         MakeAudioBuffer<float>(kSampleFormat,
    280                                kChannelLayout,
    281                                kChannelCount,
    282                                kSamplesPerSecond,
    283                                kPlayingAudio,
    284                                0.0f,
    285                                size,
    286                                next_timestamp_->GetTimestamp());
    287     next_timestamp_->AddFrames(size);
    288 
    289     DeliverBuffer(AudioDecoder::kOk, buffer);
    290   }
    291 
    292   void DeliverEndOfStream() {
    293     DCHECK(!decode_cb_.is_null());
    294 
    295     // Return EOS buffer to trigger EOS frame.
    296     EXPECT_CALL(demuxer_stream_, Read(_))
    297         .WillOnce(RunCallback<0>(DemuxerStream::kOk,
    298                                  DecoderBuffer::CreateEOSBuffer()));
    299 
    300     // Satify pending |decode_cb_| to trigger a new DemuxerStream::Read().
    301     message_loop_.PostTask(
    302         FROM_HERE,
    303         base::Bind(base::ResetAndReturn(&decode_cb_), AudioDecoder::kOk));
    304 
    305     WaitForPendingRead();
    306 
    307     message_loop_.PostTask(
    308         FROM_HERE,
    309         base::Bind(base::ResetAndReturn(&decode_cb_), AudioDecoder::kOk));
    310 
    311     message_loop_.RunUntilIdle();
    312   }
    313 
    314   // Delivers frames until |renderer_|'s internal buffer is full and no longer
    315   // has pending reads.
    316   void DeliverRemainingAudio() {
    317     SatisfyPendingRead(frames_remaining_in_buffer());
    318   }
    319 
    320   // Attempts to consume |requested_frames| frames from |renderer_|'s internal
    321   // buffer, returning true if all |requested_frames| frames were consumed,
    322   // false if less than |requested_frames| frames were consumed.
    323   //
    324   // |muted| is optional and if passed will get set if the value of
    325   // the consumed data is muted audio.
    326   bool ConsumeBufferedData(int requested_frames, bool* muted) {
    327     scoped_ptr<AudioBus> bus =
    328         AudioBus::Create(kChannels, std::max(requested_frames, 1));
    329     int frames_read;
    330     if (!sink_->Render(bus.get(), 0, &frames_read)) {
    331       if (muted)
    332         *muted = true;
    333       return false;
    334     }
    335 
    336     if (muted)
    337       *muted = frames_read < 1 || bus->channel(0)[0] == kMutedAudio;
    338     return frames_read == requested_frames;
    339   }
    340 
    341   // Attempts to consume all data available from the renderer.  Returns the
    342   // number of frames read.  Since time is frozen, the audio delay will increase
    343   // as frames come in.
    344   int ConsumeAllBufferedData() {
    345     int frames_read = 0;
    346     int total_frames_read = 0;
    347 
    348     scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, 1024);
    349 
    350     do {
    351       TimeDelta audio_delay = TimeDelta::FromMicroseconds(
    352           total_frames_read * Time::kMicrosecondsPerSecond /
    353           static_cast<float>(hardware_config_.GetOutputConfig().sample_rate()));
    354 
    355       frames_read = renderer_->Render(
    356           bus.get(), audio_delay.InMilliseconds());
    357       total_frames_read += frames_read;
    358     } while (frames_read > 0);
    359 
    360     return total_frames_read;
    361   }
    362 
    363   int frames_buffered() {
    364     return renderer_->algorithm_->frames_buffered();
    365   }
    366 
    367   int buffer_capacity() {
    368     return renderer_->algorithm_->QueueCapacity();
    369   }
    370 
    371   int frames_remaining_in_buffer() {
    372     // This can happen if too much data was delivered, in which case the buffer
    373     // will accept the data but not increase capacity.
    374     if (frames_buffered() > buffer_capacity()) {
    375       return 0;
    376     }
    377     return buffer_capacity() - frames_buffered();
    378   }
    379 
    380   void CallResumeAfterUnderflow() {
    381     renderer_->ResumeAfterUnderflow();
    382   }
    383 
    384   TimeDelta CalculatePlayTime(int frames_filled) {
    385     return TimeDelta::FromMicroseconds(
    386         frames_filled * Time::kMicrosecondsPerSecond /
    387         renderer_->audio_parameters_.sample_rate());
    388   }
    389 
    390   void EndOfStreamTest(float playback_rate) {
    391     Initialize();
    392     Preroll();
    393     StartRendering();
    394     renderer_->SetPlaybackRate(playback_rate);
    395 
    396     // Drain internal buffer, we should have a pending read.
    397     int total_frames = frames_buffered();
    398     int frames_filled = ConsumeAllBufferedData();
    399     WaitForPendingRead();
    400 
    401     // Due to how the cross-fade algorithm works we won't get an exact match
    402     // between the ideal and expected number of frames consumed.  In the faster
    403     // than normal playback case, more frames are created than should exist and
    404     // vice versa in the slower than normal playback case.
    405     const float kEpsilon = 0.20 * (total_frames / playback_rate);
    406     EXPECT_NEAR(frames_filled, total_frames / playback_rate, kEpsilon);
    407 
    408     // Figure out how long until the ended event should fire.
    409     TimeDelta audio_play_time = CalculatePlayTime(frames_filled);
    410     DVLOG(1) << "audio_play_time = " << audio_play_time.InSecondsF();
    411 
    412     // Fulfill the read with an end-of-stream packet.  We shouldn't report ended
    413     // nor have a read until we drain the internal buffer.
    414     DeliverEndOfStream();
    415 
    416     // Advance time half way without an ended expectation.
    417     AdvanceTime(audio_play_time / 2);
    418     ConsumeBufferedData(frames_buffered(), NULL);
    419 
    420     // Advance time by other half and expect the ended event.
    421     AdvanceTime(audio_play_time / 2);
    422     ConsumeBufferedData(frames_buffered(), NULL);
    423     WaitForEnded();
    424   }
    425 
    426   void AdvanceTime(TimeDelta time) {
    427     base::AutoLock auto_lock(lock_);
    428     time_ += time;
    429   }
    430 
    431   void force_config_change() {
    432     renderer_->OnConfigChange();
    433   }
    434 
    435   int converter_input_frames_left() const {
    436     return renderer_->buffer_converter_->input_frames_left_for_testing();
    437   }
    438 
    439   bool splicer_has_next_buffer() const {
    440     return renderer_->splicer_->HasNextBuffer();
    441   }
    442 
    443   base::TimeDelta last_time_update() const {
    444     return last_time_update_;
    445   }
    446 
    447   // Fixture members.
    448   base::MessageLoop message_loop_;
    449   scoped_ptr<AudioRendererImpl> renderer_;
    450   scoped_refptr<FakeAudioRendererSink> sink_;
    451   AudioHardwareConfig hardware_config_;
    452 
    453   // Whether or not the test needs the destructor to call Stop() on
    454   // |renderer_| at destruction.
    455   bool needs_stop_;
    456 
    457  private:
    458   TimeTicks GetTime() {
    459     base::AutoLock auto_lock(lock_);
    460     return time_;
    461   }
    462 
    463   void DecodeDecoder(const scoped_refptr<DecoderBuffer>& buffer,
    464                      const AudioDecoder::DecodeCB& decode_cb) {
    465     // We shouldn't ever call Read() after Stop():
    466     EXPECT_TRUE(stop_decoder_cb_.is_null());
    467 
    468     // TODO(scherkus): Make this a DCHECK after threading semantics are fixed.
    469     if (base::MessageLoop::current() != &message_loop_) {
    470       message_loop_.PostTask(FROM_HERE, base::Bind(
    471           &AudioRendererImplTest::DecodeDecoder,
    472           base::Unretained(this), buffer, decode_cb));
    473       return;
    474     }
    475 
    476     CHECK(decode_cb_.is_null()) << "Overlapping decodes are not permitted";
    477     decode_cb_ = decode_cb;
    478 
    479     // Wake up WaitForPendingRead() if needed.
    480     if (!wait_for_pending_decode_cb_.is_null())
    481       base::ResetAndReturn(&wait_for_pending_decode_cb_).Run();
    482   }
    483 
    484   void ResetDecoder(const base::Closure& reset_cb) {
    485     if (!decode_cb_.is_null()) {
    486       // |reset_cb| will be called in DeliverBuffer(), after the decoder is
    487       // flushed.
    488       reset_cb_ = reset_cb;
    489       return;
    490     }
    491 
    492     message_loop_.PostTask(FROM_HERE, reset_cb);
    493   }
    494 
    495   void DeliverBuffer(AudioDecoder::Status status,
    496                      const scoped_refptr<AudioBuffer>& buffer) {
    497     CHECK(!decode_cb_.is_null());
    498     if (buffer && !buffer->end_of_stream())
    499       output_cb_.Run(buffer);
    500     base::ResetAndReturn(&decode_cb_).Run(status);
    501 
    502     if (!reset_cb_.is_null())
    503       base::ResetAndReturn(&reset_cb_).Run();
    504 
    505     message_loop_.RunUntilIdle();
    506   }
    507 
    508   MockDemuxerStream demuxer_stream_;
    509   MockAudioDecoder* decoder_;
    510 
    511   // Used for stubbing out time in the audio callback thread.
    512   base::Lock lock_;
    513   TimeTicks time_;
    514 
    515   // Used for satisfying reads.
    516   AudioDecoder::OutputCB output_cb_;
    517   AudioDecoder::DecodeCB decode_cb_;
    518   base::Closure reset_cb_;
    519   scoped_ptr<AudioTimestampHelper> next_timestamp_;
    520 
    521   WaitableMessageLoopEvent ended_event_;
    522 
    523   // Run during DecodeDecoder() to unblock WaitForPendingRead().
    524   base::Closure wait_for_pending_decode_cb_;
    525   base::Closure stop_decoder_cb_;
    526 
    527   PipelineStatusCB init_decoder_cb_;
    528   base::TimeDelta last_time_update_;
    529 
    530   DISALLOW_COPY_AND_ASSIGN(AudioRendererImplTest);
    531 };
    532 
    533 TEST_F(AudioRendererImplTest, Initialize_Successful) {
    534   Initialize();
    535 }
    536 
    537 TEST_F(AudioRendererImplTest, Initialize_DecoderInitFailure) {
    538   ExpectUnsupportedAudioDecoder();
    539   InitializeWithStatus(DECODER_ERROR_NOT_SUPPORTED);
    540 }
    541 
    542 TEST_F(AudioRendererImplTest, Preroll) {
    543   Initialize();
    544   Preroll();
    545 }
    546 
    547 TEST_F(AudioRendererImplTest, StartRendering) {
    548   Initialize();
    549   Preroll();
    550   StartRendering();
    551 
    552   // Drain internal buffer, we should have a pending read.
    553   EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL));
    554   WaitForPendingRead();
    555 }
    556 
    557 TEST_F(AudioRendererImplTest, EndOfStream) {
    558   EndOfStreamTest(1.0);
    559 }
    560 
    561 TEST_F(AudioRendererImplTest, EndOfStream_FasterPlaybackSpeed) {
    562   EndOfStreamTest(2.0);
    563 }
    564 
    565 TEST_F(AudioRendererImplTest, EndOfStream_SlowerPlaybackSpeed) {
    566   EndOfStreamTest(0.5);
    567 }
    568 
    569 TEST_F(AudioRendererImplTest, Underflow) {
    570   Initialize();
    571   Preroll();
    572 
    573   int initial_capacity = buffer_capacity();
    574 
    575   StartRendering();
    576 
    577   // Drain internal buffer, we should have a pending read.
    578   EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL));
    579   WaitForPendingRead();
    580 
    581   // Verify the next FillBuffer() call triggers the underflow callback
    582   // since the decoder hasn't delivered any data after it was drained.
    583   EXPECT_CALL(*this, OnUnderflow());
    584   EXPECT_FALSE(ConsumeBufferedData(kDataSize, NULL));
    585 
    586   renderer_->ResumeAfterUnderflow();
    587 
    588   // Verify after resuming that we're still not getting data.
    589   bool muted = false;
    590   EXPECT_EQ(0, frames_buffered());
    591   EXPECT_FALSE(ConsumeBufferedData(kDataSize, &muted));
    592   EXPECT_TRUE(muted);
    593 
    594   // Verify that the buffer capacity increased as a result of the underflow.
    595   EXPECT_GT(buffer_capacity(), initial_capacity);
    596 
    597   // Deliver data, we should get non-muted audio.
    598   DeliverRemainingAudio();
    599   EXPECT_TRUE(ConsumeBufferedData(kDataSize, &muted));
    600   EXPECT_FALSE(muted);
    601 }
    602 
    603 TEST_F(AudioRendererImplTest, Underflow_CapacityResetsAfterFlush) {
    604   Initialize();
    605   Preroll();
    606 
    607   int initial_capacity = buffer_capacity();
    608 
    609   StartRendering();
    610 
    611   // Drain internal buffer, we should have a pending read.
    612   EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL));
    613   WaitForPendingRead();
    614 
    615   // Verify the next FillBuffer() call triggers the underflow callback
    616   // since the decoder hasn't delivered any data after it was drained.
    617   EXPECT_CALL(*this, OnUnderflow());
    618   EXPECT_FALSE(ConsumeBufferedData(kDataSize, NULL));
    619 
    620   // Verify that the buffer capacity increased as a result of resuming after
    621   // underflow.
    622   EXPECT_EQ(buffer_capacity(), initial_capacity);
    623   renderer_->ResumeAfterUnderflow();
    624   EXPECT_GT(buffer_capacity(), initial_capacity);
    625 
    626   // Verify that the buffer capacity is restored to the |initial_capacity|.
    627   DeliverEndOfStream();
    628   Flush();
    629   EXPECT_EQ(buffer_capacity(), initial_capacity);
    630 }
    631 
    632 TEST_F(AudioRendererImplTest, Underflow_FlushWhileUnderflowed) {
    633   Initialize();
    634   Preroll();
    635   StartRendering();
    636 
    637   // Drain internal buffer, we should have a pending read.
    638   EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL));
    639   WaitForPendingRead();
    640 
    641   // Verify the next FillBuffer() call triggers the underflow callback
    642   // since the decoder hasn't delivered any data after it was drained.
    643   EXPECT_CALL(*this, OnUnderflow());
    644   EXPECT_FALSE(ConsumeBufferedData(kDataSize, NULL));
    645 
    646   // Verify that we can still Flush() before entering the rebuffering state.
    647   DeliverEndOfStream();
    648   Flush();
    649 }
    650 
    651 TEST_F(AudioRendererImplTest, Underflow_EndOfStream) {
    652   Initialize();
    653   Preroll();
    654   StartRendering();
    655 
    656   // Figure out how long until the ended event should fire.  Since
    657   // ConsumeBufferedData() doesn't provide audio delay information, the time
    658   // until the ended event fires is equivalent to the longest buffered section,
    659   // which is the initial frames_buffered() read.
    660   TimeDelta time_until_ended = CalculatePlayTime(frames_buffered());
    661 
    662   // Drain internal buffer, we should have a pending read.
    663   EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL));
    664   WaitForPendingRead();
    665 
    666   // Verify the next FillBuffer() call triggers the underflow callback
    667   // since the decoder hasn't delivered any data after it was drained.
    668   EXPECT_CALL(*this, OnUnderflow());
    669   EXPECT_FALSE(ConsumeBufferedData(kDataSize, NULL));
    670 
    671   // Deliver a little bit of data.
    672   SatisfyPendingRead(kDataSize);
    673   WaitForPendingRead();
    674 
    675   // Verify we're getting muted audio during underflow.  Note: Since resampling
    676   // is active, the number of frames_buffered() won't always match kDataSize.
    677   bool muted = false;
    678   const int kInitialFramesBuffered = 1114;
    679   EXPECT_EQ(kInitialFramesBuffered, frames_buffered());
    680   EXPECT_FALSE(ConsumeBufferedData(kInitialFramesBuffered, &muted));
    681   EXPECT_TRUE(muted);
    682 
    683   // Now deliver end of stream, we should get our little bit of data back.
    684   DeliverEndOfStream();
    685   const int kNextFramesBuffered = 1408;
    686   EXPECT_EQ(kNextFramesBuffered, frames_buffered());
    687   EXPECT_TRUE(ConsumeBufferedData(kNextFramesBuffered, &muted));
    688   EXPECT_FALSE(muted);
    689 
    690   // Attempt to read to make sure we're truly at the end of stream.
    691   AdvanceTime(time_until_ended);
    692   EXPECT_FALSE(ConsumeBufferedData(kDataSize, &muted));
    693   EXPECT_TRUE(muted);
    694   WaitForEnded();
    695 }
    696 
    697 TEST_F(AudioRendererImplTest, Underflow_ResumeFromCallback) {
    698   Initialize();
    699   Preroll();
    700   StartRendering();
    701 
    702   // Drain internal buffer, we should have a pending read.
    703   EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL));
    704   WaitForPendingRead();
    705 
    706   // Verify the next FillBuffer() call triggers the underflow callback
    707   // since the decoder hasn't delivered any data after it was drained.
    708   EXPECT_CALL(*this, OnUnderflow())
    709       .WillOnce(Invoke(this, &AudioRendererImplTest::CallResumeAfterUnderflow));
    710   EXPECT_FALSE(ConsumeBufferedData(kDataSize, NULL));
    711 
    712   // Verify after resuming that we're still not getting data.
    713   bool muted = false;
    714   EXPECT_EQ(0, frames_buffered());
    715   EXPECT_FALSE(ConsumeBufferedData(kDataSize, &muted));
    716   EXPECT_TRUE(muted);
    717 
    718   // Deliver data, we should get non-muted audio.
    719   DeliverRemainingAudio();
    720   EXPECT_TRUE(ConsumeBufferedData(kDataSize, &muted));
    721   EXPECT_FALSE(muted);
    722 }
    723 
    724 TEST_F(AudioRendererImplTest, Underflow_SetPlaybackRate) {
    725   Initialize();
    726   Preroll();
    727   StartRendering();
    728 
    729   // Drain internal buffer, we should have a pending read.
    730   EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL));
    731   WaitForPendingRead();
    732 
    733   EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
    734 
    735   // Verify the next FillBuffer() call triggers the underflow callback
    736   // since the decoder hasn't delivered any data after it was drained.
    737   EXPECT_CALL(*this, OnUnderflow())
    738       .WillOnce(Invoke(this, &AudioRendererImplTest::CallResumeAfterUnderflow));
    739   EXPECT_FALSE(ConsumeBufferedData(kDataSize, NULL));
    740   EXPECT_EQ(0, frames_buffered());
    741 
    742   EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
    743 
    744   // Simulate playback being paused.
    745   renderer_->SetPlaybackRate(0);
    746 
    747   EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
    748 
    749   // Deliver data to resolve the underflow.
    750   DeliverRemainingAudio();
    751 
    752   EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
    753 
    754   // Simulate playback being resumed.
    755   renderer_->SetPlaybackRate(1);
    756 
    757   EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
    758 }
    759 
    760 TEST_F(AudioRendererImplTest, Underflow_PausePlay) {
    761   Initialize();
    762   Preroll();
    763   StartRendering();
    764 
    765   // Drain internal buffer, we should have a pending read.
    766   EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL));
    767   WaitForPendingRead();
    768 
    769   EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
    770 
    771   // Verify the next FillBuffer() call triggers the underflow callback
    772   // since the decoder hasn't delivered any data after it was drained.
    773   EXPECT_CALL(*this, OnUnderflow())
    774       .WillOnce(Invoke(this, &AudioRendererImplTest::CallResumeAfterUnderflow));
    775   EXPECT_FALSE(ConsumeBufferedData(kDataSize, NULL));
    776   EXPECT_EQ(0, frames_buffered());
    777 
    778   EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
    779 
    780   // Simulate playback being paused, and then played again.
    781   renderer_->SetPlaybackRate(0.0);
    782   renderer_->SetPlaybackRate(1.0);
    783 
    784   // Deliver data to resolve the underflow.
    785   DeliverRemainingAudio();
    786 
    787   // We should have resumed playing now.
    788   EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
    789 }
    790 
    791 TEST_F(AudioRendererImplTest, PendingRead_Flush) {
    792   Initialize();
    793 
    794   Preroll();
    795   StartRendering();
    796 
    797   // Partially drain internal buffer so we get a pending read.
    798   EXPECT_TRUE(ConsumeBufferedData(frames_buffered() / 2, NULL));
    799   WaitForPendingRead();
    800 
    801   StopRendering();
    802 
    803   EXPECT_TRUE(IsReadPending());
    804 
    805   // Start flushing.
    806   WaitableMessageLoopEvent flush_event;
    807   renderer_->Flush(flush_event.GetClosure());
    808 
    809   SatisfyPendingRead(kDataSize);
    810 
    811   flush_event.RunAndWait();
    812 
    813   EXPECT_FALSE(IsReadPending());
    814 
    815   // Preroll again to a different timestamp and verify it completed normally.
    816   Preroll(1000, PIPELINE_OK);
    817 }
    818 
    819 TEST_F(AudioRendererImplTest, PendingRead_Stop) {
    820   Initialize();
    821 
    822   Preroll();
    823   StartRendering();
    824 
    825   // Partially drain internal buffer so we get a pending read.
    826   EXPECT_TRUE(ConsumeBufferedData(frames_buffered() / 2, NULL));
    827   WaitForPendingRead();
    828 
    829   StopRendering();
    830 
    831   EXPECT_TRUE(IsReadPending());
    832 
    833   WaitableMessageLoopEvent stop_event;
    834   renderer_->Stop(stop_event.GetClosure());
    835   needs_stop_ = false;
    836 
    837   SatisfyPendingRead(kDataSize);
    838 
    839   stop_event.RunAndWait();
    840 
    841   EXPECT_FALSE(IsReadPending());
    842 }
    843 
    844 TEST_F(AudioRendererImplTest, PendingFlush_Stop) {
    845   Initialize();
    846 
    847   Preroll();
    848   StartRendering();
    849 
    850   // Partially drain internal buffer so we get a pending read.
    851   EXPECT_TRUE(ConsumeBufferedData(frames_buffered() / 2, NULL));
    852   WaitForPendingRead();
    853 
    854   StopRendering();
    855 
    856   EXPECT_TRUE(IsReadPending());
    857 
    858   // Start flushing.
    859   WaitableMessageLoopEvent flush_event;
    860   renderer_->Flush(flush_event.GetClosure());
    861 
    862   SatisfyPendingRead(kDataSize);
    863 
    864   WaitableMessageLoopEvent event;
    865   renderer_->Stop(event.GetClosure());
    866   event.RunAndWait();
    867   needs_stop_ = false;
    868 }
    869 
    870 TEST_F(AudioRendererImplTest, InitializeThenStop) {
    871   InitializeAndStop();
    872 }
    873 
    874 TEST_F(AudioRendererImplTest, InitializeThenStopDuringDecoderInit) {
    875   InitializeAndStopDuringDecoderInit();
    876 }
    877 
    878 TEST_F(AudioRendererImplTest, ConfigChangeDrainsConverter) {
    879   Initialize();
    880   Preroll();
    881   StartRendering();
    882 
    883   // Drain internal buffer, we should have a pending read.
    884   EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL));
    885   WaitForPendingRead();
    886 
    887   // Deliver a little bit of data.  Use an odd data size to ensure there is data
    888   // left in the AudioBufferConverter.  Ensure no buffers are in the splicer.
    889   SatisfyPendingRead(2053);
    890   EXPECT_FALSE(splicer_has_next_buffer());
    891   EXPECT_GT(converter_input_frames_left(), 0);
    892 
    893   // Force a config change and then ensure all buffered data has been put into
    894   // the splicer.
    895   force_config_change();
    896   EXPECT_TRUE(splicer_has_next_buffer());
    897   EXPECT_EQ(0, converter_input_frames_left());
    898 }
    899 
    900 TEST_F(AudioRendererImplTest, TimeUpdatesOnFirstBuffer) {
    901   Initialize();
    902   Preroll();
    903   StartRendering();
    904 
    905   AudioTimestampHelper timestamp_helper(kOutputSamplesPerSecond);
    906   EXPECT_EQ(kNoTimestamp(), last_time_update());
    907 
    908   // Preroll() should be buffered some data, consume half of it now.
    909   int frames_to_consume = frames_buffered() / 2;
    910   EXPECT_TRUE(ConsumeBufferedData(frames_to_consume, NULL));
    911   WaitForPendingRead();
    912   base::RunLoop().RunUntilIdle();
    913 
    914   // ConsumeBufferedData() uses an audio delay of zero, so ensure we received
    915   // a time update that's equal to |kFramesToConsume| from above.
    916   timestamp_helper.SetBaseTimestamp(base::TimeDelta());
    917   timestamp_helper.AddFrames(frames_to_consume);
    918   EXPECT_EQ(timestamp_helper.GetTimestamp(), last_time_update());
    919 
    920   // The next time update should match the remaining frames_buffered(), but only
    921   // after running the message loop.
    922   frames_to_consume = frames_buffered();
    923   EXPECT_TRUE(ConsumeBufferedData(frames_to_consume, NULL));
    924   EXPECT_EQ(timestamp_helper.GetTimestamp(), last_time_update());
    925 
    926   base::RunLoop().RunUntilIdle();
    927   timestamp_helper.AddFrames(frames_to_consume);
    928   EXPECT_EQ(timestamp_helper.GetTimestamp(), last_time_update());
    929 }
    930 
    931 TEST_F(AudioRendererImplTest, ImmediateEndOfStream) {
    932   Initialize();
    933   {
    934     SCOPED_TRACE("Preroll()");
    935     WaitableMessageLoopEvent event;
    936     renderer_->Preroll(base::TimeDelta(), event.GetPipelineStatusCB());
    937     WaitForPendingRead();
    938     DeliverEndOfStream();
    939     event.RunAndWaitForStatus(PIPELINE_OK);
    940   }
    941   StartRendering();
    942 
    943   // Read a single frame. We shouldn't be able to satisfy it.
    944   EXPECT_FALSE(ConsumeBufferedData(1, NULL));
    945   WaitForEnded();
    946 }
    947 
    948 TEST_F(AudioRendererImplTest, OnRenderErrorCausesDecodeError) {
    949   Initialize();
    950   Preroll();
    951   StartRendering();
    952 
    953   EXPECT_CALL(*this, OnError(PIPELINE_ERROR_DECODE));
    954   sink_->OnRenderError();
    955 }
    956 
    957 // Test for AudioRendererImpl calling Pause()/Play() on the sink when the
    958 // playback rate is set to zero and non-zero.
    959 TEST_F(AudioRendererImplTest, SetPlaybackRate) {
    960   Initialize();
    961   Preroll();
    962 
    963   // Rendering hasn't started. Sink should always be paused.
    964   EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
    965   renderer_->SetPlaybackRate(0.0f);
    966   EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
    967   renderer_->SetPlaybackRate(1.0f);
    968   EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
    969 
    970   // Rendering has started with non-zero rate. Rate changes will affect sink
    971   // state.
    972   renderer_->StartRendering();
    973   EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
    974   renderer_->SetPlaybackRate(0.0f);
    975   EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
    976   renderer_->SetPlaybackRate(1.0f);
    977   EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
    978 
    979   // Rendering has stopped. Sink should be paused.
    980   renderer_->StopRendering();
    981   EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
    982 
    983   // Start rendering with zero playback rate. Sink should be paused until
    984   // non-zero rate is set.
    985   renderer_->SetPlaybackRate(0.0f);
    986   renderer_->StartRendering();
    987   EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
    988   renderer_->SetPlaybackRate(1.0f);
    989   EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
    990 }
    991 
    992 }  // namespace media
    993