cam_demo_multi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /*
  2. * Copyright (C) 2021 Alibaba Group Holding Limited
  3. * Author: ShenWuYi <shenwuyi.swy@alibaba-inc.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <fcntl.h>
  13. #include <unistd.h>
  14. #include <sys/mman.h>
  15. #define LOG_LEVEL 2
  16. #define LOG_PREFIX "cam_demo_multi"
  17. #include <syslog.h>
  18. #include <csi_frame.h>
  19. #include <csi_camera.h>
  20. #include <csi_camera_dev_api.h>
  21. #ifdef PLATFORM_SIMULATOR
  22. #include "apputilities.h"
  23. #endif
  24. #define MAX_CAM_NUM 3
  25. #define MAX_CHANNEL_NUM 3
  26. static void dump_camera_meta(csi_frame_s *frame);
  27. static void dump_camera_frame(csi_frame_s *frame, csi_pixel_fmt_e fmt, int camera_id);
  28. // extern int csi_camera_frame_unlock(csi_cam_handle_t cam_handle, csi_frame_s *frame);
  29. #define TEST_DEVICE_NAME "/dev/video0"
  30. #define CSI_CAMERA_TRUE 1
  31. #define CSI_CAMERA_FALSE 0
  32. typedef struct frame_fps {
  33. uint64_t frameNumber;
  34. struct timeval initTick;
  35. float fps;
  36. } frame_fps_t;
  37. typedef enum _frame_mode{
  38. FRAME_NONE =0,
  39. FRAME_SAVE_IMG =1,
  40. FRAME_SAVE_STREAM =2,
  41. FRAME_SAVE_DISPLAY,
  42. FRAME_INVALID,
  43. }frame_mode_t;
  44. typedef enum _cam_type{
  45. CAM_TYEP_RGB =0,
  46. CAM_TYEP_IR,
  47. CAM_TYEP_INVALID,
  48. }cam_type_e;
  49. typedef struct _camera_param{
  50. int video_id;
  51. int channel_num;
  52. struct {
  53. int width;
  54. int height;
  55. csi_pixel_fmt_e fmt;
  56. }out_pic[MAX_CHANNEL_NUM];
  57. cam_type_e type;
  58. frame_mode_t mode;
  59. int frames_to_stop;
  60. }camera_param_t;
  61. typedef struct event_queue_item{
  62. struct csi_camera_event evet;
  63. struct event_queue_item *next;
  64. }event_queue_item_t;
  65. typedef struct _camera_ctx{
  66. int cam_id;
  67. pthread_t cam_thread;
  68. int exit;
  69. csi_cam_handle_t cam_handle;
  70. int channel_num;
  71. csi_cam_event_handle_t event_handle;
  72. csi_camera_event_subscription_s event_subscribe;
  73. csi_camera_channel_cfg_s chn_cfg[MAX_CHANNEL_NUM];
  74. void * disaply_plink[MAX_CHANNEL_NUM];
  75. int frame_num;
  76. frame_mode_t frame_mode[MAX_CHANNEL_NUM];
  77. }camera_ctx_t;
  78. int file_id = 0;
  79. extern void *vi_plink_create(csi_camera_channel_cfg_s *chn_cfg);
  80. extern void vi_plink_release(void * plink);
  81. extern void display_camera_frame(void * plink, csi_frame_s *frame);
  82. static void usage(void)
  83. {
  84. printf(" 1 : RGB Camera id \n");
  85. printf(" 2 : RGB Camera id channel num\n");
  86. printf(" 3 : RGB Camera frame mode :0 none ,1 dump enable,2 display \n");
  87. printf(" 4 : RGB Camera frame num :0 not stop ,else frame num to stop \n");
  88. printf(" 5 : IR Camera id \n");
  89. printf(" 6 : IR Camera id channel num\n");
  90. printf(" 7 : IR Camera frame mode :0 none ,1 dump enable,2 display \n");
  91. printf(" 8 : IR Camera frame num :0 not stop ,else frame num to stop \n");
  92. }
  93. void printUsage(char *name)
  94. {
  95. printf("usage: %s [options]\n"
  96. "\n"
  97. " Available options:\n"
  98. " -f fisrt camera device 5 param \n"
  99. " 1:video id 2: channel num 3: camrea type (0->rgb ;1->IR) (mandatory)\n"
  100. " 3: mode【0->none ,1->save image,2->display 】(default 1) \n"
  101. " 4: frame num to stop 【0 not stop】(default 0) \n"
  102. " -s second camera device 5 param)\n"
  103. " same param setting as above)\n"
  104. " -t third camera device 5 param)\n"
  105. " same param setting above)\n"
  106. "\n", name);
  107. }
  108. static int parseParams(int argc, char **argv, camera_param_t *params)
  109. {
  110. int index =0;
  111. params[index].video_id = 2;
  112. params[index].type = CAM_TYEP_RGB;
  113. params[index].channel_num =1;
  114. params[index].out_pic[0].width = 1920;
  115. params[index].out_pic[0].height = 1080;
  116. params[index].out_pic[0].fmt = CSI_PIX_FMT_NV12;
  117. params[index].mode = FRAME_SAVE_IMG;
  118. params[index].frames_to_stop = 0;
  119. index++;
  120. params[index].video_id = 6;
  121. params[index].type = CAM_TYEP_IR;
  122. params[index].channel_num = 2;
  123. params[index].out_pic[0].width = 1080;
  124. params[index].out_pic[0].height = 1280;
  125. params[index].out_pic[0].fmt =CSI_PIX_FMT_RAW_12BIT;
  126. params[index].out_pic[1].width = 1080;
  127. params[index].out_pic[1].height = 1280;
  128. params[index].out_pic[1].fmt =CSI_PIX_FMT_RAW_12BIT;
  129. params[index].mode = FRAME_SAVE_IMG;
  130. params[index].frames_to_stop = 0;
  131. index++;
  132. return index;
  133. }
  134. static void get_system_time(const char *func, int line_num)
  135. {
  136. struct timeval cur_time;
  137. memset(&cur_time, 0, sizeof(cur_time));
  138. gettimeofday(&cur_time, 0);
  139. printf("%s %s line_num = %d, cur_time.tv_sec = %ld, cur_time.tv_usec = %ld\n",
  140. __func__, func, line_num, cur_time.tv_sec, cur_time.tv_usec);
  141. }
  142. static int camera_get_chl_id(int env_type, int *chl_id)
  143. {
  144. if (chl_id == NULL) {
  145. LOG_E("fail to check param chl_id = %p\n", chl_id);
  146. return -1;
  147. }
  148. switch (env_type) {
  149. case CSI_CAMERA_EVENT_TYPE_CHANNEL0:
  150. *chl_id = CSI_CAMERA_CHANNEL_0;
  151. break;
  152. case CSI_CAMERA_EVENT_TYPE_CHANNEL1:
  153. *chl_id = CSI_CAMERA_CHANNEL_1;
  154. break;
  155. case CSI_CAMERA_EVENT_TYPE_CHANNEL2:
  156. *chl_id = CSI_CAMERA_CHANNEL_2;
  157. break;
  158. default:
  159. LOG_D("%s fail to check env_type = %d unsupport\n", env_type);
  160. break;
  161. }
  162. return 0;
  163. }
  164. /***********************DSP ALGO *******************************/
  165. typedef struct cb_context{
  166. int id;
  167. csi_cam_handle_t cam_handle;
  168. int dsp_id;
  169. int dsp_path;
  170. void *ref_buf;
  171. int buf_size;
  172. }cb_context_t;
  173. typedef struct cb_args{
  174. char lib_name[8];
  175. char setting[16];
  176. uint32_t frame_id;
  177. uint64_t timestap;
  178. uint32_t temp_projector; //投射器温度
  179. uint32_t temp_sensor; //sensor 温度
  180. }cb_args_t;
  181. cb_context_t cb_context[2];
  182. void dsp_algo_result_cb(void*context,void*arg)
  183. {
  184. cb_context_t *ctx = (cb_context_t *)context;
  185. cb_args_t *cb_args= (cb_args_t*)arg;
  186. char update_setting[16];
  187. struct timeval times_value;
  188. uint32_t frame_id = cb_args->frame_id;
  189. uint64_t frame_timestap = cb_args->timestap;
  190. times_value.tv_sec = cb_args->timestap/1000000;
  191. times_value.tv_usec = cb_args->timestap%1000000;
  192. LOG_O("cb:%d,%s,setting:%s,frame :%d,timestad:(%ld s,%ld us)\n",ctx->id,cb_args->lib_name,cb_args->setting,
  193. frame_id,times_value.tv_sec,times_value.tv_usec);
  194. LOG_O("temperate projector:%d,sensor:%d\n",cb_args->temp_projector,cb_args->temp_sensor);
  195. // if(test_flag!=0)
  196. {
  197. sprintf(update_setting, "update_%d", frame_id);
  198. csi_camera_update_dsp_algo_setting(ctx->cam_handle,ctx->dsp_id,ctx->dsp_path,update_setting);
  199. if(frame_id%20==0)
  200. {
  201. void *replace_buf;
  202. *((uint32_t*)ctx->ref_buf) = frame_id;
  203. csi_camera_update_dsp_algo_buf(ctx->cam_handle, ctx->dsp_id,ctx->dsp_path,ctx->ref_buf,&replace_buf);
  204. LOG_D("cb:%d,old buffer:0x%llx,new buffer:0x%llx\n",ctx->id,ctx->ref_buf,replace_buf);
  205. ctx->ref_buf = replace_buf;
  206. }
  207. }
  208. }
  209. static void *camera_event_thread(void *arg)
  210. {
  211. int ret = 0;
  212. if (arg == NULL) {
  213. LOG_E("NULL Ptr\n");
  214. pthread_exit(0);
  215. }
  216. camera_ctx_t* ctx = (camera_ctx_t*)arg;
  217. csi_cam_event_handle_t ev_handle = ctx->event_handle;
  218. csi_camera_channel_cfg_s *ch_cfg=NULL;
  219. struct timeval init_time, cur_time;
  220. memset(&init_time, 0, sizeof(init_time));
  221. memset(&cur_time, 0, sizeof(cur_time));
  222. frame_fps_t demo_fps;
  223. memset(&demo_fps, 0, sizeof(demo_fps));
  224. csi_frame_s frame;
  225. if (ev_handle == NULL) {
  226. LOG_E("fail to get ev_handle ev_handle\n");
  227. pthread_exit(0);
  228. }
  229. int timeout = -1; // unit: ms, -1 means wait forever, or until error occurs
  230. int loop_count = 0;
  231. struct csi_camera_event event;
  232. LOG_O("Theard is runing............\n");
  233. while (ctx->exit==0) {
  234. int timeout = -1; // unit: ms, -1 means wait forever, or until error occurs
  235. csi_camera_get_event(ev_handle, &event, timeout);
  236. LOG_D("Camera_%d event.type = %d, event.id = %d\n",ctx->cam_id ,event.type, event.id);
  237. switch (event.type) {
  238. case CSI_CAMERA_EVENT_TYPE_CAMERA:
  239. switch (event.id) {
  240. case CSI_CAMERA_EVENT_ERROR:
  241. // do sth.
  242. LOG_E("get CAMERA EVENT CSI_CAMERA_EVENT_ERROR!\n");
  243. break;
  244. case CSI_CAMERA_EVENT_WARNING:
  245. LOG_W("get CAMERA EVENT CSI_CAMERA_EVENT_WRN,RC: %s\n",event.bin);
  246. break;
  247. default:
  248. break;
  249. }
  250. break;
  251. case CSI_CAMERA_EVENT_TYPE_CHANNEL0:
  252. case CSI_CAMERA_EVENT_TYPE_CHANNEL1:
  253. case CSI_CAMERA_EVENT_TYPE_CHANNEL2:
  254. case CSI_CAMERA_EVENT_TYPE_CHANNEL3:
  255. switch (event.id) {
  256. case CSI_CAMERA_CHANNEL_EVENT_FRAME_READY: {
  257. int chn_id = 0;
  258. ret = camera_get_chl_id(event.type, &chn_id);
  259. if (ret) {
  260. LOG_E("fail to get chl_id = %d\n", chn_id);
  261. continue;
  262. }
  263. ch_cfg = &ctx->chn_cfg[chn_id];
  264. get_system_time(__func__, __LINE__);
  265. int read_frame_count = csi_camera_get_frame_count(ctx->cam_handle,
  266. chn_id);
  267. unsigned long diff;
  268. if (init_time.tv_usec == 0)
  269. gettimeofday(&init_time,0);//osGetTick();
  270. gettimeofday(&cur_time, 0);
  271. diff = 1000000 * (cur_time.tv_sec-init_time.tv_sec)+ cur_time.tv_usec-init_time.tv_usec;
  272. demo_fps.frameNumber++;
  273. if (diff != 0)
  274. demo_fps.fps = (float)demo_fps.frameNumber / diff * 1000000.0f;
  275. LOG_O("cam:%d,channle:%d ,read_frame_count = %d, frame_count = %ld, fps = %.2f diff = %ld\n", ctx->cam_id, chn_id, read_frame_count, demo_fps.frameNumber, demo_fps.fps, diff);
  276. for (int i = 0; i < read_frame_count; i++) {
  277. csi_camera_get_frame(ctx->cam_handle, chn_id, &frame, timeout);
  278. dump_camera_meta(&frame);
  279. if (ctx->frame_mode[chn_id] == FRAME_SAVE_IMG) {
  280. dump_camera_frame(&frame, ch_cfg->img_fmt.pix_fmt, chn_id);
  281. }
  282. if (ctx->disaply_plink && ctx->frame_mode[chn_id]==FRAME_SAVE_DISPLAY) {
  283. frame.img.pix_format = ch_cfg->img_fmt.pix_fmt;
  284. display_camera_frame(ctx->disaply_plink, &frame);
  285. }
  286. csi_camera_put_frame(&frame);
  287. ctx->frame_num++;
  288. }
  289. break;
  290. }
  291. default:
  292. break;
  293. }
  294. break;
  295. default:
  296. break;
  297. }
  298. }
  299. LOG_O("exit!\n");
  300. pthread_exit(0);
  301. }
  302. static void camera_start_all(camera_ctx_t * ctx)
  303. {
  304. camera_ctx_t * cam_ctx = ctx;
  305. int loop_ch;
  306. for(loop_ch=0;loop_ch<cam_ctx->channel_num;loop_ch++)
  307. {
  308. csi_camera_channel_start(cam_ctx->cam_handle, cam_ctx->chn_cfg[loop_ch].chn_id);
  309. }
  310. }
  311. static void camera_stop_all(camera_ctx_t * ctx)
  312. {
  313. camera_ctx_t * cam_ctx = ctx;
  314. int loop_ch;
  315. for(loop_ch=0;loop_ch<cam_ctx->channel_num;loop_ch++)
  316. {
  317. csi_camera_channel_stop(cam_ctx->cam_handle, cam_ctx->chn_cfg[loop_ch].chn_id);
  318. }
  319. }
  320. static camera_ctx_t * camera_open(camera_param_t *params)
  321. {
  322. int ret = 0;
  323. char dev_name[128];
  324. camera_ctx_t * cam_ctx = NULL;
  325. int loop_ch;
  326. LOG_O("Open Camera %d\n",params->video_id);
  327. if(params==NULL || params->video_id <0)
  328. {
  329. LOG_E("param err\n");
  330. return NULL;
  331. }
  332. if(params->channel_num> MAX_CHANNEL_NUM)
  333. {
  334. LOG_E("unsupoort channle num:%d \n",params->channel_num);
  335. return NULL;
  336. }
  337. if(params->mode >=FRAME_INVALID)
  338. {
  339. LOG_E("unsupoort frame process mode:%d \n",params->mode);
  340. return NULL;
  341. }
  342. cam_ctx = malloc(sizeof(camera_ctx_t));
  343. if(!cam_ctx)
  344. {
  345. return NULL;
  346. }
  347. memset(cam_ctx,0x0,sizeof(camera_ctx_t));
  348. // 打开Camera设备获取句柄,作为后续操对象
  349. sprintf(dev_name, "/dev/video%d", params->video_id);
  350. if(csi_camera_open(&cam_ctx->cam_handle, dev_name))
  351. {
  352. LOG_E("Fail to open cam :%s\n",dev_name);
  353. goto ONE_ERR;
  354. }
  355. for(loop_ch= CSI_CAMERA_CHANNEL_0;loop_ch<params->channel_num;loop_ch++)
  356. {
  357. cam_ctx->chn_cfg[loop_ch].chn_id = loop_ch;
  358. cam_ctx->chn_cfg[loop_ch].img_fmt.pix_fmt = params->out_pic[loop_ch].fmt;
  359. cam_ctx->chn_cfg[loop_ch].img_fmt.width= params->out_pic[loop_ch].width;
  360. cam_ctx->chn_cfg[loop_ch].img_fmt.height = params->out_pic[loop_ch].height;
  361. cam_ctx->chn_cfg[loop_ch].img_type = CSI_IMG_TYPE_DMA_BUF;
  362. cam_ctx->chn_cfg[loop_ch].meta_fields = CSI_CAMERA_META_DEFAULT_FIELDS;
  363. if(csi_camera_channel_open(cam_ctx->cam_handle,&cam_ctx->chn_cfg[loop_ch]))
  364. {
  365. LOG_E("Fail to open cam %s,channel :%d\n",dev_name,loop_ch);
  366. goto TWO_ERR;
  367. }
  368. if(params->mode == FRAME_SAVE_DISPLAY)
  369. {
  370. cam_ctx->disaply_plink[loop_ch] = vi_plink_create(&cam_ctx->chn_cfg[loop_ch]);
  371. if(!cam_ctx->disaply_plink[loop_ch]&& loop_ch== CSI_CAMERA_CHANNEL_0) //temp only channel to display
  372. {
  373. LOG_E("Fail to create plink for cam %s,channel :%d\n",dev_name,loop_ch);
  374. goto TWO_ERR;
  375. }
  376. }
  377. cam_ctx->frame_mode[loop_ch]=params->mode;
  378. cam_ctx->channel_num++;
  379. }
  380. if(csi_camera_create_event(&cam_ctx->event_handle,cam_ctx->cam_handle))
  381. {
  382. LOG_E("Fail to create event handler for cam %s\n",dev_name);
  383. goto TWO_ERR;
  384. }
  385. cam_ctx->event_subscribe.type = CSI_CAMERA_EVENT_TYPE_CAMERA;
  386. cam_ctx->event_subscribe.id = CSI_CAMERA_EVENT_WARNING | CSI_CAMERA_EVENT_ERROR;
  387. if(csi_camera_subscribe_event(cam_ctx->event_handle,&cam_ctx->event_subscribe))
  388. {
  389. LOG_E("Fail to subscribe eventfor cam %s\n",dev_name);
  390. goto TWO_ERR;
  391. }
  392. for(loop_ch=0;loop_ch<cam_ctx->channel_num;loop_ch++)
  393. {
  394. cam_ctx->event_subscribe.type = CSI_CAMERA_EVENT_TYPE_CHANNEL0+loop_ch;
  395. cam_ctx->event_subscribe.id = CSI_CAMERA_CHANNEL_EVENT_FRAME_READY;
  396. if(csi_camera_subscribe_event(cam_ctx->event_handle,&cam_ctx->event_subscribe))
  397. {
  398. LOG_E("Fail to subscribe eventfor cam %s\n",dev_name);
  399. goto TWO_ERR;
  400. }
  401. }
  402. LOG_O("%s open successfully\n",dev_name);
  403. ret = pthread_create(&cam_ctx->cam_thread, NULL,
  404. (void *)camera_event_thread, cam_ctx);
  405. if (ret != 0) {
  406. LOG_E("pthread_create() failed, ret=%d", ret);
  407. goto TWO_ERR;
  408. }
  409. if(params->type == CAM_TYEP_IR)
  410. {
  411. cb_context[0].id=0;
  412. cb_context[0].cam_handle = cam_ctx->cam_handle;
  413. cb_context[0].dsp_id = 1;
  414. cb_context[0].dsp_path = 3;
  415. char test_set[16]="init";
  416. cb_context[0].buf_size = 1920*1080;
  417. cb_context[0].ref_buf=NULL;
  418. csi_camera_dsp_algo_param_t algo_param_1={
  419. .algo_name = "dsp1_dummy_algo_flo_1",
  420. .algo_cb.cb = dsp_algo_result_cb,
  421. .algo_cb.context = &cb_context[0],
  422. .algo_cb.arg_size = sizeof(cb_args_t),
  423. .sett_ptr = test_set,
  424. .sett_size =sizeof(test_set),
  425. .extra_buf_num =1,
  426. .extra_buf_sizes = &cb_context[0].buf_size,
  427. .extra_bufs = &cb_context[0].ref_buf,
  428. };
  429. if(csi_camera_set_dsp_algo_param(cam_ctx->cam_handle, cb_context[0].dsp_id,cb_context[0].dsp_path, &algo_param_1))
  430. {
  431. LOG_E("set DSP algo fail\n");
  432. goto TWO_ERR;
  433. }
  434. cb_context[1].id=1;
  435. cb_context[1].cam_handle = cam_ctx->cam_handle;
  436. cb_context[1].dsp_id = 1;
  437. cb_context[1].dsp_path = 4;
  438. cb_context[1].buf_size = 1920*1080;
  439. cb_context[1].ref_buf=NULL;
  440. csi_camera_dsp_algo_param_t algo_param_2={
  441. .algo_name = "dsp1_dummy_algo_flo",
  442. .algo_cb.cb = dsp_algo_result_cb,
  443. .algo_cb.context = &cb_context[1],
  444. .algo_cb.arg_size = sizeof(cb_args_t),
  445. .sett_ptr = test_set,
  446. .sett_size =sizeof(test_set),
  447. .extra_buf_num =1,
  448. .extra_buf_sizes = &cb_context[1].buf_size,
  449. .extra_bufs = &cb_context[1].ref_buf,
  450. };
  451. if(csi_camera_set_dsp_algo_param(cam_ctx->cam_handle, cb_context[1].dsp_id,cb_context[1].dsp_path, &algo_param_2))
  452. {
  453. LOG_E("set DSP algo fail\n");
  454. goto TWO_ERR;
  455. }
  456. csi_camera_floodlight_led_set_flash_bright(cam_ctx->cam_handle, 500); //500ma
  457. csi_camera_projection_led_set_flash_bright(cam_ctx->cam_handle, 500); //500ma
  458. csi_camera_projection_led_set_mode(cam_ctx->cam_handle, LED_IR_ENABLE);
  459. csi_camera_floodlight_led_set_mode(cam_ctx->cam_handle, LED_IR_ENABLE);
  460. csi_camera_led_enable(cam_ctx->cam_handle, LED_FLOODLIGHT_PROJECTION);
  461. }
  462. // for(loop_ch=0;loop_ch<cam_ctx->channel_num;loop_ch++)
  463. // {
  464. // csi_camera_channel_start(cam_ctx->cam_handle, cam_ctx->chn_cfg[loop_ch].chn_id);
  465. // }
  466. get_system_time(__func__, __LINE__);
  467. return cam_ctx;
  468. TWO_ERR:
  469. for(loop_ch=0;loop_ch<cam_ctx->channel_num;loop_ch++)
  470. {
  471. csi_camera_channel_close(cam_ctx->cam_handle, cam_ctx->chn_cfg[loop_ch].chn_id);
  472. }
  473. csi_camera_close(cam_ctx->cam_handle);
  474. ONE_ERR:
  475. free(cam_ctx);
  476. return NULL;
  477. }
  478. static void camera_close(camera_ctx_t *ctx)
  479. {
  480. int loop_ch;
  481. if(ctx->cam_handle==NULL)
  482. {
  483. return;
  484. }
  485. if (pthread_join(ctx->cam_thread, NULL)) {
  486. LOG_E("pthread_join() failed\n");
  487. }
  488. csi_camera_unsubscribe_event(ctx->event_handle, &ctx->event_subscribe);
  489. csi_camera_destory_event(ctx->event_handle);
  490. // int loop_ch;
  491. // for(loop_ch=0;loop_ch<ctx->channel_num;loop_ch++)
  492. // {
  493. // csi_camera_channel_stop(cam_ctx->cam_handle, cam_ctx->chn_cfg[loop_ch].chn_id);
  494. // }
  495. for(loop_ch=0;loop_ch<ctx->channel_num;loop_ch++)
  496. {
  497. csi_camera_channel_close(ctx->cam_handle, ctx->chn_cfg[loop_ch].chn_id);
  498. }
  499. csi_camera_close(ctx->cam_handle);
  500. free(ctx);
  501. }
  502. int main(int argc, char *argv[])
  503. {
  504. char dev_name[128];
  505. int camera_id = 0;
  506. // 打印HAL接口版本号
  507. csi_api_version_u version;
  508. csi_camera_get_version(&version);
  509. camera_param_t params[MAX_CAM_NUM];
  510. camera_ctx_t * ctx[MAX_CAM_NUM]={NULL,NULL,NULL};
  511. int cam_num=0;
  512. bool running = false;
  513. cam_num =parseParams(argc,argv,params);
  514. if(cam_num <=0 || cam_num>MAX_CAM_NUM)
  515. {
  516. LOG_E("not camera is active\n");
  517. exit(0);
  518. }
  519. for(int i =0;i<cam_num;i++)
  520. {
  521. switch(params[i].type)
  522. {
  523. case CAM_TYEP_RGB:
  524. ctx[i]=camera_open(&params[i]);
  525. break;
  526. case CAM_TYEP_IR:
  527. ctx[i]=camera_open(&params[i]);
  528. break;
  529. default:
  530. LOG_E("unspuoort camera :%d\n",i);
  531. continue;
  532. }
  533. if(ctx[i]==NULL)
  534. {
  535. LOG_E("camera_%d open %d fail\n",i,params[i].type);
  536. goto ONR_ERR;
  537. }
  538. ctx[i]->cam_id = i;
  539. ctx[i]->frame_num =0;
  540. ctx[i]->exit = 0;
  541. camera_start_all(ctx[i]);
  542. running = true;
  543. }
  544. int all_exit;
  545. do{
  546. sleep(1);
  547. all_exit = 1;
  548. for(int i =0;i<cam_num;i++)
  549. {
  550. if(ctx[i]&&
  551. ctx[i]->exit==0 &&
  552. params[i].frames_to_stop>0 &&
  553. ctx[i]->frame_num >=params[i].frames_to_stop)
  554. {
  555. camera_stop_all(ctx[i]);
  556. ctx[i]->exit=1;
  557. }
  558. if(ctx[i])
  559. all_exit &= ctx[i]->exit;
  560. }
  561. if(all_exit ==1)
  562. {
  563. LOG_O("all_exit\n");
  564. running = false;
  565. }
  566. }while(running);
  567. ONR_ERR:
  568. for(int i =0;i<cam_num;i++)
  569. {
  570. if(ctx[i])
  571. camera_close(ctx[i]);
  572. }
  573. exit(0);
  574. }
  575. static void dump_camera_meta(csi_frame_s *frame)
  576. {
  577. int i;
  578. //printf("%s\n", __func__);
  579. if (frame->meta.type != CSI_META_TYPE_CAMERA)
  580. return;
  581. csi_camera_meta_s *meta_data = (csi_camera_meta_s *)frame->meta.data;
  582. int meta_count = meta_data->count;
  583. csi_camrea_meta_unit_s meta_unit;
  584. csi_camera_frame_get_meta_unit(
  585. &meta_unit, meta_data, CSI_CAMERA_META_ID_FRAME_ID);
  586. LOG_I("meta_id=%d, meta_type=%d, meta_value=%d\n",
  587. meta_unit.id, meta_unit.type, meta_unit.int_value);
  588. }
  589. static void dump_camera_frame(csi_frame_s *frame, csi_pixel_fmt_e fmt, int chan_id)
  590. {
  591. char file[128];
  592. static int file_indx=0;
  593. int size;
  594. uint32_t indexd, j;
  595. void *buf[3];
  596. int buf_size;
  597. csi_camera_meta_s *meta_data = (csi_camera_meta_s *)frame->meta.data;
  598. csi_camrea_meta_unit_s meta_unit;
  599. csi_camera_frame_get_meta_unit(
  600. &meta_unit, meta_data, CSI_CAMERA_META_ID_CAMERA_NAME);
  601. sprintf(file,"demo_save_img%s_%d_%d",meta_unit.str_value,chan_id, file_indx++%10);
  602. int fd = open(file, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IROTH);
  603. if(fd == -1) {
  604. LOG_E("%s, %d, open file error!!!!!!!!!!!!!!!\n", __func__, __LINE__);
  605. return ;
  606. }
  607. if (frame->img.type == CSI_IMG_TYPE_DMA_BUF) {
  608. buf_size = frame->img.strides[0]*frame->img.height;
  609. switch(fmt) {
  610. case CSI_PIX_FMT_NV12:
  611. case CSI_PIX_FMT_YUV_SEMIPLANAR_420:
  612. if(frame->img.num_planes ==2)
  613. {
  614. buf_size+=frame->img.strides[1]*frame->img.height/2;
  615. }
  616. else{
  617. LOG_O("img fomrat is not match with frame planes:%d\n",frame->img.num_planes);
  618. return;
  619. }
  620. break;
  621. case CSI_PIX_FMT_YUV_SEMIPLANAR_422:
  622. if(frame->img.num_planes ==2)
  623. {
  624. buf_size+=frame->img.strides[1]*frame->img.height;
  625. }
  626. else{
  627. LOG_O("img fomrat is not match with frame planes:%d\n",frame->img.num_planes);
  628. return;
  629. }
  630. break;
  631. default:
  632. break;
  633. }
  634. buf[0]= mmap(0, buf_size, PROT_READ | PROT_WRITE,
  635. MAP_SHARED, frame->img.dmabuf[0].fds, frame->img.dmabuf[0].offset);
  636. // plane_size[1] = frame->img.strides[1]*frame->img.height;
  637. // printf("frame plane 1 stride:%d,offset:%d\n", frame->img.strides[1],(uint32_t)frame->img.dmabuf[1].offset);
  638. if(frame->img.num_planes ==2)
  639. {
  640. // buf[1] = mmap(0, plane_size[1]/2, PROT_READ | PROT_WRITE,
  641. // MAP_SHARED, frame->img.dmabuf[1].fds, frame->img.dmabuf[1].offset);
  642. buf[1] = buf[0]+frame->img.dmabuf[1].offset;
  643. }
  644. }
  645. else{
  646. buf[0] = frame->img.usr_addr[0];
  647. buf[1] = frame->img.usr_addr[1];
  648. }
  649. LOG_O("%s,save img from : (%lx,%lx)size:%d to %s, fmt:%d width:%d stride:%d height:%d\n",__FUNCTION__, (uint64_t)buf[0],(uint64_t)buf[1],size, file, fmt, frame->img.width, frame->img.strides[0], frame->img.height);
  650. if (frame->img.strides[0] == 0) {
  651. frame->img.strides[0] = frame->img.width;
  652. }
  653. switch(fmt) {
  654. case CSI_PIX_FMT_NV12:
  655. case CSI_PIX_FMT_YUV_SEMIPLANAR_420:
  656. for (j = 0; j < frame->img.height; j++) { //Y
  657. indexd = j*frame->img.strides[0];
  658. write(fd, buf[0] + indexd, frame->img.width);
  659. }
  660. for (j = 0; j < frame->img.height / 2; j++) { //UV
  661. indexd = j*frame->img.strides[0];
  662. write(fd, buf[1] + indexd, frame->img.width);
  663. }
  664. break;
  665. case CSI_PIX_FMT_RGB_INTEVLEAVED_888:
  666. case CSI_PIX_FMT_YUV_TEVLEAVED_444:
  667. size = frame->img.width*3;
  668. for (j = 0; j < frame->img.height; j++) {
  669. indexd = j*frame->img.strides[0];
  670. write(fd, buf[0] + indexd, size);
  671. }
  672. break;
  673. case CSI_PIX_FMT_BGR:
  674. case CSI_PIX_FMT_RGB_PLANAR_888:
  675. case CSI_PIX_FMT_YUV_PLANAR_444:
  676. size = frame->img.width * frame->img.height * 3;
  677. write(fd, buf[0], size);
  678. break;
  679. case CSI_PIX_FMT_RAW_8BIT:
  680. size = frame->img.width;
  681. for (j = 0; j < frame->img.height; j++) {
  682. indexd = j*frame->img.strides[0];
  683. write(fd, buf[0] + indexd, size);
  684. }
  685. break;
  686. case CSI_PIX_FMT_YUV_TEVLEAVED_422:
  687. case CSI_PIX_FMT_RAW_10BIT:
  688. case CSI_PIX_FMT_RAW_12BIT:
  689. case CSI_PIX_FMT_RAW_14BIT:
  690. case CSI_PIX_FMT_RAW_16BIT:
  691. size = frame->img.width*2;
  692. for (j = 0; j < frame->img.height; j++) {
  693. indexd = j*frame->img.strides[0];
  694. write(fd, buf[0] + indexd, size);
  695. }
  696. break;
  697. case CSI_PIX_FMT_YUV_SEMIPLANAR_422:
  698. if (frame->img.strides[0] == 0) {
  699. frame->img.strides[0] = frame->img.width;
  700. }
  701. for (j = 0; j < frame->img.height; j++) { //Y
  702. indexd = j*frame->img.strides[0];
  703. write(fd,buf[0] + indexd, frame->img.width);
  704. }
  705. for (j = 0; j < frame->img.height; j++) { //UV
  706. indexd = j*frame->img.strides[0];
  707. write(fd, buf[1]+ indexd, frame->img.width);
  708. }
  709. break;
  710. default:
  711. LOG_E("%s unsupported format to save\n", __func__);
  712. exit(-1);
  713. break;
  714. }
  715. close(fd);
  716. munmap(buf[0],buf_size);
  717. // munmap(buf[1],plane_size[1]);
  718. LOG_O("%s exit\n", __func__);
  719. }