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;
42 static av_cold int pulse_write_header(AVFormatContext *h)
44 PulseData *s = h->priv_data;
48 pa_buffer_attr attr = { -1, -1, -1, -1, -1 };
49 const char *stream_name = s->stream_name;
51 if (h->nb_streams != 1 || h->streams[0]->codec->codec_type != AVMEDIA_TYPE_AUDIO) {
52 av_log(s, AV_LOG_ERROR, "Only a single audio stream is supported.\n");
53 return AVERROR(EINVAL);
59 stream_name = h->filename;
61 stream_name = "Playback";
64 if (s->buffer_duration) {
65 int64_t bytes = s->buffer_duration;
66 bytes *= st->codec->channels * st->codec->sample_rate *
67 av_get_bytes_per_sample(st->codec->sample_fmt);
69 attr.tlength = FFMAX(s->buffer_size, av_clip64(bytes, 0, UINT32_MAX - 1));
70 av_log(s, AV_LOG_DEBUG,
71 "Buffer duration: %ums recalculated into %"PRId64" bytes buffer.\n",
72 s->buffer_duration, bytes);
73 av_log(s, AV_LOG_DEBUG, "Real buffer length is %u bytes\n", attr.tlength);
74 } else if (s->buffer_size)
75 attr.tlength = s->buffer_size;
77 ss.format = ff_codec_id_to_pulse_format(st->codec->codec_id);
78 ss.rate = st->codec->sample_rate;
79 ss.channels = st->codec->channels;
81 s->pa = pa_simple_new(s->server, // Server
82 s->name, // Application name
85 stream_name, // Description of a stream
87 NULL, // Use default channel map
88 &attr, // Buffering attributes
92 av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n", pa_strerror(ret));
96 avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
101 static av_cold int pulse_write_trailer(AVFormatContext *h)
103 PulseData *s = h->priv_data;
104 pa_simple_flush(s->pa, NULL);
105 pa_simple_free(s->pa);
110 static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt)
112 PulseData *s = h->priv_data;
116 if (pa_simple_flush(s->pa, &error) < 0) {
117 av_log(s, AV_LOG_ERROR, "pa_simple_flush failed: %s\n", pa_strerror(error));
123 if (pkt->dts != AV_NOPTS_VALUE)
124 s->timestamp = pkt->dts;
127 s->timestamp += pkt->duration;
129 AVStream *st = h->streams[0];
130 AVCodecContext *codec_ctx = st->codec;
131 AVRational r = { 1, codec_ctx->sample_rate };
132 int64_t samples = pkt->size / (av_get_bytes_per_sample(codec_ctx->sample_fmt) * codec_ctx->channels);
133 s->timestamp += av_rescale_q(samples, r, st->time_base);
136 if (pa_simple_write(s->pa, pkt->data, pkt->size, &error) < 0) {
137 av_log(s, AV_LOG_ERROR, "pa_simple_write failed: %s\n", pa_strerror(error));
144 static void pulse_get_output_timestamp(AVFormatContext *h, int stream, int64_t *dts, int64_t *wall)
146 PulseData *s = h->priv_data;
147 pa_usec_t latency = pa_simple_get_latency(s->pa, NULL);
148 *wall = av_gettime();
149 *dts = s->timestamp - latency;
152 #define OFFSET(a) offsetof(PulseData, a)
153 #define E AV_OPT_FLAG_ENCODING_PARAM
155 static const AVOption options[] = {
156 { "server", "set PulseAudio server", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
157 { "name", "set application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT}, 0, 0, E },
158 { "stream_name", "set stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
159 { "device", "set device name", OFFSET(device), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
160 { "buffer_size", "set buffer size in bytes", OFFSET(buffer_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
161 { "buffer_duration", "set buffer duration in millisecs", OFFSET(buffer_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
165 static const AVClass pulse_muxer_class = {
166 .class_name = "Pulse muxer",
167 .item_name = av_default_item_name,
169 .version = LIBAVUTIL_VERSION_INT,
172 AVOutputFormat ff_pulse_muxer = {
174 .long_name = NULL_IF_CONFIG_SMALL("Pulse audio output"),
175 .priv_data_size = sizeof(PulseData),
176 .audio_codec = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
177 .video_codec = AV_CODEC_ID_NONE,
178 .write_header = pulse_write_header,
179 .write_packet = pulse_write_packet,
180 .write_trailer = pulse_write_trailer,
181 .get_output_timestamp = pulse_get_output_timestamp,
182 .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
183 .priv_class = &pulse_muxer_class,