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 Libav.
8
* Libav 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
* Libav 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 Libav; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 <alsa/asoundlib.h>
32
#include "libavformat/avformat.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_F64LE: return SND_PCM_FORMAT_FLOAT64_LE;
40
case CODEC_ID_PCM_F64BE: return SND_PCM_FORMAT_FLOAT64_BE;
41
case CODEC_ID_PCM_F32LE: return SND_PCM_FORMAT_FLOAT_LE;
42
case CODEC_ID_PCM_F32BE: return SND_PCM_FORMAT_FLOAT_BE;
43
case CODEC_ID_PCM_S32LE: return SND_PCM_FORMAT_S32_LE;
44
case CODEC_ID_PCM_S32BE: return SND_PCM_FORMAT_S32_BE;
45
case CODEC_ID_PCM_U32LE: return SND_PCM_FORMAT_U32_LE;
46
case CODEC_ID_PCM_U32BE: return SND_PCM_FORMAT_U32_BE;
47
case CODEC_ID_PCM_S24LE: return SND_PCM_FORMAT_S24_3LE;
48
case CODEC_ID_PCM_S24BE: return SND_PCM_FORMAT_S24_3BE;
49
case CODEC_ID_PCM_U24LE: return SND_PCM_FORMAT_U24_3LE;
50
case CODEC_ID_PCM_U24BE: return SND_PCM_FORMAT_U24_3BE;
51
case CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE;
52
case CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
53
case CODEC_ID_PCM_U16LE: return SND_PCM_FORMAT_U16_LE;
54
case CODEC_ID_PCM_U16BE: return SND_PCM_FORMAT_U16_BE;
55
case CODEC_ID_PCM_S8: return SND_PCM_FORMAT_S8;
56
case CODEC_ID_PCM_U8: return SND_PCM_FORMAT_U8;
57
case CODEC_ID_PCM_MULAW: return SND_PCM_FORMAT_MU_LAW;
58
case CODEC_ID_PCM_ALAW: return SND_PCM_FORMAT_A_LAW;
59
default: return SND_PCM_FORMAT_UNKNOWN;
63
av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
64
unsigned int *sample_rate,
65
int channels, enum CodecID *codec_id)
67
AlsaData *s = ctx->priv_data;
68
const char *audio_device;
70
snd_pcm_format_t format;
72
snd_pcm_hw_params_t *hw_params;
73
snd_pcm_uframes_t buffer_size, period_size;
75
if (ctx->filename[0] == 0) audio_device = "default";
76
else audio_device = ctx->filename;
78
if (*codec_id == CODEC_ID_NONE)
79
*codec_id = DEFAULT_CODEC_ID;
80
format = codec_id_to_pcm_format(*codec_id);
81
if (format == SND_PCM_FORMAT_UNKNOWN) {
82
av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
83
return AVERROR(ENOSYS);
85
s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
87
if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
88
flags = SND_PCM_NONBLOCK;
90
res = snd_pcm_open(&h, audio_device, mode, flags);
92
av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
93
audio_device, snd_strerror(res));
97
res = snd_pcm_hw_params_malloc(&hw_params);
99
av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
104
res = snd_pcm_hw_params_any(h, hw_params);
106
av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
111
res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
113
av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
118
res = snd_pcm_hw_params_set_format(h, hw_params, format);
120
av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
121
*codec_id, format, snd_strerror(res));
125
res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
127
av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
132
res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
134
av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
135
channels, snd_strerror(res));
139
snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
140
/* TODO: maybe use ctx->max_picture_buffer somehow */
141
res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
143
av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
148
snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
150
period_size = buffer_size / 4;
151
res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
153
av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
157
s->period_size = period_size;
159
res = snd_pcm_hw_params(h, hw_params);
161
av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
166
snd_pcm_hw_params_free(hw_params);
172
snd_pcm_hw_params_free(hw_params);
178
av_cold int ff_alsa_close(AVFormatContext *s1)
180
AlsaData *s = s1->priv_data;
186
int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
188
AlsaData *s = s1->priv_data;
189
snd_pcm_t *handle = s->h;
191
av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
193
err = snd_pcm_prepare(handle);
195
av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
199
} else if (err == -ESTRPIPE) {
200
av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");