一、源码test.c

#include <fcntl.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <linux/videodev2.h>

int main(){
  //////
  int fd = open("/dev/video0",O_RDWR);
  printf("TK------->>>fd is %d ",fd);
  //////
  struct v4l2_capability cap;
  ioctl(fd,VIDIOC_QUERYCAP,&cap);
  printf("TK---------->>>>>Driver Name:%s Card Name:%s Bus info:%s ",cap.driver,cap.card,cap.bus_info);
  //////
  struct v4l2_fmtdesc fmtdesc;
  fmtdesc.index = 0; fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  while(ioctl(fd,VIDIOC_ENUM_FMT,&fmtdesc) != -1){
   printf("TK-------->>>>>fmtdesc.description is %s ",fmtdesc.description);
   fmtdesc.index ++;
  }
  //////
  struct v4l2_format fmt;
  fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  ioctl(fd,VIDIOC_G_FMT,&fmt);
  printf("TK----------->>>>>fmt.fmt.width is %d fmt.fmt.pix.height is %d fmt.fmt.pix.colorspace is %d ",fmt.fmt.pix.width,fmt.fmt.pix.height,fmt.fmt.pix.colorspace);
  //////
  struct v4l2_requestbuffers req;
  req.count = 4;
  req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  req.memory = V4L2_MEMORY_MMAP;
  ioctl(fd,VIDIOC_REQBUFS,&req);
  struct buffer{
    void *start;
    unsigned int length;
  }*buffers;
  buffers = (struct buffer*)calloc (req.count, sizeof(*buffers));
  unsigned int n_buffers = 0;
  for(n_buffers = 0; n_buffers < req.count; ++n_buffers){
    struct v4l2_buffer buf;
    memset(&buf,0,sizeof(buf));
    buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    buf.memory = V4L2_MEMORY_MMAP;
    buf.index = n_buffers;
    if(ioctl(fd,VIDIOC_QUERYBUF,&buf) == -1){
      printf("TK---------_>>>>>>error ");
      close(fd);
      exit(-1);
    }
    buffers[n_buffers].length = buf.length;
    buffers[n_buffers].start = mmap(NULL,buf.length,PROT_READ|PROT_WRITE,MAP_SHARED,fd,buf.m.offset);
    if(MAP_FAILED == buffers[n_buffers].start){
      printf("TK--------__>>>>>error 2 ");
      close(fd);
      exit(-1);
    }
  }