2013-10-04 12:49:07 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Lukasz Marek <lukasz.m.luki@gmail.com>
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg 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.
|
|
|
|
*
|
|
|
|
* FFmpeg 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 FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <pulse/simple.h>
|
|
|
|
#include <pulse/error.h>
|
2013-10-04 15:21:29 +03:00
|
|
|
#include "libavformat/avformat.h"
|
|
|
|
#include "libavformat/internal.h"
|
2013-10-04 12:49:07 +03:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/time.h"
|
|
|
|
#include "libavutil/log.h"
|
2013-10-04 15:21:29 +03:00
|
|
|
#include "pulse_audio_common.h"
|
2013-10-04 12:49:07 +03:00
|
|
|
|
|
|
|
typedef struct PulseData {
|
|
|
|
AVClass *class;
|
|
|
|
const char *server;
|
|
|
|
const char *name;
|
|
|
|
const char *stream_name;
|
|
|
|
const char *device;
|
|
|
|
pa_simple *pa;
|
2013-10-24 16:11:19 +03:00
|
|
|
int64_t timestamp;
|
2014-04-18 02:10:20 +03:00
|
|
|
int buffer_size; /**< Buffer size in bytes */
|
|
|
|
int buffer_duration; /**< Buffer size in ms, recalculated to buffer_size */
|
2013-10-04 12:49:07 +03:00
|
|
|
} PulseData;
|
|
|
|
|
|
|
|
static av_cold int pulse_write_header(AVFormatContext *h)
|
|
|
|
{
|
|
|
|
PulseData *s = h->priv_data;
|
|
|
|
AVStream *st = NULL;
|
|
|
|
int ret;
|
2014-04-18 02:10:20 +03:00
|
|
|
pa_sample_spec sample_spec;
|
|
|
|
pa_buffer_attr buffer_attributes = { -1, -1, -1, -1, -1 };
|
2013-10-04 12:49:07 +03:00
|
|
|
const char *stream_name = s->stream_name;
|
|
|
|
|
2013-10-27 23:27:39 +03:00
|
|
|
if (h->nb_streams != 1 || h->streams[0]->codec->codec_type != AVMEDIA_TYPE_AUDIO) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Only a single audio stream is supported.\n");
|
2013-10-04 12:49:07 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2013-10-27 23:27:39 +03:00
|
|
|
st = h->streams[0];
|
2013-10-04 12:49:07 +03:00
|
|
|
|
2013-10-24 19:10:06 +03:00
|
|
|
if (!stream_name) {
|
2013-10-27 23:18:27 +03:00
|
|
|
if (h->filename[0])
|
2013-10-24 19:10:06 +03:00
|
|
|
stream_name = h->filename;
|
|
|
|
else
|
|
|
|
stream_name = "Playback";
|
|
|
|
}
|
2013-10-04 12:49:07 +03:00
|
|
|
|
2013-11-24 04:35:33 +03:00
|
|
|
if (s->buffer_duration) {
|
|
|
|
int64_t bytes = s->buffer_duration;
|
|
|
|
bytes *= st->codec->channels * st->codec->sample_rate *
|
|
|
|
av_get_bytes_per_sample(st->codec->sample_fmt);
|
|
|
|
bytes /= 1000;
|
2014-04-18 02:10:20 +03:00
|
|
|
buffer_attributes.tlength = FFMAX(s->buffer_size, av_clip64(bytes, 0, UINT32_MAX - 1));
|
2013-11-24 04:35:33 +03:00
|
|
|
av_log(s, AV_LOG_DEBUG,
|
|
|
|
"Buffer duration: %ums recalculated into %"PRId64" bytes buffer.\n",
|
|
|
|
s->buffer_duration, bytes);
|
2014-04-18 02:10:20 +03:00
|
|
|
av_log(s, AV_LOG_DEBUG, "Real buffer length is %u bytes\n", buffer_attributes.tlength);
|
2013-11-24 04:35:33 +03:00
|
|
|
} else if (s->buffer_size)
|
2014-04-18 02:10:20 +03:00
|
|
|
buffer_attributes.tlength = s->buffer_size;
|
2013-11-24 04:35:33 +03:00
|
|
|
|
2014-04-18 02:10:20 +03:00
|
|
|
sample_spec.format = ff_codec_id_to_pulse_format(st->codec->codec_id);
|
|
|
|
sample_spec.rate = st->codec->sample_rate;
|
|
|
|
sample_spec.channels = st->codec->channels;
|
|
|
|
if (!pa_sample_spec_valid(&sample_spec)) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Invalid sample spec.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2013-10-04 12:49:07 +03:00
|
|
|
|
|
|
|
s->pa = pa_simple_new(s->server, // Server
|
|
|
|
s->name, // Application name
|
|
|
|
PA_STREAM_PLAYBACK,
|
|
|
|
s->device, // Device
|
|
|
|
stream_name, // Description of a stream
|
2014-04-18 02:10:20 +03:00
|
|
|
&sample_spec, // Sample format
|
2013-10-04 12:49:07 +03:00
|
|
|
NULL, // Use default channel map
|
2014-04-18 02:10:20 +03:00
|
|
|
&buffer_attributes, // Buffering attributes
|
2013-10-04 12:49:07 +03:00
|
|
|
&ret); // Result
|
|
|
|
|
|
|
|
if (!s->pa) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n", pa_strerror(ret));
|
|
|
|
return AVERROR(EIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int pulse_write_trailer(AVFormatContext *h)
|
|
|
|
{
|
|
|
|
PulseData *s = h->priv_data;
|
|
|
|
pa_simple_flush(s->pa, NULL);
|
|
|
|
pa_simple_free(s->pa);
|
|
|
|
s->pa = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
PulseData *s = h->priv_data;
|
|
|
|
int error;
|
|
|
|
|
2013-10-19 00:41:20 +03:00
|
|
|
if (!pkt) {
|
|
|
|
if (pa_simple_flush(s->pa, &error) < 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "pa_simple_flush failed: %s\n", pa_strerror(error));
|
|
|
|
return AVERROR(EIO);
|
|
|
|
}
|
2013-11-01 17:10:56 +03:00
|
|
|
return 1;
|
2013-10-19 00:41:20 +03:00
|
|
|
}
|
|
|
|
|
2013-10-24 16:11:19 +03:00
|
|
|
if (pkt->dts != AV_NOPTS_VALUE)
|
|
|
|
s->timestamp = pkt->dts;
|
|
|
|
|
|
|
|
if (pkt->duration) {
|
2013-10-28 02:52:11 +03:00
|
|
|
s->timestamp += pkt->duration;
|
2013-10-24 16:11:19 +03:00
|
|
|
} else {
|
2013-10-27 23:27:39 +03:00
|
|
|
AVStream *st = h->streams[0];
|
2013-10-24 16:11:19 +03:00
|
|
|
AVCodecContext *codec_ctx = st->codec;
|
|
|
|
AVRational r = { 1, codec_ctx->sample_rate };
|
|
|
|
int64_t samples = pkt->size / (av_get_bytes_per_sample(codec_ctx->sample_fmt) * codec_ctx->channels);
|
|
|
|
s->timestamp += av_rescale_q(samples, r, st->time_base);
|
|
|
|
}
|
|
|
|
|
2013-10-19 00:41:20 +03:00
|
|
|
if (pa_simple_write(s->pa, pkt->data, pkt->size, &error) < 0) {
|
2013-10-04 12:49:07 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "pa_simple_write failed: %s\n", pa_strerror(error));
|
|
|
|
return AVERROR(EIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-24 19:55:07 +03:00
|
|
|
static int pulse_write_frame(AVFormatContext *h, int stream_index,
|
|
|
|
AVFrame **frame, unsigned flags)
|
|
|
|
{
|
|
|
|
AVPacket pkt;
|
|
|
|
|
|
|
|
/* Planar formats are not supported yet. */
|
|
|
|
if (flags & AV_WRITE_UNCODED_FRAME_QUERY)
|
|
|
|
return av_sample_fmt_is_planar(h->streams[stream_index]->codec->sample_fmt) ?
|
|
|
|
AVERROR(EINVAL) : 0;
|
|
|
|
|
|
|
|
pkt.data = (*frame)->data[0];
|
|
|
|
pkt.size = (*frame)->nb_samples * av_get_bytes_per_sample((*frame)->format) * (*frame)->channels;
|
|
|
|
pkt.dts = (*frame)->pkt_dts;
|
|
|
|
pkt.duration = av_frame_get_pkt_duration(*frame);
|
|
|
|
return pulse_write_packet(h, &pkt);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-04 12:49:07 +03:00
|
|
|
static void pulse_get_output_timestamp(AVFormatContext *h, int stream, int64_t *dts, int64_t *wall)
|
|
|
|
{
|
|
|
|
PulseData *s = h->priv_data;
|
|
|
|
pa_usec_t latency = pa_simple_get_latency(s->pa, NULL);
|
|
|
|
*wall = av_gettime();
|
2013-10-24 16:11:19 +03:00
|
|
|
*dts = s->timestamp - latency;
|
2013-10-04 12:49:07 +03:00
|
|
|
}
|
|
|
|
|
2014-02-25 03:04:15 +03:00
|
|
|
static int pulse_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list)
|
|
|
|
{
|
|
|
|
PulseData *s = h->priv_data;
|
|
|
|
return ff_pulse_audio_get_devices(device_list, s->server, 1);
|
|
|
|
}
|
|
|
|
|
2013-10-04 12:49:07 +03:00
|
|
|
#define OFFSET(a) offsetof(PulseData, a)
|
|
|
|
#define E AV_OPT_FLAG_ENCODING_PARAM
|
|
|
|
static const AVOption options[] = {
|
2014-04-18 02:10:20 +03:00
|
|
|
{ "server", "set PulseAudio server", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
|
|
|
|
{ "name", "set application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT}, 0, 0, E },
|
|
|
|
{ "stream_name", "set stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
|
|
|
|
{ "device", "set device name", OFFSET(device), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
|
|
|
|
{ "buffer_size", "set buffer size in bytes", OFFSET(buffer_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
|
|
|
|
{ "buffer_duration", "set buffer duration in millisecs", OFFSET(buffer_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
|
2013-10-04 12:49:07 +03:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass pulse_muxer_class = {
|
2014-04-18 02:10:20 +03:00
|
|
|
.class_name = "PulseAudio muxer",
|
2013-10-04 12:49:07 +03:00
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
2014-02-23 01:32:51 +03:00
|
|
|
.category = AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT,
|
2013-10-04 12:49:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
AVOutputFormat ff_pulse_muxer = {
|
2014-04-18 02:10:20 +03:00
|
|
|
.name = "pulse",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Pulse audio output"),
|
|
|
|
.priv_data_size = sizeof(PulseData),
|
|
|
|
.audio_codec = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
|
|
|
|
.video_codec = AV_CODEC_ID_NONE,
|
|
|
|
.write_header = pulse_write_header,
|
|
|
|
.write_packet = pulse_write_packet,
|
|
|
|
.write_uncoded_frame = pulse_write_frame,
|
|
|
|
.write_trailer = pulse_write_trailer,
|
2013-10-04 12:49:07 +03:00
|
|
|
.get_output_timestamp = pulse_get_output_timestamp,
|
2014-04-18 02:10:20 +03:00
|
|
|
.get_device_list = pulse_get_device_list,
|
|
|
|
.flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
|
|
|
|
.priv_class = &pulse_muxer_class,
|
2013-10-04 12:49:07 +03:00
|
|
|
};
|