videodevice_info_linux.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #include "../imediadeviceinfo.h"
  2. #include <errno.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <sys/ioctl.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <md5.h>
  9. //v4l includes
  10. #include <linux/videodev2.h>
  11. #ifndef MAX_PATH
  12. #define MAX_PATH 260
  13. #endif // !MAX_PATH
  14. int rvc_videocap_get_device_count()
  15. {
  16. int icount = 0;
  17. int fd = -1;
  18. for (int index = 0; index < 64; index++)
  19. {
  20. char device[MAX_PATH] = { 0 };
  21. snprintf(device, MAX_PATH, "/dev/video%d", index);
  22. if (-1 != (fd = open(device, O_RDONLY)) )
  23. {
  24. // query device capabilities
  25. struct v4l2_capability cap = {0};
  26. if (ioctl(fd, VIDIOC_QUERYCAP, &cap) < 0){
  27. close(fd);
  28. continue;
  29. }
  30. if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)){
  31. continue;
  32. }
  33. if (cap.capabilities & V4L2_CAP_STREAMING) {
  34. }
  35. if (cap.capabilities & V4L2_CAP_READWRITE) {
  36. }
  37. /* 查询支持的格式 */
  38. struct v4l2_fmtdesc tFmtDesc = {0};
  39. int iPixelFormat = 0;
  40. tFmtDesc.index = 0;
  41. tFmtDesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  42. while ((ioctl(fd, VIDIOC_ENUM_FMT, &tFmtDesc)) == 0) {
  43. if (V4L2_PIX_FMT_YUYV == tFmtDesc.pixelformat || V4L2_PIX_FMT_MJPEG == tFmtDesc.pixelformat)
  44. {
  45. iPixelFormat = tFmtDesc.pixelformat;
  46. icount++;
  47. break;
  48. }
  49. tFmtDesc.index++;
  50. }
  51. }
  52. }
  53. return icount;
  54. }
  55. int rvc_videocap_get_device_name(int device_id, char* buf, int len)
  56. {
  57. int iret = -1;
  58. char device[MAX_PATH] = { 0 };
  59. int fd = -1;
  60. snprintf(device, MAX_PATH, "/dev/video%d", device_id);
  61. if (-1 != (fd = open(device, O_RDONLY)))
  62. {
  63. // query device capabilities
  64. struct v4l2_capability cap = { 0 };
  65. if (ioctl(fd, VIDIOC_QUERYCAP, &cap) < 0){
  66. close(fd);
  67. return iret;
  68. }
  69. if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)){
  70. return iret;
  71. }
  72. if (cap.capabilities & V4L2_CAP_STREAMING) {
  73. }
  74. if (cap.capabilities & V4L2_CAP_READWRITE) {
  75. }
  76. /* 查询支持的格式 */
  77. struct v4l2_fmtdesc tFmtDesc = { 0 };
  78. int iPixelFormat = 0;
  79. tFmtDesc.index = 0;
  80. tFmtDesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  81. while ((ioctl(fd, VIDIOC_ENUM_FMT, &tFmtDesc)) == 0) {
  82. if (V4L2_PIX_FMT_YUYV == tFmtDesc.pixelformat || V4L2_PIX_FMT_MJPEG == tFmtDesc.pixelformat){
  83. iPixelFormat = tFmtDesc.pixelformat;
  84. break;
  85. }
  86. tFmtDesc.index++;
  87. }
  88. if (0 != iPixelFormat)
  89. {
  90. snprintf(buf, len, "%s%s%s", cap.card, ";", cap.bus_info);
  91. iret = 0;
  92. }
  93. }
  94. return iret;
  95. }
  96. int rvc_videocap_get_device_path(int device_id, char* buf, int len)
  97. {
  98. return 0;
  99. }
  100. int rvc_videocap_get_device_info(int device_id, char* namebuf, int namelen, char* pathbuf, int pathlen)
  101. {
  102. int iret = -1;
  103. char device[MAX_PATH] = { 0 };
  104. int fd = -1;
  105. snprintf(device, MAX_PATH, "/dev/video%d", device_id);
  106. if (-1 != (fd = open(device, O_RDONLY)))
  107. {
  108. // query device capabilities
  109. struct v4l2_capability cap = { 0 };
  110. if (ioctl(fd, VIDIOC_QUERYCAP, &cap) < 0)
  111. {
  112. close(fd);
  113. return iret;
  114. }
  115. if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
  116. {
  117. return iret;
  118. }
  119. if (cap.capabilities & V4L2_CAP_STREAMING) {
  120. }
  121. if (cap.capabilities & V4L2_CAP_READWRITE) {
  122. }
  123. /* 查询支持的格式 */
  124. struct v4l2_fmtdesc tFmtDesc = { 0 };
  125. int iPixelFormat = 0;
  126. tFmtDesc.index = 0;
  127. tFmtDesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  128. while ((ioctl(fd, VIDIOC_ENUM_FMT, &tFmtDesc)) == 0) {
  129. if (V4L2_PIX_FMT_YUYV == tFmtDesc.pixelformat || V4L2_PIX_FMT_RGB565 == tFmtDesc.pixelformat)
  130. {
  131. iPixelFormat = tFmtDesc.pixelformat;
  132. break;
  133. }
  134. tFmtDesc.index++;
  135. }
  136. if (0 != iPixelFormat)
  137. {
  138. snprintf(namebuf, namelen, "%s", cap.card);
  139. snprintf(pathbuf, pathlen, "%s", cap.bus_info);
  140. iret = 0;
  141. }
  142. }
  143. return iret;
  144. }
  145. static int bin2str(unsigned char* x, int xlen, char* str, int str_size)
  146. {
  147. static const char* hex2char = "0123456789ABCDEF";
  148. int i, k = 0;
  149. if (str_size <= xlen * 2)
  150. return -1;
  151. for (i = 0; i < xlen; ++i) {
  152. int h = x[i] >> 4;
  153. int l = x[i] & 0xf;
  154. str[k++] = hex2char[h];
  155. str[k++] = hex2char[l];
  156. }
  157. str[k] = 0;
  158. return k;
  159. }
  160. int rvc_videocap_get_device_fullpathname(int device_id, char* fullnamebuf, int fulllen)
  161. {
  162. int iret = -1;
  163. char device[MAX_PATH] = { 0 };
  164. int fd = -1;
  165. snprintf(device, MAX_PATH, "/dev/video%d", device_id);
  166. if (-1 != (fd = open(device, O_RDONLY)))
  167. {
  168. // query device capabilities
  169. struct v4l2_capability cap = { 0 };
  170. if (ioctl(fd, VIDIOC_QUERYCAP, &cap) < 0)
  171. {
  172. close(fd);
  173. return iret;
  174. }
  175. if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
  176. {
  177. return iret;
  178. }
  179. if (cap.capabilities & V4L2_CAP_STREAMING) {
  180. }
  181. if (cap.capabilities & V4L2_CAP_READWRITE) {
  182. }
  183. /* 查询支持的格式 */
  184. struct v4l2_fmtdesc tFmtDesc = { 0 };
  185. int iPixelFormat = 0;
  186. tFmtDesc.index = 0;
  187. tFmtDesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  188. while ((ioctl(fd, VIDIOC_ENUM_FMT, &tFmtDesc)) == 0) {
  189. if (V4L2_PIX_FMT_YUYV == tFmtDesc.pixelformat || V4L2_PIX_FMT_RGB565 == tFmtDesc.pixelformat)
  190. {
  191. iPixelFormat = tFmtDesc.pixelformat;
  192. break;
  193. }
  194. tFmtDesc.index++;
  195. }
  196. if (0 != iPixelFormat)
  197. {
  198. if (NULL != cap.bus_info && NULL != cap.card)
  199. {
  200. char strmd5[MAX_PATH] = { 0 };
  201. snprintf(strmd5, MAX_PATH, "%s", cap.bus_info);
  202. unsigned char x[MD5_DIGESTSIZE];
  203. md5_ctx_t ctx;
  204. md5_init(&ctx);
  205. md5(x, strmd5, strlen(strmd5));
  206. bin2str(x, sizeof(x), strmd5, sizeof(strmd5));
  207. snprintf(fullnamebuf, fulllen, "%s:%s", cap.card, strmd5);
  208. iret = 0;
  209. }
  210. }
  211. }
  212. return iret;
  213. }
  214. int rvc_videocap_get_video_device_id(const char* dev_name)
  215. {
  216. int iret = -1;
  217. if (NULL == dev_name){
  218. return iret;
  219. }
  220. int icount = rvc_videocap_get_device_count();
  221. int ifound = 0;
  222. for (int i = 0; i < 64 && ifound < icount; ++i) {
  223. char strfullname[2*MAX_PATH] = { 0 };
  224. if (0 == rvc_videocap_get_device_fullpathname(i, strfullname, 2 * MAX_PATH)){
  225. ifound++;
  226. printf("camera name is: %s\n", strfullname);
  227. printf(" dev name is: %s\n", dev_name);
  228. if (0 == strcasecmp(strfullname, dev_name))
  229. {
  230. iret = i;
  231. }
  232. }
  233. }
  234. return iret;
  235. }