9ef552eeea5d9076c790c80717b0365d467af4cc
[platform/upstream/gstreamer.git] / tests / icles / test-xoverlay.c
1 /* GStreamer
2  * Copyright (C) <2008> Stefan Kost <ensonic@users.sf.net>
3  *
4  * test-xoverlay: test xoverlay 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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <math.h>
29
30 #include <glib.h>
31 #include <gdk/gdkx.h>
32 #include <gtk/gtk.h>
33
34 #include <gst/gst.h>
35 #include <gst/interfaces/xoverlay.h>
36 #include <gst/video/gstvideosink.h>
37
38 static struct
39 {
40   gint w, h;
41   GstXOverlay *overlay;
42   GtkWidget *widget;
43   gdouble a, p;
44   GstVideoRectangle rect;
45   gboolean running;
46 } anim_state;
47
48 static gboolean verbose = FALSE;
49
50 static gboolean
51 animate_render_rect (gpointer user_data)
52 {
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);
57
58     anim_state.a += anim_state.p;
59     if (anim_state.a > (G_PI + G_PI))
60       anim_state.a -= (G_PI + G_PI);
61
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);
66
67     gst_x_overlay_set_render_rectangle (anim_state.overlay, r->x, r->y,
68         r->w, r->h);
69     gtk_widget_queue_draw (anim_state.widget);
70   }
71   return TRUE;
72 }
73
74 static gboolean
75 handle_resize_cb (GtkWidget * widget, GdkEventConfigure * event,
76     gpointer user_data)
77 {
78   GtkAllocation allocation;
79
80   gtk_widget_get_allocation (widget, &allocation);
81
82   if (verbose) {
83     g_print ("resize(%p): %dx%d\n", widget, allocation.width,
84         allocation.height);
85   }
86   anim_state.w = allocation.width;
87   anim_state.h = allocation.height;
88   animate_render_rect (NULL);
89
90   return FALSE;
91 }
92
93 static gboolean
94 handle_draw_cb (GtkWidget * widget, cairo_t * cr, gpointer user_data)
95 {
96   GstVideoRectangle *r = &anim_state.rect;
97   GtkStyle *style;
98   int width, height;
99
100   width = gtk_widget_get_allocated_width (widget);
101   height = gtk_widget_get_allocated_height (widget);
102
103   style = gtk_widget_get_style (widget);
104
105   gdk_cairo_set_source_color (cr, &style->bg[GTK_STATE_NORMAL]);
106
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);
110
111   cairo_rectangle (cr, 0, 0, width, r->y);
112   cairo_rectangle (cr, 0, r->y + r->h, width, height - (r->y + r->h));
113
114   cairo_fill (cr);
115
116   if (verbose) {
117     g_print ("draw(%p)\n", widget);
118   }
119   gst_x_overlay_expose (anim_state.overlay);
120   return FALSE;
121 }
122
123 static void
124 window_closed (GtkWidget * widget, GdkEvent * event, gpointer user_data)
125 {
126   GstElement *pipeline = user_data;
127
128   if (verbose) {
129     g_print ("stopping\n");
130   }
131   anim_state.running = FALSE;
132   gtk_widget_hide (widget);
133   gst_element_set_state (pipeline, GST_STATE_NULL);
134   gtk_main_quit ();
135 }
136
137 gint
138 main (gint argc, gchar ** argv)
139 {
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;
146
147   if (!g_thread_supported ())
148     g_thread_init (NULL);
149
150   gst_init (&argc, &argv);
151   gtk_init (&argc, &argv);
152
153   if (argc) {
154     gint arg;
155     for (arg = 0; arg < argc; arg++) {
156       if (!strcmp (argv[arg], "-a"))
157         force_aspect = TRUE;
158       else if (!strcmp (argv[arg], "-b"))
159         draw_borders = TRUE;
160       else if (!strcmp (argv[arg], "-v"))
161         verbose = TRUE;
162     }
163   }
164
165   /* prepare the pipeline */
166
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);
172
173   g_object_set (G_OBJECT (sink), "handle-events", FALSE,
174       "force-aspect-ratio", force_aspect, "draw-borders", draw_borders, NULL);
175
176   /* prepare the ui */
177
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);
182
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);
186
187   /* show the gui and play */
188   gtk_widget_show_all (window);
189
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);
194
195   video_window_xwindow = gtk_widget_get_window (video_window);
196   embed_xid = GDK_WINDOW_XID (video_window_xwindow);
197   if (verbose) {
198     g_print ("Window realize: got XID %lu\n", embed_xid);
199   }
200
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);
205
206   anim_state.overlay = GST_X_OVERLAY (sink);
207   anim_state.widget = video_window;
208   anim_state.w = 320;
209   anim_state.h = 240;
210   anim_state.a = 0.0;
211   anim_state.p = (G_PI + G_PI) / 200.0;
212
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);
217
218   g_timeout_add (50, (GSourceFunc) animate_render_rect, NULL);
219
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);
224   else {
225     anim_state.running = TRUE;
226     gtk_main ();
227   }
228
229   gst_object_unref (pipeline);
230   return 0;
231 }