]> git.sesse.net Git - vlc/blob - modules/audio_output/pulse.c
PulseAudio: preserve balance when setting the volume
[vlc] / modules / audio_output / pulse.c
1 /*****************************************************************************
2  * pulse.c : Pulseaudio output plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2008 the VideoLAN team
5  * Copyright (C) 2009-2011 RĂ©mi Denis-Courmont
6  *
7  * Authors: Martin Hamrle <hamrle @ post . cz>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_plugin.h>
30 #include <vlc_aout.h>
31 #include <vlc_cpu.h>
32
33 #include <pulse/pulseaudio.h>
34 #include <vlc_pulse.h>
35
36 static int  Open        ( vlc_object_t * );
37 static void Close       ( vlc_object_t * );
38
39 vlc_module_begin ()
40     set_shortname( "PulseAudio" )
41     set_description( N_("Pulseaudio audio output") )
42     set_capability( "audio output", 160 )
43     set_category( CAT_AUDIO )
44     set_subcategory( SUBCAT_AUDIO_AOUT )
45     add_shortcut( "pulseaudio", "pa" )
46     set_callbacks( Open, Close )
47 vlc_module_end ()
48
49 /* TODO:
50  * - pause input on policy event
51  * - resample to compensate for long term drift
52  * - select music or video stream property correctly (?)
53  * - set further appropriate stream properties
54  * - update output devices list dynamically
55  */
56
57 /* NOTE:
58  * Be careful what you do when the PulseAudio mainloop is held, which is to say
59  * within PulseAudio callbacks, or after vlc_pa_lock().
60  * In particular, a VLC variable callback cannot be triggered nor deleted with
61  * the PulseAudio mainloop lock held, if the callback acquires the lock. */
62
63 struct aout_sys_t
64 {
65     pa_stream *stream; /**< PulseAudio playback stream object */
66     pa_context *context; /**< PulseAudio connection context */
67     pa_volume_t base_volume; /**< 0dB reference volume */
68     pa_cvolume cvolume; /**< actual sink input volume */
69     mtime_t pts; /**< Play time of buffer write offset */
70     mtime_t desync; /**< Measured desynchronization */
71     unsigned rate; /**< Current stream sample rate */
72 };
73
74 static void sink_input_info_cb(pa_context *, const pa_sink_input_info *,
75                                int, void *);
76
77 /*** Context ***/
78 static void context_cb(pa_context *ctx, pa_subscription_event_type_t type,
79                        uint32_t idx, void *userdata)
80 {
81     audio_output_t *aout = userdata;
82     aout_sys_t *sys = aout->sys;
83     pa_operation *op;
84
85     switch (type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK)
86     {
87       case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
88         if (idx != pa_stream_get_index(sys->stream))
89             break; /* only interested in our sink input */
90
91         /* Gee... PA will not provide the infos directly in the event. */
92         switch (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK)
93         {
94           case PA_SUBSCRIPTION_EVENT_REMOVE:
95             msg_Err(aout, "sink input killed!");
96             break;
97
98           default:
99             op = pa_context_get_sink_input_info(ctx, idx, sink_input_info_cb,
100                                                 aout);
101             if (likely(op != NULL))
102                 pa_operation_unref(op);
103             break;
104         }
105         break;
106
107       default: /* unsubscribed facility?! */
108         assert(0);
109     }
110 }
111
112
113 /*** Sink ***/
114 static void sink_list_cb(pa_context *c, const pa_sink_info *i, int eol,
115                          void *userdata)
116 {
117     audio_output_t *aout = userdata;
118     vlc_value_t val, text;
119
120     if (eol)
121         return;
122     (void) c;
123
124     msg_Dbg(aout, "listing sink %s (%"PRIu32"): %s", i->name, i->index,
125             i->description);
126     val.i_int = i->index;
127     text.psz_string = (char *)i->description;
128     var_Change(aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text);
129 }
130
131 static void sink_info_cb(pa_context *c, const pa_sink_info *i, int eol,
132                          void *userdata)
133 {
134     audio_output_t *aout = userdata;
135     aout_sys_t *sys = aout->sys;
136
137     if (eol)
138         return;
139     (void) c;
140
141     /* PulseAudio flat volume NORM / 100% / 0dB corresponds to no software
142      * amplification and maximum hardware amplification.
143      * VLC maps DEFAULT / 100% to no gain at all (software/hardware).
144      * Thus we need to use the sink base_volume as a multiplier,
145      * if and only if flat volume is active for our current sink. */
146     if (i->flags & PA_SINK_FLAT_VOLUME)
147         sys->base_volume = i->base_volume;
148     else
149         sys->base_volume = PA_VOLUME_NORM;
150     msg_Dbg(aout, "base volume: %f", pa_sw_volume_to_linear(sys->base_volume));
151 }
152
153 /*** Stream helpers ***/
154 static void stream_reset_sync(pa_stream *s, audio_output_t *aout)
155 {
156     aout_sys_t *sys = aout->sys;
157     const unsigned rate = aout->format.i_rate;
158
159     sys->pts = VLC_TS_INVALID;
160     sys->desync = 0;
161     pa_operation *op = pa_stream_update_sample_rate(s, rate, NULL, NULL);
162     if (unlikely(op == NULL))
163         return;
164     pa_operation_unref(op);
165     sys->rate = rate;
166 }
167
168 static void stream_state_cb(pa_stream *s, void *userdata)
169 {
170     switch (pa_stream_get_state(s)) {
171         case PA_STREAM_READY:
172         case PA_STREAM_FAILED:
173         case PA_STREAM_TERMINATED:
174             vlc_pa_signal(0);
175         default:
176             break;
177     }
178     (void) userdata;
179 }
180
181 /* Latency management and lip synchronization */
182 static void stream_latency_cb(pa_stream *s, void *userdata)
183 {
184     audio_output_t *aout = userdata;
185     aout_sys_t *sys = aout->sys;
186     mtime_t delta, change;
187
188     if (sys->pts == VLC_TS_INVALID)
189     {
190         msg_Dbg(aout, "missing latency from input");
191         return;
192     }
193
194     /* Compute lip desynchronization */
195     {
196         pa_usec_t latency;
197         int negative;
198
199         if (pa_stream_get_latency(s, &latency, &negative)) {
200             vlc_pa_error(aout, "missing latency", sys->context);
201             return;
202         }
203         delta = sys->pts - mdate();
204         if (unlikely(negative))
205            delta += latency;
206         else
207            delta -= latency;
208     }
209
210     change = delta - sys->desync;
211     sys->desync = delta;
212     //msg_Dbg(aout, "desync: %+"PRId64" us (variation: %+"PRId64" us)",
213     //        delta, change);
214
215     if (delta < -AOUT_MAX_PTS_DELAY)
216         msg_Warn(aout, "too late by %"PRId64" us", -delta);
217     else if (delta > +AOUT_MAX_PTS_ADVANCE)
218         msg_Warn(aout, "too early by %"PRId64" us", delta);
219
220     /* Compute playback sample rate */
221     const unsigned inrate = aout->format.i_rate;
222
223 #define ADJUST_FACTOR 4
224 #define ADJUST_MAX    1000 /* Hz (max rate variation per call) */
225     /* This is empirical. Feel free to define something smarter. */
226     int adj = sys->rate * (delta + change) / (CLOCK_FREQ * ADJUST_FACTOR);
227
228     /* This avoids too fast rate variation. They sound ugly as hell and they
229      * make the algorithm unstable (e.g. oscillation around inrate). */
230     if (adj > +ADJUST_MAX)
231         adj = +ADJUST_MAX;
232     if (adj < -ADJUST_MAX)
233         adj = -ADJUST_MAX;
234
235     unsigned outrate = sys->rate - adj;
236     /* Favor native rate to avoid resampling (FIXME: really a good idea?) */
237     if (abs(outrate - inrate) < (inrate >> 10))
238         outrate = inrate;
239
240     /* This keeps the effective rate within specified range
241      * (+/-AOUT_MAX_RESAMPLING% - see <vlc_aout.h>) of the nominal rate. */
242     const int limit = inrate * AOUT_MAX_RESAMPLING / 100;
243     if (outrate > inrate + limit)
244         outrate = inrate + limit;
245     if (outrate < inrate - limit)
246         outrate = inrate - limit;
247
248     /* Apply adjusted sample rate */
249     if (outrate == sys->rate)
250         return;
251     pa_operation *op = pa_stream_update_sample_rate(s, outrate, NULL, NULL);
252     if (unlikely(op == NULL)) {
253         vlc_pa_error(aout, "cannot change sample rate", sys->context);
254         return;
255     }
256     pa_operation_unref(op);
257     msg_Dbg(aout, "changed sample rate to %u Hz",outrate);
258     sys->rate = outrate;
259 }
260
261 static void stream_moved_cb(pa_stream *s, void *userdata)
262 {
263     audio_output_t *aout = userdata;
264     aout_sys_t *sys = aout->sys;
265     pa_operation *op;
266     uint32_t idx = pa_stream_get_device_index(s);
267
268     msg_Dbg(aout, "connected to sink %"PRIu32": %s", idx,
269                   pa_stream_get_device_name(s));
270     op = pa_context_get_sink_info_by_index(sys->context, idx,
271                                            sink_info_cb, aout);
272     if (likely(op != NULL))
273         pa_operation_unref(op);
274
275     /* Update the variable if someone else moved our stream */
276     var_Change(aout, "audio-device", VLC_VAR_SETVALUE,
277                &(vlc_value_t){ .i_int = idx }, NULL);
278 }
279
280 static void stream_overflow_cb(pa_stream *s, void *userdata)
281 {
282     audio_output_t *aout = userdata;
283
284     msg_Err(aout, "overflow");
285     (void) s;
286 }
287
288 static void stream_started_cb(pa_stream *s, void *userdata)
289 {
290     audio_output_t *aout = userdata;
291
292     msg_Dbg(aout, "started");
293     (void) s;
294 }
295
296 static void stream_suspended_cb(pa_stream *s, void *userdata)
297 {
298     audio_output_t *aout = userdata;
299
300     msg_Dbg(aout, "suspended");
301     stream_reset_sync(s, aout);
302 }
303
304 static void stream_underflow_cb(pa_stream *s, void *userdata)
305 {
306     audio_output_t *aout = userdata;
307     pa_operation *op;
308
309     msg_Warn(aout, "underflow");
310     op = pa_stream_cork(s, 1, NULL, NULL);
311     if (op != NULL)
312         pa_operation_unref(op);
313     stream_reset_sync(s, aout);
314 }
315
316 static int stream_wait(pa_stream *stream)
317 {
318     pa_stream_state_t state;
319
320     while ((state = pa_stream_get_state(stream)) != PA_STREAM_READY) {
321         if (state == PA_STREAM_FAILED || state == PA_STREAM_TERMINATED)
322             return -1;
323         vlc_pa_wait();
324     }
325     return 0;
326 }
327
328 #ifdef LIBPULSE_GETS_A_CLUE
329 static void stream_success_cb(pa_stream *s, int success, void *userdata)
330 {
331     vlc_pa_signal(0);
332     (void) s; (void) success; (void) userdata;
333 }
334 #else
335 # define stream_success_cb NULL
336 #endif
337
338
339 /*** Sink input ***/
340 static void sink_input_info_cb(pa_context *ctx, const pa_sink_input_info *i,
341                                int eol, void *userdata)
342 {
343     audio_output_t *aout = userdata;
344     aout_sys_t *sys = aout->sys;
345     float volume;
346
347     if (eol)
348         return;
349     (void) ctx;
350
351     sys->cvolume = i->volume;
352     volume = pa_sw_volume_to_linear(pa_cvolume_max(&i->volume));
353     aout_VolumeHardSet(aout, volume, i->mute);
354 }
355
356
357 /*** VLC audio output callbacks ***/
358
359 /* Memory free callback. The block_t address is in front of the data. */
360 static void data_free(void *data)
361 {
362     block_t **pp = data, *block;
363
364     memcpy(&block, pp - 1, sizeof (block));
365     block_Release(block);
366 }
367
368 static void *data_convert(block_t **pp)
369 {
370     block_t *block = *pp;
371     /* In most cases, there is enough head room, and this is really cheap: */
372     block = block_Realloc(block, sizeof (block), block->i_buffer);
373     *pp = block;
374     if (unlikely(block == NULL))
375         return NULL;
376
377     memcpy(block->p_buffer, &block, sizeof (block));
378     block->p_buffer += sizeof (block);
379     block->i_buffer -= sizeof (block);
380     return block->p_buffer;
381 }
382
383 /**
384  * Queue one audio frame to the playabck stream
385  */
386 static void Play(audio_output_t *aout)
387 {
388     aout_sys_t *sys = aout->sys;
389     pa_stream *s = sys->stream;
390
391     /* This function is called exactly once per block in the output FIFO. */
392     block_t *block = aout_FifoPop(&aout->fifo);
393     assert (block != NULL);
394
395     const void *ptr = data_convert(&block);
396     if (unlikely(ptr == NULL))
397         return;
398
399     size_t len = block->i_buffer;
400     mtime_t pts = block->i_pts + block->i_length;
401
402     /* Note: The core already holds the output FIFO lock at this point.
403      * Therefore we must not under any circumstances (try to) acquire the
404      * output FIFO lock while the PulseAudio threaded main loop lock is held
405      * (including from PulseAudio stream callbacks). Otherwise lock inversion
406      * will take place, and sooner or later a deadlock. */
407     vlc_pa_lock();
408
409     if (pa_stream_is_corked(s) > 0) {
410         /* Start or resume the stream. Zeroes are prepended to sync.
411          * This does not really work because PulseAudio latency measurement is
412          * garbage at start. */
413         pa_operation *op;
414         pa_usec_t latency;
415         int negative;
416
417         if (pa_stream_get_latency(s, &latency, &negative) == 0)
418             msg_Dbg(aout, "starting with %c%"PRIu64" us latency",
419                     negative ? '-' : '+', latency);
420         else
421             latency = negative = 0;
422
423         mtime_t advance = block->i_pts - mdate();
424         if (negative)
425             advance += latency;
426         else
427             advance -= latency;
428
429         if (advance > 0) {
430             size_t nb = (advance * aout->format.i_rate) / CLOCK_FREQ;
431             size_t size = aout->format.i_bytes_per_frame;
432             float *zeroes = calloc (nb, size);
433
434             msg_Dbg(aout, "prepending %zu zeroes", nb);
435 #if 0 /* Fault injector: add delay */
436             pa_stream_write(s, zeroes, nb * size, NULL, 0, PA_SEEK_RELATIVE);
437             pa_stream_write(s, zeroes, nb * size, NULL, 0, PA_SEEK_RELATIVE);
438 #endif
439             if (likely(zeroes != NULL))
440                 if (pa_stream_write(s, zeroes, nb * size, free, 0,
441                                     PA_SEEK_RELATIVE) < 0)
442                     free(zeroes);
443         }
444
445         op = pa_stream_cork(s, 0, NULL, NULL);
446         if (op != NULL)
447             pa_operation_unref(op);
448         op = pa_stream_trigger(s, NULL, NULL);
449         if (op != NULL)
450             pa_operation_unref(op);
451         msg_Dbg(aout, "uncorking");
452     }
453
454 #if 0 /* Fault injector to test underrun recovery */
455     static volatile unsigned u = 0;
456     if ((++u % 1000) == 0) {
457         msg_Err(aout, "fault injection");
458         pa_operation_unref(pa_stream_flush(s, NULL, NULL));
459     }
460 #endif
461
462     if (pa_stream_write(s, ptr, len, data_free, 0, PA_SEEK_RELATIVE) < 0) {
463         vlc_pa_error(aout, "cannot write", sys->context);
464         block_Release(block);
465     }
466     sys->pts = pts;
467
468     vlc_pa_unlock();
469 }
470
471 /**
472  * Cork or uncork the playback stream
473  */
474 static void Pause(audio_output_t *aout, bool b_paused, mtime_t i_date)
475 {
476     aout_sys_t *sys = aout->sys;
477     pa_stream *s = sys->stream;
478
479     if (!b_paused)
480         return; /* nothing to do - yet */
481
482     vlc_pa_lock();
483
484     pa_operation *op = pa_stream_cork(s, 1, NULL, NULL);
485     if (op != NULL)
486         pa_operation_unref(op);
487     stream_reset_sync(s, aout);
488
489     vlc_pa_unlock();
490     (void) i_date;
491 }
492
493 static int VolumeSet(audio_output_t *aout, float vol, bool mute)
494 {
495     aout_sys_t *sys = aout->sys;
496     pa_operation *op;
497     uint32_t idx = pa_stream_get_index(sys->stream);
498
499     pa_cvolume cvolume = sys->cvolume;
500     pa_volume_t volume = pa_sw_volume_multiply(pa_sw_volume_from_linear(vol),
501                                                sys->base_volume);
502
503     pa_cvolume_scale(&cvolume, PA_VOLUME_NORM); /* preserve balance */
504     pa_sw_cvolume_multiply_scalar(&cvolume, &cvolume, volume);
505     assert(pa_cvolume_valid(&cvolume));
506
507     vlc_pa_lock();
508     op = pa_context_set_sink_input_volume(sys->context, idx, &cvolume, NULL, NULL);
509     if (likely(op != NULL))
510         pa_operation_unref(op);
511     op = pa_context_set_sink_input_mute(sys->context, idx, mute, NULL, NULL);
512     if (likely(op != NULL))
513         pa_operation_unref(op);
514     vlc_pa_unlock();
515
516     return 0;
517 }
518
519 static int StreamMove(vlc_object_t *obj, const char *varname, vlc_value_t old,
520                       vlc_value_t val, void *userdata)
521 {
522     audio_output_t *aout = (audio_output_t *)obj;
523     aout_sys_t *sys = aout->sys;
524     pa_stream *s = userdata;
525     pa_operation *op;
526     uint32_t idx = pa_stream_get_index(s);
527     uint32_t sink_idx = val.i_int;
528
529     (void) varname; (void) old;
530
531     vlc_pa_lock();
532     op = pa_context_move_sink_input_by_index(sys->context, idx, sink_idx,
533                                              NULL, NULL);
534     if (likely(op != NULL)) {
535         pa_operation_unref(op);
536         msg_Dbg(aout, "moving to sink %"PRIu32, sink_idx);
537     } else
538         vlc_pa_error(obj, "cannot move sink", sys->context);
539     vlc_pa_unlock();
540
541     return (op != NULL) ? VLC_SUCCESS : VLC_EGENERIC;
542 }
543
544
545 /**
546  * Create a PulseAudio playback stream, a.k.a. a sink input.
547  */
548 static int Open(vlc_object_t *obj)
549 {
550     audio_output_t *aout = (audio_output_t *)obj;
551     pa_operation *op;
552
553     /* Sample format specification */
554     struct pa_sample_spec ss;
555     vlc_fourcc_t format = aout->format.i_format;
556
557     switch(format)
558     {
559         case VLC_CODEC_F64B:
560             format = VLC_CODEC_F32B;
561         case VLC_CODEC_F32B:
562             ss.format = PA_SAMPLE_FLOAT32BE;
563             break;
564         case VLC_CODEC_F64L:
565             format = VLC_CODEC_F32L;
566         case VLC_CODEC_F32L:
567             ss.format = PA_SAMPLE_FLOAT32LE;
568             break;
569         case VLC_CODEC_FI32:
570             format = VLC_CODEC_FL32;
571             ss.format = PA_SAMPLE_FLOAT32NE;
572             break;
573         case VLC_CODEC_S32B:
574             ss.format = PA_SAMPLE_S32BE;
575             break;
576         case VLC_CODEC_S32L:
577             ss.format = PA_SAMPLE_S32LE;
578             break;
579         case VLC_CODEC_S24B:
580             ss.format = PA_SAMPLE_S24BE;
581             break;
582         case VLC_CODEC_S24L:
583             ss.format = PA_SAMPLE_S24LE;
584             break;
585         case VLC_CODEC_S16B:
586             ss.format = PA_SAMPLE_S16BE;
587             break;
588         case VLC_CODEC_S16L:
589             ss.format = PA_SAMPLE_S16LE;
590             break;
591         case VLC_CODEC_S8:
592             format = VLC_CODEC_U8;
593         case VLC_CODEC_U8:
594             ss.format = PA_SAMPLE_U8;
595             break;
596         default:
597             if (HAVE_FPU)
598             {
599                 format = VLC_CODEC_FL32;
600                 ss.format = PA_SAMPLE_FLOAT32NE;
601             }
602             else
603             {
604                 format = VLC_CODEC_S16N;
605                 ss.format = PA_SAMPLE_S16NE;
606             }
607             break;
608     }
609
610     ss.rate = aout->format.i_rate;
611     ss.channels = aout_FormatNbChannels(&aout->format);
612     if (!pa_sample_spec_valid(&ss)) {
613         msg_Err(aout, "unsupported sample specification");
614         return VLC_EGENERIC;
615     }
616
617     /* Channel mapping (order defined in vlc_aout.h) */
618     struct pa_channel_map map;
619     map.channels = 0;
620
621     if (aout->format.i_physical_channels & AOUT_CHAN_LEFT)
622         map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
623     if (aout->format.i_physical_channels & AOUT_CHAN_RIGHT)
624         map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
625     if (aout->format.i_physical_channels & AOUT_CHAN_MIDDLELEFT)
626         map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_LEFT;
627     if (aout->format.i_physical_channels & AOUT_CHAN_MIDDLERIGHT)
628         map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_RIGHT;
629     if (aout->format.i_physical_channels & AOUT_CHAN_REARLEFT)
630         map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_LEFT;
631     if (aout->format.i_physical_channels & AOUT_CHAN_REARRIGHT)
632         map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_RIGHT;
633     if (aout->format.i_physical_channels & AOUT_CHAN_REARCENTER)
634         map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_CENTER;
635     if (aout->format.i_physical_channels & AOUT_CHAN_CENTER)
636     {
637         if (ss.channels == 1)
638             map.map[map.channels++] = PA_CHANNEL_POSITION_MONO;
639         else
640             map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_CENTER;
641     }
642     if (aout->format.i_physical_channels & AOUT_CHAN_LFE)
643         map.map[map.channels++] = PA_CHANNEL_POSITION_LFE;
644
645     for (unsigned i = 0; map.channels < ss.channels; i++) {
646         map.map[map.channels++] = PA_CHANNEL_POSITION_AUX0 + i;
647         msg_Warn(aout, "mapping channel %"PRIu8" to AUX%u", map.channels, i);
648     }
649
650     if (!pa_channel_map_valid(&map)) {
651         msg_Err(aout, "unsupported channel map");
652         return VLC_EGENERIC;
653     } else {
654         const char *name = pa_channel_map_to_name(&map);
655         msg_Dbg(aout, "using %s channel map", (name != NULL) ? name : "?");
656     }
657
658     /* Stream parameters */
659     const pa_stream_flags_t flags = PA_STREAM_START_CORKED
660                                   //| PA_STREAM_INTERPOLATE_TIMING
661                                   | PA_STREAM_AUTO_TIMING_UPDATE
662                                   | PA_STREAM_VARIABLE_RATE;
663
664     struct pa_buffer_attr attr;
665     attr.maxlength = -1;
666     /* PulseAudio assumes that tlength bytes are available in the buffer. Thus
667      * we need to be conservative and set the minimum value that the VLC
668      * audio decoder thread warrants. Otherwise, PulseAudio buffers will
669      * underrun on hardware with large buffers. VLC keeps at least
670      * AOUT_MIN_PREPARE and at most AOUT_MAX_PREPARE worth of audio buffers.
671      * TODO? tlength could be adaptively increased to reduce wakeups. */
672     attr.tlength = pa_usec_to_bytes(AOUT_MIN_PREPARE_TIME, &ss);
673     attr.prebuf = 0; /* trigger manually */
674     attr.minreq = -1;
675     attr.fragsize = 0; /* not used for output */
676
677     /* Allocate structures */
678     aout_sys_t *sys = malloc(sizeof(*sys));
679     if (unlikely(sys == NULL))
680         return VLC_ENOMEM;
681
682     pa_context *ctx = vlc_pa_connect (obj);
683     if (ctx == NULL)
684     {
685         free (sys);
686         return VLC_EGENERIC;
687     }
688
689     aout->sys = sys;
690     sys->stream = NULL;
691     sys->context = ctx;
692     sys->pts = VLC_TS_INVALID;
693     sys->desync = 0;
694     sys->rate = ss.rate;
695
696     /* Context events */
697     const pa_subscription_mask_t mask = PA_SUBSCRIPTION_MASK_SINK_INPUT;
698
699     pa_context_set_subscribe_callback(ctx, context_cb, aout);
700     op = pa_context_subscribe(ctx, mask, NULL, NULL);
701     if (likely(op != NULL))
702        pa_operation_unref(op);
703
704     /* Channel volume */
705     sys->base_volume = PA_VOLUME_NORM;
706     pa_cvolume_set(&sys->cvolume, ss.channels, PA_VOLUME_NORM);
707
708     vlc_pa_lock();
709     /* Create a playback stream */
710     pa_stream *s = pa_stream_new(ctx, "audio stream", &ss, &map);
711     if (s == NULL) {
712         vlc_pa_error(obj, "stream creation failure", ctx);
713         goto fail;
714     }
715     sys->stream = s;
716     pa_stream_set_state_callback(s, stream_state_cb, NULL);
717     pa_stream_set_latency_update_callback(s, stream_latency_cb, aout);
718     pa_stream_set_moved_callback(s, stream_moved_cb, aout);
719     pa_stream_set_overflow_callback(s, stream_overflow_cb, aout);
720     pa_stream_set_started_callback(s, stream_started_cb, aout);
721     pa_stream_set_suspended_callback(s, stream_suspended_cb, aout);
722     pa_stream_set_underflow_callback(s, stream_underflow_cb, aout);
723
724     if (pa_stream_connect_playback(s, NULL, &attr, flags, NULL, NULL) < 0
725      || stream_wait(s)) {
726         vlc_pa_error(obj, "stream connection failure", ctx);
727         goto fail;
728     }
729
730     const struct pa_buffer_attr *pba = pa_stream_get_buffer_attr(s);
731     msg_Dbg(aout, "using buffer metrics: maxlength=%u, tlength=%u, "
732             "prebuf=%u, minreq=%u",
733             pba->maxlength, pba->tlength, pba->prebuf, pba->minreq);
734
735     aout->i_nb_samples = pba->minreq / pa_frame_size(&ss);
736
737     var_Create(aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE);
738     var_Change(aout, "audio-device", VLC_VAR_SETTEXT,
739                &(vlc_value_t){ .psz_string = (char *)_("Audio device") },
740                NULL);
741     var_AddCallback (aout, "audio-device", StreamMove, s);
742     op = pa_context_get_sink_info_list(ctx, sink_list_cb, aout);
743     /* We may need to wait for completion... once LibVLC supports this */
744     if (op != NULL)
745         pa_operation_unref(op);
746     stream_moved_cb(s, aout);
747     vlc_pa_unlock();
748
749     aout->format.i_format = format;
750     aout->pf_play = Play;
751     aout->pf_pause = Pause;
752     aout->pf_volume_set = VolumeSet;
753     return VLC_SUCCESS;
754
755 fail:
756     vlc_pa_unlock();
757     Close(obj);
758     return VLC_EGENERIC;
759 }
760
761 /**
762  * Removes a PulseAudio playback stream
763  */
764 static void Close (vlc_object_t *obj)
765 {
766     audio_output_t *aout = (audio_output_t *)obj;
767     aout_sys_t *sys = aout->sys;
768     pa_context *ctx = sys->context;
769     pa_stream *s = sys->stream;
770
771     if (s != NULL) {
772         /* The callback takes mainloop lock, so it CANNOT be held here! */
773         var_DelCallback (aout, "audio-device", StreamMove, s);
774         var_Destroy (aout, "audio-device");
775     }
776
777     vlc_pa_lock();
778     if (s != NULL) {
779         pa_operation *op;
780
781         if (pa_stream_is_corked(s) > 0)
782             /* Stream paused: discard all buffers */
783             op = pa_stream_flush(s, stream_success_cb, NULL);
784         else
785             /* Stream playing: wait until buffers are played */
786             op = pa_stream_drain(s, stream_success_cb, NULL);
787         if (likely(op != NULL)) {
788 #ifdef LIBPULSE_GETS_A_CLUE
789             while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
790                 vlc_pa_wait();
791 #endif
792             pa_operation_unref(op);
793         }
794
795         pa_stream_disconnect(s);
796         pa_stream_unref(s);
797     }
798     vlc_pa_unlock();
799
800     vlc_pa_disconnect(obj, ctx);
801     free(sys);
802 }