[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 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_proxy.h" |
| 6 | |
[email protected] | ac9ba8fe | 2010-12-30 18:08:36 | [diff] [blame] | 7 | #include "base/threading/simple_thread.h" |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 8 | #include "ppapi/c/pp_errors.h" |
[email protected] | b9a5984 | 2011-01-15 01:04:00 | [diff] [blame] | 9 | #include "ppapi/c/ppb_audio.h" |
| 10 | #include "ppapi/c/trusted/ppb_audio_trusted.h" |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 11 | #include "ppapi/proxy/interface_id.h" |
| 12 | #include "ppapi/proxy/plugin_dispatcher.h" |
| 13 | #include "ppapi/proxy/plugin_resource.h" |
| 14 | #include "ppapi/proxy/ppapi_messages.h" |
| 15 | #include "ppapi/shared_impl/audio_impl.h" |
| 16 | |
| 17 | namespace pp { |
| 18 | namespace proxy { |
| 19 | |
| 20 | class Audio : public PluginResource, public pp::shared_impl::AudioImpl { |
| 21 | public: |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 22 | Audio(const HostResource& audio_id, |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 23 | PP_Resource config_id, |
| 24 | PPB_Audio_Callback callback, |
| 25 | void* user_data) |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 26 | : PluginResource(audio_id), |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 27 | config_(config_id) { |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 28 | SetCallback(callback, user_data); |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 29 | PluginResourceTracker::GetInstance()->AddRefResource(config_); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 30 | } |
| 31 | virtual ~Audio() { |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 32 | PluginResourceTracker::GetInstance()->ReleaseResource(config_); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // Resource overrides. |
| 36 | virtual Audio* AsAudio() { return this; } |
| 37 | |
| 38 | PP_Resource config() const { return config_; } |
| 39 | |
| 40 | void StartPlayback(PP_Resource resource) { |
| 41 | if (playing()) |
| 42 | return; |
| 43 | SetStartPlaybackState(); |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 44 | PluginDispatcher::GetForInstance(instance())->Send( |
| 45 | new PpapiHostMsg_PPBAudio_StartOrStop( |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 46 | INTERFACE_ID_PPB_AUDIO, host_resource(), true)); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void StopPlayback(PP_Resource resource) { |
| 50 | if (!playing()) |
| 51 | return; |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 52 | PluginDispatcher::GetForInstance(instance())->Send( |
| 53 | new PpapiHostMsg_PPBAudio_StartOrStop( |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 54 | INTERFACE_ID_PPB_AUDIO, host_resource(), false)); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 55 | SetStopPlaybackState(); |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | PP_Resource config_; |
| 60 | |
| 61 | DISALLOW_COPY_AND_ASSIGN(Audio); |
| 62 | }; |
| 63 | |
| 64 | namespace { |
| 65 | |
| 66 | PP_Resource Create(PP_Instance instance_id, |
| 67 | PP_Resource config_id, |
| 68 | PPB_Audio_Callback callback, |
| 69 | void* user_data) { |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 70 | PluginResource* config = PluginResourceTracker::GetInstance()-> |
| 71 | GetResourceObject(config_id); |
| 72 | if (!config) |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 73 | return 0; |
| 74 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 75 | HostResource result; |
| 76 | PluginDispatcher::Get()->Send(new PpapiHostMsg_PPBAudio_Create( |
| 77 | INTERFACE_ID_PPB_AUDIO, instance_id, config->host_resource(), &result)); |
| 78 | if (result.is_null()) |
| 79 | return 0; |
| 80 | |
| 81 | linked_ptr<Audio> object(new Audio(result, config_id, callback, user_data)); |
| 82 | return PluginResourceTracker::GetInstance()->AddResource(object); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | PP_Bool IsAudio(PP_Resource resource) { |
| 86 | Audio* object = PluginResource::GetAs<Audio>(resource); |
| 87 | return BoolToPPBool(!!object); |
| 88 | } |
| 89 | |
| 90 | PP_Resource GetCurrentConfiguration(PP_Resource audio_id) { |
| 91 | Audio* object = PluginResource::GetAs<Audio>(audio_id); |
| 92 | if (!object) |
| 93 | return 0; |
| 94 | PP_Resource result = object->config(); |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 95 | PluginResourceTracker::GetInstance()->AddRefResource(result); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 96 | return result; |
| 97 | } |
| 98 | |
| 99 | PP_Bool StartPlayback(PP_Resource audio_id) { |
| 100 | Audio* object = PluginResource::GetAs<Audio>(audio_id); |
| 101 | if (!object) |
| 102 | return PP_FALSE; |
| 103 | object->StartPlayback(audio_id); |
| 104 | return PP_TRUE; |
| 105 | } |
| 106 | |
| 107 | PP_Bool StopPlayback(PP_Resource audio_id) { |
| 108 | Audio* object = PluginResource::GetAs<Audio>(audio_id); |
| 109 | if (!object) |
| 110 | return PP_FALSE; |
| 111 | object->StopPlayback(audio_id); |
| 112 | return PP_TRUE; |
| 113 | } |
| 114 | |
[email protected] | b9a5984 | 2011-01-15 01:04:00 | [diff] [blame] | 115 | const PPB_Audio audio_interface = { |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 116 | &Create, |
| 117 | &IsAudio, |
| 118 | &GetCurrentConfiguration, |
| 119 | &StartPlayback, |
| 120 | &StopPlayback |
| 121 | }; |
| 122 | |
| 123 | } // namespace |
| 124 | |
| 125 | PPB_Audio_Proxy::PPB_Audio_Proxy(Dispatcher* dispatcher, |
| 126 | const void* target_interface) |
| 127 | : InterfaceProxy(dispatcher, target_interface), |
| 128 | callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 129 | } |
| 130 | |
| 131 | PPB_Audio_Proxy::~PPB_Audio_Proxy() { |
| 132 | } |
| 133 | |
| 134 | const void* PPB_Audio_Proxy::GetSourceInterface() const { |
| 135 | return &audio_interface; |
| 136 | } |
| 137 | |
| 138 | InterfaceID PPB_Audio_Proxy::GetInterfaceId() const { |
| 139 | return INTERFACE_ID_PPB_AUDIO; |
| 140 | } |
| 141 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 142 | bool PPB_Audio_Proxy::OnMessageReceived(const IPC::Message& msg) { |
| 143 | bool handled = true; |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 144 | IPC_BEGIN_MESSAGE_MAP(PPB_Audio_Proxy, msg) |
| 145 | IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBAudio_Create, OnMsgCreate) |
| 146 | IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBAudio_StartOrStop, |
| 147 | OnMsgStartOrStop) |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 148 | IPC_MESSAGE_HANDLER(PpapiMsg_PPBAudio_NotifyAudioStreamCreated, |
| 149 | OnMsgNotifyAudioStreamCreated) |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 150 | IPC_MESSAGE_UNHANDLED(handled = false) |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 151 | IPC_END_MESSAGE_MAP() |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 152 | return handled; |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void PPB_Audio_Proxy::OnMsgCreate(PP_Instance instance_id, |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 156 | const HostResource& config_id, |
| 157 | HostResource* result) { |
[email protected] | b9a5984 | 2011-01-15 01:04:00 | [diff] [blame] | 158 | const PPB_AudioTrusted* audio_trusted = |
| 159 | reinterpret_cast<const PPB_AudioTrusted*>( |
| 160 | dispatcher()->GetLocalInterface(PPB_AUDIO_TRUSTED_INTERFACE)); |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 161 | if (!audio_trusted) |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 162 | return; |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 163 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 164 | result->SetHostResource(instance_id, |
| 165 | audio_trusted->CreateTrusted(instance_id)); |
| 166 | if (result->is_null()) |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 167 | return; |
| 168 | |
| 169 | CompletionCallback callback = callback_factory_.NewCallback( |
| 170 | &PPB_Audio_Proxy::AudioChannelConnected, *result); |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 171 | int32_t open_error = audio_trusted->Open(result->host_resource(), |
| 172 | config_id.host_resource(), |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 173 | callback.pp_completion_callback()); |
| 174 | if (open_error != PP_ERROR_WOULDBLOCK) |
| 175 | callback.Run(open_error); |
| 176 | } |
| 177 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 178 | void PPB_Audio_Proxy::OnMsgStartOrStop(const HostResource& audio_id, |
| 179 | bool play) { |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 180 | if (play) |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 181 | ppb_audio_target()->StartPlayback(audio_id.host_resource()); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 182 | else |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 183 | ppb_audio_target()->StopPlayback(audio_id.host_resource()); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 184 | } |
| 185 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 186 | // Processed in the plugin (message from host). |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 187 | void PPB_Audio_Proxy::OnMsgNotifyAudioStreamCreated( |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 188 | const PPBAudio_NotifyAudioStreamCreated_Params& params) { |
| 189 | PP_Resource plugin_resource = |
| 190 | PluginResourceTracker::GetInstance()->PluginResourceForHostResource( |
| 191 | params.audio_id); |
| 192 | Audio* object = plugin_resource ? |
| 193 | PluginResource::GetAs<Audio>(plugin_resource) : NULL; |
| 194 | if (!object || params.result_code != PP_OK) { |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 195 | // The caller may still have given us these handles in the failure case. |
| 196 | // The easiest way to clean these up is to just put them in the objects |
| 197 | // and then close them. This failure case is not performance critical. |
| 198 | base::SyncSocket temp_socket( |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 199 | IPC::PlatformFileForTransitToPlatformFile(params.socket_handle)); |
| 200 | base::SharedMemory temp_mem(params.handle, false); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 201 | return; |
| 202 | } |
| 203 | object->SetStreamInfo( |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 204 | params.handle, params.length, |
| 205 | IPC::PlatformFileForTransitToPlatformFile(params.socket_handle)); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 206 | } |
| 207 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 208 | void PPB_Audio_Proxy::AudioChannelConnected( |
| 209 | int32_t result, |
| 210 | const HostResource& resource) { |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 211 | IPC::PlatformFileForTransit socket_handle = |
| 212 | IPC::InvalidPlatformFileForTransit(); |
| 213 | #if defined(OS_WIN) |
| 214 | base::SharedMemoryHandle shared_memory = NULL; |
| 215 | #elif defined(OS_POSIX) |
| 216 | base::SharedMemoryHandle shared_memory(-1, false); |
| 217 | #else |
| 218 | #error Not implemented. |
| 219 | #endif |
| 220 | uint32_t shared_memory_length = 0; |
| 221 | |
| 222 | int32_t result_code = result; |
| 223 | if (result_code == PP_OK) { |
| 224 | result_code = GetAudioConnectedHandles(resource, &socket_handle, |
| 225 | &shared_memory, |
| 226 | &shared_memory_length); |
| 227 | } |
| 228 | |
| 229 | // Send all the values, even on error. This simplifies some of our cleanup |
| 230 | // code since the handles will be in the other process and could be |
| 231 | // inconvenient to clean up. Our IPC code will automatically handle this for |
| 232 | // us, as long as the remote side always closes the handles it receives |
| 233 | // (in OnMsgNotifyAudioStreamCreated), even in the failure case. |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 234 | PPBAudio_NotifyAudioStreamCreated_Params params; |
| 235 | params.audio_id = resource; |
| 236 | params.result_code = result; |
| 237 | params.socket_handle = socket_handle; |
| 238 | params.handle = shared_memory; |
| 239 | params.length = shared_memory_length; |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 240 | dispatcher()->Send(new PpapiMsg_PPBAudio_NotifyAudioStreamCreated( |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 241 | INTERFACE_ID_PPB_AUDIO, params)); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | int32_t PPB_Audio_Proxy::GetAudioConnectedHandles( |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 245 | const HostResource& resource, |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 246 | IPC::PlatformFileForTransit* foreign_socket_handle, |
| 247 | base::SharedMemoryHandle* foreign_shared_memory_handle, |
| 248 | uint32_t* shared_memory_length) { |
| 249 | // Get the trusted audio interface which will give us the handles. |
[email protected] | b9a5984 | 2011-01-15 01:04:00 | [diff] [blame] | 250 | const PPB_AudioTrusted* audio_trusted = |
| 251 | reinterpret_cast<const PPB_AudioTrusted*>( |
| 252 | dispatcher()->GetLocalInterface(PPB_AUDIO_TRUSTED_INTERFACE)); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 253 | if (!audio_trusted) |
| 254 | return PP_ERROR_NOINTERFACE; |
| 255 | |
| 256 | // Get the socket handle for signaling. |
| 257 | int32_t socket_handle; |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 258 | int32_t result = audio_trusted->GetSyncSocket(resource.host_resource(), |
| 259 | &socket_handle); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 260 | if (result != PP_OK) |
| 261 | return result; |
| 262 | |
| 263 | #if defined(OS_WIN) |
| 264 | // On Windows, duplicate the socket into the plugin process, this will |
| 265 | // automatically close the source handle. |
| 266 | ::DuplicateHandle( |
| 267 | GetCurrentProcess(), |
| 268 | reinterpret_cast<HANDLE>(static_cast<intptr_t>(socket_handle)), |
| 269 | dispatcher()->remote_process_handle(), foreign_socket_handle, |
| 270 | STANDARD_RIGHTS_REQUIRED | FILE_MAP_READ | FILE_MAP_WRITE, |
| 271 | FALSE, DUPLICATE_CLOSE_SOURCE); |
| 272 | #else |
| 273 | // On Posix, the socket handle will be auto-duplicated when we send the |
| 274 | // FileDescriptor. Set AutoClose since we don't need the handle any more. |
| 275 | *foreign_socket_handle = base::FileDescriptor(socket_handle, true); |
| 276 | #endif |
| 277 | |
| 278 | // Get the shared memory for the buffer. |
| 279 | // TODO(brettw) remove the reinterpret cast when the interface is updated. |
| 280 | int shared_memory_handle; |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 281 | result = audio_trusted->GetSharedMemory(resource.host_resource(), |
| 282 | &shared_memory_handle, |
| 283 | shared_memory_length); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 284 | if (result != PP_OK) |
| 285 | return result; |
| 286 | |
| 287 | base::SharedMemory shared_memory( |
| 288 | #if defined(OS_WIN) |
| 289 | reinterpret_cast<HANDLE>(static_cast<intptr_t>(shared_memory_handle)), |
| 290 | #else |
| 291 | base::FileDescriptor(shared_memory_handle, false), |
| 292 | #endif |
| 293 | false); |
| 294 | |
| 295 | // Duplicate the shared memory to the plugin process. This will automatically |
| 296 | // close the source handle. |
| 297 | if (!shared_memory.GiveToProcess(dispatcher()->remote_process_handle(), |
| 298 | foreign_shared_memory_handle)) |
| 299 | return PP_ERROR_FAILED; |
| 300 | |
| 301 | return PP_OK; |
| 302 | } |
| 303 | |
| 304 | } // namespace proxy |
| 305 | } // namespace pp |