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 main (int argc, char **argv)
155 GstElement *pipeline, *src;
157 GstStateChangeReturn sret;
158 GstPropertyProbe *probe;
161 if (!g_thread_supported ())
162 g_thread_init (NULL);
164 if (!XInitThreads ()) {
165 g_print ("XInitThreads failed\n");
169 gst_init (&argc, &argv);
170 gtk_init (&argc, &argv);
172 /* prepare the pipeline */
174 pipeline = gst_pipeline_new ("xvoverlay");
175 src = gst_element_factory_make ("videotestsrc", NULL);
176 sink = gst_element_factory_make ("xvimagesink", NULL);
177 gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
178 gst_element_link (src, sink);
180 g_object_set (G_OBJECT (sink), "autopaint-colorkey", FALSE, "force-aspect-ratio", TRUE, "draw-borders", FALSE, "colorkey", 0x7F7F7F, /* gray */
183 /* check xvimagesink capabilities */
184 sret = gst_element_set_state (pipeline, GST_STATE_READY);
185 if (sret == GST_STATE_CHANGE_FAILURE) {
186 g_printerr ("Can't set pipelien to READY\n");
187 gst_object_unref (pipeline);
191 probe = GST_PROPERTY_PROBE (sink);
193 g_printerr ("Can't probe sink\n");
194 gst_element_set_state (pipeline, GST_STATE_NULL);
195 gst_object_unref (pipeline);
199 gst_property_probe_probe_and_get_values_name (probe,
200 "autopaint-colorkey");
201 if (!arr || !arr->n_values) {
202 g_printerr ("Can't disable autopaint-colorkey property\n");
203 gst_element_set_state (pipeline, GST_STATE_NULL);
204 gst_object_unref (pipeline);
208 g_value_array_free (arr);
210 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
211 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bus_sync_handler,
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 gtk_widget_set_double_buffered (video_window, FALSE);
231 gtk_container_add (GTK_CONTAINER (window), video_window);
233 /* show the gui and play */
235 gtk_widget_show_all (window);
236 sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
237 if (sret == GST_STATE_CHANGE_FAILURE) {
238 gst_element_set_state (pipeline, GST_STATE_NULL);
239 gst_object_unref (pipeline);
243 gst_object_unref (pipeline);