Advertisement
Guest User

Untitled

a guest
Jul 19th, 2016
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 19.48 KB | None | 0 0
  1. /*
  2.  *  V4L2 video capture example
  3.  *
  4.  *  This program can be used and distributed without restrictions.
  5.  *
  6.  *      This program is provided with the V4L2 API
  7.  * see https://linuxtv.org/docs.php for more information
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <assert.h>
  14.  
  15. #include <getopt.h>             /* getopt_long() */
  16.  
  17. #include <fcntl.h>              /* low-level i/o */
  18. #include <unistd.h>
  19. #include <errno.h>
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22. #include <sys/time.h>
  23. #include <sys/mman.h>
  24. #include <sys/ioctl.h>
  25.  
  26. #include <linux/videodev2.h>
  27.  
  28. #define CLEAR(x) memset(&(x), 0, sizeof(x))
  29.  
  30. enum io_method {
  31.         IO_METHOD_READ,
  32.         IO_METHOD_MMAP,
  33.         IO_METHOD_USERPTR,
  34. };
  35.  
  36. struct buffer {
  37.         void   *start;
  38.         size_t  length;
  39. };
  40.  
  41. static char            *dev_name;
  42. static enum io_method   io = IO_METHOD_MMAP;
  43. static int              fd = -1;
  44. struct buffer          *buffers;
  45. static unsigned int     n_buffers;
  46. static int              out_buf;
  47. static int              force_format;
  48. static int              frame_count = 70;
  49.  
  50. static void errno_exit(const char *s)
  51. {
  52.         fprintf(stderr, "%s error %d, %s\n", s, errno, strerror(errno));
  53.         exit(EXIT_FAILURE);
  54. }
  55.  
  56. static int xioctl(int fh, int request, void *arg)
  57. {
  58.         int r;
  59.  
  60.         do {
  61.                 r = ioctl(fh, request, arg);
  62.         } while (-1 == r && EINTR == errno);
  63.  
  64.         return r;
  65. }
  66.  
  67. static void process_image(const void *p, int size)
  68. {
  69.         if (out_buf)
  70.                 fwrite(p, size, 1, stdout);
  71.  
  72.         fflush(stderr);
  73.         fprintf(stderr, ".");
  74.         fflush(stdout);
  75. }
  76.  
  77. static int read_frame(void)
  78. {
  79.         struct v4l2_buffer buf;
  80.         unsigned int i;
  81.  
  82.         switch (io) {
  83.         case IO_METHOD_READ:
  84.                 if (-1 == read(fd, buffers[0].start, buffers[0].length)) {
  85.                         switch (errno) {
  86.                         case EAGAIN:
  87.                                 return 0;
  88.  
  89.                         case EIO:
  90.                                 /* Could ignore EIO, see spec. */
  91.  
  92.                                 /* fall through */
  93.  
  94.                         default:
  95.                                 errno_exit("read");
  96.                         }
  97.                 }
  98.  
  99.         fprintf(stderr, "%d %d.%06d\n", buf.sequence, buf.timestamp.tv_sec, buf.timestamp.tv_usec);
  100. //              process_image(buffers[0].start, buffers[0].length, );
  101.                 break;
  102.  
  103.         case IO_METHOD_MMAP:
  104.                 CLEAR(buf);
  105.  
  106.                 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  107.                 buf.memory = V4L2_MEMORY_MMAP;
  108.  
  109.                 if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) {
  110.                         switch (errno) {
  111.                         case EAGAIN:
  112.                                 return 0;
  113.  
  114.                         case EIO:
  115.                                 /* Could ignore EIO, see spec. */
  116.  
  117.                                 /* fall through */
  118.  
  119.                         default:
  120.                                 errno_exit("VIDIOC_DQBUF");
  121.                         }
  122.                 }
  123.  
  124.                 assert(buf.index < n_buffers);
  125.  
  126.         fprintf(stderr, "%d %d.%06d\n", buf.sequence, buf.timestamp.tv_sec, buf.timestamp.tv_usec);
  127. //              process_image(buffers[buf.index].start, buf.bytesused);
  128.  
  129.                 if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
  130.                         errno_exit("VIDIOC_QBUF");
  131.                 break;
  132.  
  133.         case IO_METHOD_USERPTR:
  134.                 CLEAR(buf);
  135.  
  136.                 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  137.                 buf.memory = V4L2_MEMORY_USERPTR;
  138.  
  139.                 if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) {
  140.                         switch (errno) {
  141.                         case EAGAIN:
  142.                                 return 0;
  143.  
  144.                         case EIO:
  145.                                 /* Could ignore EIO, see spec. */
  146.  
  147.                                 /* fall through */
  148.  
  149.                         default:
  150.                                 errno_exit("VIDIOC_DQBUF");
  151.                         }
  152.                 }
  153.  
  154.                 for (i = 0; i < n_buffers; ++i)
  155.                         if (buf.m.userptr == (unsigned long)buffers[i].start
  156.                             && buf.length == buffers[i].length)
  157.                                 break;
  158.  
  159.                 assert(i < n_buffers);
  160.  
  161.                 process_image((void *)buf.m.userptr, buf.bytesused);
  162.  
  163.                 if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
  164.                         errno_exit("VIDIOC_QBUF");
  165.                 break;
  166.         }
  167.  
  168.         return 1;
  169. }
  170.  
  171. static void mainloop(void)
  172. {
  173.         unsigned int count;
  174.  
  175.         count = frame_count;
  176.  
  177.         while (count-- > 0) {
  178.                 for (;;) {
  179.                         fd_set fds;
  180.                         struct timeval tv;
  181.                         int r;
  182.  
  183.                         FD_ZERO(&fds);
  184.                         FD_SET(fd, &fds);
  185.  
  186.                         /* Timeout. */
  187.                         tv.tv_sec = 60;
  188.                         tv.tv_usec = 0;
  189.  
  190.                         r = select(fd + 1, &fds, NULL, NULL, &tv);
  191.  
  192.                         if (-1 == r) {
  193.                                 if (EINTR == errno)
  194.                                         continue;
  195.                                 errno_exit("select");
  196.                         }
  197.  
  198.                         if (0 == r) {
  199.                                 fprintf(stderr, "select timeout\n");
  200.                                 exit(EXIT_FAILURE);
  201.                         }
  202.  
  203.                         if (read_frame())
  204.                                 break;
  205.                         /* EAGAIN - continue select loop. */
  206.                 }
  207.         }
  208. }
  209.  
  210. static void stop_capturing(void)
  211. {
  212.         enum v4l2_buf_type type;
  213.  
  214.         switch (io) {
  215.         case IO_METHOD_READ:
  216.                 /* Nothing to do. */
  217.                 break;
  218.  
  219.         case IO_METHOD_MMAP:
  220.         case IO_METHOD_USERPTR:
  221.                 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  222.                 if (-1 == xioctl(fd, VIDIOC_STREAMOFF, &type))
  223.                         errno_exit("VIDIOC_STREAMOFF");
  224.                 break;
  225.         }
  226. }
  227.  
  228. static void start_capturing(void)
  229. {
  230.         unsigned int i;
  231.         enum v4l2_buf_type type;
  232.  
  233.         switch (io) {
  234.         case IO_METHOD_READ:
  235.                 /* Nothing to do. */
  236.                 break;
  237.  
  238.         case IO_METHOD_MMAP:
  239.                 for (i = 0; i < n_buffers; ++i) {
  240.                         struct v4l2_buffer buf;
  241.  
  242.                         CLEAR(buf);
  243.                         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  244.                         buf.memory = V4L2_MEMORY_MMAP;
  245.                         buf.index = i;
  246.  
  247.                         if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
  248.                                 errno_exit("VIDIOC_QBUF");
  249.                 }
  250.                 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  251.                 if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
  252.                         errno_exit("VIDIOC_STREAMON");
  253.                 break;
  254.  
  255.         case IO_METHOD_USERPTR:
  256.                 for (i = 0; i < n_buffers; ++i) {
  257.                         struct v4l2_buffer buf;
  258.  
  259.                         CLEAR(buf);
  260.                         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  261.                         buf.memory = V4L2_MEMORY_USERPTR;
  262.                         buf.index = i;
  263.                         buf.m.userptr = (unsigned long)buffers[i].start;
  264.                         buf.length = buffers[i].length;
  265.  
  266.                         if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
  267.                                 errno_exit("VIDIOC_QBUF");
  268.                 }
  269.                 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  270.                 if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
  271.                         errno_exit("VIDIOC_STREAMON");
  272.                 break;
  273.         }
  274. }
  275.  
  276. static void uninit_device(void)
  277. {
  278.         unsigned int i;
  279.  
  280.         switch (io) {
  281.         case IO_METHOD_READ:
  282.                 free(buffers[0].start);
  283.                 break;
  284.  
  285.         case IO_METHOD_MMAP:
  286.                 for (i = 0; i < n_buffers; ++i)
  287.                         if (-1 == munmap(buffers[i].start, buffers[i].length))
  288.                                 errno_exit("munmap");
  289.                 break;
  290.  
  291.         case IO_METHOD_USERPTR:
  292.                 for (i = 0; i < n_buffers; ++i)
  293.                         free(buffers[i].start);
  294.                 break;
  295.         }
  296.  
  297.         free(buffers);
  298. }
  299.  
  300. static void init_read(unsigned int buffer_size)
  301. {
  302.         buffers = calloc(1, sizeof(*buffers));
  303.  
  304.         if (!buffers) {
  305.                 fprintf(stderr, "Out of memory\n");
  306.                 exit(EXIT_FAILURE);
  307.         }
  308.  
  309.         buffers[0].length = buffer_size;
  310.         buffers[0].start = malloc(buffer_size);
  311.  
  312.         if (!buffers[0].start) {
  313.                 fprintf(stderr, "Out of memory\n");
  314.                 exit(EXIT_FAILURE);
  315.         }
  316. }
  317.  
  318. static void init_mmap(void)
  319. {
  320.         struct v4l2_requestbuffers req;
  321.  
  322.         CLEAR(req);
  323.  
  324.         req.count = 4;
  325.         req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  326.         req.memory = V4L2_MEMORY_MMAP;
  327.  
  328.         if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) {
  329.                 if (EINVAL == errno) {
  330.                         fprintf(stderr, "%s does not support "
  331.                                  "memory mapping\n", dev_name);
  332.                         exit(EXIT_FAILURE);
  333.                 } else {
  334.                         errno_exit("VIDIOC_REQBUFS");
  335.                 }
  336.         }
  337.  
  338.         if (req.count < 2) {
  339.                 fprintf(stderr, "Insufficient buffer memory on %s\n",
  340.                          dev_name);
  341.                 exit(EXIT_FAILURE);
  342.         }
  343.  
  344.         buffers = calloc(req.count, sizeof(*buffers));
  345.  
  346.         if (!buffers) {
  347.                 fprintf(stderr, "Out of memory\n");
  348.                 exit(EXIT_FAILURE);
  349.         }
  350.  
  351.         for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
  352.                 struct v4l2_buffer buf;
  353.  
  354.                 CLEAR(buf);
  355.  
  356.                 buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  357.                 buf.memory      = V4L2_MEMORY_MMAP;
  358.                 buf.index       = n_buffers;
  359.  
  360.                 if (-1 == xioctl(fd, VIDIOC_QUERYBUF, &buf))
  361.                         errno_exit("VIDIOC_QUERYBUF");
  362.  
  363.                 buffers[n_buffers].length = buf.length;
  364.                 buffers[n_buffers].start =
  365.                         mmap(NULL /* start anywhere */,
  366.                               buf.length,
  367.                               PROT_READ | PROT_WRITE /* required */,
  368.                               MAP_SHARED /* recommended */,
  369.                               fd, buf.m.offset);
  370.  
  371.                 if (MAP_FAILED == buffers[n_buffers].start)
  372.                         errno_exit("mmap");
  373.         }
  374. }
  375.  
  376. static void init_userp(unsigned int buffer_size)
  377. {
  378.         struct v4l2_requestbuffers req;
  379.  
  380.         CLEAR(req);
  381.  
  382.         req.count  = 4;
  383.         req.type   = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  384.         req.memory = V4L2_MEMORY_USERPTR;
  385.  
  386.         if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) {
  387.                 if (EINVAL == errno) {
  388.                         fprintf(stderr, "%s does not support "
  389.                                  "user pointer i/o\n", dev_name);
  390.                         exit(EXIT_FAILURE);
  391.                 } else {
  392.                         errno_exit("VIDIOC_REQBUFS");
  393.                 }
  394.         }
  395.  
  396.         buffers = calloc(4, sizeof(*buffers));
  397.  
  398.         if (!buffers) {
  399.                 fprintf(stderr, "Out of memory\n");
  400.                 exit(EXIT_FAILURE);
  401.         }
  402.  
  403.         for (n_buffers = 0; n_buffers < 4; ++n_buffers) {
  404.                 buffers[n_buffers].length = buffer_size;
  405.                 buffers[n_buffers].start = malloc(buffer_size);
  406.  
  407.                 if (!buffers[n_buffers].start) {
  408.                         fprintf(stderr, "Out of memory\n");
  409.                         exit(EXIT_FAILURE);
  410.                 }
  411.         }
  412. }
  413.  
  414. static void init_device(void)
  415. {
  416.         struct v4l2_capability cap;
  417.         struct v4l2_cropcap cropcap;
  418.         struct v4l2_crop crop;
  419.         struct v4l2_format fmt;
  420.         unsigned int min;
  421.  
  422.         if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) {
  423.                 if (EINVAL == errno) {
  424.                         fprintf(stderr, "%s is no V4L2 device\n",
  425.                                  dev_name);
  426.                         exit(EXIT_FAILURE);
  427.                 } else {
  428.                         errno_exit("VIDIOC_QUERYCAP");
  429.                 }
  430.         }
  431.  
  432.         if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
  433.                 fprintf(stderr, "%s is no video capture device\n",
  434.                          dev_name);
  435.                 exit(EXIT_FAILURE);
  436.         }
  437.  
  438.         switch (io) {
  439.         case IO_METHOD_READ:
  440.                 if (!(cap.capabilities & V4L2_CAP_READWRITE)) {
  441.                         fprintf(stderr, "%s does not support read i/o\n",
  442.                                  dev_name);
  443.                         exit(EXIT_FAILURE);
  444.                 }
  445.                 break;
  446.  
  447.         case IO_METHOD_MMAP:
  448.         case IO_METHOD_USERPTR:
  449.                 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
  450.                         fprintf(stderr, "%s does not support streaming i/o\n",
  451.                                  dev_name);
  452.                         exit(EXIT_FAILURE);
  453.                 }
  454.                 break;
  455.         }
  456.  
  457.  
  458.         /* Select video input, video standard and tune here. */
  459.  
  460.  
  461.         CLEAR(cropcap);
  462.  
  463.         cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  464.  
  465.         if (0 == xioctl(fd, VIDIOC_CROPCAP, &cropcap)) {
  466.                 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  467.                 crop.c = cropcap.defrect; /* reset to default */
  468.  
  469.                 if (-1 == xioctl(fd, VIDIOC_S_CROP, &crop)) {
  470.                         switch (errno) {
  471.                         case EINVAL:
  472.                                 /* Cropping not supported. */
  473.                                 break;
  474.                         default:
  475.                                 /* Errors ignored. */
  476.                                 break;
  477.                         }
  478.                 }
  479.         } else {
  480.                 /* Errors ignored. */
  481.         }
  482.  
  483.  
  484.         CLEAR(fmt);
  485.  
  486.         fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  487.         if (force_format) {
  488.                 fmt.fmt.pix.width       = 160;
  489.                 fmt.fmt.pix.height      = 120;
  490.                 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  491.                 fmt.fmt.pix.field       = V4L2_FIELD_INTERLACED;
  492.  
  493.                 if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt))
  494.                         errno_exit("VIDIOC_S_FMT");
  495.  
  496.                 /* Note VIDIOC_S_FMT may change width and height. */
  497.         } else {
  498.                 /* Preserve original settings as set by v4l2-ctl for example */
  499.                 if (-1 == xioctl(fd, VIDIOC_G_FMT, &fmt))
  500.                         errno_exit("VIDIOC_G_FMT");
  501.         }
  502.  
  503.         /* Buggy driver paranoia. */
  504.         min = fmt.fmt.pix.width * 2;
  505.         if (fmt.fmt.pix.bytesperline < min)
  506.                 fmt.fmt.pix.bytesperline = min;
  507.         min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
  508.         if (fmt.fmt.pix.sizeimage < min)
  509.                 fmt.fmt.pix.sizeimage = min;
  510.  
  511.         switch (io) {
  512.         case IO_METHOD_READ:
  513.                 init_read(fmt.fmt.pix.sizeimage);
  514.                 break;
  515.  
  516.         case IO_METHOD_MMAP:
  517.                 init_mmap();
  518.                 break;
  519.  
  520.         case IO_METHOD_USERPTR:
  521.                 init_userp(fmt.fmt.pix.sizeimage);
  522.                 break;
  523.         }
  524. }
  525.  
  526. static void close_device(void)
  527. {
  528.         if (-1 == close(fd))
  529.                 errno_exit("close");
  530.  
  531.         fd = -1;
  532. }
  533.  
  534. static void open_device(void)
  535. {
  536.         struct stat st;
  537.  
  538.         if (-1 == stat(dev_name, &st)) {
  539.                 fprintf(stderr, "Cannot identify '%s': %d, %s\n",
  540.                          dev_name, errno, strerror(errno));
  541.                 exit(EXIT_FAILURE);
  542.         }
  543.  
  544.         if (!S_ISCHR(st.st_mode)) {
  545.                 fprintf(stderr, "%s is no device\n", dev_name);
  546.                 exit(EXIT_FAILURE);
  547.         }
  548.  
  549.         fd = open(dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
  550.  
  551.         if (-1 == fd) {
  552.                 fprintf(stderr, "Cannot open '%s': %d, %s\n",
  553.                          dev_name, errno, strerror(errno));
  554.                 exit(EXIT_FAILURE);
  555.         }
  556. }
  557.  
  558. static void usage(FILE *fp, int argc, char **argv)
  559. {
  560.         fprintf(fp,
  561.                  "Usage: %s [options]\n\n"
  562.                  "Version 1.3\n"
  563.                  "Options:\n"
  564.                  "-d | --device name   Video device name [%s]\n"
  565.                  "-h | --help          Print this message\n"
  566.                  "-m | --mmap          Use memory mapped buffers [default]\n"
  567.                  "-r | --read          Use read() calls\n"
  568.                  "-u | --userp         Use application allocated buffers\n"
  569.                  "-o | --output        Outputs stream to stdout\n"
  570.                  "-f | --format        Force format to 640x480 YUYV\n"
  571.                  "-c | --count         Number of frames to grab [%i]\n"
  572.                  "",
  573.                  argv[0], dev_name, frame_count);
  574. }
  575.  
  576. static const char short_options[] = "d:hmruofc:";
  577.  
  578. static const struct option
  579. long_options[] = {
  580.         { "device", required_argument, NULL, 'd' },
  581.         { "help",   no_argument,       NULL, 'h' },
  582.         { "mmap",   no_argument,       NULL, 'm' },
  583.         { "read",   no_argument,       NULL, 'r' },
  584.         { "userp",  no_argument,       NULL, 'u' },
  585.         { "output", no_argument,       NULL, 'o' },
  586.         { "format", no_argument,       NULL, 'f' },
  587.         { "count",  required_argument, NULL, 'c' },
  588.         { 0, 0, 0, 0 }
  589. };
  590.  
  591. int main(int argc, char **argv)
  592. {
  593.         dev_name = "/dev/video0";
  594.  
  595.         for (;;) {
  596.                 int idx;
  597.                 int c;
  598.  
  599.                 c = getopt_long(argc, argv,
  600.                                 short_options, long_options, &idx);
  601.  
  602.                 if (-1 == c)
  603.                         break;
  604.  
  605.                 switch (c) {
  606.                 case 0: /* getopt_long() flag */
  607.                         break;
  608.  
  609.                 case 'd':
  610.                         dev_name = optarg;
  611.                         break;
  612.  
  613.                 case 'h':
  614.                         usage(stdout, argc, argv);
  615.                         exit(EXIT_SUCCESS);
  616.  
  617.                 case 'm':
  618.                         io = IO_METHOD_MMAP;
  619.                         break;
  620.  
  621.                 case 'r':
  622.                         io = IO_METHOD_READ;
  623.                         break;
  624.  
  625.                 case 'u':
  626.                         io = IO_METHOD_USERPTR;
  627.                         break;
  628.  
  629.                 case 'o':
  630.                         out_buf++;
  631.                         break;
  632.  
  633.                 case 'f':
  634.                         force_format++;
  635.                         break;
  636.  
  637.                 case 'c':
  638.                         errno = 0;
  639.                         frame_count = strtol(optarg, NULL, 0);
  640.                         if (errno)
  641.                                 errno_exit(optarg);
  642.                         break;
  643.  
  644.                 default:
  645.                         usage(stderr, argc, argv);
  646.                         exit(EXIT_FAILURE);
  647.                 }
  648.         }
  649.  
  650.         open_device();
  651.         init_device();
  652.         start_capturing();
  653.         mainloop();
  654.         stop_capturing();
  655.         uninit_device();
  656.         close_device();
  657.         fprintf(stderr, "\n");
  658.         return 0;
  659. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement