2011-10-26 16:27:53 +00:00
|
|
|
/*
|
|
|
|
* Pulseaudio input
|
|
|
|
* Copyright (c) 2011 Luca Barbato <lu_zero@gentoo.org>
|
|
|
|
*
|
|
|
|
* This file is part of Libav.
|
|
|
|
*
|
|
|
|
* Libav is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Libav is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with Libav; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* PulseAudio input using the simple API.
|
|
|
|
* @author Luca Barbato <lu_zero@gentoo.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <pulse/simple.h>
|
|
|
|
#include <pulse/rtclock.h>
|
|
|
|
#include <pulse/error.h>
|
|
|
|
|
2015-04-08 15:27:42 +00:00
|
|
|
#include "libavutil/internal.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/time.h"
|
|
|
|
|
2011-10-26 16:27:53 +00:00
|
|
|
#include "libavformat/avformat.h"
|
2011-11-29 18:28:15 +00:00
|
|
|
#include "libavformat/internal.h"
|
2011-10-26 16:27:53 +00:00
|
|
|
|
2012-08-05 09:11:04 +00:00
|
|
|
#define DEFAULT_CODEC_ID AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE)
|
2011-10-26 16:27:53 +00:00
|
|
|
|
|
|
|
typedef struct PulseData {
|
|
|
|
AVClass *class;
|
|
|
|
char *server;
|
|
|
|
char *name;
|
|
|
|
char *stream_name;
|
|
|
|
int sample_rate;
|
|
|
|
int channels;
|
|
|
|
int frame_size;
|
|
|
|
int fragment_size;
|
|
|
|
pa_simple *s;
|
|
|
|
int64_t pts;
|
2011-11-14 14:53:59 +00:00
|
|
|
int64_t frame_duration;
|
2014-08-23 17:03:21 +00:00
|
|
|
int wallclock;
|
2011-10-26 16:27:53 +00:00
|
|
|
} PulseData;
|
|
|
|
|
|
|
|
static pa_sample_format_t codec_id_to_pulse_format(int codec_id) {
|
|
|
|
switch (codec_id) {
|
2012-08-05 09:11:04 +00:00
|
|
|
case AV_CODEC_ID_PCM_U8: return PA_SAMPLE_U8;
|
|
|
|
case AV_CODEC_ID_PCM_ALAW: return PA_SAMPLE_ALAW;
|
|
|
|
case AV_CODEC_ID_PCM_MULAW: return PA_SAMPLE_ULAW;
|
|
|
|
case AV_CODEC_ID_PCM_S16LE: return PA_SAMPLE_S16LE;
|
|
|
|
case AV_CODEC_ID_PCM_S16BE: return PA_SAMPLE_S16BE;
|
|
|
|
case AV_CODEC_ID_PCM_F32LE: return PA_SAMPLE_FLOAT32LE;
|
|
|
|
case AV_CODEC_ID_PCM_F32BE: return PA_SAMPLE_FLOAT32BE;
|
|
|
|
case AV_CODEC_ID_PCM_S32LE: return PA_SAMPLE_S32LE;
|
|
|
|
case AV_CODEC_ID_PCM_S32BE: return PA_SAMPLE_S32BE;
|
|
|
|
case AV_CODEC_ID_PCM_S24LE: return PA_SAMPLE_S24LE;
|
|
|
|
case AV_CODEC_ID_PCM_S24BE: return PA_SAMPLE_S24BE;
|
2011-10-26 16:27:53 +00:00
|
|
|
default: return PA_SAMPLE_INVALID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-12 12:20:36 +00:00
|
|
|
static av_cold int pulse_read_header(AVFormatContext *s)
|
2011-10-26 16:27:53 +00:00
|
|
|
{
|
|
|
|
PulseData *pd = s->priv_data;
|
|
|
|
AVStream *st;
|
|
|
|
char *device = NULL;
|
|
|
|
int ret;
|
2012-08-05 09:11:04 +00:00
|
|
|
enum AVCodecID codec_id =
|
|
|
|
s->audio_codec_id == AV_CODEC_ID_NONE ? DEFAULT_CODEC_ID : s->audio_codec_id;
|
2011-10-26 16:27:53 +00:00
|
|
|
const pa_sample_spec ss = { codec_id_to_pulse_format(codec_id),
|
|
|
|
pd->sample_rate,
|
|
|
|
pd->channels };
|
|
|
|
|
|
|
|
pa_buffer_attr attr = { -1 };
|
|
|
|
|
|
|
|
st = avformat_new_stream(s, NULL);
|
|
|
|
|
|
|
|
if (!st) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Cannot add stream\n");
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
attr.fragsize = pd->fragment_size;
|
|
|
|
|
|
|
|
if (strcmp(s->filename, "default"))
|
|
|
|
device = s->filename;
|
|
|
|
|
|
|
|
pd->s = pa_simple_new(pd->server, pd->name,
|
|
|
|
PA_STREAM_RECORD,
|
|
|
|
device, pd->stream_name, &ss,
|
|
|
|
NULL, &attr, &ret);
|
|
|
|
|
|
|
|
if (!pd->s) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n",
|
|
|
|
pa_strerror(ret));
|
|
|
|
return AVERROR(EIO);
|
|
|
|
}
|
|
|
|
/* take real parameters */
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
st->codecpar->codec_id = codec_id;
|
|
|
|
st->codecpar->sample_rate = pd->sample_rate;
|
|
|
|
st->codecpar->channels = pd->channels;
|
2011-11-29 18:28:15 +00:00
|
|
|
avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
|
2011-10-26 16:27:53 +00:00
|
|
|
|
|
|
|
pd->pts = AV_NOPTS_VALUE;
|
2011-11-14 14:53:59 +00:00
|
|
|
pd->frame_duration = (pd->frame_size * 1000000LL * 8) /
|
|
|
|
(pd->sample_rate * pd->channels * av_get_bits_per_sample(codec_id));
|
2011-10-26 16:27:53 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
PulseData *pd = s->priv_data;
|
|
|
|
int res;
|
|
|
|
pa_usec_t latency;
|
|
|
|
|
|
|
|
if (av_new_packet(pkt, pd->frame_size) < 0) {
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((pa_simple_read(pd->s, pkt->data, pkt->size, &res)) < 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "pa_simple_read failed: %s\n",
|
|
|
|
pa_strerror(res));
|
2015-10-23 09:11:31 +00:00
|
|
|
av_packet_unref(pkt);
|
2011-10-26 16:27:53 +00:00
|
|
|
return AVERROR(EIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((latency = pa_simple_get_latency(pd->s, &res)) == (pa_usec_t) -1) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "pa_simple_get_latency() failed: %s\n",
|
|
|
|
pa_strerror(res));
|
|
|
|
return AVERROR(EIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pd->pts == AV_NOPTS_VALUE) {
|
|
|
|
pd->pts = -latency;
|
2014-08-23 17:03:21 +00:00
|
|
|
if (pd->wallclock)
|
|
|
|
pd->pts += av_gettime();
|
2011-10-26 16:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pkt->pts = pd->pts;
|
|
|
|
|
2011-11-14 14:53:59 +00:00
|
|
|
pd->pts += pd->frame_duration;
|
2011-10-26 16:27:53 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int pulse_close(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
PulseData *pd = s->priv_data;
|
|
|
|
pa_simple_free(pd->s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define OFFSET(a) offsetof(PulseData, a)
|
|
|
|
#define D AV_OPT_FLAG_DECODING_PARAM
|
|
|
|
|
|
|
|
static const AVOption options[] = {
|
|
|
|
{ "server", "pulse server name", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, D },
|
|
|
|
{ "name", "application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = "libav"}, 0, 0, D },
|
|
|
|
{ "stream_name", "stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = "record"}, 0, 0, D },
|
2012-08-31 10:22:31 +00:00
|
|
|
{ "sample_rate", "sample rate in Hz", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, D },
|
|
|
|
{ "channels", "number of audio channels", OFFSET(channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, D },
|
|
|
|
{ "frame_size", "number of bytes per frame", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, D },
|
|
|
|
{ "fragment_size", "buffering size, affects latency and cpu usage", OFFSET(fragment_size), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D },
|
2014-08-24 12:13:45 +00:00
|
|
|
{ "wallclock", "set the initial pts using the current time", OFFSET(wallclock), AV_OPT_TYPE_INT, {.i64 = 1}, -1, 1, D },
|
2011-10-26 16:27:53 +00:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass pulse_demuxer_class = {
|
|
|
|
.class_name = "Pulse demuxer",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
|
|
|
AVInputFormat ff_pulse_demuxer = {
|
|
|
|
.name = "pulse",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Pulse audio input"),
|
|
|
|
.priv_data_size = sizeof(PulseData),
|
|
|
|
.read_header = pulse_read_header,
|
|
|
|
.read_packet = pulse_read_packet,
|
|
|
|
.read_close = pulse_close,
|
|
|
|
.flags = AVFMT_NOFILE,
|
|
|
|
.priv_class = &pulse_demuxer_class,
|
|
|
|
};
|