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
23 * Note: The example program not run on non-X11 platforms because
24 * it is using the xvimageoverlay element. That part of the code was
25 * roughly based on gst_x_overlay documentation.
30 #include <gst/video/video.h>
31 #include <gst/interfaces/xoverlay.h>
35 #include <cairo-gobject.h>
37 #ifdef GDK_WINDOWING_X11
41 static gulong video_window_xid = 0;
43 static GstBusSyncReply
44 bus_sync_handler (GstBus * bus, GstMessage * message, gpointer user_data)
46 if (GST_MESSAGE_TYPE (message) != GST_MESSAGE_ELEMENT)
48 if (!gst_structure_has_name (message->structure, "prepare-xwindow-id"))
51 if (video_window_xid != 0) {
52 GstXOverlay *xoverlay;
54 xoverlay = GST_X_OVERLAY (GST_MESSAGE_SRC (message));
55 gst_x_overlay_set_window_handle (xoverlay, video_window_xid);
57 g_warning ("Should have obtained video_window_xid by now!");
60 gst_message_unref (message);
65 video_widget_realize_cb (GtkWidget * widget, gpointer data)
67 #ifdef GDK_WINDOWING_X11
68 video_window_xid = GDK_WINDOW_XID (widget->window);
73 setup_gtk_window (void)
75 GtkWidget *video_window;
76 GtkWidget *app_window;
78 app_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
80 video_window = gtk_drawing_area_new ();
81 g_signal_connect (video_window, "realize",
82 G_CALLBACK (video_widget_realize_cb), NULL);
83 gtk_widget_set_double_buffered (video_window, FALSE);
85 gtk_container_add (GTK_CONTAINER (app_window), video_window);
86 gtk_widget_show_all (app_window);
88 gtk_widget_realize (app_window);
89 g_assert (video_window_xid != 0);
94 /* Datastructure to share the state we are interested in between
95 * prepare and render function. */
103 /* Store the information from the caps that we are interested in. */
105 prepare_overlay (GstElement * overlay, GstCaps * caps, gpointer user_data)
107 CairoOverlayState *state = (CairoOverlayState *) user_data;
109 gst_video_format_parse_caps (caps, NULL, &state->width, &state->height);
114 * This function draws a cute "beating" heart. */
116 draw_overlay (GstElement * overlay, cairo_t * cr, guint64 timestamp,
117 guint64 duration, gpointer user_data)
119 CairoOverlayState *s = (CairoOverlayState *) user_data;
125 scale = 2 * (((timestamp / (int) 1e7) % 70) + 30) / 100.0;
126 cairo_translate (cr, s->width / 2, (s->height / 2) - 30);
127 cairo_scale (cr, scale, scale);
129 cairo_move_to (cr, 0, 0);
130 cairo_curve_to (cr, 0, -30, -50, -30, -50, 0);
131 cairo_curve_to (cr, -50, 30, 0, 35, 0, 60);
132 cairo_curve_to (cr, 0, 35, 50, 30, 50, 0);
133 cairo_curve_to (cr, 50, -30, 0, -30, 0, 0);
134 cairo_set_source_rgba (cr, 0.9, 0.0, 0.1, 0.7);
139 setup_gst_pipeline (CairoOverlayState * overlay_state)
141 GstElement *pipeline;
142 GstElement *cairo_overlay;
143 GstElement *source, *adaptor1, *adaptor2, *sink;
146 pipeline = gst_pipeline_new ("cairo-overlay-example");
148 /* Adaptors needed because cairooverlay only supports ARGB data */
149 source = gst_element_factory_make ("videotestsrc", "source");
150 adaptor1 = gst_element_factory_make ("ffmpegcolorspace", "adaptor1");
151 cairo_overlay = gst_element_factory_make ("cairooverlay", "overlay");
152 adaptor2 = gst_element_factory_make ("ffmpegcolorspace", "adaptor2");
153 sink = gst_element_factory_make ("xvimagesink", "sink");
155 /* If failing, the element could not be created */
156 g_assert (cairo_overlay);
158 /* Hook up the neccesary signals for cairooverlay */
159 g_signal_connect (cairo_overlay, "draw",
160 G_CALLBACK (draw_overlay), overlay_state);
161 g_signal_connect (cairo_overlay, "caps-changed",
162 G_CALLBACK (prepare_overlay), overlay_state);
164 gst_bin_add_many (GST_BIN (pipeline), source, adaptor1,
165 cairo_overlay, adaptor2, sink, NULL);
167 if (!gst_element_link_many (source, adaptor1,
168 cairo_overlay, adaptor2, sink, NULL)) {
169 g_warning ("Failed to link elements!");
172 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
173 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bus_sync_handler, NULL);
174 gst_object_unref (bus);
180 main (int argc, char **argv)
183 GstElement *pipeline;
184 CairoOverlayState *overlay_state;
186 gtk_init (&argc, &argv);
187 gst_init (&argc, &argv);
189 window = setup_gtk_window ();
190 overlay_state = g_new (CairoOverlayState, 1);
191 pipeline = setup_gst_pipeline (overlay_state);
193 gst_element_set_state (pipeline, GST_STATE_PLAYING);
196 gst_object_unref (pipeline);
197 gtk_widget_destroy (GTK_WIDGET (window));
198 g_free (overlay_state);