tests: make Gtk+ test programs compile with -DGSEAL_ENABLE
[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 <gst/gst.h>
34 #include <gst/interfaces/xoverlay.h>
35 #include <gst/interfaces/propertyprobe.h>
36
37 #if !GTK_CHECK_VERSION (2, 17, 7)
38 static void
39 gtk_widget_get_allocation (GtkWidget * w, GtkAllocation * a)
40 {
41   *a = w->allocation;
42 }
43 #endif
44
45 static GtkWidget *video_window = NULL;
46 static GstElement *sink = NULL;
47 static gulong embed_xid = 0;
48 static GdkGC *trans_gc = NULL;
49
50 static void
51 redraw_overlay (GtkWidget * widget)
52 {
53   GtkAllocation allocation;
54   GdkWindow *window = gtk_widget_get_window (widget);
55   GtkStyle *style = gtk_widget_get_style (widget);
56
57   gtk_widget_get_allocation (widget, &allocation);
58   gdk_draw_rectangle (window, style->white_gc, TRUE, 0, 0,
59       allocation.width, allocation.height);
60
61   if (trans_gc) {
62     guint x, y;
63     guint h = allocation.height * 0.75;
64
65     gdk_draw_rectangle (window, trans_gc, TRUE, 0, 0, allocation.width, h);
66
67     for (y = h; y < allocation.height; y++) {
68       for (x = 0; x < allocation.width; x++) {
69         if (((x & 1) || (y & 1)) && (x & 1) != (y & 1)) {
70           gdk_draw_point (window, trans_gc, x, y);
71         }
72       }
73     }
74   }
75 }
76
77 static gboolean
78 handle_resize_cb (GtkWidget * widget, GdkEventConfigure * event, gpointer data)
79 {
80   redraw_overlay (widget);
81   return FALSE;
82 }
83
84 static gboolean
85 handle_expose_cb (GtkWidget * widget, GdkEventExpose * event, gpointer data)
86 {
87   redraw_overlay (widget);
88   return FALSE;
89 }
90
91 static void
92 realize_cb (GtkWidget * widget, gpointer data)
93 {
94 #if GTK_CHECK_VERSION(2,18,0)
95   {
96     GdkWindow *window = gtk_widget_get_window (widget);
97
98     /* This is here just for pedagogical purposes, GDK_WINDOW_XID will call it
99      * as well */
100     if (!gdk_window_ensure_native (window))
101       g_error ("Couldn't create native window needed for GstXOverlay!");
102   }
103 #endif
104
105   {
106     GdkWindow *window = gtk_widget_get_window (video_window);
107
108     embed_xid = GDK_WINDOW_XID (window);
109     g_print ("Window realize: video window XID = %lu\n", embed_xid);
110   }
111 }
112
113 static void
114 msg_state_changed (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
115 {
116   const GstStructure *s;
117
118   s = gst_message_get_structure (message);
119
120   /* We only care about state changed on the pipeline */
121   if (s && GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
122     GstState old, new, pending;
123     gint color;
124
125     gst_message_parse_state_changed (message, &old, &new, &pending);
126
127     /* When state of the pipeline changes to paused or playing we start updating scale */
128     switch (GST_STATE_TRANSITION (old, new)) {
129       case GST_STATE_CHANGE_READY_TO_PAUSED:{
130         GdkWindow *window = gtk_widget_get_window (video_window);
131
132         g_object_get (G_OBJECT (sink), "colorkey", &color, NULL);
133         if (color != -1) {
134           GdkColor trans_color = { 0,
135             (color & 0xff0000) >> 8,
136             (color & 0xff00),
137             (color & 0xff) << 8
138           };
139
140           trans_gc = gdk_gc_new (window);
141           gdk_gc_set_rgb_fg_color (trans_gc, &trans_color);
142         }
143         handle_resize_cb (video_window, NULL, NULL);
144         break;
145       }
146       default:
147         break;
148     }
149   }
150 }
151
152 static void
153 window_closed (GtkWidget * widget, GdkEvent * event, gpointer user_data)
154 {
155   GstElement *pipeline = user_data;
156
157   g_print ("stopping\n");
158   gtk_widget_hide_all (widget);
159   gst_element_set_state (pipeline, GST_STATE_NULL);
160   gtk_main_quit ();
161 }
162
163 static gboolean
164 start_pipeline (gpointer user_data)
165 {
166   GstElement *pipeline = GST_ELEMENT (user_data);
167   GstStateChangeReturn sret;
168
169   sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
170   if (sret == GST_STATE_CHANGE_FAILURE) {
171     gst_element_set_state (pipeline, GST_STATE_NULL);
172     gst_object_unref (pipeline);
173     gtk_main_quit ();
174   }
175   return FALSE;
176 }
177
178 int
179 main (int argc, char **argv)
180 {
181   GtkWidget *window;
182   GstElement *pipeline, *src;
183   GstBus *bus;
184   GstStateChangeReturn sret;
185   GstPropertyProbe *probe;
186   GValueArray *arr;
187
188   if (!g_thread_supported ())
189     g_thread_init (NULL);
190
191   gst_init (&argc, &argv);
192   gtk_init (&argc, &argv);
193
194   /* prepare the pipeline */
195
196   pipeline = gst_pipeline_new ("xvoverlay");
197   src = gst_element_factory_make ("videotestsrc", NULL);
198   sink = gst_element_factory_make ("xvimagesink", NULL);
199   gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
200   gst_element_link (src, sink);
201
202 #define COLOR_GRAY 0x7F7F7F
203
204   g_object_set (G_OBJECT (sink), "autopaint-colorkey", FALSE,
205       "force-aspect-ratio", TRUE, "draw-borders", FALSE,
206       "colorkey", COLOR_GRAY, NULL);
207
208   /* check xvimagesink capabilities */
209   sret = gst_element_set_state (pipeline, GST_STATE_READY);
210   if (sret == GST_STATE_CHANGE_FAILURE) {
211     g_printerr ("Can't set pipeline to READY\n");
212     gst_object_unref (pipeline);
213     return -1;
214   }
215
216   probe = GST_PROPERTY_PROBE (sink);
217   if (!probe) {
218     g_printerr ("Can't probe sink\n");
219     gst_element_set_state (pipeline, GST_STATE_NULL);
220     gst_object_unref (pipeline);
221     return -1;
222   }
223   arr =
224       gst_property_probe_probe_and_get_values_name (probe,
225       "autopaint-colorkey");
226   if (!arr || !arr->n_values) {
227     g_printerr ("Can't disable autopaint-colorkey property\n");
228     gst_element_set_state (pipeline, GST_STATE_NULL);
229     gst_object_unref (pipeline);
230     return -1;
231   }
232   if (arr)
233     g_value_array_free (arr);
234
235   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
236   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
237   g_signal_connect (bus, "message::state-changed",
238       G_CALLBACK (msg_state_changed), pipeline);
239   gst_object_unref (bus);
240
241   /* prepare the ui */
242
243   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
244   g_signal_connect (G_OBJECT (window), "delete-event",
245       G_CALLBACK (window_closed), (gpointer) pipeline);
246   gtk_window_set_default_size (GTK_WINDOW (window), 320, 240);
247
248   video_window = gtk_drawing_area_new ();
249   g_signal_connect (G_OBJECT (video_window), "configure-event",
250       G_CALLBACK (handle_resize_cb), NULL);
251   g_signal_connect (G_OBJECT (video_window), "expose-event",
252       G_CALLBACK (handle_expose_cb), NULL);
253   g_signal_connect (video_window, "realize", G_CALLBACK (realize_cb), NULL);
254   gtk_widget_set_double_buffered (video_window, FALSE);
255   gtk_container_add (GTK_CONTAINER (window), video_window);
256
257   /* show the gui and play */
258
259   gtk_widget_show_all (window);
260
261   /* realize window now so that the video window gets created and we can
262    * obtain its XID before the pipeline is started up and the videosink
263    * asks for the XID of the window to render onto */
264   gtk_widget_realize (window);
265
266   /* we should have the XID now */
267   g_assert (embed_xid != 0);
268
269   /* we know what the video sink is in this case (xvimagesink), so we can
270    * just set it directly here now (instead of waiting for a prepare-xwindow-id
271    * element message in a sync bus handler and setting it there) */
272   g_print ("setting XID %lu\n", embed_xid);
273   gst_x_overlay_set_xwindow_id (GST_X_OVERLAY (sink), embed_xid);
274
275   g_idle_add (start_pipeline, pipeline);
276   gtk_main ();
277
278   gst_object_unref (pipeline);
279
280   return 0;
281 }