1 /* GStreamer
2  * Copyright (C) <2008> Stefan Kost <ensonic@users.sf.net>
3  *
4  * test-videooverlay: test videooverlay custom event handling and subregions
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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
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
28 
29 #include <stdlib.h>
30 #include <string.h>
31 #include <math.h>
32 
33 #include <glib.h>
34 #include <gdk/gdkx.h>
35 #include <gtk/gtk.h>
36 
37 #include <gst/gst.h>
38 #include <gst/video/videooverlay.h>
39 #include <gst/video/gstvideosink.h>
40 
41 static struct
42 {
43   gint w, h;
44   GstVideoOverlay *overlay;
45   GtkWidget *widget;
46   gdouble a, p;
47   GstVideoRectangle rect;
48   gboolean running;
49 } anim_state;
50 
51 static gboolean verbose = FALSE;
52 
53 static gboolean
animate_render_rect(gpointer user_data)54 animate_render_rect (gpointer user_data)
55 {
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);
60 
61     anim_state.a += anim_state.p;
62     if (anim_state.a > (G_PI + G_PI))
63       anim_state.a -= (G_PI + G_PI);
64 
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);
69 
70     gst_video_overlay_set_render_rectangle (anim_state.overlay, r->x, r->y,
71         r->w, r->h);
72     gtk_widget_queue_draw (anim_state.widget);
73   }
74   return TRUE;
75 }
76 
77 static gboolean
handle_resize_cb(GtkWidget * widget,GdkEventConfigure * event,gpointer user_data)78 handle_resize_cb (GtkWidget * widget, GdkEventConfigure * event,
79     gpointer user_data)
80 {
81   GtkAllocation allocation;
82 
83   gtk_widget_get_allocation (widget, &allocation);
84 
85   if (verbose) {
86     g_print ("resize(%p): %dx%d\n", widget, allocation.width,
87         allocation.height);
88   }
89   anim_state.w = allocation.width;
90   anim_state.h = allocation.height;
91   animate_render_rect (NULL);
92 
93   return FALSE;
94 }
95 
96 static gboolean
handle_draw_cb(GtkWidget * widget,cairo_t * cr,gpointer user_data)97 handle_draw_cb (GtkWidget * widget, cairo_t * cr, gpointer user_data)
98 {
99   GstVideoRectangle *r = &anim_state.rect;
100   GtkStyleContext *style;
101   GdkRGBA color;
102   int width, height;
103 
104   width = gtk_widget_get_allocated_width (widget);
105   height = gtk_widget_get_allocated_height (widget);
106 
107   style = gtk_widget_get_style_context (widget);
108 
109   gtk_style_context_get_color (style, 0, &color);
110   gdk_cairo_set_source_rgba (cr, &color);
111 
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);
115 
116   cairo_rectangle (cr, 0, 0, width, r->y);
117   cairo_rectangle (cr, 0, r->y + r->h, width, height - (r->y + r->h));
118 
119   cairo_fill (cr);
120 
121   if (verbose) {
122     g_print ("draw(%p)\n", widget);
123   }
124   gst_video_overlay_expose (anim_state.overlay);
125   return FALSE;
126 }
127 
128 static void
window_closed(GtkWidget * widget,GdkEvent * event,gpointer user_data)129 window_closed (GtkWidget * widget, GdkEvent * event, gpointer user_data)
130 {
131   GstElement *pipeline = user_data;
132 
133   if (verbose) {
134     g_print ("stopping\n");
135   }
136   anim_state.running = FALSE;
137   gtk_widget_hide (widget);
138   gst_element_set_state (pipeline, GST_STATE_NULL);
139   gtk_main_quit ();
140 }
141 
142 gint
main(gint argc,gchar ** argv)143 main (gint argc, gchar ** argv)
144 {
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;
151 
152   gst_init (&argc, &argv);
153   gtk_init (&argc, &argv);
154 
155   if (argc) {
156     gint arg;
157     for (arg = 0; arg < argc; arg++) {
158       if (!strcmp (argv[arg], "-a"))
159         force_aspect = TRUE;
160       else if (!strcmp (argv[arg], "-b"))
161         draw_borders = TRUE;
162       else if (!strcmp (argv[arg], "-v"))
163         verbose = TRUE;
164     }
165   }
166 
167   /* prepare the pipeline */
168 
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);
174 
175   g_object_set (G_OBJECT (sink), "handle-events", FALSE,
176       "force-aspect-ratio", force_aspect, "draw-borders", draw_borders, NULL);
177 
178   /* prepare the ui */
179 
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);
184 
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);
188 
189   /* show the gui and play */
190   gtk_widget_show_all (window);
191 
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);
196 
197   video_window_xwindow = gtk_widget_get_window (video_window);
198   embed_xid = GDK_WINDOW_XID (video_window_xwindow);
199   if (verbose) {
200     g_print ("Window realize: got XID %lu\n", embed_xid);
201   }
202 
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
206    * it there) */
207   gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (sink), embed_xid);
208 
209   anim_state.overlay = GST_VIDEO_OVERLAY (sink);
210   anim_state.widget = video_window;
211   anim_state.w = 320;
212   anim_state.h = 240;
213   anim_state.a = 0.0;
214   anim_state.p = (G_PI + G_PI) / 200.0;
215 
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);
220 
221   g_timeout_add (50, (GSourceFunc) animate_render_rect, NULL);
222 
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);
227   else {
228     anim_state.running = TRUE;
229     gtk_main ();
230   }
231 
232   gst_object_unref (pipeline);
233   return 0;
234 }
235