Video for linux 2 example (v4l2 demo)

来源:互联网 发布:java静态变量怎样泛型 编辑:程序博客网 时间:2024/06/10 16:45

   V4L2 API讲解附demo

(注:从他人博客整理修正而来)

(看完本文后,更简便的api请移步至video for linux 2 API)

1. 定义


V4L2(Video For Linux Two) 是内核提供给应用程序访问音、视频驱动的统一接口。


2. 工作流程:


打开设备-> 检查和设置设备属性-> 设置帧格式-> 设置一种输入输出方法(缓冲 区管理)-> 循环获取数据-> 关闭设备。


3. 设备的打开和关闭:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include <fcntl.h>  
  2.   
  3. int open(const char *device_name, int flags);  
  4.   
  5. #include <unistd.h>  
  6.   
  7. int clo se(int fd);  

例:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. int fd=open(“/dev/video0”,O_RDWR); // 打开设备  
  2.   
  3. close(fd); // 关闭设备  

注意:V4L2 的相关定义包含在头文件<linux/videodev2.h> 中.


4. 查询设备属性: VIDIOC_QUERYCAP


相关函数:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. int ioctl(int fd, int request, struct v4l2_capability *argp);  

相关结构体:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct v4l2_capability  
  2.   
  3. {  
  4.   
  5. u8 driver[16]; // 驱动名字  
  6.   
  7. u8 card[32]; // 设备名字  
  8.   
  9. u8 bus_info[32]; // 设备在系统中的位置  
  10.   
  11. u32 version; // 驱动版本号  
  12.   
  13. u32 capabilities; // 设备支持的操作  
  14.   
  15. u32 reserved[4]; // 保留字段  
  16.   
  17. };  

capabilities 常用值:

V4L2_CAP_VIDEO_CAPTURE // 是否支持图像获取

例:显示设备信息


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct v4l2_capability cap;  
  2.   
  3. ioctl(fd,VIDIOC_QUERYCAP,&cap);  
  4.   
  5. printf(“Driver Name:%s\nCard Name:%s\nBus info:%s\nDriver Version:%u.%u.%u\n”,cap.driver,cap.card,cap.bus_info,(cap.version>>16)&0XFF, (cap.version>>8)&0XFF,cap.version&0XFF);  

5. 设置视频的制式和帧格式


制式包括PAL,NTSC,帧的格式个包括宽度和高度等。

相关函数:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. int ioctl(int fd, int request, struct v4l2_fmtdesc *argp);  
  2.   
  3. int ioctl(int fd, int request, struct v4l2_format *argp);  

相关结构体:

v4l2_cropcap 结构体用来设置摄像头的捕捉能力,在捕捉上视频时应先先设置

v4l2_cropcap 的 type 域,再通过 VIDIO_CROPCAP 操作命令获取设备捕捉能力的参数,保存于 v4l2_cropcap 结构体中,包括 bounds(最大捕捉方框的左上角坐标和宽高),defrect

(默认捕捉方框的左上角坐标和宽高)等。

v4l2_format 结构体用来设置摄像头的视频制式、帧格式等,在设置这个参数时应先填 好 v4l2_format 的各个域,如 type(传输流类型),fmt.pix.width(宽),

fmt.pix.heigth(高),fmt.pix.field(采样区域,如隔行采样),fmt.pix.pixelformat(采

样类型,如 YUV4:2:2),然后通过 VIDIO_S_FMT 操作命令设置视频捕捉格式。如下图所示:

clip_image004



5.1 查询并显示所有支持的格式:VIDIOC_ENUM_FMT


相关函数:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. int ioctl(int fd, int request, struct v4l2_fmtdesc *argp);  


相关结构体:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct v4l2_fmtdesc  
  2.   
  3. {  
  4.   
  5. u32 index; // 要查询的格式序号,应用程序设置  
  6.   
  7. enum v4l2_buf_type type; // 帧类型,应用程序设置  
  8.   
  9. u32 flags; // 是否为压缩格式  
  10.   
  11. u8 description[32]; // 格式名称  
  12.   
  13. u32 pixelformat; // 格式  
  14.   
  15. u32 reserved[4]; // 保留  
  16.   
  17. };  

例:显示所有支持的格式


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct v4l2_fmtdesc fmtdesc; fmtdesc.index=0; fmtdesc.type=V4L2_BUF_TYPE_VIDEO_CAPTURE; printf("Support format:\n");  
  2.   
  3. while(ioctl(fd, VIDIOC_ENUM_FMT, &fmtdesc) != -1)  
  4.   
  5. {  
  6.   
  7. printf("\t%d.%s\n",fmtdesc.index+1,fmtdesc.description);  
  8.   
  9. fmtdesc.index++;  
  10.   
  11. }  

5.2 查看或设置当前格式: VIDIOC_G_FMT, VIDIOC_S_FMT


检查是否支持某种格式:VIDIOC_TRY_FMT

相关函数:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. int ioctl(int fd, int request, struct v4l2_format *argp);  

相关结构体:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct v4l2_format  
  2. {  
  3.   
  4. enum v4l2_buf_type type; // 帧类型,应用程序设置  
  5.   
  6. union fmt  
  7.     {  
  8.   
  9.         struct v4l2_pix_format pix; // 视频设备使用  
  10.   
  11.         struct v4l2_window win;  
  12.   
  13.         struct v4l2_vbi_format vbi;  
  14.   
  15.         struct v4l2_sliced_vbi_format sliced;  
  16.   
  17.         u8 raw_data[200];  
  18.   
  19.     };  
  20. };  
  21.   
  22. struct v4l2_pix_format  
  23.   
  24. {  
  25.   
  26.     u32 width; // 帧宽,单位像素  
  27.   
  28.     u32 height; // 帧高,单位像素  
  29.   
  30.     u32 pixelformat; // 帧格式  
  31.   
  32.     enum v4l2_field field;  
  33.   
  34.     u32 bytesperline;  
  35.   
  36.     u32 sizeimage;  
  37.   
  38.     enum v4l2_colorspace colorspace;  
  39.   
  40.     u32 priv;  
  41.   
  42. };  

例:显示当前帧的相关信息


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct v4l2_format fmt;  
  2.   
  3. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  4.   
  5. ioctl(fd, VIDIOC_G_FMT, &fmt);  
  6.   
  7. printf(“Current data format information:\n\twidth:%d\n\theight:%d\n”, fmt.fmt.pix.width,fmt.fmt.pix.height);  
  8.   
  9. struct v4l2_fmtdesc fmtdesc;  
  10.   
  11. fmtdesc.index = 0;  
  12.   
  13. fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  14.   
  15. while(ioctl(fd,VIDIOC_ENUM_FMT,&fmtdesc) != -1)  
  16. {  
  17.   
  18.     if(fmtdesc.pixelformat & fmt.fmt.pix.pixelformat)  
  19.     {  
  20.   
  21.         printf(“\tformat:%s\n”,fmtdesc.description);  
  22.   
  23.         break;  
  24.   
  25.     }  
  26.     fmtdesc.index++;  
  27.   
  28. }  


例:检查是否支持某种帧格式


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct v4l2_format fmt;   
  2.   
  3. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;   
  4.   
  5. fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB32;   
  6.   
  7. if(ioctl(fd,VIDIOC_TRY_FMT,&fmt) == -1)   
  8. {  
  9.     if(errno==EINVAL)  
  10.     {  
  11.         printf(“not support format RGB32!\n”);  
  12.     }  
  13. }  


6. 图像的缩放 VIDIOC_CROPCAP


相关函数:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. int ioctl(int fd, int request, struct v4l2_cropcap *argp);  
  2.   
  3. int ioctl(int fd, int request, struct v4l2_crop *argp);  
  4.   
  5. int ioctl(int fd, int request, const struct v4l2_crop *argp);  

相关结构体:

v4l2_cropcap 结构体用来设置摄像头的捕捉能力,在捕捉上视频时应先先设置v4l2_cropcap 的 type 域,再通过 VIDIO_CROPCAP 操作命令获取设备捕捉能力的参数,保存于 v4l2_cropcap 结构体中,包括 bounds(最大捕捉方框的左上角坐标和宽高),defrect(默认捕捉方框的左上角坐标和宽高)等。

Cropping 和 scaling 主要指的是图像的取景范围及图片的比例缩放的支持。Crop 就 是把得到的数据作一定的裁剪和伸缩,裁剪可以只取样我们可以得到的图像大小的一部分, 剪裁的主要参数是位置、长度、宽度。而 scale 的设置是通过 VIDIOC_G_FMT 和 VIDIOC_S_FMT 来获得和设置当前的 image 的长度,宽度来实现的。看下图

image

我们可以假设 bounds 是 sensor 最大能捕捉到的图像范围,而 defrect 是设备默认 的最大取样范围,这个可以通过 VIDIOC_CROPCAP 的 ioctl 来获得设备的 crap 相关的属 性 v4l2_cropcap,其中的 bounds 就是这个 bounds,其实就是上限。每个设备都有个默 认的取样范围,就是 defrect,就是 default rect 的意思,它比 bounds 要小一些。这 个范围也是通过 VIDIOC_CROPCAP 的 ioctl 来获得的 v4l2_cropcap 结构中的 defrect 来表示的,我们可以通过 VIDIOC_G_CROP 和 VIDIOC_S_CROP 来获取和设置设备当前的 crop 设置。


6.1 设置设备捕捉能力的参数


相关函数:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. int ioctl(int fd, int request, struct v4l2_cropcap *argp);  


相关结构体:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct v4l2_cropcap  
  2. {  
  3.   
  4.     enum v4l2_buf_type type; // 数据流的类型,应用程序设置  
  5.   
  6.     struct v4l2_rect bounds; // 这是 camera 的镜头能捕捉到的窗口大小的局限  
  7.   
  8.     struct v4l2_rect defrect; // 定义默认窗口大小,包括起点位置及长,宽的大小,大小以像素为单位  
  9.   
  10.     struct v4l2_fract pixelaspect; // 定义了图片的宽高比  
  11.   
  12. };  

6.2 设置窗口取景参数 VIDIOC_G_CROP 和 VIDIOC_S_CROP


相关函数:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. int ioctl(int fd, int request, struct v4l2_crop *argp);  
  2.   
  3. int ioctl(int fd, int request, const struct v4l2_crop *argp);  


相关结构体:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct v4l2_crop  
  2. {  
  3.   
  4.     enum v4l2_buf_type type;// 应用程序设置  
  5.   
  6.     struct v4l2_rect c;  
  7.   
  8. }  


7.video Inputs and Outputs


VIDIOC_G_INPUT 和 VIDIOC_S_INPUT 用来查询和选则当前的 input,一个 video 设备 节点可能对应多个视频源,比如 saf7113 可以最多支持四路 cvbs 输入,如果上层想在四 个cvbs视频输入间切换,那么就要调用 ioctl(fd, VIDIOC_S_INPUT, &input) 来切换。VIDIOC_G_INPUT and VIDIOC_G_OUTPUT 返回当前的 video input和output的index.

相关函数:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. int ioctl(int fd, int request, struct v4l2_input *argp);  

相关结构体:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct v4l2_input   
  2. {  
  3.     __u32 index;    /* Which input */  
  4.     __u8 name[32];  /* Label */  
  5.     __u32 type;     /* Type of input */  
  6.     __u32 audioset; /* Associated audios (bitfield) */  
  7.     __u32 tuner;    /* Associated tuner */  
  8.     v4l2_std_id std;  
  9.     __u32 status;  
  10.     __u32 reserved[4];  
  11. };  

我们可以通过VIDIOC_ENUMINPUT and VIDIOC_ENUMOUTPUT 分别列举一个input或者 output的信息,我们使用一个v4l2_input结构体来存放查询结果,这个结构体中有一个 index域用来指定你索要查询的是第几个input/ouput,如果你所查询的这个input是当前正 在使用的,那么在v4l2_input还会包含一些当前的状态信息,如果所 查询的input/output 不存在,那么回返回EINVAL错误,所以,我们通过循环查找,直到返回错误来遍历所有的 input/output. VIDIOC_G_INPUT and VIDIOC_G_OUTPUT 返回当前的video input和output 的index.


例: 列举当前输入视频所支持的视频格式


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct v4l2_input input;  
  2.   
  3. struct v4l2_standard standard;  
  4.   
  5. memset (&input, 0, sizeof (input));  
  6.   
  7. //首先获得当前输入的 index,注意只是 index,要获得具体的信息,就的调用列举操作  
  8.   
  9. if (-1 == ioctl (fd, VIDIOC_G_INPUT, &input.index))   
  10. {  
  11.   
  12.     perror (”VIDIOC_G_INPUT”);  
  13.   
  14.     exit (EXIT_FAILURE);  
  15.   
  16. }  
  17.   
  18. //调用列举操作,获得 input.index 对应的输入的具体信息  
  19.   
  20. if (-1 == ioctl (fd, VIDIOC_ENUMINPUT, &input))   
  21. {  
  22.   
  23.     perror (”VIDIOC_ENUM_INPUT”);  
  24.   
  25.     exit (EXIT_FAILURE);  
  26.   
  27. }  
  28.   
  29. printf (”Current input %s supports:\n”, input.name); memset (&standard, 0, sizeof (standard)); standard.index = 0;  
  30.   
  31. //列举所有的所支持的 standard,如果 standard.id 与当前 input 的 input.std 有共同的  
  32. //bit flag,意味着当前的输入支持这个 standard,这样将所有驱动所支持的 standard 列举一个  
  33. //遍,就可以找到该输入所支持的所有 standard 了。  
  34.   
  35. while (0 == ioctl (fd, VIDIOC_ENUMSTD, &standard))   
  36. {  
  37.   
  38.     if (standard.id & input.std)  
  39.   
  40.     printf (”%s\n”, standard.name);  
  41.   
  42.     standard.index++;  
  43.   
  44. }  
  45.   
  46. /* EINVAL indicates the end of the enumeration, which cannot be empty unless this device falls under the USB exception. */  
  47.   
  48. if (errno != EINVAL || standard.index == 0)   
  49. {  
  50.   
  51.     perror (”VIDIOC_ENUMSTD”);  
  52.   
  53.     exit (EXIT_FAILURE);  
  54.   
  55. }  

8. Video standards


相关函数:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. v4l2_std_id std_id; //这个就是个64bit得数  
  2.   
  3. int ioctl(int fd, int request, struct v4l2_standard *argp);  


相关结构体:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. typedef u64 v4l2_std_id;  
  2.   
  3. struct v4l2_standard   
  4. {  
  5.   
  6.     u32 index;  
  7.   
  8.     v4l2_std_id id;  
  9.   
  10.     u8 name[24];  
  11.   
  12.     struct v4l2_fract frameperiod; /* Frames, not fields */  
  13.   
  14.     u32 framelines;  
  15.   
  16.     u32 reserved[4];  
  17.   
  18. };  

当然世界上现在有多个视频标准,如NTSC和PAL,他们又细分为好多种,那么我们的设 备输入/输出究竟支持什么样的标准呢?我们的当前在使用的输入和输出正在使用的是哪 个标准呢?我们怎么设置我们的某个输入输出使用的标准呢?这都是有方法的。

查询我们的输入支持什么标准,首先就得找到当前的这个输入的index,然后查出它的 属性,在其属性里面可以得到该输入所支持的标准,将它所支持的各个标准与所有的标准 的信息进行比较,就可以获知所支持的各个标准的属性。一个输入所支持的标准应该是一 个集合,而这个集合是用bit与的方式用一个64位数字表示。因此我们所查到的是一个数字。

Example: Information about the current video standard v4l2_std_id std_id; //这个就是个64bit得数


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct v4l2_standard standard;  
  2.   
  3. // VIDIOC_G_STD就是获得当前输入使用的standard,不过这里只是得到了该标准的id  
  4.   
  5. // 即flag,还没有得到其具体的属性信息,具体的属性信息要通过列举操作来得到。  
  6.   
  7. if (-1 == ioctl (fd, VIDIOC_G_STD, &std_id))   
  8. {  
  9.   
  10.     perror (”VIDIOC_G_STD”);  
  11.   
  12.     exit (EXIT_FAILURE);  
  13. //获得了当前输入使用的standard  
  14.   
  15. // Note when VIDIOC_ENUMSTD always returns EINVAL this is no video device  
  16.   
  17. // or it falls under the USB exception, and VIDIOC_G_STD returning EINVAL  
  18.   
  19. // is no error.  
  20.   
  21. }  
  22.   
  23. memset (&standard, 0, sizeof (standard));  
  24.   
  25. standard.index = 0; //从第一个开始列举  
  26.   
  27. // VIDIOC_ENUMSTD用来列举所支持的所有的video标准的信息,不过要先给standard  
  28.   
  29. // 结构的index域制定一个数值,所列举的标 准的信息属性包含在standard里面,  
  30.   
  31. // 如果我们所列举的标准和std_id有共同的bit,那么就意味着这个标准就是当前输  
  32.   
  33. // 入所使用的标准,这样我们就得到了当前输入使用的标准的属性信息  
  34.   
  35. while (0 == ioctl (fd, VIDIOC_ENUMSTD, &standard))   
  36. {  
  37.   
  38.     if (standard.id & std_id)   
  39.     {  
  40.   
  41.         printf (”Current video standard: %s\n”, standard.name);  
  42.   
  43.         exit (EXIT_SUCCESS);  
  44.   
  45.     }  
  46.   
  47.     standard.index++;  
  48.   
  49. }  
  50.   
  51. /* EINVAL indicates the end of the enumeration, which cannot be empty unless this device falls under the USB exception. */  
  52.   
  53. if (errno == EINVAL || standard.index == 0)   
  54. {  
  55.   
  56.     perror (”VIDIOC_ENUMSTD”);  
  57.   
  58.     exit (EXIT_FAILURE);  
  59.   
  60. }  

9. 申请和管理缓冲区


应用程序和设备有三种交换数据的方法,直接 read/write、内存映射(memory mapping)

和用户指针。这里只讨论内存映射(memory mapping)。


9.1 向设备申请缓冲区 VIDIOC_REQBUFS


相关函数:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. int ioctl(int fd, int request, struct v4l2_requestbuffers *argp);  


相关结构体:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct v4l2_requestbuffers  
  2. {  
  3.   
  4.     u32 count; // 缓冲区内缓冲帧的数目  
  5.   
  6.     enum v4l2_buf_type type; // 缓冲帧数据格式  
  7.   
  8.     enum v4l2_memory memory; // 区别是内存映射还是用户指针方式  
  9.   
  10.     u32 reserved[2];  
  11.   
  12. };  

注:

enum v4l2_memoy

{

        V4L2_MEMORY_MMAP, V4L2_MEMORY_USERPTR

};

//count,type,memory 都要应用程序设置


例:申请一个拥有四个缓冲帧的缓冲区


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct v4l2_requestbuffers req;   
  2.   
  3. req.count = 4;   
  4.   
  5. req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;   
  6.   
  7. req.memory = V4L2_MEMORY_MMAP;   
  8.   
  9. ioctl(fd,VIDIOC_REQBUFS,&req);  

9.2 获取缓冲帧的地址,长度:VIDIOC_QUERYBUF

相关函数:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. int ioctl(int fd, int request, struct v4l2_buffer *argp);  

相关结构体:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct v4l2_buffer  
  2. {  
  3.   
  4.     u32 index; //buffer 序号  
  5.   
  6.     enum v4l2_buf_type type; //buffer 类型  
  7.   
  8.     u32 byteused; //buffer 中已使用的字节数  
  9.   
  10.     u32 flags; // 区分是MMAP 还是USERPTR  
  11.   
  12.     enum v4l2_field field;  
  13.   
  14.     struct timeval timestamp; // 获取第一个字节时的系统时间  
  15.   
  16.     struct v4l2_timecode timecode;  
  17.   
  18.     u32 sequence; // 队列中的序号  
  19.   
  20.     enum v4l2_memory memory; //IO 方式,被应用程序设置  
  21.   
  22.     union m  
  23.   
  24.     {  
  25.   
  26.         u32 offset; // 缓冲帧地址,只对MMAP 有效  
  27.   
  28.         unsigned long userptr;  
  29.   
  30.     };  
  31.   
  32.     u32 length; // 缓冲帧长度  
  33.   
  34.     u32 input;  
  35.   
  36.     u32 reserved;  
  37.   
  38. };  

9.3 内存映射MMAP 及定义一个结构体来映射每个缓冲帧。 相关结构体:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct buffer  
  2. {  
  3.   
  4.     void* start;  
  5.   
  6.     unsigned int length;  
  7.   
  8. }*buffers;  

相关函数:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include <sys/mman.h>  
  2.   
  3. void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)  

//addr 映射起始地址,一般为NULL ,让内核自动选择

//length 被映射内存块的长度

//prot 标志映射后能否被读写,其值为PROT_EXEC,PROT_READ,PROT_WRITE, PROT_NONE

//flags 确定此内存映射能否被其他进程共享,MAP_SHARED,MAP_PRIVATE

//fd,offset, 确定被映射的内存地址 返回成功映射后的地址,不成功返回MAP_FAILED ((void*)-1)


相关函数:


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. int munmap(void *addr, size_t length);// 断开映射  

//addr 为映射后的地址,length 为映射后的内存长度

例:将四个已申请到的缓冲帧映射到应用程序,用buffers 指针记录。


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. buffers = (buffer*)calloc (req.count, sizeof (*buffers));  
  2.   
  3. if (!buffers)   
  4. {  
  5. // 映射  
  6.     fprintf (stderr, "Out of memory/n");  
  7.   
  8.     exit (EXIT_FAILURE);  
  9.   
  10. }  
  11.   
  12. for (unsigned int n_buffers = 0; n_buffers < req.count; ++n_buffers)  
  13.   
  14. {  
  15.   
  16.     struct v4l2_buffer buf;  
  17.   
  18.     memset(&buf,0,sizeof(buf));  
  19.   
  20.     buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  21.   
  22.     buf.memory = V4L2_MEMORY_MMAP;  
  23.   
  24.     buf.index = n_buffers;  
  25.   
  26.     // 查询序号为n_buffers 的缓冲区,得到其起始物理地址和大小  
  27.   
  28.     if (-1 == ioctl (fd, VIDIOC_QUERYBUF, &buf))  
  29.     {  
  30.         exit(-1);  
  31.     }  
  32.       
  33.   
  34.     buffers[n_buffers].length = buf.length;  
  35.   
  36.     // 映射内存  
  37.   
  38.     buffers[n_buffers].start = mmap (NULL,buf.length,PROT_READ | PROT_WRITE ,MAP_SHARED,fd, buf.m.offset);  
  39.   
  40.     if (MAP_FAILED == buffers[n_buffers].start)  
  41.     {  
  42.         exit(-1);  
  43.     }  
  44. }  

10. 缓冲区处理好之后,就可以开始获取数据了


10.1 启动 或 停止数据流 VIDIOC_STREAMON, VIDIOC_STREAMOFF


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. int ioctl(int fd, int request, const int *argp);  

//argp 为流类型指针,如V4L2_BUF_TYPE_VIDEO_CAPTURE.


10.2 在开始之前,还应当把缓冲帧放入缓冲队列:

VIDIOC_QBUF// 把帧放入队列

VIDIOC_DQBUF// 从队列中取出帧


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. int ioctl(int fd, int request, struct v4l2_buffer *argp);  

例:把四个缓冲帧放入队列,并启动数据流


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. unsigned int i;  
  2.   
  3. enum v4l2_buf_type type;  
  4.   
  5. for (i = 0; i < 4; ++i) // 将缓冲帧放入队列  
  6. {  
  7.   
  8.     struct v4l2_buffer buf;  
  9.   
  10.     buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  11.   
  12.     buf.memory = V4L2_MEMORY_MMAP;  
  13.   
  14.     buf.index = i;  
  15.   
  16.     ioctl (fd, VIDIOC_QBUF, &buf);  
  17.   
  18. }  
  19.   
  20. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  21.   
  22. ioctl (fd, VIDIOC_STREAMON, &type);  
  23.   
  24. // 这有个问题,这些buf 看起来和前面申请的buf 没什么关系,为什么呢?  

例:获取一帧并处理

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct v4l2_buffer buf; CLEAR (buf);  
  2.   
  3. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  4.   
  5. buf.memory = V4L2_MEMORY_MMAP;  
  6.   
  7. ioctl (fd, VIDIOC_DQBUF, &buf); // 从缓冲区取出一个缓冲帧  
  8.   
  9. process_image (buffers[buf.index.]start); //  
  10.   
  11. ioctl (fdVIDIOC_QBUF&buf); //  

附官方 v4l2 video capture example

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

0 0