]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/pulse.c
taglib: init attachments before reading special tags
[vlc] / modules / audio_output / pulse.c
index fbf9a85e2cf73eb8309e9caba76c468b9bce7378..601911e32438d519444326bc29fca692c5485997 100644 (file)
 #include <vlc_cpu.h>
 
 #include <pulse/pulseaudio.h>
-#include "vlcpulse.h"
-#if !PA_CHECK_VERSION(0,9,22)
-# include <vlc_xlib.h>
-#endif
+#include "audio_output/vlcpulse.h"
 
 static int  Open        ( vlc_object_t * );
 static void Close       ( vlc_object_t * );
@@ -56,6 +53,14 @@ vlc_module_end ()
  * In particular, a VLC variable callback cannot be triggered nor deleted with
  * the PulseAudio mainloop lock held, if the callback acquires the lock. */
 
+struct sink
+{
+    struct sink *next;
+    uint32_t index;
+    pa_volume_t base_volume;
+    char name[1];
+};
+
 struct aout_sys_t
 {
     pa_stream *stream; /**< PulseAudio playback stream object */
@@ -65,125 +70,149 @@ struct aout_sys_t
     pa_volume_t base_volume; /**< 0dB reference volume */
     pa_cvolume cvolume; /**< actual sink input volume */
     mtime_t first_pts; /**< Play time of buffer start */
-    mtime_t paused; /**< Time when (last) paused */
-};
 
-static void sink_list_cb(pa_context *, const pa_sink_info *, int, void *);
-static void sink_input_info_cb(pa_context *, const pa_sink_input_info *,
-                               int, void *);
+    pa_volume_t volume_force; /**< Forced volume (stream must be NULL) */
+    pa_stream_flags_t flags_force; /**< Forced flags (stream must be NULL) */
+    char *sink_force; /**< Forced sink name (stream must be NULL) */
 
-/*** Context ***/
-static void context_cb(pa_context *ctx, pa_subscription_event_type_t type,
-                       uint32_t idx, void *userdata)
+    struct sink *sinks; /**< Locally-cached list of sinks */
+};
+
+static void VolumeReport(audio_output_t *aout)
 {
-    audio_output_t *aout = userdata;
     aout_sys_t *sys = aout->sys;
-    pa_operation *op;
-
-    switch (type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK)
-    {
-      case PA_SUBSCRIPTION_EVENT_SINK:
-        switch (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK)
-        {
-          case PA_SUBSCRIPTION_EVENT_NEW:
-          case PA_SUBSCRIPTION_EVENT_CHANGE:
-            op = pa_context_get_sink_info_by_index(ctx, idx, sink_list_cb, aout);
-            if (likely(op != NULL))
-                pa_operation_unref(op);
-            break;
-
-          case PA_SUBSCRIPTION_EVENT_REMOVE:
-            var_Change(aout, "audio-device", VLC_VAR_DELCHOICE,
-                       &(vlc_value_t){ .i_int = idx }, NULL);
-            break;
-        }
-        break;
-
-      case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
-        if (sys->stream == NULL || idx != pa_stream_get_index(sys->stream))
-            break; /* only interested in our sink input */
-
-        /* Gee... PA will not provide the infos directly in the event. */
-        switch (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK)
-        {
-          case PA_SUBSCRIPTION_EVENT_REMOVE:
-            msg_Err(aout, "sink input killed!");
-            break;
+    pa_volume_t volume = pa_cvolume_max(&sys->cvolume);
 
-          default:
-            op = pa_context_get_sink_input_info(ctx, idx, sink_input_info_cb,
-                                                aout);
-            if (likely(op != NULL))
-                pa_operation_unref(op);
-            break;
-        }
-        break;
+    volume = pa_sw_volume_divide(volume, sys->base_volume);
+    aout_VolumeReport(aout, (float)volume / PA_VOLUME_NORM);
+}
 
-      default: /* unsubscribed facility?! */
-        assert(0);
-    }
+/*** Sink ***/
+static struct sink *sink_find(aout_sys_t *sys, uint32_t index)
+{
+    for (struct sink *sink = sys->sinks; sink != NULL; sink = sink->next)
+        if (sink->index == index)
+            return sink;
+    return NULL;
 }
 
+static struct sink *sink_find_by_name(aout_sys_t *sys, const char *name)
+{
+    for (struct sink *sink = sys->sinks; sink != NULL; sink = sink->next)
+        if (!strcmp(sink->name, name))
+            return sink;
+    return NULL;
+}
 
-/*** Sink ***/
-static void sink_list_cb(pa_context *c, const pa_sink_info *i, int eol,
-                         void *userdata)
+static void sink_add_cb(pa_context *ctx, const pa_sink_info *i, int eol,
+                        void *userdata)
 {
     audio_output_t *aout = userdata;
     aout_sys_t *sys = aout->sys;
-    vlc_value_t val, text;
 
     if (eol)
+    {
+        pa_threaded_mainloop_signal(sys->mainloop, 0);
         return;
-    (void) c;
+    }
+    (void) ctx;
 
-    msg_Dbg(aout, "listing sink %s (%"PRIu32"): %s", i->name, i->index,
+    msg_Dbg(aout, "adding sink %"PRIu32": %s (%s)", i->index, i->name,
             i->description);
-    val.i_int = i->index;
-    text.psz_string = (char *)i->description;
-    /* FIXME: There is no way to replace a choice explicitly. */
-    var_Change(aout, "audio-device", VLC_VAR_DELCHOICE, &val, NULL);
-    var_Change(aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text);
-    /* FIXME: var_Change() can change the variable value if we remove the
-     * current value from the choice list, or if we add a choice while there
-     * was none. So force the correct value back. */
-    if (sys->stream != NULL)
-    {
-        val.i_int = pa_stream_get_device_index(sys->stream);
-        var_Change(aout, "audio-device", VLC_VAR_SETVALUE, &val, NULL);
-    }
+    aout_HotplugReport(aout, i->name, i->description);
+
+    size_t namelen = strlen(i->name);
+    struct sink *sink = malloc(sizeof (*sink) + namelen);
+    if (unlikely(sink == NULL))
+        return;
+
+    sink->next = sys->sinks;
+    sink->index = i->index;
+    /* PulseAudio flat volume NORM / 100% / 0dB corresponds to no software
+     * amplification and maximum hardware amplification.
+     * VLC maps DEFAULT / 100% to no gain at all (software/hardware).
+     * Thus we need to use the sink base_volume as a multiplier,
+     * if and only if flat volume is active for our current sink. */
+    if (i->flags & PA_SINK_FLAT_VOLUME)
+        sink->base_volume = i->base_volume;
+    else
+        sink->base_volume = PA_VOLUME_NORM;
+    memcpy(sink->name, i->name, namelen + 1);
+    sys->sinks = sink;
 }
 
-static void sink_info_cb(pa_context *c, const pa_sink_info *i, int eol,
-                         void *userdata)
+static void sink_mod_cb(pa_context *ctx, const pa_sink_info *i, int eol,
+                        void *userdata)
 {
     audio_output_t *aout = userdata;
     aout_sys_t *sys = aout->sys;
 
     if (eol)
         return;
-    (void) c;
+    (void) ctx;
+
+    msg_Dbg(aout, "changing sink %"PRIu32": %s (%s)", i->index, i->name,
+            i->description);
+    aout_HotplugReport(aout, i->name, i->description);
+
+    struct sink *sink = sink_find(sys, i->index);
+    if (unlikely(sink == NULL))
+        return;
 
-    /* PulseAudio flat volume NORM / 100% / 0dB corresponds to no software
-     * amplification and maximum hardware amplification.
-     * VLC maps DEFAULT / 100% to no gain at all (software/hardware).
-     * Thus we need to use the sink base_volume as a multiplier,
-     * if and only if flat volume is active for our current sink. */
     if (i->flags & PA_SINK_FLAT_VOLUME)
-        sys->base_volume = i->base_volume;
+        sink->base_volume = i->base_volume;
     else
-        sys->base_volume = PA_VOLUME_NORM;
-    msg_Dbg(aout, "base volume: %"PRIu32, sys->base_volume);
+        sink->base_volume = PA_VOLUME_NORM;
+}
+
+static void sink_del(uint32_t index, audio_output_t *aout)
+{
+    aout_sys_t *sys = aout->sys;
+    struct sink **pp = &sys->sinks, *sink;
+
+    msg_Dbg(aout, "removing sink %"PRIu32, index);
+
+    while ((sink = *pp) != NULL)
+        if (sink->index == index)
+        {
+            *pp = sink->next;
+            aout_HotplugReport(aout, sink->name, NULL);
+            free(sink);
+        }
+        else
+            pp = &sink->next;
+}
+
+static void sink_event(pa_context *ctx, unsigned type, uint32_t idx,
+                       audio_output_t *aout)
+{
+    pa_operation *op = NULL;
+
+    switch (type)
+    {
+        case PA_SUBSCRIPTION_EVENT_NEW:
+            op = pa_context_get_sink_info_by_index(ctx, idx, sink_add_cb,
+                                                   aout);
+            break;
+        case PA_SUBSCRIPTION_EVENT_CHANGE:
+            op = pa_context_get_sink_info_by_index(ctx, idx, sink_mod_cb,
+                                                   aout);
+            break;
+        case PA_SUBSCRIPTION_EVENT_REMOVE:
+            sink_del(idx, aout);
+            break;
+    }
+    if (op != NULL)
+        pa_operation_unref(op);
 }
 
 
 /*** Latency management and lip synchronization ***/
 static void stream_start_now(pa_stream *s, audio_output_t *aout)
 {
-    aout_sys_t *sys = aout->sys;
     pa_operation *op;
 
-    assert (sys->trigger == NULL);
+    assert (aout->sys->trigger == NULL);
 
     op = pa_stream_cork(s, 0, NULL, NULL);
     if (op != NULL)
@@ -264,8 +293,7 @@ static void stream_latency_cb(pa_stream *s, void *userdata)
     audio_output_t *aout = userdata;
     aout_sys_t *sys = aout->sys;
 
-    if (sys->paused != VLC_TS_INVALID)
-        return; /* nothing to do while paused */
+    /* This callback is _never_ called while paused. */
     if (sys->first_pts == VLC_TS_INVALID)
         return; /* nothing to do if buffers are (still) empty */
     if (pa_stream_is_corked(s) > 0)
@@ -309,15 +337,11 @@ static void stream_event_cb(pa_stream *s, const char *name, pa_proplist *pl,
     if (!strcmp(name, PA_STREAM_EVENT_REQUEST_UNCORK))
         aout_PolicyReport(aout, false);
     else
-#if PA_CHECK_VERSION(1,0,0)
     /* FIXME: expose aout_Restart() directly */
     if (!strcmp(name, PA_STREAM_EVENT_FORMAT_LOST)) {
-        vlc_value_t dummy = { .i_int = 0 };
-
         msg_Dbg (aout, "format lost");
         aout_RestartRequest (aout, AOUT_RESTART_OUTPUT);
     } else
-#endif
         msg_Warn (aout, "unhandled stream event \"%s\"", name);
     (void) s;
     (void) pl;
@@ -327,29 +351,17 @@ static void stream_moved_cb(pa_stream *s, void *userdata)
 {
     audio_output_t *aout = userdata;
     aout_sys_t *sys = aout->sys;
-    pa_operation *op;
-    uint32_t idx = pa_stream_get_device_index(s);
-
-    msg_Dbg(aout, "connected to sink %"PRIu32": %s", idx,
-                  pa_stream_get_device_name(s));
-    op = pa_context_get_sink_info_by_index(sys->context, idx,
-                                           sink_info_cb, aout);
-    if (likely(op != NULL))
-        pa_operation_unref(op);
+    const char *name = pa_stream_get_device_name(s);
+    struct sink *sink = sink_find(sys, pa_stream_get_device_index(s));
 
-    /* Update the variable if someone else moved our stream */
-    var_Change(aout, "audio-device", VLC_VAR_SETVALUE,
-               &(vlc_value_t){ .i_int = idx }, NULL);
+    msg_Dbg(aout, "connected to sink %s", name);
+    aout_DeviceReport(aout, name);
 
-    /* Sink unknown as yet, create stub choice for it */
-    if (var_GetInteger(aout, "audio-device") != idx)
-    {
-        var_Change(aout, "audio-device", VLC_VAR_ADDCHOICE,
-                   &(vlc_value_t){ .i_int = idx },
-                   &(vlc_value_t){ .psz_string = (char *)"?" });
-        var_Change(aout, "audio-device", VLC_VAR_SETVALUE,
-                   &(vlc_value_t){ .i_int = idx }, NULL);
-    }
+    sys->base_volume = likely(sink != NULL) ? sink->base_volume
+                                            : PA_VOLUME_INVALID;
+    msg_Dbg(aout, "base volume: %"PRIu32, sys->base_volume);
+    if (pa_cvolume_valid(&sys->cvolume))
+        VolumeReport(aout);
 }
 
 static void stream_overflow_cb(pa_stream *s, void *userdata)
@@ -415,13 +427,60 @@ static void sink_input_info_cb(pa_context *ctx, const pa_sink_input_info *i,
     (void) ctx;
 
     sys->cvolume = i->volume; /* cache volume for balance preservation */
-
-    pa_volume_t volume = pa_cvolume_max(&i->volume);
-    volume = pa_sw_volume_divide(volume, sys->base_volume);
-    aout_VolumeReport(aout, (float)volume / PA_VOLUME_NORM);
+    if (PA_VOLUME_IS_VALID(sys->base_volume))
+        VolumeReport(aout);
     aout_MuteReport(aout, i->mute);
 }
 
+static void sink_input_event(pa_context *ctx,
+                             pa_subscription_event_type_t type,
+                             uint32_t idx, audio_output_t *aout)
+{
+    pa_operation *op;
+
+    /* Gee... PA will not provide the infos directly in the event. */
+    switch (type)
+    {
+        case PA_SUBSCRIPTION_EVENT_REMOVE:
+            msg_Err(aout, "sink input killed!");
+            break;
+
+        default:
+            op = pa_context_get_sink_input_info(ctx, idx, sink_input_info_cb,
+                                                aout);
+            if (likely(op != NULL))
+                pa_operation_unref(op);
+            break;
+    }
+}
+
+
+/*** Context ***/
+static void context_cb(pa_context *ctx, pa_subscription_event_type_t type,
+                       uint32_t idx, void *userdata)
+{
+    audio_output_t *aout = userdata;
+    aout_sys_t *sys = aout->sys;
+    unsigned facility = type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
+
+    type &= PA_SUBSCRIPTION_EVENT_TYPE_MASK;
+    switch (facility)
+    {
+        case PA_SUBSCRIPTION_EVENT_SINK:
+            sink_event(ctx, type, idx, userdata);
+            break;
+
+        case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
+            /* only interested in our sink input */
+            if (sys->stream != NULL && idx == pa_stream_get_index(sys->stream))
+                sink_input_event(ctx, type, idx, userdata);
+            break;
+
+        default: /* unsubscribed facility?! */
+            assert(0);
+    }
+}
+
 
 /*** VLC audio output callbacks ***/
 
@@ -473,8 +532,6 @@ static void Play(audio_output_t *aout, block_t *block)
     aout_sys_t *sys = aout->sys;
     pa_stream *s = sys->stream;
 
-    assert (sys->paused == VLC_TS_INVALID);
-
     const void *ptr = data_convert(&block);
     if (unlikely(ptr == NULL))
         return;
@@ -521,21 +578,16 @@ static void Pause(audio_output_t *aout, bool paused, mtime_t date)
     pa_threaded_mainloop_lock(sys->mainloop);
 
     if (paused) {
-        sys->paused = date;
+        pa_stream_set_latency_update_callback(s, NULL, NULL);
         stream_stop(s, aout);
     } else {
-        assert (sys->paused != VLC_TS_INVALID);
-        date -= sys->paused;
-        msg_Dbg(aout, "resuming after %"PRId64" us", date);
-        sys->paused = VLC_TS_INVALID;
-
-        if (sys->first_pts != VLC_TS_INVALID) {
-            sys->first_pts += date;
-            stream_start(s, aout);
-        }
+        pa_stream_set_latency_update_callback(s, stream_latency_cb, aout);
+        if (likely(sys->first_pts != VLC_TS_INVALID))
+            stream_start_now(s, aout);
     }
 
     pa_threaded_mainloop_unlock(sys->mainloop);
+    (void) date;
 }
 
 /**
@@ -562,11 +614,10 @@ static void Flush(audio_output_t *aout, bool wait)
 static int VolumeSet(audio_output_t *aout, float vol)
 {
     aout_sys_t *sys = aout->sys;
-    if (sys->stream == NULL)
-    {
-        msg_Err (aout, "cannot change volume while not playing");
-        return -1;
-    }
+    pa_stream *s = sys->stream;
+    pa_operation *op;
+    int ret = -1;
+    pa_volume_t base_volume;
 
     /* VLC provides the software volume so convert directly to PulseAudio
      * software volume, pa_volume_t. This is not a linear amplification factor
@@ -574,7 +625,46 @@ static int VolumeSet(audio_output_t *aout, float vol)
     vol *= PA_VOLUME_NORM;
     if (unlikely(vol >= PA_VOLUME_MAX))
         vol = PA_VOLUME_MAX;
-    pa_volume_t volume = pa_sw_volume_multiply(lround(vol), sys->base_volume);
+
+    pa_threaded_mainloop_lock(sys->mainloop);
+
+    base_volume = sys->base_volume;
+    if (!PA_VOLUME_IS_VALID(base_volume))
+    {   /* Base volume is unknown, typically because sink is unknown.
+         * Try to guess the base volume. */
+        const struct sink *sink = sys->sinks;
+        if (unlikely(sink == NULL))
+        {
+            msg_Err(aout, "cannot change volume without sink");
+            goto out;
+        }
+
+        base_volume = sink->base_volume;
+        while ((sink = sink->next) != NULL)
+            if (sink->base_volume != base_volume)
+            {
+                msg_Err(aout, "cannot change volume without base");
+                goto out;
+            }
+    }
+
+    pa_volume_t volume = pa_sw_volume_multiply(lroundf(vol), base_volume);
+
+    if (s == NULL)
+    {
+        sys->volume_force = volume;
+        aout_VolumeReport(aout, vol / (float)PA_VOLUME_NORM);
+        ret = 0;
+        goto out;
+    }
+
+    if (!pa_cvolume_valid(&sys->cvolume))
+    {
+        const pa_sample_spec *ss = pa_stream_get_sample_spec(s);
+
+        msg_Warn(aout, "balance clobbered by volume change");
+        pa_cvolume_set(&sys->cvolume, ss->channels, PA_VOLUME_NORM);
+    }
 
     /* Preserve the balance (VLC does not support it). */
     pa_cvolume cvolume = sys->cvolume;
@@ -582,25 +672,29 @@ static int VolumeSet(audio_output_t *aout, float vol)
     pa_sw_cvolume_multiply_scalar(&cvolume, &cvolume, volume);
     assert(pa_cvolume_valid(&cvolume));
 
-    pa_operation *op;
-    uint32_t idx = pa_stream_get_index(sys->stream);
-    pa_threaded_mainloop_lock(sys->mainloop);
-    op = pa_context_set_sink_input_volume(sys->context, idx, &cvolume,
-                                          NULL, NULL);
+    op = pa_context_set_sink_input_volume(sys->context, pa_stream_get_index(s),
+                                          &cvolume, NULL, NULL);
     if (likely(op != NULL))
+    {
         pa_operation_unref(op);
+        ret = 0;
+    }
+out:
     pa_threaded_mainloop_unlock(sys->mainloop);
-
-    return 0;
+    return ret;
 }
 
 static int MuteSet(audio_output_t *aout, bool mute)
 {
     aout_sys_t *sys = aout->sys;
+
     if (sys->stream == NULL)
     {
-        msg_Err (aout, "cannot change volume while not playing");
-        return -1;
+        sys->flags_force &= ~(PA_STREAM_START_MUTED|PA_STREAM_START_UNMUTED);
+        sys->flags_force |=
+            mute ? PA_STREAM_START_MUTED : PA_STREAM_START_UNMUTED;
+        aout_MuteReport(aout, mute);
+        return 0;
     }
 
     pa_operation *op;
@@ -614,29 +708,36 @@ static int MuteSet(audio_output_t *aout, bool mute)
     return 0;
 }
 
-static int StreamMove(vlc_object_t *obj, const char *varname, vlc_value_t old,
-                      vlc_value_t val, void *userdata)
+static int StreamMove(audio_output_t *aout, const char *name)
 {
-    audio_output_t *aout = (audio_output_t *)obj;
     aout_sys_t *sys = aout->sys;
-    pa_stream *s = userdata;
-    pa_operation *op;
-    uint32_t idx = pa_stream_get_index(s);
-    uint32_t sink_idx = val.i_int;
 
-    (void) varname; (void) old;
+    if (sys->stream == NULL)
+    {
+        struct sink *sink = sink_find_by_name(sys, name);
+
+        sys->base_volume = likely(sink != NULL) ? sink->base_volume
+                                                : PA_VOLUME_INVALID;
+        msg_Dbg(aout, "will connect to sink %s", name);
+        free(sys->sink_force);
+        sys->sink_force = strdup(name);
+        return 0;
+    }
+
+    pa_operation *op;
+    uint32_t idx = pa_stream_get_index(sys->stream);
 
     pa_threaded_mainloop_lock(sys->mainloop);
-    op = pa_context_move_sink_input_by_index(sys->context, idx, sink_idx,
-                                             NULL, NULL);
+    op = pa_context_move_sink_input_by_name(sys->context, idx, name,
+                                            NULL, NULL);
     if (likely(op != NULL)) {
         pa_operation_unref(op);
-        msg_Dbg(aout, "moving to sink %"PRIu32, sink_idx);
+        msg_Dbg(aout, "moving to sink %s", name);
     } else
-        vlc_pa_error(obj, "cannot move sink", sys->context);
+        vlc_pa_error(aout, "cannot move sink input", sys->context);
     pa_threaded_mainloop_unlock(sys->mainloop);
 
-    return (op != NULL) ? VLC_SUCCESS : VLC_EGENERIC;
+    return (op != NULL) ? 0 : -1;
 }
 
 static void Stop(audio_output_t *);
@@ -650,46 +751,24 @@ static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
 
     /* Sample format specification */
     struct pa_sample_spec ss;
-#if PA_CHECK_VERSION(1,0,0)
     pa_encoding_t encoding = PA_ENCODING_INVALID;
-#endif
 
     switch (fmt->i_format)
     {
-        case VLC_CODEC_F64B:
-            fmt->i_format = VLC_CODEC_F32B;
-        case VLC_CODEC_F32B:
-            ss.format = PA_SAMPLE_FLOAT32BE;
-            break;
-        case VLC_CODEC_F64L:
-            fmt->i_format = VLC_CODEC_F32L;
-        case VLC_CODEC_F32L:
-            ss.format = PA_SAMPLE_FLOAT32LE;
-            break;
-        case VLC_CODEC_S32B:
-            ss.format = PA_SAMPLE_S32BE;
-            break;
-        case VLC_CODEC_S32L:
-            ss.format = PA_SAMPLE_S32LE;
+        case VLC_CODEC_FL64:
+            fmt->i_format = VLC_CODEC_FL32;
+        case VLC_CODEC_FL32:
+            ss.format = PA_SAMPLE_FLOAT32NE;
             break;
-        case VLC_CODEC_S24B:
-            ss.format = PA_SAMPLE_S24BE;
+        case VLC_CODEC_S32N:
+            ss.format = PA_SAMPLE_S32NE;
             break;
-        case VLC_CODEC_S24L:
-            ss.format = PA_SAMPLE_S24LE;
+        case VLC_CODEC_S16N:
+            ss.format = PA_SAMPLE_S16NE;
             break;
-        case VLC_CODEC_S16B:
-            ss.format = PA_SAMPLE_S16BE;
-            break;
-        case VLC_CODEC_S16L:
-            ss.format = PA_SAMPLE_S16LE;
-            break;
-        case VLC_CODEC_S8:
-            fmt->i_format = VLC_CODEC_U8;
         case VLC_CODEC_U8:
             ss.format = PA_SAMPLE_U8;
             break;
-#if PA_CHECK_VERSION(1,0,0)
         case VLC_CODEC_A52:
             fmt->i_format = VLC_CODEC_SPDIFL;
             encoding = PA_ENCODING_AC3_IEC61937;
@@ -710,7 +789,6 @@ static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
             encoding = PA_ENCODING_DTS_IEC61937;
             ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
             break;
-#endif
         default:
             if (HAVE_FPU)
             {
@@ -774,11 +852,12 @@ static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
     }
 
     /* Stream parameters */
-    const pa_stream_flags_t flags = PA_STREAM_START_CORKED
-                                  | PA_STREAM_INTERPOLATE_TIMING
-                                  | PA_STREAM_NOT_MONOTONIC
-                                  | PA_STREAM_AUTO_TIMING_UPDATE
-                                  | PA_STREAM_FIX_RATE;
+    pa_stream_flags_t flags = sys->flags_force
+                            | PA_STREAM_START_CORKED
+                            | PA_STREAM_INTERPOLATE_TIMING
+                            | PA_STREAM_NOT_MONOTONIC
+                            | PA_STREAM_AUTO_TIMING_UPDATE
+                            | PA_STREAM_FIX_RATE;
 
     struct pa_buffer_attr attr;
     attr.maxlength = -1;
@@ -790,16 +869,17 @@ static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
     attr.minreq = pa_usec_to_bytes(AOUT_MIN_PREPARE_TIME, &ss);
     attr.fragsize = 0; /* not used for output */
 
-    sys->stream = NULL;
+    pa_cvolume *cvolume = NULL, cvolumebuf;
+    if (PA_VOLUME_IS_VALID(sys->volume_force))
+    {
+        cvolume = &cvolumebuf;
+        pa_cvolume_set(cvolume, ss.channels, sys->volume_force);
+    }
+
     sys->trigger = NULL;
+    pa_cvolume_init(&sys->cvolume);
     sys->first_pts = VLC_TS_INVALID;
-    sys->paused = VLC_TS_INVALID;
 
-    /* Channel volume */
-    sys->base_volume = PA_VOLUME_NORM;
-    pa_cvolume_set(&sys->cvolume, ss.channels, PA_VOLUME_NORM);
-
-#if PA_CHECK_VERSION(1,0,0)
     pa_format_info *formatv[2];
     unsigned formatc = 0;
 
@@ -811,6 +891,12 @@ static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
         pa_format_info_set_channels(formatv[formatc], ss.channels);
         pa_format_info_set_channel_map(formatv[formatc], &map);
         formatc++;
+
+        /* FIX flags are only permitted for PCM, and there is no way to pass
+         * different flags for different formats... */
+        flags &= ~(PA_STREAM_FIX_FORMAT
+                 | PA_STREAM_FIX_RATE
+                 | PA_STREAM_FIX_CHANNELS);
     }
 
     /* Fallback to PCM */
@@ -823,29 +909,26 @@ static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
     formatc++;
 
     /* Create a playback stream */
-    pa_stream *s;
     pa_proplist *props = pa_proplist_new();
     if (likely(props != NULL))
         /* TODO: set other stream properties */
         pa_proplist_sets (props, PA_PROP_MEDIA_ROLE, "video");
 
     pa_threaded_mainloop_lock(sys->mainloop);
-    s = pa_stream_new_extended(sys->context, "audio stream", formatv, formatc,
-                               props);
+    pa_stream *s = pa_stream_new_extended(sys->context, "audio stream",
+                                          formatv, formatc, props);
+
     if (likely(props != NULL))
         pa_proplist_free(props);
-
     for (unsigned i = 0; i < formatc; i++)
         pa_format_info_free(formatv[i]);
-#else
-    pa_threaded_mainloop_lock(sys->mainloop);
-    pa_stream *s = pa_stream_new(sys->context, "audio stream", &ss, &map);
-#endif
+
     if (s == NULL) {
         pa_threaded_mainloop_unlock(sys->mainloop);
         vlc_pa_error(aout, "stream creation failure", sys->context);
         return VLC_EGENERIC;
     }
+    assert(sys->stream == NULL);
     sys->stream = s;
     pa_stream_set_state_callback(s, stream_state_cb, sys->mainloop);
     pa_stream_set_buffer_attr_callback(s, stream_buffer_attr_cb, aout);
@@ -857,14 +940,18 @@ static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
     pa_stream_set_suspended_callback(s, stream_suspended_cb, aout);
     pa_stream_set_underflow_callback(s, stream_underflow_cb, aout);
 
-    if (pa_stream_connect_playback(s, NULL, &attr, flags, NULL, NULL) < 0
+    if (pa_stream_connect_playback(s, sys->sink_force, &attr, flags,
+                                   cvolume, NULL) < 0
      || stream_wait(s, sys->mainloop)) {
         vlc_pa_error(aout, "stream connection failure", sys->context);
         goto fail;
     }
+    sys->volume_force = PA_VOLUME_INVALID;
+    sys->flags_force = PA_STREAM_NOFLAGS;
+    free(sys->sink_force);
+    sys->sink_force = NULL;
 
     const struct pa_sample_spec *spec = pa_stream_get_sample_spec(s);
-#if PA_CHECK_VERSION(1,0,0)
     if (encoding != PA_ENCODING_INVALID) {
         const pa_format_info *info = pa_stream_get_format_info(s);
 
@@ -877,20 +964,17 @@ static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
             spec = NULL;
         }
     }
-#endif
     if (spec != NULL)
         fmt->i_rate = spec->rate;
 
     stream_buffer_attr_cb(s, aout);
     stream_moved_cb(s, aout);
     pa_threaded_mainloop_unlock(sys->mainloop);
-    var_AddCallback (aout, "audio-device", StreamMove, s);
 
     return VLC_SUCCESS;
 
 fail:
     pa_threaded_mainloop_unlock(sys->mainloop);
-    var_AddCallback (aout, "audio-device", StreamMove, s);
     Stop(aout);
     return VLC_EGENERIC;
 }
@@ -903,9 +987,6 @@ static void Stop(audio_output_t *aout)
     aout_sys_t *sys = aout->sys;
     pa_stream *s = sys->stream;
 
-    /* The callback takes mainloop lock, so it CANNOT be held here! */
-    var_DelCallback (aout, "audio-device", StreamMove, s);
-
     pa_threaded_mainloop_lock(sys->mainloop);
     if (unlikely(sys->trigger != NULL))
         vlc_pa_rttime_free(sys->mainloop, sys->trigger);
@@ -924,6 +1005,7 @@ static void Stop(audio_output_t *aout)
 
     pa_stream_unref(s);
     sys->stream = NULL;
+    sys->base_volume = PA_VOLUME_INVALID;
     pa_threaded_mainloop_unlock(sys->mainloop);
 }
 
@@ -933,10 +1015,6 @@ static int Open(vlc_object_t *obj)
     aout_sys_t *sys = malloc(sizeof (*sys));
     pa_operation *op;
 
-#if !PA_CHECK_VERSION(0,9,22)
-    if (!vlc_xlib_init(obj))
-        return VLC_EGENERIC;
-#endif
     if (unlikely(sys == NULL))
         return VLC_ENOMEM;
 
@@ -949,6 +1027,11 @@ static int Open(vlc_object_t *obj)
     }
     sys->stream = NULL;
     sys->context = ctx;
+    sys->base_volume = PA_VOLUME_INVALID;
+    sys->volume_force = PA_VOLUME_INVALID;
+    sys->flags_force = PA_STREAM_NOFLAGS;
+    sys->sink_force = NULL;
+    sys->sinks = NULL;
 
     aout->sys = sys;
     aout->start = Start;
@@ -959,17 +1042,17 @@ static int Open(vlc_object_t *obj)
     aout->flush = Flush;
     aout->volume_set = VolumeSet;
     aout->mute_set = MuteSet;
-
-    /* Devices (sinks) */
-    var_Create(aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE);
-    var_Change(aout, "audio-device", VLC_VAR_SETTEXT,
-               &(vlc_value_t){ .psz_string = (char *)_("Audio device") },
-               NULL);
+    aout->device_select = StreamMove;
 
     pa_threaded_mainloop_lock(sys->mainloop);
-    op = pa_context_get_sink_info_list(sys->context, sink_list_cb, aout);
-    if (op != NULL)
+    /* Sinks (output devices) list */
+    op = pa_context_get_sink_info_list(sys->context, sink_add_cb, aout);
+    if (likely(op != NULL))
+    {
+        while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
+            pa_threaded_mainloop_wait(sys->mainloop);
         pa_operation_unref(op);
+    }
 
     /* Context events */
     const pa_subscription_mask_t mask = PA_SUBSCRIPTION_MASK_SINK
@@ -994,6 +1077,11 @@ static void Close(vlc_object_t *obj)
     pa_threaded_mainloop_unlock(sys->mainloop);
     vlc_pa_disconnect(obj, ctx, sys->mainloop);
 
-    var_Destroy (aout, "audio-device");
+    for (struct sink *sink = sys->sinks, *next; sink != NULL; sink = next)
+    {
+        next = sink->next;
+        free(sink);
+    }
+    free(sys->sink_force);
     free(sys);
 }