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" | |
13 #include "content/renderer/media/audio_input_message_filter.h" | 12 #include "content/renderer/media/audio_input_message_filter.h" |
14 #include "content/renderer/pepper/pepper_plugin_delegate_impl.h" | 13 #include "content/renderer/pepper/pepper_plugin_delegate_impl.h" |
15 #include "content/renderer/render_thread_impl.h" | 14 #include "content/renderer/render_thread_impl.h" |
16 #include "media/audio/audio_manager_base.h" | 15 #include "media/audio/audio_manager_base.h" |
17 | 16 |
18 namespace content { | 17 namespace content { |
19 | 18 |
20 // static | 19 // static |
21 PepperPlatformAudioInputImpl* PepperPlatformAudioInputImpl::Create( | 20 PepperPlatformAudioInputImpl* PepperPlatformAudioInputImpl::Create( |
22 const base::WeakPtr<PepperPluginDelegateImpl>& plugin_delegate, | 21 const base::WeakPtr<PepperPluginDelegateImpl>& plugin_delegate, |
(...skipping 25 matching lines...) Expand all Loading... |
48 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); | 47 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); |
49 | 48 |
50 ChildProcess::current()->io_message_loop()->PostTask( | 49 ChildProcess::current()->io_message_loop()->PostTask( |
51 FROM_HERE, | 50 FROM_HERE, |
52 base::Bind(&PepperPlatformAudioInputImpl::StopCaptureOnIOThread, this)); | 51 base::Bind(&PepperPlatformAudioInputImpl::StopCaptureOnIOThread, this)); |
53 } | 52 } |
54 | 53 |
55 void PepperPlatformAudioInputImpl::ShutDown() { | 54 void PepperPlatformAudioInputImpl::ShutDown() { |
56 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); | 55 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); |
57 | 56 |
| 57 // Make sure we don't call shutdown more than once. |
| 58 if (!client_) |
| 59 return; |
| 60 |
58 // Called on the main thread to stop all audio callbacks. We must only change | 61 // 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. | 62 // the client on the main thread, and the delegates from the I/O thread. |
60 client_ = NULL; | 63 client_ = NULL; |
61 ChildProcess::current()->io_message_loop()->PostTask( | 64 ChildProcess::current()->io_message_loop()->PostTask( |
62 FROM_HERE, | 65 FROM_HERE, |
63 base::Bind(&PepperPlatformAudioInputImpl::ShutDownOnIOThread, this)); | 66 base::Bind(&PepperPlatformAudioInputImpl::ShutDownOnIOThread, this)); |
64 } | 67 } |
65 | 68 |
66 void PepperPlatformAudioInputImpl::OnStreamCreated( | 69 void PepperPlatformAudioInputImpl::OnStreamCreated( |
67 base::SharedMemoryHandle handle, | 70 base::SharedMemoryHandle handle, |
68 base::SyncSocket::Handle socket_handle, | 71 base::SyncSocket::Handle socket_handle, |
69 int length) { | 72 int length) { |
70 #if defined(OS_WIN) | 73 #if defined(OS_WIN) |
71 DCHECK(handle); | 74 DCHECK(handle); |
72 DCHECK(socket_handle); | 75 DCHECK(socket_handle); |
73 #else | 76 #else |
74 DCHECK_NE(-1, handle.fd); | 77 DCHECK_NE(-1, handle.fd); |
75 DCHECK_NE(-1, socket_handle); | 78 DCHECK_NE(-1, socket_handle); |
76 #endif | 79 #endif |
77 DCHECK(length); | 80 DCHECK(length); |
78 | 81 |
79 if (base::MessageLoopProxy::current() != main_message_loop_proxy_) { | 82 if (base::MessageLoopProxy::current() != main_message_loop_proxy_) { |
80 // No need to check |shutdown_called_| here. If shutdown has occurred, | 83 // If shutdown has occurred, |client_| will be NULL and the handles will be |
81 // |client_| will be NULL and the handles will be cleaned up on the main | 84 // cleaned up on the main thread. |
82 // thread. | |
83 main_message_loop_proxy_->PostTask( | 85 main_message_loop_proxy_->PostTask( |
84 FROM_HERE, | 86 FROM_HERE, |
85 base::Bind(&PepperPlatformAudioInputImpl::OnStreamCreated, this, | 87 base::Bind(&PepperPlatformAudioInputImpl::OnStreamCreated, this, |
86 handle, socket_handle, length)); | 88 handle, socket_handle, length)); |
87 } else { | 89 } else { |
88 // Must dereference the client only on the main thread. Shutdown may have | 90 // 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. | 91 // occurred while the request was in-flight, so we need to NULL check. |
90 if (client_) { | 92 if (client_) { |
91 client_->StreamCreated(handle, length, socket_handle); | 93 client_->StreamCreated(handle, length, socket_handle); |
92 } else { | 94 } else { |
93 // Clean up the handles. | 95 // Clean up the handles. |
94 base::SyncSocket temp_socket(socket_handle); | 96 base::SyncSocket temp_socket(socket_handle); |
95 base::SharedMemory temp_shared_memory(handle, false); | 97 base::SharedMemory temp_shared_memory(handle, false); |
96 } | 98 } |
97 } | 99 } |
98 } | 100 } |
99 | 101 |
100 void PepperPlatformAudioInputImpl::OnVolume(double volume) {} | 102 void PepperPlatformAudioInputImpl::OnVolume(double volume) {} |
101 | 103 |
102 void PepperPlatformAudioInputImpl::OnStateChanged( | 104 void PepperPlatformAudioInputImpl::OnStateChanged( |
103 media::AudioInputIPCDelegate::State state) { | 105 media::AudioInputIPCDelegate::State state) { |
104 } | 106 } |
105 | 107 |
106 void PepperPlatformAudioInputImpl::OnDeviceReady(const std::string& device_id) { | 108 void PepperPlatformAudioInputImpl::OnDeviceReady(const std::string& device_id) { |
107 DCHECK(ChildProcess::current()->io_message_loop_proxy()-> | 109 DCHECK(ChildProcess::current()->io_message_loop_proxy()-> |
108 BelongsToCurrentThread()); | 110 BelongsToCurrentThread()); |
109 | 111 |
110 if (shutdown_called_) | 112 if (!ipc_) |
111 return; | 113 return; |
112 | 114 |
113 if (device_id.empty()) { | 115 if (device_id.empty()) { |
114 main_message_loop_proxy_->PostTask( | 116 main_message_loop_proxy_->PostTask( |
115 FROM_HERE, | 117 FROM_HERE, |
116 base::Bind(&PepperPlatformAudioInputImpl::NotifyStreamCreationFailed, | 118 base::Bind(&PepperPlatformAudioInputImpl::NotifyStreamCreationFailed, |
117 this)); | 119 this)); |
118 } else { | 120 } else { |
119 // We will be notified by OnStreamCreated(). | 121 // We will be notified by OnStreamCreated(). |
120 ipc_->CreateStream(stream_id_, params_, device_id, false); | 122 ipc_->CreateStream(this, params_, device_id, false); |
121 } | 123 } |
122 } | 124 } |
123 | 125 |
124 void PepperPlatformAudioInputImpl::OnIPCClosed() { | 126 void PepperPlatformAudioInputImpl::OnIPCClosed() { |
125 ipc_ = NULL; | 127 ipc_.reset(); |
126 } | 128 } |
127 | 129 |
128 PepperPlatformAudioInputImpl::~PepperPlatformAudioInputImpl() { | 130 PepperPlatformAudioInputImpl::~PepperPlatformAudioInputImpl() { |
129 // Make sure we have been shut down. Warning: this may happen on the I/O | 131 // Make sure we have been shut down. Warning: this may happen on the I/O |
130 // thread! | 132 // thread! |
131 // Although these members should be accessed on a specific thread (either the | 133 // 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 | 134 // main thread or the I/O thread), it should be fine to examine their value |
133 // here. | 135 // here. |
134 DCHECK_EQ(0, stream_id_); | 136 DCHECK(!ipc_); |
135 DCHECK(!client_); | 137 DCHECK(!client_); |
136 DCHECK(label_.empty()); | 138 DCHECK(label_.empty()); |
137 DCHECK(shutdown_called_); | |
138 } | 139 } |
139 | 140 |
140 PepperPlatformAudioInputImpl::PepperPlatformAudioInputImpl() | 141 PepperPlatformAudioInputImpl::PepperPlatformAudioInputImpl() |
141 : client_(NULL), | 142 : client_(NULL), |
142 stream_id_(0), | 143 main_message_loop_proxy_(base::MessageLoopProxy::current()) { |
143 render_view_id_(MSG_ROUTING_NONE), | |
144 main_message_loop_proxy_(base::MessageLoopProxy::current()), | |
145 shutdown_called_(false) { | |
146 ipc_ = RenderThreadImpl::current()->audio_input_message_filter(); | |
147 } | 144 } |
148 | 145 |
149 bool PepperPlatformAudioInputImpl::Initialize( | 146 bool PepperPlatformAudioInputImpl::Initialize( |
150 const base::WeakPtr<PepperPluginDelegateImpl>& plugin_delegate, | 147 const base::WeakPtr<PepperPluginDelegateImpl>& plugin_delegate, |
151 const std::string& device_id, | 148 const std::string& device_id, |
152 int sample_rate, | 149 int sample_rate, |
153 int frames_per_buffer, | 150 int frames_per_buffer, |
154 webkit::ppapi::PluginDelegate::PlatformAudioInputClient* client) { | 151 webkit::ppapi::PluginDelegate::PlatformAudioInputClient* client) { |
155 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); | 152 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); |
156 | 153 |
157 if (!plugin_delegate || !client) | 154 if (!plugin_delegate || !client) |
158 return false; | 155 return false; |
159 | 156 |
| 157 ipc_ = RenderThreadImpl::current()->audio_input_message_filter()-> |
| 158 CreateAudioInputIPC(plugin_delegate->GetRoutingID()); |
| 159 CHECK(ipc_); |
| 160 |
160 plugin_delegate_ = plugin_delegate; | 161 plugin_delegate_ = plugin_delegate; |
161 render_view_id_ = plugin_delegate_->GetRoutingID(); | |
162 client_ = client; | 162 client_ = client; |
163 | 163 |
164 params_.Reset(media::AudioParameters::AUDIO_PCM_LINEAR, | 164 params_.Reset(media::AudioParameters::AUDIO_PCM_LINEAR, |
165 media::CHANNEL_LAYOUT_MONO, 0, | 165 media::CHANNEL_LAYOUT_MONO, 0, |
166 sample_rate, 16, frames_per_buffer); | 166 sample_rate, 16, frames_per_buffer); |
167 | 167 |
168 if (device_id.empty()) { | 168 if (device_id.empty()) { |
169 // Use the default device. | 169 // Use the default device. |
170 ChildProcess::current()->io_message_loop()->PostTask( | 170 ChildProcess::current()->io_message_loop()->PostTask( |
171 FROM_HERE, | 171 FROM_HERE, |
172 base::Bind(&PepperPlatformAudioInputImpl::InitializeOnIOThread, | 172 base::Bind(&PepperPlatformAudioInputImpl::InitializeOnIOThread, |
173 this, 0)); | 173 this, 0)); |
174 } else { | 174 } else { |
175 // We need to open the device and obtain the label and session ID before | 175 // We need to open the device and obtain the label and session ID before |
176 // initializing. | 176 // initializing. |
177 plugin_delegate_->OpenDevice( | 177 plugin_delegate_->OpenDevice( |
178 PP_DEVICETYPE_DEV_AUDIOCAPTURE, device_id, | 178 PP_DEVICETYPE_DEV_AUDIOCAPTURE, device_id, |
179 base::Bind(&PepperPlatformAudioInputImpl::OnDeviceOpened, this)); | 179 base::Bind(&PepperPlatformAudioInputImpl::OnDeviceOpened, this)); |
180 } | 180 } |
181 return true; | 181 return true; |
182 } | 182 } |
183 | 183 |
184 void PepperPlatformAudioInputImpl::InitializeOnIOThread(int session_id) { | 184 void PepperPlatformAudioInputImpl::InitializeOnIOThread(int session_id) { |
185 DCHECK(ChildProcess::current()->io_message_loop_proxy()-> | 185 DCHECK(ChildProcess::current()->io_message_loop_proxy()-> |
186 BelongsToCurrentThread()); | 186 BelongsToCurrentThread()); |
187 | 187 |
188 if (shutdown_called_) | 188 if (!ipc_) |
189 return; | 189 return; |
190 | 190 |
191 // Make sure we don't call init more than once. | |
192 DCHECK_EQ(0, stream_id_); | |
193 stream_id_ = ipc_->AddDelegate(this); | |
194 DCHECK_NE(0, stream_id_); | |
195 | |
196 if (!session_id) { | 191 if (!session_id) { |
197 // We will be notified by OnStreamCreated(). | 192 // We will be notified by OnStreamCreated(). |
198 ipc_->CreateStream(stream_id_, params_, | 193 ipc_->CreateStream( |
199 media::AudioManagerBase::kDefaultDeviceId, false); | 194 this, params_, media::AudioManagerBase::kDefaultDeviceId, false); |
200 } else { | 195 } else { |
201 // We will be notified by OnDeviceReady(). | 196 // We will be notified by OnDeviceReady(). |
202 ipc_->StartDevice(stream_id_, session_id); | 197 ipc_->StartDevice(this, session_id); |
203 } | 198 } |
204 } | 199 } |
205 | 200 |
206 void PepperPlatformAudioInputImpl::StartCaptureOnIOThread() { | 201 void PepperPlatformAudioInputImpl::StartCaptureOnIOThread() { |
207 DCHECK(ChildProcess::current()->io_message_loop_proxy()-> | 202 DCHECK(ChildProcess::current()->io_message_loop_proxy()-> |
208 BelongsToCurrentThread()); | 203 BelongsToCurrentThread()); |
209 | 204 |
210 if (stream_id_) { | 205 if (ipc_) |
211 ipc_->AssociateStreamWithConsumer(stream_id_, render_view_id_); | 206 ipc_->RecordStream(); |
212 ipc_->RecordStream(stream_id_); | |
213 } | |
214 } | 207 } |
215 | 208 |
216 void PepperPlatformAudioInputImpl::StopCaptureOnIOThread() { | 209 void PepperPlatformAudioInputImpl::StopCaptureOnIOThread() { |
217 DCHECK(ChildProcess::current()->io_message_loop_proxy()-> | 210 DCHECK(ChildProcess::current()->io_message_loop_proxy()-> |
218 BelongsToCurrentThread()); | 211 BelongsToCurrentThread()); |
219 | 212 |
220 // TODO(yzshen): We cannot re-start capturing if the stream is closed. | 213 // TODO(yzshen): We cannot re-start capturing if the stream is closed. |
221 if (stream_id_) | 214 if (ipc_) { |
222 ipc_->CloseStream(stream_id_); | 215 ipc_->CloseStream(); |
| 216 ipc_.reset(); |
| 217 } |
223 } | 218 } |
224 | 219 |
225 void PepperPlatformAudioInputImpl::ShutDownOnIOThread() { | 220 void PepperPlatformAudioInputImpl::ShutDownOnIOThread() { |
226 DCHECK(ChildProcess::current()->io_message_loop_proxy()-> | 221 DCHECK(ChildProcess::current()->io_message_loop_proxy()-> |
227 BelongsToCurrentThread()); | 222 BelongsToCurrentThread()); |
228 | 223 |
229 // Make sure we don't call shutdown more than once. | 224 StopCaptureOnIOThread(); |
230 if (shutdown_called_) | |
231 return; | |
232 shutdown_called_ = true; | |
233 | |
234 if (stream_id_) { | |
235 ipc_->CloseStream(stream_id_); | |
236 ipc_->RemoveDelegate(stream_id_); | |
237 stream_id_ = 0; | |
238 } | |
239 | 225 |
240 main_message_loop_proxy_->PostTask( | 226 main_message_loop_proxy_->PostTask( |
241 FROM_HERE, | 227 FROM_HERE, |
242 base::Bind(&PepperPlatformAudioInputImpl::CloseDevice, this)); | 228 base::Bind(&PepperPlatformAudioInputImpl::CloseDevice, this)); |
243 | 229 |
244 Release(); // Release for the delegate, balances out the reference taken in | 230 Release(); // Release for the delegate, balances out the reference taken in |
245 // PepperPluginDelegateImpl::CreateAudioInput. | 231 // PepperPluginDelegateImpl::CreateAudioInput. |
246 } | 232 } |
247 | 233 |
248 void PepperPlatformAudioInputImpl::OnDeviceOpened(int request_id, | 234 void PepperPlatformAudioInputImpl::OnDeviceOpened(int request_id, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 } | 266 } |
281 | 267 |
282 void PepperPlatformAudioInputImpl::NotifyStreamCreationFailed() { | 268 void PepperPlatformAudioInputImpl::NotifyStreamCreationFailed() { |
283 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); | 269 DCHECK(main_message_loop_proxy_->BelongsToCurrentThread()); |
284 | 270 |
285 if (client_) | 271 if (client_) |
286 client_->StreamCreationFailed(); | 272 client_->StreamCreationFailed(); |
287 } | 273 } |
288 | 274 |
289 } // namespace content | 275 } // namespace content |
OLD | NEW |