Debian Codegraph @

Xpjproject/pjmedia/src/pjmedia-videodev/android_dev.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2015 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include "util.h"
19 #include <pjmedia-videodev/videodev_imp.h>
20 #include <pj/assert.h>
21 #include <pj/log.h>
22 #include <pj/math.h>
23 #include <pj/os.h>
24
25
26 #if defined(PJMEDIA_HAS_VIDEO) && PJMEDIA_HAS_VIDEO != 0 && \
27 defined(PJMEDIA_VIDEO_DEV_HAS_ANDROID) && \
28 PJMEDIA_VIDEO_DEV_HAS_ANDROID != 0
29
30 #include <jni.h>
31
32 #define THIS_FILE "android_dev.c"
33
34 /* Default video params */
35 #define DEFAULT_CLOCK_RATE 90000
36 #define DEFAULT_WIDTH 352
37 #define DEFAULT_HEIGHT 288
38 #define DEFAULT_FPS 15
39 #define ALIGN16(x) ((((x)+15) >> 4) << 4)
40
41 /* Define whether we should maintain the aspect ratio when rotating the image.
42 * For more details, please refer to util.h.
43 */
44 #define MAINTAIN_ASPECT_RATIO PJ_TRUE
45
46 /* Format map info */
47 typedef struct and_fmt_map
48 {
49 pjmedia_format_id fmt_id;
50 pj_uint32_t and_fmt_id;
51 } and_fmt_map;
52
53
54 /* Format map.
55 * Note: it seems that most of Android devices don't support I420, while
56 * unfortunately, our converter (libyuv based) only support I420 & RGBA,
57 * so in this case, we'd just pretend that we support I420 and we'll do
58 * the NV21/YV12 -> I420 conversion here.
59 */
60 static and_fmt_map fmt_map[] =
61 {
62 {PJMEDIA_FORMAT_NV21, 0x00000011},
63 {PJMEDIA_FORMAT_YV12, 0x32315659},
64 {PJMEDIA_FORMAT_I420, 0x00000023}, /* YUV_420_888 */
65 };
66
67
68 /* Device info */
69 typedef struct and_dev_info
70 {
71 pjmedia_vid_dev_info info; /**< Base info */
72 unsigned dev_idx; /**< Original dev ID */
73 pj_bool_t facing; /**< Front/back camera?*/
74 unsigned sup_size_cnt; /**< # of supp'd size */
75 pjmedia_rect_size *sup_size; /**< Supported size */
76 unsigned sup_fps_cnt; /**< # of supp'd FPS */
77 pjmedia_rect_size *sup_fps; /**< Supported FPS */
78 pj_bool_t has_yv12; /**< Support YV12? */
79 pj_bool_t has_nv21; /**< Support NV21? */
80 pj_bool_t forced_i420; /**< Support I420 with
81 conversion */
82 } and_dev_info;
83
84
85 /* Video factory */
86 typedef struct and_factory
87 {
88 pjmedia_vid_dev_factory base; /**< Base factory */
89 pj_pool_t *pool; /**< Memory pool */
90 pj_pool_factory *pf; /**< Pool factory */
91
92 pj_pool_t *dev_pool; /**< Device list pool */
93 unsigned dev_count; /**< Device count */
94 and_dev_info *dev_info; /**< Device info list */
95 } and_factory;
96
97
98 /* Video stream. */
99 typedef struct and_stream
100 {
101 pjmedia_vid_dev_stream base; /**< Base stream */
102 pjmedia_vid_dev_param param; /**< Settings */
103 pj_pool_t *pool; /**< Memory pool */
104 and_factory *factory; /**< Factory */
105
106 pjmedia_vid_dev_cb vid_cb; /**< Stream callback */
107 void *user_data; /**< Application data */
108 pj_bool_t is_running; /**< Stream running? */
109
110 jobject jcam; /**< PjCamera instance */
111
112 pj_timestamp frame_ts; /**< Current timestamp */
113 unsigned ts_inc; /**< Timestamp interval*/
114 unsigned convert_to_i420; /**< Need to convert to I420?
115 0: no
116 1: from NV21
117 2: from YV12 */
118
119 /** Capture thread info */
120 pj_bool_t thread_initialized;
121 pj_thread_desc thread_desc;
122 pj_thread_t *thread;
123
124 /** NV21/YV12 -> I420 Conversion buffer */
125 pj_uint8_t *convert_buf;
126 pjmedia_rect_size cam_size;
127
128 /** Converter to rotate frame */
129 pjmedia_vid_dev_conv conv;
130
131 /** Frame format param for NV21/YV12 -> I420 conversion */
132 pjmedia_video_apply_fmt_param vafp;
133 } and_stream;
134
135
136 /* Prototypes */
137 static pj_status_t and_factory_init(pjmedia_vid_dev_factory *f);
138 static pj_status_t and_factory_destroy(pjmedia_vid_dev_factory *f);
139 static pj_status_t and_factory_refresh(pjmedia_vid_dev_factory *f);
140 static unsigned and_factory_get_dev_count(pjmedia_vid_dev_factory *f);
141 static pj_status_t and_factory_get_dev_info(pjmedia_vid_dev_factory *f,
142 unsigned index,
143 pjmedia_vid_dev_info *info);
144 static pj_status_t and_factory_default_param(pj_pool_t *pool,
145 pjmedia_vid_dev_factory *f,
146 unsigned index,
147 pjmedia_vid_dev_param *param);
148 static pj_status_t and_factory_create_stream(
149 pjmedia_vid_dev_factory *f,
150 pjmedia_vid_dev_param *param,
151 const pjmedia_vid_dev_cb *cb,
152 void *user_data,
153 pjmedia_vid_dev_stream **p_vid_strm);
154
155
156 static pj_status_t and_stream_get_param(pjmedia_vid_dev_stream *strm,
157 pjmedia_vid_dev_param *param);
158 static pj_status_t and_stream_get_cap(pjmedia_vid_dev_stream *strm,
159 pjmedia_vid_dev_cap cap,
160 void *value);
161 static pj_status_t and_stream_set_cap(pjmedia_vid_dev_stream *strm,
162 pjmedia_vid_dev_cap cap,
163 const void *value);
164 static pj_status_t and_stream_start(pjmedia_vid_dev_stream *strm);
165 static pj_status_t and_stream_stop(pjmedia_vid_dev_stream *strm);
166 static pj_status_t and_stream_destroy(pjmedia_vid_dev_stream *strm);
167
168
169 /* Operations */
170 static pjmedia_vid_dev_factory_op factory_op =
171 {
172 &and_factory_init,
173 &and_factory_destroy,
174 &and_factory_get_dev_count,
175 &and_factory_get_dev_info,
176 &and_factory_default_param,
177 &and_factory_create_stream,
178 &and_factory_refresh
179 };
180
181 static pjmedia_vid_dev_stream_op stream_op =
182 {
183 &and_stream_get_param,
184 &and_stream_get_cap,
185 &and_stream_set_cap,
186 &and_stream_start,
187 NULL,
188 NULL,
189 &and_stream_stop,
190 &and_stream_destroy
191 };
192
193
194 /****************************************************************************
195 * JNI stuff
196 */
197 extern JavaVM *pj_jni_jvm;
198
199 /* Use camera2 (since Android API level 21) */
200 #define USE_CAMERA2 1
201
202 #if USE_CAMERA2
203 #define PJ_CAMERA "PjCamera2"
204 #define PJ_CAMERA_INFO "PjCameraInfo2"
205 #else
206 #define PJ_CAMERA "PjCamera"
207 #define PJ_CAMERA_INFO "PjCameraInfo"
208 #endif
209
210 #define PJ_CLASS_PATH "org/pjsip/"
211 #define PJ_CAMERA_CLASS_PATH PJ_CLASS_PATH PJ_CAMERA
212 #define PJ_CAMERA_INFO_CLASS_PATH PJ_CLASS_PATH PJ_CAMERA_INFO
213
214
215 static struct jni_objs_t
216 {
217 struct {
218 jclass cls;
219 jmethodID m_init;
220 jmethodID m_start;
221 jmethodID m_stop;
222 jmethodID m_switch;
223 } cam;
224
225 struct {
226 jclass cls;
227 jmethodID m_get_cnt;
228 jmethodID m_get_info;
229 jfieldID f_facing;
230 jfieldID f_orient;
231 jfieldID f_sup_size;
232 jfieldID f_sup_fmt;
233 jfieldID f_sup_fps;
234 } cam_info;
235
236 } jobjs;
237
238
239 #if USE_CAMERA2
240 static void JNICALL OnGetFrame2(JNIEnv *env, jobject obj,
241 jlong user_data,
242 jobject plane0, jint rowStride0, jint pixStride0,
243 jobject plane1, jint rowStride1, jint pixStride1,
244 jobject plane2, jint rowStride2, jint pixStride2);
245 #else
246 static void JNICALL OnGetFrame(JNIEnv *env, jobject obj,
247 jbyteArray data, jint length,
248 jlong user_data);
249 #endif
250
251 #define jni_get_env(jni_env) pj_jni_attach_jvm((void **)jni_env)
252 #define jni_detach_env(attached) pj_jni_detach_jvm(attached)
253
254
255 /* Get Java object IDs (via FindClass, GetMethodID, GetFieldID, etc).
256 * Note that this function should be called from library-loader thread,
257 * otherwise FindClass, etc, may fail, see:
258 * http://developer.android.com/training/articles/perf-jni.html#faq_FindClass
259 */
260 static pj_status_t jni_init_ids()
261 {
262 JNIEnv *jni_env;
263 pj_status_t status = PJ_SUCCESS;
264 pj_bool_t with_attach = jni_get_env(&jni_env);
265
266 #define GET_CLASS(class_path, class_name, cls) \
267 cls = (*jni_env)->FindClass(jni_env, class_path); \
268 if (cls == NULL || (*jni_env)->ExceptionCheck(jni_env)) { \
269 (*jni_env)->ExceptionClear(jni_env); \
270 PJ_LOG(3, (THIS_FILE, "[JNI] Unable to find class '" \
271 class_name "'")); \
272 status = PJMEDIA_EVID_SYSERR; \
273 goto on_return; \
274 } else { \
275 jclass tmp = cls; \
276 cls = (jclass)(*jni_env)->NewGlobalRef(jni_env, tmp); \
277 (*jni_env)->DeleteLocalRef(jni_env, tmp); \
278 if (cls == NULL) { \
279 PJ_LOG(3, (THIS_FILE, "[JNI] Unable to get global ref for " \
280 "class '" class_name "'")); \
281 status = PJMEDIA_EVID_SYSERR; \
282 goto on_return; \
283 } \
284 }
285 #define GET_METHOD_ID(cls, class_name, method_name, signature, id) \
286 id = (*jni_env)->GetMethodID(jni_env, cls, method_name, signature); \
287 if (id == 0) { \
288 PJ_LOG(3, (THIS_FILE, "[JNI] Unable to find method '" method_name \
289 "' in class '" class_name "'")); \
290 status = PJMEDIA_EVID_SYSERR; \
291 goto on_return; \
292 }
293 #define GET_SMETHOD_ID(cls, class_name, method_name, signature, id) \
294 id = (*jni_env)->GetStaticMethodID(jni_env, cls, method_name, signature); \
295 if (id == 0) { \
296 PJ_LOG(3, (THIS_FILE, "[JNI] Unable to find static method '" \
297 method_name "' in class '" class_name "'")); \
298 status = PJMEDIA_EVID_SYSERR; \
299 goto on_return; \
300 }
301 #define GET_FIELD_ID(cls, class_name, field_name, signature, id) \
302 id = (*jni_env)->GetFieldID(jni_env, cls, field_name, signature); \
303 if (id == 0) { \
304 PJ_LOG(3, (THIS_FILE, "[JNI] Unable to find field '" field_name \
305 "' in class '" class_name "'")); \
306 status = PJMEDIA_EVID_SYSERR; \
307 goto on_return; \
308 }
309
310 /* PjCamera class info */
311 GET_CLASS(PJ_CAMERA_CLASS_PATH, PJ_CAMERA, jobjs.cam.cls);
312 GET_METHOD_ID(jobjs.cam.cls, PJ_CAMERA, "<init>",
313 "(IIIIIJLandroid/view/SurfaceView;)V",
314 jobjs.cam.m_init);
315 GET_METHOD_ID(jobjs.cam.cls, PJ_CAMERA, "Start", "()I",
316 jobjs.cam.m_start);
317 GET_METHOD_ID(jobjs.cam.cls, PJ_CAMERA, "Stop", "()V",
318 jobjs.cam.m_stop);
319 GET_METHOD_ID(jobjs.cam.cls, PJ_CAMERA, "SwitchDevice", "(I)I",
320 jobjs.cam.m_switch);
321
322 /* PjCameraInfo class info */
323 GET_CLASS(PJ_CAMERA_INFO_CLASS_PATH, PJ_CAMERA_INFO, jobjs.cam_info.cls);
324 GET_SMETHOD_ID(jobjs.cam_info.cls, PJ_CAMERA_INFO, "GetCameraCount", "()I",
325 jobjs.cam_info.m_get_cnt);
326 GET_SMETHOD_ID(jobjs.cam_info.cls, PJ_CAMERA_INFO, "GetCameraInfo",
327 "(I)L" PJ_CAMERA_INFO_CLASS_PATH ";",
328 jobjs.cam_info.m_get_info);
329 GET_FIELD_ID(jobjs.cam_info.cls, PJ_CAMERA_INFO, "facing", "I",
330 jobjs.cam_info.f_facing);
331 GET_FIELD_ID(jobjs.cam_info.cls, PJ_CAMERA_INFO, "orient", "I",
332 jobjs.cam_info.f_orient);
333 GET_FIELD_ID(jobjs.cam_info.cls, PJ_CAMERA_INFO, "supportedSize", "[I",
334 jobjs.cam_info.f_sup_size);
335 GET_FIELD_ID(jobjs.cam_info.cls, PJ_CAMERA_INFO, "supportedFormat", "[I",
336 jobjs.cam_info.f_sup_fmt);
337 GET_FIELD_ID(jobjs.cam_info.cls, PJ_CAMERA_INFO, "supportedFps1000", "[I",
338 jobjs.cam_info.f_sup_fps);
339
340 #undef GET_CLASS_ID
341 #undef GET_METHOD_ID
342 #undef GET_SMETHOD_ID
343 #undef GET_FIELD_ID
344
345 /* Register native function */
346 {
347 #if USE_CAMERA2
348 JNINativeMethod m = { "PushFrame2", "(JLjava/nio/ByteBuffer;IILjava/nio/ByteBuffer;IILjava/nio/ByteBuffer;II)V", (void*)&OnGetFrame2 };
349 #else
350 JNINativeMethod m = { "PushFrame", "([BIJ)V", (void*)&OnGetFrame };
351 #endif
352 if ((*jni_env)->RegisterNatives(jni_env, jobjs.cam.cls, &m, 1)) {
353 PJ_LOG(3, (THIS_FILE, "[JNI] Failed in registering native "
354 "function 'OnGetFrame()'"));
355 status = PJMEDIA_EVID_SYSERR;
356 }
357 }
358
359 on_return:
360 jni_detach_env(with_attach);
361 return status;
362 }
363
364
365 static void jni_deinit_ids()
366 {
367 JNIEnv *jni_env;
368 pj_bool_t with_attach = jni_get_env(&jni_env);
369
370 if (jobjs.cam.cls) {
371 (*jni_env)->DeleteGlobalRef(jni_env, jobjs.cam.cls);
372 jobjs.cam.cls = NULL;
373 }
374
375 if (jobjs.cam_info.cls) {
376 (*jni_env)->DeleteGlobalRef(jni_env, jobjs.cam_info.cls);
377 jobjs.cam_info.cls = NULL;
378 }
379
380 jni_detach_env(with_attach);
381 }
382
383
384 /****************************************************************************
385 * Helper functions
386 */
387 static pjmedia_format_id and_fmt_to_pj(pj_uint32_t fmt)
388 {
389 unsigned i;
390 for (i = 0; i < PJ_ARRAY_SIZE(fmt_map); i++) {
391 if (fmt_map[i].and_fmt_id == fmt)
392 return fmt_map[i].fmt_id;
393 }
394 return 0;
395 }
396
397 static pj_uint32_t pj_fmt_to_and(pjmedia_format_id fmt)
398 {
399 unsigned i;
400 for (i = 0; i < PJ_ARRAY_SIZE(fmt_map); i++) {
401 if (fmt_map[i].fmt_id == fmt)
402 return fmt_map[i].and_fmt_id;
403 }
404 return 0;
405 }
406
407
408 /****************************************************************************
409 * Factory operations
410 */
411 /*
412 * Init and_ video driver.
413 */
414 pjmedia_vid_dev_factory* pjmedia_and_factory(pj_pool_factory *pf)
415 {
416 and_factory *f;
417 pj_pool_t *pool;
418
419 pool = pj_pool_create(pf, "and_video", 512, 512, NULL);
420 f = PJ_POOL_ZALLOC_T(pool, and_factory);
421 f->pf = pf;
422 f->pool = pool;
423 f->base.op = &factory_op;
424 f->dev_pool = pj_pool_create(pf, "and_video_dev", 512, 512, NULL);
425
426 return &f->base;
427 }
428
429
430 /* API: init factory */
431 static pj_status_t and_factory_init(pjmedia_vid_dev_factory *ff)
432 {
433 pj_status_t status;
434
435 status = jni_init_ids();
436 if (status != PJ_SUCCESS)
437 return status;
438
439 status = and_factory_refresh(ff);
440 if (status != PJ_SUCCESS)
441 return status;
442
443 return PJ_SUCCESS;
444 }
445
446
447 /* API: destroy factory */
448 static pj_status_t and_factory_destroy(pjmedia_vid_dev_factory *ff)
449 {
450 and_factory *f = (and_factory*)ff;
451
452 jni_deinit_ids();
453 pj_pool_safe_release(&f->dev_pool);
454 pj_pool_safe_release(&f->pool);
455
456 return PJ_SUCCESS;
457 }
458
459
460 /* API: refresh the list of devices */
461 static pj_status_t and_factory_refresh(pjmedia_vid_dev_factory *ff)
462 {
463 and_factory *f = (and_factory*)ff;
464 pj_status_t status = PJ_SUCCESS;
465
466 JNIEnv *jni_env;
467 pj_bool_t with_attach, found_front = PJ_FALSE;
468 int i, dev_count = 0;
469
470 /* Clean up device info and pool */
471 f->dev_count = 0;
472 pj_pool_reset(f->dev_pool);
473
474 with_attach = jni_get_env(&jni_env);
475
476 /* dev_count = PjCameraInfo::GetCameraCount() */
477 dev_count = (*jni_env)->CallStaticIntMethod(jni_env, jobjs.cam_info.cls,
478 jobjs.cam_info.m_get_cnt);
479 if (dev_count < 0) {
480 PJ_LOG(3, (THIS_FILE, "Failed to get camera count"));
481 status = PJMEDIA_EVID_SYSERR;
482 goto on_return;
483 }
484
485 /* Start querying device info */
486 f->dev_info = (and_dev_info*)
487 pj_pool_calloc(f->dev_pool, dev_count,
488 sizeof(and_dev_info));
489
490 for (i = 0; i < dev_count; i++) {
491 and_dev_info *adi = &f->dev_info[f->dev_count];
492 pjmedia_vid_dev_info *vdi = &adi->info;
493 jobject jdev_info;
494 jobject jtmp;
495 int facing, max_fmt_cnt = PJMEDIA_VID_DEV_INFO_FMT_CNT;
496
497 /* jdev_info = PjCameraInfo::GetCameraInfo(i) */
498 jdev_info = (*jni_env)->CallStaticObjectMethod(
499 jni_env,
500 jobjs.cam_info.cls,
501 jobjs.cam_info.m_get_info,
502 i);
503 if (jdev_info == NULL)
504 continue;
505
506 /* Get camera facing: 0=back 1=front */
507 facing = (*jni_env)->GetIntField(jni_env, jdev_info,
508 jobjs.cam_info.f_facing);
509 if (facing < 0)
510 goto on_skip_dev;
511
512 /* Set device ID, direction, and has_callback info */
513 adi->dev_idx = i;
514 vdi->id = f->dev_count;
515 vdi->dir = PJMEDIA_DIR_CAPTURE;
516 vdi->has_callback = PJ_TRUE;
517 vdi->caps = PJMEDIA_VID_DEV_CAP_SWITCH |
518 PJMEDIA_VID_DEV_CAP_ORIENTATION;
519
520 /* Set driver & name info */
521 pj_ansi_strxcpy(vdi->driver, "Android", sizeof(vdi->driver));
522 adi->facing = facing;
523 if (facing == 0) {
524 pj_ansi_strxcpy(vdi->name, "Back camera", sizeof(vdi->name));
525 } else {
526 pj_ansi_strxcpy(vdi->name, "Front camera", sizeof(vdi->name));
527 }
528
529 /* Get supported sizes */
530 jtmp = (*jni_env)->GetObjectField(jni_env, jdev_info,
531 jobjs.cam_info.f_sup_size);
532 if (jtmp) {
533 jintArray jiarray = (jintArray*)jtmp;
534 jint *sizes;
535 jsize cnt, j;
536
537 cnt = (*jni_env)->GetArrayLength(jni_env, jiarray);
538 sizes = (*jni_env)->GetIntArrayElements(jni_env, jiarray, 0);
539
540 adi->sup_size_cnt = cnt/2;
541 adi->sup_size = pj_pool_calloc(f->dev_pool, adi->sup_size_cnt,
542 sizeof(adi->sup_size[0]));
543 for (j = 0; j < adi->sup_size_cnt; j++) {
544 adi->sup_size[j].w = sizes[j*2];
545 adi->sup_size[j].h = sizes[j*2+1];
546 }
547 (*jni_env)->ReleaseIntArrayElements(jni_env, jiarray, sizes, 0);
548 (*jni_env)->DeleteLocalRef(jni_env, jtmp);
549 } else {
550 goto on_skip_dev;
551 }
552
553 /* Get supported formats */
554 jtmp = (*jni_env)->GetObjectField(jni_env, jdev_info,
555 jobjs.cam_info.f_sup_fmt);
556 if (jtmp) {
557 jintArray jiarray = (jintArray*)jtmp;
558 jint *fmts;
559 jsize cnt, j;
560 pj_bool_t has_i420 = PJ_FALSE;
561 int k;
562
563 cnt = (*jni_env)->GetArrayLength(jni_env, jiarray);
564 fmts = (*jni_env)->GetIntArrayElements(jni_env, jiarray, 0);
565 for (j = 0; j < cnt; j++) {
566 pjmedia_format_id fmt = and_fmt_to_pj((pj_uint32_t)fmts[j]);
567
568 /* Make sure we recognize this format */
569 if (fmt == 0)
570 continue;
571
572 /* Check formats for I420 conversion */
573 if (fmt == PJMEDIA_FORMAT_I420) has_i420 = PJ_TRUE;
574 else if (fmt == PJMEDIA_FORMAT_YV12) adi->has_yv12 = PJ_TRUE;
575 else if (fmt == PJMEDIA_FORMAT_NV21) adi->has_nv21 = PJ_TRUE;
576 }
577 (*jni_env)->ReleaseIntArrayElements(jni_env, jiarray, fmts,
578 JNI_ABORT);
579 (*jni_env)->DeleteLocalRef(jni_env, jtmp);
580
581 /* Always put I420/IYUV and in the first place, for better
582 * compatibility.
583 */
584 adi->forced_i420 = !has_i420;
585 for (k = 0; k < adi->sup_size_cnt &&
586 vdi->fmt_cnt < max_fmt_cnt-1; k++)
587 {
588 /* Landscape video */
589 pjmedia_format_init_video(&vdi->fmt[vdi->fmt_cnt++],
590 PJMEDIA_FORMAT_I420,
591 adi->sup_size[k].w,
592 adi->sup_size[k].h,
593 DEFAULT_FPS, 1);
594 /* Portrait video */
595 pjmedia_format_init_video(&vdi->fmt[vdi->fmt_cnt++],
596 PJMEDIA_FORMAT_I420,
597 adi->sup_size[k].h,
598 adi->sup_size[k].w,
599 DEFAULT_FPS, 1);
600 }
601
602 /* Camera2 supports only I420 for now */
603 #if !USE_CAMERA2
604 /* YV12 */
605 if (adi->has_yv12) {
606 for (k = 0; k < adi->sup_size_cnt &&
607 vdi->fmt_cnt < max_fmt_cnt-1; k++)
608 {
609 /* Landscape video */
610 pjmedia_format_init_video(&vdi->fmt[vdi->fmt_cnt++],
611 PJMEDIA_FORMAT_YV12,
612 adi->sup_size[k].w,
613 adi->sup_size[k].h,
614 DEFAULT_FPS, 1);
615 /* Portrait video */
616 pjmedia_format_init_video(&vdi->fmt[vdi->fmt_cnt++],
617 PJMEDIA_FORMAT_YV12,
618 adi->sup_size[k].h,
619 adi->sup_size[k].w,
620 DEFAULT_FPS, 1);
621 }
622 }
623
624 /* NV21 */
625 if (adi->has_nv21) {
626 for (k = 0; k < adi->sup_size_cnt &&
627 vdi->fmt_cnt < max_fmt_cnt-1; k++)
628 {
629 /* Landscape video */
630 pjmedia_format_init_video(&vdi->fmt[vdi->fmt_cnt++],
631 PJMEDIA_FORMAT_NV21,
632 adi->sup_size[k].w,
633 adi->sup_size[k].h,
634 DEFAULT_FPS, 1);
635 /* Portrait video */
636 pjmedia_format_init_video(&vdi->fmt[vdi->fmt_cnt++],
637 PJMEDIA_FORMAT_NV21,
638 adi->sup_size[k].h,
639 adi->sup_size[k].w,
640 DEFAULT_FPS, 1);
641 }
642 }
643 #endif
644
645 } else {
646 goto on_skip_dev;
647 }
648
649 /* If this is front camera, set it as first/default (if not yet) */
650 if (facing == 1) {
651 if (!found_front && f->dev_count > 0) {
652 /* Swap this front cam info with one whose idx==0 */
653 and_dev_info tmp_adi;
654 pj_memcpy(&tmp_adi, &f->dev_info[0], sizeof(tmp_adi));
655 pj_memcpy(&f->dev_info[0], adi, sizeof(tmp_adi));
656 pj_memcpy(adi, &tmp_adi, sizeof(tmp_adi));
657 f->dev_info[0].info.id = 0;
658 f->dev_info[f->dev_count].info.id = f->dev_count;
659 }
660 found_front = PJ_TRUE;
661 }
662
663 f->dev_count++;
664
665 on_skip_dev:
666 (*jni_env)->DeleteLocalRef(jni_env, jdev_info);
667 }
668
669 PJ_LOG(4, (THIS_FILE,
670 "Android video capture initialized with %d device(s):",
671 f->dev_count));
672 for (i = 0; i < f->dev_count; i++) {
673 and_dev_info *adi = &f->dev_info[i];
674 char tmp_str[2048], *p;
675 int j, plen, slen;
676 PJ_LOG(4, (THIS_FILE, "%2d: %s", i, f->dev_info[i].info.name));
677
678 /* Print supported formats */
679 p = tmp_str;
680 plen = sizeof(tmp_str);
681 for (j = 0; j < adi->info.fmt_cnt; j++) {
682 char tmp_str2[5];
683 const pjmedia_video_format_detail *vfd =
684 pjmedia_format_get_video_format_detail(&adi->info.fmt[j], 0);
685 pjmedia_fourcc_name(adi->info.fmt[j].id, tmp_str2);
686 slen = pj_ansi_snprintf(p, plen, "%s/%dx%d ",
687 tmp_str2, vfd->size.w, vfd->size.h);
688 if (slen < 0 || slen >= plen) break;
689 plen -= slen;
690 p += slen;
691 }
692 PJ_LOG(4, (THIS_FILE, " supported format = %s", tmp_str));
693 }
694
695 on_return:
696 jni_detach_env(with_attach);
697 return status;
698 }
699
700
701 /* API: get number of devices */
702 static unsigned and_factory_get_dev_count(pjmedia_vid_dev_factory *ff)
703 {
704 and_factory *f = (and_factory*)ff;
705 return f->dev_count;
706 }
707
708
709 /* API: get device info */
710 static pj_status_t and_factory_get_dev_info(pjmedia_vid_dev_factory *f,
711 unsigned index,
712 pjmedia_vid_dev_info *info)
713 {
714 and_factory *cf = (and_factory*)f;
715
716 PJ_ASSERT_RETURN(index < cf->dev_count, PJMEDIA_EVID_INVDEV);
717
718 pj_memcpy(info, &cf->dev_info[index].info, sizeof(*info));
719
720 return PJ_SUCCESS;
721 }
722
723
724 /* API: create default device parameter */
725 static pj_status_t and_factory_default_param(pj_pool_t *pool,
726 pjmedia_vid_dev_factory *f,
727 unsigned index,
728 pjmedia_vid_dev_param *param)
729 {
730 and_factory *cf = (and_factory*)f;
731 and_dev_info *di = &cf->dev_info[index];
732
733 PJ_ASSERT_RETURN(index < cf->dev_count, PJMEDIA_EVID_INVDEV);
734
735 PJ_UNUSED_ARG(pool);
736
737 pj_bzero(param, sizeof(*param));
738 param->dir = PJMEDIA_DIR_CAPTURE;
739 param->cap_id = index;
740 param->rend_id = PJMEDIA_VID_INVALID_DEV;
741 param->flags = PJMEDIA_VID_DEV_CAP_FORMAT;
742 param->clock_rate = DEFAULT_CLOCK_RATE;
743 pj_memcpy(&param->fmt, &di->info.fmt[0], sizeof(param->fmt));
744
745 return PJ_SUCCESS;
746 }
747
748
749 /* API: create stream */
750 static pj_status_t and_factory_create_stream(
751 pjmedia_vid_dev_factory *ff,
752 pjmedia_vid_dev_param *param,
753 const pjmedia_vid_dev_cb *cb,
754 void *user_data,
755 pjmedia_vid_dev_stream **p_vid_strm)
756 {
757 and_factory *f = (and_factory*)ff;
758 pj_pool_t *pool;
759 and_stream *strm;
760 and_dev_info *adi;
761 const pjmedia_video_format_detail *vfd;
762 const pjmedia_video_format_info *vfi;
763 pjmedia_video_apply_fmt_param vafp;
764 pj_uint32_t and_fmt = 0;
765 unsigned convert_to_i420 = 0;
766 pj_status_t status = PJ_SUCCESS;
767
768 JNIEnv *jni_env;
769 pj_bool_t with_attach;
770 jobject jcam;
771
772 PJ_ASSERT_RETURN(f && param && p_vid_strm, PJ_EINVAL);
773 PJ_ASSERT_RETURN(param->fmt.type == PJMEDIA_TYPE_VIDEO &&
774 param->fmt.detail_type == PJMEDIA_FORMAT_DETAIL_VIDEO &&
775 param->dir == PJMEDIA_DIR_CAPTURE,
776 PJ_EINVAL);
777
778 /* Camera2 supports only I420 for now */
779 #if USE_CAMERA2
780 if (param->fmt.id != PJMEDIA_FORMAT_I420)
781 return PJMEDIA_EVID_BADFORMAT;
782 #endif
783
784 pj_bzero(&vafp, sizeof(vafp));
785 adi = &f->dev_info[param->cap_id];
786 vfd = pjmedia_format_get_video_format_detail(&param->fmt, PJ_TRUE);
787 vfi = pjmedia_get_video_format_info(NULL, param->fmt.id);
788
789 if (param->fmt.id == PJMEDIA_FORMAT_I420 && adi->forced_i420) {
790 /* Not really support I420, need to convert it from YV12/NV21 */
791 if (adi->has_nv21) {
792 and_fmt = pj_fmt_to_and(PJMEDIA_FORMAT_NV21);
793 convert_to_i420 = 1;
794 } else if (adi->has_yv12) {
795 and_fmt = pj_fmt_to_and(PJMEDIA_FORMAT_YV12);
796 convert_to_i420 = 2;
797 } else
798 pj_assert(!"Bug!");
799 } else {
800 and_fmt = pj_fmt_to_and(param->fmt.id);
801 }
802 if (!vfi || !and_fmt)
803 return PJMEDIA_EVID_BADFORMAT;
804
805 vafp.size = vfd->size;
806 if (vfi->apply_fmt(vfi, &vafp) != PJ_SUCCESS)
807 return PJMEDIA_EVID_BADFORMAT;
808
809 /* Create and Initialize stream descriptor */
810 pool = pj_pool_create(f->pf, "and-dev", 512, 512, NULL);
811 PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM);
812
813 strm = PJ_POOL_ZALLOC_T(pool, and_stream);
814 pj_memcpy(&strm->param, param, sizeof(*param));
815 strm->pool = pool;
816 strm->factory = f;
817 pj_memcpy(&strm->vid_cb, cb, sizeof(*cb));
818 strm->user_data = user_data;
819 pj_memcpy(&strm->vafp, &vafp, sizeof(vafp));
820 strm->ts_inc = PJMEDIA_SPF2(param->clock_rate, &vfd->fps, 1);
821
822 /* Allocate buffer for YV12 -> I420 conversion.
823 * The camera2 is a bit tricky with format, for example it reports
824 * for I420 support (and no NV21 support), however the incoming frame
825 * buffers are actually in NV21 format (e.g: pixel stride is 2), so
826 * we should always check and conversion buffer may be needed.
827 */
828 if (USE_CAMERA2 || convert_to_i420) {
829 pj_assert(vfi->plane_cnt > 1);
830 strm->convert_to_i420 = convert_to_i420;
831 strm->convert_buf = pj_pool_alloc(pool, vafp.plane_bytes[1]);
832 }
833
834 /* Native preview */
835 if (param->flags & PJMEDIA_VID_DEV_CAP_INPUT_PREVIEW) {
836 }
837
838 with_attach = jni_get_env(&jni_env);
839
840 /* Instantiate PjCamera */
841 strm->cam_size.w = (vfd->size.w > vfd->size.h? vfd->size.w: vfd->size.h);
842 strm->cam_size.h = (vfd->size.w > vfd->size.h? vfd->size.h: vfd->size.w);
843 jcam = (*jni_env)->NewObject(jni_env, jobjs.cam.cls, jobjs.cam.m_init,
844 adi->dev_idx, /* idx */
845 strm->cam_size.w, /* w */
846 strm->cam_size.h, /* h */
847 and_fmt, /* fmt */
848 #if USE_CAMERA2
849 vfd->fps.num/
850 #else
851 vfd->fps.num*1000/
852 #endif
853 vfd->fps.denum, /* fps */
854 (jlong)(intptr_t)strm, /* user data */
855 NULL /* SurfaceView */
856 );
857 if (jcam == NULL) {
858 PJ_LOG(3, (THIS_FILE, "Unable to create PjCamera instance"));
859 status = PJMEDIA_EVID_SYSERR;
860 goto on_return;
861 }
862 strm->jcam = (jobject)(*jni_env)->NewGlobalRef(jni_env, jcam);
863 (*jni_env)->DeleteLocalRef(jni_env, jcam);
864 if (strm->jcam == NULL) {
865 PJ_LOG(3, (THIS_FILE, "Unable to create global ref to PjCamera"));
866 status = PJMEDIA_EVID_SYSERR;
867 goto on_return;
868 }
869
870 /* Video orientation.
871 * If we send in portrait, we need to set up orientation converter
872 * as well.
873 */
874 if ((param->flags & PJMEDIA_VID_DEV_CAP_ORIENTATION) ||
875 (vfd->size.h > vfd->size.w))
876 {
877 if (param->orient == PJMEDIA_ORIENT_UNKNOWN)
878 param->orient = PJMEDIA_ORIENT_NATURAL;
879 and_stream_set_cap(&strm->base, PJMEDIA_VID_DEV_CAP_ORIENTATION,
880 &param->orient);
881 }
882
883 on_return:
884 jni_detach_env(with_attach);
885
886 /* Success */
887 if (status == PJ_SUCCESS) {
888 strm->base.op = &stream_op;
889 *p_vid_strm = &strm->base;
890 }
891
892 return status;
893 }
894
895
896 /****************************************************************************
897 * Stream operations
898 */
899
900
901 /* API: Get stream info. */
902 static pj_status_t and_stream_get_param(pjmedia_vid_dev_stream *s,
903 pjmedia_vid_dev_param *pi)
904 {
905 and_stream *strm = (and_stream*)s;
906
907 PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL);
908
909 pj_memcpy(pi, &strm->param, sizeof(*pi));
910
911 if (and_stream_get_cap(s, PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW,
912 &pi->window) == PJ_SUCCESS)
913 {
914 pi->flags |= PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW;
915 }
916
917 return PJ_SUCCESS;
918 }
919
920
921 /* API: get capability */
922 static pj_status_t and_stream_get_cap(pjmedia_vid_dev_stream *s,
923 pjmedia_vid_dev_cap cap,
924 void *pval)
925 {
926 and_stream *strm = (and_stream*)s;
927
928 PJ_UNUSED_ARG(strm);
929
930 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
931
932 if (cap == PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW) {
933 //pjmedia_vid_dev_hwnd *wnd = (pjmedia_vid_dev_hwnd *)pval;
934 //wnd->info.android.window = strm->window;
935 //return PJ_SUCCESS;
936 }
937
938 return PJMEDIA_EVID_INVCAP;
939 }
940
941
942 /* API: set capability */
943 static pj_status_t and_stream_set_cap(pjmedia_vid_dev_stream *s,
944 pjmedia_vid_dev_cap cap,
945 const void *pval)
946 {
947 and_stream *strm = (and_stream*)s;
948 JNIEnv *jni_env;
949 pj_bool_t with_attach;
950 pj_status_t status = PJ_SUCCESS;
951
952 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
953
954 switch (cap) {
955 case PJMEDIA_VID_DEV_CAP_SWITCH:
956 {
957 pjmedia_vid_dev_switch_param *p = (pjmedia_vid_dev_switch_param*)
958 pval;
959 and_dev_info *adi;
960 int res;
961
962 /* Just return if current and target device are the same */
963 if (strm->param.cap_id == p->target_id)
964 return PJ_SUCCESS;
965
966 /* Verify target capture ID */
967 if (p->target_id < 0 || p->target_id >= strm->factory->dev_count)
968 return PJ_EINVAL;
969
970 /* Ok, let's do the switch */
971 adi = &strm->factory->dev_info[p->target_id];
972 PJ_LOG(4, (THIS_FILE, "Switching camera to %s..", adi->info.name));
973
974 /* Call PjCamera::Start() method */
975 with_attach = jni_get_env(&jni_env);
976 res = (*jni_env)->CallIntMethod(jni_env, strm->jcam,
977 jobjs.cam.m_switch, adi->dev_idx);
978 if (res < 0) {
979 PJ_LOG(3, (THIS_FILE, "Failed to switch camera (err=%d)",
980 res));
981 status = PJMEDIA_EVID_SYSERR;
982 } else {
983 strm->param.cap_id = p->target_id;
984
985 /* If successful, set the orientation as well */
986 and_stream_set_cap(s, PJMEDIA_VID_DEV_CAP_ORIENTATION,
987 &strm->param.orient);
988 }
989 jni_detach_env(with_attach);
990 break;
991 }
992
993 case PJMEDIA_VID_DEV_CAP_ORIENTATION:
994 {
995 pjmedia_orient orient = *(pjmedia_orient *)pval;
996 pjmedia_orient eff_ori;
997 and_dev_info *adi;
998
999 pj_assert(orient >= PJMEDIA_ORIENT_UNKNOWN &&
1000 orient <= PJMEDIA_ORIENT_ROTATE_270DEG);
1001
1002 if (orient == PJMEDIA_ORIENT_UNKNOWN)
1003 return PJ_EINVAL;
1004
1005 pj_memcpy(&strm->param.orient, pval,
1006 sizeof(strm->param.orient));
1007
1008 if (!strm->conv.conv) {
1009 status = pjmedia_vid_dev_conv_create_converter(
1010 &strm->conv, strm->pool,
1011 &strm->param.fmt,
1012 strm->cam_size,
1013 strm->param.fmt.det.vid.size,
1014 PJ_TRUE,
1015 MAINTAIN_ASPECT_RATIO);
1016
1017 if (status != PJ_SUCCESS)
1018 return status;
1019 }
1020
1021 eff_ori = strm->param.orient;
1022 adi = &strm->factory->dev_info[strm->param.cap_id];
1023 /* Normalize the orientation for back-facing camera */
1024 if (!adi->facing) {
1025 if (eff_ori == PJMEDIA_ORIENT_ROTATE_90DEG)
1026 eff_ori = PJMEDIA_ORIENT_ROTATE_270DEG;
1027 else if (eff_ori == PJMEDIA_ORIENT_ROTATE_270DEG)
1028 eff_ori = PJMEDIA_ORIENT_ROTATE_90DEG;
1029 }
1030 pjmedia_vid_dev_conv_set_rotation(&strm->conv, eff_ori);
1031
1032 PJ_LOG(4, (THIS_FILE, "Video capture orientation set to %d",
1033 strm->param.orient));
1034
1035 break;
1036 }
1037
1038 default:
1039 status = PJMEDIA_EVID_INVCAP;
1040 break;
1041 }
1042
1043 return status;
1044 }
1045
1046
1047 /* API: Start stream. */
1048 static pj_status_t and_stream_start(pjmedia_vid_dev_stream *s)
1049 {
1050 and_stream *strm = (and_stream*)s;
1051 JNIEnv *jni_env;
1052 pj_bool_t with_attach;
1053 jint res;
1054 pj_status_t status = PJ_SUCCESS;
1055
1056 PJ_LOG(4, (THIS_FILE, "Starting Android camera stream"));
1057
1058 with_attach = jni_get_env(&jni_env);
1059
1060 /* Call PjCamera::Start() method */
1061 res = (*jni_env)->CallIntMethod(jni_env, strm->jcam, jobjs.cam.m_start);
1062 if (res < 0) {
1063 PJ_LOG(3, (THIS_FILE, "Failed to start camera (err=%d)", res));
1064 status = PJMEDIA_EVID_SYSERR;
1065 goto on_return;
1066 }
1067
1068 strm->is_running = PJ_TRUE;
1069
1070 on_return:
1071 jni_detach_env(with_attach);
1072 return status;
1073 }
1074
1075
1076 /* API: Stop stream. */
1077 static pj_status_t and_stream_stop(pjmedia_vid_dev_stream *s)
1078 {
1079 and_stream *strm = (and_stream*)s;
1080 JNIEnv *jni_env;
1081 pj_bool_t with_attach;
1082 pj_status_t status = PJ_SUCCESS;
1083
1084 PJ_ASSERT_RETURN(strm != NULL, PJ_EINVAL);
1085
1086 PJ_LOG(4, (THIS_FILE, "Stopping Android camera stream"));
1087
1088 with_attach = jni_get_env(&jni_env);
1089
1090 /* Call PjCamera::Stop() method */
1091 (*jni_env)->CallVoidMethod(jni_env, strm->jcam, jobjs.cam.m_stop);
1092
1093 strm->is_running = PJ_FALSE;
1094
1095 jni_detach_env(with_attach);
1096
1097 return status;
1098 }
1099
1100
1101 /* API: Destroy stream. */
1102 static pj_status_t and_stream_destroy(pjmedia_vid_dev_stream *s)
1103 {
1104 and_stream *strm = (and_stream*)s;
1105 JNIEnv *jni_env;
1106 pj_bool_t with_attach;
1107
1108 PJ_ASSERT_RETURN(strm != NULL, PJ_EINVAL);
1109
1110 with_attach = jni_get_env(&jni_env);
1111
1112 if (strm->is_running)
1113 and_stream_stop(s);
1114
1115 if (strm->jcam) {
1116 (*jni_env)->DeleteGlobalRef(jni_env, strm->jcam);
1117 strm->jcam = NULL;
1118 }
1119
1120 jni_detach_env(with_attach);
1121
1122 pjmedia_vid_dev_conv_destroy_converter(&strm->conv);
1123
1124 if (strm->pool)
1125 pj_pool_release(strm->pool);
1126
1127 PJ_LOG(4, (THIS_FILE, "Android camera stream destroyed"));
1128
1129 return PJ_SUCCESS;
1130 }
1131
1132 #if USE_CAMERA2
1133
1134 PJ_INLINE(void) strip_padding(void *dst, void *src, int w, int h, int stride)
1135 {
1136 int i;
1137 for (i = 0; i < h; ++i) {
1138 pj_memmove(dst, src, w);
1139 src += stride;
1140 dst += w;
1141 }
1142 }
1143
1144 static void JNICALL OnGetFrame2(JNIEnv *env, jobject obj,
1145 jlong user_data,
1146 jobject plane0, jint rowStride0, jint pixStride0,
1147 jobject plane1, jint rowStride1, jint pixStride1,
1148 jobject plane2, jint rowStride2, jint pixStride2)
1149 {
1150 and_stream *strm = (and_stream*)(intptr_t)user_data;
1151 pjmedia_frame f;
1152 pj_uint8_t *p0, *p1, *p2, *p0_end;
1153 pj_uint8_t *Y, *U, *V;
1154 pj_status_t status;
1155 void *frame_buf, *data_buf;
1156
1157 strm->frame_ts.u64 += strm->ts_inc;
1158 if (!strm->vid_cb.capture_cb)
1159 return;
1160
1161 if (strm->thread_initialized == 0 || !pj_thread_is_registered()) {
1162 pj_status_t status;
1163 pj_bzero(strm->thread_desc, sizeof(pj_thread_desc));
1164 status = pj_thread_register("and_cam", strm->thread_desc,
1165 &strm->thread);
1166 if (status != PJ_SUCCESS)
1167 return;
1168 strm->thread_initialized = 1;
1169 PJ_LOG(5,(THIS_FILE, "Android camera thread registered"));
1170 }
1171
1172 p0 = (pj_uint8_t*)(*env)->GetDirectBufferAddress(env, plane0);
1173 p1 = (pj_uint8_t*)(*env)->GetDirectBufferAddress(env, plane1);
1174 p2 = (pj_uint8_t*)(*env)->GetDirectBufferAddress(env, plane2);
1175
1176 /* Assuming the buffers are originally a large contigue buffer,
1177 * minimum check for now: plane 1 or 2 must be after plane 0.
1178 */
1179 p0_end = p0 + strm->cam_size.h * rowStride0;
1180 pj_assert(p1 >= p0_end || p2 >= p0_end);
1181
1182 f.type = PJMEDIA_FRAME_TYPE_VIDEO;
1183 f.size = strm->vafp.framebytes;
1184 f.timestamp.u64 = strm->frame_ts.u64;
1185 f.buf = data_buf = p0;
1186
1187 /* In this implementation, we only return I420 frames, so here we need to
1188 * convert other formats and strip any padding.
1189 */
1190
1191 Y = (pj_uint8_t*)f.buf;
1192 U = Y + strm->vafp.plane_bytes[0];
1193 V = U + strm->vafp.plane_bytes[1];
1194
1195 /* Check if we need conversion here, this is the tricky part of camera2.
1196 * When we request I420, the returned buffer may not be actually I420,
1197 * for example NV21. The camera2 'cheats' us via it's Plane type which
1198 * has pixel stride attribute. For example, NV21 buffer structure will
1199 * be represented as I420 using Plane array with the following attributes:
1200 * - Plane[1], or U plane, points to address of (Plane[0] + Ysize + 1).
1201 * - Plane[2], or V plane, points to address of (Plane[0] + Ysize).
1202 * - Pixel stride is set to 2 for U & V planes, and 1 for Y plane.
1203 */
1204
1205 /* Already I420 without padding, nothing to do */
1206 if (p1 == U && p2 == V) {}
1207
1208 /* I420 with padding, remove padding */
1209 else if (pixStride1==1 && pixStride2==1 && p2 > p1 && p1 > p0)
1210 {
1211 /* Strip out Y padding */
1212 if (rowStride0 > strm->cam_size.w) {
1213 strip_padding(Y, p0, strm->cam_size.w, strm->cam_size.h,
1214 rowStride0);
1215 }
1216
1217 /* Get U & V planes */
1218
1219 if (rowStride1 == strm->cam_size.w/2) {
1220 /* No padding, simply bulk memmove U & V */
1221 pj_memmove(U, p1, strm->vafp.plane_bytes[1]);
1222 pj_memmove(V, p2, strm->vafp.plane_bytes[2]);
1223 } else if (rowStride1 > strm->cam_size.w/2) {
1224 /* Strip padding */
1225 strip_padding(U, p1, strm->cam_size.w/2, strm->cam_size.h/2,
1226 rowStride1);
1227 strip_padding(V, p2, strm->cam_size.w/2, strm->cam_size.h/2,
1228 rowStride2);
1229 }
1230 }
1231
1232 /* The buffer may be originally NV21/NV12, i.e: VU/UV is interleaved */
1233 else if ((p1-p2==1 || p2-p1==1) &&
1234 pixStride0==1 && pixStride1==2 && pixStride2==2)
1235 {
1236 pj_bool_t nv21 = p1 > p2;
1237
1238 /* Strip out Y padding */
1239 if (rowStride0 > strm->cam_size.w) {
1240 strip_padding(Y, p0, strm->cam_size.w, strm->cam_size.h,
1241 rowStride0);
1242 }
1243
1244 /* Get U & V, and strip if needed */
1245 {
1246 pj_uint8_t *dst_u = U;
1247 pj_uint8_t *dst_v = strm->convert_buf;
1248 int diff = rowStride1 - strm->cam_size.w;
1249 int i, j;
1250
1251 if (nv21) {
1252 pj_uint8_t *src = p2;
1253 for (i = 0; i < strm->cam_size.h/2; ++i) {
1254 for (j = 0; j < strm->cam_size.w/2; ++j) {
1255 *dst_v++ = *src++;
1256 *dst_u++ = *src++;
1257 }
1258 src += diff; /* stripping any padding */
1259 }
1260 } else {
1261 pj_uint8_t *src = p1;
1262 for (i = 0; i < strm->cam_size.h/2; ++i) {
1263 for (j = 0; j < strm->cam_size.w/2; ++j) {
1264 *dst_u++ = *src++;
1265 *dst_v++ = *src++;
1266 }
1267 src += diff; /* stripping any padding */
1268 }
1269 }
1270 pj_memcpy(V, strm->convert_buf, strm->vafp.plane_bytes[2]);
1271 }
1272 }
1273
1274 /* The buffer may be originally YV12, i.e: U & V planes are swapped.
1275 * We also need to strip out padding, if any.
1276 */
1277 else if (pixStride1==1 && pixStride2==1 && p1 > p2 && p2 > p0)
1278 {
1279 /* Strip out Y padding */
1280 if (rowStride0 > strm->cam_size.w) {
1281 strip_padding(Y, p0, strm->cam_size.w, strm->cam_size.h,
1282 rowStride0);
1283 }
1284
1285 /* Swap U & V planes */
1286 if (rowStride1 == strm->cam_size.w/2) {
1287
1288 /* No padding, note Y plane should be no padding too! */
1289 pj_assert(rowStride0 == strm->cam_size.w);
1290 pj_memcpy(strm->convert_buf, p1, strm->vafp.plane_bytes[1]);
1291 pj_memmove(U, p1, strm->vafp.plane_bytes[1]);
1292 pj_memcpy(V, strm->convert_buf, strm->vafp.plane_bytes[1]);
1293
1294 } else if (rowStride1 > strm->cam_size.w/2) {
1295
1296 /* Strip padding */
1297 strip_padding(strm->convert_buf, p1, strm->cam_size.w/2,
1298 strm->cam_size.h/2, rowStride1);
1299 strip_padding(V, p2, strm->cam_size.w/2, strm->cam_size.h/2,
1300 rowStride2);
1301
1302 /* Get V plane data from conversion buffer */
1303 pj_memcpy(V, strm->convert_buf, strm->vafp.plane_bytes[2]);
1304
1305 }
1306 }
1307
1308 /* Else, let's just print log for now */
1309 else {
1310 jlong p0_len, p1_len, p2_len;
1311
1312 p0_len = (*env)->GetDirectBufferCapacity(env, plane0);
1313 p1_len = (*env)->GetDirectBufferCapacity(env, plane1);
1314 p2_len = (*env)->GetDirectBufferCapacity(env, plane2);
1315
1316 PJ_LOG(1,(THIS_FILE, "Unrecognized image format from Android camera2, "
1317 "please report the following plane format:"));
1318 PJ_LOG(1,(THIS_FILE, " Planes (buf/len/row_stride/pix_stride):"
1319 " p0=%p/%ld/%d/%d p1=%p/%ld/%d/%d "
1320 "p2=%p/%ld/%d/%d",
1321 p0, p0_len, rowStride0, pixStride0,
1322 p1, p1_len, rowStride1, pixStride1,
1323 p2, p2_len, rowStride2, pixStride2));
1324
1325 #if 1
1326 /* Generic converter to I420, based on row stride & pixel stride */
1327
1328 /* Strip out Y padding */
1329 if (rowStride0 > strm->cam_size.w) {
1330 strip_padding(Y, p0, strm->cam_size.w, strm->cam_size.h,
1331 rowStride0);
1332 }
1333
1334 /* Get U & V, and strip if needed */
1335 {
1336 pj_uint8_t *src_u = p1;
1337 pj_uint8_t *src_v = p2;
1338 pj_uint8_t *dst_u = U;
1339 pj_uint8_t *dst_v = strm->convert_buf;
1340 int i;
1341
1342 /* Note, we use convert buffer for V, just in case U & V are
1343 * swapped.
1344 */
1345 for (i = 0; i < strm->cam_size.h/2; ++i) {
1346 int j;
1347 for (j = 0; j < strm->cam_size.w/2; ++j) {
1348 *dst_v++ = *(src_v + j*pixStride2);
1349 *dst_u++ = *(src_u + j*pixStride1);
1350 }
1351 src_u += rowStride1;
1352 src_v += rowStride2;
1353 }
1354 pj_memcpy(V, strm->convert_buf, strm->vafp.plane_bytes[2]);
1355 }
1356 #endif
1357
1358 }
1359
1360 status = pjmedia_vid_dev_conv_resize_and_rotate(&strm->conv,
1361 f.buf,
1362 &frame_buf);
1363 if (status == PJ_SUCCESS) {
1364 f.buf = frame_buf;
1365 }
1366
1367 (*strm->vid_cb.capture_cb)(&strm->base, strm->user_data, &f);
1368 }
1369
1370 #else
1371
1372 static void JNICALL OnGetFrame(JNIEnv *env, jobject obj,
1373 jbyteArray data, jint length,
1374 jlong user_data)
1375 {
1376 and_stream *strm = (and_stream*)(intptr_t)user_data;
1377 pjmedia_frame f;
1378 pj_uint8_t *Y, *U, *V;
1379 pj_status_t status;
1380 void *frame_buf, *data_buf;
1381
1382 strm->frame_ts.u64 += strm->ts_inc;
1383 if (!strm->vid_cb.capture_cb)
1384 return;
1385
1386 if (strm->thread_initialized == 0 || !pj_thread_is_registered()) {
1387 pj_status_t status;
1388 pj_bzero(strm->thread_desc, sizeof(pj_thread_desc));
1389 status = pj_thread_register("and_cam", strm->thread_desc,
1390 &strm->thread);
1391 if (status != PJ_SUCCESS)
1392 return;
1393 strm->thread_initialized = 1;
1394 PJ_LOG(5,(THIS_FILE, "Android camera thread registered"));
1395 }
1396
1397 f.type = PJMEDIA_FRAME_TYPE_VIDEO;
1398 f.size = length;
1399 f.timestamp.u64 = strm->frame_ts.u64;
1400 f.buf = data_buf = (*env)->GetByteArrayElements(env, data, 0);
1401
1402 Y = (pj_uint8_t*)f.buf;
1403 U = Y + strm->vafp.plane_bytes[0];
1404 V = U + strm->vafp.plane_bytes[1];
1405
1406 /* Convert NV21 -> I420, i.e: separate V/U interleaved data plane
1407 * into U & V planes.
1408 */
1409 if (strm->convert_to_i420 == 1) {
1410 pj_uint8_t *src = U;
1411 pj_uint8_t *dst_u = U;
1412 pj_uint8_t *end_u = U + strm->vafp.plane_bytes[1];
1413 pj_uint8_t *dst_v = strm->convert_buf;
1414 while (dst_u < end_u) {
1415 *dst_v++ = *src++;
1416 *dst_u++ = *src++;
1417 }
1418 pj_memcpy(V, strm->convert_buf, strm->vafp.plane_bytes[2]);
1419 }
1420
1421 /* Convert YV12 -> I420, i.e: swap U & V planes. We also need to
1422 * strip out padding, if any.
1423 */
1424 else if (strm->convert_to_i420 == 2) {
1425 int y_stride = ALIGN16(strm->vafp.size.w);
1426 int uv_stride = ALIGN16(strm->vafp.size.w/2);
1427
1428 /* Strip out Y padding */
1429 if (y_stride > strm->vafp.size.w) {
1430 int i;
1431 pj_uint8_t *src = Y + y_stride;
1432 pj_uint8_t *dst = Y + strm->vafp.size.w;
1433
1434 for (i = 1; i < strm->vafp.size.h; ++i) {
1435 memmove(dst, src, strm->vafp.size.w);
1436 src += y_stride;
1437 dst += strm->vafp.size.w;
1438 }
1439 }
1440
1441 /* Swap U & V planes */
1442 if (uv_stride == strm->vafp.size.w/2) {
1443
1444 /* No padding, note Y plane should be no padding too! */
1445 pj_assert(y_stride == strm->vafp.size.w);
1446 pj_memcpy(strm->convert_buf, U, strm->vafp.plane_bytes[1]);
1447 pj_memmove(U, V, strm->vafp.plane_bytes[1]);
1448 pj_memcpy(V, strm->convert_buf, strm->vafp.plane_bytes[1]);
1449
1450 } else if (uv_stride > strm->vafp.size.w/2) {
1451
1452 /* Strip & copy V plane into conversion buffer */
1453 pj_uint8_t *src = Y + y_stride*strm->vafp.size.h;
1454 pj_uint8_t *dst = strm->convert_buf;
1455 unsigned dst_stride = strm->vafp.size.w/2;
1456 int i;
1457 for (i = 0; i < strm->vafp.size.h/2; ++i) {
1458 memmove(dst, src, dst_stride);
1459 src += uv_stride;
1460 dst += dst_stride;
1461 }
1462
1463 /* Strip U plane */
1464 dst = U;
1465 for (i = 0; i < strm->vafp.size.h/2; ++i) {
1466 memmove(dst, src, dst_stride);
1467 src += uv_stride;
1468 dst += dst_stride;
1469 }
1470
1471 /* Get V plane data from conversion buffer */
1472 pj_memcpy(V, strm->convert_buf, strm->vafp.plane_bytes[2]);
1473
1474 }
1475 }
1476
1477 status = pjmedia_vid_dev_conv_resize_and_rotate(&strm->conv,
1478 f.buf,
1479 &frame_buf);
1480 if (status == PJ_SUCCESS) {
1481 f.buf = frame_buf;
1482 }
1483
1484 (*strm->vid_cb.capture_cb)(&strm->base, strm->user_data, &f);
1485 (*env)->ReleaseByteArrayElements(env, data, data_buf, JNI_ABORT);
1486 }
1487
1488 #endif /* USE_CAMERA2 */
1489
1490 #endif /* PJMEDIA_VIDEO_DEV_HAS_ANDROID */