Make use of CreateWebUIForRenderManager (doesn't change the actual behavior)
[chromium-blink-merge.git] / media / filters / audio_renderer_impl.cc
blob2acb40163b2369c0f7d25c3b90fcaa66c29336e5
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 "media/filters/audio_renderer_impl.h"
7 #include <math.h>
9 #include <algorithm>
11 #include "base/bind.h"
12 #include "base/callback.h"
13 #include "base/callback_helpers.h"
14 #include "base/command_line.h"
15 #include "base/logging.h"
16 #include "base/message_loop_proxy.h"
17 #include "media/audio/audio_util.h"
18 #include "media/base/audio_splicer.h"
19 #include "media/base/bind_to_loop.h"
20 #include "media/base/data_buffer.h"
21 #include "media/base/demuxer_stream.h"
22 #include "media/base/media_switches.h"
23 #include "media/filters/audio_decoder_selector.h"
24 #include "media/filters/decrypting_demuxer_stream.h"
26 namespace media {
28 AudioRendererImpl::AudioRendererImpl(
29 const scoped_refptr<base::MessageLoopProxy>& message_loop,
30 media::AudioRendererSink* sink,
31 const SetDecryptorReadyCB& set_decryptor_ready_cb)
32 : message_loop_(message_loop),
33 weak_factory_(this),
34 sink_(sink),
35 set_decryptor_ready_cb_(set_decryptor_ready_cb),
36 now_cb_(base::Bind(&base::Time::Now)),
37 state_(kUninitialized),
38 pending_read_(false),
39 received_end_of_stream_(false),
40 rendered_end_of_stream_(false),
41 audio_time_buffered_(kNoTimestamp()),
42 current_time_(kNoTimestamp()),
43 underflow_disabled_(false),
44 preroll_aborted_(false),
45 actual_frames_per_buffer_(0) {
48 AudioRendererImpl::~AudioRendererImpl() {
49 // Stop() should have been called and |algorithm_| should have been destroyed.
50 DCHECK(state_ == kUninitialized || state_ == kStopped);
51 DCHECK(!algorithm_.get());
54 void AudioRendererImpl::Play(const base::Closure& callback) {
55 DCHECK(message_loop_->BelongsToCurrentThread());
57 float playback_rate = 0;
59 base::AutoLock auto_lock(lock_);
60 DCHECK_EQ(kPaused, state_);
61 state_ = kPlaying;
62 callback.Run();
63 playback_rate = algorithm_->playback_rate();
66 if (playback_rate != 0.0f) {
67 DoPlay();
68 } else {
69 DoPause();
73 void AudioRendererImpl::DoPlay() {
74 DCHECK(message_loop_->BelongsToCurrentThread());
75 DCHECK(sink_);
77 base::AutoLock auto_lock(lock_);
78 earliest_end_time_ = now_cb_.Run();
80 sink_->Play();
83 void AudioRendererImpl::Pause(const base::Closure& callback) {
84 DCHECK(message_loop_->BelongsToCurrentThread());
87 base::AutoLock auto_lock(lock_);
88 DCHECK(state_ == kPlaying || state_ == kUnderflow ||
89 state_ == kRebuffering);
90 pause_cb_ = callback;
91 state_ = kPaused;
93 // Pause only when we've completed our pending read.
94 if (!pending_read_)
95 base::ResetAndReturn(&pause_cb_).Run();
98 DoPause();
101 void AudioRendererImpl::DoPause() {
102 DCHECK(message_loop_->BelongsToCurrentThread());
103 DCHECK(sink_);
104 sink_->Pause(false);
107 void AudioRendererImpl::Flush(const base::Closure& callback) {
108 DCHECK(message_loop_->BelongsToCurrentThread());
110 if (decrypting_demuxer_stream_) {
111 decrypting_demuxer_stream_->Reset(base::Bind(
112 &AudioRendererImpl::ResetDecoder, weak_this_, callback));
113 return;
116 decoder_->Reset(callback);
119 void AudioRendererImpl::ResetDecoder(const base::Closure& callback) {
120 DCHECK(message_loop_->BelongsToCurrentThread());
121 decoder_->Reset(callback);
124 void AudioRendererImpl::Stop(const base::Closure& callback) {
125 DCHECK(message_loop_->BelongsToCurrentThread());
126 DCHECK(!callback.is_null());
128 // TODO(scherkus): Consider invalidating |weak_factory_| and replacing
129 // task-running guards that check |state_| with DCHECK().
131 if (sink_) {
132 sink_->Stop();
133 sink_ = NULL;
137 base::AutoLock auto_lock(lock_);
138 state_ = kStopped;
139 algorithm_.reset(NULL);
140 init_cb_.Reset();
141 underflow_cb_.Reset();
142 time_cb_.Reset();
145 callback.Run();
148 void AudioRendererImpl::Preroll(base::TimeDelta time,
149 const PipelineStatusCB& cb) {
150 DCHECK(message_loop_->BelongsToCurrentThread());
151 DCHECK(sink_);
154 base::AutoLock auto_lock(lock_);
155 DCHECK_EQ(kPaused, state_);
156 DCHECK(!pending_read_) << "Pending read must complete before seeking";
157 DCHECK(pause_cb_.is_null());
158 DCHECK(preroll_cb_.is_null());
159 state_ = kPrerolling;
160 preroll_cb_ = cb;
161 preroll_timestamp_ = time;
163 // Throw away everything and schedule our reads.
164 audio_time_buffered_ = kNoTimestamp();
165 current_time_ = kNoTimestamp();
166 received_end_of_stream_ = false;
167 rendered_end_of_stream_ = false;
168 preroll_aborted_ = false;
170 splicer_->Reset();
171 algorithm_->FlushBuffers();
172 earliest_end_time_ = now_cb_.Run();
174 AttemptRead_Locked();
177 // Pause and flush the stream when we preroll to a new location.
178 sink_->Pause(true);
181 void AudioRendererImpl::Initialize(const scoped_refptr<DemuxerStream>& stream,
182 const AudioDecoderList& decoders,
183 const PipelineStatusCB& init_cb,
184 const StatisticsCB& statistics_cb,
185 const base::Closure& underflow_cb,
186 const TimeCB& time_cb,
187 const base::Closure& ended_cb,
188 const base::Closure& disabled_cb,
189 const PipelineStatusCB& error_cb) {
190 DCHECK(message_loop_->BelongsToCurrentThread());
191 DCHECK(stream);
192 DCHECK(!decoders.empty());
193 DCHECK_EQ(stream->type(), DemuxerStream::AUDIO);
194 DCHECK(!init_cb.is_null());
195 DCHECK(!statistics_cb.is_null());
196 DCHECK(!underflow_cb.is_null());
197 DCHECK(!time_cb.is_null());
198 DCHECK(!ended_cb.is_null());
199 DCHECK(!disabled_cb.is_null());
200 DCHECK(!error_cb.is_null());
201 DCHECK_EQ(kUninitialized, state_);
202 DCHECK(sink_);
204 weak_this_ = weak_factory_.GetWeakPtr();
205 init_cb_ = init_cb;
206 statistics_cb_ = statistics_cb;
207 underflow_cb_ = underflow_cb;
208 time_cb_ = time_cb;
209 ended_cb_ = ended_cb;
210 disabled_cb_ = disabled_cb;
211 error_cb_ = error_cb;
213 scoped_ptr<AudioDecoderSelector> decoder_selector(
214 new AudioDecoderSelector(base::MessageLoopProxy::current(),
215 decoders,
216 set_decryptor_ready_cb_));
218 // To avoid calling |decoder_selector| methods and passing ownership of
219 // |decoder_selector| in the same line.
220 AudioDecoderSelector* decoder_selector_ptr = decoder_selector.get();
222 decoder_selector_ptr->SelectAudioDecoder(
223 stream,
224 statistics_cb,
225 base::Bind(&AudioRendererImpl::OnDecoderSelected, weak_this_,
226 base::Passed(&decoder_selector)));
229 void AudioRendererImpl::OnDecoderSelected(
230 scoped_ptr<AudioDecoderSelector> decoder_selector,
231 const scoped_refptr<AudioDecoder>& selected_decoder,
232 const scoped_refptr<DecryptingDemuxerStream>& decrypting_demuxer_stream) {
233 DCHECK(message_loop_->BelongsToCurrentThread());
235 if (state_ == kStopped) {
236 DCHECK(!sink_);
237 return;
240 if (!selected_decoder) {
241 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED);
242 return;
245 decoder_ = selected_decoder;
246 decrypting_demuxer_stream_ = decrypting_demuxer_stream;
248 int sample_rate = decoder_->samples_per_second();
249 int buffer_size = GetHighLatencyOutputBufferSize(sample_rate);
250 AudioParameters::Format format = AudioParameters::AUDIO_PCM_LINEAR;
252 // Either AudioOutputResampler or renderer side mixing must be enabled to use
253 // the low latency pipeline.
254 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
255 if (!cmd_line->HasSwitch(switches::kDisableRendererSideMixing) ||
256 !cmd_line->HasSwitch(switches::kDisableAudioOutputResampler)) {
257 // There are two cases here:
259 // 1. Renderer side mixing is enabled and the buffer size is actually
260 // controlled by the size of the AudioBus provided to Render(). In this
261 // case the buffer size below is ignored.
263 // 2. Renderer side mixing is disabled and AudioOutputResampler on the
264 // browser side is rebuffering to the hardware size on the fly.
266 // In the second case we need to choose a a buffer size small enough that
267 // the decoder can fulfill the high frequency low latency audio callbacks,
268 // but not so small that it's less than the hardware buffer size (or we'll
269 // run into issues since the shared memory sync is non-blocking).
271 // The buffer size below is arbitrarily the same size used by Pepper Flash
272 // for consistency. Since renderer side mixing is only disabled for debug
273 // purposes it's okay that this buffer size might lead to jitter since it's
274 // not a multiple of the hardware buffer size.
275 format = AudioParameters::AUDIO_PCM_LOW_LATENCY;
276 buffer_size = 2048;
279 audio_parameters_ = AudioParameters(
280 format, decoder_->channel_layout(), sample_rate,
281 decoder_->bits_per_channel(), buffer_size);
282 if (!audio_parameters_.IsValid()) {
283 base::ResetAndReturn(&init_cb_).Run(PIPELINE_ERROR_INITIALIZATION_FAILED);
284 return;
287 int channels = ChannelLayoutToChannelCount(decoder_->channel_layout());
288 int bytes_per_frame = channels * decoder_->bits_per_channel() / 8;
289 splicer_.reset(new AudioSplicer(bytes_per_frame, sample_rate));
291 // We're all good! Continue initializing the rest of the audio renderer based
292 // on the decoder format.
293 algorithm_.reset(new AudioRendererAlgorithm());
294 algorithm_->Initialize(0, audio_parameters_);
296 state_ = kPaused;
298 sink_->Initialize(audio_parameters_, weak_this_);
299 sink_->Start();
301 base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK);
304 void AudioRendererImpl::ResumeAfterUnderflow(bool buffer_more_audio) {
305 DCHECK(message_loop_->BelongsToCurrentThread());
306 base::AutoLock auto_lock(lock_);
307 if (state_ == kUnderflow) {
308 // The "&& preroll_aborted_" is a hack. If preroll is aborted, then we
309 // shouldn't even reach the kUnderflow state to begin with. But for now
310 // we're just making sure that the audio buffer capacity (i.e. the
311 // number of bytes that need to be buffered for preroll to complete)
312 // does not increase due to an aborted preroll.
313 // TODO(vrk): Fix this bug correctly! (crbug.com/151352)
314 if (buffer_more_audio && !preroll_aborted_)
315 algorithm_->IncreaseQueueCapacity();
317 state_ = kRebuffering;
321 void AudioRendererImpl::SetVolume(float volume) {
322 DCHECK(message_loop_->BelongsToCurrentThread());
323 DCHECK(sink_);
324 sink_->SetVolume(volume);
327 void AudioRendererImpl::DecodedAudioReady(
328 AudioDecoder::Status status,
329 const scoped_refptr<DataBuffer>& buffer) {
330 DCHECK(message_loop_->BelongsToCurrentThread());
332 base::AutoLock auto_lock(lock_);
333 DCHECK(state_ == kPaused || state_ == kPrerolling || state_ == kPlaying ||
334 state_ == kUnderflow || state_ == kRebuffering || state_ == kStopped);
336 CHECK(pending_read_);
337 pending_read_ = false;
339 if (status == AudioDecoder::kAborted) {
340 HandleAbortedReadOrDecodeError(false);
341 return;
344 if (status == AudioDecoder::kDecodeError) {
345 HandleAbortedReadOrDecodeError(true);
346 return;
349 DCHECK_EQ(status, AudioDecoder::kOk);
350 DCHECK(buffer);
352 if (!splicer_->AddInput(buffer)) {
353 HandleAbortedReadOrDecodeError(true);
354 return;
357 if (!splicer_->HasNextBuffer()) {
358 AttemptRead_Locked();
359 return;
362 bool need_another_buffer = false;
363 while (splicer_->HasNextBuffer())
364 need_another_buffer = HandleSplicerBuffer(splicer_->GetNextBuffer());
366 if (!need_another_buffer && !CanRead_Locked())
367 return;
369 AttemptRead_Locked();
372 bool AudioRendererImpl::HandleSplicerBuffer(
373 const scoped_refptr<DataBuffer>& buffer) {
374 if (buffer->IsEndOfStream()) {
375 received_end_of_stream_ = true;
377 // Transition to kPlaying if we are currently handling an underflow since
378 // no more data will be arriving.
379 if (state_ == kUnderflow || state_ == kRebuffering)
380 state_ = kPlaying;
383 switch (state_) {
384 case kUninitialized:
385 NOTREACHED();
386 return false;
387 case kPaused:
388 if (!buffer->IsEndOfStream())
389 algorithm_->EnqueueBuffer(buffer);
390 DCHECK(!pending_read_);
391 base::ResetAndReturn(&pause_cb_).Run();
392 return false;
393 case kPrerolling:
394 if (IsBeforePrerollTime(buffer))
395 return true;
397 if (!buffer->IsEndOfStream()) {
398 algorithm_->EnqueueBuffer(buffer);
399 if (!algorithm_->IsQueueFull())
400 return false;
402 state_ = kPaused;
403 base::ResetAndReturn(&preroll_cb_).Run(PIPELINE_OK);
404 return false;
405 case kPlaying:
406 case kUnderflow:
407 case kRebuffering:
408 if (!buffer->IsEndOfStream())
409 algorithm_->EnqueueBuffer(buffer);
410 return false;
411 case kStopped:
412 return false;
414 return false;
417 void AudioRendererImpl::AttemptRead() {
418 base::AutoLock auto_lock(lock_);
419 AttemptRead_Locked();
422 void AudioRendererImpl::AttemptRead_Locked() {
423 DCHECK(message_loop_->BelongsToCurrentThread());
424 lock_.AssertAcquired();
426 if (!CanRead_Locked())
427 return;
429 pending_read_ = true;
430 decoder_->Read(base::Bind(&AudioRendererImpl::DecodedAudioReady, weak_this_));
433 bool AudioRendererImpl::CanRead_Locked() {
434 lock_.AssertAcquired();
436 switch (state_) {
437 case kUninitialized:
438 case kPaused:
439 case kStopped:
440 return false;
442 case kPrerolling:
443 case kPlaying:
444 case kUnderflow:
445 case kRebuffering:
446 break;
449 return !pending_read_ && !received_end_of_stream_ &&
450 !algorithm_->IsQueueFull();
453 void AudioRendererImpl::SetPlaybackRate(float playback_rate) {
454 DCHECK(message_loop_->BelongsToCurrentThread());
455 DCHECK_LE(0.0f, playback_rate);
456 DCHECK(sink_);
458 // We have two cases here:
459 // Play: current_playback_rate == 0.0 && playback_rate != 0.0
460 // Pause: current_playback_rate != 0.0 && playback_rate == 0.0
461 float current_playback_rate = algorithm_->playback_rate();
462 if (current_playback_rate == 0.0f && playback_rate != 0.0f) {
463 DoPlay();
464 } else if (current_playback_rate != 0.0f && playback_rate == 0.0f) {
465 // Pause is easy, we can always pause.
466 DoPause();
469 base::AutoLock auto_lock(lock_);
470 algorithm_->SetPlaybackRate(playback_rate);
473 bool AudioRendererImpl::IsBeforePrerollTime(
474 const scoped_refptr<DataBuffer>& buffer) {
475 return (state_ == kPrerolling) && buffer && !buffer->IsEndOfStream() &&
476 (buffer->GetTimestamp() + buffer->GetDuration()) < preroll_timestamp_;
479 int AudioRendererImpl::Render(AudioBus* audio_bus,
480 int audio_delay_milliseconds) {
481 if (actual_frames_per_buffer_ != audio_bus->frames()) {
482 audio_buffer_.reset(
483 new uint8[audio_bus->frames() * audio_parameters_.GetBytesPerFrame()]);
484 actual_frames_per_buffer_ = audio_bus->frames();
487 int frames_filled = FillBuffer(
488 audio_buffer_.get(), audio_bus->frames(), audio_delay_milliseconds);
489 DCHECK_LE(frames_filled, actual_frames_per_buffer_);
491 // Deinterleave audio data into the output bus.
492 audio_bus->FromInterleaved(
493 audio_buffer_.get(), frames_filled,
494 audio_parameters_.bits_per_sample() / 8);
496 return frames_filled;
499 uint32 AudioRendererImpl::FillBuffer(uint8* dest,
500 uint32 requested_frames,
501 int audio_delay_milliseconds) {
502 base::TimeDelta current_time = kNoTimestamp();
503 base::TimeDelta max_time = kNoTimestamp();
504 base::TimeDelta playback_delay = base::TimeDelta::FromMilliseconds(
505 audio_delay_milliseconds);
507 size_t frames_written = 0;
508 base::Closure underflow_cb;
510 base::AutoLock auto_lock(lock_);
512 // Ensure Stop() hasn't destroyed our |algorithm_| on the pipeline thread.
513 if (!algorithm_)
514 return 0;
516 float playback_rate = algorithm_->playback_rate();
517 if (playback_rate == 0.0f)
518 return 0;
520 if (state_ == kRebuffering && algorithm_->IsQueueFull())
521 state_ = kPlaying;
523 // Mute audio by returning 0 when not playing.
524 if (state_ != kPlaying) {
525 // TODO(scherkus): To keep the audio hardware busy we write at most 8k of
526 // zeros. This gets around the tricky situation of pausing and resuming
527 // the audio IPC layer in Chrome. Ideally, we should return zero and then
528 // the subclass can restart the conversation.
530 // This should get handled by the subclass http://crbug.com/106600
531 const uint32 kZeroLength = 8192;
532 size_t zeros_to_write = std::min(
533 kZeroLength, requested_frames * audio_parameters_.GetBytesPerFrame());
534 memset(dest, 0, zeros_to_write);
535 return zeros_to_write / audio_parameters_.GetBytesPerFrame();
538 // We use the following conditions to determine end of playback:
539 // 1) Algorithm can not fill the audio callback buffer
540 // 2) We received an end of stream buffer
541 // 3) We haven't already signalled that we've ended
542 // 4) Our estimated earliest end time has expired
544 // TODO(enal): we should replace (4) with a check that the browser has no
545 // more audio data or at least use a delayed callback.
547 // We use the following conditions to determine underflow:
548 // 1) Algorithm can not fill the audio callback buffer
549 // 2) We have NOT received an end of stream buffer
550 // 3) We are in the kPlaying state
552 // Otherwise the buffer has data we can send to the device.
553 frames_written = algorithm_->FillBuffer(dest, requested_frames);
554 if (frames_written == 0) {
555 base::Time now = now_cb_.Run();
557 if (received_end_of_stream_ && !rendered_end_of_stream_ &&
558 now >= earliest_end_time_) {
559 rendered_end_of_stream_ = true;
560 ended_cb_.Run();
561 } else if (!received_end_of_stream_ && state_ == kPlaying &&
562 !underflow_disabled_) {
563 state_ = kUnderflow;
564 underflow_cb = underflow_cb_;
565 } else {
566 // We can't write any data this cycle. For example, we may have
567 // sent all available data to the audio device while not reaching
568 // |earliest_end_time_|.
572 if (CanRead_Locked()) {
573 message_loop_->PostTask(FROM_HERE, base::Bind(
574 &AudioRendererImpl::AttemptRead, weak_this_));
577 // The |audio_time_buffered_| is the ending timestamp of the last frame
578 // buffered at the audio device. |playback_delay| is the amount of time
579 // buffered at the audio device. The current time can be computed by their
580 // difference.
581 if (audio_time_buffered_ != kNoTimestamp()) {
582 // Adjust the delay according to playback rate.
583 base::TimeDelta adjusted_playback_delay =
584 base::TimeDelta::FromMicroseconds(ceil(
585 playback_delay.InMicroseconds() * playback_rate));
587 base::TimeDelta previous_time = current_time_;
588 current_time_ = audio_time_buffered_ - adjusted_playback_delay;
590 // Time can change in one of two ways:
591 // 1) The time of the audio data at the audio device changed, or
592 // 2) The playback delay value has changed
594 // We only want to set |current_time| (and thus execute |time_cb_|) if
595 // time has progressed and we haven't signaled end of stream yet.
597 // Why? The current latency of the system results in getting the last call
598 // to FillBuffer() later than we'd like, which delays firing the 'ended'
599 // event, which delays the looping/trigging performance of short sound
600 // effects.
602 // TODO(scherkus): revisit this and switch back to relying on playback
603 // delay after we've revamped our audio IPC subsystem.
604 if (current_time_ > previous_time && !rendered_end_of_stream_) {
605 current_time = current_time_;
609 // The call to FillBuffer() on |algorithm_| has increased the amount of
610 // buffered audio data. Update the new amount of time buffered.
611 max_time = algorithm_->GetTime();
612 audio_time_buffered_ = max_time;
614 UpdateEarliestEndTime_Locked(
615 frames_written, playback_delay, now_cb_.Run());
618 if (current_time != kNoTimestamp() && max_time != kNoTimestamp()) {
619 time_cb_.Run(current_time, max_time);
622 if (!underflow_cb.is_null())
623 underflow_cb.Run();
625 return frames_written;
628 void AudioRendererImpl::UpdateEarliestEndTime_Locked(
629 int frames_filled, base::TimeDelta playback_delay, base::Time time_now) {
630 if (frames_filled <= 0)
631 return;
633 base::TimeDelta predicted_play_time = base::TimeDelta::FromMicroseconds(
634 static_cast<float>(frames_filled) * base::Time::kMicrosecondsPerSecond /
635 audio_parameters_.sample_rate());
637 lock_.AssertAcquired();
638 earliest_end_time_ = std::max(
639 earliest_end_time_, time_now + playback_delay + predicted_play_time);
642 void AudioRendererImpl::OnRenderError() {
643 disabled_cb_.Run();
646 void AudioRendererImpl::DisableUnderflowForTesting() {
647 underflow_disabled_ = true;
650 void AudioRendererImpl::HandleAbortedReadOrDecodeError(bool is_decode_error) {
651 PipelineStatus status = is_decode_error ? PIPELINE_ERROR_DECODE : PIPELINE_OK;
652 switch (state_) {
653 case kUninitialized:
654 NOTREACHED();
655 return;
656 case kPaused:
657 if (status != PIPELINE_OK)
658 error_cb_.Run(status);
659 base::ResetAndReturn(&pause_cb_).Run();
660 return;
661 case kPrerolling:
662 // This is a signal for abort if it's not an error.
663 preroll_aborted_ = !is_decode_error;
664 state_ = kPaused;
665 base::ResetAndReturn(&preroll_cb_).Run(status);
666 return;
667 case kPlaying:
668 case kUnderflow:
669 case kRebuffering:
670 case kStopped:
671 if (status != PIPELINE_OK)
672 error_cb_.Run(status);
673 return;
677 } // namespace media