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" |
11 #include "content/child/child_process.h" | 11 #include "content/child/child_process.h" |
12 #include "content/renderer/media/audio_input_message_filter.h" | 12 #include "content/renderer/media/audio_input_message_filter.h" |
13 #include "content/renderer/pepper/pepper_audio_input_host.h" | 13 #include "content/renderer/pepper/pepper_audio_input_host.h" |
14 #include "content/renderer/pepper/pepper_media_device_manager.h" | 14 #include "content/renderer/pepper/pepper_media_device_manager.h" |
| 15 #include "content/renderer/render_frame_impl.h" |
15 #include "content/renderer/render_thread_impl.h" | 16 #include "content/renderer/render_thread_impl.h" |
16 #include "content/renderer/render_view_impl.h" | 17 #include "content/renderer/render_view_impl.h" |
17 #include "media/audio/audio_manager_base.h" | 18 #include "media/audio/audio_manager_base.h" |
18 #include "ppapi/shared_impl/ppb_audio_config_shared.h" | 19 #include "ppapi/shared_impl/ppb_audio_config_shared.h" |
19 #include "url/gurl.h" | 20 #include "url/gurl.h" |
20 | 21 |
21 namespace content { | 22 namespace content { |
22 | 23 |
23 // static | 24 // static |
24 PepperPlatformAudioInput* PepperPlatformAudioInput::Create( | 25 PepperPlatformAudioInput* PepperPlatformAudioInput::Create( |
25 const base::WeakPtr<RenderViewImpl>& render_view, | 26 int render_frame_id, |
26 const std::string& device_id, | 27 const std::string& device_id, |
27 const GURL& document_url, | 28 const GURL& document_url, |
28 int sample_rate, | 29 int sample_rate, |
29 int frames_per_buffer, | 30 int frames_per_buffer, |
30 PepperAudioInputHost* client) { | 31 PepperAudioInputHost* client) { |
31 scoped_refptr<PepperPlatformAudioInput> audio_input( | 32 scoped_refptr<PepperPlatformAudioInput> audio_input( |
32 new PepperPlatformAudioInput()); | 33 new PepperPlatformAudioInput()); |
33 if (audio_input->Initialize(render_view, | 34 if (audio_input->Initialize(render_frame_id, |
34 device_id, | 35 device_id, |
35 document_url, | 36 document_url, |
36 sample_rate, | 37 sample_rate, |
37 frames_per_buffer, | 38 frames_per_buffer, |
38 client)) { | 39 client)) { |
39 // Balanced by Release invoked in | 40 // Balanced by Release invoked in |
40 // PepperPlatformAudioInput::ShutDownOnIOThread(). | 41 // PepperPlatformAudioInput::ShutDownOnIOThread(). |
41 audio_input->AddRef(); | 42 audio_input->AddRef(); |
42 return audio_input.get(); | 43 return audio_input.get(); |
43 } | 44 } |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 DCHECK(!ipc_); | 133 DCHECK(!ipc_); |
133 DCHECK(!client_); | 134 DCHECK(!client_); |
134 DCHECK(label_.empty()); | 135 DCHECK(label_.empty()); |
135 DCHECK(!pending_open_device_); | 136 DCHECK(!pending_open_device_); |
136 } | 137 } |
137 | 138 |
138 PepperPlatformAudioInput::PepperPlatformAudioInput() | 139 PepperPlatformAudioInput::PepperPlatformAudioInput() |
139 : client_(NULL), | 140 : client_(NULL), |
140 main_message_loop_proxy_(base::MessageLoopProxy::current()), | 141 main_message_loop_proxy_(base::MessageLoopProxy::current()), |
141 io_message_loop_proxy_(ChildProcess::current()->io_message_loop_proxy()), | 142 io_message_loop_proxy_(ChildProcess::current()->io_message_loop_proxy()), |
| 143 render_frame_id_(MSG_ROUTING_NONE), |
142 create_stream_sent_(false), | 144 create_stream_sent_(false), |
143 pending_open_device_(false), | 145 pending_open_device_(false), |
144 pending_open_device_id_(-1) {} | 146 pending_open_device_id_(-1) {} |
145 | 147 |
146 bool PepperPlatformAudioInput::Initialize( | 148 bool PepperPlatformAudioInput::Initialize( |
147 const base::WeakPtr<RenderViewImpl>& render_view, | 149 int render_frame_id, |
148 const std::string& device_id, | 150 const std::string& device_id, |
149 const GURL& document_url, | 151 const GURL& document_url, |
150 int sample_rate, | 152 int sample_rate, |
151 int frames_per_buffer, | 153 int frames_per_buffer, |
152 PepperAudioInputHost* client) { | 154 PepperAudioInputHost* client) { |
153 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); | 155 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); |
154 | 156 |
155 if (!render_view.get() || !client) | 157 RenderFrameImpl* const render_frame = |
| 158 RenderFrameImpl::FromRoutingID(render_frame_id); |
| 159 if (!render_frame || !client) |
| 160 return false; |
| 161 |
| 162 render_frame_id_ = render_frame_id; |
| 163 client_ = client; |
| 164 |
| 165 if (!GetMediaDeviceManager()) |
156 return false; | 166 return false; |
157 | 167 |
158 ipc_ = RenderThreadImpl::current() | 168 ipc_ = RenderThreadImpl::current() |
159 ->audio_input_message_filter() | 169 ->audio_input_message_filter() |
160 ->CreateAudioInputIPC(render_view->GetRoutingID()); | 170 ->CreateAudioInputIPC(render_frame->render_view()->GetRoutingID()); |
161 | |
162 render_view_ = render_view; | |
163 client_ = client; | |
164 | 171 |
165 params_.Reset(media::AudioParameters::AUDIO_PCM_LINEAR, | 172 params_.Reset(media::AudioParameters::AUDIO_PCM_LINEAR, |
166 media::CHANNEL_LAYOUT_MONO, | 173 media::CHANNEL_LAYOUT_MONO, |
167 ppapi::kAudioInputChannels, | 174 ppapi::kAudioInputChannels, |
168 0, | 175 0, |
169 sample_rate, | 176 sample_rate, |
170 ppapi::kBitsPerAudioInputSample, | 177 ppapi::kBitsPerAudioInputSample, |
171 frames_per_buffer); | 178 frames_per_buffer); |
172 | 179 |
173 // We need to open the device and obtain the label and session ID before | 180 // We need to open the device and obtain the label and session ID before |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 } | 230 } |
224 | 231 |
225 void PepperPlatformAudioInput::OnDeviceOpened(int request_id, | 232 void PepperPlatformAudioInput::OnDeviceOpened(int request_id, |
226 bool succeeded, | 233 bool succeeded, |
227 const std::string& label) { | 234 const std::string& label) { |
228 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); | 235 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); |
229 | 236 |
230 pending_open_device_ = false; | 237 pending_open_device_ = false; |
231 pending_open_device_id_ = -1; | 238 pending_open_device_id_ = -1; |
232 | 239 |
233 if (succeeded && render_view_.get()) { | 240 PepperMediaDeviceManager* const device_manager = GetMediaDeviceManager(); |
| 241 if (succeeded && device_manager) { |
234 DCHECK(!label.empty()); | 242 DCHECK(!label.empty()); |
235 label_ = label; | 243 label_ = label; |
236 | 244 |
237 if (client_) { | 245 if (client_) { |
238 int session_id = GetMediaDeviceManager()->GetSessionID( | 246 int session_id = device_manager->GetSessionID( |
239 PP_DEVICETYPE_DEV_AUDIOCAPTURE, label); | 247 PP_DEVICETYPE_DEV_AUDIOCAPTURE, label); |
240 io_message_loop_proxy_->PostTask( | 248 io_message_loop_proxy_->PostTask( |
241 FROM_HERE, | 249 FROM_HERE, |
242 base::Bind(&PepperPlatformAudioInput::InitializeOnIOThread, | 250 base::Bind(&PepperPlatformAudioInput::InitializeOnIOThread, |
243 this, | 251 this, |
244 session_id)); | 252 session_id)); |
245 } else { | 253 } else { |
246 // Shutdown has occurred. | 254 // Shutdown has occurred. |
247 CloseDevice(); | 255 CloseDevice(); |
248 } | 256 } |
249 } else { | 257 } else { |
250 NotifyStreamCreationFailed(); | 258 NotifyStreamCreationFailed(); |
251 } | 259 } |
252 } | 260 } |
253 | 261 |
254 void PepperPlatformAudioInput::CloseDevice() { | 262 void PepperPlatformAudioInput::CloseDevice() { |
255 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); | 263 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); |
256 | 264 |
257 if (render_view_.get()) { | 265 if (!label_.empty()) { |
258 if (!label_.empty()) { | 266 PepperMediaDeviceManager* const device_manager = GetMediaDeviceManager(); |
259 GetMediaDeviceManager()->CloseDevice(label_); | 267 if (device_manager) |
260 label_.clear(); | 268 device_manager->CloseDevice(label_); |
261 } | 269 label_.clear(); |
262 if (pending_open_device_) { | 270 } |
263 GetMediaDeviceManager()->CancelOpenDevice(pending_open_device_id_); | 271 if (pending_open_device_) { |
264 pending_open_device_ = false; | 272 PepperMediaDeviceManager* const device_manager = GetMediaDeviceManager(); |
265 pending_open_device_id_ = -1; | 273 if (device_manager) |
266 } | 274 device_manager->CancelOpenDevice(pending_open_device_id_); |
| 275 pending_open_device_ = false; |
| 276 pending_open_device_id_ = -1; |
267 } | 277 } |
268 } | 278 } |
269 | 279 |
270 void PepperPlatformAudioInput::NotifyStreamCreationFailed() { | 280 void PepperPlatformAudioInput::NotifyStreamCreationFailed() { |
271 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); | 281 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); |
272 | 282 |
273 if (client_) | 283 if (client_) |
274 client_->StreamCreationFailed(); | 284 client_->StreamCreationFailed(); |
275 } | 285 } |
276 | 286 |
277 PepperMediaDeviceManager* PepperPlatformAudioInput::GetMediaDeviceManager() { | 287 PepperMediaDeviceManager* PepperPlatformAudioInput::GetMediaDeviceManager() { |
278 return PepperMediaDeviceManager::GetForRenderView(render_view_.get()); | 288 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); |
| 289 |
| 290 RenderFrameImpl* const render_frame = |
| 291 RenderFrameImpl::FromRoutingID(render_frame_id_); |
| 292 return render_frame ? |
| 293 PepperMediaDeviceManager::GetForRenderFrame(render_frame) : NULL; |
279 } | 294 } |
280 | 295 |
281 } // namespace content | 296 } // namespace content |
OLD | NEW |