- add sources.
[platform/framework/web/crosswalk.git] / src / media / filters / audio_renderer_impl_unittest.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/bind.h"
6 #include "base/callback_helpers.h"
7 #include "base/gtest_prod_util.h"
8 #include "base/memory/scoped_vector.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/stl_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "media/base/audio_buffer.h"
13 #include "media/base/audio_timestamp_helper.h"
14 #include "media/base/fake_audio_renderer_sink.h"
15 #include "media/base/gmock_callback_support.h"
16 #include "media/base/mock_filters.h"
17 #include "media/base/test_helpers.h"
18 #include "media/filters/audio_renderer_impl.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 using ::base::Time;
22 using ::base::TimeTicks;
23 using ::base::TimeDelta;
24 using ::testing::_;
25 using ::testing::AnyNumber;
26 using ::testing::Invoke;
27 using ::testing::Return;
28
29 namespace media {
30
31 // Constants to specify the type of audio data used.
32 static AudioCodec kCodec = kCodecVorbis;
33 static SampleFormat kSampleFormat = kSampleFormatPlanarF32;
34 static ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO;
35 static int kChannels = ChannelLayoutToChannelCount(kChannelLayout);
36 static int kSamplesPerSecond = 44100;
37
38 // Constants for distinguishing between muted audio and playing audio when using
39 // ConsumeBufferedData(). Must match the type needed by kSampleFormat.
40 static float kMutedAudio = 0.0f;
41 static float kPlayingAudio = 0.5f;
42
43 class AudioRendererImplTest : public ::testing::Test {
44  public:
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,
50                                     kSampleFormat,
51                                     kChannelLayout,
52                                     kSamplesPerSecond,
53                                     NULL,
54                                     0,
55                                     false);
56     demuxer_stream_.set_audio_decoder_config(audio_config);
57
58     // Used to save callbacks and run them at a later time.
59     EXPECT_CALL(*decoder_, Read(_))
60         .WillRepeatedly(Invoke(this, &AudioRendererImplTest::ReadDecoder));
61
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()));
69
70     ScopedVector<AudioDecoder> decoders;
71     decoders.push_back(decoder_);
72     sink_ = new FakeAudioRendererSink();
73     renderer_.reset(new AudioRendererImpl(
74         message_loop_.message_loop_proxy(),
75         sink_,
76         decoders.Pass(),
77         SetDecryptorReadyCB(),
78         false));
79
80     // Stub out time.
81     renderer_->set_now_cb_for_testing(base::Bind(
82         &AudioRendererImplTest::GetTime, base::Unretained(this)));
83   }
84
85   virtual ~AudioRendererImplTest() {
86     SCOPED_TRACE("~AudioRendererImplTest()");
87     WaitableMessageLoopEvent event;
88     renderer_->Stop(event.GetClosure());
89     event.RunAndWait();
90   }
91
92   void ExpectUnsupportedAudioDecoder() {
93     EXPECT_CALL(*decoder_, Initialize(_, _, _))
94         .WillOnce(RunCallback<1>(DECODER_ERROR_NOT_SUPPORTED));
95   }
96
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));
106   }
107
108   MOCK_METHOD1(OnStatistics, void(const PipelineStatistics&));
109   MOCK_METHOD0(OnUnderflow, void());
110   MOCK_METHOD0(OnDisabled, void());
111   MOCK_METHOD1(OnError, void(PipelineStatus));
112
113   void OnAudioTimeCallback(TimeDelta current_time, TimeDelta max_time) {
114     CHECK(current_time <= max_time);
115   }
116
117   void Initialize() {
118     EXPECT_CALL(*decoder_, Initialize(_, _, _))
119         .WillOnce(RunCallback<1>(PIPELINE_OK));
120     InitializeWithStatus(PIPELINE_OK);
121
122     next_timestamp_.reset(
123         new AudioTimestampHelper(decoder_->samples_per_second()));
124   }
125
126   void InitializeWithStatus(PipelineStatus expected) {
127     SCOPED_TRACE(base::StringPrintf("InitializeWithStatus(%d)", expected));
128
129     WaitableMessageLoopEvent event;
130     renderer_->Initialize(
131         &demuxer_stream_,
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);
145
146     // We should have no reads.
147     EXPECT_TRUE(read_cb_.is_null());
148   }
149
150   void Preroll() {
151     Preroll(0, PIPELINE_OK);
152   }
153
154   void Preroll(int timestamp_ms, PipelineStatus expected) {
155     SCOPED_TRACE(base::StringPrintf("Preroll(%d, %d)", timestamp_ms, expected));
156
157     TimeDelta timestamp = TimeDelta::FromMilliseconds(timestamp_ms);
158     next_timestamp_->SetBaseTimestamp(timestamp);
159
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);
166
167     // We should have no reads.
168     EXPECT_TRUE(read_cb_.is_null());
169   }
170
171   void Play() {
172     SCOPED_TRACE("Play()");
173     WaitableMessageLoopEvent event;
174     renderer_->Play(event.GetClosure());
175     renderer_->SetPlaybackRate(1.0f);
176     event.RunAndWait();
177   }
178
179   void WaitForEnded() {
180     SCOPED_TRACE("WaitForEnded()");
181     ended_event_.RunAndWait();
182   }
183
184   void WaitForPendingRead() {
185     SCOPED_TRACE("WaitForPendingRead()");
186     if (!read_cb_.is_null())
187       return;
188
189     DCHECK(wait_for_pending_read_cb_.is_null());
190
191     WaitableMessageLoopEvent event;
192     wait_for_pending_read_cb_ = event.GetClosure();
193     event.RunAndWait();
194
195     DCHECK(!read_cb_.is_null());
196     DCHECK(wait_for_pending_read_cb_.is_null());
197   }
198
199   // Delivers |size| frames with value kPlayingAudio to |renderer_|.
200   void SatisfyPendingRead(size_t size) {
201     CHECK(!read_cb_.is_null());
202
203     scoped_refptr<AudioBuffer> buffer =
204         MakePlanarAudioBuffer<float>(kSampleFormat,
205                                      kChannels,
206                                      kPlayingAudio,
207                                      0.0f,
208                                      size,
209                                      next_timestamp_->GetTimestamp(),
210                                      next_timestamp_->GetFrameDuration(size));
211     next_timestamp_->AddFrames(size);
212
213     DeliverBuffer(AudioDecoder::kOk, buffer);
214   }
215
216   void AbortPendingRead() {
217     DeliverBuffer(AudioDecoder::kAborted, NULL);
218   }
219
220   void DeliverEndOfStream() {
221     DeliverBuffer(AudioDecoder::kOk, AudioBuffer::CreateEOSBuffer());
222   }
223
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());
228   }
229
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.
233   //
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));
239     int frames_read;
240     if (!sink_->Render(bus.get(), 0, &frames_read)) {
241       if (muted)
242         *muted = true;
243       return false;
244     }
245
246     if (muted)
247       *muted = frames_read < 1 || bus->channel(0)[0] == kMutedAudio;
248     return frames_read == requested_frames;
249   }
250
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();
256
257     int frames_read = 0;
258     int total_frames_read = 0;
259
260     scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, 1024);
261
262     do {
263       TimeDelta audio_delay = TimeDelta::FromMicroseconds(
264           total_frames_read * Time::kMicrosecondsPerSecond /
265           static_cast<float>(decoder_->samples_per_second()));
266
267       frames_read = renderer_->Render(
268           bus.get(), audio_delay.InMilliseconds());
269       total_frames_read += frames_read;
270     } while (frames_read > 0);
271
272     return total_frames_read;
273   }
274
275   uint32 frames_buffered() {
276     return renderer_->algorithm_->frames_buffered();
277   }
278
279   uint32 buffer_capacity() {
280     return renderer_->algorithm_->QueueCapacity();
281   }
282
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()) {
287       return 0;
288     }
289     return buffer_capacity() - frames_buffered();
290   }
291
292   void CallResumeAfterUnderflow() {
293     renderer_->ResumeAfterUnderflow();
294   }
295
296   TimeDelta CalculatePlayTime(int frames_filled) {
297     return TimeDelta::FromMicroseconds(
298         frames_filled * Time::kMicrosecondsPerSecond /
299         renderer_->audio_parameters_.sample_rate());
300   }
301
302   void EndOfStreamTest(float playback_rate) {
303     Initialize();
304     Preroll();
305     Play();
306     renderer_->SetPlaybackRate(playback_rate);
307
308     // Drain internal buffer, we should have a pending read.
309     int total_frames = frames_buffered();
310     int frames_filled = ConsumeAllBufferedData();
311     WaitForPendingRead();
312
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);
319
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();
323
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();
327
328     // Advance time half way without an ended expectation.
329     AdvanceTime(audio_play_time / 2);
330     ConsumeBufferedData(frames_buffered(), NULL);
331
332     // Advance time by other half and expect the ended event.
333     AdvanceTime(audio_play_time / 2);
334     ConsumeBufferedData(frames_buffered(), NULL);
335     WaitForEnded();
336   }
337
338   void AdvanceTime(TimeDelta time) {
339     base::AutoLock auto_lock(lock_);
340     time_ += time;
341   }
342
343   // Fixture members.
344   base::MessageLoop message_loop_;
345   scoped_ptr<AudioRendererImpl> renderer_;
346   scoped_refptr<FakeAudioRendererSink> sink_;
347
348  private:
349   TimeTicks GetTime() {
350     base::AutoLock auto_lock(lock_);
351     return time_;
352   }
353
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));
360       return;
361     }
362
363     CHECK(read_cb_.is_null()) << "Overlapping reads are not permitted";
364     read_cb_ = read_cb;
365
366     // Wake up WaitForPendingRead() if needed.
367     if (!wait_for_pending_read_cb_.is_null())
368       base::ResetAndReturn(&wait_for_pending_read_cb_).Run();
369   }
370
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);
375   }
376
377   MockDemuxerStream demuxer_stream_;
378   MockAudioDecoder* decoder_;
379
380   // Used for stubbing out time in the audio callback thread.
381   base::Lock lock_;
382   TimeTicks time_;
383
384   // Used for satisfying reads.
385   AudioDecoder::ReadCB read_cb_;
386   scoped_ptr<AudioTimestampHelper> next_timestamp_;
387
388   WaitableMessageLoopEvent ended_event_;
389
390   // Run during ReadDecoder() to unblock WaitForPendingRead().
391   base::Closure wait_for_pending_read_cb_;
392
393   DISALLOW_COPY_AND_ASSIGN(AudioRendererImplTest);
394 };
395
396 TEST_F(AudioRendererImplTest, Initialize_Failed) {
397   ExpectUnsupportedAudioDecoderConfig();
398   InitializeWithStatus(PIPELINE_ERROR_INITIALIZATION_FAILED);
399 }
400
401 TEST_F(AudioRendererImplTest, Initialize_Successful) {
402   Initialize();
403 }
404
405 TEST_F(AudioRendererImplTest, Initialize_DecoderInitFailure) {
406   ExpectUnsupportedAudioDecoder();
407   InitializeWithStatus(DECODER_ERROR_NOT_SUPPORTED);
408 }
409
410 TEST_F(AudioRendererImplTest, Preroll) {
411   Initialize();
412   Preroll();
413 }
414
415 TEST_F(AudioRendererImplTest, Play) {
416   Initialize();
417   Preroll();
418   Play();
419
420   // Drain internal buffer, we should have a pending read.
421   EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL));
422   WaitForPendingRead();
423 }
424
425 TEST_F(AudioRendererImplTest, EndOfStream) {
426   EndOfStreamTest(1.0);
427 }
428
429 TEST_F(AudioRendererImplTest, EndOfStream_FasterPlaybackSpeed) {
430   EndOfStreamTest(2.0);
431 }
432
433 TEST_F(AudioRendererImplTest, EndOfStream_SlowerPlaybackSpeed) {
434   EndOfStreamTest(0.5);
435 }
436
437 TEST_F(AudioRendererImplTest, Underflow) {
438   Initialize();
439   Preroll();
440   Play();
441
442   // Drain internal buffer, we should have a pending read.
443   EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL));
444   WaitForPendingRead();
445
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));
451
452   renderer_->ResumeAfterUnderflow();
453
454   // Verify after resuming that we're still not getting data.
455   bool muted = false;
456   EXPECT_EQ(0u, frames_buffered());
457   EXPECT_FALSE(ConsumeBufferedData(kDataSize, &muted));
458   EXPECT_TRUE(muted);
459
460   // Deliver data, we should get non-muted audio.
461   DeliverRemainingAudio();
462   EXPECT_TRUE(ConsumeBufferedData(kDataSize, &muted));
463   EXPECT_FALSE(muted);
464 }
465
466 TEST_F(AudioRendererImplTest, Underflow_EndOfStream) {
467   Initialize();
468   Preroll();
469   Play();
470
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());
476
477   // Drain internal buffer, we should have a pending read.
478   EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL));
479   WaitForPendingRead();
480
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));
486
487   // Deliver a little bit of data.
488   SatisfyPendingRead(kDataSize);
489   WaitForPendingRead();
490
491   // Verify we're getting muted audio during underflow.
492   bool muted = false;
493   EXPECT_EQ(kDataSize, frames_buffered());
494   EXPECT_FALSE(ConsumeBufferedData(kDataSize, &muted));
495   EXPECT_TRUE(muted);
496
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));
501   EXPECT_FALSE(muted);
502
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));
506   EXPECT_TRUE(muted);
507   WaitForEnded();
508 }
509
510 TEST_F(AudioRendererImplTest, Underflow_ResumeFromCallback) {
511   Initialize();
512   Preroll();
513   Play();
514
515   // Drain internal buffer, we should have a pending read.
516   EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL));
517   WaitForPendingRead();
518
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));
525
526   // Verify after resuming that we're still not getting data.
527   bool muted = false;
528   EXPECT_EQ(0u, frames_buffered());
529   EXPECT_FALSE(ConsumeBufferedData(kDataSize, &muted));
530   EXPECT_TRUE(muted);
531
532   // Deliver data, we should get non-muted audio.
533   DeliverRemainingAudio();
534   EXPECT_TRUE(ConsumeBufferedData(kDataSize, &muted));
535   EXPECT_FALSE(muted);
536 }
537
538 TEST_F(AudioRendererImplTest, Underflow_SetPlaybackRate) {
539   Initialize();
540   Preroll();
541   Play();
542
543   // Drain internal buffer, we should have a pending read.
544   EXPECT_TRUE(ConsumeBufferedData(frames_buffered(), NULL));
545   WaitForPendingRead();
546
547   EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
548
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());
556
557   EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
558
559   // Simulate playback being paused.
560   renderer_->SetPlaybackRate(0);
561
562   EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
563
564   // Deliver data to resolve the underflow.
565   DeliverRemainingAudio();
566
567   EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
568
569   // Simulate playback being resumed.
570   renderer_->SetPlaybackRate(1);
571
572   EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
573 }
574
575 TEST_F(AudioRendererImplTest, AbortPendingRead_Preroll) {
576   Initialize();
577
578   // Start prerolling and wait for a read.
579   WaitableMessageLoopEvent event;
580   renderer_->Preroll(TimeDelta(), event.GetPipelineStatusCB());
581   WaitForPendingRead();
582
583   // Simulate the decoder aborting the pending read.
584   AbortPendingRead();
585   event.RunAndWaitForStatus(PIPELINE_OK);
586
587   // Preroll again to a different timestamp and verify it completed normally.
588   Preroll(1000, PIPELINE_OK);
589 }
590
591 TEST_F(AudioRendererImplTest, AbortPendingRead_Pause) {
592   Initialize();
593
594   Preroll();
595   Play();
596
597   // Partially drain internal buffer so we get a pending read.
598   EXPECT_TRUE(ConsumeBufferedData(frames_buffered() / 2, NULL));
599   WaitForPendingRead();
600
601   // Start pausing.
602   WaitableMessageLoopEvent event;
603   renderer_->Pause(event.GetClosure());
604
605   // Simulate the decoder aborting the pending read.
606   AbortPendingRead();
607   event.RunAndWait();
608
609   // Preroll again to a different timestamp and verify it completed normally.
610   Preroll(1000, PIPELINE_OK);
611 }
612
613 }  // namespace media