| 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_input_impl.h" | 5 #include "content/renderer/pepper/pepper_platform_audio_input_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" |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 | 57 |
| 58 // Called on the main thread to stop all audio callbacks. We must only change | 58 // Called on the main thread to stop all audio callbacks. We must only change |
| 59 // the client on the main thread, and the delegates from the I/O thread. | 59 // the client on the main thread, and the delegates from the I/O thread. |
| 60 client_ = NULL; | 60 client_ = NULL; |
| 61 ChildProcess::current()->io_message_loop()->PostTask( | 61 ChildProcess::current()->io_message_loop()->PostTask( |
| 62 FROM_HERE, | 62 FROM_HERE, |
| 63 base::Bind(&PepperPlatformAudioInputImpl::ShutDownOnIOThread, this)); | 63 base::Bind(&PepperPlatformAudioInputImpl::ShutDownOnIOThread, this)); |
| 64 } | 64 } |
| 65 | 65 |
| 66 void PepperPlatformAudioInputImpl::OnStreamCreated( | 66 void PepperPlatformAudioInputImpl::OnStreamCreated( |
| 67 base::SharedMemoryHandle handle, | |
| 68 base::SyncSocket::Handle socket_handle, | 67 base::SyncSocket::Handle socket_handle, |
| 69 int length) { | 68 int total_handles) { |
| 70 #if defined(OS_WIN) | 69 DCHECK_NE(socket_handle, base::SyncSocket::kInvalidHandle); |
| 71 DCHECK(handle); | 70 DCHECK_EQ(total_handles, 1); |
| 72 DCHECK(socket_handle); | |
| 73 #else | |
| 74 DCHECK_NE(-1, handle.fd); | |
| 75 DCHECK_NE(-1, socket_handle); | |
| 76 #endif | |
| 77 DCHECK(length); | |
| 78 | 71 |
| 79 if (base::MessageLoopProxy::current() != main_message_loop_proxy_) { | 72 if (base::MessageLoopProxy::current() != main_message_loop_proxy_) { |
| 80 // No need to check |shutdown_called_| here. If shutdown has occurred, | 73 // No need to check |shutdown_called_| here. If shutdown has occurred, |
| 81 // |client_| will be NULL and the handles will be cleaned up on the main | 74 // |client_| will be NULL and the handles will be cleaned up on the main |
| 82 // thread. | 75 // thread. |
| 83 main_message_loop_proxy_->PostTask( | 76 main_message_loop_proxy_->PostTask( |
| 84 FROM_HERE, | 77 FROM_HERE, |
| 85 base::Bind(&PepperPlatformAudioInputImpl::OnStreamCreated, this, | 78 base::Bind(&PepperPlatformAudioInputImpl::OnStreamCreated, this, |
| 86 handle, socket_handle, length)); | 79 socket_handle, total_handles)); |
| 87 } else { | 80 } else { |
| 88 // Must dereference the client only on the main thread. Shutdown may have | 81 // Must dereference the client only on the main thread. Shutdown may have |
| 89 // occurred while the request was in-flight, so we need to NULL check. | 82 // occurred while the request was in-flight, so we need to NULL check. |
| 90 if (client_) { | 83 if (client_) { |
| 91 client_->StreamCreated(handle, length, socket_handle); | 84 socket_handle_ = socket_handle; |
| 92 } else { | 85 } else { |
| 93 // Clean up the handles. | 86 // Clean up the handles. |
| 94 base::SyncSocket temp_socket(socket_handle); | 87 base::SyncSocket temp_socket(socket_handle); |
| 88 } |
| 89 } |
| 90 } |
| 91 |
| 92 void PepperPlatformAudioInputImpl::OnSharedMemoryCreated( |
| 93 base::SharedMemoryHandle handle, |
| 94 int length, |
| 95 int index) { |
| 96 DCHECK(base::SharedMemory::IsHandleValid(handle)); |
| 97 DCHECK(length); |
| 98 DCHECK_NE(socket_handle_, base::SyncSocket::kInvalidHandle); |
| 99 |
| 100 if (base::MessageLoopProxy::current() != main_message_loop_proxy_) { |
| 101 // No need to check |shutdown_called_| here. If shutdown has occurred, |
| 102 // |client_| will be NULL and the handles will be cleaned up on the main |
| 103 // thread. |
| 104 main_message_loop_proxy_->PostTask( |
| 105 FROM_HERE, |
| 106 base::Bind(&PepperPlatformAudioInputImpl::OnSharedMemoryCreated, this, |
| 107 handle, length, index)); |
| 108 } else { |
| 109 // Must dereference the client only on the main thread. Shutdown may have |
| 110 // occurred while the request was in-flight, so we need to NULL check. |
| 111 if (client_) { |
| 112 client_->StreamCreated(handle, length, socket_handle_); |
| 113 } else { |
| 114 // Clean up the handles. |
| 95 base::SharedMemory temp_shared_memory(handle, false); | 115 base::SharedMemory temp_shared_memory(handle, false); |
| 96 } | 116 } |
| 97 } | 117 } |
| 98 } | 118 } |
| 99 | 119 |
| 100 void PepperPlatformAudioInputImpl::OnVolume(double volume) {} | 120 void PepperPlatformAudioInputImpl::OnVolume(double volume) {} |
| 101 | 121 |
| 102 void PepperPlatformAudioInputImpl::OnStateChanged( | 122 void PepperPlatformAudioInputImpl::OnStateChanged( |
| 103 media::AudioInputIPCDelegate::State state) { | 123 media::AudioInputIPCDelegate::State state) { |
| 104 } | 124 } |
| 105 | 125 |
| 106 void PepperPlatformAudioInputImpl::OnDeviceReady(const std::string& device_id) { | 126 void PepperPlatformAudioInputImpl::OnDeviceReady(const std::string& device_id) { |
| 107 DCHECK(ChildProcess::current()->io_message_loop_proxy()-> | 127 DCHECK(ChildProcess::current()->io_message_loop_proxy()-> |
| 108 BelongsToCurrentThread()); | 128 BelongsToCurrentThread()); |
| 109 | 129 |
| 110 if (shutdown_called_) | 130 if (shutdown_called_) |
| 111 return; | 131 return; |
| 112 | 132 |
| 113 if (device_id.empty()) { | 133 if (device_id.empty()) { |
| 114 main_message_loop_proxy_->PostTask( | 134 main_message_loop_proxy_->PostTask( |
| 115 FROM_HERE, | 135 FROM_HERE, |
| 116 base::Bind(&PepperPlatformAudioInputImpl::NotifyStreamCreationFailed, | 136 base::Bind(&PepperPlatformAudioInputImpl::NotifyStreamCreationFailed, |
| 117 this)); | 137 this)); |
| 118 } else { | 138 } else { |
| 119 // We will be notified by OnStreamCreated(). | 139 // We will be notified by OnStreamCreated(). |
| 120 ipc_->CreateStream(stream_id_, params_, device_id, false); | 140 ipc_->CreateStream(stream_id_, params_, device_id, false, 1); |
| 121 } | 141 } |
| 122 } | 142 } |
| 123 | 143 |
| 124 void PepperPlatformAudioInputImpl::OnIPCClosed() { | 144 void PepperPlatformAudioInputImpl::OnIPCClosed() { |
| 125 ipc_ = NULL; | 145 ipc_ = NULL; |
| 126 } | 146 } |
| 127 | 147 |
| 128 PepperPlatformAudioInputImpl::~PepperPlatformAudioInputImpl() { | 148 PepperPlatformAudioInputImpl::~PepperPlatformAudioInputImpl() { |
| 129 // Make sure we have been shut down. Warning: this may happen on the I/O | 149 // Make sure we have been shut down. Warning: this may happen on the I/O |
| 130 // thread! | 150 // thread! |
| 131 // Although these members should be accessed on a specific thread (either the | 151 // Although these members should be accessed on a specific thread (either the |
| 132 // main thread or the I/O thread), it should be fine to examine their value | 152 // main thread or the I/O thread), it should be fine to examine their value |
| 133 // here. | 153 // here. |
| 134 DCHECK_EQ(0, stream_id_); | 154 DCHECK_EQ(0, stream_id_); |
| 135 DCHECK(!client_); | 155 DCHECK(!client_); |
| 136 DCHECK(label_.empty()); | 156 DCHECK(label_.empty()); |
| 137 DCHECK(shutdown_called_); | 157 DCHECK(shutdown_called_); |
| 138 } | 158 } |
| 139 | 159 |
| 140 PepperPlatformAudioInputImpl::PepperPlatformAudioInputImpl() | 160 PepperPlatformAudioInputImpl::PepperPlatformAudioInputImpl() |
| 141 : client_(NULL), | 161 : client_(NULL), |
| 162 socket_handle_(base::SyncSocket::kInvalidHandle), |
| 142 stream_id_(0), | 163 stream_id_(0), |
| 143 render_view_id_(MSG_ROUTING_NONE), | 164 render_view_id_(MSG_ROUTING_NONE), |
| 144 main_message_loop_proxy_(base::MessageLoopProxy::current()), | 165 main_message_loop_proxy_(base::MessageLoopProxy::current()), |
| 145 shutdown_called_(false) { | 166 shutdown_called_(false) { |
| 146 ipc_ = RenderThreadImpl::current()->audio_input_message_filter(); | 167 ipc_ = RenderThreadImpl::current()->audio_input_message_filter(); |
| 147 } | 168 } |
| 148 | 169 |
| 149 bool PepperPlatformAudioInputImpl::Initialize( | 170 bool PepperPlatformAudioInputImpl::Initialize( |
| 150 const base::WeakPtr<PepperPluginDelegateImpl>& plugin_delegate, | 171 const base::WeakPtr<PepperPluginDelegateImpl>& plugin_delegate, |
| 151 const std::string& device_id, | 172 const std::string& device_id, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 return; | 210 return; |
| 190 | 211 |
| 191 // Make sure we don't call init more than once. | 212 // Make sure we don't call init more than once. |
| 192 DCHECK_EQ(0, stream_id_); | 213 DCHECK_EQ(0, stream_id_); |
| 193 stream_id_ = ipc_->AddDelegate(this); | 214 stream_id_ = ipc_->AddDelegate(this); |
| 194 DCHECK_NE(0, stream_id_); | 215 DCHECK_NE(0, stream_id_); |
| 195 | 216 |
| 196 if (!session_id) { | 217 if (!session_id) { |
| 197 // We will be notified by OnStreamCreated(). | 218 // We will be notified by OnStreamCreated(). |
| 198 ipc_->CreateStream(stream_id_, params_, | 219 ipc_->CreateStream(stream_id_, params_, |
| 199 media::AudioManagerBase::kDefaultDeviceId, false); | 220 media::AudioManagerBase::kDefaultDeviceId, false, 1); |
| 200 } else { | 221 } else { |
| 201 // We will be notified by OnDeviceReady(). | 222 // We will be notified by OnDeviceReady(). |
| 202 ipc_->StartDevice(stream_id_, session_id); | 223 ipc_->StartDevice(stream_id_, session_id); |
| 203 } | 224 } |
| 204 } | 225 } |
| 205 | 226 |
| 206 void PepperPlatformAudioInputImpl::StartCaptureOnIOThread() { | 227 void PepperPlatformAudioInputImpl::StartCaptureOnIOThread() { |
| 207 DCHECK(ChildProcess::current()->io_message_loop_proxy()-> | 228 DCHECK(ChildProcess::current()->io_message_loop_proxy()-> |
| 208 BelongsToCurrentThread()); | 229 BelongsToCurrentThread()); |
| 209 | 230 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 } | 301 } |
| 281 | 302 |
| 282 void PepperPlatformAudioInputImpl::NotifyStreamCreationFailed() { | 303 void PepperPlatformAudioInputImpl::NotifyStreamCreationFailed() { |
| 283 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); | 304 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); |
| 284 | 305 |
| 285 if (client_) | 306 if (client_) |
| 286 client_->StreamCreationFailed(); | 307 client_->StreamCreationFailed(); |
| 287 } | 308 } |
| 288 | 309 |
| 289 } // namespace content | 310 } // namespace content |
| OLD | NEW |