Implement gtk sinks

two sinks are provided.  gtksink which is a cairo/software based renderer
and gtkglsink which utilises the GL support in gtk and gstreamer.
This commit is contained in:
Matthew Waters 2014-12-18 17:00:30 +11:00
parent 898b436219
commit 23fb666dd7
17 changed files with 2423 additions and 3 deletions

View file

@ -2585,6 +2585,27 @@ AG_GST_CHECK_FEATURE(GL, [gl elements], gl, [
fi
])
dnl *** gtk+ ***
HAVE_GTK3_GL="no"
translit(dnm, m, l) AM_CONDITIONAL(USE_GTK3, true)
AG_GST_CHECK_FEATURE(GTK3, [Gtk+ elements], gtk, [
PKG_CHECK_MODULES(GTK3, gtk+-3.0, [
AC_DEFINE([HAVE_GTK3], 1, [Define if Gtk+ 3.0 is installed])
HAVE_GTK3="yes"
], [
HAVE_GTK3="no"
])
PKG_CHECK_MODULES(GTK3_GL, gtk+-3.0 >= 3.15.0, [
AC_DEFINE([HAVE_GTK3_GL], 1, [Define if Gtk+ 3.0 GL is installed])
if test "x$HAVE_GL" = "xyes"; then
HAVE_GTK3_GL="yes"
fi
], [
HAVE_GTK3_GL="no"
])
])
AM_CONDITIONAL(USE_GTK3_GL, test "x$HAVE_GTK3_GL" = "xyes")
dnl *** libvisual ***
translit(dnm, m, l) AM_CONDITIONAL(USE_LIBVISUAL, true)
AG_GST_CHECK_FEATURE(LIBVISUAL, [libvisual visualization library], libvisual, [
@ -2989,6 +3010,8 @@ AM_CONDITIONAL(USE_FLITE, false)
AM_CONDITIONAL(USE_FLUIDSYNTH, false)
AM_CONDITIONAL(USE_GL, false)
AM_CONDITIONAL(USE_GSM, false)
AM_CONDITIONAL(USE_GTK3, false)
AM_CONDITIONAL(USE_GTK3_GL, false)
AM_CONDITIONAL(USE_HLS, false)
AM_CONDITIONAL(USE_KATE, false)
AM_CONDITIONAL(USE_TIGER, false)
@ -3268,6 +3291,7 @@ tests/examples/gl/gtk/fxtest/Makefile
tests/examples/gl/gtk/switchvideooverlay/Makefile
tests/examples/gl/qt/Makefile
tests/examples/gl/sdl/Makefile
tests/examples/gtk/Makefile
tests/examples/mpegts/Makefile
tests/examples/mxf/Makefile
tests/examples/opencv/Makefile
@ -3288,6 +3312,7 @@ ext/wayland/Makefile
ext/daala/Makefile
ext/dts/Makefile
ext/gl/Makefile
ext/gtk/Makefile
ext/faac/Makefile
ext/faad/Makefile
ext/flite/Makefile

View file

@ -88,6 +88,12 @@ else
GL_DIR=
endif
if USE_GTK3
GTK3_DIR=gtk
else
GTK3_DIR=
endif
if USE_RESINDVD
RESINDVD_DIR = resindvd
else
@ -436,6 +442,7 @@ SUBDIRS=\
$(DTS_DIR) \
$(RESINDVD_DIR) \
$(GL_DIR) \
$(GTK3_DIR) \
$(FAAC_DIR) \
$(FAAD_DIR) \
$(FLITE_DIR) \
@ -516,6 +523,7 @@ DIST_SUBDIRS = \
daala \
dts \
gl \
gtk \
modplug \
mimic \
mpeg2enc \

43
ext/gtk/Makefile.am Normal file
View file

@ -0,0 +1,43 @@
# preamble
NULL =
BUILT_SOURCES =
CLEANFILES =
EXTRA_DIST =
DISTCLEANFILES =
lib_LTLIBRARIES =
# source
sources = \
gtkgstwidget.c \
gtkgstwidget.h \
gstgtksink.c \
gstgtksink.h \
gstplugin.c \
$(NULL)
libgstgtksink_la_CFLAGS = \
-I$(top_srcdir)/gst-libs \
-I$(top_builddir)/gst-libs \
$(GTK3_CFLAGS) \
$(GST_CFLAGS) \
$(GST_BASE_CFLAGS) \
$(GST_PLUGINS_BASE_CFLAGS)
libgstgtksink_la_LIBADD = \
$(GTK3_LIBS) \
$(GST_BASE_LIBS) \
$(GST_PLUGINS_BASE_LIBS) \
-lgstvideo-$(GST_API_VERSION)
libgstgtksink_la_SOURCES = $(sources)
libgstgtksink_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
libgstgtksink_la_LIBTOOLFLAGS = $(GST_PLUGIN_LIBTOOLFLAGS)
if USE_GTK3_GL
if USE_GL
libgstgtksink_la_SOURCES += gstgtkglsink.c gstgtkglsink.h gtkgstglwidget.c gtkgstglwidget.h
libgstgtksink_la_LIBADD += \
$(top_builddir)/gst-libs/gst/gl/libgstgl-$(GST_API_VERSION).la
endif
endif
plugin_LTLIBRARIES = libgstgtksink.la

435
ext/gtk/gstgtkglsink.c Normal file
View file

@ -0,0 +1,435 @@
/*
* GStreamer
* Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
*
* 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/**
* SECTION:gstgtkglsink
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstgtkglsink.h"
GST_DEBUG_CATEGORY (gst_debug_gtk_gl_sink);
#define GST_CAT_DEFAULT gst_debug_gtk_gl_sink
static void gst_gtk_gl_sink_finalize (GObject * object);
static void gst_gtk_gl_sink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * param_spec);
static void gst_gtk_gl_sink_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * param_spec);
static gboolean gst_gtk_gl_sink_stop (GstBaseSink * bsink);
static gboolean gst_gtk_gl_sink_query (GstBaseSink * bsink, GstQuery * query);
static GstStateChangeReturn
gst_gtk_gl_sink_change_state (GstElement * element, GstStateChange transition);
static void gst_gtk_gl_sink_get_times (GstBaseSink * bsink, GstBuffer * buf,
GstClockTime * start, GstClockTime * end);
static gboolean gst_gtk_gl_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
static GstFlowReturn gst_gtk_gl_sink_show_frame (GstVideoSink * bsink,
GstBuffer * buf);
static gboolean gst_gtk_gl_sink_propose_allocation (GstBaseSink * bsink,
GstQuery * query);
static GstStaticPadTemplate gst_gtk_gl_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
(GST_CAPS_FEATURE_MEMORY_GL_MEMORY, "RGBA")));
enum
{
ARG_0,
PROP_WIDGET
};
enum
{
SIGNAL_0,
LAST_SIGNAL
};
#define gst_gtk_gl_sink_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstGtkGLSink, gst_gtk_gl_sink,
GST_TYPE_VIDEO_SINK, GST_DEBUG_CATEGORY_INIT (gst_debug_gtk_gl_sink,
"gtkglsink", 0, "Gtk Video Sink"));
static void
gst_gtk_gl_sink_class_init (GstGtkGLSinkClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
GstBaseSinkClass *gstbasesink_class;
GstVideoSinkClass *gstvideosink_class;
gobject_class = (GObjectClass *) klass;
gstelement_class = (GstElementClass *) klass;
gstbasesink_class = (GstBaseSinkClass *) klass;
gstvideosink_class = (GstVideoSinkClass *) klass;
gobject_class->set_property = gst_gtk_gl_sink_set_property;
gobject_class->get_property = gst_gtk_gl_sink_get_property;
gst_element_class_set_metadata (gstelement_class, "Gtk Video Sink",
"Sink/Video", "A video sink the renders to a GtkWidget",
"Matthew Waters <matthew@centricular.com>");
g_object_class_install_property (gobject_class, PROP_WIDGET,
g_param_spec_object ("widget", "Gtk Widget",
"The GtkWidget to place in the widget heirachy",
GTK_TYPE_WIDGET, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&gst_gtk_gl_sink_template));
gobject_class->finalize = gst_gtk_gl_sink_finalize;
gstelement_class->change_state = gst_gtk_gl_sink_change_state;
gstbasesink_class->query = gst_gtk_gl_sink_query;
gstbasesink_class->set_caps = gst_gtk_gl_sink_set_caps;
gstbasesink_class->get_times = gst_gtk_gl_sink_get_times;
gstbasesink_class->propose_allocation = gst_gtk_gl_sink_propose_allocation;
gstbasesink_class->stop = gst_gtk_gl_sink_stop;
gstvideosink_class->show_frame = gst_gtk_gl_sink_show_frame;
}
static void
gst_gtk_gl_sink_init (GstGtkGLSink * gtk_sink)
{
gtk_sink->widget = (GtkGstGLWidget *) gtk_gst_gl_widget_new ();
}
static void
gst_gtk_gl_sink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
switch (prop_id) {
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
_reset (GstGtkGLSink * gtk_sink)
{
if (gtk_sink->display) {
gst_object_unref (gtk_sink->display);
gtk_sink->display = NULL;
}
if (gtk_sink->context) {
gst_object_unref (gtk_sink->context);
gtk_sink->context = NULL;
}
if (gtk_sink->gtk_context) {
gst_object_unref (gtk_sink->gtk_context);
gtk_sink->gtk_context = NULL;
}
}
static void
gst_gtk_gl_sink_finalize (GObject * object)
{
GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (object);;
_reset (gtk_sink);
g_object_unref (gtk_sink->widget);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gst_gtk_gl_sink_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstGtkGLSink *gtk_sink;
gtk_sink = GST_GTK_GL_SINK (object);
switch (prop_id) {
case PROP_WIDGET:
g_value_set_object (value, gtk_sink->widget);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static gboolean
gst_gtk_gl_sink_query (GstBaseSink * bsink, GstQuery * query)
{
GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (bsink);
gboolean res = FALSE;
switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_CONTEXT:
{
const gchar *context_type;
GstContext *context, *old_context;
gboolean ret;
ret = gst_gl_handle_context_query ((GstElement *) gtk_sink, query,
&gtk_sink->display, &gtk_sink->gtk_context);
if (gtk_sink->display)
gst_gl_display_filter_gl_api (gtk_sink->display, GST_GL_API_OPENGL3);
gst_query_parse_context_type (query, &context_type);
if (g_strcmp0 (context_type, "gst.gl.local_context") == 0) {
GstStructure *s;
gst_query_parse_context (query, &old_context);
if (old_context)
context = gst_context_copy (old_context);
else
context = gst_context_new ("gst.gl.local_context", FALSE);
s = gst_context_writable_structure (context);
gst_structure_set (s, "context", GST_GL_TYPE_CONTEXT, gtk_sink->context,
NULL);
gst_query_set_context (query, context);
gst_context_unref (context);
ret = gtk_sink->context != NULL;
}
GST_LOG_OBJECT (gtk_sink, "context query of type %s %i", context_type,
ret);
if (ret)
return ret;
}
default:
res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
break;
}
return res;
}
static gboolean
gst_gtk_gl_sink_stop (GstBaseSink * bsink)
{
return TRUE;
}
static GstStateChangeReturn
gst_gtk_gl_sink_change_state (GstElement * element, GstStateChange transition)
{
GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (element);
GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
GST_DEBUG ("changing state: %s => %s",
gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
if (!gtk_gst_gl_widget_init_winsys (gtk_sink->widget))
return GST_STATE_CHANGE_FAILURE;
gtk_sink->display = gtk_gst_gl_widget_get_display (gtk_sink->widget);
gtk_sink->context = gtk_gst_gl_widget_get_context (gtk_sink->widget);
gtk_sink->gtk_context =
gtk_gst_gl_widget_get_gtk_context (gtk_sink->widget);
if (!gtk_sink->display || !gtk_sink->context || !gtk_sink->gtk_context)
return GST_STATE_CHANGE_FAILURE;
break;
case GST_STATE_CHANGE_READY_TO_PAUSED:
break;
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
break;
default:
break;
}
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
if (ret == GST_STATE_CHANGE_FAILURE)
return ret;
switch (transition) {
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
break;
case GST_STATE_CHANGE_PAUSED_TO_READY:
gtk_gst_gl_widget_set_buffer (gtk_sink->widget, NULL);
break;
case GST_STATE_CHANGE_READY_TO_NULL:
break;
default:
break;
}
return ret;
}
static void
gst_gtk_gl_sink_get_times (GstBaseSink * bsink, GstBuffer * buf,
GstClockTime * start, GstClockTime * end)
{
GstGtkGLSink *gtk_sink;
gtk_sink = GST_GTK_GL_SINK (bsink);
if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
*start = GST_BUFFER_TIMESTAMP (buf);
if (GST_BUFFER_DURATION_IS_VALID (buf))
*end = *start + GST_BUFFER_DURATION (buf);
else {
if (GST_VIDEO_INFO_FPS_N (&gtk_sink->v_info) > 0) {
*end = *start +
gst_util_uint64_scale_int (GST_SECOND,
GST_VIDEO_INFO_FPS_D (&gtk_sink->v_info),
GST_VIDEO_INFO_FPS_N (&gtk_sink->v_info));
}
}
}
}
gboolean
gst_gtk_gl_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
{
GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (bsink);
GST_DEBUG ("set caps with %" GST_PTR_FORMAT, caps);
_reset (gtk_sink);
if (!gst_video_info_from_caps (&gtk_sink->v_info, caps))
return FALSE;
if (!gtk_gst_gl_widget_set_caps (gtk_sink->widget, caps))
return FALSE;
return TRUE;
}
static GstFlowReturn
gst_gtk_gl_sink_show_frame (GstVideoSink * vsink, GstBuffer * buf)
{
GstGtkGLSink *gtk_sink;
GST_TRACE ("rendering buffer:%p", buf);
gtk_sink = GST_GTK_GL_SINK (vsink);
gtk_gst_gl_widget_set_buffer (gtk_sink->widget, buf);
return GST_FLOW_OK;
}
static gboolean
gst_gtk_gl_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
{
GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (bsink);
GstBufferPool *pool;
GstStructure *config;
GstCaps *caps;
guint size;
gboolean need_pool;
if (!gtk_sink->display || !gtk_sink->context)
return FALSE;
gst_query_parse_allocation (query, &caps, &need_pool);
if (caps == NULL)
goto no_caps;
if ((pool = gtk_sink->pool))
gst_object_ref (pool);
if (pool != NULL) {
GstCaps *pcaps;
/* we had a pool, check caps */
GST_DEBUG_OBJECT (gtk_sink, "check existing pool caps");
config = gst_buffer_pool_get_config (pool);
gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
if (!gst_caps_is_equal (caps, pcaps)) {
GST_DEBUG_OBJECT (gtk_sink, "pool has different caps");
/* different caps, we can't use this pool */
gst_object_unref (pool);
pool = NULL;
}
gst_structure_free (config);
}
if (pool == NULL && need_pool) {
GstVideoInfo info;
if (!gst_video_info_from_caps (&info, caps))
goto invalid_caps;
GST_DEBUG_OBJECT (gtk_sink, "create new pool");
pool = gst_gl_buffer_pool_new (gtk_sink->context);
/* the normal size of a frame */
size = info.size;
config = gst_buffer_pool_get_config (pool);
gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
if (!gst_buffer_pool_set_config (pool, config))
goto config_failed;
}
/* we need at least 2 buffer because we hold on to the last one */
if (pool) {
gst_query_add_allocation_pool (query, pool, size, 2, 0);
gst_object_unref (pool);
}
/* we also support various metadata */
gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, 0);
if (gtk_sink->context->gl_vtable->FenceSync)
gst_query_add_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, 0);
return TRUE;
/* ERRORS */
no_caps:
{
GST_DEBUG_OBJECT (bsink, "no caps specified");
return FALSE;
}
invalid_caps:
{
GST_DEBUG_OBJECT (bsink, "invalid caps specified");
return FALSE;
}
config_failed:
{
GST_DEBUG_OBJECT (bsink, "failed setting config");
return FALSE;
}
}

86
ext/gtk/gstgtkglsink.h Normal file
View file

@ -0,0 +1,86 @@
/*
* GStreamer
* Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
*
* 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_GTK_GL_SINK_H__
#define __GST_GTK_GL_SINK_H__
#include <gtk/gtk.h>
#include <gst/gst.h>
#include <gst/video/gstvideosink.h>
#include <gst/video/video.h>
#include <gst/gl/gl.h>
typedef struct _GstGtkGLSink GstGtkGLSink;
typedef struct _GstGtkGLSinkClass GstGtkGLSinkClass;
typedef struct _GstGtkGLSinkPrivate GstGtkGLSinkPrivate;
#include <gtkgstglwidget.h>
G_BEGIN_DECLS
GType gst_gtk_gl_sink_get_type (void);
#define GST_TYPE_GTK_GL_SINK (gst_gtk_gl_sink_get_type())
#define GST_GTK_GL_SINK(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_GTK_GL_SINK,GstGtkGLSink))
#define GST_GTK_GL_SINK_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_GTK_GL_SINK,GstGtkGLSinkClass))
#define GST_IS_GTK_GL_SINK(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_GTK_GL_SINK))
#define GST_IS_GTK_GL_SINK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_GTK_GL_SINK))
#define GST_GTK_GL_SINK_CAST(obj) ((GstGtkGLSink*)(obj))
/**
* GstGtkGLSink:
*
* Opaque #GstGtkGLSink object
*/
struct _GstGtkGLSink
{
/* <private> */
GstVideoSink parent;
GtkGstGLWidget *widget;
GstVideoInfo v_info;
GstBufferPool *pool;
GstGLDisplay *display;
GstGLContext *context;
GstGLContext *gtk_context;
GstGLUpload *upload;
GstBuffer *uploaded_buffer;
GstGtkGLSinkPrivate *priv;
};
/**
* GstGtkGLSinkClass:
*
* The #GstGtkGLSinkClass struct only contains private data
*/
struct _GstGtkGLSinkClass
{
/* <private> */
GstVideoSinkClass object_class;
};
GstGtkGLSink * gst_gtk_gl_sink_new (void);
G_END_DECLS
#endif /* __GST_GTK_GL_SINK_H__ */

274
ext/gtk/gstgtksink.c Normal file
View file

@ -0,0 +1,274 @@
/*
* GStreamer
* Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
*
* 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/**
* SECTION:gtkgstsink
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstgtksink.h"
GST_DEBUG_CATEGORY (gst_debug_gtk_sink);
#define GST_CAT_DEFAULT gst_debug_gtk_sink
static void gst_gtk_sink_finalize (GObject * object);
static void gst_gtk_sink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * param_spec);
static void gst_gtk_sink_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * param_spec);
static gboolean gst_gtk_sink_stop (GstBaseSink * bsink);
static gboolean gst_gtk_sink_query (GstBaseSink * bsink, GstQuery * query);
static GstStateChangeReturn
gst_gtk_sink_change_state (GstElement * element, GstStateChange transition);
static void gst_gtk_sink_get_times (GstBaseSink * bsink, GstBuffer * buf,
GstClockTime * start, GstClockTime * end);
static gboolean gst_gtk_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
static GstFlowReturn gst_gtk_sink_show_frame (GstVideoSink * bsink,
GstBuffer * buf);
static GstStaticPadTemplate gst_gtk_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("BGRA"))
);
enum
{
ARG_0,
PROP_WIDGET
};
enum
{
SIGNAL_0,
LAST_SIGNAL
};
#define gst_gtk_sink_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstGtkSink, gst_gtk_sink,
GST_TYPE_VIDEO_SINK, GST_DEBUG_CATEGORY_INIT (gst_debug_gtk_sink, "gtksink",
0, "Gtk Video Sink"));
static void
gst_gtk_sink_class_init (GstGtkSinkClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
GstBaseSinkClass *gstbasesink_class;
GstVideoSinkClass *gstvideosink_class;
gobject_class = (GObjectClass *) klass;
gstelement_class = (GstElementClass *) klass;
gstbasesink_class = (GstBaseSinkClass *) klass;
gstvideosink_class = (GstVideoSinkClass *) klass;
gobject_class->set_property = gst_gtk_sink_set_property;
gobject_class->get_property = gst_gtk_sink_get_property;
gst_element_class_set_metadata (gstelement_class, "Gtk Video Sink",
"Sink/Video", "A video sink the renders to a GtkWidget",
"Matthew Waters <matthew@centricular.com>");
g_object_class_install_property (gobject_class, PROP_WIDGET,
g_param_spec_object ("widget", "Gtk Widget",
"The GtkWidget to place in the widget heirachy",
GTK_TYPE_WIDGET, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&gst_gtk_sink_template));
gobject_class->finalize = gst_gtk_sink_finalize;
gstelement_class->change_state = gst_gtk_sink_change_state;
gstbasesink_class->query = gst_gtk_sink_query;
gstbasesink_class->set_caps = gst_gtk_sink_set_caps;
gstbasesink_class->get_times = gst_gtk_sink_get_times;
gstbasesink_class->stop = gst_gtk_sink_stop;
gstvideosink_class->show_frame = gst_gtk_sink_show_frame;
}
static void
gst_gtk_sink_init (GstGtkSink * gtk_sink)
{
gtk_sink->widget = (GtkGstWidget *) gtk_gst_widget_new ();
}
static void
gst_gtk_sink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
switch (prop_id) {
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_gtk_sink_finalize (GObject * object)
{
GstGtkSink *gtk_sink = GST_GTK_SINK (object);;
g_object_unref (gtk_sink->widget);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gst_gtk_sink_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstGtkSink *gtk_sink;
gtk_sink = GST_GTK_SINK (object);
switch (prop_id) {
case PROP_WIDGET:
g_value_set_object (value, gtk_sink->widget);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static gboolean
gst_gtk_sink_query (GstBaseSink * bsink, GstQuery * query)
{
gboolean res = FALSE;
switch (GST_QUERY_TYPE (query)) {
default:
res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
break;
}
return res;
}
static gboolean
gst_gtk_sink_stop (GstBaseSink * bsink)
{
return TRUE;
}
static GstStateChangeReturn
gst_gtk_sink_change_state (GstElement * element, GstStateChange transition)
{
GstGtkSink *gtk_sink = GST_GTK_SINK (element);
GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
GST_DEBUG ("changing state: %s => %s",
gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
break;
case GST_STATE_CHANGE_READY_TO_PAUSED:
break;
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
break;
default:
break;
}
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
if (ret == GST_STATE_CHANGE_FAILURE)
return ret;
switch (transition) {
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
break;
case GST_STATE_CHANGE_PAUSED_TO_READY:
gtk_gst_widget_set_buffer (gtk_sink->widget, NULL);
break;
case GST_STATE_CHANGE_READY_TO_NULL:
break;
default:
break;
}
return ret;
}
static void
gst_gtk_sink_get_times (GstBaseSink * bsink, GstBuffer * buf,
GstClockTime * start, GstClockTime * end)
{
GstGtkSink *gtk_sink;
gtk_sink = GST_GTK_SINK (bsink);
if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
*start = GST_BUFFER_TIMESTAMP (buf);
if (GST_BUFFER_DURATION_IS_VALID (buf))
*end = *start + GST_BUFFER_DURATION (buf);
else {
if (GST_VIDEO_INFO_FPS_N (&gtk_sink->v_info) > 0) {
*end = *start +
gst_util_uint64_scale_int (GST_SECOND,
GST_VIDEO_INFO_FPS_D (&gtk_sink->v_info),
GST_VIDEO_INFO_FPS_N (&gtk_sink->v_info));
}
}
}
}
gboolean
gst_gtk_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
{
GstGtkSink *gtk_sink = GST_GTK_SINK (bsink);
GST_DEBUG ("set caps with %" GST_PTR_FORMAT, caps);
if (!gst_video_info_from_caps (&gtk_sink->v_info, caps))
return FALSE;
if (!gtk_gst_widget_set_caps (gtk_sink->widget, caps))
return FALSE;
return TRUE;
}
static GstFlowReturn
gst_gtk_sink_show_frame (GstVideoSink * vsink, GstBuffer * buf)
{
GstGtkSink *gtk_sink;
GST_TRACE ("rendering buffer:%p", buf);
gtk_sink = GST_GTK_SINK (vsink);
if (gtk_sink->widget)
gtk_gst_widget_set_buffer (gtk_sink->widget, buf);
return GST_FLOW_OK;
}

77
ext/gtk/gstgtksink.h Normal file
View file

@ -0,0 +1,77 @@
/*
* GStreamer
* Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
*
* 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_GTK_SINK_H__
#define __GST_GTK_SINK_H__
#include <gtk/gtk.h>
#include <gst/gst.h>
#include <gst/video/gstvideosink.h>
#include <gst/video/video.h>
typedef struct _GstGtkSink GstGtkSink;
typedef struct _GstGtkSinkClass GstGtkSinkClass;
typedef struct _GstGtkSinkPrivate GstGtkSinkPrivate;
#include <gtkgstwidget.h>
G_BEGIN_DECLS
GType gst_gtk_sink_get_type (void);
#define GST_TYPE_GTK_SINK (gst_gtk_sink_get_type())
#define GST_GTK_SINK(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_GTK_SINK,GstGtkSink))
#define GST_GTK_SINK_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_GTK_SINK,GstGtkSinkClass))
#define GST_IS_GTK_SINK(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_GTK_SINK))
#define GST_IS_GTK_SINK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_GTK_SINK))
#define GST_GTK_SINK_CAST(obj) ((GstGtkSink*)(obj))
/**
* GstGtkSink:
*
* Opaque #GstGtkSink object
*/
struct _GstGtkSink
{
/* <private> */
GstVideoSink parent;
GstVideoInfo v_info;
GtkGstWidget *widget;
GstGtkSinkPrivate *priv;
};
/**
* GstGtkSinkClass:
*
* The #GstGtkSinkClass struct only contains private data
*/
struct _GstGtkSinkClass
{
/* <private> */
GstVideoSinkClass object_class;
};
GstGtkSink * gst_gtk_sink_new (void);
G_END_DECLS
#endif /* __GST_GTK_SINK_H__ */

52
ext/gtk/gstplugin.c Normal file
View file

@ -0,0 +1,52 @@
/*
* GStreamer
* Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
*
* 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstgtksink.h"
#if defined(HAVE_GTK3_GL)
#include "gstgtkglsink.h"
#endif
static gboolean
plugin_init (GstPlugin * plugin)
{
if (!gst_element_register (plugin, "gtksink",
GST_RANK_NONE, GST_TYPE_GTK_SINK)) {
return FALSE;
}
#if defined(HAVE_GTK3_GL)
if (!gst_element_register (plugin, "gtkglsink",
GST_RANK_NONE, GST_TYPE_GTK_GL_SINK)) {
return FALSE;
}
#endif
return TRUE;
}
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
GST_VERSION_MINOR,
gstgtk,
"Gtk+ sink",
plugin_init, PACKAGE_VERSION, GST_LICENSE, GST_PACKAGE_NAME,
GST_PACKAGE_ORIGIN)

617
ext/gtk/gtkgstglwidget.c Normal file
View file

@ -0,0 +1,617 @@
/*
* GStreamer
* Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
*
* 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include "gtkgstglwidget.h"
#include <gst/video/video.h>
#if GST_GL_HAVE_WINDOW_X11 && defined (GDK_WINDOWING_X11)
#include <gdk/gdkx.h>
#include <gst/gl/x11/gstgldisplay_x11.h>
#include <gst/gl/x11/gstglcontext_glx.h>
#endif
/**
* SECTION:gtkgstglwidget
* @short_description: a #GtkGLArea that renders GStreamer video #GstBuffers
* @see_also: #GtkGLArea, #GstBuffer
*
* #GtkGstGLWidget is an #GtkWidget that renders GStreamer video buffers.
*/
#define GST_CAT_DEFAULT gtk_gst_gl_widget_debug
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
G_DEFINE_TYPE_WITH_CODE (GtkGstGLWidget, gtk_gst_gl_widget, GTK_TYPE_GL_AREA,
GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gtkgstglwidget", 0,
"Gtk Gst GL Widget"););
#define GTK_GST_GL_WIDGET_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
GTK_TYPE_GST_GL_WIDGET, GtkGstGLWidgetPrivate))
struct _GtkGstGLWidgetPrivate
{
GMutex lock;
gboolean negotiated;
GstBuffer *buffer;
GstCaps *gl_caps;
GstCaps *caps;
GstVideoInfo v_info;
gboolean new_buffer;
gboolean initted;
GstGLDisplay *display;
GdkGLContext *gdk_context;
GstGLContext *other_context;
GstGLContext *context;
GstGLUpload *upload;
GstGLShader *shader;
GLuint vao;
GLuint vertex_buffer;
GLint attr_position;
GLint attr_texture;
GLuint current_tex;
};
static void
gtk_gst_gl_widget_get_preferred_width (GtkWidget * widget, gint * min,
gint * natural)
{
GtkGstGLWidget *gst_widget = (GtkGstGLWidget *) widget;
gint video_width = GST_VIDEO_INFO_WIDTH (&gst_widget->priv->v_info);
if (!gst_widget->priv->negotiated)
video_width = 10;
if (min)
*min = 1;
if (natural)
*natural = video_width;
}
static void
gtk_gst_gl_widget_get_preferred_height (GtkWidget * widget, gint * min,
gint * natural)
{
GtkGstGLWidget *gst_widget = (GtkGstGLWidget *) widget;
gint video_height = GST_VIDEO_INFO_HEIGHT (&gst_widget->priv->v_info);
if (!gst_widget->priv->negotiated)
video_height = 10;
if (min)
*min = 1;
if (natural)
*natural = video_height;
}
static const GLfloat vertices[] = {
1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
-1.0f, -1.0f, 0.0f, 0.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f, 1.0f
};
static void
gtk_gst_gl_widget_bind_buffer (GtkGstGLWidget * gst_widget)
{
const GstGLFuncs *gl = gst_widget->priv->context->gl_vtable;
gl->BindBuffer (GL_ARRAY_BUFFER, gst_widget->priv->vertex_buffer);
/* Load the vertex position */
gl->VertexAttribPointer (gst_widget->priv->attr_position, 3, GL_FLOAT,
GL_FALSE, 5 * sizeof (GLfloat), (void *) 0);
/* Load the texture coordinate */
gl->VertexAttribPointer (gst_widget->priv->attr_texture, 2, GL_FLOAT,
GL_FALSE, 5 * sizeof (GLfloat), (void *) (3 * sizeof (GLfloat)));
gl->EnableVertexAttribArray (gst_widget->priv->attr_position);
gl->EnableVertexAttribArray (gst_widget->priv->attr_texture);
}
static void
gtk_gst_gl_widget_unbind_buffer (GtkGstGLWidget * gst_widget)
{
const GstGLFuncs *gl = gst_widget->priv->context->gl_vtable;
gl->BindBuffer (GL_ARRAY_BUFFER, 0);
gl->DisableVertexAttribArray (gst_widget->priv->attr_position);
gl->DisableVertexAttribArray (gst_widget->priv->attr_texture);
}
static void
gtk_gst_gl_widget_init_redisplay (GtkGstGLWidget * gst_widget)
{
const GstGLFuncs *gl = gst_widget->priv->context->gl_vtable;
gst_widget->priv->shader = gst_gl_shader_new (gst_widget->priv->context);
gst_gl_shader_compile_with_default_vf_and_check
(gst_widget->priv->shader, &gst_widget->priv->attr_position,
&gst_widget->priv->attr_texture);
if (gl->GenVertexArrays) {
gl->GenVertexArrays (1, &gst_widget->priv->vao);
gl->BindVertexArray (gst_widget->priv->vao);
}
gl->GenBuffers (1, &gst_widget->priv->vertex_buffer);
gl->BindBuffer (GL_ARRAY_BUFFER, gst_widget->priv->vertex_buffer);
gl->BufferData (GL_ARRAY_BUFFER, 4 * 5 * sizeof (GLfloat), vertices,
GL_STATIC_DRAW);
if (gl->GenVertexArrays) {
gtk_gst_gl_widget_bind_buffer (gst_widget);
gl->BindVertexArray (0);
}
gl->BindBuffer (GL_ARRAY_BUFFER, 0);
gst_widget->priv->initted = TRUE;
}
static void
_redraw_texture (GtkGstGLWidget * gst_widget, guint tex)
{
const GstGLFuncs *gl = gst_widget->priv->context->gl_vtable;
GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
gst_gl_shader_use (gst_widget->priv->shader);
if (gl->BindVertexArray)
gl->BindVertexArray (gst_widget->priv->vao);
else
gtk_gst_gl_widget_bind_buffer (gst_widget);
gl->ActiveTexture (GL_TEXTURE0);
gl->BindTexture (GL_TEXTURE_2D, tex);
gst_gl_shader_set_uniform_1i (gst_widget->priv->shader, "tex", 0);
gl->DrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
if (gl->BindVertexArray)
gl->BindVertexArray (0);
else
gtk_gst_gl_widget_unbind_buffer (gst_widget);
gl->BindTexture (GL_TEXTURE_2D, 0);
}
static gboolean
gtk_gst_gl_widget_render (GtkGLArea * widget, GdkGLContext * context)
{
GtkGstGLWidget *gst_widget = (GtkGstGLWidget *) widget;
g_mutex_lock (&gst_widget->priv->lock);
if (!gst_widget->priv->initted)
gtk_gst_gl_widget_init_redisplay (gst_widget);
if (gst_widget->priv->initted && gst_widget->priv->negotiated
&& gst_widget->priv->buffer) {
GST_DEBUG ("rendering buffer %p with gdk context %p",
gst_widget->priv->buffer, context);
gst_gl_context_activate (gst_widget->priv->other_context, TRUE);
if (gst_widget->priv->new_buffer || gst_widget->priv->current_tex == 0) {
GstVideoFrame gl_frame;
GstGLSyncMeta *sync_meta;
if (!gst_video_frame_map (&gl_frame, &gst_widget->priv->v_info,
gst_widget->priv->buffer, GST_MAP_READ | GST_MAP_GL)) {
goto error;
}
sync_meta = gst_buffer_get_gl_sync_meta (gst_widget->priv->buffer);
if (sync_meta) {
gst_gl_sync_meta_set_sync_point (sync_meta, gst_widget->priv->context);
gst_gl_sync_meta_wait (sync_meta, gst_widget->priv->other_context);
}
gst_widget->priv->current_tex = *(guint *) gl_frame.data[0];
gst_video_frame_unmap (&gl_frame);
}
_redraw_texture (gst_widget, gst_widget->priv->current_tex);
gst_widget->priv->new_buffer = FALSE;
} else {
error:
/* FIXME: nothing to display */
glClearColor (0.0, 0.0, 0.0, 0.0);
glClear (GL_COLOR_BUFFER_BIT);
}
gst_gl_context_activate (gst_widget->priv->other_context, FALSE);
g_mutex_unlock (&gst_widget->priv->lock);
return FALSE;
}
typedef void (*ThreadFunc) (gpointer data);
struct invoke_context
{
ThreadFunc func;
gpointer data;
GMutex lock;
GCond cond;
gboolean fired;
};
static gboolean
_invoke_func (struct invoke_context *info)
{
g_mutex_lock (&info->lock);
info->func (info->data);
info->fired = TRUE;
g_cond_signal (&info->cond);
g_mutex_unlock (&info->lock);
return G_SOURCE_REMOVE;
}
static void
_invoke_on_main (ThreadFunc func, gpointer data)
{
GMainContext *main_context = g_main_context_default ();
struct invoke_context info;
g_mutex_init (&info.lock);
g_cond_init (&info.cond);
info.fired = FALSE;
info.func = func;
info.data = data;
g_main_context_invoke (main_context, (GSourceFunc) _invoke_func, &info);
g_mutex_lock (&info.lock);
while (!info.fired)
g_cond_wait (&info.cond, &info.lock);
g_mutex_unlock (&info.lock);
g_mutex_clear (&info.lock);
g_cond_clear (&info.cond);
}
static void
_reset_gl (GtkGstGLWidget * gst_widget)
{
const GstGLFuncs *gl = gst_widget->priv->other_context->gl_vtable;
if (!gst_widget->priv->gdk_context)
gst_widget->priv->gdk_context =
gtk_gl_area_get_context (GTK_GL_AREA (gst_widget));
if (gst_widget->priv->gdk_context == NULL)
return;
gdk_gl_context_make_current (gst_widget->priv->gdk_context);
gst_gl_context_activate (gst_widget->priv->other_context, TRUE);
if (gst_widget->priv->vao) {
gl->DeleteVertexArrays (1, &gst_widget->priv->vao);
gst_widget->priv->vao = 0;
}
if (gst_widget->priv->vertex_buffer) {
gl->DeleteBuffers (1, &gst_widget->priv->vertex_buffer);
gst_widget->priv->vertex_buffer = 0;
}
if (gst_widget->priv->upload) {
gst_object_unref (gst_widget->priv->upload);
gst_widget->priv->upload = NULL;
}
if (gst_widget->priv->shader) {
gst_object_unref (gst_widget->priv->shader);
gst_widget->priv->shader = NULL;
}
gst_gl_context_activate (gst_widget->priv->other_context, FALSE);
gst_object_unref (gst_widget->priv->other_context);
gst_widget->priv->other_context = NULL;
gdk_gl_context_clear_current ();
g_object_unref (gst_widget->priv->gdk_context);
gst_widget->priv->gdk_context = NULL;
}
static void
_reset (GtkGstGLWidget * gst_widget)
{
gst_buffer_replace (&gst_widget->priv->buffer, NULL);
gst_caps_replace (&gst_widget->priv->caps, NULL);
gst_caps_replace (&gst_widget->priv->gl_caps, NULL);
gst_widget->priv->negotiated = FALSE;
gst_widget->priv->initted = FALSE;
gst_widget->priv->vao = 0;
gst_widget->priv->vertex_buffer = 0;
gst_widget->priv->attr_position = 0;
gst_widget->priv->attr_texture = 0;
gst_widget->priv->current_tex = 0;
gst_widget->priv->new_buffer = TRUE;
}
static void
gtk_gst_gl_widget_finalize (GObject * object)
{
GtkGstGLWidget *widget = GTK_GST_GL_WIDGET_CAST (object);
g_mutex_clear (&widget->priv->lock);
_reset (widget);
if (widget->priv->other_context) {
_invoke_on_main ((ThreadFunc) _reset_gl, widget);
}
if (widget->priv->context) {
gst_object_unref (widget->priv->context);
widget->priv->context = NULL;
}
if (widget->priv->display) {
gst_object_unref (widget->priv->display);
widget->priv->display = NULL;
}
G_OBJECT_CLASS (gtk_gst_gl_widget_parent_class)->finalize (object);
}
static void
gtk_gst_gl_widget_class_init (GtkGstGLWidgetClass * klass)
{
GtkWidgetClass *widget_klass = (GtkWidgetClass *) klass;
GtkGLAreaClass *gl_widget_klass = (GtkGLAreaClass *) klass;
g_type_class_add_private (klass, sizeof (GtkGstGLWidgetPrivate));
gl_widget_klass->render = gtk_gst_gl_widget_render;
widget_klass->get_preferred_width = gtk_gst_gl_widget_get_preferred_width;
widget_klass->get_preferred_height = gtk_gst_gl_widget_get_preferred_height;
G_OBJECT_CLASS (klass)->finalize = gtk_gst_gl_widget_finalize;
}
static void
gtk_gst_gl_widget_init (GtkGstGLWidget * widget)
{
GdkDisplay *display;
widget->priv = GTK_GST_GL_WIDGET_GET_PRIVATE (widget);
g_mutex_init (&widget->priv->lock);
display = gdk_display_get_default ();
#if GST_GL_HAVE_WINDOW_X11 && defined (GDK_WINDOWING_X11)
if (GDK_IS_X11_DISPLAY (display))
widget->priv->display = (GstGLDisplay *)
gst_gl_display_x11_new_with_display (gdk_x11_display_get_xdisplay
(display));
#endif
if (!widget->priv->display)
widget->priv->display = gst_gl_display_new ();
gtk_gl_area_set_has_alpha ((GtkGLArea *) widget, TRUE);
}
GtkWidget *
gtk_gst_gl_widget_new (void)
{
return (GtkWidget *) g_object_new (GTK_TYPE_GST_GL_WIDGET, NULL);
}
static gboolean
_queue_draw (GtkGstGLWidget * widget)
{
gtk_widget_queue_draw (GTK_WIDGET (widget));
return G_SOURCE_REMOVE;
}
void
gtk_gst_gl_widget_set_buffer (GtkGstGLWidget * widget, GstBuffer * buffer)
{
GMainContext *main_context = g_main_context_default ();
g_return_if_fail (GTK_IS_GST_GL_WIDGET (widget));
g_return_if_fail (widget->priv->negotiated);
g_mutex_lock (&widget->priv->lock);
gst_buffer_replace (&widget->priv->buffer, buffer);
widget->priv->new_buffer = TRUE;
g_mutex_unlock (&widget->priv->lock);
g_main_context_invoke (main_context, (GSourceFunc) _queue_draw, widget);
}
static void
_get_gl_context (GtkGstGLWidget * gst_widget)
{
GstGLPlatform platform;
GstGLAPI gl_api;
guintptr gl_handle;
gtk_widget_realize (GTK_WIDGET (gst_widget));
if (gst_widget->priv->gdk_context)
g_object_unref (gst_widget->priv->gdk_context);
gst_widget->priv->gdk_context =
gtk_gl_area_get_context (GTK_GL_AREA (gst_widget));
if (gst_widget->priv->gdk_context == NULL) {
g_assert_not_reached ();
return;
}
g_object_ref (gst_widget->priv->gdk_context);
gdk_gl_context_make_current (gst_widget->priv->gdk_context);
#if GST_GL_HAVE_WINDOW_X11 && defined (GDK_WINDOWING_X11)
if (GST_IS_GL_DISPLAY_X11 (gst_widget->priv->display)) {
platform = GST_GL_PLATFORM_GLX;
gl_api = gst_gl_context_get_current_gl_api (NULL, NULL);
gl_handle = gst_gl_context_get_current_gl_context (platform);
if (gl_handle)
gst_widget->priv->other_context =
gst_gl_context_new_wrapped (gst_widget->priv->display, gl_handle,
platform, gl_api);
}
#endif
if (gst_widget->priv->other_context) {
GError *error = NULL;
gst_gl_context_activate (gst_widget->priv->other_context, TRUE);
if (!gst_gl_context_fill_info (gst_widget->priv->other_context, &error)) {
GST_ERROR ("failed to retreive gdk context info: %s", error->message);
g_object_unref (gst_widget->priv->other_context);
gst_widget->priv->other_context = NULL;
} else {
gst_gl_context_activate (gst_widget->priv->other_context, FALSE);
}
}
}
static gboolean
_queue_resize (GtkGstGLWidget * widget)
{
gtk_widget_queue_resize (GTK_WIDGET (widget));
return G_SOURCE_REMOVE;
}
gboolean
gtk_gst_gl_widget_init_winsys (GtkGstGLWidget * widget)
{
g_return_val_if_fail (GTK_IS_GST_GL_WIDGET (widget), FALSE);
g_mutex_lock (&widget->priv->lock);
if (widget->priv->display && widget->priv->gdk_context
&& widget->priv->other_context) {
g_mutex_unlock (&widget->priv->lock);
return TRUE;
}
if (!widget->priv->other_context) {
_invoke_on_main ((ThreadFunc) _get_gl_context, widget);
}
if (!GST_GL_IS_CONTEXT (widget->priv->other_context)) {
g_mutex_unlock (&widget->priv->lock);
return FALSE;
}
widget->priv->context = gst_gl_context_new (widget->priv->display);
if (!widget->priv->context) {
g_mutex_unlock (&widget->priv->lock);
return FALSE;
}
gst_gl_context_create (widget->priv->context, widget->priv->other_context,
NULL);
g_mutex_unlock (&widget->priv->lock);
return TRUE;
}
gboolean
gtk_gst_gl_widget_set_caps (GtkGstGLWidget * widget, GstCaps * caps)
{
GMainContext *main_context = g_main_context_default ();
GstVideoInfo v_info;
g_return_val_if_fail (GTK_IS_GST_GL_WIDGET (widget), FALSE);
g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
if (widget->priv->caps && gst_caps_is_equal_fixed (widget->priv->caps, caps))
return TRUE;
if (!gst_video_info_from_caps (&v_info, caps))
return FALSE;
g_mutex_lock (&widget->priv->lock);
_reset (widget);
gst_caps_replace (&widget->priv->caps, caps);
widget->priv->gl_caps = gst_video_info_to_caps (&v_info);
gst_caps_set_features (widget->priv->gl_caps, 0,
gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_GL_MEMORY));
widget->priv->v_info = v_info;
widget->priv->negotiated = TRUE;
g_mutex_unlock (&widget->priv->lock);
g_main_context_invoke (main_context, (GSourceFunc) _queue_resize, widget);
return TRUE;
}
GstGLContext *
gtk_gst_gl_widget_get_gtk_context (GtkGstGLWidget * gst_widget)
{
if (!gst_widget->priv->other_context)
return NULL;
return gst_object_ref (gst_widget->priv->other_context);
}
GstGLContext *
gtk_gst_gl_widget_get_context (GtkGstGLWidget * gst_widget)
{
if (!gst_widget->priv->context)
return NULL;
return gst_object_ref (gst_widget->priv->context);
}
GstGLDisplay *
gtk_gst_gl_widget_get_display (GtkGstGLWidget * gst_widget)
{
if (!gst_widget->priv->display)
return NULL;
return gst_object_ref (gst_widget->priv->display);
}

77
ext/gtk/gtkgstglwidget.h Normal file
View file

@ -0,0 +1,77 @@
/*
* GStreamer
* Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
*
* 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GTK_GST_GL_WIDGET_H__
#define __GTK_GST_GL_WIDGET_H__
#include <gtk/gtk.h>
#include <gst/gst.h>
#include <gst/gl/gl.h>
G_BEGIN_DECLS
GType gtk_gst_gl_widget_get_type (void);
#define GTK_TYPE_GST_GL_WIDGET (gtk_gst_gl_widget_get_type())
#define GTK_GST_GL_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GTK_TYPE_GST_GL_WIDGET,GtkGstGLWidget))
#define GTK_GST_GL_WIDGET_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GTK_TYPE_GST_GL_WIDGET,GtkGstGLWidgetClass))
#define GTK_IS_GST_GL_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GTK_TYPE_GST_GL_WIDGET))
#define GTK_IS_GST_GL_WIDGET_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GTK_TYPE_GST_GL_WIDGET))
#define GTK_GST_GL_WIDGET_CAST(obj) ((GtkGstGLWidget*)(obj))
typedef struct _GtkGstGLWidget GtkGstGLWidget;
typedef struct _GtkGstGLWidgetClass GtkGstGLWidgetClass;
typedef struct _GtkGstGLWidgetPrivate GtkGstGLWidgetPrivate;
/**
* GtkGstGLWidget:
*
* Opaque #GtkGstGLWidget object
*/
struct _GtkGstGLWidget
{
/* <private> */
GtkGLArea parent;
GtkGstGLWidgetPrivate *priv;
};
/**
* GtkGstGLWidgetClass:
*
* The #GtkGstGLWidgetClass struct only contains private data
*/
struct _GtkGstGLWidgetClass
{
/* <private> */
GtkGLAreaClass object_class;
};
GtkWidget * gtk_gst_gl_widget_new (void);
gboolean gtk_gst_gl_widget_init_winsys (GtkGstGLWidget * widget);
gboolean gtk_gst_gl_widget_set_caps (GtkGstGLWidget * widget, GstCaps *caps);
void gtk_gst_gl_widget_set_buffer (GtkGstGLWidget * widget, GstBuffer *buffer);
GstGLDisplay * gtk_gst_gl_widget_get_display (GtkGstGLWidget * widget);
GstGLContext * gtk_gst_gl_widget_get_context (GtkGstGLWidget * widget);
GstGLContext * gtk_gst_gl_widget_get_gtk_context (GtkGstGLWidget * widget);
G_END_DECLS
#endif /* __GTK_GST_GL_WIDGET_H__ */

240
ext/gtk/gtkgstwidget.c Normal file
View file

@ -0,0 +1,240 @@
/*
* GStreamer
* Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
*
* 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include "gtkgstwidget.h"
#include <gst/video/video.h>
/**
* SECTION:gtkgstwidget
* @short_description: a #GtkWidget that renders GStreamer video #GstBuffers
* @see_also: #GtkDrawingArea, #GstBuffer
*
* #GtkGstWidget is an #GtkWidget that renders GStreamer video buffers.
*/
G_DEFINE_TYPE (GtkGstWidget, gtk_gst_widget, GTK_TYPE_DRAWING_AREA);
#define GTK_GST_WIDGET_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
GTK_TYPE_GST_WIDGET, GtkGstWidgetPrivate))
struct _GtkGstWidgetPrivate
{
GMutex lock;
gboolean negotiated;
GstBuffer *buffer;
GstCaps *caps;
GstVideoInfo v_info;
};
static void
gtk_gst_widget_get_preferred_width (GtkWidget * widget, gint * min,
gint * natural)
{
GtkGstWidget *gst_widget = (GtkGstWidget *) widget;
gint video_width = GST_VIDEO_INFO_WIDTH (&gst_widget->priv->v_info);
if (!gst_widget->priv->negotiated)
video_width = 10;
if (min)
*min = 1;
if (natural)
*natural = video_width;
}
static void
gtk_gst_widget_get_preferred_height (GtkWidget * widget, gint * min,
gint * natural)
{
GtkGstWidget *gst_widget = (GtkGstWidget *) widget;
gint video_height = GST_VIDEO_INFO_HEIGHT (&gst_widget->priv->v_info);
if (!gst_widget->priv->negotiated)
video_height = 10;
if (min)
*min = 1;
if (natural)
*natural = video_height;
}
static gboolean
gtk_gst_widget_draw (GtkWidget * widget, cairo_t * cr)
{
GtkGstWidget *gst_widget = (GtkGstWidget *) widget;
guint widget_width, widget_height;
cairo_surface_t *surface;
GstVideoFrame frame;
widget_width = gtk_widget_get_allocated_width (widget);
widget_height = gtk_widget_get_allocated_height (widget);
g_mutex_lock (&gst_widget->priv->lock);
/* failed to map the video frame */
if (gst_widget->priv->negotiated && gst_widget->priv->buffer
&& gst_video_frame_map (&frame, &gst_widget->priv->v_info,
gst_widget->priv->buffer, GST_MAP_READ)) {
gdouble scale_x =
(gdouble) widget_width / GST_VIDEO_INFO_WIDTH (&frame.info);
gdouble scale_y =
(gdouble) widget_height / GST_VIDEO_INFO_HEIGHT (&frame.info);
gst_widget->priv->v_info = frame.info;
surface = cairo_image_surface_create_for_data (frame.data[0],
CAIRO_FORMAT_ARGB32, frame.info.width, frame.info.height,
frame.info.stride[0]);
cairo_scale (cr, scale_x, scale_y);
cairo_rectangle (cr, 0, 0, widget_width, widget_height);
cairo_set_source_surface (cr, surface, 0, 0);
cairo_paint (cr);
cairo_surface_destroy (surface);
gst_video_frame_unmap (&frame);
} else {
GdkRGBA color;
gtk_style_context_get_color (gtk_widget_get_style_context (widget), 0,
&color);
gdk_cairo_set_source_rgba (cr, &color);
cairo_rectangle (cr, 0, 0, widget_width, widget_height);
cairo_fill (cr);
}
g_mutex_unlock (&gst_widget->priv->lock);
return FALSE;
}
static void
gtk_gst_widget_finalize (GObject * object)
{
GtkGstWidget *widget = GTK_GST_WIDGET_CAST (object);
gst_buffer_replace (&widget->priv->buffer, NULL);
g_mutex_clear (&widget->priv->lock);
G_OBJECT_CLASS (gtk_gst_widget_parent_class)->finalize (object);
}
static void
gtk_gst_widget_class_init (GtkGstWidgetClass * klass)
{
GtkWidgetClass *widget_klass = (GtkWidgetClass *) klass;
g_type_class_add_private (klass, sizeof (GtkGstWidgetPrivate));
widget_klass->draw = gtk_gst_widget_draw;
widget_klass->get_preferred_width = gtk_gst_widget_get_preferred_width;
widget_klass->get_preferred_height = gtk_gst_widget_get_preferred_height;
G_OBJECT_CLASS (klass)->finalize = gtk_gst_widget_finalize;
}
static void
gtk_gst_widget_init (GtkGstWidget * widget)
{
widget->priv = GTK_GST_WIDGET_GET_PRIVATE (widget);
g_mutex_init (&widget->priv->lock);
}
GtkWidget *
gtk_gst_widget_new (void)
{
return (GtkWidget *) g_object_new (GTK_TYPE_GST_WIDGET, NULL);
}
static gboolean
_queue_draw (GtkGstWidget * widget)
{
gtk_widget_queue_draw (GTK_WIDGET (widget));
return G_SOURCE_REMOVE;
}
void
gtk_gst_widget_set_buffer (GtkGstWidget * widget, GstBuffer * buffer)
{
GMainContext *main_context = g_main_context_default ();
g_return_if_fail (GTK_IS_GST_WIDGET (widget));
g_return_if_fail (widget->priv->negotiated);
g_mutex_lock (&widget->priv->lock);
gst_buffer_replace (&widget->priv->buffer, buffer);
g_mutex_unlock (&widget->priv->lock);
g_main_context_invoke (main_context, (GSourceFunc) _queue_draw, widget);
}
static gboolean
_queue_resize (GtkGstWidget * widget)
{
gtk_widget_queue_resize (GTK_WIDGET (widget));
return G_SOURCE_REMOVE;
}
gboolean
gtk_gst_widget_set_caps (GtkGstWidget * widget, GstCaps * caps)
{
GMainContext *main_context = g_main_context_default ();
GstVideoInfo v_info;
g_return_val_if_fail (GTK_IS_GST_WIDGET (widget), FALSE);
g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
if (widget->priv->caps && gst_caps_is_equal_fixed (widget->priv->caps, caps))
return TRUE;
if (!gst_video_info_from_caps (&v_info, caps))
return FALSE;
/* FIXME: support other formats */
g_return_val_if_fail (GST_VIDEO_INFO_FORMAT (&v_info) ==
GST_VIDEO_FORMAT_BGRA, FALSE);
g_mutex_lock (&widget->priv->lock);
gst_caps_replace (&widget->priv->caps, caps);
widget->priv->v_info = v_info;
widget->priv->negotiated = TRUE;
g_mutex_unlock (&widget->priv->lock);
gtk_widget_queue_resize (GTK_WIDGET (widget));
g_main_context_invoke (main_context, (GSourceFunc) _queue_resize, widget);
return TRUE;
}

72
ext/gtk/gtkgstwidget.h Normal file
View file

@ -0,0 +1,72 @@
/*
* GStreamer
* Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
*
* 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GTK_GST_WIDGET_H__
#define __GTK_GST_WIDGET_H__
#include <gtk/gtk.h>
#include <gst/gst.h>
G_BEGIN_DECLS
GType gtk_gst_widget_get_type (void);
#define GTK_TYPE_GST_WIDGET (gtk_gst_widget_get_type())
#define GTK_GST_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GTK_TYPE_GST_WIDGET,GtkGstWidget))
#define GTK_GST_WIDGET_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GTK_TYPE_GST_WIDGET,GtkGstWidgetClass))
#define GTK_IS_GST_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GTK_TYPE_GST_WIDGET))
#define GST_IS_GST_WIDGET_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GTK_TYPE_GST_WIDGET))
#define GTK_GST_WIDGET_CAST(obj) ((GtkGstWidget*)(obj))
typedef struct _GtkGstWidget GtkGstWidget;
typedef struct _GtkGstWidgetClass GtkGstWidgetClass;
typedef struct _GtkGstWidgetPrivate GtkGstWidgetPrivate;
/**
* GtkGstWidget:
*
* Opaque #GtkGstWidget object
*/
struct _GtkGstWidget
{
/* <private> */
GtkDrawingArea parent;
GtkGstWidgetPrivate *priv;
};
/**
* GtkGstWidgetClass:
*
* The #GtkGstWidgetClass struct only contains private data
*/
struct _GtkGstWidgetClass
{
/* <private> */
GtkDrawingAreaClass object_class;
};
GtkWidget * gtk_gst_widget_new (void);
gboolean gtk_gst_widget_set_caps (GtkGstWidget * widget, GstCaps *caps);
void gtk_gst_widget_set_buffer (GtkGstWidget * widget, GstBuffer *buffer);
G_END_DECLS
#endif /* __GTK_GST_WIDGET_H__ */

View file

@ -19,7 +19,8 @@ AM_TESTS_ENVIRONMENT = \
decklinksink dtlssrtpdec dtlssrtpenc dvbsrc dvbbasebin dfbvideosink festival fluidsynth gsettingsvideosrc \
gsettingsvideosink gsettingsaudiosrc gsettingsaudiosink linsyssdisrc linsyssdisink nassink \
rsndvdbin sdlaudiosink sdlvideosink vcdsrc rfbsrc vdpauyuvvideo vdpauvideoyuv \
vdpaumpegdec vdpaumpeg4dec vdpauh264dec vdpauvideopostprocess vdpausink neonhttpsrc"
vdpaumpegdec vdpaumpeg4dec vdpauh264dec vdpauvideopostprocess vdpausink neonhttpsrc \
gtksink gtkglsink"
plugindir = $(libdir)/gstreamer-@GST_API_VERSION@

View file

@ -34,8 +34,14 @@ else
AVSAMPLE_DIR=
endif
if USE_GTK3
GTK3_DIR=gtk
else
GTK3_DIR=
endif
SUBDIRS= mpegts $(DIRECTFB_DIR) $(GTK_EXAMPLES) $(OPENCV_EXAMPLES) $(GL_DIR) \
$(AVSAMPLE_DIR)
DIST_SUBDIRS= mpegts camerabin2 directfb mxf opencv uvch264 gl avsamplesink
$(GTK3_DIR) $(AVSAMPLE_DIR)
DIST_SUBDIRS= mpegts camerabin2 directfb mxf opencv uvch264 gl gtk avsamplesink
include $(top_srcdir)/common/parallel-subdirs.mak

View file

@ -0,0 +1,22 @@
noinst_PROGRAMS = gtksink
gtksink_SOURCES = gtksink.c
gtksink_CFLAGS = $(GTK3_CFLAGS) \
$(GST_CFLAGS)
gtksink_LDADD = $(GTK3_LIBS) \
$(GST_LIBS)
if USE_GTK3_GL
if USE_GL
noinst_PROGRAMS += gtkglsink
gtkglsink_SOURCES = gtkglsink.c
gtkglsink_CFLAGS = $(GTK3_CFLAGS) \
$(GST_CFLAGS) \
$(GL_CFLAGS)
gtkglsink_LDADD = $(GTK3_LIBS) \
$(GST_LIBS) \
$(GL_LIBS)
endif
endif

View file

@ -0,0 +1,201 @@
/*
* GStreamer
* Copyright (C) 2014 Matthew Waters <matthew@centricular.com>
*
* 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <gtk/gtk.h>
#include <gst/gst.h>
#include <gst/gl/gl.h>
#if GST_GL_HAVE_WINDOW_X11 && defined (GDK_WINDOWING_X11)
#include <gst/gl/x11/gstgldisplay_x11.h>
#endif
static void
button_state_null_cb (GtkWidget * widget, GstElement * pipeline)
{
gst_element_set_state (pipeline, GST_STATE_NULL);
g_print ("GST_STATE_NULL\n");
}
static void
button_state_ready_cb (GtkWidget * widget, GstElement * pipeline)
{
gst_element_set_state (pipeline, GST_STATE_READY);
g_print ("GST_STATE_READY\n");
}
static void
button_state_paused_cb (GtkWidget * widget, GstElement * pipeline)
{
gst_element_set_state (pipeline, GST_STATE_PAUSED);
g_print ("GST_STATE_PAUSED\n");
}
static void
button_state_playing_cb (GtkWidget * widget, GstElement * pipeline)
{
gst_element_set_state (pipeline, GST_STATE_PLAYING);
g_print ("GST_STATE_PLAYING\n");
}
static void
end_stream_cb (GstBus * bus, GstMessage * message, GstElement * pipeline)
{
g_print ("End of stream\n");
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
gtk_main_quit ();
}
static void
destroy_cb (GtkWidget * widget, GdkEvent * event, GstElement * pipeline)
{
g_print ("Close\n");
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
gtk_main_quit ();
}
int
main (int argc, char *argv[])
{
GtkWidget *window, *window_control;
GtkWidget *button_state_null, *button_state_ready;
GtkWidget *button_state_paused, *button_state_playing;
GtkWidget *grid, *area;
GstElement *pipeline;
GstElement *videosrc, *upload, *effect, *videosink;
GstStateChangeReturn ret;
GstCaps *caps;
GstBus *bus;
#if GST_GL_HAVE_WINDOW_X11 && defined(GDK_WINDOWING_X11)
XInitThreads ();
#endif
gst_init (&argc, &argv);
gtk_init (&argc, &argv);
pipeline = gst_pipeline_new ("pipeline");
//window that contains an area where the video is drawn
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size (GTK_WINDOW (window), 640, 480);
gtk_window_move (GTK_WINDOW (window), 300, 10);
gtk_window_set_title (GTK_WINDOW (window), "gtkgstwidget");
//window to control the states
window_control = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_resizable (GTK_WINDOW (window_control), FALSE);
gtk_window_move (GTK_WINDOW (window_control), 10, 10);
grid = gtk_grid_new ();
gtk_container_add (GTK_CONTAINER (window_control), grid);
//control state null
button_state_null = gtk_button_new_with_label ("GST_STATE_NULL");
g_signal_connect (G_OBJECT (button_state_null), "clicked",
G_CALLBACK (button_state_null_cb), pipeline);
gtk_grid_attach (GTK_GRID (grid), button_state_null, 0, 1, 1, 1);
gtk_widget_show (button_state_null);
//control state ready
button_state_ready = gtk_button_new_with_label ("GST_STATE_READY");
g_signal_connect (G_OBJECT (button_state_ready), "clicked",
G_CALLBACK (button_state_ready_cb), pipeline);
gtk_grid_attach (GTK_GRID (grid), button_state_ready, 0, 2, 1, 1);
gtk_widget_show (button_state_ready);
//control state paused
button_state_paused = gtk_button_new_with_label ("GST_STATE_PAUSED");
g_signal_connect (G_OBJECT (button_state_paused), "clicked",
G_CALLBACK (button_state_paused_cb), pipeline);
gtk_grid_attach (GTK_GRID (grid), button_state_paused, 0, 3, 1, 1);
gtk_widget_show (button_state_paused);
//control state playing
button_state_playing = gtk_button_new_with_label ("GST_STATE_PLAYING");
g_signal_connect (G_OBJECT (button_state_playing), "clicked",
G_CALLBACK (button_state_playing_cb), pipeline);
gtk_grid_attach (GTK_GRID (grid), button_state_playing, 0, 4, 1, 1);
gtk_widget_show (button_state_playing);
gtk_widget_show (grid);
gtk_widget_show (window_control);
g_signal_connect (G_OBJECT (window), "delete-event", G_CALLBACK (destroy_cb),
pipeline);
//configure the pipeline
videosrc = gst_element_factory_make ("videotestsrc", "videotestsrc");
upload = gst_element_factory_make ("glupload", "glupload");
effect = gst_element_factory_make ("glfiltercube", "glfiltercube");
videosink = gst_element_factory_make ("gtkglsink", "gtksink");
g_object_get (videosink, "widget", &area, NULL);
gtk_container_add (GTK_CONTAINER (window), area);
gtk_widget_realize (area);
caps = gst_caps_new_simple ("video/x-raw", "width", G_TYPE_INT, 640,
"height", G_TYPE_INT, 480, "format", G_TYPE_STRING, "RGBA",
"framerate", GST_TYPE_FRACTION, 30, 1, NULL);
gst_bin_add_many (GST_BIN (pipeline), videosrc, upload, effect, videosink,
NULL);
if (!gst_element_link_filtered (videosrc, upload, caps)) {
gst_caps_unref (caps);
g_warning ("Failed to link videosrc to glfiltercube!\n");
return -1;
}
gst_caps_unref (caps);
if (!gst_element_link_many (upload, effect, videosink, NULL)) {
g_warning ("Failed to link videosrc to glfiltercube!\n");
return -1;
}
//set window id on this event
bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
g_signal_connect (bus, "message::error", G_CALLBACK (end_stream_cb),
pipeline);
g_signal_connect (bus, "message::warning", G_CALLBACK (end_stream_cb),
pipeline);
g_signal_connect (bus, "message::eos", G_CALLBACK (end_stream_cb), pipeline);
gst_object_unref (bus);
//start
ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
if (ret == GST_STATE_CHANGE_FAILURE) {
g_print ("Failed to start up pipeline!\n");
return -1;
}
gtk_widget_show_all (window);
gtk_main ();
gst_deinit ();
return 0;
}

View file

@ -0,0 +1,184 @@
/*
* GStreamer
* Copyright (C) 2014 Matthew Waters <matthew@centricular.com>
*
* 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <gtk/gtk.h>
#include <gst/gst.h>
static void
button_state_null_cb (GtkWidget * widget, GstElement * pipeline)
{
gst_element_set_state (pipeline, GST_STATE_NULL);
g_print ("GST_STATE_NULL\n");
}
static void
button_state_ready_cb (GtkWidget * widget, GstElement * pipeline)
{
gst_element_set_state (pipeline, GST_STATE_READY);
g_print ("GST_STATE_READY\n");
}
static void
button_state_paused_cb (GtkWidget * widget, GstElement * pipeline)
{
gst_element_set_state (pipeline, GST_STATE_PAUSED);
g_print ("GST_STATE_PAUSED\n");
}
static void
button_state_playing_cb (GtkWidget * widget, GstElement * pipeline)
{
gst_element_set_state (pipeline, GST_STATE_PLAYING);
g_print ("GST_STATE_PLAYING\n");
}
static void
end_stream_cb (GstBus * bus, GstMessage * message, GstElement * pipeline)
{
g_print ("End of stream\n");
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
gtk_main_quit ();
}
static void
destroy_cb (GtkWidget * widget, GdkEvent * event, GstElement * pipeline)
{
g_print ("Close\n");
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
gtk_main_quit ();
}
int
main (int argc, char *argv[])
{
GtkWidget *window, *window_control;
GtkWidget *button_state_null, *button_state_ready;
GtkWidget *button_state_paused, *button_state_playing;
GtkWidget *grid, *area;
GstElement *pipeline;
GstElement *videosrc, *videosink;
GstStateChangeReturn ret;
GstCaps *caps;
GstBus *bus;
gst_init (&argc, &argv);
gtk_init (&argc, &argv);
pipeline = gst_pipeline_new ("pipeline");
//window that contains an area where the video is drawn
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size (GTK_WINDOW (window), 640, 480);
gtk_window_move (GTK_WINDOW (window), 300, 10);
gtk_window_set_title (GTK_WINDOW (window), "gtkgstwidget");
//window to control the states
window_control = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_resizable (GTK_WINDOW (window_control), FALSE);
gtk_window_move (GTK_WINDOW (window_control), 10, 10);
grid = gtk_grid_new ();
gtk_container_add (GTK_CONTAINER (window_control), grid);
//control state null
button_state_null = gtk_button_new_with_label ("GST_STATE_NULL");
g_signal_connect (G_OBJECT (button_state_null), "clicked",
G_CALLBACK (button_state_null_cb), pipeline);
gtk_grid_attach (GTK_GRID (grid), button_state_null, 0, 1, 1, 1);
gtk_widget_show (button_state_null);
//control state ready
button_state_ready = gtk_button_new_with_label ("GST_STATE_READY");
g_signal_connect (G_OBJECT (button_state_ready), "clicked",
G_CALLBACK (button_state_ready_cb), pipeline);
gtk_grid_attach (GTK_GRID (grid), button_state_ready, 0, 2, 1, 1);
gtk_widget_show (button_state_ready);
//control state paused
button_state_paused = gtk_button_new_with_label ("GST_STATE_PAUSED");
g_signal_connect (G_OBJECT (button_state_paused), "clicked",
G_CALLBACK (button_state_paused_cb), pipeline);
gtk_grid_attach (GTK_GRID (grid), button_state_paused, 0, 3, 1, 1);
gtk_widget_show (button_state_paused);
//control state playing
button_state_playing = gtk_button_new_with_label ("GST_STATE_PLAYING");
g_signal_connect (G_OBJECT (button_state_playing), "clicked",
G_CALLBACK (button_state_playing_cb), pipeline);
gtk_grid_attach (GTK_GRID (grid), button_state_playing, 0, 4, 1, 1);
gtk_widget_show (button_state_playing);
gtk_widget_show (grid);
gtk_widget_show (window_control);
g_signal_connect (G_OBJECT (window), "delete-event", G_CALLBACK (destroy_cb),
pipeline);
//configure the pipeline
videosrc = gst_element_factory_make ("videotestsrc", "videotestsrc");
videosink = gst_element_factory_make ("gtksink", "gtksink");
g_object_get (videosink, "widget", &area, NULL);
gtk_container_add (GTK_CONTAINER (window), area);
gtk_widget_realize (area);
caps = gst_caps_new_simple ("video/x-raw",
"width", G_TYPE_INT, 640,
"height", G_TYPE_INT, 480, "format", G_TYPE_STRING, "BGRA", NULL);
gst_bin_add_many (GST_BIN (pipeline), videosrc, videosink, NULL);
if (!gst_element_link_filtered (videosrc, videosink, caps)) {
gst_caps_unref (caps);
g_warning ("Failed to link videosrc to glfiltercube!\n");
return -1;
}
gst_caps_unref (caps);
//set window id on this event
bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
g_signal_connect (bus, "message::error", G_CALLBACK (end_stream_cb),
pipeline);
g_signal_connect (bus, "message::warning", G_CALLBACK (end_stream_cb),
pipeline);
g_signal_connect (bus, "message::eos", G_CALLBACK (end_stream_cb), pipeline);
gst_object_unref (bus);
//start
ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
if (ret == GST_STATE_CHANGE_FAILURE) {
g_print ("Failed to start up pipeline!\n");
return -1;
}
gtk_widget_show_all (window);
gtk_main ();
gst_deinit ();
return 0;
}