| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/renderer/pepper/pepper_platform_audio_output_impl.h" | 5 #include "content/renderer/pepper/pepper_platform_audio_output_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop_proxy.h" | 9 #include "base/message_loop_proxy.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "content/common/child_process.h" | 11 #include "content/common/child_process.h" |
| 12 #include "content/common/media/audio_messages.h" | 12 #include "content/common/media/audio_messages.h" |
| 13 #include "content/renderer/media/audio_hardware.h" | 13 #include "content/renderer/media/audio_hardware.h" |
| 14 #include "content/renderer/render_thread_impl.h" | 14 #include "content/renderer/render_thread_impl.h" |
| 15 | 15 |
| 16 namespace content { | 16 namespace content { |
| 17 | 17 |
| 18 PepperPlatformAudioOutputImpl::PepperPlatformAudioOutputImpl() | |
| 19 : client_(NULL), | |
| 20 stream_id_(0), | |
| 21 main_message_loop_proxy_(base::MessageLoopProxy::current()) { | |
| 22 filter_ = RenderThreadImpl::current()->audio_message_filter(); | |
| 23 } | |
| 24 | |
| 25 PepperPlatformAudioOutputImpl::~PepperPlatformAudioOutputImpl() { | |
| 26 // Make sure we have been shut down. Warning: this will usually happen on | |
| 27 // the I/O thread! | |
| 28 DCHECK_EQ(0, stream_id_); | |
| 29 DCHECK(!client_); | |
| 30 } | |
| 31 | |
| 32 // static | 18 // static |
| 33 PepperPlatformAudioOutputImpl* PepperPlatformAudioOutputImpl::Create( | 19 PepperPlatformAudioOutputImpl* PepperPlatformAudioOutputImpl::Create( |
| 34 int sample_rate, | 20 int sample_rate, |
| 35 int frames_per_buffer, | 21 int frames_per_buffer, |
| 36 webkit::ppapi::PluginDelegate::PlatformAudioOutputClient* client) { | 22 webkit::ppapi::PluginDelegate::PlatformAudioOutputClient* client) { |
| 37 scoped_refptr<PepperPlatformAudioOutputImpl> audio_output( | 23 scoped_refptr<PepperPlatformAudioOutputImpl> audio_output( |
| 38 new PepperPlatformAudioOutputImpl); | 24 new PepperPlatformAudioOutputImpl); |
| 39 if (audio_output->Initialize(sample_rate, frames_per_buffer, client)) { | 25 if (audio_output->Initialize(sample_rate, frames_per_buffer, client)) { |
| 40 // Balanced by Release invoked in | 26 // Balanced by Release invoked in |
| 41 // PepperPlatformAudioOutputImpl::ShutDownOnIOThread(). | 27 // PepperPlatformAudioOutputImpl::ShutDownOnIOThread(). |
| (...skipping 26 matching lines...) Expand all Loading... |
| 68 | 54 |
| 69 void PepperPlatformAudioOutputImpl::ShutDown() { | 55 void PepperPlatformAudioOutputImpl::ShutDown() { |
| 70 // Called on the main thread to stop all audio callbacks. We must only change | 56 // Called on the main thread to stop all audio callbacks. We must only change |
| 71 // the client on the main thread, and the delegates from the I/O thread. | 57 // the client on the main thread, and the delegates from the I/O thread. |
| 72 client_ = NULL; | 58 client_ = NULL; |
| 73 ChildProcess::current()->io_message_loop()->PostTask( | 59 ChildProcess::current()->io_message_loop()->PostTask( |
| 74 FROM_HERE, | 60 FROM_HERE, |
| 75 base::Bind(&PepperPlatformAudioOutputImpl::ShutDownOnIOThread, this)); | 61 base::Bind(&PepperPlatformAudioOutputImpl::ShutDownOnIOThread, this)); |
| 76 } | 62 } |
| 77 | 63 |
| 64 void PepperPlatformAudioOutputImpl::OnStateChanged(AudioStreamState state) {} |
| 65 |
| 66 void PepperPlatformAudioOutputImpl::OnStreamCreated( |
| 67 base::SharedMemoryHandle handle, |
| 68 base::SyncSocket::Handle socket_handle, |
| 69 uint32 length) { |
| 70 #if defined(OS_WIN) |
| 71 DCHECK(handle); |
| 72 DCHECK(socket_handle); |
| 73 #else |
| 74 DCHECK_NE(-1, handle.fd); |
| 75 DCHECK_NE(-1, socket_handle); |
| 76 #endif |
| 77 DCHECK(length); |
| 78 |
| 79 if (base::MessageLoopProxy::current() == main_message_loop_proxy_) { |
| 80 // Must dereference the client only on the main thread. Shutdown may have |
| 81 // occurred while the request was in-flight, so we need to NULL check. |
| 82 if (client_) |
| 83 client_->StreamCreated(handle, length, socket_handle); |
| 84 } else { |
| 85 main_message_loop_proxy_->PostTask(FROM_HERE, |
| 86 base::Bind(&PepperPlatformAudioOutputImpl::OnStreamCreated, this, |
| 87 handle, socket_handle, length)); |
| 88 } |
| 89 } |
| 90 |
| 91 PepperPlatformAudioOutputImpl::~PepperPlatformAudioOutputImpl() { |
| 92 // Make sure we have been shut down. Warning: this will usually happen on |
| 93 // the I/O thread! |
| 94 DCHECK_EQ(0, stream_id_); |
| 95 DCHECK(!client_); |
| 96 } |
| 97 |
| 98 PepperPlatformAudioOutputImpl::PepperPlatformAudioOutputImpl() |
| 99 : client_(NULL), |
| 100 stream_id_(0), |
| 101 main_message_loop_proxy_(base::MessageLoopProxy::current()) { |
| 102 filter_ = RenderThreadImpl::current()->audio_message_filter(); |
| 103 } |
| 104 |
| 78 bool PepperPlatformAudioOutputImpl::Initialize( | 105 bool PepperPlatformAudioOutputImpl::Initialize( |
| 79 int sample_rate, | 106 int sample_rate, |
| 80 int frames_per_buffer, | 107 int frames_per_buffer, |
| 81 webkit::ppapi::PluginDelegate::PlatformAudioOutputClient* client) { | 108 webkit::ppapi::PluginDelegate::PlatformAudioOutputClient* client) { |
| 82 DCHECK(client); | 109 DCHECK(client); |
| 83 // Make sure we don't call init more than once. | 110 // Make sure we don't call init more than once. |
| 84 DCHECK_EQ(0, stream_id_); | 111 DCHECK_EQ(0, stream_id_); |
| 85 | 112 |
| 86 client_ = client; | 113 client_ = client; |
| 87 | 114 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 return; | 156 return; |
| 130 | 157 |
| 131 filter_->Send(new AudioHostMsg_CloseStream(stream_id_)); | 158 filter_->Send(new AudioHostMsg_CloseStream(stream_id_)); |
| 132 filter_->RemoveDelegate(stream_id_); | 159 filter_->RemoveDelegate(stream_id_); |
| 133 stream_id_ = 0; | 160 stream_id_ = 0; |
| 134 | 161 |
| 135 Release(); // Release for the delegate, balances out the reference taken in | 162 Release(); // Release for the delegate, balances out the reference taken in |
| 136 // PepperPluginDelegateImpl::CreateAudio. | 163 // PepperPluginDelegateImpl::CreateAudio. |
| 137 } | 164 } |
| 138 | 165 |
| 139 void PepperPlatformAudioOutputImpl::OnStateChanged(AudioStreamState state) { | |
| 140 } | |
| 141 | |
| 142 void PepperPlatformAudioOutputImpl::OnStreamCreated( | |
| 143 base::SharedMemoryHandle handle, | |
| 144 base::SyncSocket::Handle socket_handle, | |
| 145 uint32 length) { | |
| 146 #if defined(OS_WIN) | |
| 147 DCHECK(handle); | |
| 148 DCHECK(socket_handle); | |
| 149 #else | |
| 150 DCHECK_NE(-1, handle.fd); | |
| 151 DCHECK_NE(-1, socket_handle); | |
| 152 #endif | |
| 153 DCHECK(length); | |
| 154 | |
| 155 if (base::MessageLoopProxy::current() == main_message_loop_proxy_) { | |
| 156 // Must dereference the client only on the main thread. Shutdown may have | |
| 157 // occurred while the request was in-flight, so we need to NULL check. | |
| 158 if (client_) | |
| 159 client_->StreamCreated(handle, length, socket_handle); | |
| 160 } else { | |
| 161 main_message_loop_proxy_->PostTask(FROM_HERE, | |
| 162 base::Bind(&PepperPlatformAudioOutputImpl::OnStreamCreated, this, | |
| 163 handle, socket_handle, length)); | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 } // namespace content | 166 } // namespace content |
| OLD | NEW |