2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
30 #include <SLES/OpenSLES.h>
32 #include <SLES/OpenSLES_Android.h>
34 extern SLAPIENTRY
const SLInterfaceID SL_IID_ANDROIDSIMPLEBUFFERQUEUE
;
36 struct SLAndroidSimpleBufferQueueItf_
;
37 typedef const struct SLAndroidSimpleBufferQueueItf_
* const * SLAndroidSimpleBufferQueueItf
;
39 typedef void (*slAndroidSimpleBufferQueueCallback
)(SLAndroidSimpleBufferQueueItf caller
, void *pContext
);
41 typedef struct SLAndroidSimpleBufferQueueState_
{
44 } SLAndroidSimpleBufferQueueState
;
47 struct SLAndroidSimpleBufferQueueItf_
{
49 SLAndroidSimpleBufferQueueItf self
,
54 SLAndroidSimpleBufferQueueItf self
56 SLresult (*GetState
) (
57 SLAndroidSimpleBufferQueueItf self
,
58 SLAndroidSimpleBufferQueueState
*pState
60 SLresult (*RegisterCallback
) (
61 SLAndroidSimpleBufferQueueItf self
,
62 slAndroidSimpleBufferQueueCallback callback
,
67 #define SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE ((SLuint32) 0x800007BD)
69 typedef struct SLDataLocator_AndroidSimpleBufferQueue
{
72 } SLDataLocator_AndroidSimpleBufferQueue
;
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)))
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)))
84 #define SLPlayItf_SetPlayState(a,b) ((*(a))->SetPlayState((a),(b)))
88 /* engine interfaces */
89 SLObjectItf engineObject
;
92 /* output mix interfaces */
93 SLObjectItf outputMix
;
95 /* buffer queue player interfaces */
96 SLObjectItf bufferQueueObject
;
105 static const ALCchar opensl_device
[] = "OpenSL";
108 static SLuint32
GetChannelMask(enum DevFmtChannels chans
)
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
;
134 static const char *res_str(SLresult result
)
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";
159 return "Unknown error code";
162 #define PRINTERR(x, s) do { \
163 if((x) != SL_RESULT_SUCCESS) \
164 ERR("%s: %s\n", (s), res_str((x))); \
167 /* this callback handler is called every time a buffer finishes playing */
168 static void opensl_callback(SLAndroidSimpleBufferQueueItf bq
, void *context
)
170 ALCdevice
*Device
= context
;
171 osl_data
*data
= Device
->ExtraData
;
174 aluMixData(Device
, data
->buffer
, data
->bufferSize
/data
->frameSize
);
176 result
= (*bq
)->Enqueue(bq
, data
->buffer
, data
->bufferSize
);
177 PRINTERR(result
, "bq->Enqueue");
181 static ALCenum
opensl_open_playback(ALCdevice
*Device
, const ALCchar
*deviceName
)
183 osl_data
*data
= NULL
;
187 deviceName
= opensl_device
;
188 else if(strcmp(deviceName
, opensl_device
) != 0)
189 return ALC_INVALID_VALUE
;
191 data
= calloc(1, sizeof(*data
));
193 return ALC_OUT_OF_MEMORY
;
196 result
= slCreateEngine(&data
->engineObject
, 0, NULL
, 0, NULL
, NULL
);
197 PRINTERR(result
, "slCreateEngine");
198 if(SL_RESULT_SUCCESS
== result
)
200 result
= SLObjectItf_Realize(data
->engineObject
, SL_BOOLEAN_FALSE
);
201 PRINTERR(result
, "engine->Realize");
203 if(SL_RESULT_SUCCESS
== result
)
205 result
= SLObjectItf_GetInterface(data
->engineObject
, SL_IID_ENGINE
, &data
->engine
);
206 PRINTERR(result
, "engine->GetInterface");
208 if(SL_RESULT_SUCCESS
== result
)
210 result
= SLEngineItf_CreateOutputMix(data
->engine
, &data
->outputMix
, 0, NULL
, NULL
);
211 PRINTERR(result
, "engine->CreateOutputMix");
213 if(SL_RESULT_SUCCESS
== result
)
215 result
= SLObjectItf_Realize(data
->outputMix
, SL_BOOLEAN_FALSE
);
216 PRINTERR(result
, "outputMix->Realize");
219 if(SL_RESULT_SUCCESS
!= result
)
221 if(data
->outputMix
!= NULL
)
222 SLObjectItf_Destroy(data
->outputMix
);
223 data
->outputMix
= NULL
;
225 if(data
->engineObject
!= NULL
)
226 SLObjectItf_Destroy(data
->engineObject
);
227 data
->engineObject
= NULL
;
231 return ALC_INVALID_VALUE
;
234 Device
->DeviceName
= strdup(deviceName
);
235 Device
->ExtraData
= data
;
241 static void opensl_close_playback(ALCdevice
*Device
)
243 osl_data
*data
= Device
->ExtraData
;
245 if(data
->bufferQueueObject
!= NULL
)
246 SLObjectItf_Destroy(data
->bufferQueueObject
);
247 data
->bufferQueueObject
= NULL
;
249 SLObjectItf_Destroy(data
->outputMix
);
250 data
->outputMix
= NULL
;
252 SLObjectItf_Destroy(data
->engineObject
);
253 data
->engineObject
= NULL
;
257 Device
->ExtraData
= NULL
;
260 static ALCboolean
opensl_reset_playback(ALCdevice
*Device
)
262 osl_data
*data
= Device
->ExtraData
;
263 SLDataLocator_AndroidSimpleBufferQueue loc_bufq
;
264 SLDataLocator_OutputMix loc_outmix
;
265 SLDataFormat_PCM format_pcm
;
266 SLDataSource audioSrc
;
273 Device
->UpdateSize
= (ALuint64
)Device
->UpdateSize
* 44100 / Device
->Frequency
;
274 Device
->UpdateSize
= Device
->UpdateSize
* Device
->NumUpdates
/ 2;
275 Device
->NumUpdates
= 2;
277 Device
->Frequency
= 44100;
278 Device
->FmtChans
= DevFmtStereo
;
279 Device
->FmtType
= DevFmtShort
;
281 SetDefaultWFXChannelOrder(Device
);
284 id
= SL_IID_ANDROIDSIMPLEBUFFERQUEUE
;
285 req
= SL_BOOLEAN_TRUE
;
287 loc_bufq
.locatorType
= SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE
;
288 loc_bufq
.numBuffers
= Device
->NumUpdates
;
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
;
298 audioSrc
.pLocator
= &loc_bufq
;
299 audioSrc
.pFormat
= &format_pcm
;
301 loc_outmix
.locatorType
= SL_DATALOCATOR_OUTPUTMIX
;
302 loc_outmix
.outputMix
= data
->outputMix
;
303 audioSnk
.pLocator
= &loc_outmix
;
304 audioSnk
.pFormat
= NULL
;
307 if(data
->bufferQueueObject
!= NULL
)
308 SLObjectItf_Destroy(data
->bufferQueueObject
);
309 data
->bufferQueueObject
= NULL
;
311 result
= SLEngineItf_CreateAudioPlayer(data
->engine
, &data
->bufferQueueObject
, &audioSrc
, &audioSnk
, 1, &id
, &req
);
312 PRINTERR(result
, "engine->CreateAudioPlayer");
313 if(SL_RESULT_SUCCESS
== result
)
315 result
= SLObjectItf_Realize(data
->bufferQueueObject
, SL_BOOLEAN_FALSE
);
316 PRINTERR(result
, "bufferQueue->Realize");
319 if(SL_RESULT_SUCCESS
!= result
)
321 if(data
->bufferQueueObject
!= NULL
)
322 SLObjectItf_Destroy(data
->bufferQueueObject
);
323 data
->bufferQueueObject
= NULL
;
331 static ALCboolean
opensl_start_playback(ALCdevice
*Device
)
333 osl_data
*data
= Device
->ExtraData
;
334 SLAndroidSimpleBufferQueueItf bufferQueue
;
339 result
= SLObjectItf_GetInterface(data
->bufferQueueObject
, SL_IID_BUFFERQUEUE
, &bufferQueue
);
340 PRINTERR(result
, "bufferQueue->GetInterface");
341 if(SL_RESULT_SUCCESS
== result
)
343 result
= (*bufferQueue
)->RegisterCallback(bufferQueue
, opensl_callback
, Device
);
344 PRINTERR(result
, "bufferQueue->RegisterCallback");
346 if(SL_RESULT_SUCCESS
== result
)
348 data
->frameSize
= FrameSizeFromDevFmt(Device
->FmtChans
, Device
->FmtType
);
349 data
->bufferSize
= Device
->UpdateSize
* data
->frameSize
;
350 data
->buffer
= calloc(1, data
->bufferSize
);
353 result
= SL_RESULT_MEMORY_FAILURE
;
354 PRINTERR(result
, "calloc");
357 /* enqueue the first buffer to kick off the callbacks */
358 for(i
= 0;i
< Device
->NumUpdates
;i
++)
360 if(SL_RESULT_SUCCESS
== result
)
362 result
= (*bufferQueue
)->Enqueue(bufferQueue
, data
->buffer
, data
->bufferSize
);
363 PRINTERR(result
, "bufferQueue->Enqueue");
366 if(SL_RESULT_SUCCESS
== result
)
368 result
= SLObjectItf_GetInterface(data
->bufferQueueObject
, SL_IID_PLAY
, &player
);
369 PRINTERR(result
, "bufferQueue->GetInterface");
371 if(SL_RESULT_SUCCESS
== result
)
373 result
= SLPlayItf_SetPlayState(player
, SL_PLAYSTATE_PLAYING
);
374 PRINTERR(result
, "player->SetPlayState");
377 if(SL_RESULT_SUCCESS
!= result
)
379 if(data
->bufferQueueObject
!= NULL
)
380 SLObjectItf_Destroy(data
->bufferQueueObject
);
381 data
->bufferQueueObject
= NULL
;
385 data
->bufferSize
= 0;
394 static void opensl_stop_playback(ALCdevice
*Device
)
396 osl_data
*data
= Device
->ExtraData
;
400 data
->bufferSize
= 0;
404 static const BackendFuncs opensl_funcs
= {
405 opensl_open_playback
,
406 opensl_close_playback
,
407 opensl_reset_playback
,
408 opensl_start_playback
,
409 opensl_stop_playback
,
416 ALCdevice_LockDefault
,
417 ALCdevice_UnlockDefault
,
418 ALCdevice_GetLatencyDefault
422 ALCboolean
alc_opensl_init(BackendFuncs
*func_list
)
424 *func_list
= opensl_funcs
;
428 void alc_opensl_deinit(void)
432 void alc_opensl_probe(enum DevProbe type
)
436 case ALL_DEVICE_PROBE
:
437 AppendAllDevicesList(opensl_device
);
439 case CAPTURE_DEVICE_PROBE
: