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.
6 #include "base/callback_helpers.h"
7 #include "base/gtest_prod_util.h"
8 #include "base/memory/scoped_vector.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/stl_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "media/base/audio_buffer.h"
13 #include "media/base/audio_timestamp_helper.h"
14 #include "media/base/fake_audio_renderer_sink.h"
15 #include "media/base/gmock_callback_support.h"
16 #include "media/base/mock_filters.h"
17 #include "media/base/test_helpers.h"
18 #include "media/filters/audio_renderer_impl.h"
19 #include "testing/gtest/include/gtest/gtest.h"
22 using ::base::TimeTicks;
23 using ::base::TimeDelta;
25 using ::testing::AnyNumber;
26 using ::testing::Invoke;
27 using ::testing::Return;
31 // Constants to specify the type of audio data used.
32 static AudioCodec kCodec = kCodecVorbis;
33 static SampleFormat kSampleFormat = kSampleFormatPlanarF32;
34 static ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO;
35 static int kChannels = ChannelLayoutToChannelCount(kChannelLayout);
36 static int kSamplesPerSecond = 44100;
38 // Constants for distinguishing between muted audio and playing audio when using
39 // ConsumeBufferedData(). Must match the type needed by kSampleFormat.
40 static float kMutedAudio = 0.0f;
41 static float kPlayingAudio = 0.5f;
43 class AudioRendererImplTest : public ::testing::Test {
45 // Give the decoder some non-garbage media properties.
46 AudioRendererImplTest()
47 : demuxer_stream_(DemuxerStream::AUDIO),
48 decoder_(new MockAudioDecoder()) {
49 AudioDecoderConfig audio_config(kCodec,
56 demuxer_stream_.set_audio_decoder_config(audio_config);
58 // Used to save callbacks and run them at a later time.
59 EXPECT_CALL(*decoder_, Read(_))
60 .WillRepeatedly(Invoke(this, &AudioRendererImplTest::ReadDecoder));
62 // Set up audio properties.
63 EXPECT_CALL(*decoder_, bits_per_channel())
64 .WillRepeatedly(Return(audio_config.bits_per_channel()));
65 EXPECT_CALL(*decoder_, channel_layout())
66 .WillRepeatedly(Return(audio_config.channel_layout()));
67 EXPECT_CALL(*decoder_, samples_per_second())
68 .WillRepeatedly(Return(audio_config.samples_per_second()));
70 ScopedVector<AudioDecoder> decoders;
71 decoders.push_back(decoder_);
72 sink_ = new FakeAudioRendererSink();
73 renderer_.reset(new AudioRendererImpl(
74 message_loop_.message_loop_proxy(),
77 SetDecryptorReadyCB(),
81 renderer_->set_now_cb_for_testing(base::Bind(
82 &AudioRendererImplTest::GetTime, base::Unretained(this)));
85 virtual ~AudioRendererImplTest() {
86 SCOPED_TRACE("~AudioRendererImplTest()");
87 WaitableMessageLoopEvent event;
88 renderer_->Stop(event.GetClosure());
92 void ExpectUnsupportedAudioDecoder() {
93 EXPECT_CALL(*decoder_, Initialize(_, _, _))
94 .WillOnce(RunCallback<1>(DECODER_ERROR_NOT_SUPPORTED));
97 void ExpectUnsupportedAudioDecoderConfig() {
98 EXPECT_CALL(*decoder_, bits_per_channel())
99 .WillRepeatedly(Return(3));
100 EXPECT_CALL(*decoder_, channel_layout())
101 .WillRepeatedly(Return(CHANNEL_LAYOUT_UNSUPPORTED));
102 EXPECT_CALL(*decoder_, samples_per_second())
103 .WillRepeatedly(Return(0));
104 EXPECT_CALL(*decoder_, Initialize(_, _, _))
105 .WillOnce(RunCallback<1>(PIPELINE_OK));
108 MOCK_METHOD1(OnStatistics, void(const PipelineStatistics&));
109 MOCK_METHOD0(OnUnderflow, void());
110 MOCK_METHOD0(OnDisabled, void());
111 MOCK_METHOD1(OnError, void(PipelineStatus));
113 void OnAudioTimeCallback(TimeDelta current_time, TimeDelta max_time) {
114 CHECK(current_time <= max_time);
118 EXPECT_CALL(*decoder_, Initialize(_, _, _))
119 .WillOnce(RunCallback<1>(PIPELINE_OK));
120 InitializeWithStatus(PIPELINE_OK);
122 next_timestamp_.reset(
123 new AudioTimestampHelper(decoder_->samples_per_second()));
126 void InitializeWithStatus(PipelineStatus expected) {
127 SCOPED_TRACE(base::StringPrintf("InitializeWithStatus(%d)", expected));
129 WaitableMessageLoopEvent event;
130 renderer_->Initialize(
132 event.GetPipelineStatusCB(),
133 base::Bind(&AudioRendererImplTest::OnStatistics,
134 base::Unretained(this)),
135 base::Bind(&AudioRendererImplTest::OnUnderflow,
136 base::Unretained(this)),
137 base::Bind(&AudioRendererImplTest::OnAudioTimeCallback,
138 base::Unretained(this)),
139 ended_event_.GetClosure(),
140 base::Bind(&AudioRendererImplTest::OnDisabled,
141 base::Unretained(this)),
142 base::Bind(&AudioRendererImplTest::OnError,
143 base::Unretained(this)));
144 event.RunAndWaitForStatus(expected);
146 // We should have no reads.
147 EXPECT_TRUE(read_cb_.is_null());
151 Preroll(0, PIPELINE_OK);
154 void Preroll(int timestamp_ms, PipelineStatus expected) {
155 SCOPED_TRACE(base::StringPrintf("Preroll(%d, %d)", timestamp_ms, expected));
157 TimeDelta timestamp = TimeDelta::FromMilliseconds(timestamp_ms);
158 next_timestamp_->SetBaseTimestamp(timestamp);
160 // Fill entire buffer to complete prerolling.
161 WaitableMessageLoopEvent event;
162 renderer_->Preroll(timestamp, event.GetPipelineStatusCB());
163 WaitForPendingRead();
164 DeliverRemainingAudio();
165 event.RunAndWaitForStatus(PIPELINE_OK);
167 // We should have no reads.
168 EXPECT_TRUE(read_cb_.is_null());
172 SCOPED_TRACE("Play()");
173 WaitableMessageLoopEvent event;
174 renderer_->Play(event.GetClosure());
175 renderer_->SetPlaybackRate(1.0f);
179 void WaitForEnded() {
180 SCOPED_TRACE("WaitForEnded()");
181 ended_event_.RunAndWait();
184 void WaitForPendingRead() {
185 SCOPED_TRACE("WaitForPendingRead()");
186 if (!read_cb_.is_null())
189 DCHECK(wait_for_pending_read_cb_.is_null());
191 WaitableMessageLoopEvent event;
192 wait_for_pending_read_cb_ = event.GetClosure();
195 DCHECK(!read_cb_.is_null());
196 DCHECK(wait_for_pending_read_cb_.is_null());
199 // Delivers |size| frames with value kPlayingAudio to |renderer_|.
200 void SatisfyPendingRead(size_t size) {
201 CHECK(!read_cb_.is_null());
203 scoped_refptr<AudioBuffer> buffer =
204 MakePlanarAudioBuffer<float>(kSampleFormat,
209 next_timestamp_->GetTimestamp(),
210 next_timestamp_->GetFrameDuration(size));
211 next_timestamp_->AddFrames(size);
213 DeliverBuffer(AudioDecoder::kOk, buffer);
216 void AbortPendingRead() {
217 DeliverBuffer(AudioDecoder::kAborted, NULL);
220 void DeliverEndOfStream() {
221 DeliverBuffer(AudioDecoder::kOk, AudioBuffer::CreateEOSBuffer());
224 // Delivers frames until |renderer_|'s internal buffer is full and no longer
225 // has pending reads.
226 void DeliverRemainingAudio() {
227 SatisfyPendingRead(frames_remaining_in_buffer());
230 // Attempts to consume |requested_frames| frames from |renderer_|'s internal
231 // buffer, returning true if all |requested_frames| frames were consumed,
232 // false if less than |requested_frames| frames were consumed.
234 // |muted| is optional and if passed will get set if the value of
235 // the consumed data is muted audio.
236 bool ConsumeBufferedData(int requested_frames, bool* muted) {
237 scoped_ptr<AudioBus> bus =
238 AudioBus::Create(kChannels, std::max(requested_frames, 1));
240 if (!sink_->Render(bus.get(), 0, &frames_read)) {
247 *muted = frames_read < 1 || bus->channel(0)[0] == kMutedAudio;
248 return frames_read == requested_frames;
251 // Attempts to consume all data available from the renderer. Returns the
252 // number of frames read. Since time is frozen, the audio delay will increase
253 // as frames come in.
254 int ConsumeAllBufferedData() {
255 renderer_->DisableUnderflowForTesting();
258 int total_frames_read = 0;
260 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, 1024);
263 TimeDelta audio_delay = TimeDelta::FromMicroseconds(
264 total_frames_read * Time::kMicrosecondsPerSecond /
265 static_cast<float>(decoder_->samples_per_second()));
267 frames_read = renderer_->Render(
268 bus.get(), audio_delay.InMilliseconds());
269 total_frames_read += frames_read;
270 } while (frames_read > 0);
272 return total_frames_read;
275 uint32 frames_buffered() {
276 return renderer_->algorithm_->frames_buffered();
279 uint32 buffer_capacity() {
280 return renderer_->algorithm_->QueueCapacity();
283 uint32 frames_remaining_in_buffer() {
284 // This can happen if too much data was delivered, in which case the buffer
285 // will accept the data but not increase capacity.
286 if (frames_buffered() > buffer_capacity()) {
289 return buffer_capacity() - frames_buffered();
292 void CallResumeAfterUnderflow() {
293 renderer_->ResumeAfterUnderflow();
296 TimeDelta CalculatePlayTime(int frames_filled) {
297 return TimeDelta::FromMicroseconds(
298 frames_filled * Time::kMicrosecondsPerSecond /
299 renderer_->audio_parameters_.sample_rate());
302 void EndOfStreamTest(float playback_rate) {
306 renderer_->SetPlaybackRate(playback_rate);
308 // Drain internal buffer, we should have a pending read.
309 int total_frames = frames_buffered();
310 int frames_filled = ConsumeAllBufferedData();
311 WaitForPendingRead();
313 // Due to how the cross-fade algorithm works we won't get an exact match
314 // between the ideal and expected number of frames consumed. In the faster
315 // than normal playback case, more frames are created than should exist and
316 // vice versa in the slower than normal playback case.
317 const float kEpsilon = 0.20 * (total_frames / playback_rate);
318 EXPECT_NEAR(frames_filled, total_frames / playback_rate, kEpsilon);
320 // Figure out how long until the ended event should fire.
321 TimeDelta audio_play_time = CalculatePlayTime(frames_filled);
322 DVLOG(1) << "audio_play_time = " << audio_play_time.InSecondsF();
324 // Fulfill the read with an end-of-stream packet. We shouldn't report ended
325 // nor have a read until we drain the internal buffer.
326 DeliverEndOfStream();
328 // Advance time half way without an ended expectation.
329 AdvanceTime(audio_play_time / 2);
330 ConsumeBufferedData(frames_buffered(), NULL);
332 // Advance time by other half and expect the ended event.
333 AdvanceTime(audio_play_time / 2);
334 ConsumeBufferedData(frames_buffered(), NULL);
338 void AdvanceTime(TimeDelta time) {
339 base::AutoLock auto_lock(lock_);
344 base::MessageLoop message_loop_;
345 scoped_ptr<AudioRendererImpl> renderer_;
346 scoped_refptr<FakeAudioRendererSink> sink_;
349 TimeTicks GetTime() {
350 base::AutoLock auto_lock(lock_);
354 void ReadDecoder(const AudioDecoder::ReadCB& read_cb) {
355 // TODO(scherkus): Make this a DCHECK after threading semantics are fixed.
356 if (base::MessageLoop::current() != &message_loop_) {
357 message_loop_.PostTask(FROM_HERE, base::Bind(
358 &AudioRendererImplTest::ReadDecoder,
359 base::Unretained(this), read_cb));
363 CHECK(read_cb_.is_null()) << "Overlapping reads are not permitted";
366 // Wake up WaitForPendingRead() if needed.
367 if (!wait_for_pending_read_cb_.is_null())
368 base::ResetAndReturn(&wait_for_pending_read_cb_).Run();
371 void DeliverBuffer(AudioDecoder::Status status,
372 const scoped_refptr<AudioBuffer>& buffer) {
373 CHECK(!read_cb_.is_null());
374 base::ResetAndReturn(&read_cb_).Run(status, buffer);
377 MockDemuxerStream demuxer_stream_;
378 MockAudioDecoder* decoder_;
380 // Used for stubbing out time in the audio callback thread.
384 // Used for satisfying reads.
385 AudioDecoder::ReadCB read_cb_;
386 scoped_ptr<AudioTimestampHelper> next_timestamp_;
388 WaitableMessageLoopEvent ended_event_;
390 // Run during ReadDecoder() to unblock WaitForPendingRead().
391 base::Closure wait_for_pending_read_cb_;
393 DISALLOW_COPY_AND_ASSIGN(AudioRendererImplTest);
396 TEST_F(AudioRendererImplTest, Initialize_Failed) {
397 ExpectUnsupportedAudioDecoderConfig();
398 InitializeWithStatus(PIPELINE_ERROR_INITIALIZATION_FAILED);
401 TEST_F(AudioRendererImplTest, Initialize_Successful) {
405 TEST_F(AudioRendererImplTest, Initialize_DecoderInitFailure) {
406 ExpectUnsupportedAudioDecoder();
407 InitializeWithStatus(DECODER_ERROR_NOT_SUPPORTED);
410 TEST_F(AudioRendererImplTest, Preroll) {
415 TEST_F(AudioRendererImplTest, Play) {
420 // Drain internal buffer, we should have a pending read.
421 EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL));
422 WaitForPendingRead();
425 TEST_F(AudioRendererImplTest, EndOfStream) {
426 EndOfStreamTest(1.0);
429 TEST_F(AudioRendererImplTest, EndOfStream_FasterPlaybackSpeed) {
430 EndOfStreamTest(2.0);
433 TEST_F(AudioRendererImplTest, EndOfStream_SlowerPlaybackSpeed) {
434 EndOfStreamTest(0.5);
437 TEST_F(AudioRendererImplTest, Underflow) {
442 // Drain internal buffer, we should have a pending read.
443 EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL));
444 WaitForPendingRead();
446 // Verify the next FillBuffer() call triggers the underflow callback
447 // since the decoder hasn't delivered any data after it was drained.
448 const size_t kDataSize = 1024;
449 EXPECT_CALL(*this, OnUnderflow());
450 EXPECT_FALSE(ConsumeBufferedData(kDataSize, NULL));
452 renderer_->ResumeAfterUnderflow();
454 // Verify after resuming that we're still not getting data.
456 EXPECT_EQ(0u, frames_buffered());
457 EXPECT_FALSE(ConsumeBufferedData(kDataSize, &muted));
460 // Deliver data, we should get non-muted audio.
461 DeliverRemainingAudio();
462 EXPECT_TRUE(ConsumeBufferedData(kDataSize, &muted));
466 TEST_F(AudioRendererImplTest, Underflow_EndOfStream) {
471 // Figure out how long until the ended event should fire. Since
472 // ConsumeBufferedData() doesn't provide audio delay information, the time
473 // until the ended event fires is equivalent to the longest buffered section,
474 // which is the initial frames_buffered() read.
475 TimeDelta time_until_ended = CalculatePlayTime(frames_buffered());
477 // Drain internal buffer, we should have a pending read.
478 EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL));
479 WaitForPendingRead();
481 // Verify the next FillBuffer() call triggers the underflow callback
482 // since the decoder hasn't delivered any data after it was drained.
483 const size_t kDataSize = 1024;
484 EXPECT_CALL(*this, OnUnderflow());
485 EXPECT_FALSE(ConsumeBufferedData(kDataSize, NULL));
487 // Deliver a little bit of data.
488 SatisfyPendingRead(kDataSize);
489 WaitForPendingRead();
491 // Verify we're getting muted audio during underflow.
493 EXPECT_EQ(kDataSize, frames_buffered());
494 EXPECT_FALSE(ConsumeBufferedData(kDataSize, &muted));
497 // Now deliver end of stream, we should get our little bit of data back.
498 DeliverEndOfStream();
499 EXPECT_EQ(kDataSize, frames_buffered());
500 EXPECT_TRUE(ConsumeBufferedData(kDataSize, &muted));
503 // Attempt to read to make sure we're truly at the end of stream.
504 AdvanceTime(time_until_ended);
505 EXPECT_FALSE(ConsumeBufferedData(kDataSize, &muted));
510 TEST_F(AudioRendererImplTest, Underflow_ResumeFromCallback) {
515 // Drain internal buffer, we should have a pending read.
516 EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL));
517 WaitForPendingRead();
519 // Verify the next FillBuffer() call triggers the underflow callback
520 // since the decoder hasn't delivered any data after it was drained.
521 const size_t kDataSize = 1024;
522 EXPECT_CALL(*this, OnUnderflow())
523 .WillOnce(Invoke(this, &AudioRendererImplTest::CallResumeAfterUnderflow));
524 EXPECT_FALSE(ConsumeBufferedData(kDataSize, NULL));
526 // Verify after resuming that we're still not getting data.
528 EXPECT_EQ(0u, frames_buffered());
529 EXPECT_FALSE(ConsumeBufferedData(kDataSize, &muted));
532 // Deliver data, we should get non-muted audio.
533 DeliverRemainingAudio();
534 EXPECT_TRUE(ConsumeBufferedData(kDataSize, &muted));
538 TEST_F(AudioRendererImplTest, Underflow_SetPlaybackRate) {
543 // Drain internal buffer, we should have a pending read.
544 EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL));
545 WaitForPendingRead();
547 EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
549 // Verify the next FillBuffer() call triggers the underflow callback
550 // since the decoder hasn't delivered any data after it was drained.
551 const size_t kDataSize = 1024;
552 EXPECT_CALL(*this, OnUnderflow())
553 .WillOnce(Invoke(this, &AudioRendererImplTest::CallResumeAfterUnderflow));
554 EXPECT_FALSE(ConsumeBufferedData(kDataSize, NULL));
555 EXPECT_EQ(0u, frames_buffered());
557 EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
559 // Simulate playback being paused.
560 renderer_->SetPlaybackRate(0);
562 EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
564 // Deliver data to resolve the underflow.
565 DeliverRemainingAudio();
567 EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
569 // Simulate playback being resumed.
570 renderer_->SetPlaybackRate(1);
572 EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
575 TEST_F(AudioRendererImplTest, AbortPendingRead_Preroll) {
578 // Start prerolling and wait for a read.
579 WaitableMessageLoopEvent event;
580 renderer_->Preroll(TimeDelta(), event.GetPipelineStatusCB());
581 WaitForPendingRead();
583 // Simulate the decoder aborting the pending read.
585 event.RunAndWaitForStatus(PIPELINE_OK);
587 // Preroll again to a different timestamp and verify it completed normally.
588 Preroll(1000, PIPELINE_OK);
591 TEST_F(AudioRendererImplTest, AbortPendingRead_Pause) {
597 // Partially drain internal buffer so we get a pending read.
598 EXPECT_TRUE(ConsumeBufferedData(frames_buffered() / 2, NULL));
599 WaitForPendingRead();
602 WaitableMessageLoopEvent event;
603 renderer_->Pause(event.GetClosure());
605 // Simulate the decoder aborting the pending read.
609 // Preroll again to a different timestamp and verify it completed normally.
610 Preroll(1000, PIPELINE_OK);