Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / tests / icles / stress-xoverlay.c
1 /* GStreamer
2  * Copyright (C) <2005> Julien Moutte <julien@moutte.net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <gst/gst.h>
25 #include <gst/interfaces/xoverlay.h>
26
27 #include <X11/Xlib.h>
28 #include <X11/Xutil.h>
29
30 #include <math.h>
31 #include <sys/time.h>
32
33 static GMainLoop *loop;
34
35 static Display *disp;
36 static Window root, win = 0;
37 static GC gc;
38 static gint width = 320, height = 240, x = 0, y = 0;
39 static gint disp_width, disp_height;
40
41 static inline long
42 myclock (void)
43 {
44   struct timeval tv;
45
46   gettimeofday (&tv, NULL);
47   return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
48 }
49
50 static void
51 open_display (void)
52 {
53   gint screen_num;
54
55   disp = XOpenDisplay (NULL);
56   root = DefaultRootWindow (disp);
57   screen_num = DefaultScreen (disp);
58   disp_width = DisplayWidth (disp, screen_num);
59   disp_height = DisplayHeight (disp, screen_num);
60 }
61
62 static void
63 close_display (void)
64 {
65   XCloseDisplay (disp);
66 }
67
68 static gboolean
69 resize_window (GstPipeline * pipeline)
70 {
71   width = (sin (myclock () / 300.0) * 200) + 640;
72   height = (sin (myclock () / 300.0) * 200) + 480;
73
74   XResizeWindow (disp, win, width, height);
75
76   XSync (disp, FALSE);
77
78   return TRUE;
79 }
80
81 static gboolean
82 move_window (GstPipeline * pipeline)
83 {
84   x += 5;
85   y = disp_height - height + (sin (myclock () / 300.0) * height);
86   if (x > disp_width)
87     x = 0;
88
89   XMoveWindow (disp, win, x, y);
90
91   XSync (disp, FALSE);
92
93   return TRUE;
94 }
95
96 static gboolean
97 toggle_events (GstXOverlay * ov)
98 {
99   static gboolean events_toggled;
100
101   gst_x_overlay_handle_events (ov, events_toggled);
102
103   if (events_toggled) {
104     g_print ("Events are handled\n");
105     events_toggled = FALSE;
106   } else {
107     g_print ("Events are NOT handled\n");
108     events_toggled = TRUE;
109   }
110
111   return TRUE;
112 }
113
114 static gboolean
115 cycle_window (GstXOverlay * ov)
116 {
117   XGCValues values;
118   Window old_win = win;
119   GC old_gc = gc;
120
121   win = XCreateSimpleWindow (disp, root, 0, 0, width, height, 0, 0, 0);
122
123   XSetWindowBackgroundPixmap (disp, win, None);
124
125   gc = XCreateGC (disp, win, 0, &values);
126
127   XMapRaised (disp, win);
128
129   XSync (disp, FALSE);
130
131   gst_x_overlay_set_window_handle (ov, win);
132
133   if (old_win) {
134     XDestroyWindow (disp, old_win);
135     XFreeGC (disp, old_gc);
136     XSync (disp, FALSE);
137   }
138
139   return TRUE;
140 }
141
142 static GstBusSyncReply
143 create_window (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
144 {
145   const GstStructure *s;
146   GstXOverlay *ov = NULL;
147
148   s = gst_message_get_structure (message);
149   if (s == NULL || !gst_structure_has_name (s, "prepare-xwindow-id")) {
150     return GST_BUS_PASS;
151   }
152
153   ov = GST_X_OVERLAY (GST_MESSAGE_SRC (message));
154
155   g_print ("Creating our own window\n");
156
157   cycle_window (ov);
158
159   g_timeout_add (50, (GSourceFunc) resize_window, pipeline);
160   g_timeout_add (50, (GSourceFunc) move_window, pipeline);
161   g_timeout_add (100, (GSourceFunc) cycle_window, ov);
162   g_timeout_add (2000, (GSourceFunc) toggle_events, ov);
163
164   gst_message_unref (message);
165   return GST_BUS_DROP;
166 }
167
168 #if 0
169 static gboolean
170 terminate_playback (GstElement * pipeline)
171 {
172   g_print ("Terminating playback\n");
173   g_main_loop_quit (loop);
174   return FALSE;
175 }
176 #endif
177
178 static gboolean
179 pause_playback (GstElement * pipeline)
180 {
181   g_print ("Pausing playback\n");
182   gst_element_set_state (pipeline, GST_STATE_PAUSED);
183   return FALSE;
184 }
185
186 static gboolean
187 start_playback (GstElement * pipeline)
188 {
189   g_print ("Starting playback\n");
190   gst_element_set_state (pipeline, GST_STATE_PLAYING);
191   return FALSE;
192 }
193
194 int
195 main (int argc, char **argv)
196 {
197   GstElement *pipeline;
198   GstBus *bus;
199
200 #ifndef GST_DISABLE_PARSE
201   GError *error = NULL;
202 #endif
203
204   gst_init (&argc, &argv);
205
206   if (argc != 2) {
207     g_print ("Usage: %s \"pipeline description with launch format\"\n",
208         argv[0]);
209     g_print ("The pipeline should contain an element implementing XOverlay.\n");
210     g_print ("Example: %s \"videotestsrc ! ximagesink\"\n", argv[0]);
211     return -1;
212   }
213 #ifdef GST_DISABLE_PARSE
214   g_print ("GStreamer was built without pipeline parsing capabilities.\n");
215   g_print
216       ("Please rebuild GStreamer with pipeline parsing capabilities activated to use this example.\n");
217   return 1;
218 #else
219   pipeline = gst_parse_launch (argv[1], &error);
220   if (error) {
221     g_print ("Error while parsing pipeline description: %s\n", error->message);
222     return -1;
223   }
224 #endif
225
226   loop = g_main_loop_new (NULL, FALSE);
227
228   open_display ();
229
230   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
231   gst_bus_set_sync_handler (bus, (GstBusSyncHandler) create_window, pipeline);
232
233   gst_element_set_state (pipeline, GST_STATE_PLAYING);
234
235   /* We want to get out after */
236   //g_timeout_add (500000, (GSourceFunc) terminate_playback, pipeline);
237   g_timeout_add (10000, (GSourceFunc) pause_playback, pipeline);
238   g_timeout_add (20000, (GSourceFunc) start_playback, pipeline);
239
240   g_main_loop_run (loop);
241
242   close_display ();
243
244   g_main_loop_unref (loop);
245
246   return 0;
247 }