Switch TestFrameNavigationObserver to DidCommitProvisionalLoadForFrame.
[chromium-blink-merge.git] / media / renderers / audio_renderer_impl_unittest.cc
blob119fb76b464915a3cd8ed0d432f2c3d211c30a74
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.
5 #include "base/bind.h"
6 #include "base/callback_helpers.h"
7 #include "base/format_macros.h"
8 #include "base/run_loop.h"
9 #include "base/strings/stringprintf.h"
10 #include "media/base/audio_buffer_converter.h"
11 #include "media/base/audio_hardware_config.h"
12 #include "media/base/audio_splicer.h"
13 #include "media/base/fake_audio_renderer_sink.h"
14 #include "media/base/gmock_callback_support.h"
15 #include "media/base/mock_filters.h"
16 #include "media/base/test_helpers.h"
17 #include "media/renderers/audio_renderer_impl.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 using ::base::TimeDelta;
21 using ::testing::_;
22 using ::testing::Return;
23 using ::testing::SaveArg;
25 namespace media {
27 namespace {
29 // Since AudioBufferConverter is used due to different input/output sample
30 // rates, define some helper types to differentiate between the two.
31 struct InputFrames {
32 explicit InputFrames(int value) : value(value) {}
33 int value;
36 struct OutputFrames {
37 explicit OutputFrames(int value) : value(value) {}
38 int value;
41 } // namespace
43 // Constants to specify the type of audio data used.
44 static AudioCodec kCodec = kCodecVorbis;
45 static SampleFormat kSampleFormat = kSampleFormatPlanarF32;
46 static ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO;
47 static int kChannelCount = 2;
48 static int kChannels = ChannelLayoutToChannelCount(kChannelLayout);
50 // Use a different output sample rate so the AudioBufferConverter is invoked.
51 static int kInputSamplesPerSecond = 5000;
52 static int kOutputSamplesPerSecond = 10000;
54 ACTION_P(EnterPendingDecoderInitStateAction, test) {
55 test->EnterPendingDecoderInitState(arg1);
58 class AudioRendererImplTest : public ::testing::Test {
59 public:
60 // Give the decoder some non-garbage media properties.
61 AudioRendererImplTest()
62 : hardware_config_(AudioParameters(), AudioParameters()),
63 demuxer_stream_(DemuxerStream::AUDIO),
64 decoder_(new MockAudioDecoder()),
65 ended_(false) {
66 AudioDecoderConfig audio_config(kCodec,
67 kSampleFormat,
68 kChannelLayout,
69 kInputSamplesPerSecond,
70 NULL,
72 false);
73 demuxer_stream_.set_audio_decoder_config(audio_config);
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));
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_.get(),
98 decoders.Pass(),
99 hardware_config_,
100 new MediaLog()));
103 virtual ~AudioRendererImplTest() {
104 SCOPED_TRACE("~AudioRendererImplTest()");
107 void ExpectUnsupportedAudioDecoder() {
108 EXPECT_CALL(*decoder_, Initialize(_, _, _))
109 .WillOnce(DoAll(SaveArg<2>(&output_cb_),
110 RunCallback<1>(DECODER_ERROR_NOT_SUPPORTED)));
113 MOCK_METHOD1(OnStatistics, void(const PipelineStatistics&));
114 MOCK_METHOD1(OnBufferingStateChange, void(BufferingState));
115 MOCK_METHOD1(OnError, void(PipelineStatus));
116 MOCK_METHOD0(OnWaitingForDecryptionKey, void(void));
118 void InitializeRenderer(const PipelineStatusCB& pipeline_status_cb) {
119 EXPECT_CALL(*this, OnWaitingForDecryptionKey()).Times(0);
120 renderer_->Initialize(
121 &demuxer_stream_, pipeline_status_cb, SetDecryptorReadyCB(),
122 base::Bind(&AudioRendererImplTest::OnStatistics,
123 base::Unretained(this)),
124 base::Bind(&AudioRendererImplTest::OnBufferingStateChange,
125 base::Unretained(this)),
126 base::Bind(&AudioRendererImplTest::OnEnded, base::Unretained(this)),
127 base::Bind(&AudioRendererImplTest::OnError, base::Unretained(this)),
128 base::Bind(&AudioRendererImplTest::OnWaitingForDecryptionKey,
129 base::Unretained(this)));
132 void Initialize() {
133 EXPECT_CALL(*decoder_, Initialize(_, _, _))
134 .WillOnce(DoAll(SaveArg<2>(&output_cb_),
135 RunCallback<1>(PIPELINE_OK)));
136 InitializeWithStatus(PIPELINE_OK);
138 next_timestamp_.reset(new AudioTimestampHelper(kInputSamplesPerSecond));
141 void InitializeWithStatus(PipelineStatus expected) {
142 SCOPED_TRACE(base::StringPrintf("InitializeWithStatus(%d)", expected));
144 WaitableMessageLoopEvent event;
145 InitializeRenderer(event.GetPipelineStatusCB());
146 event.RunAndWaitForStatus(expected);
148 // We should have no reads.
149 EXPECT_TRUE(decode_cb_.is_null());
152 void InitializeAndDestroy() {
153 EXPECT_CALL(*decoder_, Initialize(_, _, _))
154 .WillOnce(RunCallback<1>(PIPELINE_OK));
156 WaitableMessageLoopEvent event;
157 InitializeRenderer(event.GetPipelineStatusCB());
159 // Destroy the |renderer_| before we let the MessageLoop run, this simulates
160 // an interleaving in which we end up destroying the |renderer_| while the
161 // OnDecoderSelected callback is in flight.
162 renderer_.reset();
163 event.RunAndWaitForStatus(PIPELINE_ERROR_ABORT);
166 void InitializeAndDestroyDuringDecoderInit() {
167 EXPECT_CALL(*decoder_, Initialize(_, _, _))
168 .WillOnce(EnterPendingDecoderInitStateAction(this));
170 WaitableMessageLoopEvent event;
171 InitializeRenderer(event.GetPipelineStatusCB());
172 base::RunLoop().RunUntilIdle();
173 DCHECK(!init_decoder_cb_.is_null());
175 renderer_.reset();
176 event.RunAndWaitForStatus(PIPELINE_ERROR_ABORT);
179 void EnterPendingDecoderInitState(PipelineStatusCB cb) {
180 init_decoder_cb_ = cb;
183 void FlushDuringPendingRead() {
184 SCOPED_TRACE("FlushDuringPendingRead()");
185 WaitableMessageLoopEvent flush_event;
186 renderer_->Flush(flush_event.GetClosure());
187 SatisfyPendingRead(InputFrames(256));
188 flush_event.RunAndWait();
190 EXPECT_FALSE(IsReadPending());
193 void Preroll() {
194 Preroll(base::TimeDelta(), base::TimeDelta(), PIPELINE_OK);
197 void Preroll(base::TimeDelta start_timestamp,
198 base::TimeDelta first_timestamp,
199 PipelineStatus expected) {
200 SCOPED_TRACE(base::StringPrintf("Preroll(%" PRId64 ", %d)",
201 first_timestamp.InMilliseconds(),
202 expected));
203 next_timestamp_->SetBaseTimestamp(first_timestamp);
205 // Fill entire buffer to complete prerolling.
206 renderer_->SetMediaTime(start_timestamp);
207 renderer_->StartPlaying();
208 WaitForPendingRead();
209 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH));
210 DeliverRemainingAudio();
213 void StartTicking() {
214 renderer_->StartTicking();
215 renderer_->SetPlaybackRate(1.0);
218 void StopTicking() { renderer_->StopTicking(); }
220 bool IsReadPending() const {
221 return !decode_cb_.is_null();
224 void WaitForPendingRead() {
225 SCOPED_TRACE("WaitForPendingRead()");
226 if (!decode_cb_.is_null())
227 return;
229 DCHECK(wait_for_pending_decode_cb_.is_null());
231 WaitableMessageLoopEvent event;
232 wait_for_pending_decode_cb_ = event.GetClosure();
233 event.RunAndWait();
235 DCHECK(!decode_cb_.is_null());
236 DCHECK(wait_for_pending_decode_cb_.is_null());
239 // Delivers decoded frames to |renderer_|.
240 void SatisfyPendingRead(InputFrames frames) {
241 CHECK_GT(frames.value, 0);
242 CHECK(!decode_cb_.is_null());
244 scoped_refptr<AudioBuffer> buffer =
245 MakeAudioBuffer<float>(kSampleFormat,
246 kChannelLayout,
247 kChannelCount,
248 kInputSamplesPerSecond,
249 1.0f,
250 0.0f,
251 frames.value,
252 next_timestamp_->GetTimestamp());
253 next_timestamp_->AddFrames(frames.value);
255 DeliverBuffer(AudioDecoder::kOk, buffer);
258 void DeliverEndOfStream() {
259 DCHECK(!decode_cb_.is_null());
261 // Return EOS buffer to trigger EOS frame.
262 EXPECT_CALL(demuxer_stream_, Read(_))
263 .WillOnce(RunCallback<0>(DemuxerStream::kOk,
264 DecoderBuffer::CreateEOSBuffer()));
266 // Satify pending |decode_cb_| to trigger a new DemuxerStream::Read().
267 message_loop_.PostTask(
268 FROM_HERE,
269 base::Bind(base::ResetAndReturn(&decode_cb_), AudioDecoder::kOk));
271 WaitForPendingRead();
273 message_loop_.PostTask(
274 FROM_HERE,
275 base::Bind(base::ResetAndReturn(&decode_cb_), AudioDecoder::kOk));
277 base::RunLoop().RunUntilIdle();
280 // Delivers frames until |renderer_|'s internal buffer is full and no longer
281 // has pending reads.
282 void DeliverRemainingAudio() {
283 while (frames_remaining_in_buffer().value > 0) {
284 SatisfyPendingRead(InputFrames(256));
288 // Attempts to consume |requested_frames| frames from |renderer_|'s internal
289 // buffer. Returns true if and only if all of |requested_frames| were able
290 // to be consumed.
291 bool ConsumeBufferedData(OutputFrames requested_frames) {
292 scoped_ptr<AudioBus> bus =
293 AudioBus::Create(kChannels, requested_frames.value);
294 int frames_read = 0;
295 EXPECT_TRUE(sink_->Render(bus.get(), 0, &frames_read));
296 return frames_read == requested_frames.value;
299 OutputFrames frames_buffered() {
300 return OutputFrames(renderer_->algorithm_->frames_buffered());
303 OutputFrames buffer_capacity() {
304 return OutputFrames(renderer_->algorithm_->QueueCapacity());
307 OutputFrames frames_remaining_in_buffer() {
308 // This can happen if too much data was delivered, in which case the buffer
309 // will accept the data but not increase capacity.
310 if (frames_buffered().value > buffer_capacity().value) {
311 return OutputFrames(0);
313 return OutputFrames(buffer_capacity().value - frames_buffered().value);
316 void force_config_change() {
317 renderer_->OnConfigChange();
320 InputFrames converter_input_frames_left() const {
321 return InputFrames(
322 renderer_->buffer_converter_->input_frames_left_for_testing());
325 bool splicer_has_next_buffer() const {
326 return renderer_->splicer_->HasNextBuffer();
329 base::TimeDelta CurrentMediaTime() {
330 return renderer_->CurrentMediaTime();
333 bool ended() const { return ended_; }
335 // Fixture members.
336 base::MessageLoop message_loop_;
337 scoped_ptr<AudioRendererImpl> renderer_;
338 scoped_refptr<FakeAudioRendererSink> sink_;
339 AudioHardwareConfig hardware_config_;
341 private:
342 void DecodeDecoder(const scoped_refptr<DecoderBuffer>& buffer,
343 const AudioDecoder::DecodeCB& decode_cb) {
344 // TODO(scherkus): Make this a DCHECK after threading semantics are fixed.
345 if (base::MessageLoop::current() != &message_loop_) {
346 message_loop_.PostTask(FROM_HERE, base::Bind(
347 &AudioRendererImplTest::DecodeDecoder,
348 base::Unretained(this), buffer, decode_cb));
349 return;
352 CHECK(decode_cb_.is_null()) << "Overlapping decodes are not permitted";
353 decode_cb_ = decode_cb;
355 // Wake up WaitForPendingRead() if needed.
356 if (!wait_for_pending_decode_cb_.is_null())
357 base::ResetAndReturn(&wait_for_pending_decode_cb_).Run();
360 void ResetDecoder(const base::Closure& reset_cb) {
361 if (!decode_cb_.is_null()) {
362 // |reset_cb| will be called in DeliverBuffer(), after the decoder is
363 // flushed.
364 reset_cb_ = reset_cb;
365 return;
368 message_loop_.PostTask(FROM_HERE, reset_cb);
371 void DeliverBuffer(AudioDecoder::Status status,
372 const scoped_refptr<AudioBuffer>& buffer) {
373 CHECK(!decode_cb_.is_null());
374 if (buffer.get() && !buffer->end_of_stream())
375 output_cb_.Run(buffer);
376 base::ResetAndReturn(&decode_cb_).Run(status);
378 if (!reset_cb_.is_null())
379 base::ResetAndReturn(&reset_cb_).Run();
381 base::RunLoop().RunUntilIdle();
384 void OnEnded() {
385 CHECK(!ended_);
386 ended_ = true;
389 MockDemuxerStream demuxer_stream_;
390 MockAudioDecoder* decoder_;
392 // Used for satisfying reads.
393 AudioDecoder::OutputCB output_cb_;
394 AudioDecoder::DecodeCB decode_cb_;
395 base::Closure reset_cb_;
396 scoped_ptr<AudioTimestampHelper> next_timestamp_;
398 // Run during DecodeDecoder() to unblock WaitForPendingRead().
399 base::Closure wait_for_pending_decode_cb_;
401 PipelineStatusCB init_decoder_cb_;
402 bool ended_;
404 DISALLOW_COPY_AND_ASSIGN(AudioRendererImplTest);
407 TEST_F(AudioRendererImplTest, Initialize_Successful) {
408 Initialize();
411 TEST_F(AudioRendererImplTest, Initialize_DecoderInitFailure) {
412 ExpectUnsupportedAudioDecoder();
413 InitializeWithStatus(DECODER_ERROR_NOT_SUPPORTED);
416 TEST_F(AudioRendererImplTest, Preroll) {
417 Initialize();
418 Preroll();
421 TEST_F(AudioRendererImplTest, StartTicking) {
422 Initialize();
423 Preroll();
424 StartTicking();
426 // Drain internal buffer, we should have a pending read.
427 EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
428 WaitForPendingRead();
431 TEST_F(AudioRendererImplTest, EndOfStream) {
432 Initialize();
433 Preroll();
434 StartTicking();
436 // Drain internal buffer, we should have a pending read.
437 EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
438 WaitForPendingRead();
440 // Forcefully trigger underflow.
441 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
442 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING));
444 // Fulfill the read with an end-of-stream buffer. Doing so should change our
445 // buffering state so playback resumes.
446 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH));
447 DeliverEndOfStream();
449 // Consume all remaining data. We shouldn't have signal ended yet.
450 EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
451 base::RunLoop().RunUntilIdle();
452 EXPECT_FALSE(ended());
454 // Ended should trigger on next render call.
455 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
456 base::RunLoop().RunUntilIdle();
457 EXPECT_TRUE(ended());
460 TEST_F(AudioRendererImplTest, Underflow) {
461 Initialize();
462 Preroll();
463 StartTicking();
465 // Drain internal buffer, we should have a pending read.
466 EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
467 WaitForPendingRead();
469 // Verify the next FillBuffer() call triggers a buffering state change
470 // update.
471 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING));
472 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
474 // Verify we're still not getting audio data.
475 EXPECT_EQ(0, frames_buffered().value);
476 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
478 // Deliver enough data to have enough for buffering.
479 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH));
480 DeliverRemainingAudio();
482 // Verify we're getting audio data.
483 EXPECT_TRUE(ConsumeBufferedData(OutputFrames(1)));
486 TEST_F(AudioRendererImplTest, Underflow_CapacityResetsAfterFlush) {
487 Initialize();
488 Preroll();
489 StartTicking();
491 // Drain internal buffer, we should have a pending read.
492 EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
493 WaitForPendingRead();
495 // Verify the next FillBuffer() call triggers the underflow callback
496 // since the decoder hasn't delivered any data after it was drained.
497 OutputFrames initial_capacity = buffer_capacity();
498 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING));
499 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
501 // Verify that the buffer capacity increased as a result of underflowing.
502 EXPECT_GT(buffer_capacity().value, initial_capacity.value);
504 // Verify that the buffer capacity is restored to the |initial_capacity|.
505 FlushDuringPendingRead();
506 EXPECT_EQ(buffer_capacity().value, initial_capacity.value);
509 TEST_F(AudioRendererImplTest, Underflow_Flush) {
510 Initialize();
511 Preroll();
512 StartTicking();
514 // Force underflow.
515 EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
516 WaitForPendingRead();
517 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING));
518 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
519 WaitForPendingRead();
520 StopTicking();
522 // After time stops ticking wall clock times should not be returned.
523 EXPECT_FALSE(
524 renderer_->GetWallClockTimes(std::vector<base::TimeDelta>(1), nullptr));
526 // We shouldn't expect another buffering state change when flushing.
527 FlushDuringPendingRead();
530 TEST_F(AudioRendererImplTest, PendingRead_Flush) {
531 Initialize();
533 Preroll();
534 StartTicking();
536 // Partially drain internal buffer so we get a pending read.
537 EXPECT_TRUE(ConsumeBufferedData(OutputFrames(256)));
538 WaitForPendingRead();
540 StopTicking();
542 EXPECT_TRUE(IsReadPending());
544 // Flush and expect to be notified that we have nothing.
545 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING));
546 FlushDuringPendingRead();
548 // Preroll again to a different timestamp and verify it completed normally.
549 const base::TimeDelta seek_timestamp =
550 base::TimeDelta::FromMilliseconds(1000);
551 Preroll(seek_timestamp, seek_timestamp, PIPELINE_OK);
554 TEST_F(AudioRendererImplTest, PendingRead_Destroy) {
555 Initialize();
557 Preroll();
558 StartTicking();
560 // Partially drain internal buffer so we get a pending read.
561 EXPECT_TRUE(ConsumeBufferedData(OutputFrames(256)));
562 WaitForPendingRead();
564 StopTicking();
566 EXPECT_TRUE(IsReadPending());
568 renderer_.reset();
571 TEST_F(AudioRendererImplTest, PendingFlush_Destroy) {
572 Initialize();
574 Preroll();
575 StartTicking();
577 // Partially drain internal buffer so we get a pending read.
578 EXPECT_TRUE(ConsumeBufferedData(OutputFrames(256)));
579 WaitForPendingRead();
581 StopTicking();
583 EXPECT_TRUE(IsReadPending());
585 // Start flushing.
586 WaitableMessageLoopEvent flush_event;
587 renderer_->Flush(flush_event.GetClosure());
589 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING));
590 SatisfyPendingRead(InputFrames(256));
592 renderer_.reset();
595 TEST_F(AudioRendererImplTest, InitializeThenDestroy) {
596 InitializeAndDestroy();
599 TEST_F(AudioRendererImplTest, InitializeThenDestroyDuringDecoderInit) {
600 InitializeAndDestroyDuringDecoderInit();
603 TEST_F(AudioRendererImplTest, ConfigChangeDrainsConverter) {
604 Initialize();
605 Preroll();
606 StartTicking();
608 // Drain internal buffer, we should have a pending read.
609 EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
610 WaitForPendingRead();
612 // Deliver a little bit of data. Use an odd data size to ensure there is data
613 // left in the AudioBufferConverter. Ensure no buffers are in the splicer.
614 SatisfyPendingRead(InputFrames(2053));
615 EXPECT_FALSE(splicer_has_next_buffer());
616 EXPECT_GT(converter_input_frames_left().value, 0);
618 // Force a config change and then ensure all buffered data has been put into
619 // the splicer.
620 force_config_change();
621 EXPECT_TRUE(splicer_has_next_buffer());
622 EXPECT_EQ(0, converter_input_frames_left().value);
625 TEST_F(AudioRendererImplTest, TimeUpdatesOnFirstBuffer) {
626 Initialize();
627 Preroll();
628 StartTicking();
630 AudioTimestampHelper timestamp_helper(kOutputSamplesPerSecond);
631 timestamp_helper.SetBaseTimestamp(base::TimeDelta());
633 // Time should be the starting timestamp as nothing's been consumed yet.
634 EXPECT_EQ(timestamp_helper.GetTimestamp(), CurrentMediaTime());
636 // Consume some audio data.
637 OutputFrames frames_to_consume(frames_buffered().value / 2);
638 EXPECT_TRUE(ConsumeBufferedData(frames_to_consume));
639 WaitForPendingRead();
641 // Time shouldn't change just yet because we've only sent the initial audio
642 // data to the hardware.
643 EXPECT_EQ(timestamp_helper.GetTimestamp(), CurrentMediaTime());
645 // Consume some more audio data.
646 frames_to_consume = frames_buffered();
647 EXPECT_TRUE(ConsumeBufferedData(frames_to_consume));
649 // Now time should change now that the audio hardware has called back.
650 timestamp_helper.AddFrames(frames_to_consume.value);
651 EXPECT_EQ(timestamp_helper.GetTimestamp(), CurrentMediaTime());
654 TEST_F(AudioRendererImplTest, RenderingDelayedForEarlyStartTime) {
655 Initialize();
657 // Choose a first timestamp a few buffers into the future, which ends halfway
658 // through the desired output buffer; this allows for maximum test coverage.
659 const double kBuffers = 4.5;
660 const base::TimeDelta first_timestamp = base::TimeDelta::FromSecondsD(
661 hardware_config_.GetOutputBufferSize() * kBuffers /
662 hardware_config_.GetOutputSampleRate());
664 Preroll(base::TimeDelta(), first_timestamp, PIPELINE_OK);
665 StartTicking();
667 // Verify the first few buffers are silent.
668 scoped_ptr<AudioBus> bus =
669 AudioBus::Create(hardware_config_.GetOutputConfig());
670 int frames_read = 0;
671 for (int i = 0; i < std::floor(kBuffers); ++i) {
672 EXPECT_TRUE(sink_->Render(bus.get(), 0, &frames_read));
673 EXPECT_EQ(frames_read, bus->frames());
674 for (int j = 0; j < bus->frames(); ++j)
675 ASSERT_FLOAT_EQ(0.0f, bus->channel(0)[j]);
676 WaitForPendingRead();
677 DeliverRemainingAudio();
680 // Verify the last buffer is half silence and half real data.
681 EXPECT_TRUE(sink_->Render(bus.get(), 0, &frames_read));
682 EXPECT_EQ(frames_read, bus->frames());
683 const int zero_frames =
684 bus->frames() * (kBuffers - static_cast<int>(kBuffers));
686 for (int i = 0; i < zero_frames; ++i)
687 ASSERT_FLOAT_EQ(0.0f, bus->channel(0)[i]);
688 for (int i = zero_frames; i < bus->frames(); ++i)
689 ASSERT_NE(0.0f, bus->channel(0)[i]);
692 TEST_F(AudioRendererImplTest, ImmediateEndOfStream) {
693 Initialize();
695 SCOPED_TRACE("Preroll()");
696 renderer_->StartPlaying();
697 WaitForPendingRead();
698 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH));
699 DeliverEndOfStream();
701 StartTicking();
703 // Read a single frame. We shouldn't be able to satisfy it.
704 EXPECT_FALSE(ended());
705 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
706 base::RunLoop().RunUntilIdle();
707 EXPECT_TRUE(ended());
710 TEST_F(AudioRendererImplTest, OnRenderErrorCausesDecodeError) {
711 Initialize();
712 Preroll();
713 StartTicking();
715 EXPECT_CALL(*this, OnError(PIPELINE_ERROR_DECODE));
716 sink_->OnRenderError();
717 base::RunLoop().RunUntilIdle();
720 // Test for AudioRendererImpl calling Pause()/Play() on the sink when the
721 // playback rate is set to zero and non-zero.
722 TEST_F(AudioRendererImplTest, SetPlaybackRate) {
723 Initialize();
724 Preroll();
726 // Rendering hasn't started. Sink should always be paused.
727 EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
728 renderer_->SetPlaybackRate(0.0);
729 EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
730 renderer_->SetPlaybackRate(1.0);
731 EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
733 // Rendering has started with non-zero rate. Rate changes will affect sink
734 // state.
735 renderer_->StartTicking();
736 EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
737 renderer_->SetPlaybackRate(0.0);
738 EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
739 renderer_->SetPlaybackRate(1.0);
740 EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
742 // Rendering has stopped. Sink should be paused.
743 renderer_->StopTicking();
744 EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
746 // Start rendering with zero playback rate. Sink should be paused until
747 // non-zero rate is set.
748 renderer_->SetPlaybackRate(0.0);
749 renderer_->StartTicking();
750 EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
751 renderer_->SetPlaybackRate(1.0);
752 EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
755 } // namespace media