<div dir="ltr">Hi<br><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:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">In case of volume-sync between client and guest, we request volume-info<br>
from the availables streams and if the stream is not available we rely<br>
on ext-stream-restore.<br>
<br>
By using ext-stream-restore we can get the last stream data of the<br>
application that is stored by PulseAudio.<br>
<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 | 412 ++++++++++++++++++++++++++++++++++++++++++++++++++++--<br>
 1 file changed, 403 insertions(+), 9 deletions(-)<br>
<br>
diff --git a/gtk/spice-pulse.c b/gtk/spice-pulse.c<br>
index c583032..811449d 100644<br>
--- a/gtk/spice-pulse.c<br>
+++ b/gtk/spice-pulse.c<br>
@@ -25,18 +25,31 @@<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>
+  Â  SpicePulse  Â  Â  Â  Â  Â  Â  Â  Â *pulse;<br>
+  Â  SpiceMainChannel  Â  Â  Â  Â  Â *main_channel;<br>
+  Â  GSimpleAsyncResult  Â  Â  Â  Â *res;<br>
+  Â  GAsyncReadyCallback  Â  Â  Â  callback;<br>
+  Â  gpointer  Â  Â  Â  Â  Â  Â  Â  Â  Â user_data;<br>
+  Â  gboolean  Â  Â  Â  Â  Â  Â  Â  Â  Â is_playback;<br></blockquote><div><br></div><div>You should track and cancel associated pa_operation when cancelling tasks<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>
 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></blockquote><div><br></div><div>It took me a while to understand "changed", perhaps rename it "info_updated" ?<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">
+  Â  gchar  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  *name;<br>
+  Â  pa_ext_stream_restore_info info;<br>
 };<br>
<br>
 struct _SpicePulsePrivate {<br>
@@ -50,6 +63,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 +92,18 @@ 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,<br>
+  Â  Â  Â  struct async_task *task, const gchar *err_msg);<br></blockquote><div><br></div><div>I would use split it in 2 functions:<br><br>spice_pulse_complete_async_task<br>spice_pulse_complete_all_async_tasks<br><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">
+static void spice_pulse_get_playback_volume_info_async(SpiceAudio *audio, GCancellable *cancellable,<br>
+  Â  Â  Â  SpiceMainChannel *main_channel, 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, GCancellable *cancellable,<br>
+  Â  Â  Â  SpiceMainChannel *main_channel, 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 +145,15 @@ 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, NULL, "PulseAudio is being dispose");<br>
+<br>
+  Â  if (p-><a href="http://playback.name" target="_blank">playback.name</a>)<br>
+  Â  Â  Â  g_free(p-><a href="http://playback.name" target="_blank">playback.name</a>);<br>
+<br>
+  Â  if (p-><a href="http://record.name" target="_blank">record.name</a>)<br>
+  Â  Â  Â  g_free(p-><a href="http://record.name" target="_blank">record.name</a>);<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 +176,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>
@@ -812,18 +852,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, NULL,<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, NULL, errmsg);<br>
  Â  Â }<br>
 }<br>
<br>
@@ -849,9 +910,342 @@ 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 free_async_task(struct async_task *task)<br>
+{<br>
+  Â  if (task == NULL)<br>
+  Â  Â  Â  return;<br>
+<br>
+  Â  if (task->pulse)<br>
+  Â  Â  Â  g_object_unref(task->pulse);<br>
+<br>
+  Â  if (task->res)<br>
+  Â  Â  Â  g_object_unref(task->res);<br>
+<br>
+  Â  if (task->main_channel)<br>
+  Â  Â  Â  g_object_unref(task->main_channel);<br>
+<br>
+  Â  g_free(task);<br>
+}<br>
+<br>
+static void complete_task(SpicePulse *pulse, struct async_task *task, const gchar *err_msg)<br>
+{<br>
+  Â  SpicePulsePrivate *p = pulse->priv;<br>
+<br>
+  Â  /* If we do have any err_msg, we failed */<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>
+  Â  /* 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>
+<br>
+  Â  /* As all async calls to PulseAudio are done with glib mainloop, it is<br>
+  Â  Â * safe to complete the operation synchronously here. */<br>
+  Â  g_simple_async_result_complete(task->res);<br>
+}<br>
+<br>
+static void pulse_stream_restore_info_async_complete(SpicePulse *pulse,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â struct async_task *task,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â const gchar *err_msg)<br>
+{<br>
+  Â  SpicePulsePrivate *p = pulse->priv;<br>
+  Â  GList *it;<br>
+<br>
+  Â  /* Only complete this task */<br>
+  Â  if (task != NULL) {<br>
+  Â  Â  Â  complete_task(pulse, task, err_msg);<br>
+  Â  Â  Â  if (p->results != NULL) {<br>
+  Â  Â  Â  Â  Â  p->results = g_list_remove(p->results, task);<br>
+  Â  Â  Â  Â  Â  SPICE_DEBUG("%s Number of async task is %d",<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  __func__, g_list_length(p->results));<br>
+  Â  Â  Â  }<br>
+  Â  Â  Â  free_async_task(task);<br>
+  Â  Â  Â  return;<br>
+  Â  }<br>
+<br>
+  Â  /* Complete all tasks in list */<br>
+  Â  for(it = p->results; it != NULL; it = it->next) {<br>
+  Â  Â  Â  struct async_task *task = it->data;<br>
+  Â  Â  Â  complete_task(pulse, task, err_msg);<br>
+  Â  Â  Â  free_async_task(task);<br>
+  Â  }<br>
+  Â  g_list_free(p->results);<br>
+  Â  p->results = NULL;<br>
+  Â  p->restore_stream_info = FALSE;<br>
+  Â  SPICE_DEBUG("%s Number of async task is %d", __func__, g_list_length(p->results));<br></blockquote><div><br></div><div>You are using __func__ in many places, I think G_STRFUNC could be better? SPICE_DEBUG() already has G_STRLOC so perhaps not really needed<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>
+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, 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></blockquote><div><br></div><div>g_warn_if_reached() ?<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">
+  Â  Â  Â  return;<br>
+  Â  }<br>
+<br>
+  Â  if (info->channel_map.channels == 0) {<br>
+  Â  Â  Â  SPICE_DEBUG("%s - Number of channels stored is zero. Ignore. (%s)", __func__, info->name);<br>
+  Â  Â  Â  return;<br>
+  Â  </blockquote><div><br></div><div>What if there is already a playing stream, is this going to reset it to restore value?<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>
+  Â  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, sizeof(pa_channel_map));<br></blockquote><div><br></div><div>No need for memcpy please: <br>pstream->info.channel_map = info->channel_map;<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">
+  Â  memcpy(&pstream->info.volume, &info->volume, sizeof(pa_cvolume));<br>
+}<br>
+<br>
+static void source_output_info_cb(pa_context *context,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  const pa_source_output_info *info,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  int eol,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  void *userdata)<br>
+{<br>
+  Â  struct async_task *task = userdata;<br>
+  Â  SpicePulsePrivate *p = task->pulse->priv;<br>
+  Â  struct stream *pstream = &p->record;<br>
+<br>
+  Â  if (eol) {<br>
+  Â  Â  Â  pulse_stream_restore_info_async_complete(task->pulse, task, NULL);<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, sizeof(pa_channel_map));<br>
+  Â  memcpy(&pstream->info.volume, &info->volume, sizeof(pa_cvolume));<br>
+}<br>
+<br>
+static void sink_input_info_cb(pa_context *context,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â const pa_sink_input_info *info,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â int eol,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â void *userdata)<br>
+{<br>
+  Â  struct async_task *task = userdata;<br>
+  Â  SpicePulsePrivate *p = task->pulse->priv;<br>
+  Â  struct stream *pstream = &p->playback;<br>
+<br>
+  Â  if (eol) {<br>
+  Â  Â  Â  struct async_task *task = userdata;<br></blockquote><div><br></div><div>already declared above<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">
+  Â  Â  Â  pulse_stream_restore_info_async_complete(task->pulse, task, NULL);<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, sizeof(pa_channel_map));<br>
+  Â  memcpy(&pstream->info.volume, &info->volume, sizeof(pa_cvolume));<br>
+}<br>
+<br>
+/* to avoid code duplication */<br>
+static void pulse_stream_restore_info_async(gboolean is_playback,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  SpiceAudio *audio,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  GCancellable *cancellable,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  SpiceMainChannel *main_channel,<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>
+  Â  pa_operation *op = NULL;<br>
+<br>
+  Â  simple = g_simple_async_result_new(G_OBJECT(audio),<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â callback,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â user_data,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â pulse_stream_restore_info_async);<br>
+  Â  g_simple_async_result_set_check_cancellable (simple, cancellable);<br>
+<br>
+  Â  task->res = simple;<br>
+  Â  task->pulse = g_object_ref(audio);<br>
+  Â  task->callback = callback;<br>
+  Â  task->user_data = user_data;<br>
+  Â  task->is_playback = is_playback;<br>
+  Â  task->main_channel = g_object_ref(main_channel);<br>
+<br>
+  Â  /* If Playback/Record stream is created we use pulse API to get volume-info<br>
+  Â  Â * from those streams directly. If the stream is not created, retrieve last<br>
+  Â  Â * volume/mute values from Pulse database using the application name; */<br>
+<br>
+  Â  if (is_playback == TRUE &&<br>
+  Â  Â  Â  Â  Â  p->playback.stream != NULL &&<br>
+  Â  Â  Â  Â  Â  pa_stream_get_index(p->playback.stream) != PA_INVALID_INDEX) {<br>
+  Â  Â  Â  SPICE_DEBUG("%s - Playback stream is created - get-sink-input-info", __func__);<br>
+  Â  Â  Â  p->playback.changed = FALSE;<br>
+  Â  Â  Â  op = pa_context_get_sink_input_info(p->context,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  pa_stream_get_index(p->playback.stream),<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  sink_input_info_cb,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  task);<br>
+  Â  Â  Â  if (!op)<br>
+  Â  Â  Â  Â  Â  goto fail;<br>
+  Â  Â  Â  pa_operation_unref(op);<br>
+<br>
+  Â  } else if (is_playback == FALSE &&<br>
+  Â  Â  Â  Â  Â  p->record.stream != NULL &&<br>
+  Â  Â  Â  Â  Â  pa_stream_get_index(p->record.stream) != PA_INVALID_INDEX) {<br>
+  Â  Â  Â  SPICE_DEBUG("%s - Record stream is created - get-source-output-info", __func__);<br>
+  Â  Â  Â  p->record.changed = FALSE;<br>
+  Â  Â  Â  op = pa_context_get_source_output_info(p->context,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â pa_stream_get_index(p->record.stream),<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â source_output_info_cb,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â task);<br>
+  Â  Â  Â  if (!op)<br>
+  Â  Â  Â  Â  Â  goto fail;<br>
+  Â  Â  Â  pa_operation_unref(op);<br>
+<br>
+  Â  } else if (p->results == NULL) {<br>
+  Â  Â  Â  SPICE_DEBUG("%s - Streams are not created - ext-stream-restore", __func__);<br>
+  Â  Â  Â  p->playback.changed = FALSE;<br>
+  Â  Â  Â  p->record.changed = FALSE;<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>
+<br>
+  Â  Â  Â  Â  Â  /* Stream is not created. Restore value from pulse db */<br>
+  Â  Â  Â  Â  Â  op = pa_ext_stream_restore_read(p->context, stream_restore_read_cb, audio);<br>
+  Â  Â  Â  Â  Â  if (!op)<br>
+  Â  Â  Â  Â  Â  Â  Â  goto fail;<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></blockquote><div><br></div><div>else ? adding a useless task? (if there is already a playback info request, the record will be ignored?)<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>
+  Â  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>
+  Â  return;<br>
+<br>
+fail:<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>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  "Volume-Info failed: %s",<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  pa_strerror(pa_context_errno(p->context)));<br>
+  Â  Â  Â  free_async_task(task);<br>
+  Â  }<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] = MIN(pstream->info.volume.values[i], G_MAXUINT16);<br>
+  Â  Â  Â  Â  Â  SPICE_DEBUG("%s (%s) volume at channel %d is %u", __func__,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  (is_playback) ? "playback" : "record", i, (*volume)[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>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â GCancellable *cancellable,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â SpiceMainChannel *main_channel,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â GAsyncReadyCallback callback,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â gpointer user_data)<br>
+{<br>
+  Â  pulse_stream_restore_info_async(TRUE, audio, cancellable, main_channel, 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>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â GCancellable *cancellable,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â SpiceMainChannel *main_channel,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â GAsyncReadyCallback callback,<br>
+  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â gpointer user_data)<br>
+{<br>
+  Â  pulse_stream_restore_info_async(FALSE, audio, cancellable, main_channel, 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><font color="#888888">--<br>
2.1.0<br>
<br>
_______________________________________________<br>
Spice-devel mailing list<br>
<a href="mailto:Spice-devel@lists.freedesktop.org" target="_blank">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>Marc-André Lureau</div>
</div></div>