camera_demo4.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (C) 2021 Alibaba Group Holding Limited
  3. * Author: LuChongzhi <chongzhi.lcz@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 <errno.h>
  13. #include <pthread.h>
  14. #define LOG_LEVEL 3
  15. #define LOG_PREFIX "camera_demo4"
  16. #include <syslog.h>
  17. #include <csi_frame.h>
  18. #include <csi_camera.h>
  19. #define TEST_DEVICE_NAME "/dev/video0"
  20. typedef struct cam_channel_control {
  21. pthread_t thread_id;
  22. bool run;
  23. csi_cam_handle_t cam_handle;
  24. csi_camera_channel_cfg_s chn_cfg;
  25. } cam_channel_control_t;
  26. static void dump_camera_meta(csi_frame_s *frame);
  27. static void *channel_thread(void *arg);
  28. int main(int argc, char *argv[])
  29. {
  30. bool running = true;
  31. csi_camera_info_s camera_info;
  32. // 获取设备中,所有的Camera
  33. struct csi_camera_infos camera_infos;
  34. csi_camera_query_list(&camera_infos);
  35. LOG_O("csi_camera_query_list() OK\n");
  36. // 打印所有设备所支持的Camera
  37. for (uint32_t i = 0; i < camera_infos.count; i++) {
  38. printf("Camera[%d]: camera_name='%s', device_name='%s', bus_info='%s', capabilities=0x%08x\n",
  39. i, camera_infos.info[i].camera_name, camera_infos.info[i].device_name,
  40. camera_infos.info[i].bus_info, camera_infos.info[i].capabilities);
  41. }
  42. /* Camera[0]: camera_name='RGB_Camera', device_name='/dev/video0', bus_info='CSI-MIPI', capabilities=0x00800001
  43. * Camera[1]: camera_name:'Mono_Camera', device_name:'/dev/video8', bus_info='USB', capabilities=0x00000001
  44. */
  45. bool found_camera = false;
  46. for (uint32_t i = 0; i < camera_infos.count; i++) {
  47. if (strcmp(camera_infos.info[i].device_name, TEST_DEVICE_NAME) == 0) {
  48. camera_info = camera_infos.info[i];
  49. printf("found device_name:'%s'\n", camera_info.device_name);
  50. found_camera = true;
  51. break;
  52. }
  53. }
  54. if (!found_camera) {
  55. LOG_E("Can't find camera_name:'%s'\n", TEST_DEVICE_NAME);
  56. exit(-1);
  57. }
  58. printf("The caps are:\n");
  59. for (uint32_t i = 1; i < 0x80000000; i = i << 1) {
  60. switch (camera_info.capabilities & i) {
  61. case CSI_CAMERA_CAP_VIDEO_CAPTURE:
  62. printf("\t Video capture\n");
  63. break;
  64. case CSI_CAMERA_CAP_META_CAPTURE:
  65. printf("\t metadata capture\n");
  66. break;
  67. default:
  68. if (camera_info.capabilities & i) {
  69. printf("\t unknown capabilities(0x%08x)\n", camera_info.capabilities & i);
  70. }
  71. break;
  72. }
  73. }
  74. /* The caps are: Video capture, metadata capture */
  75. // 打开Camera设备获取句柄,作为后续操对象
  76. csi_cam_handle_t cam_handle;
  77. csi_camera_open(&cam_handle, camera_info.device_name);
  78. LOG_O("csi_camera_open() OK\n");
  79. // 获取Camera支持的工作模式
  80. struct csi_camera_modes camera_modes;
  81. camera_modes.count = 0;
  82. csi_camera_get_modes(cam_handle, &camera_modes);
  83. LOG_O("csi_camera_get_modes() OK\n");
  84. // 打印camera所支持的所有工作模式
  85. printf(" Camera:'%s' modes are:{\n", TEST_DEVICE_NAME);
  86. for (uint32_t i = 0; i < camera_modes.count; i++) {
  87. printf("\t mode_id=%d: description:'%s'\n",
  88. camera_modes.modes[i].mode_id, camera_modes.modes[i].description);
  89. }
  90. printf("}\n");
  91. // 设置camera的工作模式及其配置
  92. csi_camera_mode_cfg_s camera_cfg;
  93. camera_cfg.mode_id = 1;
  94. camera_cfg.calibriation = NULL; // 采用系统默认配置
  95. camera_cfg.lib3a = NULL; // 采用系统默认配置
  96. csi_camera_set_mode(cam_handle, &camera_cfg);
  97. // 获取单个可控单元的属性
  98. csi_camera_property_description_s description;
  99. description.id = CSI_CAMERA_PID_HFLIP;
  100. csi_camera_query_property(cam_handle, &description);
  101. printf("properity id=0x%08x type=%d default=%d value=%d\n",
  102. description.id, description.type,
  103. description.default_value.bool_value, description.value.bool_value);
  104. /* id=0x0098090x, type=2 default=0 value=1 */
  105. /* Other example:
  106. * id=0x0098090y, type=3 min=0 max=255 step=1 default=127 value=116
  107. * id=0x0098090z, type=4 min=0 max=3 default=0 value=2
  108. * 0: IDLE
  109. * 1: BUSY
  110. * 2: REACHED
  111. * 3: FAILED
  112. */
  113. // 轮询获取所有可控制的单元
  114. description.id = CSI_CAMERA_PID_HFLIP;
  115. while (!csi_camera_query_property(cam_handle, &description)) {
  116. //printf(...); // 打印属性
  117. description.id |= CSI_CAMERA_FLAG_NEXT_CTRL;
  118. }
  119. LOG_O("csi_camera_query_property() OK\n");
  120. // 同时配置多个参数
  121. csi_camera_properties_s properties;
  122. csi_camera_property_s property[2];
  123. property[0].id = CSI_CAMERA_PID_HFLIP;
  124. property[0].type = CSI_CAMERA_PROPERTY_TYPE_BOOLEAN;
  125. property[0].value.bool_value = true;
  126. property[0].id = CSI_CAMERA_PID_VFLIP;
  127. property[0].type = CSI_CAMERA_PROPERTY_TYPE_BOOLEAN;
  128. property[0].value.bool_value = true;
  129. properties.count = 2;
  130. properties.property = property;
  131. csi_camera_get_property(cam_handle, &properties);
  132. LOG_O("csi_camera_get_property() OK\n");
  133. cam_channel_control_t channel_control[2];
  134. for (int i = 0; i < 2; i++) {
  135. cam_channel_control_t *chn_ctrl = &channel_control[i];
  136. pthread_t *thread_id = &(chn_ctrl->thread_id);
  137. csi_camera_channel_cfg_s *chn_cfg = &(chn_ctrl->chn_cfg);
  138. chn_ctrl->run = true;
  139. chn_ctrl->cam_handle = cam_handle;
  140. chn_cfg->frm_cnt = 4;
  141. if (i = 0) {
  142. chn_cfg->chn_id = CSI_CAMERA_CHANNEL_0;
  143. chn_cfg->img_fmt.width = 640;
  144. chn_cfg->img_fmt.height = 480;
  145. chn_cfg->img_fmt.pix_fmt = CSI_PIX_FMT_NV12;
  146. } else {
  147. chn_cfg->chn_id = CSI_CAMERA_CHANNEL_1;
  148. chn_cfg->img_fmt.width = 1280;
  149. chn_cfg->img_fmt.height = 720;
  150. chn_cfg->img_fmt.pix_fmt = CSI_PIX_FMT_I420;
  151. }
  152. chn_cfg->meta_fields = CSI_CAMERA_META_DEFAULT_FIELDS;
  153. chn_cfg->capture_type = CSI_CAMERA_CHANNEL_CAPTURE_VIDEO |
  154. CSI_CAMERA_CHANNEL_CAPTURE_META;
  155. pthread_create(thread_id, NULL, channel_thread, chn_ctrl);
  156. sleep(1); // channel[1] start after 1 second
  157. }
  158. sleep(3);
  159. channel_control[0].run = false;
  160. sleep(2);
  161. channel_control[1].run = false;
  162. for (int i = 0; i < 2; i++) {
  163. cam_channel_control_t *chn_ctrl = &channel_control[i];
  164. int chn_id = chn_ctrl->chn_cfg.chn_id;
  165. if (pthread_join(chn_ctrl->thread_id, NULL)) {
  166. LOG_E("[chn-%d] pthread_join() failed, %s\n", chn_id, strerror(errno));
  167. }
  168. }
  169. csi_camera_close(cam_handle);
  170. return 0;
  171. }
  172. static void *channel_thread(void *arg)
  173. {
  174. int ret;
  175. cam_channel_control_t *chn_ctrl = (cam_channel_control_t *)arg;
  176. csi_cam_handle_t cam_handle = chn_ctrl->cam_handle;
  177. int chn_id = chn_ctrl->chn_cfg.chn_id;
  178. ret = csi_camera_channel_open(cam_handle, &(chn_ctrl->chn_cfg));
  179. if (ret != 0) {
  180. LOG_E("[chn_%d] csi_camera_channel_open() failed, ret=%d\n", chn_id, ret);
  181. return NULL;
  182. }
  183. LOG_O("[chn_%d] csi_camera_channel_open() OK\n", chn_id);
  184. // 订阅Event
  185. csi_cam_event_handle_t event_handle;
  186. csi_camera_create_event(&event_handle, cam_handle);
  187. struct csi_camera_event_subscription subscribe_cam;
  188. struct csi_camera_event_subscription subscribe_chn;
  189. subscribe_cam.type = CSI_CAMERA_EVENT_TYPE_CAMERA; // 订阅Camera的ERROR事件
  190. subscribe_cam.id = CSI_CAMERA_EVENT_WARNING | CSI_CAMERA_EVENT_ERROR;
  191. csi_camera_subscribe_event(event_handle, &subscribe_cam);
  192. subscribe_chn.type = chn_id; // 订阅Channel0的FRAME_READY事件
  193. subscribe_chn.id = CSI_CAMERA_CHANNEL_EVENT_FRAME_READY |
  194. CSI_CAMERA_CHANNEL_EVENT_OVERFLOW;
  195. csi_camera_subscribe_event(event_handle, &subscribe_chn);
  196. LOG_O("Event subscript OK\n");
  197. // 处理订阅的Event
  198. LOG_O("[chn_%d] Starting get events...\n", chn_id);
  199. csi_frame_s frame;
  200. struct csi_camera_event event;
  201. while (chn_ctrl->run) {
  202. int timeout = 100; // unit: ms, -1 means wait forever, or until error occurs
  203. LOG_D("[chn_%d] before csi_camera_get_event\n", chn_id);
  204. csi_camera_get_event(event_handle, &event, timeout);
  205. LOG_D("[chn_%d] after csi_camera_get_event\n", chn_id);
  206. switch (event.type) {
  207. case CSI_CAMERA_EVENT_TYPE_CAMERA:
  208. switch (event.id) {
  209. case CSI_CAMERA_EVENT_ERROR:
  210. // do sth.
  211. break;
  212. default:
  213. break;
  214. }
  215. case CSI_CAMERA_EVENT_TYPE_CHANNEL0:
  216. case CSI_CAMERA_EVENT_TYPE_CHANNEL1:
  217. if (event.type != chn_id) {
  218. LOG_E("[chn_%d] Recv type(%d) is not expected\n",
  219. chn_id, event.type);
  220. break;
  221. }
  222. switch (event.id) {
  223. case CSI_CAMERA_CHANNEL_EVENT_FRAME_READY: {
  224. LOG_O("[chn_%d] Get ready frame\n", chn_id);
  225. int read_frame_count = csi_camera_get_frame_count(cam_handle, chn_id);
  226. for (int i = 0; i < read_frame_count; i++) {
  227. csi_camera_get_frame(cam_handle, chn_id, &frame, timeout);
  228. dump_camera_meta(&frame);
  229. csi_camera_put_frame(&frame);
  230. }
  231. break;
  232. }
  233. default:
  234. LOG_W("[chn_%d] Get unknown event type\n", event.type);
  235. break;
  236. }
  237. default:
  238. break;
  239. }
  240. }
  241. csi_camera_unsubscribe_event(event_handle, &subscribe_cam);
  242. csi_camera_unsubscribe_event(event_handle, &subscribe_chn);
  243. csi_camera_destory_event(event_handle);
  244. csi_camera_channel_stop(cam_handle, chn_id);
  245. pthread_exit(NULL);
  246. }
  247. static void dump_camera_meta(csi_frame_s *frame)
  248. {
  249. int i;
  250. if (frame->meta.type != CSI_META_TYPE_CAMERA)
  251. return;
  252. csi_camera_meta_s *meta_data = (csi_camera_meta_s *)frame->meta.data;
  253. int meta_count = meta_data->count;
  254. csi_camrea_meta_unit_s meta_unit;
  255. for (i = 0; i < meta_count; i++) {
  256. csi_camera_frame_get_meta_unit(
  257. &meta_unit, meta_data, CSI_CAMERA_META_ID_FRAME_ID);
  258. printf("meta_id=%d, meta_type=%d, meta_value=%d",
  259. meta_unit.id, meta_unit.type, meta_unit.int_value);
  260. }
  261. }