Allow setting colorkey if possible. Implement property probe interface for optional...
[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 <X11/Xlib.h>
34
35 #include <gst/gst.h>
36 #include <gst/interfaces/xoverlay.h>
37 #include <gst/interfaces/propertyprobe.h>
38
39 static GtkWidget *video_window = NULL;
40 static GstElement *sink = NULL;
41 static guint embed_xid = 0;
42 static GdkGC *trans_gc = NULL;
43
44 static GstBusSyncReply
45 bus_sync_handler (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
46 {
47   if ((GST_MESSAGE_TYPE (message) == GST_MESSAGE_ELEMENT) &&
48       gst_structure_has_name (message->structure, "prepare-xwindow-id")) {
49     GstElement *element = GST_ELEMENT (GST_MESSAGE_SRC (message));
50
51     g_print ("got prepare-xwindow-id\n");
52     if (!embed_xid) {
53       embed_xid = GDK_WINDOW_XID (GDK_WINDOW (video_window->window));
54     }
55
56     if (g_object_class_find_property (G_OBJECT_GET_CLASS (element),
57             "force-aspect-ratio")) {
58       g_object_set (element, "force-aspect-ratio", TRUE, NULL);
59     }
60
61     gst_x_overlay_set_xwindow_id (GST_X_OVERLAY (GST_MESSAGE_SRC (message)),
62         embed_xid);
63   }
64   return GST_BUS_PASS;
65 }
66
67 static void
68 redraw_overlay (GtkWidget * widget)
69 {
70   gdk_draw_rectangle (widget->window, widget->style->white_gc, TRUE,
71       0, 0, widget->allocation.width, widget->allocation.height);
72
73   if (trans_gc) {
74     guint x, y;
75     guint h = widget->allocation.height * 0.75;
76
77     gdk_draw_rectangle (widget->window, trans_gc, TRUE,
78         0, 0, widget->allocation.width, h);
79
80     for (y = h; y < widget->allocation.height; y++) {
81       for (x = 0; x < widget->allocation.width; x++) {
82         if (((x & 1) || (y & 1)) && (x & 1) != (y & 1)) {
83           gdk_draw_point (widget->window, trans_gc, x, y);
84         }
85       }
86     }
87   }
88 }
89
90 static gboolean
91 handle_resize_cb (GtkWidget * widget, GdkEventConfigure * event, gpointer data)
92 {
93   redraw_overlay (widget);
94   return FALSE;
95 }
96
97 static gboolean
98 handle_expose_cb (GtkWidget * widget, GdkEventExpose * event, gpointer data)
99 {
100   redraw_overlay (widget);
101   return FALSE;
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           GdkColor trans_color = { 0,
124             (color & 0xff0000) >> 8,
125             (color & 0xff00),
126             (color & 0xff) << 8
127           };
128
129           trans_gc = gdk_gc_new (video_window->window);
130           gdk_gc_set_rgb_fg_color (trans_gc, &trans_color);
131         }
132         handle_resize_cb (video_window, NULL, NULL);
133         break;
134       default:
135         break;
136     }
137   }
138 }
139
140 static void
141 window_closed (GtkWidget * widget, GdkEvent * event, gpointer user_data)
142 {
143   GstElement *pipeline = user_data;
144
145   g_print ("stopping\n");
146   gtk_widget_hide_all (widget);
147   gst_element_set_state (pipeline, GST_STATE_NULL);
148   gtk_main_quit ();
149 }
150
151 int
152 main (int argc, char **argv)
153 {
154   GtkWidget *window;
155   GstElement *pipeline, *src;
156   GstBus *bus;
157   GstStateChangeReturn sret;
158   GstPropertyProbe *probe;
159   GValueArray *arr;
160
161   if (!g_thread_supported ())
162     g_thread_init (NULL);
163
164   if (!XInitThreads ()) {
165     g_print ("XInitThreads failed\n");
166     exit (-1);
167   }
168
169   gst_init (&argc, &argv);
170   gtk_init (&argc, &argv);
171
172   /* prepare the pipeline */
173
174   pipeline = gst_pipeline_new ("xvoverlay");
175   src = gst_element_factory_make ("videotestsrc", NULL);
176   sink = gst_element_factory_make ("xvimagesink", NULL);
177   gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
178   gst_element_link (src, sink);
179
180   g_object_set (G_OBJECT (sink), "autopaint-colorkey", FALSE, "force-aspect-ratio", TRUE, "draw-borders", FALSE, "colorkey", 0x7F7F7F,  /* gray */
181       NULL);
182
183   /* check xvimagesink capabilities */
184   sret = gst_element_set_state (pipeline, GST_STATE_READY);
185   if (sret == GST_STATE_CHANGE_FAILURE) {
186     g_printerr ("Can't set pipelien to READY\n");
187     gst_object_unref (pipeline);
188     return -1;
189   }
190
191   probe = GST_PROPERTY_PROBE (sink);
192   if (!probe) {
193     g_printerr ("Can't probe sink\n");
194     gst_element_set_state (pipeline, GST_STATE_NULL);
195     gst_object_unref (pipeline);
196     return -1;
197   }
198   arr =
199       gst_property_probe_probe_and_get_values_name (probe,
200       "autopaint-colorkey");
201   if (!arr || !arr->n_values) {
202     g_printerr ("Can't disable autopaint-colorkey property\n");
203     gst_element_set_state (pipeline, GST_STATE_NULL);
204     gst_object_unref (pipeline);
205     return -1;
206   }
207   if (arr)
208     g_value_array_free (arr);
209
210   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
211   gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bus_sync_handler,
212       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   gtk_widget_set_double_buffered (video_window, FALSE);
231   gtk_container_add (GTK_CONTAINER (window), video_window);
232
233   /* show the gui and play */
234
235   gtk_widget_show_all (window);
236   sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
237   if (sret == GST_STATE_CHANGE_FAILURE) {
238     gst_element_set_state (pipeline, GST_STATE_NULL);
239     gst_object_unref (pipeline);
240     return -1;
241   }
242   gtk_main ();
243   gst_object_unref (pipeline);
244
245   return 0;
246 }