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