Imported Debian version 2.5.0~trusty1.1
[deb_ffmpeg.git] / ffmpeg / libavdevice / alsa-audio-enc.c
CommitLineData
2ba45a60
DM
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: output
26 * @author Luca Abeni ( lucabe72 email it )
27 * @author Benoit Fouet ( benoit fouet free fr )
28 *
29 * This avdevice encoder allows to play audio to an ALSA (Advanced Linux
30 * Sound Architecture) device.
31 *
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".
35 *
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.
38 */
39
40#include <alsa/asoundlib.h>
41
42#include "libavutil/time.h"
43#include "libavformat/internal.h"
44#include "avdevice.h"
45#include "alsa-audio.h"
46
47static av_cold int audio_write_header(AVFormatContext *s1)
48{
49 AlsaData *s = s1->priv_data;
50 AVStream *st = NULL;
51 unsigned int sample_rate;
52 enum AVCodecID codec_id;
53 int res;
54
55 if (s1->nb_streams != 1 || s1->streams[0]->codec->codec_type != AVMEDIA_TYPE_AUDIO) {
56 av_log(s1, AV_LOG_ERROR, "Only a single audio stream is supported.\n");
57 return AVERROR(EINVAL);
58 }
59 st = s1->streams[0];
60
61 sample_rate = st->codec->sample_rate;
62 codec_id = st->codec->codec_id;
63 res = ff_alsa_open(s1, SND_PCM_STREAM_PLAYBACK, &sample_rate,
64 st->codec->channels, &codec_id);
65 if (sample_rate != st->codec->sample_rate) {
66 av_log(s1, AV_LOG_ERROR,
67 "sample rate %d not available, nearest is %d\n",
68 st->codec->sample_rate, sample_rate);
69 goto fail;
70 }
71 avpriv_set_pts_info(st, 64, 1, sample_rate);
72
73 return res;
74
75fail:
76 snd_pcm_close(s->h);
77 return AVERROR(EIO);
78}
79
80static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
81{
82 AlsaData *s = s1->priv_data;
83 int res;
84 int size = pkt->size;
85 uint8_t *buf = pkt->data;
86
87 size /= s->frame_size;
88 if (pkt->dts != AV_NOPTS_VALUE)
89 s->timestamp = pkt->dts;
90 s->timestamp += pkt->duration ? pkt->duration : size;
91
92 if (s->reorder_func) {
93 if (size > s->reorder_buf_size)
94 if (ff_alsa_extend_reorder_buf(s, size))
95 return AVERROR(ENOMEM);
96 s->reorder_func(buf, s->reorder_buf, size);
97 buf = s->reorder_buf;
98 }
99 while ((res = snd_pcm_writei(s->h, buf, size)) < 0) {
100 if (res == -EAGAIN) {
101
102 return AVERROR(EAGAIN);
103 }
104
105 if (ff_alsa_xrun_recover(s1, res) < 0) {
106 av_log(s1, AV_LOG_ERROR, "ALSA write error: %s\n",
107 snd_strerror(res));
108
109 return AVERROR(EIO);
110 }
111 }
112
113 return 0;
114}
115
116static int audio_write_frame(AVFormatContext *s1, int stream_index,
117 AVFrame **frame, unsigned flags)
118{
119 AlsaData *s = s1->priv_data;
120 AVPacket pkt;
121
122 /* ff_alsa_open() should have accepted only supported formats */
123 if ((flags & AV_WRITE_UNCODED_FRAME_QUERY))
124 return av_sample_fmt_is_planar(s1->streams[stream_index]->codec->sample_fmt) ?
125 AVERROR(EINVAL) : 0;
126 /* set only used fields */
127 pkt.data = (*frame)->data[0];
128 pkt.size = (*frame)->nb_samples * s->frame_size;
129 pkt.dts = (*frame)->pkt_dts;
130 pkt.duration = av_frame_get_pkt_duration(*frame);
131 return audio_write_packet(s1, &pkt);
132}
133
134static void
135audio_get_output_timestamp(AVFormatContext *s1, int stream,
136 int64_t *dts, int64_t *wall)
137{
138 AlsaData *s = s1->priv_data;
139 snd_pcm_sframes_t delay = 0;
140 *wall = av_gettime();
141 snd_pcm_delay(s->h, &delay);
142 *dts = s->timestamp - delay;
143}
144
f6fa7814
DM
145static int audio_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list)
146{
147 return ff_alsa_get_device_list(device_list, SND_PCM_STREAM_PLAYBACK);
148}
149
2ba45a60
DM
150static const AVClass alsa_muxer_class = {
151 .class_name = "ALSA muxer",
152 .item_name = av_default_item_name,
153 .version = LIBAVUTIL_VERSION_INT,
154 .category = AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT,
155};
156
157AVOutputFormat ff_alsa_muxer = {
158 .name = "alsa",
159 .long_name = NULL_IF_CONFIG_SMALL("ALSA audio output"),
160 .priv_data_size = sizeof(AlsaData),
161 .audio_codec = DEFAULT_CODEC_ID,
162 .video_codec = AV_CODEC_ID_NONE,
163 .write_header = audio_write_header,
164 .write_packet = audio_write_packet,
165 .write_trailer = ff_alsa_close,
166 .write_uncoded_frame = audio_write_frame,
f6fa7814 167 .get_device_list = audio_get_device_list,
2ba45a60
DM
168 .get_output_timestamp = audio_get_output_timestamp,
169 .flags = AVFMT_NOFILE,
170 .priv_class = &alsa_muxer_class,
171};