lavd/pulse_audio_enc: add buffer fullness messages
[ffmpeg.git] / 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/pulseaudio.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 "libavutil/attributes.h"
29 #include "pulse_audio_common.h"
30
31 typedef struct PulseData {
32     AVClass *class;
33     const char *server;
34     const char *name;
35     const char *stream_name;
36     const char *device;
37     int64_t timestamp;
38     int buffer_size;               /**< Buffer size in bytes */
39     int buffer_duration;           /**< Buffer size in ms, recalculated to buffer_size */
40     int last_result;
41     pa_threaded_mainloop *mainloop;
42     pa_context *ctx;
43     pa_stream *stream;
44     int nonblocking;
45 } PulseData;
46
47 static void pulse_stream_writable(pa_stream *stream, size_t nbytes, void *userdata)
48 {
49     AVFormatContext *h = userdata;
50     PulseData *s = h->priv_data;
51     int64_t val = nbytes;
52
53     if (stream != s->stream)
54         return;
55
56     avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_BUFFER_WRITABLE, &val, sizeof(val));
57     pa_threaded_mainloop_signal(s->mainloop, 0);
58 }
59
60 static void pulse_overflow(pa_stream *stream, void *userdata)
61 {
62     AVFormatContext *h = userdata;
63     avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_BUFFER_OVERFLOW, NULL, 0);
64 }
65
66 static void pulse_underflow(pa_stream *stream, void *userdata)
67 {
68     AVFormatContext *h = userdata;
69     avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_BUFFER_UNDERFLOW, NULL, 0);
70 }
71
72 static void pulse_stream_state(pa_stream *stream, void *userdata)
73 {
74     PulseData *s = userdata;
75
76     if (stream != s->stream)
77         return;
78
79     switch (pa_stream_get_state(s->stream)) {
80         case PA_STREAM_READY:
81         case PA_STREAM_FAILED:
82         case PA_STREAM_TERMINATED:
83             pa_threaded_mainloop_signal(s->mainloop, 0);
84         default:
85             break;
86     }
87 }
88
89 static int pulse_stream_wait(PulseData *s)
90 {
91     pa_stream_state_t state;
92
93     while ((state = pa_stream_get_state(s->stream)) != PA_STREAM_READY) {
94         if (state == PA_STREAM_FAILED || state == PA_STREAM_TERMINATED)
95             return AVERROR_EXTERNAL;
96         pa_threaded_mainloop_wait(s->mainloop);
97     }
98     return 0;
99 }
100
101 static void pulse_context_state(pa_context *ctx, void *userdata)
102 {
103     PulseData *s = userdata;
104
105     if (s->ctx != ctx)
106         return;
107
108     switch (pa_context_get_state(ctx)) {
109         case PA_CONTEXT_READY:
110         case PA_CONTEXT_FAILED:
111         case PA_CONTEXT_TERMINATED:
112             pa_threaded_mainloop_signal(s->mainloop, 0);
113         default:
114             break;
115     }
116 }
117
118 static int pulse_context_wait(PulseData *s)
119 {
120     pa_context_state_t state;
121
122     while ((state = pa_context_get_state(s->ctx)) != PA_CONTEXT_READY) {
123         if (state == PA_CONTEXT_FAILED || state == PA_CONTEXT_TERMINATED)
124             return AVERROR_EXTERNAL;
125         pa_threaded_mainloop_wait(s->mainloop);
126     }
127     return 0;
128 }
129
130 static void pulse_stream_result(pa_stream *stream, int success, void *userdata)
131 {
132     PulseData *s = userdata;
133
134     if (stream != s->stream)
135         return;
136
137     s->last_result = success ? 0 : AVERROR_EXTERNAL;
138     pa_threaded_mainloop_signal(s->mainloop, 0);
139 }
140
141 static int pulse_finish_stream_operation(PulseData *s, pa_operation *op, const char *name)
142 {
143     if (!op) {
144         pa_threaded_mainloop_unlock(s->mainloop);
145         av_log(s, AV_LOG_ERROR, "%s failed.\n", name);
146         return AVERROR_EXTERNAL;
147     }
148     s->last_result = 2;
149     while (s->last_result == 2)
150         pa_threaded_mainloop_wait(s->mainloop);
151     pa_operation_unref(op);
152     pa_threaded_mainloop_unlock(s->mainloop);
153     if (s->last_result != 0)
154         av_log(s, AV_LOG_ERROR, "%s failed.\n", name);
155     return s->last_result;
156 }
157
158 static int pulse_flash_stream(PulseData *s)
159 {
160     pa_operation *op;
161     pa_threaded_mainloop_lock(s->mainloop);
162     op = pa_stream_flush(s->stream, pulse_stream_result, s);
163     return pulse_finish_stream_operation(s, op, "pa_stream_flush");
164 }
165
166 static void pulse_map_channels_to_pulse(int64_t channel_layout, pa_channel_map *channel_map)
167 {
168     channel_map->channels = 0;
169     if (channel_layout & AV_CH_FRONT_LEFT)
170         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
171     if (channel_layout & AV_CH_FRONT_RIGHT)
172         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
173     if (channel_layout & AV_CH_FRONT_CENTER)
174         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_CENTER;
175     if (channel_layout & AV_CH_LOW_FREQUENCY)
176         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_LFE;
177     if (channel_layout & AV_CH_BACK_LEFT)
178         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_REAR_LEFT;
179     if (channel_layout & AV_CH_BACK_RIGHT)
180         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_REAR_RIGHT;
181     if (channel_layout & AV_CH_FRONT_LEFT_OF_CENTER)
182         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER;
183     if (channel_layout & AV_CH_FRONT_RIGHT_OF_CENTER)
184         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
185     if (channel_layout & AV_CH_BACK_CENTER)
186         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_REAR_CENTER;
187     if (channel_layout & AV_CH_SIDE_LEFT)
188         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_SIDE_LEFT;
189     if (channel_layout & AV_CH_SIDE_RIGHT)
190         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_SIDE_RIGHT;
191     if (channel_layout & AV_CH_TOP_CENTER)
192         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_CENTER;
193     if (channel_layout & AV_CH_TOP_FRONT_LEFT)
194         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_FRONT_LEFT;
195     if (channel_layout & AV_CH_TOP_FRONT_CENTER)
196         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_FRONT_CENTER;
197     if (channel_layout & AV_CH_TOP_FRONT_RIGHT)
198         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_FRONT_RIGHT;
199     if (channel_layout & AV_CH_TOP_BACK_LEFT)
200         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_REAR_LEFT;
201     if (channel_layout & AV_CH_TOP_BACK_CENTER)
202         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_REAR_CENTER;
203     if (channel_layout & AV_CH_TOP_BACK_RIGHT)
204         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
205     if (channel_layout & AV_CH_STEREO_LEFT)
206         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
207     if (channel_layout & AV_CH_STEREO_RIGHT)
208         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
209     if (channel_layout & AV_CH_WIDE_LEFT)
210         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX0;
211     if (channel_layout & AV_CH_WIDE_RIGHT)
212         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX1;
213     if (channel_layout & AV_CH_SURROUND_DIRECT_LEFT)
214         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX2;
215     if (channel_layout & AV_CH_SURROUND_DIRECT_RIGHT)
216         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX3;
217     if (channel_layout & AV_CH_LOW_FREQUENCY_2)
218         channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_LFE;
219 }
220
221 static av_cold int pulse_write_trailer(AVFormatContext *h)
222 {
223     PulseData *s = h->priv_data;
224
225     if (s->mainloop) {
226         pa_threaded_mainloop_lock(s->mainloop);
227         if (s->stream) {
228             pa_stream_disconnect(s->stream);
229             pa_stream_set_state_callback(s->stream, NULL, NULL);
230             pa_stream_set_write_callback(s->stream, NULL, NULL);
231             pa_stream_set_overflow_callback(s->stream, NULL, NULL);
232             pa_stream_set_underflow_callback(s->stream, NULL, NULL);
233             pa_stream_unref(s->stream);
234             s->stream = NULL;
235         }
236         if (s->ctx) {
237             pa_context_disconnect(s->ctx);
238             pa_context_set_state_callback(s->ctx, NULL, NULL);
239             pa_context_unref(s->ctx);
240             s->ctx = NULL;
241         }
242         pa_threaded_mainloop_unlock(s->mainloop);
243         pa_threaded_mainloop_stop(s->mainloop);
244         pa_threaded_mainloop_free(s->mainloop);
245         s->mainloop = NULL;
246     }
247
248     return 0;
249 }
250
251 static av_cold int pulse_write_header(AVFormatContext *h)
252 {
253     PulseData *s = h->priv_data;
254     AVStream *st = NULL;
255     int ret;
256     pa_sample_spec sample_spec;
257     pa_buffer_attr buffer_attributes = { -1, -1, -1, -1, -1 };
258     pa_channel_map channel_map;
259     pa_mainloop_api *mainloop_api;
260     const char *stream_name = s->stream_name;
261     static const pa_stream_flags_t stream_flags = PA_STREAM_INTERPOLATE_TIMING |
262                                                   PA_STREAM_AUTO_TIMING_UPDATE |
263                                                   PA_STREAM_NOT_MONOTONIC;
264
265     if (h->nb_streams != 1 || h->streams[0]->codec->codec_type != AVMEDIA_TYPE_AUDIO) {
266         av_log(s, AV_LOG_ERROR, "Only a single audio stream is supported.\n");
267         return AVERROR(EINVAL);
268     }
269     st = h->streams[0];
270
271     if (!stream_name) {
272         if (h->filename[0])
273             stream_name = h->filename;
274         else
275             stream_name = "Playback";
276     }
277     s->nonblocking = (h->flags & AVFMT_FLAG_NONBLOCK);
278
279     if (s->buffer_duration) {
280         int64_t bytes = s->buffer_duration;
281         bytes *= st->codec->channels * st->codec->sample_rate *
282                  av_get_bytes_per_sample(st->codec->sample_fmt);
283         bytes /= 1000;
284         buffer_attributes.tlength = FFMAX(s->buffer_size, av_clip64(bytes, 0, UINT32_MAX - 1));
285         av_log(s, AV_LOG_DEBUG,
286                "Buffer duration: %ums recalculated into %"PRId64" bytes buffer.\n",
287                s->buffer_duration, bytes);
288         av_log(s, AV_LOG_DEBUG, "Real buffer length is %u bytes\n", buffer_attributes.tlength);
289     } else if (s->buffer_size)
290         buffer_attributes.tlength = s->buffer_size;
291
292     sample_spec.format = ff_codec_id_to_pulse_format(st->codec->codec_id);
293     sample_spec.rate = st->codec->sample_rate;
294     sample_spec.channels = st->codec->channels;
295     if (!pa_sample_spec_valid(&sample_spec)) {
296         av_log(s, AV_LOG_ERROR, "Invalid sample spec.\n");
297         return AVERROR(EINVAL);
298     }
299
300     if (sample_spec.channels == 1) {
301         channel_map.channels = 1;
302         channel_map.map[0] = PA_CHANNEL_POSITION_MONO;
303     } else if (st->codec->channel_layout) {
304         if (av_get_channel_layout_nb_channels(st->codec->channel_layout) != st->codec->channels)
305             return AVERROR(EINVAL);
306         pulse_map_channels_to_pulse(st->codec->channel_layout, &channel_map);
307         /* Unknown channel is present in channel_layout, let PulseAudio use its default. */
308         if (channel_map.channels != sample_spec.channels) {
309             av_log(s, AV_LOG_WARNING, "Unknown channel. Using defaul channel map.\n");
310             channel_map.channels = 0;
311         }
312     } else
313         channel_map.channels = 0;
314
315     if (!channel_map.channels)
316         av_log(s, AV_LOG_WARNING, "Using PulseAudio's default channel map.\n");
317     else if (!pa_channel_map_valid(&channel_map)) {
318         av_log(s, AV_LOG_ERROR, "Invalid channel map.\n");
319         return AVERROR(EINVAL);
320     }
321
322     /* start main loop */
323     s->mainloop = pa_threaded_mainloop_new();
324     if (!s->mainloop) {
325         av_log(s, AV_LOG_ERROR, "Cannot create threaded mainloop.\n");
326         return AVERROR(ENOMEM);
327     }
328     if ((ret = pa_threaded_mainloop_start(s->mainloop)) < 0) {
329         av_log(s, AV_LOG_ERROR, "Cannot start threaded mainloop: %s.\n", pa_strerror(ret));
330         pa_threaded_mainloop_free(s->mainloop);
331         s->mainloop = NULL;
332         return AVERROR_EXTERNAL;
333     }
334
335     pa_threaded_mainloop_lock(s->mainloop);
336
337     mainloop_api = pa_threaded_mainloop_get_api(s->mainloop);
338     if (!mainloop_api) {
339         av_log(s, AV_LOG_ERROR, "Cannot get mainloop API.\n");
340         ret = AVERROR_EXTERNAL;
341         goto fail;
342     }
343
344     s->ctx = pa_context_new(mainloop_api, s->name);
345     if (!s->ctx) {
346         av_log(s, AV_LOG_ERROR, "Cannot create context.\n");
347         ret = AVERROR(ENOMEM);
348         goto fail;
349     }
350     pa_context_set_state_callback(s->ctx, pulse_context_state, s);
351
352     if ((ret = pa_context_connect(s->ctx, s->server, 0, NULL)) < 0) {
353         av_log(s, AV_LOG_ERROR, "Cannot connect context: %s.\n", pa_strerror(ret));
354         ret = AVERROR_EXTERNAL;
355         goto fail;
356     }
357
358     if ((ret = pulse_context_wait(s)) < 0) {
359         av_log(s, AV_LOG_ERROR, "Context failed.\n");
360         goto fail;
361     }
362
363     s->stream = pa_stream_new(s->ctx, stream_name, &sample_spec,
364                               channel_map.channels ? &channel_map : NULL);
365     if (!s->stream) {
366         av_log(s, AV_LOG_ERROR, "Cannot create stream.\n");
367         ret = AVERROR(ENOMEM);
368         goto fail;
369     }
370     pa_stream_set_state_callback(s->stream, pulse_stream_state, s);
371     pa_stream_set_write_callback(s->stream, pulse_stream_writable, h);
372     pa_stream_set_overflow_callback(s->stream, pulse_overflow, h);
373     pa_stream_set_underflow_callback(s->stream, pulse_underflow, h);
374
375     if ((ret = pa_stream_connect_playback(s->stream, s->device, &buffer_attributes,
376                                           stream_flags, NULL, NULL)) < 0) {
377         av_log(s, AV_LOG_ERROR, "pa_stream_connect_playback failed: %s.\n", pa_strerror(ret));
378         ret = AVERROR_EXTERNAL;
379         goto fail;
380     }
381
382     if ((ret = pulse_stream_wait(s)) < 0) {
383         av_log(s, AV_LOG_ERROR, "Stream failed.\n");
384         goto fail;
385     }
386
387     pa_threaded_mainloop_unlock(s->mainloop);
388
389     avpriv_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
390
391     return 0;
392   fail:
393     pa_threaded_mainloop_unlock(s->mainloop);
394     pulse_write_trailer(h);
395     return ret;
396 }
397
398 static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt)
399 {
400     PulseData *s = h->priv_data;
401     int ret;
402
403     if (!pkt)
404         return pulse_flash_stream(s);
405
406     if (pkt->dts != AV_NOPTS_VALUE)
407         s->timestamp = pkt->dts;
408
409     if (pkt->duration) {
410         s->timestamp += pkt->duration;
411     } else {
412         AVStream *st = h->streams[0];
413         AVCodecContext *codec_ctx = st->codec;
414         AVRational r = { 1, codec_ctx->sample_rate };
415         int64_t samples = pkt->size / (av_get_bytes_per_sample(codec_ctx->sample_fmt) * codec_ctx->channels);
416         s->timestamp += av_rescale_q(samples, r, st->time_base);
417     }
418
419     pa_threaded_mainloop_lock(s->mainloop);
420     if (!PA_STREAM_IS_GOOD(pa_stream_get_state(s->stream))) {
421         av_log(s, AV_LOG_ERROR, "PulseAudio stream is in invalid state.\n");
422         goto fail;
423     }
424     while (!pa_stream_writable_size(s->stream)) {
425         if (s->nonblocking) {
426             pa_threaded_mainloop_unlock(s->mainloop);
427             return AVERROR(EAGAIN);
428         } else
429             pa_threaded_mainloop_wait(s->mainloop);
430     }
431
432     if ((ret = pa_stream_write(s->stream, pkt->data, pkt->size, NULL, 0, PA_SEEK_RELATIVE)) < 0) {
433         av_log(s, AV_LOG_ERROR, "pa_stream_write failed: %s\n", pa_strerror(ret));
434         goto fail;
435     }
436     pa_threaded_mainloop_unlock(s->mainloop);
437
438     return 0;
439   fail:
440     pa_threaded_mainloop_unlock(s->mainloop);
441     return AVERROR_EXTERNAL;
442 }
443
444 static int pulse_write_frame(AVFormatContext *h, int stream_index,
445                              AVFrame **frame, unsigned flags)
446 {
447     AVPacket pkt;
448
449     /* Planar formats are not supported yet. */
450     if (flags & AV_WRITE_UNCODED_FRAME_QUERY)
451         return av_sample_fmt_is_planar(h->streams[stream_index]->codec->sample_fmt) ?
452                AVERROR(EINVAL) : 0;
453
454     pkt.data     = (*frame)->data[0];
455     pkt.size     = (*frame)->nb_samples * av_get_bytes_per_sample((*frame)->format) * (*frame)->channels;
456     pkt.dts      = (*frame)->pkt_dts;
457     pkt.duration = av_frame_get_pkt_duration(*frame);
458     return pulse_write_packet(h, &pkt);
459 }
460
461
462 static void pulse_get_output_timestamp(AVFormatContext *h, int stream, int64_t *dts, int64_t *wall)
463 {
464     PulseData *s = h->priv_data;
465     pa_usec_t latency;
466     int neg;
467     pa_threaded_mainloop_lock(s->mainloop);
468     pa_stream_get_latency(s->stream, &latency, &neg);
469     pa_threaded_mainloop_unlock(s->mainloop);
470     *wall = av_gettime();
471     *dts = s->timestamp - (neg ? -latency : latency);
472 }
473
474 static int pulse_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list)
475 {
476     PulseData *s = h->priv_data;
477     return ff_pulse_audio_get_devices(device_list, s->server, 1);
478 }
479
480 #define OFFSET(a) offsetof(PulseData, a)
481 #define E AV_OPT_FLAG_ENCODING_PARAM
482 static const AVOption options[] = {
483     { "server",          "set PulseAudio server",            OFFSET(server),          AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
484     { "name",            "set application name",             OFFSET(name),            AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT},  0, 0, E },
485     { "stream_name",     "set stream description",           OFFSET(stream_name),     AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
486     { "device",          "set device name",                  OFFSET(device),          AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
487     { "buffer_size",     "set buffer size in bytes",         OFFSET(buffer_size),     AV_OPT_TYPE_INT,    {.i64 = 0}, 0, INT_MAX, E },
488     { "buffer_duration", "set buffer duration in millisecs", OFFSET(buffer_duration), AV_OPT_TYPE_INT,    {.i64 = 0}, 0, INT_MAX, E },
489     { NULL }
490 };
491
492 static const AVClass pulse_muxer_class = {
493     .class_name     = "PulseAudio muxer",
494     .item_name      = av_default_item_name,
495     .option         = options,
496     .version        = LIBAVUTIL_VERSION_INT,
497     .category       = AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT,
498 };
499
500 AVOutputFormat ff_pulse_muxer = {
501     .name                 = "pulse",
502     .long_name            = NULL_IF_CONFIG_SMALL("Pulse audio output"),
503     .priv_data_size       = sizeof(PulseData),
504     .audio_codec          = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
505     .video_codec          = AV_CODEC_ID_NONE,
506     .write_header         = pulse_write_header,
507     .write_packet         = pulse_write_packet,
508     .write_uncoded_frame  = pulse_write_frame,
509     .write_trailer        = pulse_write_trailer,
510     .get_output_timestamp = pulse_get_output_timestamp,
511     .get_device_list      = pulse_get_device_list,
512     .flags                = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
513     .priv_class           = &pulse_muxer_class,
514 };