Implements TemplateURLServiceFactory on iOS
[chromium-blink-merge.git] / media / renderers / audio_renderer_impl_unittest.cc
blob9e9bf87dcbe41da1843d1619fda589a68655446e
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 "base/test/simple_test_tick_clock.h"
11 #include "media/base/audio_buffer_converter.h"
12 #include "media/base/audio_hardware_config.h"
13 #include "media/base/audio_splicer.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/renderers/audio_renderer_impl.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 using ::base::TimeDelta;
22 using ::testing::_;
23 using ::testing::Return;
24 using ::testing::SaveArg;
26 namespace media {
28 namespace {
30 // Since AudioBufferConverter is used due to different input/output sample
31 // rates, define some helper types to differentiate between the two.
32 struct InputFrames {
33 explicit InputFrames(int value) : value(value) {}
34 int value;
37 struct OutputFrames {
38 explicit OutputFrames(int value) : value(value) {}
39 int value;
42 } // namespace
44 // Constants to specify the type of audio data used.
45 static AudioCodec kCodec = kCodecVorbis;
46 static SampleFormat kSampleFormat = kSampleFormatPlanarF32;
47 static ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO;
48 static int kChannelCount = 2;
49 static int kChannels = ChannelLayoutToChannelCount(kChannelLayout);
51 // Use a different output sample rate so the AudioBufferConverter is invoked.
52 static int kInputSamplesPerSecond = 5000;
53 static int kOutputSamplesPerSecond = 10000;
55 ACTION_P(EnterPendingDecoderInitStateAction, test) {
56 test->EnterPendingDecoderInitState(arg1);
59 class AudioRendererImplTest : public ::testing::Test {
60 public:
61 // Give the decoder some non-garbage media properties.
62 AudioRendererImplTest()
63 : hardware_config_(AudioParameters(), AudioParameters()),
64 tick_clock_(new base::SimpleTestTickClock()),
65 demuxer_stream_(DemuxerStream::AUDIO),
66 decoder_(new MockAudioDecoder()),
67 ended_(false) {
68 AudioDecoderConfig audio_config(kCodec,
69 kSampleFormat,
70 kChannelLayout,
71 kInputSamplesPerSecond,
72 NULL,
74 false);
75 demuxer_stream_.set_audio_decoder_config(audio_config);
77 // Used to save callbacks and run them at a later time.
78 EXPECT_CALL(*decoder_, Decode(_, _))
79 .WillRepeatedly(Invoke(this, &AudioRendererImplTest::DecodeDecoder));
80 EXPECT_CALL(*decoder_, Reset(_))
81 .WillRepeatedly(Invoke(this, &AudioRendererImplTest::ResetDecoder));
83 // Mock out demuxer reads.
84 EXPECT_CALL(demuxer_stream_, Read(_)).WillRepeatedly(
85 RunCallback<0>(DemuxerStream::kOk,
86 scoped_refptr<DecoderBuffer>(new DecoderBuffer(0))));
87 EXPECT_CALL(demuxer_stream_, SupportsConfigChanges())
88 .WillRepeatedly(Return(true));
89 AudioParameters out_params(AudioParameters::AUDIO_PCM_LOW_LATENCY,
90 kChannelLayout,
91 kOutputSamplesPerSecond,
92 SampleFormatToBytesPerChannel(kSampleFormat) * 8,
93 512);
94 hardware_config_.UpdateOutputConfig(out_params);
95 ScopedVector<AudioDecoder> decoders;
96 decoders.push_back(decoder_);
97 sink_ = new FakeAudioRendererSink();
98 renderer_.reset(new AudioRendererImpl(message_loop_.task_runner(),
99 sink_.get(),
100 decoders.Pass(),
101 hardware_config_,
102 new MediaLog()));
103 renderer_->tick_clock_.reset(tick_clock_);
104 tick_clock_->Advance(base::TimeDelta::FromSeconds(1));
107 virtual ~AudioRendererImplTest() {
108 SCOPED_TRACE("~AudioRendererImplTest()");
111 void ExpectUnsupportedAudioDecoder() {
112 EXPECT_CALL(*decoder_, Initialize(_, _, _))
113 .WillOnce(DoAll(SaveArg<2>(&output_cb_), RunCallback<1>(false)));
116 MOCK_METHOD1(OnStatistics, void(const PipelineStatistics&));
117 MOCK_METHOD1(OnBufferingStateChange, void(BufferingState));
118 MOCK_METHOD1(OnError, void(PipelineStatus));
119 MOCK_METHOD0(OnWaitingForDecryptionKey, void(void));
121 void InitializeRenderer(const PipelineStatusCB& pipeline_status_cb) {
122 EXPECT_CALL(*this, OnWaitingForDecryptionKey()).Times(0);
123 renderer_->Initialize(
124 &demuxer_stream_, pipeline_status_cb, SetDecryptorReadyCB(),
125 base::Bind(&AudioRendererImplTest::OnStatistics,
126 base::Unretained(this)),
127 base::Bind(&AudioRendererImplTest::OnBufferingStateChange,
128 base::Unretained(this)),
129 base::Bind(&AudioRendererImplTest::OnEnded, base::Unretained(this)),
130 base::Bind(&AudioRendererImplTest::OnError, base::Unretained(this)),
131 base::Bind(&AudioRendererImplTest::OnWaitingForDecryptionKey,
132 base::Unretained(this)));
135 void Initialize() {
136 EXPECT_CALL(*decoder_, Initialize(_, _, _))
137 .WillOnce(DoAll(SaveArg<2>(&output_cb_), RunCallback<1>(true)));
138 InitializeWithStatus(PIPELINE_OK);
140 next_timestamp_.reset(new AudioTimestampHelper(kInputSamplesPerSecond));
143 void InitializeWithStatus(PipelineStatus expected) {
144 SCOPED_TRACE(base::StringPrintf("InitializeWithStatus(%d)", expected));
146 WaitableMessageLoopEvent event;
147 InitializeRenderer(event.GetPipelineStatusCB());
148 event.RunAndWaitForStatus(expected);
150 // We should have no reads.
151 EXPECT_TRUE(decode_cb_.is_null());
154 void InitializeAndDestroy() {
155 EXPECT_CALL(*decoder_, Initialize(_, _, _)).WillOnce(RunCallback<1>(true));
157 WaitableMessageLoopEvent event;
158 InitializeRenderer(event.GetPipelineStatusCB());
160 // Destroy the |renderer_| before we let the MessageLoop run, this simulates
161 // an interleaving in which we end up destroying the |renderer_| while the
162 // OnDecoderSelected callback is in flight.
163 renderer_.reset();
164 event.RunAndWaitForStatus(PIPELINE_ERROR_ABORT);
167 void InitializeAndDestroyDuringDecoderInit() {
168 EXPECT_CALL(*decoder_, Initialize(_, _, _))
169 .WillOnce(EnterPendingDecoderInitStateAction(this));
171 WaitableMessageLoopEvent event;
172 InitializeRenderer(event.GetPipelineStatusCB());
173 base::RunLoop().RunUntilIdle();
174 DCHECK(!init_decoder_cb_.is_null());
176 renderer_.reset();
177 event.RunAndWaitForStatus(PIPELINE_ERROR_ABORT);
180 void EnterPendingDecoderInitState(const AudioDecoder::InitCB& cb) {
181 init_decoder_cb_ = cb;
184 void FlushDuringPendingRead() {
185 SCOPED_TRACE("FlushDuringPendingRead()");
186 WaitableMessageLoopEvent flush_event;
187 renderer_->Flush(flush_event.GetClosure());
188 SatisfyPendingRead(InputFrames(256));
189 flush_event.RunAndWait();
191 EXPECT_FALSE(IsReadPending());
194 void Preroll() {
195 Preroll(base::TimeDelta(), base::TimeDelta(), PIPELINE_OK);
198 void Preroll(base::TimeDelta start_timestamp,
199 base::TimeDelta first_timestamp,
200 PipelineStatus expected) {
201 SCOPED_TRACE(base::StringPrintf("Preroll(%" PRId64 ", %d)",
202 first_timestamp.InMilliseconds(),
203 expected));
204 next_timestamp_->SetBaseTimestamp(first_timestamp);
206 // Fill entire buffer to complete prerolling.
207 renderer_->SetMediaTime(start_timestamp);
208 renderer_->StartPlaying();
209 WaitForPendingRead();
210 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH));
211 DeliverRemainingAudio();
214 void StartTicking() {
215 renderer_->StartTicking();
216 renderer_->SetPlaybackRate(1.0);
219 void StopTicking() { renderer_->StopTicking(); }
221 bool IsReadPending() const {
222 return !decode_cb_.is_null();
225 void WaitForPendingRead() {
226 SCOPED_TRACE("WaitForPendingRead()");
227 if (!decode_cb_.is_null())
228 return;
230 DCHECK(wait_for_pending_decode_cb_.is_null());
232 WaitableMessageLoopEvent event;
233 wait_for_pending_decode_cb_ = event.GetClosure();
234 event.RunAndWait();
236 DCHECK(!decode_cb_.is_null());
237 DCHECK(wait_for_pending_decode_cb_.is_null());
240 // Delivers decoded frames to |renderer_|.
241 void SatisfyPendingRead(InputFrames frames) {
242 CHECK_GT(frames.value, 0);
243 CHECK(!decode_cb_.is_null());
245 scoped_refptr<AudioBuffer> buffer =
246 MakeAudioBuffer<float>(kSampleFormat,
247 kChannelLayout,
248 kChannelCount,
249 kInputSamplesPerSecond,
250 1.0f,
251 0.0f,
252 frames.value,
253 next_timestamp_->GetTimestamp());
254 next_timestamp_->AddFrames(frames.value);
256 DeliverBuffer(AudioDecoder::kOk, buffer);
259 void DeliverEndOfStream() {
260 DCHECK(!decode_cb_.is_null());
262 // Return EOS buffer to trigger EOS frame.
263 EXPECT_CALL(demuxer_stream_, Read(_))
264 .WillOnce(RunCallback<0>(DemuxerStream::kOk,
265 DecoderBuffer::CreateEOSBuffer()));
267 // Satify pending |decode_cb_| to trigger a new DemuxerStream::Read().
268 message_loop_.PostTask(
269 FROM_HERE,
270 base::Bind(base::ResetAndReturn(&decode_cb_), AudioDecoder::kOk));
272 WaitForPendingRead();
274 message_loop_.PostTask(
275 FROM_HERE,
276 base::Bind(base::ResetAndReturn(&decode_cb_), AudioDecoder::kOk));
278 base::RunLoop().RunUntilIdle();
281 // Delivers frames until |renderer_|'s internal buffer is full and no longer
282 // has pending reads.
283 void DeliverRemainingAudio() {
284 while (frames_remaining_in_buffer().value > 0) {
285 SatisfyPendingRead(InputFrames(256));
289 // Attempts to consume |requested_frames| frames from |renderer_|'s internal
290 // buffer. Returns true if and only if all of |requested_frames| were able
291 // to be consumed.
292 bool ConsumeBufferedData(OutputFrames requested_frames,
293 base::TimeDelta delay) {
294 scoped_ptr<AudioBus> bus =
295 AudioBus::Create(kChannels, requested_frames.value);
296 int frames_read = 0;
297 EXPECT_TRUE(sink_->Render(bus.get(), delay.InMilliseconds(), &frames_read));
298 return frames_read == requested_frames.value;
301 bool ConsumeBufferedData(OutputFrames requested_frames) {
302 return ConsumeBufferedData(requested_frames, base::TimeDelta());
305 base::TimeTicks ConvertMediaTime(base::TimeDelta timestamp,
306 bool* is_time_moving) {
307 std::vector<base::TimeTicks> wall_clock_times;
308 *is_time_moving = renderer_->GetWallClockTimes(
309 std::vector<base::TimeDelta>(1, timestamp), &wall_clock_times);
310 return wall_clock_times[0];
313 base::TimeTicks CurrentMediaWallClockTime(bool* is_time_moving) {
314 std::vector<base::TimeTicks> wall_clock_times;
315 *is_time_moving = renderer_->GetWallClockTimes(
316 std::vector<base::TimeDelta>(), &wall_clock_times);
317 return wall_clock_times[0];
320 OutputFrames frames_buffered() {
321 return OutputFrames(renderer_->algorithm_->frames_buffered());
324 OutputFrames buffer_capacity() {
325 return OutputFrames(renderer_->algorithm_->QueueCapacity());
328 OutputFrames frames_remaining_in_buffer() {
329 // This can happen if too much data was delivered, in which case the buffer
330 // will accept the data but not increase capacity.
331 if (frames_buffered().value > buffer_capacity().value) {
332 return OutputFrames(0);
334 return OutputFrames(buffer_capacity().value - frames_buffered().value);
337 void force_config_change() {
338 renderer_->OnConfigChange();
341 InputFrames converter_input_frames_left() const {
342 return InputFrames(
343 renderer_->buffer_converter_->input_frames_left_for_testing());
346 bool splicer_has_next_buffer() const {
347 return renderer_->splicer_->HasNextBuffer();
350 base::TimeDelta CurrentMediaTime() {
351 return renderer_->CurrentMediaTime();
354 bool ended() const { return ended_; }
356 // Fixture members.
357 base::MessageLoop message_loop_;
358 scoped_ptr<AudioRendererImpl> renderer_;
359 scoped_refptr<FakeAudioRendererSink> sink_;
360 AudioHardwareConfig hardware_config_;
361 base::SimpleTestTickClock* tick_clock_;
363 private:
364 void DecodeDecoder(const scoped_refptr<DecoderBuffer>& buffer,
365 const AudioDecoder::DecodeCB& decode_cb) {
366 // TODO(scherkus): Make this a DCHECK after threading semantics are fixed.
367 if (base::MessageLoop::current() != &message_loop_) {
368 message_loop_.PostTask(FROM_HERE, base::Bind(
369 &AudioRendererImplTest::DecodeDecoder,
370 base::Unretained(this), buffer, decode_cb));
371 return;
374 CHECK(decode_cb_.is_null()) << "Overlapping decodes are not permitted";
375 decode_cb_ = decode_cb;
377 // Wake up WaitForPendingRead() if needed.
378 if (!wait_for_pending_decode_cb_.is_null())
379 base::ResetAndReturn(&wait_for_pending_decode_cb_).Run();
382 void ResetDecoder(const base::Closure& reset_cb) {
383 if (!decode_cb_.is_null()) {
384 // |reset_cb| will be called in DeliverBuffer(), after the decoder is
385 // flushed.
386 reset_cb_ = reset_cb;
387 return;
390 message_loop_.PostTask(FROM_HERE, reset_cb);
393 void DeliverBuffer(AudioDecoder::Status status,
394 const scoped_refptr<AudioBuffer>& buffer) {
395 CHECK(!decode_cb_.is_null());
396 if (buffer.get() && !buffer->end_of_stream())
397 output_cb_.Run(buffer);
398 base::ResetAndReturn(&decode_cb_).Run(status);
400 if (!reset_cb_.is_null())
401 base::ResetAndReturn(&reset_cb_).Run();
403 base::RunLoop().RunUntilIdle();
406 void OnEnded() {
407 CHECK(!ended_);
408 ended_ = true;
411 MockDemuxerStream demuxer_stream_;
412 MockAudioDecoder* decoder_;
414 // Used for satisfying reads.
415 AudioDecoder::OutputCB output_cb_;
416 AudioDecoder::DecodeCB decode_cb_;
417 base::Closure reset_cb_;
418 scoped_ptr<AudioTimestampHelper> next_timestamp_;
420 // Run during DecodeDecoder() to unblock WaitForPendingRead().
421 base::Closure wait_for_pending_decode_cb_;
423 AudioDecoder::InitCB init_decoder_cb_;
424 bool ended_;
426 DISALLOW_COPY_AND_ASSIGN(AudioRendererImplTest);
429 TEST_F(AudioRendererImplTest, Initialize_Successful) {
430 Initialize();
433 TEST_F(AudioRendererImplTest, Initialize_DecoderInitFailure) {
434 ExpectUnsupportedAudioDecoder();
435 InitializeWithStatus(DECODER_ERROR_NOT_SUPPORTED);
438 TEST_F(AudioRendererImplTest, Preroll) {
439 Initialize();
440 Preroll();
443 TEST_F(AudioRendererImplTest, StartTicking) {
444 Initialize();
445 Preroll();
446 StartTicking();
448 // Drain internal buffer, we should have a pending read.
449 EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
450 WaitForPendingRead();
453 TEST_F(AudioRendererImplTest, EndOfStream) {
454 Initialize();
455 Preroll();
456 StartTicking();
458 // Drain internal buffer, we should have a pending read.
459 EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
460 WaitForPendingRead();
462 // Forcefully trigger underflow.
463 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
464 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING));
466 // Fulfill the read with an end-of-stream buffer. Doing so should change our
467 // buffering state so playback resumes.
468 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH));
469 DeliverEndOfStream();
471 // Consume all remaining data. We shouldn't have signal ended yet.
472 EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
473 base::RunLoop().RunUntilIdle();
474 EXPECT_FALSE(ended());
476 // Ended should trigger on next render call.
477 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
478 base::RunLoop().RunUntilIdle();
479 EXPECT_TRUE(ended());
482 TEST_F(AudioRendererImplTest, Underflow) {
483 Initialize();
484 Preroll();
485 StartTicking();
487 // Drain internal buffer, we should have a pending read.
488 EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
489 WaitForPendingRead();
491 // Verify the next FillBuffer() call triggers a buffering state change
492 // update.
493 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING));
494 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
496 // Verify we're still not getting audio data.
497 EXPECT_EQ(0, frames_buffered().value);
498 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
500 // Deliver enough data to have enough for buffering.
501 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH));
502 DeliverRemainingAudio();
504 // Verify we're getting audio data.
505 EXPECT_TRUE(ConsumeBufferedData(OutputFrames(1)));
508 TEST_F(AudioRendererImplTest, Underflow_CapacityResetsAfterFlush) {
509 Initialize();
510 Preroll();
511 StartTicking();
513 // Drain internal buffer, we should have a pending read.
514 EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
515 WaitForPendingRead();
517 // Verify the next FillBuffer() call triggers the underflow callback
518 // since the decoder hasn't delivered any data after it was drained.
519 OutputFrames initial_capacity = buffer_capacity();
520 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING));
521 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
523 // Verify that the buffer capacity increased as a result of underflowing.
524 EXPECT_GT(buffer_capacity().value, initial_capacity.value);
526 // Verify that the buffer capacity is restored to the |initial_capacity|.
527 FlushDuringPendingRead();
528 EXPECT_EQ(buffer_capacity().value, initial_capacity.value);
531 TEST_F(AudioRendererImplTest, Underflow_Flush) {
532 Initialize();
533 Preroll();
534 StartTicking();
536 // Force underflow.
537 EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
538 WaitForPendingRead();
539 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING));
540 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
541 WaitForPendingRead();
542 StopTicking();
544 // We shouldn't expect another buffering state change when flushing.
545 FlushDuringPendingRead();
548 TEST_F(AudioRendererImplTest, PendingRead_Flush) {
549 Initialize();
551 Preroll();
552 StartTicking();
554 // Partially drain internal buffer so we get a pending read.
555 EXPECT_TRUE(ConsumeBufferedData(OutputFrames(256)));
556 WaitForPendingRead();
558 StopTicking();
560 EXPECT_TRUE(IsReadPending());
562 // Flush and expect to be notified that we have nothing.
563 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING));
564 FlushDuringPendingRead();
566 // Preroll again to a different timestamp and verify it completed normally.
567 const base::TimeDelta seek_timestamp =
568 base::TimeDelta::FromMilliseconds(1000);
569 Preroll(seek_timestamp, seek_timestamp, PIPELINE_OK);
572 TEST_F(AudioRendererImplTest, PendingRead_Destroy) {
573 Initialize();
575 Preroll();
576 StartTicking();
578 // Partially drain internal buffer so we get a pending read.
579 EXPECT_TRUE(ConsumeBufferedData(OutputFrames(256)));
580 WaitForPendingRead();
582 StopTicking();
584 EXPECT_TRUE(IsReadPending());
586 renderer_.reset();
589 TEST_F(AudioRendererImplTest, PendingFlush_Destroy) {
590 Initialize();
592 Preroll();
593 StartTicking();
595 // Partially drain internal buffer so we get a pending read.
596 EXPECT_TRUE(ConsumeBufferedData(OutputFrames(256)));
597 WaitForPendingRead();
599 StopTicking();
601 EXPECT_TRUE(IsReadPending());
603 // Start flushing.
604 WaitableMessageLoopEvent flush_event;
605 renderer_->Flush(flush_event.GetClosure());
607 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING));
608 SatisfyPendingRead(InputFrames(256));
610 renderer_.reset();
613 TEST_F(AudioRendererImplTest, InitializeThenDestroy) {
614 InitializeAndDestroy();
617 TEST_F(AudioRendererImplTest, InitializeThenDestroyDuringDecoderInit) {
618 InitializeAndDestroyDuringDecoderInit();
621 TEST_F(AudioRendererImplTest, ConfigChangeDrainsConverter) {
622 Initialize();
623 Preroll();
624 StartTicking();
626 // Drain internal buffer, we should have a pending read.
627 EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
628 WaitForPendingRead();
630 // Deliver a little bit of data. Use an odd data size to ensure there is data
631 // left in the AudioBufferConverter. Ensure no buffers are in the splicer.
632 SatisfyPendingRead(InputFrames(2053));
633 EXPECT_FALSE(splicer_has_next_buffer());
634 EXPECT_GT(converter_input_frames_left().value, 0);
636 // Force a config change and then ensure all buffered data has been put into
637 // the splicer.
638 force_config_change();
639 EXPECT_TRUE(splicer_has_next_buffer());
640 EXPECT_EQ(0, converter_input_frames_left().value);
643 TEST_F(AudioRendererImplTest, TimeUpdatesOnFirstBuffer) {
644 Initialize();
645 Preroll();
646 StartTicking();
648 AudioTimestampHelper timestamp_helper(kOutputSamplesPerSecond);
649 timestamp_helper.SetBaseTimestamp(base::TimeDelta());
651 // Time should be the starting timestamp as nothing's been consumed yet.
652 EXPECT_EQ(timestamp_helper.GetTimestamp(), CurrentMediaTime());
654 // Consume some audio data.
655 OutputFrames frames_to_consume(frames_buffered().value / 2);
656 EXPECT_TRUE(ConsumeBufferedData(frames_to_consume));
657 WaitForPendingRead();
659 // Time shouldn't change just yet because we've only sent the initial audio
660 // data to the hardware.
661 EXPECT_EQ(timestamp_helper.GetTimestamp(), CurrentMediaTime());
663 // Consume some more audio data.
664 frames_to_consume = frames_buffered();
665 EXPECT_TRUE(ConsumeBufferedData(frames_to_consume));
667 // Now time should change now that the audio hardware has called back.
668 timestamp_helper.AddFrames(frames_to_consume.value);
669 EXPECT_EQ(timestamp_helper.GetTimestamp(), CurrentMediaTime());
672 TEST_F(AudioRendererImplTest, RenderingDelayedForEarlyStartTime) {
673 Initialize();
675 // Choose a first timestamp a few buffers into the future, which ends halfway
676 // through the desired output buffer; this allows for maximum test coverage.
677 const double kBuffers = 4.5;
678 const base::TimeDelta first_timestamp = base::TimeDelta::FromSecondsD(
679 hardware_config_.GetOutputBufferSize() * kBuffers /
680 hardware_config_.GetOutputSampleRate());
682 Preroll(base::TimeDelta(), first_timestamp, PIPELINE_OK);
683 StartTicking();
685 // Verify the first few buffers are silent.
686 scoped_ptr<AudioBus> bus =
687 AudioBus::Create(hardware_config_.GetOutputConfig());
688 int frames_read = 0;
689 for (int i = 0; i < std::floor(kBuffers); ++i) {
690 EXPECT_TRUE(sink_->Render(bus.get(), 0, &frames_read));
691 EXPECT_EQ(frames_read, bus->frames());
692 for (int j = 0; j < bus->frames(); ++j)
693 ASSERT_FLOAT_EQ(0.0f, bus->channel(0)[j]);
694 WaitForPendingRead();
695 DeliverRemainingAudio();
698 // Verify the last buffer is half silence and half real data.
699 EXPECT_TRUE(sink_->Render(bus.get(), 0, &frames_read));
700 EXPECT_EQ(frames_read, bus->frames());
701 const int zero_frames =
702 bus->frames() * (kBuffers - static_cast<int>(kBuffers));
704 for (int i = 0; i < zero_frames; ++i)
705 ASSERT_FLOAT_EQ(0.0f, bus->channel(0)[i]);
706 for (int i = zero_frames; i < bus->frames(); ++i)
707 ASSERT_NE(0.0f, bus->channel(0)[i]);
710 TEST_F(AudioRendererImplTest, ImmediateEndOfStream) {
711 Initialize();
713 SCOPED_TRACE("Preroll()");
714 renderer_->StartPlaying();
715 WaitForPendingRead();
716 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH));
717 DeliverEndOfStream();
719 StartTicking();
721 // Read a single frame. We shouldn't be able to satisfy it.
722 EXPECT_FALSE(ended());
723 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
724 base::RunLoop().RunUntilIdle();
725 EXPECT_TRUE(ended());
728 TEST_F(AudioRendererImplTest, OnRenderErrorCausesDecodeError) {
729 Initialize();
730 Preroll();
731 StartTicking();
733 EXPECT_CALL(*this, OnError(PIPELINE_ERROR_DECODE));
734 sink_->OnRenderError();
735 base::RunLoop().RunUntilIdle();
738 // Test for AudioRendererImpl calling Pause()/Play() on the sink when the
739 // playback rate is set to zero and non-zero.
740 TEST_F(AudioRendererImplTest, SetPlaybackRate) {
741 Initialize();
742 Preroll();
744 // Rendering hasn't started. Sink should always be paused.
745 EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
746 renderer_->SetPlaybackRate(0.0);
747 EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
748 renderer_->SetPlaybackRate(1.0);
749 EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
751 // Rendering has started with non-zero rate. Rate changes will affect sink
752 // state.
753 renderer_->StartTicking();
754 EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
755 renderer_->SetPlaybackRate(0.0);
756 EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
757 renderer_->SetPlaybackRate(1.0);
758 EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
760 // Rendering has stopped. Sink should be paused.
761 renderer_->StopTicking();
762 EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
764 // Start rendering with zero playback rate. Sink should be paused until
765 // non-zero rate is set.
766 renderer_->SetPlaybackRate(0.0);
767 renderer_->StartTicking();
768 EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
769 renderer_->SetPlaybackRate(1.0);
770 EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
773 TEST_F(AudioRendererImplTest, TimeSourceBehavior) {
774 Initialize();
775 Preroll();
777 AudioTimestampHelper timestamp_helper(kOutputSamplesPerSecond);
778 timestamp_helper.SetBaseTimestamp(base::TimeDelta());
780 // Prior to start, time should be shown as not moving.
781 bool is_time_moving = false;
782 EXPECT_EQ(base::TimeTicks(),
783 ConvertMediaTime(base::TimeDelta(), &is_time_moving));
784 EXPECT_FALSE(is_time_moving);
786 EXPECT_EQ(base::TimeTicks(), CurrentMediaWallClockTime(&is_time_moving));
787 EXPECT_FALSE(is_time_moving);
789 // Start ticking, but use a zero playback rate, time should still be stopped
790 // until a positive playback rate is set and the first Render() is called.
791 renderer_->SetPlaybackRate(0.0);
792 StartTicking();
793 EXPECT_EQ(base::TimeTicks(), CurrentMediaWallClockTime(&is_time_moving));
794 EXPECT_FALSE(is_time_moving);
795 renderer_->SetPlaybackRate(1.0);
796 EXPECT_EQ(base::TimeTicks(), CurrentMediaWallClockTime(&is_time_moving));
797 EXPECT_FALSE(is_time_moving);
798 renderer_->SetPlaybackRate(1.0);
800 // Issue the first render call to start time moving.
801 OutputFrames frames_to_consume(frames_buffered().value / 2);
802 EXPECT_TRUE(ConsumeBufferedData(frames_to_consume));
803 WaitForPendingRead();
805 // Time shouldn't change just yet because we've only sent the initial audio
806 // data to the hardware.
807 EXPECT_EQ(tick_clock_->NowTicks(),
808 ConvertMediaTime(base::TimeDelta(), &is_time_moving));
809 EXPECT_TRUE(is_time_moving);
811 // Consume some more audio data.
812 frames_to_consume = frames_buffered();
813 tick_clock_->Advance(
814 base::TimeDelta::FromSecondsD(1.0 / kOutputSamplesPerSecond));
815 EXPECT_TRUE(ConsumeBufferedData(frames_to_consume));
817 // Time should change now that the audio hardware has called back.
818 const base::TimeTicks wall_clock_time_zero =
819 tick_clock_->NowTicks() -
820 timestamp_helper.GetFrameDuration(frames_to_consume.value);
821 EXPECT_EQ(wall_clock_time_zero,
822 ConvertMediaTime(base::TimeDelta(), &is_time_moving));
823 EXPECT_TRUE(is_time_moving);
825 // The current wall clock time should change as our tick clock advances, up
826 // until we've reached the end of played out frames.
827 const int kSteps = 4;
828 const base::TimeDelta kAdvanceDelta =
829 timestamp_helper.GetFrameDuration(frames_to_consume.value) / kSteps;
831 for (int i = 0; i < kSteps; ++i) {
832 tick_clock_->Advance(kAdvanceDelta);
833 EXPECT_EQ(tick_clock_->NowTicks(),
834 CurrentMediaWallClockTime(&is_time_moving));
835 EXPECT_TRUE(is_time_moving);
838 // Converting the current media time should be relative to wall clock zero.
839 EXPECT_EQ(wall_clock_time_zero + kSteps * kAdvanceDelta,
840 ConvertMediaTime(renderer_->CurrentMediaTime(), &is_time_moving));
841 EXPECT_TRUE(is_time_moving);
843 // Advancing once more will exceed the amount of played out frames finally.
844 base::TimeTicks current_time = tick_clock_->NowTicks();
845 tick_clock_->Advance(
846 base::TimeDelta::FromSecondsD(1.0 / kOutputSamplesPerSecond));
847 EXPECT_EQ(current_time, CurrentMediaWallClockTime(&is_time_moving));
848 EXPECT_TRUE(is_time_moving);
850 StopTicking();
851 DeliverRemainingAudio();
853 // Elapse a lot of time between StopTicking() and the next Render() call.
854 const base::TimeDelta kOneSecond = base::TimeDelta::FromSeconds(1);
855 tick_clock_->Advance(kOneSecond);
856 StartTicking();
858 // Time should be stopped until the next render call.
859 EXPECT_EQ(current_time, CurrentMediaWallClockTime(&is_time_moving));
860 EXPECT_FALSE(is_time_moving);
862 // Consume some buffered data with a small delay.
863 base::TimeDelta delay_time = base::TimeDelta::FromMilliseconds(50);
864 frames_to_consume.value = frames_buffered().value / 16;
865 EXPECT_TRUE(ConsumeBufferedData(frames_to_consume, delay_time));
867 // Verify time is adjusted for the current delay.
868 current_time = tick_clock_->NowTicks() + delay_time;
869 EXPECT_EQ(current_time, CurrentMediaWallClockTime(&is_time_moving));
870 EXPECT_TRUE(is_time_moving);
871 EXPECT_EQ(current_time,
872 ConvertMediaTime(renderer_->CurrentMediaTime(), &is_time_moving));
873 EXPECT_TRUE(is_time_moving);
875 // Advance far enough that we shouldn't be clamped to current time (tested
876 // already above).
877 tick_clock_->Advance(kOneSecond);
878 EXPECT_EQ(
879 current_time + timestamp_helper.GetFrameDuration(frames_to_consume.value),
880 CurrentMediaWallClockTime(&is_time_moving));
881 EXPECT_TRUE(is_time_moving);
884 } // namespace media