diff options
author | xians@chromium.org <xians@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-07 14:12:21 +0000 |
---|---|---|
committer | xians@chromium.org <xians@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-07 14:12:21 +0000 |
commit | 4ba14e1044e2e837a8c81894df156e4fd4be86bd (patch) | |
tree | fb8ec039504e26e4c3b8db67bf55ba88b138c89b /content/renderer/media/audio_device.cc | |
parent | fc5427c5ffa4590cb984472813e69a726dc78927 (diff) | |
download | chromium_src-4ba14e1044e2e837a8c81894df156e4fd4be86bd.zip chromium_src-4ba14e1044e2e837a8c81894df156e4fd4be86bd.tar.gz chromium_src-4ba14e1044e2e837a8c81894df156e4fd4be86bd.tar.bz2 |
There is a racing between SyncSocket::Receive in audio_thread_ and SyncSocket::Close in renderer thread.
This patch fixes it by using a waitable event to signal the audio thread that it should stop.
Test=content_unittests by running Valgrind
BUG=103711
Review URL: http://codereview.chromium.org/8659040
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@113386 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/renderer/media/audio_device.cc')
-rw-r--r-- | content/renderer/media/audio_device.cc | 51 |
1 files changed, 30 insertions, 21 deletions
diff --git a/content/renderer/media/audio_device.cc b/content/renderer/media/audio_device.cc index c3bef803..a1e3991 100644 --- a/content/renderer/media/audio_device.cc +++ b/content/renderer/media/audio_device.cc @@ -25,7 +25,8 @@ AudioDevice::AudioDevice(size_t buffer_size, callback_(callback), audio_delay_milliseconds_(0), volume_(1.0), - stream_id_(0) { + stream_id_(0), + memory_length_(0) { filter_ = RenderThreadImpl::current()->audio_message_filter(); audio_data_.reserve(channels); for (int i = 0; i < channels; ++i) { @@ -55,7 +56,8 @@ void AudioDevice::Start() { base::Bind(&AudioDevice::InitializeOnIOThread, this, params)); } -bool AudioDevice::Stop() { +void AudioDevice::Stop() { + DCHECK(MessageLoop::current() != ChildProcess::current()->io_message_loop()); // 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. @@ -70,18 +72,19 @@ bool AudioDevice::Stop() { // We wait here for the IO task to be completed to remove race conflicts // with OnLowLatencyCreated() and to ensure that Stop() acts as a synchronous // function call. - if (completion.TimedWait(kMaxTimeOut)) { - if (audio_thread_.get()) { - socket_->Close(); - audio_thread_->Join(); - audio_thread_.reset(NULL); - } - } else { + if (!completion.TimedWait(kMaxTimeOut)) { LOG(ERROR) << "Failed to shut down audio output on IO thread"; - return false; } - return true; + if (audio_thread_.get()) { + // Close the socket handler to terminate the main thread function in the + // audio thread. + { + base::SyncSocket socket(socket_handle_); + } + audio_thread_->Join(); + audio_thread_.reset(NULL); + } } bool AudioDevice::SetVolume(double volume) { @@ -167,6 +170,7 @@ void AudioDevice::OnLowLatencyCreated( DCHECK_GE(socket_handle, 0); #endif DCHECK(length); + DCHECK(!audio_thread_.get()); // Takes care of the case when Stop() is called before OnLowLatencyCreated(). if (!stream_id_) { @@ -176,14 +180,12 @@ void AudioDevice::OnLowLatencyCreated( return; } - shared_memory_.reset(new base::SharedMemory(handle, false)); - shared_memory_->Map(length); + shared_memory_handle_ = handle; + memory_length_ = length; DCHECK_GE(length, buffer_size_ * sizeof(int16) * channels_); - socket_.reset(new base::SyncSocket(socket_handle)); - // Allow the client to pre-populate the buffer. - FireRenderCallback(); + socket_handle_ = socket_handle; audio_thread_.reset( new base::DelegateSimpleThread(this, "renderer_audio_thread")); @@ -206,21 +208,28 @@ void AudioDevice::Send(IPC::Message* message) { void AudioDevice::Run() { audio_thread_->SetThreadPriority(base::kThreadPriority_RealtimeAudio); + base::SharedMemory shared_memory(shared_memory_handle_, false); + shared_memory.Map(memory_length_); + // Allow the client to pre-populate the buffer. + FireRenderCallback(reinterpret_cast<int16*>(shared_memory.memory())); + + base::SyncSocket socket(socket_handle_); + 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))) && + 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(); + FireRenderCallback(reinterpret_cast<int16*>(shared_memory.memory())); } } -void AudioDevice::FireRenderCallback() { +void AudioDevice::FireRenderCallback(int16* data) { TRACE_EVENT0("audio", "AudioDevice::FireRenderCallback"); if (callback_) { @@ -229,7 +238,7 @@ void AudioDevice::FireRenderCallback() { // Interleave, scale, and clip to int16. media::InterleaveFloatToInt16(audio_data_, - static_cast<int16*>(shared_memory_data()), + data, buffer_size_); } } |