2 * Copyright (C) <2008> Stefan Kost <ensonic@users.sf.net>
4 * test-colorkey: test manual colorkey handling
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
26 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
27 * with newer GTK versions (>= 3.3.0) */
28 #define GLIB_DISABLE_DEPRECATION_WARNINGS
29 #define GDK_DISABLE_DEPRECATION_WARNINGS
39 #include <gst/video/videooverlay.h>
41 static GtkWidget *video_window = NULL;
42 static GstElement *sink = NULL;
43 static gulong embed_xid = 0;
44 static GdkColor trans_color;
45 static gboolean trans_color_set = FALSE;
48 redraw_overlay (GtkWidget * widget)
50 GtkAllocation allocation;
51 GdkWindow *window = gtk_widget_get_window (widget);
54 cr = gdk_cairo_create (window);
55 gtk_widget_get_allocation (widget, &allocation);
57 cairo_set_source_rgb (cr, 1, 1, 1);
58 cairo_rectangle (cr, 0, 0, allocation.width, allocation.height);
61 if (trans_color_set) {
63 guint h = allocation.height * 0.75;
65 gdk_cairo_set_source_color (cr, &trans_color);
66 cairo_rectangle (cr, 0, 0, allocation.width, h);
69 for (y = h; y < allocation.height; y++) {
70 for (x = 0; x < allocation.width; x++) {
71 if (((x & 1) || (y & 1)) && (x & 1) != (y & 1)) {
72 cairo_move_to (cr, x, y);
81 handle_resize_cb (GtkWidget * widget, GdkEventConfigure * event, gpointer data)
83 redraw_overlay (widget);
88 draw_cb (GtkWidget * widget, cairo_t * cr, gpointer data)
90 redraw_overlay (widget);
95 realize_cb (GtkWidget * widget, gpointer data)
97 GdkWindow *window = gtk_widget_get_window (widget);
99 /* This is here just for pedagogical purposes, GDK_WINDOW_XID will call it
101 if (!gdk_window_ensure_native (window))
102 g_error ("Couldn't create native window needed for GstXOverlay!");
104 embed_xid = GDK_WINDOW_XID (window);
105 g_print ("Window realize: video window XID = %lu\n", embed_xid);
109 msg_state_changed (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
111 const GstStructure *s;
113 s = gst_message_get_structure (message);
115 /* We only care about state changed on the pipeline */
116 if (s && GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
117 GstState old, new, pending;
120 gst_message_parse_state_changed (message, &old, &new, &pending);
122 /* When state of the pipeline changes to paused or playing we start updating scale */
123 switch (GST_STATE_TRANSITION (old, new)) {
124 case GST_STATE_CHANGE_READY_TO_PAUSED:{
125 g_object_get (G_OBJECT (sink), "colorkey", &color, NULL);
127 trans_color.red = (color & 0xff0000) >> 8;
128 trans_color.green = (color & 0xff00);
129 trans_color.blue = (color & 0xff) << 8;
130 trans_color_set = TRUE;
132 trans_color_set = FALSE;
134 handle_resize_cb (video_window, NULL, NULL);
144 window_closed (GtkWidget * widget, GdkEvent * event, gpointer user_data)
146 GstElement *pipeline = user_data;
148 g_print ("stopping\n");
149 gtk_widget_hide (widget);
150 gst_element_set_state (pipeline, GST_STATE_NULL);
155 start_pipeline (gpointer user_data)
157 GstElement *pipeline = GST_ELEMENT (user_data);
158 GstStateChangeReturn sret;
160 sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
161 if (sret == GST_STATE_CHANGE_FAILURE) {
162 gst_element_set_state (pipeline, GST_STATE_NULL);
163 gst_object_unref (pipeline);
170 main (int argc, char **argv)
173 GstElement *pipeline, *src;
175 GstStateChangeReturn sret;
177 GstPropertyProbe *probe;
181 gst_init (&argc, &argv);
182 gtk_init (&argc, &argv);
184 /* prepare the pipeline */
186 pipeline = gst_pipeline_new ("xvoverlay");
187 src = gst_element_factory_make ("videotestsrc", NULL);
188 sink = gst_element_factory_make ("xvimagesink", NULL);
189 gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
190 gst_element_link (src, sink);
192 #define COLOR_GRAY 0x7F7F7F
194 g_object_set (G_OBJECT (sink), "autopaint-colorkey", FALSE,
195 "force-aspect-ratio", TRUE, "draw-borders", FALSE,
196 "colorkey", COLOR_GRAY, NULL);
198 /* check xvimagesink capabilities */
199 sret = gst_element_set_state (pipeline, GST_STATE_READY);
200 if (sret == GST_STATE_CHANGE_FAILURE) {
201 g_printerr ("Can't set pipeline to READY\n");
202 gst_object_unref (pipeline);
206 probe = GST_PROPERTY_PROBE (sink);
208 g_printerr ("Can't probe sink\n");
209 gst_element_set_state (pipeline, GST_STATE_NULL);
210 gst_object_unref (pipeline);
214 gst_property_probe_probe_and_get_values_name (probe,
215 "autopaint-colorkey");
216 if (!arr || !arr->n_values) {
217 g_printerr ("Can't disable autopaint-colorkey property\n");
218 gst_element_set_state (pipeline, GST_STATE_NULL);
219 gst_object_unref (pipeline);
223 g_value_array_free (arr);
226 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
227 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
228 g_signal_connect (bus, "message::state-changed",
229 G_CALLBACK (msg_state_changed), pipeline);
230 gst_object_unref (bus);
234 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
235 g_signal_connect (G_OBJECT (window), "delete-event",
236 G_CALLBACK (window_closed), (gpointer) pipeline);
237 gtk_window_set_default_size (GTK_WINDOW (window), 320, 240);
239 video_window = gtk_drawing_area_new ();
240 g_signal_connect (G_OBJECT (video_window), "configure-event",
241 G_CALLBACK (handle_resize_cb), NULL);
242 g_signal_connect (G_OBJECT (video_window), "draw",
243 G_CALLBACK (draw_cb), NULL);
244 g_signal_connect (video_window, "realize", G_CALLBACK (realize_cb), NULL);
245 gtk_widget_set_double_buffered (video_window, FALSE);
246 gtk_container_add (GTK_CONTAINER (window), video_window);
248 /* show the gui and play */
250 gtk_widget_show_all (window);
252 /* realize window now so that the video window gets created and we can
253 * obtain its XID before the pipeline is started up and the videosink
254 * asks for the XID of the window to render onto */
255 gtk_widget_realize (window);
257 /* we should have the XID now */
258 g_assert (embed_xid != 0);
260 /* we know what the video sink is in this case (xvimagesink), so we can
261 * just set it directly here now (instead of waiting for a
262 * prepare-window-handle element message in a sync bus handler and setting
264 g_print ("setting XID %lu\n", embed_xid);
265 gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (sink), embed_xid);
267 g_idle_add (start_pipeline, pipeline);
270 gst_object_unref (pipeline);