OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ppapi/proxy/ppb_audio_input_proxy.h" | |
6 | |
7 #include "base/compiler_specific.h" | |
8 #include "media/audio/shared_memory_util.h" | |
9 #include "ppapi/c/dev/ppb_audio_input_dev.h" | |
10 #include "ppapi/c/pp_errors.h" | |
11 #include "ppapi/c/ppb_audio_config.h" | |
12 #include "ppapi/proxy/enter_proxy.h" | |
13 #include "ppapi/proxy/plugin_dispatcher.h" | |
14 #include "ppapi/proxy/ppapi_messages.h" | |
15 #include "ppapi/shared_impl/api_id.h" | |
16 #include "ppapi/shared_impl/platform_file.h" | |
17 #include "ppapi/shared_impl/ppapi_globals.h" | |
18 #include "ppapi/shared_impl/ppb_audio_input_shared.h" | |
19 #include "ppapi/shared_impl/ppb_device_ref_shared.h" | |
20 #include "ppapi/shared_impl/tracked_callback.h" | |
21 #include "ppapi/thunk/enter.h" | |
22 #include "ppapi/thunk/resource_creation_api.h" | |
23 #include "ppapi/thunk/thunk.h" | |
24 | |
25 using ppapi::IntToPlatformFile; | |
26 using ppapi::thunk::PPB_AudioInput_API; | |
27 | |
28 namespace ppapi { | |
29 namespace proxy { | |
30 | |
31 class AudioInput : public PPB_AudioInput_Shared { | |
32 public: | |
33 explicit AudioInput(const HostResource& audio_input); | |
34 virtual ~AudioInput(); | |
35 | |
36 // Implementation of PPB_AudioInput_API trusted methods. | |
37 virtual int32_t OpenTrusted( | |
38 const std::string& device_id, | |
39 PP_Resource config, | |
40 scoped_refptr<TrackedCallback> create_callback) OVERRIDE; | |
41 virtual int32_t GetSyncSocket(int* sync_socket) OVERRIDE; | |
42 virtual int32_t GetSharedMemory(int* shm_handle, uint32_t* shm_size) OVERRIDE; | |
43 virtual const std::vector<DeviceRefData>& GetDeviceRefData() const OVERRIDE; | |
44 | |
45 private: | |
46 // PPB_AudioInput_Shared implementation. | |
47 virtual int32_t InternalEnumerateDevices( | |
48 PP_Resource* devices, | |
49 scoped_refptr<TrackedCallback> callback) OVERRIDE; | |
50 virtual int32_t InternalOpen( | |
51 const std::string& device_id, | |
52 PP_AudioSampleRate sample_rate, | |
53 uint32_t sample_frame_count, | |
54 scoped_refptr<TrackedCallback> callback) OVERRIDE; | |
55 virtual PP_Bool InternalStartCapture() OVERRIDE; | |
56 virtual PP_Bool InternalStopCapture() OVERRIDE; | |
57 virtual void InternalClose() OVERRIDE; | |
58 | |
59 PluginDispatcher* GetDispatcher() const { | |
60 return PluginDispatcher::GetForResource(this); | |
61 } | |
62 | |
63 DISALLOW_COPY_AND_ASSIGN(AudioInput); | |
64 }; | |
65 | |
66 AudioInput::AudioInput(const HostResource& audio_input) | |
67 : PPB_AudioInput_Shared(audio_input) { | |
68 } | |
69 | |
70 AudioInput::~AudioInput() { | |
71 Close(); | |
72 } | |
73 | |
74 int32_t AudioInput::OpenTrusted( | |
75 const std::string& device_id, | |
76 PP_Resource config, | |
77 scoped_refptr<TrackedCallback> create_callback) { | |
78 return PP_ERROR_NOTSUPPORTED; // Don't proxy the trusted interface. | |
79 } | |
80 | |
81 int32_t AudioInput::GetSyncSocket(int* sync_socket) { | |
82 return PP_ERROR_NOTSUPPORTED; // Don't proxy the trusted interface. | |
83 } | |
84 | |
85 int32_t AudioInput::GetSharedMemory(int* shm_handle, uint32_t* shm_size) { | |
86 return PP_ERROR_NOTSUPPORTED; // Don't proxy the trusted interface. | |
87 } | |
88 | |
89 const std::vector<DeviceRefData>& AudioInput::GetDeviceRefData() const { | |
90 // Don't proxy the trusted interface. | |
91 static std::vector<DeviceRefData> result; | |
92 return result; | |
93 } | |
94 | |
95 int32_t AudioInput::InternalEnumerateDevices( | |
96 PP_Resource* devices, | |
97 scoped_refptr<TrackedCallback> callback) { | |
98 devices_ = devices; | |
99 enumerate_devices_callback_ = callback; | |
100 GetDispatcher()->Send(new PpapiHostMsg_PPBAudioInput_EnumerateDevices( | |
101 API_ID_PPB_AUDIO_INPUT_DEV, host_resource())); | |
102 return PP_OK_COMPLETIONPENDING; | |
103 } | |
104 | |
105 int32_t AudioInput::InternalOpen(const std::string& device_id, | |
106 PP_AudioSampleRate sample_rate, | |
107 uint32_t sample_frame_count, | |
108 scoped_refptr<TrackedCallback> callback) { | |
109 open_callback_ = callback; | |
110 GetDispatcher()->Send(new PpapiHostMsg_PPBAudioInput_Open( | |
111 API_ID_PPB_AUDIO_INPUT_DEV, host_resource(), device_id, sample_rate, | |
112 sample_frame_count)); | |
113 return PP_OK_COMPLETIONPENDING; | |
114 } | |
115 | |
116 PP_Bool AudioInput::InternalStartCapture() { | |
117 SetStartCaptureState(); | |
118 GetDispatcher()->Send(new PpapiHostMsg_PPBAudioInput_StartOrStop( | |
119 API_ID_PPB_AUDIO_INPUT_DEV, host_resource(), true)); | |
120 return PP_TRUE; | |
121 } | |
122 | |
123 PP_Bool AudioInput::InternalStopCapture() { | |
124 GetDispatcher()->Send(new PpapiHostMsg_PPBAudioInput_StartOrStop( | |
125 API_ID_PPB_AUDIO_INPUT_DEV, host_resource(), false)); | |
126 SetStopCaptureState(); | |
127 return PP_TRUE; | |
128 } | |
129 | |
130 void AudioInput::InternalClose() { | |
131 GetDispatcher()->Send(new PpapiHostMsg_PPBAudioInput_Close( | |
132 API_ID_PPB_AUDIO_INPUT_DEV, host_resource())); | |
133 } | |
134 | |
135 PPB_AudioInput_Proxy::PPB_AudioInput_Proxy(Dispatcher* dispatcher) | |
136 : InterfaceProxy(dispatcher), | |
137 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
138 } | |
139 | |
140 PPB_AudioInput_Proxy::~PPB_AudioInput_Proxy() { | |
141 } | |
142 | |
143 // static | |
144 PP_Resource PPB_AudioInput_Proxy::CreateProxyResource0_1( | |
145 PP_Instance instance, | |
146 PP_Resource config, | |
147 PPB_AudioInput_Callback audio_input_callback, | |
148 void* user_data) { | |
149 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); | |
150 if (!dispatcher) | |
151 return 0; | |
152 | |
153 HostResource result; | |
154 dispatcher->Send(new PpapiHostMsg_PPBAudioInput_Create( | |
155 API_ID_PPB_AUDIO_INPUT_DEV, instance, &result)); | |
156 if (result.is_null()) | |
157 return 0; | |
158 | |
159 AudioInput* audio_input = new AudioInput(result); | |
160 int32_t open_result = audio_input->Open("", config, audio_input_callback, | |
161 user_data, AudioInput::MakeIgnoredCompletionCallback(audio_input)); | |
162 if (open_result != PP_OK && open_result != PP_OK_COMPLETIONPENDING) { | |
163 delete audio_input; | |
164 return 0; | |
165 } | |
166 return audio_input->GetReference(); | |
167 } | |
168 | |
169 // static | |
170 PP_Resource PPB_AudioInput_Proxy::CreateProxyResource( | |
171 PP_Instance instance) { | |
172 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); | |
173 if (!dispatcher) | |
174 return 0; | |
175 | |
176 HostResource result; | |
177 dispatcher->Send(new PpapiHostMsg_PPBAudioInput_Create( | |
178 API_ID_PPB_AUDIO_INPUT_DEV, instance, &result)); | |
179 if (result.is_null()) | |
180 return 0; | |
181 | |
182 return (new AudioInput(result))->GetReference(); | |
183 } | |
184 | |
185 bool PPB_AudioInput_Proxy::OnMessageReceived(const IPC::Message& msg) { | |
186 bool handled = true; | |
187 IPC_BEGIN_MESSAGE_MAP(PPB_AudioInput_Proxy, msg) | |
188 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBAudioInput_Create, OnMsgCreate) | |
189 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBAudioInput_EnumerateDevices, | |
190 OnMsgEnumerateDevices) | |
191 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBAudioInput_Open, OnMsgOpen) | |
192 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBAudioInput_StartOrStop, | |
193 OnMsgStartOrStop) | |
194 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBAudioInput_Close, OnMsgClose) | |
195 | |
196 IPC_MESSAGE_HANDLER(PpapiMsg_PPBAudioInput_EnumerateDevicesACK, | |
197 OnMsgEnumerateDevicesACK) | |
198 IPC_MESSAGE_HANDLER(PpapiMsg_PPBAudioInput_OpenACK, OnMsgOpenACK) | |
199 IPC_MESSAGE_UNHANDLED(handled = false) | |
200 IPC_END_MESSAGE_MAP() | |
201 // TODO(brettw) handle bad messages! | |
202 | |
203 return handled; | |
204 } | |
205 | |
206 void PPB_AudioInput_Proxy::OnMsgCreate(PP_Instance instance, | |
207 HostResource* result) { | |
208 thunk::EnterResourceCreation resource_creation(instance); | |
209 if (resource_creation.succeeded()) { | |
210 result->SetHostResource( | |
211 instance, resource_creation.functions()->CreateAudioInput(instance)); | |
212 } | |
213 } | |
214 | |
215 void PPB_AudioInput_Proxy::OnMsgEnumerateDevices( | |
216 const ppapi::HostResource& audio_input) { | |
217 EnterHostFromHostResourceForceCallback<PPB_AudioInput_API> enter( | |
218 audio_input, callback_factory_, | |
219 &PPB_AudioInput_Proxy::EnumerateDevicesACKInHost, audio_input); | |
220 | |
221 if (enter.succeeded()) | |
222 enter.SetResult(enter.object()->EnumerateDevices(NULL, enter.callback())); | |
223 } | |
224 | |
225 void PPB_AudioInput_Proxy::OnMsgOpen(const ppapi::HostResource& audio_input, | |
226 const std::string& device_id, | |
227 int32_t sample_rate, | |
228 uint32_t sample_frame_count) { | |
229 // The ...ForceCallback class will help ensure the callback is always called. | |
230 // All error cases must call SetResult on this class. | |
231 EnterHostFromHostResourceForceCallback<PPB_AudioInput_API> enter( | |
232 audio_input, callback_factory_, &PPB_AudioInput_Proxy::OpenACKInHost, | |
233 audio_input); | |
234 if (enter.failed()) | |
235 return; // When enter fails, it will internally schedule the callback. | |
236 | |
237 thunk::EnterResourceCreation resource_creation(audio_input.instance()); | |
238 // Make an audio config object. | |
239 PP_Resource audio_config_res = | |
240 resource_creation.functions()->CreateAudioConfig( | |
241 audio_input.instance(), static_cast<PP_AudioSampleRate>(sample_rate), | |
242 sample_frame_count); | |
243 if (!audio_config_res) { | |
244 enter.SetResult(PP_ERROR_FAILED); | |
245 return; | |
246 } | |
247 | |
248 // Initiate opening the audio object. | |
249 enter.SetResult(enter.object()->OpenTrusted( | |
250 device_id, audio_config_res, enter.callback())); | |
251 | |
252 // Clean up the temporary audio config resource we made. | |
253 const PPB_Core* core = static_cast<const PPB_Core*>( | |
254 dispatcher()->local_get_interface()(PPB_CORE_INTERFACE)); | |
255 core->ReleaseResource(audio_config_res); | |
256 } | |
257 | |
258 void PPB_AudioInput_Proxy::OnMsgStartOrStop(const HostResource& audio_input, | |
259 bool capture) { | |
260 EnterHostFromHostResource<PPB_AudioInput_API> enter(audio_input); | |
261 if (enter.failed()) | |
262 return; | |
263 if (capture) | |
264 enter.object()->StartCapture(); | |
265 else | |
266 enter.object()->StopCapture(); | |
267 } | |
268 | |
269 void PPB_AudioInput_Proxy::OnMsgClose(const ppapi::HostResource& audio_input) { | |
270 EnterHostFromHostResource<PPB_AudioInput_API> enter(audio_input); | |
271 if (enter.succeeded()) | |
272 enter.object()->Close(); | |
273 } | |
274 | |
275 // Processed in the plugin (message from host). | |
276 void PPB_AudioInput_Proxy::OnMsgEnumerateDevicesACK( | |
277 const ppapi::HostResource& audio_input, | |
278 int32_t result, | |
279 const std::vector<ppapi::DeviceRefData>& devices) { | |
280 EnterPluginFromHostResource<PPB_AudioInput_API> enter(audio_input); | |
281 if (enter.succeeded()) { | |
282 static_cast<AudioInput*>(enter.object())->OnEnumerateDevicesComplete( | |
283 result, devices); | |
284 } | |
285 } | |
286 | |
287 void PPB_AudioInput_Proxy::OnMsgOpenACK( | |
288 const HostResource& audio_input, | |
289 int32_t result, | |
290 const ppapi::proxy::SerializedHandle& socket_handle, | |
291 const ppapi::proxy::SerializedHandle& handle) { | |
292 CHECK(socket_handle.is_socket()); | |
293 CHECK(handle.is_shmem()); | |
294 EnterPluginFromHostResource<PPB_AudioInput_API> enter(audio_input); | |
295 if (enter.failed()) { | |
296 // The caller may still have given us these handles in the failure case. | |
297 // The easiest way to clean these up is to just put them in the objects | |
298 // and then close them. This failure case is not performance critical. | |
299 base::SyncSocket temp_socket( | |
300 IPC::PlatformFileForTransitToPlatformFile( | |
301 socket_handle.descriptor())); | |
302 base::SharedMemory temp_mem(handle.shmem(), false); | |
303 } else { | |
304 // See the comment above about how we must call | |
305 // TotalSharedMemorySizeInBytes to get the actual size of the buffer. Here, | |
306 // we must call PacketSizeInBytes to get back the size of the audio buffer, | |
307 // excluding the bytes that audio uses for book-keeping. | |
308 static_cast<AudioInput*>(enter.object())->OnOpenComplete( | |
309 result, handle.shmem(), media::PacketSizeInBytes(handle.size()), | |
310 IPC::PlatformFileForTransitToPlatformFile(socket_handle.descriptor())); | |
311 } | |
312 } | |
313 | |
314 void PPB_AudioInput_Proxy::EnumerateDevicesACKInHost( | |
315 int32_t result, | |
316 const HostResource& audio_input) { | |
317 EnterHostFromHostResource<PPB_AudioInput_API> enter(audio_input); | |
318 dispatcher()->Send(new PpapiMsg_PPBAudioInput_EnumerateDevicesACK( | |
319 API_ID_PPB_AUDIO_INPUT_DEV, audio_input, result, | |
320 enter.succeeded() && result == PP_OK ? | |
321 enter.object()->GetDeviceRefData() : std::vector<DeviceRefData>())); | |
322 } | |
323 | |
324 void PPB_AudioInput_Proxy::OpenACKInHost(int32_t result, | |
325 const HostResource& audio_input) { | |
326 ppapi::proxy::SerializedHandle socket_handle( | |
327 ppapi::proxy::SerializedHandle::SOCKET); | |
328 ppapi::proxy::SerializedHandle shared_memory( | |
329 ppapi::proxy::SerializedHandle::SHARED_MEMORY); | |
330 | |
331 if (result == PP_OK) { | |
332 IPC::PlatformFileForTransit temp_socket; | |
333 base::SharedMemoryHandle temp_shmem; | |
334 uint32_t audio_buffer_size; | |
335 result = GetAudioInputConnectedHandles(audio_input, &temp_socket, | |
336 &temp_shmem, &audio_buffer_size); | |
337 if (result == PP_OK) { | |
338 socket_handle.set_socket(temp_socket); | |
339 // Note that we must call TotalSharedMemorySizeInBytes because | |
340 // Audio allocates extra space in shared memory for book-keeping, so the | |
341 // actual size of the shared memory buffer is larger than | |
342 // audio_buffer_length. When sending to NaCl, NaClIPCAdapter expects this | |
343 // size to match the size of the full shared memory buffer. | |
344 shared_memory.set_shmem( | |
345 temp_shmem, | |
346 media::TotalSharedMemorySizeInBytes(audio_buffer_size)); | |
347 } | |
348 } | |
349 | |
350 // Send all the values, even on error. This simplifies some of our cleanup | |
351 // code since the handles will be in the other process and could be | |
352 // inconvenient to clean up. Our IPC code will automatically handle this for | |
353 // us, as long as the remote side always closes the handles it receives | |
354 // (in OnMsgOpenACK), even in the failure case. | |
355 dispatcher()->Send(new PpapiMsg_PPBAudioInput_OpenACK( | |
356 API_ID_PPB_AUDIO_INPUT_DEV, audio_input, result, socket_handle, | |
357 shared_memory)); | |
358 } | |
359 | |
360 int32_t PPB_AudioInput_Proxy::GetAudioInputConnectedHandles( | |
361 const HostResource& resource, | |
362 IPC::PlatformFileForTransit* foreign_socket_handle, | |
363 base::SharedMemoryHandle* foreign_shared_memory_handle, | |
364 uint32_t* shared_memory_length) { | |
365 // Get the audio interface which will give us the handles. | |
366 EnterHostFromHostResource<PPB_AudioInput_API> enter(resource); | |
367 if (enter.failed()) | |
368 return PP_ERROR_NOINTERFACE; | |
369 | |
370 // Get the socket handle for signaling. | |
371 int32_t socket_handle; | |
372 int32_t result = enter.object()->GetSyncSocket(&socket_handle); | |
373 if (result != PP_OK) | |
374 return result; | |
375 | |
376 // socket_handle doesn't belong to us: don't close it. | |
377 *foreign_socket_handle = dispatcher()->ShareHandleWithRemote( | |
378 IntToPlatformFile(socket_handle), false); | |
379 if (*foreign_socket_handle == IPC::InvalidPlatformFileForTransit()) | |
380 return PP_ERROR_FAILED; | |
381 | |
382 // Get the shared memory for the buffer. | |
383 int shared_memory_handle; | |
384 result = enter.object()->GetSharedMemory(&shared_memory_handle, | |
385 shared_memory_length); | |
386 if (result != PP_OK) | |
387 return result; | |
388 | |
389 // shared_memory_handle doesn't belong to us: don't close it. | |
390 *foreign_shared_memory_handle = dispatcher()->ShareHandleWithRemote( | |
391 IntToPlatformFile(shared_memory_handle), false); | |
392 if (*foreign_shared_memory_handle == IPC::InvalidPlatformFileForTransit()) | |
393 return PP_ERROR_FAILED; | |
394 | |
395 return PP_OK; | |
396 } | |
397 | |
398 } // namespace proxy | |
399 } // namespace ppapi | |
OLD | NEW |