pulse.c
Go to the documentation of this file.
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 
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/opt.h"
35 
36 #define DEFAULT_CODEC_ID AV_NE(CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE)
37 
38 typedef struct PulseData {
39  AVClass *class;
40  char *server;
41  char *name;
42  char *stream_name;
44  int channels;
47  pa_simple *s;
48  int64_t pts;
49  int64_t frame_duration;
50 } PulseData;
51 
52 static pa_sample_format_t codec_id_to_pulse_format(int codec_id) {
53  switch (codec_id) {
54  case CODEC_ID_PCM_U8: return PA_SAMPLE_U8;
55  case CODEC_ID_PCM_ALAW: return PA_SAMPLE_ALAW;
56  case CODEC_ID_PCM_MULAW: return PA_SAMPLE_ULAW;
57  case CODEC_ID_PCM_S16LE: return PA_SAMPLE_S16LE;
58  case CODEC_ID_PCM_S16BE: return PA_SAMPLE_S16BE;
59  case CODEC_ID_PCM_F32LE: return PA_SAMPLE_FLOAT32LE;
60  case CODEC_ID_PCM_F32BE: return PA_SAMPLE_FLOAT32BE;
61  case CODEC_ID_PCM_S32LE: return PA_SAMPLE_S32LE;
62  case CODEC_ID_PCM_S32BE: return PA_SAMPLE_S32BE;
63  case CODEC_ID_PCM_S24LE: return PA_SAMPLE_S24LE;
64  case CODEC_ID_PCM_S24BE: return PA_SAMPLE_S24BE;
65  default: return PA_SAMPLE_INVALID;
66  }
67 }
68 
71 {
72  PulseData *pd = s->priv_data;
73  AVStream *st;
74  char *device = NULL;
75  int ret;
76  enum CodecID codec_id =
78  const pa_sample_spec ss = { codec_id_to_pulse_format(codec_id),
79  pd->sample_rate,
80  pd->channels };
81 
82  pa_buffer_attr attr = { -1 };
83 
84  st = avformat_new_stream(s, NULL);
85 
86  if (!st) {
87  av_log(s, AV_LOG_ERROR, "Cannot add stream\n");
88  return AVERROR(ENOMEM);
89  }
90 
91  attr.fragsize = pd->fragment_size;
92 
93  if (strcmp(s->filename, "default"))
94  device = s->filename;
95 
96  pd->s = pa_simple_new(pd->server, pd->name,
97  PA_STREAM_RECORD,
98  device, pd->stream_name, &ss,
99  NULL, &attr, &ret);
100 
101  if (!pd->s) {
102  av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n",
103  pa_strerror(ret));
104  return AVERROR(EIO);
105  }
106  /* take real parameters */
108  st->codec->codec_id = codec_id;
109  st->codec->sample_rate = pd->sample_rate;
110  st->codec->channels = pd->channels;
111  avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
112 
113  pd->pts = AV_NOPTS_VALUE;
114  pd->frame_duration = (pd->frame_size * 1000000LL * 8) /
115  (pd->sample_rate * pd->channels * av_get_bits_per_sample(codec_id));
116 
117  return 0;
118 }
119 
121 {
122  PulseData *pd = s->priv_data;
123  int res;
124  pa_usec_t latency;
125 
126  if (av_new_packet(pkt, pd->frame_size) < 0) {
127  return AVERROR(ENOMEM);
128  }
129 
130  if ((pa_simple_read(pd->s, pkt->data, pkt->size, &res)) < 0) {
131  av_log(s, AV_LOG_ERROR, "pa_simple_read failed: %s\n",
132  pa_strerror(res));
133  av_free_packet(pkt);
134  return AVERROR(EIO);
135  }
136 
137  if ((latency = pa_simple_get_latency(pd->s, &res)) == (pa_usec_t) -1) {
138  av_log(s, AV_LOG_ERROR, "pa_simple_get_latency() failed: %s\n",
139  pa_strerror(res));
140  return AVERROR(EIO);
141  }
142 
143  if (pd->pts == AV_NOPTS_VALUE) {
144  pd->pts = -latency;
145  }
146 
147  pkt->pts = pd->pts;
148 
149  pd->pts += pd->frame_duration;
150 
151  return 0;
152 }
153 
155 {
156  PulseData *pd = s->priv_data;
157  pa_simple_free(pd->s);
158  return 0;
159 }
160 
161 #define OFFSET(a) offsetof(PulseData, a)
162 #define D AV_OPT_FLAG_DECODING_PARAM
163 
164 static const AVOption options[] = {
165  { "server", "pulse server name", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, D },
166  { "name", "application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = "libav"}, 0, 0, D },
167  { "stream_name", "stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = "record"}, 0, 0, D },
168  { "sample_rate", "sample rate in Hz", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.dbl = 48000}, 1, INT_MAX, D },
169  { "channels", "number of audio channels", OFFSET(channels), AV_OPT_TYPE_INT, {.dbl = 2}, 1, INT_MAX, D },
170  { "frame_size", "number of bytes per frame", OFFSET(frame_size), AV_OPT_TYPE_INT, {.dbl = 1024}, 1, INT_MAX, D },
171  { "fragment_size", "buffering size, affects latency and cpu usage", OFFSET(fragment_size), AV_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX, D },
172  { NULL },
173 };
174 
175 static const AVClass pulse_demuxer_class = {
176  .class_name = "Pulse demuxer",
177  .item_name = av_default_item_name,
178  .option = options,
179  .version = LIBAVUTIL_VERSION_INT,
180 };
181 
183  .name = "pulse",
184  .long_name = NULL_IF_CONFIG_SMALL("Pulse audio input"),
185  .priv_data_size = sizeof(PulseData),
189  .flags = AVFMT_NOFILE,
190  .priv_class = &pulse_demuxer_class,
191 };