2 * Copyright (C) <2011> Jon Nordby <jononor@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * SECTION:element-cairooverlay
23 * cairooverlay renders an overlay using a application provided render function.
25 * The full example can be found in tests/examples/cairo/cairo_overlay.c
27 * <title>Example code</title>
30 * #include <gst/gst.h>
31 * #include <gst/video/video.h>
39 * } CairoOverlayState;
44 * prepare_overlay (GstElement * overlay, GstCaps * caps, gpointer user_data)
46 * CairoOverlayState *state = (CairoOverlayState *)user_data;
48 * gst_video_format_parse_caps (caps, NULL, &state->width, &state->height);
49 * state->valid = TRUE;
53 * draw_overlay (GstElement * overlay, cairo_t * cr, guint64 timestamp,
54 * guint64 duration, gpointer user_data)
56 * CairoOverlayState *s = (CairoOverlayState *)user_data;
62 * scale = 2*(((timestamp/(int)1e7) % 70)+30)/100.0;
63 * cairo_translate(cr, s->width/2, (s->height/2)-30);
64 * cairo_scale (cr, scale, scale);
66 * cairo_move_to (cr, 0, 0);
67 * cairo_curve_to (cr, 0,-30, -50,-30, -50,0);
68 * cairo_curve_to (cr, -50,30, 0,35, 0,60 );
69 * cairo_curve_to (cr, 0,35, 50,30, 50,0 ); *
70 * cairo_curve_to (cr, 50,-30, 0,-30, 0,0 );
71 * cairo_set_source_rgba (cr, 0.9, 0.0, 0.1, 0.7);
77 * cairo_overlay = gst_element_factory_make ("cairooverlay", "overlay");
79 * g_signal_connect (cairo_overlay, "draw", G_CALLBACK (draw_overlay),
81 * g_signal_connect (cairo_overlay, "caps-changed",
82 * G_CALLBACK (prepare_overlay), overlay_state);
93 #include "gstcairooverlay.h"
95 #include <gst/video/video.h>
99 /* RGB16 is native-endianness in GStreamer */
100 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
101 #define TEMPLATE_CAPS GST_VIDEO_CAPS_MAKE("{ BGRx, BGRA, RGB16 }")
103 #define TEMPLATE_CAPS GST_VIDEO_CAPS_MAKE("{ xRGB, ARGB, RGB16 }")
106 static GstStaticPadTemplate gst_cairo_overlay_src_template =
107 GST_STATIC_PAD_TEMPLATE ("src",
110 GST_STATIC_CAPS (TEMPLATE_CAPS)
113 static GstStaticPadTemplate gst_cairo_overlay_sink_template =
114 GST_STATIC_PAD_TEMPLATE ("sink",
117 GST_STATIC_CAPS (TEMPLATE_CAPS)
120 G_DEFINE_TYPE (GstCairoOverlay, gst_cairo_overlay, GST_TYPE_VIDEO_FILTER);
129 static guint gst_cairo_overlay_signals[N_SIGNALS];
132 gst_cairo_overlay_set_info (GstVideoFilter * vfilter, GstCaps * in_caps,
133 GstVideoInfo * in_info, GstCaps * out_caps, GstVideoInfo * out_info)
135 GstCairoOverlay *overlay = GST_CAIRO_OVERLAY (vfilter);
137 g_signal_emit (overlay, gst_cairo_overlay_signals[SIGNAL_CAPS_CHANGED], 0,
144 gst_cairo_overlay_transform_frame_ip (GstVideoFilter * vfilter,
145 GstVideoFrame * frame)
147 GstCairoOverlay *overlay = GST_CAIRO_OVERLAY (vfilter);
148 cairo_surface_t *surface;
150 cairo_format_t format;
152 switch (GST_VIDEO_FRAME_FORMAT (frame)) {
153 case GST_VIDEO_FORMAT_ARGB:
154 case GST_VIDEO_FORMAT_BGRA:
155 format = CAIRO_FORMAT_ARGB32;
157 case GST_VIDEO_FORMAT_xRGB:
158 case GST_VIDEO_FORMAT_BGRx:
159 format = CAIRO_FORMAT_RGB24;
161 case GST_VIDEO_FORMAT_RGB16:
162 format = CAIRO_FORMAT_RGB16_565;
166 GST_WARNING ("No matching cairo format for %s",
167 gst_video_format_to_string (GST_VIDEO_FRAME_FORMAT (frame)));
168 return GST_FLOW_ERROR;
173 cairo_image_surface_create_for_data (GST_VIDEO_FRAME_PLANE_DATA (frame,
174 0), format, GST_VIDEO_FRAME_WIDTH (frame),
175 GST_VIDEO_FRAME_HEIGHT (frame), GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0));
177 if (G_UNLIKELY (!surface))
178 return GST_FLOW_ERROR;
180 cr = cairo_create (surface);
181 if (G_UNLIKELY (!cr)) {
182 cairo_surface_destroy (surface);
183 return GST_FLOW_ERROR;
186 g_signal_emit (overlay, gst_cairo_overlay_signals[SIGNAL_DRAW], 0,
187 cr, GST_BUFFER_PTS (frame->buffer), GST_BUFFER_DURATION (frame->buffer),
191 cairo_surface_destroy (surface);
197 gst_cairo_overlay_class_init (GstCairoOverlayClass * klass)
199 GstVideoFilterClass *vfilter_class;
200 GstElementClass *element_class;
202 vfilter_class = (GstVideoFilterClass *) klass;
203 element_class = (GstElementClass *) klass;
205 vfilter_class->set_info = gst_cairo_overlay_set_info;
206 vfilter_class->transform_frame_ip = gst_cairo_overlay_transform_frame_ip;
209 * GstCairoOverlay::draw:
210 * @overlay: Overlay element emitting the signal.
211 * @cr: Cairo context to draw to.
212 * @timestamp: Timestamp (see #GstClockTime) of the current buffer.
213 * @duration: Duration (see #GstClockTime) of the current buffer.
215 * This signal is emitted when the overlay should be drawn.
217 gst_cairo_overlay_signals[SIGNAL_DRAW] =
218 g_signal_new ("draw",
219 G_TYPE_FROM_CLASS (klass),
224 g_cclosure_marshal_generic,
225 G_TYPE_NONE, 3, CAIRO_GOBJECT_TYPE_CONTEXT, G_TYPE_UINT64, G_TYPE_UINT64);
228 * GstCairoOverlay::caps-changed:
229 * @overlay: Overlay element emitting the signal.
230 * @caps: The #GstCaps of the element.
232 * This signal is emitted when the caps of the element has changed.
234 gst_cairo_overlay_signals[SIGNAL_CAPS_CHANGED] =
235 g_signal_new ("caps-changed",
236 G_TYPE_FROM_CLASS (klass),
238 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_CAPS);
240 gst_element_class_set_static_metadata (element_class, "Cairo overlay",
241 "Filter/Editor/Video",
242 "Render overlay on a video stream using Cairo",
243 "Jon Nordby <jononor@gmail.com>");
245 gst_element_class_add_static_pad_template (element_class,
246 &gst_cairo_overlay_sink_template);
247 gst_element_class_add_static_pad_template (element_class,
248 &gst_cairo_overlay_src_template);
252 gst_cairo_overlay_init (GstCairoOverlay * overlay)