Make castv2 performance test work.
[chromium-blink-merge.git] / ppapi / proxy / ppb_audio_proxy.cc
blobca714355b839c69bbdff7b16443fb98463c247ba
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.
5 #include "ppapi/proxy/ppb_audio_proxy.h"
7 #include "base/compiler_specific.h"
8 #include "base/threading/simple_thread.h"
9 #include "ppapi/c/pp_errors.h"
10 #include "ppapi/c/ppb_audio.h"
11 #include "ppapi/c/ppb_audio_config.h"
12 #include "ppapi/c/ppb_var.h"
13 #include "ppapi/proxy/enter_proxy.h"
14 #include "ppapi/proxy/plugin_dispatcher.h"
15 #include "ppapi/proxy/ppapi_messages.h"
16 #include "ppapi/shared_impl/api_id.h"
17 #include "ppapi/shared_impl/platform_file.h"
18 #include "ppapi/shared_impl/ppapi_globals.h"
19 #include "ppapi/shared_impl/ppb_audio_shared.h"
20 #include "ppapi/shared_impl/resource.h"
21 #include "ppapi/thunk/ppb_audio_config_api.h"
22 #include "ppapi/thunk/enter.h"
23 #include "ppapi/thunk/resource_creation_api.h"
24 #include "ppapi/thunk/thunk.h"
26 using ppapi::IntToPlatformFile;
27 using ppapi::proxy::SerializedHandle;
28 using ppapi::thunk::EnterResourceNoLock;
29 using ppapi::thunk::PPB_Audio_API;
30 using ppapi::thunk::PPB_AudioConfig_API;
32 namespace ppapi {
33 namespace proxy {
35 class Audio : public Resource, public PPB_Audio_Shared {
36 public:
37 Audio(const HostResource& audio_id,
38 PP_Resource config_id,
39 const AudioCallbackCombined& callback,
40 void* user_data);
41 ~Audio() override;
43 // Resource overrides.
44 PPB_Audio_API* AsPPB_Audio_API() override;
46 // PPB_Audio_API implementation.
47 PP_Resource GetCurrentConfig() override;
48 PP_Bool StartPlayback() override;
49 PP_Bool StopPlayback() override;
50 int32_t Open(PP_Resource config_id,
51 scoped_refptr<TrackedCallback> create_callback) override;
52 int32_t GetSyncSocket(int* sync_socket) override;
53 int32_t GetSharedMemory(int* shm_handle, uint32_t* shm_size) override;
55 private:
56 // Owning reference to the current config object. This isn't actually used,
57 // we just dish it out as requested by the plugin.
58 PP_Resource config_;
60 DISALLOW_COPY_AND_ASSIGN(Audio);
63 Audio::Audio(const HostResource& audio_id,
64 PP_Resource config_id,
65 const AudioCallbackCombined& callback,
66 void* user_data)
67 : Resource(OBJECT_IS_PROXY, audio_id),
68 config_(config_id) {
69 SetCallback(callback, user_data);
70 PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(config_);
73 Audio::~Audio() {
74 #if defined(OS_NACL)
75 // Invoke StopPlayback() to ensure audio back-end has a chance to send the
76 // escape value over the sync socket, which will terminate the client side
77 // audio callback loop. This is required for NaCl Plugins that can't escape
78 // by shutting down the sync_socket.
79 StopPlayback();
80 #endif
81 PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(config_);
84 PPB_Audio_API* Audio::AsPPB_Audio_API() {
85 return this;
88 PP_Resource Audio::GetCurrentConfig() {
89 // AddRef for the caller.
90 PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(config_);
91 return config_;
94 PP_Bool Audio::StartPlayback() {
95 if (playing())
96 return PP_TRUE;
97 if (!PPB_Audio_Shared::IsThreadFunctionReady())
98 return PP_FALSE;
99 SetStartPlaybackState();
100 PluginDispatcher::GetForResource(this)->Send(
101 new PpapiHostMsg_PPBAudio_StartOrStop(
102 API_ID_PPB_AUDIO, host_resource(), true));
103 return PP_TRUE;
106 PP_Bool Audio::StopPlayback() {
107 if (!playing())
108 return PP_TRUE;
109 PluginDispatcher::GetForResource(this)->Send(
110 new PpapiHostMsg_PPBAudio_StartOrStop(
111 API_ID_PPB_AUDIO, host_resource(), false));
112 SetStopPlaybackState();
113 return PP_TRUE;
116 int32_t Audio::Open(PP_Resource config_id,
117 scoped_refptr<TrackedCallback> create_callback) {
118 return PP_ERROR_NOTSUPPORTED; // Don't proxy the trusted interface.
121 int32_t Audio::GetSyncSocket(int* sync_socket) {
122 return PP_ERROR_NOTSUPPORTED; // Don't proxy the trusted interface.
125 int32_t Audio::GetSharedMemory(int* shm_handle, uint32_t* shm_size) {
126 return PP_ERROR_NOTSUPPORTED; // Don't proxy the trusted interface.
129 PPB_Audio_Proxy::PPB_Audio_Proxy(Dispatcher* dispatcher)
130 : InterfaceProxy(dispatcher),
131 callback_factory_(this) {
134 PPB_Audio_Proxy::~PPB_Audio_Proxy() {
137 // static
138 PP_Resource PPB_Audio_Proxy::CreateProxyResource(
139 PP_Instance instance_id,
140 PP_Resource config_id,
141 const AudioCallbackCombined& audio_callback,
142 void* user_data) {
143 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id);
144 if (!dispatcher)
145 return 0;
147 EnterResourceNoLock<PPB_AudioConfig_API> config(config_id, true);
148 if (config.failed())
149 return 0;
151 if (!audio_callback.IsValid())
152 return 0;
154 HostResource result;
155 dispatcher->Send(new PpapiHostMsg_PPBAudio_Create(
156 API_ID_PPB_AUDIO, instance_id,
157 config.object()->GetSampleRate(), config.object()->GetSampleFrameCount(),
158 &result));
159 if (result.is_null())
160 return 0;
162 return (new Audio(result, config_id,
163 audio_callback, user_data))->GetReference();
166 bool PPB_Audio_Proxy::OnMessageReceived(const IPC::Message& msg) {
167 bool handled = true;
168 IPC_BEGIN_MESSAGE_MAP(PPB_Audio_Proxy, msg)
169 // Don't build host side into NaCl IRT.
170 #if !defined(OS_NACL)
171 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBAudio_Create, OnMsgCreate)
172 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBAudio_StartOrStop,
173 OnMsgStartOrStop)
174 #endif
175 IPC_MESSAGE_HANDLER(PpapiMsg_PPBAudio_NotifyAudioStreamCreated,
176 OnMsgNotifyAudioStreamCreated)
177 IPC_MESSAGE_UNHANDLED(handled = false)
178 IPC_END_MESSAGE_MAP()
179 return handled;
182 #if !defined(OS_NACL)
183 void PPB_Audio_Proxy::OnMsgCreate(PP_Instance instance_id,
184 int32_t sample_rate,
185 uint32_t sample_frame_count,
186 HostResource* result) {
187 thunk::EnterResourceCreation resource_creation(instance_id);
188 if (resource_creation.failed())
189 return;
191 // Make the resource and get the API pointer to its trusted interface.
192 result->SetHostResource(
193 instance_id,
194 resource_creation.functions()->CreateAudioTrusted(instance_id));
195 if (result->is_null())
196 return;
198 // At this point, we've set the result resource, and this is a sync request.
199 // Anything below this point must issue the AudioChannelConnected callback
200 // to the browser. Since that's an async message, it will be issued back to
201 // the plugin after the Create function returns (which is good because it
202 // would be weird to get a connected message with a failure code for a
203 // resource you haven't finished creating yet).
205 // The ...ForceCallback class will help ensure the callback is always called.
206 // All error cases must call SetResult on this class.
207 EnterHostFromHostResourceForceCallback<PPB_Audio_API> enter(
208 *result, callback_factory_,
209 &PPB_Audio_Proxy::AudioChannelConnected, *result);
210 if (enter.failed())
211 return; // When enter fails, it will internally schedule the callback.
213 // Make an audio config object.
214 PP_Resource audio_config_res =
215 resource_creation.functions()->CreateAudioConfig(
216 instance_id, static_cast<PP_AudioSampleRate>(sample_rate),
217 sample_frame_count);
218 if (!audio_config_res) {
219 enter.SetResult(PP_ERROR_FAILED);
220 return;
223 // Initiate opening the audio object.
224 enter.SetResult(enter.object()->Open(audio_config_res,
225 enter.callback()));
227 // Clean up the temporary audio config resource we made.
228 const PPB_Core* core = static_cast<const PPB_Core*>(
229 dispatcher()->local_get_interface()(PPB_CORE_INTERFACE));
230 core->ReleaseResource(audio_config_res);
233 void PPB_Audio_Proxy::OnMsgStartOrStop(const HostResource& audio_id,
234 bool play) {
235 EnterHostFromHostResource<PPB_Audio_API> enter(audio_id);
236 if (enter.failed())
237 return;
238 if (play)
239 enter.object()->StartPlayback();
240 else
241 enter.object()->StopPlayback();
244 void PPB_Audio_Proxy::AudioChannelConnected(
245 int32_t result,
246 const HostResource& resource) {
247 IPC::PlatformFileForTransit socket_handle =
248 IPC::InvalidPlatformFileForTransit();
249 base::SharedMemoryHandle shared_memory = IPC::InvalidPlatformFileForTransit();
250 uint32_t audio_buffer_length = 0;
252 int32_t result_code = result;
253 if (result_code == PP_OK) {
254 result_code = GetAudioConnectedHandles(resource, &socket_handle,
255 &shared_memory,
256 &audio_buffer_length);
259 // Send all the values, even on error. This simplifies some of our cleanup
260 // code since the handles will be in the other process and could be
261 // inconvenient to clean up. Our IPC code will automatically handle this for
262 // us, as long as the remote side always closes the handles it receives
263 // (in OnMsgNotifyAudioStreamCreated), even in the failure case.
264 SerializedHandle fd_wrapper(SerializedHandle::SOCKET, socket_handle);
265 SerializedHandle handle_wrapper(shared_memory, audio_buffer_length);
266 dispatcher()->Send(new PpapiMsg_PPBAudio_NotifyAudioStreamCreated(
267 API_ID_PPB_AUDIO, resource, result_code, fd_wrapper, handle_wrapper));
270 int32_t PPB_Audio_Proxy::GetAudioConnectedHandles(
271 const HostResource& resource,
272 IPC::PlatformFileForTransit* foreign_socket_handle,
273 base::SharedMemoryHandle* foreign_shared_memory_handle,
274 uint32_t* shared_memory_length) {
275 // Get the audio interface which will give us the handles.
276 EnterHostFromHostResource<PPB_Audio_API> enter(resource);
277 if (enter.failed())
278 return PP_ERROR_NOINTERFACE;
280 // Get the socket handle for signaling.
281 int32_t socket_handle;
282 int32_t result = enter.object()->GetSyncSocket(&socket_handle);
283 if (result != PP_OK)
284 return result;
286 // socket_handle doesn't belong to us: don't close it.
287 *foreign_socket_handle = dispatcher()->ShareHandleWithRemote(
288 IntToPlatformFile(socket_handle), false);
289 if (*foreign_socket_handle == IPC::InvalidPlatformFileForTransit())
290 return PP_ERROR_FAILED;
292 // Get the shared memory for the buffer.
293 int shared_memory_handle;
294 result = enter.object()->GetSharedMemory(&shared_memory_handle,
295 shared_memory_length);
296 if (result != PP_OK)
297 return result;
299 // shared_memory_handle doesn't belong to us: don't close it.
300 *foreign_shared_memory_handle = dispatcher()->ShareHandleWithRemote(
301 IntToPlatformFile(shared_memory_handle), false);
302 if (*foreign_shared_memory_handle == IPC::InvalidPlatformFileForTransit())
303 return PP_ERROR_FAILED;
305 return PP_OK;
307 #endif // !defined(OS_NACL)
309 // Processed in the plugin (message from host).
310 void PPB_Audio_Proxy::OnMsgNotifyAudioStreamCreated(
311 const HostResource& audio_id,
312 int32_t result_code,
313 SerializedHandle socket_handle,
314 SerializedHandle handle) {
315 CHECK(socket_handle.is_socket());
316 CHECK(handle.is_shmem());
317 EnterPluginFromHostResource<PPB_Audio_API> enter(audio_id);
318 if (enter.failed() || result_code != PP_OK) {
319 // The caller may still have given us these handles in the failure case.
320 // The easiest way to clean these up is to just put them in the objects
321 // and then close them. This failure case is not performance critical.
322 base::SyncSocket temp_socket(
323 IPC::PlatformFileForTransitToPlatformFile(socket_handle.descriptor()));
324 base::SharedMemory temp_mem(handle.shmem(), false);
325 } else {
326 EnterResourceNoLock<PPB_AudioConfig_API> config(
327 static_cast<Audio*>(enter.object())->GetCurrentConfig(), true);
328 static_cast<Audio*>(enter.object())->SetStreamInfo(
329 enter.resource()->pp_instance(), handle.shmem(), handle.size(),
330 IPC::PlatformFileForTransitToPlatformFile(socket_handle.descriptor()),
331 config.object()->GetSampleRate(),
332 config.object()->GetSampleFrameCount());
336 } // namespace proxy
337 } // namespace ppapi