FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavdevice/alsa.c
Date: 2024-04-25 05:10:44
Exec Total Coverage
Lines: 0 206 0.0%
Functions: 0 19 0.0%
Branches: 0 151 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: common code
26 * @author Luca Abeni ( lucabe72 email it )
27 * @author Benoit Fouet ( benoit fouet free fr )
28 * @author Nicolas George ( nicolas george normalesup org )
29 */
30
31 #include "config_components.h"
32
33 #include <alsa/asoundlib.h>
34 #include "avdevice.h"
35 #include "libavutil/avassert.h"
36 #include "libavutil/channel_layout.h"
37 #include "libavutil/mem.h"
38
39 #include "alsa.h"
40
41 static av_cold snd_pcm_format_t codec_id_to_pcm_format(int codec_id)
42 {
43 switch(codec_id) {
44 case AV_CODEC_ID_PCM_F64LE: return SND_PCM_FORMAT_FLOAT64_LE;
45 case AV_CODEC_ID_PCM_F64BE: return SND_PCM_FORMAT_FLOAT64_BE;
46 case AV_CODEC_ID_PCM_F32LE: return SND_PCM_FORMAT_FLOAT_LE;
47 case AV_CODEC_ID_PCM_F32BE: return SND_PCM_FORMAT_FLOAT_BE;
48 case AV_CODEC_ID_PCM_S32LE: return SND_PCM_FORMAT_S32_LE;
49 case AV_CODEC_ID_PCM_S32BE: return SND_PCM_FORMAT_S32_BE;
50 case AV_CODEC_ID_PCM_U32LE: return SND_PCM_FORMAT_U32_LE;
51 case AV_CODEC_ID_PCM_U32BE: return SND_PCM_FORMAT_U32_BE;
52 case AV_CODEC_ID_PCM_S24LE: return SND_PCM_FORMAT_S24_3LE;
53 case AV_CODEC_ID_PCM_S24BE: return SND_PCM_FORMAT_S24_3BE;
54 case AV_CODEC_ID_PCM_U24LE: return SND_PCM_FORMAT_U24_3LE;
55 case AV_CODEC_ID_PCM_U24BE: return SND_PCM_FORMAT_U24_3BE;
56 case AV_CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE;
57 case AV_CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
58 case AV_CODEC_ID_PCM_U16LE: return SND_PCM_FORMAT_U16_LE;
59 case AV_CODEC_ID_PCM_U16BE: return SND_PCM_FORMAT_U16_BE;
60 case AV_CODEC_ID_PCM_S8: return SND_PCM_FORMAT_S8;
61 case AV_CODEC_ID_PCM_U8: return SND_PCM_FORMAT_U8;
62 case AV_CODEC_ID_PCM_MULAW: return SND_PCM_FORMAT_MU_LAW;
63 case AV_CODEC_ID_PCM_ALAW: return SND_PCM_FORMAT_A_LAW;
64 default: return SND_PCM_FORMAT_UNKNOWN;
65 }
66 }
67
68 #define MAKE_REORDER_FUNC(NAME, TYPE, CHANNELS, LAYOUT, MAP) \
69 static void alsa_reorder_ ## NAME ## _ ## LAYOUT(const void *in_v, \
70 void *out_v, \
71 int n) \
72 { \
73 const TYPE *in = in_v; \
74 TYPE *out = out_v; \
75 \
76 while (n-- > 0) { \
77 MAP \
78 in += CHANNELS; \
79 out += CHANNELS; \
80 } \
81 }
82
83 #define MAKE_REORDER_FUNCS(CHANNELS, LAYOUT, MAP) \
84 MAKE_REORDER_FUNC(int8, int8_t, CHANNELS, LAYOUT, MAP) \
85 MAKE_REORDER_FUNC(int16, int16_t, CHANNELS, LAYOUT, MAP) \
86 MAKE_REORDER_FUNC(int32, int32_t, CHANNELS, LAYOUT, MAP) \
87 MAKE_REORDER_FUNC(f32, float, CHANNELS, LAYOUT, MAP)
88
89 MAKE_REORDER_FUNCS(5, out_50, \
90 out[0] = in[0]; \
91 out[1] = in[1]; \
92 out[2] = in[3]; \
93 out[3] = in[4]; \
94 out[4] = in[2]; \
95 )
96
97 MAKE_REORDER_FUNCS(6, out_51, \
98 out[0] = in[0]; \
99 out[1] = in[1]; \
100 out[2] = in[4]; \
101 out[3] = in[5]; \
102 out[4] = in[2]; \
103 out[5] = in[3]; \
104 )
105
106 MAKE_REORDER_FUNCS(8, out_71, \
107 out[0] = in[0]; \
108 out[1] = in[1]; \
109 out[2] = in[4]; \
110 out[3] = in[5]; \
111 out[4] = in[2]; \
112 out[5] = in[3]; \
113 out[6] = in[6]; \
114 out[7] = in[7]; \
115 )
116
117 #define FORMAT_I8 0
118 #define FORMAT_I16 1
119 #define FORMAT_I32 2
120 #define FORMAT_F32 3
121
122 #define PICK_REORDER(layout)\
123 switch(format) {\
124 case FORMAT_I8: s->reorder_func = alsa_reorder_int8_out_ ##layout; break;\
125 case FORMAT_I16: s->reorder_func = alsa_reorder_int16_out_ ##layout; break;\
126 case FORMAT_I32: s->reorder_func = alsa_reorder_int32_out_ ##layout; break;\
127 case FORMAT_F32: s->reorder_func = alsa_reorder_f32_out_ ##layout; break;\
128 }
129
130 static av_cold int find_reorder_func(AlsaData *s, int codec_id, AVChannelLayout *layout, int out)
131 {
132 int format;
133
134 /* reordering input is not currently supported */
135 if (!out)
136 return AVERROR(ENOSYS);
137
138 /* reordering is not needed for QUAD or 2_2 layout */
139 if (!av_channel_layout_compare(layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_QUAD) ||
140 !av_channel_layout_compare(layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_2_2))
141 return 0;
142
143 switch (codec_id) {
144 case AV_CODEC_ID_PCM_S8:
145 case AV_CODEC_ID_PCM_U8:
146 case AV_CODEC_ID_PCM_ALAW:
147 case AV_CODEC_ID_PCM_MULAW: format = FORMAT_I8; break;
148 case AV_CODEC_ID_PCM_S16LE:
149 case AV_CODEC_ID_PCM_S16BE:
150 case AV_CODEC_ID_PCM_U16LE:
151 case AV_CODEC_ID_PCM_U16BE: format = FORMAT_I16; break;
152 case AV_CODEC_ID_PCM_S32LE:
153 case AV_CODEC_ID_PCM_S32BE:
154 case AV_CODEC_ID_PCM_U32LE:
155 case AV_CODEC_ID_PCM_U32BE: format = FORMAT_I32; break;
156 case AV_CODEC_ID_PCM_F32LE:
157 case AV_CODEC_ID_PCM_F32BE: format = FORMAT_F32; break;
158 default: return AVERROR(ENOSYS);
159 }
160
161 if (!av_channel_layout_compare(layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0_BACK) ||
162 !av_channel_layout_compare(layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0))
163 PICK_REORDER(50)
164 else if (!av_channel_layout_compare(layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1_BACK) ||
165 !av_channel_layout_compare(layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1))
166 PICK_REORDER(51)
167 else if (!av_channel_layout_compare(layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_7POINT1))
168 PICK_REORDER(71)
169
170 return s->reorder_func ? 0 : AVERROR(ENOSYS);
171 }
172
173 av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
174 unsigned int *sample_rate,
175 int channels, enum AVCodecID *codec_id)
176 {
177 AlsaData *s = ctx->priv_data;
178 AVChannelLayout *layout = &ctx->streams[0]->codecpar->ch_layout;
179 const char *audio_device;
180 int res, flags = 0;
181 snd_pcm_format_t format;
182 snd_pcm_t *h;
183 snd_pcm_hw_params_t *hw_params;
184 snd_pcm_uframes_t buffer_size, period_size;
185
186 if (ctx->url[0] == 0) audio_device = "default";
187 else audio_device = ctx->url;
188
189 if (*codec_id == AV_CODEC_ID_NONE)
190 *codec_id = DEFAULT_CODEC_ID;
191 format = codec_id_to_pcm_format(*codec_id);
192 if (format == SND_PCM_FORMAT_UNKNOWN) {
193 av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
194 return AVERROR(ENOSYS);
195 }
196 s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
197
198 if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
199 flags = SND_PCM_NONBLOCK;
200 }
201 res = snd_pcm_open(&h, audio_device, mode, flags);
202 if (res < 0) {
203 av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
204 audio_device, snd_strerror(res));
205 return AVERROR(EIO);
206 }
207
208 res = snd_pcm_hw_params_malloc(&hw_params);
209 if (res < 0) {
210 av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
211 snd_strerror(res));
212 goto fail1;
213 }
214
215 res = snd_pcm_hw_params_any(h, hw_params);
216 if (res < 0) {
217 av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
218 snd_strerror(res));
219 goto fail;
220 }
221
222 res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
223 if (res < 0) {
224 av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
225 snd_strerror(res));
226 goto fail;
227 }
228
229 res = snd_pcm_hw_params_set_format(h, hw_params, format);
230 if (res < 0) {
231 av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
232 *codec_id, format, snd_strerror(res));
233 goto fail;
234 }
235
236 res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
237 if (res < 0) {
238 av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
239 snd_strerror(res));
240 goto fail;
241 }
242
243 res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
244 if (res < 0) {
245 av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
246 channels, snd_strerror(res));
247 goto fail;
248 }
249
250 snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
251 buffer_size = FFMIN(buffer_size, ALSA_BUFFER_SIZE_MAX);
252 /* TODO: maybe use ctx->max_picture_buffer somehow */
253 res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
254 if (res < 0) {
255 av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
256 snd_strerror(res));
257 goto fail;
258 }
259
260 snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
261 if (!period_size)
262 period_size = buffer_size / 4;
263 res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
264 if (res < 0) {
265 av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
266 snd_strerror(res));
267 goto fail;
268 }
269 s->period_size = period_size;
270
271 res = snd_pcm_hw_params(h, hw_params);
272 if (res < 0) {
273 av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
274 snd_strerror(res));
275 goto fail;
276 }
277
278 snd_pcm_hw_params_free(hw_params);
279
280 if (channels > 2 && layout->order != AV_CHANNEL_ORDER_UNSPEC) {
281 if (find_reorder_func(s, *codec_id, layout, mode == SND_PCM_STREAM_PLAYBACK) < 0) {
282 char name[128];
283 av_channel_layout_describe(layout, name, sizeof(name));
284 av_log(ctx, AV_LOG_WARNING, "ALSA channel layout unknown or unimplemented for %s %s.\n",
285 name, mode == SND_PCM_STREAM_PLAYBACK ? "playback" : "capture");
286 }
287 if (s->reorder_func) {
288 s->reorder_buf_size = buffer_size;
289 s->reorder_buf = av_malloc_array(s->reorder_buf_size, s->frame_size);
290 if (!s->reorder_buf)
291 goto fail1;
292 }
293 }
294
295 s->pkt = av_packet_alloc();
296 if (!s->pkt)
297 goto fail1;
298
299 s->h = h;
300 return 0;
301
302 fail:
303 snd_pcm_hw_params_free(hw_params);
304 fail1:
305 snd_pcm_close(h);
306 return AVERROR(EIO);
307 }
308
309 av_cold int ff_alsa_close(AVFormatContext *s1)
310 {
311 AlsaData *s = s1->priv_data;
312
313 if (snd_pcm_stream(s->h) == SND_PCM_STREAM_PLAYBACK) {
314 snd_pcm_nonblock(s->h, 0);
315 snd_pcm_drain(s->h);
316 }
317 av_freep(&s->reorder_buf);
318 if (CONFIG_ALSA_INDEV)
319 ff_timefilter_destroy(s->timefilter);
320 snd_pcm_close(s->h);
321 av_packet_free(&s->pkt);
322 return 0;
323 }
324
325 int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
326 {
327 AlsaData *s = s1->priv_data;
328 snd_pcm_t *handle = s->h;
329
330 av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
331 if (err == -EPIPE) {
332 err = snd_pcm_prepare(handle);
333 if (err < 0) {
334 av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
335
336 return AVERROR(EIO);
337 }
338 } else if (err == -ESTRPIPE) {
339 av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");
340
341 return -1;
342 }
343 return err;
344 }
345
346 int ff_alsa_extend_reorder_buf(AlsaData *s, int min_size)
347 {
348 int size = s->reorder_buf_size;
349 void *r;
350
351 av_assert0(size != 0);
352 while (size < min_size)
353 size *= 2;
354 r = av_realloc_array(s->reorder_buf, size, s->frame_size);
355 if (!r)
356 return AVERROR(ENOMEM);
357 s->reorder_buf = r;
358 s->reorder_buf_size = size;
359 return 0;
360 }
361
362 /* ported from alsa-utils/aplay.c */
363 int ff_alsa_get_device_list(AVDeviceInfoList *device_list, snd_pcm_stream_t stream_type)
364 {
365 int ret = 0;
366 void **hints, **n;
367 char *name = NULL, *descr = NULL, *io = NULL, *tmp;
368 AVDeviceInfo *new_device = NULL;
369 const char *filter = stream_type == SND_PCM_STREAM_PLAYBACK ? "Output" : "Input";
370
371 if (snd_device_name_hint(-1, "pcm", &hints) < 0)
372 return AVERROR_EXTERNAL;
373 n = hints;
374 while (*n && !ret) {
375 name = snd_device_name_get_hint(*n, "NAME");
376 descr = snd_device_name_get_hint(*n, "DESC");
377 io = snd_device_name_get_hint(*n, "IOID");
378 if (!io || !strcmp(io, filter)) {
379 new_device = av_mallocz(sizeof(AVDeviceInfo));
380 if (!new_device) {
381 ret = AVERROR(ENOMEM);
382 goto fail;
383 }
384 new_device->device_name = av_strdup(name);
385 if ((tmp = strrchr(descr, '\n')) && tmp[1])
386 new_device->device_description = av_strdup(&tmp[1]);
387 else
388 new_device->device_description = av_strdup(descr);
389 if (!new_device->device_description || !new_device->device_name) {
390 ret = AVERROR(ENOMEM);
391 goto fail;
392 }
393 if ((ret = av_dynarray_add_nofree(&device_list->devices,
394 &device_list->nb_devices, new_device)) < 0) {
395 goto fail;
396 }
397 if (!strcmp(new_device->device_name, "default"))
398 device_list->default_device = device_list->nb_devices - 1;
399 new_device = NULL;
400 }
401 fail:
402 free(io);
403 free(name);
404 free(descr);
405 n++;
406 }
407 if (new_device) {
408 av_free(new_device->device_description);
409 av_free(new_device->device_name);
410 av_free(new_device);
411 }
412 snd_device_name_free_hint(hints);
413 return ret;
414 }
415