1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> 2 <HTML 3 ><HEAD 4 ><TITLE 5 >Streaming I/O (User Pointers)</TITLE 6 ><META 7 NAME="GENERATOR" 8 CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK 9 REL="HOME" 10 TITLE="Video for Linux Two API Specification" 11 HREF="book1.htm"><LINK 12 REL="UP" 13 TITLE="Input/Output" 14 HREF="c5742.htm"><LINK 15 REL="PREVIOUS" 16 TITLE="Streaming I/O (Memory Mapping)" 17 HREF="x5791.htm"><LINK 18 REL="NEXT" 19 TITLE="Asynchronous I/O" 20 HREF="x5950.htm"></HEAD 21 ><BODY 22 CLASS="SECTION" 23 BGCOLOR="#FFFFFF" 24 TEXT="#000000" 25 LINK="#0000FF" 26 VLINK="#840084" 27 ALINK="#0000FF" 28 ><DIV 29 CLASS="NAVHEADER" 30 ><TABLE 31 SUMMARY="Header navigation table" 32 WIDTH="100%" 33 BORDER="0" 34 CELLPADDING="0" 35 CELLSPACING="0" 36 ><TR 37 ><TH 38 COLSPAN="3" 39 ALIGN="center" 40 >Video for Linux Two API Specification: Revision 0.24</TH 41 ></TR 42 ><TR 43 ><TD 44 WIDTH="10%" 45 ALIGN="left" 46 VALIGN="bottom" 47 ><A 48 HREF="x5791.htm" 49 ACCESSKEY="P" 50 >Prev</A 51 ></TD 52 ><TD 53 WIDTH="80%" 54 ALIGN="center" 55 VALIGN="bottom" 56 >Chapter 3. Input/Output</TD 57 ><TD 58 WIDTH="10%" 59 ALIGN="right" 60 VALIGN="bottom" 61 ><A 62 HREF="x5950.htm" 63 ACCESSKEY="N" 64 >Next</A 65 ></TD 66 ></TR 67 ></TABLE 68 ><HR 69 ALIGN="LEFT" 70 WIDTH="100%"></DIV 71 ><DIV 72 CLASS="SECTION" 73 ><H1 74 CLASS="SECTION" 75 ><A 76 NAME="USERP" 77 >3.3. Streaming I/O (User Pointers)</A 78 ></H1 79 ><P 80 >Input and output devices support this I/O method when the 81 <CODE 82 CLASS="CONSTANT" 83 >V4L2_CAP_STREAMING</CODE 84 > flag in the 85 <CODE 86 CLASS="STRUCTFIELD" 87 >capabilities</CODE 88 > field of struct <A 89 HREF="r13105.htm#V4L2-CAPABILITY" 90 >v4l2_capability</A 91 > 92 returned by the <A 93 HREF="r13105.htm" 94 ><CODE 95 CLASS="CONSTANT" 96 >VIDIOC_QUERYCAP</CODE 97 ></A 98 > ioctl is set. If the particular user 99 pointer method (not only memory mapping) is supported must be 100 determined by calling the <A 101 HREF="r13696.htm" 102 ><CODE 103 CLASS="CONSTANT" 104 >VIDIOC_REQBUFS</CODE 105 ></A 106 > ioctl.</P 107 ><P 108 >This I/O method combines advantages of the read/write and 109 memory mapping methods. Buffers are allocated by the application 110 itself, and can reside for example in virtual or shared memory. Only 111 pointers to data are exchanged, these pointers and meta-information 112 are passed in struct <A 113 HREF="x5953.htm#V4L2-BUFFER" 114 >v4l2_buffer</A 115 >. The driver must be switched 116 into user pointer I/O mode by calling the <A 117 HREF="r13696.htm" 118 ><CODE 119 CLASS="CONSTANT" 120 >VIDIOC_REQBUFS</CODE 121 ></A 122 > with the 123 desired buffer type. No buffers are allocated beforehands, 124 consequently they are not indexed and cannot be queried like mapped 125 buffers with the <CODE 126 CLASS="CONSTANT" 127 >VIDIOC_QUERYBUF</CODE 128 > ioctl.</P 129 ><DIV 130 CLASS="EXAMPLE" 131 ><A 132 NAME="AEN5899" 133 ></A 134 ><P 135 ><B 136 >Example 3-2. Initiating streaming I/O with user pointers</B 137 ></P 138 ><PRE 139 CLASS="PROGRAMLISTING" 140 >struct <A 141 HREF="r13696.htm#V4L2-REQUESTBUFFERS" 142 >v4l2_requestbuffers</A 143 > reqbuf; 144 145 memset (&reqbuf, 0, sizeof (reqbuf)); 146 reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 147 reqbuf.memory = V4L2_MEMORY_USERPTR; 148 149 if (ioctl (fd, <A 150 HREF="r13696.htm" 151 ><CODE 152 CLASS="CONSTANT" 153 >VIDIOC_REQBUFS</CODE 154 ></A 155 >, &reqbuf) == -1) { 156 if (errno == EINVAL) 157 printf ("Video capturing or user pointer streaming is not supported\n"); 158 else 159 perror ("VIDIOC_REQBUFS"); 160 161 exit (EXIT_FAILURE); 162 } 163 </PRE 164 ></DIV 165 ><P 166 >Buffer addresses and sizes are passed on the fly with the 167 <A 168 HREF="r12878.htm" 169 ><CODE 170 CLASS="CONSTANT" 171 >VIDIOC_QBUF</CODE 172 ></A 173 > ioctl. Although buffers are commonly cycled, 174 applications can pass different addresses and sizes at each 175 <CODE 176 CLASS="CONSTANT" 177 >VIDIOC_QBUF</CODE 178 > call. If required by the hardware the 179 driver swaps memory pages within physical memory to create a 180 continuous area of memory. This happens transparently to the 181 application in the virtual memory subsystem of the kernel. When buffer 182 pages have been swapped out to disk they are brought back and finally 183 locked in physical memory for DMA.<A 184 NAME="AEN5909" 185 HREF="x5884.htm#FTN.AEN5909" 186 ><SPAN 187 CLASS="footnote" 188 >[1]</SPAN 189 ></A 190 ></P 191 ><P 192 >Filled or displayed buffers are dequeued with the 193 <A 194 HREF="r12878.htm" 195 ><CODE 196 CLASS="CONSTANT" 197 >VIDIOC_DQBUF</CODE 198 ></A 199 > ioctl. The driver can unlock the memory pages at any 200 time between the completion of the DMA and this ioctl. The memory is 201 also unlocked when <A 202 HREF="r13817.htm" 203 ><CODE 204 CLASS="CONSTANT" 205 >VIDIOC_STREAMOFF</CODE 206 ></A 207 > is called, <A 208 HREF="r13696.htm" 209 ><CODE 210 CLASS="CONSTANT" 211 >VIDIOC_REQBUFS</CODE 212 ></A 213 >, or 214 when the device is closed. Applications must take care not to free 215 buffers without dequeuing. For once, the buffers remain locked until 216 further, wasting physical memory. Second the driver will not be 217 notified when the memory is returned to the application's free list 218 and subsequently reused for other purposes, possibly completing the 219 requested DMA and overwriting valuable data.</P 220 ><P 221 >For capturing applications it is customary to enqueue a 222 number of empty buffers, to start capturing and enter the read loop. 223 Here the application waits until a filled buffer can be dequeued, and 224 re-enqueues the buffer when the data is no longer needed. Output 225 applications fill and enqueue buffers, when enough buffers are stacked 226 up output is started. In the write loop, when the application 227 runs out of free buffers it must wait until an empty buffer can be 228 dequeued and reused. Two methods exist to suspend execution of the 229 application until one or more buffers can be dequeued. By default 230 <CODE 231 CLASS="CONSTANT" 232 >VIDIOC_DQBUF</CODE 233 > blocks when no buffer is in the 234 outgoing queue. When the <CODE 235 CLASS="CONSTANT" 236 >O_NONBLOCK</CODE 237 > flag was 238 given to the <A 239 HREF="r14090.htm" 240 ><CODE 241 CLASS="FUNCTION" 242 >open()</CODE 243 ></A 244 > function, <CODE 245 CLASS="CONSTANT" 246 >VIDIOC_DQBUF</CODE 247 > 248 returns immediately with an <SPAN 249 CLASS="ERRORCODE" 250 >EAGAIN</SPAN 251 > error code when no buffer is available. The 252 <A 253 HREF="r14390.htm" 254 ><CODE 255 CLASS="FUNCTION" 256 >select()</CODE 257 ></A 258 > or <A 259 HREF="r14169.htm" 260 ><CODE 261 CLASS="FUNCTION" 262 >poll()</CODE 263 ></A 264 > function are always available.</P 265 ><P 266 >To start and stop capturing or output applications call the 267 <A 268 HREF="r13817.htm" 269 ><CODE 270 CLASS="CONSTANT" 271 >VIDIOC_STREAMON</CODE 272 ></A 273 > and <A 274 HREF="r13817.htm" 275 ><CODE 276 CLASS="CONSTANT" 277 >VIDIOC_STREAMOFF</CODE 278 ></A 279 > ioctl. Note 280 <CODE 281 CLASS="CONSTANT" 282 >VIDIOC_STREAMOFF</CODE 283 > removes all buffers from both 284 queues and unlocks all buffers as a side effect. Since there is no 285 notion of doing anything "now" on a multitasking system, if an 286 application needs to synchronize with another event it should examine 287 the struct <A 288 HREF="x5953.htm#V4L2-BUFFER" 289 >v4l2_buffer</A 290 > <CODE 291 CLASS="STRUCTFIELD" 292 >timestamp</CODE 293 > of captured 294 buffers, or set the field before enqueuing buffers for output.</P 295 ><P 296 >Drivers implementing user pointer I/O must 297 support the <CODE 298 CLASS="CONSTANT" 299 >VIDIOC_REQBUFS</CODE 300 >, 301 <CODE 302 CLASS="CONSTANT" 303 >VIDIOC_QBUF</CODE 304 >, <CODE 305 CLASS="CONSTANT" 306 >VIDIOC_DQBUF</CODE 307 >, 308 <CODE 309 CLASS="CONSTANT" 310 >VIDIOC_STREAMON</CODE 311 > and 312 <CODE 313 CLASS="CONSTANT" 314 >VIDIOC_STREAMOFF</CODE 315 > ioctl, the 316 <CODE 317 CLASS="FUNCTION" 318 >select()</CODE 319 > and <CODE 320 CLASS="FUNCTION" 321 >poll()</CODE 322 > function.<A 323 NAME="AEN5945" 324 HREF="x5884.htm#FTN.AEN5945" 325 ><SPAN 326 CLASS="footnote" 327 >[2]</SPAN 328 ></A 329 ></P 330 ></DIV 331 ><H3 332 CLASS="FOOTNOTES" 333 >Notes</H3 334 ><TABLE 335 BORDER="0" 336 CLASS="FOOTNOTES" 337 WIDTH="100%" 338 ><TR 339 ><TD 340 ALIGN="LEFT" 341 VALIGN="TOP" 342 WIDTH="5%" 343 ><A 344 NAME="FTN.AEN5909" 345 HREF="x5884.htm#AEN5909" 346 ><SPAN 347 CLASS="footnote" 348 >[1]</SPAN 349 ></A 350 ></TD 351 ><TD 352 ALIGN="LEFT" 353 VALIGN="TOP" 354 WIDTH="95%" 355 ><P 356 >We expect that frequently used buffers are typically not 357 swapped out. Anyway, the process of swapping, locking or generating 358 scatter-gather lists may be time consuming. The delay can be masked by 359 the depth of the incoming buffer queue, and perhaps by maintaining 360 caches assuming a buffer will be soon enqueued again. On the other 361 hand, to optimize memory usage drivers can limit the number of buffers 362 locked in advance and recycle the most recently used buffers first. Of 363 course, the pages of empty buffers in the incoming queue need not be 364 saved to disk. Output buffers must be saved on the incoming and 365 outgoing queue because an application may share them with other 366 processes.</P 367 ></TD 368 ></TR 369 ><TR 370 ><TD 371 ALIGN="LEFT" 372 VALIGN="TOP" 373 WIDTH="5%" 374 ><A 375 NAME="FTN.AEN5945" 376 HREF="x5884.htm#AEN5945" 377 ><SPAN 378 CLASS="footnote" 379 >[2]</SPAN 380 ></A 381 ></TD 382 ><TD 383 ALIGN="LEFT" 384 VALIGN="TOP" 385 WIDTH="95%" 386 ><P 387 >At the driver level <CODE 388 CLASS="FUNCTION" 389 >select()</CODE 390 > and 391 <CODE 392 CLASS="FUNCTION" 393 >poll()</CODE 394 > are the same, and 395 <CODE 396 CLASS="FUNCTION" 397 >select()</CODE 398 > is too important to be optional. The 399 rest should be evident.</P 400 ></TD 401 ></TR 402 ></TABLE 403 ><DIV 404 CLASS="NAVFOOTER" 405 ><HR 406 ALIGN="LEFT" 407 WIDTH="100%"><TABLE 408 SUMMARY="Footer navigation table" 409 WIDTH="100%" 410 BORDER="0" 411 CELLPADDING="0" 412 CELLSPACING="0" 413 ><TR 414 ><TD 415 WIDTH="33%" 416 ALIGN="left" 417 VALIGN="top" 418 ><A 419 HREF="x5791.htm" 420 ACCESSKEY="P" 421 >Prev</A 422 ></TD 423 ><TD 424 WIDTH="34%" 425 ALIGN="center" 426 VALIGN="top" 427 ><A 428 HREF="book1.htm" 429 ACCESSKEY="H" 430 >Home</A 431 ></TD 432 ><TD 433 WIDTH="33%" 434 ALIGN="right" 435 VALIGN="top" 436 ><A 437 HREF="x5950.htm" 438 ACCESSKEY="N" 439 >Next</A 440 ></TD 441 ></TR 442 ><TR 443 ><TD 444 WIDTH="33%" 445 ALIGN="left" 446 VALIGN="top" 447 >Streaming I/O (Memory Mapping)</TD 448 ><TD 449 WIDTH="34%" 450 ALIGN="center" 451 VALIGN="top" 452 ><A 453 HREF="c5742.htm" 454 ACCESSKEY="U" 455 >Up</A 456 ></TD 457 ><TD 458 WIDTH="33%" 459 ALIGN="right" 460 VALIGN="top" 461 >Asynchronous I/O</TD 462 ></TR 463 ></TABLE 464 ></DIV 465 ></BODY 466 ></HTML 467 > 468