Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(115)

Side by Side Diff: ppapi/proxy/ppb_audio_input_proxy.cc

Issue 8138008: Implementation of ppapi audio. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:executable
+ *
OLDNEW
(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/proxy/ppb_audio_input_proxy.h"
6
7 #include <vector>
8
9 #include "base/logging.h"
10 #include "build/build_config.h"
11 #include "ppapi/c/pp_errors.h"
12 #include "ppapi/c/pp_resource.h"
13 #include "ppapi/c/ppb_core.h"
14 #include "ppapi/c/ppb_var.h"
15 #include "ppapi/c/dev/ppb_audio_input_dev.h"
16 #include "ppapi/proxy/enter_proxy.h"
17 #include "ppapi/proxy/interface_id.h"
18 #include "ppapi/proxy/plugin_dispatcher.h"
19 #include "ppapi/proxy/ppapi_messages.h"
20 #include "ppapi/shared_impl/audio_input_impl.h"
21 #include "ppapi/shared_impl/resource.h"
22 #include "ppapi/thunk/ppb_audio_input_api.h"
23 #include "ppapi/thunk/enter.h"
24 #include "ppapi/thunk/resource_creation_api.h"
25 #include "ppapi/thunk/thunk.h"
26
27 using ppapi::thunk::EnterResourceNoLock;
28 using ppapi::thunk::PPB_Buffer_API;
29 using ppapi::thunk::PPB_BufferTrusted_API;
30 using ppapi::thunk::PPB_AudioInput_API;
31
32 namespace ppapi {
33 namespace proxy {
34
35 class AudioInput : public Resource, public AudioInputImpl {
36 public:
37 AudioInput(const HostResource& audio_input_id,
38 PPB_AudioInput_Callback callback,
39 void* user_data);
40 virtual ~AudioInput();
41
42 // Resource overrides.
43 virtual PPB_AudioInput_API* AsPPB_AudioInput_API();
44
45 // PPB_Audio_API implementation.
46 virtual PP_Bool StartCapture() OVERRIDE;
47 virtual PP_Bool StopCapture() OVERRIDE;
48
49 virtual int32_t OpenTrusted(PP_CompletionCallback create_callback) OVERRIDE;
50 virtual int32_t GetSyncSocket(int* sync_socket) OVERRIDE;
51 virtual int32_t GetSharedMemory(int* shm_handle, uint32_t* shm_size) OVERRIDE;
52
53 private:
54 DISALLOW_COPY_AND_ASSIGN(AudioInput);
55 };
56
57 AudioInput::AudioInput(const HostResource& audio_input_id,
58 PPB_AudioInput_Callback callback,
59 void* user_data)
60 : Resource(audio_input_id) {
61 SetCallback(callback, user_data);
62 }
63
64 AudioInput::~AudioInput() {
65 }
66
67 PPB_AudioInput_API* AudioInput::AsPPB_AudioInput_API() {
68 return this;
69 }
70
71
72 PP_Bool AudioInput::StartCapture() {
73 if (capturing())
74 return PP_TRUE;
75 SetStartCaptureState();
76 PluginDispatcher::GetForResource(this)->Send(
77 new PpapiHostMsg_PPBAudioInput_StartOrStop(
78 INTERFACE_ID_PPB_AUDIO_INPUT_DEV, host_resource(), true));
79 return PP_TRUE;
80 }
81
82 PP_Bool AudioInput::StopCapture() {
83 if (!capturing())
84 return PP_TRUE;
85 PluginDispatcher::GetForResource(this)->Send(
86 new PpapiHostMsg_PPBAudioInput_StartOrStop(
87 INTERFACE_ID_PPB_AUDIO_INPUT_DEV, host_resource(), false));
88 SetStopCaptureState();
89 return PP_TRUE;
90 }
91
92 int32_t AudioInput::OpenTrusted(PP_CompletionCallback create_callback) {
93 return PP_ERROR_NOTSUPPORTED; // Don't proxy the trusted interface.
94 }
95
96 int32_t AudioInput::GetSyncSocket(int* sync_socket) {
97 return PP_ERROR_NOTSUPPORTED; // Don't proxy the trusted interface.
98 }
99
100 int32_t AudioInput::GetSharedMemory(int* shm_handle, uint32_t* shm_size) {
101 return PP_ERROR_NOTSUPPORTED; // Don't proxy the trusted interface.
102 }
103
104
105 namespace {
106
107 base::PlatformFile IntToPlatformFile(int32_t handle) {
108 // TODO(piman/brettw): Change trusted interface to return a PP_FileHandle,
109 // those casts are ugly.
110 #if defined(OS_WIN)
111 return reinterpret_cast<HANDLE>(static_cast<intptr_t>(handle));
112 #elif defined(OS_POSIX)
113 return handle;
114 #else
115 #error Not implemented.
116 #endif
117 }
118
119 } // namespace
120
121
122
123 PPB_AudioInput_Proxy::PPB_AudioInput_Proxy(Dispatcher* dispatcher)
124 : InterfaceProxy(dispatcher),
125 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
126 }
127
128 PPB_AudioInput_Proxy::~PPB_AudioInput_Proxy() {
129 }
130
131 // static
132 PP_Resource PPB_AudioInput_Proxy::CreateProxyResource(
133 PP_Instance instance_id,
134 PPB_AudioInput_Callback audio_input_callback,
135 void* user_data) {
136
137 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id);
138 if (!dispatcher)
139 return 0;
140
141 HostResource result;
142 dispatcher->Send(new PpapiHostMsg_PPBAudioInput_Create(
143 INTERFACE_ID_PPB_AUDIO_INPUT_DEV, instance_id, &result));
144 if (result.is_null())
145 return 0;
146
147 return (new AudioInput(result, audio_input_callback, user_data))->GetReference ();
148 }
149
150 bool PPB_AudioInput_Proxy::OnMessageReceived(const IPC::Message& msg) {
151 bool handled = true;
152 IPC_BEGIN_MESSAGE_MAP(PPB_AudioInput_Proxy, msg)
153 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBAudioInput_Create, OnMsgCreate)
154 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBAudioInput_StartOrStop,
155 OnMsgStartOrStop)
156 IPC_MESSAGE_HANDLER(PpapiMsg_PPBAudioInput_NotifyAudioStreamCreated,
157 OnMsgNotifyAudioStreamCreated)
158 IPC_MESSAGE_UNHANDLED(handled = false)
159 IPC_END_MESSAGE_MAP()
160 // TODO(brettw) handle bad messages!
161
162 return handled;
163 }
164
165 void PPB_AudioInput_Proxy::OnMsgCreate(PP_Instance instance_id,
166 HostResource* result) {
167 thunk::EnterFunction<thunk::ResourceCreationAPI> resource_creation(
168 instance_id, true);
169 if (resource_creation.failed())
170 return;
171
172 // Make the resource and get the API pointer to its trusted interface.
173 //pbbb
174 // will add audio input trusted
175 /* result->SetHostResource(
176 instance_id,
177 resource_creation.functions()->CreateAudioInputTrusted(instance_id));
178 if (result->is_null())
179 return;
180 */
181
182 // At this point, we've set the result resource, and this is a sync request.
183 // Anything below this point must issue the AudioInputChannelConnected callbac k
184 // to the browser. Since that's an async message, it will be issued back to
185 // the plugin after the Create function returns (which is good because it
186 // would be weird to get a connected message with a failure code for a
187 // resource you haven't finished creating yet).
188 //
189 // The ...ForceCallback class will help ensure the callback is always called.
190 // All error cases must call SetResult on this class.
191
192 EnterHostFromHostResourceForceCallback<PPB_AudioInput_API> enter(
193 *result, callback_factory_,
194 &PPB_AudioInput_Proxy::AudioInputChannelConnected, *result);
195 if (enter.failed())
196 return; // When enter fails, it will internally schedule the callback.
197
198 // Initiate opening the audio object.
199 //pbbb check
200 // enter.SetResult(enter.object()->OpenTrusted(
201 // enter.callback()));
202
203 }
204
205 void PPB_AudioInput_Proxy::OnMsgStartOrStop(
206 const HostResource& resource,
207 bool capture)
208 {
209 EnterHostFromHostResource<PPB_AudioInput_API> enter(resource);
210 if (enter.failed())
211 return;
212 if (capture)
213 enter.object()->StartCapture();
214 else
215 enter.object()->StopCapture();
216 }
217
218 // Processed in the plugin (message from host).
219 void PPB_AudioInput_Proxy::OnMsgNotifyAudioStreamCreated(
220 const HostResource& audio_id,
221 int32_t result_code,
222 IPC::PlatformFileForTransit socket_handle,
223 base::SharedMemoryHandle handle,
224 uint32_t length) {
225 EnterPluginFromHostResource<PPB_AudioInput_API> enter(audio_id);
226 if (enter.failed() || result_code != PP_OK) {
227 // The caller may still have given us these handles in the failure case.
228 // The easiest way to clean these up is to just put them in the objects
229 // and then close them. This failure case is not performance critical.
230 base::SyncSocket temp_socket(
231 IPC::PlatformFileForTransitToPlatformFile(socket_handle));
232 base::SharedMemory temp_mem(handle, false);
233 } else {
234 static_cast<AudioInput*>(enter.object())->SetStreamInfo(
235 handle, length,
236 IPC::PlatformFileForTransitToPlatformFile(socket_handle));
237 }
238 }
239
240 void PPB_AudioInput_Proxy::AudioInputChannelConnected(
241 int32_t result,
242 const HostResource& resource) {
243 IPC::PlatformFileForTransit socket_handle =
244 IPC::InvalidPlatformFileForTransit();
245 base::SharedMemoryHandle shared_memory = IPC::InvalidPlatformFileForTransit();
246 uint32_t shared_memory_length = 0;
247
248 int32_t result_code = result;
249 if (result_code == PP_OK) {
250 result_code = GetAudioInputConnectedHandles(resource, &socket_handle,
251 &shared_memory,
252 &shared_memory_length);
253 }
254
255 // Send all the values, even on error. This simplifies some of our cleanup
256 // code since the handles will be in the other process and could be
257 // inconvenient to clean up. Our IPC code will automatically handle this for
258 // us, as long as the remote side always closes the handles it receives
259 // (in OnMsgNotifyAudioStreamCreated), even in the failure case.
260 dispatcher()->Send(new PpapiMsg_PPBAudioInput_NotifyAudioStreamCreated(
261 INTERFACE_ID_PPB_AUDIO_INPUT_DEV, resource, result_code, socket_handle,
262 shared_memory, shared_memory_length));
263 }
264
265 int32_t PPB_AudioInput_Proxy::GetAudioInputConnectedHandles(
266 const HostResource& resource,
267 IPC::PlatformFileForTransit* foreign_socket_handle,
268 base::SharedMemoryHandle* foreign_shared_memory_handle,
269 uint32_t* shared_memory_length) {
270 // Get the audio interface which will give us the handles.
271 EnterHostFromHostResource<PPB_AudioInput_API> enter(resource);
272 if (enter.failed())
273 return PP_ERROR_NOINTERFACE;
274
275 // Get the socket handle for signaling.
276 int32_t socket_handle;
277 int32_t result = enter.object()->GetSyncSocket(&socket_handle);
278 if (result != PP_OK)
279 return result;
280
281 // socket_handle doesn't belong to us: don't close it.
282 *foreign_socket_handle = dispatcher()->ShareHandleWithRemote(
283 IntToPlatformFile(socket_handle), false);
284 if (*foreign_socket_handle == IPC::InvalidPlatformFileForTransit())
285 return PP_ERROR_FAILED;
286
287 // Get the shared memory for the buffer.
288 int shared_memory_handle;
289 result = enter.object()->GetSharedMemory(&shared_memory_handle,
290 shared_memory_length);
291 if (result != PP_OK)
292 return result;
293
294 // shared_memory_handle doesn't belong to us: don't close it.
295 *foreign_shared_memory_handle = dispatcher()->ShareHandleWithRemote(
296 IntToPlatformFile(shared_memory_handle), false);
297 if (*foreign_shared_memory_handle == IPC::InvalidPlatformFileForTransit())
298 return PP_ERROR_FAILED;
299
300 return PP_OK;
301 }
302
303
304 } // namespace proxy
305 } // namespace ppapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698