]> git.sesse.net Git - ffmpeg/blob - libavdevice/pulse_audio_dec.c
avformat/matroskaenc: fix indention level
[ffmpeg] / libavdevice / pulse_audio_dec.c
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;
43     int  sample_rate;
44     int  channels;
45     int  frame_size;
46     int  fragment_size;
47     pa_simple *s;
48     int64_t pts;
49     int64_t frame_duration;
50 } PulseData;
51
52 static av_cold int pulse_read_header(AVFormatContext *s)
53 {
54     PulseData *pd = s->priv_data;
55     AVStream *st;
56     char *device = NULL;
57     int ret, sample_bytes;
58     enum AVCodecID codec_id =
59         s->audio_codec_id == AV_CODEC_ID_NONE ? DEFAULT_CODEC_ID : s->audio_codec_id;
60     const pa_sample_spec ss = { ff_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 */
89     st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
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, pd->sample_rate);  /* 64 bits pts in us */
94
95     pd->pts = AV_NOPTS_VALUE;
96     sample_bytes = (av_get_bits_per_sample(codec_id) >> 3) * pd->channels;
97
98     if (pd->frame_size % sample_bytes) {
99         av_log(s, AV_LOG_WARNING, "frame_size %i is not divisible by %i "
100             "(channels * bytes_per_sample) \n", pd->frame_size, sample_bytes);
101     }
102
103     pd->frame_duration = pd->frame_size / sample_bytes;
104
105     return 0;
106 }
107
108 static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt)
109 {
110     PulseData *pd  = s->priv_data;
111     int res;
112
113     if (av_new_packet(pkt, pd->frame_size) < 0) {
114         return AVERROR(ENOMEM);
115     }
116
117     if ((pa_simple_read(pd->s, pkt->data, pkt->size, &res)) < 0) {
118         av_log(s, AV_LOG_ERROR, "pa_simple_read failed: %s\n",
119                pa_strerror(res));
120         av_free_packet(pkt);
121         return AVERROR(EIO);
122     }
123
124     if (pd->pts == AV_NOPTS_VALUE) {
125         pa_usec_t latency;
126
127         if ((latency = pa_simple_get_latency(pd->s, &res)) == (pa_usec_t) -1) {
128             av_log(s, AV_LOG_ERROR, "pa_simple_get_latency() failed: %s\n",
129                    pa_strerror(res));
130             return AVERROR(EIO);
131         }
132
133         pd->pts = -latency;
134     }
135
136     pkt->pts = pd->pts;
137
138     pd->pts += pd->frame_duration;
139
140     return 0;
141 }
142
143 static av_cold int pulse_close(AVFormatContext *s)
144 {
145     PulseData *pd = s->priv_data;
146     pa_simple_free(pd->s);
147     return 0;
148 }
149
150 static int pulse_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list)
151 {
152     PulseData *s = h->priv_data;
153     return ff_pulse_audio_get_devices(device_list, s->server, 0);
154 }
155
156 #define OFFSET(a) offsetof(PulseData, a)
157 #define D AV_OPT_FLAG_DECODING_PARAM
158
159 static const AVOption options[] = {
160     { "server",        "set PulseAudio server",                             OFFSET(server),        AV_OPT_TYPE_STRING, {.str = NULL},     0, 0, D },
161     { "name",          "set application name",                              OFFSET(name),          AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT},  0, 0, D },
162     { "stream_name",   "set stream description",                            OFFSET(stream_name),   AV_OPT_TYPE_STRING, {.str = "record"}, 0, 0, D },
163     { "sample_rate",   "set sample rate in Hz",                             OFFSET(sample_rate),   AV_OPT_TYPE_INT,    {.i64 = 48000},    1, INT_MAX, D },
164     { "channels",      "set number of audio channels",                      OFFSET(channels),      AV_OPT_TYPE_INT,    {.i64 = 2},        1, INT_MAX, D },
165     { "frame_size",    "set number of bytes per frame",                     OFFSET(frame_size),    AV_OPT_TYPE_INT,    {.i64 = 1024},     1, INT_MAX, D },
166     { "fragment_size", "set buffering size, affects latency and cpu usage", OFFSET(fragment_size), AV_OPT_TYPE_INT,    {.i64 = -1},      -1, INT_MAX, D },
167     { NULL },
168 };
169
170 static const AVClass pulse_demuxer_class = {
171     .class_name     = "Pulse demuxer",
172     .item_name      = av_default_item_name,
173     .option         = options,
174     .version        = LIBAVUTIL_VERSION_INT,
175     .category       = AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT,
176 };
177
178 AVInputFormat ff_pulse_demuxer = {
179     .name           = "pulse",
180     .long_name      = NULL_IF_CONFIG_SMALL("Pulse audio input"),
181     .priv_data_size = sizeof(PulseData),
182     .read_header    = pulse_read_header,
183     .read_packet    = pulse_read_packet,
184     .read_close     = pulse_close,
185     .get_device_list = pulse_get_device_list,
186     .flags          = AVFMT_NOFILE,
187     .priv_class     = &pulse_demuxer_class,
188 };