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 * Example showing usage of the cairooverlay element
25 #include <gst/video/video.h>
28 #include <cairo-gobject.h>
32 /* Datastructure to share the state we are interested in between
33 * prepare and render function. */
41 /* Store the information from the caps that we are interested in. */
43 prepare_overlay (GstElement * overlay, GstCaps * caps, gpointer user_data)
45 CairoOverlayState *state = (CairoOverlayState *) user_data;
47 gst_video_format_parse_caps (caps, NULL, &state->width, &state->height);
52 * This function draws a cute "beating" heart. */
54 draw_overlay (GstElement * overlay, cairo_t * cr, guint64 timestamp,
55 guint64 duration, gpointer user_data)
57 CairoOverlayState *s = (CairoOverlayState *) user_data;
63 scale = 2 * (((timestamp / (int) 1e7) % 70) + 30) / 100.0;
64 cairo_translate (cr, s->width / 2, (s->height / 2) - 30);
65 cairo_scale (cr, scale, scale);
67 cairo_move_to (cr, 0, 0);
68 cairo_curve_to (cr, 0, -30, -50, -30, -50, 0);
69 cairo_curve_to (cr, -50, 30, 0, 35, 0, 60);
70 cairo_curve_to (cr, 0, 35, 50, 30, 50, 0);
71 cairo_curve_to (cr, 50, -30, 0, -30, 0, 0);
72 cairo_set_source_rgba (cr, 0.9, 0.0, 0.1, 0.7);
77 setup_gst_pipeline (CairoOverlayState * overlay_state)
80 GstElement *cairo_overlay;
81 GstElement *source, *adaptor1, *adaptor2, *sink;
83 pipeline = gst_pipeline_new ("cairo-overlay-example");
85 /* Adaptors needed because cairooverlay only supports ARGB data */
86 source = gst_element_factory_make ("videotestsrc", "source");
87 adaptor1 = gst_element_factory_make ("ffmpegcolorspace", "adaptor1");
88 cairo_overlay = gst_element_factory_make ("cairooverlay", "overlay");
89 adaptor2 = gst_element_factory_make ("ffmpegcolorspace", "adaptor2");
90 sink = gst_element_factory_make ("autovideosink", "sink");
92 /* If failing, the element could not be created */
93 g_assert (cairo_overlay);
95 /* Hook up the neccesary signals for cairooverlay */
96 g_signal_connect (cairo_overlay, "draw",
97 G_CALLBACK (draw_overlay), overlay_state);
98 g_signal_connect (cairo_overlay, "caps-changed",
99 G_CALLBACK (prepare_overlay), overlay_state);
101 gst_bin_add_many (GST_BIN (pipeline), source, adaptor1,
102 cairo_overlay, adaptor2, sink, NULL);
104 if (!gst_element_link_many (source, adaptor1,
105 cairo_overlay, adaptor2, sink, NULL)) {
106 g_warning ("Failed to link elements!");
113 main (int argc, char **argv)
116 GstElement *pipeline;
117 CairoOverlayState *overlay_state;
119 gst_init (&argc, &argv);
120 loop = g_main_loop_new (NULL, FALSE);
122 overlay_state = g_new0 (CairoOverlayState, 1);
123 pipeline = setup_gst_pipeline (overlay_state);
125 gst_element_set_state (pipeline, GST_STATE_PLAYING);
126 g_main_loop_run (loop);
128 gst_object_unref (pipeline);
129 g_free (overlay_state);