123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- #include "../imediadeviceinfo.h"
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <sys/ioctl.h>
- #include <string.h>
- #include <unistd.h>
- #include <md5.h>
- //v4l includes
- #include <linux/videodev2.h>
- #ifndef MAX_PATH
- #define MAX_PATH 260
- #endif // !MAX_PATH
- int rvc_videocap_get_device_count()
- {
- int icount = 0;
- int fd = -1;
-
- for (int index = 0; index < 64; index++)
- {
- char device[MAX_PATH] = { 0 };
- snprintf(device, MAX_PATH, "/dev/video%d", index);
- if (-1 != (fd = open(device, O_RDONLY)) )
- {
- // query device capabilities
- struct v4l2_capability cap = {0};
- if (ioctl(fd, VIDIOC_QUERYCAP, &cap) < 0){
- close(fd);
- continue;
- }
- if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)){
- close(fd);
- continue;
- }
- if (cap.capabilities & V4L2_CAP_STREAMING) {
- }
- if (cap.capabilities & V4L2_CAP_READWRITE) {
- }
- /* 查询支持的格式 */
- struct v4l2_fmtdesc tFmtDesc = {0};
- int iPixelFormat = 0;
- tFmtDesc.index = 0;
- tFmtDesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- while ((ioctl(fd, VIDIOC_ENUM_FMT, &tFmtDesc)) == 0) {
- if (V4L2_PIX_FMT_YUYV == tFmtDesc.pixelformat || V4L2_PIX_FMT_MJPEG == tFmtDesc.pixelformat)
- {
- iPixelFormat = tFmtDesc.pixelformat;
- icount++;
- break;
- }
- tFmtDesc.index++;
- }
- close(fd);
- }
- }
- return icount;
- }
- int rvc_videocap_get_device_name(int device_id, char* buf, int len)
- {
- int iret = -1;
- char device[MAX_PATH] = { 0 };
- int fd = -1;
- snprintf(device, MAX_PATH, "/dev/video%d", device_id);
- if (-1 != (fd = open(device, O_RDONLY)))
- {
- // query device capabilities
- struct v4l2_capability cap = { 0 };
- if (ioctl(fd, VIDIOC_QUERYCAP, &cap) < 0){
- close(fd);
- return iret;
- }
- if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)){
- close(fd);
- return iret;
- }
- if (cap.capabilities & V4L2_CAP_STREAMING) {
- }
- if (cap.capabilities & V4L2_CAP_READWRITE) {
- }
- /* 查询支持的格式 */
- struct v4l2_fmtdesc tFmtDesc = { 0 };
- int iPixelFormat = 0;
- tFmtDesc.index = 0;
- tFmtDesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- while ((ioctl(fd, VIDIOC_ENUM_FMT, &tFmtDesc)) == 0) {
- if (V4L2_PIX_FMT_YUYV == tFmtDesc.pixelformat || V4L2_PIX_FMT_MJPEG == tFmtDesc.pixelformat){
- iPixelFormat = tFmtDesc.pixelformat;
- break;
- }
- tFmtDesc.index++;
- }
- if (0 != iPixelFormat)
- {
- snprintf(buf, len, "%s%s%s", cap.card, ";", cap.bus_info);
- iret = 0;
- }
- close(fd);
- }
- return iret;
- }
- int rvc_videocap_get_device_path(int device_id, char* buf, int len)
- {
- return 0;
- }
- int rvc_videocap_get_device_info(int device_id, char* namebuf, int namelen, char* pathbuf, int pathlen)
- {
- int iret = -1;
- char device[MAX_PATH] = { 0 };
- int fd = -1;
- snprintf(device, MAX_PATH, "/dev/video%d", device_id);
- if (-1 != (fd = open(device, O_RDONLY)))
- {
- // query device capabilities
- struct v4l2_capability cap = { 0 };
- if (ioctl(fd, VIDIOC_QUERYCAP, &cap) < 0)
- {
- close(fd);
- return iret;
- }
- if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
- {
- close(fd);
- return iret;
- }
- if (cap.capabilities & V4L2_CAP_STREAMING) {
- }
- if (cap.capabilities & V4L2_CAP_READWRITE) {
- }
- /* 查询支持的格式 */
- struct v4l2_fmtdesc tFmtDesc = { 0 };
- int iPixelFormat = 0;
- tFmtDesc.index = 0;
- tFmtDesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- while ((ioctl(fd, VIDIOC_ENUM_FMT, &tFmtDesc)) == 0) {
- if (V4L2_PIX_FMT_YUYV == tFmtDesc.pixelformat || V4L2_PIX_FMT_MJPEG == tFmtDesc.pixelformat)
- {
- iPixelFormat = tFmtDesc.pixelformat;
- break;
- }
- tFmtDesc.index++;
- }
- if (0 != iPixelFormat)
- {
- snprintf(namebuf, namelen, "%s", cap.card);
- snprintf(pathbuf, pathlen, "%s", cap.bus_info);
- iret = 0;
- }
- close(fd);
- }
- return iret;
- }
- static int bin2str(unsigned char* x, int xlen, char* str, int str_size)
- {
- static const char* hex2char = "0123456789ABCDEF";
- int i, k = 0;
- if (str_size <= xlen * 2)
- return -1;
- for (i = 0; i < xlen; ++i) {
- int h = x[i] >> 4;
- int l = x[i] & 0xf;
- str[k++] = hex2char[h];
- str[k++] = hex2char[l];
- }
- str[k] = 0;
- return k;
- }
- int rvc_videocap_get_device_fullpathname(int device_id, char* fullnamebuf, int fulllen)
- {
- int iret = -1;
- char device[MAX_PATH] = { 0 };
- int fd = -1;
- snprintf(device, MAX_PATH, "/dev/video%d", device_id);
- if (-1 != (fd = open(device, O_RDONLY))){
- // query device capabilities
- struct v4l2_capability cap = { 0 };
- if (ioctl(fd, VIDIOC_QUERYCAP, &cap) < 0){
- close(fd);
- return iret;
- }
- if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)){
- close(fd);
- return iret;
- }
- if (cap.capabilities & V4L2_CAP_STREAMING) {
- }
- if (cap.capabilities & V4L2_CAP_READWRITE) {
- }
- /* 查询支持的格式 */
- struct v4l2_fmtdesc tFmtDesc = { 0 };
- int iPixelFormat = 0;
- tFmtDesc.index = 0;
- tFmtDesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- while ((ioctl(fd, VIDIOC_ENUM_FMT, &tFmtDesc)) == 0) {
- if (V4L2_PIX_FMT_YUYV == tFmtDesc.pixelformat || V4L2_PIX_FMT_MJPEG == tFmtDesc.pixelformat){
- iPixelFormat = tFmtDesc.pixelformat;
- break;
- }
- tFmtDesc.index++;
- }
- if (0 != iPixelFormat){
- if (NULL != cap.bus_info && NULL != cap.card){
- char strmd5[MAX_PATH] = { 0 };
- snprintf(strmd5, MAX_PATH, "%s", cap.bus_info);
- unsigned char x[MD5_DIGESTSIZE] = {0};
- md5_ctx_t ctx;
- md5_init(&ctx);
- md5(x, strmd5, strlen(strmd5));
- bin2str(x, sizeof(x), strmd5, sizeof(strmd5));
- snprintf(fullnamebuf, fulllen, "%s:%s", cap.card, strmd5);
- iret = 0;
- }
- }
- close(fd);
- }
- return iret;
- }
- int rvc_videocap_get_video_device_id(const char* dev_name)
- {
- int iret = -1;
- if (NULL == dev_name){
- return iret;
- }
- int icount = rvc_videocap_get_device_count();
- int ifound = 0;
- for (int i = 0; i < 64 && ifound < icount; ++i) {
- char strfullname[2*MAX_PATH] = { 0 };
- if (0 == rvc_videocap_get_device_fullpathname(i, strfullname, 2 * MAX_PATH)){
- ifound++;
- if (0 == strcasecmp(strfullname, dev_name)){
- iret = i;
- break;
- }
- }
- }
- return iret;
- }
- int rvc_videocap_get_camera_infos(int device_id, rvc_camera_info_t* tinfo)
- {
- int iret = -1;
- if (NULL == tinfo) {
- return iret;
- }
- char device[MAX_PATH] = { 0 };
- int fd = -1;
- snprintf(device, MAX_PATH, "/dev/video%d", device_id);
- if (-1 != (fd = open(device, O_RDONLY))) {
- // query device capabilities
- struct v4l2_capability cap = { 0 };
- if (ioctl(fd, VIDIOC_QUERYCAP, &cap) < 0) {
- close(fd);
- return iret;
- }
- if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
- close(fd);
- return iret;
- }
- if (cap.capabilities & V4L2_CAP_STREAMING) {
- }
- if (cap.capabilities & V4L2_CAP_READWRITE) {
- }
- /* 查询支持的格式 */
- struct v4l2_fmtdesc tFmtDesc = { 0 };
- int iPixelFormat = 0;
- tFmtDesc.index = 0;
- tFmtDesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- while ((ioctl(fd, VIDIOC_ENUM_FMT, &tFmtDesc)) == 0) {
- if (V4L2_PIX_FMT_YUYV == tFmtDesc.pixelformat || V4L2_PIX_FMT_MJPEG == tFmtDesc.pixelformat) {
- iPixelFormat = tFmtDesc.pixelformat;
- break;
- }
- tFmtDesc.index++;
- }
- if (0 != iPixelFormat) {
- if (NULL != cap.bus_info && NULL != cap.card) {
- memcpy(tinfo->strdriver, cap.driver, 16);
- memcpy(tinfo->strcard, cap.card, 32);
- memcpy(tinfo->strbus_info, cap.bus_info, 32);
- iret = 0;
- }
- }
- close(fd);
- }
- return iret;
- }
|