FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavdevice/alsa_enc.c
Date: 2024-10-04 17:46:48
Exec Total Coverage
Lines: 0 60 0.0%
Functions: 0 5 0.0%
Branches: 0 26 0.0%

Line Branch Exec Source
1 /*
2 * ALSA input and output
3 * Copyright (c) 2007 Luca Abeni ( lucabe72 email it )
4 * Copyright (c) 2007 Benoit Fouet ( benoit fouet free fr )
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * ALSA input and output: output
26 * @author Luca Abeni ( lucabe72 email it )
27 * @author Benoit Fouet ( benoit fouet free fr )
28 *
29 * This avdevice encoder can play audio to an ALSA (Advanced Linux
30 * Sound Architecture) device.
31 *
32 * The filename parameter is the name of an ALSA PCM device capable of
33 * capture, for example "default" or "plughw:1"; see the ALSA documentation
34 * for naming conventions. The empty string is equivalent to "default".
35 *
36 * The playback period is set to the lower value available for the device,
37 * which gives a low latency suitable for real-time playback.
38 */
39
40 #include <alsa/asoundlib.h>
41
42 #include "libavutil/frame.h"
43 #include "libavutil/internal.h"
44 #include "libavutil/time.h"
45
46
47 #include "libavformat/internal.h"
48 #include "libavformat/mux.h"
49 #include "avdevice.h"
50 #include "alsa.h"
51
52 static av_cold int audio_write_header(AVFormatContext *s1)
53 {
54 AlsaData *s = s1->priv_data;
55 AVStream *st = NULL;
56 unsigned int sample_rate;
57 enum AVCodecID codec_id;
58 int res;
59
60 if (s1->nb_streams != 1 || s1->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
61 av_log(s1, AV_LOG_ERROR, "Only a single audio stream is supported.\n");
62 return AVERROR(EINVAL);
63 }
64 st = s1->streams[0];
65
66 sample_rate = st->codecpar->sample_rate;
67 codec_id = st->codecpar->codec_id;
68 res = ff_alsa_open(s1, SND_PCM_STREAM_PLAYBACK, &sample_rate,
69 st->codecpar->ch_layout.nb_channels, &codec_id);
70 if (sample_rate != st->codecpar->sample_rate) {
71 av_log(s1, AV_LOG_ERROR,
72 "sample rate %d not available, nearest is %d\n",
73 st->codecpar->sample_rate, sample_rate);
74 goto fail;
75 }
76 avpriv_set_pts_info(st, 64, 1, sample_rate);
77
78 return res;
79
80 fail:
81 snd_pcm_close(s->h);
82 return AVERROR(EIO);
83 }
84
85 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
86 {
87 AlsaData *s = s1->priv_data;
88 int res;
89 int size = pkt->size;
90 const uint8_t *buf = pkt->data;
91
92 size /= s->frame_size;
93 if (pkt->dts != AV_NOPTS_VALUE)
94 s->timestamp = pkt->dts;
95 s->timestamp += pkt->duration ? pkt->duration : size;
96
97 if (s->reorder_func) {
98 if (size > s->reorder_buf_size)
99 if (ff_alsa_extend_reorder_buf(s, size))
100 return AVERROR(ENOMEM);
101 s->reorder_func(buf, s->reorder_buf, size);
102 buf = s->reorder_buf;
103 }
104 while ((res = snd_pcm_writei(s->h, buf, size)) < 0) {
105 if (res == -EAGAIN) {
106
107 return AVERROR(EAGAIN);
108 }
109
110 if (ff_alsa_xrun_recover(s1, res) < 0) {
111 av_log(s1, AV_LOG_ERROR, "ALSA write error: %s\n",
112 snd_strerror(res));
113
114 return AVERROR(EIO);
115 }
116 }
117
118 return 0;
119 }
120
121 static int audio_write_frame(AVFormatContext *s1, int stream_index,
122 AVFrame **frame, unsigned flags)
123 {
124 AlsaData *s = s1->priv_data;
125 AVPacket pkt;
126
127 /* ff_alsa_open() should have accepted only supported formats */
128 if ((flags & AV_WRITE_UNCODED_FRAME_QUERY))
129 return av_sample_fmt_is_planar(s1->streams[stream_index]->codecpar->format) ?
130 AVERROR(EINVAL) : 0;
131 /* set only used fields */
132 pkt.data = (*frame)->data[0];
133 pkt.size = (*frame)->nb_samples * s->frame_size;
134 pkt.dts = (*frame)->pkt_dts;
135 pkt.duration = (*frame)->duration;
136 return audio_write_packet(s1, &pkt);
137 }
138
139 static void
140 audio_get_output_timestamp(AVFormatContext *s1, int stream,
141 int64_t *dts, int64_t *wall)
142 {
143 AlsaData *s = s1->priv_data;
144 snd_pcm_sframes_t delay = 0;
145 *wall = av_gettime();
146 snd_pcm_delay(s->h, &delay);
147 *dts = s->timestamp - delay;
148 }
149
150 static int audio_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list)
151 {
152 return ff_alsa_get_device_list(device_list, SND_PCM_STREAM_PLAYBACK);
153 }
154
155 static const AVClass alsa_muxer_class = {
156 .class_name = "ALSA outdev",
157 .item_name = av_default_item_name,
158 .version = LIBAVUTIL_VERSION_INT,
159 .category = AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT,
160 };
161
162 const FFOutputFormat ff_alsa_muxer = {
163 .p.name = "alsa",
164 .p.long_name = NULL_IF_CONFIG_SMALL("ALSA audio output"),
165 .priv_data_size = sizeof(AlsaData),
166 .p.audio_codec = DEFAULT_CODEC_ID,
167 .p.video_codec = AV_CODEC_ID_NONE,
168 .write_header = audio_write_header,
169 .write_packet = audio_write_packet,
170 .write_trailer = ff_alsa_close,
171 .write_uncoded_frame = audio_write_frame,
172 .get_device_list = audio_get_device_list,
173 .get_output_timestamp = audio_get_output_timestamp,
174 .p.flags = AVFMT_NOFILE,
175 .p.priv_class = &alsa_muxer_class,
176 };
177