162306a36Sopenharmony_ci.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci.. _audio:
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci************************
662306a36Sopenharmony_ciAudio Inputs and Outputs
762306a36Sopenharmony_ci************************
862306a36Sopenharmony_ci
962306a36Sopenharmony_ciAudio inputs and outputs are physical connectors of a device. Video
1062306a36Sopenharmony_cicapture devices have inputs, output devices have outputs, zero or more
1162306a36Sopenharmony_cieach. Radio devices have no audio inputs or outputs. They have exactly
1262306a36Sopenharmony_cione tuner which in fact *is* an audio source, but this API associates
1362306a36Sopenharmony_cituners with video inputs or outputs only, and radio devices have none of
1462306a36Sopenharmony_cithese. [#f1]_ A connector on a TV card to loop back the received audio
1562306a36Sopenharmony_cisignal to a sound card is not considered an audio output.
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ciAudio and video inputs and outputs are associated. Selecting a video
1862306a36Sopenharmony_cisource also selects an audio source. This is most evident when the video
1962306a36Sopenharmony_ciand audio source is a tuner. Further audio connectors can combine with
2062306a36Sopenharmony_cimore than one video input or output. Assumed two composite video inputs
2162306a36Sopenharmony_ciand two audio inputs exist, there may be up to four valid combinations.
2262306a36Sopenharmony_ciThe relation of video and audio connectors is defined in the
2362306a36Sopenharmony_ci``audioset`` field of the respective struct
2462306a36Sopenharmony_ci:c:type:`v4l2_input` or struct
2562306a36Sopenharmony_ci:c:type:`v4l2_output`, where each bit represents the index
2662306a36Sopenharmony_cinumber, starting at zero, of one audio input or output.
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ciTo learn about the number and attributes of the available inputs and
2962306a36Sopenharmony_cioutputs applications can enumerate them with the
3062306a36Sopenharmony_ci:ref:`VIDIOC_ENUMAUDIO` and
3162306a36Sopenharmony_ci:ref:`VIDIOC_ENUMAUDOUT <VIDIOC_ENUMAUDOUT>` ioctl, respectively.
3262306a36Sopenharmony_ciThe struct :c:type:`v4l2_audio` returned by the
3362306a36Sopenharmony_ci:ref:`VIDIOC_ENUMAUDIO` ioctl also contains signal
3462306a36Sopenharmony_cistatus information applicable when the current audio input is queried.
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ciThe :ref:`VIDIOC_G_AUDIO <VIDIOC_G_AUDIO>` and
3762306a36Sopenharmony_ci:ref:`VIDIOC_G_AUDOUT <VIDIOC_G_AUDOUT>` ioctls report the current
3862306a36Sopenharmony_ciaudio input and output, respectively.
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci.. note::
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci   Note that, unlike :ref:`VIDIOC_G_INPUT <VIDIOC_G_INPUT>` and
4362306a36Sopenharmony_ci   :ref:`VIDIOC_G_OUTPUT <VIDIOC_G_OUTPUT>` these ioctls return a
4462306a36Sopenharmony_ci   structure as :ref:`VIDIOC_ENUMAUDIO` and
4562306a36Sopenharmony_ci   :ref:`VIDIOC_ENUMAUDOUT <VIDIOC_ENUMAUDOUT>` do, not just an index.
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ciTo select an audio input and change its properties applications call the
4862306a36Sopenharmony_ci:ref:`VIDIOC_S_AUDIO <VIDIOC_G_AUDIO>` ioctl. To select an audio
4962306a36Sopenharmony_cioutput (which presently has no changeable properties) applications call
5062306a36Sopenharmony_cithe :ref:`VIDIOC_S_AUDOUT <VIDIOC_G_AUDOUT>` ioctl.
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ciDrivers must implement all audio input ioctls when the device has
5362306a36Sopenharmony_cimultiple selectable audio inputs, all audio output ioctls when the
5462306a36Sopenharmony_cidevice has multiple selectable audio outputs. When the device has any
5562306a36Sopenharmony_ciaudio inputs or outputs the driver must set the ``V4L2_CAP_AUDIO`` flag
5662306a36Sopenharmony_ciin the struct :c:type:`v4l2_capability` returned by
5762306a36Sopenharmony_cithe :ref:`VIDIOC_QUERYCAP` ioctl.
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ciExample: Information about the current audio input
6162306a36Sopenharmony_ci==================================================
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci.. code-block:: c
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci    struct v4l2_audio audio;
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci    memset(&audio, 0, sizeof(audio));
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci    if (-1 == ioctl(fd, VIDIOC_G_AUDIO, &audio)) {
7062306a36Sopenharmony_ci	perror("VIDIOC_G_AUDIO");
7162306a36Sopenharmony_ci	exit(EXIT_FAILURE);
7262306a36Sopenharmony_ci    }
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci    printf("Current input: %s\\n", audio.name);
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ciExample: Switching to the first audio input
7862306a36Sopenharmony_ci===========================================
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci.. code-block:: c
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci    struct v4l2_audio audio;
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci    memset(&audio, 0, sizeof(audio)); /* clear audio.mode, audio.reserved */
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci    audio.index = 0;
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci    if (-1 == ioctl(fd, VIDIOC_S_AUDIO, &audio)) {
8962306a36Sopenharmony_ci	perror("VIDIOC_S_AUDIO");
9062306a36Sopenharmony_ci	exit(EXIT_FAILURE);
9162306a36Sopenharmony_ci    }
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci.. [#f1]
9462306a36Sopenharmony_ci   Actually struct :c:type:`v4l2_audio` ought to have a
9562306a36Sopenharmony_ci   ``tuner`` field like struct :c:type:`v4l2_input`, not
9662306a36Sopenharmony_ci   only making the API more consistent but also permitting radio devices
9762306a36Sopenharmony_ci   with multiple tuners.
98