2 * Copyright (C) <2008> Stefan Kost <ensonic@users.sf.net>
4 * test-colorkey: test manual colorkey handling
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.
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.
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.
34 #include <gst/video/videooverlay.h>
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;
43 redraw_overlay (GtkWidget * widget)
45 GtkAllocation allocation;
46 GdkWindow *window = gtk_widget_get_window (widget);
49 cr = gdk_cairo_create (window);
50 gtk_widget_get_allocation (widget, &allocation);
52 cairo_set_source_rgb (cr, 1, 1, 1);
53 cairo_rectangle (cr, 0, 0, allocation.width, allocation.height);
56 if (trans_color_set) {
58 guint h = allocation.height * 0.75;
60 gdk_cairo_set_source_color (cr, &trans_color);
61 cairo_rectangle (cr, 0, 0, allocation.width, h);
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);
76 handle_resize_cb (GtkWidget * widget, GdkEventConfigure * event, gpointer data)
78 redraw_overlay (widget);
83 draw_cb (GtkWidget * widget, cairo_t * cr, gpointer data)
85 redraw_overlay (widget);
90 realize_cb (GtkWidget * widget, gpointer data)
92 GdkWindow *window = gtk_widget_get_window (widget);
94 /* This is here just for pedagogical purposes, GDK_WINDOW_XID will call it
96 if (!gdk_window_ensure_native (window))
97 g_error ("Couldn't create native window needed for GstXOverlay!");
99 embed_xid = GDK_WINDOW_XID (window);
100 g_print ("Window realize: video window XID = %lu\n", embed_xid);
104 msg_state_changed (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
106 const GstStructure *s;
108 s = gst_message_get_structure (message);
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;
115 gst_message_parse_state_changed (message, &old, &new, &pending);
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);
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;
127 trans_color_set = FALSE;
129 handle_resize_cb (video_window, NULL, NULL);
139 window_closed (GtkWidget * widget, GdkEvent * event, gpointer user_data)
141 GstElement *pipeline = user_data;
143 g_print ("stopping\n");
144 gtk_widget_hide (widget);
145 gst_element_set_state (pipeline, GST_STATE_NULL);
150 start_pipeline (gpointer user_data)
152 GstElement *pipeline = GST_ELEMENT (user_data);
153 GstStateChangeReturn sret;
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);
165 main (int argc, char **argv)
168 GstElement *pipeline, *src;
170 GstStateChangeReturn sret;
172 GstPropertyProbe *probe;
176 gst_init (&argc, &argv);
177 gtk_init (&argc, &argv);
179 /* prepare the pipeline */
181 pipeline = gst_pipeline_new ("xvoverlay");
182 src = gst_element_factory_make ("videotestsrc", NULL);
183 sink = gst_element_factory_make ("xvimagesink", NULL);
184 gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
185 gst_element_link (src, sink);
187 #define COLOR_GRAY 0x7F7F7F
189 g_object_set (G_OBJECT (sink), "autopaint-colorkey", FALSE,
190 "force-aspect-ratio", TRUE, "draw-borders", FALSE,
191 "colorkey", COLOR_GRAY, NULL);
193 /* check xvimagesink capabilities */
194 sret = gst_element_set_state (pipeline, GST_STATE_READY);
195 if (sret == GST_STATE_CHANGE_FAILURE) {
196 g_printerr ("Can't set pipeline to READY\n");
197 gst_object_unref (pipeline);
201 probe = GST_PROPERTY_PROBE (sink);
203 g_printerr ("Can't probe sink\n");
204 gst_element_set_state (pipeline, GST_STATE_NULL);
205 gst_object_unref (pipeline);
209 gst_property_probe_probe_and_get_values_name (probe,
210 "autopaint-colorkey");
211 if (!arr || !arr->n_values) {
212 g_printerr ("Can't disable autopaint-colorkey property\n");
213 gst_element_set_state (pipeline, GST_STATE_NULL);
214 gst_object_unref (pipeline);
218 g_value_array_free (arr);
221 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
222 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
223 g_signal_connect (bus, "message::state-changed",
224 G_CALLBACK (msg_state_changed), pipeline);
225 gst_object_unref (bus);
229 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
230 g_signal_connect (G_OBJECT (window), "delete-event",
231 G_CALLBACK (window_closed), (gpointer) pipeline);
232 gtk_window_set_default_size (GTK_WINDOW (window), 320, 240);
234 video_window = gtk_drawing_area_new ();
235 g_signal_connect (G_OBJECT (video_window), "configure-event",
236 G_CALLBACK (handle_resize_cb), NULL);
237 g_signal_connect (G_OBJECT (video_window), "draw",
238 G_CALLBACK (draw_cb), NULL);
239 g_signal_connect (video_window, "realize", G_CALLBACK (realize_cb), NULL);
240 gtk_widget_set_double_buffered (video_window, FALSE);
241 gtk_container_add (GTK_CONTAINER (window), video_window);
243 /* show the gui and play */
245 gtk_widget_show_all (window);
247 /* realize window now so that the video window gets created and we can
248 * obtain its XID before the pipeline is started up and the videosink
249 * asks for the XID of the window to render onto */
250 gtk_widget_realize (window);
252 /* we should have the XID now */
253 g_assert (embed_xid != 0);
255 /* we know what the video sink is in this case (xvimagesink), so we can
256 * just set it directly here now (instead of waiting for a
257 * prepare-window-handle element message in a sync bus handler and setting
259 g_print ("setting XID %lu\n", embed_xid);
260 gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (sink), embed_xid);
262 g_idle_add (start_pipeline, pipeline);
265 gst_object_unref (pipeline);