android_dev.c 51 KB

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