1 /*
2 * GStreamer
3 * Copyright (C) 2008-2009 Julien Isorce <julien.isorce@gmail.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <gst/gst.h>
26 #include <gtk/gtk.h>
27 #include <gdk/gdk.h>
28
29 #include "../gstgtk.h"
30
31 #ifdef HAVE_X11
32 #include <X11/Xlib.h>
33 #endif
34
create_window(GstBus * bus,GstMessage * message,GtkWidget * widget)35 static GstBusSyncReply create_window (GstBus* bus, GstMessage* message, GtkWidget* widget)
36 {
37 GtkAllocation allocation;
38
39 if (gst_gtk_handle_need_context (bus, message, NULL))
40 return GST_BUS_DROP;
41
42 // ignore anything but 'prepare-window-handle' element messages
43 if (GST_MESSAGE_TYPE (message) != GST_MESSAGE_ELEMENT)
44 return GST_BUS_PASS;
45
46 if (!gst_is_video_overlay_prepare_window_handle_message (message))
47 return GST_BUS_PASS;
48
49 g_print ("setting window handle %p\n", widget);
50
51 gst_video_overlay_set_gtk_window (GST_VIDEO_OVERLAY (GST_MESSAGE_SRC (message)), widget);
52
53 gtk_widget_get_allocation (widget, &allocation);
54 gst_video_overlay_set_render_rectangle (GST_VIDEO_OVERLAY (GST_MESSAGE_SRC (message)), allocation.x, allocation.y, allocation.width, allocation.height);
55
56 gst_message_unref (message);
57
58 return GST_BUS_DROP;
59 }
60
61 static gboolean
resize_cb(GtkWidget * widget,GdkEvent * event,gpointer sink)62 resize_cb (GtkWidget * widget, GdkEvent * event, gpointer sink)
63 {
64 GtkAllocation allocation;
65
66 gtk_widget_get_allocation (widget, &allocation);
67 gst_video_overlay_set_render_rectangle (GST_VIDEO_OVERLAY (sink), allocation.x, allocation.y, allocation.width, allocation.height);
68
69 return FALSE;
70 }
71
end_stream_cb(GstBus * bus,GstMessage * message,GstElement * pipeline)72 static void end_stream_cb(GstBus* bus, GstMessage* message, GstElement* pipeline)
73 {
74 GError *error = NULL;
75 gchar *details;
76
77 switch (GST_MESSAGE_TYPE (message)) {
78 case GST_MESSAGE_ERROR:
79 gst_message_parse_error (message, &error, &details);
80
81 g_print("Error %s\n", error->message);
82 g_print("Details %s\n", details);
83 /* fallthrough */
84 case GST_MESSAGE_EOS:
85 g_print("End of stream\n");
86
87 gst_element_set_state (pipeline, GST_STATE_NULL);
88 gst_object_unref(pipeline);
89
90 gtk_main_quit();
91 break;
92 case GST_MESSAGE_WARNING:
93 gst_message_parse_warning (message, &error, &details);
94
95 g_print("Warning %s\n", error->message);
96 g_print("Details %s\n", details);
97 break;
98 default:
99 break;
100 }
101 }
102
expose_cb(GtkWidget * widget,cairo_t * cr,GstElement * videosink)103 static gboolean expose_cb(GtkWidget* widget, cairo_t *cr, GstElement* videosink)
104 {
105 g_print ("expose %p\n", widget);
106 g_print ("event mask: 0x%x, button_press 0x%x\n", gtk_widget_get_events (widget), GDK_BUTTON_PRESS_MASK);
107 gst_video_overlay_expose (GST_VIDEO_OVERLAY (videosink));
108 return FALSE;
109 }
110
on_click_drawing_area(GtkWidget * widget,GdkEventButton * event,GstElement * videosink)111 static gboolean on_click_drawing_area(GtkWidget* widget, GdkEventButton* event, GstElement* videosink)
112 {
113 GtkAllocation allocation;
114 GtkWidget *parent = gtk_widget_get_parent (widget);
115
116 g_print ("switch the drawing area %p\n", widget);
117 gst_video_overlay_set_gtk_window (GST_VIDEO_OVERLAY (videosink), widget);
118
119 gtk_widget_get_allocation (widget, &allocation);
120 gst_video_overlay_set_render_rectangle (GST_VIDEO_OVERLAY (videosink), allocation.x, allocation.y, allocation.width, allocation.height);
121
122 /* XXX: required on wayland as changing the window handle (subsurface)
123 * requires a wl_surface::commit from the parent */
124 if (parent)
125 gtk_widget_queue_draw (parent);
126
127 return FALSE;
128 }
129
130
destroy_cb(GtkWidget * widget,GdkEvent * event,GstElement * pipeline)131 static void destroy_cb(GtkWidget* widget, GdkEvent* event, GstElement* pipeline)
132 {
133 g_print("Close\n");
134
135 gst_element_set_state (pipeline, GST_STATE_NULL);
136 gst_object_unref(pipeline);
137
138 gtk_main_quit();
139 }
140
141
button_state_null_cb(GtkWidget * widget,GstElement * pipeline)142 static void button_state_null_cb(GtkWidget* widget, GstElement* pipeline)
143 {
144 gst_element_set_state (pipeline, GST_STATE_NULL);
145 g_print ("GST_STATE_NULL\n");
146 }
147
148
button_state_ready_cb(GtkWidget * widget,GstElement * pipeline)149 static void button_state_ready_cb(GtkWidget* widget, GstElement* pipeline)
150 {
151 gst_element_set_state (pipeline, GST_STATE_READY);
152 g_print ("GST_STATE_READY\n");
153 }
154
155
button_state_paused_cb(GtkWidget * widget,GstElement * pipeline)156 static void button_state_paused_cb(GtkWidget* widget, GstElement* pipeline)
157 {
158 gst_element_set_state (pipeline, GST_STATE_PAUSED);
159 g_print ("GST_STATE_PAUSED\n");
160 }
161
162
button_state_playing_cb(GtkWidget * widget,GstElement * pipeline)163 static void button_state_playing_cb(GtkWidget* widget, GstElement* pipeline)
164 {
165 gst_element_set_state (pipeline, GST_STATE_PLAYING);
166 g_print ("GST_STATE_PLAYING\n");
167 }
168
main(gint argc,gchar * argv[])169 gint main (gint argc, gchar *argv[])
170 {
171 #ifdef HAVE_X11
172 XInitThreads();
173 #endif
174
175 gtk_init (&argc, &argv);
176 gst_init (&argc, &argv);
177
178 GstElement* pipeline = gst_pipeline_new ("pipeline");
179
180 //window that contains several ares where the video is drawn
181 GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
182 gtk_widget_set_size_request (window, 640, 240);
183 gtk_window_move (GTK_WINDOW (window), 300, 10);
184 gtk_window_set_title (GTK_WINDOW (window), "click on left, right or outside the main window to switch the drawing area");
185 GdkGeometry geometry;
186 geometry.min_width = 1;
187 geometry.min_height = 1;
188 geometry.max_width = -1;
189 geometry.max_height = -1;
190 gtk_window_set_geometry_hints (GTK_WINDOW (window), window, &geometry, GDK_HINT_MIN_SIZE);
191
192 //window to control the states
193 GtkWidget* window_control = gtk_window_new (GTK_WINDOW_TOPLEVEL);
194 geometry.min_width = 1;
195 geometry.min_height = 1;
196 geometry.max_width = -1;
197 geometry.max_height = -1;
198 gtk_window_set_geometry_hints (GTK_WINDOW (window_control), window_control, &geometry, GDK_HINT_MIN_SIZE);
199 gtk_window_set_resizable (GTK_WINDOW (window_control), FALSE);
200 gtk_window_move (GTK_WINDOW (window_control), 10, 10);
201 GtkWidget* table = gtk_grid_new ();
202 gtk_container_add (GTK_CONTAINER (window_control), table);
203
204 //control state null
205 GtkWidget* button_state_null = gtk_button_new_with_label ("GST_STATE_NULL");
206 g_signal_connect (G_OBJECT (button_state_null), "clicked",
207 G_CALLBACK (button_state_null_cb), pipeline);
208 gtk_grid_attach (GTK_GRID (table), button_state_null, 0, 0, 1, 1);
209 gtk_widget_show (button_state_null);
210
211 //control state ready
212 GtkWidget* button_state_ready = gtk_button_new_with_label ("GST_STATE_READY");
213 g_signal_connect (G_OBJECT (button_state_ready), "clicked",
214 G_CALLBACK (button_state_ready_cb), pipeline);
215 gtk_grid_attach (GTK_GRID (table), button_state_ready, 0, 1, 1, 1);
216 gtk_widget_show (button_state_ready);
217
218 //control state paused
219 GtkWidget* button_state_paused = gtk_button_new_with_label ("GST_STATE_PAUSED");
220 g_signal_connect (G_OBJECT (button_state_paused), "clicked",
221 G_CALLBACK (button_state_paused_cb), pipeline);
222 gtk_grid_attach (GTK_GRID (table), button_state_paused, 0, 2, 1, 1);
223 gtk_widget_show (button_state_paused);
224
225 //control state playing
226 GtkWidget* button_state_playing = gtk_button_new_with_label ("GST_STATE_PLAYING");
227 g_signal_connect (G_OBJECT (button_state_playing), "clicked",
228 G_CALLBACK (button_state_playing_cb), pipeline);
229 gtk_grid_attach (GTK_GRID (table), button_state_playing, 0, 3, 1, 1);
230 gtk_widget_show (button_state_playing);
231
232 gtk_widget_show (table);
233 gtk_widget_show (window_control);
234
235 //configure the pipeline
236 g_signal_connect(G_OBJECT(window), "delete-event", G_CALLBACK(destroy_cb), pipeline);
237
238 GstElement* videosrc = gst_element_factory_make ("videotestsrc", "videotestsrc");
239 GstElement* videosink = gst_element_factory_make ("glimagesink", "glimagesink");
240
241 gst_bin_add_many (GST_BIN (pipeline), videosrc, videosink, NULL);
242
243 gboolean link_ok = gst_element_link_many(videosrc, videosink, NULL);
244 if(!link_ok)
245 {
246 g_warning("Failed to link videosrc to videosink!\n") ;
247 return -1;
248 }
249
250 //areas where the video is drawn
251 GtkWidget* table_areas = gtk_grid_new ();
252 gtk_container_add (GTK_CONTAINER (window), table_areas);
253 GtkWidget* area_top_left = gtk_drawing_area_new();
254 gtk_widget_add_events(area_top_left, GDK_BUTTON_PRESS_MASK);
255 gtk_widget_set_size_request (area_top_left, 320, 240);
256 gtk_grid_attach (GTK_GRID (table_areas), area_top_left, 0, 0, 1, 1);
257 GtkWidget* area_top_right = gtk_drawing_area_new();
258 gtk_widget_add_events(area_top_right, GDK_BUTTON_PRESS_MASK);
259 gtk_widget_set_size_request (area_top_right, 320, 240);
260 gtk_grid_attach (GTK_GRID (table_areas), area_top_right, 1, 0, 1, 1);
261
262 gtk_widget_set_redraw_on_allocate (area_top_left, TRUE);
263 gtk_widget_set_redraw_on_allocate (area_top_right, TRUE);
264 gtk_widget_realize(area_top_left);
265 gtk_widget_realize(area_top_right);
266
267 GstBus* bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
268 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) create_window, area_top_right, NULL);
269 gst_bus_add_signal_watch (bus);
270 g_signal_connect(bus, "message::error", G_CALLBACK(end_stream_cb), pipeline);
271 g_signal_connect(bus, "message::warning", G_CALLBACK(end_stream_cb), pipeline);
272 g_signal_connect(bus, "message::eos", G_CALLBACK(end_stream_cb), pipeline);
273 gst_object_unref (bus);
274
275 //needed when being in GST_STATE_READY, GST_STATE_PAUSED
276 //or resizing/obscuring the window
277 g_signal_connect(area_top_left, "draw", G_CALLBACK(expose_cb), videosink);
278 g_signal_connect(area_top_left, "configure-event", G_CALLBACK(resize_cb), videosink);
279 g_signal_connect(area_top_right, "draw", G_CALLBACK(expose_cb), videosink);
280 g_signal_connect(area_top_right, "configure-event", G_CALLBACK(resize_cb), videosink);
281
282 //switch the drawing area
283 g_signal_connect(area_top_left, "button-press-event", G_CALLBACK(on_click_drawing_area), videosink);
284 g_signal_connect(area_top_right, "button-press-event", G_CALLBACK(on_click_drawing_area), videosink);
285
286 gtk_widget_show_all (window);
287
288 gst_element_set_state(pipeline, GST_STATE_PLAYING);
289
290 gtk_main();
291
292 return 0;
293 }
294
295