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
25 * ALSA input and output: output
26 * @author Luca Abeni ( lucabe72 email it )
27 * @author Benoit Fouet ( benoit fouet free fr )
29 * This avdevice encoder allows to play audio to an ALSA (Advanced Linux
30 * Sound Architecture) device.
32 * The filename parameter is the name of an ALSA PCM device capable of
33 * capture, for example "default" or "plughw:1"; see the ALSA documentation
34 * for naming conventions. The empty string is equivalent to "default".
36 * The playback period is set to the lower value available for the device,
37 * which gives a low latency suitable for real-time playback.
40 #include <alsa/asoundlib.h>
42 #include "libavutil/time.h"
43 #include "libavformat/internal.h"
45 #include "alsa-audio.h"
47 static av_cold int audio_write_header(AVFormatContext *s1)
49 AlsaData *s = s1->priv_data;
51 unsigned int sample_rate;
52 enum AVCodecID codec_id;
56 sample_rate = st->codec->sample_rate;
57 codec_id = st->codec->codec_id;
58 res = ff_alsa_open(s1, SND_PCM_STREAM_PLAYBACK, &sample_rate,
59 st->codec->channels, &codec_id);
60 if (sample_rate != st->codec->sample_rate) {
61 av_log(s1, AV_LOG_ERROR,
62 "sample rate %d not available, nearest is %d\n",
63 st->codec->sample_rate, sample_rate);
66 avpriv_set_pts_info(st, 64, 1, sample_rate);
75 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
77 AlsaData *s = s1->priv_data;
80 uint8_t *buf = pkt->data;
82 size /= s->frame_size;
83 if (s->reorder_func) {
84 if (size > s->reorder_buf_size)
85 if (ff_alsa_extend_reorder_buf(s, size))
86 return AVERROR(ENOMEM);
87 s->reorder_func(buf, s->reorder_buf, size);
90 while ((res = snd_pcm_writei(s->h, buf, size)) < 0) {
93 return AVERROR(EAGAIN);
96 if (ff_alsa_xrun_recover(s1, res) < 0) {
97 av_log(s1, AV_LOG_ERROR, "ALSA write error: %s\n",
108 audio_get_output_timestamp(AVFormatContext *s1, int stream,
109 int64_t *dts, int64_t *wall)
111 AlsaData *s = s1->priv_data;
112 snd_pcm_sframes_t delay = 0;
113 *wall = av_gettime();
114 snd_pcm_delay(s->h, &delay);
115 *dts = s1->streams[0]->cur_dts - delay;
118 AVOutputFormat ff_alsa_muxer = {
120 .long_name = NULL_IF_CONFIG_SMALL("ALSA audio output"),
121 .priv_data_size = sizeof(AlsaData),
122 .audio_codec = DEFAULT_CODEC_ID,
123 .video_codec = AV_CODEC_ID_NONE,
124 .write_header = audio_write_header,
125 .write_packet = audio_write_packet,
126 .write_trailer = ff_alsa_close,
127 .get_output_timestamp = audio_get_output_timestamp,
128 .flags = AVFMT_NOFILE,