]> git.sesse.net Git - ffmpeg/blob - libavdevice/pulse_audio_enc.c
Merge commit 'b6d5e6aa10a2f09351f287e876f7ed4504e1f75d'
[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     int64_t timestamp;
38     int buffer_size;
39     int buffer_duration;
40 } PulseData;
41
42 static av_cold int pulse_write_header(AVFormatContext *h)
43 {
44     PulseData *s = h->priv_data;
45     AVStream *st = NULL;
46     int ret;
47     pa_sample_spec ss;
48     pa_buffer_attr attr = { -1, -1, -1, -1, -1 };
49     const char *stream_name = s->stream_name;
50
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);
54     }
55     st = h->streams[0];
56
57     if (!stream_name) {
58         if (h->filename[0])
59             stream_name = h->filename;
60         else
61             stream_name = "Playback";
62     }
63
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);
68         bytes /= 1000;
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;
76
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;
80
81     s->pa = pa_simple_new(s->server,                 // Server
82                           s->name,                   // Application name
83                           PA_STREAM_PLAYBACK,
84                           s->device,                 // Device
85                           stream_name,               // Description of a stream
86                           &ss,                       // Sample format
87                           NULL,                      // Use default channel map
88                           &attr,                     // Buffering attributes
89                           &ret);                     // Result
90
91     if (!s->pa) {
92         av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n", pa_strerror(ret));
93         return AVERROR(EIO);
94     }
95
96     avpriv_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
97
98     return 0;
99 }
100
101 static av_cold int pulse_write_trailer(AVFormatContext *h)
102 {
103     PulseData *s = h->priv_data;
104     pa_simple_flush(s->pa, NULL);
105     pa_simple_free(s->pa);
106     s->pa = NULL;
107     return 0;
108 }
109
110 static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt)
111 {
112     PulseData *s = h->priv_data;
113     int error;
114
115     if (!pkt) {
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));
118             return AVERROR(EIO);
119         }
120         return 1;
121     }
122
123     if (pkt->dts != AV_NOPTS_VALUE)
124         s->timestamp = pkt->dts;
125
126     if (pkt->duration) {
127         s->timestamp += pkt->duration;
128     } else {
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);
134     }
135
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));
138         return AVERROR(EIO);
139     }
140
141     return 0;
142 }
143
144 static void pulse_get_output_timestamp(AVFormatContext *h, int stream, int64_t *dts, int64_t *wall)
145 {
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;
150 }
151
152 #define OFFSET(a) offsetof(PulseData, a)
153 #define E AV_OPT_FLAG_ENCODING_PARAM
154
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 },
162     { NULL }
163 };
164
165 static const AVClass pulse_muxer_class = {
166     .class_name     = "Pulse muxer",
167     .item_name      = av_default_item_name,
168     .option         = options,
169     .version        = LIBAVUTIL_VERSION_INT,
170 };
171
172 AVOutputFormat ff_pulse_muxer = {
173     .name           = "pulse",
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,
184 };