opensl.c 15 KB

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