2 * Copyright (C) <2008> Stefan Kost <ensonic@users.sf.net>
4 * test-videooverlay: test videooverlay 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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
25 /* Disable deprecation warnings because we need to use
26 * gtk_widget_set_double_buffered () or display will flicker */
27 #define GDK_DISABLE_DEPRECATION_WARNINGS
38 #include <gst/video/videooverlay.h>
39 #include <gst/video/gstvideosink.h>
44 GstVideoOverlay *overlay;
47 GstVideoRectangle rect;
51 static gboolean verbose = FALSE;
54 animate_render_rect (gpointer user_data)
56 if (anim_state.running) {
57 GstVideoRectangle *r = &anim_state.rect;
58 gdouble s = sin (3.0 * anim_state.a);
59 gdouble c = cos (2.0 * anim_state.a);
61 anim_state.a += anim_state.p;
62 if (anim_state.a > (G_PI + G_PI))
63 anim_state.a -= (G_PI + G_PI);
65 r->w = anim_state.w / 2;
66 r->x = (r->w - (r->w / 2)) + c * (r->w / 2);
67 r->h = anim_state.h / 2;
68 r->y = (r->h - (r->h / 2)) + s * (r->h / 2);
70 gst_video_overlay_set_render_rectangle (anim_state.overlay, r->x, r->y,
72 gtk_widget_queue_draw (anim_state.widget);
78 handle_resize_cb (GtkWidget * widget, GdkEventConfigure * event,
81 GtkAllocation allocation;
83 gtk_widget_get_allocation (widget, &allocation);
86 g_print ("resize(%p): %dx%d\n", widget, allocation.width,
89 anim_state.w = allocation.width;
90 anim_state.h = allocation.height;
91 animate_render_rect (NULL);
97 handle_draw_cb (GtkWidget * widget, cairo_t * cr, gpointer user_data)
99 GstVideoRectangle *r = &anim_state.rect;
100 GtkStyleContext *style;
104 width = gtk_widget_get_allocated_width (widget);
105 height = gtk_widget_get_allocated_height (widget);
107 style = gtk_widget_get_style_context (widget);
109 gtk_style_context_get_color (style, 0, &color);
110 gdk_cairo_set_source_rgba (cr, &color);
112 /* we should only redraw outside of the video rect! */
113 cairo_rectangle (cr, 0, 0, r->x, height);
114 cairo_rectangle (cr, r->x + r->w, 0, width - (r->x + r->w), height);
116 cairo_rectangle (cr, 0, 0, width, r->y);
117 cairo_rectangle (cr, 0, r->y + r->h, width, height - (r->y + r->h));
122 g_print ("draw(%p)\n", widget);
124 gst_video_overlay_expose (anim_state.overlay);
129 window_closed (GtkWidget * widget, GdkEvent * event, gpointer user_data)
131 GstElement *pipeline = user_data;
134 g_print ("stopping\n");
136 anim_state.running = FALSE;
137 gtk_widget_hide (widget);
138 gst_element_set_state (pipeline, GST_STATE_NULL);
143 main (gint argc, gchar ** argv)
145 GdkWindow *video_window_xwindow;
146 GtkWidget *window, *video_window;
147 GstElement *pipeline, *src, *sink;
148 GstStateChangeReturn sret;
149 gulong embed_xid = 0;
150 gboolean force_aspect = FALSE, draw_borders = FALSE;
152 gst_init (&argc, &argv);
153 gtk_init (&argc, &argv);
157 for (arg = 0; arg < argc; arg++) {
158 if (!strcmp (argv[arg], "-a"))
160 else if (!strcmp (argv[arg], "-b"))
162 else if (!strcmp (argv[arg], "-v"))
167 /* prepare the pipeline */
169 pipeline = gst_pipeline_new ("xvoverlay");
170 src = gst_element_factory_make ("videotestsrc", NULL);
171 sink = gst_element_factory_make ("xvimagesink", NULL);
172 gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
173 gst_element_link (src, sink);
175 g_object_set (G_OBJECT (sink), "handle-events", FALSE,
176 "force-aspect-ratio", force_aspect, "draw-borders", draw_borders, NULL);
180 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
181 g_signal_connect (G_OBJECT (window), "delete-event",
182 G_CALLBACK (window_closed), (gpointer) pipeline);
183 gtk_window_set_default_size (GTK_WINDOW (window), 320, 240);
185 video_window = gtk_drawing_area_new ();
186 gtk_widget_set_double_buffered (video_window, FALSE);
187 gtk_container_add (GTK_CONTAINER (window), video_window);
189 /* show the gui and play */
190 gtk_widget_show_all (window);
192 /* realize window now so that the video window gets created and we can
193 * obtain its XID before the pipeline is started up and the videosink
194 * asks for the XID of the window to render onto */
195 gtk_widget_realize (window);
197 video_window_xwindow = gtk_widget_get_window (video_window);
198 embed_xid = GDK_WINDOW_XID (video_window_xwindow);
200 g_print ("Window realize: got XID %lu\n", embed_xid);
203 /* we know what the video sink is in this case (xvimagesink), so we can
204 * just set it directly here now (instead of waiting for a
205 * prepare-window-handle element message in a sync bus handler and setting
207 gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (sink), embed_xid);
209 anim_state.overlay = GST_VIDEO_OVERLAY (sink);
210 anim_state.widget = video_window;
214 anim_state.p = (G_PI + G_PI) / 200.0;
216 handle_resize_cb (video_window, NULL, sink);
217 g_signal_connect (video_window, "configure-event",
218 G_CALLBACK (handle_resize_cb), NULL);
219 g_signal_connect (video_window, "draw", G_CALLBACK (handle_draw_cb), NULL);
221 g_timeout_add (50, (GSourceFunc) animate_render_rect, NULL);
223 /* run the pipeline */
224 sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
225 if (sret == GST_STATE_CHANGE_FAILURE)
226 gst_element_set_state (pipeline, GST_STATE_NULL);
228 anim_state.running = TRUE;
232 gst_object_unref (pipeline);