FFmpeg  2.1.1
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  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 #include "libavformat/avformat.h"
32 #include "libavformat/internal.h"
33 #include "libavutil/opt.h"
34 #include "pulse_audio_common.h"
35 
36 #define DEFAULT_CODEC_ID AV_NE(AV_CODEC_ID_PCM_S16BE, AV_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 
53 {
54  PulseData *pd = s->priv_data;
55  AVStream *st;
56  char *device = NULL;
57  int ret;
58  enum AVCodecID codec_id =
60  const pa_sample_spec ss = { codec_id_to_pulse_format(codec_id),
61  pd->sample_rate,
62  pd->channels };
63 
64  pa_buffer_attr attr = { -1 };
65 
66  st = avformat_new_stream(s, NULL);
67 
68  if (!st) {
69  av_log(s, AV_LOG_ERROR, "Cannot add stream\n");
70  return AVERROR(ENOMEM);
71  }
72 
73  attr.fragsize = pd->fragment_size;
74 
75  if (strcmp(s->filename, "default"))
76  device = s->filename;
77 
78  pd->s = pa_simple_new(pd->server, pd->name,
79  PA_STREAM_RECORD,
80  device, pd->stream_name, &ss,
81  NULL, &attr, &ret);
82 
83  if (!pd->s) {
84  av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n",
85  pa_strerror(ret));
86  return AVERROR(EIO);
87  }
88  /* take real parameters */
90  st->codec->codec_id = codec_id;
91  st->codec->sample_rate = pd->sample_rate;
92  st->codec->channels = pd->channels;
93  avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
94 
95  pd->pts = AV_NOPTS_VALUE;
96  pd->frame_duration = (pd->frame_size * 1000000LL * 8) /
97  (pd->sample_rate * pd->channels * av_get_bits_per_sample(codec_id));
98 
99  return 0;
100 }
101 
103 {
104  PulseData *pd = s->priv_data;
105  int res;
106  pa_usec_t latency;
107 
108  if (av_new_packet(pkt, pd->frame_size) < 0) {
109  return AVERROR(ENOMEM);
110  }
111 
112  if ((pa_simple_read(pd->s, pkt->data, pkt->size, &res)) < 0) {
113  av_log(s, AV_LOG_ERROR, "pa_simple_read failed: %s\n",
114  pa_strerror(res));
115  av_free_packet(pkt);
116  return AVERROR(EIO);
117  }
118 
119  if ((latency = pa_simple_get_latency(pd->s, &res)) == (pa_usec_t) -1) {
120  av_log(s, AV_LOG_ERROR, "pa_simple_get_latency() failed: %s\n",
121  pa_strerror(res));
122  return AVERROR(EIO);
123  }
124 
125  if (pd->pts == AV_NOPTS_VALUE) {
126  pd->pts = -latency;
127  }
128 
129  pkt->pts = pd->pts;
130 
131  pd->pts += pd->frame_duration;
132 
133  return 0;
134 }
135 
137 {
138  PulseData *pd = s->priv_data;
139  pa_simple_free(pd->s);
140  return 0;
141 }
142 
143 #define OFFSET(a) offsetof(PulseData, a)
144 #define D AV_OPT_FLAG_DECODING_PARAM
145 
146 static const AVOption options[] = {
147  { "server", "set PulseAudio server", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, D },
148  { "name", "set application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT}, 0, 0, D },
149  { "stream_name", "set stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = "record"}, 0, 0, D },
150  { "sample_rate", "set sample rate in Hz", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, D },
151  { "channels", "set number of audio channels", OFFSET(channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, D },
152  { "frame_size", "set number of bytes per frame", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, D },
153  { "fragment_size", "set buffering size, affects latency and cpu usage", OFFSET(fragment_size), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D },
154  { NULL },
155 };
156 
157 static const AVClass pulse_demuxer_class = {
158  .class_name = "Pulse demuxer",
159  .item_name = av_default_item_name,
160  .option = options,
161  .version = LIBAVUTIL_VERSION_INT,
162 };
163 
165  .name = "pulse",
166  .long_name = NULL_IF_CONFIG_SMALL("Pulse audio input"),
167  .priv_data_size = sizeof(PulseData),
171  .flags = AVFMT_NOFILE,
172  .priv_class = &pulse_demuxer_class,
173 };
const char * name
Definition: avisynth_c.h:675
const char * s
Definition: avisynth_c.h:668
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:279
AVOption.
Definition: opt.h:253
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:478
#define LIBAVUTIL_VERSION_INT
Definition: avcodec.h:820
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:3922
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:686
int size
Definition: avcodec.h:1064
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
static av_cold int pulse_close(AVFormatContext *s)
AVInputFormat ff_pulse_demuxer
char * stream_name
#define av_cold
Definition: avcodec.h:653
static const AVClass pulse_demuxer_class
Format I/O context.
Definition: avformat.h:968
static uint8_t * res
Definition: ffhash.c:43
const char * av_default_item_name(void *ctx)
Return the context name.
Definition: log.c:145
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:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:347
#define LIBAVFORMAT_IDENT
Definition: version.h:44
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3348
const OptionDef options[]
Definition: ffserver.c:4682
pa_simple * s
static const uint8_t frame_size[4]
Definition: g723_1_data.h:58
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: avcodec.h:4147
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:2928
void * priv_data
Format private data.
Definition: avformat.h:988
char filename[1024]
input or output filename
Definition: avformat.h:1018
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
enum AVCodecID codec_id
Definition: mov_chan.c:433
int64_t frame_duration
#define OFFSET(a)
enum AVCodecID audio_codec_id
Forced audio codec_id.
Definition: avformat.h:1088
ret
Definition: avfilter.c:961
pa_sample_format_t av_cold codec_id_to_pulse_format(int codec_id)
char * server
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:592
Stream structure.
Definition: avformat.h:667
sample_rate
enum AVMediaType codec_type
Definition: avcodec.h:1154
enum AVCodecID codec_id
Definition: avcodec.h:1157
static av_cold int pulse_read_header(AVFormatContext *s)
int sample_rate
samples per second
Definition: avcodec.h:1873
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
Describe the class of an AVClass context structure.
Definition: log.h:50
uint8_t * data
Definition: avcodec.h:1063
#define D
Definition: options_table.h:39
static int flags
Definition: cpu.c:45
int channels
number of audio channels
Definition: avcodec.h:1874
#define AVERROR(e)
#define DEFAULT_CODEC_ID
static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt)
static AVPacket pkt
Definition: demuxing.c:52
This structure stores compressed data.
Definition: avcodec.h:1040
int64_t pts
int64_t pts
Presentation timestamp in AVStream-&gt;time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1056
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avcodec.h:2278