|
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 "webkit/plugins/ppapi/ppb_audio_input_impl.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "ppapi/c/pp_completion_callback.h" | |
9 #include "ppapi/c/dev/ppb_audio_input_dev.h" | |
10 #include "ppapi/shared_impl/resource_tracker.h" | |
11 #include "ppapi/shared_impl/tracker_base.h" | |
12 #include "ppapi/thunk/enter.h" | |
13 #include "ppapi/thunk/thunk.h" | |
14 #include "webkit/plugins/ppapi/common.h" | |
15 #include "webkit/plugins/ppapi/resource_helper.h" | |
16 | |
17 using ppapi::thunk::EnterResourceNoLock; | |
18 using ppapi::thunk::PPB_AudioInput_API; | |
19 | |
20 namespace webkit { | |
21 namespace ppapi { | |
22 | |
23 // PPB_AudioInput_Impl --------------------------------------------------------- | |
24 | |
25 PPB_AudioInput_Impl::PPB_AudioInput_Impl(PP_Instance instance) | |
26 : Resource(instance), | |
27 audio_input_(NULL), | |
28 create_callback_pending_(false), | |
29 shared_memory_size_for_create_callback_(0) { | |
30 create_callback_ = PP_MakeCompletionCallback(NULL, NULL); | |
31 } | |
32 | |
33 PPB_AudioInput_Impl::~PPB_AudioInput_Impl() { | |
34 // Calling ShutDown() makes sure StreamCreated cannot be called anymore and | |
35 // releases the audio data associated with the pointer. Note however, that | |
36 // until ShutDown returns, StreamCreated may still be called. This will be | |
37 // OK since we'll just immediately clean up the data it stored later in this | |
38 // destructor. | |
39 if (audio_input_) { | |
40 audio_input_->ShutDown(); | |
41 audio_input_ = NULL; | |
42 } | |
43 | |
44 // If the completion callback hasn't fired yet, do so here | |
45 // with an error condition. | |
46 if (create_callback_pending_) { | |
47 PP_RunCompletionCallback(&create_callback_, PP_ERROR_ABORTED); | |
48 create_callback_pending_ = false; | |
49 } | |
50 } | |
51 | |
52 // static | |
53 PP_Resource PPB_AudioInput_Impl::Create(PP_Instance instance, | |
54 PPB_AudioInput_Callback audio_input_callback, | |
brettw
2011/10/12 19:33:13
Since these don't fit, move all the args to 4 spac
| |
55 void* user_data) { | |
56 scoped_refptr<PPB_AudioInput_Impl> | |
57 audio_input(new PPB_AudioInput_Impl(instance)); | |
brettw
2011/10/12 19:33:13
4 space.
| |
58 if (!audio_input->Init(audio_input_callback, user_data)) | |
59 return 0; | |
60 return audio_input->GetReference(); | |
61 } | |
62 | |
63 PPB_AudioInput_API* PPB_AudioInput_Impl::AsPPB_AudioInput_API() { | |
64 return this; | |
65 } | |
66 | |
67 bool PPB_AudioInput_Impl::Init(PPB_AudioInput_Callback callback, | |
68 void* user_data) { | |
69 if (!callback) | |
70 return false; | |
71 SetCallback(callback, user_data); | |
72 | |
73 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); | |
74 if (!plugin_delegate) | |
75 return false; | |
76 | |
77 // When the stream is created, we'll get called back on StreamCreated(). | |
78 CHECK(!audio_input_); | |
79 audio_input_ = plugin_delegate->CreateAudioInput(this); | |
80 return audio_input_ != NULL; | |
81 } | |
82 | |
83 PP_Bool PPB_AudioInput_Impl::StartCapture() { | |
84 if (!audio_input_) | |
85 return PP_FALSE; | |
86 if (capturing()) | |
87 return PP_TRUE; | |
88 SetStartCaptureState(); | |
89 return BoolToPPBool(audio_input_->StartCapture()); | |
90 } | |
91 | |
92 PP_Bool PPB_AudioInput_Impl::StopCapture() { | |
93 if (!audio_input_) | |
94 return PP_FALSE; | |
95 if (!capturing()) | |
96 return PP_TRUE; | |
97 if (!audio_input_->StopCapture()) | |
98 return PP_FALSE; | |
99 SetStopCaptureState(); | |
100 return PP_TRUE; | |
101 } | |
102 | |
103 int32_t PPB_AudioInput_Impl::OpenTrusted( | |
104 PP_CompletionCallback create_callback) { | |
brettw
2011/10/12 19:33:13
4 space indent.
| |
105 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); | |
106 if (!plugin_delegate) | |
107 return PP_ERROR_FAILED; | |
108 | |
109 // When the stream is created, we'll get called back on StreamCreated(). | |
110 DCHECK(!audio_input_); | |
111 audio_input_ = plugin_delegate->CreateAudioInput(this); | |
112 | |
113 if (!audio_input_) | |
114 return PP_ERROR_FAILED; | |
115 | |
116 // At this point, we are guaranteeing ownership of the completion | |
117 // callback. Audio promises to fire the completion callback | |
118 // once and only once. | |
119 create_callback_ = create_callback; | |
120 create_callback_pending_ = true; | |
121 return PP_OK_COMPLETIONPENDING; | |
122 } | |
123 | |
124 int32_t PPB_AudioInput_Impl::GetSyncSocket(int* sync_socket) { | |
brettw
2011/10/12 20:39:34
These 3 functions are exactly copied from audio ou
viettrungluu
2011/11/02 21:03:07
What Brett said here still applies, and his respon
| |
125 if (socket_for_create_callback_.get()) { | |
126 #if defined(OS_POSIX) | |
127 *sync_socket = socket_for_create_callback_->handle(); | |
128 #elif defined(OS_WIN) | |
129 *sync_socket = reinterpret_cast<int>(socket_for_create_callback_->handle()); | |
130 #else | |
131 #error "Platform not supported." | |
132 #endif | |
133 return PP_OK; | |
134 } | |
135 return PP_ERROR_FAILED; | |
136 } | |
137 | |
138 int32_t PPB_AudioInput_Impl::GetSharedMemory(int* shm_handle, | |
139 uint32_t* shm_size) { | |
140 if (shared_memory_for_create_callback_.get()) { | |
141 #if defined(OS_POSIX) | |
142 *shm_handle = shared_memory_for_create_callback_->handle().fd; | |
143 #elif defined(OS_WIN) | |
144 *shm_handle = reinterpret_cast<int>( | |
145 shared_memory_for_create_callback_->handle()); | |
146 #else | |
147 #error "Platform not supported." | |
148 #endif | |
149 *shm_size = shared_memory_size_for_create_callback_; | |
150 return PP_OK; | |
151 } | |
152 return PP_ERROR_FAILED; | |
153 } | |
154 | |
155 void PPB_AudioInput_Impl::StreamCreated( | |
156 base::SharedMemoryHandle shared_memory_handle, | |
157 size_t shared_memory_size, | |
158 base::SyncSocket::Handle socket_handle) { | |
159 if (create_callback_pending_) { | |
160 // Trusted side of proxy can specify a callback to recieve handles. In | |
161 // this case we don't need to map any data or start the thread since it | |
162 // will be handled by the proxy. | |
163 shared_memory_for_create_callback_.reset( | |
164 new base::SharedMemory(shared_memory_handle, false)); | |
165 shared_memory_size_for_create_callback_ = shared_memory_size; | |
166 socket_for_create_callback_.reset(new base::SyncSocket(socket_handle)); | |
167 | |
168 PP_RunCompletionCallback(&create_callback_, 0); | |
169 create_callback_pending_ = false; | |
170 | |
171 // It might be nice to close the handles here to free up some system | |
172 // resources, but we can't since there's a race condition. The handles must | |
173 // be valid until they're sent over IPC, which is done from the I/O thread | |
174 // which will often get done after this code executes. We could do | |
175 // something more elaborate like an ACK from the plugin or post a task to | |
176 // the I/O thread and back, but this extra complexity doesn't seem worth it | |
177 // just to clean up these handles faster. | |
178 } else { | |
179 SetStreamInfo(shared_memory_handle, shared_memory_size, socket_handle); | |
180 } | |
181 } | |
182 | |
183 } // namespace ppapi | |
184 } // namespace webkit | |
OLD | NEW |