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/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" | |
9 | |
10 namespace webkit { | |
11 namespace ppapi { | |
12 | |
13 // AudioHelper ----------------------------------------------------------------- | |
14 | |
15 AudioHelper::AudioHelper() | |
16 : create_callback_pending_(false), | |
17 shared_memory_size_for_create_callback_(0) { | |
18 create_callback_ = PP_MakeCompletionCallback(NULL, NULL); | |
19 } | |
20 | |
21 AudioHelper::~AudioHelper() { | |
22 // If the completion callback hasn't fired yet, do so here | |
23 // with an error condition. | |
24 if (create_callback_pending_) { | |
25 PP_RunCompletionCallback(&create_callback_, PP_ERROR_ABORTED); | |
26 create_callback_pending_ = false; | |
27 } | |
28 } | |
29 | |
30 int32_t AudioHelper::GetSyncSocketImpl(int* sync_socket) { | |
31 if (socket_for_create_callback_.get()) { | |
32 #if defined(OS_POSIX) | |
33 *sync_socket = socket_for_create_callback_->handle(); | |
34 #elif defined(OS_WIN) | |
35 *sync_socket = reinterpret_cast<int>(socket_for_create_callback_->handle()); | |
36 #else | |
37 #error "Platform not supported." | |
38 #endif | |
39 return PP_OK; | |
40 } | |
41 return PP_ERROR_FAILED; | |
42 } | |
43 | |
44 int32_t AudioHelper::GetSharedMemoryImpl(int* shm_handle, uint32_t* shm_size) { | |
45 if (shared_memory_for_create_callback_.get()) { | |
46 #if defined(OS_POSIX) | |
47 *shm_handle = shared_memory_for_create_callback_->handle().fd; | |
48 #elif defined(OS_WIN) | |
49 *shm_handle = reinterpret_cast<int>( | |
50 shared_memory_for_create_callback_->handle()); | |
51 #else | |
52 #error "Platform not supported." | |
53 #endif | |
54 *shm_size = shared_memory_size_for_create_callback_; | |
55 return PP_OK; | |
56 } | |
57 return PP_ERROR_FAILED; | |
58 } | |
59 | |
60 void AudioHelper::StreamCreated( | |
61 base::SharedMemoryHandle shared_memory_handle, | |
62 size_t shared_memory_size, | |
63 base::SyncSocket::Handle socket_handle) { | |
64 if (create_callback_pending_) { | |
65 // Trusted side of proxy can specify a callback to recieve handles. In | |
66 // this case we don't need to map any data or start the thread since it | |
67 // will be handled by the proxy. | |
68 shared_memory_for_create_callback_.reset( | |
69 new base::SharedMemory(shared_memory_handle, false)); | |
70 shared_memory_size_for_create_callback_ = shared_memory_size; | |
71 socket_for_create_callback_.reset(new base::SyncSocket(socket_handle)); | |
72 | |
73 PP_RunCompletionCallback(&create_callback_, 0); | |
74 create_callback_pending_ = false; | |
75 | |
76 // It might be nice to close the handles here to free up some system | |
77 // resources, but we can't since there's a race condition. The handles must | |
78 // be valid until they're sent over IPC, which is done from the I/O thread | |
79 // which will often get done after this code executes. We could do | |
80 // something more elaborate like an ACK from the plugin or post a task to | |
81 // the I/O thread and back, but this extra complexity doesn't seem worth it | |
82 // just to clean up these handles faster. | |
83 } else { | |
84 OnSetStreamInfo(shared_memory_handle, shared_memory_size, socket_handle); | |
85 } | |
86 } | |
87 | |
88 void AudioHelper::SetCallbackInfo(bool create_callback_pending, | |
89 PP_CompletionCallback create_callback) { | |
90 create_callback_pending_ = create_callback_pending; | |
91 create_callback_ = create_callback; | |
92 } | |
93 | |
94 } // namespace ppapi | |
95 } // namespace webkit | |
OLD | NEW |