1 /*
2 * GStreamer
3 * Copyright (C) 2008-2009 Julien Isorce <julien.isorce@gmail.com>
4 * Copyright (C) 2014-2015 Jan Schmidt <jan@centricular.com>
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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <string.h>
26
27 #include <gdk/gdk.h>
28 #if defined (GDK_WINDOWING_X11)
29 #include <X11/Xlib.h>
30 #endif
31
32 #include <gst/gst.h>
33 #include <gtk/gtk.h>
34 #include <gst/video/video-info.h>
35
36 #include "../gstgtk.h"
37 #include "mviewwidget.h"
38
39 /* Until playbin properties support dynamic changes,
40 * use our own glviewconvert */
41 #define USE_GLCONVERT_FOR_INPUT 1
42
43 typedef struct _localstate
44 {
45 GstVideoMultiviewFramePacking in_mode;
46 GstVideoMultiviewFlags out_mode;
47 GstVideoMultiviewFlags in_flags, out_flags;
48 } LocalState;
49
50 static GstBusSyncReply
create_window(GstBus * bus,GstMessage * message,GtkWidget * widget)51 create_window (GstBus * bus, GstMessage * message, GtkWidget * widget)
52 {
53 if (gst_gtk_handle_need_context (bus, message, NULL))
54 return GST_BUS_DROP;
55
56 /* ignore anything but 'prepare-window-handle' element messages */
57 if (GST_MESSAGE_TYPE (message) != GST_MESSAGE_ELEMENT)
58 return GST_BUS_PASS;
59
60 if (!gst_is_video_overlay_prepare_window_handle_message (message))
61 return GST_BUS_PASS;
62
63 /* do not call gdk_window_ensure_native for the first time here because
64 * we are in a different thread than the main thread */
65 gst_video_overlay_set_gtk_window (GST_VIDEO_OVERLAY (GST_MESSAGE_SRC
66 (message)), widget);
67
68 gst_message_unref (message);
69
70 return GST_BUS_DROP;
71 }
72
73 static void
end_stream_cb(GstBus * bus,GstMessage * message,GstElement * pipeline)74 end_stream_cb (GstBus * bus, GstMessage * message, GstElement * pipeline)
75 {
76 switch (GST_MESSAGE_TYPE (message)) {
77 case GST_MESSAGE_EOS:
78 g_print ("End of stream\n");
79
80 gst_element_set_state (pipeline, GST_STATE_NULL);
81 gst_object_unref (pipeline);
82 gtk_main_quit ();
83 break;
84 case GST_MESSAGE_ERROR:
85 {
86 gchar *debug = NULL;
87 GError *err = NULL;
88
89 gst_message_parse_error (message, &err, &debug);
90
91 g_print ("Error: %s\n", err->message);
92 g_error_free (err);
93
94 if (debug) {
95 g_print ("Debug details: %s\n", debug);
96 g_free (debug);
97 }
98
99 gst_element_set_state (pipeline, GST_STATE_NULL);
100 gst_object_unref (pipeline);
101 gtk_main_quit ();
102 break;
103 }
104 default:
105 break;
106 }
107 }
108
109 static gboolean
draw_cb(GtkWidget * widget,cairo_t * cr,GstElement * videosink)110 draw_cb (GtkWidget * widget, cairo_t * cr, GstElement * videosink)
111 {
112 gst_video_overlay_expose (GST_VIDEO_OVERLAY (videosink));
113 return FALSE;
114 }
115
116 static gboolean
resize_cb(GtkWidget * widget,GdkEvent * event,gpointer sink)117 resize_cb (GtkWidget * widget, GdkEvent * event, gpointer sink)
118 {
119 GtkAllocation allocation;
120 gint scale = 1;
121
122 #if GTK_CHECK_VERSION(3, 10, 0)
123 scale = gtk_widget_get_scale_factor (widget);
124 #endif
125
126 gtk_widget_get_allocation (widget, &allocation);
127 gst_video_overlay_set_render_rectangle (GST_VIDEO_OVERLAY (sink),
128 allocation.x * scale, allocation.y * scale, allocation.width * scale,
129 allocation.height * scale);
130
131 return G_SOURCE_CONTINUE;
132 }
133
134 static void
destroy_cb(GtkWidget * widget,GdkEvent * event,GstElement * pipeline)135 destroy_cb (GtkWidget * widget, GdkEvent * event, GstElement * pipeline)
136 {
137 gst_element_set_state (pipeline, GST_STATE_NULL);
138 gst_object_unref (pipeline);
139
140 gtk_main_quit ();
141 }
142
143 static void
button_state_ready_cb(GtkWidget * widget,GstElement * pipeline)144 button_state_ready_cb (GtkWidget * widget, GstElement * pipeline)
145 {
146 gst_element_set_state (pipeline, GST_STATE_READY);
147 }
148
149 static void
button_state_paused_cb(GtkWidget * widget,GstElement * pipeline)150 button_state_paused_cb (GtkWidget * widget, GstElement * pipeline)
151 {
152 gst_element_set_state (pipeline, GST_STATE_PAUSED);
153 }
154
155 static void
button_state_playing_cb(GtkWidget * widget,GstElement * pipeline)156 button_state_playing_cb (GtkWidget * widget, GstElement * pipeline)
157 {
158 gst_element_set_state (pipeline, GST_STATE_PLAYING);
159 }
160
161 static gboolean
set_mview_mode(GtkWidget * combo,GObject * target,const gchar * prop_name)162 set_mview_mode (GtkWidget * combo, GObject * target, const gchar * prop_name)
163 {
164 gchar *mview_mode = NULL;
165 GEnumClass *p_class;
166 GEnumValue *v;
167 GParamSpec *p =
168 g_object_class_find_property (G_OBJECT_GET_CLASS (target), prop_name);
169
170 g_return_val_if_fail (p != NULL, FALSE);
171
172 p_class = G_PARAM_SPEC_ENUM (p)->enum_class;
173 g_return_val_if_fail (p_class != NULL, FALSE);
174
175 g_object_get (G_OBJECT (combo), "active-id", &mview_mode, NULL);
176 g_return_val_if_fail (mview_mode != NULL, FALSE);
177
178 v = g_enum_get_value_by_nick (p_class, mview_mode);
179 g_return_val_if_fail (v != NULL, FALSE);
180
181 g_object_set (target, prop_name, v->value, NULL);
182
183 return FALSE;
184 }
185
186 static gboolean
set_mview_input_mode(GtkWidget * widget,gpointer data)187 set_mview_input_mode (GtkWidget * widget, gpointer data)
188 {
189 #if USE_GLCONVERT_FOR_INPUT
190 return set_mview_mode (widget, G_OBJECT (data), "input-mode-override");
191 #else
192 return set_mview_mode (widget, G_OBJECT (data), "video-multiview-mode");
193 #endif
194 }
195
196 static gboolean
set_mview_output_mode(GtkWidget * widget,gpointer data)197 set_mview_output_mode (GtkWidget * widget, gpointer data)
198 {
199 GstElement *sink = gst_bin_get_by_name (GST_BIN (data), "sink");
200 set_mview_mode (widget, G_OBJECT (sink), "output-multiview-mode");
201 gst_object_unref (GST_OBJECT (sink));
202 return FALSE;
203 }
204
205 static void
input_flags_changed(GObject * gobject,GParamSpec * pspec,gpointer user_data)206 input_flags_changed (GObject * gobject, GParamSpec * pspec, gpointer user_data)
207 {
208 GObject *target = G_OBJECT (user_data);
209 GstVideoMultiviewFlags flags;
210
211 g_object_get (gobject, "flags", &flags, NULL);
212 #if USE_GLCONVERT_FOR_INPUT
213 g_object_set (target, "input-flags-override", flags, NULL);
214 #else
215 g_object_set (target, "video-multiview-flags", flags, NULL);
216 #endif
217 }
218
219 static void
output_flags_changed(GObject * gobject,GParamSpec * pspec,gpointer user_data)220 output_flags_changed (GObject * gobject, GParamSpec * pspec, gpointer user_data)
221 {
222 GObject *target = G_OBJECT (user_data);
223 GstVideoMultiviewFlags flags;
224 GstElement *sink = gst_bin_get_by_name (GST_BIN (target), "sink");
225
226 g_object_get (gobject, "flags", &flags, NULL);
227 g_object_set (G_OBJECT (sink), "output-multiview-flags", flags, NULL);
228
229 gst_object_unref (GST_OBJECT (sink));
230 }
231
232 static void
downmix_method_changed(GObject * gobject,GParamSpec * pspec,gpointer user_data)233 downmix_method_changed (GObject * gobject, GParamSpec * pspec, gpointer user_data)
234 {
235 GObject *target = G_OBJECT (user_data);
236 GstGLStereoDownmix downmix_method;
237 GstElement *sink = gst_bin_get_by_name (GST_BIN (target), "sink");
238
239 g_object_get (gobject, "downmix-mode", &downmix_method, NULL);
240 g_object_set (sink, "output-multiview-downmix-mode", downmix_method, NULL);
241 gst_object_unref (GST_OBJECT (sink));
242 }
243
244 static const gchar *
enum_value_to_nick(GType enum_type,guint value)245 enum_value_to_nick (GType enum_type, guint value)
246 {
247 GEnumClass *enum_info;
248 GEnumValue *v;
249 const gchar *nick;
250
251 enum_info = (GEnumClass *) (g_type_class_ref (enum_type));
252 g_return_val_if_fail (enum_info != NULL, NULL);
253
254 v = g_enum_get_value (enum_info, value);
255 g_return_val_if_fail (v != NULL, NULL);
256
257 nick = v->value_nick;
258
259 g_type_class_unref (enum_info);
260
261 return nick;
262 }
263
264 static void
detect_mode_from_uri(LocalState * state,const gchar * uri)265 detect_mode_from_uri (LocalState * state, const gchar * uri)
266 {
267 if (strstr (uri, "HSBS")) {
268 state->in_mode = GST_VIDEO_MULTIVIEW_FRAME_PACKING_SIDE_BY_SIDE;
269 state->in_flags = GST_VIDEO_MULTIVIEW_FLAGS_HALF_ASPECT;
270 } else if (strstr (uri, "SBS")) {
271 state->in_mode = GST_VIDEO_MULTIVIEW_FRAME_PACKING_SIDE_BY_SIDE;
272 if (g_regex_match_simple ("half", uri, G_REGEX_CASELESS,
273 (GRegexMatchFlags) 0)) {
274 state->in_flags = GST_VIDEO_MULTIVIEW_FLAGS_HALF_ASPECT;
275 }
276 }
277 }
278
279 gint
main(gint argc,gchar * argv[])280 main (gint argc, gchar * argv[])
281 {
282 LocalState state;
283 GtkWidget *area, *combo, *w;
284 const gchar *uri;
285
286 #if defined (GDK_WINDOWING_X11)
287 XInitThreads ();
288 #endif
289
290 gst_init (&argc, &argv);
291 gtk_init (&argc, &argv);
292
293 if (argc < 2) {
294 g_print ("Usage: 3dvideo <uri-to-play>\n");
295 return 1;
296 }
297
298 uri = argv[1];
299
300 GstElement *pipeline = gst_element_factory_make ("playbin", NULL);
301 GstBin *sinkbin = (GstBin *) gst_parse_bin_from_description ("glupload ! glcolorconvert ! glviewconvert name=viewconvert ! glimagesink name=sink", TRUE, NULL);
302 #if USE_GLCONVERT_FOR_INPUT
303 GstElement *glconvert = gst_bin_get_by_name (sinkbin, "viewconvert");
304 #endif
305 GstElement *videosink = gst_bin_get_by_name (sinkbin, "sink");
306
307 /* Get defaults */
308 g_object_get (pipeline, "video-multiview-mode", &state.in_mode,
309 "video-multiview-flags", &state.in_flags, NULL);
310 gst_child_proxy_get (GST_CHILD_PROXY (videosink), "sink::output-multiview-mode", &state.out_mode,
311 "sink::output-multiview-flags", &state.out_flags, NULL);
312
313 detect_mode_from_uri (&state, uri);
314
315 g_return_val_if_fail (pipeline != NULL, 1);
316 g_return_val_if_fail (videosink != NULL, 1);
317
318 g_object_set (G_OBJECT (pipeline), "video-sink", sinkbin, NULL);
319 g_object_set (G_OBJECT (pipeline), "uri", uri, NULL);
320
321 #if USE_GLCONVERT_FOR_INPUT
322 g_object_set (G_OBJECT (glconvert), "input-mode-override", state.in_mode,
323 NULL);
324 g_object_set (G_OBJECT (glconvert), "input-flags-override", state.in_flags,
325 NULL);
326 #else
327 g_object_set (G_OBJECT (pipeline), "video-multiview-mode", state.in_mode,
328 NULL);
329 g_object_set (G_OBJECT (pipeline), "video-multiview-flags", state.in_flags,
330 NULL);
331 #endif
332
333 /* Connect to bus for signal handling */
334 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
335 gst_bus_add_signal_watch (bus);
336 g_signal_connect (bus, "message::error", G_CALLBACK (end_stream_cb),
337 pipeline);
338 g_signal_connect (bus, "message::warning", G_CALLBACK (end_stream_cb),
339 pipeline);
340 g_signal_connect (bus, "message::eos", G_CALLBACK (end_stream_cb), pipeline);
341
342 gst_element_set_state (pipeline, GST_STATE_READY);
343
344 area = gtk_drawing_area_new ();
345 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) create_window, area, NULL);
346 gst_object_unref (bus);
347
348 /* Toplevel window */
349 GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
350 gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
351 gtk_window_set_title (GTK_WINDOW (window), "Stereoscopic video demo");
352 GdkGeometry geometry;
353 geometry.min_width = 1;
354 geometry.min_height = 1;
355 geometry.max_width = -1;
356 geometry.max_height = -1;
357 gtk_window_set_geometry_hints (GTK_WINDOW (window), window, &geometry,
358 GDK_HINT_MIN_SIZE);
359
360 GtkWidget *vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
361 gtk_container_add (GTK_CONTAINER (window), vbox);
362
363 /* area where the video is drawn */
364 gtk_box_pack_start (GTK_BOX (vbox), area, TRUE, TRUE, 0);
365
366 /* Buttons to control the pipeline state */
367 GtkWidget *table = gtk_grid_new ();
368 gtk_container_add (GTK_CONTAINER (vbox), table);
369
370 GtkWidget *button_state_ready = gtk_button_new_with_label ("Stop");
371 g_signal_connect (G_OBJECT (button_state_ready), "clicked",
372 G_CALLBACK (button_state_ready_cb), pipeline);
373 gtk_grid_attach (GTK_GRID (table), button_state_ready, 1, 0, 1, 1);
374 gtk_widget_show (button_state_ready);
375
376 //control state paused
377 GtkWidget *button_state_paused = gtk_button_new_with_label ("Pause");
378 g_signal_connect (G_OBJECT (button_state_paused), "clicked",
379 G_CALLBACK (button_state_paused_cb), pipeline);
380 gtk_grid_attach (GTK_GRID (table), button_state_paused, 2, 0, 1, 1);
381 gtk_widget_show (button_state_paused);
382
383 //control state playing
384 GtkWidget *button_state_playing = gtk_button_new_with_label ("Play");
385 g_signal_connect (G_OBJECT (button_state_playing), "clicked",
386 G_CALLBACK (button_state_playing_cb), pipeline);
387 gtk_grid_attach (GTK_GRID (table), button_state_playing, 3, 0, 1, 1);
388 //gtk_widget_show (button_state_playing);
389
390 w = gst_mview_widget_new (FALSE);
391 combo = GST_MVIEW_WIDGET (w)->mode_selector;
392 gtk_combo_box_set_active_id (GTK_COMBO_BOX (combo),
393 enum_value_to_nick (GST_TYPE_VIDEO_MULTIVIEW_FRAME_PACKING,
394 state.in_mode));
395 #if USE_GLCONVERT_FOR_INPUT
396 g_signal_connect (G_OBJECT (combo), "changed",
397 G_CALLBACK (set_mview_input_mode), glconvert);
398 #else
399 g_signal_connect (G_OBJECT (combo), "changed",
400 G_CALLBACK (set_mview_input_mode), pipeline);
401 #endif
402
403 g_object_set (G_OBJECT (w), "flags", state.in_flags, NULL);
404 #if USE_GLCONVERT_FOR_INPUT
405 g_signal_connect (G_OBJECT (w), "notify::flags",
406 G_CALLBACK (input_flags_changed), glconvert);
407 #else
408 g_signal_connect (G_OBJECT (w), "notify::flags",
409 G_CALLBACK (input_flags_changed), pipeline);
410 #endif
411 gtk_container_add (GTK_CONTAINER (vbox), w);
412
413 w = gst_mview_widget_new (TRUE);
414 combo = GST_MVIEW_WIDGET (w)->mode_selector;
415 gtk_combo_box_set_active_id (GTK_COMBO_BOX (combo),
416 enum_value_to_nick (GST_TYPE_VIDEO_MULTIVIEW_MODE, state.out_mode));
417 g_signal_connect (G_OBJECT (combo), "changed",
418 G_CALLBACK (set_mview_output_mode), videosink);
419
420 g_object_set (G_OBJECT (w), "flags", state.out_flags, NULL);
421 g_signal_connect (G_OBJECT (w), "notify::flags",
422 G_CALLBACK (output_flags_changed), videosink);
423 g_signal_connect (G_OBJECT (w), "notify::downmix-mode",
424 G_CALLBACK (downmix_method_changed), videosink);
425 gtk_container_add (GTK_CONTAINER (vbox), w);
426
427 //configure the pipeline
428 g_signal_connect (G_OBJECT (window), "delete-event", G_CALLBACK (destroy_cb),
429 pipeline);
430
431 gtk_widget_realize (area);
432
433 /* Redraw needed when paused or stopped (PAUSED or READY) */
434 g_signal_connect (area, "draw", G_CALLBACK (draw_cb), videosink);
435 g_signal_connect(area, "configure-event", G_CALLBACK(resize_cb), videosink);
436
437 gtk_widget_show_all (window);
438
439 gst_element_set_state (pipeline, GST_STATE_PLAYING);
440
441 gtk_main ();
442
443 return 0;
444 }
445