2 * ALSA input and output
3 * Copyright (c) 2007 Luca Abeni ( lucabe72 email it )
4 * Copyright (c) 2007 Benoit Fouet ( benoit fouet free fr )
6 * This file is part of FFmpeg.
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.
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.
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
24 * @file libavdevice/alsa-audio-common.c
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 )
31 #include "libavformat/avformat.h"
32 #include <alsa/asoundlib.h>
34 #include "alsa-audio.h"
36 static av_cold snd_pcm_format_t codec_id_to_pcm_format(int codec_id)
39 case CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE;
40 case CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
41 case CODEC_ID_PCM_S8: return SND_PCM_FORMAT_S8;
42 default: return SND_PCM_FORMAT_UNKNOWN;
46 av_cold int ff_alsa_open(AVFormatContext *ctx, int mode,
47 unsigned int *sample_rate,
48 int channels, int *codec_id)
50 AlsaData *s = ctx->priv_data;
51 const char *audio_device;
53 snd_pcm_format_t format;
55 snd_pcm_hw_params_t *hw_params;
56 snd_pcm_uframes_t buffer_size, period_size;
58 if (ctx->filename[0] == 0) audio_device = "default";
59 else audio_device = ctx->filename;
61 if (*codec_id == CODEC_ID_NONE)
62 *codec_id = DEFAULT_CODEC_ID;
63 format = codec_id_to_pcm_format(*codec_id);
64 if (format == SND_PCM_FORMAT_UNKNOWN) {
65 av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
66 return AVERROR(ENOSYS);
68 s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
70 if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
71 flags = SND_PCM_NONBLOCK;
73 res = snd_pcm_open(&h, audio_device, mode, flags);
75 av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
76 audio_device, snd_strerror(res));
80 res = snd_pcm_hw_params_malloc(&hw_params);
82 av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
87 res = snd_pcm_hw_params_any(h, hw_params);
89 av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
94 res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
96 av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
101 res = snd_pcm_hw_params_set_format(h, hw_params, format);
103 av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
104 *codec_id, format, snd_strerror(res));
108 res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
110 av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
115 res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
117 av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
118 channels, snd_strerror(res));
122 snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
123 /* TODO: maybe use ctx->max_picture_buffer somehow */
124 res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
126 av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
131 snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
132 res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
134 av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
138 s->period_size = period_size;
140 res = snd_pcm_hw_params(h, hw_params);
142 av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
147 snd_pcm_hw_params_free(hw_params);
153 snd_pcm_hw_params_free(hw_params);
159 av_cold int ff_alsa_close(AVFormatContext *s1)
161 AlsaData *s = s1->priv_data;
167 int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
169 AlsaData *s = s1->priv_data;
170 snd_pcm_t *handle = s->h;
172 av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
174 err = snd_pcm_prepare(handle);
176 av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
180 } else if (err == -ESTRPIPE) {
181 av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");