Libav
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 "libavutil/internal.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/time.h"
35 
36 #include "libavformat/avformat.h"
37 #include "libavformat/internal.h"
38 
39 #define DEFAULT_CODEC_ID AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE)
40 
41 typedef struct PulseData {
42  AVClass *class;
43  char *server;
44  char *name;
45  char *stream_name;
47  int channels;
50  pa_simple *s;
51  int64_t pts;
52  int64_t frame_duration;
53  int wallclock;
54 } PulseData;
55 
56 static pa_sample_format_t codec_id_to_pulse_format(int codec_id) {
57  switch (codec_id) {
58  case AV_CODEC_ID_PCM_U8: return PA_SAMPLE_U8;
59  case AV_CODEC_ID_PCM_ALAW: return PA_SAMPLE_ALAW;
60  case AV_CODEC_ID_PCM_MULAW: return PA_SAMPLE_ULAW;
61  case AV_CODEC_ID_PCM_S16LE: return PA_SAMPLE_S16LE;
62  case AV_CODEC_ID_PCM_S16BE: return PA_SAMPLE_S16BE;
63  case AV_CODEC_ID_PCM_F32LE: return PA_SAMPLE_FLOAT32LE;
64  case AV_CODEC_ID_PCM_F32BE: return PA_SAMPLE_FLOAT32BE;
65  case AV_CODEC_ID_PCM_S32LE: return PA_SAMPLE_S32LE;
66  case AV_CODEC_ID_PCM_S32BE: return PA_SAMPLE_S32BE;
67  case AV_CODEC_ID_PCM_S24LE: return PA_SAMPLE_S24LE;
68  case AV_CODEC_ID_PCM_S24BE: return PA_SAMPLE_S24BE;
69  default: return PA_SAMPLE_INVALID;
70  }
71 }
72 
74 {
75  PulseData *pd = s->priv_data;
76  AVStream *st;
77  char *device = NULL;
78  int ret;
79  enum AVCodecID codec_id =
81  const pa_sample_spec ss = { codec_id_to_pulse_format(codec_id),
82  pd->sample_rate,
83  pd->channels };
84 
85  pa_buffer_attr attr = { -1 };
86 
87  st = avformat_new_stream(s, NULL);
88 
89  if (!st) {
90  av_log(s, AV_LOG_ERROR, "Cannot add stream\n");
91  return AVERROR(ENOMEM);
92  }
93 
94  attr.fragsize = pd->fragment_size;
95 
96  if (strcmp(s->filename, "default"))
97  device = s->filename;
98 
99  pd->s = pa_simple_new(pd->server, pd->name,
100  PA_STREAM_RECORD,
101  device, pd->stream_name, &ss,
102  NULL, &attr, &ret);
103 
104  if (!pd->s) {
105  av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n",
106  pa_strerror(ret));
107  return AVERROR(EIO);
108  }
109  /* take real parameters */
111  st->codecpar->codec_id = codec_id;
112  st->codecpar->sample_rate = pd->sample_rate;
113  st->codecpar->channels = pd->channels;
114  avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
115 
116  pd->pts = AV_NOPTS_VALUE;
117  pd->frame_duration = (pd->frame_size * 1000000LL * 8) /
118  (pd->sample_rate * pd->channels * av_get_bits_per_sample(codec_id));
119 
120  return 0;
121 }
122 
124 {
125  PulseData *pd = s->priv_data;
126  int res;
127  pa_usec_t latency;
128 
129  if (av_new_packet(pkt, pd->frame_size) < 0) {
130  return AVERROR(ENOMEM);
131  }
132 
133  if ((pa_simple_read(pd->s, pkt->data, pkt->size, &res)) < 0) {
134  av_log(s, AV_LOG_ERROR, "pa_simple_read failed: %s\n",
135  pa_strerror(res));
136  av_packet_unref(pkt);
137  return AVERROR(EIO);
138  }
139 
140  if ((latency = pa_simple_get_latency(pd->s, &res)) == (pa_usec_t) -1) {
141  av_log(s, AV_LOG_ERROR, "pa_simple_get_latency() failed: %s\n",
142  pa_strerror(res));
143  return AVERROR(EIO);
144  }
145 
146  if (pd->pts == AV_NOPTS_VALUE) {
147  pd->pts = -latency;
148  if (pd->wallclock)
149  pd->pts += av_gettime();
150  }
151 
152  pkt->pts = pd->pts;
153 
154  pd->pts += pd->frame_duration;
155 
156  return 0;
157 }
158 
160 {
161  PulseData *pd = s->priv_data;
162  pa_simple_free(pd->s);
163  return 0;
164 }
165 
166 #define OFFSET(a) offsetof(PulseData, a)
167 #define D AV_OPT_FLAG_DECODING_PARAM
168 
169 static const AVOption options[] = {
170  { "server", "pulse server name", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, D },
171  { "name", "application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = "libav"}, 0, 0, D },
172  { "stream_name", "stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = "record"}, 0, 0, D },
173  { "sample_rate", "sample rate in Hz", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, D },
174  { "channels", "number of audio channels", OFFSET(channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, D },
175  { "frame_size", "number of bytes per frame", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, D },
176  { "fragment_size", "buffering size, affects latency and cpu usage", OFFSET(fragment_size), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D },
177  { "wallclock", "set the initial pts using the current time", OFFSET(wallclock), AV_OPT_TYPE_INT, {.i64 = 1}, -1, 1, D },
178  { NULL },
179 };
180 
181 static const AVClass pulse_demuxer_class = {
182  .class_name = "Pulse demuxer",
183  .item_name = av_default_item_name,
184  .option = options,
185  .version = LIBAVUTIL_VERSION_INT,
186 };
187 
189  .name = "pulse",
190  .long_name = NULL_IF_CONFIG_SMALL("Pulse audio input"),
191  .priv_data_size = sizeof(PulseData),
195  .flags = AVFMT_NOFILE,
196  .priv_class = &pulse_demuxer_class,
197 };
AVOption.
Definition: opt.h:234
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:2986
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
char * stream_name
Definition: pulse.c:45
static av_cold int pulse_close(AVFormatContext *s)
Definition: pulse.c:159
static pa_sample_format_t codec_id_to_pulse_format(int codec_id)
Definition: pulse.c:56
static const AVOption options[]
Definition: pulse.c:169
Format I/O context.
Definition: avformat.h:940
int channels
Definition: pulse.c:47
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:39
#define av_cold
Definition: attributes.h:66
AVOptions.
#define DEFAULT_CODEC_ID
Definition: pulse.c:39
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
uint8_t * data
Definition: avcodec.h:1346
static int flags
Definition: log.c:50
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
pa_simple * s
Definition: pulse.c:50
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:84
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:193
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:2348
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
common internal API header
int64_t frame_duration
Definition: pulse.c:52
char filename[1024]
input or output filename
Definition: avformat.h:1016
enum AVCodecID audio_codec_id
Forced audio codec_id.
Definition: avformat.h:1100
enum AVCodecID codec_id
Definition: avconv_vaapi.c:149
int fragment_size
Definition: pulse.c:49
static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: pulse.c:123
char * server
Definition: pulse.c:43
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:546
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:40
Stream structure.
Definition: avformat.h:705
char * name
Definition: pulse.c:44
NULL
Definition: eval.c:55
int sample_rate
Definition: pulse.c:46
int wallclock
Definition: pulse.c:53
av_default_item_name
Definition: dnxhdenc.c:55
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:347
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
AVInputFormat ff_pulse_demuxer
Definition: pulse.c:188
Describe the class of an AVClass context structure.
Definition: log.h:34
int frame_size
Definition: pulse.c:48
static const AVClass pulse_demuxer_class
Definition: pulse.c:181
static av_cold int pulse_read_header(AVFormatContext *s)
Definition: pulse.c:73
int sample_rate
Audio only.
Definition: avcodec.h:3564
Main libavformat public API header.
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:412
#define D
Definition: pulse.c:167
void * priv_data
Format private data.
Definition: avformat.h:968
int channels
Audio only.
Definition: avcodec.h:3560
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:529
AVCodecParameters * codecpar
Definition: avformat.h:831
This structure stores compressed data.
Definition: avcodec.h:1323
int64_t pts
Definition: pulse.c:51
#define OFFSET(a)
Definition: pulse.c:166
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1339
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:235