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/interfaces/xoverlay.h>
36 static GtkWidget *video_window = NULL;
37 static GstElement *sink = NULL;
38 static guint embed_xid = 0;
39 static GdkGC *trans_gc = NULL;
41 static GstBusSyncReply
42 bus_sync_handler (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
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));
48 g_print ("got prepare-xwindow-id\n");
50 embed_xid = GDK_WINDOW_XID (GDK_WINDOW (video_window->window));
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);
58 gst_x_overlay_set_xwindow_id (GST_X_OVERLAY (GST_MESSAGE_SRC (message)),
65 handle_resize_cb (GtkWidget * widget, GdkEventConfigure * event, gpointer data)
67 gdk_draw_rectangle (widget->window, widget->style->white_gc, TRUE,
68 0, 0, widget->allocation.width, widget->allocation.height);
72 guint h = widget->allocation.height * 0.75;
74 gdk_draw_rectangle (widget->window, trans_gc, TRUE,
75 0, 0, widget->allocation.width, h);
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);
89 msg_state_changed (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
91 const GstStructure *s;
93 s = gst_message_get_structure (message);
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;
100 gst_message_parse_state_changed (message, &old, &new, &pending);
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);
107 GdkColor trans_color = { 0,
108 (color & 0xff0000) >> 8,
113 trans_gc = gdk_gc_new (video_window->window);
114 gdk_gc_set_rgb_fg_color (trans_gc, &trans_color);
116 handle_resize_cb (video_window, NULL, NULL);
125 window_closed (GtkWidget * widget, GdkEvent * event, gpointer user_data)
127 GstElement *pipeline = user_data;
129 g_print ("stopping\n");
130 gtk_widget_hide_all (widget);
131 gst_element_set_state (pipeline, GST_STATE_NULL);
136 main (int argc, char **argv)
139 GstElement *pipeline, *src;
142 if (!g_thread_supported ())
143 g_thread_init (NULL);
145 if (!XInitThreads ()) {
146 g_print ("XInitThreads failed\n");
150 gst_init (&argc, &argv);
151 gtk_init (&argc, &argv);
153 /* prepare the pipeline */
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);
161 g_object_set (G_OBJECT (sink),
162 "autopaint-colorkey", FALSE,
163 "force-aspect-ratio", TRUE, "draw-borders", FALSE, NULL);
165 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
166 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bus_sync_handler,
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);
175 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
176 video_window = gtk_drawing_area_new ();
177 gtk_widget_set_double_buffered (video_window, FALSE);
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);
187 gtk_widget_show_all (window);
189 //connect_bus_signals (pipeline);
190 gst_element_set_state (pipeline, GST_STATE_PLAYING);
192 gst_object_unref (pipeline);