colorkey-test: fix xsync error
[platform/upstream/gst-plugins-base.git] / tests / icles / test-colorkey.c
1 /* GStreamer
2  * Copyright (C) <2008> Stefan Kost <ensonic@users.sf.net>
3  *
4  * test-colorkey: test manual colorkey handling
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <glib.h>
30 #include <gdk/gdkx.h>
31 #include <gtk/gtk.h>
32
33 #include <X11/Xlib.h>
34
35 #include <gst/gst.h>
36 #include <gst/interfaces/xoverlay.h>
37 #include <gst/interfaces/propertyprobe.h>
38
39 static GtkWidget *video_window = NULL;
40 static GstElement *sink = NULL;
41 static guint embed_xid = 0;
42 static GdkGC *trans_gc = NULL;
43
44 static GstBusSyncReply
45 bus_sync_handler (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
46 {
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));
50
51     g_print ("got prepare-xwindow-id\n");
52     if (!embed_xid) {
53       embed_xid = GDK_WINDOW_XID (GDK_WINDOW (video_window->window));
54     }
55
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);
59     }
60
61     gst_x_overlay_set_xwindow_id (GST_X_OVERLAY (GST_MESSAGE_SRC (message)),
62         embed_xid);
63   }
64   return GST_BUS_PASS;
65 }
66
67 static void
68 redraw_overlay (GtkWidget * widget)
69 {
70   gdk_draw_rectangle (widget->window, widget->style->white_gc, TRUE,
71       0, 0, widget->allocation.width, widget->allocation.height);
72
73   if (trans_gc) {
74     guint x, y;
75     guint h = widget->allocation.height * 0.75;
76
77     gdk_draw_rectangle (widget->window, trans_gc, TRUE,
78         0, 0, widget->allocation.width, h);
79
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);
84         }
85       }
86     }
87   }
88 }
89
90 static gboolean
91 handle_resize_cb (GtkWidget * widget, GdkEventConfigure * event, gpointer data)
92 {
93   redraw_overlay (widget);
94   return FALSE;
95 }
96
97 static gboolean
98 handle_expose_cb (GtkWidget * widget, GdkEventExpose * event, gpointer data)
99 {
100   redraw_overlay (widget);
101   return FALSE;
102 }
103
104 static void
105 msg_state_changed (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
106 {
107   const GstStructure *s;
108
109   s = gst_message_get_structure (message);
110
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;
114     gint color;
115
116     gst_message_parse_state_changed (message, &old, &new, &pending);
117
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);
122         if (color != -1) {
123           GdkColor trans_color = { 0,
124             (color & 0xff0000) >> 8,
125             (color & 0xff00),
126             (color & 0xff) << 8
127           };
128
129           trans_gc = gdk_gc_new (video_window->window);
130           gdk_gc_set_rgb_fg_color (trans_gc, &trans_color);
131         }
132         handle_resize_cb (video_window, NULL, NULL);
133         break;
134       default:
135         break;
136     }
137   }
138 }
139
140 static void
141 window_closed (GtkWidget * widget, GdkEvent * event, gpointer user_data)
142 {
143   GstElement *pipeline = user_data;
144
145   g_print ("stopping\n");
146   gtk_widget_hide_all (widget);
147   gst_element_set_state (pipeline, GST_STATE_NULL);
148   gtk_main_quit ();
149 }
150
151 static gboolean
152 start_pipeline (gpointer user_data)
153 {
154   GstElement *pipeline = GST_ELEMENT (user_data);
155   GstStateChangeReturn sret;
156
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);
161     gtk_main_quit ();
162   }
163   return FALSE;
164 }
165
166 int
167 main (int argc, char **argv)
168 {
169   GtkWidget *window;
170   GstElement *pipeline, *src;
171   GstBus *bus;
172   GstStateChangeReturn sret;
173   GstPropertyProbe *probe;
174   GValueArray *arr;
175
176   if (!g_thread_supported ())
177     g_thread_init (NULL);
178
179   if (!XInitThreads ()) {
180     g_print ("XInitThreads failed\n");
181     exit (-1);
182   }
183
184   gst_init (&argc, &argv);
185   gtk_init (&argc, &argv);
186
187   /* prepare the pipeline */
188
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);
194
195   g_object_set (G_OBJECT (sink), "autopaint-colorkey", FALSE, "force-aspect-ratio", TRUE, "draw-borders", FALSE, "colorkey", 0x7F7F7F,  /* gray */
196       NULL);
197
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);
203     return -1;
204   }
205
206   probe = GST_PROPERTY_PROBE (sink);
207   if (!probe) {
208     g_printerr ("Can't probe sink\n");
209     gst_element_set_state (pipeline, GST_STATE_NULL);
210     gst_object_unref (pipeline);
211     return -1;
212   }
213   arr =
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);
220     return -1;
221   }
222   if (arr)
223     g_value_array_free (arr);
224
225   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
226   gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bus_sync_handler,
227       pipeline);
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);
232
233   /* prepare the ui */
234
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);
239
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);
247
248   /* show the gui and play */
249
250   gtk_widget_show_all (window);
251   g_idle_add (start_pipeline, pipeline);
252   gtk_main ();
253
254   gst_object_unref (pipeline);
255
256   return 0;
257 }