xref: /OK3568_Linux_fs/kernel/Documentation/userspace-api/media/v4l/audio.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun.. _audio:
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun************************
6*4882a593SmuzhiyunAudio Inputs and Outputs
7*4882a593Smuzhiyun************************
8*4882a593Smuzhiyun
9*4882a593SmuzhiyunAudio inputs and outputs are physical connectors of a device. Video
10*4882a593Smuzhiyuncapture devices have inputs, output devices have outputs, zero or more
11*4882a593Smuzhiyuneach. Radio devices have no audio inputs or outputs. They have exactly
12*4882a593Smuzhiyunone tuner which in fact *is* an audio source, but this API associates
13*4882a593Smuzhiyuntuners with video inputs or outputs only, and radio devices have none of
14*4882a593Smuzhiyunthese. [#f1]_ A connector on a TV card to loop back the received audio
15*4882a593Smuzhiyunsignal to a sound card is not considered an audio output.
16*4882a593Smuzhiyun
17*4882a593SmuzhiyunAudio and video inputs and outputs are associated. Selecting a video
18*4882a593Smuzhiyunsource also selects an audio source. This is most evident when the video
19*4882a593Smuzhiyunand audio source is a tuner. Further audio connectors can combine with
20*4882a593Smuzhiyunmore than one video input or output. Assumed two composite video inputs
21*4882a593Smuzhiyunand two audio inputs exist, there may be up to four valid combinations.
22*4882a593SmuzhiyunThe relation of video and audio connectors is defined in the
23*4882a593Smuzhiyun``audioset`` field of the respective struct
24*4882a593Smuzhiyun:c:type:`v4l2_input` or struct
25*4882a593Smuzhiyun:c:type:`v4l2_output`, where each bit represents the index
26*4882a593Smuzhiyunnumber, starting at zero, of one audio input or output.
27*4882a593Smuzhiyun
28*4882a593SmuzhiyunTo learn about the number and attributes of the available inputs and
29*4882a593Smuzhiyunoutputs applications can enumerate them with the
30*4882a593Smuzhiyun:ref:`VIDIOC_ENUMAUDIO` and
31*4882a593Smuzhiyun:ref:`VIDIOC_ENUMAUDOUT <VIDIOC_ENUMAUDOUT>` ioctl, respectively.
32*4882a593SmuzhiyunThe struct :c:type:`v4l2_audio` returned by the
33*4882a593Smuzhiyun:ref:`VIDIOC_ENUMAUDIO` ioctl also contains signal
34*4882a593Smuzhiyunstatus information applicable when the current audio input is queried.
35*4882a593Smuzhiyun
36*4882a593SmuzhiyunThe :ref:`VIDIOC_G_AUDIO <VIDIOC_G_AUDIO>` and
37*4882a593Smuzhiyun:ref:`VIDIOC_G_AUDOUT <VIDIOC_G_AUDOUT>` ioctls report the current
38*4882a593Smuzhiyunaudio input and output, respectively.
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun.. note::
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun   Note that, unlike :ref:`VIDIOC_G_INPUT <VIDIOC_G_INPUT>` and
43*4882a593Smuzhiyun   :ref:`VIDIOC_G_OUTPUT <VIDIOC_G_OUTPUT>` these ioctls return a
44*4882a593Smuzhiyun   structure as :ref:`VIDIOC_ENUMAUDIO` and
45*4882a593Smuzhiyun   :ref:`VIDIOC_ENUMAUDOUT <VIDIOC_ENUMAUDOUT>` do, not just an index.
46*4882a593Smuzhiyun
47*4882a593SmuzhiyunTo select an audio input and change its properties applications call the
48*4882a593Smuzhiyun:ref:`VIDIOC_S_AUDIO <VIDIOC_G_AUDIO>` ioctl. To select an audio
49*4882a593Smuzhiyunoutput (which presently has no changeable properties) applications call
50*4882a593Smuzhiyunthe :ref:`VIDIOC_S_AUDOUT <VIDIOC_G_AUDOUT>` ioctl.
51*4882a593Smuzhiyun
52*4882a593SmuzhiyunDrivers must implement all audio input ioctls when the device has
53*4882a593Smuzhiyunmultiple selectable audio inputs, all audio output ioctls when the
54*4882a593Smuzhiyundevice has multiple selectable audio outputs. When the device has any
55*4882a593Smuzhiyunaudio inputs or outputs the driver must set the ``V4L2_CAP_AUDIO`` flag
56*4882a593Smuzhiyunin the struct :c:type:`v4l2_capability` returned by
57*4882a593Smuzhiyunthe :ref:`VIDIOC_QUERYCAP` ioctl.
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun
60*4882a593SmuzhiyunExample: Information about the current audio input
61*4882a593Smuzhiyun==================================================
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun.. code-block:: c
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun    struct v4l2_audio audio;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun    memset(&audio, 0, sizeof(audio));
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun    if (-1 == ioctl(fd, VIDIOC_G_AUDIO, &audio)) {
70*4882a593Smuzhiyun	perror("VIDIOC_G_AUDIO");
71*4882a593Smuzhiyun	exit(EXIT_FAILURE);
72*4882a593Smuzhiyun    }
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun    printf("Current input: %s\\n", audio.name);
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun
77*4882a593SmuzhiyunExample: Switching to the first audio input
78*4882a593Smuzhiyun===========================================
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun.. code-block:: c
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun    struct v4l2_audio audio;
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun    memset(&audio, 0, sizeof(audio)); /* clear audio.mode, audio.reserved */
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun    audio.index = 0;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun    if (-1 == ioctl(fd, VIDIOC_S_AUDIO, &audio)) {
89*4882a593Smuzhiyun	perror("VIDIOC_S_AUDIO");
90*4882a593Smuzhiyun	exit(EXIT_FAILURE);
91*4882a593Smuzhiyun    }
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun.. [#f1]
94*4882a593Smuzhiyun   Actually struct :c:type:`v4l2_audio` ought to have a
95*4882a593Smuzhiyun   ``tuner`` field like struct :c:type:`v4l2_input`, not
96*4882a593Smuzhiyun   only making the API more consistent but also permitting radio devices
97*4882a593Smuzhiyun   with multiple tuners.
98