FFmpeg  2.6.3
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
pulse_audio_dec.c
Go to the documentation of this file.
1 /*
2  * Pulseaudio input
3  * Copyright (c) 2011 Luca Barbato <lu_zero@gentoo.org>
4  * Copyright 2004-2006 Lennart Poettering
5  * Copyright (c) 2014 Michael Niedermayer <michaelni@gmx.at>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #include <pulse/rtclock.h>
25 #include <pulse/error.h>
26 #include "libavformat/avformat.h"
27 #include "libavformat/internal.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/time.h"
30 #include "pulse_audio_common.h"
31 #include "timefilter.h"
32 
33 #define DEFAULT_CODEC_ID AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE)
34 
35 typedef struct PulseData {
36  AVClass *class;
37  char *server;
38  char *name;
39  char *stream_name;
41  int channels;
44 
45  pa_threaded_mainloop *mainloop;
46  pa_context *context;
47  pa_stream *stream;
48 
51  int wallclock;
52 } PulseData;
53 
54 
55 #define CHECK_SUCCESS_GOTO(rerror, expression, label) \
56  do { \
57  if (!(expression)) { \
58  rerror = AVERROR_EXTERNAL; \
59  goto label; \
60  } \
61  } while (0)
62 
63 #define CHECK_DEAD_GOTO(p, rerror, label) \
64  do { \
65  if (!(p)->context || !PA_CONTEXT_IS_GOOD(pa_context_get_state((p)->context)) || \
66  !(p)->stream || !PA_STREAM_IS_GOOD(pa_stream_get_state((p)->stream))) { \
67  rerror = AVERROR_EXTERNAL; \
68  goto label; \
69  } \
70  } while (0)
71 
72 static void context_state_cb(pa_context *c, void *userdata) {
73  PulseData *p = userdata;
74 
75  switch (pa_context_get_state(c)) {
76  case PA_CONTEXT_READY:
77  case PA_CONTEXT_TERMINATED:
78  case PA_CONTEXT_FAILED:
79  pa_threaded_mainloop_signal(p->mainloop, 0);
80  break;
81  }
82 }
83 
84 static void stream_state_cb(pa_stream *s, void * userdata) {
85  PulseData *p = userdata;
86 
87  switch (pa_stream_get_state(s)) {
88  case PA_STREAM_READY:
89  case PA_STREAM_FAILED:
90  case PA_STREAM_TERMINATED:
91  pa_threaded_mainloop_signal(p->mainloop, 0);
92  break;
93  }
94 }
95 
96 static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
97  PulseData *p = userdata;
98 
99  pa_threaded_mainloop_signal(p->mainloop, 0);
100 }
101 
102 static void stream_latency_update_cb(pa_stream *s, void *userdata) {
103  PulseData *p = userdata;
104 
105  pa_threaded_mainloop_signal(p->mainloop, 0);
106 }
107 
109 {
110  PulseData *pd = s->priv_data;
111 
112  if (pd->mainloop)
113  pa_threaded_mainloop_stop(pd->mainloop);
114 
115  if (pd->stream)
116  pa_stream_unref(pd->stream);
117  pd->stream = NULL;
118 
119  if (pd->context) {
120  pa_context_disconnect(pd->context);
121  pa_context_unref(pd->context);
122  }
123  pd->context = NULL;
124 
125  if (pd->mainloop)
126  pa_threaded_mainloop_free(pd->mainloop);
127  pd->mainloop = NULL;
128 
130  pd->timefilter = NULL;
131 
132  return 0;
133 }
134 
136 {
137  PulseData *pd = s->priv_data;
138  AVStream *st;
139  char *device = NULL;
140  int ret;
141  enum AVCodecID codec_id =
143  const pa_sample_spec ss = { ff_codec_id_to_pulse_format(codec_id),
144  pd->sample_rate,
145  pd->channels };
146 
147  pa_buffer_attr attr = { -1 };
148 
149  st = avformat_new_stream(s, NULL);
150 
151  if (!st) {
152  av_log(s, AV_LOG_ERROR, "Cannot add stream\n");
153  return AVERROR(ENOMEM);
154  }
155 
156  attr.fragsize = pd->fragment_size;
157 
158  if (s->filename[0] != '\0' && strcmp(s->filename, "default"))
159  device = s->filename;
160 
161  if (!(pd->mainloop = pa_threaded_mainloop_new())) {
162  pulse_close(s);
163  return AVERROR_EXTERNAL;
164  }
165 
166  if (!(pd->context = pa_context_new(pa_threaded_mainloop_get_api(pd->mainloop), pd->name))) {
167  pulse_close(s);
168  return AVERROR_EXTERNAL;
169  }
170 
171  pa_context_set_state_callback(pd->context, context_state_cb, pd);
172 
173  if (pa_context_connect(pd->context, pd->server, 0, NULL) < 0) {
174  pulse_close(s);
175  return AVERROR(pa_context_errno(pd->context));
176  }
177 
178  pa_threaded_mainloop_lock(pd->mainloop);
179 
180  if (pa_threaded_mainloop_start(pd->mainloop) < 0) {
181  ret = -1;
182  goto unlock_and_fail;
183  }
184 
185  for (;;) {
186  pa_context_state_t state;
187 
188  state = pa_context_get_state(pd->context);
189 
190  if (state == PA_CONTEXT_READY)
191  break;
192 
193  if (!PA_CONTEXT_IS_GOOD(state)) {
194  ret = AVERROR(pa_context_errno(pd->context));
195  goto unlock_and_fail;
196  }
197 
198  /* Wait until the context is ready */
199  pa_threaded_mainloop_wait(pd->mainloop);
200  }
201 
202  if (!(pd->stream = pa_stream_new(pd->context, pd->stream_name, &ss, NULL))) {
203  ret = AVERROR(pa_context_errno(pd->context));
204  goto unlock_and_fail;
205  }
206 
207  pa_stream_set_state_callback(pd->stream, stream_state_cb, pd);
208  pa_stream_set_read_callback(pd->stream, stream_request_cb, pd);
209  pa_stream_set_write_callback(pd->stream, stream_request_cb, pd);
210  pa_stream_set_latency_update_callback(pd->stream, stream_latency_update_cb, pd);
211 
212  ret = pa_stream_connect_record(pd->stream, device, &attr,
213  PA_STREAM_INTERPOLATE_TIMING
214  |PA_STREAM_ADJUST_LATENCY
215  |PA_STREAM_AUTO_TIMING_UPDATE);
216 
217  if (ret < 0) {
218  ret = AVERROR(pa_context_errno(pd->context));
219  goto unlock_and_fail;
220  }
221 
222  for (;;) {
223  pa_stream_state_t state;
224 
225  state = pa_stream_get_state(pd->stream);
226 
227  if (state == PA_STREAM_READY)
228  break;
229 
230  if (!PA_STREAM_IS_GOOD(state)) {
231  ret = AVERROR(pa_context_errno(pd->context));
232  goto unlock_and_fail;
233  }
234 
235  /* Wait until the stream is ready */
236  pa_threaded_mainloop_wait(pd->mainloop);
237  }
238 
239  pa_threaded_mainloop_unlock(pd->mainloop);
240 
241  /* take real parameters */
243  st->codec->codec_id = codec_id;
244  st->codec->sample_rate = pd->sample_rate;
245  st->codec->channels = pd->channels;
246  avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
247 
248  pd->timefilter = ff_timefilter_new(1000000.0 / pd->sample_rate,
249  1000, 1.5E-6);
250 
251  if (!pd->timefilter) {
252  pulse_close(s);
253  return AVERROR(ENOMEM);
254  }
255 
256  return 0;
257 
258 unlock_and_fail:
259  pa_threaded_mainloop_unlock(pd->mainloop);
260 
261  pulse_close(s);
262  return ret;
263 }
264 
266 {
267  PulseData *pd = s->priv_data;
268  int ret;
269  size_t read_length;
270  const void *read_data = NULL;
271  int64_t dts;
272  pa_usec_t latency;
273  int negative;
274 
275  pa_threaded_mainloop_lock(pd->mainloop);
276 
277  CHECK_DEAD_GOTO(pd, ret, unlock_and_fail);
278 
279  while (!read_data) {
280  int r;
281 
282  r = pa_stream_peek(pd->stream, &read_data, &read_length);
283  CHECK_SUCCESS_GOTO(ret, r == 0, unlock_and_fail);
284 
285  if (read_length <= 0) {
286  pa_threaded_mainloop_wait(pd->mainloop);
287  CHECK_DEAD_GOTO(pd, ret, unlock_and_fail);
288  } else if (!read_data) {
289  /* There's a hole in the stream, skip it. We could generate
290  * silence, but that wouldn't work for compressed streams. */
291  r = pa_stream_drop(pd->stream);
292  CHECK_SUCCESS_GOTO(ret, r == 0, unlock_and_fail);
293  }
294  }
295 
296  if (av_new_packet(pkt, read_length) < 0) {
297  ret = AVERROR(ENOMEM);
298  goto unlock_and_fail;
299  }
300 
301  dts = av_gettime();
302  pa_operation_unref(pa_stream_update_timing_info(pd->stream, NULL, NULL));
303 
304  if (pa_stream_get_latency(pd->stream, &latency, &negative) >= 0) {
305  enum AVCodecID codec_id =
307  int frame_size = ((av_get_bits_per_sample(codec_id) >> 3) * pd->channels);
308  int frame_duration = read_length / frame_size;
309 
310 
311  if (negative) {
312  dts += latency;
313  } else
314  dts -= latency;
315  if (pd->wallclock)
316  pkt->pts = ff_timefilter_update(pd->timefilter, dts, pd->last_period);
317 
318  pd->last_period = frame_duration;
319  } else {
320  av_log(s, AV_LOG_WARNING, "pa_stream_get_latency() failed\n");
321  }
322 
323  memcpy(pkt->data, read_data, read_length);
324  pa_stream_drop(pd->stream);
325 
326  pa_threaded_mainloop_unlock(pd->mainloop);
327  return 0;
328 
329 unlock_and_fail:
330  pa_threaded_mainloop_unlock(pd->mainloop);
331  return ret;
332 }
333 
335 {
336  PulseData *s = h->priv_data;
337  return ff_pulse_audio_get_devices(device_list, s->server, 0);
338 }
339 
340 #define OFFSET(a) offsetof(PulseData, a)
341 #define D AV_OPT_FLAG_DECODING_PARAM
342 
343 static const AVOption options[] = {
344  { "server", "set PulseAudio server", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, D },
345  { "name", "set application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT}, 0, 0, D },
346  { "stream_name", "set stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = "record"}, 0, 0, D },
347  { "sample_rate", "set sample rate in Hz", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, D },
348  { "channels", "set number of audio channels", OFFSET(channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, D },
349  { "frame_size", "set number of bytes per frame", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, D },
350  { "fragment_size", "set buffering size, affects latency and cpu usage", OFFSET(fragment_size), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D },
351  { "wallclock", "set the initial pts using the current time", OFFSET(wallclock), AV_OPT_TYPE_INT, {.i64 = 1}, -1, 1, D },
352  { NULL },
353 };
354 
355 static const AVClass pulse_demuxer_class = {
356  .class_name = "Pulse demuxer",
357  .item_name = av_default_item_name,
358  .option = options,
359  .version = LIBAVUTIL_VERSION_INT,
361 };
362 
364  .name = "pulse",
365  .long_name = NULL_IF_CONFIG_SMALL("Pulse audio input"),
366  .priv_data_size = sizeof(PulseData),
371  .flags = AVFMT_NOFILE,
372  .priv_class = &pulse_demuxer_class,
373 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:669
AVOption.
Definition: opt.h:255
void ff_timefilter_destroy(TimeFilter *self)
Free all resources associated with the filter.
Definition: timefilter.c:62
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:181
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
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:3993
#define D
#define CHECK_DEAD_GOTO(p, rerror, label)
static av_cold int pulse_close(AVFormatContext *s)
static AVPacket pkt
AVInputFormat ff_pulse_demuxer
char * stream_name
static const AVClass pulse_demuxer_class
Format I/O context.
Definition: avformat.h:1214
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:71
Opaque type representing a time filter state.
Definition: timefilter.c:30
#define av_cold
Definition: attributes.h:74
AVOptions.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3659
uint8_t * data
Definition: avcodec.h:1160
static int read_data(void *opaque, uint8_t *buf, int buf_size)
Definition: hls.c:1018
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
#define CHECK_SUCCESS_GOTO(rerror, expression, label)
#define av_log(a,...)
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:83
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:102
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:175
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:3295
av_default_item_name
#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:180
const char * r
Definition: vf_curves.c:107
GLsizei GLsizei * length
Definition: opengl_enc.c:115
enum AVCodecID codec_id
Definition: mov_chan.c:433
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:814
#define LIBAVFORMAT_IDENT
Definition: version.h:44
pa_stream * stream
#define OFFSET(a)
char filename[1024]
input or output filename
Definition: avformat.h:1290
enum AVCodecID audio_codec_id
Forced audio codec_id.
Definition: avformat.h:1375
ret
Definition: avfilter.c:974
pa_sample_format_t av_cold ff_codec_id_to_pulse_format(enum AVCodecID codec_id)
static const AVOption options[]
TimeFilter * timefilter
char * server
static void stream_request_cb(pa_stream *s, size_t length, void *userdata)
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:619
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
Stream structure.
Definition: avformat.h:795
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
sample_rate
TimeFilter * ff_timefilter_new(double time_base, double period, double bandwidth)
Create a new Delay Locked Loop time filter.
Definition: timefilter.c:46
int frame_size
Definition: mxfenc.c:1618
static void stream_state_cb(pa_stream *s, void *userdata)
enum AVMediaType codec_type
Definition: avcodec.h:1247
pa_context * context
enum AVCodecID codec_id
Definition: avcodec.h:1256
static av_cold int pulse_read_header(AVFormatContext *s)
int sample_rate
samples per second
Definition: avcodec.h:1983
Describe the class of an AVClass context structure.
Definition: log.h:66
int ff_pulse_audio_get_devices(AVDeviceInfoList *devices, const char *server, int output)
static void context_state_cb(pa_context *c, void *userdata)
static int get_device_list(AVOpenCLDeviceList *device_list)
Definition: opencl.c:193
List of devices.
Definition: avdevice.h:459
static uint32_t state
Definition: trasher.c:27
static int flags
Definition: cpu.c:47
Main libavformat public API header.
double ff_timefilter_update(TimeFilter *self, double system_time, double period)
Update the filter.
Definition: timefilter.c:72
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:418
static double c[64]
static void stream_latency_update_cb(pa_stream *s, void *userdata)
int channels
number of audio channels
Definition: avcodec.h:1984
void * priv_data
Format private data.
Definition: avformat.h:1242
static int pulse_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list)
pa_threaded_mainloop * mainloop
#define DEFAULT_CODEC_ID
static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:581
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
This structure stores compressed data.
Definition: avcodec.h:1137
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1153
const char * name
Definition: opengl_enc.c:103