[totem] main: Add async thumbnailing helper



commit 87afdef288e273214484528257e101c74ac2d226
Author: Bastien Nocera <hadess hadess net>
Date:   Mon Aug 25 18:03:03 2014 +0200

    main: Add async thumbnailing helper
    
    To thumbnail videos using totem-video-thumbnailer through gnome-desktop,
    using a GThreadPool to make sure we don't request more than 5 thumbnails
    at a time.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=727748

 configure.ac       |    2 +-
 src/icon-helpers.c |   71 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+), 1 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 0bdec27..8792399 100644
--- a/configure.ac
+++ b/configure.ac
@@ -235,7 +235,7 @@ AM_CONDITIONAL([ENABLE_PYTHON],[test "x$enable_python" = "xyes"])
 # Player requirements
 #================================================================
 
-PKG_CHECK_MODULES(PLAYER, $BACKEND_MODULES glib-2.0 >= $GLIB_REQS gio-2.0 >= $GIO_REQS gtk+-3.0 >= $GTK_REQS 
gdk-x11-3.0 >= $GTK_REQS gmodule-2.0 totem-plparser >= $TOTEM_PLPARSER_REQS libpeas-1.0 >= $PEAS_REQS 
libpeas-gtk-1.0 >= $PEAS_REQS $PYTHON_MODULES grilo-0.2 >= $GRILO_REQS grilo-pls-0.2 >= $GRILO_PLS_REQS)
+PKG_CHECK_MODULES(PLAYER, $BACKEND_MODULES glib-2.0 >= $GLIB_REQS gio-2.0 >= $GIO_REQS gtk+-3.0 >= $GTK_REQS 
gdk-x11-3.0 >= $GTK_REQS gmodule-2.0 totem-plparser >= $TOTEM_PLPARSER_REQS libpeas-1.0 >= $PEAS_REQS 
libpeas-gtk-1.0 >= $PEAS_REQS $PYTHON_MODULES grilo-0.2 >= $GRILO_REQS grilo-pls-0.2 >= $GRILO_PLS_REQS 
gnome-desktop-3.0)
 PKG_CHECK_MODULES(LIBPLAYER, glib-2.0 >= $GLIB_REQS gio-2.0 >= $GIO_REQS gtk+-3.0 >= $GTK_REQS gdk-x11-3.0 
= $GTK_REQS clutter-gtk-1.0)
 PKG_CHECK_MODULES(HELPER, gstreamer-1.0 gstreamer-tag-1.0)
 PKG_CHECK_MODULES(TIME_HELPER, glib-2.0)
diff --git a/src/icon-helpers.c b/src/icon-helpers.c
index 059813c..06c28c1 100644
--- a/src/icon-helpers.c
+++ b/src/icon-helpers.c
@@ -27,6 +27,9 @@
 
 #include <icon-helpers.h>
 
+#define GNOME_DESKTOP_USE_UNSTABLE_API 1
+#include <libgnome-desktop/gnome-desktop-thumbnail.h>
+
 #define THUMB_SEARCH_SIZE     256
 #define THUMB_SEARCH_HEIGHT   (THUMB_SEARCH_SIZE / 4 * 3)
 
@@ -38,6 +41,8 @@ typedef enum {
        NUM_ICONS
 } IconType;
 
+static GnomeDesktopThumbnailFactory *factory;
+static GThreadPool *thumbnail_pool;
 static GdkPixbuf *icons[NUM_ICONS];
 static GHashTable *cache_thumbnails; /* key=url, value=GdkPixbuf */
 
@@ -114,6 +119,66 @@ get_stream_thumbnail_cb (GObject *source_object,
        g_object_unref (G_OBJECT (stream));
 }
 
+static void
+thumbnail_media_async_thread (GTask    *task,
+                             gpointer  user_data)
+{
+       GrlMedia *media;
+       GdkPixbuf *pixbuf;
+       const char *uri;
+       GDateTime *mtime;
+
+       if (g_task_return_error_if_cancelled (task)) {
+               g_object_unref (task);
+               return;
+       }
+
+       media = GRL_MEDIA (g_task_get_source_object (task));
+       uri = grl_media_get_url (media);
+       mtime = grl_media_get_modification_date (media);
+
+       if (!uri || !mtime) {
+               g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED, "URI or mtime missing");
+               g_object_unref (task);
+               return;
+       }
+
+       pixbuf = gnome_desktop_thumbnail_factory_generate_thumbnail (factory, uri, "video/x-totem-stream");
+
+       if (!pixbuf) {
+               g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED, "Thumbnailing failed");
+               g_object_unref (task);
+               return;
+       }
+
+       gnome_desktop_thumbnail_factory_save_thumbnail (factory, pixbuf, uri, g_date_time_to_unix (mtime));
+       g_task_return_pointer (task, pixbuf, g_object_unref);
+       g_object_unref (task);
+}
+
+static void
+totem_grilo_thumbnail_media (GrlMedia            *media,
+                            GCancellable        *cancellable,
+                            GAsyncReadyCallback  callback,
+                            gpointer             user_data)
+{
+       GTask *task;
+
+       task = g_task_new (media, cancellable, callback, user_data);
+       g_task_set_priority (task, G_PRIORITY_LOW);
+       g_thread_pool_push (thumbnail_pool, task, NULL);
+}
+
+static GdkPixbuf *
+totem_grilo_thumbnail_media_finish (GrlMedia      *media,
+                                   GAsyncResult  *res,
+                                   GError       **error)
+{
+       g_return_val_if_fail (g_task_is_valid (res, media), NULL);
+
+       return g_task_propagate_pointer (G_TASK (res), error);
+}
+
 void
 totem_grilo_get_thumbnail (GObject             *object,
                           GCancellable        *cancellable,
@@ -276,6 +341,9 @@ totem_grilo_clear_icons (void)
                g_clear_object (&icons[i]);
 
        g_clear_pointer (&cache_thumbnails, g_hash_table_destroy);
+       g_clear_object (&factory);
+       g_thread_pool_free (thumbnail_pool, TRUE, FALSE);
+       thumbnail_pool = NULL;
 }
 
 void
@@ -290,4 +358,7 @@ totem_grilo_setup_icons (Totem *totem)
                                                  g_str_equal,
                                                  g_free,
                                                  g_object_unref);
+
+       factory = gnome_desktop_thumbnail_factory_new (GNOME_DESKTOP_THUMBNAIL_SIZE_LARGE);
+       thumbnail_pool = g_thread_pool_new ((GFunc) thumbnail_media_async_thread, NULL, 5, TRUE, NULL);
 }


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