seek: Add support for sending navigation commands
[platform/upstream/gstreamer.git] / tests / examples / seek / seek.c
1 /* GStreamer
2  *
3  * seek.c: seeking sample application
4  *
5  * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
6  *               2006 Stefan Kost <ensonic@users.sf.net>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
28  * with newer GTK versions (>= 3.3.0) */
29 #define GDK_DISABLE_DEPRECATION_WARNINGS
30
31 #include <stdlib.h>
32 #include <math.h>
33 #include <glib.h>
34 #include <gtk/gtk.h>
35 #include <gst/gst.h>
36 #include <string.h>
37
38 #include <gdk/gdk.h>
39 #if defined (GDK_WINDOWING_X11)
40 #include <gdk/gdkx.h>
41 #elif defined (GDK_WINDOWING_WIN32)
42 #include <gdk/gdkwin32.h>
43 #elif defined (GDK_WINDOWING_QUARTZ)
44 #include <gdk/gdkquartzwindow.h>
45 #endif
46
47 #include <gst/interfaces/xoverlay.h>
48 #include <gst/interfaces/navigation.h>
49
50 GST_DEBUG_CATEGORY_STATIC (seek_debug);
51 #define GST_CAT_DEFAULT (seek_debug)
52
53 /* configuration */
54
55 #define SOURCE "filesrc"
56
57 static gchar *opt_audiosink_str;        /* NULL */
58 static gchar *opt_videosink_str;        /* NULL */
59
60 #define FILL_INTERVAL 100
61 //#define UPDATE_INTERVAL 500
62 //#define UPDATE_INTERVAL 100
63 #define UPDATE_INTERVAL 40
64
65 /* number of milliseconds to play for after a seek */
66 #define SCRUB_TIME 100
67
68 /* timeout for gst_element_get_state() after a seek */
69 #define SEEK_TIMEOUT 40 * GST_MSECOND
70
71 #define DEFAULT_VIDEO_HEIGHT 300
72
73 /* the state to go to when stop is pressed */
74 #define STOP_STATE      GST_STATE_READY
75
76 #define N_GRAD 1000.0
77
78 static GList *seekable_pads = NULL;
79 static GList *rate_pads = NULL;
80 static GList *seekable_elements = NULL;
81
82 static gboolean accurate_seek = FALSE;
83 static gboolean keyframe_seek = FALSE;
84 static gboolean loop_seek = FALSE;
85 static gboolean flush_seek = TRUE;
86 static gboolean scrub = TRUE;
87 static gboolean play_scrub = FALSE;
88 static gboolean skip_seek = FALSE;
89 static gdouble rate = 1.0;
90
91 static GstElement *pipeline;
92 static gint pipeline_type;
93 static const gchar *pipeline_spec;
94 static gint64 position = -1;
95 static gint64 duration = -1;
96 static GtkAdjustment *adjustment;
97 static GtkWidget *hscale, *statusbar;
98 static guint status_id = 0;
99 static gboolean stats = FALSE;
100 static gboolean elem_seek = FALSE;
101 static gboolean verbose = FALSE;
102
103 static gboolean is_live = FALSE;
104 static gboolean buffering = FALSE;
105 static GstBufferingMode mode;
106 static gint64 buffering_left;
107 static GstState state = GST_STATE_NULL;
108 static guint update_id = 0;
109 static guint seek_timeout_id = 0;
110 static gulong changed_id;
111 static guint fill_id = 0;
112
113 static gint n_video = 0, n_audio = 0, n_text = 0;
114 static gboolean need_streams = TRUE;
115 static GtkWidget *video_combo, *audio_combo, *text_combo, *vis_combo;
116 static GtkWidget *vis_checkbox, *video_checkbox, *audio_checkbox;
117 static GtkWidget *text_checkbox, *mute_checkbox, *volume_spinbutton;
118 static GtkWidget *skip_checkbox, *video_window, *download_checkbox;
119 static GtkWidget *buffer_checkbox, *rate_spinbutton;
120
121 static GStaticMutex state_mutex = G_STATIC_MUTEX_INIT;
122
123 static GtkWidget *format_combo, *step_amount_spinbutton, *step_rate_spinbutton;
124 static GtkWidget *shuttle_checkbox, *step_button;
125 static GtkWidget *shuttle_hscale;
126 static GtkAdjustment *shuttle_adjustment;
127
128 static struct
129 {
130   GstNavigationCommand cmd;
131   GtkWidget *button;
132 } navigation_buttons[14];
133
134 static GList *paths = NULL, *l = NULL;
135
136 /* we keep an array of the visualisation entries so that we can easily switch
137  * with the combo box index. */
138 typedef struct
139 {
140   GstElementFactory *factory;
141 } VisEntry;
142
143 static GArray *vis_entries;
144
145 static void clear_streams (GstElement * pipeline);
146 static void volume_notify_cb (GstElement * pipeline, GParamSpec * arg,
147     gpointer user_dat);
148 static void find_navigation_element (void);
149
150 /* pipeline construction */
151
152 typedef struct
153 {
154   const gchar *padname;
155   GstPad *target;
156   GstElement *bin;
157 }
158 dyn_link;
159
160 static GstElement *
161 gst_element_factory_make_or_warn (const gchar * type, const gchar * name)
162 {
163   GstElement *element = gst_element_factory_make (type, name);
164
165 #ifndef GST_DISABLE_PARSE
166   if (!element) {
167     /* Try parsing it as a pipeline description */
168     element = gst_parse_bin_from_description (type, TRUE, NULL);
169     if (element) {
170       gst_element_set_name (element, name);
171     }
172   }
173 #endif
174
175   if (!element) {
176     g_warning ("Failed to create element %s of type %s", name, type);
177   }
178
179   return element;
180 }
181
182 static void
183 dynamic_link (GstPadTemplate * templ, GstPad * newpad, gpointer data)
184 {
185   gchar *padname;
186   dyn_link *connect = (dyn_link *) data;
187
188   padname = gst_pad_get_name (newpad);
189
190   if (connect->padname == NULL || !strcmp (padname, connect->padname)) {
191     if (connect->bin)
192       gst_bin_add (GST_BIN (pipeline), connect->bin);
193     gst_pad_link (newpad, connect->target);
194
195     //seekable_pads = g_list_prepend (seekable_pads, newpad);
196     rate_pads = g_list_prepend (rate_pads, newpad);
197   }
198   g_free (padname);
199 }
200
201 static void
202 setup_dynamic_link (GstElement * element, const gchar * padname,
203     GstPad * target, GstElement * bin)
204 {
205   dyn_link *connect;
206
207   connect = g_new0 (dyn_link, 1);
208   connect->padname = g_strdup (padname);
209   connect->target = target;
210   connect->bin = bin;
211
212   g_signal_connect (G_OBJECT (element), "pad-added", G_CALLBACK (dynamic_link),
213       connect);
214 }
215
216 static GstElement *
217 make_mod_pipeline (const gchar * location)
218 {
219   GstElement *pipeline;
220   GstElement *src, *decoder, *audiosink;
221   GstPad *seekable;
222
223   pipeline = gst_pipeline_new ("app");
224
225   src = gst_element_factory_make_or_warn (SOURCE, "src");
226   decoder = gst_element_factory_make_or_warn ("modplug", "decoder");
227   audiosink = gst_element_factory_make_or_warn (opt_audiosink_str, "sink");
228   //g_object_set (G_OBJECT (audiosink), "sync", FALSE, NULL);
229
230   g_object_set (G_OBJECT (src), "location", location, NULL);
231
232   gst_bin_add (GST_BIN (pipeline), src);
233   gst_bin_add (GST_BIN (pipeline), decoder);
234   gst_bin_add (GST_BIN (pipeline), audiosink);
235
236   gst_element_link (src, decoder);
237   gst_element_link (decoder, audiosink);
238
239   seekable = gst_element_get_static_pad (decoder, "src");
240   seekable_pads = g_list_prepend (seekable_pads, seekable);
241   rate_pads = g_list_prepend (rate_pads, seekable);
242   rate_pads =
243       g_list_prepend (rate_pads, gst_element_get_static_pad (decoder, "sink"));
244
245   return pipeline;
246 }
247
248 static GstElement *
249 make_dv_pipeline (const gchar * location)
250 {
251   GstElement *pipeline;
252   GstElement *src, *demux, *decoder, *audiosink, *videosink;
253   GstElement *a_queue, *v_queue;
254   GstPad *seekable;
255
256   pipeline = gst_pipeline_new ("app");
257
258   src = gst_element_factory_make_or_warn (SOURCE, "src");
259   demux = gst_element_factory_make_or_warn ("dvdemux", "demuxer");
260   v_queue = gst_element_factory_make_or_warn ("queue", "v_queue");
261   decoder = gst_element_factory_make_or_warn ("ffdec_dvvideo", "decoder");
262   videosink = gst_element_factory_make_or_warn (opt_videosink_str, "v_sink");
263   a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
264   audiosink = gst_element_factory_make_or_warn ("alsasink", "a_sink");
265
266   g_object_set (G_OBJECT (src), "location", location, NULL);
267
268   gst_bin_add (GST_BIN (pipeline), src);
269   gst_bin_add (GST_BIN (pipeline), demux);
270   gst_bin_add (GST_BIN (pipeline), a_queue);
271   gst_bin_add (GST_BIN (pipeline), audiosink);
272   gst_bin_add (GST_BIN (pipeline), v_queue);
273   gst_bin_add (GST_BIN (pipeline), decoder);
274   gst_bin_add (GST_BIN (pipeline), videosink);
275
276   gst_element_link (src, demux);
277   gst_element_link (a_queue, audiosink);
278   gst_element_link (v_queue, decoder);
279   gst_element_link (decoder, videosink);
280
281   setup_dynamic_link (demux, "video", gst_element_get_static_pad (v_queue,
282           "sink"), NULL);
283   setup_dynamic_link (demux, "audio", gst_element_get_static_pad (a_queue,
284           "sink"), NULL);
285
286   seekable = gst_element_get_static_pad (decoder, "src");
287   seekable_pads = g_list_prepend (seekable_pads, seekable);
288   rate_pads = g_list_prepend (rate_pads, seekable);
289
290   return pipeline;
291 }
292
293 static GstElement *
294 make_wav_pipeline (const gchar * location)
295 {
296   GstElement *pipeline;
297   GstElement *src, *decoder, *audiosink;
298
299   pipeline = gst_pipeline_new ("app");
300
301   src = gst_element_factory_make_or_warn (SOURCE, "src");
302   decoder = gst_element_factory_make_or_warn ("wavparse", "decoder");
303   audiosink = gst_element_factory_make_or_warn (opt_audiosink_str, "sink");
304
305   g_object_set (G_OBJECT (src), "location", location, NULL);
306
307   gst_bin_add (GST_BIN (pipeline), src);
308   gst_bin_add (GST_BIN (pipeline), decoder);
309   gst_bin_add (GST_BIN (pipeline), audiosink);
310
311   gst_element_link (src, decoder);
312
313   setup_dynamic_link (decoder, "src", gst_element_get_static_pad (audiosink,
314           "sink"), NULL);
315
316   seekable_elements = g_list_prepend (seekable_elements, audiosink);
317
318   /* force element seeking on this pipeline */
319   elem_seek = TRUE;
320
321   return pipeline;
322 }
323
324 static GstElement *
325 make_flac_pipeline (const gchar * location)
326 {
327   GstElement *pipeline;
328   GstElement *src, *decoder, *audiosink;
329   GstPad *seekable;
330
331   pipeline = gst_pipeline_new ("app");
332
333   src = gst_element_factory_make_or_warn (SOURCE, "src");
334   decoder = gst_element_factory_make_or_warn ("flacdec", "decoder");
335   audiosink = gst_element_factory_make_or_warn (opt_audiosink_str, "sink");
336   g_object_set (G_OBJECT (audiosink), "sync", FALSE, NULL);
337
338   g_object_set (G_OBJECT (src), "location", location, NULL);
339
340   gst_bin_add (GST_BIN (pipeline), src);
341   gst_bin_add (GST_BIN (pipeline), decoder);
342   gst_bin_add (GST_BIN (pipeline), audiosink);
343
344   gst_element_link (src, decoder);
345   gst_element_link (decoder, audiosink);
346
347   seekable = gst_element_get_static_pad (decoder, "src");
348   seekable_pads = g_list_prepend (seekable_pads, seekable);
349   rate_pads = g_list_prepend (rate_pads, seekable);
350   rate_pads =
351       g_list_prepend (rate_pads, gst_element_get_static_pad (decoder, "sink"));
352
353   return pipeline;
354 }
355
356 static GstElement *
357 make_sid_pipeline (const gchar * location)
358 {
359   GstElement *pipeline;
360   GstElement *src, *decoder, *audiosink;
361   GstPad *seekable;
362
363   pipeline = gst_pipeline_new ("app");
364
365   src = gst_element_factory_make_or_warn (SOURCE, "src");
366   decoder = gst_element_factory_make_or_warn ("siddec", "decoder");
367   audiosink = gst_element_factory_make_or_warn (opt_audiosink_str, "sink");
368   //g_object_set (G_OBJECT (audiosink), "sync", FALSE, NULL);
369
370   g_object_set (G_OBJECT (src), "location", location, NULL);
371
372   gst_bin_add (GST_BIN (pipeline), src);
373   gst_bin_add (GST_BIN (pipeline), decoder);
374   gst_bin_add (GST_BIN (pipeline), audiosink);
375
376   gst_element_link (src, decoder);
377   gst_element_link (decoder, audiosink);
378
379   seekable = gst_element_get_static_pad (decoder, "src");
380   seekable_pads = g_list_prepend (seekable_pads, seekable);
381   rate_pads = g_list_prepend (rate_pads, seekable);
382   rate_pads =
383       g_list_prepend (rate_pads, gst_element_get_static_pad (decoder, "sink"));
384
385   return pipeline;
386 }
387
388 static GstElement *
389 make_parse_pipeline (const gchar * location)
390 {
391   GstElement *pipeline;
392   GstElement *src, *parser, *fakesink;
393   GstPad *seekable;
394
395   pipeline = gst_pipeline_new ("app");
396
397   src = gst_element_factory_make_or_warn (SOURCE, "src");
398   parser = gst_element_factory_make_or_warn ("mpegparse", "parse");
399   fakesink = gst_element_factory_make_or_warn ("fakesink", "sink");
400   g_object_set (G_OBJECT (fakesink), "silent", TRUE, NULL);
401   g_object_set (G_OBJECT (fakesink), "sync", TRUE, NULL);
402
403   g_object_set (G_OBJECT (src), "location", location, NULL);
404
405   gst_bin_add (GST_BIN (pipeline), src);
406   gst_bin_add (GST_BIN (pipeline), parser);
407   gst_bin_add (GST_BIN (pipeline), fakesink);
408
409   gst_element_link (src, parser);
410   gst_element_link (parser, fakesink);
411
412   seekable = gst_element_get_static_pad (parser, "src");
413   seekable_pads = g_list_prepend (seekable_pads, seekable);
414   rate_pads = g_list_prepend (rate_pads, seekable);
415   rate_pads =
416       g_list_prepend (rate_pads, gst_element_get_static_pad (parser, "sink"));
417
418   return pipeline;
419 }
420
421 static GstElement *
422 make_vorbis_pipeline (const gchar * location)
423 {
424   GstElement *pipeline, *audio_bin;
425   GstElement *src, *demux, *decoder, *convert, *audiosink;
426   GstPad *pad, *seekable;
427
428   pipeline = gst_pipeline_new ("app");
429
430   src = gst_element_factory_make_or_warn (SOURCE, "src");
431   demux = gst_element_factory_make_or_warn ("oggdemux", "demux");
432   decoder = gst_element_factory_make_or_warn ("vorbisdec", "decoder");
433   convert = gst_element_factory_make_or_warn ("audioconvert", "convert");
434   audiosink = gst_element_factory_make_or_warn (opt_audiosink_str, "sink");
435   g_object_set (G_OBJECT (audiosink), "sync", TRUE, NULL);
436
437   g_object_set (G_OBJECT (src), "location", location, NULL);
438
439   audio_bin = gst_bin_new ("a_decoder_bin");
440
441   gst_bin_add (GST_BIN (pipeline), src);
442   gst_bin_add (GST_BIN (pipeline), demux);
443   gst_bin_add (GST_BIN (audio_bin), decoder);
444   gst_bin_add (GST_BIN (audio_bin), convert);
445   gst_bin_add (GST_BIN (audio_bin), audiosink);
446   gst_bin_add (GST_BIN (pipeline), audio_bin);
447
448   gst_element_link (src, demux);
449   gst_element_link (decoder, convert);
450   gst_element_link (convert, audiosink);
451
452   pad = gst_element_get_static_pad (decoder, "sink");
453   gst_element_add_pad (audio_bin, gst_ghost_pad_new ("sink", pad));
454   gst_object_unref (pad);
455
456   setup_dynamic_link (demux, NULL, gst_element_get_static_pad (audio_bin,
457           "sink"), NULL);
458
459   seekable = gst_element_get_static_pad (decoder, "src");
460   seekable_pads = g_list_prepend (seekable_pads, seekable);
461   rate_pads = g_list_prepend (rate_pads, seekable);
462   rate_pads =
463       g_list_prepend (rate_pads, gst_element_get_static_pad (decoder, "sink"));
464
465   return pipeline;
466 }
467
468 static GstElement *
469 make_theora_pipeline (const gchar * location)
470 {
471   GstElement *pipeline, *video_bin;
472   GstElement *src, *demux, *decoder, *convert, *videosink;
473   GstPad *pad, *seekable;
474
475   pipeline = gst_pipeline_new ("app");
476
477   src = gst_element_factory_make_or_warn (SOURCE, "src");
478   demux = gst_element_factory_make_or_warn ("oggdemux", "demux");
479   decoder = gst_element_factory_make_or_warn ("theoradec", "decoder");
480   convert = gst_element_factory_make_or_warn ("ffmpegcolorspace", "convert");
481   videosink = gst_element_factory_make_or_warn (opt_videosink_str, "sink");
482
483   g_object_set (G_OBJECT (src), "location", location, NULL);
484
485   video_bin = gst_bin_new ("v_decoder_bin");
486
487   gst_bin_add (GST_BIN (pipeline), src);
488   gst_bin_add (GST_BIN (pipeline), demux);
489   gst_bin_add (GST_BIN (video_bin), decoder);
490   gst_bin_add (GST_BIN (video_bin), convert);
491   gst_bin_add (GST_BIN (video_bin), videosink);
492   gst_bin_add (GST_BIN (pipeline), video_bin);
493
494   gst_element_link (src, demux);
495   gst_element_link (decoder, convert);
496   gst_element_link (convert, videosink);
497
498   pad = gst_element_get_static_pad (decoder, "sink");
499   gst_element_add_pad (video_bin, gst_ghost_pad_new ("sink", pad));
500   gst_object_unref (pad);
501
502   setup_dynamic_link (demux, NULL, gst_element_get_static_pad (video_bin,
503           "sink"), NULL);
504
505   seekable = gst_element_get_static_pad (decoder, "src");
506   seekable_pads = g_list_prepend (seekable_pads, seekable);
507   rate_pads = g_list_prepend (rate_pads, seekable);
508   rate_pads =
509       g_list_prepend (rate_pads, gst_element_get_static_pad (decoder, "sink"));
510
511   return pipeline;
512 }
513
514 static GstElement *
515 make_vorbis_theora_pipeline (const gchar * location)
516 {
517   GstElement *pipeline, *audio_bin, *video_bin;
518   GstElement *src, *demux, *a_decoder, *a_convert, *v_decoder, *v_convert;
519   GstElement *audiosink, *videosink;
520   GstElement *a_queue, *v_queue, *v_scale;
521   GstPad *seekable;
522   GstPad *pad;
523
524   pipeline = gst_pipeline_new ("app");
525
526   src = gst_element_factory_make_or_warn (SOURCE, "src");
527   g_object_set (G_OBJECT (src), "location", location, NULL);
528
529   demux = gst_element_factory_make_or_warn ("oggdemux", "demux");
530
531   gst_bin_add (GST_BIN (pipeline), src);
532   gst_bin_add (GST_BIN (pipeline), demux);
533   gst_element_link (src, demux);
534
535   audio_bin = gst_bin_new ("a_decoder_bin");
536   a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
537   a_decoder = gst_element_factory_make_or_warn ("vorbisdec", "a_dec");
538   a_convert = gst_element_factory_make_or_warn ("audioconvert", "a_convert");
539   audiosink = gst_element_factory_make_or_warn (opt_audiosink_str, "a_sink");
540
541   gst_bin_add (GST_BIN (pipeline), audio_bin);
542
543   gst_bin_add (GST_BIN (audio_bin), a_queue);
544   gst_bin_add (GST_BIN (audio_bin), a_decoder);
545   gst_bin_add (GST_BIN (audio_bin), a_convert);
546   gst_bin_add (GST_BIN (audio_bin), audiosink);
547
548   gst_element_link (a_queue, a_decoder);
549   gst_element_link (a_decoder, a_convert);
550   gst_element_link (a_convert, audiosink);
551
552   pad = gst_element_get_static_pad (a_queue, "sink");
553   gst_element_add_pad (audio_bin, gst_ghost_pad_new ("sink", pad));
554   gst_object_unref (pad);
555
556   setup_dynamic_link (demux, NULL, gst_element_get_static_pad (audio_bin,
557           "sink"), NULL);
558
559   video_bin = gst_bin_new ("v_decoder_bin");
560   v_queue = gst_element_factory_make_or_warn ("queue", "v_queue");
561   v_decoder = gst_element_factory_make_or_warn ("theoradec", "v_dec");
562   v_convert =
563       gst_element_factory_make_or_warn ("ffmpegcolorspace", "v_convert");
564   v_scale = gst_element_factory_make_or_warn ("videoscale", "v_scale");
565   videosink = gst_element_factory_make_or_warn (opt_videosink_str, "v_sink");
566
567   gst_bin_add (GST_BIN (pipeline), video_bin);
568
569   gst_bin_add (GST_BIN (video_bin), v_queue);
570   gst_bin_add (GST_BIN (video_bin), v_decoder);
571   gst_bin_add (GST_BIN (video_bin), v_convert);
572   gst_bin_add (GST_BIN (video_bin), v_scale);
573   gst_bin_add (GST_BIN (video_bin), videosink);
574
575   gst_element_link_many (v_queue, v_decoder, v_convert, v_scale, videosink,
576       NULL);
577
578   pad = gst_element_get_static_pad (v_queue, "sink");
579   gst_element_add_pad (video_bin, gst_ghost_pad_new ("sink", pad));
580   gst_object_unref (pad);
581
582   setup_dynamic_link (demux, NULL, gst_element_get_static_pad (video_bin,
583           "sink"), NULL);
584
585   seekable = gst_element_get_static_pad (a_decoder, "src");
586   seekable_pads = g_list_prepend (seekable_pads, seekable);
587   rate_pads = g_list_prepend (rate_pads, seekable);
588   rate_pads =
589       g_list_prepend (rate_pads, gst_element_get_static_pad (a_decoder,
590           "sink"));
591
592   return pipeline;
593 }
594
595 static GstElement *
596 make_avi_msmpeg4v3_mp3_pipeline (const gchar * location)
597 {
598   GstElement *pipeline, *audio_bin, *video_bin;
599   GstElement *src, *demux, *a_decoder, *a_convert, *v_decoder, *v_convert;
600   GstElement *audiosink, *videosink;
601   GstElement *a_queue, *v_queue;
602   GstPad *seekable, *pad;
603
604   pipeline = gst_pipeline_new ("app");
605
606   src = gst_element_factory_make_or_warn (SOURCE, "src");
607   g_object_set (G_OBJECT (src), "location", location, NULL);
608
609   demux = gst_element_factory_make_or_warn ("avidemux", "demux");
610
611   gst_bin_add (GST_BIN (pipeline), src);
612   gst_bin_add (GST_BIN (pipeline), demux);
613   gst_element_link (src, demux);
614
615   audio_bin = gst_bin_new ("a_decoder_bin");
616   a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
617   a_decoder = gst_element_factory_make_or_warn ("mad", "a_dec");
618   a_convert = gst_element_factory_make_or_warn ("audioconvert", "a_convert");
619   audiosink = gst_element_factory_make_or_warn (opt_audiosink_str, "a_sink");
620
621   gst_bin_add (GST_BIN (audio_bin), a_queue);
622   gst_bin_add (GST_BIN (audio_bin), a_decoder);
623   gst_bin_add (GST_BIN (audio_bin), a_convert);
624   gst_bin_add (GST_BIN (audio_bin), audiosink);
625
626   gst_element_link (a_queue, a_decoder);
627   gst_element_link (a_decoder, a_convert);
628   gst_element_link (a_convert, audiosink);
629
630   gst_bin_add (GST_BIN (pipeline), audio_bin);
631
632   pad = gst_element_get_static_pad (a_queue, "sink");
633   gst_element_add_pad (audio_bin, gst_ghost_pad_new ("sink", pad));
634   gst_object_unref (pad);
635
636   setup_dynamic_link (demux, NULL, gst_element_get_static_pad (audio_bin,
637           "sink"), NULL);
638
639   video_bin = gst_bin_new ("v_decoder_bin");
640   v_queue = gst_element_factory_make_or_warn ("queue", "v_queue");
641   v_decoder = gst_element_factory_make_or_warn ("ffdec_msmpeg4", "v_dec");
642   v_convert =
643       gst_element_factory_make_or_warn ("ffmpegcolorspace", "v_convert");
644   videosink = gst_element_factory_make_or_warn (opt_videosink_str, "v_sink");
645
646   gst_bin_add (GST_BIN (video_bin), v_queue);
647   gst_bin_add (GST_BIN (video_bin), v_decoder);
648   gst_bin_add (GST_BIN (video_bin), v_convert);
649   gst_bin_add (GST_BIN (video_bin), videosink);
650
651   gst_element_link_many (v_queue, v_decoder, v_convert, videosink, NULL);
652
653   gst_bin_add (GST_BIN (pipeline), video_bin);
654
655   pad = gst_element_get_static_pad (v_queue, "sink");
656   gst_element_add_pad (video_bin, gst_ghost_pad_new ("sink", pad));
657   gst_object_unref (pad);
658
659   setup_dynamic_link (demux, NULL, gst_element_get_static_pad (video_bin,
660           "sink"), NULL);
661
662   seekable = gst_element_get_static_pad (a_decoder, "src");
663   seekable_pads = g_list_prepend (seekable_pads, seekable);
664   rate_pads = g_list_prepend (rate_pads, seekable);
665   rate_pads =
666       g_list_prepend (rate_pads, gst_element_get_static_pad (a_decoder,
667           "sink"));
668
669   return pipeline;
670 }
671
672 static GstElement *
673 make_mp3_pipeline (const gchar * location)
674 {
675   GstElement *pipeline;
676   GstElement *src, *parser, *decoder, *audiosink, *queue;
677   GstPad *seekable;
678
679   pipeline = gst_pipeline_new ("app");
680
681   src = gst_element_factory_make_or_warn (SOURCE, "src");
682   parser = gst_element_factory_make_or_warn ("mp3parse", "parse");
683   decoder = gst_element_factory_make_or_warn ("mad", "dec");
684   queue = gst_element_factory_make_or_warn ("queue", "queue");
685   audiosink = gst_element_factory_make_or_warn (opt_audiosink_str, "sink");
686
687   seekable_elements = g_list_prepend (seekable_elements, audiosink);
688
689   g_object_set (G_OBJECT (src), "location", location, NULL);
690   //g_object_set (G_OBJECT (audiosink), "fragment", 0x00180008, NULL);
691
692   gst_bin_add (GST_BIN (pipeline), src);
693   gst_bin_add (GST_BIN (pipeline), parser);
694   gst_bin_add (GST_BIN (pipeline), decoder);
695   gst_bin_add (GST_BIN (pipeline), queue);
696   gst_bin_add (GST_BIN (pipeline), audiosink);
697
698   gst_element_link (src, parser);
699   gst_element_link (parser, decoder);
700   gst_element_link (decoder, queue);
701   gst_element_link (queue, audiosink);
702
703   seekable = gst_element_get_static_pad (queue, "src");
704   seekable_pads = g_list_prepend (seekable_pads, seekable);
705   rate_pads = g_list_prepend (rate_pads, seekable);
706   rate_pads =
707       g_list_prepend (rate_pads, gst_element_get_static_pad (decoder, "sink"));
708
709   return pipeline;
710 }
711
712 static GstElement *
713 make_avi_pipeline (const gchar * location)
714 {
715   GstElement *pipeline, *audio_bin, *video_bin;
716   GstElement *src, *demux, *a_decoder, *v_decoder, *audiosink, *videosink;
717   GstElement *a_queue = NULL, *v_queue = NULL;
718   GstPad *seekable;
719
720   pipeline = gst_pipeline_new ("app");
721
722   src = gst_element_factory_make_or_warn (SOURCE, "src");
723   g_object_set (G_OBJECT (src), "location", location, NULL);
724
725   demux = gst_element_factory_make_or_warn ("avidemux", "demux");
726   seekable_elements = g_list_prepend (seekable_elements, demux);
727
728   gst_bin_add (GST_BIN (pipeline), src);
729   gst_bin_add (GST_BIN (pipeline), demux);
730   gst_element_link (src, demux);
731
732   audio_bin = gst_bin_new ("a_decoder_bin");
733   a_decoder = gst_element_factory_make_or_warn ("mad", "a_dec");
734   audiosink = gst_element_factory_make_or_warn (opt_audiosink_str, "a_sink");
735   a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
736   gst_element_link (a_decoder, a_queue);
737   gst_element_link (a_queue, audiosink);
738   gst_bin_add (GST_BIN (audio_bin), a_decoder);
739   gst_bin_add (GST_BIN (audio_bin), a_queue);
740   gst_bin_add (GST_BIN (audio_bin), audiosink);
741   gst_element_set_state (audio_bin, GST_STATE_PAUSED);
742
743   setup_dynamic_link (demux, "audio_00", gst_element_get_static_pad (a_decoder,
744           "sink"), audio_bin);
745
746   seekable = gst_element_get_static_pad (a_queue, "src");
747   seekable_pads = g_list_prepend (seekable_pads, seekable);
748   rate_pads = g_list_prepend (rate_pads, seekable);
749   rate_pads =
750       g_list_prepend (rate_pads, gst_element_get_static_pad (a_decoder,
751           "sink"));
752
753   video_bin = gst_bin_new ("v_decoder_bin");
754   v_decoder = gst_element_factory_make_or_warn ("ffmpegdecall", "v_dec");
755   videosink = gst_element_factory_make_or_warn (opt_videosink_str, "v_sink");
756   v_queue = gst_element_factory_make_or_warn ("queue", "v_queue");
757   gst_element_link (v_decoder, v_queue);
758   gst_element_link (v_queue, videosink);
759   gst_bin_add (GST_BIN (video_bin), v_decoder);
760   gst_bin_add (GST_BIN (video_bin), v_queue);
761   gst_bin_add (GST_BIN (video_bin), videosink);
762
763   gst_element_set_state (video_bin, GST_STATE_PAUSED);
764
765   setup_dynamic_link (demux, "video_00", gst_element_get_static_pad (v_decoder,
766           "sink"), video_bin);
767
768   seekable = gst_element_get_static_pad (v_queue, "src");
769   seekable_pads = g_list_prepend (seekable_pads, seekable);
770   rate_pads = g_list_prepend (rate_pads, seekable);
771   rate_pads =
772       g_list_prepend (rate_pads, gst_element_get_static_pad (v_decoder,
773           "sink"));
774
775   return pipeline;
776 }
777
778 static GstElement *
779 make_mpeg_pipeline (const gchar * location)
780 {
781   GstElement *pipeline, *audio_bin, *video_bin;
782   GstElement *src, *demux, *a_decoder, *v_decoder, *v_filter;
783   GstElement *audiosink, *videosink;
784   GstElement *a_queue, *v_queue;
785   GstPad *seekable;
786   GstPad *pad;
787
788   pipeline = gst_pipeline_new ("app");
789
790   src = gst_element_factory_make_or_warn (SOURCE, "src");
791   g_object_set (G_OBJECT (src), "location", location, NULL);
792
793   //demux = gst_element_factory_make_or_warn ("mpegdemux", "demux");
794   demux = gst_element_factory_make_or_warn ("flupsdemux", "demux");
795
796   gst_bin_add (GST_BIN (pipeline), src);
797   gst_bin_add (GST_BIN (pipeline), demux);
798   gst_element_link (src, demux);
799
800   audio_bin = gst_bin_new ("a_decoder_bin");
801   a_decoder = gst_element_factory_make_or_warn ("mad", "a_dec");
802   a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
803   audiosink = gst_element_factory_make_or_warn (opt_audiosink_str, "a_sink");
804   gst_bin_add (GST_BIN (audio_bin), a_decoder);
805   gst_bin_add (GST_BIN (audio_bin), a_queue);
806   gst_bin_add (GST_BIN (audio_bin), audiosink);
807
808   gst_element_link (a_decoder, a_queue);
809   gst_element_link (a_queue, audiosink);
810
811   gst_bin_add (GST_BIN (pipeline), audio_bin);
812
813   pad = gst_element_get_static_pad (a_decoder, "sink");
814   gst_element_add_pad (audio_bin, gst_ghost_pad_new ("sink", pad));
815   gst_object_unref (pad);
816
817   setup_dynamic_link (demux, "audio_c0", gst_element_get_static_pad (audio_bin,
818           "sink"), NULL);
819
820   video_bin = gst_bin_new ("v_decoder_bin");
821   v_decoder = gst_element_factory_make_or_warn ("mpeg2dec", "v_dec");
822   v_queue = gst_element_factory_make_or_warn ("queue", "v_queue");
823   v_filter = gst_element_factory_make_or_warn ("ffmpegcolorspace", "v_filter");
824   videosink = gst_element_factory_make_or_warn (opt_videosink_str, "v_sink");
825
826   gst_bin_add (GST_BIN (video_bin), v_decoder);
827   gst_bin_add (GST_BIN (video_bin), v_queue);
828   gst_bin_add (GST_BIN (video_bin), v_filter);
829   gst_bin_add (GST_BIN (video_bin), videosink);
830
831   gst_element_link (v_decoder, v_queue);
832   gst_element_link (v_queue, v_filter);
833   gst_element_link (v_filter, videosink);
834
835   gst_bin_add (GST_BIN (pipeline), video_bin);
836
837   pad = gst_element_get_static_pad (v_decoder, "sink");
838   gst_element_add_pad (video_bin, gst_ghost_pad_new ("sink", pad));
839   gst_object_unref (pad);
840
841   setup_dynamic_link (demux, "video_e0", gst_element_get_static_pad (video_bin,
842           "sink"), NULL);
843
844   seekable = gst_element_get_static_pad (v_filter, "src");
845   seekable_pads = g_list_prepend (seekable_pads, seekable);
846   rate_pads = g_list_prepend (rate_pads, seekable);
847   rate_pads =
848       g_list_prepend (rate_pads, gst_element_get_static_pad (v_decoder,
849           "sink"));
850
851   return pipeline;
852 }
853
854 static GstElement *
855 make_mpegnt_pipeline (const gchar * location)
856 {
857   GstElement *pipeline, *audio_bin, *video_bin;
858   GstElement *src, *demux, *a_decoder, *v_decoder, *v_filter;
859   GstElement *audiosink, *videosink;
860   GstElement *a_queue;
861   GstPad *seekable;
862
863   pipeline = gst_pipeline_new ("app");
864
865   src = gst_element_factory_make_or_warn (SOURCE, "src");
866   g_object_set (G_OBJECT (src), "location", location, NULL);
867
868   demux = gst_element_factory_make_or_warn ("mpegdemux", "demux");
869   //g_object_set (G_OBJECT (demux), "sync", TRUE, NULL);
870
871   seekable_elements = g_list_prepend (seekable_elements, demux);
872
873   gst_bin_add (GST_BIN (pipeline), src);
874   gst_bin_add (GST_BIN (pipeline), demux);
875   gst_element_link (src, demux);
876
877   audio_bin = gst_bin_new ("a_decoder_bin");
878   a_decoder = gst_element_factory_make_or_warn ("mad", "a_dec");
879   a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
880   audiosink = gst_element_factory_make_or_warn (opt_audiosink_str, "a_sink");
881   //g_object_set (G_OBJECT (audiosink), "fragment", 0x00180008, NULL);
882   g_object_set (G_OBJECT (audiosink), "sync", FALSE, NULL);
883   gst_element_link (a_decoder, a_queue);
884   gst_element_link (a_queue, audiosink);
885   gst_bin_add (GST_BIN (audio_bin), a_decoder);
886   gst_bin_add (GST_BIN (audio_bin), a_queue);
887   gst_bin_add (GST_BIN (audio_bin), audiosink);
888
889   setup_dynamic_link (demux, "audio_00", gst_element_get_static_pad (a_decoder,
890           "sink"), audio_bin);
891
892   seekable = gst_element_get_static_pad (a_queue, "src");
893   seekable_pads = g_list_prepend (seekable_pads, seekable);
894   rate_pads = g_list_prepend (rate_pads, seekable);
895   rate_pads =
896       g_list_prepend (rate_pads, gst_element_get_static_pad (a_decoder,
897           "sink"));
898
899   video_bin = gst_bin_new ("v_decoder_bin");
900   v_decoder = gst_element_factory_make_or_warn ("mpeg2dec", "v_dec");
901   v_filter = gst_element_factory_make_or_warn ("ffmpegcolorspace", "v_filter");
902   videosink = gst_element_factory_make_or_warn (opt_videosink_str, "v_sink");
903   gst_element_link_many (v_decoder, v_filter, videosink, NULL);
904
905   gst_bin_add_many (GST_BIN (video_bin), v_decoder, v_filter, videosink, NULL);
906
907   setup_dynamic_link (demux, "video_00", gst_element_get_static_pad (v_decoder,
908           "sink"), video_bin);
909
910   seekable = gst_element_get_static_pad (v_decoder, "src");
911   seekable_pads = g_list_prepend (seekable_pads, seekable);
912   rate_pads = g_list_prepend (rate_pads, seekable);
913   rate_pads =
914       g_list_prepend (rate_pads, gst_element_get_static_pad (v_decoder,
915           "sink"));
916
917   return pipeline;
918 }
919
920 static void
921 playerbin_set_uri (GstElement * player, const gchar * location)
922 {
923   gchar *uri;
924
925   /* Add "file://" prefix for convenience */
926   if (g_str_has_prefix (location, "/") || !gst_uri_is_valid (location)) {
927     uri = gst_filename_to_uri (location, NULL);
928     g_print ("Setting URI: %s\n", uri);
929     g_object_set (G_OBJECT (player), "uri", uri, NULL);
930     g_free (uri);
931   } else {
932     g_print ("Setting URI: %s\n", location);
933     g_object_set (G_OBJECT (player), "uri", location, NULL);
934   }
935 }
936
937 static GstElement *
938 construct_playerbin (const gchar * name, const gchar * location)
939 {
940   GstElement *player;
941   GstElement *avsink;
942
943   player = gst_element_factory_make (name, "player");
944   g_assert (player);
945
946   playerbin_set_uri (player, location);
947
948   seekable_elements = g_list_prepend (seekable_elements, player);
949
950   /* force element seeking on this pipeline */
951   elem_seek = TRUE;
952
953   avsink = gst_element_factory_make_or_warn (opt_audiosink_str, "a_sink");
954   if (avsink)
955     g_object_set (player, "audio-sink", avsink, NULL);
956
957   avsink = gst_element_factory_make_or_warn (opt_videosink_str, "v_sink");
958   if (avsink)
959     g_object_set (player, "video-sink", avsink, NULL);
960
961   return player;
962 }
963
964 static GstElement *
965 make_playerbin_pipeline (const gchar * location)
966 {
967   return construct_playerbin ("playbin", location);
968 }
969
970 static GstElement *
971 make_playerbin2_pipeline (const gchar * location)
972 {
973   GstElement *pipeline = construct_playerbin ("playbin2", location);
974
975   /* FIXME: this is not triggered, playbin2 is not forwarding it from the sink */
976   g_signal_connect (pipeline, "notify::volume", G_CALLBACK (volume_notify_cb),
977       NULL);
978   return pipeline;
979 }
980
981 #ifndef GST_DISABLE_PARSE
982 static GstElement *
983 make_parselaunch_pipeline (const gchar * description)
984 {
985   GstElement *pipeline;
986   GError *error = NULL;
987
988   pipeline = gst_parse_launch (description, &error);
989
990   seekable_elements = g_list_prepend (seekable_elements, pipeline);
991
992   elem_seek = TRUE;
993
994   return pipeline;
995 }
996 #endif
997
998 typedef struct
999 {
1000   const gchar *name;
1001   GstElement *(*func) (const gchar * location);
1002 }
1003 Pipeline;
1004
1005 static Pipeline pipelines[] = {
1006   {"mp3", make_mp3_pipeline},
1007   {"avi", make_avi_pipeline},
1008   {"mpeg1", make_mpeg_pipeline},
1009   {"mpegparse", make_parse_pipeline},
1010   {"vorbis", make_vorbis_pipeline},
1011   {"theora", make_theora_pipeline},
1012   {"ogg/v/t", make_vorbis_theora_pipeline},
1013   {"avi/msmpeg4v3/mp3", make_avi_msmpeg4v3_mp3_pipeline},
1014   {"sid", make_sid_pipeline},
1015   {"flac", make_flac_pipeline},
1016   {"wav", make_wav_pipeline},
1017   {"mod", make_mod_pipeline},
1018   {"dv", make_dv_pipeline},
1019   {"mpeg1nothreads", make_mpegnt_pipeline},
1020   {"playerbin", make_playerbin_pipeline},
1021 #ifndef GST_DISABLE_PARSE
1022   {"parse-launch", make_parselaunch_pipeline},
1023 #endif
1024   {"playerbin2", make_playerbin2_pipeline},
1025   {NULL, NULL},
1026 };
1027
1028 #define NUM_TYPES       ((sizeof (pipelines) / sizeof (Pipeline)) - 1)
1029
1030 /* ui callbacks and helpers */
1031
1032 static gchar *
1033 format_value (GtkScale * scale, gdouble value)
1034 {
1035   gint64 real;
1036   gint64 seconds;
1037   gint64 subseconds;
1038
1039   real = value * duration / N_GRAD;
1040   seconds = (gint64) real / GST_SECOND;
1041   subseconds = (gint64) real / (GST_SECOND / N_GRAD);
1042
1043   return g_strdup_printf ("%02" G_GINT64_FORMAT ":%02" G_GINT64_FORMAT ":%02"
1044       G_GINT64_FORMAT, seconds / 60, seconds % 60, subseconds % 100);
1045 }
1046
1047
1048 static gchar *
1049 shuttle_format_value (GtkScale * scale, gdouble value)
1050 {
1051   return g_strdup_printf ("%0.*g", gtk_scale_get_digits (scale), value);
1052 }
1053
1054 typedef struct
1055 {
1056   const gchar *name;
1057   const GstFormat format;
1058 }
1059 seek_format;
1060
1061 static seek_format seek_formats[] = {
1062   {"tim", GST_FORMAT_TIME},
1063   {"byt", GST_FORMAT_BYTES},
1064   {"buf", GST_FORMAT_BUFFERS},
1065   {"def", GST_FORMAT_DEFAULT},
1066   {NULL, 0},
1067 };
1068
1069 G_GNUC_UNUSED static void
1070 query_rates (void)
1071 {
1072   GList *walk = rate_pads;
1073
1074   while (walk) {
1075     GstPad *pad = GST_PAD (walk->data);
1076     gint i = 0;
1077
1078     g_print ("rate/sec  %8.8s: ", GST_PAD_NAME (pad));
1079     while (seek_formats[i].name) {
1080       gint64 value;
1081       GstFormat format;
1082
1083       format = seek_formats[i].format;
1084
1085       if (gst_pad_query_convert (pad, GST_FORMAT_TIME, GST_SECOND, &format,
1086               &value)) {
1087         g_print ("%s %13" G_GINT64_FORMAT " | ", seek_formats[i].name, value);
1088       } else {
1089         g_print ("%s %13.13s | ", seek_formats[i].name, "*NA*");
1090       }
1091
1092       i++;
1093     }
1094     g_print (" %s:%s\n", GST_DEBUG_PAD_NAME (pad));
1095
1096     walk = g_list_next (walk);
1097   }
1098 }
1099
1100 G_GNUC_UNUSED static void
1101 query_positions_elems (void)
1102 {
1103   GList *walk = seekable_elements;
1104
1105   while (walk) {
1106     GstElement *element = GST_ELEMENT (walk->data);
1107     gint i = 0;
1108
1109     g_print ("positions %8.8s: ", GST_ELEMENT_NAME (element));
1110     while (seek_formats[i].name) {
1111       gint64 position, total;
1112       GstFormat format;
1113
1114       format = seek_formats[i].format;
1115
1116       if (gst_element_query_position (element, &format, &position) &&
1117           gst_element_query_duration (element, &format, &total)) {
1118         g_print ("%s %13" G_GINT64_FORMAT " / %13" G_GINT64_FORMAT " | ",
1119             seek_formats[i].name, position, total);
1120       } else {
1121         g_print ("%s %13.13s / %13.13s | ", seek_formats[i].name, "*NA*",
1122             "*NA*");
1123       }
1124       i++;
1125     }
1126     g_print (" %s\n", GST_ELEMENT_NAME (element));
1127
1128     walk = g_list_next (walk);
1129   }
1130 }
1131
1132 G_GNUC_UNUSED static void
1133 query_positions_pads (void)
1134 {
1135   GList *walk = seekable_pads;
1136
1137   while (walk) {
1138     GstPad *pad = GST_PAD (walk->data);
1139     gint i = 0;
1140
1141     g_print ("positions %8.8s: ", GST_PAD_NAME (pad));
1142     while (seek_formats[i].name) {
1143       GstFormat format;
1144       gint64 position, total;
1145
1146       format = seek_formats[i].format;
1147
1148       if (gst_pad_query_position (pad, &format, &position) &&
1149           gst_pad_query_duration (pad, &format, &total)) {
1150         g_print ("%s %13" G_GINT64_FORMAT " / %13" G_GINT64_FORMAT " | ",
1151             seek_formats[i].name, position, total);
1152       } else {
1153         g_print ("%s %13.13s / %13.13s | ", seek_formats[i].name, "*NA*",
1154             "*NA*");
1155       }
1156
1157       i++;
1158     }
1159     g_print (" %s:%s\n", GST_DEBUG_PAD_NAME (pad));
1160
1161     walk = g_list_next (walk);
1162   }
1163 }
1164
1165 static gboolean start_seek (GtkWidget * widget, GdkEventButton * event,
1166     gpointer user_data);
1167 static gboolean stop_seek (GtkWidget * widget, GdkEventButton * event,
1168     gpointer user_data);
1169 static void seek_cb (GtkWidget * widget);
1170
1171 static void
1172 set_scale (gdouble value)
1173 {
1174   g_signal_handlers_block_by_func (hscale, (void *) start_seek,
1175       (void *) pipeline);
1176   g_signal_handlers_block_by_func (hscale, (void *) stop_seek,
1177       (void *) pipeline);
1178   g_signal_handlers_block_by_func (hscale, (void *) seek_cb, (void *) pipeline);
1179   gtk_adjustment_set_value (adjustment, value);
1180   g_signal_handlers_unblock_by_func (hscale, (void *) start_seek,
1181       (void *) pipeline);
1182   g_signal_handlers_unblock_by_func (hscale, (void *) stop_seek,
1183       (void *) pipeline);
1184   g_signal_handlers_unblock_by_func (hscale, (void *) seek_cb,
1185       (void *) pipeline);
1186   gtk_widget_queue_draw (hscale);
1187 }
1188
1189 static gboolean
1190 update_fill (gpointer data)
1191 {
1192   if (elem_seek) {
1193     if (seekable_elements) {
1194       GstElement *element = GST_ELEMENT (seekable_elements->data);
1195       GstQuery *query;
1196
1197       query = gst_query_new_buffering (GST_FORMAT_PERCENT);
1198       if (gst_element_query (element, query)) {
1199         gint64 start, stop, buffering_total;
1200         GstFormat format;
1201         gdouble fill;
1202         gboolean busy;
1203         gint percent;
1204         GstBufferingMode mode;
1205         gint avg_in, avg_out;
1206         gint64 buffering_left;
1207
1208         gst_query_parse_buffering_percent (query, &busy, &percent);
1209         gst_query_parse_buffering_range (query, &format, &start, &stop,
1210             &buffering_total);
1211         gst_query_parse_buffering_stats (query, &mode, &avg_in, &avg_out,
1212             &buffering_left);
1213
1214         /* note that we could start the playback when buffering_left < remaining
1215          * playback time */
1216         GST_DEBUG ("buffering total %" G_GINT64_FORMAT " ms, left %"
1217             G_GINT64_FORMAT " ms", buffering_total, buffering_left);
1218         GST_DEBUG ("start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT,
1219             start, stop);
1220
1221         if (stop != -1)
1222           fill = N_GRAD * stop / GST_FORMAT_PERCENT_MAX;
1223         else
1224           fill = N_GRAD;
1225
1226         gtk_range_set_fill_level (GTK_RANGE (hscale), fill);
1227       }
1228       gst_query_unref (query);
1229     }
1230   }
1231   return TRUE;
1232 }
1233
1234 static gboolean
1235 update_scale (gpointer data)
1236 {
1237   GstFormat format = GST_FORMAT_TIME;
1238
1239   //position = 0;
1240   //duration = 0;
1241
1242   if (elem_seek) {
1243     if (seekable_elements) {
1244       GstElement *element = GST_ELEMENT (seekable_elements->data);
1245
1246       gst_element_query_position (element, &format, &position);
1247       gst_element_query_duration (element, &format, &duration);
1248     }
1249   } else {
1250     if (seekable_pads) {
1251       GstPad *pad = GST_PAD (seekable_pads->data);
1252
1253       gst_pad_query_position (pad, &format, &position);
1254       gst_pad_query_duration (pad, &format, &duration);
1255     }
1256   }
1257
1258   if (stats) {
1259     if (elem_seek) {
1260       query_positions_elems ();
1261     } else {
1262       query_positions_pads ();
1263     }
1264     query_rates ();
1265   }
1266
1267   if (position >= duration)
1268     duration = position;
1269
1270   if (duration > 0) {
1271     set_scale (position * N_GRAD / duration);
1272   }
1273
1274   /* FIXME: see make_playerbin2_pipeline() and volume_notify_cb() */
1275   if (pipeline_type == 16) {
1276     g_object_notify (G_OBJECT (pipeline), "volume");
1277   }
1278
1279   return TRUE;
1280 }
1281
1282 static void do_seek (GtkWidget * widget);
1283 static void connect_bus_signals (GstElement * pipeline);
1284 static void set_update_scale (gboolean active);
1285 static void set_update_fill (gboolean active);
1286
1287 static gboolean
1288 end_scrub (GtkWidget * widget)
1289 {
1290   GST_DEBUG ("end scrub, PAUSE");
1291   gst_element_set_state (pipeline, GST_STATE_PAUSED);
1292   seek_timeout_id = 0;
1293
1294   return FALSE;
1295 }
1296
1297 static gboolean
1298 send_event (GstEvent * event)
1299 {
1300   gboolean res = FALSE;
1301
1302   if (!elem_seek) {
1303     GList *walk = seekable_pads;
1304
1305     while (walk) {
1306       GstPad *seekable = GST_PAD (walk->data);
1307
1308       GST_DEBUG ("send event on pad %s:%s", GST_DEBUG_PAD_NAME (seekable));
1309
1310       gst_event_ref (event);
1311       res = gst_pad_send_event (seekable, event);
1312
1313       walk = g_list_next (walk);
1314     }
1315   } else {
1316     GList *walk = seekable_elements;
1317
1318     while (walk) {
1319       GstElement *seekable = GST_ELEMENT (walk->data);
1320
1321       GST_DEBUG ("send event on element %s", GST_ELEMENT_NAME (seekable));
1322
1323       gst_event_ref (event);
1324       res = gst_element_send_event (seekable, event);
1325
1326       walk = g_list_next (walk);
1327     }
1328   }
1329   gst_event_unref (event);
1330   return res;
1331 }
1332
1333 static void
1334 do_seek (GtkWidget * widget)
1335 {
1336   gint64 real;
1337   gboolean res = FALSE;
1338   GstEvent *s_event;
1339   GstSeekFlags flags;
1340
1341   real = gtk_range_get_value (GTK_RANGE (widget)) * duration / N_GRAD;
1342
1343   GST_DEBUG ("value=%f, real=%" G_GINT64_FORMAT,
1344       gtk_range_get_value (GTK_RANGE (widget)), real);
1345
1346   flags = 0;
1347   if (flush_seek)
1348     flags |= GST_SEEK_FLAG_FLUSH;
1349   if (accurate_seek)
1350     flags |= GST_SEEK_FLAG_ACCURATE;
1351   if (keyframe_seek)
1352     flags |= GST_SEEK_FLAG_KEY_UNIT;
1353   if (loop_seek)
1354     flags |= GST_SEEK_FLAG_SEGMENT;
1355   if (skip_seek)
1356     flags |= GST_SEEK_FLAG_SKIP;
1357
1358   if (rate >= 0) {
1359     s_event = gst_event_new_seek (rate,
1360         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, real, GST_SEEK_TYPE_SET,
1361         GST_CLOCK_TIME_NONE);
1362     GST_DEBUG ("seek with rate %lf to %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT,
1363         rate, GST_TIME_ARGS (real), GST_TIME_ARGS (duration));
1364   } else {
1365     s_event = gst_event_new_seek (rate,
1366         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
1367         GST_SEEK_TYPE_SET, real);
1368     GST_DEBUG ("seek with rate %lf to %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT,
1369         rate, GST_TIME_ARGS (0), GST_TIME_ARGS (real));
1370   }
1371
1372   res = send_event (s_event);
1373
1374   if (res) {
1375     if (flush_seek) {
1376       gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL, SEEK_TIMEOUT);
1377     } else {
1378       set_update_scale (TRUE);
1379     }
1380   } else {
1381     g_print ("seek failed\n");
1382     set_update_scale (TRUE);
1383   }
1384 }
1385
1386 static void
1387 seek_cb (GtkWidget * widget)
1388 {
1389   /* If the timer hasn't expired yet, then the pipeline is running */
1390   if (play_scrub && seek_timeout_id != 0) {
1391     GST_DEBUG ("do scrub seek, PAUSED");
1392     gst_element_set_state (pipeline, GST_STATE_PAUSED);
1393   }
1394
1395   GST_DEBUG ("do seek");
1396   do_seek (widget);
1397
1398   if (play_scrub) {
1399     GST_DEBUG ("do scrub seek, PLAYING");
1400     gst_element_set_state (pipeline, GST_STATE_PLAYING);
1401
1402     if (seek_timeout_id == 0) {
1403       seek_timeout_id =
1404           g_timeout_add (SCRUB_TIME, (GSourceFunc) end_scrub, widget);
1405     }
1406   }
1407 }
1408
1409 static void
1410 set_update_fill (gboolean active)
1411 {
1412   GST_DEBUG ("fill scale is %d", active);
1413
1414   if (active) {
1415     if (fill_id == 0) {
1416       fill_id =
1417           g_timeout_add (FILL_INTERVAL, (GSourceFunc) update_fill, pipeline);
1418     }
1419   } else {
1420     if (fill_id) {
1421       g_source_remove (fill_id);
1422       fill_id = 0;
1423     }
1424   }
1425 }
1426
1427 static void
1428 set_update_scale (gboolean active)
1429 {
1430
1431   GST_DEBUG ("update scale is %d", active);
1432
1433   if (active) {
1434     if (update_id == 0) {
1435       update_id =
1436           g_timeout_add (UPDATE_INTERVAL, (GSourceFunc) update_scale, pipeline);
1437     }
1438   } else {
1439     if (update_id) {
1440       g_source_remove (update_id);
1441       update_id = 0;
1442     }
1443   }
1444 }
1445
1446 static gboolean
1447 start_seek (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
1448 {
1449   if (event->type != GDK_BUTTON_PRESS)
1450     return FALSE;
1451
1452   set_update_scale (FALSE);
1453
1454   if (state == GST_STATE_PLAYING && flush_seek && scrub) {
1455     GST_DEBUG ("start scrub seek, PAUSE");
1456     gst_element_set_state (pipeline, GST_STATE_PAUSED);
1457   }
1458
1459   if (changed_id == 0 && flush_seek && scrub) {
1460     changed_id =
1461         g_signal_connect (hscale, "value_changed", G_CALLBACK (seek_cb),
1462         pipeline);
1463   }
1464
1465   return FALSE;
1466 }
1467
1468 static gboolean
1469 stop_seek (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
1470 {
1471   if (changed_id) {
1472     g_signal_handler_disconnect (hscale, changed_id);
1473     changed_id = 0;
1474   }
1475
1476   if (!flush_seek || !scrub) {
1477     GST_DEBUG ("do final seek");
1478     do_seek (widget);
1479   }
1480
1481   if (seek_timeout_id != 0) {
1482     g_source_remove (seek_timeout_id);
1483     seek_timeout_id = 0;
1484     /* Still scrubbing, so the pipeline is playing, see if we need PAUSED
1485      * instead. */
1486     if (state == GST_STATE_PAUSED) {
1487       GST_DEBUG ("stop scrub seek, PAUSED");
1488       gst_element_set_state (pipeline, GST_STATE_PAUSED);
1489     }
1490   } else {
1491     if (state == GST_STATE_PLAYING) {
1492       GST_DEBUG ("stop scrub seek, PLAYING");
1493       gst_element_set_state (pipeline, GST_STATE_PLAYING);
1494     }
1495   }
1496
1497   return FALSE;
1498 }
1499
1500 static void
1501 play_cb (GtkButton * button, gpointer data)
1502 {
1503   GstStateChangeReturn ret;
1504
1505   if (state != GST_STATE_PLAYING) {
1506     g_print ("PLAY pipeline\n");
1507     gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
1508
1509     ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
1510     switch (ret) {
1511       case GST_STATE_CHANGE_FAILURE:
1512         goto failed;
1513       case GST_STATE_CHANGE_NO_PREROLL:
1514         is_live = TRUE;
1515         break;
1516       default:
1517         break;
1518     }
1519     state = GST_STATE_PLAYING;
1520     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Playing");
1521   }
1522
1523   return;
1524
1525 failed:
1526   {
1527     g_print ("PLAY failed\n");
1528     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Play failed");
1529   }
1530 }
1531
1532 static void
1533 pause_cb (GtkButton * button, gpointer data)
1534 {
1535   g_static_mutex_lock (&state_mutex);
1536   if (state != GST_STATE_PAUSED) {
1537     GstStateChangeReturn ret;
1538
1539     gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
1540     g_print ("PAUSE pipeline\n");
1541     ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
1542     switch (ret) {
1543       case GST_STATE_CHANGE_FAILURE:
1544         goto failed;
1545       case GST_STATE_CHANGE_NO_PREROLL:
1546         is_live = TRUE;
1547         break;
1548       default:
1549         break;
1550     }
1551
1552     state = GST_STATE_PAUSED;
1553     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Paused");
1554   }
1555   g_static_mutex_unlock (&state_mutex);
1556
1557   return;
1558
1559 failed:
1560   {
1561     g_static_mutex_unlock (&state_mutex);
1562     g_print ("PAUSE failed\n");
1563     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Pause failed");
1564   }
1565 }
1566
1567 static void
1568 stop_cb (GtkButton * button, gpointer data)
1569 {
1570   if (state != STOP_STATE) {
1571     GstStateChangeReturn ret;
1572     gint i;
1573
1574     g_print ("READY pipeline\n");
1575     gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
1576
1577     g_static_mutex_lock (&state_mutex);
1578     ret = gst_element_set_state (pipeline, STOP_STATE);
1579     if (ret == GST_STATE_CHANGE_FAILURE)
1580       goto failed;
1581
1582     state = STOP_STATE;
1583     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Stopped");
1584     gtk_widget_queue_draw (video_window);
1585
1586     is_live = FALSE;
1587     buffering = FALSE;
1588     set_update_scale (FALSE);
1589     set_scale (0.0);
1590     set_update_fill (FALSE);
1591
1592     if (pipeline_type == 16)
1593       clear_streams (pipeline);
1594     g_static_mutex_unlock (&state_mutex);
1595
1596 #if 0
1597     /* if one uses parse_launch, play, stop and play again it fails as all the
1598      * pads after the demuxer can't be reconnected
1599      */
1600     if (!strcmp (pipelines[pipeline_type].name, "parse-launch")) {
1601       gst_element_set_state (pipeline, GST_STATE_NULL);
1602       gst_object_unref (pipeline);
1603
1604       g_list_free (seekable_elements);
1605       seekable_elements = NULL;
1606       g_list_free (seekable_pads);
1607       seekable_pads = NULL;
1608       g_list_free (rate_pads);
1609       rate_pads = NULL;
1610
1611       pipeline = pipelines[pipeline_type].func (pipeline_spec);
1612       g_assert (pipeline);
1613       gst_element_set_state (pipeline, STOP_STATE);
1614       connect_bus_signals (pipeline);
1615     }
1616 #endif
1617     gtk_widget_set_sensitive (GTK_WIDGET (hscale), TRUE);
1618     for (i = 0; i < G_N_ELEMENTS (navigation_buttons); i++)
1619       gtk_widget_set_sensitive (navigation_buttons[i].button, FALSE);
1620   }
1621   return;
1622
1623 failed:
1624   {
1625     g_static_mutex_unlock (&state_mutex);
1626     g_print ("STOP failed\n");
1627     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Stop failed");
1628   }
1629 }
1630
1631 static void
1632 accurate_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1633 {
1634   accurate_seek = gtk_toggle_button_get_active (button);
1635 }
1636
1637 static void
1638 key_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1639 {
1640   keyframe_seek = gtk_toggle_button_get_active (button);
1641 }
1642
1643 static void
1644 loop_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1645 {
1646   loop_seek = gtk_toggle_button_get_active (button);
1647   if (state == GST_STATE_PLAYING) {
1648     do_seek (hscale);
1649   }
1650 }
1651
1652 static void
1653 flush_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1654 {
1655   flush_seek = gtk_toggle_button_get_active (button);
1656 }
1657
1658 static void
1659 scrub_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1660 {
1661   scrub = gtk_toggle_button_get_active (button);
1662 }
1663
1664 static void
1665 play_scrub_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1666 {
1667   play_scrub = gtk_toggle_button_get_active (button);
1668 }
1669
1670 static void
1671 skip_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1672 {
1673   skip_seek = gtk_toggle_button_get_active (button);
1674   if (state == GST_STATE_PLAYING) {
1675     do_seek (hscale);
1676   }
1677 }
1678
1679 static void
1680 rate_spinbutton_changed_cb (GtkSpinButton * button, GstPipeline * pipeline)
1681 {
1682   gboolean res = FALSE;
1683   GstEvent *s_event;
1684   GstSeekFlags flags;
1685
1686   rate = gtk_spin_button_get_value (button);
1687
1688   GST_DEBUG ("rate changed to %lf", rate);
1689
1690   flags = 0;
1691   if (flush_seek)
1692     flags |= GST_SEEK_FLAG_FLUSH;
1693   if (loop_seek)
1694     flags |= GST_SEEK_FLAG_SEGMENT;
1695   if (accurate_seek)
1696     flags |= GST_SEEK_FLAG_ACCURATE;
1697   if (keyframe_seek)
1698     flags |= GST_SEEK_FLAG_KEY_UNIT;
1699   if (skip_seek)
1700     flags |= GST_SEEK_FLAG_SKIP;
1701
1702   if (rate >= 0.0) {
1703     s_event = gst_event_new_seek (rate,
1704         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, position,
1705         GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE);
1706   } else {
1707     s_event = gst_event_new_seek (rate,
1708         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
1709         GST_SEEK_TYPE_SET, position);
1710   }
1711
1712   res = send_event (s_event);
1713
1714   if (res) {
1715     if (flush_seek) {
1716       gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL, SEEK_TIMEOUT);
1717     }
1718   } else
1719     g_print ("seek failed\n");
1720 }
1721
1722 static void
1723 update_flag (GstPipeline * pipeline, gint num, gboolean state)
1724 {
1725   gint flags;
1726
1727   g_object_get (pipeline, "flags", &flags, NULL);
1728   if (state)
1729     flags |= (1 << num);
1730   else
1731     flags &= ~(1 << num);
1732   g_object_set (pipeline, "flags", flags, NULL);
1733 }
1734
1735 static void
1736 vis_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1737 {
1738   gboolean state;
1739
1740   state = gtk_toggle_button_get_active (button);
1741   update_flag (pipeline, 3, state);
1742   gtk_widget_set_sensitive (vis_combo, state);
1743 }
1744
1745 static void
1746 audio_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1747 {
1748   gboolean state;
1749
1750   state = gtk_toggle_button_get_active (button);
1751   update_flag (pipeline, 1, state);
1752   gtk_widget_set_sensitive (audio_combo, state);
1753 }
1754
1755 static void
1756 video_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1757 {
1758   gboolean state;
1759
1760   state = gtk_toggle_button_get_active (button);
1761   update_flag (pipeline, 0, state);
1762   gtk_widget_set_sensitive (video_combo, state);
1763 }
1764
1765 static void
1766 text_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1767 {
1768   gboolean state;
1769
1770   state = gtk_toggle_button_get_active (button);
1771   update_flag (pipeline, 2, state);
1772   gtk_widget_set_sensitive (text_combo, state);
1773 }
1774
1775 static void
1776 mute_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1777 {
1778   gboolean mute;
1779
1780   mute = gtk_toggle_button_get_active (button);
1781   g_object_set (pipeline, "mute", mute, NULL);
1782 }
1783
1784 static void
1785 download_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1786 {
1787   gboolean state;
1788
1789   state = gtk_toggle_button_get_active (button);
1790   update_flag (pipeline, 7, state);
1791 }
1792
1793 static void
1794 buffer_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1795 {
1796   gboolean state;
1797
1798   state = gtk_toggle_button_get_active (button);
1799   update_flag (pipeline, 8, state);
1800 }
1801
1802 static void
1803 clear_streams (GstElement * pipeline)
1804 {
1805   gint i;
1806
1807   /* remove previous info */
1808   for (i = 0; i < n_video; i++)
1809     gtk_combo_box_text_remove (GTK_COMBO_BOX_TEXT (video_combo), 0);
1810   for (i = 0; i < n_audio; i++)
1811     gtk_combo_box_text_remove (GTK_COMBO_BOX_TEXT (audio_combo), 0);
1812   for (i = 0; i < n_text; i++)
1813     gtk_combo_box_text_remove (GTK_COMBO_BOX_TEXT (text_combo), 0);
1814
1815   n_audio = n_video = n_text = 0;
1816   gtk_widget_set_sensitive (video_combo, FALSE);
1817   gtk_widget_set_sensitive (audio_combo, FALSE);
1818   gtk_widget_set_sensitive (text_combo, FALSE);
1819
1820   need_streams = TRUE;
1821 }
1822
1823 static void
1824 update_streams (GstPipeline * pipeline)
1825 {
1826   gint i;
1827
1828   if (pipeline_type == 16 && need_streams) {
1829     GstTagList *tags;
1830     gchar *name, *str;
1831     gint active_idx;
1832     gboolean state;
1833
1834     /* remove previous info */
1835     clear_streams (GST_ELEMENT_CAST (pipeline));
1836
1837     /* here we get and update the different streams detected by playbin2 */
1838     g_object_get (pipeline, "n-video", &n_video, NULL);
1839     g_object_get (pipeline, "n-audio", &n_audio, NULL);
1840     g_object_get (pipeline, "n-text", &n_text, NULL);
1841
1842     g_print ("video %d, audio %d, text %d\n", n_video, n_audio, n_text);
1843
1844     active_idx = 0;
1845     for (i = 0; i < n_video; i++) {
1846       g_signal_emit_by_name (pipeline, "get-video-tags", i, &tags);
1847       if (tags) {
1848         str = gst_structure_to_string ((GstStructure *) tags);
1849         g_print ("video %d: %s\n", i, str);
1850         g_free (str);
1851       }
1852       /* find good name for the label */
1853       name = g_strdup_printf ("video %d", i + 1);
1854       gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (video_combo), name);
1855       g_free (name);
1856     }
1857     state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (video_checkbox));
1858     gtk_widget_set_sensitive (video_combo, state && n_video > 0);
1859     gtk_combo_box_set_active (GTK_COMBO_BOX (video_combo), active_idx);
1860
1861     active_idx = 0;
1862     for (i = 0; i < n_audio; i++) {
1863       g_signal_emit_by_name (pipeline, "get-audio-tags", i, &tags);
1864       if (tags) {
1865         str = gst_structure_to_string ((GstStructure *) tags);
1866         g_print ("audio %d: %s\n", i, str);
1867         g_free (str);
1868       }
1869       /* find good name for the label */
1870       name = g_strdup_printf ("audio %d", i + 1);
1871       gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (audio_combo), name);
1872       g_free (name);
1873     }
1874     state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (audio_checkbox));
1875     gtk_widget_set_sensitive (audio_combo, state && n_audio > 0);
1876     gtk_combo_box_set_active (GTK_COMBO_BOX (audio_combo), active_idx);
1877
1878     active_idx = 0;
1879     for (i = 0; i < n_text; i++) {
1880       g_signal_emit_by_name (pipeline, "get-text-tags", i, &tags);
1881
1882       name = NULL;
1883       if (tags) {
1884         const GValue *value;
1885
1886         str = gst_structure_to_string ((GstStructure *) tags);
1887         g_print ("text %d: %s\n", i, str);
1888         g_free (str);
1889
1890         /* get the language code if we can */
1891         value = gst_tag_list_get_value_index (tags, GST_TAG_LANGUAGE_CODE, 0);
1892         if (value && G_VALUE_HOLDS_STRING (value)) {
1893           name = g_strdup_printf ("text %s", g_value_get_string (value));
1894         }
1895       }
1896       /* find good name for the label if we didn't use a tag */
1897       if (name == NULL)
1898         name = g_strdup_printf ("text %d", i + 1);
1899
1900       gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (text_combo), name);
1901       g_free (name);
1902     }
1903     state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (text_checkbox));
1904     gtk_widget_set_sensitive (text_combo, state && n_text > 0);
1905     gtk_combo_box_set_active (GTK_COMBO_BOX (text_combo), active_idx);
1906
1907     need_streams = FALSE;
1908   }
1909 }
1910
1911 static void
1912 video_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1913 {
1914   gint active;
1915
1916   active = gtk_combo_box_get_active (combo);
1917
1918   g_print ("setting current video track %d\n", active);
1919   g_object_set (pipeline, "current-video", active, NULL);
1920 }
1921
1922 static void
1923 audio_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1924 {
1925   gint active;
1926
1927   active = gtk_combo_box_get_active (combo);
1928
1929   g_print ("setting current audio track %d\n", active);
1930   g_object_set (pipeline, "current-audio", active, NULL);
1931 }
1932
1933 static void
1934 text_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1935 {
1936   gint active;
1937
1938   active = gtk_combo_box_get_active (combo);
1939
1940   g_print ("setting current text track %d\n", active);
1941   g_object_set (pipeline, "current-text", active, NULL);
1942 }
1943
1944 static gboolean
1945 filter_features (GstPluginFeature * feature, gpointer data)
1946 {
1947   GstElementFactory *f;
1948
1949   if (!GST_IS_ELEMENT_FACTORY (feature))
1950     return FALSE;
1951   f = GST_ELEMENT_FACTORY (feature);
1952   if (!g_strrstr (gst_element_factory_get_klass (f), "Visualization"))
1953     return FALSE;
1954
1955   return TRUE;
1956 }
1957
1958 static void
1959 init_visualization_features (void)
1960 {
1961   GList *list, *walk;
1962
1963   vis_entries = g_array_new (FALSE, FALSE, sizeof (VisEntry));
1964
1965   list = gst_registry_feature_filter (gst_registry_get_default (),
1966       filter_features, FALSE, NULL);
1967
1968   for (walk = list; walk; walk = g_list_next (walk)) {
1969     VisEntry entry;
1970     const gchar *name;
1971
1972     entry.factory = GST_ELEMENT_FACTORY (walk->data);
1973     name = gst_element_factory_get_longname (entry.factory);
1974
1975     g_array_append_val (vis_entries, entry);
1976     gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (vis_combo), name);
1977   }
1978   gtk_combo_box_set_active (GTK_COMBO_BOX (vis_combo), 0);
1979 }
1980
1981 static void
1982 vis_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1983 {
1984   guint index;
1985   VisEntry *entry;
1986   GstElement *element;
1987
1988   /* get the selected index and get the factory for this index */
1989   index = gtk_combo_box_get_active (GTK_COMBO_BOX (vis_combo));
1990   if (vis_entries->len > 0) {
1991     entry = &g_array_index (vis_entries, VisEntry, index);
1992
1993     /* create an instance of the element from the factory */
1994     element = gst_element_factory_create (entry->factory, NULL);
1995     if (!element)
1996       return;
1997
1998     /* set vis plugin for playbin2 */
1999     g_object_set (pipeline, "vis-plugin", element, NULL);
2000   }
2001 }
2002
2003 static void
2004 volume_spinbutton_changed_cb (GtkSpinButton * button, GstPipeline * pipeline)
2005 {
2006   gdouble volume;
2007
2008   volume = gtk_spin_button_get_value (button);
2009
2010   g_object_set (pipeline, "volume", volume, NULL);
2011 }
2012
2013 static void
2014 volume_notify_cb (GstElement * pipeline, GParamSpec * arg, gpointer user_dat)
2015 {
2016   gdouble cur_volume, new_volume;
2017
2018   g_object_get (pipeline, "volume", &new_volume, NULL);
2019   cur_volume = gtk_spin_button_get_value (GTK_SPIN_BUTTON (volume_spinbutton));
2020   if (fabs (cur_volume - new_volume) > 0.001) {
2021     g_signal_handlers_block_by_func (volume_spinbutton,
2022         volume_spinbutton_changed_cb, pipeline);
2023     gtk_spin_button_set_value (GTK_SPIN_BUTTON (volume_spinbutton), new_volume);
2024     g_signal_handlers_unblock_by_func (volume_spinbutton,
2025         volume_spinbutton_changed_cb, pipeline);
2026   }
2027 }
2028
2029 static void
2030 shot_cb (GtkButton * button, gpointer data)
2031 {
2032   GstBuffer *buffer;
2033   GstCaps *caps;
2034
2035   /* convert to our desired format (RGB24) */
2036   caps = gst_caps_new_simple ("video/x-raw-rgb",
2037       "bpp", G_TYPE_INT, 24, "depth", G_TYPE_INT, 24,
2038       /* Note: we don't ask for a specific width/height here, so that
2039        * videoscale can adjust dimensions from a non-1/1 pixel aspect
2040        * ratio to a 1/1 pixel-aspect-ratio */
2041       "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
2042       "endianness", G_TYPE_INT, G_BIG_ENDIAN,
2043       "red_mask", G_TYPE_INT, 0xff0000,
2044       "green_mask", G_TYPE_INT, 0x00ff00,
2045       "blue_mask", G_TYPE_INT, 0x0000ff, NULL);
2046
2047   /* convert the latest frame to the requested format */
2048   g_signal_emit_by_name (pipeline, "convert-frame", caps, &buffer);
2049   gst_caps_unref (caps);
2050
2051   if (buffer) {
2052     GstCaps *caps;
2053     GstStructure *s;
2054     gboolean res;
2055     gint width, height;
2056     GdkPixbuf *pixbuf;
2057     GError *error = NULL;
2058
2059     /* get the snapshot buffer format now. We set the caps on the appsink so
2060      * that it can only be an rgb buffer. The only thing we have not specified
2061      * on the caps is the height, which is dependant on the pixel-aspect-ratio
2062      * of the source material */
2063     caps = GST_BUFFER_CAPS (buffer);
2064     if (!caps) {
2065       g_warning ("could not get snapshot format\n");
2066       goto done;
2067     }
2068     s = gst_caps_get_structure (caps, 0);
2069
2070     /* we need to get the final caps on the buffer to get the size */
2071     res = gst_structure_get_int (s, "width", &width);
2072     res |= gst_structure_get_int (s, "height", &height);
2073     if (!res) {
2074       g_warning ("could not get snapshot dimension\n");
2075       goto done;
2076     }
2077
2078     /* create pixmap from buffer and save, gstreamer video buffers have a stride
2079      * that is rounded up to the nearest multiple of 4 */
2080     pixbuf = gdk_pixbuf_new_from_data (GST_BUFFER_DATA (buffer),
2081         GDK_COLORSPACE_RGB, FALSE, 8, width, height,
2082         GST_ROUND_UP_4 (width * 3), NULL, NULL);
2083
2084     /* save the pixbuf */
2085     gdk_pixbuf_save (pixbuf, "snapshot.png", "png", &error, NULL);
2086
2087   done:
2088     gst_buffer_unref (buffer);
2089   }
2090 }
2091
2092 /* called when the Step button is pressed */
2093 static void
2094 step_cb (GtkButton * button, gpointer data)
2095 {
2096   GstEvent *event;
2097   GstFormat format;
2098   guint64 amount;
2099   gdouble rate;
2100   gboolean flush, res;
2101   gint active;
2102
2103   active = gtk_combo_box_get_active (GTK_COMBO_BOX (format_combo));
2104   amount =
2105       gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
2106       (step_amount_spinbutton));
2107   rate = gtk_spin_button_get_value (GTK_SPIN_BUTTON (step_rate_spinbutton));
2108   flush = TRUE;
2109
2110   switch (active) {
2111     case 0:
2112       format = GST_FORMAT_BUFFERS;
2113       break;
2114     case 1:
2115       format = GST_FORMAT_TIME;
2116       amount *= GST_MSECOND;
2117       break;
2118     default:
2119       format = GST_FORMAT_UNDEFINED;
2120       break;
2121   }
2122
2123   event = gst_event_new_step (format, amount, rate, flush, FALSE);
2124
2125   res = send_event (event);
2126
2127   if (!res) {
2128     g_print ("Sending step event failed\n");
2129   }
2130 }
2131
2132 static void
2133 message_received (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
2134 {
2135   const GstStructure *s;
2136
2137   switch (GST_MESSAGE_TYPE (message)) {
2138     case GST_MESSAGE_ERROR:
2139       GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
2140           GST_DEBUG_GRAPH_SHOW_ALL, "seek.error");
2141       break;
2142     case GST_MESSAGE_WARNING:
2143       GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
2144           GST_DEBUG_GRAPH_SHOW_ALL, "seek.warning");
2145       break;
2146     default:
2147       break;
2148   }
2149
2150   s = gst_message_get_structure (message);
2151   g_print ("message from \"%s\" (%s): ",
2152       GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
2153       gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
2154   if (s) {
2155     gchar *sstr;
2156
2157     sstr = gst_structure_to_string (s);
2158     g_print ("%s\n", sstr);
2159     g_free (sstr);
2160   } else {
2161     g_print ("no message details\n");
2162   }
2163 }
2164
2165 static gboolean shuttling = FALSE;
2166 static gdouble shuttle_rate = 0.0;
2167 static gdouble play_rate = 1.0;
2168
2169 static void
2170 do_shuttle (GstElement * element)
2171 {
2172   guint64 duration;
2173
2174   if (shuttling)
2175     duration = 40 * GST_MSECOND;
2176   else
2177     duration = -1;
2178
2179   gst_element_send_event (element,
2180       gst_event_new_step (GST_FORMAT_TIME, duration, shuttle_rate, FALSE,
2181           FALSE));
2182 }
2183
2184 static void
2185 msg_sync_step_done (GstBus * bus, GstMessage * message, GstElement * element)
2186 {
2187   GstFormat format;
2188   guint64 amount;
2189   gdouble rate;
2190   gboolean flush;
2191   gboolean intermediate;
2192   guint64 duration;
2193   gboolean eos;
2194
2195   gst_message_parse_step_done (message, &format, &amount, &rate, &flush,
2196       &intermediate, &duration, &eos);
2197
2198   if (eos) {
2199     g_print ("stepped till EOS\n");
2200     return;
2201   }
2202
2203   if (g_static_mutex_trylock (&state_mutex)) {
2204     if (shuttling)
2205       do_shuttle (element);
2206     g_static_mutex_unlock (&state_mutex);
2207   } else {
2208     /* ignore step messages that come while we are doing a state change */
2209     g_print ("state change is busy\n");
2210   }
2211 }
2212
2213 static void
2214 shuttle_toggled (GtkToggleButton * button, GstElement * element)
2215 {
2216   gboolean active;
2217
2218   active = gtk_toggle_button_get_active (button);
2219
2220   if (active != shuttling) {
2221     shuttling = active;
2222     g_print ("shuttling %s\n", shuttling ? "active" : "inactive");
2223     if (active) {
2224       shuttle_rate = 0.0;
2225       play_rate = 1.0;
2226       pause_cb (NULL, NULL);
2227       gst_element_get_state (element, NULL, NULL, -1);
2228     }
2229   }
2230 }
2231
2232 static void
2233 shuttle_rate_switch (GstElement * element)
2234 {
2235   GstSeekFlags flags;
2236   GstEvent *s_event;
2237   gboolean res;
2238
2239   if (state == GST_STATE_PLAYING) {
2240     /* pause when we need to */
2241     pause_cb (NULL, NULL);
2242     gst_element_get_state (element, NULL, NULL, -1);
2243   }
2244
2245   if (play_rate == 1.0)
2246     play_rate = -1.0;
2247   else
2248     play_rate = 1.0;
2249
2250   g_print ("rate changed to %lf %" GST_TIME_FORMAT "\n", play_rate,
2251       GST_TIME_ARGS (position));
2252
2253   flags = GST_SEEK_FLAG_FLUSH;
2254   flags |= GST_SEEK_FLAG_ACCURATE;
2255
2256   if (play_rate >= 0.0) {
2257     s_event = gst_event_new_seek (play_rate,
2258         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, position,
2259         GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE);
2260   } else {
2261     s_event = gst_event_new_seek (play_rate,
2262         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
2263         GST_SEEK_TYPE_SET, position);
2264   }
2265   res = send_event (s_event);
2266   if (res) {
2267     gst_element_get_state (element, NULL, NULL, SEEK_TIMEOUT);
2268   } else {
2269     g_print ("seek failed\n");
2270   }
2271 }
2272
2273 static void
2274 shuttle_value_changed (GtkRange * range, GstElement * element)
2275 {
2276   gdouble rate;
2277
2278   rate = gtk_adjustment_get_value (shuttle_adjustment);
2279
2280   if (rate == 0.0) {
2281     g_print ("rate 0.0, pause\n");
2282     pause_cb (NULL, NULL);
2283     gst_element_get_state (element, NULL, NULL, -1);
2284   } else {
2285     g_print ("rate changed %0.3g\n", rate);
2286
2287     if ((rate < 0.0 && play_rate > 0.0) || (rate > 0.0 && play_rate < 0.0)) {
2288       shuttle_rate_switch (element);
2289     }
2290
2291     shuttle_rate = ABS (rate);
2292     if (state != GST_STATE_PLAYING) {
2293       do_shuttle (element);
2294       play_cb (NULL, NULL);
2295     }
2296   }
2297 }
2298
2299 static void
2300 msg_async_done (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
2301 {
2302   GST_DEBUG ("async done");
2303   /* when we get ASYNC_DONE we can query position, duration and other
2304    * properties */
2305   update_scale (pipeline);
2306
2307   /* update the available streams */
2308   update_streams (pipeline);
2309
2310   find_navigation_element ();
2311 }
2312
2313 static void
2314 msg_state_changed (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
2315 {
2316   const GstStructure *s;
2317
2318   s = gst_message_get_structure (message);
2319
2320   /* We only care about state changed on the pipeline */
2321   if (s && GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
2322     GstState old, new, pending;
2323
2324     gst_message_parse_state_changed (message, &old, &new, &pending);
2325
2326     /* When state of the pipeline changes to paused or playing we start updating scale */
2327     if (new == GST_STATE_PLAYING) {
2328       set_update_scale (TRUE);
2329     } else {
2330       set_update_scale (FALSE);
2331     }
2332
2333     /* dump graph for (some) pipeline state changes */
2334     {
2335       gchar *dump_name;
2336
2337       dump_name = g_strdup_printf ("seek.%s_%s",
2338           gst_element_state_get_name (old), gst_element_state_get_name (new));
2339
2340       GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
2341           GST_DEBUG_GRAPH_SHOW_ALL, dump_name);
2342
2343       g_free (dump_name);
2344     }
2345   }
2346 }
2347
2348 static void
2349 msg_segment_done (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
2350 {
2351   GstEvent *s_event;
2352   GstSeekFlags flags;
2353   gboolean res;
2354   GstFormat format;
2355
2356   GST_DEBUG ("position is %" GST_TIME_FORMAT, GST_TIME_ARGS (position));
2357   gst_message_parse_segment_done (message, &format, &position);
2358   GST_DEBUG ("end of segment at %" GST_TIME_FORMAT, GST_TIME_ARGS (position));
2359
2360   flags = 0;
2361   /* in the segment-done callback we never flush as this would not make sense
2362    * for seamless playback. */
2363   if (loop_seek)
2364     flags |= GST_SEEK_FLAG_SEGMENT;
2365   if (skip_seek)
2366     flags |= GST_SEEK_FLAG_SKIP;
2367
2368   s_event = gst_event_new_seek (rate,
2369       GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
2370       GST_SEEK_TYPE_SET, duration);
2371
2372   GST_DEBUG ("restart loop with rate %lf to 0 / %" GST_TIME_FORMAT,
2373       rate, GST_TIME_ARGS (duration));
2374
2375   res = send_event (s_event);
2376   if (!res)
2377     g_print ("segment seek failed\n");
2378 }
2379
2380 /* in stream buffering mode we PAUSE the pipeline until we receive a 100%
2381  * message */
2382 static void
2383 do_stream_buffering (gint percent)
2384 {
2385   gchar *bufstr;
2386
2387   gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
2388   bufstr = g_strdup_printf ("Buffering...%d", percent);
2389   gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, bufstr);
2390   g_free (bufstr);
2391
2392   if (percent == 100) {
2393     /* a 100% message means buffering is done */
2394     buffering = FALSE;
2395     /* if the desired state is playing, go back */
2396     if (state == GST_STATE_PLAYING) {
2397       /* no state management needed for live pipelines */
2398       if (!is_live) {
2399         fprintf (stderr, "Done buffering, setting pipeline to PLAYING ...\n");
2400         gst_element_set_state (pipeline, GST_STATE_PLAYING);
2401       }
2402       gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
2403       gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Playing");
2404     }
2405   } else {
2406     /* buffering busy */
2407     if (buffering == FALSE && state == GST_STATE_PLAYING) {
2408       /* we were not buffering but PLAYING, PAUSE  the pipeline. */
2409       if (!is_live) {
2410         fprintf (stderr, "Buffering, setting pipeline to PAUSED ...\n");
2411         gst_element_set_state (pipeline, GST_STATE_PAUSED);
2412       }
2413     }
2414     buffering = TRUE;
2415   }
2416 }
2417
2418 static void
2419 do_download_buffering (gint percent)
2420 {
2421   if (!buffering && percent < 100) {
2422     gchar *bufstr;
2423
2424     buffering = TRUE;
2425
2426     bufstr = g_strdup_printf ("Downloading...");
2427     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, bufstr);
2428     g_free (bufstr);
2429
2430     /* once we get a buffering message, we'll do the fill update */
2431     set_update_fill (TRUE);
2432
2433     if (state == GST_STATE_PLAYING && !is_live) {
2434       fprintf (stderr, "Downloading, setting pipeline to PAUSED ...\n");
2435       gst_element_set_state (pipeline, GST_STATE_PAUSED);
2436       /* user has to manually start the playback */
2437       state = GST_STATE_PAUSED;
2438     }
2439   }
2440 }
2441
2442 static void
2443 msg_buffering (GstBus * bus, GstMessage * message, GstPipeline * data)
2444 {
2445   gint percent;
2446
2447   gst_message_parse_buffering (message, &percent);
2448
2449   /* get more stats */
2450   gst_message_parse_buffering_stats (message, &mode, NULL, NULL,
2451       &buffering_left);
2452
2453   switch (mode) {
2454     case GST_BUFFERING_DOWNLOAD:
2455       do_download_buffering (percent);
2456       break;
2457     case GST_BUFFERING_LIVE:
2458       is_live = TRUE;
2459     case GST_BUFFERING_TIMESHIFT:
2460     case GST_BUFFERING_STREAM:
2461       do_stream_buffering (percent);
2462       break;
2463   }
2464 }
2465
2466 static void
2467 msg_clock_lost (GstBus * bus, GstMessage * message, GstPipeline * data)
2468 {
2469   g_print ("clock lost! PAUSE and PLAY to select a new clock\n");
2470   if (state == GST_STATE_PLAYING) {
2471     gst_element_set_state (pipeline, GST_STATE_PAUSED);
2472     gst_element_set_state (pipeline, GST_STATE_PLAYING);
2473   }
2474 }
2475
2476 static GstElement *navigation_element = NULL;
2477
2478 static void
2479 find_navigation_element (void)
2480 {
2481   GstElement *video_sink;
2482
2483   g_object_get (pipeline, "video-sink", &video_sink, NULL);
2484   if (!video_sink)
2485     return;
2486
2487   if (navigation_element)
2488     gst_object_unref (navigation_element);
2489
2490   if (GST_IS_NAVIGATION (video_sink)) {
2491     navigation_element = gst_object_ref (video_sink);
2492   } else if (GST_IS_BIN (video_sink)) {
2493     navigation_element =
2494         gst_bin_get_by_interface (GST_BIN (video_sink), GST_TYPE_NAVIGATION);
2495   } else {
2496     navigation_element = NULL;
2497   }
2498
2499   gst_object_unref (video_sink);
2500 }
2501
2502 /* called when Navigation command button is pressed */
2503 static void
2504 navigation_cmd_cb (GtkButton * button, gpointer data)
2505 {
2506   GstNavigationCommand cmd = GPOINTER_TO_INT (data);
2507
2508   if (!navigation_element) {
2509     find_navigation_element ();
2510     if (!navigation_element)
2511       return;
2512   }
2513
2514   gst_navigation_send_command (GST_NAVIGATION (navigation_element), cmd);
2515 }
2516
2517 #if defined (GDK_WINDOWING_X11) || defined (GDK_WINDOWING_WIN32) || defined (GDK_WINDOWING_QUARTZ)
2518
2519 static GstElement *xoverlay_element = NULL;
2520 static guintptr embed_xid = 0;
2521
2522 /* We set the xid here in response to the prepare-xwindow-id message via a
2523  * bus sync handler because we don't know the actual videosink used from the
2524  * start (as we don't know the pipeline, or bin elements such as autovideosink
2525  * or gconfvideosink may be used which create the actual videosink only once
2526  * the pipeline is started) */
2527 static GstBusSyncReply
2528 bus_sync_handler (GstBus * bus, GstMessage * message, GstPipeline * data)
2529 {
2530   if ((GST_MESSAGE_TYPE (message) == GST_MESSAGE_ELEMENT) &&
2531       gst_structure_has_name (message->structure, "prepare-xwindow-id")) {
2532     GstElement *element = GST_ELEMENT (GST_MESSAGE_SRC (message));
2533
2534     if (xoverlay_element)
2535       gst_object_unref (xoverlay_element);
2536     xoverlay_element = GST_ELEMENT (gst_object_ref (element));
2537
2538     g_print ("got prepare-xwindow-id, setting XID %lu\n", embed_xid);
2539
2540     if (g_object_class_find_property (G_OBJECT_GET_CLASS (element),
2541             "force-aspect-ratio")) {
2542       g_object_set (element, "force-aspect-ratio", TRUE, NULL);
2543     }
2544
2545     /* Should have been initialised from main thread before (can't use
2546      * GDK_WINDOW_XID here with Gtk+ >= 2.18, because the sync handler will
2547      * be called from a streaming thread and GDK_WINDOW_XID maps to more than
2548      * a simple structure lookup with Gtk+ >= 2.18, where 'more' is stuff that
2549      * shouldn't be done from a non-GUI thread without explicit locking).  */
2550     g_assert (embed_xid != 0);
2551
2552     gst_x_overlay_set_window_handle (GST_X_OVERLAY (element), embed_xid);
2553     gst_x_overlay_handle_events (GST_X_OVERLAY (element), FALSE);
2554
2555     find_navigation_element ();
2556   }
2557   return GST_BUS_PASS;
2558 }
2559 #endif
2560
2561 static gboolean
2562 draw_cb (GtkWidget * widget, cairo_t * cr, gpointer data)
2563 {
2564   if (state < GST_STATE_PAUSED) {
2565     int width, height;
2566
2567     width = gtk_widget_get_allocated_width (widget);
2568     height = gtk_widget_get_allocated_height (widget);
2569     cairo_set_source_rgb (cr, 0, 0, 0);
2570     cairo_rectangle (cr, 0, 0, width, height);
2571     cairo_fill (cr);
2572     return TRUE;
2573   }
2574
2575   if (xoverlay_element)
2576     gst_x_overlay_expose (GST_X_OVERLAY (xoverlay_element));
2577
2578   return FALSE;
2579 }
2580
2581 static void
2582 realize_cb (GtkWidget * widget, gpointer data)
2583 {
2584   GdkWindow *window = gtk_widget_get_window (widget);
2585
2586   /* This is here just for pedagogical purposes, GDK_WINDOW_XID will call it
2587    * as well */
2588   if (!gdk_window_ensure_native (window))
2589     g_error ("Couldn't create native window needed for GstXOverlay!");
2590
2591 #if defined (GDK_WINDOWING_WIN32)
2592   embed_xid = GDK_WINDOW_HWND (window);
2593   g_print ("Window realize: video window HWND = %lu\n", embed_xid);
2594 #elif defined (GDK_WINDOWING_QUARTZ)
2595   embed_xid = gdk_quartz_window_get_nsview (window);
2596   g_print ("Window realize: video window NSView = %p\n", embed_xid);
2597 #elif defined (GDK_WINDOWING_X11)
2598   embed_xid = GDK_WINDOW_XID (window);
2599   g_print ("Window realize: video window XID = %lu\n", embed_xid);
2600 #endif
2601 }
2602
2603 static gboolean
2604 button_press_cb (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
2605 {
2606   gtk_widget_grab_focus (widget);
2607
2608   if (navigation_element)
2609     gst_navigation_send_mouse_event (GST_NAVIGATION (navigation_element),
2610         "mouse-button-press", event->button, event->x, event->y);
2611
2612   return FALSE;
2613 }
2614
2615 static gboolean
2616 button_release_cb (GtkWidget * widget, GdkEventButton * event,
2617     gpointer user_data)
2618 {
2619   if (navigation_element)
2620     gst_navigation_send_mouse_event (GST_NAVIGATION (navigation_element),
2621         "mouse-button-release", event->button, event->x, event->y);
2622
2623   return FALSE;
2624 }
2625
2626 static gboolean
2627 key_press_cb (GtkWidget * widget, GdkEventKey * event, gpointer user_data)
2628 {
2629   if (navigation_element)
2630     gst_navigation_send_key_event (GST_NAVIGATION (navigation_element),
2631         "key-press", gdk_keyval_name (event->keyval));
2632
2633   return FALSE;
2634 }
2635
2636 static gboolean
2637 key_release_cb (GtkWidget * widget, GdkEventKey * event, gpointer user_data)
2638 {
2639   if (navigation_element)
2640     gst_navigation_send_key_event (GST_NAVIGATION (navigation_element),
2641         "key-release", gdk_keyval_name (event->keyval));
2642
2643   return FALSE;
2644 }
2645
2646 static gboolean
2647 motion_notify_cb (GtkWidget * widget, GdkEventMotion * event,
2648     gpointer user_data)
2649 {
2650   if (navigation_element)
2651     gst_navigation_send_mouse_event (GST_NAVIGATION (navigation_element),
2652         "mouse-move", 0, event->x, event->y);
2653
2654   return FALSE;
2655 }
2656
2657 static void
2658 msg_eos (GstBus * bus, GstMessage * message, GstPipeline * data)
2659 {
2660   message_received (bus, message, data);
2661
2662   /* Set new uri for playerbins and continue playback */
2663   if (l && (pipeline_type == 14 || pipeline_type == 16)) {
2664     stop_cb (NULL, NULL);
2665     l = g_list_next (l);
2666     if (l) {
2667       playerbin_set_uri (GST_ELEMENT (data), l->data);
2668       play_cb (NULL, NULL);
2669     }
2670   }
2671 }
2672
2673 static void
2674 msg_step_done (GstBus * bus, GstMessage * message, GstPipeline * data)
2675 {
2676   if (!shuttling)
2677     message_received (bus, message, data);
2678 }
2679
2680 static void
2681 msg (GstBus * bus, GstMessage * message, GstPipeline * data)
2682 {
2683   GstNavigationMessageType nav_type;
2684
2685   nav_type = gst_navigation_message_get_type (message);
2686   switch (nav_type) {
2687     case GST_NAVIGATION_MESSAGE_COMMANDS_CHANGED:{
2688       GstQuery *query;
2689       gboolean res, j;
2690
2691       /* Heuristic to detect if we're dealing with a DVD menu */
2692       query = gst_navigation_query_new_commands ();
2693       res = gst_element_query (GST_ELEMENT (GST_MESSAGE_SRC (message)), query);
2694
2695       for (j = 0; j < G_N_ELEMENTS (navigation_buttons); j++)
2696         gtk_widget_set_sensitive (navigation_buttons[j].button, FALSE);
2697
2698       if (res) {
2699         gboolean is_menu = FALSE;
2700         guint i, n;
2701
2702         if (gst_navigation_query_parse_commands_length (query, &n)) {
2703           for (i = 0; i < n; i++) {
2704             GstNavigationCommand cmd;
2705
2706             if (!gst_navigation_query_parse_commands_nth (query, i, &cmd))
2707               break;
2708
2709             is_menu |= (cmd == GST_NAVIGATION_COMMAND_ACTIVATE);
2710             is_menu |= (cmd == GST_NAVIGATION_COMMAND_LEFT);
2711             is_menu |= (cmd == GST_NAVIGATION_COMMAND_RIGHT);
2712             is_menu |= (cmd == GST_NAVIGATION_COMMAND_UP);
2713             is_menu |= (cmd == GST_NAVIGATION_COMMAND_DOWN);
2714
2715             for (j = 0; j < G_N_ELEMENTS (navigation_buttons); j++) {
2716               if (navigation_buttons[j].cmd != cmd)
2717                 continue;
2718
2719               gtk_widget_set_sensitive (navigation_buttons[j].button, TRUE);
2720             }
2721           }
2722         }
2723
2724         gtk_widget_set_sensitive (GTK_WIDGET (hscale), !is_menu);
2725       } else {
2726         g_assert_not_reached ();
2727       }
2728
2729       gst_query_unref (query);
2730       break;
2731     }
2732     default:
2733       break;
2734   }
2735 }
2736
2737 static void
2738 connect_bus_signals (GstElement * pipeline)
2739 {
2740   GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
2741
2742 #if defined (GDK_WINDOWING_X11) || defined (GDK_WINDOWING_WIN32) || defined (GDK_WINDOWING_QUARTZ)
2743   /* handle prepare-xwindow-id element message synchronously */
2744   gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bus_sync_handler,
2745       pipeline);
2746 #endif
2747
2748   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
2749   gst_bus_enable_sync_message_emission (bus);
2750
2751   g_signal_connect (bus, "message::state-changed",
2752       (GCallback) msg_state_changed, pipeline);
2753   g_signal_connect (bus, "message::segment-done", (GCallback) msg_segment_done,
2754       pipeline);
2755   g_signal_connect (bus, "message::async-done", (GCallback) msg_async_done,
2756       pipeline);
2757
2758   g_signal_connect (bus, "message::new-clock", (GCallback) message_received,
2759       pipeline);
2760   g_signal_connect (bus, "message::clock-lost", (GCallback) msg_clock_lost,
2761       pipeline);
2762   g_signal_connect (bus, "message::error", (GCallback) message_received,
2763       pipeline);
2764   g_signal_connect (bus, "message::warning", (GCallback) message_received,
2765       pipeline);
2766   g_signal_connect (bus, "message::eos", (GCallback) msg_eos, pipeline);
2767   g_signal_connect (bus, "message::tag", (GCallback) message_received,
2768       pipeline);
2769   g_signal_connect (bus, "message::element", (GCallback) message_received,
2770       pipeline);
2771   g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
2772       pipeline);
2773   g_signal_connect (bus, "message::buffering", (GCallback) msg_buffering,
2774       pipeline);
2775 //  g_signal_connect (bus, "message::step-done", (GCallback) msg_step_done,
2776 //      pipeline);
2777   g_signal_connect (bus, "message::step-start", (GCallback) msg_step_done,
2778       pipeline);
2779   g_signal_connect (bus, "sync-message::step-done",
2780       (GCallback) msg_sync_step_done, pipeline);
2781   g_signal_connect (bus, "message", (GCallback) msg, pipeline);
2782
2783   gst_object_unref (bus);
2784 }
2785
2786 /* Return GList of paths described in location string */
2787 static GList *
2788 handle_wildcards (const gchar * location)
2789 {
2790   GList *res = NULL;
2791   gchar *path = g_path_get_dirname (location);
2792   gchar *pattern = g_path_get_basename (location);
2793   GPatternSpec *pspec = g_pattern_spec_new (pattern);
2794   GDir *dir = g_dir_open (path, 0, NULL);
2795   const gchar *name;
2796
2797   g_print ("matching %s from %s\n", pattern, path);
2798
2799   if (!dir) {
2800     g_print ("opening directory %s failed\n", path);
2801     goto out;
2802   }
2803
2804   while ((name = g_dir_read_name (dir)) != NULL) {
2805     if (g_pattern_match_string (pspec, name)) {
2806       res = g_list_append (res, g_strjoin ("/", path, name, NULL));
2807       g_print ("  found clip %s\n", name);
2808     }
2809   }
2810
2811   g_dir_close (dir);
2812 out:
2813   g_pattern_spec_free (pspec);
2814   g_free (pattern);
2815   g_free (path);
2816
2817   return res;
2818 }
2819
2820 static void
2821 delete_event_cb (void)
2822 {
2823   stop_cb (NULL, NULL);
2824   gtk_main_quit ();
2825 }
2826
2827 static void
2828 print_usage (int argc, char **argv)
2829 {
2830   gint i;
2831
2832   g_print ("usage: %s <type> <filename>\n", argv[0]);
2833   g_print ("   possible types:\n");
2834
2835   for (i = 0; i < NUM_TYPES; i++) {
2836     g_print ("     %d = %s\n", i, pipelines[i].name);
2837   }
2838 }
2839
2840 int
2841 main (int argc, char **argv)
2842 {
2843   GtkWidget *window, *hbox, *vbox, *panel, *expander, *pb2vbox, *boxes,
2844       *flagtable, *boxes2, *step, *navigation;
2845   GtkWidget *play_button, *pause_button, *stop_button, *shot_button;
2846   GtkWidget *accurate_checkbox, *key_checkbox, *loop_checkbox, *flush_checkbox;
2847   GtkWidget *scrub_checkbox, *play_scrub_checkbox;
2848   GtkWidget *rate_label, *volume_label;
2849   GOptionEntry options[] = {
2850     {"audiosink", '\0', 0, G_OPTION_ARG_STRING, &opt_audiosink_str,
2851         "audio sink to use (default: " DEFAULT_AUDIOSINK ")", NULL},
2852     {"stats", 's', 0, G_OPTION_ARG_NONE, &stats,
2853         "Show pad stats", NULL},
2854     {"elem", 'e', 0, G_OPTION_ARG_NONE, &elem_seek,
2855         "Seek on elements instead of pads", NULL},
2856     {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
2857         "Verbose properties", NULL},
2858     {"videosink", '\0', 0, G_OPTION_ARG_STRING, &opt_videosink_str,
2859         "video sink to use (default: " DEFAULT_VIDEOSINK ")", NULL},
2860     {NULL}
2861   };
2862   GOptionContext *ctx;
2863   GError *err = NULL;
2864
2865 #if !GLIB_CHECK_VERSION (2, 31, 0)
2866   if (!g_thread_supported ())
2867     g_thread_init (NULL);
2868 #endif
2869
2870   ctx = g_option_context_new ("- test seeking in gsteamer");
2871   g_option_context_add_main_entries (ctx, options, NULL);
2872   g_option_context_add_group (ctx, gst_init_get_option_group ());
2873   g_option_context_add_group (ctx, gtk_get_option_group (TRUE));
2874
2875   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
2876     g_print ("Error initializing: %s\n", err->message);
2877     exit (1);
2878   }
2879
2880   if (opt_audiosink_str == NULL)
2881     opt_audiosink_str = g_strdup (DEFAULT_AUDIOSINK);
2882
2883   if (opt_videosink_str == NULL)
2884     opt_videosink_str = g_strdup (DEFAULT_VIDEOSINK);
2885
2886   GST_DEBUG_CATEGORY_INIT (seek_debug, "seek", 0, "seek example");
2887
2888   if (argc != 3) {
2889     print_usage (argc, argv);
2890     exit (-1);
2891   }
2892
2893   pipeline_type = atoi (argv[1]);
2894
2895   if (pipeline_type < 0 || pipeline_type >= NUM_TYPES) {
2896     print_usage (argc, argv);
2897     exit (-1);
2898   }
2899
2900   pipeline_spec = argv[2];
2901
2902   if (g_path_is_absolute (pipeline_spec) &&
2903       (g_strrstr (pipeline_spec, "*") != NULL ||
2904           g_strrstr (pipeline_spec, "?") != NULL)) {
2905     paths = handle_wildcards (pipeline_spec);
2906   } else {
2907     paths = g_list_prepend (paths, g_strdup (pipeline_spec));
2908   }
2909
2910   if (!paths) {
2911     g_print ("opening %s failed\n", pipeline_spec);
2912     exit (-1);
2913   }
2914
2915   l = paths;
2916
2917   pipeline = pipelines[pipeline_type].func ((gchar *) l->data);
2918   g_assert (pipeline);
2919
2920   /* initialize gui elements ... */
2921   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
2922   video_window = gtk_drawing_area_new ();
2923   g_signal_connect (video_window, "draw", G_CALLBACK (draw_cb), NULL);
2924   g_signal_connect (video_window, "realize", G_CALLBACK (realize_cb), NULL);
2925   g_signal_connect (video_window, "button-press-event",
2926       G_CALLBACK (button_press_cb), NULL);
2927   g_signal_connect (video_window, "button-release-event",
2928       G_CALLBACK (button_release_cb), NULL);
2929   g_signal_connect (video_window, "key-press-event", G_CALLBACK (key_press_cb),
2930       NULL);
2931   g_signal_connect (video_window, "key-release-event",
2932       G_CALLBACK (key_release_cb), NULL);
2933   g_signal_connect (video_window, "motion-notify-event",
2934       G_CALLBACK (motion_notify_cb), NULL);
2935   gtk_widget_set_can_focus (video_window, TRUE);
2936   gtk_widget_set_double_buffered (video_window, FALSE);
2937   gtk_widget_add_events (video_window,
2938       GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
2939       | GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);
2940
2941   statusbar = gtk_statusbar_new ();
2942   status_id = gtk_statusbar_get_context_id (GTK_STATUSBAR (statusbar), "seek");
2943   gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Stopped");
2944   hbox = gtk_hbox_new (FALSE, 0);
2945   vbox = gtk_vbox_new (FALSE, 0);
2946   flagtable = gtk_table_new (4, 2, FALSE);
2947   gtk_container_set_border_width (GTK_CONTAINER (vbox), 3);
2948
2949   /* media controls */
2950   play_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_PLAY);
2951   pause_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_PAUSE);
2952   stop_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_STOP);
2953
2954   /* seek flags */
2955   accurate_checkbox = gtk_check_button_new_with_label ("Accurate Seek");
2956   key_checkbox = gtk_check_button_new_with_label ("Key-unit Seek");
2957   loop_checkbox = gtk_check_button_new_with_label ("Loop");
2958   flush_checkbox = gtk_check_button_new_with_label ("Flush");
2959   scrub_checkbox = gtk_check_button_new_with_label ("Scrub");
2960   play_scrub_checkbox = gtk_check_button_new_with_label ("Play Scrub");
2961   skip_checkbox = gtk_check_button_new_with_label ("Play Skip");
2962   rate_spinbutton = gtk_spin_button_new_with_range (-100, 100, 0.1);
2963   gtk_spin_button_set_digits (GTK_SPIN_BUTTON (rate_spinbutton), 3);
2964   rate_label = gtk_label_new ("Rate");
2965
2966   gtk_widget_set_tooltip_text (accurate_checkbox,
2967       "accurate position is requested, this might be considerably slower for some formats");
2968   gtk_widget_set_tooltip_text (key_checkbox,
2969       "seek to the nearest keyframe. This might be faster but less accurate");
2970   gtk_widget_set_tooltip_text (loop_checkbox, "loop playback");
2971   gtk_widget_set_tooltip_text (flush_checkbox, "flush pipeline after seeking");
2972   gtk_widget_set_tooltip_text (rate_spinbutton, "define the playback rate, "
2973       "negative value trigger reverse playback");
2974   gtk_widget_set_tooltip_text (scrub_checkbox, "show images while seeking");
2975   gtk_widget_set_tooltip_text (play_scrub_checkbox, "play video while seeking");
2976   gtk_widget_set_tooltip_text (skip_checkbox,
2977       "Skip frames while playing at high frame rates");
2978
2979   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (flush_checkbox), TRUE);
2980   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (scrub_checkbox), TRUE);
2981
2982   gtk_spin_button_set_value (GTK_SPIN_BUTTON (rate_spinbutton), rate);
2983
2984   /* step expander */
2985   {
2986     GtkWidget *hbox;
2987
2988     step = gtk_expander_new ("step options");
2989     hbox = gtk_hbox_new (FALSE, 0);
2990
2991     format_combo = gtk_combo_box_text_new ();
2992     gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (format_combo),
2993         "frames");
2994     gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (format_combo),
2995         "time (ms)");
2996     gtk_combo_box_set_active (GTK_COMBO_BOX (format_combo), 0);
2997     gtk_box_pack_start (GTK_BOX (hbox), format_combo, FALSE, FALSE, 2);
2998
2999     step_amount_spinbutton = gtk_spin_button_new_with_range (1, 1000, 1);
3000     gtk_spin_button_set_digits (GTK_SPIN_BUTTON (step_amount_spinbutton), 0);
3001     gtk_spin_button_set_value (GTK_SPIN_BUTTON (step_amount_spinbutton), 1.0);
3002     gtk_box_pack_start (GTK_BOX (hbox), step_amount_spinbutton, FALSE, FALSE,
3003         2);
3004
3005     step_rate_spinbutton = gtk_spin_button_new_with_range (0.0, 100, 0.1);
3006     gtk_spin_button_set_digits (GTK_SPIN_BUTTON (step_rate_spinbutton), 3);
3007     gtk_spin_button_set_value (GTK_SPIN_BUTTON (step_rate_spinbutton), 1.0);
3008     gtk_box_pack_start (GTK_BOX (hbox), step_rate_spinbutton, FALSE, FALSE, 2);
3009
3010     step_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_FORWARD);
3011     gtk_button_set_label (GTK_BUTTON (step_button), "Step");
3012     gtk_box_pack_start (GTK_BOX (hbox), step_button, FALSE, FALSE, 2);
3013
3014     g_signal_connect (G_OBJECT (step_button), "clicked", G_CALLBACK (step_cb),
3015         pipeline);
3016
3017     /* shuttle scale */
3018     shuttle_checkbox = gtk_check_button_new_with_label ("Shuttle");
3019     gtk_box_pack_start (GTK_BOX (hbox), shuttle_checkbox, FALSE, FALSE, 2);
3020     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (shuttle_checkbox), FALSE);
3021     g_signal_connect (shuttle_checkbox, "toggled", G_CALLBACK (shuttle_toggled),
3022         pipeline);
3023
3024     shuttle_adjustment =
3025         GTK_ADJUSTMENT (gtk_adjustment_new (0.0, -3.00, 4.0, 0.1, 1.0, 1.0));
3026     shuttle_hscale = gtk_hscale_new (shuttle_adjustment);
3027     gtk_scale_set_digits (GTK_SCALE (shuttle_hscale), 2);
3028     gtk_scale_set_value_pos (GTK_SCALE (shuttle_hscale), GTK_POS_TOP);
3029     g_signal_connect (shuttle_hscale, "value_changed",
3030         G_CALLBACK (shuttle_value_changed), pipeline);
3031     g_signal_connect (shuttle_hscale, "format_value",
3032         G_CALLBACK (shuttle_format_value), pipeline);
3033
3034     gtk_box_pack_start (GTK_BOX (hbox), shuttle_hscale, TRUE, TRUE, 2);
3035
3036     gtk_container_add (GTK_CONTAINER (step), hbox);
3037   }
3038
3039   /* navigation command expander */
3040   {
3041     GtkWidget *navigation_button;
3042     GtkWidget *grid;
3043     gint i = 0;
3044
3045     navigation = gtk_expander_new ("navigation commands");
3046     grid = gtk_grid_new ();
3047     gtk_grid_set_row_spacing (GTK_GRID (grid), 2);
3048     gtk_grid_set_row_homogeneous (GTK_GRID (grid), TRUE);
3049     gtk_grid_set_column_spacing (GTK_GRID (grid), 2);
3050     gtk_grid_set_column_homogeneous (GTK_GRID (grid), TRUE);
3051
3052     navigation_button = gtk_button_new_with_label ("Menu 1");
3053     g_signal_connect (G_OBJECT (navigation_button), "clicked",
3054         G_CALLBACK (navigation_cmd_cb),
3055         GINT_TO_POINTER (GST_NAVIGATION_COMMAND_MENU1));
3056     gtk_grid_attach (GTK_GRID (grid), navigation_button, i, 0, 1, 1);
3057     gtk_widget_set_sensitive (navigation_button, FALSE);
3058     gtk_widget_set_tooltip_text (navigation_button, "DVD Menu");
3059     navigation_buttons[i].button = navigation_button;
3060     navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_MENU1;
3061
3062     navigation_button = gtk_button_new_with_label ("Menu 2");
3063     g_signal_connect (G_OBJECT (navigation_button), "clicked",
3064         G_CALLBACK (navigation_cmd_cb),
3065         GINT_TO_POINTER (GST_NAVIGATION_COMMAND_MENU2));
3066     gtk_grid_attach (GTK_GRID (grid), navigation_button, i, 0, 1, 1);
3067     gtk_widget_set_sensitive (navigation_button, FALSE);
3068     gtk_widget_set_tooltip_text (navigation_button, "DVD Title Menu");
3069     navigation_buttons[i].button = navigation_button;
3070     navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_MENU2;
3071
3072     navigation_button = gtk_button_new_with_label ("Menu 3");
3073     g_signal_connect (G_OBJECT (navigation_button), "clicked",
3074         G_CALLBACK (navigation_cmd_cb),
3075         GINT_TO_POINTER (GST_NAVIGATION_COMMAND_MENU3));
3076     gtk_grid_attach (GTK_GRID (grid), navigation_button, i, 0, 1, 1);
3077     gtk_widget_set_sensitive (navigation_button, FALSE);
3078     gtk_widget_set_tooltip_text (navigation_button, "DVD Root Menu");
3079     navigation_buttons[i].button = navigation_button;
3080     navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_MENU3;
3081
3082     navigation_button = gtk_button_new_with_label ("Menu 4");
3083     g_signal_connect (G_OBJECT (navigation_button), "clicked",
3084         G_CALLBACK (navigation_cmd_cb),
3085         GINT_TO_POINTER (GST_NAVIGATION_COMMAND_MENU4));
3086     gtk_grid_attach (GTK_GRID (grid), navigation_button, i, 0, 1, 1);
3087     gtk_widget_set_sensitive (navigation_button, FALSE);
3088     gtk_widget_set_tooltip_text (navigation_button, "DVD Subpicture Menu");
3089     navigation_buttons[i].button = navigation_button;
3090     navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_MENU4;
3091
3092     navigation_button = gtk_button_new_with_label ("Menu 5");
3093     g_signal_connect (G_OBJECT (navigation_button), "clicked",
3094         G_CALLBACK (navigation_cmd_cb),
3095         GINT_TO_POINTER (GST_NAVIGATION_COMMAND_MENU5));
3096     gtk_grid_attach (GTK_GRID (grid), navigation_button, i, 0, 1, 1);
3097     gtk_widget_set_sensitive (navigation_button, FALSE);
3098     gtk_widget_set_tooltip_text (navigation_button, "DVD Audio Menu");
3099     navigation_buttons[i].button = navigation_button;
3100     navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_MENU5;
3101
3102     navigation_button = gtk_button_new_with_label ("Menu 6");
3103     g_signal_connect (G_OBJECT (navigation_button), "clicked",
3104         G_CALLBACK (navigation_cmd_cb),
3105         GINT_TO_POINTER (GST_NAVIGATION_COMMAND_MENU6));
3106     gtk_grid_attach (GTK_GRID (grid), navigation_button, i, 0, 1, 1);
3107     gtk_widget_set_sensitive (navigation_button, FALSE);
3108     gtk_widget_set_tooltip_text (navigation_button, "DVD Angle Menu");
3109     navigation_buttons[i].button = navigation_button;
3110     navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_MENU6;
3111
3112     navigation_button = gtk_button_new_with_label ("Menu 7");
3113     g_signal_connect (G_OBJECT (navigation_button), "clicked",
3114         G_CALLBACK (navigation_cmd_cb),
3115         GINT_TO_POINTER (GST_NAVIGATION_COMMAND_MENU7));
3116     gtk_grid_attach (GTK_GRID (grid), navigation_button, i, 0, 1, 1);
3117     gtk_widget_set_sensitive (navigation_button, FALSE);
3118     gtk_widget_set_tooltip_text (navigation_button, "DVD Chapter Menu");
3119     navigation_buttons[i].button = navigation_button;
3120     navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_MENU7;
3121
3122     navigation_button = gtk_button_new_with_label ("Left");
3123     g_signal_connect (G_OBJECT (navigation_button), "clicked",
3124         G_CALLBACK (navigation_cmd_cb),
3125         GINT_TO_POINTER (GST_NAVIGATION_COMMAND_LEFT));
3126     gtk_grid_attach (GTK_GRID (grid), navigation_button, i - 7, 1, 1, 1);
3127     gtk_widget_set_sensitive (navigation_button, FALSE);
3128     navigation_buttons[i].button = navigation_button;
3129     navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_LEFT;
3130
3131     navigation_button = gtk_button_new_with_label ("Right");
3132     g_signal_connect (G_OBJECT (navigation_button), "clicked",
3133         G_CALLBACK (navigation_cmd_cb),
3134         GINT_TO_POINTER (GST_NAVIGATION_COMMAND_RIGHT));
3135     gtk_grid_attach (GTK_GRID (grid), navigation_button, i - 7, 1, 1, 1);
3136     gtk_widget_set_sensitive (navigation_button, FALSE);
3137     navigation_buttons[i].button = navigation_button;
3138     navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_RIGHT;
3139
3140     navigation_button = gtk_button_new_with_label ("Up");
3141     g_signal_connect (G_OBJECT (navigation_button), "clicked",
3142         G_CALLBACK (navigation_cmd_cb),
3143         GINT_TO_POINTER (GST_NAVIGATION_COMMAND_UP));
3144     gtk_grid_attach (GTK_GRID (grid), navigation_button, i - 7, 1, 1, 1);
3145     gtk_widget_set_sensitive (navigation_button, FALSE);
3146     navigation_buttons[i].button = navigation_button;
3147     navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_UP;
3148
3149     navigation_button = gtk_button_new_with_label ("Down");
3150     g_signal_connect (G_OBJECT (navigation_button), "clicked",
3151         G_CALLBACK (navigation_cmd_cb),
3152         GINT_TO_POINTER (GST_NAVIGATION_COMMAND_DOWN));
3153     gtk_grid_attach (GTK_GRID (grid), navigation_button, i - 7, 1, 1, 1);
3154     gtk_widget_set_sensitive (navigation_button, FALSE);
3155     navigation_buttons[i].button = navigation_button;
3156     navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_DOWN;
3157
3158     navigation_button = gtk_button_new_with_label ("Activate");
3159     g_signal_connect (G_OBJECT (navigation_button), "clicked",
3160         G_CALLBACK (navigation_cmd_cb),
3161         GINT_TO_POINTER (GST_NAVIGATION_COMMAND_ACTIVATE));
3162     gtk_grid_attach (GTK_GRID (grid), navigation_button, i - 7, 1, 1, 1);
3163     gtk_widget_set_sensitive (navigation_button, FALSE);
3164     navigation_buttons[i].button = navigation_button;
3165     navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_ACTIVATE;
3166
3167     navigation_button = gtk_button_new_with_label ("Prev. Angle");
3168     g_signal_connect (G_OBJECT (navigation_button), "clicked",
3169         G_CALLBACK (navigation_cmd_cb),
3170         GINT_TO_POINTER (GST_NAVIGATION_COMMAND_PREV_ANGLE));
3171     gtk_grid_attach (GTK_GRID (grid), navigation_button, i - 7, 1, 1, 1);
3172     gtk_widget_set_sensitive (navigation_button, FALSE);
3173     navigation_buttons[i].button = navigation_button;
3174     navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_PREV_ANGLE;
3175
3176     navigation_button = gtk_button_new_with_label ("Next. Angle");
3177     g_signal_connect (G_OBJECT (navigation_button), "clicked",
3178         G_CALLBACK (navigation_cmd_cb),
3179         GINT_TO_POINTER (GST_NAVIGATION_COMMAND_NEXT_ANGLE));
3180     gtk_grid_attach (GTK_GRID (grid), navigation_button, i - 7, 1, 1, 1);
3181     gtk_widget_set_sensitive (navigation_button, FALSE);
3182     navigation_buttons[i].button = navigation_button;
3183     navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_NEXT_ANGLE;
3184
3185     gtk_container_add (GTK_CONTAINER (navigation), grid);
3186   }
3187
3188   /* seek bar */
3189   adjustment =
3190       GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.00, N_GRAD, 0.1, 1.0, 1.0));
3191   hscale = gtk_hscale_new (adjustment);
3192   gtk_scale_set_digits (GTK_SCALE (hscale), 2);
3193   gtk_scale_set_value_pos (GTK_SCALE (hscale), GTK_POS_RIGHT);
3194   gtk_range_set_show_fill_level (GTK_RANGE (hscale), TRUE);
3195   gtk_range_set_fill_level (GTK_RANGE (hscale), N_GRAD);
3196
3197   g_signal_connect (hscale, "button_press_event", G_CALLBACK (start_seek),
3198       pipeline);
3199   g_signal_connect (hscale, "button_release_event", G_CALLBACK (stop_seek),
3200       pipeline);
3201   g_signal_connect (hscale, "format_value", G_CALLBACK (format_value),
3202       pipeline);
3203
3204   if (pipeline_type == 16) {
3205     /* the playbin2 panel controls for the video/audio/subtitle tracks */
3206     panel = gtk_hbox_new (FALSE, 0);
3207     video_combo = gtk_combo_box_text_new ();
3208     audio_combo = gtk_combo_box_text_new ();
3209     text_combo = gtk_combo_box_text_new ();
3210     gtk_widget_set_sensitive (video_combo, FALSE);
3211     gtk_widget_set_sensitive (audio_combo, FALSE);
3212     gtk_widget_set_sensitive (text_combo, FALSE);
3213     gtk_box_pack_start (GTK_BOX (panel), video_combo, TRUE, TRUE, 2);
3214     gtk_box_pack_start (GTK_BOX (panel), audio_combo, TRUE, TRUE, 2);
3215     gtk_box_pack_start (GTK_BOX (panel), text_combo, TRUE, TRUE, 2);
3216     g_signal_connect (G_OBJECT (video_combo), "changed",
3217         G_CALLBACK (video_combo_cb), pipeline);
3218     g_signal_connect (G_OBJECT (audio_combo), "changed",
3219         G_CALLBACK (audio_combo_cb), pipeline);
3220     g_signal_connect (G_OBJECT (text_combo), "changed",
3221         G_CALLBACK (text_combo_cb), pipeline);
3222     /* playbin2 panel for flag checkboxes and volume/mute */
3223     boxes = gtk_hbox_new (FALSE, 0);
3224     vis_checkbox = gtk_check_button_new_with_label ("Vis");
3225     video_checkbox = gtk_check_button_new_with_label ("Video");
3226     audio_checkbox = gtk_check_button_new_with_label ("Audio");
3227     text_checkbox = gtk_check_button_new_with_label ("Text");
3228     mute_checkbox = gtk_check_button_new_with_label ("Mute");
3229     download_checkbox = gtk_check_button_new_with_label ("Download");
3230     buffer_checkbox = gtk_check_button_new_with_label ("Buffer");
3231     volume_label = gtk_label_new ("Volume");
3232     volume_spinbutton = gtk_spin_button_new_with_range (0, 10.0, 0.1);
3233     gtk_spin_button_set_value (GTK_SPIN_BUTTON (volume_spinbutton), 1.0);
3234     gtk_box_pack_start (GTK_BOX (boxes), video_checkbox, TRUE, TRUE, 2);
3235     gtk_box_pack_start (GTK_BOX (boxes), audio_checkbox, TRUE, TRUE, 2);
3236     gtk_box_pack_start (GTK_BOX (boxes), text_checkbox, TRUE, TRUE, 2);
3237     gtk_box_pack_start (GTK_BOX (boxes), vis_checkbox, TRUE, TRUE, 2);
3238     gtk_box_pack_start (GTK_BOX (boxes), mute_checkbox, TRUE, TRUE, 2);
3239     gtk_box_pack_start (GTK_BOX (boxes), download_checkbox, TRUE, TRUE, 2);
3240     gtk_box_pack_start (GTK_BOX (boxes), buffer_checkbox, TRUE, TRUE, 2);
3241     gtk_box_pack_start (GTK_BOX (boxes), volume_label, TRUE, TRUE, 2);
3242     gtk_box_pack_start (GTK_BOX (boxes), volume_spinbutton, TRUE, TRUE, 2);
3243     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (vis_checkbox), FALSE);
3244     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (audio_checkbox), TRUE);
3245     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (video_checkbox), TRUE);
3246     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (text_checkbox), TRUE);
3247     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (mute_checkbox), FALSE);
3248     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (download_checkbox), FALSE);
3249     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (buffer_checkbox), FALSE);
3250     g_signal_connect (G_OBJECT (vis_checkbox), "toggled",
3251         G_CALLBACK (vis_toggle_cb), pipeline);
3252     g_signal_connect (G_OBJECT (audio_checkbox), "toggled",
3253         G_CALLBACK (audio_toggle_cb), pipeline);
3254     g_signal_connect (G_OBJECT (video_checkbox), "toggled",
3255         G_CALLBACK (video_toggle_cb), pipeline);
3256     g_signal_connect (G_OBJECT (text_checkbox), "toggled",
3257         G_CALLBACK (text_toggle_cb), pipeline);
3258     g_signal_connect (G_OBJECT (mute_checkbox), "toggled",
3259         G_CALLBACK (mute_toggle_cb), pipeline);
3260     g_signal_connect (G_OBJECT (download_checkbox), "toggled",
3261         G_CALLBACK (download_toggle_cb), pipeline);
3262     g_signal_connect (G_OBJECT (buffer_checkbox), "toggled",
3263         G_CALLBACK (buffer_toggle_cb), pipeline);
3264     g_signal_connect (G_OBJECT (volume_spinbutton), "value_changed",
3265         G_CALLBACK (volume_spinbutton_changed_cb), pipeline);
3266     /* playbin2 panel for snapshot */
3267     boxes2 = gtk_hbox_new (FALSE, 0);
3268     shot_button = gtk_button_new_from_stock (GTK_STOCK_SAVE);
3269     gtk_widget_set_tooltip_text (shot_button,
3270         "save a screenshot .png in the current directory");
3271     g_signal_connect (G_OBJECT (shot_button), "clicked", G_CALLBACK (shot_cb),
3272         pipeline);
3273     vis_combo = gtk_combo_box_text_new ();
3274     g_signal_connect (G_OBJECT (vis_combo), "changed",
3275         G_CALLBACK (vis_combo_cb), pipeline);
3276     gtk_widget_set_sensitive (vis_combo, FALSE);
3277     gtk_box_pack_start (GTK_BOX (boxes2), shot_button, TRUE, TRUE, 2);
3278     gtk_box_pack_start (GTK_BOX (boxes2), vis_combo, TRUE, TRUE, 2);
3279
3280     /* fill the vis combo box and the array of factories */
3281     init_visualization_features ();
3282   } else {
3283     panel = boxes = boxes2 = NULL;
3284   }
3285
3286   /* do the packing stuff ... */
3287   gtk_window_set_default_size (GTK_WINDOW (window), 250, 96);
3288   /* FIXME: can we avoid this for audio only? */
3289   gtk_widget_set_size_request (GTK_WIDGET (video_window), -1,
3290       DEFAULT_VIDEO_HEIGHT);
3291   gtk_container_add (GTK_CONTAINER (window), vbox);
3292   gtk_box_pack_start (GTK_BOX (vbox), video_window, TRUE, TRUE, 2);
3293   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 2);
3294   gtk_box_pack_start (GTK_BOX (hbox), play_button, FALSE, FALSE, 2);
3295   gtk_box_pack_start (GTK_BOX (hbox), pause_button, FALSE, FALSE, 2);
3296   gtk_box_pack_start (GTK_BOX (hbox), stop_button, FALSE, FALSE, 2);
3297   gtk_box_pack_start (GTK_BOX (hbox), flagtable, FALSE, FALSE, 2);
3298   gtk_table_attach_defaults (GTK_TABLE (flagtable), accurate_checkbox, 0, 1, 0,
3299       1);
3300   gtk_table_attach_defaults (GTK_TABLE (flagtable), flush_checkbox, 1, 2, 0, 1);
3301   gtk_table_attach_defaults (GTK_TABLE (flagtable), loop_checkbox, 2, 3, 0, 1);
3302   gtk_table_attach_defaults (GTK_TABLE (flagtable), key_checkbox, 0, 1, 1, 2);
3303   gtk_table_attach_defaults (GTK_TABLE (flagtable), scrub_checkbox, 1, 2, 1, 2);
3304   gtk_table_attach_defaults (GTK_TABLE (flagtable), play_scrub_checkbox, 2, 3,
3305       1, 2);
3306   gtk_table_attach_defaults (GTK_TABLE (flagtable), skip_checkbox, 3, 4, 0, 1);
3307   gtk_table_attach_defaults (GTK_TABLE (flagtable), rate_label, 4, 5, 0, 1);
3308   gtk_table_attach_defaults (GTK_TABLE (flagtable), rate_spinbutton, 4, 5, 1,
3309       2);
3310   if (panel && boxes && boxes2) {
3311     expander = gtk_expander_new ("playbin2 options");
3312     pb2vbox = gtk_vbox_new (FALSE, 0);
3313     gtk_box_pack_start (GTK_BOX (pb2vbox), panel, FALSE, FALSE, 2);
3314     gtk_box_pack_start (GTK_BOX (pb2vbox), boxes, FALSE, FALSE, 2);
3315     gtk_box_pack_start (GTK_BOX (pb2vbox), boxes2, FALSE, FALSE, 2);
3316     gtk_container_add (GTK_CONTAINER (expander), pb2vbox);
3317     gtk_box_pack_start (GTK_BOX (vbox), expander, FALSE, FALSE, 2);
3318   }
3319   gtk_box_pack_start (GTK_BOX (vbox), step, FALSE, FALSE, 2);
3320   gtk_box_pack_start (GTK_BOX (vbox), navigation, FALSE, FALSE, 2);
3321   gtk_box_pack_start (GTK_BOX (vbox), hscale, FALSE, FALSE, 2);
3322   gtk_box_pack_start (GTK_BOX (vbox), statusbar, FALSE, FALSE, 2);
3323
3324   /* connect things ... */
3325   g_signal_connect (G_OBJECT (play_button), "clicked", G_CALLBACK (play_cb),
3326       pipeline);
3327   g_signal_connect (G_OBJECT (pause_button), "clicked", G_CALLBACK (pause_cb),
3328       pipeline);
3329   g_signal_connect (G_OBJECT (stop_button), "clicked", G_CALLBACK (stop_cb),
3330       pipeline);
3331   g_signal_connect (G_OBJECT (accurate_checkbox), "toggled",
3332       G_CALLBACK (accurate_toggle_cb), pipeline);
3333   g_signal_connect (G_OBJECT (key_checkbox), "toggled",
3334       G_CALLBACK (key_toggle_cb), pipeline);
3335   g_signal_connect (G_OBJECT (loop_checkbox), "toggled",
3336       G_CALLBACK (loop_toggle_cb), pipeline);
3337   g_signal_connect (G_OBJECT (flush_checkbox), "toggled",
3338       G_CALLBACK (flush_toggle_cb), pipeline);
3339   g_signal_connect (G_OBJECT (scrub_checkbox), "toggled",
3340       G_CALLBACK (scrub_toggle_cb), pipeline);
3341   g_signal_connect (G_OBJECT (play_scrub_checkbox), "toggled",
3342       G_CALLBACK (play_scrub_toggle_cb), pipeline);
3343   g_signal_connect (G_OBJECT (skip_checkbox), "toggled",
3344       G_CALLBACK (skip_toggle_cb), pipeline);
3345   g_signal_connect (G_OBJECT (rate_spinbutton), "value_changed",
3346       G_CALLBACK (rate_spinbutton_changed_cb), pipeline);
3347
3348   g_signal_connect (G_OBJECT (window), "delete-event", delete_event_cb, NULL);
3349
3350   /* show the gui. */
3351   gtk_widget_show_all (window);
3352
3353   /* realize window now so that the video window gets created and we can
3354    * obtain its XID before the pipeline is started up and the videosink
3355    * asks for the XID of the window to render onto */
3356   gtk_widget_realize (window);
3357
3358 #if defined (GDK_WINDOWING_X11) || defined (GDK_WINDOWING_WIN32) || defined (GDK_WINDOWING_QUARTZ)
3359   /* we should have the XID now */
3360   g_assert (embed_xid != 0);
3361 #endif
3362
3363   if (verbose) {
3364     g_signal_connect (pipeline, "deep_notify",
3365         G_CALLBACK (gst_object_default_deep_notify), NULL);
3366   }
3367
3368   connect_bus_signals (pipeline);
3369   gtk_main ();
3370
3371   g_print ("NULL pipeline\n");
3372   gst_element_set_state (pipeline, GST_STATE_NULL);
3373
3374   if (xoverlay_element)
3375     gst_object_unref (xoverlay_element);
3376   if (navigation_element)
3377     gst_object_unref (navigation_element);
3378
3379   g_print ("free pipeline\n");
3380   gst_object_unref (pipeline);
3381
3382   g_list_foreach (paths, (GFunc) g_free, NULL);
3383   g_list_free (paths);
3384
3385   return 0;
3386 }