]> git.sesse.net Git - ffmpeg/blob - libavdevice/pulse_audio_enc.c
Merge commit 'afa93d198aaf2cc661c4df6d4095cd030265d30a'
[ffmpeg] / libavdevice / pulse_audio_enc.c
1 /*
2  * Copyright (c) 2013 Lukasz Marek <lukasz.m.luki@gmail.com>
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
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"
29
30 typedef struct PulseData {
31     AVClass *class;
32     const char *server;
33     const char *name;
34     const char *stream_name;
35     const char *device;
36     pa_simple *pa;
37     unsigned int stream_index;
38 } PulseData;
39
40 static av_cold int pulse_write_header(AVFormatContext *h)
41 {
42     PulseData *s = h->priv_data;
43     AVStream *st = NULL;
44     int ret;
45     unsigned int i;
46     pa_sample_spec ss;
47     pa_buffer_attr attr = { -1, -1, -1, -1, -1 };
48     const char *stream_name = s->stream_name;
49
50     for (i = 0; i < h->nb_streams; i++) {
51         if (h->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
52             st = h->streams[i];
53             s->stream_index = i;
54             break;
55         }
56     }
57
58     if (!st) {
59         av_log(s, AV_LOG_ERROR, "No audio stream found.\n");
60         return AVERROR(EINVAL);
61     }
62
63     if (!stream_name) {
64         if (h->filename)
65             stream_name = h->filename;
66         else
67             stream_name = "Playback";
68     }
69
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;
73
74     s->pa = pa_simple_new(s->server,                 // Server
75                           s->name,                   // Application name
76                           PA_STREAM_PLAYBACK,
77                           s->device,                 // Device
78                           stream_name,               // Description of a stream
79                           &ss,                       // Sample format
80                           NULL,                      // Use default channel map
81                           &attr,                     // Buffering attributes
82                           &ret);                     // Result
83
84     if (!s->pa) {
85         av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n", pa_strerror(ret));
86         return AVERROR(EIO);
87     }
88
89     avpriv_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
90
91     return 0;
92 }
93
94 static av_cold int pulse_write_trailer(AVFormatContext *h)
95 {
96     PulseData *s = h->priv_data;
97     pa_simple_flush(s->pa, NULL);
98     pa_simple_free(s->pa);
99     s->pa = NULL;
100     return 0;
101 }
102
103 static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt)
104 {
105     PulseData *s = h->priv_data;
106     int error;
107
108     if (!pkt) {
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));
111             return AVERROR(EIO);
112         }
113         return 0;
114     }
115
116     if (s->stream_index != pkt->stream_index)
117         return 0;
118
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));
121         return AVERROR(EIO);
122     }
123
124     return 0;
125 }
126
127 static void pulse_get_output_timestamp(AVFormatContext *h, int stream, int64_t *dts, int64_t *wall)
128 {
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;
133 }
134
135 #define OFFSET(a) offsetof(PulseData, a)
136 #define E AV_OPT_FLAG_ENCODING_PARAM
137
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 },
143     { NULL }
144 };
145
146 static const AVClass pulse_muxer_class = {
147     .class_name     = "Pulse muxer",
148     .item_name      = av_default_item_name,
149     .option         = options,
150     .version        = LIBAVUTIL_VERSION_INT,
151 };
152
153 AVOutputFormat ff_pulse_muxer = {
154     .name           = "pulse",
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,
165 };