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.

590 lines
17KB

  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. #include <string.h>
  29. #include <unistd.h>
  30. //#define CONV16BIT 32768
  31. //#define CONVMYFLT (1./32768.)
  32. #define CONV16BIT 32640
  33. #define CONVMYFLT (1./32640.)
  34. static void* createThreadLock(void);
  35. static int waitThreadLock(void *lock);
  36. static void notifyThreadLock(void *lock);
  37. static void destroyThreadLock(void *lock);
  38. static void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context);
  39. static void bqRecorderCallback(SLAndroidSimpleBufferQueueItf bq, void *context);
  40. // creates the OpenSL ES audio engine
  41. static SLresult openSLCreateEngine(OPENSL_STREAM *p)
  42. {
  43. SLresult result;
  44. // create engine
  45. result = slCreateEngine(&(p->engineObject), 0, NULL, 0, NULL, NULL);
  46. if(result != SL_RESULT_SUCCESS) goto engine_end;
  47. // realize the engine
  48. result = (*p->engineObject)->Realize(p->engineObject, SL_BOOLEAN_FALSE);
  49. if(result != SL_RESULT_SUCCESS) goto engine_end;
  50. // get the engine interface, which is needed in order to create other objects
  51. result = (*p->engineObject)->GetInterface(p->engineObject, SL_IID_ENGINE, &(p->engineEngine));
  52. if(result != SL_RESULT_SUCCESS) goto engine_end;
  53. engine_end:
  54. return result;
  55. }
  56. // opens the OpenSL ES device for output
  57. static SLresult openSLPlayOpen(OPENSL_STREAM *p)
  58. {
  59. SLresult result;
  60. SLuint32 sr = p->sr;
  61. SLuint32 channels = p->outchannels;
  62. if(channels){
  63. // configure audio source
  64. SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};
  65. switch(sr){
  66. case 8000:
  67. sr = SL_SAMPLINGRATE_8;
  68. break;
  69. case 11025:
  70. sr = SL_SAMPLINGRATE_11_025;
  71. break;
  72. case 16000:
  73. sr = SL_SAMPLINGRATE_16;
  74. break;
  75. case 22050:
  76. sr = SL_SAMPLINGRATE_22_05;
  77. break;
  78. case 24000:
  79. sr = SL_SAMPLINGRATE_24;
  80. break;
  81. case 32000:
  82. sr = SL_SAMPLINGRATE_32;
  83. break;
  84. case 44100:
  85. sr = SL_SAMPLINGRATE_44_1;
  86. break;
  87. case 48000:
  88. sr = SL_SAMPLINGRATE_48;
  89. break;
  90. case 64000:
  91. sr = SL_SAMPLINGRATE_64;
  92. break;
  93. case 88200:
  94. sr = SL_SAMPLINGRATE_88_2;
  95. break;
  96. case 96000:
  97. sr = SL_SAMPLINGRATE_96;
  98. break;
  99. case 192000:
  100. sr = SL_SAMPLINGRATE_192;
  101. break;
  102. default:
  103. return -1;
  104. }
  105. const SLInterfaceID ids[] = {SL_IID_VOLUME};
  106. const SLboolean req[] = {SL_BOOLEAN_FALSE};
  107. result = (*p->engineEngine)->CreateOutputMix(p->engineEngine, &(p->outputMixObject), 1, ids, req);
  108. if(result != SL_RESULT_SUCCESS) return result;
  109. // realize the output mix
  110. result = (*p->outputMixObject)->Realize(p->outputMixObject, SL_BOOLEAN_FALSE);
  111. SLuint32 speakers;
  112. if(channels > 1)
  113. speakers = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
  114. else speakers = SL_SPEAKER_FRONT_CENTER;
  115. SLDataFormat_PCM format_pcm = {SL_DATAFORMAT_PCM,channels, sr,
  116. SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16,
  117. speakers, SL_BYTEORDER_LITTLEENDIAN};
  118. SLDataSource audioSrc = {&loc_bufq, &format_pcm};
  119. // configure audio sink
  120. SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, p->outputMixObject};
  121. SLDataSink audioSnk = {&loc_outmix, NULL};
  122. // create audio player
  123. const SLInterfaceID ids1[] = {SL_IID_ANDROIDSIMPLEBUFFERQUEUE};
  124. const SLboolean req1[] = {SL_BOOLEAN_TRUE};
  125. result = (*p->engineEngine)->CreateAudioPlayer(p->engineEngine, &(p->bqPlayerObject), &audioSrc, &audioSnk,
  126. 1, ids1, req1);
  127. if(result != SL_RESULT_SUCCESS) goto end_openaudio;
  128. // realize the player
  129. result = (*p->bqPlayerObject)->Realize(p->bqPlayerObject, SL_BOOLEAN_FALSE);
  130. if(result != SL_RESULT_SUCCESS) goto end_openaudio;
  131. // get the play interface
  132. result = (*p->bqPlayerObject)->GetInterface(p->bqPlayerObject, SL_IID_PLAY, &(p->bqPlayerPlay));
  133. if(result != SL_RESULT_SUCCESS) goto end_openaudio;
  134. // get the buffer queue interface
  135. result = (*p->bqPlayerObject)->GetInterface(p->bqPlayerObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
  136. &(p->bqPlayerBufferQueue));
  137. if(result != SL_RESULT_SUCCESS) goto end_openaudio;
  138. // register callback on the buffer queue
  139. result = (*p->bqPlayerBufferQueue)->RegisterCallback(p->bqPlayerBufferQueue, bqPlayerCallback, p);
  140. if(result != SL_RESULT_SUCCESS) goto end_openaudio;
  141. // set the player's state to playing
  142. result = (*p->bqPlayerPlay)->SetPlayState(p->bqPlayerPlay, SL_PLAYSTATE_PLAYING);
  143. end_openaudio:
  144. return result;
  145. }
  146. return SL_RESULT_SUCCESS;
  147. }
  148. // Open the OpenSL ES device for input
  149. static SLresult openSLRecOpen(OPENSL_STREAM *p){
  150. SLresult result;
  151. SLuint32 sr = p->sr;
  152. SLuint32 channels = p->inchannels;
  153. if(channels){
  154. switch(sr){
  155. case 8000:
  156. sr = SL_SAMPLINGRATE_8;
  157. break;
  158. case 11025:
  159. sr = SL_SAMPLINGRATE_11_025;
  160. break;
  161. case 16000:
  162. sr = SL_SAMPLINGRATE_16;
  163. break;
  164. case 22050:
  165. sr = SL_SAMPLINGRATE_22_05;
  166. break;
  167. case 24000:
  168. sr = SL_SAMPLINGRATE_24;
  169. break;
  170. case 32000:
  171. sr = SL_SAMPLINGRATE_32;
  172. break;
  173. case 44100:
  174. sr = SL_SAMPLINGRATE_44_1;
  175. break;
  176. case 48000:
  177. sr = SL_SAMPLINGRATE_48;
  178. break;
  179. case 64000:
  180. sr = SL_SAMPLINGRATE_64;
  181. break;
  182. case 88200:
  183. sr = SL_SAMPLINGRATE_88_2;
  184. break;
  185. case 96000:
  186. sr = SL_SAMPLINGRATE_96;
  187. break;
  188. case 192000:
  189. sr = SL_SAMPLINGRATE_192;
  190. break;
  191. default:
  192. return -1;
  193. }
  194. // configure audio source
  195. SLDataLocator_IODevice loc_dev = {SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT,
  196. SL_DEFAULTDEVICEID_AUDIOINPUT, NULL};
  197. SLDataSource audioSrc = {&loc_dev, NULL};
  198. // configure audio sink
  199. SLuint32 speakers;
  200. if(channels > 1)
  201. speakers = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
  202. else speakers = SL_SPEAKER_FRONT_CENTER;
  203. SLDataLocator_AndroidSimpleBufferQueue loc_bq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};
  204. SLDataFormat_PCM format_pcm = {SL_DATAFORMAT_PCM, channels, sr,
  205. SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16,
  206. speakers, SL_BYTEORDER_LITTLEENDIAN};
  207. SLDataSink audioSnk = {&loc_bq, &format_pcm};
  208. // create audio recorder
  209. // (requires the RECORD_AUDIO permission)
  210. const SLInterfaceID id[1] = {SL_IID_ANDROIDSIMPLEBUFFERQUEUE};
  211. const SLboolean req[1] = {SL_BOOLEAN_TRUE};
  212. result = (*p->engineEngine)->CreateAudioRecorder(p->engineEngine, &(p->recorderObject), &audioSrc,
  213. &audioSnk, 1, id, req);
  214. if (SL_RESULT_SUCCESS != result) goto end_recopen;
  215. // realize the audio recorder
  216. result = (*p->recorderObject)->Realize(p->recorderObject, SL_BOOLEAN_FALSE);
  217. if (SL_RESULT_SUCCESS != result) goto end_recopen;
  218. // get the record interface
  219. result = (*p->recorderObject)->GetInterface(p->recorderObject, SL_IID_RECORD, &(p->recorderRecord));
  220. if (SL_RESULT_SUCCESS != result) goto end_recopen;
  221. // get the buffer queue interface
  222. result = (*p->recorderObject)->GetInterface(p->recorderObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
  223. &(p->recorderBufferQueue));
  224. if (SL_RESULT_SUCCESS != result) goto end_recopen;
  225. // register callback on the buffer queue
  226. result = (*p->recorderBufferQueue)->RegisterCallback(p->recorderBufferQueue, bqRecorderCallback,
  227. p);
  228. if (SL_RESULT_SUCCESS != result) goto end_recopen;
  229. result = (*p->recorderRecord)->SetRecordState(p->recorderRecord, SL_RECORDSTATE_RECORDING);
  230. end_recopen:
  231. return result;
  232. }
  233. else return SL_RESULT_SUCCESS;
  234. }
  235. // close the OpenSL IO and destroy the audio engine
  236. static void openSLDestroyEngine(OPENSL_STREAM *p){
  237. // destroy buffer queue audio player object, and invalidate all associated interfaces
  238. if (p->bqPlayerObject != NULL) {
  239. (*p->bqPlayerObject)->Destroy(p->bqPlayerObject);
  240. p->bqPlayerObject = NULL;
  241. p->bqPlayerPlay = NULL;
  242. p->bqPlayerBufferQueue = NULL;
  243. p->bqPlayerEffectSend = NULL;
  244. }
  245. // destroy audio recorder object, and invalidate all associated interfaces
  246. if (p->recorderObject != NULL) {
  247. (*p->recorderObject)->Destroy(p->recorderObject);
  248. p->recorderObject = NULL;
  249. p->recorderRecord = NULL;
  250. p->recorderBufferQueue = NULL;
  251. }
  252. // destroy output mix object, and invalidate all associated interfaces
  253. if (p->outputMixObject != NULL) {
  254. (*p->outputMixObject)->Destroy(p->outputMixObject);
  255. p->outputMixObject = NULL;
  256. }
  257. // destroy engine object, and invalidate all associated interfaces
  258. if (p->engineObject != NULL) {
  259. (*p->engineObject)->Destroy(p->engineObject);
  260. p->engineObject = NULL;
  261. p->engineEngine = NULL;
  262. }
  263. }
  264. // open the android audio device for input and/or output
  265. OPENSL_STREAM *android_OpenAudioDevice(int sr, int inchannels, int outchannels, int bufferframes){
  266. OPENSL_STREAM *p;
  267. p = (OPENSL_STREAM *) calloc(sizeof(OPENSL_STREAM),1);
  268. p->inchannels = inchannels;
  269. p->outchannels = outchannels;
  270. p->sr = sr;
  271. p->inlock = createThreadLock();
  272. if (p->inlock == NULL)
  273. return NULL;
  274. p->outlock = createThreadLock();
  275. if (p->outlock == NULL)
  276. return NULL;
  277. if((p->outBufSamples = bufferframes*outchannels) != 0) {
  278. if((p->outputBuffer[0] = (short *) calloc(p->outBufSamples, sizeof(short))) == NULL ||
  279. (p->outputBuffer[1] = (short *) calloc(p->outBufSamples, sizeof(short))) == NULL) {
  280. android_CloseAudioDevice(p);
  281. return NULL;
  282. }
  283. }
  284. if((p->inBufSamples = bufferframes*inchannels) != 0){
  285. if((p->inputBuffer[0] = (short *) calloc(p->inBufSamples, sizeof(short))) == NULL ||
  286. (p->inputBuffer[1] = (short *) calloc(p->inBufSamples, sizeof(short))) == NULL){
  287. android_CloseAudioDevice(p);
  288. return NULL;
  289. }
  290. }
  291. p->currentInputIndex = 0;
  292. p->currentOutputBuffer = 0;
  293. p->outputBufferSize[0] = 0;
  294. p->outputBufferSize[1] = 0;
  295. p->inputBufferSize[0] = 0;
  296. p->inputBufferSize[1] = 0;
  297. p->currentInputIndex = p->inBufSamples;
  298. p->currentInputBuffer = 0;
  299. if(openSLCreateEngine(p) != SL_RESULT_SUCCESS) {
  300. android_CloseAudioDevice(p);
  301. return NULL;
  302. }
  303. if(openSLRecOpen(p) != SL_RESULT_SUCCESS) {
  304. android_CloseAudioDevice(p);
  305. return NULL;
  306. }
  307. if(openSLPlayOpen(p) != SL_RESULT_SUCCESS) {
  308. android_CloseAudioDevice(p);
  309. return NULL;
  310. }
  311. notifyThreadLock(p->outlock);
  312. notifyThreadLock(p->inlock);
  313. p->time = 0.;
  314. if (p->bqPlayerBufferQueue)
  315. (*p->bqPlayerBufferQueue)->Enqueue(p->bqPlayerBufferQueue,
  316. p->outputBuffer[0], p->outBufSamples*sizeof(short));
  317. if (p->recorderBufferQueue)
  318. (*p->recorderBufferQueue)->Enqueue(p->recorderBufferQueue,
  319. p->inputBuffer[0], p->inBufSamples*sizeof(short));
  320. return p;
  321. }
  322. // close the android audio device
  323. void android_CloseAudioDevice(OPENSL_STREAM *p){
  324. if (p == NULL)
  325. return;
  326. openSLDestroyEngine(p);
  327. if (p->inlock != NULL) {
  328. notifyThreadLock(p->inlock);
  329. destroyThreadLock(p->inlock);
  330. p->inlock = NULL;
  331. }
  332. if (p->outlock != NULL) {
  333. notifyThreadLock(p->outlock);
  334. destroyThreadLock(p->outlock);
  335. p->inlock = NULL;
  336. }
  337. if (p->outputBuffer[0] != NULL) {
  338. free(p->outputBuffer[0]);
  339. p->outputBuffer[0] = NULL;
  340. }
  341. if (p->outputBuffer[1] != NULL) {
  342. free(p->outputBuffer[1]);
  343. p->outputBuffer[1] = NULL;
  344. }
  345. if (p->inputBuffer[0] != NULL) {
  346. free(p->inputBuffer[0]);
  347. p->inputBuffer[0] = NULL;
  348. }
  349. if (p->inputBuffer[1] != NULL) {
  350. free(p->inputBuffer[1]);
  351. p->inputBuffer[1] = NULL;
  352. }
  353. free(p);
  354. }
  355. // returns timestamp of the processed stream
  356. double android_GetTimestamp(OPENSL_STREAM *p){
  357. return p->time;
  358. }
  359. // this callback handler is called every time a buffer finishes recording
  360. void bqRecorderCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
  361. {
  362. OPENSL_STREAM *p = (OPENSL_STREAM *) context;
  363. while ((p->inputBufferSize[0] > 0) && (p->inputBufferSize[1] > 0))
  364. usleep(1000);
  365. p->currentInputBuffer = (p->currentInputBuffer ? 0 : 1);
  366. int queueInputBuffer = (p->currentInputBuffer ? 0 : 1);
  367. int size = p->inBufSamples;
  368. short *inBuffer = p->inputBuffer[queueInputBuffer];
  369. (*p->recorderBufferQueue)->Enqueue(p->recorderBufferQueue,
  370. inBuffer, size * sizeof(short));
  371. p->inputBufferSize[queueInputBuffer] = size;
  372. notifyThreadLock(p->inlock);
  373. }
  374. // gets a buffer of size samples from the device
  375. int android_AudioIn(OPENSL_STREAM *p,float *buffer,int size){
  376. short *inBuffer;
  377. int i, bufsamps, index;
  378. if(p == NULL) return 0;
  379. bufsamps = p->inBufSamples;
  380. if(bufsamps == 0) return 0;
  381. if (size > bufsamps) return 0;
  382. if (p->inputBufferSize[p->currentInputBuffer] == 0)
  383. waitThreadLock(p->inlock);
  384. index = 0;
  385. inBuffer = p->inputBuffer[p->currentInputBuffer];
  386. for(i=0; i < size; i++){
  387. buffer[i] = (float) inBuffer[index++]*CONVMYFLT;
  388. }
  389. p->inputBufferSize[p->currentInputBuffer] = 0;
  390. if(p->outchannels == 0) p->time += (double) size/(p->sr*p->inchannels);
  391. return size;
  392. }
  393. // this callback handler is called every time a buffer finishes playing
  394. void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
  395. {
  396. OPENSL_STREAM *p = (OPENSL_STREAM *) context;
  397. while ((p->outputBufferSize[0] == 0) && (p->outputBufferSize[1] == 0))
  398. usleep(1000);
  399. p->currentOutputBuffer = (p->currentOutputBuffer ? 0 : 1);
  400. int queueOutputBuffer = (p->currentOutputBuffer ? 0 : 1);
  401. int size = p->outputBufferSize[queueOutputBuffer];
  402. short *outBuffer = p->outputBuffer[queueOutputBuffer];
  403. (*p->bqPlayerBufferQueue)->Enqueue(p->bqPlayerBufferQueue,
  404. outBuffer, size * sizeof(short));
  405. p->outputBufferSize[queueOutputBuffer] = 0;
  406. notifyThreadLock(p->outlock);
  407. }
  408. // puts a buffer of size samples to the device
  409. int android_AudioOut(OPENSL_STREAM *p, float *buffer,int size){
  410. short *outBuffer;
  411. int i, bufsamps, index;
  412. if(p == NULL) return 0;
  413. bufsamps = p->outBufSamples;
  414. if(bufsamps == 0) return 0;
  415. if (size > bufsamps) return 0;
  416. if (p->outputBufferSize[p->currentOutputBuffer] != 0)
  417. waitThreadLock(p->outlock);
  418. index = 0;
  419. outBuffer = p->outputBuffer[p->currentOutputBuffer];
  420. for(i=0; i < size; i++){
  421. outBuffer[index++] = (short) (buffer[i]*CONV16BIT);
  422. }
  423. p->outputBufferSize[p->currentOutputBuffer] = size;
  424. p->time += (double) size/(p->sr*p->outchannels);
  425. return size;
  426. }
  427. //----------------------------------------------------------------------
  428. // thread Locks
  429. // to ensure synchronisation between callbacks and processing code
  430. void* createThreadLock(void)
  431. {
  432. threadLock *p;
  433. p = (threadLock*) malloc(sizeof(threadLock));
  434. if (p == NULL)
  435. return NULL;
  436. memset(p, 0, sizeof(threadLock));
  437. if (pthread_mutex_init(&(p->m), (pthread_mutexattr_t*) NULL) != 0) {
  438. free((void*) p);
  439. return NULL;
  440. }
  441. if (pthread_cond_init(&(p->c), (pthread_condattr_t*) NULL) != 0) {
  442. pthread_mutex_destroy(&(p->m));
  443. free((void*) p);
  444. return NULL;
  445. }
  446. p->s = (unsigned char) 1;
  447. return p;
  448. }
  449. int waitThreadLock(void *lock)
  450. {
  451. threadLock *p;
  452. int retval = 0;
  453. p = (threadLock*) lock;
  454. pthread_mutex_lock(&(p->m));
  455. p->s = (unsigned char) 0;
  456. while (!p->s) {
  457. pthread_cond_wait(&(p->c), &(p->m));
  458. }
  459. pthread_mutex_unlock(&(p->m));
  460. return retval;
  461. }
  462. void notifyThreadLock(void *lock)
  463. {
  464. threadLock *p;
  465. p = (threadLock*) lock;
  466. pthread_mutex_lock(&(p->m));
  467. p->s = (unsigned char) 1;
  468. pthread_cond_signal(&(p->c));
  469. pthread_mutex_unlock(&(p->m));
  470. return;
  471. }
  472. void destroyThreadLock(void *lock)
  473. {
  474. threadLock *p;
  475. p = (threadLock*) lock;
  476. if (p == NULL)
  477. return;
  478. notifyThreadLock(p);
  479. pthread_cond_destroy(&(p->c));
  480. pthread_mutex_destroy(&(p->m));
  481. free(p);
  482. }