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