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.
25 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
26 * with newer GTK versions (>= 3.3.0) */
27 #define GDK_DISABLE_DEPRECATION_WARNINGS
37 #include <gst/video/videooverlay.h>
39 static GtkWidget *video_window = NULL;
40 static GstElement *sink = NULL;
41 static gulong embed_xid = 0;
42 static GdkColor trans_color;
43 static gboolean trans_color_set = FALSE;
46 redraw_overlay (GtkWidget * widget)
48 GtkAllocation allocation;
49 GdkWindow *window = gtk_widget_get_window (widget);
52 cr = gdk_cairo_create (window);
53 gtk_widget_get_allocation (widget, &allocation);
55 cairo_set_source_rgb (cr, 1, 1, 1);
56 cairo_rectangle (cr, 0, 0, allocation.width, allocation.height);
59 if (trans_color_set) {
61 guint h = allocation.height * 0.75;
63 gdk_cairo_set_source_color (cr, &trans_color);
64 cairo_rectangle (cr, 0, 0, allocation.width, h);
67 for (y = h; y < allocation.height; y++) {
68 for (x = 0; x < allocation.width; x++) {
69 if (((x & 1) || (y & 1)) && (x & 1) != (y & 1)) {
70 cairo_move_to (cr, x, y);
79 handle_resize_cb (GtkWidget * widget, GdkEventConfigure * event, gpointer data)
81 redraw_overlay (widget);
86 draw_cb (GtkWidget * widget, cairo_t * cr, gpointer data)
88 redraw_overlay (widget);
93 realize_cb (GtkWidget * widget, gpointer data)
95 GdkWindow *window = gtk_widget_get_window (widget);
97 /* This is here just for pedagogical purposes, GDK_WINDOW_XID will call it
99 if (!gdk_window_ensure_native (window))
100 g_error ("Couldn't create native window needed for GstXOverlay!");
102 embed_xid = GDK_WINDOW_XID (window);
103 g_print ("Window realize: video window XID = %lu\n", embed_xid);
107 msg_state_changed (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
109 const GstStructure *s;
111 s = gst_message_get_structure (message);
113 /* We only care about state changed on the pipeline */
114 if (s && GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
115 GstState old, new, pending;
118 gst_message_parse_state_changed (message, &old, &new, &pending);
120 /* When state of the pipeline changes to paused or playing we start updating scale */
121 switch (GST_STATE_TRANSITION (old, new)) {
122 case GST_STATE_CHANGE_READY_TO_PAUSED:{
123 g_object_get (G_OBJECT (sink), "colorkey", &color, NULL);
125 trans_color.red = (color & 0xff0000) >> 8;
126 trans_color.green = (color & 0xff00);
127 trans_color.blue = (color & 0xff) << 8;
128 trans_color_set = TRUE;
130 trans_color_set = FALSE;
132 handle_resize_cb (video_window, NULL, NULL);
142 window_closed (GtkWidget * widget, GdkEvent * event, gpointer user_data)
144 GstElement *pipeline = user_data;
146 g_print ("stopping\n");
147 gtk_widget_hide (widget);
148 gst_element_set_state (pipeline, GST_STATE_NULL);
153 start_pipeline (gpointer user_data)
155 GstElement *pipeline = GST_ELEMENT (user_data);
156 GstStateChangeReturn sret;
158 sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
159 if (sret == GST_STATE_CHANGE_FAILURE) {
160 gst_element_set_state (pipeline, GST_STATE_NULL);
161 gst_object_unref (pipeline);
168 main (int argc, char **argv)
171 GstElement *pipeline, *src;
173 GstStateChangeReturn sret;
175 GstPropertyProbe *probe;
179 gst_init (&argc, &argv);
180 gtk_init (&argc, &argv);
182 /* prepare the pipeline */
184 pipeline = gst_pipeline_new ("xvoverlay");
185 src = gst_element_factory_make ("videotestsrc", NULL);
186 sink = gst_element_factory_make ("xvimagesink", NULL);
187 gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
188 gst_element_link (src, sink);
190 #define COLOR_GRAY 0x7F7F7F
192 g_object_set (G_OBJECT (sink), "autopaint-colorkey", FALSE,
193 "force-aspect-ratio", TRUE, "draw-borders", FALSE,
194 "colorkey", COLOR_GRAY, NULL);
196 /* check xvimagesink capabilities */
197 sret = gst_element_set_state (pipeline, GST_STATE_READY);
198 if (sret == GST_STATE_CHANGE_FAILURE) {
199 g_printerr ("Can't set pipeline to READY\n");
200 gst_object_unref (pipeline);
204 probe = GST_PROPERTY_PROBE (sink);
206 g_printerr ("Can't probe sink\n");
207 gst_element_set_state (pipeline, GST_STATE_NULL);
208 gst_object_unref (pipeline);
212 gst_property_probe_probe_and_get_values_name (probe,
213 "autopaint-colorkey");
214 if (!arr || !arr->n_values) {
215 g_printerr ("Can't disable autopaint-colorkey property\n");
216 gst_element_set_state (pipeline, GST_STATE_NULL);
217 gst_object_unref (pipeline);
221 g_value_array_free (arr);
224 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
225 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
226 g_signal_connect (bus, "message::state-changed",
227 G_CALLBACK (msg_state_changed), pipeline);
228 gst_object_unref (bus);
232 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
233 g_signal_connect (G_OBJECT (window), "delete-event",
234 G_CALLBACK (window_closed), (gpointer) pipeline);
235 gtk_window_set_default_size (GTK_WINDOW (window), 320, 240);
237 video_window = gtk_drawing_area_new ();
238 g_signal_connect (G_OBJECT (video_window), "configure-event",
239 G_CALLBACK (handle_resize_cb), NULL);
240 g_signal_connect (G_OBJECT (video_window), "draw",
241 G_CALLBACK (draw_cb), NULL);
242 g_signal_connect (video_window, "realize", G_CALLBACK (realize_cb), NULL);
243 gtk_widget_set_double_buffered (video_window, FALSE);
244 gtk_container_add (GTK_CONTAINER (window), video_window);
246 /* show the gui and play */
248 gtk_widget_show_all (window);
250 /* realize window now so that the video window gets created and we can
251 * obtain its XID before the pipeline is started up and the videosink
252 * asks for the XID of the window to render onto */
253 gtk_widget_realize (window);
255 /* we should have the XID now */
256 g_assert (embed_xid != 0);
258 /* we know what the video sink is in this case (xvimagesink), so we can
259 * just set it directly here now (instead of waiting for a
260 * prepare-window-handle element message in a sync bus handler and setting
262 g_print ("setting XID %lu\n", embed_xid);
263 gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (sink), embed_xid);
265 g_idle_add (start_pipeline, pipeline);
268 gst_object_unref (pipeline);