Markus Heiser | 5377d91 | 2016-06-30 15:18:56 +0200 | [diff] [blame] | 1 | .. -*- coding: utf-8; mode: rst -*- |
| 2 | |
| 3 | file: media/v4l/v4l2grab.c |
| 4 | ========================== |
| 5 | |
| 6 | .. code-block:: c |
| 7 | |
| 8 | /* V4L2 video picture grabber |
| 9 | Copyright (C) 2009 Mauro Carvalho Chehab <mchehab@infradead.org> |
| 10 | |
| 11 | This program is free software; you can redistribute it and/or modify |
| 12 | it under the terms of the GNU General Public License as published by |
| 13 | the Free Software Foundation version 2 of the License. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | */ |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <errno.h> |
| 26 | #include <sys/ioctl.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <sys/mman.h> |
| 30 | #include <linux/videodev2.h> |
| 31 | #include "../libv4l/include/libv4l2.h" |
| 32 | |
| 33 | #define CLEAR(x) memset(&(x), 0, sizeof(x)) |
| 34 | |
| 35 | struct buffer { |
Mauro Carvalho Chehab | 0579e6e | 2016-07-04 16:25:48 -0300 | [diff] [blame] | 36 | void *start; |
| 37 | size_t length; |
Markus Heiser | 5377d91 | 2016-06-30 15:18:56 +0200 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | static void xioctl(int fh, int request, void *arg) |
| 41 | { |
Mauro Carvalho Chehab | 0579e6e | 2016-07-04 16:25:48 -0300 | [diff] [blame] | 42 | int r; |
Markus Heiser | 5377d91 | 2016-06-30 15:18:56 +0200 | [diff] [blame] | 43 | |
Mauro Carvalho Chehab | 0579e6e | 2016-07-04 16:25:48 -0300 | [diff] [blame] | 44 | do { |
| 45 | r = v4l2_ioctl(fh, request, arg); |
| 46 | } while (r == -1 && ((errno == EINTR) || (errno == EAGAIN))); |
Markus Heiser | 5377d91 | 2016-06-30 15:18:56 +0200 | [diff] [blame] | 47 | |
Mauro Carvalho Chehab | 0579e6e | 2016-07-04 16:25:48 -0300 | [diff] [blame] | 48 | if (r == -1) { |
| 49 | fprintf(stderr, "error %d, %s\\n", errno, strerror(errno)); |
| 50 | exit(EXIT_FAILURE); |
| 51 | } |
Markus Heiser | 5377d91 | 2016-06-30 15:18:56 +0200 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | int main(int argc, char **argv) |
| 55 | { |
Mauro Carvalho Chehab | 0579e6e | 2016-07-04 16:25:48 -0300 | [diff] [blame] | 56 | struct v4l2_format fmt; |
| 57 | struct v4l2_buffer buf; |
| 58 | struct v4l2_requestbuffers req; |
| 59 | enum v4l2_buf_type type; |
| 60 | fd_set fds; |
| 61 | struct timeval tv; |
| 62 | int r, fd = -1; |
| 63 | unsigned int i, n_buffers; |
| 64 | char *dev_name = "/dev/video0"; |
Mauro Carvalho Chehab | 8968da9 | 2016-07-13 08:43:30 -0300 | [diff] [blame] | 65 | char out_name[256]; |
Mauro Carvalho Chehab | 0579e6e | 2016-07-04 16:25:48 -0300 | [diff] [blame] | 66 | FILE *fout; |
| 67 | struct buffer *buffers; |
Markus Heiser | 5377d91 | 2016-06-30 15:18:56 +0200 | [diff] [blame] | 68 | |
Mauro Carvalho Chehab | 0579e6e | 2016-07-04 16:25:48 -0300 | [diff] [blame] | 69 | fd = v4l2_open(dev_name, O_RDWR | O_NONBLOCK, 0); |
| 70 | if (fd < 0) { |
| 71 | perror("Cannot open device"); |
| 72 | exit(EXIT_FAILURE); |
| 73 | } |
Markus Heiser | 5377d91 | 2016-06-30 15:18:56 +0200 | [diff] [blame] | 74 | |
Mauro Carvalho Chehab | 0579e6e | 2016-07-04 16:25:48 -0300 | [diff] [blame] | 75 | CLEAR(fmt); |
| 76 | fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 77 | fmt.fmt.pix.width = 640; |
| 78 | fmt.fmt.pix.height = 480; |
| 79 | fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24; |
| 80 | fmt.fmt.pix.field = V4L2_FIELD_INTERLACED; |
| 81 | xioctl(fd, VIDIOC_S_FMT, &fmt); |
| 82 | if (fmt.fmt.pix.pixelformat != V4L2_PIX_FMT_RGB24) { |
| 83 | printf("Libv4l didn't accept RGB24 format. Can't proceed.\\n"); |
| 84 | exit(EXIT_FAILURE); |
| 85 | } |
| 86 | if ((fmt.fmt.pix.width != 640) || (fmt.fmt.pix.height != 480)) |
| 87 | printf("Warning: driver is sending image at %dx%d\\n", |
| 88 | fmt.fmt.pix.width, fmt.fmt.pix.height); |
Markus Heiser | 5377d91 | 2016-06-30 15:18:56 +0200 | [diff] [blame] | 89 | |
Mauro Carvalho Chehab | 0579e6e | 2016-07-04 16:25:48 -0300 | [diff] [blame] | 90 | CLEAR(req); |
| 91 | req.count = 2; |
| 92 | req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 93 | req.memory = V4L2_MEMORY_MMAP; |
| 94 | xioctl(fd, VIDIOC_REQBUFS, &req); |
Markus Heiser | 5377d91 | 2016-06-30 15:18:56 +0200 | [diff] [blame] | 95 | |
Mauro Carvalho Chehab | 0579e6e | 2016-07-04 16:25:48 -0300 | [diff] [blame] | 96 | buffers = calloc(req.count, sizeof(*buffers)); |
| 97 | for (n_buffers = 0; n_buffers < req.count; ++n_buffers) { |
| 98 | CLEAR(buf); |
Markus Heiser | 5377d91 | 2016-06-30 15:18:56 +0200 | [diff] [blame] | 99 | |
Mauro Carvalho Chehab | 0579e6e | 2016-07-04 16:25:48 -0300 | [diff] [blame] | 100 | buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 101 | buf.memory = V4L2_MEMORY_MMAP; |
| 102 | buf.index = n_buffers; |
Markus Heiser | 5377d91 | 2016-06-30 15:18:56 +0200 | [diff] [blame] | 103 | |
Mauro Carvalho Chehab | 0579e6e | 2016-07-04 16:25:48 -0300 | [diff] [blame] | 104 | xioctl(fd, VIDIOC_QUERYBUF, &buf); |
Markus Heiser | 5377d91 | 2016-06-30 15:18:56 +0200 | [diff] [blame] | 105 | |
Mauro Carvalho Chehab | 0579e6e | 2016-07-04 16:25:48 -0300 | [diff] [blame] | 106 | buffers[n_buffers].length = buf.length; |
| 107 | buffers[n_buffers].start = v4l2_mmap(NULL, buf.length, |
| 108 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 109 | fd, buf.m.offset); |
Markus Heiser | 5377d91 | 2016-06-30 15:18:56 +0200 | [diff] [blame] | 110 | |
Mauro Carvalho Chehab | 0579e6e | 2016-07-04 16:25:48 -0300 | [diff] [blame] | 111 | if (MAP_FAILED == buffers[n_buffers].start) { |
| 112 | perror("mmap"); |
| 113 | exit(EXIT_FAILURE); |
| 114 | } |
| 115 | } |
Markus Heiser | 5377d91 | 2016-06-30 15:18:56 +0200 | [diff] [blame] | 116 | |
Mauro Carvalho Chehab | 0579e6e | 2016-07-04 16:25:48 -0300 | [diff] [blame] | 117 | for (i = 0; i < n_buffers; ++i) { |
| 118 | CLEAR(buf); |
| 119 | buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 120 | buf.memory = V4L2_MEMORY_MMAP; |
| 121 | buf.index = i; |
| 122 | xioctl(fd, VIDIOC_QBUF, &buf); |
| 123 | } |
| 124 | type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Markus Heiser | 5377d91 | 2016-06-30 15:18:56 +0200 | [diff] [blame] | 125 | |
Mauro Carvalho Chehab | 0579e6e | 2016-07-04 16:25:48 -0300 | [diff] [blame] | 126 | xioctl(fd, VIDIOC_STREAMON, &type); |
| 127 | for (i = 0; i < 20; i++) { |
| 128 | do { |
| 129 | FD_ZERO(&fds); |
| 130 | FD_SET(fd, &fds); |
Markus Heiser | 5377d91 | 2016-06-30 15:18:56 +0200 | [diff] [blame] | 131 | |
Mauro Carvalho Chehab | 0579e6e | 2016-07-04 16:25:48 -0300 | [diff] [blame] | 132 | /* Timeout. */ |
| 133 | tv.tv_sec = 2; |
| 134 | tv.tv_usec = 0; |
Markus Heiser | 5377d91 | 2016-06-30 15:18:56 +0200 | [diff] [blame] | 135 | |
Mauro Carvalho Chehab | 0579e6e | 2016-07-04 16:25:48 -0300 | [diff] [blame] | 136 | r = select(fd + 1, &fds, NULL, NULL, &tv); |
| 137 | } while ((r == -1 && (errno = EINTR))); |
| 138 | if (r == -1) { |
| 139 | perror("select"); |
| 140 | return errno; |
| 141 | } |
Markus Heiser | 5377d91 | 2016-06-30 15:18:56 +0200 | [diff] [blame] | 142 | |
Mauro Carvalho Chehab | 0579e6e | 2016-07-04 16:25:48 -0300 | [diff] [blame] | 143 | CLEAR(buf); |
| 144 | buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 145 | buf.memory = V4L2_MEMORY_MMAP; |
| 146 | xioctl(fd, VIDIOC_DQBUF, &buf); |
Markus Heiser | 5377d91 | 2016-06-30 15:18:56 +0200 | [diff] [blame] | 147 | |
Mauro Carvalho Chehab | 0579e6e | 2016-07-04 16:25:48 -0300 | [diff] [blame] | 148 | sprintf(out_name, "out%03d.ppm", i); |
| 149 | fout = fopen(out_name, "w"); |
| 150 | if (!fout) { |
| 151 | perror("Cannot open image"); |
| 152 | exit(EXIT_FAILURE); |
| 153 | } |
| 154 | fprintf(fout, "P6\\n%d %d 255\\n", |
| 155 | fmt.fmt.pix.width, fmt.fmt.pix.height); |
| 156 | fwrite(buffers[buf.index].start, buf.bytesused, 1, fout); |
| 157 | fclose(fout); |
Markus Heiser | 5377d91 | 2016-06-30 15:18:56 +0200 | [diff] [blame] | 158 | |
Mauro Carvalho Chehab | 0579e6e | 2016-07-04 16:25:48 -0300 | [diff] [blame] | 159 | xioctl(fd, VIDIOC_QBUF, &buf); |
| 160 | } |
Markus Heiser | 5377d91 | 2016-06-30 15:18:56 +0200 | [diff] [blame] | 161 | |
Mauro Carvalho Chehab | 0579e6e | 2016-07-04 16:25:48 -0300 | [diff] [blame] | 162 | type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 163 | xioctl(fd, VIDIOC_STREAMOFF, &type); |
| 164 | for (i = 0; i < n_buffers; ++i) |
| 165 | v4l2_munmap(buffers[i].start, buffers[i].length); |
| 166 | v4l2_close(fd); |
Markus Heiser | 5377d91 | 2016-06-30 15:18:56 +0200 | [diff] [blame] | 167 | |
Mauro Carvalho Chehab | 0579e6e | 2016-07-04 16:25:48 -0300 | [diff] [blame] | 168 | return 0; |
Markus Heiser | 5377d91 | 2016-06-30 15:18:56 +0200 | [diff] [blame] | 169 | } |