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 * Example showing usage of the cairooverlay element
25 #include <gst/video/video.h>
28 #include <cairo-gobject.h>
34 on_message (GstBus * bus, GstMessage * message, gpointer user_data)
36 GMainLoop *loop = (GMainLoop *) user_data;
38 switch (GST_MESSAGE_TYPE (message)) {
39 case GST_MESSAGE_ERROR:{
43 gst_message_parse_error (message, &err, &debug);
44 g_critical ("Got ERROR: %s (%s)", err->message, GST_STR_NULL (debug));
45 g_main_loop_quit (loop);
48 case GST_MESSAGE_WARNING:{
52 gst_message_parse_warning (message, &err, &debug);
53 g_warning ("Got WARNING: %s (%s)", err->message, GST_STR_NULL (debug));
54 g_main_loop_quit (loop);
58 g_main_loop_quit (loop);
67 /* Datastructure to share the state we are interested in between
68 * prepare and render function. */
75 /* Store the information from the caps that we are interested in. */
77 prepare_overlay (GstElement * overlay, GstCaps * caps, gpointer user_data)
79 CairoOverlayState *state = (CairoOverlayState *) user_data;
81 state->valid = gst_video_info_from_caps (&state->vinfo, caps);
85 * This function draws a cute "beating" heart. */
87 draw_overlay (GstElement * overlay, cairo_t * cr, guint64 timestamp,
88 guint64 duration, gpointer user_data)
90 CairoOverlayState *s = (CairoOverlayState *) user_data;
97 width = GST_VIDEO_INFO_WIDTH (&s->vinfo);
98 height = GST_VIDEO_INFO_HEIGHT (&s->vinfo);
100 scale = 2 * (((timestamp / (int) 1e7) % 70) + 30) / 100.0;
101 cairo_translate (cr, width / 2, (height / 2) - 30);
103 /* FIXME: this assumes a pixel-aspect-ratio of 1/1 */
104 cairo_scale (cr, scale, scale);
106 cairo_move_to (cr, 0, 0);
107 cairo_curve_to (cr, 0, -30, -50, -30, -50, 0);
108 cairo_curve_to (cr, -50, 30, 0, 35, 0, 60);
109 cairo_curve_to (cr, 0, 35, 50, 30, 50, 0);
110 cairo_curve_to (cr, 50, -30, 0, -30, 0, 0);
111 cairo_set_source_rgba (cr, 0.9, 0.0, 0.1, 0.7);
116 setup_gst_pipeline (CairoOverlayState * overlay_state)
118 GstElement *pipeline;
119 GstElement *cairo_overlay;
120 GstElement *source, *adaptor1, *adaptor2, *sink;
122 pipeline = gst_pipeline_new ("cairo-overlay-example");
124 /* Adaptors needed because cairooverlay only supports ARGB data */
125 source = gst_element_factory_make ("videotestsrc", "source");
126 adaptor1 = gst_element_factory_make ("videoconvert", "adaptor1");
127 cairo_overlay = gst_element_factory_make ("cairooverlay", "overlay");
128 adaptor2 = gst_element_factory_make ("videoconvert", "adaptor2");
129 sink = gst_element_factory_make ("ximagesink", "sink");
131 sink = gst_element_factory_make ("autovideosink", "sink");
133 /* If failing, the element could not be created */
134 g_assert (cairo_overlay);
136 /* Hook up the neccesary signals for cairooverlay */
137 g_signal_connect (cairo_overlay, "draw",
138 G_CALLBACK (draw_overlay), overlay_state);
139 g_signal_connect (cairo_overlay, "caps-changed",
140 G_CALLBACK (prepare_overlay), overlay_state);
142 gst_bin_add_many (GST_BIN (pipeline), source, adaptor1,
143 cairo_overlay, adaptor2, sink, NULL);
145 if (!gst_element_link_many (source, adaptor1,
146 cairo_overlay, adaptor2, sink, NULL)) {
147 g_warning ("Failed to link elements!");
154 main (int argc, char **argv)
157 GstElement *pipeline;
159 CairoOverlayState *overlay_state;
161 gst_init (&argc, &argv);
162 loop = g_main_loop_new (NULL, FALSE);
164 /* allocate on heap for pedagogical reasons, makes code easier to transfer */
165 overlay_state = g_new0 (CairoOverlayState, 1);
167 pipeline = setup_gst_pipeline (overlay_state);
169 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
170 gst_bus_add_signal_watch (bus);
171 g_signal_connect (G_OBJECT (bus), "message", G_CALLBACK (on_message), loop);
172 gst_object_unref (GST_OBJECT (bus));
174 gst_element_set_state (pipeline, GST_STATE_PLAYING);
175 g_main_loop_run (loop);
177 gst_element_set_state (pipeline, GST_STATE_NULL);
178 gst_object_unref (pipeline);
180 g_free (overlay_state);