summaryrefslogtreecommitdiff
path: root/v4l2-apps/test/capture_example.c
diff options
context:
space:
mode:
Diffstat (limited to 'v4l2-apps/test/capture_example.c')
-rw-r--r--v4l2-apps/test/capture_example.c111
1 files changed, 52 insertions, 59 deletions
diff --git a/v4l2-apps/test/capture_example.c b/v4l2-apps/test/capture_example.c
index 7e967e891..a15ef3c09 100644
--- a/v4l2-apps/test/capture_example.c
+++ b/v4l2-apps/test/capture_example.c
@@ -36,22 +36,21 @@ typedef enum {
} io_method;
struct buffer {
- void * start;
- size_t length;
+ void *start;
+ size_t length;
};
-static char * dev_name = NULL;
-static io_method io = IO_METHOD_MMAP;
-static int fd = -1;
-struct buffer * buffers = NULL;
-static unsigned int n_buffers = 0;
-static int out_buf = 0;
+static char *dev_name;
+static io_method io = IO_METHOD_MMAP;
+static int fd = -1;
+struct buffer *buffers;
+static unsigned int n_buffers;
+static int out_buf;
+static int force_format;
static void errno_exit(const char *s)
{
- fprintf(stderr, "%s error %d, %s\n",
- s, errno, strerror(errno));
-
+ fprintf(stderr, "%s error %d, %s\n", s, errno, strerror(errno));
exit(EXIT_FAILURE);
}
@@ -99,7 +98,6 @@ static int read_frame(void)
}
process_image(buffers[0].start, buffers[0].length);
-
break;
case IO_METHOD_MMAP:
@@ -129,7 +127,6 @@ static int read_frame(void)
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
errno_exit("VIDIOC_QBUF");
-
break;
case IO_METHOD_USERPTR:
@@ -154,17 +151,16 @@ static int read_frame(void)
}
for (i = 0; i < n_buffers; ++i)
- if (buf.m.userptr == (unsigned long) buffers[i].start
+ if (buf.m.userptr == (unsigned long)buffers[i].start
&& buf.length == buffers[i].length)
break;
assert(i < n_buffers);
- process_image((void *) buf.m.userptr, buf.bytesused);
+ process_image((void *)buf.m.userptr, buf.bytesused);
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
errno_exit("VIDIOC_QBUF");
-
break;
}
@@ -195,7 +191,6 @@ static void mainloop(void)
if (-1 == r) {
if (EINTR == errno)
continue;
-
errno_exit("select");
}
@@ -206,7 +201,6 @@ static void mainloop(void)
if (read_frame())
break;
-
/* EAGAIN - continue select loop. */
}
}
@@ -224,10 +218,8 @@ static void stop_capturing(void)
case IO_METHOD_MMAP:
case IO_METHOD_USERPTR:
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
if (-1 == xioctl(fd, VIDIOC_STREAMOFF, &type))
errno_exit("VIDIOC_STREAMOFF");
-
break;
}
}
@@ -247,20 +239,16 @@ static void start_capturing(void)
struct v4l2_buffer buf;
CLEAR(buf);
-
- buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- buf.memory = V4L2_MEMORY_MMAP;
- buf.index = i;
+ buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ buf.memory = V4L2_MEMORY_MMAP;
+ buf.index = i;
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
errno_exit("VIDIOC_QBUF");
}
-
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
errno_exit("VIDIOC_STREAMON");
-
break;
case IO_METHOD_USERPTR:
@@ -268,22 +256,18 @@ static void start_capturing(void)
struct v4l2_buffer buf;
CLEAR(buf);
-
- buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- buf.memory = V4L2_MEMORY_USERPTR;
- buf.index = i;
- buf.m.userptr = (unsigned long) buffers[i].start;
- buf.length = buffers[i].length;
+ buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ buf.memory = V4L2_MEMORY_USERPTR;
+ buf.index = i;
+ buf.m.userptr = (unsigned long)buffers[i].start;
+ buf.length = buffers[i].length;
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
errno_exit("VIDIOC_QBUF");
}
-
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
errno_exit("VIDIOC_STREAMON");
-
break;
}
}
@@ -336,9 +320,9 @@ static void init_mmap(void)
CLEAR(req);
- req.count = 4;
- req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- req.memory = V4L2_MEMORY_MMAP;
+ req.count = 4;
+ req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ req.memory = V4L2_MEMORY_MMAP;
if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) {
if (EINVAL == errno) {
@@ -457,7 +441,6 @@ static void init_device(void)
dev_name);
exit(EXIT_FAILURE);
}
-
break;
case IO_METHOD_MMAP:
@@ -467,7 +450,6 @@ static void init_device(void)
dev_name);
exit(EXIT_FAILURE);
}
-
break;
}
@@ -500,16 +482,22 @@ static void init_device(void)
CLEAR(fmt);
- fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- fmt.fmt.pix.width = 640;
- fmt.fmt.pix.height = 480;
- fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
- fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
+ fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ if (force_format) {
+ fmt.fmt.pix.width = 640;
+ fmt.fmt.pix.height = 480;
+ fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
+ fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
- if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt))
- errno_exit("VIDIOC_S_FMT");
+ if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt))
+ errno_exit("VIDIOC_S_FMT");
- /* Note VIDIOC_S_FMT may change width and height. */
+ /* Note VIDIOC_S_FMT may change width and height. */
+ } else {
+ /* Preserve original settings as set by v4l2-ctl for example */
+ if (-1 == xioctl(fd, VIDIOC_G_FMT, &fmt))
+ errno_exit("VIDIOC_G_FMT");
+ }
/* Buggy driver paranoia. */
min = fmt.fmt.pix.width * 2;
@@ -577,20 +565,22 @@ static void usage(FILE *fp, int argc, char **argv)
"-r | --read Use read() calls\n"
"-u | --userp Use application allocated buffers\n"
"-o | --output Outputs stream to stdout\n"
+ "-f | --format Force format to 640x480 YUYV\n"
"",
argv[0]);
}
-static const char short_options [] = "d:hmruo";
+static const char short_options[] = "d:hmruof";
static const struct option
-long_options [] = {
- { "device", required_argument, NULL, 'd' },
- { "help", no_argument, NULL, 'h' },
- { "mmap", no_argument, NULL, 'm' },
- { "read", no_argument, NULL, 'r' },
- { "userp", no_argument, NULL, 'u' },
- { "output", no_argument, NULL, 'o' },
+long_options[] = {
+ { "device", required_argument, NULL, 'd' },
+ { "help", no_argument, NULL, 'h' },
+ { "mmap", no_argument, NULL, 'm' },
+ { "read", no_argument, NULL, 'r' },
+ { "userp", no_argument, NULL, 'u' },
+ { "output", no_argument, NULL, 'o' },
+ { "format", no_argument, NULL, 'f' },
{ 0, 0, 0, 0 }
};
@@ -603,8 +593,7 @@ int main(int argc, char **argv)
int c;
c = getopt_long(argc, argv,
- short_options, long_options,
- &idx);
+ short_options, long_options, &idx);
if (-1 == c)
break;
@@ -637,6 +626,10 @@ int main(int argc, char **argv)
out_buf++;
break;
+ case 'f':
+ force_format++;
+ break;
+
default:
usage(stderr, argc, argv);
exit(EXIT_FAILURE);