Chromium Code Reviews| Index: content/renderer/media/audio_device.cc |
| =================================================================== |
| --- content/renderer/media/audio_device.cc (revision 107169) |
| +++ content/renderer/media/audio_device.cc (working copy) |
| @@ -14,6 +14,19 @@ |
| #include "content/renderer/render_thread_impl.h" |
| #include "media/audio/audio_util.h" |
| +AudioDevice::AudioDevice(RenderCallback* callback) |
| + : buffer_size_(0), |
|
scherkus (not reviewing)
2011/11/09 02:39:05
indent by two more
Chris Rogers
2011/11/10 02:17:22
Done.
|
| + channels_(0), |
| + bits_per_sample_(16), |
| + sample_rate_(0), |
| + latency_format_(AudioParameters::AUDIO_PCM_LOW_LATENCY), |
| + callback_(callback), |
| + audio_delay_milliseconds_(0), |
| + volume_(1.0), |
| + stream_id_(0) { |
| + filter_ = RenderThreadImpl::current()->audio_message_filter(); |
| +} |
| + |
| AudioDevice::AudioDevice(size_t buffer_size, |
| int channels, |
| double sample_rate, |
| @@ -27,6 +40,29 @@ |
| volume_(1.0), |
| stream_id_(0) { |
| filter_ = RenderThreadImpl::current()->audio_message_filter(); |
| + Initialize(buffer_size, |
| + channels, |
| + sample_rate, |
| + AudioParameters::AUDIO_PCM_LOW_LATENCY); |
| +} |
| + |
| +void AudioDevice::Initialize(size_t buffer_size, |
| + int channels, |
| + double sample_rate, |
| + AudioParameters::Format latency_format) { |
| + CHECK_EQ(0, stream_id_); |
|
scherkus (not reviewing)
2011/11/09 02:39:05
nit: perhaps add logging?
CHECK_EQ(0, stream_id) <
Chris Rogers
2011/11/10 02:17:22
Done.
|
| + if (stream_id_) |
| + return; |
| + |
| + buffer_size_ = buffer_size; |
| + channels_ = channels; |
| + sample_rate_ = sample_rate; |
| + latency_format_ = latency_format; |
| + |
| + // Cleanup from any previous initialization. |
| + for (size_t i = 0; i < audio_data_.size(); ++i) |
| + delete [] audio_data_[i]; |
|
acolwell GONE FROM CHROMIUM
2011/11/08 21:44:20
How about changing audio_data_ to use scoped_array
Chris Rogers
2011/11/10 02:17:22
It seems that scoped_array<> cannot be used with S
|
| + |
| audio_data_.reserve(channels); |
| for (int i = 0; i < channels; ++i) { |
| float* channel_data = new float[buffer_size]; |
| @@ -34,6 +70,10 @@ |
| } |
| } |
| +bool AudioDevice::IsInitialized() { |
| + return audio_data_.size() > 0; |
| +} |
| + |
| AudioDevice::~AudioDevice() { |
| // The current design requires that the user calls Stop() before deleting |
| // this class. |
| @@ -43,8 +83,11 @@ |
| } |
| void AudioDevice::Start() { |
| + if (stream_id_) |
|
acolwell GONE FROM CHROMIUM
2011/11/08 21:44:20
Should this be a CHECK_EQ() like elsewhere? We wan
henrika (OOO until Aug 14)
2011/11/09 17:05:22
Why is the existing check in InitializeOnIOThread(
Chris Rogers
2011/11/10 02:17:22
I believe Henrik is right here. This check is not
|
| + return; |
| + |
| AudioParameters params; |
| - params.format = AudioParameters::AUDIO_PCM_LOW_LATENCY; |
| + params.format = latency_format_; |
| params.channels = channels_; |
| params.sample_rate = static_cast<int>(sample_rate_); |
| params.bits_per_sample = bits_per_sample_; |
| @@ -56,6 +99,9 @@ |
| } |
| bool AudioDevice::Stop() { |
| + if (!stream_id_) |
|
acolwell GONE FROM CHROMIUM
2011/11/08 21:44:20
CHECK_NE(0, stream_id_) ?
henrika (OOO until Aug 14)
2011/11/09 17:05:22
Similar comment as for Start().
Chris Rogers
2011/11/10 02:17:22
Yes the check that's already in ShutDownOnIOThread
|
| + return true; |
| + |
| // Max waiting time for Stop() to complete. If this time limit is passed, |
| // we will stop waiting and return false. It ensures that Stop() can't block |
| // the calling thread forever. |
| @@ -84,6 +130,18 @@ |
| return true; |
| } |
| +void AudioDevice::Play() { |
| + ChildProcess::current()->io_message_loop()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&AudioDevice::PlayOnIOThread, this)); |
| +} |
| + |
| +void AudioDevice::Pause(bool flush) { |
|
henrika (OOO until Aug 14)
2011/11/09 17:05:22
What happens if a user calls Start(), Pause(), Pla
Chris Rogers
2011/11/10 02:17:22
Yes, I think we can make it even simpler and consi
henrika (OOO until Aug 14)
2011/11/10 11:45:07
Fine by me. In general I prefer simple and readabl
Chris Rogers
2011/11/15 22:48:29
I think I've addressed this problem with |play_on_
|
| + ChildProcess::current()->io_message_loop()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&AudioDevice::PauseOnIOThread, this, flush)); |
| +} |
| + |
| bool AudioDevice::SetVolume(double volume) { |
| if (volume < 0 || volume > 1.0) |
| return false; |
| @@ -103,7 +161,7 @@ |
| } |
| void AudioDevice::InitializeOnIOThread(const AudioParameters& params) { |
| - // Make sure we don't call Start() more than once. |
| + // Make sure we don't create the stream more than once. |
| DCHECK_EQ(0, stream_id_); |
| if (stream_id_) |
| return; |
| @@ -112,11 +170,19 @@ |
| Send(new AudioHostMsg_CreateStream(stream_id_, params, true)); |
| } |
| -void AudioDevice::StartOnIOThread() { |
| +void AudioDevice::PlayOnIOThread() { |
| if (stream_id_) |
| Send(new AudioHostMsg_PlayStream(stream_id_)); |
| } |
| +void AudioDevice::PauseOnIOThread(bool flush) { |
| + if (stream_id_) { |
| + Send(new AudioHostMsg_PauseStream(stream_id_)); |
| + if (flush) |
| + Send(new AudioHostMsg_FlushStream(stream_id_)); |
| + } |
| +} |
| + |
| void AudioDevice::ShutDownOnIOThread(base::WaitableEvent* completion) { |
| // Make sure we don't call shutdown more than once. |
| if (!stream_id_) { |
| @@ -138,20 +204,17 @@ |
| void AudioDevice::OnRequestPacket(AudioBuffersState buffers_state) { |
| // This method does not apply to the low-latency system. |
| - NOTIMPLEMENTED(); |
|
acolwell GONE FROM CHROMIUM
2011/11/08 21:44:20
Why? This and the 2 below.
Chris Rogers
2011/11/10 02:17:22
These are unrelated changes - just part of general
|
| } |
| void AudioDevice::OnStateChanged(AudioStreamState state) { |
| if (state == kAudioStreamError) { |
| DLOG(WARNING) << "AudioDevice::OnStateChanged(kError)"; |
| } |
| - NOTIMPLEMENTED(); |
| } |
| void AudioDevice::OnCreated( |
| base::SharedMemoryHandle handle, uint32 length) { |
| // Not needed in this simple implementation. |
| - NOTIMPLEMENTED(); |
| } |
| void AudioDevice::OnLowLatencyCreated( |
| @@ -191,7 +254,7 @@ |
| MessageLoop::current()->PostTask( |
| FROM_HERE, |
| - base::Bind(&AudioDevice::StartOnIOThread, this)); |
| + base::Bind(&AudioDevice::PlayOnIOThread, this)); |
| } |
| void AudioDevice::OnVolume(double volume) { |
| @@ -210,10 +273,8 @@ |
| const int samples_per_ms = static_cast<int>(sample_rate_) / 1000; |
| const int bytes_per_ms = channels_ * (bits_per_sample_ / 8) * samples_per_ms; |
| - while ((sizeof(pending_data) == socket_->Receive(&pending_data, |
| - sizeof(pending_data))) && |
| - (pending_data >= 0)) { |
|
acolwell GONE FROM CHROMIUM
2011/11/08 21:44:20
Why is it safe to remove the >= 0 check?
Chris Rogers
2011/11/10 02:17:22
We need to remove this check, because this case co
acolwell GONE FROM CHROMIUM
2011/11/10 21:58:15
Won't allow pending_data < 0 break the audio_delay
Chris Rogers
2011/11/15 22:48:29
Yes, good point -- fixed.
On 2011/11/10 21:58:15,
|
| - |
| + while (sizeof(pending_data) == |
| + socket_->Receive(&pending_data, sizeof(pending_data))) { |
| // Convert the number of pending bytes in the render buffer |
| // into milliseconds. |
| audio_delay_milliseconds_ = pending_data / bytes_per_ms; |