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" |
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/pepper/pepper_plugin_delegate_impl.h" | 13 #include "content/renderer/pepper/pepper_plugin_delegate_impl.h" |
14 #include "content/renderer/render_thread_impl.h" | 14 #include "content/renderer/render_thread_impl.h" |
15 #include "media/audio/audio_manager_base.h" | 15 #include "media/audio/audio_manager_base.h" |
16 | 16 |
17 namespace content { | 17 namespace content { |
18 | 18 |
19 PepperPlatformAudioInputImpl::PepperPlatformAudioInputImpl() | |
20 : client_(NULL), | |
21 stream_id_(0), | |
22 main_message_loop_proxy_(base::MessageLoopProxy::current()), | |
23 shutdown_called_(false) { | |
24 filter_ = RenderThreadImpl::current()->audio_input_message_filter(); | |
25 } | |
26 | |
27 PepperPlatformAudioInputImpl::~PepperPlatformAudioInputImpl() { | |
28 // Make sure we have been shut down. Warning: this may happen on the I/O | |
29 // thread! | |
30 // Although these members should be accessed on a specific thread (either the | |
31 // main thread or the I/O thread), it should be fine to examine their value | |
32 // here. | |
33 DCHECK_EQ(0, stream_id_); | |
34 DCHECK(!client_); | |
35 DCHECK(label_.empty()); | |
36 DCHECK(shutdown_called_); | |
37 } | |
38 | |
39 // static | 19 // static |
40 PepperPlatformAudioInputImpl* PepperPlatformAudioInputImpl::Create( | 20 PepperPlatformAudioInputImpl* PepperPlatformAudioInputImpl::Create( |
41 const base::WeakPtr<PepperPluginDelegateImpl>& plugin_delegate, | 21 const base::WeakPtr<PepperPluginDelegateImpl>& plugin_delegate, |
42 const std::string& device_id, | 22 const std::string& device_id, |
43 int sample_rate, | 23 int sample_rate, |
44 int frames_per_buffer, | 24 int frames_per_buffer, |
45 webkit::ppapi::PluginDelegate::PlatformAudioInputClient* client) { | 25 webkit::ppapi::PluginDelegate::PlatformAudioInputClient* client) { |
46 scoped_refptr<PepperPlatformAudioInputImpl> audio_input( | 26 scoped_refptr<PepperPlatformAudioInputImpl> audio_input( |
47 new PepperPlatformAudioInputImpl); | 27 new PepperPlatformAudioInputImpl); |
48 if (audio_input->Initialize(plugin_delegate, device_id, sample_rate, | 28 if (audio_input->Initialize(plugin_delegate, device_id, sample_rate, |
(...skipping 25 matching lines...) Expand all Loading... |
74 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); | 54 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); |
75 | 55 |
76 // 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 |
77 // 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. |
78 client_ = NULL; | 58 client_ = NULL; |
79 ChildProcess::current()->io_message_loop()->PostTask( | 59 ChildProcess::current()->io_message_loop()->PostTask( |
80 FROM_HERE, | 60 FROM_HERE, |
81 base::Bind(&PepperPlatformAudioInputImpl::ShutDownOnIOThread, this)); | 61 base::Bind(&PepperPlatformAudioInputImpl::ShutDownOnIOThread, this)); |
82 } | 62 } |
83 | 63 |
| 64 void PepperPlatformAudioInputImpl::OnStreamCreated( |
| 65 base::SharedMemoryHandle handle, |
| 66 base::SyncSocket::Handle socket_handle, |
| 67 uint32 length) { |
| 68 #if defined(OS_WIN) |
| 69 DCHECK(handle); |
| 70 DCHECK(socket_handle); |
| 71 #else |
| 72 DCHECK_NE(-1, handle.fd); |
| 73 DCHECK_NE(-1, socket_handle); |
| 74 #endif |
| 75 DCHECK(length); |
| 76 |
| 77 if (base::MessageLoopProxy::current() != main_message_loop_proxy_) { |
| 78 // No need to check |shutdown_called_| here. If shutdown has occurred, |
| 79 // |client_| will be NULL and the handles will be cleaned up on the main |
| 80 // thread. |
| 81 main_message_loop_proxy_->PostTask( |
| 82 FROM_HERE, |
| 83 base::Bind(&PepperPlatformAudioInputImpl::OnStreamCreated, this, |
| 84 handle, socket_handle, length)); |
| 85 } else { |
| 86 // Must dereference the client only on the main thread. Shutdown may have |
| 87 // occurred while the request was in-flight, so we need to NULL check. |
| 88 if (client_) { |
| 89 client_->StreamCreated(handle, length, socket_handle); |
| 90 } else { |
| 91 // Clean up the handles. |
| 92 base::SyncSocket temp_socket(socket_handle); |
| 93 base::SharedMemory temp_shared_memory(handle, false); |
| 94 } |
| 95 } |
| 96 } |
| 97 |
| 98 void PepperPlatformAudioInputImpl::OnVolume(double volume) {} |
| 99 |
| 100 void PepperPlatformAudioInputImpl::OnStateChanged(AudioStreamState state) {} |
| 101 |
| 102 void PepperPlatformAudioInputImpl::OnDeviceReady(const std::string& device_id) { |
| 103 DCHECK(ChildProcess::current()->io_message_loop_proxy()-> |
| 104 BelongsToCurrentThread()); |
| 105 |
| 106 if (shutdown_called_) |
| 107 return; |
| 108 |
| 109 if (device_id.empty()) { |
| 110 main_message_loop_proxy_->PostTask( |
| 111 FROM_HERE, |
| 112 base::Bind(&PepperPlatformAudioInputImpl::NotifyStreamCreationFailed, |
| 113 this)); |
| 114 } else { |
| 115 // We will be notified by OnStreamCreated(). |
| 116 filter_->Send(new AudioInputHostMsg_CreateStream(stream_id_, params_, |
| 117 device_id, false)); |
| 118 } |
| 119 } |
| 120 |
| 121 PepperPlatformAudioInputImpl::~PepperPlatformAudioInputImpl() { |
| 122 // Make sure we have been shut down. Warning: this may happen on the I/O |
| 123 // thread! |
| 124 // 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 |
| 126 // here. |
| 127 DCHECK_EQ(0, stream_id_); |
| 128 DCHECK(!client_); |
| 129 DCHECK(label_.empty()); |
| 130 DCHECK(shutdown_called_); |
| 131 } |
| 132 |
| 133 PepperPlatformAudioInputImpl::PepperPlatformAudioInputImpl() |
| 134 : client_(NULL), |
| 135 stream_id_(0), |
| 136 main_message_loop_proxy_(base::MessageLoopProxy::current()), |
| 137 shutdown_called_(false) { |
| 138 filter_ = RenderThreadImpl::current()->audio_input_message_filter(); |
| 139 } |
| 140 |
84 bool PepperPlatformAudioInputImpl::Initialize( | 141 bool PepperPlatformAudioInputImpl::Initialize( |
85 const base::WeakPtr<PepperPluginDelegateImpl>& plugin_delegate, | 142 const base::WeakPtr<PepperPluginDelegateImpl>& plugin_delegate, |
86 const std::string& device_id, | 143 const std::string& device_id, |
87 int sample_rate, | 144 int sample_rate, |
88 int frames_per_buffer, | 145 int frames_per_buffer, |
89 webkit::ppapi::PluginDelegate::PlatformAudioInputClient* client) { | 146 webkit::ppapi::PluginDelegate::PlatformAudioInputClient* client) { |
90 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); | 147 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); |
91 | 148 |
92 if (!plugin_delegate || !client) | 149 if (!plugin_delegate || !client) |
93 return false; | 150 return false; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 } | 227 } |
171 | 228 |
172 main_message_loop_proxy_->PostTask( | 229 main_message_loop_proxy_->PostTask( |
173 FROM_HERE, | 230 FROM_HERE, |
174 base::Bind(&PepperPlatformAudioInputImpl::CloseDevice, this)); | 231 base::Bind(&PepperPlatformAudioInputImpl::CloseDevice, this)); |
175 | 232 |
176 Release(); // Release for the delegate, balances out the reference taken in | 233 Release(); // Release for the delegate, balances out the reference taken in |
177 // PepperPluginDelegateImpl::CreateAudioInput. | 234 // PepperPluginDelegateImpl::CreateAudioInput. |
178 } | 235 } |
179 | 236 |
180 void PepperPlatformAudioInputImpl::OnStreamCreated( | |
181 base::SharedMemoryHandle handle, | |
182 base::SyncSocket::Handle socket_handle, | |
183 uint32 length) { | |
184 #if defined(OS_WIN) | |
185 DCHECK(handle); | |
186 DCHECK(socket_handle); | |
187 #else | |
188 DCHECK_NE(-1, handle.fd); | |
189 DCHECK_NE(-1, socket_handle); | |
190 #endif | |
191 DCHECK(length); | |
192 | |
193 if (base::MessageLoopProxy::current() != main_message_loop_proxy_) { | |
194 // No need to check |shutdown_called_| here. If shutdown has occurred, | |
195 // |client_| will be NULL and the handles will be cleaned up on the main | |
196 // thread. | |
197 main_message_loop_proxy_->PostTask( | |
198 FROM_HERE, | |
199 base::Bind(&PepperPlatformAudioInputImpl::OnStreamCreated, this, | |
200 handle, socket_handle, length)); | |
201 } else { | |
202 // Must dereference the client only on the main thread. Shutdown may have | |
203 // occurred while the request was in-flight, so we need to NULL check. | |
204 if (client_) { | |
205 client_->StreamCreated(handle, length, socket_handle); | |
206 } else { | |
207 // Clean up the handles. | |
208 base::SyncSocket temp_socket(socket_handle); | |
209 base::SharedMemory temp_shared_memory(handle, false); | |
210 } | |
211 } | |
212 } | |
213 | |
214 void PepperPlatformAudioInputImpl::OnVolume(double volume) { | |
215 } | |
216 | |
217 void PepperPlatformAudioInputImpl::OnStateChanged(AudioStreamState state) { | |
218 } | |
219 | |
220 void PepperPlatformAudioInputImpl::OnDeviceReady(const std::string& device_id) { | |
221 DCHECK(ChildProcess::current()->io_message_loop_proxy()-> | |
222 BelongsToCurrentThread()); | |
223 | |
224 if (shutdown_called_) | |
225 return; | |
226 | |
227 if (device_id.empty()) { | |
228 main_message_loop_proxy_->PostTask( | |
229 FROM_HERE, | |
230 base::Bind(&PepperPlatformAudioInputImpl::NotifyStreamCreationFailed, | |
231 this)); | |
232 } else { | |
233 // We will be notified by OnStreamCreated(). | |
234 filter_->Send(new AudioInputHostMsg_CreateStream(stream_id_, params_, | |
235 device_id, false)); | |
236 } | |
237 } | |
238 | |
239 void PepperPlatformAudioInputImpl::OnDeviceOpened(int request_id, | 237 void PepperPlatformAudioInputImpl::OnDeviceOpened(int request_id, |
240 bool succeeded, | 238 bool succeeded, |
241 const std::string& label) { | 239 const std::string& label) { |
242 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); | 240 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); |
243 | 241 |
244 if (succeeded && plugin_delegate_) { | 242 if (succeeded && plugin_delegate_) { |
245 DCHECK(!label.empty()); | 243 DCHECK(!label.empty()); |
246 label_ = label; | 244 label_ = label; |
247 | 245 |
248 if (client_) { | 246 if (client_) { |
(...skipping 22 matching lines...) Expand all Loading... |
271 } | 269 } |
272 | 270 |
273 void PepperPlatformAudioInputImpl::NotifyStreamCreationFailed() { | 271 void PepperPlatformAudioInputImpl::NotifyStreamCreationFailed() { |
274 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); | 272 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); |
275 | 273 |
276 if (client_) | 274 if (client_) |
277 client_->StreamCreationFailed(); | 275 client_->StreamCreationFailed(); |
278 } | 276 } |
279 | 277 |
280 } // namespace content | 278 } // namespace content |
OLD | NEW |