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 "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;
45  int channels;
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 
72 {
73  PulseData *pd = s->priv_data;
74  AVStream *st;
75  char *device = NULL;
76  int ret;
77  enum AVCodecID 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 */
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 
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 
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 
187  .name = "pulse",
188  .long_name = NULL_IF_CONFIG_SMALL("Pulse audio input"),
189  .priv_data_size = sizeof(PulseData),
193  .flags = AVFMT_NOFILE,
194  .priv_class = &pulse_demuxer_class,
195 };
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:243
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:2829
int size
Definition: avcodec.h:974
char * stream_name
Definition: pulse.c:43
static av_cold int pulse_close(AVFormatContext *s)
Definition: pulse.c:157
static pa_sample_format_t codec_id_to_pulse_format(int codec_id)
Definition: pulse.c:54
static const AVOption options[]
Definition: pulse.c:167
Format I/O context.
Definition: avformat.h:922
int channels
Definition: pulse.c:45
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:38
#define av_cold
Definition: attributes.h:66
AVOptions.
#define DEFAULT_CODEC_ID
Definition: pulse.c:37
const char * name
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2521
uint8_t * data
Definition: avcodec.h:973
static int flags
Definition: log.c:44
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
pa_simple * s
Definition: pulse.c:48
static const uint8_t frame_size[4]
Definition: g723_1_data.h:47
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:81
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:105
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:2055
#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:145
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
enum AVCodecID codec_id
Definition: mov_chan.c:432
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
int64_t frame_duration
Definition: pulse.c:50
char filename[1024]
input or output filename
Definition: avformat.h:998
enum AVCodecID audio_codec_id
Forced audio codec_id.
Definition: avformat.h:1082
int fragment_size
Definition: pulse.c:47
static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: pulse.c:121
char * server
Definition: pulse.c:41
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:544
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:37
Stream structure.
Definition: avformat.h:699
char * name
Definition: pulse.c:42
NULL
Definition: eval.c:55
enum AVMediaType codec_type
Definition: avcodec.h:1058
int sample_rate
Definition: pulse.c:44
int wallclock
Definition: pulse.c:51
enum AVCodecID codec_id
Definition: avcodec.h:1067
int sample_rate
samples per second
Definition: avcodec.h:1807
av_default_item_name
Definition: dnxhdenc.c:52
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
AVInputFormat ff_pulse_demuxer
Definition: pulse.c:186
Describe the class of an AVClass context structure.
Definition: log.h:33
int frame_size
Definition: pulse.c:46
static const AVClass pulse_demuxer_class
Definition: pulse.c:179
static av_cold int pulse_read_header(AVFormatContext *s)
Definition: pulse.c:71
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:409
#define D
Definition: pulse.c:165
int channels
number of audio channels
Definition: avcodec.h:1808
void * priv_data
Format private data.
Definition: avformat.h:950
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
This structure stores compressed data.
Definition: avcodec.h:950
int64_t pts
Definition: pulse.c:49
#define OFFSET(a)
Definition: pulse.c:164
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228