684470760eb7a8ba7389634185b78e0bcd683b7e
[platform/upstream/gst-plugins-base.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 gboolean verbose = FALSE;
48
49 static gboolean
50 animate_render_rect (gpointer user_data)
51 {
52   if (anim_state.running) {
53     GstVideoRectangle *r = &anim_state.rect;
54     gdouble s = sin (3.0 * anim_state.a);
55     gdouble c = cos (2.0 * anim_state.a);
56
57     anim_state.a += anim_state.p;
58     if (anim_state.a > (M_PI + M_PI))
59       anim_state.a -= (M_PI + M_PI);
60
61     r->w = anim_state.w / 2;
62     r->x = (r->w - (r->w / 2)) + c * (r->w / 2);
63     r->h = anim_state.h / 2;
64     r->y = (r->h - (r->h / 2)) + s * (r->h / 2);
65
66     gst_x_overlay_set_render_rectangle (anim_state.overlay, r);
67     gtk_widget_queue_draw (anim_state.widget);
68   }
69   return TRUE;
70 }
71
72 static gboolean
73 handle_resize_cb (GtkWidget * widget, GdkEventConfigure * event,
74     gpointer user_data)
75 {
76   GtkAllocation allocation;
77
78   allocation = widget->allocation;
79
80   if (verbose) {
81     g_print ("resize(%p): %dx%d\n", widget, allocation.width,
82         allocation.height);
83   }
84   anim_state.w = allocation.width;
85   anim_state.h = allocation.height;
86   animate_render_rect (NULL);
87
88   return FALSE;
89 }
90
91 static gboolean
92 handle_expose_cb (GtkWidget * widget, GdkEventExpose * event,
93     gpointer user_data)
94 {
95   GstVideoRectangle *r = &anim_state.rect;
96
97   /* we should only redraw outside of the video rect! */
98   /*
99      gdk_draw_rectangle (widget->window, widget->style->bg_gc[0], TRUE,
100      0, 0, widget->allocation.width, widget->allocation.height);
101      gdk_draw_rectangle (widget->window, widget->style->bg_gc[0], TRUE,
102      event->area.x, event->area.y, event->area.width, event->area.height);
103    */
104   gdk_draw_rectangle (widget->window, widget->style->bg_gc[0], TRUE,
105       0, event->area.y, r->x, event->area.height);
106   gdk_draw_rectangle (widget->window, widget->style->bg_gc[0], TRUE,
107       r->x + r->w, event->area.y,
108       widget->allocation.width - (r->x + r->w), event->area.height);
109
110   gdk_draw_rectangle (widget->window, widget->style->bg_gc[0], TRUE,
111       event->area.x, 0, event->area.width, r->y);
112   gdk_draw_rectangle (widget->window, widget->style->bg_gc[0], TRUE,
113       event->area.x, r->y + r->h,
114       event->area.width, widget->allocation.height - (r->y + r->h));
115   if (verbose) {
116     g_print ("expose(%p)\n", widget);
117   }
118   gst_x_overlay_expose (anim_state.overlay);
119   return FALSE;
120 }
121
122 static void
123 window_closed (GtkWidget * widget, GdkEvent * event, gpointer user_data)
124 {
125   GstElement *pipeline = user_data;
126
127   if (verbose) {
128     g_print ("stopping\n");
129   }
130   anim_state.running = FALSE;
131   gtk_widget_hide_all (widget);
132   gst_element_set_state (pipeline, GST_STATE_NULL);
133   gtk_main_quit ();
134 }
135
136 gint
137 main (gint argc, gchar ** argv)
138 {
139   GtkWidget *window, *video_window;
140   GstElement *pipeline, *src, *sink;
141   GstStateChangeReturn sret;
142   gulong embed_xid = 0;
143   gboolean force_aspect = FALSE, draw_borders = FALSE;
144
145   if (!g_thread_supported ())
146     g_thread_init (NULL);
147
148   gst_init (&argc, &argv);
149   gtk_init (&argc, &argv);
150
151   if (argc) {
152     gint arg;
153     for (arg = 0; arg < argc; arg++) {
154       if (!strcmp (argv[arg], "-a"))
155         force_aspect = TRUE;
156       else if (!strcmp (argv[arg], "-b"))
157         draw_borders = TRUE;
158       else if (!strcmp (argv[arg], "-v"))
159         verbose = TRUE;
160     }
161   }
162
163   /* prepare the pipeline */
164
165   pipeline = gst_pipeline_new ("xvoverlay");
166   src = gst_element_factory_make ("videotestsrc", NULL);
167   sink = gst_element_factory_make ("xvimagesink", NULL);
168   gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
169   gst_element_link (src, sink);
170
171   g_object_set (G_OBJECT (sink), "handle-events", FALSE,
172       "force-aspect-ratio", force_aspect, "draw-borders", draw_borders, NULL);
173
174   /* prepare the ui */
175
176   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
177   g_signal_connect (G_OBJECT (window), "delete-event",
178       G_CALLBACK (window_closed), (gpointer) pipeline);
179   gtk_window_set_default_size (GTK_WINDOW (window), 320, 240);
180
181   video_window = gtk_drawing_area_new ();
182   gtk_widget_set_double_buffered (video_window, FALSE);
183   gtk_container_add (GTK_CONTAINER (window), video_window);
184
185   /* show the gui and play */
186   gtk_widget_show_all (window);
187
188   /* realize window now so that the video window gets created and we can
189    * obtain its XID before the pipeline is started up and the videosink
190    * asks for the XID of the window to render onto */
191   gtk_widget_realize (window);
192
193   embed_xid = GDK_WINDOW_XID (video_window->window);
194   if (verbose) {
195     g_print ("Window realize: got XID %lu\n", embed_xid);
196   }
197
198   /* we know what the video sink is in this case (xvimagesink), so we can
199    * just set it directly here now (instead of waiting for a prepare-xwindow-id
200    * element message in a sync bus handler and setting it there) */
201   gst_x_overlay_set_xwindow_id (GST_X_OVERLAY (sink), embed_xid);
202
203   anim_state.overlay = GST_X_OVERLAY (sink);
204   anim_state.widget = video_window;
205   anim_state.w = 320;
206   anim_state.h = 240;
207   anim_state.a = 0.0;
208   anim_state.p = (M_PI + M_PI) / 200.0;
209
210   handle_resize_cb (video_window, NULL, sink);
211   g_signal_connect (video_window, "configure-event",
212       G_CALLBACK (handle_resize_cb), NULL);
213   g_signal_connect (video_window, "expose-event",
214       G_CALLBACK (handle_expose_cb), NULL);
215
216   g_timeout_add (50, (GSourceFunc) animate_render_rect, NULL);
217
218   /* run the pipeline */
219   sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
220   if (sret == GST_STATE_CHANGE_FAILURE)
221     gst_element_set_state (pipeline, GST_STATE_NULL);
222   else {
223     anim_state.running = TRUE;
224     gtk_main ();
225   }
226
227   gst_object_unref (pipeline);
228   return 0;
229 }