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.
34 #include <gst/interfaces/xoverlay.h>
35 #include <gst/interfaces/propertyprobe.h>
37 static GtkWidget *video_window = NULL;
38 static GstElement *sink = NULL;
39 static gulong embed_xid = 0;
40 static GdkColor trans_color;
41 static gboolean trans_color_set = FALSE;
44 redraw_overlay (GtkWidget * widget)
46 GtkAllocation allocation;
47 GdkWindow *window = gtk_widget_get_window (widget);
50 cr = gdk_cairo_create (window);
51 gtk_widget_get_allocation (widget, &allocation);
53 cairo_set_source_rgb (cr, 1, 1, 1);
54 cairo_rectangle (cr, 0, 0, allocation.width, allocation.height);
57 if (trans_color_set) {
59 guint h = allocation.height * 0.75;
61 gdk_cairo_set_source_color (cr, &trans_color);
62 cairo_rectangle (cr, 0, 0, allocation.width, h);
65 for (y = h; y < allocation.height; y++) {
66 for (x = 0; x < allocation.width; x++) {
67 if (((x & 1) || (y & 1)) && (x & 1) != (y & 1)) {
68 cairo_move_to (cr, x, y);
77 handle_resize_cb (GtkWidget * widget, GdkEventConfigure * event, gpointer data)
79 redraw_overlay (widget);
84 draw_cb (GtkWidget * widget, cairo_t * cr, gpointer data)
86 redraw_overlay (widget);
91 realize_cb (GtkWidget * widget, gpointer data)
93 GdkWindow *window = gtk_widget_get_window (widget);
95 /* This is here just for pedagogical purposes, GDK_WINDOW_XID will call it
97 if (!gdk_window_ensure_native (window))
98 g_error ("Couldn't create native window needed for GstXOverlay!");
100 embed_xid = GDK_WINDOW_XID (window);
101 g_print ("Window realize: video window XID = %lu\n", embed_xid);
105 msg_state_changed (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
107 const GstStructure *s;
109 s = gst_message_get_structure (message);
111 /* We only care about state changed on the pipeline */
112 if (s && GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
113 GstState old, new, pending;
116 gst_message_parse_state_changed (message, &old, &new, &pending);
118 /* When state of the pipeline changes to paused or playing we start updating scale */
119 switch (GST_STATE_TRANSITION (old, new)) {
120 case GST_STATE_CHANGE_READY_TO_PAUSED:{
121 g_object_get (G_OBJECT (sink), "colorkey", &color, NULL);
123 trans_color.red = (color & 0xff0000) >> 8;
124 trans_color.green = (color & 0xff00);
125 trans_color.blue = (color & 0xff) << 8;
126 trans_color_set = TRUE;
128 trans_color_set = FALSE;
130 handle_resize_cb (video_window, NULL, NULL);
140 window_closed (GtkWidget * widget, GdkEvent * event, gpointer user_data)
142 GstElement *pipeline = user_data;
144 g_print ("stopping\n");
145 gtk_widget_hide (widget);
146 gst_element_set_state (pipeline, GST_STATE_NULL);
151 start_pipeline (gpointer user_data)
153 GstElement *pipeline = GST_ELEMENT (user_data);
154 GstStateChangeReturn sret;
156 sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
157 if (sret == GST_STATE_CHANGE_FAILURE) {
158 gst_element_set_state (pipeline, GST_STATE_NULL);
159 gst_object_unref (pipeline);
166 main (int argc, char **argv)
169 GstElement *pipeline, *src;
171 GstStateChangeReturn sret;
172 GstPropertyProbe *probe;
175 #if !GLIB_CHECK_VERSION (2, 31, 0)
176 if (!g_thread_supported ())
177 g_thread_init (NULL);
180 gst_init (&argc, &argv);
181 gtk_init (&argc, &argv);
183 /* prepare the pipeline */
185 pipeline = gst_pipeline_new ("xvoverlay");
186 src = gst_element_factory_make ("videotestsrc", NULL);
187 sink = gst_element_factory_make ("xvimagesink", NULL);
188 gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
189 gst_element_link (src, sink);
191 #define COLOR_GRAY 0x7F7F7F
193 g_object_set (G_OBJECT (sink), "autopaint-colorkey", FALSE,
194 "force-aspect-ratio", TRUE, "draw-borders", FALSE,
195 "colorkey", COLOR_GRAY, NULL);
197 /* check xvimagesink capabilities */
198 sret = gst_element_set_state (pipeline, GST_STATE_READY);
199 if (sret == GST_STATE_CHANGE_FAILURE) {
200 g_printerr ("Can't set pipeline to READY\n");
201 gst_object_unref (pipeline);
205 probe = GST_PROPERTY_PROBE (sink);
207 g_printerr ("Can't probe sink\n");
208 gst_element_set_state (pipeline, GST_STATE_NULL);
209 gst_object_unref (pipeline);
213 gst_property_probe_probe_and_get_values_name (probe,
214 "autopaint-colorkey");
215 if (!arr || !arr->n_values) {
216 g_printerr ("Can't disable autopaint-colorkey property\n");
217 gst_element_set_state (pipeline, GST_STATE_NULL);
218 gst_object_unref (pipeline);
222 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 prepare-xwindow-id
260 * element message in a sync bus handler and setting it there) */
261 g_print ("setting XID %lu\n", embed_xid);
262 gst_x_overlay_set_window_handle (GST_X_OVERLAY (sink), embed_xid);
264 g_idle_add (start_pipeline, pipeline);
267 gst_object_unref (pipeline);