pulse: introduce pulseaudio input
[libav.git] / libavdevice / pulse.c
CommitLineData
0de9c41f
LB
1/*
2 * Pulseaudio input
3 * Copyright (c) 2011 Luca Barbato <lu_zero@gentoo.org>
4 *
5 * This file is part of Libav.
6 *
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
23 * @file
24 * PulseAudio input using the simple API.
25 * @author Luca Barbato <lu_zero@gentoo.org>
26 *
27 */
28
29#include <pulse/simple.h>
30#include <pulse/rtclock.h>
31#include <pulse/error.h>
32
33#include "libavformat/avformat.h"
34#include "libavutil/opt.h"
35
36#define DEFAULT_CODEC_ID AV_NE(CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE)
37
38typedef struct PulseData {
39 AVClass *class;
40 char *server;
41 char *name;
42 char *stream_name;
43 int sample_rate;
44 int channels;
45 int frame_size;
46 int fragment_size;
47 pa_simple *s;
48 int64_t pts;
49} PulseData;
50
51static pa_sample_format_t codec_id_to_pulse_format(int codec_id) {
52 switch (codec_id) {
53 case CODEC_ID_PCM_U8: return PA_SAMPLE_U8;
54 case CODEC_ID_PCM_ALAW: return PA_SAMPLE_ALAW;
55 case CODEC_ID_PCM_MULAW: return PA_SAMPLE_ULAW;
56 case CODEC_ID_PCM_S16LE: return PA_SAMPLE_S16LE;
57 case CODEC_ID_PCM_S16BE: return PA_SAMPLE_S16BE;
58 case CODEC_ID_PCM_F32LE: return PA_SAMPLE_FLOAT32LE;
59 case CODEC_ID_PCM_F32BE: return PA_SAMPLE_FLOAT32BE;
60 case CODEC_ID_PCM_S32LE: return PA_SAMPLE_S32LE;
61 case CODEC_ID_PCM_S32BE: return PA_SAMPLE_S32BE;
62 case CODEC_ID_PCM_S24LE: return PA_SAMPLE_S24LE;
63 case CODEC_ID_PCM_S24BE: return PA_SAMPLE_S24BE;
64 default: return PA_SAMPLE_INVALID;
65 }
66}
67
68static av_cold int pulse_read_header(AVFormatContext *s,
69 AVFormatParameters *ap)
70{
71 PulseData *pd = s->priv_data;
72 AVStream *st;
73 char *device = NULL;
74 int ret;
75 enum CodecID codec_id =
76 s->audio_codec_id == CODEC_ID_NONE ? DEFAULT_CODEC_ID : s->audio_codec_id;
77 const pa_sample_spec ss = { codec_id_to_pulse_format(codec_id),
78 pd->sample_rate,
79 pd->channels };
80
81 pa_buffer_attr attr = { -1 };
82
83 st = avformat_new_stream(s, NULL);
84
85 if (!st) {
86 av_log(s, AV_LOG_ERROR, "Cannot add stream\n");
87 return AVERROR(ENOMEM);
88 }
89
90 attr.fragsize = pd->fragment_size;
91
92 if (strcmp(s->filename, "default"))
93 device = s->filename;
94
95 pd->s = pa_simple_new(pd->server, pd->name,
96 PA_STREAM_RECORD,
97 device, pd->stream_name, &ss,
98 NULL, &attr, &ret);
99
100 if (!pd->s) {
101 av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n",
102 pa_strerror(ret));
103 return AVERROR(EIO);
104 }
105 /* take real parameters */
106 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
107 st->codec->codec_id = codec_id;
108 st->codec->sample_rate = pd->sample_rate;
109 st->codec->channels = pd->channels;
110 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
111
112 pd->pts = AV_NOPTS_VALUE;
113
114 return 0;
115}
116
117static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt)
118{
119 PulseData *pd = s->priv_data;
120 int res;
121 pa_usec_t latency;
122 uint64_t frame_duration =
123 (pd->frame_size*1000000LL) / (pd->sample_rate * pd->channels);
124
125 if (av_new_packet(pkt, pd->frame_size) < 0) {
126 return AVERROR(ENOMEM);
127 }
128
129 if ((pa_simple_read(pd->s, pkt->data, pkt->size, &res)) < 0) {
130 av_log(s, AV_LOG_ERROR, "pa_simple_read failed: %s\n",
131 pa_strerror(res));
132 av_free_packet(pkt);
133 return AVERROR(EIO);
134 }
135
136 if ((latency = pa_simple_get_latency(pd->s, &res)) == (pa_usec_t) -1) {
137 av_log(s, AV_LOG_ERROR, "pa_simple_get_latency() failed: %s\n",
138 pa_strerror(res));
139 return AVERROR(EIO);
140 }
141
142 if (pd->pts == AV_NOPTS_VALUE) {
143 pd->pts = -latency;
144 }
145
146 pkt->pts = pd->pts;
147
148 pd->pts += frame_duration;
149
150 return 0;
151}
152
153static av_cold int pulse_close(AVFormatContext *s)
154{
155 PulseData *pd = s->priv_data;
156 pa_simple_free(pd->s);
157 return 0;
158}
159
160#define OFFSET(a) offsetof(PulseData, a)
161#define D AV_OPT_FLAG_DECODING_PARAM
162
163static const AVOption options[] = {
164 { "server", "pulse server name", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, D },
165 { "name", "application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = "libav"}, 0, 0, D },
166 { "stream_name", "stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = "record"}, 0, 0, D },
167 { "sample_rate", "sample rate in Hz", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.dbl = 48000}, 1, INT_MAX, D },
168 { "channels", "number of audio channels", OFFSET(channels), AV_OPT_TYPE_INT, {.dbl = 2}, 1, INT_MAX, D },
169 { "frame_size", "number of bytes per frame", OFFSET(frame_size), AV_OPT_TYPE_INT, {.dbl = 1024}, 1, INT_MAX, D },
170 { "fragment_size", "buffering size, affects latency and cpu usage", OFFSET(fragment_size), AV_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX, D },
171 { NULL },
172};
173
174static const AVClass pulse_demuxer_class = {
175 .class_name = "Pulse demuxer",
176 .item_name = av_default_item_name,
177 .option = options,
178 .version = LIBAVUTIL_VERSION_INT,
179};
180
181AVInputFormat ff_pulse_demuxer = {
182 .name = "pulse",
183 .long_name = NULL_IF_CONFIG_SMALL("Pulse audio input"),
184 .priv_data_size = sizeof(PulseData),
185 .read_header = pulse_read_header,
186 .read_packet = pulse_read_packet,
187 .read_close = pulse_close,
188 .flags = AVFMT_NOFILE,
189 .priv_class = &pulse_demuxer_class,
190};