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