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