jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

540 lines
16KB

  1. /*
  2. opensl_io.c:
  3. Android OpenSL input/output module
  4. Copyright (c) 2012, Victor Lazzarini
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. * Neither the name of the <organization> nor the
  14. names of its contributors may be used to endorse or promote products
  15. derived from this software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  20. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "opensl_io.h"
  28. //#define CONV16BIT 32768
  29. //#define CONVMYFLT (1./32768.)
  30. #define CONV16BIT 32640
  31. #define CONVMYFLT (1./32640.)
  32. static void* createThreadLock(void);
  33. static int waitThreadLock(void *lock);
  34. static void notifyThreadLock(void *lock);
  35. static void destroyThreadLock(void *lock);
  36. static void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context);
  37. static void bqRecorderCallback(SLAndroidSimpleBufferQueueItf bq, void *context);
  38. // creates the OpenSL ES audio engine
  39. static SLresult openSLCreateEngine(OPENSL_STREAM *p)
  40. {
  41. SLresult result;
  42. // create engine
  43. result = slCreateEngine(&(p->engineObject), 0, NULL, 0, NULL, NULL);
  44. if(result != SL_RESULT_SUCCESS) goto engine_end;
  45. // realize the engine
  46. result = (*p->engineObject)->Realize(p->engineObject, SL_BOOLEAN_FALSE);
  47. if(result != SL_RESULT_SUCCESS) goto engine_end;
  48. // get the engine interface, which is needed in order to create other objects
  49. result = (*p->engineObject)->GetInterface(p->engineObject, SL_IID_ENGINE, &(p->engineEngine));
  50. if(result != SL_RESULT_SUCCESS) goto engine_end;
  51. engine_end:
  52. return result;
  53. }
  54. // opens the OpenSL ES device for output
  55. static SLresult openSLPlayOpen(OPENSL_STREAM *p)
  56. {
  57. SLresult result;
  58. SLuint32 sr = p->sr;
  59. SLuint32 channels = p->outchannels;
  60. if(channels){
  61. // configure audio source
  62. SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};
  63. switch(sr){
  64. case 8000:
  65. sr = SL_SAMPLINGRATE_8;
  66. break;
  67. case 11025:
  68. sr = SL_SAMPLINGRATE_11_025;
  69. break;
  70. case 16000:
  71. sr = SL_SAMPLINGRATE_16;
  72. break;
  73. case 22050:
  74. sr = SL_SAMPLINGRATE_22_05;
  75. break;
  76. case 24000:
  77. sr = SL_SAMPLINGRATE_24;
  78. break;
  79. case 32000:
  80. sr = SL_SAMPLINGRATE_32;
  81. break;
  82. case 44100:
  83. sr = SL_SAMPLINGRATE_44_1;
  84. break;
  85. case 48000:
  86. sr = SL_SAMPLINGRATE_48;
  87. break;
  88. case 64000:
  89. sr = SL_SAMPLINGRATE_64;
  90. break;
  91. case 88200:
  92. sr = SL_SAMPLINGRATE_88_2;
  93. break;
  94. case 96000:
  95. sr = SL_SAMPLINGRATE_96;
  96. break;
  97. case 192000:
  98. sr = SL_SAMPLINGRATE_192;
  99. break;
  100. default:
  101. return -1;
  102. }
  103. const SLInterfaceID ids[] = {SL_IID_VOLUME};
  104. const SLboolean req[] = {SL_BOOLEAN_FALSE};
  105. result = (*p->engineEngine)->CreateOutputMix(p->engineEngine, &(p->outputMixObject), 1, ids, req);
  106. if(result != SL_RESULT_SUCCESS) goto end_openaudio;
  107. // realize the output mix
  108. result = (*p->outputMixObject)->Realize(p->outputMixObject, SL_BOOLEAN_FALSE);
  109. int speakers;
  110. if(channels > 1)
  111. speakers = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
  112. else speakers = SL_SPEAKER_FRONT_CENTER;
  113. SLDataFormat_PCM format_pcm = {SL_DATAFORMAT_PCM,channels, sr,
  114. SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16,
  115. speakers, SL_BYTEORDER_LITTLEENDIAN};
  116. SLDataSource audioSrc = {&loc_bufq, &format_pcm};
  117. // configure audio sink
  118. SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, p->outputMixObject};
  119. SLDataSink audioSnk = {&loc_outmix, NULL};
  120. // create audio player
  121. const SLInterfaceID ids1[] = {SL_IID_ANDROIDSIMPLEBUFFERQUEUE};
  122. const SLboolean req1[] = {SL_BOOLEAN_TRUE};
  123. result = (*p->engineEngine)->CreateAudioPlayer(p->engineEngine, &(p->bqPlayerObject), &audioSrc, &audioSnk,
  124. 1, ids1, req1);
  125. if(result != SL_RESULT_SUCCESS) goto end_openaudio;
  126. // realize the player
  127. result = (*p->bqPlayerObject)->Realize(p->bqPlayerObject, SL_BOOLEAN_FALSE);
  128. if(result != SL_RESULT_SUCCESS) goto end_openaudio;
  129. // get the play interface
  130. result = (*p->bqPlayerObject)->GetInterface(p->bqPlayerObject, SL_IID_PLAY, &(p->bqPlayerPlay));
  131. if(result != SL_RESULT_SUCCESS) goto end_openaudio;
  132. // get the buffer queue interface
  133. result = (*p->bqPlayerObject)->GetInterface(p->bqPlayerObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
  134. &(p->bqPlayerBufferQueue));
  135. if(result != SL_RESULT_SUCCESS) goto end_openaudio;
  136. // register callback on the buffer queue
  137. result = (*p->bqPlayerBufferQueue)->RegisterCallback(p->bqPlayerBufferQueue, bqPlayerCallback, p);
  138. if(result != SL_RESULT_SUCCESS) goto end_openaudio;
  139. // set the player's state to playing
  140. result = (*p->bqPlayerPlay)->SetPlayState(p->bqPlayerPlay, SL_PLAYSTATE_PLAYING);
  141. end_openaudio:
  142. return result;
  143. }
  144. return SL_RESULT_SUCCESS;
  145. }
  146. // Open the OpenSL ES device for input
  147. static SLresult openSLRecOpen(OPENSL_STREAM *p){
  148. SLresult result;
  149. SLuint32 sr = p->sr;
  150. SLuint32 channels = p->inchannels;
  151. if(channels){
  152. switch(sr){
  153. case 8000:
  154. sr = SL_SAMPLINGRATE_8;
  155. break;
  156. case 11025:
  157. sr = SL_SAMPLINGRATE_11_025;
  158. break;
  159. case 16000:
  160. sr = SL_SAMPLINGRATE_16;
  161. break;
  162. case 22050:
  163. sr = SL_SAMPLINGRATE_22_05;
  164. break;
  165. case 24000:
  166. sr = SL_SAMPLINGRATE_24;
  167. break;
  168. case 32000:
  169. sr = SL_SAMPLINGRATE_32;
  170. break;
  171. case 44100:
  172. sr = SL_SAMPLINGRATE_44_1;
  173. break;
  174. case 48000:
  175. sr = SL_SAMPLINGRATE_48;
  176. break;
  177. case 64000:
  178. sr = SL_SAMPLINGRATE_64;
  179. break;
  180. case 88200:
  181. sr = SL_SAMPLINGRATE_88_2;
  182. break;
  183. case 96000:
  184. sr = SL_SAMPLINGRATE_96;
  185. break;
  186. case 192000:
  187. sr = SL_SAMPLINGRATE_192;
  188. break;
  189. default:
  190. return -1;
  191. }
  192. // configure audio source
  193. SLDataLocator_IODevice loc_dev = {SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT,
  194. SL_DEFAULTDEVICEID_AUDIOINPUT, NULL};
  195. SLDataSource audioSrc = {&loc_dev, NULL};
  196. // configure audio sink
  197. int speakers;
  198. if(channels > 1)
  199. speakers = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
  200. else speakers = SL_SPEAKER_FRONT_CENTER;
  201. SLDataLocator_AndroidSimpleBufferQueue loc_bq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};
  202. SLDataFormat_PCM format_pcm = {SL_DATAFORMAT_PCM, channels, sr,
  203. SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16,
  204. speakers, SL_BYTEORDER_LITTLEENDIAN};
  205. SLDataSink audioSnk = {&loc_bq, &format_pcm};
  206. // create audio recorder
  207. // (requires the RECORD_AUDIO permission)
  208. const SLInterfaceID id[1] = {SL_IID_ANDROIDSIMPLEBUFFERQUEUE};
  209. const SLboolean req[1] = {SL_BOOLEAN_TRUE};
  210. result = (*p->engineEngine)->CreateAudioRecorder(p->engineEngine, &(p->recorderObject), &audioSrc,
  211. &audioSnk, 1, id, req);
  212. if (SL_RESULT_SUCCESS != result) goto end_recopen;
  213. // realize the audio recorder
  214. result = (*p->recorderObject)->Realize(p->recorderObject, SL_BOOLEAN_FALSE);
  215. if (SL_RESULT_SUCCESS != result) goto end_recopen;
  216. // get the record interface
  217. result = (*p->recorderObject)->GetInterface(p->recorderObject, SL_IID_RECORD, &(p->recorderRecord));
  218. if (SL_RESULT_SUCCESS != result) goto end_recopen;
  219. // get the buffer queue interface
  220. result = (*p->recorderObject)->GetInterface(p->recorderObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
  221. &(p->recorderBufferQueue));
  222. if (SL_RESULT_SUCCESS != result) goto end_recopen;
  223. // register callback on the buffer queue
  224. result = (*p->recorderBufferQueue)->RegisterCallback(p->recorderBufferQueue, bqRecorderCallback,
  225. p);
  226. if (SL_RESULT_SUCCESS != result) goto end_recopen;
  227. result = (*p->recorderRecord)->SetRecordState(p->recorderRecord, SL_RECORDSTATE_RECORDING);
  228. end_recopen:
  229. return result;
  230. }
  231. else return SL_RESULT_SUCCESS;
  232. }
  233. // close the OpenSL IO and destroy the audio engine
  234. static void openSLDestroyEngine(OPENSL_STREAM *p){
  235. // destroy buffer queue audio player object, and invalidate all associated interfaces
  236. if (p->bqPlayerObject != NULL) {
  237. (*p->bqPlayerObject)->Destroy(p->bqPlayerObject);
  238. p->bqPlayerObject = NULL;
  239. p->bqPlayerPlay = NULL;
  240. p->bqPlayerBufferQueue = NULL;
  241. p->bqPlayerEffectSend = NULL;
  242. }
  243. // destroy audio recorder object, and invalidate all associated interfaces
  244. if (p->recorderObject != NULL) {
  245. (*p->recorderObject)->Destroy(p->recorderObject);
  246. p->recorderObject = NULL;
  247. p->recorderRecord = NULL;
  248. p->recorderBufferQueue = NULL;
  249. }
  250. // destroy output mix object, and invalidate all associated interfaces
  251. if (p->outputMixObject != NULL) {
  252. (*p->outputMixObject)->Destroy(p->outputMixObject);
  253. p->outputMixObject = NULL;
  254. }
  255. // destroy engine object, and invalidate all associated interfaces
  256. if (p->engineObject != NULL) {
  257. (*p->engineObject)->Destroy(p->engineObject);
  258. p->engineObject = NULL;
  259. p->engineEngine = NULL;
  260. }
  261. }
  262. // open the android audio device for input and/or output
  263. OPENSL_STREAM *android_OpenAudioDevice(int sr, int inchannels, int outchannels, int bufferframes){
  264. OPENSL_STREAM *p;
  265. p = (OPENSL_STREAM *) calloc(sizeof(OPENSL_STREAM),1);
  266. p->inchannels = inchannels;
  267. p->outchannels = outchannels;
  268. p->sr = sr;
  269. p->inlock = createThreadLock();
  270. p->outlock = createThreadLock();
  271. if((p->outBufSamples = bufferframes*outchannels) != 0) {
  272. if((p->outputBuffer[0] = (short *) calloc(p->outBufSamples, sizeof(short))) == NULL ||
  273. (p->outputBuffer[1] = (short *) calloc(p->outBufSamples, sizeof(short))) == NULL) {
  274. android_CloseAudioDevice(p);
  275. return NULL;
  276. }
  277. }
  278. if((p->inBufSamples = bufferframes*inchannels) != 0){
  279. if((p->inputBuffer[0] = (short *) calloc(p->inBufSamples, sizeof(short))) == NULL ||
  280. (p->inputBuffer[1] = (short *) calloc(p->inBufSamples, sizeof(short))) == NULL){
  281. android_CloseAudioDevice(p);
  282. return NULL;
  283. }
  284. }
  285. p->currentInputIndex = 0;
  286. p->currentOutputBuffer = 0;
  287. p->currentInputIndex = p->inBufSamples;
  288. p->currentInputBuffer = 0;
  289. if(openSLCreateEngine(p) != SL_RESULT_SUCCESS) {
  290. android_CloseAudioDevice(p);
  291. return NULL;
  292. }
  293. if(openSLRecOpen(p) != SL_RESULT_SUCCESS) {
  294. android_CloseAudioDevice(p);
  295. return NULL;
  296. }
  297. if(openSLPlayOpen(p) != SL_RESULT_SUCCESS) {
  298. android_CloseAudioDevice(p);
  299. return NULL;
  300. }
  301. notifyThreadLock(p->outlock);
  302. notifyThreadLock(p->inlock);
  303. p->time = 0.;
  304. return p;
  305. }
  306. // close the android audio device
  307. void android_CloseAudioDevice(OPENSL_STREAM *p){
  308. if (p == NULL)
  309. return;
  310. openSLDestroyEngine(p);
  311. if (p->inlock != NULL) {
  312. notifyThreadLock(p->inlock);
  313. destroyThreadLock(p->inlock);
  314. p->inlock = NULL;
  315. }
  316. if (p->outlock != NULL) {
  317. notifyThreadLock(p->outlock);
  318. destroyThreadLock(p->outlock);
  319. p->inlock = NULL;
  320. }
  321. if (p->outputBuffer[0] != NULL) {
  322. free(p->outputBuffer[0]);
  323. p->outputBuffer[0] = NULL;
  324. }
  325. if (p->outputBuffer[1] != NULL) {
  326. free(p->outputBuffer[1]);
  327. p->outputBuffer[1] = NULL;
  328. }
  329. if (p->inputBuffer[0] != NULL) {
  330. free(p->inputBuffer[0]);
  331. p->inputBuffer[0] = NULL;
  332. }
  333. if (p->inputBuffer[1] != NULL) {
  334. free(p->inputBuffer[1]);
  335. p->inputBuffer[1] = NULL;
  336. }
  337. free(p);
  338. }
  339. // returns timestamp of the processed stream
  340. double android_GetTimestamp(OPENSL_STREAM *p){
  341. return p->time;
  342. }
  343. // this callback handler is called every time a buffer finishes recording
  344. void bqRecorderCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
  345. {
  346. OPENSL_STREAM *p = (OPENSL_STREAM *) context;
  347. notifyThreadLock(p->inlock);
  348. }
  349. // gets a buffer of size samples from the device
  350. int android_AudioIn(OPENSL_STREAM *p,float *buffer,int size){
  351. short *inBuffer;
  352. int i, bufsamps, index;
  353. if(p == NULL) return 0;
  354. bufsamps = p->inBufSamples;
  355. if(bufsamps == 0) return 0;
  356. index = p->currentInputIndex;
  357. inBuffer = p->inputBuffer[p->currentInputBuffer];
  358. for(i=0; i < size; i++){
  359. if (index >= bufsamps) {
  360. waitThreadLock(p->inlock);
  361. (*p->recorderBufferQueue)->Enqueue(p->recorderBufferQueue,
  362. inBuffer,bufsamps*sizeof(short));
  363. p->currentInputBuffer = (p->currentInputBuffer ? 0 : 1);
  364. index = 0;
  365. inBuffer = p->inputBuffer[p->currentInputBuffer];
  366. }
  367. buffer[i] = (float) inBuffer[index++]*CONVMYFLT;
  368. }
  369. p->currentInputIndex = index;
  370. if(p->outchannels == 0) p->time += (double) size/(p->sr*p->inchannels);
  371. return i;
  372. }
  373. // this callback handler is called every time a buffer finishes playing
  374. void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
  375. {
  376. OPENSL_STREAM *p = (OPENSL_STREAM *) context;
  377. notifyThreadLock(p->outlock);
  378. }
  379. // puts a buffer of size samples to the device
  380. int android_AudioOut(OPENSL_STREAM *p, float *buffer,int size){
  381. short *outBuffer;
  382. int i, bufsamps, index;
  383. if(p == NULL) return 0;
  384. bufsamps = p->outBufSamples;
  385. if(bufsamps == 0) return 0;
  386. index = p->currentOutputIndex;
  387. outBuffer = p->outputBuffer[p->currentOutputBuffer];
  388. for(i=0; i < size; i++){
  389. outBuffer[index++] = (short) (buffer[i]*CONV16BIT);
  390. if (index >= p->outBufSamples) {
  391. waitThreadLock(p->outlock);
  392. (*p->bqPlayerBufferQueue)->Enqueue(p->bqPlayerBufferQueue,
  393. outBuffer,bufsamps*sizeof(short));
  394. p->currentOutputBuffer = (p->currentOutputBuffer ? 0 : 1);
  395. index = 0;
  396. outBuffer = p->outputBuffer[p->currentOutputBuffer];
  397. }
  398. }
  399. p->currentOutputIndex = index;
  400. p->time += (double) size/(p->sr*p->outchannels);
  401. return i;
  402. }
  403. //----------------------------------------------------------------------
  404. // thread Locks
  405. // to ensure synchronisation between callbacks and processing code
  406. void* createThreadLock(void)
  407. {
  408. threadLock *p;
  409. p = (threadLock*) malloc(sizeof(threadLock));
  410. if (p == NULL)
  411. return NULL;
  412. memset(p, 0, sizeof(threadLock));
  413. if (pthread_mutex_init(&(p->m), (pthread_mutexattr_t*) NULL) != 0) {
  414. free((void*) p);
  415. return NULL;
  416. }
  417. if (pthread_cond_init(&(p->c), (pthread_condattr_t*) NULL) != 0) {
  418. pthread_mutex_destroy(&(p->m));
  419. free((void*) p);
  420. return NULL;
  421. }
  422. p->s = (unsigned char) 1;
  423. return p;
  424. }
  425. int waitThreadLock(void *lock)
  426. {
  427. threadLock *p;
  428. int retval = 0;
  429. p = (threadLock*) lock;
  430. pthread_mutex_lock(&(p->m));
  431. while (!p->s) {
  432. pthread_cond_wait(&(p->c), &(p->m));
  433. }
  434. p->s = (unsigned char) 0;
  435. pthread_mutex_unlock(&(p->m));
  436. return NULL;
  437. }
  438. void notifyThreadLock(void *lock)
  439. {
  440. threadLock *p;
  441. p = (threadLock*) lock;
  442. pthread_mutex_lock(&(p->m));
  443. p->s = (unsigned char) 1;
  444. pthread_cond_signal(&(p->c));
  445. pthread_mutex_unlock(&(p->m));
  446. return;
  447. }
  448. void destroyThreadLock(void *lock)
  449. {
  450. threadLock *p;
  451. p = (threadLock*) lock;
  452. if (p == NULL)
  453. return;
  454. notifyThreadLock(p);
  455. pthread_cond_destroy(&(p->c));
  456. pthread_mutex_destroy(&(p->m));
  457. free(p);
  458. }