V4l2 API defines several data transmission modes to read and write data from the video device. The driver must support one of them to exchange data with the application.
The classic I/O method is the read () and write () methods that can be used after the v4l2 device is enabled. The driver does not support the read/write method. In this case, the read/write operation returns an error.
Other I/O methods must be executed through negotiation; Streaming I/O mode: The application determines whether it is memory mapped or user buffers when calling vidioc_reqbufs; asynchronous I/O not implemented by v4l2 APIs
Video overlay can be considered another form of I/O mode, although the application cannot receive image data from the v4l2 Device
Read/write
If the input and output devices support read/write operations, the capabilities of the v4l2_capability structure returned by vidioc_querycap IOCTL must contain the v4l2_cap_readwrite flag.
Compared with stream I/O mode, read/write requires the CPU to copy data. (Of course, the user pointers of stream I/O also needs to copy data. If the user pointers is not a continuous physical page, the driving DMA requires a large continuous physical page ). In addition, compared with stream-io mode, read/write cannot obtain meta-information and is considered as a low-level I/O operation, such as frame count and timestamp, these meta-information are essential for recognizing frames and synchronizing data with other data streams. Of course, read/write is the simplest I/O method, so the encoding is simpler.
Streaming I/O(Memory mapping)
If the input and output devices support stream I/O operations, the capabilities of the v4l2_capability structure returned by vidioc_querycap IOCTL must contain the v4l2_cap_streaming flag.
The streaming I/O method only exchanges the pointer of buffers between the application and driver, and the data itself does not copy. Memory Mapping maps the physical memory in the device to the address space of the application. The physical memory of the device can be the video memory of the graphics card or the DMA memory in the main memory.
One Driver supports multiple buffers. Each group is identified by a unique buffer type value. Each group of buffers stores different types of data independently. Different file descriptors are required to access different types of data at the same time.
The application calls vidioc_reqbufs IOCTL to allocate device buffers. The parameter identifies the required quantity and type. This IOCTL can also be used to change buffers data and release allocated memory.
Before applications can access these buffers, they must call the MMAP function to map these physical addresses to user space addresses. These buffers are obtained through vidioc_querybuf in the physical location of the device space, vidioc_querybuf sets the v4l2_buffer structure. In single-planar mode, the Buf. m. offset and Buf. length returns the physical address of the buffer and the length of the buffer. When using the multi-planar API, the buffer of each layer needs to be mapped separately, that is, the number of calls to MMAP is the number of planar contained in each buffer.
Remember, buffers is the allocated physical memory. After using these buffers, the application needs to release the virtual memory address space through the munmap () function, the physical memory is released through vidioc_reqbufs (set the Buf count parameter to (0)
Theoretically, the streaming driver maintains two buffer Queues: input queue and output queue. Because synchronous capture or output operations are performed in real time, the introduction of these two queues can avoid data loss caused by disk, network latency and replacement by other application processes. Queues are organized according to the operating system, buffers output order is in the order of input queues, and capture is in the order of output queues
Streaming I/O(User pointers)
This I/O combines the advantages of read/write and memory mapping. The application allocates buffers (including planes) and buffers is virtual memory or shared memory. The driver and the application exchange data through data pointers,
These pointers and meta-information are transmitted through the v4l2_buffer structure.
The application allocates space for each buffers in the application space. Because the space is a virtual address space, this means that the driver of the v4l2 device must copy data, copy from the driver's DMA memory to the user's address space