zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
opensl.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /* This is an OpenAL backend for Android using the native audio APIs based on
18  * OpenSL ES 1.0.1. It is based on source code for the native-audio sample app
19  * bundled with NDK.
20  */
21 
22 #include "config.h"
23 
24 #include <stdlib.h>
25 
26 #include "alMain.h"
27 #include "alu.h"
28 
29 
30 #include <SLES/OpenSLES.h>
31 #if 1
32 #include <SLES/OpenSLES_Android.h>
33 #else
34 extern SLAPIENTRY const SLInterfaceID SL_IID_ANDROIDSIMPLEBUFFERQUEUE;
35 
36 struct SLAndroidSimpleBufferQueueItf_;
37 typedef const struct SLAndroidSimpleBufferQueueItf_ * const * SLAndroidSimpleBufferQueueItf;
38 
39 typedef void (*slAndroidSimpleBufferQueueCallback)(SLAndroidSimpleBufferQueueItf caller, void *pContext);
40 
41 typedef struct SLAndroidSimpleBufferQueueState_ {
42  SLuint32 count;
43  SLuint32 index;
44 } SLAndroidSimpleBufferQueueState;
45 
46 
47 struct SLAndroidSimpleBufferQueueItf_ {
48  SLresult (*Enqueue) (
49  SLAndroidSimpleBufferQueueItf self,
50  const void *pBuffer,
51  SLuint32 size
52  );
53  SLresult (*Clear) (
54  SLAndroidSimpleBufferQueueItf self
55  );
56  SLresult (*GetState) (
57  SLAndroidSimpleBufferQueueItf self,
58  SLAndroidSimpleBufferQueueState *pState
59  );
60  SLresult (*RegisterCallback) (
61  SLAndroidSimpleBufferQueueItf self,
62  slAndroidSimpleBufferQueueCallback callback,
63  void* pContext
64  );
65 };
66 
67 #define SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE ((SLuint32) 0x800007BD)
68 
69 typedef struct SLDataLocator_AndroidSimpleBufferQueue {
70  SLuint32 locatorType;
71  SLuint32 numBuffers;
72 } SLDataLocator_AndroidSimpleBufferQueue;
73 
74 #endif
75 
76 /* Helper macros */
77 #define SLObjectItf_Realize(a,b) ((*(a))->Realize((a),(b)))
78 #define SLObjectItf_GetInterface(a,b,c) ((*(a))->GetInterface((a),(b),(c)))
79 #define SLObjectItf_Destroy(a) ((*(a))->Destroy((a)))
80 
81 #define SLEngineItf_CreateOutputMix(a,b,c,d,e) ((*(a))->CreateOutputMix((a),(b),(c),(d),(e)))
82 #define SLEngineItf_CreateAudioPlayer(a,b,c,d,e,f,g) ((*(a))->CreateAudioPlayer((a),(b),(c),(d),(e),(f),(g)))
83 
84 #define SLPlayItf_SetPlayState(a,b) ((*(a))->SetPlayState((a),(b)))
85 
86 
87 typedef struct {
88  /* engine interfaces */
89  SLObjectItf engineObject;
90  SLEngineItf engine;
91 
92  /* output mix interfaces */
93  SLObjectItf outputMix;
94 
95  /* buffer queue player interfaces */
96  SLObjectItf bufferQueueObject;
97 
98  void *buffer;
99  ALuint bufferSize;
100 
101  ALuint frameSize;
102 } osl_data;
103 
104 
105 static const ALCchar opensl_device[] = "OpenSL";
106 
107 
108 static SLuint32 GetChannelMask(enum DevFmtChannels chans)
109 {
110  switch(chans)
111  {
112  case DevFmtMono: return SL_SPEAKER_FRONT_CENTER;
113  case DevFmtStereo: return SL_SPEAKER_FRONT_LEFT|SL_SPEAKER_FRONT_RIGHT;
114  case DevFmtQuad: return SL_SPEAKER_FRONT_LEFT|SL_SPEAKER_FRONT_RIGHT|
115  SL_SPEAKER_BACK_LEFT|SL_SPEAKER_BACK_RIGHT;
116  case DevFmtX51: return SL_SPEAKER_FRONT_LEFT|SL_SPEAKER_FRONT_RIGHT|
117  SL_SPEAKER_FRONT_CENTER|SL_SPEAKER_LOW_FREQUENCY|
118  SL_SPEAKER_BACK_LEFT|SL_SPEAKER_BACK_RIGHT;
119  case DevFmtX61: return SL_SPEAKER_FRONT_LEFT|SL_SPEAKER_FRONT_RIGHT|
120  SL_SPEAKER_FRONT_CENTER|SL_SPEAKER_LOW_FREQUENCY|
121  SL_SPEAKER_BACK_CENTER|
122  SL_SPEAKER_SIDE_LEFT|SL_SPEAKER_SIDE_RIGHT;
123  case DevFmtX71: return SL_SPEAKER_FRONT_LEFT|SL_SPEAKER_FRONT_RIGHT|
124  SL_SPEAKER_FRONT_CENTER|SL_SPEAKER_LOW_FREQUENCY|
125  SL_SPEAKER_BACK_LEFT|SL_SPEAKER_BACK_RIGHT|
126  SL_SPEAKER_SIDE_LEFT|SL_SPEAKER_SIDE_RIGHT;
127  case DevFmtX51Side: return SL_SPEAKER_FRONT_LEFT|SL_SPEAKER_FRONT_RIGHT|
128  SL_SPEAKER_FRONT_CENTER|SL_SPEAKER_LOW_FREQUENCY|
129  SL_SPEAKER_SIDE_LEFT|SL_SPEAKER_SIDE_RIGHT;
130  }
131  return 0;
132 }
133 
134 static const char *res_str(SLresult result)
135 {
136  switch(result)
137  {
138  case SL_RESULT_SUCCESS: return "Success";
139  case SL_RESULT_PRECONDITIONS_VIOLATED: return "Preconditions violated";
140  case SL_RESULT_PARAMETER_INVALID: return "Parameter invalid";
141  case SL_RESULT_MEMORY_FAILURE: return "Memory failure";
142  case SL_RESULT_RESOURCE_ERROR: return "Resource error";
143  case SL_RESULT_RESOURCE_LOST: return "Resource lost";
144  case SL_RESULT_IO_ERROR: return "I/O error";
145  case SL_RESULT_BUFFER_INSUFFICIENT: return "Buffer insufficient";
146  case SL_RESULT_CONTENT_CORRUPTED: return "Content corrupted";
147  case SL_RESULT_CONTENT_UNSUPPORTED: return "Content unsupported";
148  case SL_RESULT_CONTENT_NOT_FOUND: return "Content not found";
149  case SL_RESULT_PERMISSION_DENIED: return "Permission denied";
150  case SL_RESULT_FEATURE_UNSUPPORTED: return "Feature unsupported";
151  case SL_RESULT_INTERNAL_ERROR: return "Internal error";
152  case SL_RESULT_UNKNOWN_ERROR: return "Unknown error";
153  case SL_RESULT_OPERATION_ABORTED: return "Operation aborted";
154  case SL_RESULT_CONTROL_LOST: return "Control lost";
155  case SL_RESULT_READONLY: return "ReadOnly";
156  case SL_RESULT_ENGINEOPTION_UNSUPPORTED: return "Engine option unsupported";
157  case SL_RESULT_SOURCE_SINK_INCOMPATIBLE: return "Source/Sink incompatible";
158  }
159  return "Unknown error code";
160 }
161 
162 #define PRINTERR(x, s) do { \
163  if((x) != SL_RESULT_SUCCESS) \
164  ERR("%s: %s\n", (s), res_str((x))); \
165 } while(0)
166 
167 /* this callback handler is called every time a buffer finishes playing */
168 static void opensl_callback(SLAndroidSimpleBufferQueueItf bq, void *context)
169 {
170  ALCdevice *Device = context;
171  osl_data *data = Device->ExtraData;
172  SLresult result;
173 
174  aluMixData(Device, data->buffer, data->bufferSize/data->frameSize);
175 
176  result = (*bq)->Enqueue(bq, data->buffer, data->bufferSize);
177  PRINTERR(result, "bq->Enqueue");
178 }
179 
180 
181 static ALCenum opensl_open_playback(ALCdevice *Device, const ALCchar *deviceName)
182 {
183  osl_data *data = NULL;
184  SLresult result;
185 
186  if(!deviceName)
187  deviceName = opensl_device;
188  else if(strcmp(deviceName, opensl_device) != 0)
189  return ALC_INVALID_VALUE;
190 
191  data = calloc(1, sizeof(*data));
192  if(!data)
193  return ALC_OUT_OF_MEMORY;
194 
195  // create engine
196  result = slCreateEngine(&data->engineObject, 0, NULL, 0, NULL, NULL);
197  PRINTERR(result, "slCreateEngine");
198  if(SL_RESULT_SUCCESS == result)
199  {
200  result = SLObjectItf_Realize(data->engineObject, SL_BOOLEAN_FALSE);
201  PRINTERR(result, "engine->Realize");
202  }
203  if(SL_RESULT_SUCCESS == result)
204  {
205  result = SLObjectItf_GetInterface(data->engineObject, SL_IID_ENGINE, &data->engine);
206  PRINTERR(result, "engine->GetInterface");
207  }
208  if(SL_RESULT_SUCCESS == result)
209  {
210  result = SLEngineItf_CreateOutputMix(data->engine, &data->outputMix, 0, NULL, NULL);
211  PRINTERR(result, "engine->CreateOutputMix");
212  }
213  if(SL_RESULT_SUCCESS == result)
214  {
215  result = SLObjectItf_Realize(data->outputMix, SL_BOOLEAN_FALSE);
216  PRINTERR(result, "outputMix->Realize");
217  }
218 
219  if(SL_RESULT_SUCCESS != result)
220  {
221  if(data->outputMix != NULL)
222  SLObjectItf_Destroy(data->outputMix);
223  data->outputMix = NULL;
224 
225  if(data->engineObject != NULL)
226  SLObjectItf_Destroy(data->engineObject);
227  data->engineObject = NULL;
228  data->engine = NULL;
229 
230  free(data);
231  return ALC_INVALID_VALUE;
232  }
233 
234  Device->DeviceName = strdup(deviceName);
235  Device->ExtraData = data;
236 
237  return ALC_NO_ERROR;
238 }
239 
240 
241 static void opensl_close_playback(ALCdevice *Device)
242 {
243  osl_data *data = Device->ExtraData;
244 
245  if(data->bufferQueueObject != NULL)
246  SLObjectItf_Destroy(data->bufferQueueObject);
247  data->bufferQueueObject = NULL;
248 
249  SLObjectItf_Destroy(data->outputMix);
250  data->outputMix = NULL;
251 
252  SLObjectItf_Destroy(data->engineObject);
253  data->engineObject = NULL;
254  data->engine = NULL;
255 
256  free(data);
257  Device->ExtraData = NULL;
258 }
259 
261 {
262  osl_data *data = Device->ExtraData;
263  SLDataLocator_AndroidSimpleBufferQueue loc_bufq;
264  SLDataLocator_OutputMix loc_outmix;
265  SLDataFormat_PCM format_pcm;
266  SLDataSource audioSrc;
267  SLDataSink audioSnk;
268  SLInterfaceID id;
269  SLboolean req;
270  SLresult result;
271 
272 
273  Device->UpdateSize = (ALuint64)Device->UpdateSize * 44100 / Device->Frequency;
274  Device->UpdateSize = Device->UpdateSize * Device->NumUpdates / 2;
275  Device->NumUpdates = 2;
276 
277  Device->Frequency = 44100;
278  Device->FmtChans = DevFmtStereo;
279  Device->FmtType = DevFmtShort;
280 
282 
283 
284  id = SL_IID_ANDROIDSIMPLEBUFFERQUEUE;
285  req = SL_BOOLEAN_TRUE;
286 
287  loc_bufq.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE;
288  loc_bufq.numBuffers = Device->NumUpdates;
289 
290  format_pcm.formatType = SL_DATAFORMAT_PCM;
291  format_pcm.numChannels = ChannelsFromDevFmt(Device->FmtChans);
292  format_pcm.samplesPerSec = Device->Frequency * 1000;
293  format_pcm.bitsPerSample = BytesFromDevFmt(Device->FmtType) * 8;
294  format_pcm.containerSize = format_pcm.bitsPerSample;
295  format_pcm.channelMask = GetChannelMask(Device->FmtChans);
296  format_pcm.endianness = SL_BYTEORDER_NATIVE;
297 
298  audioSrc.pLocator = &loc_bufq;
299  audioSrc.pFormat = &format_pcm;
300 
301  loc_outmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
302  loc_outmix.outputMix = data->outputMix;
303  audioSnk.pLocator = &loc_outmix;
304  audioSnk.pFormat = NULL;
305 
306 
307  if(data->bufferQueueObject != NULL)
308  SLObjectItf_Destroy(data->bufferQueueObject);
309  data->bufferQueueObject = NULL;
310 
311  result = SLEngineItf_CreateAudioPlayer(data->engine, &data->bufferQueueObject, &audioSrc, &audioSnk, 1, &id, &req);
312  PRINTERR(result, "engine->CreateAudioPlayer");
313  if(SL_RESULT_SUCCESS == result)
314  {
315  result = SLObjectItf_Realize(data->bufferQueueObject, SL_BOOLEAN_FALSE);
316  PRINTERR(result, "bufferQueue->Realize");
317  }
318 
319  if(SL_RESULT_SUCCESS != result)
320  {
321  if(data->bufferQueueObject != NULL)
322  SLObjectItf_Destroy(data->bufferQueueObject);
323  data->bufferQueueObject = NULL;
324 
325  return ALC_FALSE;
326  }
327 
328  return ALC_TRUE;
329 }
330 
332 {
333  osl_data *data = Device->ExtraData;
334  SLAndroidSimpleBufferQueueItf bufferQueue;
335  SLPlayItf player;
336  SLresult result;
337  ALuint i;
338 
339  result = SLObjectItf_GetInterface(data->bufferQueueObject, SL_IID_BUFFERQUEUE, &bufferQueue);
340  PRINTERR(result, "bufferQueue->GetInterface");
341  if(SL_RESULT_SUCCESS == result)
342  {
343  result = (*bufferQueue)->RegisterCallback(bufferQueue, opensl_callback, Device);
344  PRINTERR(result, "bufferQueue->RegisterCallback");
345  }
346  if(SL_RESULT_SUCCESS == result)
347  {
348  data->frameSize = FrameSizeFromDevFmt(Device->FmtChans, Device->FmtType);
349  data->bufferSize = Device->UpdateSize * data->frameSize;
350  data->buffer = calloc(1, data->bufferSize);
351  if(!data->buffer)
352  {
353  result = SL_RESULT_MEMORY_FAILURE;
354  PRINTERR(result, "calloc");
355  }
356  }
357  /* enqueue the first buffer to kick off the callbacks */
358  for(i = 0;i < Device->NumUpdates;i++)
359  {
360  if(SL_RESULT_SUCCESS == result)
361  {
362  result = (*bufferQueue)->Enqueue(bufferQueue, data->buffer, data->bufferSize);
363  PRINTERR(result, "bufferQueue->Enqueue");
364  }
365  }
366  if(SL_RESULT_SUCCESS == result)
367  {
368  result = SLObjectItf_GetInterface(data->bufferQueueObject, SL_IID_PLAY, &player);
369  PRINTERR(result, "bufferQueue->GetInterface");
370  }
371  if(SL_RESULT_SUCCESS == result)
372  {
373  result = SLPlayItf_SetPlayState(player, SL_PLAYSTATE_PLAYING);
374  PRINTERR(result, "player->SetPlayState");
375  }
376 
377  if(SL_RESULT_SUCCESS != result)
378  {
379  if(data->bufferQueueObject != NULL)
380  SLObjectItf_Destroy(data->bufferQueueObject);
381  data->bufferQueueObject = NULL;
382 
383  free(data->buffer);
384  data->buffer = NULL;
385  data->bufferSize = 0;
386 
387  return ALC_FALSE;
388  }
389 
390  return ALC_TRUE;
391 }
392 
393 
394 static void opensl_stop_playback(ALCdevice *Device)
395 {
396  osl_data *data = Device->ExtraData;
397 
398  free(data->buffer);
399  data->buffer = NULL;
400  data->bufferSize = 0;
401 }
402 
403 
404 static const BackendFuncs opensl_funcs = {
410  NULL,
411  NULL,
412  NULL,
413  NULL,
414  NULL,
415  NULL,
419 };
420 
421 
423 {
424  *func_list = opensl_funcs;
425  return ALC_TRUE;
426 }
427 
429 {
430 }
431 
433 {
434  switch(type)
435  {
436  case ALL_DEVICE_PROBE:
438  break;
440  break;
441  }
442 }
#define SLObjectItf_Realize(a, b)
Definition: opensl.c:77
#define ALC_TRUE
Definition: alc.h:84
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum GLenum type
Definition: gl2ext.h:845
GLvoid **typedef void(GLAPIENTRY *PFNGLGETVERTEXATTRIBDVPROC)(GLuint
Definition: glew.h:1824
char * strdup(const char *inStr)
Definition: strdup.c:6
#define NULL
Definition: ftobjs.h:61
static void opensl_stop_playback(ALCdevice *Device)
Definition: opensl.c:394
SDL_EventEntry * free
Definition: SDL_events.c:80
ALuint Frequency
Definition: alMain.h:569
ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size)
Definition: ALu.c:970
ALint64 ALCdevice_GetLatencyDefault(ALCdevice *device)
Definition: ALc.c:1285
static const ALCchar opensl_device[]
Definition: opensl.c:105
GLuint id
Definition: gl2ext.h:1142
#define calloc
Definition: SDL_malloc.c:636
static void opensl_close_playback(ALCdevice *Device)
Definition: opensl.c:241
char ALCchar
Definition: alc.h:42
EGLContext EGLenum EGLClientBuffer buffer
Definition: eglext.h:87
static const char * res_str(SLresult result)
Definition: opensl.c:134
void * ExtraData
Definition: alMain.h:630
#define ALC_FALSE
Definition: alc.h:81
GLuint64EXT * result
Definition: glew.h:12708
void alc_opensl_deinit(void)
Definition: opensl.c:428
static void opensl_callback(SLAndroidSimpleBufferQueueItf bq, void *context)
Definition: opensl.c:168
void SetDefaultWFXChannelOrder(ALCdevice *device)
Definition: ALc.c:1295
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl2ext.h:848
GLint GLsizei count
Definition: gl2ext.h:1011
ALCboolean alc_opensl_init(BackendFuncs *func_list)
Definition: opensl.c:422
static void * pContext
Definition: dsound.c:51
#define SLPlayItf_SetPlayState(a, b)
Definition: opensl.c:84
#define PRINTERR(x, s)
Definition: opensl.c:162
#define SLEngineItf_CreateAudioPlayer(a, b, c, d, e, f, g)
Definition: opensl.c:82
void alc_opensl_probe(enum DevProbe type)
Definition: opensl.c:432
GLuint index
Definition: glew.h:1800
unsigned int ALuint
Definition: al.h:59
enum DevFmtChannels FmtChans
Definition: alMain.h:572
GLint GLint * numBuffers
Definition: gl2ext.h:1487
#define ALC_NO_ERROR
Definition: alc.h:102
void ALCdevice_LockDefault(ALCdevice *device)
Definition: ALc.c:1277
char ALCboolean
Definition: alc.h:39
static const BackendFuncs opensl_funcs
Definition: opensl.c:404
static SLuint32 GetChannelMask(enum DevFmtChannels chans)
Definition: opensl.c:108
ALuint UpdateSize
Definition: alMain.h:570
ALuint NumUpdates
Definition: alMain.h:571
#define SLEngineItf_CreateOutputMix(a, b, c, d, e)
Definition: opensl.c:81
enum DevFmtType FmtType
Definition: alMain.h:573
ALCchar * DeviceName
Definition: alMain.h:575
#define ALC_INVALID_VALUE
Definition: alc.h:114
#define ALC_OUT_OF_MEMORY
Definition: alc.h:117
static ALCboolean opensl_reset_playback(ALCdevice *Device)
Definition: opensl.c:260
XINPUT_STATE_EX * pState
#define SLObjectItf_Destroy(a)
Definition: opensl.c:79
TParseContext * context
int i
Definition: pngrutil.c:1377
int ALCenum
Definition: alc.h:66
ALuint BytesFromDevFmt(enum DevFmtType type)
Definition: ALc.c:1165
void ALCdevice_UnlockDefault(ALCdevice *device)
Definition: ALc.c:1281
DevFmtChannels
Definition: alMain.h:507
ALuint ChannelsFromDevFmt(enum DevFmtChannels chans)
Definition: ALc.c:1179
static ALCenum opensl_open_playback(ALCdevice *Device, const ALCchar *deviceName)
Definition: opensl.c:181
#define SLObjectItf_GetInterface(a, b, c)
Definition: opensl.c:78
void AppendAllDevicesList(const ALCchar *name)
DevProbe
Definition: alMain.h:383
static ALCboolean opensl_start_playback(ALCdevice *Device)
Definition: opensl.c:331
static __inline ALuint FrameSizeFromDevFmt(enum DevFmtChannels chans, enum DevFmtType type)
Definition: alMain.h:523
GLsizei size
Definition: gl2ext.h:1467