+ msg_Dbg(aout, "base volume: %"PRIu32, sys->base_volume);
+}
+
+
+/*** 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)
+{
+ aout_sys_t *sys = aout->sys;
+ pa_operation *op;
+
+ if (sys->trigger != NULL) {
+ vlc_pa_rttime_free(sys->mainloop, sys->trigger);
+ sys->trigger = NULL;
+ }
+
+ op = pa_stream_cork(s, 0, NULL, NULL);
+ if (op != NULL)
+ pa_operation_unref(op);
+ op = pa_stream_trigger(s, NULL, NULL);
+ if (likely(op != NULL))
+ pa_operation_unref(op);
+}
+
+static void stream_stop(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;
+ }
+
+ op = pa_stream_cork(s, 1, NULL, NULL);
+ if (op != NULL)
+ pa_operation_unref(op);
+}
+
+static void stream_trigger_cb(pa_mainloop_api *api, pa_time_event *e,
+ const struct timeval *tv, void *userdata)
+{
+ 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);
+ (void) api; (void) e; (void) tv;
+}
+
+/**
+ * Starts or resumes the playback stream.
+ * Tries start playing back audio samples at the most accurate time
+ * 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)
+{
+ aout_sys_t *sys = aout->sys;
+ mtime_t delta;
+
+ assert (sys->pts != VLC_TS_INVALID);
+
+ delta = vlc_pa_get_latency(aout, sys->context, s);
+ if (unlikely(delta == VLC_TS_INVALID))
+ delta = 0; /* screwed */
+
+ delta = (sys->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);
+ }
+ } else {
+ msg_Warn(aout, "starting late (%"PRId64" us)", delta);
+ stream_start(s, aout);
+ }
+}
+
+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;