opensl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. /* This is an OpenAL backend for Android using the native audio APIs based on
  17. * OpenSL ES 1.0.1. It is based on source code for the native-audio sample app
  18. * bundled with NDK.
  19. */
  20. #include "config.h"
  21. #include <stdlib.h>
  22. #include "alMain.h"
  23. #include "AL/al.h"
  24. #include "AL/alc.h"
  25. #include <SLES/OpenSLES.h>
  26. #ifdef HAVE_ANDROID
  27. #include <SLES/OpenSLES_Android.h>
  28. #else
  29. extern SLAPIENTRY const SLInterfaceID SL_IID_ANDROIDSIMPLEBUFFERQUEUE;
  30. struct SLAndroidSimpleBufferQueueItf_;
  31. typedef const struct SLAndroidSimpleBufferQueueItf_ * const * SLAndroidSimpleBufferQueueItf;
  32. typedef void (*slAndroidSimpleBufferQueueCallback)(SLAndroidSimpleBufferQueueItf caller, void *pContext);
  33. typedef struct SLAndroidSimpleBufferQueueState_ {
  34. SLuint32 count;
  35. SLuint32 index;
  36. } SLAndroidSimpleBufferQueueState;
  37. struct SLAndroidSimpleBufferQueueItf_ {
  38. SLresult (*Enqueue) (
  39. SLAndroidSimpleBufferQueueItf self,
  40. const void *pBuffer,
  41. SLuint32 size
  42. );
  43. SLresult (*Clear) (
  44. SLAndroidSimpleBufferQueueItf self
  45. );
  46. SLresult (*GetState) (
  47. SLAndroidSimpleBufferQueueItf self,
  48. SLAndroidSimpleBufferQueueState *pState
  49. );
  50. SLresult (*RegisterCallback) (
  51. SLAndroidSimpleBufferQueueItf self,
  52. slAndroidSimpleBufferQueueCallback callback,
  53. void* pContext
  54. );
  55. };
  56. #define SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE ((SLuint32) 0x800007BD)
  57. typedef struct SLDataLocator_AndroidSimpleBufferQueue {
  58. SLuint32 locatorType;
  59. SLuint32 numBuffers;
  60. } SLDataLocator_AndroidSimpleBufferQueue;
  61. #endif
  62. /* Helper macros */
  63. #define SLObjectItf_Realize(a,b) ((*(a))->Realize((a),(b)))
  64. #define SLObjectItf_GetInterface(a,b,c) ((*(a))->GetInterface((a),(b),(c)))
  65. #define SLObjectItf_Destroy(a) ((*(a))->Destroy((a)))
  66. #define SLEngineItf_CreateOutputMix(a,b,c,d,e) ((*(a))->CreateOutputMix((a),(b),(c),(d),(e)))
  67. #define SLEngineItf_CreateAudioPlayer(a,b,c,d,e,f,g) ((*(a))->CreateAudioPlayer((a),(b),(c),(d),(e),(f),(g)))
  68. #define SLPlayItf_SetPlayState(a,b) ((*(a))->SetPlayState((a),(b)))
  69. typedef struct {
  70. /* engine interfaces */
  71. SLObjectItf engineObject;
  72. SLEngineItf engine;
  73. /* output mix interfaces */
  74. SLObjectItf outputMix;
  75. /* buffer queue player interfaces */
  76. SLObjectItf bufferQueueObject;
  77. void *buffer;
  78. ALuint bufferSize;
  79. ALuint frameSize;
  80. } osl_data;
  81. static const ALCchar opensl_device[] = "OpenSL";
  82. static SLuint32 GetChannelMask(enum DevFmtChannels chans)
  83. {
  84. switch(chans)
  85. {
  86. case DevFmtMono: return SL_SPEAKER_FRONT_CENTER;
  87. case DevFmtStereo: return SL_SPEAKER_FRONT_LEFT|SL_SPEAKER_FRONT_RIGHT;
  88. case DevFmtQuad: return SL_SPEAKER_FRONT_LEFT|SL_SPEAKER_FRONT_RIGHT|
  89. SL_SPEAKER_BACK_LEFT|SL_SPEAKER_BACK_RIGHT;
  90. case DevFmtX51: return SL_SPEAKER_FRONT_LEFT|SL_SPEAKER_FRONT_RIGHT|
  91. SL_SPEAKER_FRONT_CENTER|SL_SPEAKER_LOW_FREQUENCY|
  92. SL_SPEAKER_BACK_LEFT|SL_SPEAKER_BACK_RIGHT;
  93. case DevFmtX61: return SL_SPEAKER_FRONT_LEFT|SL_SPEAKER_FRONT_RIGHT|
  94. SL_SPEAKER_FRONT_CENTER|SL_SPEAKER_LOW_FREQUENCY|
  95. SL_SPEAKER_BACK_CENTER|
  96. SL_SPEAKER_SIDE_LEFT|SL_SPEAKER_SIDE_RIGHT;
  97. case DevFmtX71: return SL_SPEAKER_FRONT_LEFT|SL_SPEAKER_FRONT_RIGHT|
  98. SL_SPEAKER_FRONT_CENTER|SL_SPEAKER_LOW_FREQUENCY|
  99. SL_SPEAKER_BACK_LEFT|SL_SPEAKER_BACK_RIGHT|
  100. SL_SPEAKER_SIDE_LEFT|SL_SPEAKER_SIDE_RIGHT;
  101. case DevFmtX51Side: return SL_SPEAKER_FRONT_LEFT|SL_SPEAKER_FRONT_RIGHT|
  102. SL_SPEAKER_FRONT_CENTER|SL_SPEAKER_LOW_FREQUENCY|
  103. SL_SPEAKER_SIDE_LEFT|SL_SPEAKER_SIDE_RIGHT;
  104. }
  105. return 0;
  106. }
  107. static const char *res_str(SLresult result)
  108. {
  109. switch(result)
  110. {
  111. case SL_RESULT_SUCCESS: return "Success";
  112. case SL_RESULT_PRECONDITIONS_VIOLATED: return "Preconditions violated";
  113. case SL_RESULT_PARAMETER_INVALID: return "Parameter invalid";
  114. case SL_RESULT_MEMORY_FAILURE: return "Memory failure";
  115. case SL_RESULT_RESOURCE_ERROR: return "Resource error";
  116. case SL_RESULT_RESOURCE_LOST: return "Resource lost";
  117. case SL_RESULT_IO_ERROR: return "I/O error";
  118. case SL_RESULT_BUFFER_INSUFFICIENT: return "Buffer insufficient";
  119. case SL_RESULT_CONTENT_CORRUPTED: return "Content corrupted";
  120. case SL_RESULT_CONTENT_UNSUPPORTED: return "Content unsupported";
  121. case SL_RESULT_CONTENT_NOT_FOUND: return "Content not found";
  122. case SL_RESULT_PERMISSION_DENIED: return "Permission denied";
  123. case SL_RESULT_FEATURE_UNSUPPORTED: return "Feature unsupported";
  124. case SL_RESULT_INTERNAL_ERROR: return "Internal error";
  125. case SL_RESULT_UNKNOWN_ERROR: return "Unknown error";
  126. case SL_RESULT_OPERATION_ABORTED: return "Operation aborted";
  127. case SL_RESULT_CONTROL_LOST: return "Control lost";
  128. #ifdef HAVE_OPENSL_1_1
  129. case SL_RESULT_READONLY: return "ReadOnly";
  130. case SL_RESULT_ENGINEOPTION_UNSUPPORTED: return "Engine option unsupported";
  131. case SL_RESULT_SOURCE_SINK_INCOMPATIBLE: return "Source/Sink incompatible";
  132. #endif
  133. }
  134. return "Unknown error code";
  135. }
  136. #define PRINTERR(x, s) do { \
  137. if((x) != SL_RESULT_SUCCESS) \
  138. ERR("%s: %s\n", (s), res_str((x))); \
  139. } while(0)
  140. /* this callback handler is called every time a buffer finishes playing */
  141. static void opensl_callback(SLAndroidSimpleBufferQueueItf bq, void *context)
  142. {
  143. ALCdevice *Device = context;
  144. osl_data *data = Device->ExtraData;
  145. SLresult result;
  146. aluMixData(Device, data->buffer, data->bufferSize/data->frameSize);
  147. result = (*bq)->Enqueue(bq, data->buffer, data->bufferSize);
  148. PRINTERR(result, "bq->Enqueue");
  149. }
  150. static ALCenum opensl_open_playback(ALCdevice *Device, const ALCchar *deviceName)
  151. {
  152. osl_data *data = NULL;
  153. SLresult result;
  154. if(!deviceName)
  155. deviceName = opensl_device;
  156. else if(strcmp(deviceName, opensl_device) != 0)
  157. return ALC_INVALID_VALUE;
  158. data = calloc(1, sizeof(*data));
  159. if(!data)
  160. return ALC_OUT_OF_MEMORY;
  161. // create engine
  162. result = slCreateEngine(&data->engineObject, 0, NULL, 0, NULL, NULL);
  163. PRINTERR(result, "slCreateEngine");
  164. if(SL_RESULT_SUCCESS == result)
  165. {
  166. result = SLObjectItf_Realize(data->engineObject, SL_BOOLEAN_FALSE);
  167. PRINTERR(result, "engine->Realize");
  168. }
  169. if(SL_RESULT_SUCCESS == result)
  170. {
  171. result = SLObjectItf_GetInterface(data->engineObject, SL_IID_ENGINE, &data->engine);
  172. PRINTERR(result, "engine->GetInterface");
  173. }
  174. if(SL_RESULT_SUCCESS == result)
  175. {
  176. result = SLEngineItf_CreateOutputMix(data->engine, &data->outputMix, 0, NULL, NULL);
  177. PRINTERR(result, "engine->CreateOutputMix");
  178. }
  179. if(SL_RESULT_SUCCESS == result)
  180. {
  181. result = SLObjectItf_Realize(data->outputMix, SL_BOOLEAN_FALSE);
  182. PRINTERR(result, "outputMix->Realize");
  183. }
  184. if(SL_RESULT_SUCCESS != result)
  185. {
  186. if(data->outputMix != NULL)
  187. SLObjectItf_Destroy(data->outputMix);
  188. data->outputMix = NULL;
  189. if(data->engineObject != NULL)
  190. SLObjectItf_Destroy(data->engineObject);
  191. data->engineObject = NULL;
  192. data->engine = NULL;
  193. free(data);
  194. return ALC_INVALID_VALUE;
  195. }
  196. Device->szDeviceName = strdup(deviceName);
  197. Device->ExtraData = data;
  198. return ALC_NO_ERROR;
  199. }
  200. static void opensl_close_playback(ALCdevice *Device)
  201. {
  202. osl_data *data = Device->ExtraData;
  203. if(data->bufferQueueObject != NULL)
  204. SLObjectItf_Destroy(data->bufferQueueObject);
  205. data->bufferQueueObject = NULL;
  206. SLObjectItf_Destroy(data->outputMix);
  207. data->outputMix = NULL;
  208. SLObjectItf_Destroy(data->engineObject);
  209. data->engineObject = NULL;
  210. data->engine = NULL;
  211. free(data);
  212. Device->ExtraData = NULL;
  213. }
  214. static ALCboolean opensl_reset_playback(ALCdevice *Device)
  215. {
  216. osl_data *data = Device->ExtraData;
  217. SLDataLocator_AndroidSimpleBufferQueue loc_bufq;
  218. SLDataLocator_OutputMix loc_outmix;
  219. SLDataFormat_PCM format_pcm;
  220. SLDataSource audioSrc;
  221. SLDataSink audioSnk;
  222. SLInterfaceID id;
  223. SLboolean req;
  224. SLresult result;
  225. Device->UpdateSize = (ALuint64)Device->UpdateSize * 44100 / Device->Frequency;
  226. Device->UpdateSize = Device->UpdateSize * Device->NumUpdates / 2;
  227. Device->NumUpdates = 2;
  228. Device->Frequency = 44100;
  229. Device->FmtChans = DevFmtStereo;
  230. Device->FmtType = DevFmtShort;
  231. SetDefaultWFXChannelOrder(Device);
  232. id = SL_IID_ANDROIDSIMPLEBUFFERQUEUE;
  233. req = SL_BOOLEAN_TRUE;
  234. loc_bufq.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE;
  235. loc_bufq.numBuffers = Device->NumUpdates;
  236. format_pcm.formatType = SL_DATAFORMAT_PCM;
  237. format_pcm.numChannels = ChannelsFromDevFmt(Device->FmtChans);
  238. format_pcm.samplesPerSec = Device->Frequency * 1000;
  239. format_pcm.bitsPerSample = BytesFromDevFmt(Device->FmtType) * 8;
  240. format_pcm.containerSize = format_pcm.bitsPerSample;
  241. format_pcm.channelMask = GetChannelMask(Device->FmtChans);
  242. #ifdef HAVE_OPENSL_1_1
  243. format_pcm.endianness = SL_BYTEORDER_NATIVE;
  244. #else
  245. union { unsigned short num; char buf[sizeof(unsigned short)]; } endianness;
  246. endianness.num = 1;
  247. format_pcm.endianness = endianness.buf[0] ? SL_BYTEORDER_LITTLEENDIAN : SL_BYTEORDER_BIGENDIAN;
  248. #endif
  249. audioSrc.pLocator = &loc_bufq;
  250. audioSrc.pFormat = &format_pcm;
  251. loc_outmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
  252. loc_outmix.outputMix = data->outputMix;
  253. audioSnk.pLocator = &loc_outmix;
  254. audioSnk.pFormat = NULL;
  255. if(data->bufferQueueObject != NULL)
  256. SLObjectItf_Destroy(data->bufferQueueObject);
  257. data->bufferQueueObject = NULL;
  258. result = SLEngineItf_CreateAudioPlayer(data->engine, &data->bufferQueueObject, &audioSrc, &audioSnk, 1, &id, &req);
  259. PRINTERR(result, "engine->CreateAudioPlayer");
  260. if(SL_RESULT_SUCCESS == result)
  261. {
  262. result = SLObjectItf_Realize(data->bufferQueueObject, SL_BOOLEAN_FALSE);
  263. PRINTERR(result, "bufferQueue->Realize");
  264. }
  265. if(SL_RESULT_SUCCESS != result)
  266. {
  267. if(data->bufferQueueObject != NULL)
  268. SLObjectItf_Destroy(data->bufferQueueObject);
  269. data->bufferQueueObject = NULL;
  270. return ALC_FALSE;
  271. }
  272. return ALC_TRUE;
  273. }
  274. static ALCboolean opensl_start_playback(ALCdevice *Device)
  275. {
  276. osl_data *data = Device->ExtraData;
  277. SLAndroidSimpleBufferQueueItf bufferQueue;
  278. SLPlayItf player;
  279. SLresult result;
  280. ALuint i;
  281. result = SLObjectItf_GetInterface(data->bufferQueueObject, SL_IID_BUFFERQUEUE, &bufferQueue);
  282. PRINTERR(result, "bufferQueue->GetInterface");
  283. if(SL_RESULT_SUCCESS == result)
  284. {
  285. result = (*bufferQueue)->RegisterCallback(bufferQueue, opensl_callback, Device);
  286. PRINTERR(result, "bufferQueue->RegisterCallback");
  287. }
  288. if(SL_RESULT_SUCCESS == result)
  289. {
  290. data->frameSize = FrameSizeFromDevFmt(Device->FmtChans, Device->FmtType);
  291. data->bufferSize = Device->UpdateSize * data->frameSize;
  292. data->buffer = calloc(1, data->bufferSize);
  293. if(!data->buffer)
  294. {
  295. result = SL_RESULT_MEMORY_FAILURE;
  296. PRINTERR(result, "calloc");
  297. }
  298. }
  299. /* enqueue the first buffer to kick off the callbacks */
  300. for(i = 0;i < Device->NumUpdates;i++)
  301. {
  302. if(SL_RESULT_SUCCESS == result)
  303. {
  304. result = (*bufferQueue)->Enqueue(bufferQueue, data->buffer, data->bufferSize);
  305. PRINTERR(result, "bufferQueue->Enqueue");
  306. }
  307. }
  308. if(SL_RESULT_SUCCESS == result)
  309. {
  310. result = SLObjectItf_GetInterface(data->bufferQueueObject, SL_IID_PLAY, &player);
  311. PRINTERR(result, "bufferQueue->GetInterface");
  312. }
  313. if(SL_RESULT_SUCCESS == result)
  314. {
  315. result = SLPlayItf_SetPlayState(player, SL_PLAYSTATE_PLAYING);
  316. PRINTERR(result, "player->SetPlayState");
  317. }
  318. if(SL_RESULT_SUCCESS != result)
  319. {
  320. if(data->bufferQueueObject != NULL)
  321. SLObjectItf_Destroy(data->bufferQueueObject);
  322. data->bufferQueueObject = NULL;
  323. free(data->buffer);
  324. data->buffer = NULL;
  325. data->bufferSize = 0;
  326. return ALC_FALSE;
  327. }
  328. return ALC_TRUE;
  329. }
  330. static void opensl_stop_playback(ALCdevice *Device)
  331. {
  332. osl_data *data = Device->ExtraData;
  333. free(data->buffer);
  334. data->buffer = NULL;
  335. data->bufferSize = 0;
  336. }
  337. static const BackendFuncs opensl_funcs = {
  338. opensl_open_playback,
  339. opensl_close_playback,
  340. opensl_reset_playback,
  341. opensl_start_playback,
  342. opensl_stop_playback,
  343. NULL,
  344. NULL,
  345. NULL,
  346. NULL,
  347. NULL,
  348. NULL
  349. };
  350. ALCboolean alc_opensl_init(BackendFuncs *func_list)
  351. {
  352. *func_list = opensl_funcs;
  353. return ALC_TRUE;
  354. }
  355. void alc_opensl_deinit(void)
  356. {
  357. }
  358. void alc_opensl_probe(enum DevProbe type)
  359. {
  360. switch(type)
  361. {
  362. case ALL_DEVICE_PROBE:
  363. AppendAllDeviceList(opensl_device);
  364. break;
  365. case CAPTURE_DEVICE_PROBE:
  366. break;
  367. }
  368. }