]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/pulse.c
PulseAudio: keep track of available devices (fix #5412)
[vlc] / modules / audio_output / pulse.c
index cbd3e308ea7cf9cb92b0238d69f081f7d9de79b9..cd15f84f57ed53b8540de5fe35013b1e8773d133 100644 (file)
@@ -25,6 +25,7 @@
 # include "config.h"
 #endif
 
+#include <math.h>
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_aout.h>
@@ -67,6 +68,7 @@ struct aout_sys_t
 {
     pa_stream *stream; /**< PulseAudio playback stream object */
     pa_context *context; /**< PulseAudio connection context */
+    pa_time_event *trigger; /**< Deferred stream trigger */
     pa_volume_t base_volume; /**< 0dB reference volume */
     pa_cvolume cvolume; /**< actual sink input volume */
     mtime_t paused; /**< Time when (last) paused */
@@ -75,6 +77,7 @@ struct aout_sys_t
     unsigned rate; /**< Current stream sample rate */
 };
 
+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 *);
 
@@ -88,6 +91,23 @@ static void context_cb(pa_context *ctx, pa_subscription_event_type_t type,
 
     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 (idx != pa_stream_get_index(sys->stream))
             break; /* only interested in our sink input */
@@ -129,6 +149,7 @@ static void sink_list_cb(pa_context *c, const pa_sink_info *i, int eol,
             i->description);
     val.i_int = i->index;
     text.psz_string = (char *)i->description;
+    var_Change(aout, "audio-device", VLC_VAR_DELCHOICE, &val, NULL);
     var_Change(aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text);
 }
 
@@ -163,7 +184,8 @@ static mtime_t vlc_pa_get_latency(audio_output_t *aout,
     int negative;
 
     if (pa_stream_get_latency(s, &latency, &negative)) {
-        vlc_pa_error(aout, "unknown latency", ctx);
+        if (pa_context_errno (ctx) != PA_ERR_NODATA)
+            vlc_pa_error(aout, "unknown latency", ctx);
         return VLC_TS_INVALID;
     }
     return negative ? -latency : +latency;
@@ -183,6 +205,51 @@ static void stream_reset_sync(pa_stream *s, audio_output_t *aout)
     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->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->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
@@ -192,7 +259,6 @@ static void stream_reset_sync(pa_stream *s, audio_output_t *aout)
 static void stream_resync(audio_output_t *aout, pa_stream *s)
 {
     aout_sys_t *sys = aout->sys;
-    pa_operation *op;
     mtime_t delta;
 
     assert (pa_stream_is_corked(s) > 0);
@@ -203,32 +269,17 @@ static void stream_resync(audio_output_t *aout, pa_stream *s)
         delta = 0; /* screwed */
 
     delta = (sys->pts - mdate()) - delta;
-
-    /* TODO: adjust prebuf instead of padding? */
     if (delta > 0) {
-        size_t nb = (delta * sys->rate) / CLOCK_FREQ;
-        size_t size = aout->format.i_bytes_per_frame;
-        float *zeroes = calloc (nb, size);
-
-        msg_Dbg(aout, "starting with %zu zeroes (%"PRId64" us)", nb,
-                delta);
-#if 0 /* Fault injector: add delay */
-        pa_stream_write(s, zeroes, nb * size, NULL, 0, PA_SEEK_RELATIVE);
-        pa_stream_write(s, zeroes, nb * size, NULL, 0, PA_SEEK_RELATIVE);
-#endif
-        if (likely(zeroes != NULL))
-            if (pa_stream_write(s, zeroes, nb * size, free, 0,
-                                PA_SEEK_RELATIVE) < 0)
-                free(zeroes);
-    } else
+        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);
-
-    op = pa_stream_cork(s, 0, NULL, NULL);
-    if (op != NULL)
-        pa_operation_unref(op);
-    op = pa_stream_trigger(s, NULL, NULL);
-    if (op != NULL)
-        pa_operation_unref(op);
+        stream_start(s, aout);
+    }
 }
 
 static void stream_latency_cb(pa_stream *s, void *userdata)
@@ -270,9 +321,10 @@ static void stream_latency_cb(pa_stream *s, void *userdata)
         sync = true;
 
     /* Compute playback sample rate */
-    /* This is empirical. Feel free to define something smarter. */
+    /* This is empirical (especially the shift values).
+     * Feel free to define something smarter. */
     int adj = sync ? (outrate - inrate)
-                   : outrate * (delta + change) / (CLOCK_FREQ << 4);
+                   : 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;
@@ -322,6 +374,26 @@ static void stream_state_cb(pa_stream *s, void *userdata)
     (void) userdata;
 }
 
+static void stream_event_cb(pa_stream *s, const char *name, pa_proplist *pl,
+                            void *userdata)
+{
+    audio_output_t *aout = userdata;
+
+#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_ChannelsRestart (VLC_OBJECT(aout), "audio-device",
+                              dummy, dummy, NULL);
+    } else
+#endif
+        msg_Warn (aout, "unhandled event %s", name);
+    (void) s;
+    (void) pl;
+}
+
 static void stream_moved_cb(pa_stream *s, void *userdata)
 {
     audio_output_t *aout = userdata;
@@ -339,6 +411,16 @@ static void stream_moved_cb(pa_stream *s, void *userdata)
     /* Update the variable if someone else moved our stream */
     var_Change(aout, "audio-device", VLC_VAR_SETVALUE,
                &(vlc_value_t){ .i_int = idx }, NULL);
+
+    /* 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);
+    }
 }
 
 static void stream_overflow_cb(pa_stream *s, void *userdata)
@@ -368,12 +450,9 @@ static void stream_suspended_cb(pa_stream *s, void *userdata)
 static void stream_underflow_cb(pa_stream *s, void *userdata)
 {
     audio_output_t *aout = userdata;
-    pa_operation *op;
 
     msg_Warn(aout, "underflow");
-    op = pa_stream_cork(s, 1, NULL, NULL);
-    if (op != NULL)
-        pa_operation_unref(op);
+    stream_stop(s, aout);
     stream_reset_sync(s, aout);
 }
 
@@ -389,16 +468,6 @@ static int stream_wait(pa_stream *stream)
     return 0;
 }
 
-#ifdef LIBPULSE_GETS_A_CLUE
-static void stream_success_cb(pa_stream *s, int success, void *userdata)
-{
-    vlc_pa_signal(0);
-    (void) s; (void) success; (void) userdata;
-}
-#else
-# define stream_success_cb NULL
-#endif
-
 
 /*** Sink input ***/
 static void sink_input_info_cb(pa_context *ctx, const pa_sink_input_info *i,
@@ -493,15 +562,12 @@ static void Pause(audio_output_t *aout, bool paused, mtime_t date)
 {
     aout_sys_t *sys = aout->sys;
     pa_stream *s = sys->stream;
-    pa_operation *op;
 
     vlc_pa_lock();
 
     if (paused) {
         sys->paused = date;
-        op = pa_stream_cork(s, paused, NULL, NULL);
-        if (op != NULL)
-            pa_operation_unref(op);
+        stream_stop(s, aout);
     } else {
         assert (sys->paused != VLC_TS_INVALID);
         date -= sys->paused;
@@ -611,6 +677,9 @@ static int Open(vlc_object_t *obj)
     /* 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)
     {
@@ -651,6 +720,28 @@ static int Open(vlc_object_t *obj)
         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;
+            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;
+            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;
+            encoding = PA_ENCODING_MPEG_IEC61937;
+            ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
+            break;*/
+        case VLC_CODEC_DTS:
+            format = VLC_CODEC_SPDIFL;
+            encoding = PA_ENCODING_DTS_IEC61937;
+            ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
+            break;
+#endif
         default:
             if (HAVE_FPU)
             {
@@ -747,13 +838,15 @@ static int Open(vlc_object_t *obj)
     aout->sys = sys;
     sys->stream = NULL;
     sys->context = ctx;
+    sys->trigger = NULL;
     sys->paused = VLC_TS_INVALID;
     sys->pts = VLC_TS_INVALID;
     sys->desync = 0;
     sys->rate = ss.rate;
 
     /* Context events */
-    const pa_subscription_mask_t mask = PA_SUBSCRIPTION_MASK_SINK_INPUT;
+    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);
@@ -764,15 +857,46 @@ static int Open(vlc_object_t *obj)
     sys->base_volume = PA_VOLUME_NORM;
     pa_cvolume_set(&sys->cvolume, ss.channels, PA_VOLUME_NORM);
 
-    vlc_pa_lock();
+#if PA_CHECK_VERSION(1,0,0)
+    pa_format_info *formatv[2];
+    unsigned formatc = 0;
+
+    /* Favor digital pass-through if available*/
+    if (encoding != PA_ENCODING_INVALID) {
+        formatv[formatc] = pa_format_info_new();
+        formatv[formatc]->encoding = encoding;
+        pa_format_info_set_rate(formatv[formatc], ss.rate);
+        pa_format_info_set_channels(formatv[formatc], ss.channels);
+        formatc++;
+    }
+
+    /* Fallback to PCM */
+    formatv[formatc] = pa_format_info_new();
+    formatv[formatc]->encoding = PA_ENCODING_PCM;
+    pa_format_info_set_sample_format(formatv[formatc], ss.format);
+    pa_format_info_set_rate(formatv[formatc], ss.rate);
+    pa_format_info_set_channels(formatv[formatc], ss.channels);
+    formatc++;
+
     /* Create a playback stream */
+    pa_stream *s;
+
+    vlc_pa_lock();
+    s = pa_stream_new_extended(ctx, "audio stream", formatv, formatc, NULL);
+
+    for (unsigned i = 0; i < formatc; i++)
+        pa_format_info_free(formatv[i]);
+#else
+    vlc_pa_lock();
     pa_stream *s = pa_stream_new(ctx, "audio stream", &ss, &map);
+#endif
     if (s == NULL) {
         vlc_pa_error(obj, "stream creation failure", ctx);
         goto fail;
     }
     sys->stream = s;
     pa_stream_set_state_callback(s, stream_state_cb, NULL);
+    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);
     pa_stream_set_overflow_callback(s, stream_overflow_cb, aout);
@@ -786,6 +910,21 @@ static int Open(vlc_object_t *obj)
         goto fail;
     }
 
+#if PA_CHECK_VERSION(1,0,0)
+    if (encoding != PA_ENCODING_INVALID) {
+        const pa_format_info *info = pa_stream_get_format_info(s);
+
+        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;
+        } else {
+            msg_Dbg(aout, "digital pass-through enabled");
+            pa_stream_set_latency_update_callback(s, NULL, NULL);
+        }
+    }
+#endif
+
     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",
@@ -830,30 +969,25 @@ static void Close (vlc_object_t *obj)
         /* The callback takes mainloop lock, so it CANNOT be held here! */
         var_DelCallback (aout, "audio-device", StreamMove, s);
         var_Destroy (aout, "audio-device");
-    }
 
-    vlc_pa_lock();
-    if (s != NULL) {
-        pa_operation *op;
+        vlc_pa_lock ();
+        if (unlikely(sys->trigger != NULL))
+            vlc_pa_rttime_free(sys->trigger);
+        pa_stream_disconnect(s);
 
-        if (pa_stream_is_corked(s) > 0)
-            /* Stream paused: discard all buffers */
-            op = pa_stream_flush(s, stream_success_cb, NULL);
-        else
-            /* Stream playing: wait until buffers are played */
-            op = pa_stream_drain(s, stream_success_cb, NULL);
-        if (likely(op != NULL)) {
-#ifdef LIBPULSE_GETS_A_CLUE
-            while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
-                vlc_pa_wait();
-#endif
-            pa_operation_unref(op);
-        }
+        /* 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_stream_disconnect(s);
         pa_stream_unref(s);
+        vlc_pa_unlock ();
     }
-    vlc_pa_unlock();
 
     vlc_pa_disconnect(obj, ctx);
     free(sys);