userp.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. .. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
  2. .. c:namespace:: V4L
  3. .. _userp:
  4. *****************************
  5. Streaming I/O (User Pointers)
  6. *****************************
  7. Input and output devices support this I/O method when the
  8. ``V4L2_CAP_STREAMING`` flag in the ``capabilities`` field of struct
  9. :c:type:`v4l2_capability` returned by the
  10. :ref:`VIDIOC_QUERYCAP` ioctl is set. If the
  11. particular user pointer method (not only memory mapping) is supported
  12. must be determined by calling the :ref:`VIDIOC_REQBUFS` ioctl
  13. with the memory type set to ``V4L2_MEMORY_USERPTR``.
  14. This I/O method combines advantages of the read/write and memory mapping
  15. methods. Buffers (planes) are allocated by the application itself, and
  16. can reside for example in virtual or shared memory. Only pointers to
  17. data are exchanged, these pointers and meta-information are passed in
  18. struct :c:type:`v4l2_buffer` (or in struct
  19. :c:type:`v4l2_plane` in the multi-planar API case). The
  20. driver must be switched into user pointer I/O mode by calling the
  21. :ref:`VIDIOC_REQBUFS` with the desired buffer type.
  22. No buffers (planes) are allocated beforehand, consequently they are not
  23. indexed and cannot be queried like mapped buffers with the
  24. :ref:`VIDIOC_QUERYBUF <VIDIOC_QUERYBUF>` ioctl.
  25. Example: Initiating streaming I/O with user pointers
  26. ====================================================
  27. .. code-block:: c
  28. struct v4l2_requestbuffers reqbuf;
  29. memset (&reqbuf, 0, sizeof (reqbuf));
  30. reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  31. reqbuf.memory = V4L2_MEMORY_USERPTR;
  32. if (ioctl (fd, VIDIOC_REQBUFS, &reqbuf) == -1) {
  33. if (errno == EINVAL)
  34. printf ("Video capturing or user pointer streaming is not supported\\n");
  35. else
  36. perror ("VIDIOC_REQBUFS");
  37. exit (EXIT_FAILURE);
  38. }
  39. Buffer (plane) addresses and sizes are passed on the fly with the
  40. :ref:`VIDIOC_QBUF <VIDIOC_QBUF>` ioctl. Although buffers are commonly
  41. cycled, applications can pass different addresses and sizes at each
  42. :ref:`VIDIOC_QBUF <VIDIOC_QBUF>` call. If required by the hardware the
  43. driver swaps memory pages within physical memory to create a continuous
  44. area of memory. This happens transparently to the application in the
  45. virtual memory subsystem of the kernel. When buffer pages have been
  46. swapped out to disk they are brought back and finally locked in physical
  47. memory for DMA. [#f1]_
  48. Filled or displayed buffers are dequeued with the
  49. :ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` ioctl. The driver can unlock the
  50. memory pages at any time between the completion of the DMA and this
  51. ioctl. The memory is also unlocked when
  52. :ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` is called,
  53. :ref:`VIDIOC_REQBUFS`, or when the device is closed.
  54. Applications must take care not to free buffers without dequeuing.
  55. Firstly, the buffers remain locked for longer, wasting physical memory.
  56. Secondly the driver will not be notified when the memory is returned to
  57. the application's free list and subsequently reused for other purposes,
  58. possibly completing the requested DMA and overwriting valuable data.
  59. For capturing applications it is customary to enqueue a number of empty
  60. buffers, to start capturing and enter the read loop. Here the
  61. application waits until a filled buffer can be dequeued, and re-enqueues
  62. the buffer when the data is no longer needed. Output applications fill
  63. and enqueue buffers, when enough buffers are stacked up output is
  64. started. In the write loop, when the application runs out of free
  65. buffers it must wait until an empty buffer can be dequeued and reused.
  66. Two methods exist to suspend execution of the application until one or
  67. more buffers can be dequeued. By default :ref:`VIDIOC_DQBUF
  68. <VIDIOC_QBUF>` blocks when no buffer is in the outgoing queue. When the
  69. ``O_NONBLOCK`` flag was given to the :c:func:`open()` function,
  70. :ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` returns immediately with an ``EAGAIN``
  71. error code when no buffer is available. The :ref:`select()
  72. <func-select>` or :c:func:`poll()` function are always
  73. available.
  74. To start and stop capturing or output applications call the
  75. :ref:`VIDIOC_STREAMON <VIDIOC_STREAMON>` and
  76. :ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` ioctl.
  77. .. note::
  78. :ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` removes all buffers from
  79. both queues and unlocks all buffers as a side effect. Since there is no
  80. notion of doing anything "now" on a multitasking system, if an
  81. application needs to synchronize with another event it should examine
  82. the struct :c:type:`v4l2_buffer` ``timestamp`` of captured or
  83. outputted buffers.
  84. Drivers implementing user pointer I/O must support the
  85. :ref:`VIDIOC_REQBUFS <VIDIOC_REQBUFS>`, :ref:`VIDIOC_QBUF <VIDIOC_QBUF>`,
  86. :ref:`VIDIOC_DQBUF <VIDIOC_QBUF>`, :ref:`VIDIOC_STREAMON <VIDIOC_STREAMON>`
  87. and :ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` ioctls, the
  88. :c:func:`select()` and :c:func:`poll()` function. [#f2]_
  89. .. [#f1]
  90. We expect that frequently used buffers are typically not swapped out.
  91. Anyway, the process of swapping, locking or generating scatter-gather
  92. lists may be time consuming. The delay can be masked by the depth of
  93. the incoming buffer queue, and perhaps by maintaining caches assuming
  94. a buffer will be soon enqueued again. On the other hand, to optimize
  95. memory usage drivers can limit the number of buffers locked in
  96. advance and recycle the most recently used buffers first. Of course,
  97. the pages of empty buffers in the incoming queue need not be saved to
  98. disk. Output buffers must be saved on the incoming and outgoing queue
  99. because an application may share them with other processes.
  100. .. [#f2]
  101. At the driver level :c:func:`select()` and :c:func:`poll()` are
  102. the same, and :c:func:`select()` is too important to be optional.
  103. The rest should be evident.