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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, 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"
94 #include "gstcairo-marshal.h"
96 #include <gst/video/video.h>
100 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
101 #define TEMPLATE_CAPS GST_VIDEO_CAPS_BGRx " ; " GST_VIDEO_CAPS_BGRA " ; "
103 #define TEMPLATE_CAPS GST_VIDEO_CAPS_xRGB " ; " GST_VIDEO_CAPS_ARGB " ; "
107 static GstStaticPadTemplate gst_cairo_overlay_src_template =
108 GST_STATIC_PAD_TEMPLATE ("src",
111 GST_STATIC_CAPS (TEMPLATE_CAPS)
114 static GstStaticPadTemplate gst_cairo_overlay_sink_template =
115 GST_STATIC_PAD_TEMPLATE ("sink",
118 GST_STATIC_CAPS (TEMPLATE_CAPS)
122 GST_BOILERPLATE (GstCairoOverlay, gst_cairo_overlay, GstVideoFilter,
123 GST_TYPE_VIDEO_FILTER);
132 static guint gst_cairo_overlay_signals[N_SIGNALS];
135 gst_cairo_overlay_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
138 GstCairoOverlay *overlay = GST_CAIRO_OVERLAY (btrans);
142 gst_video_format_parse_caps (incaps, &overlay->format, &overlay->width,
144 if (G_UNLIKELY (!ret))
147 g_signal_emit (overlay, gst_cairo_overlay_signals[SIGNAL_CAPS_CHANGED], 0,
154 gst_cairo_overlay_transform_ip (GstBaseTransform * btrans, GstBuffer * buf)
157 GstCairoOverlay *overlay = GST_CAIRO_OVERLAY (btrans);
158 cairo_surface_t *surface;
160 cairo_format_t format;
162 format = (overlay->format == GST_VIDEO_FORMAT_ARGB
163 || overlay->format == GST_VIDEO_FORMAT_BGRA) ?
164 CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24;
167 cairo_image_surface_create_for_data (GST_BUFFER_DATA (buf), format,
168 overlay->width, overlay->height, overlay->width * 4);
169 if (G_UNLIKELY (!surface))
170 return GST_FLOW_ERROR;
172 cr = cairo_create (surface);
173 if (G_UNLIKELY (!cr)) {
174 cairo_surface_destroy (surface);
175 return GST_FLOW_ERROR;
178 g_signal_emit (overlay, gst_cairo_overlay_signals[SIGNAL_DRAW], 0,
179 cr, GST_BUFFER_TIMESTAMP (buf), GST_BUFFER_DURATION (buf), NULL);
182 cairo_surface_destroy (surface);
188 gst_cairo_overlay_base_init (gpointer g_class)
190 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
192 gst_element_class_set_static_metadata (element_class, "Cairo overlay",
193 "Filter/Editor/Video",
194 "Render overlay on a video stream using Cairo",
195 "Jon Nordby <jononor@gmail.com>");
197 gst_element_class_add_pad_template (element_class,
198 gst_static_pad_template_get (&gst_cairo_overlay_sink_template));
199 gst_element_class_add_pad_template (element_class,
200 gst_static_pad_template_get (&gst_cairo_overlay_src_template));
204 gst_cairo_overlay_class_init (GstCairoOverlayClass * klass)
206 GstBaseTransformClass *trans_class;
208 trans_class = (GstBaseTransformClass *) klass;
210 trans_class->set_caps = gst_cairo_overlay_set_caps;
211 trans_class->transform_ip = gst_cairo_overlay_transform_ip;
214 * GstCairoOverlay::draw:
215 * @overlay: Overlay element emitting the signal.
216 * @cr: Cairo context to draw to.
217 * @timestamp: Timestamp (see #GstClockTime) of the current buffer.
218 * @duration: Duration (see #GstClockTime) of the current buffer.
220 * This signal is emitted when the overlay should be drawn.
222 gst_cairo_overlay_signals[SIGNAL_DRAW] =
223 g_signal_new ("draw",
224 G_TYPE_FROM_CLASS (klass),
229 gst_cairo_marshal_VOID__BOXED_UINT64_UINT64,
230 G_TYPE_NONE, 3, CAIRO_GOBJECT_TYPE_CONTEXT, G_TYPE_UINT64, G_TYPE_UINT64);
233 * GstCairoOverlay::caps-changed:
234 * @overlay: Overlay element emitting the signal.
235 * @caps: The #GstCaps of the element.
237 * This signal is emitted when the caps of the element has changed.
239 gst_cairo_overlay_signals[SIGNAL_CAPS_CHANGED] =
240 g_signal_new ("caps-changed",
241 G_TYPE_FROM_CLASS (klass),
244 NULL, NULL, gst_cairo_marshal_VOID__BOXED, G_TYPE_NONE, 1, GST_TYPE_CAPS);
248 gst_cairo_overlay_init (GstCairoOverlay * overlay, GstCairoOverlayClass * klass)