Debian Codegraph @

Xpjproject/pjmedia/src/pjmedia-audiodev/android_jni_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 JNI 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_ANDROID_JNI) && \
34 PJMEDIA_AUDIO_DEV_HAS_ANDROID_JNI != 0
35
36 #include <jni.h>
37 #include <sys/resource.h>
38 #include <sys/system_properties.h>
39
40 #define THIS_FILE "android_jni_dev.c"
41 #define DRIVER_NAME "Android JNI"
42
43 struct android_aud_factory
44 {
45 pjmedia_aud_dev_factory base;
46 pj_pool_factory *pf;
47 pj_pool_t *pool;
48 };
49
50 /*
51 * Sound stream descriptor.
52 * This struct may be used for both unidirectional or bidirectional sound
53 * streams.
54 */
55 struct android_aud_stream
56 {
57 pjmedia_aud_stream base;
58 pj_pool_t *pool;
59 pj_str_t name;
60 pjmedia_dir dir;
61 pjmedia_aud_param param;
62
63 int bytes_per_sample;
64 pj_uint32_t samples_per_sec;
65 unsigned samples_per_frame;
66 int channel_count;
67 void *user_data;
68 pj_bool_t quit_flag;
69 pj_bool_t running;
70
71 /* Record */
72 jobject record;
73 jclass record_class;
74 unsigned rec_buf_size;
75 pjmedia_aud_rec_cb rec_cb;
76 pj_bool_t rec_thread_exited;
77 pj_thread_t *rec_thread;
78 pj_sem_t *rec_sem;
79 pj_timestamp rec_timestamp;
80
81 /* Track */
82 jobject track;
83 jclass track_class;
84 unsigned play_buf_size;
85 pjmedia_aud_play_cb play_cb;
86 pj_bool_t play_thread_exited;
87 pj_thread_t *play_thread;
88 pj_sem_t *play_sem;
89 pj_timestamp play_timestamp;
90 };
91
92 /* Factory prototypes */
93 static pj_status_t android_init(pjmedia_aud_dev_factory *f);
94 static pj_status_t android_destroy(pjmedia_aud_dev_factory *f);
95 static pj_status_t android_refresh(pjmedia_aud_dev_factory *f);
96 static unsigned android_get_dev_count(pjmedia_aud_dev_factory *f);
97 static pj_status_t android_get_dev_info(pjmedia_aud_dev_factory *f,
98 unsigned index,
99 pjmedia_aud_dev_info *info);
100 static pj_status_t android_default_param(pjmedia_aud_dev_factory *f,
101 unsigned index,
102 pjmedia_aud_param *param);
103 static pj_status_t android_create_stream(pjmedia_aud_dev_factory *f,
104 const pjmedia_aud_param *param,
105 pjmedia_aud_rec_cb rec_cb,
106 pjmedia_aud_play_cb play_cb,
107 void *user_data,
108 pjmedia_aud_stream **p_aud_strm);
109
110 /* Stream prototypes */
111 static pj_status_t strm_get_param(pjmedia_aud_stream *strm,
112 pjmedia_aud_param *param);
113 static pj_status_t strm_get_cap(pjmedia_aud_stream *strm,
114 pjmedia_aud_dev_cap cap,
115 void *value);
116 static pj_status_t strm_set_cap(pjmedia_aud_stream *strm,
117 pjmedia_aud_dev_cap cap,
118 const void *value);
119 static pj_status_t strm_start(pjmedia_aud_stream *strm);
120 static pj_status_t strm_stop(pjmedia_aud_stream *strm);
121 static pj_status_t strm_destroy(pjmedia_aud_stream *strm);
122
123 static pjmedia_aud_dev_factory_op android_op =
124 {
125 &android_init,
126 &android_destroy,
127 &android_get_dev_count,
128 &android_get_dev_info,
129 &android_default_param,
130 &android_create_stream,
131 &android_refresh
132 };
133
134 static pjmedia_aud_stream_op android_strm_op =
135 {
136 &strm_get_param,
137 &strm_get_cap,
138 &strm_set_cap,
139 &strm_start,
140 &strm_stop,
141 &strm_destroy
142 };
143
144 #define attach_jvm(jni_env) pj_jni_attach_jvm((void **)jni_env)
145 #define detach_jvm(attached) pj_jni_detach_jvm(attached)
146 #define THREAD_PRIORITY_AUDIO -16
147 #define THREAD_PRIORITY_URGENT_AUDIO -19
148
149
150 static int AndroidRecorderCallback(void *userData)
151 {
152 struct android_aud_stream *stream = (struct android_aud_stream *)userData;
153 jmethodID read_method=0, record_method=0, stop_method=0;
154 int size = stream->rec_buf_size / 2;
155 jshortArray inputBuffer;
156 jshort *buf;
157 JNIEnv *jni_env = 0;
158 pj_bool_t attached = attach_jvm(&jni_env);
159
160 PJ_ASSERT_RETURN(jni_env, 0);
161
162 if (!stream->record) {
163 goto on_return;
164 }
165
166 PJ_LOG(5, (THIS_FILE, "Recorder thread started"));
167
168 /* Get methods ids */
169 read_method = (*jni_env)->GetMethodID(jni_env, stream->record_class,
170 "read", "([SII)I");
171 record_method = (*jni_env)->GetMethodID(jni_env, stream->record_class,
172 "startRecording", "()V");
173 stop_method = (*jni_env)->GetMethodID(jni_env, stream->record_class,
174 "stop", "()V");
175 if (read_method==0 || record_method==0 || stop_method==0) {
176 PJ_LOG(3, (THIS_FILE, "Unable to get recording methods"));
177 goto on_return;
178 }
179
180 /* Create a buffer for frames read */
181 inputBuffer = (*jni_env)->NewShortArray(jni_env, size);
182 if (inputBuffer == 0) {
183 PJ_LOG(3, (THIS_FILE, "Unable to allocate input buffer"));
184 goto on_return;
185 }
186
187 /* Start recording */
188 pj_thread_set_prio(NULL, THREAD_PRIORITY_URGENT_AUDIO);
189 pj_sem_wait(stream->rec_sem);
190 if (!stream->quit_flag)
191 (*jni_env)->CallVoidMethod(jni_env, stream->record, record_method);
192
193 while (!stream->quit_flag) {
194 pjmedia_frame frame;
195 pj_status_t status;
196 int shortRead;
197
198 if (!stream->running) {
199 (*jni_env)->CallVoidMethod(jni_env, stream->record, stop_method);
200 pj_sem_wait(stream->rec_sem);
201 if (stream->quit_flag)
202 break;
203 (*jni_env)->CallVoidMethod(jni_env, stream->record, record_method);
204 }
205
206 shortRead = (*jni_env)->CallIntMethod(jni_env, stream->record,
207 read_method, inputBuffer,
208 0, size);
209 if (shortRead <= 0 || shortRead != size) {
210 PJ_LOG (4, (THIS_FILE, "Record thread : error %d reading data",
211 shortRead));
212 continue;
213 }
214
215 buf = (*jni_env)->GetShortArrayElements(jni_env, inputBuffer, 0);
216 frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
217 frame.size = stream->rec_buf_size;
218 frame.bit_info = 0;
219 frame.buf = (void *)buf;
220 frame.timestamp.u64 = stream->rec_timestamp.u64;
221
222 status = (*stream->rec_cb)(stream->user_data, &frame);
223 (*jni_env)->ReleaseShortArrayElements(jni_env, inputBuffer, buf,
224 JNI_ABORT);
225 if (status != PJ_SUCCESS || stream->quit_flag) {
226 if (status != PJ_SUCCESS && !stream->quit_flag) {
227 pjmedia_event e;
228
229 PJ_PERROR(3, (THIS_FILE, status, "Android JNI recorder callback stopped due to error"));
230
231 /* Broadcast Android JNI record error */
232 pjmedia_event_init(&e, PJMEDIA_EVENT_AUD_DEV_ERROR,
233 &stream->rec_timestamp, &stream->base);
234 e.data.aud_dev_err.dir = PJMEDIA_DIR_CAPTURE;
235 e.data.aud_dev_err.status = status;
236 e.data.aud_dev_err.id = stream->param.rec_id;
237
238 pjmedia_event_publish(NULL, &stream->base, &e,
239 PJMEDIA_EVENT_PUBLISH_DEFAULT);
240 }
241 break;
242 }
243
244 stream->rec_timestamp.u64 += stream->param.samples_per_frame /
245 stream->param.channel_count;
246 }
247
248 (*jni_env)->DeleteLocalRef(jni_env, inputBuffer);
249
250 on_return:
251 detach_jvm(attached);
252 PJ_LOG(5, (THIS_FILE, "Recorder thread stopped"));
253 stream->rec_thread_exited = 1;
254
255 return 0;
256 }
257
258
259 static int AndroidTrackCallback(void *userData)
260 {
261 struct android_aud_stream *stream = (struct android_aud_stream*) userData;
262 jmethodID write_method=0, play_method=0, stop_method=0, flush_method=0;
263 int size = stream->play_buf_size / 2;
264 jshortArray outputBuffer;
265 jshort *buf;
266 JNIEnv *jni_env = 0;
267 pj_bool_t attached = attach_jvm(&jni_env);
268
269 if (!stream->track) {
270 goto on_return;
271 }
272
273 PJ_LOG(5, (THIS_FILE, "Playback thread started"));
274
275 /* Get methods ids */
276 write_method = (*jni_env)->GetMethodID(jni_env, stream->track_class,
277 "write", "([SII)I");
278 play_method = (*jni_env)->GetMethodID(jni_env, stream->track_class,
279 "play", "()V");
280 stop_method = (*jni_env)->GetMethodID(jni_env, stream->track_class,
281 "stop", "()V");
282 flush_method = (*jni_env)->GetMethodID(jni_env, stream->track_class,
283 "flush", "()V");
284 if (write_method==0 || play_method==0 || stop_method==0 ||
285 flush_method==0)
286 {
287 PJ_LOG(3, (THIS_FILE, "Unable to get audio track methods"));
288 goto on_return;
289 }
290
291 outputBuffer = (*jni_env)->NewShortArray(jni_env, size);
292 if (outputBuffer == 0) {
293 PJ_LOG(3, (THIS_FILE, "Unable to allocate output buffer"));
294 goto on_return;
295 }
296 buf = (*jni_env)->GetShortArrayElements(jni_env, outputBuffer, 0);
297
298 /* Start playing */
299 pj_thread_set_prio(NULL, THREAD_PRIORITY_URGENT_AUDIO);
300 pj_sem_wait(stream->play_sem);
301 if (!stream->quit_flag)
302 (*jni_env)->CallVoidMethod(jni_env, stream->track, play_method);
303
304 while (!stream->quit_flag) {
305 pjmedia_frame frame;
306 pj_status_t status;
307 int shortWritten;
308
309 if (!stream->running) {
310 (*jni_env)->CallVoidMethod(jni_env, stream->track, stop_method);
311 (*jni_env)->CallVoidMethod(jni_env, stream->track, flush_method);
312 pj_sem_wait(stream->play_sem);
313 if (stream->quit_flag)
314 break;
315 (*jni_env)->CallVoidMethod(jni_env, stream->track, play_method);
316 }
317
318 frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
319 frame.size = stream->play_buf_size;
320 frame.buf = (void *)buf;
321 frame.timestamp.u64 = stream->play_timestamp.u64;
322 frame.bit_info = 0;
323
324 status = (*stream->play_cb)(stream->user_data, &frame);
325 if (status != PJ_SUCCESS) {
326 if (!stream->quit_flag) {
327 pjmedia_event e;
328
329 PJ_PERROR(3, (THIS_FILE, status, "Android JNI playback callback stopped due to error"));
330
331 /* Broadcast Android JNI playback error */
332 pjmedia_event_init(&e, PJMEDIA_EVENT_AUD_DEV_ERROR,
333 &stream->play_timestamp, &stream->base);
334 e.data.aud_dev_err.dir = PJMEDIA_DIR_PLAYBACK;
335 e.data.aud_dev_err.status = status;
336 e.data.aud_dev_err.id = stream->param.play_id;
337
338 pjmedia_event_publish(NULL, &stream->base, &e,
339 PJMEDIA_EVENT_PUBLISH_DEFAULT);
340 }
341 continue;
342 }
343
344 if (frame.type != PJMEDIA_FRAME_TYPE_AUDIO)
345 pj_bzero(frame.buf, frame.size);
346
347 (*jni_env)->ReleaseShortArrayElements(jni_env, outputBuffer, buf,
348 JNI_COMMIT);
349
350 /* Write to the device output. */
351 shortWritten = (*jni_env)->CallIntMethod(jni_env, stream->track,
352 write_method, outputBuffer,
353 0, size);
354 if (shortWritten <= 0 || shortWritten != size) {
355 PJ_LOG(4, (THIS_FILE, "Player thread: Error %d writing data",
356 shortWritten));
357 continue;
358 }
359
360 stream->play_timestamp.u64 += stream->param.samples_per_frame /
361 stream->param.channel_count;
362 };
363
364 (*jni_env)->ReleaseShortArrayElements(jni_env, outputBuffer, buf, 0);
365 (*jni_env)->DeleteLocalRef(jni_env, outputBuffer);
366
367 on_return:
368 detach_jvm(attached);
369 PJ_LOG(5, (THIS_FILE, "Player thread stopped"));
370 stream->play_thread_exited = 1;
371
372 return 0;
373 }
374
375 /*
376 * Init Android audio driver.
377 */
378 pjmedia_aud_dev_factory* pjmedia_android_factory(pj_pool_factory *pf)
379 {
380 struct android_aud_factory *f;
381 pj_pool_t *pool;
382
383 pool = pj_pool_create(pf, "androidjni", 256, 256, NULL);
384 f = PJ_POOL_ZALLOC_T(pool, struct android_aud_factory);
385 f->pf = pf;
386 f->pool = pool;
387 f->base.op = &android_op;
388
389 return &f->base;
390 }
391
392 /* API: Init factory */
393 static pj_status_t android_init(pjmedia_aud_dev_factory *f)
394 {
395 PJ_UNUSED_ARG(f);
396
397 PJ_LOG(4, (THIS_FILE, "Android JNI sound library initialized"));
398
399 return PJ_SUCCESS;
400 }
401
402
403 /* API: refresh the list of devices */
404 static pj_status_t android_refresh(pjmedia_aud_dev_factory *f)
405 {
406 PJ_UNUSED_ARG(f);
407 return PJ_SUCCESS;
408 }
409
410
411 /* API: Destroy factory */
412 static pj_status_t android_destroy(pjmedia_aud_dev_factory *f)
413 {
414 struct android_aud_factory *pa = (struct android_aud_factory*)f;
415 pj_pool_t *pool;
416
417 PJ_LOG(4, (THIS_FILE, "Android JNI sound library shutting down.."));
418
419 pool = pa->pool;
420 pa->pool = NULL;
421 pj_pool_release(pool);
422
423 return PJ_SUCCESS;
424 }
425
426 /* API: Get device count. */
427 static unsigned android_get_dev_count(pjmedia_aud_dev_factory *f)
428 {
429 PJ_UNUSED_ARG(f);
430 return 1;
431 }
432
433 /* API: Get device info. */
434 static pj_status_t android_get_dev_info(pjmedia_aud_dev_factory *f,
435 unsigned index,
436 pjmedia_aud_dev_info *info)
437 {
438 PJ_UNUSED_ARG(f);
439
440 pj_bzero(info, sizeof(*info));
441
442 pj_ansi_strxcpy(info->name, "Android JNI", sizeof(info->name));
443 pj_ansi_strxcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
444 info->default_samples_per_sec = 8000;
445 info->caps = PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING |
446 PJMEDIA_AUD_DEV_CAP_INPUT_SOURCE;
447 info->input_count = 1;
448 info->output_count = 1;
449 info->routes = PJMEDIA_AUD_DEV_ROUTE_CUSTOM;
450
451 return PJ_SUCCESS;
452 }
453
454 /* API: fill in with default parameter. */
455 static pj_status_t android_default_param(pjmedia_aud_dev_factory *f,
456 unsigned index,
457 pjmedia_aud_param *param)
458 {
459 pjmedia_aud_dev_info adi;
460 pj_status_t status;
461
462 status = android_get_dev_info(f, index, &adi);
463 if (status != PJ_SUCCESS)
464 return status;
465
466 pj_bzero(param, sizeof(*param));
467 if (adi.input_count && adi.output_count) {
468 param->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
469 param->rec_id = index;
470 param->play_id = index;
471 } else if (adi.input_count) {
472 param->dir = PJMEDIA_DIR_CAPTURE;
473 param->rec_id = index;
474 param->play_id = PJMEDIA_AUD_INVALID_DEV;
475 } else if (adi.output_count) {
476 param->dir = PJMEDIA_DIR_PLAYBACK;
477 param->play_id = index;
478 param->rec_id = PJMEDIA_AUD_INVALID_DEV;
479 } else {
480 return PJMEDIA_EAUD_INVDEV;
481 }
482
483 param->clock_rate = adi.default_samples_per_sec;
484 param->channel_count = 1;
485 param->samples_per_frame = adi.default_samples_per_sec * 20 / 1000;
486 param->bits_per_sample = 16;
487 param->input_latency_ms = PJMEDIA_SND_DEFAULT_REC_LATENCY;
488 param->output_latency_ms = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
489
490 return PJ_SUCCESS;
491 }
492
493 /* API: create stream */
494 static pj_status_t android_create_stream(pjmedia_aud_dev_factory *f,
495 const pjmedia_aud_param *param,
496 pjmedia_aud_rec_cb rec_cb,
497 pjmedia_aud_play_cb play_cb,
498 void *user_data,
499 pjmedia_aud_stream **p_aud_strm)
500 {
501 struct android_aud_factory *pa = (struct android_aud_factory*)f;
502 pj_pool_t *pool;
503 struct android_aud_stream *stream;
504 pj_status_t status = PJ_SUCCESS;
505 int state = 0;
506 int buffSize, inputBuffSizePlay = 0, inputBuffSizeRec = 0;
507 int channelInCfg, channelOutCfg, sampleFormat;
508 jmethodID constructor_method=0, bufsize_method = 0;
509 jmethodID method_id = 0;
510 jclass jcl;
511 JNIEnv *jni_env = 0;
512 pj_bool_t attached;
513
514 PJ_ASSERT_RETURN(param->channel_count >= 1 && param->channel_count <= 2,
515 PJ_EINVAL);
516 PJ_ASSERT_RETURN(/* param->bits_per_sample==8 || */
517 param->bits_per_sample==16,
518 PJ_EINVAL);
519 PJ_ASSERT_RETURN(play_cb && rec_cb && p_aud_strm, PJ_EINVAL);
520
521 pool = pj_pool_create(pa->pf, "jnistrm", 1024, 1024, NULL);
522 if (!pool)
523 return PJ_ENOMEM;
524
525 PJ_LOG(4, (THIS_FILE, "Creating Android JNI stream"));
526
527 stream = PJ_POOL_ZALLOC_T(pool, struct android_aud_stream);
528 stream->pool = pool;
529 pj_strdup2_with_null(pool, &stream->name, "JNI stream");
530 stream->dir = param->dir;
531 pj_memcpy(&stream->param, param, sizeof(*param));
532 stream->user_data = user_data;
533 stream->rec_cb = rec_cb;
534 stream->play_cb = play_cb;
535 buffSize = stream->param.samples_per_frame*stream->param.bits_per_sample/8;
536 stream->rec_buf_size = stream->play_buf_size = buffSize;
537 channelInCfg = (param->channel_count == 1)? 16 /*CHANNEL_IN_MONO*/:
538 12 /*CHANNEL_IN_STEREO*/;
539 channelOutCfg = (param->channel_count == 1)? 4 /*CHANNEL_OUT_MONO*/:
540 12 /*CHANNEL_OUT_STEREO*/;
541
542 // The 8bit/byte read/write methods no longer support 16bit
543 //sampleFormat = (param->bits_per_sample == 8)? 3 /*ENCODING_PCM_8BIT*/:
544 // 2 /*ENCODING_PCM_16BIT*/;
545 sampleFormat = 2 /*ENCODING_PCM_16BIT*/;
546
547 attached = attach_jvm(&jni_env);
548
549 if (stream->dir & PJMEDIA_DIR_CAPTURE) {
550 /* Find audio record class and create global ref */
551 jcl = (*jni_env)->FindClass(jni_env, "android/media/AudioRecord");
552 if (jcl == NULL) {
553 PJ_LOG(3, (THIS_FILE, "Unable to find audio record class"));
554 status = PJMEDIA_EAUD_SYSERR;
555 goto on_error;
556 }
557 stream->record_class = (jclass)(*jni_env)->NewGlobalRef(jni_env, jcl);
558 (*jni_env)->DeleteLocalRef(jni_env, jcl);
559 if (stream->record_class == 0) {
560 status = PJ_ENOMEM;
561 goto on_error;
562 }
563
564 /* Get the min buffer size function */
565 bufsize_method = (*jni_env)->GetStaticMethodID(jni_env,
566 stream->record_class,
567 "getMinBufferSize",
568 "(III)I");
569 if (bufsize_method == 0) {
570 PJ_LOG(3, (THIS_FILE, "Unable to find audio record "
571 "getMinBufferSize() method"));
572 status = PJMEDIA_EAUD_SYSERR;
573 goto on_error;
574 }
575
576 inputBuffSizeRec = (*jni_env)->CallStaticIntMethod(jni_env,
577 stream->record_class,
578 bufsize_method,
579 param->clock_rate,
580 channelInCfg,
581 sampleFormat);
582 if (inputBuffSizeRec <= 0) {
583 PJ_LOG(3, (THIS_FILE, "Unsupported audio record params"));
584 status = PJMEDIA_EAUD_INIT;
585 goto on_error;
586 }
587 }
588
589 if (stream->dir & PJMEDIA_DIR_PLAYBACK) {
590 /* Find audio track class and create global ref */
591 jcl = (*jni_env)->FindClass(jni_env, "android/media/AudioTrack");
592 if (jcl == NULL) {
593 PJ_LOG(3, (THIS_FILE, "Unable to find audio track class"));
594 status = PJMEDIA_EAUD_SYSERR;
595 goto on_error;
596 }
597 stream->track_class = (jclass)(*jni_env)->NewGlobalRef(jni_env, jcl);
598 (*jni_env)->DeleteLocalRef(jni_env, jcl);
599 if (stream->track_class == 0) {
600 status = PJ_ENOMEM;
601 goto on_error;
602 }
603
604 /* Get the min buffer size function */
605 bufsize_method = (*jni_env)->GetStaticMethodID(jni_env,
606 stream->track_class,
607 "getMinBufferSize",
608 "(III)I");
609 if (bufsize_method == 0) {
610 PJ_LOG(3, (THIS_FILE, "Unable to find audio track "
611 "getMinBufferSize() method"));
612 status = PJMEDIA_EAUD_SYSERR;
613 goto on_error;
614 }
615
616 inputBuffSizePlay = (*jni_env)->CallStaticIntMethod(jni_env,
617 stream->track_class,
618 bufsize_method,
619 param->clock_rate,
620 channelOutCfg,
621 sampleFormat);
622 if (inputBuffSizePlay <= 0) {
623 PJ_LOG(3, (THIS_FILE, "Unsupported audio track params"));
624 status = PJMEDIA_EAUD_INIT;
625 goto on_error;
626 }
627 }
628
629 if (stream->dir & PJMEDIA_DIR_CAPTURE) {
630 jthrowable exc;
631 jobject record_obj;
632 int mic_source = 0; /* DEFAULT: default audio source */
633
634 if ((param->flags & PJMEDIA_AUD_DEV_CAP_INPUT_SOURCE) &&
635 (param->input_route & PJMEDIA_AUD_DEV_ROUTE_CUSTOM))
636 {
637 mic_source = param->input_route & ~PJMEDIA_AUD_DEV_ROUTE_CUSTOM;
638 }
639
640 /* Get pointer to the constructor */
641 constructor_method = (*jni_env)->GetMethodID(jni_env,
642 stream->record_class,
643 "<init>", "(IIIII)V");
644 if (constructor_method == 0) {
645 PJ_LOG(3, (THIS_FILE, "Unable to find audio record's constructor"));
646 status = PJMEDIA_EAUD_SYSERR;
647 goto on_error;
648 }
649
650 if (mic_source == 0) {
651 /* Android-L (android-21) removes __system_property_get
652 * from the NDK.
653 */
654 /*
655 char sdk_version[PROP_VALUE_MAX];
656 pj_str_t pj_sdk_version;
657 int sdk_v;
658
659 __system_property_get("ro.build.version.sdk", sdk_version);
660 pj_sdk_version = pj_str(sdk_version);
661 sdk_v = pj_strtoul(&pj_sdk_version);
662 if (sdk_v > 10)
663 */
664 mic_source = 7; /* VOICE_COMMUNICATION */
665 }
666 PJ_LOG(4, (THIS_FILE, "Using audio input source : %d", mic_source));
667
668 do {
669 record_obj = (*jni_env)->NewObject(jni_env,
670 stream->record_class,
671 constructor_method,
672 mic_source,
673 param->clock_rate,
674 channelInCfg,
675 sampleFormat,
676 inputBuffSizeRec);
677 if (record_obj == 0) {
678 PJ_LOG(3, (THIS_FILE, "Unable to create audio record object"));
679 status = PJMEDIA_EAUD_INIT;
680 goto on_error;
681 }
682
683 exc = (*jni_env)->ExceptionOccurred(jni_env);
684 if (exc) {
685 (*jni_env)->ExceptionDescribe(jni_env);
686 (*jni_env)->ExceptionClear(jni_env);
687 PJ_LOG(3, (THIS_FILE, "Failure in audio record's constructor"));
688 if (mic_source == 0) {
689 status = PJMEDIA_EAUD_INIT;
690 goto on_error;
691 }
692 mic_source = 0;
693 PJ_LOG(4, (THIS_FILE, "Trying the default audio source."));
694 continue;
695 }
696
697 /* Check state */
698 method_id = (*jni_env)->GetMethodID(jni_env, stream->record_class,
699 "getState", "()I");
700 if (method_id == 0) {
701 PJ_LOG(3, (THIS_FILE, "Unable to find audio record getState() "
702 "method"));
703 status = PJMEDIA_EAUD_SYSERR;
704 goto on_error;
705 }
706 state = (*jni_env)->CallIntMethod(jni_env, record_obj, method_id);
707 if (state == 0) { /* STATE_UNINITIALIZED */
708 PJ_LOG(3, (THIS_FILE, "Failure in initializing audio record."));
709 if (mic_source == 0) {
710 status = PJMEDIA_EAUD_INIT;
711 goto on_error;
712 }
713 mic_source = 0;
714 PJ_LOG(4, (THIS_FILE, "Trying the default audio source."));
715 }
716 } while (state == 0);
717
718 stream->record = (*jni_env)->NewGlobalRef(jni_env, record_obj);
719 if (stream->record == 0) {
720 jmethodID release_method=0;
721
722 PJ_LOG(3, (THIS_FILE, "Unable to create audio record global ref."));
723 release_method = (*jni_env)->GetMethodID(jni_env,
724 stream->record_class,
725 "release", "()V");
726 (*jni_env)->CallVoidMethod(jni_env, record_obj, release_method);
727
728 status = PJMEDIA_EAUD_INIT;
729 goto on_error;
730 }
731
732 status = pj_sem_create(stream->pool, NULL, 0, 1, &stream->rec_sem);
733 if (status != PJ_SUCCESS)
734 goto on_error;
735
736 status = pj_thread_create(stream->pool, "android_recorder",
737 AndroidRecorderCallback, stream, 0, 0,
738 &stream->rec_thread);
739 if (status != PJ_SUCCESS)
740 goto on_error;
741
742 PJ_LOG(4, (THIS_FILE, "Audio record initialized successfully."));
743 }
744
745 if (stream->dir & PJMEDIA_DIR_PLAYBACK) {
746 jthrowable exc;
747 jobject track_obj;
748
749 /* Get pointer to the constructor */
750 constructor_method = (*jni_env)->GetMethodID(jni_env,
751 stream->track_class,
752 "<init>", "(IIIIII)V");
753 if (constructor_method == 0) {
754 PJ_LOG(3, (THIS_FILE, "Unable to find audio track's constructor."));
755 status = PJMEDIA_EAUD_SYSERR;
756 goto on_error;
757 }
758
759 track_obj = (*jni_env)->NewObject(jni_env,
760 stream->track_class,
761 constructor_method,
762 0, /* STREAM_VOICE_CALL */
763 param->clock_rate,
764 channelOutCfg,
765 sampleFormat,
766 inputBuffSizePlay,
767 1 /* MODE_STREAM */);
768 if (track_obj == 0) {
769 PJ_LOG(3, (THIS_FILE, "Unable to create audio track object."));
770 status = PJMEDIA_EAUD_INIT;
771 goto on_error;
772 }
773
774 exc = (*jni_env)->ExceptionOccurred(jni_env);
775 if (exc) {
776 (*jni_env)->ExceptionDescribe(jni_env);
777 (*jni_env)->ExceptionClear(jni_env);
778 PJ_LOG(3, (THIS_FILE, "Failure in audio track's constructor"));
779 status = PJMEDIA_EAUD_INIT;
780 goto on_error;
781 }
782
783 stream->track = (*jni_env)->NewGlobalRef(jni_env, track_obj);
784 if (stream->track == 0) {
785 jmethodID release_method=0;
786
787 release_method = (*jni_env)->GetMethodID(jni_env,
788 stream->track_class,
789 "release", "()V");
790 (*jni_env)->CallVoidMethod(jni_env, track_obj, release_method);
791
792 PJ_LOG(3, (THIS_FILE, "Unable to create audio track's global ref"));
793 status = PJMEDIA_EAUD_INIT;
794 goto on_error;
795 }
796
797 /* Check state */
798 method_id = (*jni_env)->GetMethodID(jni_env, stream->track_class,
799 "getState", "()I");
800 if (method_id == 0) {
801 PJ_LOG(3, (THIS_FILE, "Unable to find audio track getState() "
802 "method"));
803 status = PJMEDIA_EAUD_SYSERR;
804 goto on_error;
805 }
806 state = (*jni_env)->CallIntMethod(jni_env, stream->track,
807 method_id);
808 if (state == 0) { /* STATE_UNINITIALIZED */
809 PJ_LOG(3, (THIS_FILE, "Failure in initializing audio track."));
810 status = PJMEDIA_EAUD_INIT;
811 goto on_error;
812 }
813
814 status = pj_sem_create(stream->pool, NULL, 0, 1, &stream->play_sem);
815 if (status != PJ_SUCCESS)
816 goto on_error;
817
818 status = pj_thread_create(stream->pool, "android_track",
819 AndroidTrackCallback, stream, 0, 0,
820 &stream->play_thread);
821 if (status != PJ_SUCCESS)
822 goto on_error;
823
824 PJ_LOG(4, (THIS_FILE, "Audio track initialized successfully."));
825 }
826
827 if (param->flags & PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING) {
828 strm_set_cap(&stream->base, PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING,
829 &param->output_vol);
830 }
831
832 /* Done */
833 stream->base.op = &android_strm_op;
834 *p_aud_strm = &stream->base;
835
836 detach_jvm(attached);
837
838 return PJ_SUCCESS;
839
840 on_error:
841 detach_jvm(attached);
842 strm_destroy(&stream->base);
843 return status;
844 }
845
846 /* API: Get stream parameters */
847 static pj_status_t strm_get_param(pjmedia_aud_stream *s,
848 pjmedia_aud_param *pi)
849 {
850 struct android_aud_stream *strm = (struct android_aud_stream*)s;
851 PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL);
852 pj_memcpy(pi, &strm->param, sizeof(*pi));
853
854 if (strm_get_cap(s, PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING,
855 &pi->output_vol) == PJ_SUCCESS)
856 {
857 pi->flags |= PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING;
858 }
859
860 return PJ_SUCCESS;
861 }
862
863 /* API: get capability */
864 static pj_status_t strm_get_cap(pjmedia_aud_stream *s,
865 pjmedia_aud_dev_cap cap,
866 void *pval)
867 {
868 struct android_aud_stream *strm = (struct android_aud_stream*)s;
869 pj_status_t status = PJMEDIA_EAUD_INVCAP;
870
871 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
872
873 if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING &&
874 (strm->param.dir & PJMEDIA_DIR_PLAYBACK))
875 {
876 }
877
878 return status;
879 }
880
881 /* API: set capability */
882 static pj_status_t strm_set_cap(pjmedia_aud_stream *s,
883 pjmedia_aud_dev_cap cap,
884 const void *value)
885 {
886 struct android_aud_stream *stream = (struct android_aud_stream*)s;
887 JNIEnv *jni_env = 0;
888 pj_bool_t attached;
889
890 PJ_ASSERT_RETURN(s && value, PJ_EINVAL);
891
892 if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING &&
893 (stream->param.dir & PJMEDIA_DIR_PLAYBACK))
894 {
895 if (stream->track) {
896 jmethodID vol_method = 0;
897 int retval = 0;
898 float vol = *(int *)value;
899
900 attached = attach_jvm(&jni_env);
901
902 vol_method = (*jni_env)->GetMethodID(jni_env, stream->track_class,
903 "setStereoVolume", "(FF)I");
904 if (vol_method) {
905 retval = (*jni_env)->CallIntMethod(jni_env, stream->track,
906 vol_method,
907 vol/100, vol/100);
908 }
909
910 detach_jvm(attached);
911
912 if (vol_method && retval == 0)
913 return PJ_SUCCESS;
914 }
915 }
916
917 return PJMEDIA_EAUD_INVCAP;
918 }
919
920 /* API: start stream. */
921 static pj_status_t strm_start(pjmedia_aud_stream *s)
922 {
923 struct android_aud_stream *stream = (struct android_aud_stream*)s;
924
925 if (!stream->running) {
926 stream->running = PJ_TRUE;
927 if (stream->record)
928 pj_sem_post(stream->rec_sem);
929 if (stream->track)
930 pj_sem_post(stream->play_sem);
931 }
932
933 PJ_LOG(4, (THIS_FILE, "Android JNI stream started"));
934
935 return PJ_SUCCESS;
936 }
937
938 /* API: stop stream. */
939 static pj_status_t strm_stop(pjmedia_aud_stream *s)
940 {
941 struct android_aud_stream *stream = (struct android_aud_stream*)s;
942
943 if (!stream->running)
944 return PJ_SUCCESS;
945
946 stream->running = PJ_FALSE;
947 PJ_LOG(4,(THIS_FILE, "Android JNI stream stopped"));
948
949 return PJ_SUCCESS;
950 }
951
952 /* API: destroy stream. */
953 static pj_status_t strm_destroy(pjmedia_aud_stream *s)
954 {
955 struct android_aud_stream *stream = (struct android_aud_stream*)s;
956 JNIEnv *jni_env = 0;
957 jmethodID release_method=0;
958 pj_bool_t attached;
959
960 PJ_LOG(4,(THIS_FILE, "Destroying Android JNI stream..."));
961
962 stream->quit_flag = PJ_TRUE;
963
964 /* Stop the stream */
965 strm_stop(s);
966
967 attached = attach_jvm(&jni_env);
968
969 if (stream->record){
970 if (stream->rec_thread) {
971 pj_sem_post(stream->rec_sem);
972 pj_thread_join(stream->rec_thread);
973 pj_thread_destroy(stream->rec_thread);
974 stream->rec_thread = NULL;
975 }
976
977 if (stream->rec_sem) {
978 pj_sem_destroy(stream->rec_sem);
979 stream->rec_sem = NULL;
980 }
981 if (stream->record_class) {
982 release_method = (*jni_env)->GetMethodID(jni_env,
983 stream->record_class,
984 "release", "()V");
985 (*jni_env)->CallVoidMethod(jni_env, stream->record,
986 release_method);
987 }
988 (*jni_env)->DeleteGlobalRef(jni_env, stream->record);
989 stream->record = NULL;
990 PJ_LOG(4, (THIS_FILE, "Audio record released"));
991 }
992 if (stream->record_class) {
993 (*jni_env)->DeleteGlobalRef(jni_env, stream->record_class);
994 stream->record_class = NULL;
995 }
996
997 if (stream->track) {
998 if (stream->play_thread) {
999 pj_sem_post(stream->play_sem);
1000 pj_thread_join(stream->play_thread);
1001 pj_thread_destroy(stream->play_thread);
1002 stream->play_thread = NULL;
1003 }
1004
1005 if (stream->play_sem) {
1006 pj_sem_destroy(stream->play_sem);
1007 stream->play_sem = NULL;
1008 }
1009 if (stream->track_class) {
1010 release_method = (*jni_env)->GetMethodID(jni_env,
1011 stream->track_class,
1012 "release", "()V");
1013 (*jni_env)->CallVoidMethod(jni_env, stream->track,
1014 release_method);
1015 }
1016 (*jni_env)->DeleteGlobalRef(jni_env, stream->track);
1017 stream->track = NULL;
1018 PJ_LOG(4, (THIS_FILE, "Audio track released"));
1019 }
1020 if (stream->track_class) {
1021 (*jni_env)->DeleteGlobalRef(jni_env, stream->track_class);
1022 stream->track_class = NULL;
1023 }
1024
1025 pj_pool_release(stream->pool);
1026 PJ_LOG(4, (THIS_FILE, "Android JNI stream destroyed"));
1027
1028 detach_jvm(attached);
1029 return PJ_SUCCESS;
1030 }
1031
1032 #endif /* PJMEDIA_AUDIO_DEV_HAS_ANDROID_JNI */