Include CoglGst

CoglGst is a GStreamer integration library that facilitates
video playback using the Cogl API. It works by retrieving
each video frame from the GStreamer pipeline and attaching
it to a Cogl pipeline in the form of a Cogl texture along
with possible color model conversion shaders. The pipeline
is then retrieved by the user during each draw. An example
use of the CoglGst API is included in the examples directory.

Reviewed-by: Neil Roberts <neil@linux.intel.com>

(cherry picked from commit dfb94cf4b0b6b42d6465df362a0c0af780596890)
This commit is contained in:
Plamena Manolova 2013-02-22 14:56:05 +00:00 committed by Lionel Landwerlin
parent 29b01c2aba
commit 1eca1631a7
13 changed files with 1922 additions and 1 deletions

1
.gitignore vendored
View File

@ -70,6 +70,7 @@ depcomp
/examples/cogl-x11-foreign
/examples/cogl-x11-tfp
/examples/cogland
/examples/cogl-basic-video-player
gtk-doc.make
install-sh
libtool

View File

@ -8,6 +8,10 @@ if BUILD_COGL_GLES2
SUBDIRS += cogl-gles2
endif
if BUILD_COGL_GST
SUBDIRS += cogl-gst
endif
SUBDIRS += examples doc po build
ACLOCAL_AMFLAGS = -I build/autotools ${ACLOCAL_FLAGS}
@ -32,6 +36,7 @@ DISTCHECK_CONFIGURE_FLAGS = \
--enable-xlib-egl-platform \
--enable-wayland-egl-platform \
--enable-glx \
--enable-wayland-egl-server
--enable-wayland-egl-server \
--enable-cogl-gst
include $(top_srcdir)/build/autotools/Makefile.am.release

75
cogl-gst/Makefile.am Normal file
View File

@ -0,0 +1,75 @@
include $(top_srcdir)/build/autotools/Makefile.am.silent
NULL =
CLEANFILES =
DISTCLEANFILES =
EXTRA_DIST =
source_c = \
cogl-gst-shader.c \
cogl-gst-video-sink.c \
$(NULL)
source_h = \
cogl-gst.h \
cogl-gst-video-sink.h \
$(NULL)
source_h_priv = \
cogl-gst-shader-private.h \
$(NULL)
lib_LTLIBRARIES = libcogl-gst.la
libcogl_gst_la_SOURCES = $(source_c) $(source_h) $(source_h_priv)
libcogl_gst_la_CFLAGS = $(COGL_DEP_CFLAGS) $(COGL_GST_DEP_CFLAGS) $(COGL_EXTRA_CFLAGS) $(MAINTAINER_CFLAGS)
libcogl_gst_la_LIBADD = $(top_builddir)/cogl/libcogl.la
libcogl_gst_la_LIBADD += $(COGL_DEP_LIBS) $(COGL_GST_DEP_LIBS) $(COGL_EXTRA_LDFLAGS)
libcogl_gst_la_LDFLAGS = \
-export-dynamic \
-export-symbols-regex "^cogl_gst_.*" \
-no-undefined \
-version-info @COGL_LT_CURRENT@:@COGL_LT_REVISION@:@COGL_LT_AGE@ \
-rpath $(libdir)
AM_CPPFLAGS = \
-DCOGL_COMPILATION \
-DG_LOG_DOMAIN=\"CoglGst\" \
-I$(top_srcdir)/cogl \
-I$(top_builddir)/cogl \
-I$(top_srcdir)/cogl/winsys \
-I$(top_srcdir) \
-I$(top_builddir)
cogl_gstheadersdir = $(includedir)/cogl/cogl-gst
cogl_gstheaders_HEADERS = $(source_h)
plugin_source_c = \
$(srcdir)/cogl-gst-plugin.c \
$(NULL)
libgstcogl_la_SOURCES = \
$(plugin_source_c) \
$(NULL)
plugin_LTLIBRARIES = libgstcogl.la
libgstcogl_la_CFLAGS = $(COGL_DEP_CFLAGS) $(COGL_GST_DEP_CFLAGS) $(COGL_EXTRA_CFLAGS) $(MAINTAINER_CFLAGS)
libgstcogl_la_LIBADD = \
$(top_builddir)/cogl/libcogl.la \
$(builddir)/libcogl-gst.la
libgstcogl_la_LIBADD += $(COGL_DEP_LIBS) $(COGL_GST_DEP_LIBS) $(COGL_EXTRA_LDFLAGS)
libgstcogl_la_LDFLAGS = \
-avoid-version -no-undefined \
$(NULL)
pc_files = cogl-gst.pc
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = $(pc_files)
EXTRA_DIST += cogl-gst.pc.in
DISTCLEANFILES += $(pc_files)

View File

@ -0,0 +1,62 @@
/*
* Cogl-GStreamer.
*
* GStreamer integration library for Cogl.
*
* cogl-gst-video-sink.c - Gstreamer Video Sink that renders to a
* Cogl Pipeline.
*
* Authored by Jonathan Matthew <jonathan@kaolin.wh9.net>,
* Chris Lord <chris@openedhand.com>
* Damien Lespiau <damien.lespiau@intel.com>
* Matthew Allum <mallum@openedhand.com>
* Plamena Manolova <plamena.n.manolova@intel.com>
*
* Copyright (C) 2007, 2008 OpenedHand
* Copyright (C) 2009, 2010, 2013 Intel Corporation
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gst/gst.h>
#include "cogl-gst-video-sink.h"
#define PACKAGE "CoglGst"
#define VERSION "0.0"
static CoglBool
_plugin_init (GstPlugin * coglgstvideosink)
{
return gst_element_register (coglgstvideosink, "coglsink", GST_RANK_PRIMARY,
COGL_GST_TYPE_VIDEO_SINK);
}
GST_PLUGIN_DEFINE (
GST_VERSION_MAJOR,
GST_VERSION_MINOR,
cogl,
"Sends video data from GStreamer to a Cogl pipeline",
_plugin_init,
VERSION,
"LGPL",
"CoglGst",
"http://gstreamer.net/"
)

View File

@ -0,0 +1,48 @@
/*
* Cogl-GStreamer.
*
* GStreamer integration library for Cogl.
*
* cogl-gst-video-sink-private.h - Miscellaneous video sink functions
*
* Authored by Jonathan Matthew <jonathan@kaolin.wh9.net>,
* Chris Lord <chris@openedhand.com>
* Damien Lespiau <damien.lespiau@intel.com>
* Matthew Allum <mallum@openedhand.com>
* Plamena Manolova <plamena.n.manolova@intel.com>
*
* Copyright (C) 2007, 2008 OpenedHand
* Copyright (C) 2009, 2010, 2013 Intel Corporation
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __COGL_GST_SHADER_PRIVATE_H__
#define __COGL_GST_SHADER_PRIVATE_H__
extern const char
_cogl_gst_shader_rgba_to_rgba_decl[];
extern const char
_cogl_gst_shader_yv12_to_rgba_decl[];
extern const char
_cogl_gst_shader_ayuv_to_rgba_decl[];
extern const char
_cogl_gst_shader_default_sample[];
#endif /* __COGL_GST_SHADER_PRIVATE_H__ */

View File

@ -0,0 +1,75 @@
/*
* Cogl-GStreamer.
*
* GStreamer integration library for Cogl.
*
* cogl-gst-video-sink-private.h - Miscellaneous video sink functions
*
* Authored by Jonathan Matthew <jonathan@kaolin.wh9.net>,
* Chris Lord <chris@openedhand.com>
* Damien Lespiau <damien.lespiau@intel.com>
* Matthew Allum <mallum@openedhand.com>
* Plamena Manolova <plamena.n.manolova@intel.com>
*
* Copyright (C) 2007, 2008 OpenedHand
* Copyright (C) 2009, 2010, 2013 Intel Corporation
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "cogl-gst-shader-private.h"
const char
_cogl_gst_shader_rgba_to_rgba_decl[] =
"vec4 cogl_gst_sample_video (vec2 UV) {\n"
" return texture2D (cogl_sampler0, UV);\n"
"}";
const char
_cogl_gst_shader_yv12_to_rgba_decl[] =
"vec4 cogl_gst_sample_video (vec2 UV) {\n"
" float y = 1.1640625 * (texture2D (cogl_sampler0, UV).g - 0.0625);\n"
" float u = texture2D (cogl_sampler1, UV).g - 0.5;\n"
" float v = texture2D (cogl_sampler2, UV).g - 0.5;\n"
" vec4 color;\n"
" color.r = y + 1.59765625 * v;\n"
" color.g = y - 0.390625 * u - 0.8125 * v;\n"
" color.b = y + 2.015625 * u;\n"
" color.a = 1.0;\n"
" return color;\n"
"}";
const char
_cogl_gst_shader_ayuv_to_rgba_decl[] =
"vec4 cogl_gst_sample_video (vec2 UV) {\n"
" vec4 color = texture2D (cogl_sampler0, UV);\n"
" float y = 1.1640625 * (color.g - 0.0625);\n"
" float u = color.b - 0.5;\n"
" float v = color.a - 0.5;\n"
" color.a = color.r;\n"
" color.r = y + 1.59765625 * v;\n"
" color.g = y - 0.390625 * u - 0.8125 * v;\n"
" color.b = y + 2.015625 * u;\n"
" return color;\n"
"}";
const char
_cogl_gst_shader_default_sample[] =
"cogl_layer = cogl_gst_sample_video (cogl_tex_coord0_in.st);";

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,121 @@
/*
* Cogl-GStreamer.
*
* GStreamer integration library for Cogl.
*
* cogl-gst-video-sink.h - Gstreamer Video Sink that renders to a
* Cogl Pipeline.
*
* Authored by Jonathan Matthew <jonathan@kaolin.wh9.net>,
* Chris Lord <chris@openedhand.com>
* Damien Lespiau <damien.lespiau@intel.com>
* Matthew Allum <mallum@openedhand.com>
* Plamena Manolova <plamena.n.manolova@intel.com>
*
* Copyright (C) 2007, 2008 OpenedHand
* Copyright (C) 2009, 2010, 2013 Intel Corporation
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
FLOP
*/
#ifndef __COGL_GST_VIDEO_SINK_H__
#define __COGL_GST_VIDEO_SINK_H__
#include <glib-object.h>
#include <gst/base/gstbasesink.h>
#include <cogl/cogl.h>
G_BEGIN_DECLS
#define COGL_GST_TYPE_VIDEO_SINK cogl_gst_video_sink_get_type()
#define COGL_GST_VIDEO_SINK(obj) \
(G_TYPE_CHECK_INSTANCE_CAST ((obj), \
COGL_GST_TYPE_VIDEO_SINK, CoglGstVideoSink))
#define COGL_GST_VIDEO_SINK_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST ((klass), \
COGL_GST_TYPE_VIDEO_SINK, CoglGstVideoSinkClass))
#define COGL_GST_IS_VIDEO_SINK(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
COGL_GST_TYPE_VIDEO_SINK))
#define COGL_GST_IS_VIDEO_SINK_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE ((klass), \
COGL_GST_TYPE_VIDEO_SINK))
#define COGL_GST_VIDEO_SINK_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS ((obj), \
COGL_GST_TYPE_VIDEO_SINK, CoglGstVideoSinkClass))
#define COGL_GST_PARAM_STATIC \
(G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
#define COGL_GST_PARAM_READABLE \
(G_PARAM_READABLE | COGL_GST_PARAM_STATIC)
#define COGL_GST_PARAM_WRITABLE \
(G_PARAM_WRITABLE | COGL_GST_PARAM_STATIC)
#define COGL_GST_PARAM_READWRITE \
(G_PARAM_READABLE | G_PARAM_WRITABLE | COGL_GST_PARAM_STATIC)
typedef struct _CoglGstVideoSink CoglGstVideoSink;
typedef struct _CoglGstVideoSinkClass CoglGstVideoSinkClass;
typedef struct _CoglGstVideoSinkPrivate CoglGstVideoSinkPrivate;
struct _CoglGstVideoSink
{
GstBaseSink parent;
CoglGstVideoSinkPrivate *priv;
};
struct _CoglGstVideoSinkClass
{
GstBaseSinkClass parent_class;
void (* new_frame) (CoglGstVideoSink *sink);
void (* pipeline_ready) (CoglGstVideoSink *sink);
void *_padding_dummy[8];
};
GType cogl_gst_video_sink_get_type (void) G_GNUC_CONST;
CoglGstVideoSink*
cogl_gst_video_sink_new (CoglContext *ctx);
CoglPipeline*
cogl_gst_video_sink_get_pipeline (CoglGstVideoSink *vt);
void
cogl_gst_video_sink_set_context (CoglGstVideoSink *vt,
CoglContext *ctx);
GMainLoop*
cogl_gst_video_sink_get_main_loop (CoglGstVideoSink *loop);
int
cogl_gst_video_sink_get_free_layer (CoglGstVideoSink *sink);
int
cogl_gst_video_sink_attach_frame (CoglGstVideoSink *sink,
CoglPipeline *pln);
G_END_DECLS
#endif

38
cogl-gst/cogl-gst.h Normal file
View File

@ -0,0 +1,38 @@
/*
* Cogl-GStreamer.
*
* GStreamer integration library for Cogl.
*
* cogl-gst.h - Top level header file
*
* Authored by Jonathan Matthew <jonathan@kaolin.wh9.net>,
* Chris Lord <chris@openedhand.com>
* Damien Lespiau <damien.lespiau@intel.com>
* Matthew Allum <mallum@openedhand.com>
* Plamena Manolova <plamena.n.manolova@intel.com>
*
* Copyright (C) 2007, 2008 OpenedHand
* Copyright (C) 2009, 2010, 2013 Intel Corporation
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __COGL_GST_H__
#define __COGL_GST_H__
#include <cogl-gst/cogl-gst-video-sink.h>
#endif

13
cogl-gst/cogl-gst.pc.in Normal file
View File

@ -0,0 +1,13 @@
prefix=@prefix@
exec_prefix=@exec_prefix@
libdir=@libdir@
includedir=@includedir@
apiversion=@COGL_API_VERSION@
requires=@COGL_PKG_REQUIRES@ @COGL_GST_PKG_REQUIRES@
Name: Cogl
Description: An gstreamer integration library for Cogl
Version: @COGL_VERSION@
Libs: -L${libdir} -lcogl-gst
Cflags: -I${includedir}/cogl -DCOGL_ENABLE_EXPERIMENTAL_2_0_API
Requires: ${requires}

View File

@ -486,6 +486,40 @@ AS_IF([test "x$enable_cogl_pango" = "xyes"],
]
)
dnl ============================================================
dnl Should cogl-gst be built?
dnl ============================================================
AS_IF([test "x$enable_glib" != "xyes"],
[
AS_IF([test "x$enable_cogl_gst" = "xyes"],
AC_MSG_ERROR([--enable-cogl-gst conflicts with --disable-glib]))
enable_cogl_gst=no
]
)
AC_ARG_ENABLE(
[cogl-gst],
[AC_HELP_STRING([--enable-cogl-gst=@<:@no/yes@:>@], [Enable gstreamer support @<:@default=no@:>@])],
[],
enable_cogl_gst=no
)
AS_IF([test "x$enable_cogl_gst" = "xyes"],
[
COGL_GST_PKG_REQUIRES="$COGL_GST_PKG_REQUIRES gstreamer-1.0 gstreamer-fft-1.0 \
gstreamer-audio-1.0 gstreamer-base-1.0 \
gstreamer-video-1.0 gstreamer-plugins-base-1.0 \
gstreamer-tag-1.0 gstreamer-controller-1.0"
GST_MAJORMINOR=1.0
dnl define location of gstreamer plugin directory
plugindir="\$(libdir)/gstreamer-$GST_MAJORMINOR"
AC_SUBST(plugindir)
]
)
dnl ============================================================
dnl Choose image loading backend
dnl ============================================================
@ -1222,6 +1256,20 @@ AS_IF([test "x$enable_cogl_pango" = "xyes"],
)
AM_CONDITIONAL([BUILD_COGL_PANGO], [test "x$enable_cogl_pango" = "xyes"])
AC_SUBST(COGL_GST_PKG_REQUIRES)
AS_IF([test "x$enable_cogl_gst" = "xyes"],
[PKG_CHECK_MODULES(COGL_GST_DEP, [$COGL_GST_PKG_REQUIRES])]
)
AM_CONDITIONAL([BUILD_COGL_GST], [test "x$enable_cogl_gst" = "xyes"])
have_hw_decoder_support=no
#PKG_CHECK_MODULES(HW, [$COGL_GST_PKG_REQUIRES], [have_hw_decoder_support=yes
# AC_DEFINE([HAVE_HW_DECODER_SUPPORT], [1],
# ["Defined if building Cogl-Gst with HW decoder support"])
# ],[ have_hw_decoder_support=no ])
dnl ================================================================
dnl Misc program dependencies.
@ -1354,6 +1402,8 @@ AC_SUBST(COGL_DEP_CFLAGS)
AC_SUBST(COGL_DEP_LIBS)
AC_SUBST(COGL_PANGO_DEP_CFLAGS)
AC_SUBST(COGL_PANGO_DEP_LIBS)
AC_SUBST(COGL_GST_DEP_CFLAGS)
AC_SUBST(COGL_GST_DEP_LIBS)
AC_SUBST(COGL_EXTRA_CFLAGS)
AC_SUBST(COGL_EXTRA_LDFLAGS)
@ -1387,6 +1437,8 @@ cogl-pango/Makefile
cogl-pango/cogl-pango-1.0.pc
cogl-pango/cogl-pango-2.0-experimental.pc
cogl-pango/cogl-pango.rc
cogl-gst/Makefile
cogl-gst/cogl-gst.pc
cogl-gles2/Makefile
cogl-gles2/cogl-gles2-experimental.pc
doc/Makefile
@ -1442,6 +1494,8 @@ echo " Build libcogl-gles2 GLES 2.0 frontend api: ${enable_cogl_gles2}"
echo " Image backend: ${COGL_IMAGE_BACKEND}"
echo " Cogl Pango: ${enable_cogl_pango}"
echo " Profiling: ${enable_profile}"
echo " CoglGst: ${enable_cogl_gst}"
echo " CoglGst has HW suppport: ${have_hw_decoder_support}"
# Compiler/Debug related flags
echo ""

View File

@ -50,6 +50,13 @@ cogl_crate_CFLAGS = $(AM_CFLAGS) $(COGL_PANGO_DEP_CFLAGS)
examples_data_DATA += crate.jpg
endif
if BUILD_COGL_GST
programs += cogl-basic-video-player
cogl_basic_video_player_SOURCES = cogl-basic-video-player.c
cogl_basic_video_player_LDADD = $(common_ldadd) $(COGL_GST_DEP_LIBS) $(top_builddir)/cogl-gst/libcogl-gst.la
cogl_basic_video_player_CFLAGS = $(AM_CFLAGS) $(COGL_GST_DEP_CFLAGS) -I$(top_builddir)/cogl-gst
endif
if X11_TESTS
programs += cogl-x11-foreign cogl-x11-tfp
cogl_x11_foreign_SOURCES = cogl-x11-foreign.c

View File

@ -0,0 +1,248 @@
#include <cogl/cogl.h>
#include <cogl-gst/cogl-gst.h>
typedef struct _Data
{
CoglFramebuffer *fb;
CoglPipeline *pln;
CoglGstVideoSink *sink;
CoglBool draw_ready;
CoglBool frame_ready;
GMainLoop *main_loop;
}Data;
static CoglBool
_bus_watch (GstBus *bus,
GstMessage *msg,
void *user_data)
{
Data *data = (Data*) user_data;
switch (GST_MESSAGE_TYPE (msg))
{
case GST_MESSAGE_EOS:
{
g_main_loop_quit (data->main_loop);
break;
}
case GST_MESSAGE_ERROR:
{
char *debug;
GError *error = NULL;
gst_message_parse_error (msg, &error, &debug);
g_free (debug);
if (error != NULL)
{
g_error ("Playback error: %s\n", error->message);
g_error_free (error);
}
g_main_loop_quit (data->main_loop);
break;
}
default:
break;
}
return TRUE;
}
static void
_draw (Data *data)
{
/*
The cogl pipeline needs to be retrieved from the sink before every draw.
This is due to the cogl-gst sink creating a new cogl pipeline for each frame
by copying the previous one and attaching the new frame to it.
*/
CoglPipeline* current = cogl_gst_video_sink_get_pipeline (data->sink);
cogl_framebuffer_clear4f (data->fb,
COGL_BUFFER_BIT_COLOR|COGL_BUFFER_BIT_DEPTH, 0,
0, 0, 1);
data->pln = current;
cogl_framebuffer_push_matrix (data->fb);
cogl_framebuffer_translate (data->fb, 640 / 2, 480 / 2, 0);
cogl_framebuffer_draw_textured_rectangle (data->fb, data->pln, -320, -240,
320, 240, 0, 0, 1, 1);
cogl_framebuffer_pop_matrix (data->fb);
cogl_onscreen_swap_buffers (COGL_ONSCREEN (data->fb));
}
static void
_check_draw (Data *data)
{
/* The frame is only drawn once we know that a new buffer is ready
* from GStreamer and that Cogl is ready to accept some new
* rendering */
if (data->draw_ready && data->frame_ready)
{
_draw (data);
data->draw_ready = FALSE;
data->frame_ready = FALSE;
}
}
static void
_frame_callback (CoglOnscreen *onscreen,
CoglFrameEvent event,
CoglFrameInfo *info,
void *user_data)
{
Data *data = user_data;
if (event == COGL_FRAME_EVENT_SYNC)
{
data->draw_ready = TRUE;
_check_draw (data);
}
}
static void
_new_frame_cb (CoglGstVideoSink *sink,
Data *data)
{
data->frame_ready = TRUE;
_check_draw (data);
}
/*
A callback like this should be attached to the cogl-pipeline-ready
signal. This way requesting the cogl pipeline before its creation
by the sink is avoided. At this point, user textures and snippets can
be added to the cogl pipeline.
*/
static void
_set_up_pipeline (gpointer instance,
gpointer user_data)
{
Data* data = (Data*) user_data;
/*
The cogl-gst sink, depending on the video format, can use up to 3 texture
layers to render a frame. To avoid overwriting frame data, the first
free layer in the cogl pipeline needs to be queried before adding any
additional textures.
*/
int free_layer = cogl_gst_video_sink_get_free_layer (data->sink);
data->pln = cogl_gst_video_sink_get_pipeline (data->sink);
while (free_layer > 0)
{
free_layer--;
cogl_pipeline_set_layer_filters (data->pln, free_layer,
COGL_PIPELINE_FILTER_LINEAR_MIPMAP_LINEAR,
COGL_PIPELINE_FILTER_LINEAR);
}
cogl_onscreen_add_frame_callback (COGL_ONSCREEN (data->fb), _frame_callback,
data, NULL);
/*
The cogl-gst-new-frame signal is emitted when the cogl-gst sink has
retrieved a new frame and attached it to the cogl pipeline. This can be
used to make sure cogl doesn't do any unnecessary drawing i.e. keeps to the
frame-rate of the video.
*/
g_signal_connect (data->sink, "new-frame", G_CALLBACK (_new_frame_cb), data);
}
int
main (int argc,
char **argv)
{
Data data;
CoglContext *ctx;
CoglOnscreen *onscreen;
CoglMatrix view;
float fovy, aspect, z_near, z_2d, z_far;
GstElement *pipeline;
GstElement *bin;
GSource *cogl_source;
GstBus *bus;
char *uri;
/* Set the necessary cogl elements */
ctx = cogl_context_new (NULL, NULL);
onscreen = cogl_onscreen_new (ctx, 640, 480);
data.fb = COGL_FRAMEBUFFER (onscreen);
cogl_onscreen_show (onscreen);
cogl_framebuffer_set_viewport (data.fb, 0, 0, 640, 480);
fovy = 60;
aspect = 640 / 480;
z_near = 0.1;
z_2d = 1000;
z_far = 2000;
cogl_framebuffer_perspective (data.fb, fovy, aspect, z_near, z_far);
cogl_matrix_init_identity (&view);
cogl_matrix_view_2d_in_perspective (&view, fovy, aspect, z_near, z_2d,
640, 480);
cogl_framebuffer_set_modelview_matrix (data.fb, &view);
/* Intialize GStreamer */
gst_init (&argc, &argv);
/*
Create the cogl-gst video sink by calling the cogl_gst_video_sink_new
function and passing it a CoglContext (this is used to create the
CoglPipeline and the texures for each frame). Alternatively you can use
gst_element_factory_make ("coglsink", "some_name") and then set the
context with cogl_gst_video_sink_set_context.
*/
data.sink = cogl_gst_video_sink_new (ctx);
pipeline = gst_pipeline_new ("gst-player");
bin = gst_element_factory_make ("playbin", "bin");
if (argc < 2)
uri = "http://docs.gstreamer.com/media/sintel_trailer-480p.webm";
else
uri = argv[1];
g_object_set (G_OBJECT (bin), "video-sink", GST_ELEMENT (data.sink), NULL);
gst_bin_add (GST_BIN (pipeline), bin);
g_object_set (G_OBJECT (bin), "uri", uri, NULL);
gst_element_set_state (pipeline, GST_STATE_PLAYING);
bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
gst_bus_add_watch (bus, _bus_watch, &data);
data.main_loop = g_main_loop_new (NULL, FALSE);
cogl_source = cogl_glib_source_new (ctx, G_PRIORITY_DEFAULT);
g_source_attach (cogl_source, NULL);
/*
The cogl-pipeline-ready signal tells you when the cogl pipeline is
initialized i.e. when cogl-gst has figured out the video format and
is prepared to retrieve and attach the first frame of the video.
*/
g_signal_connect (data.sink, "pipeline-ready",
G_CALLBACK (_set_up_pipeline), &data);
data.draw_ready = TRUE;
data.frame_ready = FALSE;
g_main_loop_run (data.main_loop);
g_source_destroy (cogl_source);
g_source_unref (cogl_source);
g_main_loop_unref (data.main_loop);
return 0;
}