[gthumb/ext: 12/18] start work on a video screenshot command



commit 6a4f9c12925ee9ed174223e510d5bbba0b5dbe4d
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Wed Nov 4 20:48:39 2009 +0100

    start work on a video screenshot command

 extensions/gstreamer/Makefile.am             |    2 +
 extensions/gstreamer/gstreamer-utils.c       |  128 +++++++++++++
 extensions/gstreamer/gstreamer-utils.h       |   16 ++-
 extensions/gstreamer/gstscreenshot.c         |  259 ++++++++++++++++++++++++++
 extensions/gstreamer/gstscreenshot.h         |   36 ++++
 extensions/gstreamer/gth-media-viewer-page.c |   43 ++++-
 gthumb/gtk-utils.c                           |    7 +-
 7 files changed, 480 insertions(+), 11 deletions(-)
---
diff --git a/extensions/gstreamer/Makefile.am b/extensions/gstreamer/Makefile.am
index 40b8f23..b7b1c92 100644
--- a/extensions/gstreamer/Makefile.am
+++ b/extensions/gstreamer/Makefile.am
@@ -8,6 +8,8 @@ extension_LTLIBRARIES = libgstreamer.la
 libgstreamer_la_SOURCES = 			\
 	gstreamer-utils.c			\
 	gstreamer-utils.h			\
+	gstscreenshot.c				\
+	gstscreenshot.h				\
 	gth-media-viewer-page.c			\
 	gth-media-viewer-page.h			\
 	gth-metadata-provider-gstreamer.c	\
diff --git a/extensions/gstreamer/gstreamer-utils.c b/extensions/gstreamer/gstreamer-utils.c
index b6c09a3..384ae98 100644
--- a/extensions/gstreamer/gstreamer-utils.c
+++ b/extensions/gstreamer/gstreamer-utils.c
@@ -45,6 +45,7 @@
 #include <gst/gst.h>
 #include <gthumb.h>
 #include "gstreamer-utils.h"
+#include "gstscreenshot.h"
 
 
 static gboolean gstreamer_initialized = FALSE;
@@ -660,3 +661,130 @@ gstreamer_read_metadata_from_file (GFile       *file,
 
 	return TRUE;
 }
+
+
+/* -- _gst_playbin_get_current_frame -- */
+
+/* this is voodoo code taken from totem, kudos to the authors, license is GPL */
+
+
+typedef struct {
+
+	GdkPixbuf          *pixbuf;
+	FrameReadyCallback  cb;
+	gpointer            user_data;
+} ScreenshotData;
+
+
+static void
+screenshot_data_finalize (ScreenshotData *data)
+{
+	if (data->cb != NULL)
+		data->cb (data->pixbuf, data->user_data);
+	g_free (data);
+}
+
+
+static void
+destroy_pixbuf (guchar *pix, gpointer data)
+{
+	gst_buffer_unref (GST_BUFFER (data));
+}
+
+
+static void
+get_current_frame_step2 (GstBuffer *buf,
+			 gpointer   user_data)
+{
+	ScreenshotData *data = user_data;
+	GstStructure   *s;
+	int             outwidth = 0;
+	int             outheight = 0;
+
+	if (buf == NULL) {
+		g_warning ("Could not take screenshot: %s", "conversion failed");
+		return screenshot_data_finalize (data);
+	}
+
+	if (GST_BUFFER_CAPS (buf) == NULL) {
+		g_warning ("Could not take screenshot: %s", "no caps on output buffer");
+		return screenshot_data_finalize (data);
+	}
+
+	s = gst_caps_get_structure (GST_BUFFER_CAPS (buf), 0);
+	gst_structure_get_int (s, "width", &outwidth);
+	gst_structure_get_int (s, "height", &outheight);
+
+	g_return_if_fail (outwidth > 0 && outheight > 0);
+
+	/* create pixbuf from that - use our own destroy function */
+
+	data->pixbuf = gdk_pixbuf_new_from_data (GST_BUFFER_DATA (buf),
+						 GDK_COLORSPACE_RGB,
+						 FALSE,
+						 8,
+						 outwidth,
+						 outheight,
+						 GST_ROUND_UP_4 (outwidth * 3),
+						 destroy_pixbuf,
+						 buf);
+	if (data->pixbuf == NULL) {
+		g_warning ("Could not take screenshot: %s", "could not create pixbuf");
+		gst_buffer_unref (buf);
+	}
+
+	screenshot_data_finalize (data);
+}
+
+
+gboolean
+_gst_playbin_get_current_frame (GstElement          *playbin,
+				int                  video_fps_n,
+				int                  video_fps_d,
+				FrameReadyCallback   cb,
+				gpointer             user_data)
+{
+	ScreenshotData *data;
+	GstBuffer      *buf;
+	GstCaps        *to_caps;
+
+	g_object_get (playbin, "frame", &buf, NULL);
+
+	if (buf == NULL) {
+		g_warning ("Could not take screenshot: %s", "no last video frame");
+		return FALSE;
+	}
+
+	if (GST_BUFFER_CAPS (buf) == NULL) {
+		g_warning ("Could not take screenshot: %s", "no caps on buffer");
+		return FALSE;
+	}
+
+	/* convert to our desired format (RGB24) */
+
+	data = g_new0 (ScreenshotData, 1);
+	data->cb = cb;
+	data->user_data = user_data;
+
+	to_caps = gst_caps_new_simple ("video/x-raw-rgb",
+				       "bpp", G_TYPE_INT, 24,
+				       "depth", G_TYPE_INT, 24,
+				       /* Note: we don't ask for a specific width/height here, so that
+				        * videoscale can adjust dimensions from a non-1/1 pixel aspect
+				        * ratio to a 1/1 pixel-aspect-ratio */
+				       "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
+				       "endianness", G_TYPE_INT, G_BIG_ENDIAN,
+				       "red_mask", G_TYPE_INT, 0xff0000,
+				       "green_mask", G_TYPE_INT, 0x00ff00,
+				       "blue_mask", G_TYPE_INT, 0x0000ff,
+				       NULL);
+
+	if (video_fps_n > 0 && video_fps_d > 0) {
+		gst_caps_set_simple (to_caps, "framerate",
+				     GST_TYPE_FRACTION, video_fps_n, video_fps_d,
+				     NULL);
+	}
+
+	return bvw_frame_conv_convert (buf, to_caps, get_current_frame_step2, data);
+}
+
diff --git a/extensions/gstreamer/gstreamer-utils.h b/extensions/gstreamer/gstreamer-utils.h
index db6eeae..033642d 100644
--- a/extensions/gstreamer/gstreamer-utils.h
+++ b/extensions/gstreamer/gstreamer-utils.h
@@ -25,14 +25,22 @@
 
 #include <glib.h>
 #include <gio/gio.h>
+#include <gst/gst.h>
 #include <gthumb.h>
 
 G_BEGIN_DECLS
 
-gboolean  gstreamer_init                    (void);
-gboolean  gstreamer_read_metadata_from_file (GFile       *file,
-					     GFileInfo   *info,
-					     GError     **error);
+typedef void (*FrameReadyCallback) (GdkPixbuf *, gpointer user_data);
+
+gboolean    gstreamer_init                    (void);
+gboolean    gstreamer_read_metadata_from_file (GFile               *file,
+					       GFileInfo           *info,
+					       GError             **error);
+gboolean    _gst_playbin_get_current_frame    (GstElement          *playbin,
+					       int                  video_fps_n,
+					       int                  video_fps_d,
+					       FrameReadyCallback   cb,
+					       gpointer             user_data);
 
 G_END_DECLS
 
diff --git a/extensions/gstreamer/gstscreenshot.c b/extensions/gstreamer/gstscreenshot.c
new file mode 100644
index 0000000..f7f177e
--- /dev/null
+++ b/extensions/gstreamer/gstscreenshot.c
@@ -0,0 +1,259 @@
+/* Small helper element for format conversion
+ * (c) 2004 Ronald Bultje <rbultje ronald bitfreak net>
+ * Portion Copyright © 2009 Nokia Corporation and/or its
+ * subsidiary(-ies).* All rights reserved. *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <gst/gst.h>
+#include <string.h>
+
+#include "gstscreenshot.h"
+
+typedef struct {
+	GstBuffer *result;
+	GstElement *src;
+	GstElement *sink;
+	GstElement *pipeline;
+	BvwFrameConvCb cb;
+	gpointer cb_data;
+} GstScreenshotData;
+
+/* GST_DEBUG_CATEGORY_EXTERN (_totem_gst_debug_cat); */
+/* #define GST_CAT_DEFAULT _totem_gst_debug_cat */
+
+static void feed_fakesrc(GstElement *src, GstBuffer *buf, GstPad *pad,
+			 gpointer data)
+{
+	GstBuffer *in_buf = GST_BUFFER(data);
+
+	g_assert(GST_BUFFER_SIZE(buf) >= GST_BUFFER_SIZE(in_buf));
+	g_assert(!GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_READONLY));
+
+	gst_buffer_set_caps(buf, GST_BUFFER_CAPS(in_buf));
+
+	memcpy(GST_BUFFER_DATA(buf), GST_BUFFER_DATA(in_buf),
+	       GST_BUFFER_SIZE(in_buf));
+
+	GST_BUFFER_SIZE(buf) = GST_BUFFER_SIZE(in_buf);
+
+	GST_DEBUG("feeding buffer %p, size %u, caps %" GST_PTR_FORMAT,
+		  buf, GST_BUFFER_SIZE(buf), GST_BUFFER_CAPS(buf));
+
+	gst_buffer_unref(in_buf);
+}
+
+static void save_result(GstElement *sink, GstBuffer *buf, GstPad *pad,
+			gpointer data)
+{
+	GstScreenshotData *gsd = data;
+
+	gsd->result = gst_buffer_ref(buf);
+
+	GST_DEBUG("received converted buffer %p with caps %" GST_PTR_FORMAT,
+		  gsd->result, GST_BUFFER_CAPS(gsd->result));
+}
+
+static gboolean create_element(const gchar *factory_name, GstElement **element,
+			       GError **err)
+{
+	*element = gst_element_factory_make(factory_name, NULL);
+	if (*element)
+		return TRUE;
+
+	if (err && *err == NULL) {
+		*err = g_error_new(
+			GST_CORE_ERROR, GST_CORE_ERROR_MISSING_PLUGIN,
+			"cannot create element '%s' - please check your "
+			"GStreamer installation", factory_name);
+	}
+
+	return FALSE;
+}
+
+static gboolean finalize_process(GstScreenshotData *gsd)
+{
+	g_signal_handlers_disconnect_matched(gsd->sink, (GSignalMatchType)
+					     G_SIGNAL_MATCH_FUNC, 0, 0, NULL,
+					     save_result, NULL);
+	g_signal_handlers_disconnect_matched(gsd->src, (GSignalMatchType)
+					     G_SIGNAL_MATCH_FUNC, 0, 0, NULL,
+					     feed_fakesrc, NULL);
+	gst_element_set_state(gsd->pipeline, GST_STATE_NULL);
+
+	g_free(gsd);
+
+	return FALSE;
+}
+
+static gboolean async_bus_handler(GstBus *bus, GstMessage *msg,
+				  gpointer data)
+{
+	GstScreenshotData *gsd = data;
+	gboolean keep_watch = TRUE;
+
+	switch (GST_MESSAGE_TYPE(msg)) {
+	case GST_MESSAGE_EOS: {
+		if (gsd->result != NULL) {
+			GST_DEBUG("conversion successful: result = %p",
+				  gsd->result);
+		} else {
+			GST_WARNING("EOS but no result frame?!");
+		}
+		gsd->cb(gsd->result, gsd->cb_data);
+		keep_watch = finalize_process(gsd);
+		break;
+	}
+	case GST_MESSAGE_ERROR: {
+		gchar *dbg = NULL;
+		GError *error = NULL;
+
+		gst_message_parse_error(msg, &error, &dbg);
+		if (error != NULL) {
+			g_warning("Could not take screenshot: %s",
+				  error->message);
+			GST_DEBUG("%s [debug: %s]", error->message,
+				  GST_STR_NULL(dbg));
+			g_error_free(error);
+		} else {
+			g_warning("Could not take screenshot(and "
+				  "NULL error!)");
+		}
+		g_free(dbg);
+		gsd->result = NULL;
+		gsd->cb(gsd->result, gsd->cb_data);
+		keep_watch = finalize_process(gsd);
+		break;
+	}
+	default:
+		break;
+	}
+
+	return keep_watch;
+}
+
+/* takes ownership of the input buffer */
+gboolean bvw_frame_conv_convert(GstBuffer *buf, GstCaps *to_caps,
+				BvwFrameConvCb cb, gpointer cb_data)
+{
+	static GstElement *src = NULL, *sink = NULL, *pipeline = NULL,
+		*filter1 = NULL, *filter2 = NULL;
+	static GstBus *bus;
+	GError *error = NULL;
+	GstCaps *to_caps_no_par;
+	GstScreenshotData *gsd;
+
+	g_return_val_if_fail(GST_BUFFER_CAPS(buf) != NULL, FALSE);
+	g_return_val_if_fail(cb != NULL, FALSE);
+
+	if (pipeline == NULL) {
+		GstElement *csp, *vscale;
+
+		pipeline = gst_pipeline_new("screenshot-pipeline");
+		if(pipeline == NULL) {
+			g_warning("Could not take screenshot: "
+				  "no pipeline (unknown error)");
+			return FALSE;
+		}
+
+		/* videoscale is here to correct for the
+		 * pixel-aspect-ratio for us */
+		GST_DEBUG("creating elements");
+		if(!create_element("fakesrc", &src, &error) ||
+		   !create_element("ffmpegcolorspace", &csp, &error) ||
+		   !create_element("videoscale", &vscale, &error) ||
+		   !create_element("capsfilter", &filter1, &error) ||
+		   !create_element("capsfilter", &filter2, &error) ||
+		   !create_element("fakesink", &sink, &error)) {
+			g_warning("Could not take screenshot: %s",
+				  error->message);
+			g_error_free(error);
+			return FALSE;
+		}
+
+		GST_DEBUG("adding elements");
+		gst_bin_add_many(GST_BIN(pipeline), src, csp, filter1, vscale,
+				 filter2, sink, NULL);
+
+		g_object_set(sink, "preroll-queue-len", 1,
+			     "signal-handoffs", TRUE, NULL);
+
+		/* set to 'fixed' sizetype */
+		g_object_set(src, "sizetype", 2, "num-buffers", 1,
+			     "signal-handoffs", TRUE, NULL);
+
+		/* FIXME: linking is still way too expensive, profile
+		 * this properly */
+		GST_DEBUG("linking src->csp");
+		if(!gst_element_link_pads(src, "src", csp, "sink"))
+			return FALSE;
+
+		GST_DEBUG("linking csp->filter1");
+		if(!gst_element_link_pads(csp, "src", filter1, "sink"))
+			return FALSE;
+
+		GST_DEBUG("linking filter1->vscale");
+		if(!gst_element_link_pads(filter1, "src", vscale, "sink"))
+			return FALSE;
+
+		GST_DEBUG("linking vscale->capsfilter");
+		if(!gst_element_link_pads(vscale, "src", filter2, "sink"))
+			return FALSE;
+
+		GST_DEBUG("linking capsfilter->sink");
+		if(!gst_element_link_pads(filter2, "src", sink, "sink"))
+			return FALSE;
+
+		bus = gst_element_get_bus(pipeline);
+	}
+
+	/* adding this superfluous capsfilter makes linking cheaper */
+	to_caps_no_par = gst_caps_copy(to_caps);
+	gst_structure_remove_field(gst_caps_get_structure(to_caps_no_par, 0),
+				   "pixel-aspect-ratio");
+	g_object_set(filter1, "caps", to_caps_no_par, NULL);
+	gst_caps_unref(to_caps_no_par);
+
+	g_object_set(filter2, "caps", to_caps, NULL);
+	gst_caps_unref(to_caps);
+
+	gsd = g_new0(GstScreenshotData, 1);
+
+	gsd->src = src;
+	gsd->sink = sink;
+	gsd->pipeline = pipeline;
+	gsd->cb = cb;
+	gsd->cb_data = cb_data;
+
+	g_signal_connect(sink, "handoff", G_CALLBACK(save_result), gsd);
+
+	g_signal_connect(src, "handoff", G_CALLBACK(feed_fakesrc), buf);
+
+	gst_bus_add_watch(bus, async_bus_handler, gsd);
+
+	/* set to 'fixed' sizetype */
+	g_object_set(src, "sizemax", GST_BUFFER_SIZE(buf), NULL);
+
+	GST_DEBUG("running conversion pipeline");
+	gst_element_set_state(pipeline, GST_STATE_PLAYING);
+
+	return TRUE;
+}
diff --git a/extensions/gstreamer/gstscreenshot.h b/extensions/gstreamer/gstscreenshot.h
new file mode 100644
index 0000000..d3cf23c
--- /dev/null
+++ b/extensions/gstreamer/gstscreenshot.h
@@ -0,0 +1,36 @@
+/* Small helper element for format conversion
+ * (c) 2004 Ronald Bultje <rbultje ronald bitfreak net>
+ * Portion Copyright © 2009 Nokia Corporation and/or its
+ * subsidiary(-ies).* All rights reserved. *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __BVW_FRAME_CONV_H__
+#define __BVW_FRAME_CONV_H__
+
+#include <gst/gst.h>
+
+G_BEGIN_DECLS
+
+typedef void (*BvwFrameConvCb)(GstBuffer *result, gpointer user_data);
+
+gboolean bvw_frame_conv_convert (GstBuffer *buf, GstCaps *to,
+				 BvwFrameConvCb cb, gpointer cb_data);
+
+G_END_DECLS
+
+#endif /* __BVW_FRAME_CONV_H__ */
diff --git a/extensions/gstreamer/gth-media-viewer-page.c b/extensions/gstreamer/gth-media-viewer-page.c
index 47733be..36b7c0e 100644
--- a/extensions/gstreamer/gth-media-viewer-page.c
+++ b/extensions/gstreamer/gth-media-viewer-page.c
@@ -72,15 +72,45 @@ static const char *media_viewer_ui_info =
 
 
 static void
+screenshot_ready_cb (GdkPixbuf *pixbuf,
+		     gpointer   user_data)
+{
+	GthMediaViewerPage *self = user_data;
+	GtkWidget          *image;
+	GtkWidget          *window;
+
+	if (pixbuf == NULL) {
+		_gtk_error_dialog_from_gerror_show (GTK_WINDOW (self->priv->browser), _("Could not take a screenshot"), NULL);
+		return;
+	}
+
+	image = gtk_image_new_from_pixbuf (pixbuf);
+	gtk_widget_show (image);
+	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+	gtk_container_add (GTK_CONTAINER (window), image);
+	gtk_window_present (GTK_WINDOW (window));
+
+	g_object_unref (pixbuf);
+}
+
+
+static void
 media_viewer_activate_action_screenshot (GtkAction          *action,
 				         GthMediaViewerPage *self)
 {
-	/* FIXME */
+	if (self->priv->playbin == NULL)
+		return;
+
+	_gst_playbin_get_current_frame (self->priv->playbin,
+					0 /*self->priv->video_fps_n*/,
+					0 /*self->priv->video_fps_d*/,
+					screenshot_ready_cb,
+					self);
 }
 
 
 static GtkActionEntry media_viewer_action_entries[] = {
-	{ "MediaViewer_Screenshot", "screenshot",
+	{ "MediaViewer_Screenshot", "camera-photo",
 	  N_("Screenshot"), NULL,
 	  N_("Take a screenshot"),
 	  G_CALLBACK (media_viewer_activate_action_screenshot) },
@@ -307,7 +337,7 @@ update_current_position_bar (GthMediaViewerPage *self)
         if (gst_element_query_position (self->priv->playbin, &format, &current_value)) {
         	char *s;
 
-        	if (self->priv->duration == 0) {
+        	if (self->priv->duration <= 0) {
         		gst_element_query_duration (self->priv->playbin, &format, &self->priv->duration);
         		s = _g_format_duration_for_display (GST_TIME_AS_MSECONDS (self->priv->duration));
         		gtk_label_set_text (GTK_LABEL (GET_WIDGET ("label_duration")), s);
@@ -315,6 +345,13 @@ update_current_position_bar (GthMediaViewerPage *self)
         		g_free (s);
         	}
 
+        	/*
+        	g_print ("==> %" G_GINT64_FORMAT " / %" G_GINT64_FORMAT " (%0.3g)\n" ,
+        		 current_value,
+        		 self->priv->duration,
+        		 ((double) current_value / self->priv->duration) * 100.0);
+        	*/
+
         	g_signal_handlers_block_by_func(GET_WIDGET ("adjustment_position"), position_value_changed_cb, self);
         	gtk_adjustment_set_value (GTK_ADJUSTMENT (GET_WIDGET ("adjustment_position")), (self->priv->duration > 0) ? ((double) current_value / self->priv->duration) * 100.0 : 0.0);
         	g_signal_handlers_unblock_by_func(GET_WIDGET ("adjustment_position"), position_value_changed_cb, self);
diff --git a/gthumb/gtk-utils.c b/gthumb/gtk-utils.c
index 590bf41..122aecd 100644
--- a/gthumb/gtk-utils.c
+++ b/gthumb/gtk-utils.c
@@ -587,20 +587,19 @@ _gtk_error_dialog_from_gerror_show (GtkWindow   *parent,
 {
 	GtkWidget *d;
 
-	g_return_if_fail (*gerror != NULL);
-
 	d = _gtk_message_dialog_new (parent,
 				     GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
 				     GTK_STOCK_DIALOG_ERROR,
 				     title,
-				     (*gerror)->message,
+				     (gerror != NULL) ? (*gerror)->message : NULL,
 				     GTK_STOCK_OK, GTK_RESPONSE_OK,
 				     NULL);
 	g_signal_connect (d, "response", G_CALLBACK (error_dialog_response_cb), NULL);
 
 	gtk_window_present (GTK_WINDOW (d));
 
-	g_clear_error (gerror);
+	if (gerror != NULL)
+		g_clear_error (gerror);
 }
 
 



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