opensl.c 13 KB

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