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>
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. */
76 /* Store the information from the caps that we are interested in. */
78 prepare_overlay (GstElement * overlay, GstCaps * caps, gpointer user_data)
80 CairoOverlayState *state = (CairoOverlayState *) user_data;
82 gst_video_format_parse_caps (caps, NULL, &state->width, &state->height);
87 * This function draws a cute "beating" heart. */
89 draw_overlay (GstElement * overlay, cairo_t * cr, guint64 timestamp,
90 guint64 duration, gpointer user_data)
92 CairoOverlayState *s = (CairoOverlayState *) user_data;
98 scale = 2 * (((timestamp / (int) 1e7) % 70) + 30) / 100.0;
99 cairo_translate (cr, s->width / 2, (s->height / 2) - 30);
100 cairo_scale (cr, scale, scale);
102 cairo_move_to (cr, 0, 0);
103 cairo_curve_to (cr, 0, -30, -50, -30, -50, 0);
104 cairo_curve_to (cr, -50, 30, 0, 35, 0, 60);
105 cairo_curve_to (cr, 0, 35, 50, 30, 50, 0);
106 cairo_curve_to (cr, 50, -30, 0, -30, 0, 0);
107 cairo_set_source_rgba (cr, 0.9, 0.0, 0.1, 0.7);
112 setup_gst_pipeline (CairoOverlayState * overlay_state)
114 GstElement *pipeline;
115 GstElement *cairo_overlay;
116 GstElement *source, *adaptor1, *adaptor2, *sink;
118 pipeline = gst_pipeline_new ("cairo-overlay-example");
120 /* Adaptors needed because cairooverlay only supports ARGB data */
121 source = gst_element_factory_make ("videotestsrc", "source");
122 adaptor1 = gst_element_factory_make ("ffmpegcolorspace", "adaptor1");
123 cairo_overlay = gst_element_factory_make ("cairooverlay", "overlay");
124 adaptor2 = gst_element_factory_make ("ffmpegcolorspace", "adaptor2");
125 sink = gst_element_factory_make ("autovideosink", "sink");
127 /* If failing, the element could not be created */
128 g_assert (cairo_overlay);
130 /* Hook up the neccesary signals for cairooverlay */
131 g_signal_connect (cairo_overlay, "draw",
132 G_CALLBACK (draw_overlay), overlay_state);
133 g_signal_connect (cairo_overlay, "caps-changed",
134 G_CALLBACK (prepare_overlay), overlay_state);
136 gst_bin_add_many (GST_BIN (pipeline), source, adaptor1,
137 cairo_overlay, adaptor2, sink, NULL);
139 if (!gst_element_link_many (source, adaptor1,
140 cairo_overlay, adaptor2, sink, NULL)) {
141 g_warning ("Failed to link elements!");
148 main (int argc, char **argv)
151 GstElement *pipeline;
153 CairoOverlayState overlay_state = { FALSE, 0, 0 };
155 gst_init (&argc, &argv);
156 loop = g_main_loop_new (NULL, FALSE);
158 pipeline = setup_gst_pipeline (&overlay_state);
160 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
161 gst_bus_add_signal_watch (bus);
162 g_signal_connect (G_OBJECT (bus), "message", G_CALLBACK (on_message), loop);
163 gst_object_unref (GST_OBJECT (bus));
165 gst_element_set_state (pipeline, GST_STATE_PLAYING);
166 g_main_loop_run (loop);
168 gst_element_set_state (pipeline, GST_STATE_NULL);
169 gst_object_unref (pipeline);