<div dir="ltr">Hi<br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Apr 3, 2015 at 3:53 PM, Victor Toso <span dir="ltr"><<a href="mailto:victortoso@redhat.com" target="_blank">victortoso@redhat.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Using ext-stream-restore we can get the last stream data of the<br>
application. This is helpful to spice-pulse in order to provide<br>
this values in case of volume-sync between client and guest.<br>
<br>
As pulseaudio don't store volume-changes immediately, we call<br>
pa_ext_stream_restore_write when the volume in the guest changes.<br></blockquote><div><br></div><div>The problem is that this value is shared among all instances, this may change the volume of other clients.<br><br>I think we shouldn't rely on stream-restore for running streams, but use instead pa_context_get_{sink_input,source_output}_info()</div><div><br></div><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
Related: <a href="https://bugzilla.redhat.com/show_bug.cgi?id=1012868" target="_blank">https://bugzilla.redhat.com/show_bug.cgi?id=1012868</a><br>
---<br>
 gtk/spice-pulse.c | 301 ++++++++++++++++++++++++++++++++++++++++++++++++++++--<br>
 1 file changed, 292 insertions(+), 9 deletions(-)<br>
<br>
diff --git a/gtk/spice-pulse.c b/gtk/spice-pulse.c<br>
index c583032..b12d30c 100644<br>
--- a/gtk/spice-pulse.c<br>
+++ b/gtk/spice-pulse.c<br>
@@ -25,18 +25,29 @@<br>
<br>
 #include <pulse/glib-mainloop.h><br>
 #include <pulse/pulseaudio.h><br>
+#include <pulse/ext-stream-restore.h><br>
<br>
 #define SPICE_PULSE_GET_PRIVATE(obj)                                  \<br>
     (G_TYPE_INSTANCE_GET_PRIVATE((obj), SPICE_TYPE_PULSE, SpicePulsePrivate))<br>
<br>
+struct async_task {<br>
+    GSimpleAsyncResult *res;<br>
+    GAsyncReadyCallback callback;<br>
+    gpointer user_data;<br>
+    gboolean is_playback;<br>
+};<br>
+<br>
 struct stream {<br>
-    pa_sample_spec          spec;<br>
-    pa_stream               *stream;<br>
-    int                     state;<br>
-    pa_operation            *uncork_op;<br>
-    pa_operation            *cork_op;<br>
-    gboolean                started;<br>
-    guint                   num_underflow;<br>
+    pa_sample_spec             spec;<br>
+    pa_stream                  *stream;<br>
+    int                        state;<br>
+    pa_operation               *uncork_op;<br>
+    pa_operation               *cork_op;<br>
+    gboolean                   started;<br>
+    guint                      num_underflow;<br>
+    gboolean                   changed;<br>
+    gchar                      *name;<br>
+    pa_ext_stream_restore_info info;<br>
 };<br>
<br>
 struct _SpicePulsePrivate {<br>
@@ -50,6 +61,8 @@ struct _SpicePulsePrivate {<br>
     struct stream           record;<br>
     guint                   last_delay;<br>
     guint                   target_delay;<br>
+    gboolean                restore_stream_info;<br>
+    GList                   *results;<br>
 };<br>
<br>
 G_DEFINE_TYPE(SpicePulse, spice_pulse, SPICE_TYPE_AUDIO)<br>
@@ -77,6 +90,17 @@ static const char *context_state_names[] = {<br>
 static void stream_stop(SpicePulse *pulse, struct stream *s);<br>
 static gboolean connect_channel(SpiceAudio *audio, SpiceChannel *channel);<br>
 static void channel_weak_notified(gpointer data, GObject *where_the_object_was);<br>
+static void pulse_stream_restore_info_async_complete(SpicePulse *pulse, const gchar *err_msg);<br>
+static void spice_pulse_get_playback_volume_info_async(SpiceAudio *audio,<br>
+        GAsyncReadyCallback callback, gpointer user_data);<br>
+static gboolean spice_pulse_get_playback_volume_info_finish(SpiceAudio *audio, GAsyncResult *res,<br>
+        gboolean *mute, guint8 *nchannels, guint16 **volume, GError **error);<br>
+static void spice_pulse_get_record_volume_info_async(SpiceAudio *audio,<br>
+        GAsyncReadyCallback callback, gpointer user_data);<br>
+static gboolean spice_pulse_get_record_volume_info_finish(SpiceAudio *audio,GAsyncResult *res,<br>
+        gboolean *mute, guint8 *nchannels, guint16 **volume, GError **error);<br>
+static void stream_restore_read_cb(pa_context *context,<br>
+        const pa_ext_stream_restore_info *info, int eol, void *userdata);<br>
<br>
 static void spice_pulse_finalize(GObject *obj)<br>
 {<br>
@@ -118,6 +142,13 @@ static void spice_pulse_dispose(GObject *obj)<br>
         pa_operation_unref(p->record.cork_op);<br>
     p->record.cork_op = NULL;<br>
<br>
+    if (p->results != NULL) {<br>
+        pulse_stream_restore_info_async_complete(pulse, "PulseAudio is being dispose");<br>
+    }<br>
+<br>
+    g_clear_pointer(&p-><a href="http://playback.name" target="_blank">playback.name</a>, g_free);<br>
+    g_clear_pointer(&p-><a href="http://record.name" target="_blank">record.name</a>, g_free);<br>
+<br>
     if (p->pchannel)<br>
         g_object_weak_unref(G_OBJECT(p->pchannel), channel_weak_notified, pulse);<br>
     p->pchannel = NULL;<br>
@@ -140,6 +171,10 @@ static void spice_pulse_class_init(SpicePulseClass *klass)<br>
     SpiceAudioClass *audio_class = SPICE_AUDIO_CLASS(klass);<br>
<br>
     audio_class->connect_channel = connect_channel;<br>
+    audio_class->get_playback_volume_info_async = spice_pulse_get_playback_volume_info_async;<br>
+    audio_class->get_playback_volume_info_finish = spice_pulse_get_playback_volume_info_finish;<br>
+    audio_class->get_record_volume_info_async = spice_pulse_get_record_volume_info_async;<br>
+    audio_class->get_record_volume_info_finish = spice_pulse_get_record_volume_info_finish;<br>
<br>
     gobject_class->finalize = spice_pulse_finalize;<br>
     gobject_class->dispose = spice_pulse_dispose;<br>
@@ -613,6 +648,20 @@ static void playback_volume_changed(GObject *object, GParamSpec *pspec, gpointer<br>
                   pa_strerror(pa_context_errno(p->context)));<br>
     else<br>
         pa_operation_unref(op);<br>
+<br>
+    if (p-><a href="http://playback.info.name" target="_blank">playback.info.name</a> != NULL) {<br>
+        /* Store new volume values to keep db up-to-date if we need them */<br>
+        p->playback.info.volume.channels = nchannels;<br>
+        memcpy(p->playback.info.volume.values, v.values, sizeof(pa_volume_t) * nchannels);<br>
+        op = pa_ext_stream_restore_write(p->context, PA_UPDATE_SET,<br>
+                                         &p-><a href="http://playback.info" target="_blank">playback.info</a>, 1, 1, NULL, NULL);<br>
+        if (!op) {<br>
+            g_warning("pa_ext_stream_restore_write() failed: %s",<br>
+                      pa_strerror(pa_context_errno(p->context)));<br>
+        } else {<br>
+            pa_operation_unref(op);<br>
+        }<br>
+    }<br>
 }<br>
<br>
 static void playback_mute_changed(GObject *object, GParamSpec *pspec, gpointer data)<br>
@@ -726,6 +775,20 @@ static void record_volume_changed(GObject *object, GParamSpec *pspec, gpointer d<br>
                   pa_strerror(pa_context_errno(p->context)));<br>
     else<br>
         pa_operation_unref(op);<br>
+<br>
+    if (p-><a href="http://record.info.name" target="_blank">record.info.name</a> != NULL) {<br>
+        /* Store new volume values to keep db up-to-date if we need them */<br>
+        p->record.info.volume.channels = nchannels;<br>
+        memcpy(p->record.info.volume.values, v.values, sizeof(pa_volume_t) * nchannels);<br>
+        op = pa_ext_stream_restore_write(p->context, PA_UPDATE_SET,<br>
+                                         &p-><a href="http://record.info" target="_blank">record.info</a>, 1, 1, NULL, NULL);<br>
+        if (!op) {<br>
+            g_warning("pa_ext_stream_restore_write() failed: %s",<br>
+                      pa_strerror(pa_context_errno(p->context)));<br>
+        } else {<br>
+            pa_operation_unref(op);<br>
+        }<br>
+    }<br>
 }<br>
<br>
 static void<br>
@@ -812,18 +875,39 @@ static void context_state_callback(pa_context *c, void *userdata)<br>
<br>
         if (!p->playback.stream && p->playback.started)<br>
             create_playback(SPICE_PULSE(userdata));<br>
+<br>
+        if (p->restore_stream_info == TRUE) {<br>
+            pa_operation *op = pa_ext_stream_restore_read(p->context,<br>
+                                                          stream_restore_read_cb,<br>
+                                                          pulse);<br>
+            if (!op) {<br>
+                pulse_stream_restore_info_async_complete(pulse,<br>
+                        pa_strerror(pa_context_errno(p->context)));<br>
+            } else {<br>
+                pa_operation_unref(op);<br>
+            }<br>
+        }<br>
         break;<br>
     }<br>
<br>
     case PA_CONTEXT_FAILED:<br>
         g_warning("PulseAudio context failed %s",<br>
                   pa_strerror(pa_context_errno(p->context)));<br>
-        break;<br>
+        goto context_fail;<br>
<br>
     case PA_CONTEXT_TERMINATED:<br>
     default:<br>
         SPICE_DEBUG("PulseAudio context terminated");<br>
-        break;<br>
+        goto context_fail;<br>
+    }<br>
+<br>
+    return;<br>
+<br>
+context_fail:<br>
+    if (p->restore_stream_info == TRUE) {<br>
+        const gchar *errmsg = pa_strerror(pa_context_errno(p->context));<br>
+        errmsg = (errmsg != NULL) ? errmsg : "PulseAudio context terminated";<br>
+        pulse_stream_restore_info_async_complete(pulse, errmsg);<br>
     }<br>
 }<br>
<br>
@@ -849,9 +933,208 @@ SpicePulse *spice_pulse_new(SpiceSession *session, GMainContext *context,<br>
         goto error;<br>
     }<br>
<br>
+    p-><a href="http://playback.name" target="_blank">playback.name</a> = g_strconcat("sink-input-by-application-name:",<br>
+                                   g_get_application_name(), NULL);<br>
+    p-><a href="http://record.name" target="_blank">record.name</a> = g_strconcat("source-output-by-application-name:",<br>
+                                 g_get_application_name(), NULL);<br>
     return pulse;<br>
<br>
 error:<br>
     g_object_unref(pulse);<br>
     return  NULL;<br>
 }<br>
+<br>
+static void pulse_stream_restore_info_async_complete(SpicePulse *pulse, const gchar *err_msg)<br>
+{<br>
+    SpicePulsePrivate *p = pulse->priv;<br>
+    GList *it;<br>
+    for(it = p->results; it != NULL; it = it->next) {<br>
+        struct async_task *task = it->data;<br>
+        /* If we do have any err_msg, we failed it all */<br>
+        if (err_msg != NULL) {<br>
+            g_simple_async_result_set_op_res_gboolean(task->res, FALSE);<br>
+            g_simple_async_result_set_error(task->res,<br>
+                                            SPICE_CLIENT_ERROR,<br>
+                                            SPICE_CLIENT_ERROR_FAILED,<br>
+                                            "restore-info failed due %s",<br>
+                                            err_msg);<br>
+<br>
+        /* Volume-info does not change if stream is not found */<br>
+        } else if ((task->is_playback == TRUE && p->playback.changed == FALSE) ||<br>
+                   (task->is_playback == FALSE && p->record.changed == FALSE)) {<br>
+            g_simple_async_result_set_op_res_gboolean(task->res, FALSE);<br>
+            g_simple_async_result_set_error(task->res,<br>
+                                            SPICE_CLIENT_ERROR,<br>
+                                            SPICE_CLIENT_ERROR_FAILED,<br>
+                                            "Stream not found by pulse");<br>
+        } else {<br>
+            g_simple_async_result_set_op_res_gboolean(task->res, TRUE);<br>
+        }<br>
+        g_simple_async_result_complete_in_idle(task->res);<br>
+        g_object_unref(task->res);<br>
+        g_free(task);<br>
+    }<br>
+    g_clear_pointer(&p->results, g_list_free);<br>
+    p->restore_stream_info = FALSE;<br>
+}<br>
+<br>
+static void stream_restore_read_cb(pa_context *context,<br>
+                                   const pa_ext_stream_restore_info *info,<br>
+                                   int eol,<br>
+                                   void *userdata)<br>
+{<br>
+    SpicePulsePrivate *p = SPICE_PULSE(userdata)->priv;<br>
+    struct stream *pstream = NULL;<br>
+<br>
+    if (eol) {<br>
+        pulse_stream_restore_info_async_complete(SPICE_PULSE(userdata), NULL);<br>
+        return;<br>
+    }<br>
+<br>
+    if (g_strcmp0(info->name, p-><a href="http://playback.name" target="_blank">playback.name</a>) == 0) {<br>
+        pstream = &p->playback;<br>
+    } else if (g_strcmp0(info->name, p-><a href="http://record.name" target="_blank">record.name</a>) == 0) {<br>
+        pstream = &p->record;<br>
+    } else {<br>
+        /* This is not the stream you are looking for. */<br>
+        return;<br>
+    }<br>
+<br>
+    if (info->channel_map.channels == 0) {<br>
+        SPICE_DEBUG("%s - Number of channels stored is zero. Ignore.", __func__);<br>
+        return;<br>
+    }<br>
+<br>
+    pstream->changed = TRUE;<br>
+    pstream-><a href="http://info.name" target="_blank">info.name</a> = pstream->name;<br>
+    pstream->info.mute = info->mute;<br>
+    memcpy(&pstream->info.channel_map, &info->channel_map.channels, sizeof(pa_channel_map));<br>
+    memcpy(&pstream->info.volume, &info->volume, sizeof(pa_cvolume));<br></blockquote><div><br></div><div>Shouldn't there be an async_complete() here?<br> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+}<br>
+<br>
+/* to avoid code duplication */<br>
+static void pulse_stream_restore_info_async(gboolean is_playback,<br>
+                                            SpiceAudio *audio,<br>
+                                            GAsyncReadyCallback callback,<br>
+                                            gpointer user_data)<br>
+{<br>
+    SpicePulsePrivate *p = SPICE_PULSE(audio)->priv;<br>
+    GSimpleAsyncResult *simple;<br>
+    struct async_task *task = g_malloc(sizeof(struct async_task));<br>
+<br>
+    simple = g_simple_async_result_new(G_OBJECT(audio),<br>
+                                       callback,<br>
+                                       user_data,<br>
+                                       pulse_stream_restore_info_async);<br>
+    task->res = simple;<br>
+    task->callback = callback;<br>
+    task->user_data = user_data;<br>
+    task->is_playback = is_playback;<br>
+    if (p->results == NULL) {<br>
+        p->playback.changed = FALSE;<br>
+        p->record.changed = FALSE;<br>
+<br>
+        if (p-><a href="http://playback.info.name" target="_blank">playback.info.name</a> != NULL ||<br>
+                p-><a href="http://record.info.name" target="_blank">record.info.name</a> != NULL ||<br>
+                pa_context_get_state(p->context) == PA_CONTEXT_READY) {<br>
+            pa_operation *op = pa_ext_stream_restore_read(p->context,<br>
+                                                          stream_restore_read_cb,<br>
+                                                          audio);<br>
+            if (!op) {<br>
+                g_simple_async_report_error_in_idle(G_OBJECT(audio),<br>
+                                                    callback,<br>
+                                                    user_data,<br>
+                                                    SPICE_CLIENT_ERROR,<br>
+                                                    SPICE_CLIENT_ERROR_FAILED,<br>
+                                                    "Restoring stream data failed: %s",<br>
+                                                    pa_strerror(pa_context_errno(p->context)));<br>
+                g_free(task);<br>
+                return;<br>
+            }<br>
+            pa_operation_unref(op);<br>
+        } else {<br>
+            /* It is possible that we want to get volume-info before the<br>
+             * context is in READY state. In this case, we wait for the<br>
+             * context state change to READY. */<br>
+            p->restore_stream_info = TRUE;<br>
+        }<br>
+    }<br>
+<br>
+    p->results = g_list_append(p->results, task);<br>
+    SPICE_DEBUG ("%s Number of async task is %d", __func__, g_list_length(p->results));<br>
+}<br>
+<br>
+/* to avoid code duplication */<br>
+static gboolean pulse_stream_restore_info_finish(gboolean is_playback,<br>
+                                                 SpiceAudio *audio,<br>
+                                                 GAsyncResult *res,<br>
+                                                 gboolean *mute,<br>
+                                                 guint8 *nchannels,<br>
+                                                 guint16 **volume,<br>
+                                                 GError **error)<br>
+{<br>
+    SpicePulsePrivate *p = SPICE_PULSE(audio)->priv;<br>
+    struct stream *pstream = (is_playback) ? &p->playback : &p->record;<br>
+    GSimpleAsyncResult *simple = (GSimpleAsyncResult *) res;<br>
+<br>
+    g_return_val_if_fail(g_simple_async_result_is_valid(res,<br>
+        G_OBJECT(audio), pulse_stream_restore_info_async), FALSE);<br>
+<br>
+    if (g_simple_async_result_propagate_error(simple, error)) {<br>
+        return FALSE;<br>
+    }<br>
+<br>
+    if (mute != NULL) {<br>
+        *mute = (pstream->info.mute) ? TRUE : FALSE;<br>
+    }<br>
+<br>
+    if (nchannels != NULL) {<br>
+        *nchannels = pstream->info.channel_map.channels;<br>
+    }<br>
+<br>
+    if (volume != NULL) {<br>
+        gint i;<br>
+        *volume = g_new(guint16, pstream->info.channel_map.channels);<br>
+        for (i = 0; i < pstream->info.channel_map.channels; i++) {<br>
+            (*volume)[i] = (guint16) pstream->info.volume.values[i];<br>
+        }<br>
+    }<br>
+<br>
+    return g_simple_async_result_get_op_res_gboolean(simple);<br>
+}<br>
+<br>
+static void spice_pulse_get_playback_volume_info_async(SpiceAudio *audio,<br>
+                                                       GAsyncReadyCallback callback,<br>
+                                                       gpointer user_data)<br>
+{<br>
+    pulse_stream_restore_info_async(TRUE, audio, callback, user_data);<br>
+}<br>
+<br>
+static gboolean spice_pulse_get_playback_volume_info_finish(SpiceAudio *audio,<br>
+                                                            GAsyncResult *res,<br>
+                                                            gboolean *mute,<br>
+                                                            guint8 *nchannels,<br>
+                                                            guint16 **volume,<br>
+                                                            GError **error)<br>
+{<br>
+    return pulse_stream_restore_info_finish(TRUE, audio, res, mute,<br>
+                                            nchannels, volume, error);<br>
+}<br>
+<br>
+static void spice_pulse_get_record_volume_info_async(SpiceAudio *audio,<br>
+                                                     GAsyncReadyCallback callback,<br>
+                                                     gpointer user_data)<br>
+{<br>
+    pulse_stream_restore_info_async(FALSE, audio, callback, user_data);<br>
+}<br>
+<br>
+static gboolean spice_pulse_get_record_volume_info_finish(SpiceAudio *audio,<br>
+                                                          GAsyncResult *res,<br>
+                                                          gboolean *mute,<br>
+                                                          guint8 *nchannels,<br>
+                                                          guint16 **volume,<br>
+                                                          GError **error)<br>
+{<br>
+    return pulse_stream_restore_info_finish(FALSE, audio, res, mute,<br>
+                                            nchannels, volume, error);<br>
+}<br>
<span class=""><font color="#888888">--<br>
2.1.0<br>
<br>
_______________________________________________<br>
Spice-devel mailing list<br>
<a href="mailto:Spice-devel@lists.freedesktop.org">Spice-devel@lists.freedesktop.org</a><br>
<a href="http://lists.freedesktop.org/mailman/listinfo/spice-devel" target="_blank">http://lists.freedesktop.org/mailman/listinfo/spice-devel</a><br>
</font></span></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature">Marc-André Lureau</div>
</div></div>