| 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.h" | 5 #include "content/renderer/pepper/pepper_platform_audio_input.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/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 // static | 23 // static |
| 24 PepperPlatformAudioInput* PepperPlatformAudioInput::Create( | 24 PepperPlatformAudioInput* PepperPlatformAudioInput::Create( |
| 25 const base::WeakPtr<RenderViewImpl>& render_view, | 25 const base::WeakPtr<RenderViewImpl>& render_view, |
| 26 const std::string& device_id, | 26 const std::string& device_id, |
| 27 const GURL& document_url, | 27 const GURL& document_url, |
| 28 int sample_rate, | 28 int sample_rate, |
| 29 int frames_per_buffer, | 29 int frames_per_buffer, |
| 30 PepperAudioInputHost* client) { | 30 PepperAudioInputHost* client) { |
| 31 scoped_refptr<PepperPlatformAudioInput> audio_input( | 31 scoped_refptr<PepperPlatformAudioInput> audio_input( |
| 32 new PepperPlatformAudioInput()); | 32 new PepperPlatformAudioInput()); |
| 33 if (audio_input->Initialize(render_view, device_id, document_url, | 33 if (audio_input->Initialize(render_view, |
| 34 sample_rate, frames_per_buffer, client)) { | 34 device_id, |
| 35 document_url, |
| 36 sample_rate, |
| 37 frames_per_buffer, |
| 38 client)) { |
| 35 // Balanced by Release invoked in | 39 // Balanced by Release invoked in |
| 36 // PepperPlatformAudioInput::ShutDownOnIOThread(). | 40 // PepperPlatformAudioInput::ShutDownOnIOThread(). |
| 37 audio_input->AddRef(); | 41 audio_input->AddRef(); |
| 38 return audio_input.get(); | 42 return audio_input.get(); |
| 39 } | 43 } |
| 40 return NULL; | 44 return NULL; |
| 41 } | 45 } |
| 42 | 46 |
| 43 void PepperPlatformAudioInput::StartCapture() { | 47 void PepperPlatformAudioInput::StartCapture() { |
| 44 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); | 48 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 DCHECK(length); | 90 DCHECK(length); |
| 87 // TODO(yzshen): Make use of circular buffer scheme. crbug.com/181449. | 91 // TODO(yzshen): Make use of circular buffer scheme. crbug.com/181449. |
| 88 DCHECK_EQ(1, total_segments); | 92 DCHECK_EQ(1, total_segments); |
| 89 | 93 |
| 90 if (base::MessageLoopProxy::current().get() != | 94 if (base::MessageLoopProxy::current().get() != |
| 91 main_message_loop_proxy_.get()) { | 95 main_message_loop_proxy_.get()) { |
| 92 // If shutdown has occurred, |client_| will be NULL and the handles will be | 96 // If shutdown has occurred, |client_| will be NULL and the handles will be |
| 93 // cleaned up on the main thread. | 97 // cleaned up on the main thread. |
| 94 main_message_loop_proxy_->PostTask( | 98 main_message_loop_proxy_->PostTask( |
| 95 FROM_HERE, | 99 FROM_HERE, |
| 96 base::Bind(&PepperPlatformAudioInput::OnStreamCreated, this, | 100 base::Bind(&PepperPlatformAudioInput::OnStreamCreated, |
| 97 handle, socket_handle, length, total_segments)); | 101 this, |
| 102 handle, |
| 103 socket_handle, |
| 104 length, |
| 105 total_segments)); |
| 98 } else { | 106 } else { |
| 99 // Must dereference the client only on the main thread. Shutdown may have | 107 // Must dereference the client only on the main thread. Shutdown may have |
| 100 // occurred while the request was in-flight, so we need to NULL check. | 108 // occurred while the request was in-flight, so we need to NULL check. |
| 101 if (client_) { | 109 if (client_) { |
| 102 client_->StreamCreated(handle, length, socket_handle); | 110 client_->StreamCreated(handle, length, socket_handle); |
| 103 } else { | 111 } else { |
| 104 // Clean up the handles. | 112 // Clean up the handles. |
| 105 base::SyncSocket temp_socket(socket_handle); | 113 base::SyncSocket temp_socket(socket_handle); |
| 106 base::SharedMemory temp_shared_memory(handle, false); | 114 base::SharedMemory temp_shared_memory(handle, false); |
| 107 } | 115 } |
| 108 } | 116 } |
| 109 } | 117 } |
| 110 | 118 |
| 111 void PepperPlatformAudioInput::OnVolume(double volume) {} | 119 void PepperPlatformAudioInput::OnVolume(double volume) {} |
| 112 | 120 |
| 113 void PepperPlatformAudioInput::OnStateChanged( | 121 void PepperPlatformAudioInput::OnStateChanged( |
| 114 media::AudioInputIPCDelegate::State state) { | 122 media::AudioInputIPCDelegate::State state) {} |
| 115 } | |
| 116 | 123 |
| 117 void PepperPlatformAudioInput::OnIPCClosed() { | 124 void PepperPlatformAudioInput::OnIPCClosed() { ipc_.reset(); } |
| 118 ipc_.reset(); | |
| 119 } | |
| 120 | 125 |
| 121 PepperPlatformAudioInput::~PepperPlatformAudioInput() { | 126 PepperPlatformAudioInput::~PepperPlatformAudioInput() { |
| 122 // Make sure we have been shut down. Warning: this may happen on the I/O | 127 // Make sure we have been shut down. Warning: this may happen on the I/O |
| 123 // thread! | 128 // thread! |
| 124 // Although these members should be accessed on a specific thread (either the | 129 // Although these members should be accessed on a specific thread (either the |
| 125 // main thread or the I/O thread), it should be fine to examine their value | 130 // main thread or the I/O thread), it should be fine to examine their value |
| 126 // here. | 131 // here. |
| 127 DCHECK(!ipc_); | 132 DCHECK(!ipc_); |
| 128 DCHECK(!client_); | 133 DCHECK(!client_); |
| 129 DCHECK(label_.empty()); | 134 DCHECK(label_.empty()); |
| 130 DCHECK(!pending_open_device_); | 135 DCHECK(!pending_open_device_); |
| 131 } | 136 } |
| 132 | 137 |
| 133 PepperPlatformAudioInput::PepperPlatformAudioInput() | 138 PepperPlatformAudioInput::PepperPlatformAudioInput() |
| 134 : client_(NULL), | 139 : client_(NULL), |
| 135 main_message_loop_proxy_(base::MessageLoopProxy::current()), | 140 main_message_loop_proxy_(base::MessageLoopProxy::current()), |
| 136 io_message_loop_proxy_(ChildProcess::current()->io_message_loop_proxy()), | 141 io_message_loop_proxy_(ChildProcess::current()->io_message_loop_proxy()), |
| 137 create_stream_sent_(false), | 142 create_stream_sent_(false), |
| 138 pending_open_device_(false), | 143 pending_open_device_(false), |
| 139 pending_open_device_id_(-1) { | 144 pending_open_device_id_(-1) {} |
| 140 } | |
| 141 | 145 |
| 142 bool PepperPlatformAudioInput::Initialize( | 146 bool PepperPlatformAudioInput::Initialize( |
| 143 const base::WeakPtr<RenderViewImpl>& render_view, | 147 const base::WeakPtr<RenderViewImpl>& render_view, |
| 144 const std::string& device_id, | 148 const std::string& device_id, |
| 145 const GURL& document_url, | 149 const GURL& document_url, |
| 146 int sample_rate, | 150 int sample_rate, |
| 147 int frames_per_buffer, | 151 int frames_per_buffer, |
| 148 PepperAudioInputHost* client) { | 152 PepperAudioInputHost* client) { |
| 149 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); | 153 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); |
| 150 | 154 |
| 151 if (!render_view.get() || !client) | 155 if (!render_view.get() || !client) |
| 152 return false; | 156 return false; |
| 153 | 157 |
| 154 ipc_ = RenderThreadImpl::current()->audio_input_message_filter()-> | 158 ipc_ = RenderThreadImpl::current() |
| 155 CreateAudioInputIPC(render_view->GetRoutingID()); | 159 ->audio_input_message_filter() |
| 160 ->CreateAudioInputIPC(render_view->GetRoutingID()); |
| 156 | 161 |
| 157 render_view_ = render_view; | 162 render_view_ = render_view; |
| 158 client_ = client; | 163 client_ = client; |
| 159 | 164 |
| 160 params_.Reset(media::AudioParameters::AUDIO_PCM_LINEAR, | 165 params_.Reset(media::AudioParameters::AUDIO_PCM_LINEAR, |
| 161 media::CHANNEL_LAYOUT_MONO, ppapi::kAudioInputChannels, 0, | 166 media::CHANNEL_LAYOUT_MONO, |
| 162 sample_rate, ppapi::kBitsPerAudioInputSample, | 167 ppapi::kAudioInputChannels, |
| 168 0, |
| 169 sample_rate, |
| 170 ppapi::kBitsPerAudioInputSample, |
| 163 frames_per_buffer); | 171 frames_per_buffer); |
| 164 | 172 |
| 165 // We need to open the device and obtain the label and session ID before | 173 // We need to open the device and obtain the label and session ID before |
| 166 // initializing. | 174 // initializing. |
| 167 pending_open_device_id_ = GetMediaDeviceManager()->OpenDevice( | 175 pending_open_device_id_ = GetMediaDeviceManager()->OpenDevice( |
| 168 PP_DEVICETYPE_DEV_AUDIOCAPTURE, | 176 PP_DEVICETYPE_DEV_AUDIOCAPTURE, |
| 169 device_id.empty() ? media::AudioManagerBase::kDefaultDeviceId : device_id, | 177 device_id.empty() ? media::AudioManagerBase::kDefaultDeviceId : device_id, |
| 170 document_url, | 178 document_url, |
| 171 base::Bind(&PepperPlatformAudioInput::OnDeviceOpened, this)); | 179 base::Bind(&PepperPlatformAudioInput::OnDeviceOpened, this)); |
| 172 pending_open_device_ = true; | 180 pending_open_device_ = true; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 201 } | 209 } |
| 202 ipc_.reset(); | 210 ipc_.reset(); |
| 203 } | 211 } |
| 204 | 212 |
| 205 void PepperPlatformAudioInput::ShutDownOnIOThread() { | 213 void PepperPlatformAudioInput::ShutDownOnIOThread() { |
| 206 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 214 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 207 | 215 |
| 208 StopCaptureOnIOThread(); | 216 StopCaptureOnIOThread(); |
| 209 | 217 |
| 210 main_message_loop_proxy_->PostTask( | 218 main_message_loop_proxy_->PostTask( |
| 211 FROM_HERE, | 219 FROM_HERE, base::Bind(&PepperPlatformAudioInput::CloseDevice, this)); |
| 212 base::Bind(&PepperPlatformAudioInput::CloseDevice, this)); | |
| 213 | 220 |
| 214 Release(); // Release for the delegate, balances out the reference taken in | 221 Release(); // Release for the delegate, balances out the reference taken in |
| 215 // PepperPlatformAudioInput::Create. | 222 // PepperPlatformAudioInput::Create. |
| 216 } | 223 } |
| 217 | 224 |
| 218 void PepperPlatformAudioInput::OnDeviceOpened(int request_id, | 225 void PepperPlatformAudioInput::OnDeviceOpened(int request_id, |
| 219 bool succeeded, | 226 bool succeeded, |
| 220 const std::string& label) { | 227 const std::string& label) { |
| 221 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); | 228 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); |
| 222 | 229 |
| 223 pending_open_device_ = false; | 230 pending_open_device_ = false; |
| 224 pending_open_device_id_ = -1; | 231 pending_open_device_id_ = -1; |
| 225 | 232 |
| 226 if (succeeded && render_view_.get()) { | 233 if (succeeded && render_view_.get()) { |
| 227 DCHECK(!label.empty()); | 234 DCHECK(!label.empty()); |
| 228 label_ = label; | 235 label_ = label; |
| 229 | 236 |
| 230 if (client_) { | 237 if (client_) { |
| 231 int session_id = GetMediaDeviceManager()->GetSessionID( | 238 int session_id = GetMediaDeviceManager()->GetSessionID( |
| 232 PP_DEVICETYPE_DEV_AUDIOCAPTURE, label); | 239 PP_DEVICETYPE_DEV_AUDIOCAPTURE, label); |
| 233 io_message_loop_proxy_->PostTask( | 240 io_message_loop_proxy_->PostTask( |
| 234 FROM_HERE, | 241 FROM_HERE, |
| 235 base::Bind(&PepperPlatformAudioInput::InitializeOnIOThread, | 242 base::Bind(&PepperPlatformAudioInput::InitializeOnIOThread, |
| 236 this, session_id)); | 243 this, |
| 244 session_id)); |
| 237 } else { | 245 } else { |
| 238 // Shutdown has occurred. | 246 // Shutdown has occurred. |
| 239 CloseDevice(); | 247 CloseDevice(); |
| 240 } | 248 } |
| 241 } else { | 249 } else { |
| 242 NotifyStreamCreationFailed(); | 250 NotifyStreamCreationFailed(); |
| 243 } | 251 } |
| 244 } | 252 } |
| 245 | 253 |
| 246 void PepperPlatformAudioInput::CloseDevice() { | 254 void PepperPlatformAudioInput::CloseDevice() { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 264 | 272 |
| 265 if (client_) | 273 if (client_) |
| 266 client_->StreamCreationFailed(); | 274 client_->StreamCreationFailed(); |
| 267 } | 275 } |
| 268 | 276 |
| 269 PepperMediaDeviceManager* PepperPlatformAudioInput::GetMediaDeviceManager() { | 277 PepperMediaDeviceManager* PepperPlatformAudioInput::GetMediaDeviceManager() { |
| 270 return PepperMediaDeviceManager::GetForRenderView(render_view_.get()); | 278 return PepperMediaDeviceManager::GetForRenderView(render_view_.get()); |
| 271 } | 279 } |
| 272 | 280 |
| 273 } // namespace content | 281 } // namespace content |
| OLD | NEW |