• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include <gst/gst.h>
25 #include <gst/video/videooverlay.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
myclock(void)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
open_display(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
close_display(void)63 close_display (void)
64 {
65   XCloseDisplay (disp);
66 }
67 
68 static gboolean
resize_window(GstPipeline * pipeline)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
move_window(GstPipeline * pipeline)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
toggle_events(GstVideoOverlay * ov)97 toggle_events (GstVideoOverlay * ov)
98 {
99   static gboolean events_toggled;
100 
101   gst_video_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
cycle_window(GstVideoOverlay * ov)115 cycle_window (GstVideoOverlay * 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_video_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
create_window(GstBus * bus,GstMessage * message,GstPipeline * pipeline)143 create_window (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
144 {
145   GstVideoOverlay *ov = NULL;
146 
147   if (!gst_is_video_overlay_prepare_window_handle_message (message))
148     return GST_BUS_PASS;
149 
150   ov = GST_VIDEO_OVERLAY (GST_MESSAGE_SRC (message));
151 
152   g_print ("Creating our own window\n");
153 
154   cycle_window (ov);
155 
156   g_timeout_add (50, (GSourceFunc) resize_window, pipeline);
157   g_timeout_add (50, (GSourceFunc) move_window, pipeline);
158   g_timeout_add (100, (GSourceFunc) cycle_window, ov);
159   g_timeout_add_seconds (2, (GSourceFunc) toggle_events, ov);
160 
161   gst_message_unref (message);
162   return GST_BUS_DROP;
163 }
164 
165 #if 0
166 static gboolean
167 terminate_playback (GstElement * pipeline)
168 {
169   g_print ("Terminating playback\n");
170   g_main_loop_quit (loop);
171   return FALSE;
172 }
173 #endif
174 
175 static gboolean
pause_playback(GstElement * pipeline)176 pause_playback (GstElement * pipeline)
177 {
178   g_print ("Pausing playback\n");
179   gst_element_set_state (pipeline, GST_STATE_PAUSED);
180   return FALSE;
181 }
182 
183 static gboolean
start_playback(GstElement * pipeline)184 start_playback (GstElement * pipeline)
185 {
186   g_print ("Starting playback\n");
187   gst_element_set_state (pipeline, GST_STATE_PLAYING);
188   return FALSE;
189 }
190 
191 int
main(int argc,char ** argv)192 main (int argc, char **argv)
193 {
194   GstElement *pipeline;
195   GstBus *bus;
196 
197 #ifndef GST_DISABLE_PARSE
198   GError *error = NULL;
199 #endif
200 
201   gst_init (&argc, &argv);
202 
203   if (argc != 2) {
204     g_print ("Usage: %s \"pipeline description with launch format\"\n",
205         argv[0]);
206     g_print
207         ("The pipeline should contain an element implementing GstVideoOverlay.\n");
208     g_print ("Example: %s \"videotestsrc ! ximagesink\"\n", argv[0]);
209     return -1;
210   }
211 #ifdef GST_DISABLE_PARSE
212   g_print ("GStreamer was built without pipeline parsing capabilities.\n");
213   g_print
214       ("Please rebuild GStreamer with pipeline parsing capabilities activated to use this example.\n");
215   return 1;
216 #else
217   pipeline = gst_parse_launch (argv[1], &error);
218   if (error) {
219     g_print ("Error while parsing pipeline description: %s\n", error->message);
220     return -1;
221   }
222 #endif
223 
224   loop = g_main_loop_new (NULL, FALSE);
225 
226   open_display ();
227 
228   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
229   gst_bus_set_sync_handler (bus, (GstBusSyncHandler) create_window, pipeline,
230       NULL);
231 
232   gst_element_set_state (pipeline, GST_STATE_PLAYING);
233 
234   /* We want to get out after */
235   //g_timeout_add (500000, (GSourceFunc) terminate_playback, pipeline);
236   g_timeout_add_seconds (10, (GSourceFunc) pause_playback, pipeline);
237   g_timeout_add_seconds (20, (GSourceFunc) start_playback, pipeline);
238 
239   g_main_loop_run (loop);
240 
241   close_display ();
242 
243   g_main_loop_unref (loop);
244 
245   gst_object_unref (bus);
246 
247   return 0;
248 }
249