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 GdkGC *trans_gc = NULL;
43 redraw_overlay (GtkWidget * widget)
45 gdk_draw_rectangle (widget->window, widget->style->white_gc, TRUE,
46 0, 0, widget->allocation.width, widget->allocation.height);
50 guint h = widget->allocation.height * 0.75;
52 gdk_draw_rectangle (widget->window, trans_gc, TRUE,
53 0, 0, widget->allocation.width, h);
55 for (y = h; y < widget->allocation.height; y++) {
56 for (x = 0; x < widget->allocation.width; x++) {
57 if (((x & 1) || (y & 1)) && (x & 1) != (y & 1)) {
58 gdk_draw_point (widget->window, trans_gc, x, y);
66 handle_resize_cb (GtkWidget * widget, GdkEventConfigure * event, gpointer data)
68 redraw_overlay (widget);
73 handle_expose_cb (GtkWidget * widget, GdkEventExpose * event, gpointer data)
75 redraw_overlay (widget);
80 realize_cb (GtkWidget * widget, gpointer data)
82 #if GTK_CHECK_VERSION(2,18,0)
83 /* This is here just for pedagogical purposes, GDK_WINDOW_XID will call it
85 if (!gdk_window_ensure_native (widget->window))
86 g_error ("Couldn't create native window needed for GstXOverlay!");
89 embed_xid = GDK_WINDOW_XID (video_window->window);
90 g_print ("Window realize: got XID %lu\n", embed_xid);
94 msg_state_changed (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
96 const GstStructure *s;
98 s = gst_message_get_structure (message);
100 /* We only care about state changed on the pipeline */
101 if (s && GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
102 GstState old, new, pending;
105 gst_message_parse_state_changed (message, &old, &new, &pending);
107 /* When state of the pipeline changes to paused or playing we start updating scale */
108 switch (GST_STATE_TRANSITION (old, new)) {
109 case GST_STATE_CHANGE_READY_TO_PAUSED:
110 g_object_get (G_OBJECT (sink), "colorkey", &color, NULL);
112 GdkColor trans_color = { 0,
113 (color & 0xff0000) >> 8,
118 trans_gc = gdk_gc_new (video_window->window);
119 gdk_gc_set_rgb_fg_color (trans_gc, &trans_color);
121 handle_resize_cb (video_window, NULL, NULL);
130 window_closed (GtkWidget * widget, GdkEvent * event, gpointer user_data)
132 GstElement *pipeline = user_data;
134 g_print ("stopping\n");
135 gtk_widget_hide_all (widget);
136 gst_element_set_state (pipeline, GST_STATE_NULL);
141 start_pipeline (gpointer user_data)
143 GstElement *pipeline = GST_ELEMENT (user_data);
144 GstStateChangeReturn sret;
146 sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
147 if (sret == GST_STATE_CHANGE_FAILURE) {
148 gst_element_set_state (pipeline, GST_STATE_NULL);
149 gst_object_unref (pipeline);
156 main (int argc, char **argv)
159 GstElement *pipeline, *src;
161 GstStateChangeReturn sret;
162 GstPropertyProbe *probe;
165 if (!g_thread_supported ())
166 g_thread_init (NULL);
168 gst_init (&argc, &argv);
169 gtk_init (&argc, &argv);
171 /* prepare the pipeline */
173 pipeline = gst_pipeline_new ("xvoverlay");
174 src = gst_element_factory_make ("videotestsrc", NULL);
175 sink = gst_element_factory_make ("xvimagesink", NULL);
176 gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
177 gst_element_link (src, sink);
179 #define COLOR_GRAY 0x7F7F7F
181 g_object_set (G_OBJECT (sink), "autopaint-colorkey", FALSE,
182 "force-aspect-ratio", TRUE, "draw-borders", FALSE,
183 "colorkey", COLOR_GRAY, NULL);
185 /* check xvimagesink capabilities */
186 sret = gst_element_set_state (pipeline, GST_STATE_READY);
187 if (sret == GST_STATE_CHANGE_FAILURE) {
188 g_printerr ("Can't set pipeline to READY\n");
189 gst_object_unref (pipeline);
193 probe = GST_PROPERTY_PROBE (sink);
195 g_printerr ("Can't probe sink\n");
196 gst_element_set_state (pipeline, GST_STATE_NULL);
197 gst_object_unref (pipeline);
201 gst_property_probe_probe_and_get_values_name (probe,
202 "autopaint-colorkey");
203 if (!arr || !arr->n_values) {
204 g_printerr ("Can't disable autopaint-colorkey property\n");
205 gst_element_set_state (pipeline, GST_STATE_NULL);
206 gst_object_unref (pipeline);
210 g_value_array_free (arr);
212 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
213 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
214 g_signal_connect (bus, "message::state-changed",
215 G_CALLBACK (msg_state_changed), pipeline);
216 gst_object_unref (bus);
220 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
221 g_signal_connect (G_OBJECT (window), "delete-event",
222 G_CALLBACK (window_closed), (gpointer) pipeline);
223 gtk_window_set_default_size (GTK_WINDOW (window), 320, 240);
225 video_window = gtk_drawing_area_new ();
226 g_signal_connect (G_OBJECT (video_window), "configure-event",
227 G_CALLBACK (handle_resize_cb), NULL);
228 g_signal_connect (G_OBJECT (video_window), "expose-event",
229 G_CALLBACK (handle_expose_cb), NULL);
230 g_signal_connect (video_window, "realize", G_CALLBACK (realize_cb), NULL);
231 gtk_widget_set_double_buffered (video_window, FALSE);
232 gtk_container_add (GTK_CONTAINER (window), video_window);
234 /* show the gui and play */
236 gtk_widget_show_all (window);
238 /* realize window now so that the video window gets created and we can
239 * obtain its XID before the pipeline is started up and the videosink
240 * asks for the XID of the window to render onto */
241 gtk_widget_realize (window);
243 /* we should have the XID now */
244 g_assert (embed_xid != 0);
246 /* we know what the video sink is in this case (xvimagesink), so we can
247 * just set it directly here now (instead of waiting for a prepare-xwindow-id
248 * element message in a sync bus handler and setting it there) */
249 g_print ("setting XID %lu\n", embed_xid);
250 gst_x_overlay_set_xwindow_id (GST_X_OVERLAY (sink), embed_xid);
252 g_idle_add (start_pipeline, pipeline);
255 gst_object_unref (pipeline);