| Index: content/renderer/audio_device.cc
|
| ===================================================================
|
| --- content/renderer/audio_device.cc (revision 79311)
|
| +++ content/renderer/audio_device.cc (working copy)
|
| @@ -1,12 +1,14 @@
|
| -// Copyright (c) 2010 The Chromium Authors. All rights reserved.
|
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| #include "content/renderer/audio_device.h"
|
|
|
| +#include "base/message_loop.h"
|
| #include "base/singleton.h"
|
| #include "chrome/renderer/render_thread.h"
|
| #include "content/common/audio_messages.h"
|
| +#include "content/common/child_process.h"
|
| #include "content/common/view_messages.h"
|
| #include "media/audio/audio_util.h"
|
|
|
| @@ -41,7 +43,7 @@
|
| scoped_refptr<AudioMessageFilter> filter_;
|
| };
|
|
|
| -}
|
| +} // namespace
|
|
|
| AudioDevice::AudioDevice(size_t buffer_size,
|
| int channels,
|
| @@ -49,17 +51,24 @@
|
| RenderCallback* callback)
|
| : buffer_size_(buffer_size),
|
| channels_(channels),
|
| + bits_per_sample_(16),
|
| sample_rate_(sample_rate),
|
| callback_(callback),
|
| + audio_delay_milliseconds_(0),
|
| + volume_(1.0),
|
| stream_id_(0) {
|
| audio_data_.reserve(channels);
|
| for (int i = 0; i < channels; ++i) {
|
| float* channel_data = new float[buffer_size];
|
| audio_data_.push_back(channel_data);
|
| }
|
| + // Lazily create the message filter and share across AudioDevice instances.
|
| + filter_ = AudioMessageFilterCreator::SharedFilter();
|
| }
|
|
|
| AudioDevice::~AudioDevice() {
|
| + // Make sure we have been shut down.
|
| + DCHECK_EQ(0, stream_id_);
|
| Stop();
|
| for (int i = 0; i < channels_; ++i)
|
| delete [] audio_data_[i];
|
| @@ -71,53 +80,102 @@
|
| if (stream_id_)
|
| return false;
|
|
|
| - // Lazily create the message filter and share across AudioDevice instances.
|
| - filter_ = AudioMessageFilterCreator::SharedFilter();
|
| -
|
| - stream_id_ = filter_->AddDelegate(this);
|
| -
|
| AudioParameters params;
|
| params.format = AudioParameters::AUDIO_PCM_LOW_LATENCY;
|
| params.channels = channels_;
|
| params.sample_rate = static_cast<int>(sample_rate_);
|
| - params.bits_per_sample = 16;
|
| + params.bits_per_sample = bits_per_sample_;
|
| params.samples_per_packet = buffer_size_;
|
|
|
| - filter_->Send(new AudioHostMsg_CreateStream(0, stream_id_, params, true));
|
| + // Ensure that the initialization task is posted on the I/O thread by
|
| + // accessing the I/O message loop directly. This approach avoids a race
|
| + // condition which could exist if the message loop of the filter was
|
| + // used instead.
|
| + DCHECK(ChildProcess::current()) << "Must be in the renderer";
|
| + MessageLoop* message_loop = ChildProcess::current()->io_message_loop();
|
| + if (!message_loop)
|
| + return false;
|
|
|
| + message_loop->PostTask(FROM_HERE,
|
| + NewRunnableMethod(this, &AudioDevice::InitializeOnIOThread, params));
|
| +
|
| return true;
|
| }
|
|
|
| bool AudioDevice::Stop() {
|
| - if (stream_id_) {
|
| - OnDestroy();
|
| - return true;
|
| + if (!stream_id_)
|
| + return false;
|
| +
|
| + filter_->message_loop()->PostTask(FROM_HERE,
|
| + NewRunnableMethod(this, &AudioDevice::ShutDownOnIOThread));
|
| +
|
| + if (audio_thread_.get()) {
|
| + socket_->Close();
|
| + audio_thread_->Join();
|
| }
|
| - return false;
|
| +
|
| + return true;
|
| }
|
|
|
| -void AudioDevice::OnDestroy() {
|
| - // Make sure we don't call destroy more than once.
|
| - DCHECK_NE(0, stream_id_);
|
| +bool AudioDevice::SetVolume(double volume) {
|
| if (!stream_id_)
|
| + return false;
|
| +
|
| + if (volume < 0 || volume > 1.0)
|
| + return false;
|
| +
|
| + filter_->message_loop()->PostTask(FROM_HERE,
|
| + NewRunnableMethod(this, &AudioDevice::SetVolumeOnIOThread, volume));
|
| +
|
| + volume_ = volume;
|
| +
|
| + return true;
|
| +}
|
| +
|
| +bool AudioDevice::GetVolume(double* volume) {
|
| + if (!stream_id_)
|
| + return false;
|
| +
|
| + // Return a locally cached version of the current scaling factor.
|
| + *volume = volume_;
|
| +
|
| + return true;
|
| +}
|
| +
|
| +void AudioDevice::InitializeOnIOThread(const AudioParameters& params) {
|
| + stream_id_ = filter_->AddDelegate(this);
|
| + filter_->Send(new AudioHostMsg_CreateStream(0, stream_id_, params, true));
|
| +}
|
| +
|
| +void AudioDevice::StartOnIOThread() {
|
| + if (stream_id_)
|
| + filter_->Send(new AudioHostMsg_PlayStream(0, stream_id_));
|
| +}
|
| +
|
| +void AudioDevice::ShutDownOnIOThread() {
|
| + // Make sure we don't call shutdown more than once.
|
| + if (!stream_id_)
|
| return;
|
|
|
| + filter_->Send(new AudioHostMsg_CloseStream(0, stream_id_));
|
| filter_->RemoveDelegate(stream_id_);
|
| - filter_->Send(new AudioHostMsg_CloseStream(0, stream_id_));
|
| stream_id_ = 0;
|
| - if (audio_thread_.get()) {
|
| - socket_->Close();
|
| - audio_thread_->Join();
|
| - }
|
| }
|
|
|
| +void AudioDevice::SetVolumeOnIOThread(double volume) {
|
| + if (stream_id_)
|
| + filter_->Send(new AudioHostMsg_SetVolume(0, stream_id_, volume));
|
| +}
|
| +
|
| void AudioDevice::OnRequestPacket(AudioBuffersState buffers_state) {
|
| // This method does not apply to the low-latency system.
|
| NOTIMPLEMENTED();
|
| }
|
|
|
| void AudioDevice::OnStateChanged(AudioStreamState state) {
|
| - // Not needed in this simple implementation.
|
| + if (state == kAudioStreamError) {
|
| + DLOG(WARNING) << "AudioDevice::OnStateChanged(kError)";
|
| + }
|
| NOTIMPLEMENTED();
|
| }
|
|
|
| @@ -131,7 +189,6 @@
|
| base::SharedMemoryHandle handle,
|
| base::SyncSocket::Handle socket_handle,
|
| uint32 length) {
|
| -
|
| #if defined(OS_WIN)
|
| DCHECK(handle);
|
| DCHECK(socket_handle);
|
| @@ -140,7 +197,6 @@
|
| DCHECK_GE(socket_handle, 0);
|
| #endif
|
| DCHECK(length);
|
| - DCHECK(!audio_thread_.get());
|
|
|
| // TODO(crogers) : check that length is big enough for buffer_size_
|
|
|
| @@ -156,28 +212,39 @@
|
| new base::DelegateSimpleThread(this, "renderer_audio_thread"));
|
| audio_thread_->Start();
|
|
|
| - filter_->Send(new AudioHostMsg_PlayStream(0, stream_id_));
|
| + if (filter_) {
|
| + filter_->message_loop()->PostTask(FROM_HERE,
|
| + NewRunnableMethod(this, &AudioDevice::StartOnIOThread));
|
| + }
|
| }
|
|
|
| void AudioDevice::OnVolume(double volume) {
|
| - // Not needed in this simple implementation.
|
| NOTIMPLEMENTED();
|
| }
|
|
|
| // Our audio thread runs here.
|
| void AudioDevice::Run() {
|
| int pending_data;
|
| + 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) {
|
| + {
|
| + // Convert the number of pending bytes in the render buffer
|
| + // into milliseconds.
|
| + audio_delay_milliseconds_ = pending_data / bytes_per_ms;
|
| + }
|
| +
|
| FireRenderCallback();
|
| }
|
| }
|
|
|
| void AudioDevice::FireRenderCallback() {
|
| if (callback_) {
|
| - // Ask client to render audio.
|
| - callback_->Render(audio_data_, buffer_size_);
|
| + // Update the audio-delay measurement then ask client to render audio.
|
| + callback_->Render(audio_data_, buffer_size_, audio_delay_milliseconds_);
|
|
|
| // Interleave, scale, and clip to int16.
|
| int16* output_buffer16 = static_cast<int16*>(shared_memory_data());
|
|
|