1 /* fswebcam - FireStorm.cx's webcam generator */
2 /*============================================================*/
3 /* Copyright (C)2005-2010 Philip Heron <phil@sanslogic.co.uk> */
5 /* This program is distributed under the terms of the GNU */
6 /* General Public License, version 2. You may use, modify, */
7 /* and redistribute it under the terms of this license. A */
8 /* copy should be included with this source. */
22 #include <sys/ioctl.h>
25 int v4l2_free_mmap(src_t *src)
27 src_v4l2_t *s = (src_v4l2_t *) src->state;
30 for(i = 0; i < s->req.count; i++)
31 munmap(s->buffer[i].start, s->buffer[i].length);
36 static int close_v4l2(src_t *src)
38 src_v4l2_t *s = (src_v4l2_t *) src->state;
42 if(!s->map) free(s->buffer[0].start);
43 else v4l2_free_mmap(src);
46 if(s->fd >= 0) close(s->fd);
53 const char *query_v4ldevice(src_t *src, char **pixelformatdescription)
57 /*ERROR("No device name specified.");*/
58 fprintf(stderr, "No device name specified.");
63 /* Allocate memory for the state structure. */
64 s = calloc(sizeof(src_v4l2_t), 1);
67 fprintf(stderr, "Out of memory.");
71 src->state = (void *) s;
73 //snprintf( value, sizeof(value), '\0' );
74 //strcpy(*pixelformatdescription, (char*) value);
75 /* Open the device. */
76 s->fd = open(src->source, O_RDWR | O_NONBLOCK);
79 fprintf(stderr, "Cannot open device.");
84 int captureEnabled = 1;
85 if(ioctl(s->fd, VIDIOC_QUERYCAP, &s->cap) < 0) {
86 fprintf(stderr, "Cannot get capabilities.");
90 res = strdup((char*) s->cap.card);
91 if(!s->cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) {
92 // Device cannot capture
98 struct v4l2_format format;
99 memset(&format,0,sizeof(format));
100 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
102 struct v4l2_fmtdesc fmt;
103 memset(&fmt,0,sizeof(fmt));
105 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
107 struct v4l2_frmsizeenum sizes;
108 memset(&sizes,0,sizeof(sizes));
110 struct v4l2_frmivalenum rates;
111 memset(&rates,0,sizeof(rates));
113 while (ioctl(s->fd, VIDIOC_ENUM_FMT, &fmt) != -1)
115 if (strlen(*pixelformatdescription) > 2000) break;
116 if (snprintf( value, sizeof(value), ">%c%c%c%c", fmt.pixelformat >> 0, fmt.pixelformat >> 8, fmt.pixelformat >> 16, fmt.pixelformat >> 24 ) > 0)
117 strcat(*pixelformatdescription, (char *) value);
118 fprintf(stderr, "detected format: %s: %c%c%c%c\n", fmt.description, fmt.pixelformat >> 0, fmt.pixelformat >> 8,
119 fmt.pixelformat >> 16, fmt.pixelformat >> 24);
121 sizes.pixel_format = fmt.pixelformat;
123 // Query supported frame size
124 while (ioctl(s->fd, VIDIOC_ENUM_FRAMESIZES, &sizes) != -1) {
125 struct v4l2_frmsize_discrete image_size = sizes.un.discrete;
126 // Query supported frame rates
128 rates.pixel_format = fmt.pixelformat;
129 rates.width = image_size.width;
130 rates.height = image_size.height;
131 if (strlen(*pixelformatdescription) > 2000) break;
132 if (snprintf( value, sizeof(value), ":%dx%d=", image_size.width, image_size.height ) > 0)
133 strcat(*pixelformatdescription, (char *) value);
134 fprintf(stderr, "Size: %dx%d: ", image_size.width, image_size.height);
135 while (ioctl(s->fd, VIDIOC_ENUM_FRAMEINTERVALS, &rates) != -1) {
136 if (strlen(*pixelformatdescription) > 2000) break;
137 if (snprintf( value, sizeof(value), "%d/%d,", rates.un.discrete.denominator, rates.un.discrete.numerator ) > 0)
138 strcat(*pixelformatdescription, (char *) value);
139 fprintf(stderr, "%d/%d, ", rates.un.discrete.numerator, rates.un.discrete.denominator);
142 fprintf(stderr, "\n");
147 /*[0x%08X] '%c%c%c%c' (%s)", v4l2_pal,
149 fmt.pixelformat >> 0, fmt.pixelformat >> 8,
150 fmt.pixelformat >> 16, fmt.pixelformat >> 24*/
154 *pixelformatdescription = '\0';