2011-01-28 00:14:04 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) <2011> Jon Nordby <jononor@gmail.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
|
2012-11-04 00:07:18 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2011-01-28 00:14:04 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:element-cairooverlay
|
|
|
|
*
|
|
|
|
* cairooverlay renders an overlay using a application provided render function.
|
|
|
|
*
|
|
|
|
* The full example can be found in tests/examples/cairo/cairo_overlay.c
|
|
|
|
* <refsect2>
|
|
|
|
* <title>Example code</title>
|
|
|
|
* |[
|
|
|
|
*
|
|
|
|
* #include <gst/gst.h>
|
|
|
|
* #include <gst/video/video.h>
|
|
|
|
*
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* typedef struct {
|
|
|
|
* gboolean valid;
|
|
|
|
* int width;
|
|
|
|
* int height;
|
|
|
|
* } CairoOverlayState;
|
|
|
|
*
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* static void
|
|
|
|
* prepare_overlay (GstElement * overlay, GstCaps * caps, gpointer user_data)
|
|
|
|
* {
|
|
|
|
* CairoOverlayState *state = (CairoOverlayState *)user_data;
|
|
|
|
*
|
|
|
|
* gst_video_format_parse_caps (caps, NULL, &state->width, &state->height);
|
|
|
|
* state->valid = TRUE;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* static void
|
|
|
|
* draw_overlay (GstElement * overlay, cairo_t * cr, guint64 timestamp,
|
|
|
|
* guint64 duration, gpointer user_data)
|
|
|
|
* {
|
|
|
|
* CairoOverlayState *s = (CairoOverlayState *)user_data;
|
|
|
|
* double scale;
|
|
|
|
*
|
|
|
|
* if (!s->valid)
|
|
|
|
* return;
|
|
|
|
*
|
|
|
|
* scale = 2*(((timestamp/(int)1e7) % 70)+30)/100.0;
|
|
|
|
* cairo_translate(cr, s->width/2, (s->height/2)-30);
|
|
|
|
* cairo_scale (cr, scale, scale);
|
|
|
|
*
|
|
|
|
* cairo_move_to (cr, 0, 0);
|
|
|
|
* cairo_curve_to (cr, 0,-30, -50,-30, -50,0);
|
|
|
|
* cairo_curve_to (cr, -50,30, 0,35, 0,60 );
|
|
|
|
* cairo_curve_to (cr, 0,35, 50,30, 50,0 ); *
|
|
|
|
* cairo_curve_to (cr, 50,-30, 0,-30, 0,0 );
|
|
|
|
* cairo_set_source_rgba (cr, 0.9, 0.0, 0.1, 0.7);
|
|
|
|
* cairo_fill (cr);
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* cairo_overlay = gst_element_factory_make ("cairooverlay", "overlay");
|
|
|
|
*
|
|
|
|
* g_signal_connect (cairo_overlay, "draw", G_CALLBACK (draw_overlay),
|
|
|
|
* overlay_state);
|
|
|
|
* g_signal_connect (cairo_overlay, "caps-changed",
|
|
|
|
* G_CALLBACK (prepare_overlay), overlay_state);
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* ]|
|
|
|
|
* </refsect2>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gstcairooverlay.h"
|
|
|
|
|
|
|
|
#include <gst/video/video.h>
|
|
|
|
|
|
|
|
#include <cairo.h>
|
|
|
|
|
|
|
|
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
|
2013-01-01 11:52:09 +00:00
|
|
|
#define TEMPLATE_CAPS GST_VIDEO_CAPS_MAKE("{ BGRx, BGRA }")
|
2011-01-28 00:14:04 +00:00
|
|
|
#else
|
2013-01-01 11:52:09 +00:00
|
|
|
#define TEMPLATE_CAPS GST_VIDEO_CAPS_MAKE("{ xRGB, ARGB }")
|
2011-01-28 00:14:04 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_cairo_overlay_src_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2011-03-02 22:14:36 +00:00
|
|
|
GST_STATIC_CAPS (TEMPLATE_CAPS)
|
2011-01-28 00:14:04 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_cairo_overlay_sink_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2011-03-02 22:14:36 +00:00
|
|
|
GST_STATIC_CAPS (TEMPLATE_CAPS)
|
2011-01-28 00:14:04 +00:00
|
|
|
);
|
|
|
|
|
2013-01-01 11:52:09 +00:00
|
|
|
G_DEFINE_TYPE (GstCairoOverlay, gst_cairo_overlay, GST_TYPE_VIDEO_FILTER);
|
2011-01-28 00:14:04 +00:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
SIGNAL_DRAW,
|
|
|
|
SIGNAL_CAPS_CHANGED,
|
|
|
|
N_SIGNALS
|
|
|
|
};
|
2011-03-02 22:14:36 +00:00
|
|
|
|
2011-01-28 00:14:04 +00:00
|
|
|
static guint gst_cairo_overlay_signals[N_SIGNALS];
|
|
|
|
|
|
|
|
static gboolean
|
2013-01-01 11:52:09 +00:00
|
|
|
gst_cairo_overlay_set_info (GstVideoFilter * vfilter, GstCaps * in_caps,
|
|
|
|
GstVideoInfo * in_info, GstCaps * out_caps, GstVideoInfo * out_info)
|
2011-01-28 00:14:04 +00:00
|
|
|
{
|
2013-01-01 11:52:09 +00:00
|
|
|
GstCairoOverlay *overlay = GST_CAIRO_OVERLAY (vfilter);
|
2011-01-28 00:14:04 +00:00
|
|
|
|
|
|
|
g_signal_emit (overlay, gst_cairo_overlay_signals[SIGNAL_CAPS_CHANGED], 0,
|
2013-01-01 11:52:09 +00:00
|
|
|
in_caps, NULL);
|
2011-01-28 00:14:04 +00:00
|
|
|
|
2013-01-01 11:52:09 +00:00
|
|
|
return TRUE;
|
2011-01-28 00:14:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2013-01-01 11:52:09 +00:00
|
|
|
gst_cairo_overlay_transform_frame_ip (GstVideoFilter * vfilter,
|
|
|
|
GstVideoFrame * frame)
|
2011-01-28 00:14:04 +00:00
|
|
|
{
|
2013-01-01 11:52:09 +00:00
|
|
|
GstCairoOverlay *overlay = GST_CAIRO_OVERLAY (vfilter);
|
2011-01-28 00:14:04 +00:00
|
|
|
cairo_surface_t *surface;
|
|
|
|
cairo_t *cr;
|
|
|
|
cairo_format_t format;
|
|
|
|
|
2013-01-01 11:52:09 +00:00
|
|
|
if (GST_VIDEO_FRAME_N_COMPONENTS (frame) == 4)
|
|
|
|
format = CAIRO_FORMAT_ARGB32;
|
|
|
|
else
|
|
|
|
format = CAIRO_FORMAT_RGB24;
|
2011-01-28 00:14:04 +00:00
|
|
|
|
|
|
|
surface =
|
2013-01-01 11:52:09 +00:00
|
|
|
cairo_image_surface_create_for_data (GST_VIDEO_FRAME_PLANE_DATA (frame,
|
|
|
|
0), format, GST_VIDEO_FRAME_WIDTH (frame),
|
|
|
|
GST_VIDEO_FRAME_HEIGHT (frame), GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0));
|
|
|
|
|
2011-01-28 00:14:04 +00:00
|
|
|
if (G_UNLIKELY (!surface))
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
|
|
|
|
cr = cairo_create (surface);
|
|
|
|
if (G_UNLIKELY (!cr)) {
|
|
|
|
cairo_surface_destroy (surface);
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_signal_emit (overlay, gst_cairo_overlay_signals[SIGNAL_DRAW], 0,
|
2013-01-01 11:52:09 +00:00
|
|
|
cr, GST_BUFFER_PTS (frame->buffer), GST_BUFFER_DURATION (frame->buffer),
|
|
|
|
NULL);
|
2011-01-28 00:14:04 +00:00
|
|
|
|
|
|
|
cairo_destroy (cr);
|
|
|
|
cairo_surface_destroy (surface);
|
2011-03-02 22:14:36 +00:00
|
|
|
|
2011-01-28 00:14:04 +00:00
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_cairo_overlay_class_init (GstCairoOverlayClass * klass)
|
|
|
|
{
|
2013-01-01 11:52:09 +00:00
|
|
|
GstVideoFilterClass *vfilter_class;
|
|
|
|
GstElementClass *element_class;
|
2011-01-28 00:14:04 +00:00
|
|
|
|
2013-01-01 11:52:09 +00:00
|
|
|
vfilter_class = (GstVideoFilterClass *) klass;
|
|
|
|
element_class = (GstElementClass *) klass;
|
2011-01-28 00:14:04 +00:00
|
|
|
|
2013-01-01 11:52:09 +00:00
|
|
|
vfilter_class->set_info = gst_cairo_overlay_set_info;
|
|
|
|
vfilter_class->transform_frame_ip = gst_cairo_overlay_transform_frame_ip;
|
2011-01-28 00:14:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GstCairoOverlay::draw:
|
|
|
|
* @overlay: Overlay element emitting the signal.
|
|
|
|
* @cr: Cairo context to draw to.
|
2011-03-02 22:14:36 +00:00
|
|
|
* @timestamp: Timestamp (see #GstClockTime) of the current buffer.
|
|
|
|
* @duration: Duration (see #GstClockTime) of the current buffer.
|
2011-01-28 00:14:04 +00:00
|
|
|
*
|
|
|
|
* This signal is emitted when the overlay should be drawn.
|
|
|
|
*/
|
|
|
|
gst_cairo_overlay_signals[SIGNAL_DRAW] =
|
|
|
|
g_signal_new ("draw",
|
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2013-01-01 11:52:09 +00:00
|
|
|
g_cclosure_marshal_generic,
|
2011-01-28 00:14:04 +00:00
|
|
|
G_TYPE_NONE, 3, CAIRO_GOBJECT_TYPE_CONTEXT, G_TYPE_UINT64, G_TYPE_UINT64);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GstCairoOverlay::caps-changed:
|
|
|
|
* @overlay: Overlay element emitting the signal.
|
2011-03-02 22:14:36 +00:00
|
|
|
* @caps: The #GstCaps of the element.
|
2011-01-28 00:14:04 +00:00
|
|
|
*
|
|
|
|
* This signal is emitted when the caps of the element has changed.
|
|
|
|
*/
|
|
|
|
gst_cairo_overlay_signals[SIGNAL_CAPS_CHANGED] =
|
|
|
|
g_signal_new ("caps-changed",
|
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
0,
|
2013-01-01 11:52:09 +00:00
|
|
|
0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_CAPS);
|
|
|
|
|
|
|
|
gst_element_class_set_static_metadata (element_class, "Cairo overlay",
|
|
|
|
"Filter/Editor/Video",
|
|
|
|
"Render overlay on a video stream using Cairo",
|
|
|
|
"Jon Nordby <jononor@gmail.com>");
|
|
|
|
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&gst_cairo_overlay_sink_template));
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&gst_cairo_overlay_src_template));
|
2011-01-28 00:14:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-01-01 11:52:09 +00:00
|
|
|
gst_cairo_overlay_init (GstCairoOverlay * overlay)
|
2011-01-28 00:14:04 +00:00
|
|
|
{
|
2013-01-01 11:52:09 +00:00
|
|
|
/* nothing to do */
|
2011-01-28 00:14:04 +00:00
|
|
|
}
|