| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/filters/audio_renderer_impl.h" | 5 #include "media/filters/reference_audio_renderer.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "media/base/filter_host.h" | 10 #include "media/base/filter_host.h" |
| 11 #include "media/audio/audio_manager.h" | 11 #include "media/audio/audio_manager.h" |
| 12 | 12 |
| 13 namespace media { | 13 namespace media { |
| 14 | 14 |
| 15 // We'll try to fill 4096 samples per buffer, which is roughly ~92ms of audio | 15 // This is an arbitrary number, ~184ms in a 44.1kHz stream, assuming a playback |
| 16 // data for a 44.1kHz audio source. | 16 // rate of 1.0. |
| 17 static const size_t kSamplesPerBuffer = 8*1024; | 17 static const size_t kSamplesPerBuffer = 8 * 1024; |
| 18 | 18 |
| 19 AudioRendererImpl::AudioRendererImpl() | 19 ReferenceAudioRenderer::ReferenceAudioRenderer() |
| 20 : AudioRendererBase(), | 20 : AudioRendererBase(), |
| 21 stream_(NULL), | 21 stream_(NULL), |
| 22 bytes_per_second_(0) { | 22 bytes_per_second_(0) { |
| 23 } | 23 } |
| 24 | 24 |
| 25 AudioRendererImpl::~AudioRendererImpl() { | 25 ReferenceAudioRenderer::~ReferenceAudioRenderer() { |
| 26 // Close down the audio device. | 26 // Close down the audio device. |
| 27 if (stream_) { | 27 if (stream_) { |
| 28 stream_->Stop(); | 28 stream_->Stop(); |
| 29 stream_->Close(); | 29 stream_->Close(); |
| 30 } | 30 } |
| 31 } | 31 } |
| 32 | 32 |
| 33 void AudioRendererImpl::SetPlaybackRate(float rate) { | 33 void ReferenceAudioRenderer::SetPlaybackRate(float rate) { |
| 34 // TODO(fbarchard): limit rate to reasonable values | 34 // TODO(fbarchard): limit rate to reasonable values |
| 35 AudioRendererBase::SetPlaybackRate(rate); | 35 AudioRendererBase::SetPlaybackRate(rate); |
| 36 | 36 |
| 37 static bool started = false; | 37 static bool started = false; |
| 38 if (rate > 0.0f && !started && stream_) | 38 if (rate > 0.0f && !started && stream_) |
| 39 stream_->Start(this); | 39 stream_->Start(this); |
| 40 } | 40 } |
| 41 | 41 |
| 42 void AudioRendererImpl::SetVolume(float volume) { | 42 void ReferenceAudioRenderer::SetVolume(float volume) { |
| 43 if (stream_) | 43 if (stream_) |
| 44 stream_->SetVolume(volume); | 44 stream_->SetVolume(volume); |
| 45 } | 45 } |
| 46 | 46 |
| 47 uint32 AudioRendererImpl::OnMoreData( | 47 uint32 ReferenceAudioRenderer::OnMoreData( |
| 48 AudioOutputStream* stream, uint8* dest, uint32 len, | 48 AudioOutputStream* stream, uint8* dest, uint32 len, |
| 49 AudioBuffersState buffers_state) { | 49 AudioBuffersState buffers_state) { |
| 50 // TODO(scherkus): handle end of stream. | 50 // TODO(scherkus): handle end of stream. |
| 51 if (!stream_) | 51 if (!stream_) |
| 52 return 0; | 52 return 0; |
| 53 | 53 |
| 54 // TODO(fbarchard): Waveout_output_win.h should handle zero length buffers | 54 // TODO(fbarchard): Waveout_output_win.h should handle zero length buffers |
| 55 // without clicking. | 55 // without clicking. |
| 56 uint32 pending_bytes = static_cast<uint32>(ceil(buffers_state.total_bytes() * | 56 uint32 pending_bytes = static_cast<uint32>(ceil(buffers_state.total_bytes() * |
| 57 GetPlaybackRate())); | 57 GetPlaybackRate())); |
| 58 base::TimeDelta delay = base::TimeDelta::FromMicroseconds( | 58 base::TimeDelta delay = base::TimeDelta::FromMicroseconds( |
| 59 base::Time::kMicrosecondsPerSecond * pending_bytes / | 59 base::Time::kMicrosecondsPerSecond * pending_bytes / |
| 60 bytes_per_second_); | 60 bytes_per_second_); |
| 61 bool buffers_empty = buffers_state.pending_bytes == 0; | 61 bool buffers_empty = buffers_state.pending_bytes == 0; |
| 62 return FillBuffer(dest, len, delay, buffers_empty); | 62 return FillBuffer(dest, len, delay, buffers_empty); |
| 63 } | 63 } |
| 64 | 64 |
| 65 void AudioRendererImpl::OnClose(AudioOutputStream* stream) { | 65 void ReferenceAudioRenderer::OnClose(AudioOutputStream* stream) { |
| 66 // TODO(scherkus): implement OnClose. | 66 // TODO(scherkus): implement OnClose. |
| 67 NOTIMPLEMENTED(); | 67 NOTIMPLEMENTED(); |
| 68 } | 68 } |
| 69 | 69 |
| 70 void AudioRendererImpl::OnError(AudioOutputStream* stream, int code) { | 70 void ReferenceAudioRenderer::OnError(AudioOutputStream* stream, int code) { |
| 71 // TODO(scherkus): implement OnError. | 71 // TODO(scherkus): implement OnError. |
| 72 NOTIMPLEMENTED(); | 72 NOTIMPLEMENTED(); |
| 73 } | 73 } |
| 74 | 74 |
| 75 bool AudioRendererImpl::OnInitialize(int bits_per_channel, | 75 bool ReferenceAudioRenderer::OnInitialize(int bits_per_channel, |
| 76 ChannelLayout channel_layout, | 76 ChannelLayout channel_layout, |
| 77 int sample_rate) { | 77 int sample_rate) { |
| 78 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, channel_layout, | 78 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, channel_layout, |
| 79 sample_rate, bits_per_channel, kSamplesPerBuffer); | 79 sample_rate, bits_per_channel, kSamplesPerBuffer); |
| 80 | 80 |
| 81 bytes_per_second_ = params.GetBytesPerSecond(); | 81 bytes_per_second_ = params.GetBytesPerSecond(); |
| 82 | 82 |
| 83 // Create our audio stream. | 83 // Create our audio stream. |
| 84 stream_ = AudioManager::GetAudioManager()->MakeAudioOutputStream(params); | 84 stream_ = AudioManager::GetAudioManager()->MakeAudioOutputStream(params); |
| 85 if (!stream_) | 85 if (!stream_) |
| 86 return false; | 86 return false; |
| 87 | 87 |
| 88 if (!stream_->Open()) { | 88 if (!stream_->Open()) { |
| 89 stream_->Close(); | 89 stream_->Close(); |
| 90 stream_ = NULL; | 90 stream_ = NULL; |
| 91 } | 91 } |
| 92 return true; | 92 return true; |
| 93 } | 93 } |
| 94 | 94 |
| 95 void AudioRendererImpl::OnStop() { | 95 void ReferenceAudioRenderer::OnStop() { |
| 96 if (stream_) | 96 if (stream_) |
| 97 stream_->Stop(); | 97 stream_->Stop(); |
| 98 } | 98 } |
| 99 | 99 |
| 100 } // namespace media | 100 } // namespace media |
| OLD | NEW |