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;
37 unsigned int stream_index;
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 for (unsigned i = 0; i < h->nb_streams; i++) {
50 if (h->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
58 av_log(s, AV_LOG_ERROR, "No audio stream found.\n");
59 return AVERROR(EINVAL);
63 stream_name = h->filename;
65 ss.format = codec_id_to_pulse_format(st->codec->codec_id);
66 ss.rate = st->codec->sample_rate;
67 ss.channels = st->codec->channels;
69 s->pa = pa_simple_new(s->server, // Server
70 s->name, // Application name
73 stream_name, // Description of a stream
75 NULL, // Use default channel map
76 &attr, // Buffering attributes
80 av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n", pa_strerror(ret));
84 avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
89 static av_cold int pulse_write_trailer(AVFormatContext *h)
91 PulseData *s = h->priv_data;
92 pa_simple_flush(s->pa, NULL);
93 pa_simple_free(s->pa);
98 static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt)
100 PulseData *s = h->priv_data;
104 if (pa_simple_flush(s->pa, &error) < 0) {
105 av_log(s, AV_LOG_ERROR, "pa_simple_flush failed: %s\n", pa_strerror(error));
111 if (s->stream_index != pkt->stream_index)
114 if (pa_simple_write(s->pa, pkt->data, pkt->size, &error) < 0) {
115 av_log(s, AV_LOG_ERROR, "pa_simple_write failed: %s\n", pa_strerror(error));
122 static void pulse_get_output_timestamp(AVFormatContext *h, int stream, int64_t *dts, int64_t *wall)
124 PulseData *s = h->priv_data;
125 pa_usec_t latency = pa_simple_get_latency(s->pa, NULL);
126 *wall = av_gettime();
127 *dts = h->streams[s->stream_index]->cur_dts - latency;
130 #define OFFSET(a) offsetof(PulseData, a)
131 #define E AV_OPT_FLAG_ENCODING_PARAM
133 static const AVOption options[] = {
134 { "server", "set PulseAudio server", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
135 { "name", "set application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT}, 0, 0, E },
136 { "stream_name", "set stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
137 { "device", "set device name", OFFSET(device), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
141 static const AVClass pulse_muxer_class = {
142 .class_name = "Pulse muxer",
143 .item_name = av_default_item_name,
145 .version = LIBAVUTIL_VERSION_INT,
148 AVOutputFormat ff_pulse_muxer = {
150 .long_name = NULL_IF_CONFIG_SMALL("Pulse audio output"),
151 .priv_data_size = sizeof(PulseData),
152 .audio_codec = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
153 .video_codec = AV_CODEC_ID_NONE,
154 .write_header = pulse_write_header,
155 .write_packet = pulse_write_packet,
156 .write_trailer = pulse_write_trailer,
157 .get_output_timestamp = pulse_get_output_timestamp,
158 .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
159 .priv_class = &pulse_muxer_class,