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.
29 #include <gst/video/video.h>
30 #include <gst/interfaces/xoverlay.h>
33 #include <cairo-gobject.h>
39 static gulong video_window_xid = 0;
41 static GstBusSyncReply
42 bus_sync_handler (GstBus * bus, GstMessage * message, gpointer user_data)
44 if (GST_MESSAGE_TYPE (message) != GST_MESSAGE_ELEMENT)
46 if (!gst_structure_has_name (message->structure, "prepare-xwindow-id"))
49 if (video_window_xid != 0) {
50 GstXOverlay *xoverlay;
52 xoverlay = GST_X_OVERLAY (GST_MESSAGE_SRC (message));
53 gst_x_overlay_set_window_handle (xoverlay, video_window_xid);
55 g_warning ("Should have obtained video_window_xid by now!");
58 gst_message_unref (message);
63 video_widget_realize_cb (GtkWidget * widget, gpointer data)
65 video_window_xid = GDK_WINDOW_XID (widget->window);
69 setup_gtk_window (void)
71 GtkWidget *video_window;
72 GtkWidget *app_window;
74 app_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
76 video_window = gtk_drawing_area_new ();
77 g_signal_connect (video_window, "realize",
78 G_CALLBACK (video_widget_realize_cb), NULL);
79 gtk_widget_set_double_buffered (video_window, FALSE);
81 gtk_container_add (GTK_CONTAINER (app_window), video_window);
82 gtk_widget_show_all (app_window);
84 gtk_widget_realize (app_window);
85 g_assert (video_window_xid != 0);
90 /* Datastructure to share the state we are interested in between
91 * prepare and render function. */
99 /* Store the information from the caps that we are interested in. */
101 prepare_overlay (GstElement * overlay, GstCaps * caps, gpointer user_data)
103 CairoOverlayState *state = (CairoOverlayState *) user_data;
105 gst_video_format_parse_caps (caps, NULL, &state->width, &state->height);
110 * This function draws a cute "beating" heart. */
112 draw_overlay (GstElement * overlay, cairo_t * cr, guint64 timestamp,
113 guint64 duration, gpointer user_data)
115 CairoOverlayState *s = (CairoOverlayState *) user_data;
121 scale = 2 * (((timestamp / (int) 1e7) % 70) + 30) / 100.0;
122 cairo_translate (cr, s->width / 2, (s->height / 2) - 30);
123 cairo_scale (cr, scale, scale);
125 cairo_move_to (cr, 0, 0);
126 cairo_curve_to (cr, 0, -30, -50, -30, -50, 0);
127 cairo_curve_to (cr, -50, 30, 0, 35, 0, 60);
128 cairo_curve_to (cr, 0, 35, 50, 30, 50, 0);
129 cairo_curve_to (cr, 50, -30, 0, -30, 0, 0);
130 cairo_set_source_rgba (cr, 0.9, 0.0, 0.1, 0.7);
135 setup_gst_pipeline (CairoOverlayState * overlay_state)
137 GstElement *pipeline;
138 GstElement *cairo_overlay;
139 GstElement *source, *adaptor1, *adaptor2, *sink;
142 pipeline = gst_pipeline_new ("cairo-overlay-example");
144 /* Adaptors needed because cairooverlay only supports ARGB data */
145 source = gst_element_factory_make ("videotestsrc", "source");
146 adaptor1 = gst_element_factory_make ("ffmpegcolorspace", "adaptor1");
147 cairo_overlay = gst_element_factory_make ("cairooverlay", "overlay");
148 adaptor2 = gst_element_factory_make ("ffmpegcolorspace", "adaptor2");
149 sink = gst_element_factory_make ("xvimagesink", "sink");
151 /* If failing, the element could not be created */
152 g_assert (cairo_overlay);
154 /* Hook up the neccesary signals for cairooverlay */
155 g_signal_connect (cairo_overlay, "draw",
156 G_CALLBACK (draw_overlay), overlay_state);
157 g_signal_connect (cairo_overlay, "caps-changed",
158 G_CALLBACK (prepare_overlay), overlay_state);
160 gst_bin_add_many (GST_BIN (pipeline), source, adaptor1,
161 cairo_overlay, adaptor2, sink, NULL);
163 if (!gst_element_link_many (source, adaptor1,
164 cairo_overlay, adaptor2, sink, NULL)) {
165 g_warning ("Failed to link elements!");
168 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
169 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bus_sync_handler, NULL);
170 gst_object_unref (bus);
176 main (int argc, char **argv)
179 GstElement *pipeline;
180 CairoOverlayState *overlay_state;
182 gtk_init (&argc, &argv);
183 gst_init (&argc, &argv);
185 window = setup_gtk_window ();
186 overlay_state = g_new0 (CairoOverlayState, 1);
187 pipeline = setup_gst_pipeline (overlay_state);
189 gst_element_set_state (pipeline, GST_STATE_PLAYING);
192 gst_object_unref (pipeline);
193 gtk_widget_destroy (GTK_WIDGET (window));
194 g_free (overlay_state);