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