camera_demo2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. #define LOG_LEVEL 2
  13. #define LOG_PREFIX "camera_demo2"
  14. #include <syslog.h>
  15. #include <csi_frame.h>
  16. #include <csi_camera.h>
  17. #ifdef PLATFORM_SIMULATOR
  18. #include "apputilities.h"
  19. #include "platform_action.h"
  20. #endif
  21. static void dump_camera_meta(csi_frame_s *frame);
  22. #define TEST_DEVICE_NAME "/dev/video0"
  23. int main(int argc, char *argv[])
  24. {
  25. uint32_t i;
  26. bool running = true;
  27. csi_camera_info_s camera_info;
  28. // 获取设备中,所有的Camera
  29. struct csi_camera_infos camera_infos;
  30. csi_camera_query_list(&camera_infos);
  31. LOG_O("csi_camera_query_list() OK\n");
  32. // 打印所有设备所支持的Camera
  33. for (i = 0; i < camera_infos.count; i++) {
  34. printf("Camera[%d]: camera_name='%s', device_name='%s', bus_info='%s', capabilities=0x%08x\n",
  35. i, camera_infos.info[i].camera_name, camera_infos.info[i].device_name,
  36. camera_infos.info[i].bus_info, camera_infos.info[i].capabilities);
  37. }
  38. /* Camera[0]: camera_name='RGB_Camera', device_name='/dev/video0', bus_info='CSI-MIPI', capabilities=0x00800001
  39. * Camera[1]: camera_name:'Mono_Camera', device_name:'/dev/video8', bus_info='USB', capabilities=0x00000001
  40. */
  41. bool found_camera = false;
  42. for (i = 0; i < camera_infos.count; i++) {
  43. if (strcmp(camera_infos.info[i].device_name, TEST_DEVICE_NAME) == 0) {
  44. camera_info = camera_infos.info[i];
  45. printf("found device_name:'%s'\n", camera_info.device_name);
  46. found_camera = true;
  47. break;
  48. }
  49. }
  50. if (!found_camera) {
  51. LOG_E("Can't find camera_name:'%s'\n", TEST_DEVICE_NAME);
  52. exit(-1);
  53. }
  54. printf("The caps are:\n");
  55. for (i = 1; i < 0x80000000; i = i << 1) {
  56. switch (camera_info.capabilities & i) {
  57. case CSI_CAMERA_CAP_VIDEO_CAPTURE:
  58. printf("\t Video capture\n");
  59. break;
  60. case CSI_CAMERA_CAP_META_CAPTURE:
  61. printf("\t metadata capture\n");
  62. break;
  63. default:
  64. if (camera_info.capabilities & i) {
  65. printf("\t unknown capabilities(0x%08x)\n", camera_info.capabilities & i);
  66. }
  67. break;
  68. }
  69. }
  70. /* The caps are: Video capture, metadata capture */
  71. // 打开Camera设备获取句柄,作为后续操对象
  72. csi_cam_handle_t cam_handle;
  73. csi_camera_open(&cam_handle, camera_info.device_name);
  74. LOG_O("csi_camera_open() OK\n");
  75. // 获取Camera支持的工作模式
  76. struct csi_camera_modes camera_modes;
  77. camera_modes.count = 0;
  78. csi_camera_get_modes(cam_handle, &camera_modes);
  79. LOG_O("csi_camera_get_modes() OK\n");
  80. // 打印camera所支持的所有工作模式
  81. printf(" Camera:'%s' modes are:{\n", TEST_DEVICE_NAME);
  82. for (i = 0; i < camera_modes.count; i++) {
  83. printf("\t mode_id=%d: description:'%s'\n",
  84. camera_modes.modes[i].mode_id, camera_modes.modes[i].description);
  85. }
  86. printf("}\n");
  87. // 设置camera的工作模式及其配置
  88. csi_camera_mode_cfg_s camera_cfg;
  89. camera_cfg.mode_id = 1;
  90. camera_cfg.calibriation = NULL; // 采用系统默认配置
  91. camera_cfg.lib3a = NULL; // 采用系统默认配置
  92. csi_camera_set_mode(cam_handle, &camera_cfg);
  93. // 获取单个可控单元的属性
  94. csi_camera_property_description_s description;
  95. description.id = CSI_CAMERA_PID_HFLIP;
  96. csi_camera_query_property(cam_handle, &description);
  97. printf("properity id=0x%08x type=%d default=%d value=%d\n",
  98. description.id, description.type,
  99. description.default_value.bool_value, description.value.bool_value);
  100. /* id=0x0098090x, type=2 default=0 value=1 */
  101. /* Other example:
  102. * id=0x0098090y, type=3 min=0 max=255 step=1 default=127 value=116
  103. * id=0x0098090z, type=4 min=0 max=3 default=0 value=2
  104. * 0: IDLE
  105. * 1: BUSY
  106. * 2: REACHED
  107. * 3: FAILED
  108. */
  109. // 轮询获取所有可控制的单元
  110. description.id = CSI_CAMERA_FLAG_NEXT_CTRL;
  111. while (!csi_camera_query_property(cam_handle, &description)) {
  112. //printf(...); // 打印属性
  113. switch (description.type) {
  114. case (CSI_CAMERA_PROPERTY_TYPE_INTEGER):
  115. printf("id=0x%08x type=%d default=%d value=%d\n",
  116. description.id, description.type,
  117. description.default_value.int_value, description.value.int_value);
  118. break;
  119. case (CSI_CAMERA_PROPERTY_TYPE_BOOLEAN):
  120. printf("id=0x%08x type=%d default=%d value=%d\n",
  121. description.id, description.type,
  122. description.default_value.bool_value, description.value.bool_value);
  123. break;
  124. case (CSI_CAMERA_PROPERTY_TYPE_ENUM):
  125. printf("id=0x%08x type=%d default=%d value=%d\n",
  126. description.id, description.type,
  127. description.default_value.enum_value, description.value.enum_value);
  128. break;
  129. case (CSI_CAMERA_PROPERTY_TYPE_STRING):
  130. printf("id=0x%08x type=%d default=%s value=%s\n",
  131. description.id, description.type,
  132. description.default_value.str_value, description.value.str_value);
  133. break;
  134. case (CSI_CAMERA_PROPERTY_TYPE_BITMASK):
  135. printf("id=0x%08x type=%d default=%x value=%x\n",
  136. description.id, description.type,
  137. description.default_value.bitmask_value, description.value.bitmask_value);
  138. break;
  139. default:
  140. LOG_E("error type!\n");
  141. break;
  142. }
  143. description.id |= CSI_CAMERA_FLAG_NEXT_CTRL;
  144. }
  145. LOG_O("csi_camera_query_property() OK\n");
  146. // 同时配置多个参数
  147. csi_camera_properties_s properties;
  148. csi_camera_property_s property[2];
  149. property[0].id = CSI_CAMERA_PID_EXPOSURE_MODE;
  150. property[0].type = CSI_CAMERA_PROPERTY_TYPE_ENUM;
  151. property[0].value.enum_value = CSI_CAMERA_EXPOSURE_MODE_AUTO;
  152. property[1].id = CSI_CAMERA_PID_EXPOSURE_MODE;
  153. property[1].type = CSI_CAMERA_PROPERTY_TYPE_ENUM;
  154. property[1].value.enum_value = CSI_CAMERA_EXPOSURE_MANUAL;
  155. properties.count = 2;
  156. properties.property = property;
  157. csi_camera_get_property(cam_handle, &properties);
  158. LOG_O("csi_camera_get_property() OK\n");
  159. // 查询输出channel
  160. csi_camera_channel_cfg_s chn_cfg;
  161. chn_cfg.chn_id = CSI_CAMERA_CHANNEL_0;
  162. csi_camera_channel_query(cam_handle, &chn_cfg);
  163. if (chn_cfg.status != CSI_CAMERA_CHANNEL_CLOSED) {
  164. printf("Can't open CSI_CAMERA_CHANNEL_0\n");
  165. exit(-1);
  166. }
  167. LOG_O("csi_camera_channel_query() OK\n");
  168. // 打开输出channel_0
  169. chn_cfg.chn_id = CSI_CAMERA_CHANNEL_0;
  170. chn_cfg.frm_cnt = 4;
  171. chn_cfg.img_fmt.width = 640;
  172. chn_cfg.img_fmt.height = 480;
  173. chn_cfg.img_fmt.pix_fmt = CSI_PIX_FMT_I420;
  174. chn_cfg.img_type = CSI_IMG_TYPE_DMA_BUF;
  175. chn_cfg.meta_fields = CSI_CAMERA_META_DEFAULT_FIELDS;
  176. chn_cfg.capture_type = CSI_CAMERA_CHANNEL_CAPTURE_VIDEO |
  177. CSI_CAMERA_CHANNEL_CAPTURE_META;
  178. csi_camera_channel_open(cam_handle, &chn_cfg);
  179. LOG_O("CSI_CAMERA_CHANNEL_0: csi_camera_channel_open() OK\n");
  180. // 打开输出channel_1
  181. chn_cfg.chn_id = CSI_CAMERA_CHANNEL_1;
  182. chn_cfg.frm_cnt = 4;
  183. chn_cfg.img_fmt.width = 1280;
  184. chn_cfg.img_fmt.height = 720;
  185. chn_cfg.img_fmt.pix_fmt = CSI_PIX_FMT_BGR;
  186. chn_cfg.img_type = CSI_IMG_TYPE_DMA_BUF;
  187. chn_cfg.meta_fields = CSI_CAMERA_META_DEFAULT_FIELDS;
  188. chn_cfg.capture_type = CSI_CAMERA_CHANNEL_CAPTURE_VIDEO |
  189. CSI_CAMERA_CHANNEL_CAPTURE_META;
  190. csi_camera_channel_open(cam_handle, &chn_cfg);
  191. LOG_O("CSI_CAMERA_CHANNEL_1: csi_camera_channel_open() OK\n");
  192. // 订阅Event
  193. csi_cam_event_handle_t event_handle;
  194. csi_camera_create_event(&event_handle, cam_handle);
  195. struct csi_camera_event_subscription subscribe;
  196. subscribe.type =
  197. CSI_CAMERA_EVENT_TYPE_CAMERA; // 订阅Camera的ERROR事件
  198. subscribe.id = CSI_CAMERA_EVENT_WARNING | CSI_CAMERA_EVENT_ERROR;
  199. csi_camera_subscribe_event(event_handle, &subscribe);
  200. subscribe.type =
  201. CSI_CAMERA_EVENT_TYPE_CHANNEL0; // 订阅Channel0的FRAME_READY事件
  202. subscribe.id = CSI_CAMERA_CHANNEL_EVENT_FRAME_READY |
  203. CSI_CAMERA_CHANNEL_EVENT_OVERFLOW;
  204. csi_camera_subscribe_event(event_handle, &subscribe);
  205. subscribe.type =
  206. CSI_CAMERA_EVENT_TYPE_CHANNEL1; // 订阅Channel0的FRAME_READY事件
  207. subscribe.id = CSI_CAMERA_CHANNEL_EVENT_FRAME_READY |
  208. CSI_CAMERA_CHANNEL_EVENT_OVERFLOW;
  209. csi_camera_subscribe_event(event_handle, &subscribe);
  210. LOG_O("Event subscript OK\n");
  211. // 开始从channel中取出准备好的frame
  212. csi_camera_channel_start(cam_handle, CSI_CAMERA_CHANNEL_0);
  213. csi_camera_channel_start(cam_handle, CSI_CAMERA_CHANNEL_1);
  214. LOG_O("Channel start OK\n");
  215. // 处理订阅的Event
  216. csi_frame_s frame;
  217. struct csi_camera_event event;
  218. LOG_O("Starting get frame...\n");
  219. while (running) {
  220. int timeout = -1; // unit: ms, -1 means wait forever, or until error occurs
  221. csi_camera_get_event(event_handle, &event, timeout);
  222. switch (event.type) {
  223. case CSI_CAMERA_EVENT_TYPE_CAMERA:
  224. switch (event.id) {
  225. case CSI_CAMERA_EVENT_ERROR:
  226. // do sth.
  227. break;
  228. default:
  229. break;
  230. }
  231. case CSI_CAMERA_EVENT_TYPE_CHANNEL0:
  232. switch (event.id) {
  233. case CSI_CAMERA_CHANNEL_EVENT_FRAME_READY: {
  234. int read_frame_count = csi_camera_get_frame_count(cam_handle,
  235. CSI_CAMERA_CHANNEL_0);
  236. csi_camera_get_frame(cam_handle,CSI_CAMERA_CHANNEL_0, &frame, timeout);
  237. #ifdef PLATFORM_SIMULATOR
  238. camera_action_image_display(&frame);
  239. csi_camera_put_frame(&frame);
  240. #endif
  241. dump_camera_meta(&frame);
  242. csi_frame_release(&frame);
  243. break;
  244. }
  245. default:
  246. break;
  247. }
  248. break;
  249. case CSI_CAMERA_EVENT_TYPE_CHANNEL1:
  250. switch (event.id) {
  251. case CSI_CAMERA_CHANNEL_EVENT_FRAME_READY: {
  252. int read_frame_count = csi_camera_get_frame_count(cam_handle,
  253. CSI_CAMERA_CHANNEL_1);
  254. for (int i = 0; i < read_frame_count; i++) {
  255. csi_camera_get_frame(cam_handle,CSI_CAMERA_CHANNEL_1, &frame, timeout);
  256. // frame.image: {.width, .height, .pix_fmt, .dma-buf}
  257. //show_frame_image(frame.image); // 伪代码
  258. // frame.meta: {.count,
  259. // .meta_data[]={meta_id, type,
  260. // union{int_value, str_value, ...}}}
  261. #ifdef PLATFORM_SIMULATOR
  262. camera_action_image_display(&frame);
  263. csi_camera_put_frame(&frame);
  264. #endif
  265. dump_camera_meta(&frame);
  266. csi_frame_release(&frame);
  267. }
  268. break;
  269. }
  270. default:
  271. break;
  272. }
  273. default:
  274. break;
  275. }
  276. }
  277. // 取消订阅某一个event, 也可以直接调用csi_camera_destory_event,结束所有的订阅
  278. subscribe.type = CSI_CAMERA_EVENT_TYPE_CHANNEL0;
  279. subscribe.id = CSI_CAMERA_CHANNEL_EVENT_FRAME_READY |
  280. CSI_CAMERA_CHANNEL_EVENT_OVERFLOW;;
  281. csi_camera_unsubscribe_event(event_handle, &subscribe);
  282. subscribe.type = CSI_CAMERA_EVENT_TYPE_CHANNEL1;
  283. subscribe.id = CSI_CAMERA_CHANNEL_EVENT_FRAME_READY |
  284. CSI_CAMERA_CHANNEL_EVENT_OVERFLOW;;
  285. csi_camera_unsubscribe_event(event_handle, &subscribe);
  286. subscribe.type = CSI_CAMERA_EVENT_TYPE_CAMERA;
  287. subscribe.id = CSI_CAMERA_EVENT_WARNING | CSI_CAMERA_EVENT_ERROR;
  288. csi_camera_unsubscribe_event(event_handle, &subscribe);
  289. csi_camera_destory_event(event_handle);
  290. csi_camera_channel_stop(cam_handle, CSI_CAMERA_CHANNEL_0);
  291. csi_camera_channel_close(cam_handle, CSI_CAMERA_CHANNEL_0);
  292. csi_camera_close(cam_handle);
  293. }
  294. static void dump_camera_meta(csi_frame_s *frame)
  295. {
  296. int i;
  297. if (frame->meta.type != CSI_META_TYPE_CAMERA)
  298. return;
  299. csi_camera_meta_s *meta_data = (csi_camera_meta_s *)frame->meta.data;
  300. int meta_count = meta_data->count;
  301. csi_camrea_meta_unit_s meta_unit;
  302. for (i = 0; i < meta_count; i++) {
  303. csi_camera_frame_get_meta_unit(
  304. &meta_unit, meta_data, CSI_CAMERA_META_ID_FRAME_ID);
  305. printf("meta_id=%d, meta_type=%d, meta_value=%d",
  306. meta_unit.id, meta_unit.type, meta_unit.int_value);
  307. }
  308. }