]> git.sesse.net Git - ffmpeg/blob - libavdevice/pulse_audio_enc.c
Merge remote-tracking branch 'qatar/master'
[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     pa_sample_spec ss;
46     pa_buffer_attr attr = { -1, -1, -1, -1, -1 };
47     const char *stream_name = s->stream_name;
48
49     for (unsigned i = 0; i < h->nb_streams; i++) {
50         if (h->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
51             st = h->streams[i];
52             s->stream_index = i;
53             break;
54         }
55     }
56
57     if (!st) {
58         av_log(s, AV_LOG_ERROR, "No audio stream found.\n");
59         return AVERROR(EINVAL);
60     }
61
62     if (!stream_name)
63         stream_name = h->filename;
64
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;
68
69     s->pa = pa_simple_new(s->server,                 // Server
70                           s->name,                   // Application name
71                           PA_STREAM_PLAYBACK,
72                           s->device,                 // Device
73                           stream_name,               // Description of a stream
74                           &ss,                       // Sample format
75                           NULL,                      // Use default channel map
76                           &attr,                     // Buffering attributes
77                           &ret);                     // Result
78
79     if (!s->pa) {
80         av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n", pa_strerror(ret));
81         return AVERROR(EIO);
82     }
83
84     avpriv_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
85
86     return 0;
87 }
88
89 static av_cold int pulse_write_trailer(AVFormatContext *h)
90 {
91     PulseData *s = h->priv_data;
92     pa_simple_flush(s->pa, NULL);
93     pa_simple_free(s->pa);
94     s->pa = NULL;
95     return 0;
96 }
97
98 static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt)
99 {
100     PulseData *s = h->priv_data;
101     int error;
102
103     if (!pkt) {
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));
106             return AVERROR(EIO);
107         }
108         return 0;
109     }
110
111     if (s->stream_index != pkt->stream_index)
112         return 0;
113
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));
116         return AVERROR(EIO);
117     }
118
119     return 0;
120 }
121
122 static void pulse_get_output_timestamp(AVFormatContext *h, int stream, int64_t *dts, int64_t *wall)
123 {
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;
128 }
129
130 #define OFFSET(a) offsetof(PulseData, a)
131 #define E AV_OPT_FLAG_ENCODING_PARAM
132
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 },
138     { NULL }
139 };
140
141 static const AVClass pulse_muxer_class = {
142     .class_name     = "Pulse muxer",
143     .item_name      = av_default_item_name,
144     .option         = options,
145     .version        = LIBAVUTIL_VERSION_INT,
146 };
147
148 AVOutputFormat ff_pulse_muxer = {
149     .name           = "pulse",
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,
160 };