Debian Codegraph @

Xpjproject/pjmedia/src/pjmedia-audiodev/opensl_dev.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2012-2012 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2010-2012 Regis Montoya (aka r3gis - www.r3gis.fr)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 /* This file is the implementation of Android OpenSL ES audio device.
20 * The original code was originally part of CSipSimple
21 * (http://code.google.com/p/csipsimple/) and was kindly donated
22 * by Regis Montoya.
23 */
24
25 #include <pjmedia-audiodev/audiodev_imp.h>
26 #include <pj/assert.h>
27 #include <pj/log.h>
28 #include <pj/os.h>
29 #include <pj/string.h>
30 #include <pjmedia/errno.h>
31 #include <pjmedia/event.h>
32
33 #if defined(PJMEDIA_AUDIO_DEV_HAS_OPENSL) && PJMEDIA_AUDIO_DEV_HAS_OPENSL != 0
34
35 #include <SLES/OpenSLES.h>
36
37 #ifdef __ANDROID__
38 #include <SLES/OpenSLES_Android.h>
39 #include <SLES/OpenSLES_AndroidConfiguration.h>
40 #include <sys/system_properties.h>
41 #include <android/api-level.h>
42
43 #define W_SLBufferQueueItf SLAndroidSimpleBufferQueueItf
44 #define W_SLBufferQueueState SLAndroidSimpleBufferQueueState
45 #define W_SL_IID_BUFFERQUEUE SL_IID_ANDROIDSIMPLEBUFFERQUEUE
46 #else
47 #define W_SLBufferQueueItf SLBufferQueueItf
48 #define W_SLBufferQueueState SLBufferQueueState
49 #define W_SL_IID_BUFFERQUEUE SL_IID_BUFFERQUEUE
50 #endif
51
52 #define THIS_FILE "opensl_dev.c"
53 #define DRIVER_NAME "OpenSL"
54
55 #define NUM_BUFFERS 2
56
57 struct opensl_aud_factory
58 {
59 pjmedia_aud_dev_factory base;
60 pj_pool_factory *pf;
61 pj_pool_t *pool;
62
63 SLObjectItf engineObject;
64 SLEngineItf engineEngine;
65 SLObjectItf outputMixObject;
66 };
67
68 /*
69 * Sound stream descriptor.
70 * This struct may be used for both unidirectional or bidirectional sound
71 * streams.
72 */
73 struct opensl_aud_stream
74 {
75 pjmedia_aud_stream base;
76 pj_pool_t *pool;
77 pj_str_t name;
78 pjmedia_dir dir;
79 pjmedia_aud_param param;
80
81 void *user_data;
82 pj_bool_t quit_flag;
83 pjmedia_aud_rec_cb rec_cb;
84 pjmedia_aud_play_cb play_cb;
85
86 pj_timestamp play_timestamp;
87 pj_timestamp rec_timestamp;
88
89 pj_bool_t rec_thread_initialized;
90 pj_thread_desc rec_thread_desc;
91 pj_thread_t *rec_thread;
92
93 pj_bool_t play_thread_initialized;
94 pj_thread_desc play_thread_desc;
95 pj_thread_t *play_thread;
96
97 /* Player */
98 SLObjectItf playerObj;
99 SLPlayItf playerPlay;
100 SLVolumeItf playerVol;
101 unsigned playerBufferSize;
102 char *playerBuffer[NUM_BUFFERS];
103 int playerBufIdx;
104
105 /* Recorder */
106 SLObjectItf recordObj;
107 SLRecordItf recordRecord;
108 unsigned recordBufferSize;
109 char *recordBuffer[NUM_BUFFERS];
110 int recordBufIdx;
111
112 W_SLBufferQueueItf playerBufQ;
113 W_SLBufferQueueItf recordBufQ;
114 };
115
116 /* Factory prototypes */
117 static pj_status_t opensl_init(pjmedia_aud_dev_factory *f);
118 static pj_status_t opensl_destroy(pjmedia_aud_dev_factory *f);
119 static pj_status_t opensl_refresh(pjmedia_aud_dev_factory *f);
120 static unsigned opensl_get_dev_count(pjmedia_aud_dev_factory *f);
121 static pj_status_t opensl_get_dev_info(pjmedia_aud_dev_factory *f,
122 unsigned index,
123 pjmedia_aud_dev_info *info);
124 static pj_status_t opensl_default_param(pjmedia_aud_dev_factory *f,
125 unsigned index,
126 pjmedia_aud_param *param);
127 static pj_status_t opensl_create_stream(pjmedia_aud_dev_factory *f,
128 const pjmedia_aud_param *param,
129 pjmedia_aud_rec_cb rec_cb,
130 pjmedia_aud_play_cb play_cb,
131 void *user_data,
132 pjmedia_aud_stream **p_aud_strm);
133
134 /* Stream prototypes */
135 static pj_status_t strm_get_param(pjmedia_aud_stream *strm,
136 pjmedia_aud_param *param);
137 static pj_status_t strm_get_cap(pjmedia_aud_stream *strm,
138 pjmedia_aud_dev_cap cap,
139 void *value);
140 static pj_status_t strm_set_cap(pjmedia_aud_stream *strm,
141 pjmedia_aud_dev_cap cap,
142 const void *value);
143 static pj_status_t strm_start(pjmedia_aud_stream *strm);
144 static pj_status_t strm_stop(pjmedia_aud_stream *strm);
145 static pj_status_t strm_destroy(pjmedia_aud_stream *strm);
146
147 static pjmedia_aud_dev_factory_op opensl_op =
148 {
149 &opensl_init,
150 &opensl_destroy,
151 &opensl_get_dev_count,
152 &opensl_get_dev_info,
153 &opensl_default_param,
154 &opensl_create_stream,
155 &opensl_refresh
156 };
157
158 static pjmedia_aud_stream_op opensl_strm_op =
159 {
160 &strm_get_param,
161 &strm_get_cap,
162 &strm_set_cap,
163 &strm_start,
164 &strm_stop,
165 &strm_destroy
166 };
167
168 /* This callback is called every time a buffer finishes playing. */
169 void bqPlayerCallback(W_SLBufferQueueItf bq, void *context)
170 {
171 struct opensl_aud_stream *stream = (struct opensl_aud_stream*) context;
172 SLresult result;
173 int status;
174
175 pj_assert(context != NULL);
176 pj_assert(bq == stream->playerBufQ);
177
178 if (stream->play_thread_initialized == 0 || !pj_thread_is_registered())
179 {
180 pj_bzero(stream->play_thread_desc, sizeof(pj_thread_desc));
181 status = pj_thread_register("opensl_play", stream->play_thread_desc,
182 &stream->play_thread);
183 stream->play_thread_initialized = 1;
184 PJ_LOG(5, (THIS_FILE, "Player thread started"));
185 }
186
187 if (!stream->quit_flag) {
188 pjmedia_frame frame;
189 char * buf;
190
191 frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
192 frame.buf = buf = stream->playerBuffer[stream->playerBufIdx++];
193 frame.size = stream->playerBufferSize;
194 frame.timestamp.u64 = stream->play_timestamp.u64;
195 frame.bit_info = 0;
196
197 status = (*stream->play_cb)(stream->user_data, &frame);
198 if (status != PJ_SUCCESS) {
199 if (!stream->quit_flag) {
200 pjmedia_event e;
201
202 PJ_PERROR(3, (THIS_FILE, status, "OpenSL playback callback stopped due to error"));
203
204 /* Broadcast OpenSL playback error */
205 pjmedia_event_init(&e, PJMEDIA_EVENT_AUD_DEV_ERROR,
206 &stream->play_timestamp, &stream->base);
207 e.data.aud_dev_err.dir = PJMEDIA_DIR_PLAYBACK;
208 e.data.aud_dev_err.status = status;
209 e.data.aud_dev_err.id = stream->param.play_id;
210
211 pjmedia_event_publish(NULL, &stream->base, &e,
212 PJMEDIA_EVENT_PUBLISH_DEFAULT);
213 }
214 }
215
216 if (status != PJ_SUCCESS || frame.type != PJMEDIA_FRAME_TYPE_AUDIO) {
217 if (frame.type != PJMEDIA_FRAME_TYPE_AUDIO)
218 pj_bzero(buf, stream->playerBufferSize);
219 if (status != PJ_SUCCESS)
220 return;
221 }
222
223 stream->play_timestamp.u64 += stream->param.samples_per_frame /
224 stream->param.channel_count;
225
226 result = (*bq)->Enqueue(bq, buf, stream->playerBufferSize);
227 if (result != SL_RESULT_SUCCESS) {
228 pjmedia_event e;
229 pj_status_t pj_status = opensl_to_pj_error(result);
230
231 PJ_LOG(3, (THIS_FILE, "Unable to enqueue next player buffer !!! %d",
232 result));
233
234 if (!stream->quit_flag) {
235 PJ_PERROR(3, (THIS_FILE, pj_status, "OpenSL playback enqueue failed"));
236
237 /* Broadcast OpenSL playback error */
238 pjmedia_event_init(&e, PJMEDIA_EVENT_AUD_DEV_ERROR,
239 &stream->play_timestamp, &stream->base);
240 e.data.aud_dev_err.dir = PJMEDIA_DIR_PLAYBACK;
241 e.data.aud_dev_err.status = pj_status;
242 e.data.aud_dev_err.id = stream->param.play_id;
243
244 pjmedia_event_publish(NULL, &stream->base, &e,
245 PJMEDIA_EVENT_PUBLISH_DEFAULT);
246 }
247 return;
248 }
249
250 stream->playerBufIdx %= NUM_BUFFERS;
251 }
252 }
253
254 /* This callback handler is called every time a buffer finishes recording */
255 void bqRecorderCallback(W_SLBufferQueueItf bq, void *context)
256 {
257 struct opensl_aud_stream *stream = (struct opensl_aud_stream*) context;
258 SLresult result;
259 int status;
260
261 pj_assert(context != NULL);
262 pj_assert(bq == stream->recordBufQ);
263
264 if (stream->rec_thread_initialized == 0 || !pj_thread_is_registered())
265 {
266 pj_bzero(stream->rec_thread_desc, sizeof(pj_thread_desc));
267 status = pj_thread_register("opensl_rec", stream->rec_thread_desc,
268 &stream->rec_thread);
269 PJ_UNUSED_ARG(status); /* Unused for now.. */
270 stream->rec_thread_initialized = 1;
271 PJ_LOG(5, (THIS_FILE, "Recorder thread started"));
272 }
273
274 if (!stream->quit_flag) {
275 pjmedia_frame frame;
276 char *buf;
277
278 frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
279 frame.buf = buf = stream->recordBuffer[stream->recordBufIdx++];
280 frame.size = stream->recordBufferSize;
281 frame.timestamp.u64 = stream->rec_timestamp.u64;
282 frame.bit_info = 0;
283
284 status = (*stream->rec_cb)(stream->user_data, &frame);
285 if (status != PJ_SUCCESS) {
286 if (!stream->quit_flag) {
287 pjmedia_event e;
288
289 PJ_PERROR(3, (THIS_FILE, status, "OpenSL record callback stopped due to error"));
290
291 /* Broadcast OpenSL record error */
292 pjmedia_event_init(&e, PJMEDIA_EVENT_AUD_DEV_ERROR,
293 &stream->rec_timestamp, &stream->base);
294 e.data.aud_dev_err.dir = PJMEDIA_DIR_CAPTURE;
295 e.data.aud_dev_err.status = status;
296 e.data.aud_dev_err.id = stream->param.rec_id;
297
298 pjmedia_event_publish(NULL, &stream->base, &e,
299 PJMEDIA_EVENT_PUBLISH_DEFAULT);
300 }
301 return;
302 }
303
304 stream->rec_timestamp.u64 += stream->param.samples_per_frame /
305 stream->param.channel_count;
306
307 /* And now enqueue next buffer */
308 result = (*bq)->Enqueue(bq, buf, stream->recordBufferSize);
309 if (result != SL_RESULT_SUCCESS) {
310 pjmedia_event e;
311 pj_status_t pj_status = opensl_to_pj_error(result);
312
313 PJ_LOG(3, (THIS_FILE, "Unable to enqueue next record buffer !!! %d",
314 result));
315
316 if (!stream->quit_flag) {
317 PJ_PERROR(3, (THIS_FILE, pj_status, "OpenSL record enqueue failed"));
318
319 /* Broadcast OpenSL record error */
320 pjmedia_event_init(&e, PJMEDIA_EVENT_AUD_DEV_ERROR,
321 &stream->rec_timestamp, &stream->base);
322 e.data.aud_dev_err.dir = PJMEDIA_DIR_CAPTURE;
323 e.data.aud_dev_err.status = pj_status;
324 e.data.aud_dev_err.id = stream->param.rec_id;
325
326 pjmedia_event_publish(NULL, &stream->base, &e,
327 PJMEDIA_EVENT_PUBLISH_DEFAULT);
328 }
329 return;
330 }
331
332 stream->recordBufIdx %= NUM_BUFFERS;
333 }
334 }
335
336 pj_status_t opensl_to_pj_error(SLresult code)
337 {
338 switch(code) {
339 case SL_RESULT_SUCCESS:
340 return PJ_SUCCESS;
341 case SL_RESULT_PRECONDITIONS_VIOLATED:
342 case SL_RESULT_PARAMETER_INVALID:
343 case SL_RESULT_CONTENT_CORRUPTED:
344 case SL_RESULT_FEATURE_UNSUPPORTED:
345 return PJMEDIA_EAUD_INVOP;
346 case SL_RESULT_MEMORY_FAILURE:
347 case SL_RESULT_BUFFER_INSUFFICIENT:
348 return PJ_ENOMEM;
349 case SL_RESULT_RESOURCE_ERROR:
350 case SL_RESULT_RESOURCE_LOST:
351 case SL_RESULT_CONTROL_LOST:
352 return PJMEDIA_EAUD_NOTREADY;
353 case SL_RESULT_CONTENT_UNSUPPORTED:
354 return PJ_ENOTSUP;
355 default:
356 return PJMEDIA_EAUD_ERR;
357 }
358 }
359
360 /* Init Android audio driver. */
361 pjmedia_aud_dev_factory* pjmedia_opensl_factory(pj_pool_factory *pf)
362 {
363 struct opensl_aud_factory *f;
364 pj_pool_t *pool;
365
366 pool = pj_pool_create(pf, "opensles", 256, 256, NULL);
367 f = PJ_POOL_ZALLOC_T(pool, struct opensl_aud_factory);
368 f->pf = pf;
369 f->pool = pool;
370 f->base.op = &opensl_op;
371
372 return &f->base;
373 }
374
375 /* API: Init factory */
376 static pj_status_t opensl_init(pjmedia_aud_dev_factory *f)
377 {
378 struct opensl_aud_factory *pa = (struct opensl_aud_factory*)f;
379 SLresult result;
380
381 /* Create engine */
382 result = slCreateEngine(&pa->engineObject, 0, NULL, 0, NULL, NULL);
383 if (result != SL_RESULT_SUCCESS) {
384 PJ_LOG(3, (THIS_FILE, "Cannot create engine %d ", result));
385 return opensl_to_pj_error(result);
386 }
387
388 /* Realize the engine */
389 result = (*pa->engineObject)->Realize(pa->engineObject, SL_BOOLEAN_FALSE);
390 if (result != SL_RESULT_SUCCESS) {
391 PJ_LOG(3, (THIS_FILE, "Cannot realize engine"));
392 opensl_destroy(f);
393 return opensl_to_pj_error(result);
394 }
395
396 /* Get the engine interface, which is needed in order to create
397 * other objects.
398 */
399 result = (*pa->engineObject)->GetInterface(pa->engineObject,
400 SL_IID_ENGINE,
401 &pa->engineEngine);
402 if (result != SL_RESULT_SUCCESS) {
403 PJ_LOG(3, (THIS_FILE, "Cannot get engine interface"));
404 opensl_destroy(f);
405 return opensl_to_pj_error(result);
406 }
407
408 /* Create output mix */
409 result = (*pa->engineEngine)->CreateOutputMix(pa->engineEngine,
410 &pa->outputMixObject,
411 0, NULL, NULL);
412 if (result != SL_RESULT_SUCCESS) {
413 PJ_LOG(3, (THIS_FILE, "Cannot create output mix"));
414 opensl_destroy(f);
415 return opensl_to_pj_error(result);
416 }
417
418 /* Realize the output mix */
419 result = (*pa->outputMixObject)->Realize(pa->outputMixObject,
420 SL_BOOLEAN_FALSE);
421 if (result != SL_RESULT_SUCCESS) {
422 PJ_LOG(3, (THIS_FILE, "Cannot realize output mix"));
423 opensl_destroy(f);
424 return opensl_to_pj_error(result);
425 }
426
427 PJ_LOG(4,(THIS_FILE, "OpenSL sound library initialized"));
428 return PJ_SUCCESS;
429 }
430
431 /* API: Destroy factory */
432 static pj_status_t opensl_destroy(pjmedia_aud_dev_factory *f)
433 {
434 struct opensl_aud_factory *pa = (struct opensl_aud_factory*)f;
435 pj_pool_t *pool;
436
437 PJ_LOG(4,(THIS_FILE, "OpenSL sound library shutting down.."));
438
439 /* Destroy Output Mix object */
440 if (pa->outputMixObject) {
441 (*pa->outputMixObject)->Destroy(pa->outputMixObject);
442 pa->outputMixObject = NULL;
443 }
444
445 /* Destroy engine object, and invalidate all associated interfaces */
446 if (pa->engineObject) {
447 (*pa->engineObject)->Destroy(pa->engineObject);
448 pa->engineObject = NULL;
449 pa->engineEngine = NULL;
450 }
451
452 pool = pa->pool;
453 pa->pool = NULL;
454 pj_pool_release(pool);
455
456 return PJ_SUCCESS;
457 }
458
459 /* API: refresh the list of devices */
460 static pj_status_t opensl_refresh(pjmedia_aud_dev_factory *f)
461 {
462 PJ_UNUSED_ARG(f);
463 return PJ_SUCCESS;
464 }
465
466 /* API: Get device count. */
467 static unsigned opensl_get_dev_count(pjmedia_aud_dev_factory *f)
468 {
469 PJ_UNUSED_ARG(f);
470 return 1;
471 }
472
473 /* API: Get device info. */
474 static pj_status_t opensl_get_dev_info(pjmedia_aud_dev_factory *f,
475 unsigned index,
476 pjmedia_aud_dev_info *info)
477 {
478 PJ_UNUSED_ARG(f);
479
480 pj_bzero(info, sizeof(*info));
481
482 pj_ansi_strxcpy(info->name, "OpenSL ES Audio", sizeof(info->name));
483 pj_ansi_strxcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
484 info->default_samples_per_sec = 8000;
485 info->caps = PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING;
486 info->input_count = 1;
487 info->output_count = 1;
488
489 return PJ_SUCCESS;
490 }
491
492 /* API: fill in with default parameter. */
493 static pj_status_t opensl_default_param(pjmedia_aud_dev_factory *f,
494 unsigned index,
495 pjmedia_aud_param *param)
496 {
497
498 pjmedia_aud_dev_info adi;
499 pj_status_t status;
500
501 status = opensl_get_dev_info(f, index, &adi);
502 if (status != PJ_SUCCESS)
503 return status;
504
505 pj_bzero(param, sizeof(*param));
506 if (adi.input_count && adi.output_count) {
507 param->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
508 param->rec_id = index;
509 param->play_id = index;
510 } else if (adi.input_count) {
511 param->dir = PJMEDIA_DIR_CAPTURE;
512 param->rec_id = index;
513 param->play_id = PJMEDIA_AUD_INVALID_DEV;
514 } else if (adi.output_count) {
515 param->dir = PJMEDIA_DIR_PLAYBACK;
516 param->play_id = index;
517 param->rec_id = PJMEDIA_AUD_INVALID_DEV;
518 } else {
519 return PJMEDIA_EAUD_INVDEV;
520 }
521
522 param->clock_rate = adi.default_samples_per_sec;
523 param->channel_count = 1;
524 param->samples_per_frame = adi.default_samples_per_sec * 20 / 1000;
525 param->bits_per_sample = 16;
526 param->input_latency_ms = PJMEDIA_SND_DEFAULT_REC_LATENCY;
527 param->output_latency_ms = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
528
529 return PJ_SUCCESS;
530 }
531
532 /* API: create stream */
533 static pj_status_t opensl_create_stream(pjmedia_aud_dev_factory *f,
534 const pjmedia_aud_param *param,
535 pjmedia_aud_rec_cb rec_cb,
536 pjmedia_aud_play_cb play_cb,
537 void *user_data,
538 pjmedia_aud_stream **p_aud_strm)
539 {
540 /* Audio sink for recorder and audio source for player */
541 #ifdef __ANDROID__
542 SLDataLocator_AndroidSimpleBufferQueue loc_bq =
543 { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, NUM_BUFFERS };
544 #else
545 SLDataLocator_BufferQueue loc_bq =
546 { SL_DATALOCATOR_BUFFERQUEUE, NUM_BUFFERS };
547 #endif
548 struct opensl_aud_factory *pa = (struct opensl_aud_factory*)f;
549 pj_pool_t *pool;
550 struct opensl_aud_stream *stream;
551 int i, bufferSize;
552 SLresult result;
553 SLDataFormat_PCM format_pcm;
554
555 /* Only supports for mono channel for now */
556 PJ_ASSERT_RETURN(param->channel_count == 1, PJ_EINVAL);
557 PJ_ASSERT_RETURN(play_cb && rec_cb && p_aud_strm, PJ_EINVAL);
558
559 PJ_LOG(4,(THIS_FILE, "Creating OpenSL stream"));
560
561 pool = pj_pool_create(pa->pf, "openslstrm", 1024, 1024, NULL);
562 if (!pool)
563 return PJ_ENOMEM;
564
565 stream = PJ_POOL_ZALLOC_T(pool, struct opensl_aud_stream);
566 stream->pool = pool;
567 pj_strdup2_with_null(pool, &stream->name, "OpenSL");
568 stream->dir = param->dir;
569 pj_memcpy(&stream->param, param, sizeof(*param));
570 stream->user_data = user_data;
571 stream->rec_cb = rec_cb;
572 stream->play_cb = play_cb;
573 bufferSize = param->samples_per_frame * param->bits_per_sample / 8;
574
575 /* Configure audio PCM format */
576 format_pcm.formatType = SL_DATAFORMAT_PCM;
577 format_pcm.numChannels = param->channel_count;
578 /* Here samples per sec should be supported else we will get an error */
579 format_pcm.samplesPerSec = (SLuint32) param->clock_rate * 1000;
580 format_pcm.bitsPerSample = (SLuint16) param->bits_per_sample;
581 format_pcm.containerSize = (SLuint16) param->bits_per_sample;
582 format_pcm.channelMask = SL_SPEAKER_FRONT_CENTER;
583 format_pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
584
585 if (stream->dir & PJMEDIA_DIR_PLAYBACK) {
586 /* Audio source */
587 SLDataSource audioSrc = {&loc_bq, &format_pcm};
588 /* Audio sink */
589 SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX,
590 pa->outputMixObject};
591 SLDataSink audioSnk = {&loc_outmix, NULL};
592 /* Audio interface */
593 #ifdef __ANDROID__
594 int numIface = 3;
595 const SLInterfaceID ids[3] = {SL_IID_BUFFERQUEUE,
596 SL_IID_VOLUME,
597 SL_IID_ANDROIDCONFIGURATION};
598 const SLboolean req[3] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE,
599 SL_BOOLEAN_TRUE};
600 SLAndroidConfigurationItf playerConfig;
601 SLint32 streamType = SL_ANDROID_STREAM_VOICE;
602 #else
603 int numIface = 2;
604 const SLInterfaceID ids[2] = {SL_IID_BUFFERQUEUE,
605 SL_IID_VOLUME};
606 const SLboolean req[2] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
607 #endif
608
609 /* Create audio player */
610 result = (*pa->engineEngine)->CreateAudioPlayer(pa->engineEngine,
611 &stream->playerObj,
612 &audioSrc, &audioSnk,
613 numIface, ids, req);
614 if (result != SL_RESULT_SUCCESS) {
615 PJ_LOG(3, (THIS_FILE, "Cannot create audio player: %d", result));
616 goto on_error;
617 }
618
619 #ifdef __ANDROID__
620 /* Set Android configuration */
621 result = (*stream->playerObj)->GetInterface(stream->playerObj,
622 SL_IID_ANDROIDCONFIGURATION,
623 &playerConfig);
624 if (result == SL_RESULT_SUCCESS && playerConfig) {
625 result = (*playerConfig)->SetConfiguration(
626 playerConfig, SL_ANDROID_KEY_STREAM_TYPE,
627 &streamType, sizeof(SLint32));
628 }
629 if (result != SL_RESULT_SUCCESS) {
630 PJ_LOG(4, (THIS_FILE, "Warning: Unable to set android "
631 "player configuration"));
632 }
633 #endif
634
635 /* Realize the player */
636 result = (*stream->playerObj)->Realize(stream->playerObj,
637 SL_BOOLEAN_FALSE);
638 if (result != SL_RESULT_SUCCESS) {
639 PJ_LOG(3, (THIS_FILE, "Cannot realize player : %d", result));
640 goto on_error;
641 }
642
643 /* Get the play interface */
644 result = (*stream->playerObj)->GetInterface(stream->playerObj,
645 SL_IID_PLAY,
646 &stream->playerPlay);
647 if (result != SL_RESULT_SUCCESS) {
648 PJ_LOG(3, (THIS_FILE, "Cannot get play interface"));
649 goto on_error;
650 }
651
652 /* Get the buffer queue interface */
653 result = (*stream->playerObj)->GetInterface(stream->playerObj,
654 SL_IID_BUFFERQUEUE,
655 &stream->playerBufQ);
656 if (result != SL_RESULT_SUCCESS) {
657 PJ_LOG(3, (THIS_FILE, "Cannot get buffer queue interface"));
658 goto on_error;
659 }
660
661 /* Get the volume interface */
662 result = (*stream->playerObj)->GetInterface(stream->playerObj,
663 SL_IID_VOLUME,
664 &stream->playerVol);
665
666 /* Register callback on the buffer queue */
667 result = (*stream->playerBufQ)->RegisterCallback(stream->playerBufQ,
668 bqPlayerCallback,
669 (void *)stream);
670 if (result != SL_RESULT_SUCCESS) {
671 PJ_LOG(3, (THIS_FILE, "Cannot register player callback"));
672 goto on_error;
673 }
674
675 stream->playerBufferSize = bufferSize;
676 for (i = 0; i < NUM_BUFFERS; i++) {
677 stream->playerBuffer[i] = (char *)
678 pj_pool_alloc(stream->pool,
679 stream->playerBufferSize);
680 }
681 }
682
683 if (stream->dir & PJMEDIA_DIR_CAPTURE) {
684 /* Audio source */
685 SLDataLocator_IODevice loc_dev = {SL_DATALOCATOR_IODEVICE,
686 SL_IODEVICE_AUDIOINPUT,
687 SL_DEFAULTDEVICEID_AUDIOINPUT,
688 NULL};
689 SLDataSource audioSrc = {&loc_dev, NULL};
690 /* Audio sink */
691 SLDataSink audioSnk = {&loc_bq, &format_pcm};
692 /* Audio interface */
693 #ifdef __ANDROID__
694 int numIface = 2;
695 const SLInterfaceID ids[2] = {W_SL_IID_BUFFERQUEUE,
696 SL_IID_ANDROIDCONFIGURATION};
697 const SLboolean req[2] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
698 SLAndroidConfigurationItf recorderConfig;
699 #else
700 int numIface = 1;
701 const SLInterfaceID ids[1] = {W_SL_IID_BUFFERQUEUE};
702 const SLboolean req[1] = {SL_BOOLEAN_TRUE};
703 #endif
704
705 /* Create audio recorder
706 * (requires the RECORD_AUDIO permission)
707 */
708 result = (*pa->engineEngine)->CreateAudioRecorder(pa->engineEngine,
709 &stream->recordObj,
710 &audioSrc, &audioSnk,
711 numIface, ids, req);
712 if (result != SL_RESULT_SUCCESS) {
713 PJ_LOG(3, (THIS_FILE, "Cannot create recorder: %d", result));
714 goto on_error;
715 }
716
717 #ifdef __ANDROID__
718 /* Set Android configuration */
719 result = (*stream->recordObj)->GetInterface(stream->recordObj,
720 SL_IID_ANDROIDCONFIGURATION,
721 &recorderConfig);
722 if (result == SL_RESULT_SUCCESS) {
723 SLint32 streamType = SL_ANDROID_RECORDING_PRESET_GENERIC;
724 #if __ANDROID_API__ >= 14
725 streamType = SL_ANDROID_RECORDING_PRESET_VOICE_COMMUNICATION;
726 #endif
727 #if 0
728 /* Android-L (android-21) removes __system_property_get
729 * from the NDK.
730 */
731 char sdk_version[PROP_VALUE_MAX];
732 pj_str_t pj_sdk_version;
733 int sdk_v;
734
735 __system_property_get("ro.build.version.sdk", sdk_version);
736 pj_sdk_version = pj_str(sdk_version);
737 sdk_v = pj_strtoul(&pj_sdk_version);
738 if (sdk_v >= 14)
739 streamType = SL_ANDROID_RECORDING_PRESET_VOICE_COMMUNICATION;
740 PJ_LOG(4, (THIS_FILE, "Recording stream type %d, SDK : %d",
741 streamType, sdk_v));
742 #endif
743 result = (*recorderConfig)->SetConfiguration(
744 recorderConfig, SL_ANDROID_KEY_RECORDING_PRESET,
745 &streamType, sizeof(SLint32));
746 }
747 if (result != SL_RESULT_SUCCESS) {
748 PJ_LOG(4, (THIS_FILE, "Warning: Unable to set android "
749 "recorder configuration"));
750 }
751 #endif
752
753 /* Realize the recorder */
754 result = (*stream->recordObj)->Realize(stream->recordObj,
755 SL_BOOLEAN_FALSE);
756 if (result != SL_RESULT_SUCCESS) {
757 PJ_LOG(3, (THIS_FILE, "Cannot realize recorder : %d", result));
758 goto on_error;
759 }
760
761 /* Get the record interface */
762 result = (*stream->recordObj)->GetInterface(stream->recordObj,
763 SL_IID_RECORD,
764 &stream->recordRecord);
765 if (result != SL_RESULT_SUCCESS) {
766 PJ_LOG(3, (THIS_FILE, "Cannot get record interface"));
767 goto on_error;
768 }
769
770 /* Get the buffer queue interface */
771 result = (*stream->recordObj)->GetInterface(
772 stream->recordObj, W_SL_IID_BUFFERQUEUE,
773 &stream->recordBufQ);
774 if (result != SL_RESULT_SUCCESS) {
775 PJ_LOG(3, (THIS_FILE, "Cannot get recorder buffer queue iface"));
776 goto on_error;
777 }
778
779 /* Register callback on the buffer queue */
780 result = (*stream->recordBufQ)->RegisterCallback(stream->recordBufQ,
781 bqRecorderCallback,
782 (void *) stream);
783 if (result != SL_RESULT_SUCCESS) {
784 PJ_LOG(3, (THIS_FILE, "Cannot register recorder callback"));
785 goto on_error;
786 }
787
788 stream->recordBufferSize = bufferSize;
789 for (i = 0; i < NUM_BUFFERS; i++) {
790 stream->recordBuffer[i] = (char *)
791 pj_pool_alloc(stream->pool,
792 stream->recordBufferSize);
793 }
794
795 }
796
797 if (param->flags & PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING) {
798 strm_set_cap(&stream->base, PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING,
799 &param->output_vol);
800 }
801
802 /* Done */
803 stream->base.op = &opensl_strm_op;
804 *p_aud_strm = &stream->base;
805 return PJ_SUCCESS;
806
807 on_error:
808 strm_destroy(&stream->base);
809 return opensl_to_pj_error(result);
810 }
811
812 /* API: Get stream parameters */
813 static pj_status_t strm_get_param(pjmedia_aud_stream *s,
814 pjmedia_aud_param *pi)
815 {
816 struct opensl_aud_stream *strm = (struct opensl_aud_stream*)s;
817 PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL);
818 pj_memcpy(pi, &strm->param, sizeof(*pi));
819
820 if (strm_get_cap(s, PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING,
821 &pi->output_vol) == PJ_SUCCESS)
822 {
823 pi->flags |= PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING;
824 }
825
826 return PJ_SUCCESS;
827 }
828
829 /* API: get capability */
830 static pj_status_t strm_get_cap(pjmedia_aud_stream *s,
831 pjmedia_aud_dev_cap cap,
832 void *pval)
833 {
834 struct opensl_aud_stream *strm = (struct opensl_aud_stream*)s;
835 pj_status_t status = PJMEDIA_EAUD_INVCAP;
836
837 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
838
839 if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING &&
840 (strm->param.dir & PJMEDIA_DIR_PLAYBACK))
841 {
842 if (strm->playerVol) {
843 SLresult res;
844 SLmillibel vol, mvol;
845
846 res = (*strm->playerVol)->GetMaxVolumeLevel(strm->playerVol,
847 &mvol);
848 if (res == SL_RESULT_SUCCESS) {
849 res = (*strm->playerVol)->GetVolumeLevel(strm->playerVol,
850 &vol);
851 if (res == SL_RESULT_SUCCESS) {
852 *(int *)pval = ((int)vol - SL_MILLIBEL_MIN) * 100 /
853 ((int)mvol - SL_MILLIBEL_MIN);
854 return PJ_SUCCESS;
855 }
856 }
857 }
858 }
859
860 return status;
861 }
862
863 /* API: set capability */
864 static pj_status_t strm_set_cap(pjmedia_aud_stream *s,
865 pjmedia_aud_dev_cap cap,
866 const void *value)
867 {
868 struct opensl_aud_stream *strm = (struct opensl_aud_stream*)s;
869
870 PJ_ASSERT_RETURN(s && value, PJ_EINVAL);
871
872 if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING &&
873 (strm->param.dir & PJMEDIA_DIR_PLAYBACK))
874 {
875 if (strm->playerVol) {
876 SLresult res;
877 SLmillibel vol, mvol;
878
879 res = (*strm->playerVol)->GetMaxVolumeLevel(strm->playerVol,
880 &mvol);
881 if (res == SL_RESULT_SUCCESS) {
882 vol = (SLmillibel)(*(int *)value *
883 ((int)mvol - SL_MILLIBEL_MIN) / 100 + SL_MILLIBEL_MIN);
884 res = (*strm->playerVol)->SetVolumeLevel(strm->playerVol,
885 vol);
886 if (res == SL_RESULT_SUCCESS)
887 return PJ_SUCCESS;
888 }
889 }
890 }
891
892 return PJMEDIA_EAUD_INVCAP;
893 }
894
895 /* API: start stream. */
896 static pj_status_t strm_start(pjmedia_aud_stream *s)
897 {
898 struct opensl_aud_stream *stream = (struct opensl_aud_stream*)s;
899 int i;
900 SLresult result = SL_RESULT_SUCCESS;
901
902 PJ_LOG(4, (THIS_FILE, "Starting %s stream..", stream->name.ptr));
903 stream->quit_flag = 0;
904
905 if (stream->recordBufQ && stream->recordRecord) {
906 /* Enqueue an empty buffer to be filled by the recorder
907 * (for streaming recording, we need to enqueue at least 2 empty
908 * buffers to start things off)
909 */
910 for (i = 0; i < NUM_BUFFERS; i++) {
911 result = (*stream->recordBufQ)->Enqueue(stream->recordBufQ,
912 stream->recordBuffer[i],
913 stream->recordBufferSize);
914 /* The most likely other result is SL_RESULT_BUFFER_INSUFFICIENT,
915 * which for this code would indicate a programming error
916 */
917 pj_assert(result == SL_RESULT_SUCCESS);
918 }
919
920 result = (*stream->recordRecord)->SetRecordState(
921 stream->recordRecord, SL_RECORDSTATE_RECORDING);
922 if (result != SL_RESULT_SUCCESS) {
923 PJ_LOG(3, (THIS_FILE, "Cannot start recorder"));
924 goto on_error;
925 }
926 }
927
928 if (stream->playerPlay && stream->playerBufQ) {
929 /* Set the player's state to playing */
930 result = (*stream->playerPlay)->SetPlayState(stream->playerPlay,
931 SL_PLAYSTATE_PLAYING);
932 if (result != SL_RESULT_SUCCESS) {
933 PJ_LOG(3, (THIS_FILE, "Cannot start player"));
934 goto on_error;
935 }
936
937 for (i = 0; i < NUM_BUFFERS; i++) {
938 pj_bzero(stream->playerBuffer[i], stream->playerBufferSize/100);
939 result = (*stream->playerBufQ)->Enqueue(stream->playerBufQ,
940 stream->playerBuffer[i],
941 stream->playerBufferSize/100);
942 pj_assert(result == SL_RESULT_SUCCESS);
943 }
944 }
945
946 PJ_LOG(4, (THIS_FILE, "%s stream started", stream->name.ptr));
947 return PJ_SUCCESS;
948
949 on_error:
950 if (result != SL_RESULT_SUCCESS)
951 strm_stop(&stream->base);
952 return opensl_to_pj_error(result);
953 }
954
955 /* API: stop stream. */
956 static pj_status_t strm_stop(pjmedia_aud_stream *s)
957 {
958 struct opensl_aud_stream *stream = (struct opensl_aud_stream*)s;
959
960 if (stream->quit_flag)
961 return PJ_SUCCESS;
962
963 PJ_LOG(4, (THIS_FILE, "Stopping stream"));
964
965 stream->quit_flag = 1;
966
967 if (stream->recordBufQ && stream->recordRecord) {
968 /* Stop recording and clear buffer queue */
969 (*stream->recordRecord)->SetRecordState(stream->recordRecord,
970 SL_RECORDSTATE_STOPPED);
971 (*stream->recordBufQ)->Clear(stream->recordBufQ);
972 }
973
974 if (stream->playerBufQ && stream->playerPlay) {
975 /* Wait until the PCM data is done playing, the buffer queue callback
976 * will continue to queue buffers until the entire PCM data has been
977 * played. This is indicated by waiting for the count member of the
978 * SLBufferQueueState to go to zero.
979 */
980 /*
981 SLresult result;
982 W_SLBufferQueueState state;
983
984 result = (*stream->playerBufQ)->GetState(stream->playerBufQ, &state);
985 while (state.count) {
986 (*stream->playerBufQ)->GetState(stream->playerBufQ, &state);
987 } */
988 /* Stop player */
989 (*stream->playerPlay)->SetPlayState(stream->playerPlay,
990 SL_PLAYSTATE_STOPPED);
991 }
992
993 PJ_LOG(4,(THIS_FILE, "OpenSL stream stopped"));
994
995 return PJ_SUCCESS;
996
997 }
998
999 /* API: destroy stream. */
1000 static pj_status_t strm_destroy(pjmedia_aud_stream *s)
1001 {
1002 struct opensl_aud_stream *stream = (struct opensl_aud_stream*)s;
1003
1004 /* Stop the stream */
1005 strm_stop(s);
1006
1007 if (stream->playerObj) {
1008 /* Destroy the player */
1009 (*stream->playerObj)->Destroy(stream->playerObj);
1010 /* Invalidate all associated interfaces */
1011 stream->playerObj = NULL;
1012 stream->playerPlay = NULL;
1013 stream->playerBufQ = NULL;
1014 stream->playerVol = NULL;
1015 }
1016
1017 if (stream->recordObj) {
1018 /* Destroy the recorder */
1019 (*stream->recordObj)->Destroy(stream->recordObj);
1020 /* Invalidate all associated interfaces */
1021 stream->recordObj = NULL;
1022 stream->recordRecord = NULL;
1023 stream->recordBufQ = NULL;
1024 }
1025
1026 pj_pool_release(stream->pool);
1027 PJ_LOG(4, (THIS_FILE, "OpenSL stream destroyed"));
1028
1029 return PJ_SUCCESS;
1030 }
1031
1032 #endif /* PJMEDIA_AUDIO_DEV_HAS_OPENSL */