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 #ifdef SL_RESULT_READONLY
156 case SL_RESULT_READONLY
: return "ReadOnly";
158 #ifdef SL_RESULT_ENGINEOPTION_UNSUPPORTED
159 case SL_RESULT_ENGINEOPTION_UNSUPPORTED
: return "Engine option unsupported";
161 #ifdef SL_RESULT_SOURCE_SINK_INCOMPATIBLE
162 case SL_RESULT_SOURCE_SINK_INCOMPATIBLE
: return "Source/Sink incompatible";
165 return "Unknown error code";
168 #define PRINTERR(x, s) do { \
169 if((x) != SL_RESULT_SUCCESS) \
170 ERR("%s: %s\n", (s), res_str((x))); \
173 /* this callback handler is called every time a buffer finishes playing */
174 static void opensl_callback(SLAndroidSimpleBufferQueueItf bq
, void *context
)
176 ALCdevice
*Device
= context
;
177 osl_data
*data
= Device
->ExtraData
;
180 aluMixData(Device
, data
->buffer
, data
->bufferSize
/data
->frameSize
);
182 result
= (*bq
)->Enqueue(bq
, data
->buffer
, data
->bufferSize
);
183 PRINTERR(result
, "bq->Enqueue");
187 static ALCenum
opensl_open_playback(ALCdevice
*Device
, const ALCchar
*deviceName
)
189 osl_data
*data
= NULL
;
193 deviceName
= opensl_device
;
194 else if(strcmp(deviceName
, opensl_device
) != 0)
195 return ALC_INVALID_VALUE
;
197 data
= calloc(1, sizeof(*data
));
199 return ALC_OUT_OF_MEMORY
;
202 result
= slCreateEngine(&data
->engineObject
, 0, NULL
, 0, NULL
, NULL
);
203 PRINTERR(result
, "slCreateEngine");
204 if(SL_RESULT_SUCCESS
== result
)
206 result
= SLObjectItf_Realize(data
->engineObject
, SL_BOOLEAN_FALSE
);
207 PRINTERR(result
, "engine->Realize");
209 if(SL_RESULT_SUCCESS
== result
)
211 result
= SLObjectItf_GetInterface(data
->engineObject
, SL_IID_ENGINE
, &data
->engine
);
212 PRINTERR(result
, "engine->GetInterface");
214 if(SL_RESULT_SUCCESS
== result
)
216 result
= SLEngineItf_CreateOutputMix(data
->engine
, &data
->outputMix
, 0, NULL
, NULL
);
217 PRINTERR(result
, "engine->CreateOutputMix");
219 if(SL_RESULT_SUCCESS
== result
)
221 result
= SLObjectItf_Realize(data
->outputMix
, SL_BOOLEAN_FALSE
);
222 PRINTERR(result
, "outputMix->Realize");
225 if(SL_RESULT_SUCCESS
!= result
)
227 if(data
->outputMix
!= NULL
)
228 SLObjectItf_Destroy(data
->outputMix
);
229 data
->outputMix
= NULL
;
231 if(data
->engineObject
!= NULL
)
232 SLObjectItf_Destroy(data
->engineObject
);
233 data
->engineObject
= NULL
;
237 return ALC_INVALID_VALUE
;
240 Device
->DeviceName
= strdup(deviceName
);
241 Device
->ExtraData
= data
;
247 static void opensl_close_playback(ALCdevice
*Device
)
249 osl_data
*data
= Device
->ExtraData
;
251 if(data
->bufferQueueObject
!= NULL
)
252 SLObjectItf_Destroy(data
->bufferQueueObject
);
253 data
->bufferQueueObject
= NULL
;
255 SLObjectItf_Destroy(data
->outputMix
);
256 data
->outputMix
= NULL
;
258 SLObjectItf_Destroy(data
->engineObject
);
259 data
->engineObject
= NULL
;
263 Device
->ExtraData
= NULL
;
266 static ALCboolean
opensl_reset_playback(ALCdevice
*Device
)
268 osl_data
*data
= Device
->ExtraData
;
269 SLDataLocator_AndroidSimpleBufferQueue loc_bufq
;
270 SLDataLocator_OutputMix loc_outmix
;
271 SLDataFormat_PCM format_pcm
;
272 SLDataSource audioSrc
;
279 Device
->UpdateSize
= (ALuint64
)Device
->UpdateSize
* 44100 / Device
->Frequency
;
280 Device
->UpdateSize
= Device
->UpdateSize
* Device
->NumUpdates
/ 2;
281 Device
->NumUpdates
= 2;
283 Device
->Frequency
= 44100;
284 Device
->FmtChans
= DevFmtStereo
;
285 Device
->FmtType
= DevFmtShort
;
287 SetDefaultWFXChannelOrder(Device
);
290 id
= SL_IID_ANDROIDSIMPLEBUFFERQUEUE
;
291 req
= SL_BOOLEAN_TRUE
;
293 loc_bufq
.locatorType
= SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE
;
294 loc_bufq
.numBuffers
= Device
->NumUpdates
;
296 format_pcm
.formatType
= SL_DATAFORMAT_PCM
;
297 format_pcm
.numChannels
= ChannelsFromDevFmt(Device
->FmtChans
);
298 format_pcm
.samplesPerSec
= Device
->Frequency
* 1000;
299 format_pcm
.bitsPerSample
= BytesFromDevFmt(Device
->FmtType
) * 8;
300 format_pcm
.containerSize
= format_pcm
.bitsPerSample
;
301 format_pcm
.channelMask
= GetChannelMask(Device
->FmtChans
);
302 format_pcm
.endianness
= IS_LITTLE_ENDIAN
? SL_BYTEORDER_LITTLEENDIAN
:
303 SL_BYTEORDER_BIGENDIAN
;
305 audioSrc
.pLocator
= &loc_bufq
;
306 audioSrc
.pFormat
= &format_pcm
;
308 loc_outmix
.locatorType
= SL_DATALOCATOR_OUTPUTMIX
;
309 loc_outmix
.outputMix
= data
->outputMix
;
310 audioSnk
.pLocator
= &loc_outmix
;
311 audioSnk
.pFormat
= NULL
;
314 if(data
->bufferQueueObject
!= NULL
)
315 SLObjectItf_Destroy(data
->bufferQueueObject
);
316 data
->bufferQueueObject
= NULL
;
318 result
= SLEngineItf_CreateAudioPlayer(data
->engine
, &data
->bufferQueueObject
, &audioSrc
, &audioSnk
, 1, &id
, &req
);
319 PRINTERR(result
, "engine->CreateAudioPlayer");
320 if(SL_RESULT_SUCCESS
== result
)
322 result
= SLObjectItf_Realize(data
->bufferQueueObject
, SL_BOOLEAN_FALSE
);
323 PRINTERR(result
, "bufferQueue->Realize");
326 if(SL_RESULT_SUCCESS
!= result
)
328 if(data
->bufferQueueObject
!= NULL
)
329 SLObjectItf_Destroy(data
->bufferQueueObject
);
330 data
->bufferQueueObject
= NULL
;
338 static ALCboolean
opensl_start_playback(ALCdevice
*Device
)
340 osl_data
*data
= Device
->ExtraData
;
341 SLAndroidSimpleBufferQueueItf bufferQueue
;
346 result
= SLObjectItf_GetInterface(data
->bufferQueueObject
, SL_IID_BUFFERQUEUE
, &bufferQueue
);
347 PRINTERR(result
, "bufferQueue->GetInterface");
348 if(SL_RESULT_SUCCESS
== result
)
350 result
= (*bufferQueue
)->RegisterCallback(bufferQueue
, opensl_callback
, Device
);
351 PRINTERR(result
, "bufferQueue->RegisterCallback");
353 if(SL_RESULT_SUCCESS
== result
)
355 data
->frameSize
= FrameSizeFromDevFmt(Device
->FmtChans
, Device
->FmtType
);
356 data
->bufferSize
= Device
->UpdateSize
* data
->frameSize
;
357 data
->buffer
= calloc(1, data
->bufferSize
);
360 result
= SL_RESULT_MEMORY_FAILURE
;
361 PRINTERR(result
, "calloc");
364 /* enqueue the first buffer to kick off the callbacks */
365 for(i
= 0;i
< Device
->NumUpdates
;i
++)
367 if(SL_RESULT_SUCCESS
== result
)
369 result
= (*bufferQueue
)->Enqueue(bufferQueue
, data
->buffer
, data
->bufferSize
);
370 PRINTERR(result
, "bufferQueue->Enqueue");
373 if(SL_RESULT_SUCCESS
== result
)
375 result
= SLObjectItf_GetInterface(data
->bufferQueueObject
, SL_IID_PLAY
, &player
);
376 PRINTERR(result
, "bufferQueue->GetInterface");
378 if(SL_RESULT_SUCCESS
== result
)
380 result
= SLPlayItf_SetPlayState(player
, SL_PLAYSTATE_PLAYING
);
381 PRINTERR(result
, "player->SetPlayState");
384 if(SL_RESULT_SUCCESS
!= result
)
386 if(data
->bufferQueueObject
!= NULL
)
387 SLObjectItf_Destroy(data
->bufferQueueObject
);
388 data
->bufferQueueObject
= NULL
;
392 data
->bufferSize
= 0;
401 static void opensl_stop_playback(ALCdevice
*Device
)
403 osl_data
*data
= Device
->ExtraData
;
407 data
->bufferSize
= 0;
411 static const BackendFuncs opensl_funcs
= {
412 opensl_open_playback
,
413 opensl_close_playback
,
414 opensl_reset_playback
,
415 opensl_start_playback
,
416 opensl_stop_playback
,
423 ALCdevice_LockDefault
,
424 ALCdevice_UnlockDefault
,
425 ALCdevice_GetLatencyDefault
429 ALCboolean
alc_opensl_init(BackendFuncs
*func_list
)
431 *func_list
= opensl_funcs
;
435 void alc_opensl_deinit(void)
439 void alc_opensl_probe(enum DevProbe type
)
443 case ALL_DEVICE_PROBE
:
444 AppendAllDevicesList(opensl_device
);
446 case CAPTURE_DEVICE_PROBE
: