2 * Copyright (c) 2013 Lukasz Marek <lukasz.m.luki@gmail.com>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <pulse/simple.h>
22 #include <pulse/error.h>
23 #include "libavformat/avformat.h"
24 #include "libavformat/internal.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/time.h"
27 #include "libavutil/log.h"
28 #include "pulse_audio_common.h"
30 typedef struct PulseData {
34 const char *stream_name;
40 static av_cold int pulse_write_header(AVFormatContext *h)
42 PulseData *s = h->priv_data;
46 pa_buffer_attr attr = { -1, -1, -1, -1, -1 };
47 const char *stream_name = s->stream_name;
49 if (h->nb_streams != 1 || h->streams[0]->codec->codec_type != AVMEDIA_TYPE_AUDIO) {
50 av_log(s, AV_LOG_ERROR, "Only a single audio stream is supported.\n");
51 return AVERROR(EINVAL);
57 stream_name = h->filename;
59 stream_name = "Playback";
62 ss.format = ff_codec_id_to_pulse_format(st->codec->codec_id);
63 ss.rate = st->codec->sample_rate;
64 ss.channels = st->codec->channels;
66 s->pa = pa_simple_new(s->server, // Server
67 s->name, // Application name
70 stream_name, // Description of a stream
72 NULL, // Use default channel map
73 &attr, // Buffering attributes
77 av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n", pa_strerror(ret));
81 avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
86 static av_cold int pulse_write_trailer(AVFormatContext *h)
88 PulseData *s = h->priv_data;
89 pa_simple_flush(s->pa, NULL);
90 pa_simple_free(s->pa);
95 static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt)
97 PulseData *s = h->priv_data;
101 if (pa_simple_flush(s->pa, &error) < 0) {
102 av_log(s, AV_LOG_ERROR, "pa_simple_flush failed: %s\n", pa_strerror(error));
108 if (pkt->dts != AV_NOPTS_VALUE)
109 s->timestamp = pkt->dts;
112 s->timestamp += pkt->duration;
114 AVStream *st = h->streams[0];
115 AVCodecContext *codec_ctx = st->codec;
116 AVRational r = { 1, codec_ctx->sample_rate };
117 int64_t samples = pkt->size / (av_get_bytes_per_sample(codec_ctx->sample_fmt) * codec_ctx->channels);
118 s->timestamp += av_rescale_q(samples, r, st->time_base);
121 if (pa_simple_write(s->pa, pkt->data, pkt->size, &error) < 0) {
122 av_log(s, AV_LOG_ERROR, "pa_simple_write failed: %s\n", pa_strerror(error));
129 static void pulse_get_output_timestamp(AVFormatContext *h, int stream, int64_t *dts, int64_t *wall)
131 PulseData *s = h->priv_data;
132 pa_usec_t latency = pa_simple_get_latency(s->pa, NULL);
133 *wall = av_gettime();
134 *dts = s->timestamp - latency;
137 #define OFFSET(a) offsetof(PulseData, a)
138 #define E AV_OPT_FLAG_ENCODING_PARAM
140 static const AVOption options[] = {
141 { "server", "set PulseAudio server", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
142 { "name", "set application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT}, 0, 0, E },
143 { "stream_name", "set stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
144 { "device", "set device name", OFFSET(device), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
148 static const AVClass pulse_muxer_class = {
149 .class_name = "Pulse muxer",
150 .item_name = av_default_item_name,
152 .version = LIBAVUTIL_VERSION_INT,
155 AVOutputFormat ff_pulse_muxer = {
157 .long_name = NULL_IF_CONFIG_SMALL("Pulse audio output"),
158 .priv_data_size = sizeof(PulseData),
159 .audio_codec = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
160 .video_codec = AV_CODEC_ID_NONE,
161 .write_header = pulse_write_header,
162 .write_packet = pulse_write_packet,
163 .write_trailer = pulse_write_trailer,
164 .get_output_timestamp = pulse_get_output_timestamp,
165 .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
166 .priv_class = &pulse_muxer_class,