userp.rst 5.6 KB

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