Updates version of android sdk and api installed and creates ARM and x86 AVD's.
[chromium-blink-merge.git] / ppapi / proxy / ppb_audio_proxy.cc
blob7b2c015134f27462dbd04f82aef08157b8c320a8
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/proxy/ppb_audio_proxy.h"
7 #include "base/compiler_specific.h"
8 #include "base/threading/simple_thread.h"
9 #include "ppapi/c/pp_errors.h"
10 #include "ppapi/c/ppb_audio.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/platform_file.h"
19 #include "ppapi/shared_impl/ppapi_globals.h"
20 #include "ppapi/shared_impl/ppb_audio_shared.h"
21 #include "ppapi/shared_impl/resource.h"
22 #include "ppapi/thunk/ppb_audio_config_api.h"
23 #include "ppapi/thunk/enter.h"
24 #include "ppapi/thunk/resource_creation_api.h"
25 #include "ppapi/thunk/thunk.h"
27 using ppapi::IntToPlatformFile;
28 using ppapi::thunk::EnterResourceNoLock;
29 using ppapi::thunk::PPB_Audio_API;
30 using ppapi::thunk::PPB_AudioConfig_API;
32 namespace ppapi {
33 namespace proxy {
35 class Audio : public Resource, public PPB_Audio_Shared {
36 public:
37 Audio(const HostResource& audio_id,
38 PP_Resource config_id,
39 PPB_Audio_Callback callback,
40 void* user_data);
41 virtual ~Audio();
43 // Resource overrides.
44 virtual PPB_Audio_API* AsPPB_Audio_API();
46 // PPB_Audio_API implementation.
47 virtual PP_Resource GetCurrentConfig() OVERRIDE;
48 virtual PP_Bool StartPlayback() OVERRIDE;
49 virtual PP_Bool StopPlayback() OVERRIDE;
50 virtual int32_t OpenTrusted(
51 PP_Resource config_id,
52 scoped_refptr<TrackedCallback> create_callback) OVERRIDE;
53 virtual int32_t GetSyncSocket(int* sync_socket) OVERRIDE;
54 virtual int32_t GetSharedMemory(int* shm_handle, uint32_t* shm_size) OVERRIDE;
56 private:
57 // Owning reference to the current config object. This isn't actually used,
58 // we just dish it out as requested by the plugin.
59 PP_Resource config_;
61 DISALLOW_COPY_AND_ASSIGN(Audio);
64 Audio::Audio(const HostResource& audio_id,
65 PP_Resource config_id,
66 PPB_Audio_Callback callback,
67 void* user_data)
68 : Resource(OBJECT_IS_PROXY, audio_id),
69 config_(config_id) {
70 SetCallback(callback, user_data);
71 PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(config_);
74 Audio::~Audio() {
75 PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(config_);
78 PPB_Audio_API* Audio::AsPPB_Audio_API() {
79 return this;
82 PP_Resource Audio::GetCurrentConfig() {
83 // AddRef for the caller.
84 PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(config_);
85 return config_;
88 PP_Bool Audio::StartPlayback() {
89 if (playing())
90 return PP_TRUE;
91 SetStartPlaybackState();
92 PluginDispatcher::GetForResource(this)->Send(
93 new PpapiHostMsg_PPBAudio_StartOrStop(
94 API_ID_PPB_AUDIO, host_resource(), true));
95 return PP_TRUE;
98 PP_Bool Audio::StopPlayback() {
99 if (!playing())
100 return PP_TRUE;
101 PluginDispatcher::GetForResource(this)->Send(
102 new PpapiHostMsg_PPBAudio_StartOrStop(
103 API_ID_PPB_AUDIO, host_resource(), false));
104 SetStopPlaybackState();
105 return PP_TRUE;
108 int32_t Audio::OpenTrusted(PP_Resource config_id,
109 scoped_refptr<TrackedCallback> create_callback) {
110 return PP_ERROR_NOTSUPPORTED; // Don't proxy the trusted interface.
113 int32_t Audio::GetSyncSocket(int* sync_socket) {
114 return PP_ERROR_NOTSUPPORTED; // Don't proxy the trusted interface.
117 int32_t Audio::GetSharedMemory(int* shm_handle, uint32_t* shm_size) {
118 return PP_ERROR_NOTSUPPORTED; // Don't proxy the trusted interface.
121 PPB_Audio_Proxy::PPB_Audio_Proxy(Dispatcher* dispatcher)
122 : InterfaceProxy(dispatcher),
123 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
126 PPB_Audio_Proxy::~PPB_Audio_Proxy() {
129 // static
130 PP_Resource PPB_Audio_Proxy::CreateProxyResource(
131 PP_Instance instance_id,
132 PP_Resource config_id,
133 PPB_Audio_Callback audio_callback,
134 void* user_data) {
135 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id);
136 if (!dispatcher)
137 return 0;
139 EnterResourceNoLock<PPB_AudioConfig_API> config(config_id, true);
140 if (config.failed())
141 return 0;
143 if (!audio_callback)
144 return 0;
146 HostResource result;
147 dispatcher->Send(new PpapiHostMsg_PPBAudio_Create(
148 API_ID_PPB_AUDIO, instance_id,
149 config.object()->GetSampleRate(), config.object()->GetSampleFrameCount(),
150 &result));
151 if (result.is_null())
152 return 0;
154 return (new Audio(result, config_id,
155 audio_callback, user_data))->GetReference();
158 bool PPB_Audio_Proxy::OnMessageReceived(const IPC::Message& msg) {
159 bool handled = true;
160 IPC_BEGIN_MESSAGE_MAP(PPB_Audio_Proxy, msg)
161 // Don't build host side into NaCl IRT.
162 #if !defined(OS_NACL)
163 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBAudio_Create, OnMsgCreate)
164 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBAudio_StartOrStop,
165 OnMsgStartOrStop)
166 #endif
167 IPC_MESSAGE_HANDLER(PpapiMsg_PPBAudio_NotifyAudioStreamCreated,
168 OnMsgNotifyAudioStreamCreated)
169 IPC_MESSAGE_UNHANDLED(handled = false)
170 IPC_END_MESSAGE_MAP()
171 return handled;
174 #if !defined(OS_NACL)
175 void PPB_Audio_Proxy::OnMsgCreate(PP_Instance instance_id,
176 int32_t sample_rate,
177 uint32_t sample_frame_count,
178 HostResource* result) {
179 thunk::EnterResourceCreation resource_creation(instance_id);
180 if (resource_creation.failed())
181 return;
183 // Make the resource and get the API pointer to its trusted interface.
184 result->SetHostResource(
185 instance_id,
186 resource_creation.functions()->CreateAudioTrusted(instance_id));
187 if (result->is_null())
188 return;
190 // At this point, we've set the result resource, and this is a sync request.
191 // Anything below this point must issue the AudioChannelConnected callback
192 // to the browser. Since that's an async message, it will be issued back to
193 // the plugin after the Create function returns (which is good because it
194 // would be weird to get a connected message with a failure code for a
195 // resource you haven't finished creating yet).
197 // The ...ForceCallback class will help ensure the callback is always called.
198 // All error cases must call SetResult on this class.
199 EnterHostFromHostResourceForceCallback<PPB_Audio_API> enter(
200 *result, callback_factory_,
201 &PPB_Audio_Proxy::AudioChannelConnected, *result);
202 if (enter.failed())
203 return; // When enter fails, it will internally schedule the callback.
205 // Make an audio config object.
206 PP_Resource audio_config_res =
207 resource_creation.functions()->CreateAudioConfig(
208 instance_id, static_cast<PP_AudioSampleRate>(sample_rate),
209 sample_frame_count);
210 if (!audio_config_res) {
211 enter.SetResult(PP_ERROR_FAILED);
212 return;
215 // Initiate opening the audio object.
216 enter.SetResult(enter.object()->OpenTrusted(audio_config_res,
217 enter.callback()));
219 // Clean up the temporary audio config resource we made.
220 const PPB_Core* core = static_cast<const PPB_Core*>(
221 dispatcher()->local_get_interface()(PPB_CORE_INTERFACE));
222 core->ReleaseResource(audio_config_res);
225 void PPB_Audio_Proxy::OnMsgStartOrStop(const HostResource& audio_id,
226 bool play) {
227 EnterHostFromHostResource<PPB_Audio_API> enter(audio_id);
228 if (enter.failed())
229 return;
230 if (play)
231 enter.object()->StartPlayback();
232 else
233 enter.object()->StopPlayback();
236 void PPB_Audio_Proxy::AudioChannelConnected(
237 int32_t result,
238 const HostResource& resource) {
239 IPC::PlatformFileForTransit socket_handle =
240 IPC::InvalidPlatformFileForTransit();
241 base::SharedMemoryHandle shared_memory = IPC::InvalidPlatformFileForTransit();
242 uint32_t shared_memory_length = 0;
244 int32_t result_code = result;
245 if (result_code == PP_OK) {
246 result_code = GetAudioConnectedHandles(resource, &socket_handle,
247 &shared_memory,
248 &shared_memory_length);
251 // Send all the values, even on error. This simplifies some of our cleanup
252 // code since the handles will be in the other process and could be
253 // inconvenient to clean up. Our IPC code will automatically handle this for
254 // us, as long as the remote side always closes the handles it receives
255 // (in OnMsgNotifyAudioStreamCreated), even in the failure case.
256 dispatcher()->Send(new PpapiMsg_PPBAudio_NotifyAudioStreamCreated(
257 API_ID_PPB_AUDIO, resource, result_code, socket_handle,
258 shared_memory, shared_memory_length));
261 int32_t PPB_Audio_Proxy::GetAudioConnectedHandles(
262 const HostResource& resource,
263 IPC::PlatformFileForTransit* foreign_socket_handle,
264 base::SharedMemoryHandle* foreign_shared_memory_handle,
265 uint32_t* shared_memory_length) {
266 // Get the audio interface which will give us the handles.
267 EnterHostFromHostResource<PPB_Audio_API> enter(resource);
268 if (enter.failed())
269 return PP_ERROR_NOINTERFACE;
271 // Get the socket handle for signaling.
272 int32_t socket_handle;
273 int32_t result = enter.object()->GetSyncSocket(&socket_handle);
274 if (result != PP_OK)
275 return result;
277 // socket_handle doesn't belong to us: don't close it.
278 *foreign_socket_handle = dispatcher()->ShareHandleWithRemote(
279 IntToPlatformFile(socket_handle), false);
280 if (*foreign_socket_handle == IPC::InvalidPlatformFileForTransit())
281 return PP_ERROR_FAILED;
283 // Get the shared memory for the buffer.
284 int shared_memory_handle;
285 result = enter.object()->GetSharedMemory(&shared_memory_handle,
286 shared_memory_length);
287 if (result != PP_OK)
288 return result;
290 // shared_memory_handle doesn't belong to us: don't close it.
291 *foreign_shared_memory_handle = dispatcher()->ShareHandleWithRemote(
292 IntToPlatformFile(shared_memory_handle), false);
293 if (*foreign_shared_memory_handle == IPC::InvalidPlatformFileForTransit())
294 return PP_ERROR_FAILED;
296 return PP_OK;
298 #endif // !defined(OS_NACL)
300 // Processed in the plugin (message from host).
301 void PPB_Audio_Proxy::OnMsgNotifyAudioStreamCreated(
302 const HostResource& audio_id,
303 int32_t result_code,
304 IPC::PlatformFileForTransit socket_handle,
305 base::SharedMemoryHandle handle,
306 uint32_t length) {
307 EnterPluginFromHostResource<PPB_Audio_API> enter(audio_id);
308 if (enter.failed() || result_code != PP_OK) {
309 // The caller may still have given us these handles in the failure case.
310 // The easiest way to clean these up is to just put them in the objects
311 // and then close them. This failure case is not performance critical.
312 base::SyncSocket temp_socket(
313 IPC::PlatformFileForTransitToPlatformFile(socket_handle));
314 base::SharedMemory temp_mem(handle, false);
315 } else {
316 static_cast<Audio*>(enter.object())->SetStreamInfo(
317 enter.resource()->pp_instance(), handle, length,
318 IPC::PlatformFileForTransitToPlatformFile(socket_handle));
322 } // namespace proxy
323 } // namespace ppapi