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