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 >Common API Elements</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="PREVIOUS" 13 TITLE="Introduction" 14 HREF="f163.htm"><LINK 15 REL="NEXT" 16 TITLE="Querying Capabilities" 17 HREF="x282.htm"></HEAD 18 ><BODY 19 CLASS="CHAPTER" 20 BGCOLOR="#FFFFFF" 21 TEXT="#000000" 22 LINK="#0000FF" 23 VLINK="#840084" 24 ALINK="#0000FF" 25 ><DIV 26 CLASS="NAVHEADER" 27 ><TABLE 28 SUMMARY="Header navigation table" 29 WIDTH="100%" 30 BORDER="0" 31 CELLPADDING="0" 32 CELLSPACING="0" 33 ><TR 34 ><TH 35 COLSPAN="3" 36 ALIGN="center" 37 >Video for Linux Two API Specification: Revision 0.24</TH 38 ></TR 39 ><TR 40 ><TD 41 WIDTH="10%" 42 ALIGN="left" 43 VALIGN="bottom" 44 ><A 45 HREF="f163.htm" 46 ACCESSKEY="P" 47 >Prev</A 48 ></TD 49 ><TD 50 WIDTH="80%" 51 ALIGN="center" 52 VALIGN="bottom" 53 ></TD 54 ><TD 55 WIDTH="10%" 56 ALIGN="right" 57 VALIGN="bottom" 58 ><A 59 HREF="x282.htm" 60 ACCESSKEY="N" 61 >Next</A 62 ></TD 63 ></TR 64 ></TABLE 65 ><HR 66 ALIGN="LEFT" 67 WIDTH="100%"></DIV 68 ><DIV 69 CLASS="CHAPTER" 70 ><H1 71 ><A 72 NAME="COMMON" 73 ></A 74 >Chapter 1. Common API Elements</H1 75 ><DIV 76 CLASS="TOC" 77 ><DL 78 ><DT 79 ><B 80 >Table of Contents</B 81 ></DT 82 ><DT 83 >1.1. <A 84 HREF="c174.htm#OPEN" 85 >Opening and Closing Devices</A 86 ></DT 87 ><DT 88 >1.2. <A 89 HREF="x282.htm" 90 >Querying Capabilities</A 91 ></DT 92 ><DT 93 >1.3. <A 94 HREF="x294.htm" 95 >Application Priority</A 96 ></DT 97 ><DT 98 >1.4. <A 99 HREF="x309.htm" 100 >Video Inputs and Outputs</A 101 ></DT 102 ><DT 103 >1.5. <A 104 HREF="x341.htm" 105 >Audio Inputs and Outputs</A 106 ></DT 107 ><DT 108 >1.6. <A 109 HREF="x394.htm" 110 >Tuners and Modulators</A 111 ></DT 112 ><DT 113 >1.7. <A 114 HREF="x448.htm" 115 >Video Standards</A 116 ></DT 117 ><DT 118 >1.8. <A 119 HREF="x542.htm" 120 >User Controls</A 121 ></DT 122 ><DT 123 >1.9. <A 124 HREF="x802.htm" 125 >Extended Controls</A 126 ></DT 127 ><DT 128 >1.10. <A 129 HREF="x1859.htm" 130 >Data Formats</A 131 ></DT 132 ><DT 133 >1.11. <A 134 HREF="x1904.htm" 135 >Image Cropping, Insertion and Scaling</A 136 ></DT 137 ><DT 138 >1.12. <A 139 HREF="x2009.htm" 140 >Streaming Parameters</A 141 ></DT 142 ></DL 143 ></DIV 144 ><P 145 >Programming a V4L2 device consists of these 146 steps:</P 147 ><P 148 ></P 149 ><UL 150 ><LI 151 ><P 152 >Opening the device</P 153 ></LI 154 ><LI 155 ><P 156 >Changing device properties, selecting a video and audio 157 input, video standard, picture brightness a. o.</P 158 ></LI 159 ><LI 160 ><P 161 >Negotiating a data format</P 162 ></LI 163 ><LI 164 ><P 165 >Negotiating an input/output method</P 166 ></LI 167 ><LI 168 ><P 169 >The actual input/output loop</P 170 ></LI 171 ><LI 172 ><P 173 >Closing the device</P 174 ></LI 175 ></UL 176 ><P 177 >In practice most steps are optional and can be executed out of 178 order. It depends on the V4L2 device type, you can read about the 179 details in <A 180 HREF="c6488.htm" 181 >Chapter 4</A 182 >. In this chapter we will discuss 183 the basic concepts applicable to all devices.</P 184 ><DIV 185 CLASS="SECTION" 186 ><H1 187 CLASS="SECTION" 188 ><A 189 NAME="OPEN" 190 >1.1. Opening and Closing Devices</A 191 ></H1 192 ><DIV 193 CLASS="SECTION" 194 ><H2 195 CLASS="SECTION" 196 ><A 197 NAME="AEN194" 198 >1.1.1. Device Naming</A 199 ></H2 200 ><P 201 >V4L2 drivers are implemented as kernel modules, loaded 202 manually by the system administrator or automatically when a device is 203 first opened. The driver modules plug into the "videodev" kernel 204 module. It provides helper functions and a common application 205 interface specified in this document.</P 206 ><P 207 >Each driver thus loaded registers one or more device nodes 208 with major number 81 and a minor number between 0 and 255. Assigning 209 minor numbers to V4L2 devices is entirely up to the system administrator, 210 this is primarily intended to solve conflicts between devices.<A 211 NAME="AEN198" 212 HREF="c174.htm#FTN.AEN198" 213 ><SPAN 214 CLASS="footnote" 215 >[1]</SPAN 216 ></A 217 > The module options to select minor numbers are named 218 after the device special file with a "_nr" suffix. For example "video_nr" 219 for <TT 220 CLASS="FILENAME" 221 >/dev/video</TT 222 > video capture devices. The number is 223 an offset to the base minor number associated with the device type. 224 <A 225 NAME="AEN201" 226 HREF="c174.htm#FTN.AEN201" 227 ><SPAN 228 CLASS="footnote" 229 >[2]</SPAN 230 ></A 231 > When the driver supports multiple devices of the same 232 type more than one minor number can be assigned, separated by commas: 233 <DIV 234 CLASS="INFORMALEXAMPLE" 235 ><P 236 ></P 237 ><A 238 NAME="AEN203" 239 ></A 240 ><PRE 241 CLASS="SCREEN" 242 >> insmod mydriver.o video_nr=0,1 radio_nr=0,1</PRE 243 ><P 244 ></P 245 ></DIV 246 ></P 247 ><P 248 >In <TT 249 CLASS="FILENAME" 250 >/etc/modules.conf</TT 251 > this may be 252 written as: <DIV 253 CLASS="INFORMALEXAMPLE" 254 ><P 255 ></P 256 ><A 257 NAME="AEN207" 258 ></A 259 ><PRE 260 CLASS="SCREEN" 261 >alias char-major-81-0 mydriver 262 alias char-major-81-1 mydriver 263 alias char-major-81-64 mydriver <A 264 NAME="ALIAS" 265 ><IMG 266 SRC="../images/callouts/1.gif" 267 HSPACE="0" 268 VSPACE="0" 269 BORDER="0" 270 ALT="(1)"></A 271 > 272 options mydriver video_nr=0,1 radio_nr=0,1 <A 273 NAME="OPTIONS" 274 ><IMG 275 SRC="../images/callouts/2.gif" 276 HSPACE="0" 277 VSPACE="0" 278 BORDER="0" 279 ALT="(2)"></A 280 > 281 </PRE 282 ><DIV 283 CLASS="CALLOUTLIST" 284 ><DL 285 COMPACT="COMPACT" 286 ><DT 287 ><A 288 HREF="c174.htm#ALIAS" 289 ><IMG 290 SRC="../images/callouts/1.gif" 291 HSPACE="0" 292 VSPACE="0" 293 BORDER="0" 294 ALT="(1)"></A 295 ></DT 296 ><DD 297 >When an application attempts to open a device 298 special file with major number 81 and minor number 0, 1, or 64, load 299 "mydriver" (and the "videodev" module it depends upon).</DD 300 ><DT 301 ><A 302 HREF="c174.htm#OPTIONS" 303 ><IMG 304 SRC="../images/callouts/2.gif" 305 HSPACE="0" 306 VSPACE="0" 307 BORDER="0" 308 ALT="(2)"></A 309 ></DT 310 ><DD 311 >Register the first two video capture devices with 312 minor number 0 and 1 (base number is 0), the first two radio device 313 with minor number 64 and 65 (base 64).</DD 314 ></DL 315 ></DIV 316 ><P 317 ></P 318 ></DIV 319 > When no minor number is given as module 320 option the driver supplies a default. <A 321 HREF="c6488.htm" 322 >Chapter 4</A 323 > 324 recommends the base minor numbers to be used for the various device 325 types. Obviously minor numbers must be unique. When the number is 326 already in use the <SPAN 327 CLASS="emphasis" 328 ><I 329 CLASS="EMPHASIS" 330 >offending device</I 331 ></SPAN 332 > will not be 333 registered. </P 334 ><P 335 >By convention system administrators create various 336 character device special files with these major and minor numbers in 337 the <TT 338 CLASS="FILENAME" 339 >/dev</TT 340 > directory. The names recomended for the 341 different V4L2 device types are listed in <A 342 HREF="c6488.htm" 343 >Chapter 4</A 344 >.</P 345 ><P 346 >The creation of character special files (with 347 <SPAN 348 CLASS="APPLICATION" 349 >mknod</SPAN 350 >) is a privileged operation and 351 devices cannot be opened by major and minor number. That means 352 applications cannot <SPAN 353 CLASS="emphasis" 354 ><I 355 CLASS="EMPHASIS" 356 >reliable</I 357 ></SPAN 358 > scan for loaded or 359 installed drivers. The user must enter a device name, or the 360 application can try the conventional device names.</P 361 ><P 362 >Under the device filesystem (devfs) the minor number 363 options are ignored. V4L2 drivers (or by proxy the "videodev" module) 364 automatically create the required device files in the 365 <TT 366 CLASS="FILENAME" 367 >/dev/v4l</TT 368 > directory using the conventional device 369 names above.</P 370 ></DIV 371 ><DIV 372 CLASS="SECTION" 373 ><H2 374 CLASS="SECTION" 375 ><A 376 NAME="RELATED" 377 >1.1.2. Related Devices</A 378 ></H2 379 ><P 380 >Devices can support several related functions. For example 381 video capturing, video overlay and VBI capturing are related because 382 these functions share, amongst other, the same video input and tuner 383 frequency. V4L and earlier versions of V4L2 used the same device name 384 and minor number for video capturing and overlay, but different ones 385 for VBI. Experience showed this approach has several problems<A 386 NAME="AEN229" 387 HREF="c174.htm#FTN.AEN229" 388 ><SPAN 389 CLASS="footnote" 390 >[3]</SPAN 391 ></A 392 >, and to make things worse the V4L videodev module 393 used to prohibit multiple opens of a device.</P 394 ><P 395 >As a remedy the present version of the V4L2 API relaxed the 396 concept of device types with specific names and minor numbers. For 397 compatibility with old applications drivers must still register different 398 minor numbers to assign a default function to the device. But if related 399 functions are supported by the driver they must be available under all 400 registered minor numbers. The desired function can be selected after 401 opening the device as described in <A 402 HREF="c6488.htm" 403 >Chapter 4</A 404 >.</P 405 ><P 406 >Imagine a driver supporting video capturing, video 407 overlay, raw VBI capturing, and FM radio reception. It registers three 408 devices with minor number 0, 64 and 224 (this numbering scheme is 409 inherited from the V4L API). Regardless if 410 <TT 411 CLASS="FILENAME" 412 >/dev/video</TT 413 > (81, 0) or 414 <TT 415 CLASS="FILENAME" 416 >/dev/vbi</TT 417 > (81, 224) is opened the application can 418 select any one of the video capturing, overlay or VBI capturing 419 functions. Without programming (e. g. reading from the device 420 with <SPAN 421 CLASS="APPLICATION" 422 >dd</SPAN 423 > or <SPAN 424 CLASS="APPLICATION" 425 >cat</SPAN 426 >) 427 <TT 428 CLASS="FILENAME" 429 >/dev/video</TT 430 > captures video images, while 431 <TT 432 CLASS="FILENAME" 433 >/dev/vbi</TT 434 > captures raw VBI data. 435 <TT 436 CLASS="FILENAME" 437 >/dev/radio</TT 438 > (81, 64) is invariable a radio device, 439 unrelated to the video functions. Being unrelated does not imply the 440 devices can be used at the same time, however. The <A 441 HREF="r14090.htm" 442 ><CODE 443 CLASS="FUNCTION" 444 >open()</CODE 445 ></A 446 > 447 function may very well return an <SPAN 448 CLASS="ERRORCODE" 449 >EBUSY</SPAN 450 > error code.</P 451 ><P 452 >Besides video input or output the hardware may also 453 support audio sampling or playback. If so, these functions are 454 implemented as OSS or ALSA PCM devices and eventually OSS or ALSA 455 audio mixer. The V4L2 API makes no provisions yet to find these 456 related devices. If you have an idea please write to the Video4Linux 457 mailing list: <A 458 HREF="https://listman.redhat.com/mailman/listinfo/video4linux-list" 459 TARGET="_top" 460 >https://listman.redhat.com/mailman/listinfo/video4linux-list</A 461 >.</P 462 ></DIV 463 ><DIV 464 CLASS="SECTION" 465 ><H2 466 CLASS="SECTION" 467 ><A 468 NAME="AEN249" 469 >1.1.3. Multiple Opens</A 470 ></H2 471 ><P 472 >In general, V4L2 devices can be opened more than once. 473 When this is supported by the driver, users can for example start a 474 "panel" application to change controls like brightness or audio 475 volume, while another application captures video and audio. In other words, panel 476 applications are comparable to an OSS or ALSA audio mixer application. 477 When a device supports multiple functions like capturing and overlay 478 <SPAN 479 CLASS="emphasis" 480 ><I 481 CLASS="EMPHASIS" 482 >simultaneously</I 483 ></SPAN 484 >, multiple opens allow concurrent 485 use of the device by forked processes or specialized applications.</P 486 ><P 487 >Multiple opens are optional, although drivers should 488 permit at least concurrent accesses without data exchange, i. e. panel 489 applications. This implies <A 490 HREF="r14090.htm" 491 ><CODE 492 CLASS="FUNCTION" 493 >open()</CODE 494 ></A 495 > can return an <SPAN 496 CLASS="ERRORCODE" 497 >EBUSY</SPAN 498 > error code when the 499 device is already in use, as well as <A 500 HREF="r7667.htm" 501 ><CODE 502 CLASS="FUNCTION" 503 >ioctl()</CODE 504 ></A 505 > functions initiating 506 data exchange (namely the <A 507 HREF="r10944.htm" 508 ><CODE 509 CLASS="CONSTANT" 510 >VIDIOC_S_FMT</CODE 511 ></A 512 > ioctl), and the <A 513 HREF="r14264.htm" 514 ><CODE 515 CLASS="FUNCTION" 516 >read()</CODE 517 ></A 518 > 519 and <A 520 HREF="r14496.htm" 521 ><CODE 522 CLASS="FUNCTION" 523 >write()</CODE 524 ></A 525 > functions.</P 526 ><P 527 >Mere opening a V4L2 device does not grant exclusive 528 access.<A 529 NAME="AEN266" 530 HREF="c174.htm#FTN.AEN266" 531 ><SPAN 532 CLASS="footnote" 533 >[4]</SPAN 534 ></A 535 > Initiating data exchange however assigns the right 536 to read or write the requested type of data, and to change related 537 properties, to this file descriptor. Applications can request 538 additional access privileges using the priority mechanism described in 539 <A 540 HREF="x294.htm" 541 >Section 1.3</A 542 >.</P 543 ></DIV 544 ><DIV 545 CLASS="SECTION" 546 ><H2 547 CLASS="SECTION" 548 ><A 549 NAME="AEN270" 550 >1.1.4. Shared Data Streams</A 551 ></H2 552 ><P 553 >V4L2 drivers should not support multiple applications 554 reading or writing the same data stream on a device by copying 555 buffers, time multiplexing or similar means. This is better handled by 556 a proxy application in user space. When the driver supports stream 557 sharing anyway it must be implemented transparently. The V4L2 API does 558 not specify how conflicts are solved. </P 559 ></DIV 560 ><DIV 561 CLASS="SECTION" 562 ><H2 563 CLASS="SECTION" 564 ><A 565 NAME="AEN273" 566 >1.1.5. Functions</A 567 ></H2 568 ><P 569 >To open and close V4L2 devices applications use the 570 <A 571 HREF="r14090.htm" 572 ><CODE 573 CLASS="FUNCTION" 574 >open()</CODE 575 ></A 576 > and <A 577 HREF="r7626.htm" 578 ><CODE 579 CLASS="FUNCTION" 580 >close()</CODE 581 ></A 582 > function, respectively. Devices are 583 programmed using the <A 584 HREF="r7667.htm" 585 ><CODE 586 CLASS="FUNCTION" 587 >ioctl()</CODE 588 ></A 589 > function as explained in the 590 following sections.</P 591 ></DIV 592 ></DIV 593 ></DIV 594 ><H3 595 CLASS="FOOTNOTES" 596 >Notes</H3 597 ><TABLE 598 BORDER="0" 599 CLASS="FOOTNOTES" 600 WIDTH="100%" 601 ><TR 602 ><TD 603 ALIGN="LEFT" 604 VALIGN="TOP" 605 WIDTH="5%" 606 ><A 607 NAME="FTN.AEN198" 608 HREF="c174.htm#AEN198" 609 ><SPAN 610 CLASS="footnote" 611 >[1]</SPAN 612 ></A 613 ></TD 614 ><TD 615 ALIGN="LEFT" 616 VALIGN="TOP" 617 WIDTH="95%" 618 ><P 619 >Access permissions are associated with character 620 device special files, hence we must ensure device numbers cannot 621 change with the module load order. To this end minor numbers are no 622 longer automatically assigned by the "videodev" module as in V4L but 623 requested by the driver. The defaults will suffice for most people 624 unless two drivers compete for the same minor numbers.</P 625 ></TD 626 ></TR 627 ><TR 628 ><TD 629 ALIGN="LEFT" 630 VALIGN="TOP" 631 WIDTH="5%" 632 ><A 633 NAME="FTN.AEN201" 634 HREF="c174.htm#AEN201" 635 ><SPAN 636 CLASS="footnote" 637 >[2]</SPAN 638 ></A 639 ></TD 640 ><TD 641 ALIGN="LEFT" 642 VALIGN="TOP" 643 WIDTH="95%" 644 ><P 645 >In earlier versions of the V4L2 API the module options 646 where named after the device special file with a "unit_" prefix, expressing 647 the minor number itself, not an offset. Rationale for this change is unknown. 648 Lastly the naming and semantics are just a convention among driver writers, 649 the point to note is that minor numbers are not supposed to be hardcoded 650 into drivers.</P 651 ></TD 652 ></TR 653 ><TR 654 ><TD 655 ALIGN="LEFT" 656 VALIGN="TOP" 657 WIDTH="5%" 658 ><A 659 NAME="FTN.AEN229" 660 HREF="c174.htm#AEN229" 661 ><SPAN 662 CLASS="footnote" 663 >[3]</SPAN 664 ></A 665 ></TD 666 ><TD 667 ALIGN="LEFT" 668 VALIGN="TOP" 669 WIDTH="95%" 670 ><P 671 >Given a device file name one cannot reliable find 672 related devices. For once names are arbitrary and in a system with 673 multiple devices, where only some support VBI capturing, a 674 <TT 675 CLASS="FILENAME" 676 >/dev/video2</TT 677 > is not necessarily related to 678 <TT 679 CLASS="FILENAME" 680 >/dev/vbi2</TT 681 >. The V4L 682 <CODE 683 CLASS="CONSTANT" 684 >VIDIOCGUNIT</CODE 685 > ioctl would require a search for a 686 device file with a particular major and minor number.</P 687 ></TD 688 ></TR 689 ><TR 690 ><TD 691 ALIGN="LEFT" 692 VALIGN="TOP" 693 WIDTH="5%" 694 ><A 695 NAME="FTN.AEN266" 696 HREF="c174.htm#AEN266" 697 ><SPAN 698 CLASS="footnote" 699 >[4]</SPAN 700 ></A 701 ></TD 702 ><TD 703 ALIGN="LEFT" 704 VALIGN="TOP" 705 WIDTH="95%" 706 ><P 707 >Drivers could recognize the 708 <CODE 709 CLASS="CONSTANT" 710 >O_EXCL</CODE 711 > open flag. Presently this is not required, 712 so applications cannot know if it really works.</P 713 ></TD 714 ></TR 715 ></TABLE 716 ><DIV 717 CLASS="NAVFOOTER" 718 ><HR 719 ALIGN="LEFT" 720 WIDTH="100%"><TABLE 721 SUMMARY="Footer navigation table" 722 WIDTH="100%" 723 BORDER="0" 724 CELLPADDING="0" 725 CELLSPACING="0" 726 ><TR 727 ><TD 728 WIDTH="33%" 729 ALIGN="left" 730 VALIGN="top" 731 ><A 732 HREF="f163.htm" 733 ACCESSKEY="P" 734 >Prev</A 735 ></TD 736 ><TD 737 WIDTH="34%" 738 ALIGN="center" 739 VALIGN="top" 740 ><A 741 HREF="book1.htm" 742 ACCESSKEY="H" 743 >Home</A 744 ></TD 745 ><TD 746 WIDTH="33%" 747 ALIGN="right" 748 VALIGN="top" 749 ><A 750 HREF="x282.htm" 751 ACCESSKEY="N" 752 >Next</A 753 ></TD 754 ></TR 755 ><TR 756 ><TD 757 WIDTH="33%" 758 ALIGN="left" 759 VALIGN="top" 760 >Introduction</TD 761 ><TD 762 WIDTH="34%" 763 ALIGN="center" 764 VALIGN="top" 765 > </TD 766 ><TD 767 WIDTH="33%" 768 ALIGN="right" 769 VALIGN="top" 770 >Querying Capabilities</TD 771 ></TR 772 ></TABLE 773 ></DIV 774 ></BODY 775 ></HTML 776 > 777