| 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 const struct { | ||
| 131 | unsigned int pos; | ||
| 132 | enum AVChannel ch; | ||
| 133 | } alsa_chmap_table[] = { | ||
| 134 | { SND_CHMAP_MONO, AV_CHAN_FRONT_CENTER }, | ||
| 135 | { SND_CHMAP_FL, AV_CHAN_FRONT_LEFT }, | ||
| 136 | { SND_CHMAP_FR, AV_CHAN_FRONT_RIGHT }, | ||
| 137 | { SND_CHMAP_RL, AV_CHAN_BACK_LEFT }, | ||
| 138 | { SND_CHMAP_RR, AV_CHAN_BACK_RIGHT }, | ||
| 139 | { SND_CHMAP_FC, AV_CHAN_FRONT_CENTER }, | ||
| 140 | { SND_CHMAP_LFE, AV_CHAN_LOW_FREQUENCY }, | ||
| 141 | { SND_CHMAP_SL, AV_CHAN_SIDE_LEFT }, | ||
| 142 | { SND_CHMAP_SR, AV_CHAN_SIDE_RIGHT }, | ||
| 143 | { SND_CHMAP_RC, AV_CHAN_BACK_CENTER }, | ||
| 144 | { SND_CHMAP_FLC, AV_CHAN_FRONT_LEFT_OF_CENTER }, | ||
| 145 | { SND_CHMAP_FRC, AV_CHAN_FRONT_RIGHT_OF_CENTER}, | ||
| 146 | { SND_CHMAP_FLW, AV_CHAN_WIDE_LEFT }, | ||
| 147 | { SND_CHMAP_FRW, AV_CHAN_WIDE_RIGHT }, | ||
| 148 | { SND_CHMAP_TC, AV_CHAN_TOP_CENTER }, | ||
| 149 | { SND_CHMAP_TFL, AV_CHAN_TOP_FRONT_LEFT }, | ||
| 150 | { SND_CHMAP_TFR, AV_CHAN_TOP_FRONT_RIGHT }, | ||
| 151 | { SND_CHMAP_TFC, AV_CHAN_TOP_FRONT_CENTER }, | ||
| 152 | { SND_CHMAP_TRL, AV_CHAN_TOP_BACK_LEFT }, | ||
| 153 | { SND_CHMAP_TRR, AV_CHAN_TOP_BACK_RIGHT }, | ||
| 154 | { SND_CHMAP_TRC, AV_CHAN_TOP_BACK_CENTER }, | ||
| 155 | { SND_CHMAP_TSL, AV_CHAN_TOP_SIDE_LEFT }, | ||
| 156 | { SND_CHMAP_TSR, AV_CHAN_TOP_SIDE_RIGHT }, | ||
| 157 | { SND_CHMAP_LLFE, AV_CHAN_LOW_FREQUENCY }, | ||
| 158 | { SND_CHMAP_RLFE, AV_CHAN_LOW_FREQUENCY_2 }, | ||
| 159 | { SND_CHMAP_BC, AV_CHAN_BOTTOM_FRONT_CENTER }, | ||
| 160 | { SND_CHMAP_BLC, AV_CHAN_BOTTOM_FRONT_LEFT }, | ||
| 161 | { SND_CHMAP_BRC, AV_CHAN_BOTTOM_FRONT_RIGHT }, | ||
| 162 | { SND_CHMAP_UNKNOWN, AV_CHAN_UNKNOWN }, | ||
| 163 | { SND_CHMAP_NA, AV_CHAN_UNUSED }, | ||
| 164 | }; | ||
| 165 | |||
| 166 | ✗ | static enum AVChannel alsa_chmap_pos_to_av_chan(unsigned int pos) | |
| 167 | { | ||
| 168 | ✗ | for (unsigned i = 0; i < FF_ARRAY_ELEMS(alsa_chmap_table); i++) | |
| 169 | ✗ | if (alsa_chmap_table[i].pos == pos) | |
| 170 | ✗ | return alsa_chmap_table[i].ch; | |
| 171 | ✗ | return AV_CHAN_NONE; | |
| 172 | } | ||
| 173 | |||
| 174 | ✗ | static int alsa_get_chmap(AVFormatContext *ctx, snd_pcm_t *h, AVChannelLayout *layout) | |
| 175 | { | ||
| 176 | ✗ | snd_pcm_chmap_t *chmap = snd_pcm_get_chmap(h); | |
| 177 | ✗ | int ret = 0; | |
| 178 | |||
| 179 | ✗ | if (!chmap) | |
| 180 | ✗ | return 0; | |
| 181 | |||
| 182 | ✗ | if (layout->nb_channels != chmap->channels) { | |
| 183 | ✗ | av_log(ctx, AV_LOG_ERROR, "Inconsistent channel layout requested!\n"); | |
| 184 | ✗ | ret = AVERROR(EINVAL); | |
| 185 | ✗ | goto out; | |
| 186 | } | ||
| 187 | |||
| 188 | ✗ | av_channel_layout_uninit(layout); | |
| 189 | ✗ | ret = av_channel_layout_custom_init(layout, chmap->channels); | |
| 190 | ✗ | if (ret < 0) | |
| 191 | ✗ | goto out; | |
| 192 | |||
| 193 | ✗ | for (unsigned i = 0; i < chmap->channels; i++) { | |
| 194 | ✗ | enum AVChannel ch = alsa_chmap_pos_to_av_chan(chmap->pos[i] & | |
| 195 | SND_CHMAP_POSITION_MASK); | ||
| 196 | ✗ | if (ch == AV_CHAN_NONE) { | |
| 197 | ✗ | av_log(ctx, AV_LOG_WARNING, | |
| 198 | "unknown ALSA channel position %u.\n", | ||
| 199 | ✗ | chmap->pos[i] & SND_CHMAP_POSITION_MASK); | |
| 200 | ✗ | continue; | |
| 201 | } | ||
| 202 | ✗ | layout->u.map[i].id = ch; | |
| 203 | } | ||
| 204 | ✗ | ret = av_channel_layout_retype(layout, 0, | |
| 205 | AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL); | ||
| 206 | |||
| 207 | ✗ | out: | |
| 208 | ✗ | free(chmap); | |
| 209 | ✗ | return ret; | |
| 210 | } | ||
| 211 | |||
| 212 | ✗ | static av_cold int find_reorder_func(AlsaData *s, int codec_id, | |
| 213 | const AVChannelLayout *layout, int out) | ||
| 214 | { | ||
| 215 | int format; | ||
| 216 | |||
| 217 | /* reordering input is not currently supported */ | ||
| 218 | ✗ | if (!out) | |
| 219 | ✗ | return AVERROR(ENOSYS); | |
| 220 | |||
| 221 | /* reordering is not needed for QUAD or 2_2 layout */ | ||
| 222 | ✗ | if (!av_channel_layout_compare(layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_QUAD) || | |
| 223 | ✗ | !av_channel_layout_compare(layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_2_2)) | |
| 224 | ✗ | return 0; | |
| 225 | |||
| 226 | ✗ | switch (codec_id) { | |
| 227 | ✗ | case AV_CODEC_ID_PCM_S8: | |
| 228 | case AV_CODEC_ID_PCM_U8: | ||
| 229 | case AV_CODEC_ID_PCM_ALAW: | ||
| 230 | ✗ | case AV_CODEC_ID_PCM_MULAW: format = FORMAT_I8; break; | |
| 231 | ✗ | case AV_CODEC_ID_PCM_S16LE: | |
| 232 | case AV_CODEC_ID_PCM_S16BE: | ||
| 233 | case AV_CODEC_ID_PCM_U16LE: | ||
| 234 | ✗ | case AV_CODEC_ID_PCM_U16BE: format = FORMAT_I16; break; | |
| 235 | ✗ | case AV_CODEC_ID_PCM_S32LE: | |
| 236 | case AV_CODEC_ID_PCM_S32BE: | ||
| 237 | case AV_CODEC_ID_PCM_U32LE: | ||
| 238 | ✗ | case AV_CODEC_ID_PCM_U32BE: format = FORMAT_I32; break; | |
| 239 | ✗ | case AV_CODEC_ID_PCM_F32LE: | |
| 240 | ✗ | case AV_CODEC_ID_PCM_F32BE: format = FORMAT_F32; break; | |
| 241 | ✗ | default: return AVERROR(ENOSYS); | |
| 242 | } | ||
| 243 | |||
| 244 | ✗ | if (!av_channel_layout_compare(layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0_BACK) || | |
| 245 | ✗ | !av_channel_layout_compare(layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0)) | |
| 246 | ✗ | PICK_REORDER(50) | |
| 247 | ✗ | else if (!av_channel_layout_compare(layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1_BACK) || | |
| 248 | ✗ | !av_channel_layout_compare(layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1)) | |
| 249 | ✗ | PICK_REORDER(51) | |
| 250 | ✗ | else if (!av_channel_layout_compare(layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_7POINT1)) | |
| 251 | ✗ | PICK_REORDER(71) | |
| 252 | |||
| 253 | ✗ | return s->reorder_func ? 0 : AVERROR(ENOSYS); | |
| 254 | } | ||
| 255 | |||
| 256 | ✗ | av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode, | |
| 257 | unsigned int *sample_rate, | ||
| 258 | AVChannelLayout *layout, enum AVCodecID *codec_id) | ||
| 259 | { | ||
| 260 | ✗ | AlsaData *s = ctx->priv_data; | |
| 261 | const char *audio_device; | ||
| 262 | ✗ | int res, flags = 0; | |
| 263 | snd_pcm_format_t format; | ||
| 264 | snd_pcm_t *h; | ||
| 265 | snd_pcm_hw_params_t *hw_params; | ||
| 266 | snd_pcm_uframes_t buffer_size, period_size; | ||
| 267 | |||
| 268 | ✗ | if (ctx->url[0] == 0) audio_device = "default"; | |
| 269 | ✗ | else audio_device = ctx->url; | |
| 270 | |||
| 271 | ✗ | if (*codec_id == AV_CODEC_ID_NONE) | |
| 272 | ✗ | *codec_id = DEFAULT_CODEC_ID; | |
| 273 | ✗ | format = codec_id_to_pcm_format(*codec_id); | |
| 274 | ✗ | if (format == SND_PCM_FORMAT_UNKNOWN) { | |
| 275 | ✗ | av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id); | |
| 276 | ✗ | return AVERROR(ENOSYS); | |
| 277 | } | ||
| 278 | ✗ | if (ctx->flags & AVFMT_FLAG_NONBLOCK) { | |
| 279 | ✗ | flags = SND_PCM_NONBLOCK; | |
| 280 | } | ||
| 281 | ✗ | res = snd_pcm_open(&h, audio_device, mode, flags); | |
| 282 | ✗ | if (res < 0) { | |
| 283 | ✗ | av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n", | |
| 284 | audio_device, snd_strerror(res)); | ||
| 285 | ✗ | return AVERROR(EIO); | |
| 286 | } | ||
| 287 | |||
| 288 | ✗ | res = snd_pcm_hw_params_malloc(&hw_params); | |
| 289 | ✗ | if (res < 0) { | |
| 290 | ✗ | av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n", | |
| 291 | snd_strerror(res)); | ||
| 292 | ✗ | goto fail1; | |
| 293 | } | ||
| 294 | |||
| 295 | ✗ | res = snd_pcm_hw_params_any(h, hw_params); | |
| 296 | ✗ | if (res < 0) { | |
| 297 | ✗ | av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n", | |
| 298 | snd_strerror(res)); | ||
| 299 | ✗ | goto fail; | |
| 300 | } | ||
| 301 | |||
| 302 | ✗ | res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); | |
| 303 | ✗ | if (res < 0) { | |
| 304 | ✗ | av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n", | |
| 305 | snd_strerror(res)); | ||
| 306 | ✗ | goto fail; | |
| 307 | } | ||
| 308 | |||
| 309 | ✗ | res = snd_pcm_hw_params_set_format(h, hw_params, format); | |
| 310 | ✗ | if (res < 0) { | |
| 311 | ✗ | av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n", | |
| 312 | *codec_id, format, snd_strerror(res)); | ||
| 313 | ✗ | goto fail; | |
| 314 | } | ||
| 315 | |||
| 316 | ✗ | res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0); | |
| 317 | ✗ | if (res < 0) { | |
| 318 | ✗ | av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n", | |
| 319 | snd_strerror(res)); | ||
| 320 | ✗ | goto fail; | |
| 321 | } | ||
| 322 | |||
| 323 | // For output streams, the stream should already have a layout. | ||
| 324 | // Adding this check for clarity. | ||
| 325 | ✗ | if (mode == SND_PCM_STREAM_CAPTURE && layout->nb_channels == 0) { | |
| 326 | ✗ | unsigned int channels = 2; | |
| 327 | ✗ | if (snd_pcm_hw_params_test_channels(h, hw_params, channels) < 0) { | |
| 328 | ✗ | res = snd_pcm_hw_params_get_channels_min(hw_params, &channels); | |
| 329 | ✗ | if (res < 0) { | |
| 330 | ✗ | av_log(ctx, AV_LOG_ERROR, "cannot get minimum channel count (%s)\n", | |
| 331 | snd_strerror(res)); | ||
| 332 | ✗ | goto fail; | |
| 333 | } | ||
| 334 | ✗ | av_log(ctx, AV_LOG_VERBOSE, | |
| 335 | "stereo not supported, falling back to %u channel(s)\n", channels); | ||
| 336 | } | ||
| 337 | ✗ | layout->order = AV_CHANNEL_ORDER_UNSPEC; | |
| 338 | ✗ | layout->nb_channels = channels; | |
| 339 | } | ||
| 340 | |||
| 341 | ✗ | res = snd_pcm_hw_params_set_channels(h, hw_params, layout->nb_channels); | |
| 342 | ✗ | if (res < 0) { | |
| 343 | ✗ | av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n", | |
| 344 | layout->nb_channels, snd_strerror(res)); | ||
| 345 | ✗ | goto fail; | |
| 346 | } | ||
| 347 | |||
| 348 | ✗ | s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * layout->nb_channels; | |
| 349 | |||
| 350 | ✗ | snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size); | |
| 351 | ✗ | buffer_size = FFMIN(buffer_size, ALSA_BUFFER_SIZE_MAX); | |
| 352 | /* TODO: maybe use ctx->max_picture_buffer somehow */ | ||
| 353 | ✗ | res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size); | |
| 354 | ✗ | if (res < 0) { | |
| 355 | ✗ | av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n", | |
| 356 | snd_strerror(res)); | ||
| 357 | ✗ | goto fail; | |
| 358 | } | ||
| 359 | |||
| 360 | ✗ | snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL); | |
| 361 | ✗ | if (!period_size) | |
| 362 | ✗ | period_size = buffer_size / 4; | |
| 363 | ✗ | res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL); | |
| 364 | ✗ | if (res < 0) { | |
| 365 | ✗ | av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n", | |
| 366 | snd_strerror(res)); | ||
| 367 | ✗ | goto fail; | |
| 368 | } | ||
| 369 | ✗ | s->period_size = period_size; | |
| 370 | |||
| 371 | ✗ | res = snd_pcm_hw_params(h, hw_params); | |
| 372 | ✗ | if (res < 0) { | |
| 373 | ✗ | av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n", | |
| 374 | snd_strerror(res)); | ||
| 375 | ✗ | goto fail; | |
| 376 | } | ||
| 377 | |||
| 378 | ✗ | snd_pcm_hw_params_free(hw_params); | |
| 379 | |||
| 380 | // TODO: when support for channel layout gets more widespread in alsa | ||
| 381 | // drivers, this might be used to supersede the re-ordering function | ||
| 382 | // below. | ||
| 383 | ✗ | if (mode == SND_PCM_STREAM_CAPTURE && | |
| 384 | ✗ | layout->order == AV_CHANNEL_ORDER_UNSPEC) { | |
| 385 | ✗ | res = alsa_get_chmap(ctx, h, layout); | |
| 386 | ✗ | if (res < 0) | |
| 387 | ✗ | goto fail1; | |
| 388 | } | ||
| 389 | |||
| 390 | ✗ | if (layout->nb_channels > 2 && layout->order != AV_CHANNEL_ORDER_UNSPEC) { | |
| 391 | // See comment above. find_reorder_func is currently only implemented in | ||
| 392 | // playback mode. | ||
| 393 | ✗ | if (find_reorder_func(s, *codec_id, layout, mode == SND_PCM_STREAM_PLAYBACK) < 0) { | |
| 394 | char name[128]; | ||
| 395 | ✗ | av_channel_layout_describe(layout, name, sizeof(name)); | |
| 396 | ✗ | av_log(ctx, AV_LOG_WARNING, "ALSA channel layout unknown or unimplemented for %s %s.\n", | |
| 397 | name, mode == SND_PCM_STREAM_PLAYBACK ? "playback" : "capture"); | ||
| 398 | } | ||
| 399 | ✗ | if (s->reorder_func) { | |
| 400 | ✗ | s->reorder_buf_size = buffer_size; | |
| 401 | ✗ | s->reorder_buf = av_malloc_array(s->reorder_buf_size, s->frame_size); | |
| 402 | ✗ | if (!s->reorder_buf) | |
| 403 | ✗ | goto fail1; | |
| 404 | } | ||
| 405 | } | ||
| 406 | |||
| 407 | ✗ | s->pkt = av_packet_alloc(); | |
| 408 | ✗ | if (!s->pkt) | |
| 409 | ✗ | goto fail1; | |
| 410 | |||
| 411 | ✗ | s->h = h; | |
| 412 | ✗ | return 0; | |
| 413 | |||
| 414 | ✗ | fail: | |
| 415 | ✗ | snd_pcm_hw_params_free(hw_params); | |
| 416 | ✗ | fail1: | |
| 417 | ✗ | snd_pcm_close(h); | |
| 418 | ✗ | return AVERROR(EIO); | |
| 419 | } | ||
| 420 | |||
| 421 | ✗ | av_cold int ff_alsa_close(AVFormatContext *s1) | |
| 422 | { | ||
| 423 | ✗ | AlsaData *s = s1->priv_data; | |
| 424 | |||
| 425 | ✗ | if (snd_pcm_stream(s->h) == SND_PCM_STREAM_PLAYBACK) { | |
| 426 | ✗ | snd_pcm_nonblock(s->h, 0); | |
| 427 | ✗ | snd_pcm_drain(s->h); | |
| 428 | } | ||
| 429 | ✗ | av_freep(&s->reorder_buf); | |
| 430 | if (CONFIG_ALSA_INDEV) | ||
| 431 | ✗ | ff_timefilter_destroy(s->timefilter); | |
| 432 | ✗ | snd_pcm_close(s->h); | |
| 433 | ✗ | av_packet_free(&s->pkt); | |
| 434 | ✗ | return 0; | |
| 435 | } | ||
| 436 | |||
| 437 | ✗ | int ff_alsa_xrun_recover(AVFormatContext *s1, int err) | |
| 438 | { | ||
| 439 | ✗ | AlsaData *s = s1->priv_data; | |
| 440 | ✗ | snd_pcm_t *handle = s->h; | |
| 441 | |||
| 442 | ✗ | av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n"); | |
| 443 | ✗ | if (err == -EPIPE) { | |
| 444 | ✗ | err = snd_pcm_prepare(handle); | |
| 445 | ✗ | if (err < 0) { | |
| 446 | ✗ | av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err)); | |
| 447 | |||
| 448 | ✗ | return AVERROR(EIO); | |
| 449 | } | ||
| 450 | #ifdef ESTRPIPE | ||
| 451 | ✗ | } else if (err == -ESTRPIPE) { | |
| 452 | ✗ | av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n"); | |
| 453 | |||
| 454 | ✗ | return -1; | |
| 455 | #endif | ||
| 456 | } | ||
| 457 | ✗ | return err; | |
| 458 | } | ||
| 459 | |||
| 460 | ✗ | int ff_alsa_extend_reorder_buf(AlsaData *s, int min_size) | |
| 461 | { | ||
| 462 | ✗ | int size = s->reorder_buf_size; | |
| 463 | void *r; | ||
| 464 | |||
| 465 | ✗ | av_assert0(size != 0); | |
| 466 | ✗ | while (size < min_size) | |
| 467 | ✗ | size *= 2; | |
| 468 | ✗ | r = av_realloc_array(s->reorder_buf, size, s->frame_size); | |
| 469 | ✗ | if (!r) | |
| 470 | ✗ | return AVERROR(ENOMEM); | |
| 471 | ✗ | s->reorder_buf = r; | |
| 472 | ✗ | s->reorder_buf_size = size; | |
| 473 | ✗ | return 0; | |
| 474 | } | ||
| 475 | |||
| 476 | /* ported from alsa-utils/aplay.c */ | ||
| 477 | 1 | int ff_alsa_get_device_list(AVDeviceInfoList *device_list, snd_pcm_stream_t stream_type) | |
| 478 | { | ||
| 479 | 1 | int ret = 0; | |
| 480 | void **hints, **n; | ||
| 481 | 1 | char *name = NULL, *descr = NULL, *io = NULL, *tmp; | |
| 482 | 1 | AVDeviceInfo *new_device = NULL; | |
| 483 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | const char *filter = stream_type == SND_PCM_STREAM_PLAYBACK ? "Output" : "Input"; |
| 484 | |||
| 485 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (snd_device_name_hint(-1, "pcm", &hints) < 0) |
| 486 | ✗ | return AVERROR_EXTERNAL; | |
| 487 | 1 | n = hints; | |
| 488 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | while (*n && !ret) { |
| 489 | 1 | name = snd_device_name_get_hint(*n, "NAME"); | |
| 490 | 1 | descr = snd_device_name_get_hint(*n, "DESC"); | |
| 491 | 1 | io = snd_device_name_get_hint(*n, "IOID"); | |
| 492 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1 | if (!io || !strcmp(io, filter)) { |
| 493 | 1 | new_device = av_mallocz(sizeof(AVDeviceInfo)); | |
| 494 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!new_device) { |
| 495 | ✗ | ret = AVERROR(ENOMEM); | |
| 496 | ✗ | goto fail; | |
| 497 | } | ||
| 498 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | new_device->device_name = av_strdup(name ? name : ""); |
| 499 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!descr) |
| 500 | 1 | new_device->device_description = av_strdup(""); | |
| 501 | ✗ | else if ((tmp = strrchr(descr, '\n')) && tmp[1]) | |
| 502 | ✗ | new_device->device_description = av_strdup(&tmp[1]); | |
| 503 | else | ||
| 504 | ✗ | new_device->device_description = av_strdup(descr); | |
| 505 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (!new_device->device_description || !new_device->device_name) { |
| 506 | ✗ | ret = AVERROR(ENOMEM); | |
| 507 | ✗ | goto fail; | |
| 508 | } | ||
| 509 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if ((ret = av_dynarray_add_nofree(&device_list->devices, |
| 510 | &device_list->nb_devices, new_device)) < 0) { | ||
| 511 | ✗ | goto fail; | |
| 512 | } | ||
| 513 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!strcmp(new_device->device_name, "default")) |
| 514 | ✗ | device_list->default_device = device_list->nb_devices - 1; | |
| 515 | 1 | new_device = NULL; | |
| 516 | } | ||
| 517 | ✗ | fail: | |
| 518 | 1 | free(io); | |
| 519 | 1 | free(name); | |
| 520 | 1 | free(descr); | |
| 521 | 1 | n++; | |
| 522 | } | ||
| 523 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (new_device) { |
| 524 | ✗ | av_free(new_device->device_description); | |
| 525 | ✗ | av_free(new_device->device_name); | |
| 526 | ✗ | av_free(new_device); | |
| 527 | } | ||
| 528 | 1 | snd_device_name_free_hint(hints); | |
| 529 | 1 | return ret; | |
| 530 | } | ||
| 531 |