[the-board] [tb] Add utils to start cheese camera asynchronously



commit 5acfeed1d2fc1097e2566b217c9e19ffb1e52a8a
Author: Lucas Rocha <lucasr gnome org>
Date:   Sat Apr 2 02:10:18 2011 +0100

    [tb] Add utils to start cheese camera asynchronously

 configure.ac            |    2 +
 src/Makefile-tb.am      |    3 ++
 src/tb/tb-cheese-util.c |   91 +++++++++++++++++++++++++++++++++++++++++++++++
 src/tb/tb-cheese-util.h |   15 ++++++++
 4 files changed, 111 insertions(+), 0 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 187629a..f970443 100644
--- a/configure.ac
+++ b/configure.ac
@@ -58,6 +58,7 @@ MX_MIN_VERSION=1.1.1
 GDK_PIXBUF_MIN_VERSION=2.22.1
 LIBNOTIFY_MIN_VERSION=0.7.1
 NAUTILUS_MIN_VERSION=2.91.3
+CHEESE_MIN_VERSION=2.91.93
 
 PKG_CHECK_MODULES(THE_BOARD,
                   glib-2.0 >= $GLIB_MIN_VERSION
@@ -77,6 +78,7 @@ PKG_CHECK_MODULES(TB,
                   clutter-1.0 >= $CLUTTER_MIN_VERSION
                   mx-1.0 >= $MX_MIN_VERSION
                   gtk+-3.0 >= $GTK_MIN_VERSION
+                  cheese >= $CHEESE_MIN_VERSION
                   gstreamer-0.10)
 
 AC_ARG_WITH([libnotify],
diff --git a/src/Makefile-tb.am b/src/Makefile-tb.am
index d467272..1ef4e37 100644
--- a/src/Makefile-tb.am
+++ b/src/Makefile-tb.am
@@ -20,6 +20,7 @@ THE_BOARD_STAMP_FILES = stamp-tb-marshal.h stamp-tb-enum-types.h
 
 tb_source_h = \
     tb/tb-box.h \
+    tb/tb-cheese-util.h \
     tb/tb-gio-util.h \
     tb/tb-gdk-util.h \
     tb/tb-gobject-util.h \
@@ -33,6 +34,7 @@ endif
 
 tb_source_c = \
     tb/tb-box.c \
+    tb/tb-cheese-util.c \
     tb/tb-gio-util.c \
     tb/tb-gdk-util.c \
     tb/tb-gobject-util.c \
@@ -95,6 +97,7 @@ TheBoard_1_0_gir_LIBS = libthe-board-1.0.la
 TheBoard_1_0_gir_CFLAGS = $(AM_CPPFLAGS) $(tb_cflags)
 TheBoard_1_0_gir_SCANNERFLAGS = --warn-all --symbol-prefix=tb --identifier-prefix=Tb
 TheBoard_1_0_gir_INCLUDES = \
+    Cheese-3.0 \
     Clutter-1.0 \
     GdkPixbuf-2.0 \
     Gtk-3.0 \
diff --git a/src/tb/tb-cheese-util.c b/src/tb/tb-cheese-util.c
new file mode 100644
index 0000000..6b26914
--- /dev/null
+++ b/src/tb/tb-cheese-util.c
@@ -0,0 +1,91 @@
+#include <glib.h>
+#include <gio/gio.h>
+#include <cheese/cheese-camera.h>
+
+#include "tb/tb-cheese-util.h"
+
+/* This is a saner way to use cheese_camera_setup()
+ * and cheese_camera_play asynchronously so that we
+ * never block the ui thread while webcam is loading */
+
+typedef struct {
+  CheeseCamera *camera;
+} CameraStartData;
+
+static void
+camera_start_data_free(void *data)
+{
+  CameraStartData *dsd;
+
+  dsd = data;
+
+  g_object_unref(dsd->camera);
+  g_slice_free(CameraStartData, dsd);
+}
+
+static void
+camera_start_async_thread(GSimpleAsyncResult *res,
+                          GObject            *object,
+                          GCancellable       *cancellable)
+{
+  CameraStartData *dsd;
+  GError *error;
+
+  error = NULL;
+
+  dsd = g_simple_async_result_get_op_res_gpointer(res);
+
+  cheese_camera_setup(dsd->camera, NULL, &error);
+
+  if (error != NULL)
+    {
+      g_simple_async_result_set_from_error(res, error);
+      g_error_free(error);
+      return;
+    }
+
+  cheese_camera_play(dsd->camera);
+}
+
+
+void
+tb_cheese_camera_start_async(CheeseCamera        *camera,
+                             GAsyncReadyCallback  callback)
+{
+  GSimpleAsyncResult *res;
+  CameraStartData *dsd;
+
+  dsd = g_slice_new0(CameraStartData);
+  dsd->camera = g_object_ref(camera);
+
+  res = g_simple_async_result_new(G_OBJECT(camera),
+                                  callback, NULL,
+                                  tb_cheese_camera_start_async);
+
+  g_simple_async_result_set_op_res_gpointer(res, dsd,
+                                            camera_start_data_free);
+
+  g_simple_async_result_run_in_thread(res,
+                                      camera_start_async_thread,
+                                      G_PRIORITY_DEFAULT,
+                                      NULL);
+
+  g_object_unref(res);
+}
+
+gboolean
+tb_cheese_camera_start_finish(CheeseCamera  *camera,
+                              GAsyncResult   *res,
+                              GError        **error)
+{
+  GSimpleAsyncResult *simple;
+
+  simple = G_SIMPLE_ASYNC_RESULT(res);
+
+  g_warn_if_fail(g_simple_async_result_get_source_tag(simple) == tb_cheese_camera_start_async);
+
+  if (g_simple_async_result_propagate_error(simple, error))
+    return FALSE;
+
+  return TRUE;
+}
diff --git a/src/tb/tb-cheese-util.h b/src/tb/tb-cheese-util.h
new file mode 100644
index 0000000..1029bed
--- /dev/null
+++ b/src/tb/tb-cheese-util.h
@@ -0,0 +1,15 @@
+#ifndef __TB_CHEESE_UTIL_H__
+#define __TB_CHEESE_UTIL_H__
+
+#include <glib.h>
+#include <gio/gio.h>
+#include <cheese/cheese-camera.h>
+
+void     tb_cheese_camera_start_async (CheeseCamera        *camera,
+                                       GAsyncReadyCallback  callback);
+
+gboolean tb_cheese_camera_start_finish  (CheeseCamera      *camera,
+                                         GAsyncResult      *res,
+                                         GError           **error);
+
+#endif /* __TB_CHEESE_UTIL_H__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]