]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/pulse.c
opensles: remove obsolete code
[vlc] / modules / audio_output / pulse.c
index f57939847de7c207faa6ccb70fc8c0f9cf998bab..5176151045d9850d70a152c90a8a39600b13add8 100644 (file)
@@ -1,24 +1,24 @@
 /*****************************************************************************
  * pulse.c : Pulseaudio output plugin for vlc
  *****************************************************************************
- * Copyright (C) 2008 the VideoLAN team
+ * Copyright (C) 2008 VLC authors and VideoLAN
  * Copyright (C) 2009-2011 RĂ©mi Denis-Courmont
  *
  * Authors: Martin Hamrle <hamrle @ post . cz>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 #ifdef HAVE_CONFIG_H
@@ -50,14 +50,6 @@ vlc_module_begin ()
     set_callbacks( Open, Close )
 vlc_module_end ()
 
-/* TODO:
- * - pause input on policy event
- * - resample to compensate for long term drift
- * - select music or video stream property correctly (?)
- * - set further appropriate stream properties
- * - update output devices list dynamically
- */
-
 /* NOTE:
  * Be careful what you do when the PulseAudio mainloop is held, which is to say
  * within PulseAudio callbacks, or after pa_threaded_mainloop_lock().
@@ -72,10 +64,8 @@ struct aout_sys_t
     pa_time_event *trigger; /**< Deferred stream trigger */
     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 */
-    mtime_t pts; /**< Play time of buffer write offset */
-    mtime_t desync; /**< Measured desynchronization */
-    unsigned rate; /**< Current stream sample rate */
 };
 
 static void sink_list_cb(pa_context *, const pa_sink_info *, int, void *);
@@ -110,7 +100,7 @@ static void context_cb(pa_context *ctx, pa_subscription_event_type_t type,
         break;
 
       case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
-        if (idx != pa_stream_get_index(sys->stream))
+        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. */
@@ -140,6 +130,7 @@ static void sink_list_cb(pa_context *c, 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)
@@ -156,8 +147,11 @@ static void sink_list_cb(pa_context *c, const pa_sink_info *i, int eol,
     /* 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. */
-    val.i_int = pa_stream_get_device_index(aout->sys->stream);
-    var_Change(aout, "audio-device", VLC_VAR_SETVALUE, &val, NULL);
+    if (sys->stream != NULL)
+    {
+        val.i_int = pa_stream_get_device_index(sys->stream);
+        var_Change(aout, "audio-device", VLC_VAR_SETVALUE, &val, NULL);
+    }
 }
 
 static void sink_info_cb(pa_context *c, const pa_sink_info *i, int eol,
@@ -184,29 +178,12 @@ static void sink_info_cb(pa_context *c, const pa_sink_info *i, int eol,
 
 
 /*** Latency management and lip synchronization ***/
-static void stream_reset_sync(pa_stream *s, audio_output_t *aout)
-{
-    aout_sys_t *sys = aout->sys;
-    const unsigned rate = aout->format.i_rate;
-
-    sys->pts = VLC_TS_INVALID;
-    sys->desync = 0;
-    pa_operation *op = pa_stream_update_sample_rate(s, rate, NULL, NULL);
-    if (unlikely(op == NULL))
-        return;
-    pa_operation_unref(op);
-    sys->rate = rate;
-}
-
-static void stream_start(pa_stream *s, audio_output_t *aout)
+static void stream_start_now(pa_stream *s, audio_output_t *aout)
 {
     aout_sys_t *sys = aout->sys;
     pa_operation *op;
 
-    if (sys->trigger != NULL) {
-        vlc_pa_rttime_free(sys->mainloop, sys->trigger);
-        sys->trigger = NULL;
-    }
+    assert (sys->trigger == NULL);
 
     op = pa_stream_cork(s, 0, NULL, NULL);
     if (op != NULL)
@@ -237,9 +214,12 @@ static void stream_trigger_cb(pa_mainloop_api *api, pa_time_event *e,
     audio_output_t *aout = userdata;
     aout_sys_t *sys = aout->sys;
 
-    msg_Dbg(aout, "starting deferred");
     assert (sys->trigger == e);
-    stream_start(sys->stream, aout);
+
+    msg_Dbg(aout, "starting deferred");
+    vlc_pa_rttime_free(sys->mainloop, sys->trigger);
+    sys->trigger = NULL;
+    stream_start_now(sys->stream, aout);
     (void) api; (void) e; (void) tv;
 }
 
@@ -249,28 +229,33 @@ static void stream_trigger_cb(pa_mainloop_api *api, pa_time_event *e,
  * in order to minimize desync and resampling during early playback.
  * @note PulseAudio lock required.
  */
-static void stream_resync(audio_output_t *aout, pa_stream *s)
+static void stream_start(pa_stream *s, audio_output_t *aout)
 {
     aout_sys_t *sys = aout->sys;
     mtime_t delta;
 
-    assert (sys->pts != VLC_TS_INVALID);
+    assert (sys->first_pts != VLC_TS_INVALID);
+
+    if (sys->trigger != NULL) {
+        vlc_pa_rttime_free(sys->mainloop, sys->trigger);
+        sys->trigger = NULL;
+    }
 
     delta = vlc_pa_get_latency(aout, sys->context, s);
-    if (unlikely(delta == VLC_TS_INVALID))
+    if (unlikely(delta == VLC_TS_INVALID)) {
+        msg_Dbg(aout, "cannot synchronize start");
         delta = 0; /* screwed */
+    }
 
-    delta = (sys->pts - mdate()) - delta;
+    delta = (sys->first_pts - mdate()) - delta;
     if (delta > 0) {
-        if (sys->trigger == NULL) {
-            msg_Dbg(aout, "deferring start (%"PRId64" us)", delta);
-            delta += pa_rtclock_now();
-            sys->trigger = pa_context_rttime_new(sys->context, delta,
-                                                 stream_trigger_cb, aout);
-        }
+        msg_Dbg(aout, "deferring start (%"PRId64" us)", delta);
+        delta += pa_rtclock_now();
+        sys->trigger = pa_context_rttime_new(sys->context, delta,
+                                             stream_trigger_cb, aout);
     } else {
         msg_Warn(aout, "starting late (%"PRId64" us)", delta);
-        stream_start(s, aout);
+        stream_start_now(s, aout);
     }
 }
 
@@ -278,77 +263,13 @@ static void stream_latency_cb(pa_stream *s, void *userdata)
 {
     audio_output_t *aout = userdata;
     aout_sys_t *sys = aout->sys;
-    mtime_t delta, change;
-
-    if (pa_stream_is_corked(s))
-        return;
-    if (sys->pts == VLC_TS_INVALID)
-    {
-        msg_Dbg(aout, "missing latency from input");
-        return;
-    }
 
-    /* Compute lip desynchronization */
-    delta = vlc_pa_get_latency(aout, sys->context, s);
-    if (delta == VLC_TS_INVALID)
-        return;
-
-    delta = (sys->pts - mdate()) - delta;
-    change = delta - sys->desync;
-    sys->desync = delta;
-    //msg_Dbg(aout, "desync: %+"PRId64" us (variation: %+"PRId64" us)",
-    //        delta, change);
-
-    const unsigned inrate = aout->format.i_rate;
-    unsigned outrate = sys->rate;
-    bool sync = false;
-
-    if (delta < -AOUT_MAX_PTS_DELAY)
-        msg_Warn(aout, "too late by %"PRId64" us", -delta);
-    else if (delta > +AOUT_MAX_PTS_ADVANCE)
-        msg_Warn(aout, "too early by %"PRId64" us", delta);
-    else if (outrate  == inrate)
-        return; /* In sync, do not add unnecessary disturbance! */
-    else
-        sync = true;
-
-    /* Compute playback sample rate */
-    /* This is empirical (especially the shift values).
-     * Feel free to define something smarter. */
-    int adj = sync ? (outrate - inrate)
-                   : outrate * ((delta >> 4) + change) / (CLOCK_FREQ << 2);
-    /* This avoids too quick rate variation. It sounds really bad and
-     * causes unstability (e.g. oscillation around the correct rate). */
-    int limit = inrate >> 10;
-    /* However, to improve stability and try to converge, closing to the
-     * nominal rate is favored over drifting from it. */
-    if ((adj > 0) == (sys->rate > inrate))
-        limit *= 2;
-    if (adj > +limit)
-        adj = +limit;
-    if (adj < -limit)
-        adj = -limit;
-    outrate -= adj;
-
-    /* This keeps the effective rate within specified range
-     * (+/-AOUT_MAX_RESAMPLING% - see <vlc_aout.h>) of the nominal rate. */
-    limit = inrate * AOUT_MAX_RESAMPLING / 100;
-    if (outrate > inrate + limit)
-        outrate = inrate + limit;
-    if (outrate < inrate - limit)
-        outrate = inrate - limit;
-
-    /* Apply adjusted sample rate */
-    if (outrate == sys->rate)
-        return;
-    pa_operation *op = pa_stream_update_sample_rate(s, outrate, NULL, NULL);
-    if (unlikely(op == NULL)) {
-        vlc_pa_error(aout, "cannot change sample rate", sys->context);
-        return;
-    }
-    pa_operation_unref(op);
-    msg_Dbg(aout, "changed sample rate to %u Hz",outrate);
-    sys->rate = outrate;
+    if (sys->paused != VLC_TS_INVALID)
+        return; /* nothing to do 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)
+        stream_start(s, aout);
 }
 
 
@@ -367,11 +288,27 @@ static void stream_state_cb(pa_stream *s, void *userdata)
     }
 }
 
+static void stream_buffer_attr_cb(pa_stream *s, void *userdata)
+{
+    audio_output_t *aout = userdata;
+    const pa_buffer_attr *pba = pa_stream_get_buffer_attr(s);
+
+    msg_Dbg(aout, "changed buffer metrics: maxlength=%u, tlength=%u, "
+            "prebuf=%u, minreq=%u",
+            pba->maxlength, pba->tlength, pba->prebuf, pba->minreq);
+}
+
 static void stream_event_cb(pa_stream *s, const char *name, pa_proplist *pl,
                             void *userdata)
 {
     audio_output_t *aout = userdata;
 
+    if (!strcmp(name, PA_STREAM_EVENT_REQUEST_CORK))
+        aout_PolicyReport(aout, true);
+    else
+    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)) {
@@ -419,13 +356,15 @@ static void stream_moved_cb(pa_stream *s, void *userdata)
 static void stream_overflow_cb(pa_stream *s, void *userdata)
 {
     audio_output_t *aout = userdata;
+    aout_sys_t *sys = aout->sys;
     pa_operation *op;
 
     msg_Err(aout, "overflow, flushing");
     op = pa_stream_flush(s, NULL, NULL);
-    if (likely(op != NULL))
-        pa_operation_unref(op);
-    stream_reset_sync(s, aout);
+    if (unlikely(op == NULL))
+        return;
+    pa_operation_unref(op);
+    sys->first_pts = VLC_TS_INVALID;
 }
 
 static void stream_started_cb(pa_stream *s, void *userdata)
@@ -441,16 +380,15 @@ static void stream_suspended_cb(pa_stream *s, void *userdata)
     audio_output_t *aout = userdata;
 
     msg_Dbg(aout, "suspended");
-    stream_reset_sync(s, aout);
+    (void) s;
 }
 
 static void stream_underflow_cb(pa_stream *s, void *userdata)
 {
     audio_output_t *aout = userdata;
 
-    msg_Warn(aout, "underflow");
-    stream_stop(s, aout);
-    stream_reset_sync(s, aout);
+    msg_Dbg(aout, "underflow");
+    (void) s;
 }
 
 static int stream_wait(pa_stream *stream, pa_threaded_mainloop *mainloop)
@@ -488,6 +426,22 @@ static void sink_input_info_cb(pa_context *ctx, const pa_sink_input_info *i,
 
 /*** VLC audio output callbacks ***/
 
+static int TimeGet(audio_output_t *aout, mtime_t *restrict delay)
+{
+    aout_sys_t *sys = aout->sys;
+    pa_stream *s = sys->stream;
+
+    if (pa_stream_is_corked(s) > 0)
+        return -1; /* latency is irrelevant if corked */
+
+    mtime_t delta = vlc_pa_get_latency(aout, sys->context, s);
+    if (delta == VLC_TS_INVALID)
+        return -1;
+
+    *delay = delta;
+    return 0;
+}
+
 /* Memory free callback. The block_t address is in front of the data. */
 static void data_free(void *data)
 {
@@ -513,9 +467,9 @@ static void *data_convert(block_t **pp)
 }
 
 /**
- * Queue one audio frame to the playabck stream
+ * Queue one audio frame to the playback stream
  */
-static void Play(audio_output_t *aout, block_t *block, mtime_t *restrict drift)
+static void Play(audio_output_t *aout, block_t *block)
 {
     aout_sys_t *sys = aout->sys;
     pa_stream *s = sys->stream;
@@ -527,7 +481,6 @@ static void Play(audio_output_t *aout, block_t *block, mtime_t *restrict drift)
         return;
 
     size_t len = block->i_buffer;
-    mtime_t pts = block->i_pts + block->i_length;
 
     /* Note: The core already holds the output FIFO lock at this point.
      * Therefore we must not under any circumstances (try to) acquire the
@@ -536,9 +489,11 @@ static void Play(audio_output_t *aout, block_t *block, mtime_t *restrict drift)
      * will take place, and sooner or later a deadlock. */
     pa_threaded_mainloop_lock(sys->mainloop);
 
-    sys->pts = pts;
+    if (sys->first_pts == VLC_TS_INVALID)
+        sys->first_pts = block->i_pts;
+
     if (pa_stream_is_corked(s) > 0)
-        stream_resync(aout, s);
+        stream_start(s, aout);
 
 #if 0 /* Fault injector to test underrun recovery */
     static volatile unsigned u = 0;
@@ -554,7 +509,6 @@ static void Play(audio_output_t *aout, block_t *block, mtime_t *restrict drift)
     }
 
     pa_threaded_mainloop_unlock(sys->mainloop);
-    (void) drift;
 }
 
 /**
@@ -575,8 +529,11 @@ static void Pause(audio_output_t *aout, bool paused, mtime_t date)
         date -= sys->paused;
         msg_Dbg(aout, "resuming after %"PRId64" us", date);
         sys->paused = VLC_TS_INVALID;
-        sys->pts += date;
-        stream_resync(aout, s);
+
+        if (sys->first_pts != VLC_TS_INVALID) {
+            sys->first_pts += date;
+            stream_start(s, aout);
+        }
     }
 
     pa_threaded_mainloop_unlock(sys->mainloop);
@@ -606,8 +563,11 @@ static void Flush(audio_output_t *aout, bool wait)
 static int VolumeSet(audio_output_t *aout, float vol)
 {
     aout_sys_t *sys = aout->sys;
-    pa_operation *op;
-    uint32_t idx = pa_stream_get_index(sys->stream);
+    if (sys->stream == NULL)
+    {
+        msg_Err (aout, "cannot change volume while not playing");
+        return -1;
+    }
 
     /* VLC provides the software volume so convert directly to PulseAudio
      * software volume, pa_volume_t. This is not a linear amplification factor
@@ -621,11 +581,13 @@ static int VolumeSet(audio_output_t *aout, float vol)
     pa_cvolume cvolume = sys->cvolume;
     pa_cvolume_scale(&cvolume, PA_VOLUME_NORM);
     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, idx, &cvolume,
+                                          NULL, NULL);
     if (likely(op != NULL))
         pa_operation_unref(op);
     pa_threaded_mainloop_unlock(sys->mainloop);
@@ -636,9 +598,14 @@ static int VolumeSet(audio_output_t *aout, float vol)
 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;
+    }
+
     pa_operation *op;
     uint32_t idx = pa_stream_get_index(sys->stream);
-
     pa_threaded_mainloop_lock(sys->mainloop);
     op = pa_context_set_sink_input_mute(sys->context, idx, mute, NULL, NULL);
     if (likely(op != NULL))
@@ -673,36 +640,30 @@ static int StreamMove(vlc_object_t *obj, const char *varname, vlc_value_t old,
     return (op != NULL) ? VLC_SUCCESS : VLC_EGENERIC;
 }
 
+static void Stop(audio_output_t *);
 
 /**
  * Create a PulseAudio playback stream, a.k.a. a sink input.
  */
-static int Open(vlc_object_t *obj)
+static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
 {
-#if !PA_CHECK_VERSION(0,9,22)
-    if (!vlc_xlib_init(obj))
-        return VLC_EGENERIC;
-#endif
-
-    audio_output_t *aout = (audio_output_t *)obj;
-    pa_operation *op;
+    aout_sys_t *sys = aout->sys;
 
     /* Sample format specification */
     struct pa_sample_spec ss;
-    vlc_fourcc_t format = aout->format.i_format;
 #if PA_CHECK_VERSION(1,0,0)
     pa_encoding_t encoding = PA_ENCODING_INVALID;
 #endif
 
-    switch(format)
+    switch (fmt->i_format)
     {
         case VLC_CODEC_F64B:
-            format = VLC_CODEC_F32B;
+            fmt->i_format = VLC_CODEC_F32B;
         case VLC_CODEC_F32B:
             ss.format = PA_SAMPLE_FLOAT32BE;
             break;
         case VLC_CODEC_F64L:
-            format = VLC_CODEC_F32L;
+            fmt->i_format = VLC_CODEC_F32L;
         case VLC_CODEC_F32L:
             ss.format = PA_SAMPLE_FLOAT32LE;
             break;
@@ -725,28 +686,28 @@ static int Open(vlc_object_t *obj)
             ss.format = PA_SAMPLE_S16LE;
             break;
         case VLC_CODEC_S8:
-            format = VLC_CODEC_U8;
+            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:
-            format = VLC_CODEC_SPDIFL;
+            fmt->i_format = VLC_CODEC_SPDIFL;
             encoding = PA_ENCODING_AC3_IEC61937;
             ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
             break;
         /*case VLC_CODEC_EAC3:
-            format = VLC_CODEC_SPDIFL FIXME;
+            fmt->i_format = VLC_CODEC_SPDIFL FIXME;
             encoding = PA_ENCODING_EAC3_IEC61937;
             ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
             break;
         case VLC_CODEC_MPGA:
-            format = VLC_CODEC_SPDIFL FIXME;
+            fmt->i_format = VLC_CODEC_SPDIFL FIXME;
             encoding = PA_ENCODING_MPEG_IEC61937;
             ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
             break;*/
         case VLC_CODEC_DTS:
-            format = VLC_CODEC_SPDIFL;
+            fmt->i_format = VLC_CODEC_SPDIFL;
             encoding = PA_ENCODING_DTS_IEC61937;
             ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
             break;
@@ -754,19 +715,19 @@ static int Open(vlc_object_t *obj)
         default:
             if (HAVE_FPU)
             {
-                format = VLC_CODEC_FL32;
+                fmt->i_format = VLC_CODEC_FL32;
                 ss.format = PA_SAMPLE_FLOAT32NE;
             }
             else
             {
-                format = VLC_CODEC_S16N;
+                fmt->i_format = VLC_CODEC_S16N;
                 ss.format = PA_SAMPLE_S16NE;
             }
             break;
     }
 
-    ss.rate = aout->format.i_rate;
-    ss.channels = aout_FormatNbChannels(&aout->format);
+    ss.rate = fmt->i_rate;
+    ss.channels = aout_FormatNbChannels(fmt);
     if (!pa_sample_spec_valid(&ss)) {
         msg_Err(aout, "unsupported sample specification");
         return VLC_EGENERIC;
@@ -776,28 +737,28 @@ static int Open(vlc_object_t *obj)
     struct pa_channel_map map;
     map.channels = 0;
 
-    if (aout->format.i_physical_channels & AOUT_CHAN_LEFT)
+    if (fmt->i_physical_channels & AOUT_CHAN_LEFT)
         map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
-    if (aout->format.i_physical_channels & AOUT_CHAN_RIGHT)
+    if (fmt->i_physical_channels & AOUT_CHAN_RIGHT)
         map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
-    if (aout->format.i_physical_channels & AOUT_CHAN_MIDDLELEFT)
+    if (fmt->i_physical_channels & AOUT_CHAN_MIDDLELEFT)
         map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_LEFT;
-    if (aout->format.i_physical_channels & AOUT_CHAN_MIDDLERIGHT)
+    if (fmt->i_physical_channels & AOUT_CHAN_MIDDLERIGHT)
         map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_RIGHT;
-    if (aout->format.i_physical_channels & AOUT_CHAN_REARLEFT)
+    if (fmt->i_physical_channels & AOUT_CHAN_REARLEFT)
         map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_LEFT;
-    if (aout->format.i_physical_channels & AOUT_CHAN_REARRIGHT)
+    if (fmt->i_physical_channels & AOUT_CHAN_REARRIGHT)
         map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_RIGHT;
-    if (aout->format.i_physical_channels & AOUT_CHAN_REARCENTER)
+    if (fmt->i_physical_channels & AOUT_CHAN_REARCENTER)
         map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_CENTER;
-    if (aout->format.i_physical_channels & AOUT_CHAN_CENTER)
+    if (fmt->i_physical_channels & AOUT_CHAN_CENTER)
     {
         if (ss.channels == 1)
             map.map[map.channels++] = PA_CHANNEL_POSITION_MONO;
         else
             map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_CENTER;
     }
-    if (aout->format.i_physical_channels & AOUT_CHAN_LFE)
+    if (fmt->i_physical_channels & AOUT_CHAN_LFE)
         map.map[map.channels++] = PA_CHANNEL_POSITION_LFE;
 
     for (unsigned i = 0; map.channels < ss.channels; i++) {
@@ -815,43 +776,25 @@ static int Open(vlc_object_t *obj)
 
     /* Stream parameters */
     const pa_stream_flags_t flags = PA_STREAM_START_CORKED
-                                  //| PA_STREAM_INTERPOLATE_TIMING
+                                  | PA_STREAM_INTERPOLATE_TIMING
+                                  | PA_STREAM_NOT_MONOTONIC
                                   | PA_STREAM_AUTO_TIMING_UPDATE
-                                  | PA_STREAM_VARIABLE_RATE;
+                                  | PA_STREAM_FIX_RATE;
 
     struct pa_buffer_attr attr;
     attr.maxlength = -1;
-    /* PulseAudio assumes that tlength bytes are available in the buffer. Thus
-     * we need to be conservative and set the minimum value that the VLC
-     * audio decoder thread warrants. Otherwise, PulseAudio buffers will
-     * underrun on hardware with large buffers. VLC keeps at least
-     * AOUT_MIN_PREPARE and at most AOUT_MAX_PREPARE worth of audio buffers.
-     * TODO? tlength could be adaptively increased to reduce wakeups. */
-    attr.tlength = pa_usec_to_bytes(AOUT_MIN_PREPARE_TIME, &ss);
+    /* PulseAudio goes berserk if the target length (tlength) is not
+     * significantly longer than 2 periods (minreq), or when the period length
+     * is unspecified and the target length is short. */
+    attr.tlength = pa_usec_to_bytes(3 * AOUT_MIN_PREPARE_TIME, &ss);
     attr.prebuf = 0; /* trigger manually */
-    attr.minreq = -1;
+    attr.minreq = pa_usec_to_bytes(AOUT_MIN_PREPARE_TIME, &ss);
     attr.fragsize = 0; /* not used for output */
 
-    /* Allocate structures */
-    aout_sys_t *sys = malloc(sizeof(*sys));
-    if (unlikely(sys == NULL))
-        return VLC_ENOMEM;
-
-    pa_context *ctx = vlc_pa_connect(obj, &sys->mainloop);
-    if (ctx == NULL)
-    {
-        free (sys);
-        return VLC_EGENERIC;
-    }
-
-    aout->sys = sys;
     sys->stream = NULL;
-    sys->context = ctx;
     sys->trigger = NULL;
+    sys->first_pts = VLC_TS_INVALID;
     sys->paused = VLC_TS_INVALID;
-    sys->pts = VLC_TS_INVALID;
-    sys->desync = 0;
-    sys->rate = ss.rate;
 
     /* Channel volume */
     sys->base_volume = PA_VOLUME_NORM;
@@ -882,22 +825,31 @@ static int Open(vlc_object_t *obj)
 
     /* 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(ctx, "audio stream", formatv, formatc, NULL);
+    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(ctx, "audio stream", &ss, &map);
+    pa_stream *s = pa_stream_new(sys->context, "audio stream", &ss, &map);
 #endif
     if (s == NULL) {
-        vlc_pa_error(obj, "stream creation failure", ctx);
-        goto fail;
+        pa_threaded_mainloop_unlock(sys->mainloop);
+        vlc_pa_error(aout, "stream creation failure", sys->context);
+        return VLC_EGENERIC;
     }
     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);
     pa_stream_set_event_callback(s, stream_event_cb, aout);
     pa_stream_set_latency_update_callback(s, stream_latency_cb, aout);
     pa_stream_set_moved_callback(s, stream_moved_cb, aout);
@@ -908,10 +860,11 @@ static int Open(vlc_object_t *obj)
 
     if (pa_stream_connect_playback(s, NULL, &attr, flags, NULL, NULL) < 0
      || stream_wait(s, sys->mainloop)) {
-        vlc_pa_error(obj, "stream connection failure", ctx);
+        vlc_pa_error(aout, "stream connection failure", sys->context);
         goto fail;
     }
 
+    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);
@@ -919,88 +872,129 @@ static int Open(vlc_object_t *obj)
         assert (info != NULL);
         if (pa_format_info_is_pcm (info)) {
             msg_Dbg(aout, "digital pass-through not available");
-            format = HAVE_FPU ? VLC_CODEC_FL32 : VLC_CODEC_S16N;
+            fmt->i_format = HAVE_FPU ? VLC_CODEC_FL32 : VLC_CODEC_S16N;
         } else {
             msg_Dbg(aout, "digital pass-through enabled");
-            pa_stream_set_latency_update_callback(s, NULL, NULL);
+            spec = NULL;
         }
     }
 #endif
+    if (spec != NULL)
+        fmt->i_rate = spec->rate;
 
-    const struct pa_buffer_attr *pba = pa_stream_get_buffer_attr(s);
-    msg_Dbg(aout, "using buffer metrics: maxlength=%u, tlength=%u, "
-            "prebuf=%u, minreq=%u",
-            pba->maxlength, pba->tlength, pba->prebuf, pba->minreq);
+    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;
+}
 
+/**
+ * Removes a PulseAudio playback stream
+ */
+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);
+    pa_stream_disconnect(s);
+
+    /* Clear all callbacks */
+    pa_stream_set_state_callback(s, NULL, NULL);
+    pa_stream_set_buffer_attr_callback(s, NULL, NULL);
+    pa_stream_set_event_callback(s, NULL, NULL);
+    pa_stream_set_latency_update_callback(s, NULL, NULL);
+    pa_stream_set_moved_callback(s, NULL, NULL);
+    pa_stream_set_overflow_callback(s, NULL, NULL);
+    pa_stream_set_started_callback(s, NULL, NULL);
+    pa_stream_set_suspended_callback(s, NULL, NULL);
+    pa_stream_set_underflow_callback(s, NULL, NULL);
+
+    pa_stream_unref(s);
+    sys->stream = NULL;
+    pa_threaded_mainloop_unlock(sys->mainloop);
+}
+
+static int Open(vlc_object_t *obj)
+{
+    audio_output_t *aout = (audio_output_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;
+
+    /* Allocate structures */
+    pa_context *ctx = vlc_pa_connect(obj, &sys->mainloop);
+    if (ctx == NULL)
+    {
+        free(sys);
+        return VLC_EGENERIC;
+    }
+    sys->stream = NULL;
+    sys->context = ctx;
+
+    aout->sys = sys;
+    aout->start = Start;
+    aout->stop = Stop;
+    aout->time_get = TimeGet;
+    aout->play = Play;
+    aout->pause = Pause;
+    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);
-    var_AddCallback (aout, "audio-device", StreamMove, s);
-    op = pa_context_get_sink_info_list(ctx, sink_list_cb, aout);
-    /* We may need to wait for completion... once LibVLC supports this */
+
+    pa_threaded_mainloop_lock(sys->mainloop);
+    op = pa_context_get_sink_info_list(sys->context, sink_list_cb, aout);
     if (op != NULL)
         pa_operation_unref(op);
-    stream_moved_cb(s, aout);
 
     /* Context events */
     const pa_subscription_mask_t mask = PA_SUBSCRIPTION_MASK_SINK
                                       | PA_SUBSCRIPTION_MASK_SINK_INPUT;
-    pa_context_set_subscribe_callback(ctx, context_cb, aout);
-    op = pa_context_subscribe(ctx, mask, NULL, NULL);
+    pa_context_set_subscribe_callback(sys->context, context_cb, aout);
+    op = pa_context_subscribe(sys->context, mask, NULL, NULL);
     if (likely(op != NULL))
        pa_operation_unref(op);
     pa_threaded_mainloop_unlock(sys->mainloop);
 
-    aout->format.i_format = format;
-    aout->pf_play = Play;
-    aout->pf_pause = Pause;
-    aout->pf_flush = Flush;
-    aout->volume_set = VolumeSet;
-    aout->mute_set = MuteSet;
     return VLC_SUCCESS;
-
-fail:
-    pa_threaded_mainloop_unlock(sys->mainloop);
-    Close(obj);
-    return VLC_EGENERIC;
 }
 
-/**
- * Removes a PulseAudio playback stream
- */
-static void Close (vlc_object_t *obj)
+static void Close(vlc_object_t *obj)
 {
     audio_output_t *aout = (audio_output_t *)obj;
     aout_sys_t *sys = aout->sys;
     pa_context *ctx = sys->context;
-    pa_stream *s = sys->stream;
-
-    if (s != NULL) {
-        /* The callback takes mainloop lock, so it CANNOT be held here! */
-        var_DelCallback (aout, "audio-device", StreamMove, s);
-        var_Destroy (aout, "audio-device");
-
-        pa_threaded_mainloop_lock(sys->mainloop);
-        if (unlikely(sys->trigger != NULL))
-            vlc_pa_rttime_free(sys->mainloop, sys->trigger);
-        pa_stream_disconnect(s);
-
-        /* Clear all callbacks */
-        pa_stream_set_state_callback(s, NULL, NULL);
-        pa_stream_set_event_callback(s, NULL, NULL);
-        pa_stream_set_latency_update_callback(s, NULL, NULL);
-        pa_stream_set_moved_callback(s, NULL, NULL);
-        pa_stream_set_overflow_callback(s, NULL, NULL);
-        pa_stream_set_started_callback(s, NULL, NULL);
-        pa_stream_set_suspended_callback(s, NULL, NULL);
-        pa_stream_set_underflow_callback(s, NULL, NULL);
-        pa_context_set_subscribe_callback(ctx, NULL, NULL);
-
-        pa_stream_unref(s);
-        pa_threaded_mainloop_unlock(sys->mainloop);
-    }
 
+    pa_threaded_mainloop_lock(sys->mainloop);
+    pa_context_set_subscribe_callback(sys->context, NULL, NULL);
+    pa_threaded_mainloop_unlock(sys->mainloop);
     vlc_pa_disconnect(obj, ctx, sys->mainloop);
+
+    var_Destroy (aout, "audio-device");
     free(sys);
 }