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

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

Powered by Google App Engine
This is Rietveld 408576698