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.
36 #include <gst/interfaces/xoverlay.h>
37 #include <gst/interfaces/propertyprobe.h>
39 static GtkWidget *video_window = NULL;
40 static GstElement *sink = NULL;
41 static guint embed_xid = 0;
42 static GdkGC *trans_gc = NULL;
44 static GstBusSyncReply
45 bus_sync_handler (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
47 if ((GST_MESSAGE_TYPE (message) == GST_MESSAGE_ELEMENT) &&
48 gst_structure_has_name (message->structure, "prepare-xwindow-id")) {
49 GstElement *element = GST_ELEMENT (GST_MESSAGE_SRC (message));
51 g_print ("got prepare-xwindow-id\n");
53 embed_xid = GDK_WINDOW_XID (GDK_WINDOW (video_window->window));
56 if (g_object_class_find_property (G_OBJECT_GET_CLASS (element),
57 "force-aspect-ratio")) {
58 g_object_set (element, "force-aspect-ratio", TRUE, NULL);
61 gst_x_overlay_set_xwindow_id (GST_X_OVERLAY (GST_MESSAGE_SRC (message)),
68 redraw_overlay (GtkWidget * widget)
70 gdk_draw_rectangle (widget->window, widget->style->white_gc, TRUE,
71 0, 0, widget->allocation.width, widget->allocation.height);
75 guint h = widget->allocation.height * 0.75;
77 gdk_draw_rectangle (widget->window, trans_gc, TRUE,
78 0, 0, widget->allocation.width, h);
80 for (y = h; y < widget->allocation.height; y++) {
81 for (x = 0; x < widget->allocation.width; x++) {
82 if (((x & 1) || (y & 1)) && (x & 1) != (y & 1)) {
83 gdk_draw_point (widget->window, trans_gc, x, y);
91 handle_resize_cb (GtkWidget * widget, GdkEventConfigure * event, gpointer data)
93 redraw_overlay (widget);
98 handle_expose_cb (GtkWidget * widget, GdkEventExpose * event, gpointer data)
100 redraw_overlay (widget);
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 GdkColor trans_color = { 0,
124 (color & 0xff0000) >> 8,
129 trans_gc = gdk_gc_new (video_window->window);
130 gdk_gc_set_rgb_fg_color (trans_gc, &trans_color);
132 handle_resize_cb (video_window, NULL, NULL);
141 window_closed (GtkWidget * widget, GdkEvent * event, gpointer user_data)
143 GstElement *pipeline = user_data;
145 g_print ("stopping\n");
146 gtk_widget_hide_all (widget);
147 gst_element_set_state (pipeline, GST_STATE_NULL);
152 start_pipeline (gpointer user_data)
154 GstElement *pipeline = GST_ELEMENT (user_data);
155 GstStateChangeReturn sret;
157 sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
158 if (sret == GST_STATE_CHANGE_FAILURE) {
159 gst_element_set_state (pipeline, GST_STATE_NULL);
160 gst_object_unref (pipeline);
167 main (int argc, char **argv)
170 GstElement *pipeline, *src;
172 GstStateChangeReturn sret;
173 GstPropertyProbe *probe;
176 if (!g_thread_supported ())
177 g_thread_init (NULL);
179 if (!XInitThreads ()) {
180 g_print ("XInitThreads failed\n");
184 gst_init (&argc, &argv);
185 gtk_init (&argc, &argv);
187 /* prepare the pipeline */
189 pipeline = gst_pipeline_new ("xvoverlay");
190 src = gst_element_factory_make ("videotestsrc", NULL);
191 sink = gst_element_factory_make ("xvimagesink", NULL);
192 gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
193 gst_element_link (src, sink);
195 g_object_set (G_OBJECT (sink), "autopaint-colorkey", FALSE, "force-aspect-ratio", TRUE, "draw-borders", FALSE, "colorkey", 0x7F7F7F, /* gray */
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 pipelien 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);
225 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
226 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bus_sync_handler,
228 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
229 g_signal_connect (bus, "message::state-changed",
230 G_CALLBACK (msg_state_changed), pipeline);
231 gst_object_unref (bus);
235 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
236 g_signal_connect (G_OBJECT (window), "delete-event",
237 G_CALLBACK (window_closed), (gpointer) pipeline);
238 gtk_window_set_default_size (GTK_WINDOW (window), 320, 240);
240 video_window = gtk_drawing_area_new ();
241 g_signal_connect (G_OBJECT (video_window), "configure-event",
242 G_CALLBACK (handle_resize_cb), NULL);
243 g_signal_connect (G_OBJECT (video_window), "expose-event",
244 G_CALLBACK (handle_expose_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);
251 g_idle_add (start_pipeline, pipeline);
254 gst_object_unref (pipeline);