]> git.sesse.net Git - vlc/blob - modules/audio_output/pulse.c
f7cb16008e23aca8653b4b0a65606c1651959405
[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_aout_intf.h>
32 #include <vlc_cpu.h>
33
34 #include <pulse/pulseaudio.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: single static mainloop */
50
51 /* NOTE:
52  * Be careful what you do when the PulseAudio mainloop is held, which is to say
53  * within PulseAudio callbacks, or after pa_threaded_mainloop_lock().
54  * In particular, a VLC variable callback cannot be triggered nor deleted with
55  * the PulseAudio mainloop lock held, if the callback acquires the lock. */
56
57 struct aout_sys_t
58 {
59     pa_stream *stream; /**< PulseAudio playback stream object */
60     pa_context *context; /**< PulseAudio connection context */
61     pa_threaded_mainloop *mainloop; /**< PulseAudio event loop */
62     pa_volume_t base_volume; /**< 0dB reference volume */
63     pa_cvolume cvolume; /**< actual sink input volume */
64     //uint32_t byterate; /**< bytes per second */
65 };
66
67 /* Context helpers */
68 static void context_state_cb(pa_context *c, void *userdata)
69 {
70     pa_threaded_mainloop *mainloop = userdata;
71
72     switch (pa_context_get_state(c)) {
73         case PA_CONTEXT_READY:
74         case PA_CONTEXT_FAILED:
75         case PA_CONTEXT_TERMINATED:
76             pa_threaded_mainloop_signal(mainloop, 0);
77         default:
78             break;
79     }
80 }
81
82 static bool context_wait(pa_threaded_mainloop *mainloop, pa_context *context)
83 {
84     pa_context_state_t state;
85
86     while ((state = pa_context_get_state(context)) != PA_CONTEXT_READY) {
87         if (state == PA_CONTEXT_FAILED || state == PA_CONTEXT_TERMINATED)
88             return -1;
89         pa_threaded_mainloop_wait(mainloop);
90     }
91     return 0;
92 }
93
94 static void error(aout_instance_t *aout, const char *msg, pa_context *context)
95 {
96     msg_Err(aout, "%s: %s", msg, pa_strerror(pa_context_errno(context)));
97 }
98
99 /* Sink */
100 static void sink_list_cb(pa_context *c, const pa_sink_info *i, int eol,
101                          void *userdata)
102 {
103     aout_instance_t *aout = userdata;
104     vlc_value_t val, text;
105
106     if (eol)
107         return;
108     (void) c;
109
110     msg_Dbg(aout, "listing sink %s (%"PRIu32"): %s", i->name, i->index,
111             i->description);
112     val.i_int = i->index;
113     text.psz_string = (char *)i->description;
114     var_Change(aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text);
115 }
116
117 static void sink_info_cb(pa_context *c, const pa_sink_info *i, int eol,
118                          void *userdata)
119 {
120     aout_instance_t *aout = userdata;
121     aout_sys_t *sys = aout->output.p_sys;
122
123     if (eol)
124         return;
125     (void) c;
126
127     /* PulseAudio flat volume NORM / 100% / 0dB corresponds to no software
128      * amplification and maximum hardware amplification.
129      * VLC maps DEFAULT / 100% to no gain at all (software/hardware).
130      * Thus we need to use the sink base_volume as a multiplier,
131      * if and only if flat volume is active for our current sink. */
132     if (i->flags & PA_SINK_FLAT_VOLUME)
133         sys->base_volume = i->base_volume;
134     else
135         sys->base_volume = PA_VOLUME_NORM;
136     msg_Dbg(aout, "base volume: %f", pa_sw_volume_to_linear(sys->base_volume));
137 }
138
139 /* Stream helpers */
140 static void stream_state_cb(pa_stream *s, void *userdata)
141 {
142     pa_threaded_mainloop *mainloop = userdata;
143
144     switch (pa_stream_get_state(s)) {
145         case PA_STREAM_READY:
146         case PA_STREAM_FAILED:
147         case PA_STREAM_TERMINATED:
148             pa_threaded_mainloop_signal(mainloop, 0);
149         default:
150             break;
151     }
152 }
153
154 static void stream_moved_cb(pa_stream *s, void *userdata)
155 {
156     aout_instance_t *aout = userdata;
157     aout_sys_t *sys = aout->output.p_sys;
158     pa_operation *op;
159     uint32_t idx = pa_stream_get_device_index(s);
160
161     msg_Dbg(aout, "connected to sink %"PRIu32": %s", idx,
162                   pa_stream_get_device_name(s));
163     op = pa_context_get_sink_info_by_index(sys->context, idx,
164                                            sink_info_cb, aout);
165     if (likely(op != NULL))
166         pa_operation_unref(op);
167
168     /* Update the variable if someone else moved our stream */
169     var_Change(aout, "audio-device", VLC_VAR_SETVALUE,
170                &(vlc_value_t){ .i_int = idx }, NULL);
171 }
172
173 static void stream_overflow_cb(pa_stream *s, void *userdata)
174 {
175     aout_instance_t *aout = userdata;
176
177     msg_Err(aout, "overflow");
178     (void) s;
179 }
180
181 static void stream_started_cb(pa_stream *s, void *userdata)
182 {
183     aout_instance_t *aout = userdata;
184
185     msg_Dbg(aout, "started");
186     (void) s;
187 }
188
189 static void stream_suspended_cb(pa_stream *s, void *userdata)
190 {
191     aout_instance_t *aout = userdata;
192
193     msg_Dbg(aout, "suspended");
194     (void) s;
195 }
196
197 static void stream_underflow_cb(pa_stream *s, void *userdata)
198 {
199     aout_instance_t *aout = userdata;
200
201     msg_Dbg(aout, "underflow");
202     (void) s;
203 }
204
205 static int stream_wait(pa_threaded_mainloop *mainloop, pa_stream *stream)
206 {
207     pa_stream_state_t state;
208
209     while ((state = pa_stream_get_state(stream)) != PA_STREAM_READY) {
210         if (state == PA_STREAM_FAILED || state == PA_STREAM_TERMINATED)
211             return -1;
212         pa_threaded_mainloop_wait(mainloop);
213     }
214     return 0;
215 }
216
217 /* Memory free callback. The block_t address is in front of the data. */
218 static void data_free(void *data)
219 {
220     block_t **pp = data, *block;
221
222     memcpy(&block, pp - 1, sizeof (block));
223     block_Release(block);
224 }
225
226 static void *data_convert(block_t **pp)
227 {
228     block_t *block = *pp;
229     /* In most cases, there is enough head room, and this is really cheap: */
230     block = block_Realloc(block, sizeof (block), block->i_buffer);
231     *pp = block;
232     if (unlikely(block == NULL))
233         return NULL;
234
235     memcpy(block->p_buffer, &block, sizeof (block));
236     block->p_buffer += sizeof (block);
237     block->i_buffer -= sizeof (block);
238     return block->p_buffer;
239 }
240
241 /*****************************************************************************
242  * Play: play a sound samples buffer
243  *****************************************************************************/
244 static void Play(aout_instance_t *aout)
245 {
246     aout_sys_t *sys = aout->output.p_sys;
247     pa_stream *s = sys->stream;
248
249     /* Note: The core already holds the output FIFO lock at this point.
250      * Therefore we must not under any circumstances (try to) acquire the
251      * output FIFO lock while the PulseAudio threaded main loop lock is held
252      * (including from PulseAudio stream callbacks). Otherwise lock inversion
253      * will take place, and sooner or later a deadlock. */
254     pa_threaded_mainloop_lock(sys->mainloop);
255
256     if (pa_stream_is_corked(s) > 0) {
257         pa_operation *op = pa_stream_cork(s, 0, NULL, NULL);
258         if (op != NULL)
259             pa_operation_unref(op);
260         msg_Dbg(aout, "uncorking");
261     }
262
263 #if 0
264     /* This function should be called by the LibVLC core a header of time,
265      * but not more than AOUT_MAX_PREPARE. The PulseAudio latency should be
266      * shorter than that (though it might not be the case with some evil piece
267      * of audio output hardware). So we may need to trigger playback early,
268      * (that is to say, short cut the PulseAudio prebuffering). Otherwise,
269      * audio and video may be out of synchronization. */
270     pa_usec_t latency;
271     int negative;
272     if (pa_stream_get_latency(s, &latency, &negative) < 0) {
273         /* Especially at start of stream, latency may not be known (yet). */
274         if (pa_context_errno(sys->context) != PA_ERR_NODATA)
275             error(aout, "cannot determine latency", sys->context);
276     } else {
277         mtime_t gap = aout_FifoFirstDate(&aout->output.fifo) - mdate()
278                 - latency;
279
280         if (gap > AOUT_PTS_TOLERANCE)
281             msg_Dbg(aout, "buffer too early (%"PRId64" us)", gap);
282         else if (gap < -AOUT_PTS_TOLERANCE)
283             msg_Err(aout, "buffer too late (%"PRId64" us)", -gap);
284     }
285 #endif
286 #if 0 /* Fault injector to test underrun recovery */
287     static unsigned u = 0;
288     if ((++u % 500) == 0) {
289         msg_Err(aout, "fault injection");
290         msleep(CLOCK_FREQ*2);
291     }
292 #endif
293
294     /* This function is called exactly once per block in the output FIFO, so
295      * this for-loop is not necessary.
296      * If this function is changed to not always dequeue blocks, be sure to
297      * limit the queue size to a reasonable limit to avoid huge leaks. */
298     for (;;) {
299         block_t *block = aout_FifoPop(&aout->output.fifo);
300         if (block == NULL)
301             break;
302
303         const void *ptr = data_convert(&block);
304         if (unlikely(ptr == NULL))
305             break;
306
307         size_t len = block->i_buffer;
308         //mtime_t pts = block->i_pts, duration = block->i_length;
309
310         if (pa_stream_write(s, ptr, len, data_free, 0, PA_SEEK_RELATIVE) < 0)
311         {
312             error(aout, "cannot write", sys->context);
313             block_Release(block);
314         }
315     }
316
317     pa_threaded_mainloop_unlock(sys->mainloop);
318 }
319
320 static void Pause(aout_instance_t *aout, bool b_paused, mtime_t i_date)
321 {
322     aout_sys_t *sys = aout->output.p_sys;
323     pa_stream *s = sys->stream;
324
325     /* Note: The core already holds the output FIFO lock at this point.
326      * Therefore we must not under any circumstances (try to) acquire the
327      * output FIFO lock while the PulseAudio threaded main loop lock is held
328      * (including from PulseAudio stream callbacks). Otherwise lock inversion
329      * will take place, and sooner or later a deadlock. */
330     pa_threaded_mainloop_lock(sys->mainloop);
331
332     pa_operation *op = pa_stream_cork(s, b_paused ? 1 : 0, NULL, NULL);
333     if (op != NULL)
334         pa_operation_unref(op);
335
336     pa_threaded_mainloop_unlock(sys->mainloop);
337 }
338
339 static int VolumeSet(aout_instance_t *aout, audio_volume_t vol, bool mute)
340 {
341     aout_sys_t *sys = aout->output.p_sys;
342     pa_threaded_mainloop *mainloop = sys->mainloop;
343     pa_operation *op;
344
345     uint32_t idx = pa_stream_get_index(sys->stream);
346     pa_volume_t volume = pa_sw_volume_from_linear(vol / (float)AOUT_VOLUME_DEFAULT);
347     pa_cvolume cvolume;
348
349     /* TODO: do not ruin the channel balance (if set outside VLC) */
350     /* TODO: notify UI about volume changes by other PulseAudio clients */
351     pa_cvolume_set(&sys->cvolume, sys->cvolume.channels, volume);
352     pa_sw_cvolume_multiply_scalar(&cvolume, &sys->cvolume, sys->base_volume);
353     assert(pa_cvolume_valid(&cvolume));
354
355     pa_threaded_mainloop_lock(mainloop);
356     op = pa_context_set_sink_input_volume(sys->context, idx, &cvolume, NULL, NULL);
357     if (likely(op != NULL))
358         pa_operation_unref(op);
359     op = pa_context_set_sink_input_mute(sys->context, idx, mute, NULL, NULL);
360     if (likely(op != NULL))
361         pa_operation_unref(op);
362     pa_threaded_mainloop_unlock(mainloop);
363
364     return 0;
365 }
366
367 static int StreamMove(vlc_object_t *obj, const char *varname, vlc_value_t old,
368                       vlc_value_t val, void *userdata)
369 {
370     aout_instance_t *aout = (aout_instance_t *)obj;
371     aout_sys_t *sys = aout->output.p_sys;
372     pa_stream *s = userdata;
373     pa_operation *op;
374     uint32_t idx = pa_stream_get_index(s);
375     uint32_t sink_idx = val.i_int;
376
377     (void) varname; (void) old;
378
379     pa_threaded_mainloop_lock(sys->mainloop);
380     op = pa_context_move_sink_input_by_index(sys->context, idx, sink_idx,
381                                              NULL, NULL);
382     if (likely(op != NULL)) {
383         pa_operation_unref(op);
384         msg_Dbg(aout, "moving to sink %"PRIu32, sink_idx);
385     } else
386         error(aout, "cannot move sink", sys->context);
387     pa_threaded_mainloop_unlock(sys->mainloop);
388
389     return (op != NULL) ? VLC_SUCCESS : VLC_EGENERIC;
390 }
391
392
393 /*****************************************************************************
394  * Open: open the audio device
395  *****************************************************************************/
396 static int Open(vlc_object_t *obj)
397 {
398     aout_instance_t *aout = (aout_instance_t *)obj;
399     pa_operation *op;
400
401     /* Sample format specification */
402     struct pa_sample_spec ss;
403
404     switch(aout->output.output.i_format)
405     {
406         case VLC_CODEC_F64B:
407             aout->output.output.i_format = VLC_CODEC_F32B;
408         case VLC_CODEC_F32B:
409             ss.format = PA_SAMPLE_FLOAT32BE;
410             break;
411         case VLC_CODEC_F64L:
412             aout->output.output.i_format = VLC_CODEC_F32L;
413         case VLC_CODEC_F32L:
414             ss.format = PA_SAMPLE_FLOAT32LE;
415             break;
416         case VLC_CODEC_FI32:
417             aout->output.output.i_format = VLC_CODEC_FL32;
418             ss.format = PA_SAMPLE_FLOAT32NE;
419             break;
420         case VLC_CODEC_S32B:
421             ss.format = PA_SAMPLE_S32BE;
422             break;
423         case VLC_CODEC_S32L:
424             ss.format = PA_SAMPLE_S32LE;
425             break;
426         case VLC_CODEC_S24B:
427             ss.format = PA_SAMPLE_S24BE;
428             break;
429         case VLC_CODEC_S24L:
430             ss.format = PA_SAMPLE_S24LE;
431             break;
432         case VLC_CODEC_S16B:
433             ss.format = PA_SAMPLE_S16BE;
434             break;
435         case VLC_CODEC_S16L:
436             ss.format = PA_SAMPLE_S16LE;
437             break;
438         case VLC_CODEC_S8:
439             aout->output.output.i_format = VLC_CODEC_U8;
440         case VLC_CODEC_U8:
441             ss.format = PA_SAMPLE_U8;
442             break;
443         default:
444             if (HAVE_FPU)
445             {
446                 aout->output.output.i_format = VLC_CODEC_FL32;
447                 ss.format = PA_SAMPLE_FLOAT32NE;
448             }
449             else
450             {
451                 aout->output.output.i_format = VLC_CODEC_S16N;
452                 ss.format = PA_SAMPLE_S16NE;
453             }
454             break;
455     }
456
457     ss.rate = aout->output.output.i_rate;
458     ss.channels = aout_FormatNbChannels(&aout->output.output);
459     if (!pa_sample_spec_valid(&ss)) {
460         msg_Err(aout, "unsupported sample specification");
461         return VLC_EGENERIC;
462     }
463
464     /* Channel mapping (order defined in vlc_aout.h) */
465     struct pa_channel_map map;
466     map.channels = 0;
467
468     if (aout->output.output.i_physical_channels & AOUT_CHAN_LEFT)
469         map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
470     if (aout->output.output.i_physical_channels & AOUT_CHAN_RIGHT)
471         map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
472     if (aout->output.output.i_physical_channels & AOUT_CHAN_MIDDLELEFT)
473         map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_LEFT;
474     if (aout->output.output.i_physical_channels & AOUT_CHAN_MIDDLERIGHT)
475         map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_RIGHT;
476     if (aout->output.output.i_physical_channels & AOUT_CHAN_REARLEFT)
477         map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_LEFT;
478     if (aout->output.output.i_physical_channels & AOUT_CHAN_REARRIGHT)
479         map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_RIGHT;
480     if (aout->output.output.i_physical_channels & AOUT_CHAN_REARCENTER)
481         map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_CENTER;
482     if (aout->output.output.i_physical_channels & AOUT_CHAN_CENTER)
483     {
484         if (ss.channels == 1)
485             map.map[map.channels++] = PA_CHANNEL_POSITION_MONO;
486         else
487             map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_CENTER;
488     }
489     if (aout->output.output.i_physical_channels & AOUT_CHAN_LFE)
490         map.map[map.channels++] = PA_CHANNEL_POSITION_LFE;
491
492     for (unsigned i = 0; map.channels < ss.channels; i++) {
493         map.map[map.channels++] = PA_CHANNEL_POSITION_AUX0 + i;
494         msg_Warn(aout, "mapping channel %"PRIu8" to AUX%u", map.channels, i);
495     }
496
497     if (!pa_channel_map_valid(&map)) {
498         msg_Err(aout, "unsupported channel map");
499         return VLC_EGENERIC;
500     } else {
501         const char *name = pa_channel_map_to_pretty_name(&map);
502         msg_Dbg(aout, "using %s channel map", (name != NULL) ? name : "?");
503     }
504
505     /* Stream parameters */
506     const pa_stream_flags_t flags = PA_STREAM_INTERPOLATE_TIMING
507                                   | PA_STREAM_AUTO_TIMING_UPDATE
508                                   | PA_STREAM_ADJUST_LATENCY
509                                   | PA_STREAM_START_CORKED;
510
511     const uint32_t byterate = pa_bytes_per_second(&ss);
512     struct pa_buffer_attr attr;
513     /* no point in larger buffers on PA side than VLC */
514     attr.maxlength = -1;
515     attr.tlength = byterate * AOUT_MAX_ADVANCE_TIME / CLOCK_FREQ;
516     attr.prebuf = byterate * AOUT_MAX_PREPARE_TIME / CLOCK_FREQ;
517     attr.minreq = -1;
518     attr.fragsize = 0; /* not used for output */
519
520     /* Allocate structures */
521     aout_sys_t *sys = malloc(sizeof(*sys));
522     if (unlikely(sys == NULL))
523         return VLC_ENOMEM;
524     aout->output.p_sys = sys;
525     sys->context = NULL;
526     sys->stream = NULL;
527     //sys->byterate = byterate;
528
529     /* Channel volume */
530     sys->base_volume = PA_VOLUME_NORM;
531     pa_cvolume_set(&sys->cvolume, ss.channels, PA_VOLUME_NORM);
532
533     /* Allocate threaded main loop */
534     pa_threaded_mainloop *mainloop = pa_threaded_mainloop_new();
535     if (unlikely(mainloop == NULL)) {
536         free(sys);
537         return VLC_ENOMEM;
538     }
539     sys->mainloop = mainloop;
540
541     if (pa_threaded_mainloop_start(mainloop) < 0) {
542         pa_threaded_mainloop_free(mainloop);
543         free(sys);
544         return VLC_ENOMEM;
545     }
546     pa_threaded_mainloop_lock(mainloop);
547
548     /* Connect to PulseAudio server */
549     char *user_agent = var_InheritString(aout, "user-agent");
550     pa_context *ctx = pa_context_new(pa_threaded_mainloop_get_api(mainloop),
551                                      user_agent);
552     free(user_agent);
553     if (unlikely(ctx == NULL))
554         goto fail;
555     sys->context = ctx;
556
557     pa_context_set_state_callback(ctx, context_state_cb, mainloop);
558     if (pa_context_connect(ctx, NULL, 0, NULL) < 0
559      || context_wait(mainloop, ctx)) {
560         error(aout, "cannot connect to server", ctx);
561         goto fail;
562     }
563
564     /* Create a playback stream */
565     pa_stream *s = pa_stream_new(ctx, "audio stream", &ss, &map);
566     if (s == NULL) {
567         error(aout, "cannot create stream", ctx);
568         goto fail;
569     }
570     sys->stream = s;
571     pa_stream_set_state_callback(s, stream_state_cb, mainloop);
572     pa_stream_set_moved_callback(s, stream_moved_cb, aout);
573     pa_stream_set_overflow_callback(s, stream_overflow_cb, aout);
574     pa_stream_set_started_callback(s, stream_started_cb, aout);
575     pa_stream_set_suspended_callback(s, stream_suspended_cb, aout);
576     pa_stream_set_underflow_callback(s, stream_underflow_cb, aout);
577
578     if (pa_stream_connect_playback(s, NULL, &attr, flags, NULL, NULL) < 0
579      || stream_wait(mainloop, s)) {
580         error(aout, "cannot connect stream", ctx);
581         goto fail;
582     }
583
584     const struct pa_buffer_attr *pba = pa_stream_get_buffer_attr(s);
585     msg_Dbg(aout, "using buffer metrics: maxlength=%u, tlength=%u, "
586             "prebuf=%u, minreq=%u",
587             pba->maxlength, pba->tlength, pba->prebuf, pba->minreq);
588
589     aout->output.i_nb_samples = pba->minreq / pa_frame_size(&ss);
590
591     var_Create(aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE);
592     var_Change(aout, "audio-device", VLC_VAR_SETTEXT,
593                &(vlc_value_t){ .psz_string = (char *)_("Audio device") },
594                NULL);
595     var_AddCallback (aout, "audio-device", StreamMove, s);
596     op = pa_context_get_sink_info_list(ctx, sink_list_cb, aout);
597     /* We may need to wait for completion... once LibVLC supports this */
598     if (op != NULL)
599         pa_operation_unref(op);
600     stream_moved_cb(s, aout);
601     pa_threaded_mainloop_unlock(mainloop);
602
603     aout->output.pf_play = Play;
604     aout->output.pf_pause = Pause;
605     aout->output.pf_volume_set = VolumeSet;
606     return VLC_SUCCESS;
607
608 fail:
609     pa_threaded_mainloop_unlock(mainloop);
610     Close(obj);
611     return VLC_EGENERIC;
612 }
613
614 /*****************************************************************************
615  * Close: close the audio device
616  *****************************************************************************/
617 static void Close (vlc_object_t *obj)
618 {
619     aout_instance_t *aout = (aout_instance_t *)obj;
620     aout_sys_t *sys = aout->output.p_sys;
621     pa_threaded_mainloop *mainloop = sys->mainloop;
622     pa_context *ctx = sys->context;
623     pa_stream *s = sys->stream;
624
625     if (s != NULL) {
626         /* The callback takes mainloop lock, so it CANNOT be held here! */
627         var_DelCallback (aout, "audio-device", StreamMove, s);
628         var_Destroy (aout, "audio-device");
629     }
630
631     pa_threaded_mainloop_lock(mainloop);
632     if (s != NULL) {
633         pa_operation *op;
634
635         op = pa_stream_flush(s, NULL, NULL);
636         if (op != NULL)
637             pa_operation_unref(op);
638         op = pa_stream_drain(s, NULL, NULL);
639         if (op != NULL)
640             pa_operation_unref(op);
641         pa_stream_disconnect(s);
642         pa_stream_unref(s);
643     }
644     if (ctx != NULL)
645         pa_context_unref(ctx);
646     pa_threaded_mainloop_unlock(mainloop);
647     pa_threaded_mainloop_free(mainloop);
648     free(sys);
649 }