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/c/pp_completion_callback.h"
6 #include "webkit/plugins/ppapi/audio_helper.h"
7 #include "webkit/plugins/ppapi/common.h"
8 #include "webkit/plugins/ppapi/resource_helper.h"
10 using ppapi::TrackedCallback
;
15 // AudioHelper -----------------------------------------------------------------
17 AudioHelper::AudioHelper() : shared_memory_size_for_create_callback_(0) {
20 AudioHelper::~AudioHelper() {
23 int32_t AudioHelper::GetSyncSocketImpl(int* sync_socket
) {
24 if (socket_for_create_callback_
.get()) {
26 *sync_socket
= socket_for_create_callback_
->handle();
28 *sync_socket
= reinterpret_cast<int>(socket_for_create_callback_
->handle());
30 #error "Platform not supported."
34 return PP_ERROR_FAILED
;
37 int32_t AudioHelper::GetSharedMemoryImpl(int* shm_handle
, uint32_t* shm_size
) {
38 if (shared_memory_for_create_callback_
.get()) {
40 *shm_handle
= shared_memory_for_create_callback_
->handle().fd
;
42 *shm_handle
= reinterpret_cast<int>(
43 shared_memory_for_create_callback_
->handle());
45 #error "Platform not supported."
47 *shm_size
= shared_memory_size_for_create_callback_
;
50 return PP_ERROR_FAILED
;
53 void AudioHelper::StreamCreated(
54 base::SharedMemoryHandle shared_memory_handle
,
55 size_t shared_memory_size
,
56 base::SyncSocket::Handle socket_handle
) {
57 if (TrackedCallback::IsPending(create_callback_
)) {
58 // Trusted side of proxy can specify a callback to recieve handles. In
59 // this case we don't need to map any data or start the thread since it
60 // will be handled by the proxy.
61 shared_memory_for_create_callback_
.reset(
62 new base::SharedMemory(shared_memory_handle
, false));
63 shared_memory_size_for_create_callback_
= shared_memory_size
;
64 socket_for_create_callback_
.reset(new base::SyncSocket(socket_handle
));
66 create_callback_
->Run(PP_OK
);
68 // It might be nice to close the handles here to free up some system
69 // resources, but we can't since there's a race condition. The handles must
70 // be valid until they're sent over IPC, which is done from the I/O thread
71 // which will often get done after this code executes. We could do
72 // something more elaborate like an ACK from the plugin or post a task to
73 // the I/O thread and back, but this extra complexity doesn't seem worth it
74 // just to clean up these handles faster.
76 OnSetStreamInfo(shared_memory_handle
, shared_memory_size
, socket_handle
);
80 void AudioHelper::SetCreateCallback(
81 scoped_refptr
< ::ppapi::TrackedCallback
> create_callback
) {
82 DCHECK(!TrackedCallback::IsPending(create_callback_
));
83 create_callback_
= create_callback
;