2 * Copyright (C) <2008> Stefan Kost <ensonic@users.sf.net>
4 * test-xoverlay: test xoverlay custom event handling and subregions
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.
35 #include <gst/interfaces/xoverlay.h>
36 #include <gst/video/gstvideosink.h>
44 GstVideoRectangle rect;
48 static gboolean verbose = FALSE;
51 animate_render_rect (gpointer user_data)
53 if (anim_state.running) {
54 GstVideoRectangle *r = &anim_state.rect;
55 gdouble s = sin (3.0 * anim_state.a);
56 gdouble c = cos (2.0 * anim_state.a);
58 anim_state.a += anim_state.p;
59 if (anim_state.a > (G_PI + G_PI))
60 anim_state.a -= (G_PI + G_PI);
62 r->w = anim_state.w / 2;
63 r->x = (r->w - (r->w / 2)) + c * (r->w / 2);
64 r->h = anim_state.h / 2;
65 r->y = (r->h - (r->h / 2)) + s * (r->h / 2);
67 gst_x_overlay_set_render_rectangle (anim_state.overlay, r->x, r->y,
69 gtk_widget_queue_draw (anim_state.widget);
75 handle_resize_cb (GtkWidget * widget, GdkEventConfigure * event,
78 GtkAllocation allocation;
80 gtk_widget_get_allocation (widget, &allocation);
83 g_print ("resize(%p): %dx%d\n", widget, allocation.width,
86 anim_state.w = allocation.width;
87 anim_state.h = allocation.height;
88 animate_render_rect (NULL);
94 handle_draw_cb (GtkWidget * widget, cairo_t * cr, gpointer user_data)
96 GstVideoRectangle *r = &anim_state.rect;
100 width = gtk_widget_get_allocated_width (widget);
101 height = gtk_widget_get_allocated_height (widget);
103 style = gtk_widget_get_style (widget);
105 gdk_cairo_set_source_color (cr, &style->bg[GTK_STATE_NORMAL]);
107 /* we should only redraw outside of the video rect! */
108 cairo_rectangle (cr, 0, 0, r->x, height);
109 cairo_rectangle (cr, r->x + r->w, 0, width - (r->x + r->w), height);
111 cairo_rectangle (cr, 0, 0, width, r->y);
112 cairo_rectangle (cr, 0, r->y + r->h, width, height - (r->y + r->h));
117 g_print ("draw(%p)\n", widget);
119 gst_x_overlay_expose (anim_state.overlay);
124 window_closed (GtkWidget * widget, GdkEvent * event, gpointer user_data)
126 GstElement *pipeline = user_data;
129 g_print ("stopping\n");
131 anim_state.running = FALSE;
132 gtk_widget_hide (widget);
133 gst_element_set_state (pipeline, GST_STATE_NULL);
138 main (gint argc, gchar ** argv)
140 GdkWindow *video_window_xwindow;
141 GtkWidget *window, *video_window;
142 GstElement *pipeline, *src, *sink;
143 GstStateChangeReturn sret;
144 gulong embed_xid = 0;
145 gboolean force_aspect = FALSE, draw_borders = FALSE;
147 if (!g_thread_supported ())
148 g_thread_init (NULL);
150 gst_init (&argc, &argv);
151 gtk_init (&argc, &argv);
155 for (arg = 0; arg < argc; arg++) {
156 if (!strcmp (argv[arg], "-a"))
158 else if (!strcmp (argv[arg], "-b"))
160 else if (!strcmp (argv[arg], "-v"))
165 /* prepare the pipeline */
167 pipeline = gst_pipeline_new ("xvoverlay");
168 src = gst_element_factory_make ("videotestsrc", NULL);
169 sink = gst_element_factory_make ("xvimagesink", NULL);
170 gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
171 gst_element_link (src, sink);
173 g_object_set (G_OBJECT (sink), "handle-events", FALSE,
174 "force-aspect-ratio", force_aspect, "draw-borders", draw_borders, NULL);
178 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
179 g_signal_connect (G_OBJECT (window), "delete-event",
180 G_CALLBACK (window_closed), (gpointer) pipeline);
181 gtk_window_set_default_size (GTK_WINDOW (window), 320, 240);
183 video_window = gtk_drawing_area_new ();
184 gtk_widget_set_double_buffered (video_window, FALSE);
185 gtk_container_add (GTK_CONTAINER (window), video_window);
187 /* show the gui and play */
188 gtk_widget_show_all (window);
190 /* realize window now so that the video window gets created and we can
191 * obtain its XID before the pipeline is started up and the videosink
192 * asks for the XID of the window to render onto */
193 gtk_widget_realize (window);
195 video_window_xwindow = gtk_widget_get_window (video_window);
196 embed_xid = GDK_WINDOW_XID (video_window_xwindow);
198 g_print ("Window realize: got XID %lu\n", embed_xid);
201 /* we know what the video sink is in this case (xvimagesink), so we can
202 * just set it directly here now (instead of waiting for a prepare-xwindow-id
203 * element message in a sync bus handler and setting it there) */
204 gst_x_overlay_set_window_handle (GST_X_OVERLAY (sink), embed_xid);
206 anim_state.overlay = GST_X_OVERLAY (sink);
207 anim_state.widget = video_window;
211 anim_state.p = (G_PI + G_PI) / 200.0;
213 handle_resize_cb (video_window, NULL, sink);
214 g_signal_connect (video_window, "configure-event",
215 G_CALLBACK (handle_resize_cb), NULL);
216 g_signal_connect (video_window, "draw", G_CALLBACK (handle_draw_cb), NULL);
218 g_timeout_add (50, (GSourceFunc) animate_render_rect, NULL);
220 /* run the pipeline */
221 sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
222 if (sret == GST_STATE_CHANGE_FAILURE)
223 gst_element_set_state (pipeline, GST_STATE_NULL);
225 anim_state.running = TRUE;
229 gst_object_unref (pipeline);