2136ee3fa44de02550c527931627bc63d87ce0dc
[libav.git] / libavdevice / pulse.c
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 #include <pulse/simple.h>
29 #include <pulse/rtclock.h>
30 #include <pulse/error.h>
31
32 #include "libavformat/avformat.h"
33 #include "libavformat/internal.h"
34 #include "libavutil/time.h"
35 #include "libavutil/opt.h"
36
37 #define DEFAULT_CODEC_ID AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE)
38
39 typedef struct PulseData {
40 AVClass *class;
41 char *server;
42 char *name;
43 char *stream_name;
44 int sample_rate;
45 int channels;
46 int frame_size;
47 int fragment_size;
48 pa_simple *s;
49 int64_t pts;
50 int64_t frame_duration;
51 int wallclock;
52 } PulseData;
53
54 static pa_sample_format_t codec_id_to_pulse_format(int codec_id) {
55 switch (codec_id) {
56 case AV_CODEC_ID_PCM_U8: return PA_SAMPLE_U8;
57 case AV_CODEC_ID_PCM_ALAW: return PA_SAMPLE_ALAW;
58 case AV_CODEC_ID_PCM_MULAW: return PA_SAMPLE_ULAW;
59 case AV_CODEC_ID_PCM_S16LE: return PA_SAMPLE_S16LE;
60 case AV_CODEC_ID_PCM_S16BE: return PA_SAMPLE_S16BE;
61 case AV_CODEC_ID_PCM_F32LE: return PA_SAMPLE_FLOAT32LE;
62 case AV_CODEC_ID_PCM_F32BE: return PA_SAMPLE_FLOAT32BE;
63 case AV_CODEC_ID_PCM_S32LE: return PA_SAMPLE_S32LE;
64 case AV_CODEC_ID_PCM_S32BE: return PA_SAMPLE_S32BE;
65 case AV_CODEC_ID_PCM_S24LE: return PA_SAMPLE_S24LE;
66 case AV_CODEC_ID_PCM_S24BE: return PA_SAMPLE_S24BE;
67 default: return PA_SAMPLE_INVALID;
68 }
69 }
70
71 static av_cold int pulse_read_header(AVFormatContext *s)
72 {
73 PulseData *pd = s->priv_data;
74 AVStream *st;
75 char *device = NULL;
76 int ret;
77 enum AVCodecID codec_id =
78 s->audio_codec_id == AV_CODEC_ID_NONE ? DEFAULT_CODEC_ID : s->audio_codec_id;
79 const pa_sample_spec ss = { codec_id_to_pulse_format(codec_id),
80 pd->sample_rate,
81 pd->channels };
82
83 pa_buffer_attr attr = { -1 };
84
85 st = avformat_new_stream(s, NULL);
86
87 if (!st) {
88 av_log(s, AV_LOG_ERROR, "Cannot add stream\n");
89 return AVERROR(ENOMEM);
90 }
91
92 attr.fragsize = pd->fragment_size;
93
94 if (strcmp(s->filename, "default"))
95 device = s->filename;
96
97 pd->s = pa_simple_new(pd->server, pd->name,
98 PA_STREAM_RECORD,
99 device, pd->stream_name, &ss,
100 NULL, &attr, &ret);
101
102 if (!pd->s) {
103 av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n",
104 pa_strerror(ret));
105 return AVERROR(EIO);
106 }
107 /* take real parameters */
108 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
109 st->codec->codec_id = codec_id;
110 st->codec->sample_rate = pd->sample_rate;
111 st->codec->channels = pd->channels;
112 avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
113
114 pd->pts = AV_NOPTS_VALUE;
115 pd->frame_duration = (pd->frame_size * 1000000LL * 8) /
116 (pd->sample_rate * pd->channels * av_get_bits_per_sample(codec_id));
117
118 return 0;
119 }
120
121 static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt)
122 {
123 PulseData *pd = s->priv_data;
124 int res;
125 pa_usec_t latency;
126
127 if (av_new_packet(pkt, pd->frame_size) < 0) {
128 return AVERROR(ENOMEM);
129 }
130
131 if ((pa_simple_read(pd->s, pkt->data, pkt->size, &res)) < 0) {
132 av_log(s, AV_LOG_ERROR, "pa_simple_read failed: %s\n",
133 pa_strerror(res));
134 av_free_packet(pkt);
135 return AVERROR(EIO);
136 }
137
138 if ((latency = pa_simple_get_latency(pd->s, &res)) == (pa_usec_t) -1) {
139 av_log(s, AV_LOG_ERROR, "pa_simple_get_latency() failed: %s\n",
140 pa_strerror(res));
141 return AVERROR(EIO);
142 }
143
144 if (pd->pts == AV_NOPTS_VALUE) {
145 pd->pts = -latency;
146 if (pd->wallclock)
147 pd->pts += av_gettime();
148 }
149
150 pkt->pts = pd->pts;
151
152 pd->pts += pd->frame_duration;
153
154 return 0;
155 }
156
157 static av_cold int pulse_close(AVFormatContext *s)
158 {
159 PulseData *pd = s->priv_data;
160 pa_simple_free(pd->s);
161 return 0;
162 }
163
164 #define OFFSET(a) offsetof(PulseData, a)
165 #define D AV_OPT_FLAG_DECODING_PARAM
166
167 static const AVOption options[] = {
168 { "server", "pulse server name", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, D },
169 { "name", "application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = "libav"}, 0, 0, D },
170 { "stream_name", "stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = "record"}, 0, 0, D },
171 { "sample_rate", "sample rate in Hz", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, D },
172 { "channels", "number of audio channels", OFFSET(channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, D },
173 { "frame_size", "number of bytes per frame", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, D },
174 { "fragment_size", "buffering size, affects latency and cpu usage", OFFSET(fragment_size), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D },
175 { "wallclock", "set the initial pts using the current time", OFFSET(wallclock), AV_OPT_TYPE_INT, {.i64 = 1}, -1, 1, D },
176 { NULL },
177 };
178
179 static const AVClass pulse_demuxer_class = {
180 .class_name = "Pulse demuxer",
181 .item_name = av_default_item_name,
182 .option = options,
183 .version = LIBAVUTIL_VERSION_INT,
184 };
185
186 AVInputFormat ff_pulse_demuxer = {
187 .name = "pulse",
188 .long_name = NULL_IF_CONFIG_SMALL("Pulse audio input"),
189 .priv_data_size = sizeof(PulseData),
190 .read_header = pulse_read_header,
191 .read_packet = pulse_read_packet,
192 .read_close = pulse_close,
193 .flags = AVFMT_NOFILE,
194 .priv_class = &pulse_demuxer_class,
195 };