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