OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ppapi/proxy/ppb_audio_proxy.h" | 5 #include "ppapi/proxy/ppb_audio_proxy.h" |
6 | 6 |
| 7 #include "base/compiler_specific.h" |
7 #include "base/threading/simple_thread.h" | 8 #include "base/threading/simple_thread.h" |
8 #include "ppapi/c/pp_errors.h" | 9 #include "ppapi/c/pp_errors.h" |
9 #include "ppapi/c/ppb_audio.h" | 10 #include "ppapi/c/ppb_audio.h" |
10 #include "ppapi/c/ppb_audio_config.h" | 11 #include "ppapi/c/ppb_audio_config.h" |
11 #include "ppapi/c/ppb_var.h" | 12 #include "ppapi/c/ppb_var.h" |
12 #include "ppapi/c/trusted/ppb_audio_trusted.h" | 13 #include "ppapi/c/trusted/ppb_audio_trusted.h" |
13 #include "ppapi/proxy/enter_proxy.h" | 14 #include "ppapi/proxy/enter_proxy.h" |
14 #include "ppapi/proxy/interface_id.h" | 15 #include "ppapi/proxy/interface_id.h" |
15 #include "ppapi/proxy/plugin_dispatcher.h" | 16 #include "ppapi/proxy/plugin_dispatcher.h" |
16 #include "ppapi/proxy/plugin_resource.h" | 17 #include "ppapi/proxy/plugin_resource.h" |
17 #include "ppapi/proxy/ppapi_messages.h" | 18 #include "ppapi/proxy/ppapi_messages.h" |
18 #include "ppapi/shared_impl/audio_impl.h" | 19 #include "ppapi/shared_impl/audio_impl.h" |
19 #include "ppapi/thunk/ppb_audio_config_api.h" | 20 #include "ppapi/thunk/ppb_audio_config_api.h" |
20 #include "ppapi/thunk/ppb_audio_trusted_api.h" | |
21 #include "ppapi/thunk/enter.h" | 21 #include "ppapi/thunk/enter.h" |
22 #include "ppapi/thunk/resource_creation_api.h" | 22 #include "ppapi/thunk/resource_creation_api.h" |
23 #include "ppapi/thunk/thunk.h" | 23 #include "ppapi/thunk/thunk.h" |
24 | 24 |
25 using ::ppapi::thunk::PPB_Audio_API; | 25 using ppapi::thunk::EnterResourceNoLock; |
| 26 using ppapi::thunk::PPB_Audio_API; |
| 27 using ppapi::thunk::PPB_AudioConfig_API; |
26 | 28 |
27 namespace pp { | 29 namespace pp { |
28 namespace proxy { | 30 namespace proxy { |
29 | 31 |
30 class Audio : public PluginResource, public ppapi::AudioImpl { | 32 class Audio : public PluginResource, public ppapi::AudioImpl { |
31 public: | 33 public: |
32 Audio(const HostResource& audio_id, | 34 Audio(const HostResource& audio_id, |
33 PP_Resource config_id, | 35 PP_Resource config_id, |
34 PPB_Audio_Callback callback, | 36 PPB_Audio_Callback callback, |
35 void* user_data); | 37 void* user_data); |
36 virtual ~Audio(); | 38 virtual ~Audio(); |
37 | 39 |
38 // ResourceObjectBase overrides. | 40 // ResourceObjectBase overrides. |
39 virtual PPB_Audio_API* AsPPB_Audio_API(); | 41 virtual PPB_Audio_API* AsPPB_Audio_API(); |
40 | 42 |
41 // PPB_Audio_API implementation. | 43 // PPB_Audio_API implementation. |
42 virtual PP_Resource GetCurrentConfig() OVERRIDE; | 44 virtual PP_Resource GetCurrentConfig() OVERRIDE; |
43 virtual PP_Bool StartPlayback() OVERRIDE; | 45 virtual PP_Bool StartPlayback() OVERRIDE; |
44 virtual PP_Bool StopPlayback() OVERRIDE; | 46 virtual PP_Bool StopPlayback() OVERRIDE; |
| 47 virtual int32_t OpenTrusted(PP_Resource config_id, |
| 48 PP_CompletionCallback create_callback) OVERRIDE; |
| 49 virtual int32_t GetSyncSocket(int* sync_socket) OVERRIDE; |
| 50 virtual int32_t GetSharedMemory(int* shm_handle, uint32_t* shm_size) OVERRIDE; |
45 | 51 |
46 private: | 52 private: |
47 // Owning reference to the current config object. This isn't actually used, | 53 // Owning reference to the current config object. This isn't actually used, |
48 // we just dish it out as requested by the plugin. | 54 // we just dish it out as requested by the plugin. |
49 PP_Resource config_; | 55 PP_Resource config_; |
50 | 56 |
51 DISALLOW_COPY_AND_ASSIGN(Audio); | 57 DISALLOW_COPY_AND_ASSIGN(Audio); |
52 }; | 58 }; |
53 | 59 |
54 Audio::Audio(const HostResource& audio_id, | 60 Audio::Audio(const HostResource& audio_id, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 PP_Bool Audio::StopPlayback() { | 94 PP_Bool Audio::StopPlayback() { |
89 if (!playing()) | 95 if (!playing()) |
90 return PP_TRUE; | 96 return PP_TRUE; |
91 PluginDispatcher::GetForInstance(instance())->Send( | 97 PluginDispatcher::GetForInstance(instance())->Send( |
92 new PpapiHostMsg_PPBAudio_StartOrStop( | 98 new PpapiHostMsg_PPBAudio_StartOrStop( |
93 INTERFACE_ID_PPB_AUDIO, host_resource(), false)); | 99 INTERFACE_ID_PPB_AUDIO, host_resource(), false)); |
94 SetStopPlaybackState(); | 100 SetStopPlaybackState(); |
95 return PP_TRUE; | 101 return PP_TRUE; |
96 } | 102 } |
97 | 103 |
| 104 int32_t Audio::OpenTrusted(PP_Resource config_id, |
| 105 PP_CompletionCallback create_callback) { |
| 106 return PP_ERROR_NOTSUPPORTED; // Don't proxy the trusted interface. |
| 107 } |
| 108 |
| 109 int32_t Audio::GetSyncSocket(int* sync_socket) { |
| 110 return PP_ERROR_NOTSUPPORTED; // Don't proxy the trusted interface. |
| 111 } |
| 112 |
| 113 int32_t Audio::GetSharedMemory(int* shm_handle, uint32_t* shm_size) { |
| 114 return PP_ERROR_NOTSUPPORTED; // Don't proxy the trusted interface. |
| 115 } |
| 116 |
98 namespace { | 117 namespace { |
99 | 118 |
100 InterfaceProxy* CreateAudioProxy(Dispatcher* dispatcher, | 119 InterfaceProxy* CreateAudioProxy(Dispatcher* dispatcher, |
101 const void* target_interface) { | 120 const void* target_interface) { |
102 return new PPB_Audio_Proxy(dispatcher, target_interface); | 121 return new PPB_Audio_Proxy(dispatcher, target_interface); |
103 } | 122 } |
104 | 123 |
105 base::PlatformFile IntToPlatformFile(int32_t handle) { | 124 base::PlatformFile IntToPlatformFile(int32_t handle) { |
106 // TODO(piman/brettw): Change trusted interface to return a PP_FileHandle, | 125 // TODO(piman/brettw): Change trusted interface to return a PP_FileHandle, |
107 // those casts are ugly. | 126 // those casts are ugly. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 // static | 159 // static |
141 PP_Resource PPB_Audio_Proxy::CreateProxyResource( | 160 PP_Resource PPB_Audio_Proxy::CreateProxyResource( |
142 PP_Instance instance_id, | 161 PP_Instance instance_id, |
143 PP_Resource config_id, | 162 PP_Resource config_id, |
144 PPB_Audio_Callback audio_callback, | 163 PPB_Audio_Callback audio_callback, |
145 void* user_data) { | 164 void* user_data) { |
146 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id); | 165 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id); |
147 if (!dispatcher) | 166 if (!dispatcher) |
148 return 0; | 167 return 0; |
149 | 168 |
150 ::ppapi::thunk::EnterResourceNoLock< ::ppapi::thunk::PPB_AudioConfig_API> | 169 EnterResourceNoLock<PPB_AudioConfig_API> config(config_id, true); |
151 config(config_id, true); | |
152 if (config.failed()) | 170 if (config.failed()) |
153 return 0; | 171 return 0; |
154 | 172 |
155 HostResource result; | 173 HostResource result; |
156 dispatcher->Send(new PpapiHostMsg_PPBAudio_Create( | 174 dispatcher->Send(new PpapiHostMsg_PPBAudio_Create( |
157 INTERFACE_ID_PPB_AUDIO, instance_id, | 175 INTERFACE_ID_PPB_AUDIO, instance_id, |
158 config.object()->GetSampleRate(), config.object()->GetSampleFrameCount(), | 176 config.object()->GetSampleRate(), config.object()->GetSampleFrameCount(), |
159 &result)); | 177 &result)); |
160 if (result.is_null()) | 178 if (result.is_null()) |
161 return 0; | 179 return 0; |
(...skipping 24 matching lines...) Expand all Loading... |
186 resource_creation(instance_id, true); | 204 resource_creation(instance_id, true); |
187 if (resource_creation.failed()) | 205 if (resource_creation.failed()) |
188 return; | 206 return; |
189 | 207 |
190 // Make the resource and get the API pointer to its trusted interface. | 208 // Make the resource and get the API pointer to its trusted interface. |
191 result->SetHostResource( | 209 result->SetHostResource( |
192 instance_id, | 210 instance_id, |
193 resource_creation.functions()->CreateAudioTrusted(instance_id)); | 211 resource_creation.functions()->CreateAudioTrusted(instance_id)); |
194 if (result->is_null()) | 212 if (result->is_null()) |
195 return; | 213 return; |
196 ::ppapi::thunk::EnterResourceNoLock< ::ppapi::thunk::PPB_AudioTrusted_API> | 214 |
197 trusted_audio(result->host_resource(), false); | 215 // At this point, we've set the result resource, and this is a sync request. |
198 if (trusted_audio.failed()) | 216 // Anything below this point must issue the AudioChannelConnected callback |
199 return; | 217 // to the browser. Since that's an async message, it will be issued back to |
| 218 // the plugin after the Create function returns (which is good because it |
| 219 // would be weird to get a connected message with a failure code for a |
| 220 // resource you haven't finished creating yet). |
| 221 // |
| 222 // The ...ForceCallback class will help ensure the callback is always called. |
| 223 // All error cases must call SetResult on this class. |
| 224 EnterHostFromHostResourceForceCallback<PPB_Audio_API> enter( |
| 225 *result, callback_factory_, |
| 226 &PPB_Audio_Proxy::AudioChannelConnected, *result); |
| 227 if (enter.failed()) |
| 228 return; // When enter fails, it will internally schedule the callback. |
200 | 229 |
201 // Make an audio config object. | 230 // Make an audio config object. |
202 PP_Resource audio_config_res = | 231 PP_Resource audio_config_res = |
203 resource_creation.functions()->CreateAudioConfig( | 232 resource_creation.functions()->CreateAudioConfig( |
204 instance_id, static_cast<PP_AudioSampleRate>(sample_rate), | 233 instance_id, static_cast<PP_AudioSampleRate>(sample_rate), |
205 sample_frame_count); | 234 sample_frame_count); |
206 if (!audio_config_res) | 235 if (!audio_config_res) { |
| 236 enter.SetResult(PP_ERROR_FAILED); |
207 return; | 237 return; |
| 238 } |
208 | 239 |
209 // Initiate opening the audio object. | 240 // Initiate opening the audio object. |
210 CompletionCallback callback = callback_factory_.NewOptionalCallback( | 241 enter.SetResult(enter.object()->OpenTrusted(audio_config_res, |
211 &PPB_Audio_Proxy::AudioChannelConnected, *result); | 242 enter.callback())); |
212 int32_t open_error = trusted_audio.object()->OpenTrusted( | |
213 audio_config_res, callback.pp_completion_callback()); | |
214 if (open_error != PP_OK_COMPLETIONPENDING) | |
215 callback.Run(open_error); | |
216 | 243 |
217 // Clean up the temporary audio config resource we made. | 244 // Clean up the temporary audio config resource we made. |
218 const PPB_Core* core = static_cast<const PPB_Core*>( | 245 const PPB_Core* core = static_cast<const PPB_Core*>( |
219 dispatcher()->GetLocalInterface(PPB_CORE_INTERFACE)); | 246 dispatcher()->GetLocalInterface(PPB_CORE_INTERFACE)); |
220 core->ReleaseResource(audio_config_res); | 247 core->ReleaseResource(audio_config_res); |
221 } | 248 } |
222 | 249 |
223 void PPB_Audio_Proxy::OnMsgStartOrStop(const HostResource& audio_id, | 250 void PPB_Audio_Proxy::OnMsgStartOrStop(const HostResource& audio_id, |
224 bool play) { | 251 bool play) { |
225 if (play) | 252 if (play) |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 dispatcher()->Send(new PpapiMsg_PPBAudio_NotifyAudioStreamCreated( | 300 dispatcher()->Send(new PpapiMsg_PPBAudio_NotifyAudioStreamCreated( |
274 INTERFACE_ID_PPB_AUDIO, resource, result_code, socket_handle, | 301 INTERFACE_ID_PPB_AUDIO, resource, result_code, socket_handle, |
275 shared_memory, shared_memory_length)); | 302 shared_memory, shared_memory_length)); |
276 } | 303 } |
277 | 304 |
278 int32_t PPB_Audio_Proxy::GetAudioConnectedHandles( | 305 int32_t PPB_Audio_Proxy::GetAudioConnectedHandles( |
279 const HostResource& resource, | 306 const HostResource& resource, |
280 IPC::PlatformFileForTransit* foreign_socket_handle, | 307 IPC::PlatformFileForTransit* foreign_socket_handle, |
281 base::SharedMemoryHandle* foreign_shared_memory_handle, | 308 base::SharedMemoryHandle* foreign_shared_memory_handle, |
282 uint32_t* shared_memory_length) { | 309 uint32_t* shared_memory_length) { |
283 // Get the trusted audio interface which will give us the handles. | 310 // Get the audio interface which will give us the handles. |
284 ::ppapi::thunk::EnterResourceNoLock< ::ppapi::thunk::PPB_AudioTrusted_API> | 311 EnterResourceNoLock<PPB_Audio_API> enter(resource.host_resource(), false); |
285 trusted_audio(resource.host_resource(), false); | 312 if (enter.failed()) |
286 if (trusted_audio.failed()) | |
287 return PP_ERROR_NOINTERFACE; | 313 return PP_ERROR_NOINTERFACE; |
288 | 314 |
289 // Get the socket handle for signaling. | 315 // Get the socket handle for signaling. |
290 int32_t socket_handle; | 316 int32_t socket_handle; |
291 int32_t result = trusted_audio.object()->GetSyncSocket(&socket_handle); | 317 int32_t result = enter.object()->GetSyncSocket(&socket_handle); |
292 if (result != PP_OK) | 318 if (result != PP_OK) |
293 return result; | 319 return result; |
294 | 320 |
295 // socket_handle doesn't belong to us: don't close it. | 321 // socket_handle doesn't belong to us: don't close it. |
296 *foreign_socket_handle = dispatcher()->ShareHandleWithRemote( | 322 *foreign_socket_handle = dispatcher()->ShareHandleWithRemote( |
297 IntToPlatformFile(socket_handle), false); | 323 IntToPlatformFile(socket_handle), false); |
298 if (*foreign_socket_handle == IPC::InvalidPlatformFileForTransit()) | 324 if (*foreign_socket_handle == IPC::InvalidPlatformFileForTransit()) |
299 return PP_ERROR_FAILED; | 325 return PP_ERROR_FAILED; |
300 | 326 |
301 // Get the shared memory for the buffer. | 327 // Get the shared memory for the buffer. |
302 int shared_memory_handle; | 328 int shared_memory_handle; |
303 result = trusted_audio.object()->GetSharedMemory(&shared_memory_handle, | 329 result = enter.object()->GetSharedMemory(&shared_memory_handle, |
304 shared_memory_length); | 330 shared_memory_length); |
305 if (result != PP_OK) | 331 if (result != PP_OK) |
306 return result; | 332 return result; |
307 | 333 |
308 // shared_memory_handle doesn't belong to us: don't close it. | 334 // shared_memory_handle doesn't belong to us: don't close it. |
309 *foreign_shared_memory_handle = dispatcher()->ShareHandleWithRemote( | 335 *foreign_shared_memory_handle = dispatcher()->ShareHandleWithRemote( |
310 IntToPlatformFile(shared_memory_handle), false); | 336 IntToPlatformFile(shared_memory_handle), false); |
311 if (*foreign_shared_memory_handle == IPC::InvalidPlatformFileForTransit()) | 337 if (*foreign_shared_memory_handle == IPC::InvalidPlatformFileForTransit()) |
312 return PP_ERROR_FAILED; | 338 return PP_ERROR_FAILED; |
313 | 339 |
314 return PP_OK; | 340 return PP_OK; |
315 } | 341 } |
316 | 342 |
317 } // namespace proxy | 343 } // namespace proxy |
318 } // namespace pp | 344 } // namespace pp |
OLD | NEW |