opensl.c 13 KB

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