[farsight2/master] Add FsNiceThread object to be a reference counted thread

Olivier Crête olivier.crete at collabora.co.uk
Tue Dec 23 15:26:21 PST 2008


---
 transmitters/nice/Makefile.am           |    6 +-
 transmitters/nice/fs-nice-thread.c      |  263 +++++++++++++++++++++++++++++++
 transmitters/nice/fs-nice-thread.h      |   98 ++++++++++++
 transmitters/nice/fs-nice-transmitter.c |    2 +
 4 files changed, 367 insertions(+), 2 deletions(-)
 create mode 100644 transmitters/nice/fs-nice-thread.c
 create mode 100644 transmitters/nice/fs-nice-thread.h

diff --git a/transmitters/nice/Makefile.am b/transmitters/nice/Makefile.am
index 2cd7abc..d7e9faf 100644
--- a/transmitters/nice/Makefile.am
+++ b/transmitters/nice/Makefile.am
@@ -6,7 +6,8 @@ plugin_LTLIBRARIES = libnice-transmitter.la
 # sources used to compile this lib
 libnice_transmitter_la_SOURCES = \
 	fs-nice-transmitter.c \
-	fs-nice-stream-transmitter.c
+	fs-nice-stream-transmitter.c \
+	fs-nice-thread.c
 
 # flags used to compile this plugin
 libnice_transmitter_la_CFLAGS = $(FS2_INTERNAL_CFLAGS) $(FS2_CFLAGS) \
@@ -18,4 +19,5 @@ libnice_transmitter_la_LIBADD = \
 
 noinst_HEADERS = \
 	fs-nice-transmitter.h \
-	fs-nice-stream-transmitter.h
+	fs-nice-stream-transmitter.h \
+	fs-nice-thread.h
diff --git a/transmitters/nice/fs-nice-thread.c b/transmitters/nice/fs-nice-thread.c
new file mode 100644
index 0000000..701d843
--- /dev/null
+++ b/transmitters/nice/fs-nice-thread.c
@@ -0,0 +1,263 @@
+/*
+ * Farsight2 - Farsight libnice Transmitter thread object
+ *
+ * Copyright 2007-2008 Collabora Ltd.
+ *  @author: Olivier Crete <olivier.crete at collabora.co.uk>
+ * Copyright 2007-2008 Nokia Corp.
+ *
+ * fs-nice-thread.c - A Farsight libnice transmitter thread object
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
+ */
+
+/**
+ * SECTION:fs-nice-thread
+ * @short_description: A transmitter for threads for libnice
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "fs-nice-transmitter.h"
+#include "fs-nice-thread.h"
+
+#include <string.h>
+#include <sys/types.h>
+
+#define GST_CAT_DEFAULT fs_nice_transmitter_debug
+
+/* Signals */
+enum
+{
+  LAST_SIGNAL
+};
+
+/* props */
+enum
+{
+  PROP_0
+};
+
+struct _FsNiceThreadPrivate
+{
+  GMainContext *main_context;
+  GMainLoop *main_loop;
+
+  GMutex *mutex;
+
+  /* Everything below is protected by the mutex */
+
+  GThread *thread;
+};
+
+#define FS_NICE_THREAD_GET_PRIVATE(o)  \
+  (G_TYPE_INSTANCE_GET_PRIVATE ((o), FS_TYPE_NICE_THREAD, \
+    FsNiceThreadPrivate))
+
+
+#define FS_NICE_THREAD_LOCK(o)   g_mutex_lock ((o)->priv->mutex)
+#define FS_NICE_THREAD_UNLOCK(o) g_mutex_unlock ((o)->priv->mutex)
+
+static void fs_nice_thread_class_init (
+    FsNiceThreadClass *klass);
+static void fs_nice_thread_init (FsNiceThread *self);
+static void fs_nice_thread_finalize (GObject *object);
+static void fs_nice_thread_stop_thread (FsNiceThread *self);
+
+
+static GObjectClass *parent_class = NULL;
+//static guint signals[LAST_SIGNAL] = { 0 };
+
+
+/*
+ * Lets register the plugin
+ */
+
+static GType type = 0;
+
+GType
+fs_nice_thread_get_type (void)
+{
+  g_assert (type);
+  return type;
+}
+
+GType
+fs_nice_thread_register_type (FsPlugin *module)
+{
+  static const GTypeInfo info = {
+    sizeof (FsNiceThreadClass),
+    NULL,
+    NULL,
+    (GClassInitFunc) fs_nice_thread_class_init,
+    NULL,
+    NULL,
+    sizeof (FsNiceThread),
+    0,
+    (GInstanceInitFunc) fs_nice_thread_init
+  };
+
+  type = g_type_module_register_type (G_TYPE_MODULE (module),
+      G_TYPE_OBJECT, "FsNiceThread", &info, 0);
+
+  return type;
+}
+
+static void
+fs_nice_thread_class_init (FsNiceThreadClass *klass)
+{
+  GObjectClass *gobject_class = (GObjectClass *) klass;
+
+  parent_class = g_type_class_peek_parent (klass);
+
+  gobject_class->finalize = fs_nice_thread_finalize;
+
+  g_type_class_add_private (klass, sizeof (FsNiceThreadPrivate));
+}
+
+static void
+fs_nice_thread_init (FsNiceThread *self)
+{
+
+  /* member init */
+  self->priv = FS_NICE_THREAD_GET_PRIVATE (self);
+
+  self->priv->mutex = g_mutex_new ();
+
+  self->priv->main_context = g_main_context_new ();
+  self->priv->main_loop = g_main_loop_new (self->priv->main_context, FALSE);
+}
+
+static void
+fs_nice_thread_finalize (GObject *object)
+{
+  FsNiceThread *self = FS_NICE_THREAD (object);
+
+  fs_nice_thread_stop_thread (self);
+
+  if (self->priv->main_context)
+  {
+    g_main_context_unref (self->priv->main_context);
+    self->priv->main_context = NULL;
+  }
+
+  if (self->priv->main_loop)
+  {
+    g_main_loop_unref (self->priv->main_loop);
+    self->priv->main_loop = NULL;
+  }
+
+  g_mutex_free (self->priv->mutex);
+
+  parent_class->finalize (object);
+}
+
+
+static gboolean
+thread_unlock_idler (gpointer data)
+{
+  FsNiceThread *self = FS_NICE_THREAD (data);
+
+  g_main_loop_quit (self->priv->main_loop);
+
+  return TRUE;
+}
+
+static void
+fs_nice_thread_stop_thread (FsNiceThread *self)
+{
+  GSource *idle_source;
+
+  FS_NICE_THREAD_LOCK(self);
+
+  if (self->priv->thread == NULL)
+  {
+    FS_NICE_THREAD_UNLOCK (self);
+    return;
+  }
+  FS_NICE_THREAD_UNLOCK (self);
+
+  g_main_loop_quit (self->priv->main_loop);
+
+  idle_source = g_idle_source_new ();
+  g_source_set_priority (idle_source, G_PRIORITY_HIGH);
+  g_source_set_callback (idle_source, thread_unlock_idler, self, NULL);
+  g_source_attach (idle_source, self->priv->main_context);
+
+  g_thread_join (self->priv->thread);
+
+  FS_NICE_THREAD_LOCK (self);
+  self->priv->thread = NULL;
+  FS_NICE_THREAD_UNLOCK (self);
+}
+
+GMainContext *
+fs_nice_thread_get_context (FsNiceThread *self)
+{
+  return self->priv->main_context;
+}
+
+static void
+object_dead (gpointer data, GObject *where_the_object_was)
+{
+  FsNiceThread *self = FS_NICE_THREAD (data);
+
+  g_object_unref (self);
+}
+
+void
+fs_nice_thread_add_weak_object (FsNiceThread *self,
+    GObject *object)
+{
+  g_object_weak_ref (G_OBJECT (object), object_dead, self);
+
+  g_object_ref (self);
+}
+
+
+
+static gpointer
+fs_nice_thread_main_thread (gpointer data)
+{
+  FsNiceThread *self = FS_NICE_THREAD (data);
+
+  g_main_loop_run (self->priv->main_loop);
+
+  return NULL;
+}
+
+FsNiceThread *
+fs_nice_thread_new (GError **error)
+{
+  FsNiceThread *self = NULL;
+
+  self = g_object_new (FS_TYPE_NICE_THREAD, NULL);
+
+  FS_NICE_THREAD_LOCK (self);
+  self->priv->thread = g_thread_create (fs_nice_thread_main_thread,
+      self, TRUE, error);
+
+  if (!self->priv->thread)
+  {
+    FS_NICE_THREAD_UNLOCK (self);
+    g_object_unref (self);
+    return NULL;
+  }
+  FS_NICE_THREAD_UNLOCK (self);
+
+  return self;
+}
diff --git a/transmitters/nice/fs-nice-thread.h b/transmitters/nice/fs-nice-thread.h
new file mode 100644
index 0000000..e858fb5
--- /dev/null
+++ b/transmitters/nice/fs-nice-thread.h
@@ -0,0 +1,98 @@
+/*
+ * Farsight2 - Farsight libnice Transmitter thread object
+ *
+ * Copyright 2007-2008 Collabora Ltd.
+ *  @author: Olivier Crete <olivier.crete at collabora.co.uk>
+ * Copyright 2007-2008 Nokia Corp.
+ *
+ * fs-nice-thread.h - A Farsight libnice transmitter thread object
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
+ */
+
+#ifndef __FS_NICE_THREAD_H__
+#define __FS_NICE_THREAD_H__
+
+#include <glib-object.h>
+#include <gst-libs/gst/farsight/fs-plugin.h>
+
+
+G_BEGIN_DECLS
+
+/* TYPE MACROS */
+#define FS_TYPE_NICE_THREAD \
+  (fs_nice_thread_get_type ())
+#define FS_NICE_THREAD(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST((obj), FS_TYPE_NICE_THREAD, \
+    FsNiceThread))
+#define FS_NICE_THREAD_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST((klass), FS_TYPE_NICE_THREAD, \
+    FsNiceThreadClass))
+#define FS_IS_NICE_THREAD(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE((obj), FS_TYPE_NICE_THREAD))
+#define FS_IS_NICE_THREAD_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_TYPE((klass), FS_TYPE_NICE_THREAD))
+#define FS_NICE_THREAD_GET_CLASS(obj) \
+  (G_TYPE_INSTANCE_GET_CLASS ((obj), FS_TYPE_NICE_THREAD, \
+    FsNiceThreadClass))
+#define FS_NICE_THREAD_CAST(obj) ((FsNiceThread *) (obj))
+
+typedef struct _FsNiceThread FsNiceThread;
+typedef struct _FsNiceThreadClass FsNiceThreadClass;
+typedef struct _FsNiceThreadPrivate FsNiceThreadPrivate;
+
+/**
+ * FsNiceThreadClass:
+ * @parent_class: Our parent
+ *
+ * The class structure
+ */
+
+struct _FsNiceThreadClass
+{
+  GObjectClass parent_class;
+};
+
+/**
+ * FsNiceThread:
+ *
+ * All members are private, access them using methods and properties
+ */
+struct _FsNiceThread
+{
+  GObject parent;
+
+  /*< private >*/
+  FsNiceThreadPrivate *priv;
+};
+
+
+GType fs_nice_thread_get_type (void);
+
+GMainContext *
+fs_nice_thread_get_context (FsNiceThread *self);
+
+void fs_nice_thread_add_weak_object (FsNiceThread *self,
+    GObject *object);
+
+FsNiceThread *fs_nice_thread_new (GError **error);
+
+
+GType
+fs_nice_thread_register_type (FsPlugin *module);
+
+G_END_DECLS
+
+#endif /* __FS_NICE_THREAD_H__ */
diff --git a/transmitters/nice/fs-nice-transmitter.c b/transmitters/nice/fs-nice-transmitter.c
index 2e4f07a..9920dfc 100644
--- a/transmitters/nice/fs-nice-transmitter.c
+++ b/transmitters/nice/fs-nice-transmitter.c
@@ -37,6 +37,7 @@
 
 #include "fs-nice-transmitter.h"
 #include "fs-nice-stream-transmitter.h"
+#include "fs-nice-thread.h"
 
 #include <gst/farsight/fs-conference-iface.h>
 #include <gst/farsight/fs-plugin.h>
@@ -165,6 +166,7 @@ fs_nice_transmitter_register_type (FsPlugin *module)
         "Farsight libnice transmitter");
 
   fs_nice_stream_transmitter_register_type (module);
+  fs_nice_thread_register_type (module);
 
   type = g_type_module_register_type (G_TYPE_MODULE (module),
     FS_TYPE_TRANSMITTER, "FsNiceTransmitter", &info, 0);
-- 
1.5.6.5




More information about the farsight-commits mailing list