tizen 2.0 init
[framework/multimedia/gst-plugins-base0.10.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 !GLIB_CHECK_VERSION (2, 31, 0)
148   if (!g_thread_supported ())
149     g_thread_init (NULL);
150 #endif
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 prepare-xwindow-id
205    * element message in a sync bus handler and setting it there) */
206   gst_x_overlay_set_window_handle (GST_X_OVERLAY (sink), embed_xid);
207
208   anim_state.overlay = GST_X_OVERLAY (sink);
209   anim_state.widget = video_window;
210   anim_state.w = 320;
211   anim_state.h = 240;
212   anim_state.a = 0.0;
213   anim_state.p = (G_PI + G_PI) / 200.0;
214
215   handle_resize_cb (video_window, NULL, sink);
216   g_signal_connect (video_window, "configure-event",
217       G_CALLBACK (handle_resize_cb), NULL);
218   g_signal_connect (video_window, "draw", G_CALLBACK (handle_draw_cb), NULL);
219
220   g_timeout_add (50, (GSourceFunc) animate_render_rect, NULL);
221
222   /* run the pipeline */
223   sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
224   if (sret == GST_STATE_CHANGE_FAILURE)
225     gst_element_set_state (pipeline, GST_STATE_NULL);
226   else {
227     anim_state.running = TRUE;
228     gtk_main ();
229   }
230
231   gst_object_unref (pipeline);
232   return 0;
233 }