camera_demo1.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 3
  13. #define LOG_PREFIX "camera_demo1"
  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/fake"
  23. #define TEST_DEVICE_NAME "/dev/video0"
  24. int main(int argc, char *argv[])
  25. {
  26. bool running = true;
  27. csi_camera_info_s camera_info;
  28. // 打印HAL接口版本号
  29. csi_api_version_u version;
  30. csi_camera_get_version(&version);
  31. printf("Camera HAL version: %d.%d\n", version.major, version.minor);
  32. // 获取设备中,所有的Camera
  33. struct csi_camera_infos camera_infos;
  34. csi_camera_query_list(&camera_infos);
  35. // 打印所有设备所支持的Camera
  36. for (int i = 0; i < camera_infos.count; i++) {
  37. camera_info = camera_infos.info[i];
  38. printf("Camera[%d]: camera_name='%s', device_name='%s', bus_info='%s', capabilities=0x%08x\n",
  39. i,
  40. camera_info.camera_name, camera_info.device_name, camera_info.bus_info,
  41. camera_info.capabilities);
  42. printf("Camera[%d] caps are:\n",
  43. i); /* The caps are: Video capture, metadata capture */
  44. for (int j = 1; j <= 0x08000000; j = j << 1) {
  45. switch (camera_info.capabilities & j) {
  46. case CSI_CAMERA_CAP_VIDEO_CAPTURE:
  47. printf("\t camera_infos.info[%d]:Video capture,\n", i);
  48. break;
  49. case CSI_CAMERA_CAP_META_CAPTURE:
  50. printf("\t camera_infos.info[%d] metadata capture,\n", i);
  51. break;
  52. default:
  53. if (camera_info.capabilities & j) {
  54. printf("\t camera_infos.info[%d] unknown capabilities(0x%08x)\n", i,
  55. camera_info.capabilities & j);
  56. }
  57. break;
  58. }
  59. }
  60. }
  61. /* Camera[0]: camera_name='RGB_Camera', device_name='/dev/video0', bus_info='CSI-MIPI', capabilities=0x00800001
  62. * Camera[1]: camera_name:'Mono_Camera', device_name:'/dev/video8', bus_info='USB', capabilities=0x00000001
  63. */
  64. bool found_camera = false;
  65. for (int i = 0; i < camera_infos.count; i++) {
  66. if (strcmp(camera_infos.info[i].device_name, TEST_DEVICE_NAME) == 0) {
  67. camera_info = camera_infos.info[i];
  68. printf("found device_name:'%s'\n", camera_info.device_name);
  69. found_camera = true;
  70. break;
  71. }
  72. }
  73. if (!found_camera) {
  74. LOG_E("Can't find camera_name:'%s'\n", TEST_DEVICE_NAME);
  75. exit(-1);
  76. }
  77. // 打开Camera设备获取句柄,作为后续操对象
  78. csi_cam_handle_t cam_handle;
  79. csi_camera_open(&cam_handle, camera_info.device_name);
  80. // 获取Camera支持的工作模式
  81. struct csi_camera_modes camera_modes;
  82. csi_camera_get_modes(cam_handle, &camera_modes);
  83. // 打印camera所支持的所有工作模式
  84. printf("Camera:'%s' modes are:\n", TEST_DEVICE_NAME);
  85. printf("{\n");
  86. for (int 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("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. printf("all properties are:\n");
  115. description.id = CSI_CAMERA_PID_HFLIP;
  116. while (!csi_camera_query_property(cam_handle, &description)) {
  117. switch (description.type) {
  118. case (CSI_CAMERA_PROPERTY_TYPE_INTEGER):
  119. printf("id=0x%08x type=%d default=%d value=%d\n",
  120. description.id, description.type,
  121. description.default_value.int_value, description.value.int_value);
  122. break;
  123. case (CSI_CAMERA_PROPERTY_TYPE_BOOLEAN):
  124. printf("id=0x%08x type=%d default=%d value=%d\n",
  125. description.id, description.type,
  126. description.default_value.bool_value, description.value.bool_value);
  127. break;
  128. case (CSI_CAMERA_PROPERTY_TYPE_ENUM):
  129. printf("id=0x%08x type=%d default=%d value=%d\n",
  130. description.id, description.type,
  131. description.default_value.enum_value, description.value.enum_value);
  132. break;
  133. case (CSI_CAMERA_PROPERTY_TYPE_STRING):
  134. printf("id=0x%08x type=%d default=%s value=%s\n",
  135. description.id, description.type,
  136. description.default_value.str_value, description.value.str_value);
  137. break;
  138. case (CSI_CAMERA_PROPERTY_TYPE_BITMASK):
  139. printf("id=0x%08x type=%d default=%x value=%x\n",
  140. description.id, description.type,
  141. description.default_value.bitmask_value, description.value.bitmask_value);
  142. break;
  143. default:
  144. LOG_E("error type!\n");
  145. break;
  146. }
  147. description.id |= CSI_CAMERA_FLAG_NEXT_CTRL;
  148. }
  149. // 同时配置多个参数
  150. csi_camera_properties_s properties;
  151. csi_camera_property_s property[2];
  152. property[0].id = CSI_CAMERA_PID_HFLIP;
  153. property[0].type = CSI_CAMERA_PROPERTY_TYPE_BOOLEAN;
  154. property[0].value.bool_value = true;
  155. property[1].id = CSI_CAMERA_PID_VFLIP;
  156. property[1].type = CSI_CAMERA_PROPERTY_TYPE_BOOLEAN;
  157. property[1].value.bool_value = false;
  158. properties.count = 2;
  159. properties.property = property;
  160. csi_camera_set_property(cam_handle, &properties);
  161. LOG_O("set_property ok!\n");
  162. csi_camera_get_property(cam_handle, &properties);
  163. // 查询输出channel
  164. csi_camera_channel_cfg_s chn_cfg;
  165. chn_cfg.chn_id = CSI_CAMERA_CHANNEL_0;
  166. csi_camera_channel_query(cam_handle, &chn_cfg);
  167. if (chn_cfg.status != CSI_CAMERA_CHANNEL_CLOSED) {
  168. printf("Can't open CSI_CAMERA_CHANNEL_0\n");
  169. exit(-1);
  170. }
  171. // 打开输出channel
  172. chn_cfg.chn_id = CSI_CAMERA_CHANNEL_0;
  173. chn_cfg.frm_cnt = 4;
  174. chn_cfg.img_fmt.width = 1280;
  175. chn_cfg.img_fmt.height = 720;
  176. chn_cfg.img_fmt.pix_fmt = CSI_PIX_FMT_BGR;
  177. chn_cfg.img_type = CSI_IMG_TYPE_DMA_BUF;
  178. chn_cfg.meta_fields = CSI_CAMERA_META_DEFAULT_FIELDS;
  179. chn_cfg.capture_type = CSI_CAMERA_CHANNEL_CAPTURE_VIDEO |
  180. CSI_CAMERA_CHANNEL_CAPTURE_META;
  181. csi_camera_channel_open(cam_handle, &chn_cfg);
  182. // 订阅Event
  183. csi_cam_event_handle_t event_handle;
  184. csi_camera_create_event(&event_handle, cam_handle);
  185. csi_camera_event_subscription_s subscribe;
  186. subscribe.type =
  187. CSI_CAMERA_EVENT_TYPE_CAMERA; // 订阅Camera的ERROR事件
  188. subscribe.id = CSI_CAMERA_EVENT_WARNING | CSI_CAMERA_EVENT_ERROR;
  189. csi_camera_subscribe_event(event_handle, &subscribe);
  190. subscribe.type =
  191. CSI_CAMERA_EVENT_TYPE_CHANNEL0; // 订阅Channel0的FRAME_READY事件
  192. subscribe.id = CSI_CAMERA_CHANNEL_EVENT_FRAME_READY |
  193. CSI_CAMERA_CHANNEL_EVENT_OVERFLOW;
  194. csi_camera_subscribe_event(event_handle, &subscribe);
  195. // 开始从channel中取出准备好的frame
  196. csi_camera_channel_start(cam_handle, CSI_CAMERA_CHANNEL_0);
  197. // 处理订阅的Event
  198. csi_frame_s frame;
  199. struct csi_camera_event event;
  200. while (running) {
  201. int timeout = -1; // unit: ms, -1 means wait forever, or until error occurs
  202. csi_camera_get_event(event_handle, &event, timeout);
  203. switch (event.type) {
  204. case CSI_CAMERA_EVENT_TYPE_CAMERA:
  205. switch (event.id) {
  206. case CSI_CAMERA_EVENT_ERROR:
  207. // do sth.
  208. LOG_D("get CAMERA EVENT CSI_CAMERA_EVENT_ERROR!\n");
  209. break;
  210. default:
  211. break;
  212. }
  213. case CSI_CAMERA_EVENT_TYPE_CHANNEL0:
  214. switch (event.id) {
  215. case CSI_CAMERA_CHANNEL_EVENT_FRAME_READY: {
  216. int read_frame_count = csi_camera_get_frame_count(cam_handle,
  217. CSI_CAMERA_CHANNEL_0);
  218. for (int i = 0; i < read_frame_count; i++) {
  219. csi_camera_get_frame(cam_handle, CSI_CAMERA_CHANNEL_0, &frame, timeout);
  220. // frame.image: {.width, .height, .pix_fmt, .dma-buf}
  221. //show_frame_image(frame.image); // 伪代码
  222. #ifdef PLATFORM_SIMULATOR
  223. camera_action_image_display(&frame);
  224. csi_camera_put_frame(&frame);
  225. #endif
  226. // frame.meta: {.count,
  227. // .meta_data[]={meta_id, type,
  228. // union{int_value, str_value, ...}}}
  229. dump_camera_meta(&frame);
  230. //csi_camera_put_frame(&frame);
  231. }
  232. break;
  233. }
  234. default:
  235. break;
  236. }
  237. default:
  238. break;
  239. }
  240. }
  241. // 取消订阅某一个event, 也可以直接调用csi_camera_destory_event,结束所有的订阅
  242. subscribe.type = CSI_CAMERA_EVENT_TYPE_CHANNEL0;
  243. subscribe.id = CSI_CAMERA_CHANNEL_EVENT_FRAME_READY;
  244. csi_camera_unsubscribe_event(event_handle, &subscribe);
  245. csi_camera_destory_event(event_handle);
  246. csi_camera_channel_stop(cam_handle, CSI_CAMERA_CHANNEL_0);
  247. csi_camera_channel_close(cam_handle, CSI_CAMERA_CHANNEL_0);
  248. csi_camera_close(cam_handle);
  249. }
  250. static void dump_camera_meta(csi_frame_s *frame)
  251. {
  252. int i;
  253. if (frame->meta.type != CSI_META_TYPE_CAMERA)
  254. return;
  255. csi_camera_meta_s *meta_data = (csi_camera_meta_s *)frame->meta.data;
  256. int meta_count = meta_data->count;
  257. csi_camrea_meta_unit_s meta_unit;
  258. for (i = 0; i < meta_count; i++) {
  259. csi_camera_frame_get_meta_unit(
  260. &meta_unit, meta_data, CSI_CAMERA_META_ID_FRAME_ID);
  261. printf("meta_id=%d, meta_type=%d, meta_value=%d",
  262. meta_unit.id, meta_unit.type, meta_unit.int_value);
  263. }
  264. }