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

我必须承认,我对v4l2api相当陌生。在以上两行中,第一行是建立缓冲区类型,第二行是将视频设备专用存储器的指针传递到缓冲区。
问题是,我不知道我的猜测是否正确,我需要有人详细解释第二行是如何工作的。

最佳答案

第二行确定如何使用缓冲区。法律价值观是:
表3.5。枚举v4l2_内存

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.

您包含的代码实际上并不分配缓冲区。下面是一个更完整的代码片段,显示了缓冲区的mmaping:
    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);
            }
    }

10-08 16:28