Use logging macros in the sndio backend
[openal-soft/android.git] / Alc / opensl.c
blob0f8f02f3a0c8b7990d582b700dd0aed96edf58ee
1 /*
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
19 * bundled with NDK.
22 #include "config.h"
24 #include <stdlib.h>
25 #include "alMain.h"
26 #include "AL/al.h"
27 #include "AL/alc.h"
30 #include <SLES/OpenSLES.h>
31 #if 1
32 #include <SLES/OpenSLES_Android.h>
33 #else
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_ {
42 SLuint32 count;
43 SLuint32 index;
44 } SLAndroidSimpleBufferQueueState;
47 struct SLAndroidSimpleBufferQueueItf_ {
48 SLresult (*Enqueue) (
49 SLAndroidSimpleBufferQueueItf self,
50 const void *pBuffer,
51 SLuint32 size
53 SLresult (*Clear) (
54 SLAndroidSimpleBufferQueueItf self
56 SLresult (*GetState) (
57 SLAndroidSimpleBufferQueueItf self,
58 SLAndroidSimpleBufferQueueState *pState
60 SLresult (*RegisterCallback) (
61 SLAndroidSimpleBufferQueueItf self,
62 slAndroidSimpleBufferQueueCallback callback,
63 void* pContext
67 #define SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE ((SLuint32) 0x800007BD)
69 typedef struct SLDataLocator_AndroidSimpleBufferQueue {
70 SLuint32 locatorType;
71 SLuint32 numBuffers;
72 } SLDataLocator_AndroidSimpleBufferQueue;
74 #endif
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)))
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)))
87 typedef struct {
88 /* engine interfaces */
89 SLObjectItf engineObject;
90 SLEngineItf engine;
92 /* output mix interfaces */
93 SLObjectItf outputMix;
95 /* buffer queue player interfaces */
96 SLObjectItf bufferQueueObject;
98 void *buffer;
99 ALuint bufferSize;
101 ALuint frameSize;
102 } osl_data;
105 static const ALCchar opensl_device[] = "OpenSL";
108 static SLuint32 GetChannelMask(enum DevFmtChannels chans)
110 switch(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;
131 return 0;
134 static const char *res_str(SLresult result)
136 switch(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 AL_PRINT("%s: %s\n", (s), res_str((x))); \
165 } while(0)
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;
172 SLresult result;
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 ALCboolean opensl_open_playback(ALCdevice *Device, const ALCchar *deviceName)
183 osl_data *data = NULL;
184 SLresult result;
186 if(!deviceName)
187 deviceName = opensl_device;
188 else if(strcmp(deviceName, opensl_device) != 0)
189 return ALC_FALSE;
191 data = calloc(1, sizeof(*data));
192 if(!data)
194 alcSetError(Device, ALC_OUT_OF_MEMORY);
195 return ALC_FALSE;
198 // create engine
199 result = slCreateEngine(&data->engineObject, 0, NULL, 0, NULL, NULL);
200 PRINTERR(result, "slCreateEngine");
201 if(SL_RESULT_SUCCESS == result)
203 result = SLObjectItf_Realize(data->engineObject, SL_BOOLEAN_FALSE);
204 PRINTERR(result, "engine->Realize");
206 if(SL_RESULT_SUCCESS == result)
208 result = SLObjectItf_GetInterface(data->engineObject, SL_IID_ENGINE, &data->engine);
209 PRINTERR(result, "engine->GetInterface");
211 if(SL_RESULT_SUCCESS == result)
213 result = SLEngineItf_CreateOutputMix(data->engine, &data->outputMix, 0, NULL, NULL);
214 PRINTERR(result, "engine->CreateOutputMix");
216 if(SL_RESULT_SUCCESS == result)
218 result = SLObjectItf_Realize(data->outputMix, SL_BOOLEAN_FALSE);
219 PRINTERR(result, "outputMix->Realize");
222 if(SL_RESULT_SUCCESS != result)
224 if(data->outputMix != NULL)
225 SLObjectItf_Destroy(data->outputMix);
226 data->outputMix = NULL;
228 if(data->engineObject != NULL)
229 SLObjectItf_Destroy(data->engineObject);
230 data->engineObject = NULL;
231 data->engine = NULL;
233 free(data);
234 return ALC_FALSE;
237 Device->szDeviceName = strdup(deviceName);
238 Device->ExtraData = data;
240 return ALC_TRUE;
244 static void opensl_close_playback(ALCdevice *Device)
246 osl_data *data = Device->ExtraData;
248 SLObjectItf_Destroy(data->outputMix);
249 data->outputMix = NULL;
251 SLObjectItf_Destroy(data->engineObject);
252 data->engineObject = NULL;
253 data->engine = NULL;
255 free(data);
256 Device->ExtraData = NULL;
259 static ALCboolean opensl_reset_playback(ALCdevice *Device)
261 osl_data *data = Device->ExtraData;
262 SLDataLocator_AndroidSimpleBufferQueue loc_bufq;
263 SLAndroidSimpleBufferQueueItf bufferQueue;
264 SLDataLocator_OutputMix loc_outmix;
265 SLDataFormat_PCM format_pcm;
266 SLDataSource audioSrc;
267 SLDataSink audioSnk;
268 SLPlayItf player;
269 SLInterfaceID id;
270 SLboolean req;
271 SLresult result;
272 ALuint i;
275 Device->UpdateSize = (ALuint64)Device->UpdateSize * 44100 / Device->Frequency;
276 Device->UpdateSize = Device->UpdateSize * Device->NumUpdates / 2;
277 Device->NumUpdates = 2;
279 Device->Frequency = 44100;
280 Device->FmtChans = DevFmtStereo;
281 Device->FmtType = DevFmtShort;
283 SetDefaultWFXChannelOrder(Device);
286 id = SL_IID_ANDROIDSIMPLEBUFFERQUEUE;
287 req = SL_BOOLEAN_TRUE;
289 loc_bufq.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE;
290 loc_bufq.numBuffers = Device->NumUpdates;
292 format_pcm.formatType = SL_DATAFORMAT_PCM;
293 format_pcm.numChannels = ChannelsFromDevFmt(Device->FmtChans);
294 format_pcm.samplesPerSec = Device->Frequency * 1000;
295 format_pcm.bitsPerSample = BytesFromDevFmt(Device->FmtType) * 8;
296 format_pcm.containerSize = format_pcm.bitsPerSample;
297 format_pcm.channelMask = GetChannelMask(Device->FmtChans);
298 format_pcm.endianness = SL_BYTEORDER_NATIVE;
300 audioSrc.pLocator = &loc_bufq;
301 audioSrc.pFormat = &format_pcm;
303 loc_outmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
304 loc_outmix.outputMix = data->outputMix;
305 audioSnk.pLocator = &loc_outmix;
306 audioSnk.pFormat = NULL;
309 result = SLEngineItf_CreateAudioPlayer(data->engine, &data->bufferQueueObject, &audioSrc, &audioSnk, 1, &id, &req);
310 PRINTERR(result, "engine->CreateAudioPlayer");
311 if(SL_RESULT_SUCCESS == result)
313 result = SLObjectItf_Realize(data->bufferQueueObject, SL_BOOLEAN_FALSE);
314 PRINTERR(result, "bufferQueue->Realize");
316 if(SL_RESULT_SUCCESS == result)
318 result = SLObjectItf_GetInterface(data->bufferQueueObject, SL_IID_BUFFERQUEUE, &bufferQueue);
319 PRINTERR(result, "bufferQueue->GetInterface");
321 if(SL_RESULT_SUCCESS == result)
323 result = (*bufferQueue)->RegisterCallback(bufferQueue, opensl_callback, Device);
324 PRINTERR(result, "bufferQueue->RegisterCallback");
326 if(SL_RESULT_SUCCESS == result)
328 data->frameSize = FrameSizeFromDevFmt(Device->FmtChans, Device->FmtType);
329 data->bufferSize = Device->UpdateSize * data->frameSize;
330 data->buffer = calloc(1, data->bufferSize);
331 if(!data->buffer)
333 result = SL_RESULT_MEMORY_FAILURE;
334 PRINTERR(result, "calloc");
337 /* enqueue the first buffer to kick off the callbacks */
338 for(i = 0;i < Device->NumUpdates;i++)
340 if(SL_RESULT_SUCCESS == result)
342 result = (*bufferQueue)->Enqueue(bufferQueue, data->buffer, data->bufferSize);
343 PRINTERR(result, "bufferQueue->Enqueue");
346 if(SL_RESULT_SUCCESS == result)
348 result = SLObjectItf_GetInterface(data->bufferQueueObject, SL_IID_PLAY, &player);
349 PRINTERR(result, "bufferQueue->GetInterface");
351 if(SL_RESULT_SUCCESS == result)
353 result = SLPlayItf_SetPlayState(player, SL_PLAYSTATE_PLAYING);
354 PRINTERR(result, "player->SetPlayState");
357 if(SL_RESULT_SUCCESS != result)
359 if(data->bufferQueueObject != NULL)
360 SLObjectItf_Destroy(data->bufferQueueObject);
361 data->bufferQueueObject = NULL;
363 free(data->buffer);
364 data->buffer = NULL;
365 data->bufferSize = 0;
367 return ALC_FALSE;
370 return ALC_TRUE;
374 static void opensl_stop_playback(ALCdevice *Device)
376 osl_data *data = Device->ExtraData;
378 if(data->bufferQueueObject != NULL)
379 SLObjectItf_Destroy(data->bufferQueueObject);
380 data->bufferQueueObject = NULL;
382 free(data->buffer);
383 data->buffer = NULL;
384 data->bufferSize = 0;
387 static ALCboolean opensl_open_capture(ALCdevice *Device, const ALCchar *deviceName)
389 return ALC_FALSE;
390 (void)Device;
391 (void)deviceName;
395 static const BackendFuncs opensl_funcs = {
396 opensl_open_playback,
397 opensl_close_playback,
398 opensl_reset_playback,
399 opensl_stop_playback,
400 opensl_open_capture,
401 NULL,
402 NULL,
403 NULL,
404 NULL,
405 NULL
409 void alc_opensl_init(BackendFuncs *func_list)
411 *func_list = opensl_funcs;
414 void alc_opensl_deinit(void)
418 void alc_opensl_probe(enum DevProbe type)
420 switch(type)
422 case DEVICE_PROBE:
423 AppendDeviceList(opensl_device);
424 break;
425 case ALL_DEVICE_PROBE:
426 AppendAllDeviceList(opensl_device);
427 break;
428 case CAPTURE_DEVICE_PROBE:
429 break;