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