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