camera.c (1870B)
1 #include <string.h> 2 #include <stdlib.h> 3 #include <stdio.h> 4 #include <fcntl.h> 5 #include <sys/ioctl.h> 6 #include <linux/videodev2.h> 7 #include <dirent.h> 8 #include <sys/types.h> 9 10 #include "debug.h" 11 #include "camera.h" 12 13 //#define KEE_VIDEO_DEVICE_TEMPLATE "/dev/video%d" 14 15 16 int kee_camera_scan(struct kee_camera_devices *devices) { 17 int r; 18 int fd; 19 int devnum; 20 struct kee_camera_devices *p; 21 struct v4l2_capability video_cap; 22 char s[1024]; 23 struct dirent *de; 24 DIR *d; 25 26 p = devices; 27 memset(p, 0, sizeof(struct kee_camera_devices)); 28 devnum = 0; 29 d = opendir("/dev"); 30 while (1) { 31 strcpy(p->path, "/dev/"); 32 de = readdir(d); 33 if (de == NULL) { 34 break; 35 } 36 if (strlen(de->d_name) < 6) { 37 continue; 38 } 39 if (memcmp(de->d_name, "video", 5)) { 40 continue; 41 } 42 //sprintf(dev.path, KEE_VIDEO_DEVICE_TEMPLATE, devnum); 43 //sprintf(p->path, "/dev/video%d", devnum); 44 strcpy(p->path + 5, de->d_name); 45 fd = open(p->path, O_RDONLY); 46 if (fd < 0) { 47 p->path[0] = 0; 48 break; 49 } 50 r = ioctl(fd, VIDIOC_QUERYCAP, &video_cap); 51 if (r == -1) { 52 debug_log(DEBUG_ERROR, "could not get video cap"); 53 kee_camera_free(devices); 54 return -1; 55 } 56 if ((video_cap.device_caps & V4L2_CAP_VIDEO_CAPTURE) == 0) { 57 sprintf(s, "%s is not a capture video device", p->path); 58 debug_log(DEBUG_DEBUG, s); 59 devnum++; 60 continue; 61 } 62 strcpy(p->label, (char*)video_cap.card); 63 p->next = calloc(1, sizeof(struct kee_camera_devices)); 64 if (p->next == NULL) { 65 debug_log(DEBUG_ERROR, "could not allocate video cap struct"); 66 kee_camera_free(devices); 67 return -1; 68 } 69 sprintf(s, "found camera: %s (%s)", p->label, p->path); 70 debug_log(DEBUG_INFO, s); 71 p = p->next; 72 devnum++; 73 } 74 closedir(d); 75 return ERR_OK; 76 } 77 78 void kee_camera_free(struct kee_camera_devices *devices) { 79 if (devices->next != NULL) { 80 kee_camera_free(devices->next); 81 } 82 free(devices->next); 83 }