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