(V4L2相关)这两条线做什么?

时间:2016-02-12 14:58:10

标签: c linux v4l2

s->buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
s->buf.memory = V4L2_MEMORY_MMAP;

我必须承认我对V4L2 API并不熟悉。我理解上面两行,第一行是建立缓冲区类型,第二行是将视频设备专用内存的指针传递给缓冲区。

问题是,我不知道我是否做出了正确的猜测,我需要有人详细解释第二条线是如何工作的。

1 个答案:

答案 0 :(得分:0)

第二行确定缓冲区的使用方式。法律价值观是:

  

表3.5。 enum v4l2_memory

V4L2_MEMORY_MMAP    1   The buffer is used for memory mapping I/O.  
V4L2_MEMORY_USERPTR 2   The buffer is used for user pointer I/O.  
V4L2_MEMORY_OVERLAY 3   [Not Yet Implemented]  
V4L2_MEMORY_DMABUF  4   The buffer is used for DMA shared buffer I/O. 

您包含的代码实际上并不分配缓冲区。这是一个更完整的代码片段,显示缓冲区的mmap

    CLEAR(req);
    req.count = 2;
    req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    req.memory = V4L2_MEMORY_MMAP;
    xioctl(fd, VIDIOC_REQBUFS, &req);

    buffers = calloc(req.count, sizeof(*buffers));
    for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
            CLEAR(buf);

            buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;
            buf.memory      = V4L2_MEMORY_MMAP;
            buf.index       = n_buffers;

            xioctl(fd, VIDIOC_QUERYBUF, &buf);

            buffers[n_buffers].length = buf.length;
            buffers[n_buffers].start = v4l2_mmap(NULL, buf.length,
                          PROT_READ | PROT_WRITE, MAP_SHARED,
                          fd, buf.m.offset);

            if (MAP_FAILED == buffers[n_buffers].start) {
                    perror("mmap");
                    exit(EXIT_FAILURE);
            }
    }