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
7 * Authors: Martin Hamrle <hamrle @ post . cz>
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.
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.
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 *****************************************************************************/
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
34 #include <pulse/pulseaudio.h>
35 #include <vlc_pulse.h>
36 #if !PA_CHECK_VERSION(0,9,22)
37 # include <vlc_xlib.h>
40 static int Open ( vlc_object_t * );
41 static void Close ( vlc_object_t * );
44 set_shortname( "PulseAudio" )
45 set_description( N_("Pulseaudio audio output") )
46 set_capability( "audio output", 160 )
47 set_category( CAT_AUDIO )
48 set_subcategory( SUBCAT_AUDIO_AOUT )
49 add_shortcut( "pulseaudio", "pa" )
50 set_callbacks( Open, Close )
54 * - pause input on policy event
55 * - resample to compensate for long term drift
56 * - select music or video stream property correctly (?)
57 * - set further appropriate stream properties
58 * - update output devices list dynamically
62 * Be careful what you do when the PulseAudio mainloop is held, which is to say
63 * within PulseAudio callbacks, or after vlc_pa_lock().
64 * In particular, a VLC variable callback cannot be triggered nor deleted with
65 * the PulseAudio mainloop lock held, if the callback acquires the lock. */
69 pa_stream *stream; /**< PulseAudio playback stream object */
70 pa_context *context; /**< PulseAudio connection context */
71 pa_time_event *trigger; /**< Deferred stream trigger */
72 pa_volume_t base_volume; /**< 0dB reference volume */
73 pa_cvolume cvolume; /**< actual sink input volume */
74 mtime_t paused; /**< Time when (last) paused */
75 mtime_t pts; /**< Play time of buffer write offset */
76 mtime_t desync; /**< Measured desynchronization */
77 unsigned rate; /**< Current stream sample rate */
80 static void sink_input_info_cb(pa_context *, const pa_sink_input_info *,
84 static void context_cb(pa_context *ctx, pa_subscription_event_type_t type,
85 uint32_t idx, void *userdata)
87 audio_output_t *aout = userdata;
88 aout_sys_t *sys = aout->sys;
91 switch (type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK)
93 case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
94 if (idx != pa_stream_get_index(sys->stream))
95 break; /* only interested in our sink input */
97 /* Gee... PA will not provide the infos directly in the event. */
98 switch (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK)
100 case PA_SUBSCRIPTION_EVENT_REMOVE:
101 msg_Err(aout, "sink input killed!");
105 op = pa_context_get_sink_input_info(ctx, idx, sink_input_info_cb,
107 if (likely(op != NULL))
108 pa_operation_unref(op);
113 default: /* unsubscribed facility?! */
120 static void sink_list_cb(pa_context *c, const pa_sink_info *i, int eol,
123 audio_output_t *aout = userdata;
124 vlc_value_t val, text;
130 msg_Dbg(aout, "listing sink %s (%"PRIu32"): %s", i->name, i->index,
132 val.i_int = i->index;
133 text.psz_string = (char *)i->description;
134 var_Change(aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text);
137 static void sink_info_cb(pa_context *c, const pa_sink_info *i, int eol,
140 audio_output_t *aout = userdata;
141 aout_sys_t *sys = aout->sys;
147 /* PulseAudio flat volume NORM / 100% / 0dB corresponds to no software
148 * amplification and maximum hardware amplification.
149 * VLC maps DEFAULT / 100% to no gain at all (software/hardware).
150 * Thus we need to use the sink base_volume as a multiplier,
151 * if and only if flat volume is active for our current sink. */
152 if (i->flags & PA_SINK_FLAT_VOLUME)
153 sys->base_volume = i->base_volume;
155 sys->base_volume = PA_VOLUME_NORM;
156 msg_Dbg(aout, "base volume: %"PRIu32, sys->base_volume);
160 /*** Latency management and lip synchronization ***/
161 static mtime_t vlc_pa_get_latency(audio_output_t *aout,
162 pa_context *ctx, pa_stream *s)
167 if (pa_stream_get_latency(s, &latency, &negative)) {
168 if (pa_context_errno (ctx) != PA_ERR_NODATA)
169 vlc_pa_error(aout, "unknown latency", ctx);
170 return VLC_TS_INVALID;
172 return negative ? -latency : +latency;
175 static void stream_reset_sync(pa_stream *s, audio_output_t *aout)
177 aout_sys_t *sys = aout->sys;
178 const unsigned rate = aout->format.i_rate;
180 sys->pts = VLC_TS_INVALID;
182 pa_operation *op = pa_stream_update_sample_rate(s, rate, NULL, NULL);
183 if (unlikely(op == NULL))
185 pa_operation_unref(op);
189 static void stream_start(pa_stream *s, audio_output_t *aout)
191 aout_sys_t *sys = aout->sys;
194 if (sys->trigger != NULL) {
195 vlc_pa_rttime_free(sys->trigger);
199 op = pa_stream_cork(s, 0, NULL, NULL);
201 pa_operation_unref(op);
202 op = pa_stream_trigger(s, NULL, NULL);
203 if (likely(op != NULL))
204 pa_operation_unref(op);
207 static void stream_stop(pa_stream *s, audio_output_t *aout)
209 aout_sys_t *sys = aout->sys;
212 if (sys->trigger != NULL) {
213 vlc_pa_rttime_free(sys->trigger);
217 op = pa_stream_cork(s, 1, NULL, NULL);
219 pa_operation_unref(op);
222 static void stream_trigger_cb(pa_mainloop_api *api, pa_time_event *e,
223 const struct timeval *tv, void *userdata)
225 audio_output_t *aout = userdata;
226 aout_sys_t *sys = aout->sys;
228 msg_Dbg(aout, "starting deferred");
229 assert (sys->trigger == e);
230 stream_start(sys->stream, aout);
231 (void) api; (void) e; (void) tv;
235 * Starts or resumes the playback stream.
236 * Tries start playing back audio samples at the most accurate time
237 * in order to minimize desync and resampling during early playback.
238 * @note PulseAudio lock required.
240 static void stream_resync(audio_output_t *aout, pa_stream *s)
242 aout_sys_t *sys = aout->sys;
245 assert (pa_stream_is_corked(s) > 0);
246 assert (sys->pts != VLC_TS_INVALID);
248 delta = vlc_pa_get_latency(aout, sys->context, s);
249 if (unlikely(delta == VLC_TS_INVALID))
250 delta = 0; /* screwed */
252 delta = (sys->pts - mdate()) - delta;
254 if (sys->trigger == NULL) {
255 msg_Dbg(aout, "deferring start (%"PRId64" us)", delta);
256 delta += pa_rtclock_now();
257 sys->trigger = pa_context_rttime_new(sys->context, delta,
258 stream_trigger_cb, aout);
261 msg_Warn(aout, "starting late (%"PRId64" us)", delta);
262 stream_start(s, aout);
266 static void stream_latency_cb(pa_stream *s, void *userdata)
268 audio_output_t *aout = userdata;
269 aout_sys_t *sys = aout->sys;
270 mtime_t delta, change;
272 if (pa_stream_is_corked(s))
274 if (sys->pts == VLC_TS_INVALID)
276 msg_Dbg(aout, "missing latency from input");
280 /* Compute lip desynchronization */
281 delta = vlc_pa_get_latency(aout, sys->context, s);
282 if (delta == VLC_TS_INVALID)
285 delta = (sys->pts - mdate()) - delta;
286 change = delta - sys->desync;
288 //msg_Dbg(aout, "desync: %+"PRId64" us (variation: %+"PRId64" us)",
291 const unsigned inrate = aout->format.i_rate;
292 unsigned outrate = sys->rate;
295 if (delta < -AOUT_MAX_PTS_DELAY)
296 msg_Warn(aout, "too late by %"PRId64" us", -delta);
297 else if (delta > +AOUT_MAX_PTS_ADVANCE)
298 msg_Warn(aout, "too early by %"PRId64" us", delta);
299 else if (outrate == inrate)
300 return; /* In sync, do not add unnecessary disturbance! */
304 /* Compute playback sample rate */
305 /* This is empirical (especially the shift values).
306 * Feel free to define something smarter. */
307 int adj = sync ? (outrate - inrate)
308 : outrate * ((delta >> 4) + change) / (CLOCK_FREQ << 2);
309 /* This avoids too quick rate variation. It sounds really bad and
310 * causes unstability (e.g. oscillation around the correct rate). */
311 int limit = inrate >> 10;
312 /* However, to improve stability and try to converge, closing to the
313 * nominal rate is favored over drifting from it. */
314 if ((adj > 0) == (sys->rate > inrate))
322 /* This keeps the effective rate within specified range
323 * (+/-AOUT_MAX_RESAMPLING% - see <vlc_aout.h>) of the nominal rate. */
324 limit = inrate * AOUT_MAX_RESAMPLING / 100;
325 if (outrate > inrate + limit)
326 outrate = inrate + limit;
327 if (outrate < inrate - limit)
328 outrate = inrate - limit;
330 /* Apply adjusted sample rate */
331 if (outrate == sys->rate)
333 pa_operation *op = pa_stream_update_sample_rate(s, outrate, NULL, NULL);
334 if (unlikely(op == NULL)) {
335 vlc_pa_error(aout, "cannot change sample rate", sys->context);
338 pa_operation_unref(op);
339 msg_Dbg(aout, "changed sample rate to %u Hz",outrate);
344 /*** Stream helpers ***/
345 static void stream_state_cb(pa_stream *s, void *userdata)
347 switch (pa_stream_get_state(s)) {
348 case PA_STREAM_READY:
349 case PA_STREAM_FAILED:
350 case PA_STREAM_TERMINATED:
358 static void stream_event_cb(pa_stream *s, const char *name, pa_proplist *pl,
361 audio_output_t *aout = userdata;
363 #if PA_CHECK_VERSION(1,0,0)
364 /* FIXME: expose aout_Restart() directly */
365 if (!strcmp(name, PA_STREAM_EVENT_FORMAT_LOST)) {
366 vlc_value_t dummy = { .i_int = 0 };
368 msg_Dbg (aout, "format lost");
369 aout_ChannelsRestart (VLC_OBJECT(aout), "audio-device",
373 msg_Warn (aout, "unhandled event %s", name);
378 static void stream_moved_cb(pa_stream *s, void *userdata)
380 audio_output_t *aout = userdata;
381 aout_sys_t *sys = aout->sys;
383 uint32_t idx = pa_stream_get_device_index(s);
385 msg_Dbg(aout, "connected to sink %"PRIu32": %s", idx,
386 pa_stream_get_device_name(s));
387 op = pa_context_get_sink_info_by_index(sys->context, idx,
389 if (likely(op != NULL))
390 pa_operation_unref(op);
392 /* Update the variable if someone else moved our stream */
393 var_Change(aout, "audio-device", VLC_VAR_SETVALUE,
394 &(vlc_value_t){ .i_int = idx }, NULL);
397 static void stream_overflow_cb(pa_stream *s, void *userdata)
399 audio_output_t *aout = userdata;
401 msg_Err(aout, "overflow");
405 static void stream_started_cb(pa_stream *s, void *userdata)
407 audio_output_t *aout = userdata;
409 msg_Dbg(aout, "started");
413 static void stream_suspended_cb(pa_stream *s, void *userdata)
415 audio_output_t *aout = userdata;
417 msg_Dbg(aout, "suspended");
418 stream_reset_sync(s, aout);
421 static void stream_underflow_cb(pa_stream *s, void *userdata)
423 audio_output_t *aout = userdata;
425 msg_Warn(aout, "underflow");
426 stream_stop(s, aout);
427 stream_reset_sync(s, aout);
430 static int stream_wait(pa_stream *stream)
432 pa_stream_state_t state;
434 while ((state = pa_stream_get_state(stream)) != PA_STREAM_READY) {
435 if (state == PA_STREAM_FAILED || state == PA_STREAM_TERMINATED)
444 static void sink_input_info_cb(pa_context *ctx, const pa_sink_input_info *i,
445 int eol, void *userdata)
447 audio_output_t *aout = userdata;
448 aout_sys_t *sys = aout->sys;
455 sys->cvolume = i->volume;
456 volume = pa_cvolume_max(&i->volume) / (float)PA_VOLUME_NORM;
457 aout_VolumeHardSet(aout, volume, i->mute);
461 /*** VLC audio output callbacks ***/
463 /* Memory free callback. The block_t address is in front of the data. */
464 static void data_free(void *data)
466 block_t **pp = data, *block;
468 memcpy(&block, pp - 1, sizeof (block));
469 block_Release(block);
472 static void *data_convert(block_t **pp)
474 block_t *block = *pp;
475 /* In most cases, there is enough head room, and this is really cheap: */
476 block = block_Realloc(block, sizeof (block), block->i_buffer);
478 if (unlikely(block == NULL))
481 memcpy(block->p_buffer, &block, sizeof (block));
482 block->p_buffer += sizeof (block);
483 block->i_buffer -= sizeof (block);
484 return block->p_buffer;
488 * Queue one audio frame to the playabck stream
490 static void Play(audio_output_t *aout, block_t *block)
492 aout_sys_t *sys = aout->sys;
493 pa_stream *s = sys->stream;
495 const void *ptr = data_convert(&block);
496 if (unlikely(ptr == NULL))
499 size_t len = block->i_buffer;
500 mtime_t pts = block->i_pts + block->i_length;
502 /* Note: The core already holds the output FIFO lock at this point.
503 * Therefore we must not under any circumstances (try to) acquire the
504 * output FIFO lock while the PulseAudio threaded main loop lock is held
505 * (including from PulseAudio stream callbacks). Otherwise lock inversion
506 * will take place, and sooner or later a deadlock. */
510 if (pa_stream_is_corked(s) > 0)
511 stream_resync(aout, s);
513 #if 0 /* Fault injector to test underrun recovery */
514 static volatile unsigned u = 0;
515 if ((++u % 1000) == 0) {
516 msg_Err(aout, "fault injection");
517 pa_operation_unref(pa_stream_flush(s, NULL, NULL));
521 if (pa_stream_write(s, ptr, len, data_free, 0, PA_SEEK_RELATIVE) < 0) {
522 vlc_pa_error(aout, "cannot write", sys->context);
523 block_Release(block);
530 * Cork or uncork the playback stream
532 static void Pause(audio_output_t *aout, bool paused, mtime_t date)
534 aout_sys_t *sys = aout->sys;
535 pa_stream *s = sys->stream;
541 stream_stop(s, aout);
543 assert (sys->paused != VLC_TS_INVALID);
545 msg_Dbg(aout, "resuming after %"PRId64" us", date);
546 sys->paused = VLC_TS_INVALID;
548 stream_resync(aout, s);
555 * Flush or drain the playback stream
557 static void Flush(audio_output_t *aout, bool wait)
559 aout_sys_t *sys = aout->sys;
560 pa_stream *s = sys->stream;
566 op = pa_stream_drain(s, NULL, NULL);
567 /* TODO: wait for drain completion*/
569 op = pa_stream_flush(s, NULL, NULL);
571 pa_operation_unref(op);
575 static int VolumeSet(audio_output_t *aout, float vol, bool mute)
577 aout_sys_t *sys = aout->sys;
579 uint32_t idx = pa_stream_get_index(sys->stream);
581 pa_cvolume cvolume = sys->cvolume;
582 pa_volume_t volume = sys->base_volume;
584 pa_cvolume_scale(&cvolume, PA_VOLUME_NORM); /* preserve balance */
586 /* VLC provides the software volume so convert directly to PulseAudio
587 * software volume, pa_volume_t. This is not a linear amplification factor
588 * so do not use PulseAudio linear amplification! */
589 vol *= PA_VOLUME_NORM;
590 if (unlikely(vol >= PA_VOLUME_MAX))
592 volume = pa_sw_volume_multiply(volume, lround(vol));
593 pa_sw_cvolume_multiply_scalar(&cvolume, &cvolume, volume);
595 assert(pa_cvolume_valid(&cvolume));
598 op = pa_context_set_sink_input_volume(sys->context, idx, &cvolume, NULL, NULL);
599 if (likely(op != NULL))
600 pa_operation_unref(op);
601 op = pa_context_set_sink_input_mute(sys->context, idx, mute, NULL, NULL);
602 if (likely(op != NULL))
603 pa_operation_unref(op);
609 static int StreamMove(vlc_object_t *obj, const char *varname, vlc_value_t old,
610 vlc_value_t val, void *userdata)
612 audio_output_t *aout = (audio_output_t *)obj;
613 aout_sys_t *sys = aout->sys;
614 pa_stream *s = userdata;
616 uint32_t idx = pa_stream_get_index(s);
617 uint32_t sink_idx = val.i_int;
619 (void) varname; (void) old;
622 op = pa_context_move_sink_input_by_index(sys->context, idx, sink_idx,
624 if (likely(op != NULL)) {
625 pa_operation_unref(op);
626 msg_Dbg(aout, "moving to sink %"PRIu32, sink_idx);
628 vlc_pa_error(obj, "cannot move sink", sys->context);
631 return (op != NULL) ? VLC_SUCCESS : VLC_EGENERIC;
636 * Create a PulseAudio playback stream, a.k.a. a sink input.
638 static int Open(vlc_object_t *obj)
640 #if !PA_CHECK_VERSION(0,9,22)
641 if (!vlc_xlib_init(obj))
645 audio_output_t *aout = (audio_output_t *)obj;
648 /* Sample format specification */
649 struct pa_sample_spec ss;
650 vlc_fourcc_t format = aout->format.i_format;
655 format = VLC_CODEC_F32B;
657 ss.format = PA_SAMPLE_FLOAT32BE;
660 format = VLC_CODEC_F32L;
662 ss.format = PA_SAMPLE_FLOAT32LE;
665 format = VLC_CODEC_FL32;
666 ss.format = PA_SAMPLE_FLOAT32NE;
669 ss.format = PA_SAMPLE_S32BE;
672 ss.format = PA_SAMPLE_S32LE;
675 ss.format = PA_SAMPLE_S24BE;
678 ss.format = PA_SAMPLE_S24LE;
681 ss.format = PA_SAMPLE_S16BE;
684 ss.format = PA_SAMPLE_S16LE;
687 format = VLC_CODEC_U8;
689 ss.format = PA_SAMPLE_U8;
694 format = VLC_CODEC_FL32;
695 ss.format = PA_SAMPLE_FLOAT32NE;
699 format = VLC_CODEC_S16N;
700 ss.format = PA_SAMPLE_S16NE;
705 ss.rate = aout->format.i_rate;
706 ss.channels = aout_FormatNbChannels(&aout->format);
707 if (!pa_sample_spec_valid(&ss)) {
708 msg_Err(aout, "unsupported sample specification");
712 /* Channel mapping (order defined in vlc_aout.h) */
713 struct pa_channel_map map;
716 if (aout->format.i_physical_channels & AOUT_CHAN_LEFT)
717 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
718 if (aout->format.i_physical_channels & AOUT_CHAN_RIGHT)
719 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
720 if (aout->format.i_physical_channels & AOUT_CHAN_MIDDLELEFT)
721 map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_LEFT;
722 if (aout->format.i_physical_channels & AOUT_CHAN_MIDDLERIGHT)
723 map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_RIGHT;
724 if (aout->format.i_physical_channels & AOUT_CHAN_REARLEFT)
725 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_LEFT;
726 if (aout->format.i_physical_channels & AOUT_CHAN_REARRIGHT)
727 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_RIGHT;
728 if (aout->format.i_physical_channels & AOUT_CHAN_REARCENTER)
729 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_CENTER;
730 if (aout->format.i_physical_channels & AOUT_CHAN_CENTER)
732 if (ss.channels == 1)
733 map.map[map.channels++] = PA_CHANNEL_POSITION_MONO;
735 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_CENTER;
737 if (aout->format.i_physical_channels & AOUT_CHAN_LFE)
738 map.map[map.channels++] = PA_CHANNEL_POSITION_LFE;
740 for (unsigned i = 0; map.channels < ss.channels; i++) {
741 map.map[map.channels++] = PA_CHANNEL_POSITION_AUX0 + i;
742 msg_Warn(aout, "mapping channel %"PRIu8" to AUX%u", map.channels, i);
745 if (!pa_channel_map_valid(&map)) {
746 msg_Err(aout, "unsupported channel map");
749 const char *name = pa_channel_map_to_name(&map);
750 msg_Dbg(aout, "using %s channel map", (name != NULL) ? name : "?");
753 /* Stream parameters */
754 const pa_stream_flags_t flags = PA_STREAM_START_CORKED
755 //| PA_STREAM_INTERPOLATE_TIMING
756 | PA_STREAM_AUTO_TIMING_UPDATE
757 | PA_STREAM_VARIABLE_RATE;
759 struct pa_buffer_attr attr;
761 /* PulseAudio assumes that tlength bytes are available in the buffer. Thus
762 * we need to be conservative and set the minimum value that the VLC
763 * audio decoder thread warrants. Otherwise, PulseAudio buffers will
764 * underrun on hardware with large buffers. VLC keeps at least
765 * AOUT_MIN_PREPARE and at most AOUT_MAX_PREPARE worth of audio buffers.
766 * TODO? tlength could be adaptively increased to reduce wakeups. */
767 attr.tlength = pa_usec_to_bytes(AOUT_MIN_PREPARE_TIME, &ss);
768 attr.prebuf = 0; /* trigger manually */
770 attr.fragsize = 0; /* not used for output */
772 /* Allocate structures */
773 aout_sys_t *sys = malloc(sizeof(*sys));
774 if (unlikely(sys == NULL))
777 pa_context *ctx = vlc_pa_connect (obj);
788 sys->paused = VLC_TS_INVALID;
789 sys->pts = VLC_TS_INVALID;
794 const pa_subscription_mask_t mask = PA_SUBSCRIPTION_MASK_SINK_INPUT;
796 pa_context_set_subscribe_callback(ctx, context_cb, aout);
797 op = pa_context_subscribe(ctx, mask, NULL, NULL);
798 if (likely(op != NULL))
799 pa_operation_unref(op);
802 sys->base_volume = PA_VOLUME_NORM;
803 pa_cvolume_set(&sys->cvolume, ss.channels, PA_VOLUME_NORM);
806 /* Create a playback stream */
807 pa_stream *s = pa_stream_new(ctx, "audio stream", &ss, &map);
809 vlc_pa_error(obj, "stream creation failure", ctx);
813 pa_stream_set_state_callback(s, stream_state_cb, NULL);
814 pa_stream_set_event_callback(s, stream_event_cb, aout);
815 pa_stream_set_latency_update_callback(s, stream_latency_cb, aout);
816 pa_stream_set_moved_callback(s, stream_moved_cb, aout);
817 pa_stream_set_overflow_callback(s, stream_overflow_cb, aout);
818 pa_stream_set_started_callback(s, stream_started_cb, aout);
819 pa_stream_set_suspended_callback(s, stream_suspended_cb, aout);
820 pa_stream_set_underflow_callback(s, stream_underflow_cb, aout);
822 if (pa_stream_connect_playback(s, NULL, &attr, flags, NULL, NULL) < 0
824 vlc_pa_error(obj, "stream connection failure", ctx);
828 const struct pa_buffer_attr *pba = pa_stream_get_buffer_attr(s);
829 msg_Dbg(aout, "using buffer metrics: maxlength=%u, tlength=%u, "
830 "prebuf=%u, minreq=%u",
831 pba->maxlength, pba->tlength, pba->prebuf, pba->minreq);
833 var_Create(aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE);
834 var_Change(aout, "audio-device", VLC_VAR_SETTEXT,
835 &(vlc_value_t){ .psz_string = (char *)_("Audio device") },
837 var_AddCallback (aout, "audio-device", StreamMove, s);
838 op = pa_context_get_sink_info_list(ctx, sink_list_cb, aout);
839 /* We may need to wait for completion... once LibVLC supports this */
841 pa_operation_unref(op);
842 stream_moved_cb(s, aout);
845 aout->format.i_format = format;
846 aout->pf_play = Play;
847 aout->pf_pause = Pause;
848 aout->pf_flush = Flush;
849 aout_VolumeHardInit (aout, VolumeSet);
859 * Removes a PulseAudio playback stream
861 static void Close (vlc_object_t *obj)
863 audio_output_t *aout = (audio_output_t *)obj;
864 aout_sys_t *sys = aout->sys;
865 pa_context *ctx = sys->context;
866 pa_stream *s = sys->stream;
869 /* The callback takes mainloop lock, so it CANNOT be held here! */
870 var_DelCallback (aout, "audio-device", StreamMove, s);
871 var_Destroy (aout, "audio-device");
874 if (unlikely(sys->trigger != NULL))
875 vlc_pa_rttime_free(sys->trigger);
876 pa_stream_disconnect(s);
878 /* Clear all callbacks */
879 pa_stream_set_state_callback(s, NULL, NULL);
880 pa_stream_set_event_callback(s, NULL, NULL);
881 pa_stream_set_latency_update_callback(s, NULL, NULL);
882 pa_stream_set_moved_callback(s, NULL, NULL);
883 pa_stream_set_overflow_callback(s, NULL, NULL);
884 pa_stream_set_started_callback(s, NULL, NULL);
885 pa_stream_set_suspended_callback(s, NULL, NULL);
886 pa_stream_set_underflow_callback(s, NULL, NULL);
892 vlc_pa_disconnect(obj, ctx);