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 <math.h>
22#include <pulse/pulseaudio.h>
23#include <pulse/error.h>
24#include "libavformat/avformat.h"
25#include "libavformat/internal.h"
26#include "libavutil/opt.h"
27#include "libavutil/time.h"
28#include "libavutil/log.h"
29#include "libavutil/attributes.h"
30#include "pulse_audio_common.h"
31
32typedef struct PulseData {
33    AVClass *class;
34    const char *server;
35    const char *name;
36    const char *stream_name;
37    const char *device;
38    int64_t timestamp;
39    int buffer_size;               /**< Buffer size in bytes */
40    int buffer_duration;           /**< Buffer size in ms, recalculated to buffer_size */
41    int prebuf;
42    int minreq;
43    int last_result;
44    pa_threaded_mainloop *mainloop;
45    pa_context *ctx;
46    pa_stream *stream;
47    int nonblocking;
48    int mute;
49    pa_volume_t base_volume;
50    pa_volume_t last_volume;
51} PulseData;
52
53static void pulse_audio_sink_device_cb(pa_context *ctx, const pa_sink_info *dev,
54                                       int eol, void *userdata)
55{
56    PulseData *s = userdata;
57
58    if (s->ctx != ctx)
59        return;
60
61    if (eol) {
62        pa_threaded_mainloop_signal(s->mainloop, 0);
63    } else {
64        if (dev->flags & PA_SINK_FLAT_VOLUME)
65            s->base_volume = dev->base_volume;
66        else
67            s->base_volume = PA_VOLUME_NORM;
68        av_log(s, AV_LOG_DEBUG, "base volume: %u\n", s->base_volume);
69    }
70}
71
72/* Mainloop must be locked before calling this function as it uses pa_threaded_mainloop_wait. */
73static int pulse_update_sink_info(AVFormatContext *h)
74{
75    PulseData *s = h->priv_data;
76    pa_operation *op;
77    if (!(op = pa_context_get_sink_info_by_name(s->ctx, s->device,
78                                                pulse_audio_sink_device_cb, s))) {
79        av_log(s, AV_LOG_ERROR, "pa_context_get_sink_info_by_name failed.\n");
80        return AVERROR_EXTERNAL;
81    }
82    while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
83        pa_threaded_mainloop_wait(s->mainloop);
84    pa_operation_unref(op);
85    return 0;
86}
87
88static void pulse_audio_sink_input_cb(pa_context *ctx, const pa_sink_input_info *i,
89                                      int eol, void *userdata)
90{
91    AVFormatContext *h = userdata;
92    PulseData *s = h->priv_data;
93
94    if (s->ctx != ctx)
95        return;
96
97    if (!eol) {
98        double val;
99        pa_volume_t vol = pa_cvolume_avg(&i->volume);
100        if (s->mute < 0 || (s->mute && !i->mute) || (!s->mute && i->mute)) {
101            s->mute = i->mute;
102            avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_MUTE_STATE_CHANGED, &s->mute, sizeof(s->mute));
103        }
104
105        vol = pa_sw_volume_divide(vol, s->base_volume);
106        if (s->last_volume != vol) {
107            val = (double)vol / PA_VOLUME_NORM;
108            avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_VOLUME_LEVEL_CHANGED, &val, sizeof(val));
109            s->last_volume = vol;
110        }
111    }
112}
113
114/* This function creates new loop so may be called from PA callbacks.
115   Mainloop must be locked before calling this function as it operates on streams. */
116static int pulse_update_sink_input_info(AVFormatContext *h)
117{
118    PulseData *s = h->priv_data;
119    pa_operation *op;
120    enum pa_operation_state op_state;
121    pa_mainloop *ml = NULL;
122    pa_context *ctx = NULL;
123    int ret = 0;
124
125    if ((ret = ff_pulse_audio_connect_context(&ml, &ctx, s->server, "Update sink input information")) < 0)
126        return ret;
127
128    if (!(op = pa_context_get_sink_input_info(ctx, pa_stream_get_index(s->stream),
129                                              pulse_audio_sink_input_cb, h))) {
130        ret = AVERROR_EXTERNAL;
131        goto fail;
132    }
133
134    while ((op_state = pa_operation_get_state(op)) == PA_OPERATION_RUNNING)
135        pa_mainloop_iterate(ml, 1, NULL);
136    pa_operation_unref(op);
137    if (op_state != PA_OPERATION_DONE) {
138        ret = AVERROR_EXTERNAL;
139        goto fail;
140    }
141
142  fail:
143    ff_pulse_audio_disconnect_context(&ml, &ctx);
144    if (ret)
145        av_log(s, AV_LOG_ERROR, "pa_context_get_sink_input_info failed.\n");
146    return ret;
147}
148
149static void pulse_event(pa_context *ctx, pa_subscription_event_type_t t,
150                        uint32_t idx, void *userdata)
151{
152    AVFormatContext *h = userdata;
153    PulseData *s = h->priv_data;
154
155    if (s->ctx != ctx)
156        return;
157
158    if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK_INPUT) {
159        if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_CHANGE)
160            // Calling from mainloop callback. No need to lock mainloop.
161            pulse_update_sink_input_info(h);
162    }
163}
164
165static void pulse_stream_writable(pa_stream *stream, size_t nbytes, void *userdata)
166{
167    AVFormatContext *h = userdata;
168    PulseData *s = h->priv_data;
169    int64_t val = nbytes;
170
171    if (stream != s->stream)
172        return;
173
174    avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_BUFFER_WRITABLE, &val, sizeof(val));
175    pa_threaded_mainloop_signal(s->mainloop, 0);
176}
177
178static void pulse_overflow(pa_stream *stream, void *userdata)
179{
180    AVFormatContext *h = userdata;
181    avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_BUFFER_OVERFLOW, NULL, 0);
182}
183
184static void pulse_underflow(pa_stream *stream, void *userdata)
185{
186    AVFormatContext *h = userdata;
187    avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_BUFFER_UNDERFLOW, NULL, 0);
188}
189
190static void pulse_stream_state(pa_stream *stream, void *userdata)
191{
192    PulseData *s = userdata;
193
194    if (stream != s->stream)
195        return;
196
197    switch (pa_stream_get_state(s->stream)) {
198        case PA_STREAM_READY:
199        case PA_STREAM_FAILED:
200        case PA_STREAM_TERMINATED:
201            pa_threaded_mainloop_signal(s->mainloop, 0);
202        default:
203            break;
204    }
205}
206
207static int pulse_stream_wait(PulseData *s)
208{
209    pa_stream_state_t state;
210
211    while ((state = pa_stream_get_state(s->stream)) != PA_STREAM_READY) {
212        if (state == PA_STREAM_FAILED || state == PA_STREAM_TERMINATED)
213            return AVERROR_EXTERNAL;
214        pa_threaded_mainloop_wait(s->mainloop);
215    }
216    return 0;
217}
218
219static void pulse_context_state(pa_context *ctx, void *userdata)
220{
221    PulseData *s = userdata;
222
223    if (s->ctx != ctx)
224        return;
225
226    switch (pa_context_get_state(ctx)) {
227        case PA_CONTEXT_READY:
228        case PA_CONTEXT_FAILED:
229        case PA_CONTEXT_TERMINATED:
230            pa_threaded_mainloop_signal(s->mainloop, 0);
231        default:
232            break;
233    }
234}
235
236static int pulse_context_wait(PulseData *s)
237{
238    pa_context_state_t state;
239
240    while ((state = pa_context_get_state(s->ctx)) != PA_CONTEXT_READY) {
241        if (state == PA_CONTEXT_FAILED || state == PA_CONTEXT_TERMINATED)
242            return AVERROR_EXTERNAL;
243        pa_threaded_mainloop_wait(s->mainloop);
244    }
245    return 0;
246}
247
248static void pulse_stream_result(pa_stream *stream, int success, void *userdata)
249{
250    PulseData *s = userdata;
251
252    if (stream != s->stream)
253        return;
254
255    s->last_result = success ? 0 : AVERROR_EXTERNAL;
256    pa_threaded_mainloop_signal(s->mainloop, 0);
257}
258
259static int pulse_finish_stream_operation(PulseData *s, pa_operation *op, const char *name)
260{
261    if (!op) {
262        pa_threaded_mainloop_unlock(s->mainloop);
263        av_log(s, AV_LOG_ERROR, "%s failed.\n", name);
264        return AVERROR_EXTERNAL;
265    }
266    s->last_result = 2;
267    while (s->last_result == 2)
268        pa_threaded_mainloop_wait(s->mainloop);
269    pa_operation_unref(op);
270    pa_threaded_mainloop_unlock(s->mainloop);
271    if (s->last_result != 0)
272        av_log(s, AV_LOG_ERROR, "%s failed.\n", name);
273    return s->last_result;
274}
275
276static int pulse_set_pause(PulseData *s, int pause)
277{
278    pa_operation *op;
279    pa_threaded_mainloop_lock(s->mainloop);
280    op = pa_stream_cork(s->stream, pause, pulse_stream_result, s);
281    return pulse_finish_stream_operation(s, op, "pa_stream_cork");
282}
283
284static int pulse_flash_stream(PulseData *s)
285{
286    pa_operation *op;
287    pa_threaded_mainloop_lock(s->mainloop);
288    op = pa_stream_flush(s->stream, pulse_stream_result, s);
289    return pulse_finish_stream_operation(s, op, "pa_stream_flush");
290}
291
292static void pulse_context_result(pa_context *ctx, int success, void *userdata)
293{
294    PulseData *s = userdata;
295
296    if (s->ctx != ctx)
297        return;
298
299    s->last_result = success ? 0 : AVERROR_EXTERNAL;
300    pa_threaded_mainloop_signal(s->mainloop, 0);
301}
302
303static int pulse_finish_context_operation(PulseData *s, pa_operation *op, const char *name)
304{
305    if (!op) {
306        pa_threaded_mainloop_unlock(s->mainloop);
307        av_log(s, AV_LOG_ERROR, "%s failed.\n", name);
308        return AVERROR_EXTERNAL;
309    }
310    s->last_result = 2;
311    while (s->last_result == 2)
312        pa_threaded_mainloop_wait(s->mainloop);
313    pa_operation_unref(op);
314    pa_threaded_mainloop_unlock(s->mainloop);
315    if (s->last_result != 0)
316        av_log(s, AV_LOG_ERROR, "%s failed.\n", name);
317    return s->last_result;
318}
319
320static int pulse_set_mute(PulseData *s)
321{
322    pa_operation *op;
323    pa_threaded_mainloop_lock(s->mainloop);
324    op = pa_context_set_sink_input_mute(s->ctx, pa_stream_get_index(s->stream),
325                                        s->mute, pulse_context_result, s);
326    return pulse_finish_context_operation(s, op, "pa_context_set_sink_input_mute");
327}
328
329static int pulse_set_volume(PulseData *s, double volume)
330{
331    pa_operation *op;
332    pa_cvolume cvol;
333    pa_volume_t vol;
334    const pa_sample_spec *ss = pa_stream_get_sample_spec(s->stream);
335
336    vol = pa_sw_volume_multiply(lround(volume * PA_VOLUME_NORM), s->base_volume);
337    pa_cvolume_set(&cvol, ss->channels, PA_VOLUME_NORM);
338    pa_sw_cvolume_multiply_scalar(&cvol, &cvol, vol);
339    pa_threaded_mainloop_lock(s->mainloop);
340    op = pa_context_set_sink_input_volume(s->ctx, pa_stream_get_index(s->stream),
341                                          &cvol, pulse_context_result, s);
342    return pulse_finish_context_operation(s, op, "pa_context_set_sink_input_volume");
343}
344
345static int pulse_subscribe_events(PulseData *s)
346{
347    pa_operation *op;
348
349    pa_threaded_mainloop_lock(s->mainloop);
350    op = pa_context_subscribe(s->ctx, PA_SUBSCRIPTION_MASK_SINK_INPUT, pulse_context_result, s);
351    return pulse_finish_context_operation(s, op, "pa_context_subscribe");
352}
353
354static void pulse_map_channels_to_pulse(int64_t channel_layout, pa_channel_map *channel_map)
355{
356    channel_map->channels = 0;
357    if (channel_layout & AV_CH_FRONT_LEFT)
358        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
359    if (channel_layout & AV_CH_FRONT_RIGHT)
360        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
361    if (channel_layout & AV_CH_FRONT_CENTER)
362        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_CENTER;
363    if (channel_layout & AV_CH_LOW_FREQUENCY)
364        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_LFE;
365    if (channel_layout & AV_CH_BACK_LEFT)
366        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_REAR_LEFT;
367    if (channel_layout & AV_CH_BACK_RIGHT)
368        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_REAR_RIGHT;
369    if (channel_layout & AV_CH_FRONT_LEFT_OF_CENTER)
370        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER;
371    if (channel_layout & AV_CH_FRONT_RIGHT_OF_CENTER)
372        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
373    if (channel_layout & AV_CH_BACK_CENTER)
374        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_REAR_CENTER;
375    if (channel_layout & AV_CH_SIDE_LEFT)
376        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_SIDE_LEFT;
377    if (channel_layout & AV_CH_SIDE_RIGHT)
378        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_SIDE_RIGHT;
379    if (channel_layout & AV_CH_TOP_CENTER)
380        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_CENTER;
381    if (channel_layout & AV_CH_TOP_FRONT_LEFT)
382        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_FRONT_LEFT;
383    if (channel_layout & AV_CH_TOP_FRONT_CENTER)
384        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_FRONT_CENTER;
385    if (channel_layout & AV_CH_TOP_FRONT_RIGHT)
386        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_FRONT_RIGHT;
387    if (channel_layout & AV_CH_TOP_BACK_LEFT)
388        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_REAR_LEFT;
389    if (channel_layout & AV_CH_TOP_BACK_CENTER)
390        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_REAR_CENTER;
391    if (channel_layout & AV_CH_TOP_BACK_RIGHT)
392        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
393    if (channel_layout & AV_CH_STEREO_LEFT)
394        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
395    if (channel_layout & AV_CH_STEREO_RIGHT)
396        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
397    if (channel_layout & AV_CH_WIDE_LEFT)
398        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX0;
399    if (channel_layout & AV_CH_WIDE_RIGHT)
400        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX1;
401    if (channel_layout & AV_CH_SURROUND_DIRECT_LEFT)
402        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX2;
403    if (channel_layout & AV_CH_SURROUND_DIRECT_RIGHT)
404        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX3;
405    if (channel_layout & AV_CH_LOW_FREQUENCY_2)
406        channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_LFE;
407}
408
409static av_cold int pulse_write_trailer(AVFormatContext *h)
410{
411    PulseData *s = h->priv_data;
412
413    if (s->mainloop) {
414        pa_threaded_mainloop_lock(s->mainloop);
415        if (s->stream) {
416            pa_stream_disconnect(s->stream);
417            pa_stream_set_state_callback(s->stream, NULL, NULL);
418            pa_stream_set_write_callback(s->stream, NULL, NULL);
419            pa_stream_set_overflow_callback(s->stream, NULL, NULL);
420            pa_stream_set_underflow_callback(s->stream, NULL, NULL);
421            pa_stream_unref(s->stream);
422            s->stream = NULL;
423        }
424        if (s->ctx) {
425            pa_context_disconnect(s->ctx);
426            pa_context_set_state_callback(s->ctx, NULL, NULL);
427            pa_context_set_subscribe_callback(s->ctx, NULL, NULL);
428            pa_context_unref(s->ctx);
429            s->ctx = NULL;
430        }
431        pa_threaded_mainloop_unlock(s->mainloop);
432        pa_threaded_mainloop_stop(s->mainloop);
433        pa_threaded_mainloop_free(s->mainloop);
434        s->mainloop = NULL;
435    }
436
437    return 0;
438}
439
440static av_cold int pulse_write_header(AVFormatContext *h)
441{
442    PulseData *s = h->priv_data;
443    AVStream *st = NULL;
444    int ret;
445    pa_sample_spec sample_spec;
446    pa_buffer_attr buffer_attributes = { -1, -1, -1, -1, -1 };
447    pa_channel_map channel_map;
448    pa_mainloop_api *mainloop_api;
449    const char *stream_name = s->stream_name;
450    static const pa_stream_flags_t stream_flags = PA_STREAM_INTERPOLATE_TIMING |
451                                                  PA_STREAM_AUTO_TIMING_UPDATE |
452                                                  PA_STREAM_NOT_MONOTONIC;
453
454    if (h->nb_streams != 1 || h->streams[0]->codec->codec_type != AVMEDIA_TYPE_AUDIO) {
455        av_log(s, AV_LOG_ERROR, "Only a single audio stream is supported.\n");
456        return AVERROR(EINVAL);
457    }
458    st = h->streams[0];
459
460    if (!stream_name) {
461        if (h->filename[0])
462            stream_name = h->filename;
463        else
464            stream_name = "Playback";
465    }
466    s->nonblocking = (h->flags & AVFMT_FLAG_NONBLOCK);
467
468    if (s->buffer_duration) {
469        int64_t bytes = s->buffer_duration;
470        bytes *= st->codec->channels * st->codec->sample_rate *
471                 av_get_bytes_per_sample(st->codec->sample_fmt);
472        bytes /= 1000;
473        buffer_attributes.tlength = FFMAX(s->buffer_size, av_clip64(bytes, 0, UINT32_MAX - 1));
474        av_log(s, AV_LOG_DEBUG,
475               "Buffer duration: %ums recalculated into %"PRId64" bytes buffer.\n",
476               s->buffer_duration, bytes);
477        av_log(s, AV_LOG_DEBUG, "Real buffer length is %u bytes\n", buffer_attributes.tlength);
478    } else if (s->buffer_size)
479        buffer_attributes.tlength = s->buffer_size;
480    if (s->prebuf)
481        buffer_attributes.prebuf = s->prebuf;
482    if (s->minreq)
483        buffer_attributes.minreq = s->minreq;
484
485    sample_spec.format = ff_codec_id_to_pulse_format(st->codec->codec_id);
486    sample_spec.rate = st->codec->sample_rate;
487    sample_spec.channels = st->codec->channels;
488    if (!pa_sample_spec_valid(&sample_spec)) {
489        av_log(s, AV_LOG_ERROR, "Invalid sample spec.\n");
490        return AVERROR(EINVAL);
491    }
492
493    if (sample_spec.channels == 1) {
494        channel_map.channels = 1;
495        channel_map.map[0] = PA_CHANNEL_POSITION_MONO;
496    } else if (st->codec->channel_layout) {
497        if (av_get_channel_layout_nb_channels(st->codec->channel_layout) != st->codec->channels)
498            return AVERROR(EINVAL);
499        pulse_map_channels_to_pulse(st->codec->channel_layout, &channel_map);
500        /* Unknown channel is present in channel_layout, let PulseAudio use its default. */
501        if (channel_map.channels != sample_spec.channels) {
502            av_log(s, AV_LOG_WARNING, "Unknown channel. Using defaul channel map.\n");
503            channel_map.channels = 0;
504        }
505    } else
506        channel_map.channels = 0;
507
508    if (!channel_map.channels)
509        av_log(s, AV_LOG_WARNING, "Using PulseAudio's default channel map.\n");
510    else if (!pa_channel_map_valid(&channel_map)) {
511        av_log(s, AV_LOG_ERROR, "Invalid channel map.\n");
512        return AVERROR(EINVAL);
513    }
514
515    /* start main loop */
516    s->mainloop = pa_threaded_mainloop_new();
517    if (!s->mainloop) {
518        av_log(s, AV_LOG_ERROR, "Cannot create threaded mainloop.\n");
519        return AVERROR(ENOMEM);
520    }
521    if ((ret = pa_threaded_mainloop_start(s->mainloop)) < 0) {
522        av_log(s, AV_LOG_ERROR, "Cannot start threaded mainloop: %s.\n", pa_strerror(ret));
523        pa_threaded_mainloop_free(s->mainloop);
524        s->mainloop = NULL;
525        return AVERROR_EXTERNAL;
526    }
527
528    pa_threaded_mainloop_lock(s->mainloop);
529
530    mainloop_api = pa_threaded_mainloop_get_api(s->mainloop);
531    if (!mainloop_api) {
532        av_log(s, AV_LOG_ERROR, "Cannot get mainloop API.\n");
533        ret = AVERROR_EXTERNAL;
534        goto fail;
535    }
536
537    s->ctx = pa_context_new(mainloop_api, s->name);
538    if (!s->ctx) {
539        av_log(s, AV_LOG_ERROR, "Cannot create context.\n");
540        ret = AVERROR(ENOMEM);
541        goto fail;
542    }
543    pa_context_set_state_callback(s->ctx, pulse_context_state, s);
544    pa_context_set_subscribe_callback(s->ctx, pulse_event, h);
545
546    if ((ret = pa_context_connect(s->ctx, s->server, 0, NULL)) < 0) {
547        av_log(s, AV_LOG_ERROR, "Cannot connect context: %s.\n", pa_strerror(ret));
548        ret = AVERROR_EXTERNAL;
549        goto fail;
550    }
551
552    if ((ret = pulse_context_wait(s)) < 0) {
553        av_log(s, AV_LOG_ERROR, "Context failed.\n");
554        goto fail;
555    }
556
557    s->stream = pa_stream_new(s->ctx, stream_name, &sample_spec,
558                              channel_map.channels ? &channel_map : NULL);
559
560    if ((ret = pulse_update_sink_info(h)) < 0) {
561        av_log(s, AV_LOG_ERROR, "Updating sink info failed.\n");
562        goto fail;
563    }
564
565    if (!s->stream) {
566        av_log(s, AV_LOG_ERROR, "Cannot create stream.\n");
567        ret = AVERROR(ENOMEM);
568        goto fail;
569    }
570    pa_stream_set_state_callback(s->stream, pulse_stream_state, s);
571    pa_stream_set_write_callback(s->stream, pulse_stream_writable, h);
572    pa_stream_set_overflow_callback(s->stream, pulse_overflow, h);
573    pa_stream_set_underflow_callback(s->stream, pulse_underflow, h);
574
575    if ((ret = pa_stream_connect_playback(s->stream, s->device, &buffer_attributes,
576                                          stream_flags, NULL, NULL)) < 0) {
577        av_log(s, AV_LOG_ERROR, "pa_stream_connect_playback failed: %s.\n", pa_strerror(ret));
578        ret = AVERROR_EXTERNAL;
579        goto fail;
580    }
581
582    if ((ret = pulse_stream_wait(s)) < 0) {
583        av_log(s, AV_LOG_ERROR, "Stream failed.\n");
584        goto fail;
585    }
586
587    /* read back buffer attributes for future use */
588    buffer_attributes = *pa_stream_get_buffer_attr(s->stream);
589    s->buffer_size = buffer_attributes.tlength;
590    s->prebuf = buffer_attributes.prebuf;
591    s->minreq = buffer_attributes.minreq;
592    av_log(s, AV_LOG_DEBUG, "Real buffer attributes: size: %d, prebuf: %d, minreq: %d\n",
593           s->buffer_size, s->prebuf, s->minreq);
594
595    pa_threaded_mainloop_unlock(s->mainloop);
596
597    if ((ret = pulse_subscribe_events(s)) < 0) {
598        av_log(s, AV_LOG_ERROR, "Event subscription failed.\n");
599        /* a bit ugly but the simplest to lock here*/
600        pa_threaded_mainloop_lock(s->mainloop);
601        goto fail;
602    }
603
604    /* force control messages */
605    s->mute = -1;
606    s->last_volume = PA_VOLUME_INVALID;
607    pa_threaded_mainloop_lock(s->mainloop);
608    if ((ret = pulse_update_sink_input_info(h)) < 0) {
609        av_log(s, AV_LOG_ERROR, "Updating sink input info failed.\n");
610        goto fail;
611    }
612    pa_threaded_mainloop_unlock(s->mainloop);
613
614    avpriv_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
615
616    return 0;
617  fail:
618    pa_threaded_mainloop_unlock(s->mainloop);
619    pulse_write_trailer(h);
620    return ret;
621}
622
623static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt)
624{
625    PulseData *s = h->priv_data;
626    int ret;
627    int64_t writable_size;
628
629    if (!pkt)
630        return pulse_flash_stream(s);
631
632    if (pkt->dts != AV_NOPTS_VALUE)
633        s->timestamp = pkt->dts;
634
635    if (pkt->duration) {
636        s->timestamp += pkt->duration;
637    } else {
638        AVStream *st = h->streams[0];
639        AVCodecContext *codec_ctx = st->codec;
640        AVRational r = { 1, codec_ctx->sample_rate };
641        int64_t samples = pkt->size / (av_get_bytes_per_sample(codec_ctx->sample_fmt) * codec_ctx->channels);
642        s->timestamp += av_rescale_q(samples, r, st->time_base);
643    }
644
645    pa_threaded_mainloop_lock(s->mainloop);
646    if (!PA_STREAM_IS_GOOD(pa_stream_get_state(s->stream))) {
647        av_log(s, AV_LOG_ERROR, "PulseAudio stream is in invalid state.\n");
648        goto fail;
649    }
650    while (pa_stream_writable_size(s->stream) < s->minreq) {
651        if (s->nonblocking) {
652            pa_threaded_mainloop_unlock(s->mainloop);
653            return AVERROR(EAGAIN);
654        } else
655            pa_threaded_mainloop_wait(s->mainloop);
656    }
657
658    if ((ret = pa_stream_write(s->stream, pkt->data, pkt->size, NULL, 0, PA_SEEK_RELATIVE)) < 0) {
659        av_log(s, AV_LOG_ERROR, "pa_stream_write failed: %s\n", pa_strerror(ret));
660        goto fail;
661    }
662    if ((writable_size = pa_stream_writable_size(s->stream)) >= s->minreq)
663        avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_BUFFER_WRITABLE, &writable_size, sizeof(writable_size));
664
665    pa_threaded_mainloop_unlock(s->mainloop);
666
667    return 0;
668  fail:
669    pa_threaded_mainloop_unlock(s->mainloop);
670    return AVERROR_EXTERNAL;
671}
672
673static int pulse_write_frame(AVFormatContext *h, int stream_index,
674                             AVFrame **frame, unsigned flags)
675{
676    AVPacket pkt;
677
678    /* Planar formats are not supported yet. */
679    if (flags & AV_WRITE_UNCODED_FRAME_QUERY)
680        return av_sample_fmt_is_planar(h->streams[stream_index]->codec->sample_fmt) ?
681               AVERROR(EINVAL) : 0;
682
683    pkt.data     = (*frame)->data[0];
684    pkt.size     = (*frame)->nb_samples * av_get_bytes_per_sample((*frame)->format) * av_frame_get_channels(*frame);
685    pkt.dts      = (*frame)->pkt_dts;
686    pkt.duration = av_frame_get_pkt_duration(*frame);
687    return pulse_write_packet(h, &pkt);
688}
689
690
691static void pulse_get_output_timestamp(AVFormatContext *h, int stream, int64_t *dts, int64_t *wall)
692{
693    PulseData *s = h->priv_data;
694    pa_usec_t latency;
695    int neg;
696    pa_threaded_mainloop_lock(s->mainloop);
697    pa_stream_get_latency(s->stream, &latency, &neg);
698    pa_threaded_mainloop_unlock(s->mainloop);
699    if (wall)
700        *wall = av_gettime();
701    if (dts)
702        *dts = s->timestamp - (neg ? -latency : latency);
703}
704
705static int pulse_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list)
706{
707    PulseData *s = h->priv_data;
708    return ff_pulse_audio_get_devices(device_list, s->server, 1);
709}
710
711static int pulse_control_message(AVFormatContext *h, int type,
712                                 void *data, size_t data_size)
713{
714    PulseData *s = h->priv_data;
715    int ret;
716
717    switch(type) {
718    case AV_APP_TO_DEV_PAUSE:
719        return pulse_set_pause(s, 1);
720    case AV_APP_TO_DEV_PLAY:
721        return pulse_set_pause(s, 0);
722    case AV_APP_TO_DEV_TOGGLE_PAUSE:
723        return pulse_set_pause(s, !pa_stream_is_corked(s->stream));
724    case AV_APP_TO_DEV_MUTE:
725        if (!s->mute) {
726            s->mute = 1;
727            return pulse_set_mute(s);
728        }
729        return 0;
730    case AV_APP_TO_DEV_UNMUTE:
731        if (s->mute) {
732            s->mute = 0;
733            return pulse_set_mute(s);
734        }
735        return 0;
736    case AV_APP_TO_DEV_TOGGLE_MUTE:
737        s->mute = !s->mute;
738        return pulse_set_mute(s);
739    case AV_APP_TO_DEV_SET_VOLUME:
740        return pulse_set_volume(s, *(double *)data);
741    case AV_APP_TO_DEV_GET_VOLUME:
742        s->last_volume = PA_VOLUME_INVALID;
743        pa_threaded_mainloop_lock(s->mainloop);
744        ret = pulse_update_sink_input_info(h);
745        pa_threaded_mainloop_unlock(s->mainloop);
746        return ret;
747    case AV_APP_TO_DEV_GET_MUTE:
748        s->mute = -1;
749        pa_threaded_mainloop_lock(s->mainloop);
750        ret = pulse_update_sink_input_info(h);
751        pa_threaded_mainloop_unlock(s->mainloop);
752        return ret;
753    default:
754        break;
755    }
756    return AVERROR(ENOSYS);
757}
758
759#define OFFSET(a) offsetof(PulseData, a)
760#define E AV_OPT_FLAG_ENCODING_PARAM
761static const AVOption options[] = {
762    { "server",          "set PulseAudio server",            OFFSET(server),          AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
763    { "name",            "set application name",             OFFSET(name),            AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT},  0, 0, E },
764    { "stream_name",     "set stream description",           OFFSET(stream_name),     AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
765    { "device",          "set device name",                  OFFSET(device),          AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
766    { "buffer_size",     "set buffer size in bytes",         OFFSET(buffer_size),     AV_OPT_TYPE_INT,    {.i64 = 0}, 0, INT_MAX, E },
767    { "buffer_duration", "set buffer duration in millisecs", OFFSET(buffer_duration), AV_OPT_TYPE_INT,    {.i64 = 0}, 0, INT_MAX, E },
768    { "prebuf",          "set pre-buffering size",           OFFSET(prebuf),          AV_OPT_TYPE_INT,    {.i64 = 0}, 0, INT_MAX, E },
769    { "minreq",          "set minimum request size",         OFFSET(minreq),          AV_OPT_TYPE_INT,    {.i64 = 0}, 0, INT_MAX, E },
770    { NULL }
771};
772
773static const AVClass pulse_muxer_class = {
774    .class_name     = "PulseAudio muxer",
775    .item_name      = av_default_item_name,
776    .option         = options,
777    .version        = LIBAVUTIL_VERSION_INT,
778    .category       = AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT,
779};
780
781AVOutputFormat ff_pulse_muxer = {
782    .name                 = "pulse",
783    .long_name            = NULL_IF_CONFIG_SMALL("Pulse audio output"),
784    .priv_data_size       = sizeof(PulseData),
785    .audio_codec          = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
786    .video_codec          = AV_CODEC_ID_NONE,
787    .write_header         = pulse_write_header,
788    .write_packet         = pulse_write_packet,
789    .write_uncoded_frame  = pulse_write_frame,
790    .write_trailer        = pulse_write_trailer,
791    .get_output_timestamp = pulse_get_output_timestamp,
792    .get_device_list      = pulse_get_device_list,
793    .control_message      = pulse_control_message,
794    .flags                = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
795    .priv_class           = &pulse_muxer_class,
796};
797