OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ppapi/proxy/ppb_audio_input_proxy.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "build/build_config.h" |
| 11 #include "ppapi/c/pp_errors.h" |
| 12 #include "ppapi/c/pp_resource.h" |
| 13 #include "ppapi/c/ppb_core.h" |
| 14 #include "ppapi/c/ppb_var.h" |
| 15 #include "ppapi/c/dev/ppb_audio_input_dev.h" |
| 16 #include "ppapi/proxy/enter_proxy.h" |
| 17 #include "ppapi/proxy/interface_id.h" |
| 18 #include "ppapi/proxy/plugin_dispatcher.h" |
| 19 #include "ppapi/proxy/ppapi_messages.h" |
| 20 #include "ppapi/shared_impl/audio_input_impl.h" |
| 21 #include "ppapi/shared_impl/resource.h" |
| 22 #include "ppapi/thunk/ppb_audio_input_api.h" |
| 23 #include "ppapi/thunk/enter.h" |
| 24 #include "ppapi/thunk/resource_creation_api.h" |
| 25 #include "ppapi/thunk/thunk.h" |
| 26 |
| 27 using ppapi::thunk::EnterResourceNoLock; |
| 28 using ppapi::thunk::PPB_Buffer_API; |
| 29 using ppapi::thunk::PPB_BufferTrusted_API; |
| 30 using ppapi::thunk::PPB_AudioInput_API; |
| 31 |
| 32 namespace ppapi { |
| 33 namespace proxy { |
| 34 |
| 35 class AudioInput : public Resource, public AudioInputImpl { |
| 36 public: |
| 37 AudioInput(const HostResource& audio_input_id, |
| 38 PPB_AudioInput_Callback callback, |
| 39 void* user_data); |
| 40 virtual ~AudioInput(); |
| 41 |
| 42 // Resource overrides. |
| 43 virtual PPB_AudioInput_API* AsPPB_AudioInput_API(); |
| 44 |
| 45 // PPB_AudioInput_API implementation. |
| 46 virtual PP_Bool StartCapture() OVERRIDE; |
| 47 virtual PP_Bool StopCapture() OVERRIDE; |
| 48 |
| 49 virtual int32_t OpenTrusted(PP_CompletionCallback create_callback) OVERRIDE; |
| 50 virtual int32_t GetSyncSocket(int* sync_socket) OVERRIDE; |
| 51 virtual int32_t GetSharedMemory(int* shm_handle, uint32_t* shm_size) OVERRIDE; |
| 52 |
| 53 private: |
| 54 DISALLOW_COPY_AND_ASSIGN(AudioInput); |
| 55 }; |
| 56 |
| 57 AudioInput::AudioInput(const HostResource& audio_input_id, |
| 58 PPB_AudioInput_Callback callback, |
| 59 void* user_data) |
| 60 : Resource(audio_input_id) { |
| 61 SetCallback(callback, user_data); |
| 62 } |
| 63 |
| 64 AudioInput::~AudioInput() { |
| 65 } |
| 66 |
| 67 PPB_AudioInput_API* AudioInput::AsPPB_AudioInput_API() { |
| 68 return this; |
| 69 } |
| 70 |
| 71 |
| 72 PP_Bool AudioInput::StartCapture() { |
| 73 if (capturing()) |
| 74 return PP_TRUE; |
| 75 SetStartCaptureState(); |
| 76 PluginDispatcher::GetForResource(this)->Send( |
| 77 new PpapiHostMsg_PPBAudioInput_StartOrStop( |
| 78 INTERFACE_ID_PPB_AUDIO_INPUT_DEV, host_resource(), true)); |
| 79 return PP_TRUE; |
| 80 } |
| 81 |
| 82 PP_Bool AudioInput::StopCapture() { |
| 83 if (!capturing()) |
| 84 return PP_TRUE; |
| 85 PluginDispatcher::GetForResource(this)->Send( |
| 86 new PpapiHostMsg_PPBAudioInput_StartOrStop( |
| 87 INTERFACE_ID_PPB_AUDIO_INPUT_DEV, host_resource(), false)); |
| 88 SetStopCaptureState(); |
| 89 return PP_TRUE; |
| 90 } |
| 91 |
| 92 int32_t AudioInput::OpenTrusted(PP_CompletionCallback create_callback) { |
| 93 return PP_ERROR_NOTSUPPORTED; // Don't proxy the trusted interface. |
| 94 } |
| 95 |
| 96 int32_t AudioInput::GetSyncSocket(int* sync_socket) { |
| 97 return PP_ERROR_NOTSUPPORTED; // Don't proxy the trusted interface. |
| 98 } |
| 99 |
| 100 int32_t AudioInput::GetSharedMemory(int* shm_handle, uint32_t* shm_size) { |
| 101 return PP_ERROR_NOTSUPPORTED; // Don't proxy the trusted interface. |
| 102 } |
| 103 |
| 104 |
| 105 namespace { |
| 106 |
| 107 base::PlatformFile IntToPlatformFile(int32_t handle) { |
| 108 // TODO(piman/brettw): Change trusted interface to return a PP_FileHandle, |
| 109 // those casts are ugly. |
| 110 #if defined(OS_WIN) |
| 111 return reinterpret_cast<HANDLE>(static_cast<intptr_t>(handle)); |
| 112 #elif defined(OS_POSIX) |
| 113 return handle; |
| 114 #else |
| 115 #error Not implemented. |
| 116 #endif |
| 117 } |
| 118 |
| 119 } // namespace |
| 120 |
| 121 |
| 122 PPB_AudioInput_Proxy::PPB_AudioInput_Proxy(Dispatcher* dispatcher) |
| 123 : InterfaceProxy(dispatcher), |
| 124 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 125 } |
| 126 |
| 127 PPB_AudioInput_Proxy::~PPB_AudioInput_Proxy() { |
| 128 } |
| 129 |
| 130 // static |
| 131 PP_Resource PPB_AudioInput_Proxy::CreateProxyResource( |
| 132 PP_Instance instance_id, |
| 133 PPB_AudioInput_Callback audio_input_callback, |
| 134 void* user_data) { |
| 135 |
| 136 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id); |
| 137 if (!dispatcher) |
| 138 return 0; |
| 139 |
| 140 HostResource result; |
| 141 dispatcher->Send(new PpapiHostMsg_PPBAudioInput_Create( |
| 142 INTERFACE_ID_PPB_AUDIO_INPUT_DEV, instance_id, &result)); |
| 143 if (result.is_null()) |
| 144 return 0; |
| 145 |
| 146 return (new AudioInput(result, audio_input_callback, |
| 147 user_data))->GetReference(); |
| 148 } |
| 149 |
| 150 bool PPB_AudioInput_Proxy::OnMessageReceived(const IPC::Message& msg) { |
| 151 bool handled = true; |
| 152 IPC_BEGIN_MESSAGE_MAP(PPB_AudioInput_Proxy, msg) |
| 153 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBAudioInput_Create, OnMsgCreate) |
| 154 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBAudioInput_StartOrStop, |
| 155 OnMsgStartOrStop) |
| 156 IPC_MESSAGE_HANDLER(PpapiMsg_PPBAudioInput_NotifyAudioStreamCreated, |
| 157 OnMsgNotifyAudioStreamCreated) |
| 158 IPC_MESSAGE_UNHANDLED(handled = false) |
| 159 IPC_END_MESSAGE_MAP() |
| 160 // TODO(brettw) handle bad messages! |
| 161 |
| 162 return handled; |
| 163 } |
| 164 |
| 165 void PPB_AudioInput_Proxy::OnMsgCreate(PP_Instance instance_id, |
| 166 HostResource* result) { |
| 167 thunk::EnterFunction<thunk::ResourceCreationAPI> resource_creation( |
| 168 instance_id, true); |
| 169 if (resource_creation.failed()) |
| 170 return; |
| 171 |
| 172 // Make the resource and get the API pointer to its trusted interface. |
| 173 result->SetHostResource( |
| 174 instance_id, |
| 175 resource_creation.functions()->CreateAudioInputTrusted(instance_id)); |
| 176 if (result->is_null()) |
| 177 return; |
| 178 |
| 179 |
| 180 // At this point, we've set the result resource, and this is a sync request. |
| 181 // Anything below this point must issue the AudioInputChannelConnected |
| 182 // callback to the browser. Since that's an async message, it will be issued |
| 183 // back to the plugin after the Create function returns (which is good |
| 184 // because it would be weird to get a connected message with a failure code |
| 185 // for a resource you haven't finished creating yet). |
| 186 // |
| 187 // The ...ForceCallback class will help ensure the callback is always called. |
| 188 // All error cases must call SetResult on this class. |
| 189 |
| 190 EnterHostFromHostResourceForceCallback<PPB_AudioInput_API> enter( |
| 191 *result, callback_factory_, |
| 192 &PPB_AudioInput_Proxy::AudioInputChannelConnected, *result); |
| 193 if (enter.failed()) |
| 194 return; // When enter fails, it will internally schedule the callback. |
| 195 |
| 196 // Initiate opening the audio object. |
| 197 enter.SetResult(enter.object()->OpenTrusted( |
| 198 enter.callback())); |
| 199 } |
| 200 |
| 201 void PPB_AudioInput_Proxy::OnMsgStartOrStop( |
| 202 const HostResource& resource, |
| 203 bool capture) |
| 204 { |
| 205 EnterHostFromHostResource<PPB_AudioInput_API> enter(resource); |
| 206 if (enter.failed()) |
| 207 return; |
| 208 if (capture) |
| 209 enter.object()->StartCapture(); |
| 210 else |
| 211 enter.object()->StopCapture(); |
| 212 } |
| 213 |
| 214 // Processed in the plugin (message from host). |
| 215 void PPB_AudioInput_Proxy::OnMsgNotifyAudioStreamCreated( |
| 216 const HostResource& audio_id, |
| 217 int32_t result_code, |
| 218 IPC::PlatformFileForTransit socket_handle, |
| 219 base::SharedMemoryHandle handle, |
| 220 uint32_t length) { |
| 221 EnterPluginFromHostResource<PPB_AudioInput_API> enter(audio_id); |
| 222 if (enter.failed() || result_code != PP_OK) { |
| 223 // The caller may still have given us these handles in the failure case. |
| 224 // The easiest way to clean these up is to just put them in the objects |
| 225 // and then close them. This failure case is not performance critical. |
| 226 base::SyncSocket temp_socket( |
| 227 IPC::PlatformFileForTransitToPlatformFile(socket_handle)); |
| 228 base::SharedMemory temp_mem(handle, false); |
| 229 } else { |
| 230 static_cast<AudioInput*>(enter.object())->SetStreamInfo( |
| 231 handle, length, |
| 232 IPC::PlatformFileForTransitToPlatformFile(socket_handle)); |
| 233 } |
| 234 } |
| 235 |
| 236 void PPB_AudioInput_Proxy::AudioInputChannelConnected( |
| 237 int32_t result, |
| 238 const HostResource& resource) { |
| 239 IPC::PlatformFileForTransit socket_handle = |
| 240 IPC::InvalidPlatformFileForTransit(); |
| 241 base::SharedMemoryHandle shared_memory = IPC::InvalidPlatformFileForTransit(); |
| 242 uint32_t shared_memory_length = 0; |
| 243 |
| 244 int32_t result_code = result; |
| 245 if (result_code == PP_OK) { |
| 246 result_code = GetAudioInputConnectedHandles(resource, &socket_handle, |
| 247 &shared_memory, |
| 248 &shared_memory_length); |
| 249 } |
| 250 |
| 251 // Send all the values, even on error. This simplifies some of our cleanup |
| 252 // code since the handles will be in the other process and could be |
| 253 // inconvenient to clean up. Our IPC code will automatically handle this for |
| 254 // us, as long as the remote side always closes the handles it receives |
| 255 // (in OnMsgNotifyAudioStreamCreated), even in the failure case. |
| 256 dispatcher()->Send(new PpapiMsg_PPBAudioInput_NotifyAudioStreamCreated( |
| 257 INTERFACE_ID_PPB_AUDIO_INPUT_DEV, resource, result_code, socket_handle, |
| 258 shared_memory, shared_memory_length)); |
| 259 } |
| 260 |
| 261 int32_t PPB_AudioInput_Proxy::GetAudioInputConnectedHandles( |
| 262 const HostResource& resource, |
| 263 IPC::PlatformFileForTransit* foreign_socket_handle, |
| 264 base::SharedMemoryHandle* foreign_shared_memory_handle, |
| 265 uint32_t* shared_memory_length) { |
| 266 // Get the audio interface which will give us the handles. |
| 267 EnterHostFromHostResource<PPB_AudioInput_API> enter(resource); |
| 268 if (enter.failed()) |
| 269 return PP_ERROR_NOINTERFACE; |
| 270 |
| 271 // Get the socket handle for signaling. |
| 272 int32_t socket_handle; |
| 273 int32_t result = enter.object()->GetSyncSocket(&socket_handle); |
| 274 if (result != PP_OK) |
| 275 return result; |
| 276 |
| 277 // socket_handle doesn't belong to us: don't close it. |
| 278 *foreign_socket_handle = dispatcher()->ShareHandleWithRemote( |
| 279 IntToPlatformFile(socket_handle), false); |
| 280 if (*foreign_socket_handle == IPC::InvalidPlatformFileForTransit()) |
| 281 return PP_ERROR_FAILED; |
| 282 |
| 283 // Get the shared memory for the buffer. |
| 284 int shared_memory_handle; |
| 285 result = enter.object()->GetSharedMemory(&shared_memory_handle, |
| 286 shared_memory_length); |
| 287 if (result != PP_OK) |
| 288 return result; |
| 289 |
| 290 // shared_memory_handle doesn't belong to us: don't close it. |
| 291 *foreign_shared_memory_handle = dispatcher()->ShareHandleWithRemote( |
| 292 IntToPlatformFile(shared_memory_handle), false); |
| 293 if (*foreign_shared_memory_handle == IPC::InvalidPlatformFileForTransit()) |
| 294 return PP_ERROR_FAILED; |
| 295 |
| 296 return PP_OK; |
| 297 } |
| 298 |
| 299 |
| 300 } // namespace proxy |
| 301 } // namespace ppapi |
OLD | NEW |