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