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 *****************************************************************************/
28 #include <vlc_common.h>
29 #include <vlc_plugin.h>
33 #include <pulse/pulseaudio.h>
34 #include <vlc_pulse.h>
35 #if !PA_CHECK_VERSION(0,9,22)
36 # include <vlc_xlib.h>
39 static int Open ( vlc_object_t * );
40 static void Close ( vlc_object_t * );
43 set_shortname( "PulseAudio" )
44 set_description( N_("Pulseaudio audio output") )
45 set_capability( "audio output", 160 )
46 set_category( CAT_AUDIO )
47 set_subcategory( SUBCAT_AUDIO_AOUT )
48 add_shortcut( "pulseaudio", "pa" )
49 set_callbacks( Open, Close )
53 * - pause input on policy event
54 * - resample to compensate for long term drift
55 * - select music or video stream property correctly (?)
56 * - set further appropriate stream properties
57 * - update output devices list dynamically
61 * Be careful what you do when the PulseAudio mainloop is held, which is to say
62 * within PulseAudio callbacks, or after vlc_pa_lock().
63 * In particular, a VLC variable callback cannot be triggered nor deleted with
64 * the PulseAudio mainloop lock held, if the callback acquires the lock. */
68 pa_stream *stream; /**< PulseAudio playback stream object */
69 pa_context *context; /**< PulseAudio connection context */
70 pa_volume_t base_volume; /**< 0dB reference volume */
71 pa_cvolume cvolume; /**< actual sink input volume */
72 mtime_t paused; /**< Time when (last) paused */
73 mtime_t pts; /**< Play time of buffer write offset */
74 mtime_t desync; /**< Measured desynchronization */
75 unsigned rate; /**< Current stream sample rate */
78 static void sink_input_info_cb(pa_context *, const pa_sink_input_info *,
82 static void context_cb(pa_context *ctx, pa_subscription_event_type_t type,
83 uint32_t idx, void *userdata)
85 audio_output_t *aout = userdata;
86 aout_sys_t *sys = aout->sys;
89 switch (type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK)
91 case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
92 if (idx != pa_stream_get_index(sys->stream))
93 break; /* only interested in our sink input */
95 /* Gee... PA will not provide the infos directly in the event. */
96 switch (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK)
98 case PA_SUBSCRIPTION_EVENT_REMOVE:
99 msg_Err(aout, "sink input killed!");
103 op = pa_context_get_sink_input_info(ctx, idx, sink_input_info_cb,
105 if (likely(op != NULL))
106 pa_operation_unref(op);
111 default: /* unsubscribed facility?! */
118 static void sink_list_cb(pa_context *c, const pa_sink_info *i, int eol,
121 audio_output_t *aout = userdata;
122 vlc_value_t val, text;
128 msg_Dbg(aout, "listing sink %s (%"PRIu32"): %s", i->name, i->index,
130 val.i_int = i->index;
131 text.psz_string = (char *)i->description;
132 var_Change(aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text);
135 static void sink_info_cb(pa_context *c, const pa_sink_info *i, int eol,
138 audio_output_t *aout = userdata;
139 aout_sys_t *sys = aout->sys;
145 /* PulseAudio flat volume NORM / 100% / 0dB corresponds to no software
146 * amplification and maximum hardware amplification.
147 * VLC maps DEFAULT / 100% to no gain at all (software/hardware).
148 * Thus we need to use the sink base_volume as a multiplier,
149 * if and only if flat volume is active for our current sink. */
150 if (i->flags & PA_SINK_FLAT_VOLUME)
151 sys->base_volume = i->base_volume;
153 sys->base_volume = PA_VOLUME_NORM;
154 msg_Dbg(aout, "base volume: %"PRIu32, sys->base_volume);
158 /*** Latency management and lip synchronization ***/
159 static mtime_t vlc_pa_get_latency(audio_output_t *aout,
160 pa_context *ctx, pa_stream *s)
165 if (pa_stream_get_latency(s, &latency, &negative)) {
166 vlc_pa_error(aout, "unknown latency", ctx);
167 return VLC_TS_INVALID;
169 return negative ? -latency : +latency;
172 static void stream_reset_sync(pa_stream *s, audio_output_t *aout)
174 aout_sys_t *sys = aout->sys;
175 const unsigned rate = aout->format.i_rate;
177 sys->pts = VLC_TS_INVALID;
179 pa_operation *op = pa_stream_update_sample_rate(s, rate, NULL, NULL);
180 if (unlikely(op == NULL))
182 pa_operation_unref(op);
187 * Starts or resumes the playback stream.
188 * Tries start playing back audio samples at the most accurate time
189 * in order to minimize desync and resampling during early playback.
190 * @note PulseAudio lock required.
192 static void stream_resync(audio_output_t *aout, pa_stream *s)
194 aout_sys_t *sys = aout->sys;
198 assert (pa_stream_is_corked(s) > 0);
199 assert (sys->pts != VLC_TS_INVALID);
201 delta = vlc_pa_get_latency(aout, sys->context, s);
202 if (unlikely(delta == VLC_TS_INVALID))
203 delta = 0; /* screwed */
205 delta = (sys->pts - mdate()) - delta;
207 /* TODO: adjust prebuf instead of padding? */
209 size_t nb = (delta * sys->rate) / CLOCK_FREQ;
210 size_t size = aout->format.i_bytes_per_frame;
211 float *zeroes = calloc (nb, size);
213 msg_Dbg(aout, "starting with %zu zeroes (%"PRId64" us)", nb,
215 #if 0 /* Fault injector: add delay */
216 pa_stream_write(s, zeroes, nb * size, NULL, 0, PA_SEEK_RELATIVE);
217 pa_stream_write(s, zeroes, nb * size, NULL, 0, PA_SEEK_RELATIVE);
219 if (likely(zeroes != NULL))
220 if (pa_stream_write(s, zeroes, nb * size, free, 0,
221 PA_SEEK_RELATIVE) < 0)
224 msg_Warn(aout, "starting late (%"PRId64" us)", delta);
226 op = pa_stream_cork(s, 0, NULL, NULL);
228 pa_operation_unref(op);
229 op = pa_stream_trigger(s, NULL, NULL);
231 pa_operation_unref(op);
234 static void stream_latency_cb(pa_stream *s, void *userdata)
236 audio_output_t *aout = userdata;
237 aout_sys_t *sys = aout->sys;
238 mtime_t delta, change;
240 if (pa_stream_is_corked(s))
242 if (sys->pts == VLC_TS_INVALID)
244 msg_Dbg(aout, "missing latency from input");
248 /* Compute lip desynchronization */
249 delta = vlc_pa_get_latency(aout, sys->context, s);
250 if (delta == VLC_TS_INVALID)
253 delta = (sys->pts - mdate()) - delta;
254 change = delta - sys->desync;
256 //msg_Dbg(aout, "desync: %+"PRId64" us (variation: %+"PRId64" us)",
259 const unsigned inrate = aout->format.i_rate;
260 unsigned outrate = sys->rate;
263 if (delta < -AOUT_MAX_PTS_DELAY)
264 msg_Warn(aout, "too late by %"PRId64" us", -delta);
265 else if (delta > +AOUT_MAX_PTS_ADVANCE)
266 msg_Warn(aout, "too early by %"PRId64" us", delta);
267 else if (outrate == inrate)
268 return; /* In sync, do not add unnecessary disturbance! */
272 /* Compute playback sample rate */
273 /* This is empirical. Feel free to define something smarter. */
274 int adj = sync ? (outrate - inrate)
275 : outrate * (delta + change) / (CLOCK_FREQ << 4);
276 /* This avoids too quick rate variation. It sounds really bad and
277 * causes unstability (e.g. oscillation around the correct rate). */
278 int limit = inrate >> 10;
279 /* However, to improve stability and try to converge, closing to the
280 * nominal rate is favored over drifting from it. */
281 if ((adj > 0) == (sys->rate > inrate))
289 /* This keeps the effective rate within specified range
290 * (+/-AOUT_MAX_RESAMPLING% - see <vlc_aout.h>) of the nominal rate. */
291 limit = inrate * AOUT_MAX_RESAMPLING / 100;
292 if (outrate > inrate + limit)
293 outrate = inrate + limit;
294 if (outrate < inrate - limit)
295 outrate = inrate - limit;
297 /* Apply adjusted sample rate */
298 if (outrate == sys->rate)
300 pa_operation *op = pa_stream_update_sample_rate(s, outrate, NULL, NULL);
301 if (unlikely(op == NULL)) {
302 vlc_pa_error(aout, "cannot change sample rate", sys->context);
305 pa_operation_unref(op);
306 msg_Dbg(aout, "changed sample rate to %u Hz",outrate);
311 /*** Stream helpers ***/
312 static void stream_state_cb(pa_stream *s, void *userdata)
314 switch (pa_stream_get_state(s)) {
315 case PA_STREAM_READY:
316 case PA_STREAM_FAILED:
317 case PA_STREAM_TERMINATED:
325 static void stream_moved_cb(pa_stream *s, void *userdata)
327 audio_output_t *aout = userdata;
328 aout_sys_t *sys = aout->sys;
330 uint32_t idx = pa_stream_get_device_index(s);
332 msg_Dbg(aout, "connected to sink %"PRIu32": %s", idx,
333 pa_stream_get_device_name(s));
334 op = pa_context_get_sink_info_by_index(sys->context, idx,
336 if (likely(op != NULL))
337 pa_operation_unref(op);
339 /* Update the variable if someone else moved our stream */
340 var_Change(aout, "audio-device", VLC_VAR_SETVALUE,
341 &(vlc_value_t){ .i_int = idx }, NULL);
344 static void stream_overflow_cb(pa_stream *s, void *userdata)
346 audio_output_t *aout = userdata;
348 msg_Err(aout, "overflow");
352 static void stream_started_cb(pa_stream *s, void *userdata)
354 audio_output_t *aout = userdata;
356 msg_Dbg(aout, "started");
360 static void stream_suspended_cb(pa_stream *s, void *userdata)
362 audio_output_t *aout = userdata;
364 msg_Dbg(aout, "suspended");
365 stream_reset_sync(s, aout);
368 static void stream_underflow_cb(pa_stream *s, void *userdata)
370 audio_output_t *aout = userdata;
373 msg_Warn(aout, "underflow");
374 op = pa_stream_cork(s, 1, NULL, NULL);
376 pa_operation_unref(op);
377 stream_reset_sync(s, aout);
380 static int stream_wait(pa_stream *stream)
382 pa_stream_state_t state;
384 while ((state = pa_stream_get_state(stream)) != PA_STREAM_READY) {
385 if (state == PA_STREAM_FAILED || state == PA_STREAM_TERMINATED)
392 #ifdef LIBPULSE_GETS_A_CLUE
393 static void stream_success_cb(pa_stream *s, int success, void *userdata)
396 (void) s; (void) success; (void) userdata;
399 # define stream_success_cb NULL
404 static void sink_input_info_cb(pa_context *ctx, const pa_sink_input_info *i,
405 int eol, void *userdata)
407 audio_output_t *aout = userdata;
408 aout_sys_t *sys = aout->sys;
415 sys->cvolume = i->volume;
416 volume = pa_cvolume_max(&i->volume) / (float)PA_VOLUME_NORM;
417 aout_VolumeHardSet(aout, volume, i->mute);
421 /*** VLC audio output callbacks ***/
423 /* Memory free callback. The block_t address is in front of the data. */
424 static void data_free(void *data)
426 block_t **pp = data, *block;
428 memcpy(&block, pp - 1, sizeof (block));
429 block_Release(block);
432 static void *data_convert(block_t **pp)
434 block_t *block = *pp;
435 /* In most cases, there is enough head room, and this is really cheap: */
436 block = block_Realloc(block, sizeof (block), block->i_buffer);
438 if (unlikely(block == NULL))
441 memcpy(block->p_buffer, &block, sizeof (block));
442 block->p_buffer += sizeof (block);
443 block->i_buffer -= sizeof (block);
444 return block->p_buffer;
448 * Queue one audio frame to the playabck stream
450 static void Play(audio_output_t *aout, block_t *block)
452 aout_sys_t *sys = aout->sys;
453 pa_stream *s = sys->stream;
455 const void *ptr = data_convert(&block);
456 if (unlikely(ptr == NULL))
459 size_t len = block->i_buffer;
460 mtime_t pts = block->i_pts + block->i_length;
462 /* Note: The core already holds the output FIFO lock at this point.
463 * Therefore we must not under any circumstances (try to) acquire the
464 * output FIFO lock while the PulseAudio threaded main loop lock is held
465 * (including from PulseAudio stream callbacks). Otherwise lock inversion
466 * will take place, and sooner or later a deadlock. */
470 if (pa_stream_is_corked(s) > 0)
471 stream_resync(aout, s);
473 #if 0 /* Fault injector to test underrun recovery */
474 static volatile unsigned u = 0;
475 if ((++u % 1000) == 0) {
476 msg_Err(aout, "fault injection");
477 pa_operation_unref(pa_stream_flush(s, NULL, NULL));
481 if (pa_stream_write(s, ptr, len, data_free, 0, PA_SEEK_RELATIVE) < 0) {
482 vlc_pa_error(aout, "cannot write", sys->context);
483 block_Release(block);
490 * Cork or uncork the playback stream
492 static void Pause(audio_output_t *aout, bool paused, mtime_t date)
494 aout_sys_t *sys = aout->sys;
495 pa_stream *s = sys->stream;
502 op = pa_stream_cork(s, paused, NULL, NULL);
504 pa_operation_unref(op);
506 assert (sys->paused != VLC_TS_INVALID);
508 msg_Dbg(aout, "resuming after %"PRId64" us", date);
509 sys->paused = VLC_TS_INVALID;
511 stream_resync(aout, s);
518 * Flush or drain the playback stream
520 static void Flush(audio_output_t *aout, bool wait)
522 aout_sys_t *sys = aout->sys;
523 pa_stream *s = sys->stream;
529 op = pa_stream_drain(s, NULL, NULL);
530 /* TODO: wait for drain completion*/
532 op = pa_stream_flush(s, NULL, NULL);
534 pa_operation_unref(op);
538 static int VolumeSet(audio_output_t *aout, float vol, bool mute)
540 aout_sys_t *sys = aout->sys;
542 uint32_t idx = pa_stream_get_index(sys->stream);
544 pa_cvolume cvolume = sys->cvolume;
545 pa_volume_t volume = sys->base_volume;
547 pa_cvolume_scale(&cvolume, PA_VOLUME_NORM); /* preserve balance */
549 /* VLC provides the software volume so convert directly to PulseAudio
550 * software volume, pa_volume_t. This is not a linear amplification factor
551 * so do not use PulseAudio linear amplification! */
552 vol *= PA_VOLUME_NORM;
553 if (unlikely(vol >= PA_VOLUME_MAX))
555 volume = pa_sw_volume_multiply(volume, lround(vol));
556 pa_sw_cvolume_multiply_scalar(&cvolume, &cvolume, volume);
558 assert(pa_cvolume_valid(&cvolume));
561 op = pa_context_set_sink_input_volume(sys->context, idx, &cvolume, NULL, NULL);
562 if (likely(op != NULL))
563 pa_operation_unref(op);
564 op = pa_context_set_sink_input_mute(sys->context, idx, mute, NULL, NULL);
565 if (likely(op != NULL))
566 pa_operation_unref(op);
572 static int StreamMove(vlc_object_t *obj, const char *varname, vlc_value_t old,
573 vlc_value_t val, void *userdata)
575 audio_output_t *aout = (audio_output_t *)obj;
576 aout_sys_t *sys = aout->sys;
577 pa_stream *s = userdata;
579 uint32_t idx = pa_stream_get_index(s);
580 uint32_t sink_idx = val.i_int;
582 (void) varname; (void) old;
585 op = pa_context_move_sink_input_by_index(sys->context, idx, sink_idx,
587 if (likely(op != NULL)) {
588 pa_operation_unref(op);
589 msg_Dbg(aout, "moving to sink %"PRIu32, sink_idx);
591 vlc_pa_error(obj, "cannot move sink", sys->context);
594 return (op != NULL) ? VLC_SUCCESS : VLC_EGENERIC;
599 * Create a PulseAudio playback stream, a.k.a. a sink input.
601 static int Open(vlc_object_t *obj)
603 #if !PA_CHECK_VERSION(0,9,22)
604 if (!vlc_xlib_init(obj))
608 audio_output_t *aout = (audio_output_t *)obj;
611 /* Sample format specification */
612 struct pa_sample_spec ss;
613 vlc_fourcc_t format = aout->format.i_format;
618 format = VLC_CODEC_F32B;
620 ss.format = PA_SAMPLE_FLOAT32BE;
623 format = VLC_CODEC_F32L;
625 ss.format = PA_SAMPLE_FLOAT32LE;
628 format = VLC_CODEC_FL32;
629 ss.format = PA_SAMPLE_FLOAT32NE;
632 ss.format = PA_SAMPLE_S32BE;
635 ss.format = PA_SAMPLE_S32LE;
638 ss.format = PA_SAMPLE_S24BE;
641 ss.format = PA_SAMPLE_S24LE;
644 ss.format = PA_SAMPLE_S16BE;
647 ss.format = PA_SAMPLE_S16LE;
650 format = VLC_CODEC_U8;
652 ss.format = PA_SAMPLE_U8;
657 format = VLC_CODEC_FL32;
658 ss.format = PA_SAMPLE_FLOAT32NE;
662 format = VLC_CODEC_S16N;
663 ss.format = PA_SAMPLE_S16NE;
668 ss.rate = aout->format.i_rate;
669 ss.channels = aout_FormatNbChannels(&aout->format);
670 if (!pa_sample_spec_valid(&ss)) {
671 msg_Err(aout, "unsupported sample specification");
675 /* Channel mapping (order defined in vlc_aout.h) */
676 struct pa_channel_map map;
679 if (aout->format.i_physical_channels & AOUT_CHAN_LEFT)
680 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
681 if (aout->format.i_physical_channels & AOUT_CHAN_RIGHT)
682 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
683 if (aout->format.i_physical_channels & AOUT_CHAN_MIDDLELEFT)
684 map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_LEFT;
685 if (aout->format.i_physical_channels & AOUT_CHAN_MIDDLERIGHT)
686 map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_RIGHT;
687 if (aout->format.i_physical_channels & AOUT_CHAN_REARLEFT)
688 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_LEFT;
689 if (aout->format.i_physical_channels & AOUT_CHAN_REARRIGHT)
690 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_RIGHT;
691 if (aout->format.i_physical_channels & AOUT_CHAN_REARCENTER)
692 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_CENTER;
693 if (aout->format.i_physical_channels & AOUT_CHAN_CENTER)
695 if (ss.channels == 1)
696 map.map[map.channels++] = PA_CHANNEL_POSITION_MONO;
698 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_CENTER;
700 if (aout->format.i_physical_channels & AOUT_CHAN_LFE)
701 map.map[map.channels++] = PA_CHANNEL_POSITION_LFE;
703 for (unsigned i = 0; map.channels < ss.channels; i++) {
704 map.map[map.channels++] = PA_CHANNEL_POSITION_AUX0 + i;
705 msg_Warn(aout, "mapping channel %"PRIu8" to AUX%u", map.channels, i);
708 if (!pa_channel_map_valid(&map)) {
709 msg_Err(aout, "unsupported channel map");
712 const char *name = pa_channel_map_to_name(&map);
713 msg_Dbg(aout, "using %s channel map", (name != NULL) ? name : "?");
716 /* Stream parameters */
717 const pa_stream_flags_t flags = PA_STREAM_START_CORKED
718 //| PA_STREAM_INTERPOLATE_TIMING
719 | PA_STREAM_AUTO_TIMING_UPDATE
720 | PA_STREAM_VARIABLE_RATE;
722 struct pa_buffer_attr attr;
724 /* PulseAudio assumes that tlength bytes are available in the buffer. Thus
725 * we need to be conservative and set the minimum value that the VLC
726 * audio decoder thread warrants. Otherwise, PulseAudio buffers will
727 * underrun on hardware with large buffers. VLC keeps at least
728 * AOUT_MIN_PREPARE and at most AOUT_MAX_PREPARE worth of audio buffers.
729 * TODO? tlength could be adaptively increased to reduce wakeups. */
730 attr.tlength = pa_usec_to_bytes(AOUT_MIN_PREPARE_TIME, &ss);
731 attr.prebuf = 0; /* trigger manually */
733 attr.fragsize = 0; /* not used for output */
735 /* Allocate structures */
736 aout_sys_t *sys = malloc(sizeof(*sys));
737 if (unlikely(sys == NULL))
740 pa_context *ctx = vlc_pa_connect (obj);
750 sys->paused = VLC_TS_INVALID;
751 sys->pts = VLC_TS_INVALID;
756 const pa_subscription_mask_t mask = PA_SUBSCRIPTION_MASK_SINK_INPUT;
758 pa_context_set_subscribe_callback(ctx, context_cb, aout);
759 op = pa_context_subscribe(ctx, mask, NULL, NULL);
760 if (likely(op != NULL))
761 pa_operation_unref(op);
764 sys->base_volume = PA_VOLUME_NORM;
765 pa_cvolume_set(&sys->cvolume, ss.channels, PA_VOLUME_NORM);
768 /* Create a playback stream */
769 pa_stream *s = pa_stream_new(ctx, "audio stream", &ss, &map);
771 vlc_pa_error(obj, "stream creation failure", ctx);
775 pa_stream_set_state_callback(s, stream_state_cb, NULL);
776 pa_stream_set_latency_update_callback(s, stream_latency_cb, aout);
777 pa_stream_set_moved_callback(s, stream_moved_cb, aout);
778 pa_stream_set_overflow_callback(s, stream_overflow_cb, aout);
779 pa_stream_set_started_callback(s, stream_started_cb, aout);
780 pa_stream_set_suspended_callback(s, stream_suspended_cb, aout);
781 pa_stream_set_underflow_callback(s, stream_underflow_cb, aout);
783 if (pa_stream_connect_playback(s, NULL, &attr, flags, NULL, NULL) < 0
785 vlc_pa_error(obj, "stream connection failure", ctx);
789 const struct pa_buffer_attr *pba = pa_stream_get_buffer_attr(s);
790 msg_Dbg(aout, "using buffer metrics: maxlength=%u, tlength=%u, "
791 "prebuf=%u, minreq=%u",
792 pba->maxlength, pba->tlength, pba->prebuf, pba->minreq);
794 aout->i_nb_samples = pba->minreq / pa_frame_size(&ss);
796 var_Create(aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE);
797 var_Change(aout, "audio-device", VLC_VAR_SETTEXT,
798 &(vlc_value_t){ .psz_string = (char *)_("Audio device") },
800 var_AddCallback (aout, "audio-device", StreamMove, s);
801 op = pa_context_get_sink_info_list(ctx, sink_list_cb, aout);
802 /* We may need to wait for completion... once LibVLC supports this */
804 pa_operation_unref(op);
805 stream_moved_cb(s, aout);
808 aout->format.i_format = format;
809 aout->pf_play = Play;
810 aout->pf_pause = Pause;
811 aout->pf_flush = Flush;
812 aout->pf_volume_set = VolumeSet;
822 * Removes a PulseAudio playback stream
824 static void Close (vlc_object_t *obj)
826 audio_output_t *aout = (audio_output_t *)obj;
827 aout_sys_t *sys = aout->sys;
828 pa_context *ctx = sys->context;
829 pa_stream *s = sys->stream;
832 /* The callback takes mainloop lock, so it CANNOT be held here! */
833 var_DelCallback (aout, "audio-device", StreamMove, s);
834 var_Destroy (aout, "audio-device");
841 if (pa_stream_is_corked(s) > 0)
842 /* Stream paused: discard all buffers */
843 op = pa_stream_flush(s, stream_success_cb, NULL);
845 /* Stream playing: wait until buffers are played */
846 op = pa_stream_drain(s, stream_success_cb, NULL);
847 if (likely(op != NULL)) {
848 #ifdef LIBPULSE_GETS_A_CLUE
849 while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
852 pa_operation_unref(op);
855 pa_stream_disconnect(s);
860 vlc_pa_disconnect(obj, ctx);