<div dir="ltr">Hi<br><div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Apr 14, 2015 at 2:18 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:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Functions to sync volume and mute value when necessary. In this patch,<br>
only one sync is allowed after agent connect.<br>
<br>
Resolves: <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/channel-main.c       | 141 +++++++++++++++++++++++++++++++++++++++++++++++<br>
 gtk/spice-session-priv.h |   2 +-<br>
 2 files changed, 142 insertions(+), 1 deletion(-)<br>
<br>
diff --git a/gtk/channel-main.c b/gtk/channel-main.c<br>
index 82169aa..1edf371 100644<br>
--- a/gtk/channel-main.c<br>
+++ b/gtk/channel-main.c<br>
@@ -30,6 +30,7 @@<br>
 #include "spice-util-priv.h"<br>
 #include "spice-channel-priv.h"<br>
 #include "spice-session-priv.h"<br>
+#include "spice-audio-priv.h"<br>
<br>
 /**<br>
  * SECTION:channel-main<br>
@@ -109,6 +110,10 @@ struct _SpiceMainChannelPrivate  {<br>
     guint                       migrate_delayed_id;<br>
     spice_migrate               *migrate_data;<br>
     int                         max_clipboard;<br>
+<br>
+    bool                        agent_volume_playback_sync;<br>
+    bool                        agent_volume_record_sync;<br>
+    GCancellable                *cancellable_volume_info;<br>
 };<br>
<br>
 struct spice_migrate {<br>
@@ -187,6 +192,7 @@ static const char *agent_msg_types[] = {<br>
     [ VD_AGENT_CLIPBOARD_GRAB          ] = "clipboard grab",<br>
     [ VD_AGENT_CLIPBOARD_REQUEST       ] = "clipboard request",<br>
     [ VD_AGENT_CLIPBOARD_RELEASE       ] = "clipboard release",<br>
+    [ VD_AGENT_AUDIO_VOLUME_SYNC       ] = "volume-sync",<br>
 };<br>
<br>
 static const char *agent_caps[] = {<br>
@@ -201,6 +207,7 @@ static const char *agent_caps[] = {<br>
     [ VD_AGENT_CAP_GUEST_LINEEND_LF    ] = "line-end lf",<br>
     [ VD_AGENT_CAP_GUEST_LINEEND_CRLF  ] = "line-end crlf",<br>
     [ VD_AGENT_CAP_MAX_CLIPBOARD       ] = "max-clipboard",<br>
+    [ VD_AGENT_CAP_AUDIO_VOLUME_SYNC   ] = "volume-sync",<br>
 };<br>
 #define NAME(_a, _i) ((_i) < SPICE_N_ELEMENTS(_a) ? (_a[(_i)] ?: "?") : "?")<br>
<br>
@@ -231,6 +238,7 @@ static void spice_main_channel_init(SpiceMainChannel *channel)<br>
     c = channel->priv = SPICE_MAIN_CHANNEL_GET_PRIVATE(channel);<br>
     c->agent_msg_queue = g_queue_new();<br>
     c->file_xfer_tasks = g_hash_table_new(g_direct_hash, g_direct_equal);<br>
+    c->cancellable_volume_info = g_cancellable_new();<br>
<br>
     spice_main_channel_reset_capabilties(SPICE_CHANNEL(channel));<br>
 }<br>
@@ -346,6 +354,9 @@ static void spice_main_channel_dispose(GObject *obj)<br>
         c->migrate_delayed_id = 0;<br>
     }<br>
<br>
+    if (c->cancellable_volume_info)<br>
+        g_object_unref(c->cancellable_volume_info);<br>
+<br></blockquote><div><br></div><div>In dispose(), this may be called multiple times, use g_clear_object(). <br><br></div><div>You should call cancel() before as it is not implicit.<br></div><div><br> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
     if (G_OBJECT_CLASS(spice_main_channel_parent_class)->dispose)<br>
         G_OBJECT_CLASS(spice_main_channel_parent_class)->dispose(obj);<br>
 }<br>
@@ -413,6 +424,9 @@ static void spice_main_channel_reset(SpiceChannel *channel, gboolean migrating)<br>
     agent_free_msg_queue(SPICE_MAIN_CHANNEL(channel));<br>
     c->agent_msg_queue = g_queue_new();<br>
<br>
+    c->agent_volume_playback_sync = false;<br>
+    c->agent_volume_record_sync = false;<br>
+<br>
     set_agent_connected(SPICE_MAIN_CHANNEL(channel), FALSE);<br>
<br>
     SPICE_CHANNEL_CLASS(spice_main_channel_parent_class)->channel_reset(channel, migrating);<br>
@@ -1092,6 +1106,128 @@ gboolean spice_main_send_monitor_config(SpiceMainChannel *channel)<br>
     return TRUE;<br>
 }<br>
<br>
+static void audio_playback_volume_info_cb(GObject *object, GAsyncResult *res, gpointer user_data)<br>
+{<br>
+    SpiceMainChannel *main_channel = user_data;<br>
+    SpiceSession *session = spice_channel_get_session(SPICE_CHANNEL(main_channel));<br>
+    SpiceAudio *audio = spice_audio_get(session, NULL);<br>
+    VDAgentAudioVolumeSync *avs;<br>
+    guint16 *volume;<br>
+    guint8 nchannels;<br>
+    gboolean mute, ret;<br>
+    gsize array_size;<br>
+    GError *error = NULL;<br>
+<br>
+    ret = SPICE_AUDIO_GET_CLASS(audio)->get_playback_volume_info_finish(audio,<br>
+                                                                        res,<br>
+                                                                        &mute,<br>
+                                                                        &nchannels,<br>
+                                                                        &volume,<br>
+                                                                        &error);<br>
+    if (ret == FALSE || volume == NULL || nchannels == 0) {<br>
+        if (error != NULL) {<br>
+            spice_warning("Failed to get playback async volume info: %s", error->message);<br>
+            g_error_free (error);<br>
+        } else {<br>
+            SPICE_DEBUG("Failed to get playback async volume info");<br>
+        }<br>
+        main_channel->priv->agent_volume_playback_sync = false;<br>
+        return;<br>
+    }<br>
+<br>
+    array_size = sizeof(uint16_t) * nchannels;<br>
+    avs = g_malloc0(sizeof(VDAgentAudioVolumeSync) + array_size);<br>
+    avs->is_playback = TRUE;<br>
+    avs->mute = mute;<br>
+    avs->nchannels = nchannels;<br>
+    memcpy(avs->volume, volume, array_size);<br>
+<br>
+    SPICE_DEBUG ("%s mute=%s nchannels=%u volume[0]=%u",<br>
+                 __func__, spice_yes_no(mute), nchannels, volume[0]);<br>
+    g_clear_pointer (&volume, g_free);<br>
+    agent_msg_queue(main_channel, VD_AGENT_AUDIO_VOLUME_SYNC,<br>
+                    sizeof(VDAgentAudioVolumeSync) + array_size, avs);<br>
+}<br>
+<br>
+static void agent_sync_audio_playback(SpiceMainChannel *main_channel)<br>
+{<br>
+    SpiceSession *session = spice_channel_get_session(SPICE_CHANNEL(main_channel));<br>
+    SpiceAudio *audio = spice_audio_get(session, NULL);<br>
+    SpiceMainChannelPrivate *c = main_channel->priv;<br>
+<br>
+    if (!test_agent_cap(main_channel, VD_AGENT_CAP_AUDIO_VOLUME_SYNC) ||<br>
+        c->agent_volume_playback_sync == true) {<br>
+        SPICE_DEBUG("%s - is not going to sync audio with guest", __func__);<br>
+        return;<br>
+    }<br>
+    /* only one per connection */<br>
+    g_cancellable_reset(c->cancellable_volume_info);<br>
+    c->agent_volume_playback_sync = true;<br>
+    SPICE_AUDIO_GET_CLASS(audio)->get_playback_volume_info_async(audio,<br>
+            c->cancellable_volume_info, main_channel, audio_playback_volume_info_cb, main_channel);<br>
+}<br>
+<br>
+static void audio_record_volume_info_cb(GObject *object, GAsyncResult *res, gpointer user_data)<br>
+{<br>
+    SpiceMainChannel *main_channel = user_data;<br>
+    SpiceSession *session = spice_channel_get_session(SPICE_CHANNEL(main_channel));<br>
+    SpiceAudio *audio = spice_audio_get(session, NULL);<br>
+    VDAgentAudioVolumeSync *avs;<br>
+    guint16 *volume;<br>
+    guint8 nchannels;<br>
+    gboolean ret, mute;<br>
+    gsize array_size;<br>
+    GError *error = NULL;<br>
+<br>
+    ret = SPICE_AUDIO_GET_CLASS(audio)->get_record_volume_info_finish(audio,<br>
+                                                                      res,<br>
+                                                                      &mute,<br>
+                                                                      &nchannels,<br>
+                                                                      &volume,<br>
+                                                                      &error);<br>
+    if (ret == FALSE || volume == NULL || nchannels == 0) {<br>
+        if (error != NULL) {<br>
+            spice_warning ("Failed to get record async volume info: %s", error->message);<br>
+            g_error_free (error);<br>
+        } else {<br>
+            SPICE_DEBUG("Failed to get record async volume info");<br>
+        }<br>
+        main_channel->priv->agent_volume_record_sync = false;<br>
+        return;<br>
+    }<br>
+<br>
+    array_size = sizeof(uint16_t) * nchannels;<br>
+    avs = g_malloc0(sizeof(VDAgentAudioVolumeSync) + array_size);<br>
+    avs->is_playback = FALSE;<br>
+    avs->mute = mute;<br>
+    avs->nchannels = nchannels;<br>
+    memcpy(avs->volume, volume, array_size);<br>
+<br>
+    SPICE_DEBUG ("%s mute=%s nchannels=%u volume[0]=%u",<br>
+                 __func__, spice_yes_no(mute), nchannels, volume[0]);<br>
+    g_clear_pointer (&volume, g_free);<br>
+    agent_msg_queue(main_channel, VD_AGENT_AUDIO_VOLUME_SYNC,<br>
+                    sizeof(VDAgentAudioVolumeSync) + array_size, avs);<br>
+}<br>
+<br>
+static void agent_sync_audio_record(SpiceMainChannel *main_channel)<br>
+{<br>
+    SpiceSession *session = spice_channel_get_session(SPICE_CHANNEL(main_channel));<br>
+    SpiceAudio *audio = spice_audio_get(session, NULL);<br>
+    SpiceMainChannelPrivate *c = main_channel->priv;<br>
+<br>
+    if (!test_agent_cap(main_channel, VD_AGENT_CAP_AUDIO_VOLUME_SYNC) ||<br>
+        c->agent_volume_record_sync == true) {<br>
+        SPICE_DEBUG("%s - is not going to sync audio with guest", __func__);<br>
+        return;<br>
+    }<br>
+    /* only one per connection */<br>
+    g_cancellable_reset(c->cancellable_volume_info);<br>
+    c->agent_volume_record_sync = true;<br>
+    SPICE_AUDIO_GET_CLASS(audio)->get_record_volume_info_async(audio,<br>
+            c->cancellable_volume_info, main_channel, audio_record_volume_info_cb, main_channel);<br>
+}<br>
+<br>
 /* any context: the message is not flushed immediately,<br>
    you can wakeup() the channel coroutine or send_msg_queue() */<br>
 static void agent_display_config(SpiceMainChannel *channel)<br>
@@ -1345,6 +1481,8 @@ static void agent_start(SpiceMainChannel *channel)<br>
     };<br>
     SpiceMsgOut *out;<br>
<br>
+    c->agent_volume_playback_sync = false;<br>
+    c->agent_volume_record_sync = false;<br>
     c->agent_caps_received = false;<br>
     set_agent_connected(channel, TRUE);<br>
<br>
@@ -1801,6 +1939,9 @@ static void main_agent_handle_msg(SpiceChannel *channel,<br>
             c->agent_display_config_sent = true;<br>
         }<br>
<br>
+        agent_sync_audio_playback(self);<br>
+        agent_sync_audio_record(self);<br>
+<br>
         agent_max_clipboard(self);<br>
<br>
         agent_send_msg_queue(self);<br>
diff --git a/gtk/spice-session-priv.h b/gtk/spice-session-priv.h<br>
index 46938ff..049973a 100644<br>
--- a/gtk/spice-session-priv.h<br>
+++ b/gtk/spice-session-priv.h<br>
@@ -98,7 +98,7 @@ PhodavServer* channel_webdav_server_new(SpiceSession *session);<br>
 guint spice_session_get_n_display_channels(SpiceSession *session);<br>
 void spice_session_set_main_channel(SpiceSession *session, SpiceChannel *channel);<br>
 gboolean spice_session_set_migration_session(SpiceSession *session, SpiceSession *mig_session);<br>
-<br>
+SpiceAudio *spice_audio_get(SpiceSession *session, GMainContext *context);<br>
 G_END_DECLS<br>
<br>
 #endif /* __SPICE_CLIENT_SESSION_PRIV_H__ */<br>
<span class="HOEnZb"><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"></div><div class="gmail_extra">ack otherwise<br><br></div><div class="gmail_extra">-- <br><div class="gmail_signature">Marc-André Lureau</div>
</div></div></div>