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;
47 pa_buffer_attr attr = { -1, -1, -1, -1, -1 };
48 const char *stream_name = s->stream_name;
50 for (i = 0; i < h->nb_streams; i++) {
51 if (h->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
59 av_log(s, AV_LOG_ERROR, "No audio stream found.\n");
60 return AVERROR(EINVAL);
65 stream_name = h->filename;
67 stream_name = "Playback";
70 ss.format = codec_id_to_pulse_format(st->codec->codec_id);
71 ss.rate = st->codec->sample_rate;
72 ss.channels = st->codec->channels;
74 s->pa = pa_simple_new(s->server, // Server
75 s->name, // Application name
78 stream_name, // Description of a stream
80 NULL, // Use default channel map
81 &attr, // Buffering attributes
85 av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n", pa_strerror(ret));
89 avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
94 static av_cold int pulse_write_trailer(AVFormatContext *h)
96 PulseData *s = h->priv_data;
97 pa_simple_flush(s->pa, NULL);
98 pa_simple_free(s->pa);
103 static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt)
105 PulseData *s = h->priv_data;
109 if (pa_simple_flush(s->pa, &error) < 0) {
110 av_log(s, AV_LOG_ERROR, "pa_simple_flush failed: %s\n", pa_strerror(error));
116 if (s->stream_index != pkt->stream_index)
119 if (pa_simple_write(s->pa, pkt->data, pkt->size, &error) < 0) {
120 av_log(s, AV_LOG_ERROR, "pa_simple_write failed: %s\n", pa_strerror(error));
127 static void pulse_get_output_timestamp(AVFormatContext *h, int stream, int64_t *dts, int64_t *wall)
129 PulseData *s = h->priv_data;
130 pa_usec_t latency = pa_simple_get_latency(s->pa, NULL);
131 *wall = av_gettime();
132 *dts = h->streams[s->stream_index]->cur_dts - latency;
135 #define OFFSET(a) offsetof(PulseData, a)
136 #define E AV_OPT_FLAG_ENCODING_PARAM
138 static const AVOption options[] = {
139 { "server", "set PulseAudio server", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
140 { "name", "set application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT}, 0, 0, E },
141 { "stream_name", "set stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
142 { "device", "set device name", OFFSET(device), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
146 static const AVClass pulse_muxer_class = {
147 .class_name = "Pulse muxer",
148 .item_name = av_default_item_name,
150 .version = LIBAVUTIL_VERSION_INT,
153 AVOutputFormat ff_pulse_muxer = {
155 .long_name = NULL_IF_CONFIG_SMALL("Pulse audio output"),
156 .priv_data_size = sizeof(PulseData),
157 .audio_codec = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
158 .video_codec = AV_CODEC_ID_NONE,
159 .write_header = pulse_write_header,
160 .write_packet = pulse_write_packet,
161 .write_trailer = pulse_write_trailer,
162 .get_output_timestamp = pulse_get_output_timestamp,
163 .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
164 .priv_class = &pulse_muxer_class,