1 // Copyright (c) 2011 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"
9 #include "base/logging.h"
10 #include "media/base/filter_host.h"
11 #include "media/audio/audio_manager.h"
15 // We'll try to fill 4096 samples per buffer, which is roughly ~92ms of audio
16 // data for a 44.1kHz audio source.
17 static const size_t kSamplesPerBuffer
= 8*1024;
19 AudioRendererImpl::AudioRendererImpl()
20 : AudioRendererBase(),
22 bytes_per_second_(0) {
25 AudioRendererImpl::~AudioRendererImpl() {
26 // Close down the audio device.
33 void AudioRendererImpl::SetPlaybackRate(float rate
) {
34 // TODO(fbarchard): limit rate to reasonable values
35 AudioRendererBase::SetPlaybackRate(rate
);
37 static bool started
= false;
38 if (rate
> 0.0f
&& !started
&& stream_
)
42 void AudioRendererImpl::SetVolume(float volume
) {
44 stream_
->SetVolume(volume
);
47 uint32
AudioRendererImpl::OnMoreData(
48 AudioOutputStream
* stream
, uint8
* dest
, uint32 len
,
49 AudioBuffersState buffers_state
) {
50 // TODO(scherkus): handle end of stream.
54 // TODO(fbarchard): Waveout_output_win.h should handle zero length buffers
56 uint32 pending_bytes
= static_cast<uint32
>(ceil(buffers_state
.total_bytes() *
58 base::TimeDelta delay
= base::TimeDelta::FromMicroseconds(
59 base::Time::kMicrosecondsPerSecond
* pending_bytes
/
61 bool buffers_empty
= buffers_state
.pending_bytes
== 0;
62 return FillBuffer(dest
, len
, delay
, buffers_empty
);
65 void AudioRendererImpl::OnClose(AudioOutputStream
* stream
) {
66 // TODO(scherkus): implement OnClose.
70 void AudioRendererImpl::OnError(AudioOutputStream
* stream
, int code
) {
71 // TODO(scherkus): implement OnError.
75 bool AudioRendererImpl::OnInitialize(const AudioDecoderConfig
& config
) {
76 AudioParameters
params(config
);
77 params
.samples_per_packet
= kSamplesPerBuffer
;
79 bytes_per_second_
= params
.GetBytesPerSecond();
81 // Create our audio stream.
82 stream_
= AudioManager::GetAudioManager()->MakeAudioOutputStream(params
);
86 if (!stream_
->Open()) {
93 void AudioRendererImpl::OnStop() {