[Spice-devel] [PATCH spice-gtk 1/4] Add spice_audio_get()

Marc-André Lureau marcandre.lureau at gmail.com
Sat Nov 5 09:56:06 PDT 2011


We are going to deprecate spice_audio_new()
some day. There are a few know problems:
- SpiceAudio is an abstract class,
  so it can't have a ctor
- SpiceAudio should be a singleton,
  associated with the session lifetime
- SpiceSession should have a enable-audio property,
  internal code should be able to access the audio object

That way of getting the audio object is similar to the smartcard manager and usb manager.
---
 gtk/map-file      |    1 +
 gtk/spice-audio.c |   33 +++++++++++++++++++++++++++++++++
 gtk/spice-audio.h |    6 +++---
 3 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/gtk/map-file b/gtk/map-file
index 0b1b26f..f9bc46c 100644
--- a/gtk/map-file
+++ b/gtk/map-file
@@ -2,6 +2,7 @@ SPICEGTK_1 {
 global:
 spice_audio_get_type;
 spice_audio_new;
+spice_audio_get;
 spice_channel_connect;
 spice_channel_destroy;
 spice_channel_disconnect;
diff --git a/gtk/spice-audio.c b/gtk/spice-audio.c
index baa7c0b..e991cea 100644
--- a/gtk/spice-audio.c
+++ b/gtk/spice-audio.c
@@ -89,3 +89,36 @@ SpiceAudio *spice_audio_new(SpiceSession *session, GMainContext *context,
 #endif
     return audio;
 }
+
+/**
+ * spice_audio_get:
+ * @session: the #SpiceSession to connect to
+ * @context: (allow-none): a #GMainContext to attach to (or %NULL for default).
+ *
+ * Gets the #SpiceAudio associated with the passed in #SpiceSession.
+ * A new #SpiceAudio instance will be created the first time this
+ * function is called for a certain #SpiceSession.
+ *
+ * Note that this function returns a weak reference, which should not be used
+ * after the #SpiceSession itself has been unref-ed by the caller.
+ *
+ * Returns: (transfer none): a weak reference to a #SpiceAudio
+ * instance or %NULL if failed.
+ **/
+SpiceAudio *spice_audio_get(SpiceSession *session, GMainContext *context)
+{
+    static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
+    SpiceAudio *self;
+
+    g_static_mutex_lock(&mutex);
+    self = g_object_get_data(G_OBJECT(session), "spice-audio");
+    if (self == NULL) {
+        self = spice_audio_new(session, context, NULL);
+        g_object_set_data(G_OBJECT(session), "spice-audio", self);
+        if (self)
+            g_object_weak_ref(G_OBJECT(session), (GWeakNotify)g_object_unref, self);
+    }
+    g_static_mutex_unlock(&mutex);
+
+    return self;
+}
diff --git a/gtk/spice-audio.h b/gtk/spice-audio.h
index b881698..dc66e88 100644
--- a/gtk/spice-audio.h
+++ b/gtk/spice-audio.h
@@ -69,9 +69,9 @@ struct _SpiceAudioClass {
 
 GType spice_audio_get_type(void);
 
-SpiceAudio *spice_audio_new(SpiceSession *session,
-                         GMainContext *context,
-                         const char *name);
+SpiceAudio* spice_audio_new(SpiceSession *session, GMainContext *context, const char *name);
+
+SpiceAudio* spice_audio_get(SpiceSession *session, GMainContext *context);
 
 G_END_DECLS
 
-- 
1.7.7



More information about the Spice-devel mailing list