sys/xvimage/xvimagesink.*: Add a "draw-border" property that can be set to false...
[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 <glib.h>
28 #include <gtk/gtk.h>
29 #include <gst/gst.h>
30 #include <string.h>
31
32 #include <X11/Xlib.h>
33 #include <gdk/gdkx.h>
34 #include <gst/interfaces/xoverlay.h>
35
36 static GtkWidget *video_window = NULL;
37 static GstElement *sink = NULL;
38 static guint embed_xid = 0;
39 static GdkGC *trans_gc = NULL;
40
41 static GstBusSyncReply
42 bus_sync_handler (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
43 {
44   if ((GST_MESSAGE_TYPE (message) == GST_MESSAGE_ELEMENT) &&
45       gst_structure_has_name (message->structure, "prepare-xwindow-id")) {
46     GstElement *element = GST_ELEMENT (GST_MESSAGE_SRC (message));
47
48     g_print ("got prepare-xwindow-id\n");
49     if (!embed_xid) {
50       embed_xid = GDK_WINDOW_XID (GDK_WINDOW (video_window->window));
51     }
52
53     if (g_object_class_find_property (G_OBJECT_GET_CLASS (element),
54             "force-aspect-ratio")) {
55       g_object_set (element, "force-aspect-ratio", TRUE, NULL);
56     }
57
58     gst_x_overlay_set_xwindow_id (GST_X_OVERLAY (GST_MESSAGE_SRC (message)),
59         embed_xid);
60   }
61   return GST_BUS_PASS;
62 }
63
64 static gboolean
65 handle_resize_cb (GtkWidget * widget, GdkEventConfigure * event, gpointer data)
66 {
67   gdk_draw_rectangle (widget->window, widget->style->white_gc, TRUE,
68       0, 0, widget->allocation.width, widget->allocation.height);
69
70   if (trans_gc) {
71     guint x, y;
72     guint h = widget->allocation.height * 0.75;
73
74     gdk_draw_rectangle (widget->window, trans_gc, TRUE,
75         0, 0, widget->allocation.width, h);
76
77     for (y = h; y < widget->allocation.height; y++) {
78       for (x = 0; x < widget->allocation.width; x++) {
79         if (((x & 1) || (y & 1)) && (x & 1) != (y & 1)) {
80           gdk_draw_point (widget->window, trans_gc, x, y);
81         }
82       }
83     }
84   }
85   return FALSE;
86 }
87
88 static void
89 msg_state_changed (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
90 {
91   const GstStructure *s;
92
93   s = gst_message_get_structure (message);
94
95   /* We only care about state changed on the pipeline */
96   if (s && GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
97     GstState old, new, pending;
98     gint color;
99
100     gst_message_parse_state_changed (message, &old, &new, &pending);
101
102     /* When state of the pipeline changes to paused or playing we start updating scale */
103     switch (GST_STATE_TRANSITION (old, new)) {
104       case GST_STATE_CHANGE_READY_TO_PAUSED:
105         g_object_get (G_OBJECT (sink), "colorkey", &color, NULL);
106         if (color != -1) {
107           GdkColor trans_color = { 0,
108             (color & 0xff0000) >> 8,
109             (color & 0xff00),
110             (color & 0xff) << 8
111           };
112
113           trans_gc = gdk_gc_new (video_window->window);
114           gdk_gc_set_rgb_fg_color (trans_gc, &trans_color);
115         }
116         handle_resize_cb (video_window, NULL, NULL);
117         break;
118       default:
119         break;
120     }
121   }
122 }
123
124 static void
125 window_closed (GtkWidget * widget, GdkEvent * event, gpointer user_data)
126 {
127   GstElement *pipeline = user_data;
128
129   g_print ("stopping\n");
130   gtk_widget_hide_all (widget);
131   gst_element_set_state (pipeline, GST_STATE_NULL);
132   gtk_main_quit ();
133 }
134
135 int
136 main (int argc, char **argv)
137 {
138   GtkWidget *window;
139   GstElement *pipeline, *src;
140   GstBus *bus;
141
142   if (!g_thread_supported ())
143     g_thread_init (NULL);
144
145   if (!XInitThreads ()) {
146     g_print ("XInitThreads failed\n");
147     exit (-1);
148   }
149
150   gst_init (&argc, &argv);
151   gtk_init (&argc, &argv);
152
153   /* prepare the pipeline */
154
155   pipeline = gst_pipeline_new ("xvoverlay");
156   src = gst_element_factory_make ("videotestsrc", NULL);
157   sink = gst_element_factory_make ("xvimagesink", NULL);
158   gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
159   gst_element_link (src, sink);
160
161   g_object_set (G_OBJECT (sink),
162       "autopaint-colorkey", FALSE,
163       "force-aspect-ratio", TRUE, "draw-borders", FALSE, NULL);
164
165   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
166   gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bus_sync_handler,
167       pipeline);
168   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
169   g_signal_connect (bus, "message::state-changed",
170       G_CALLBACK (msg_state_changed), pipeline);
171   gst_object_unref (bus);
172
173   /* prepare the ui */
174
175   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
176   video_window = gtk_drawing_area_new ();
177   gtk_widget_set_double_buffered (video_window, FALSE);
178
179   gtk_window_set_default_size (GTK_WINDOW (window), 320, 240);
180   gtk_container_add (GTK_CONTAINER (window), video_window);
181   g_signal_connect (G_OBJECT (window), "delete-event",
182       G_CALLBACK (window_closed), (gpointer) pipeline);
183   g_signal_connect (G_OBJECT (video_window), "configure-event",
184       G_CALLBACK (handle_resize_cb), NULL);
185
186   /* show the gui. */
187   gtk_widget_show_all (window);
188
189   //connect_bus_signals (pipeline);
190   gst_element_set_state (pipeline, GST_STATE_PLAYING);
191   gtk_main ();
192   gst_object_unref (pipeline);
193
194   return 0;
195 }