Merge remote-tracking branch 'origin/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 #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/video/videooverlay.h>
35
36 static GtkWidget *video_window = NULL;
37 static GstElement *sink = NULL;
38 static gulong embed_xid = 0;
39 static GdkColor trans_color;
40 static gboolean trans_color_set = FALSE;
41
42 static void
43 redraw_overlay (GtkWidget * widget)
44 {
45   GtkAllocation allocation;
46   GdkWindow *window = gtk_widget_get_window (widget);
47   cairo_t *cr;
48
49   cr = gdk_cairo_create (window);
50   gtk_widget_get_allocation (widget, &allocation);
51
52   cairo_set_source_rgb (cr, 1, 1, 1);
53   cairo_rectangle (cr, 0, 0, allocation.width, allocation.height);
54   cairo_fill (cr);
55
56   if (trans_color_set) {
57     guint x, y;
58     guint h = allocation.height * 0.75;
59
60     gdk_cairo_set_source_color (cr, &trans_color);
61     cairo_rectangle (cr, 0, 0, allocation.width, h);
62     cairo_fill (cr);
63
64     for (y = h; y < allocation.height; y++) {
65       for (x = 0; x < allocation.width; x++) {
66         if (((x & 1) || (y & 1)) && (x & 1) != (y & 1)) {
67           cairo_move_to (cr, x, y);
68           cairo_paint (cr);
69         }
70       }
71     }
72   }
73 }
74
75 static gboolean
76 handle_resize_cb (GtkWidget * widget, GdkEventConfigure * event, gpointer data)
77 {
78   redraw_overlay (widget);
79   return FALSE;
80 }
81
82 static gboolean
83 draw_cb (GtkWidget * widget, cairo_t * cr, gpointer data)
84 {
85   redraw_overlay (widget);
86   return FALSE;
87 }
88
89 static void
90 realize_cb (GtkWidget * widget, gpointer data)
91 {
92   GdkWindow *window = gtk_widget_get_window (widget);
93
94   /* This is here just for pedagogical purposes, GDK_WINDOW_XID will call it
95    * as well */
96   if (!gdk_window_ensure_native (window))
97     g_error ("Couldn't create native window needed for GstXOverlay!");
98
99   embed_xid = GDK_WINDOW_XID (window);
100   g_print ("Window realize: video window XID = %lu\n", embed_xid);
101 }
102
103 static void
104 msg_state_changed (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
105 {
106   const GstStructure *s;
107
108   s = gst_message_get_structure (message);
109
110   /* We only care about state changed on the pipeline */
111   if (s && GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
112     GstState old, new, pending;
113     gint color;
114
115     gst_message_parse_state_changed (message, &old, &new, &pending);
116
117     /* When state of the pipeline changes to paused or playing we start updating scale */
118     switch (GST_STATE_TRANSITION (old, new)) {
119       case GST_STATE_CHANGE_READY_TO_PAUSED:{
120         g_object_get (G_OBJECT (sink), "colorkey", &color, NULL);
121         if (color != -1) {
122           trans_color.red = (color & 0xff0000) >> 8;
123           trans_color.green = (color & 0xff00);
124           trans_color.blue = (color & 0xff) << 8;
125           trans_color_set = TRUE;
126         } else {
127           trans_color_set = FALSE;
128         }
129         handle_resize_cb (video_window, NULL, NULL);
130         break;
131       }
132       default:
133         break;
134     }
135   }
136 }
137
138 static void
139 window_closed (GtkWidget * widget, GdkEvent * event, gpointer user_data)
140 {
141   GstElement *pipeline = user_data;
142
143   g_print ("stopping\n");
144   gtk_widget_hide (widget);
145   gst_element_set_state (pipeline, GST_STATE_NULL);
146   gtk_main_quit ();
147 }
148
149 static gboolean
150 start_pipeline (gpointer user_data)
151 {
152   GstElement *pipeline = GST_ELEMENT (user_data);
153   GstStateChangeReturn sret;
154
155   sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
156   if (sret == GST_STATE_CHANGE_FAILURE) {
157     gst_element_set_state (pipeline, GST_STATE_NULL);
158     gst_object_unref (pipeline);
159     gtk_main_quit ();
160   }
161   return FALSE;
162 }
163
164 int
165 main (int argc, char **argv)
166 {
167   GtkWidget *window;
168   GstElement *pipeline, *src;
169   GstBus *bus;
170   GstStateChangeReturn sret;
171 #if 0
172   GstPropertyProbe *probe;
173   GValueArray *arr;
174 #endif
175
176 #if !GLIB_CHECK_VERSION (2, 31, 0)
177   if (!g_thread_supported ())
178     g_thread_init (NULL);
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 }