examples: update some more code for new Gtk+ API, with fallback for older Gtk+ versions
[platform/upstream/gstreamer.git] / tests / examples / seek / seek.c
1 /* GStreamer
2  *
3  * seek.c: seeking sample application
4  *
5  * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
6  *               2006 Stefan Kost <ensonic@users.sf.net>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <stdlib.h>
29 #include <math.h>
30 #include <glib.h>
31 #include <gtk/gtk.h>
32 #include <gst/gst.h>
33 #include <string.h>
34
35 #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, (GtkFunction) 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, (GtkFunction) 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
2107 static void
2108 message_received (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
2109 {
2110   const GstStructure *s;
2111
2112   s = gst_message_get_structure (message);
2113   g_print ("message from \"%s\" (%s): ",
2114       GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
2115       gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
2116   if (s) {
2117     gchar *sstr;
2118
2119     sstr = gst_structure_to_string (s);
2120     g_print ("%s\n", sstr);
2121     g_free (sstr);
2122   } else {
2123     g_print ("no message details\n");
2124   }
2125 }
2126
2127 static gboolean shuttling = FALSE;
2128 static gdouble shuttle_rate = 0.0;
2129 static gdouble play_rate = 1.0;
2130
2131 static void
2132 do_shuttle (GstElement * element)
2133 {
2134   guint64 duration;
2135
2136   if (shuttling)
2137     duration = 40 * GST_MSECOND;
2138   else
2139     duration = -1;
2140
2141   gst_element_send_event (element,
2142       gst_event_new_step (GST_FORMAT_TIME, duration, shuttle_rate, FALSE,
2143           FALSE));
2144 }
2145
2146 static void
2147 msg_sync_step_done (GstBus * bus, GstMessage * message, GstElement * element)
2148 {
2149   GstFormat format;
2150   guint64 amount;
2151   gdouble rate;
2152   gboolean flush;
2153   gboolean intermediate;
2154   guint64 duration;
2155   gboolean eos;
2156
2157   gst_message_parse_step_done (message, &format, &amount, &rate, &flush,
2158       &intermediate, &duration, &eos);
2159
2160   if (eos) {
2161     g_print ("stepped till EOS\n");
2162     return;
2163   }
2164
2165   if (g_static_mutex_trylock (&state_mutex)) {
2166     if (shuttling)
2167       do_shuttle (element);
2168     g_static_mutex_unlock (&state_mutex);
2169   } else {
2170     /* ignore step messages that come while we are doing a state change */
2171     g_print ("state change is busy\n");
2172   }
2173 }
2174
2175 static void
2176 shuttle_toggled (GtkToggleButton * button, GstElement * element)
2177 {
2178   gboolean active;
2179
2180   active = gtk_toggle_button_get_active (button);
2181
2182   if (active != shuttling) {
2183     shuttling = active;
2184     g_print ("shuttling %s\n", shuttling ? "active" : "inactive");
2185     if (active) {
2186       shuttle_rate = 0.0;
2187       play_rate = 1.0;
2188       pause_cb (NULL, NULL);
2189       gst_element_get_state (element, NULL, NULL, -1);
2190     }
2191   }
2192 }
2193
2194 static void
2195 shuttle_rate_switch (GstElement * element)
2196 {
2197   GstSeekFlags flags;
2198   GstEvent *s_event;
2199   gboolean res;
2200
2201   if (state == GST_STATE_PLAYING) {
2202     /* pause when we need to */
2203     pause_cb (NULL, NULL);
2204     gst_element_get_state (element, NULL, NULL, -1);
2205   }
2206
2207   if (play_rate == 1.0)
2208     play_rate = -1.0;
2209   else
2210     play_rate = 1.0;
2211
2212   g_print ("rate changed to %lf %" GST_TIME_FORMAT "\n", play_rate,
2213       GST_TIME_ARGS (position));
2214
2215   flags = GST_SEEK_FLAG_FLUSH;
2216   flags |= GST_SEEK_FLAG_ACCURATE;
2217
2218   if (play_rate >= 0.0) {
2219     s_event = gst_event_new_seek (play_rate,
2220         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, position,
2221         GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE);
2222   } else {
2223     s_event = gst_event_new_seek (play_rate,
2224         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
2225         GST_SEEK_TYPE_SET, position);
2226   }
2227   res = send_event (s_event);
2228   if (res) {
2229     gst_element_get_state (element, NULL, NULL, SEEK_TIMEOUT);
2230   } else {
2231     g_print ("seek failed\n");
2232   }
2233 }
2234
2235 static void
2236 shuttle_value_changed (GtkRange * range, GstElement * element)
2237 {
2238   gdouble rate;
2239
2240   rate = gtk_adjustment_get_value (shuttle_adjustment);
2241
2242   if (rate == 0.0) {
2243     g_print ("rate 0.0, pause\n");
2244     pause_cb (NULL, NULL);
2245     gst_element_get_state (element, NULL, NULL, -1);
2246   } else {
2247     g_print ("rate changed %0.3g\n", rate);
2248
2249     if ((rate < 0.0 && play_rate > 0.0) || (rate > 0.0 && play_rate < 0.0)) {
2250       shuttle_rate_switch (element);
2251     }
2252
2253     shuttle_rate = ABS (rate);
2254     if (state != GST_STATE_PLAYING) {
2255       do_shuttle (element);
2256       play_cb (NULL, NULL);
2257     }
2258   }
2259 }
2260
2261 static void
2262 msg_async_done (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
2263 {
2264   GST_DEBUG ("async done");
2265   /* when we get ASYNC_DONE we can query position, duration and other
2266    * properties */
2267   update_scale (pipeline);
2268
2269   /* update the available streams */
2270   update_streams (pipeline);
2271 }
2272
2273 static void
2274 msg_state_changed (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
2275 {
2276   const GstStructure *s;
2277
2278   s = gst_message_get_structure (message);
2279
2280   /* We only care about state changed on the pipeline */
2281   if (s && GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
2282     GstState old, new, pending;
2283
2284     gst_message_parse_state_changed (message, &old, &new, &pending);
2285
2286     /* When state of the pipeline changes to paused or playing we start updating scale */
2287     if (new == GST_STATE_PLAYING) {
2288       set_update_scale (TRUE);
2289     } else {
2290       set_update_scale (FALSE);
2291     }
2292   }
2293 }
2294
2295 static void
2296 msg_segment_done (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
2297 {
2298   GstEvent *s_event;
2299   GstSeekFlags flags;
2300   gboolean res;
2301   GstFormat format;
2302
2303   GST_DEBUG ("position is %" GST_TIME_FORMAT, GST_TIME_ARGS (position));
2304   gst_message_parse_segment_done (message, &format, &position);
2305   GST_DEBUG ("end of segment at %" GST_TIME_FORMAT, GST_TIME_ARGS (position));
2306
2307   flags = 0;
2308   /* in the segment-done callback we never flush as this would not make sense
2309    * for seamless playback. */
2310   if (loop_seek)
2311     flags |= GST_SEEK_FLAG_SEGMENT;
2312   if (skip_seek)
2313     flags |= GST_SEEK_FLAG_SKIP;
2314
2315   s_event = gst_event_new_seek (rate,
2316       GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
2317       GST_SEEK_TYPE_SET, duration);
2318
2319   GST_DEBUG ("restart loop with rate %lf to 0 / %" GST_TIME_FORMAT,
2320       rate, GST_TIME_ARGS (duration));
2321
2322   res = send_event (s_event);
2323   if (!res)
2324     g_print ("segment seek failed\n");
2325 }
2326
2327 /* in stream buffering mode we PAUSE the pipeline until we receive a 100%
2328  * message */
2329 static void
2330 do_stream_buffering (gint percent)
2331 {
2332   gchar *bufstr;
2333
2334   gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
2335   bufstr = g_strdup_printf ("Buffering...%d", percent);
2336   gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, bufstr);
2337   g_free (bufstr);
2338
2339   if (percent == 100) {
2340     /* a 100% message means buffering is done */
2341     buffering = FALSE;
2342     /* if the desired state is playing, go back */
2343     if (state == GST_STATE_PLAYING) {
2344       /* no state management needed for live pipelines */
2345       if (!is_live) {
2346         fprintf (stderr, "Done buffering, setting pipeline to PLAYING ...\n");
2347         gst_element_set_state (pipeline, GST_STATE_PLAYING);
2348       }
2349       gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
2350       gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Playing");
2351     }
2352   } else {
2353     /* buffering busy */
2354     if (buffering == FALSE && state == GST_STATE_PLAYING) {
2355       /* we were not buffering but PLAYING, PAUSE  the pipeline. */
2356       if (!is_live) {
2357         fprintf (stderr, "Buffering, setting pipeline to PAUSED ...\n");
2358         gst_element_set_state (pipeline, GST_STATE_PAUSED);
2359       }
2360     }
2361     buffering = TRUE;
2362   }
2363 }
2364
2365 static void
2366 do_download_buffering (gint percent)
2367 {
2368   if (!buffering && percent < 100) {
2369     gchar *bufstr;
2370
2371     buffering = TRUE;
2372
2373     bufstr = g_strdup_printf ("Downloading...");
2374     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, bufstr);
2375     g_free (bufstr);
2376
2377     /* once we get a buffering message, we'll do the fill update */
2378     set_update_fill (TRUE);
2379
2380     if (state == GST_STATE_PLAYING && !is_live) {
2381       fprintf (stderr, "Downloading, setting pipeline to PAUSED ...\n");
2382       gst_element_set_state (pipeline, GST_STATE_PAUSED);
2383       /* user has to manually start the playback */
2384       state = GST_STATE_PAUSED;
2385     }
2386   }
2387 }
2388
2389 static void
2390 msg_buffering (GstBus * bus, GstMessage * message, GstPipeline * data)
2391 {
2392   gint percent;
2393
2394   gst_message_parse_buffering (message, &percent);
2395
2396   /* get more stats */
2397   gst_message_parse_buffering_stats (message, &mode, NULL, NULL,
2398       &buffering_left);
2399
2400   switch (mode) {
2401     case GST_BUFFERING_DOWNLOAD:
2402       do_download_buffering (percent);
2403       break;
2404     case GST_BUFFERING_LIVE:
2405     case GST_BUFFERING_TIMESHIFT:
2406     case GST_BUFFERING_STREAM:
2407       do_stream_buffering (percent);
2408       break;
2409   }
2410 }
2411
2412 static void
2413 msg_clock_lost (GstBus * bus, GstMessage * message, GstPipeline * data)
2414 {
2415   g_print ("clock lost! PAUSE and PLAY to select a new clock\n");
2416   if (state == GST_STATE_PLAYING) {
2417     gst_element_set_state (pipeline, GST_STATE_PAUSED);
2418     gst_element_set_state (pipeline, GST_STATE_PLAYING);
2419   }
2420 }
2421
2422 #ifdef HAVE_X
2423
2424 static gulong embed_xid = 0;
2425
2426 /* We set the xid here in response to the prepare-xwindow-id message via a
2427  * bus sync handler because we don't know the actual videosink used from the
2428  * start (as we don't know the pipeline, or bin elements such as autovideosink
2429  * or gconfvideosink may be used which create the actual videosink only once
2430  * the pipeline is started) */
2431 static GstBusSyncReply
2432 bus_sync_handler (GstBus * bus, GstMessage * message, GstPipeline * data)
2433 {
2434   if ((GST_MESSAGE_TYPE (message) == GST_MESSAGE_ELEMENT) &&
2435       gst_structure_has_name (message->structure, "prepare-xwindow-id")) {
2436     GstElement *element = GST_ELEMENT (GST_MESSAGE_SRC (message));
2437
2438     g_print ("got prepare-xwindow-id, setting XID %lu\n", embed_xid);
2439
2440     if (g_object_class_find_property (G_OBJECT_GET_CLASS (element),
2441             "force-aspect-ratio")) {
2442       g_object_set (element, "force-aspect-ratio", TRUE, NULL);
2443     }
2444
2445     /* Should have been initialised from main thread before (can't use
2446      * GDK_WINDOW_XID here with Gtk+ >= 2.18, because the sync handler will
2447      * be called from a streaming thread and GDK_WINDOW_XID maps to more than
2448      * a simple structure lookup with Gtk+ >= 2.18, where 'more' is stuff that
2449      * shouldn't be done from a non-GUI thread without explicit locking).  */
2450     g_assert (embed_xid != 0);
2451
2452     gst_x_overlay_set_window_handle (GST_X_OVERLAY (element), embed_xid);
2453   }
2454   return GST_BUS_PASS;
2455 }
2456 #endif
2457
2458 static gboolean
2459 handle_expose_cb (GtkWidget * widget, GdkEventExpose * event, gpointer data)
2460 {
2461   if (state < GST_STATE_PAUSED) {
2462     GtkAllocation allocation;
2463     GdkWindow *window = gtk_widget_get_window (widget);
2464     cairo_t *cr;
2465
2466     gtk_widget_get_allocation (widget, &allocation);
2467     cr = gdk_cairo_create (window);
2468     cairo_set_source_rgb (cr, 0, 0, 0);
2469     cairo_rectangle (cr, 0, 0, allocation.width, allocation.height);
2470     cairo_fill (cr);
2471     cairo_destroy (cr);
2472   }
2473   return FALSE;
2474 }
2475
2476 static void
2477 realize_cb (GtkWidget * widget, gpointer data)
2478 {
2479 #if GTK_CHECK_VERSION(2,18,0)
2480   {
2481     GdkWindow *window = gtk_widget_get_window (widget);
2482
2483     /* This is here just for pedagogical purposes, GDK_WINDOW_XID will call it
2484      * as well */
2485     if (!gdk_window_ensure_native (window))
2486       g_error ("Couldn't create native window needed for GstXOverlay!");
2487   }
2488 #endif
2489
2490 #ifdef HAVE_X
2491   {
2492     GdkWindow *window = gtk_widget_get_window (video_window);
2493
2494     embed_xid = GDK_WINDOW_XID (window);
2495     g_print ("Window realize: video window XID = %lu\n", embed_xid);
2496   }
2497 #endif
2498 }
2499
2500 static void
2501 msg_eos (GstBus * bus, GstMessage * message, GstPipeline * data)
2502 {
2503   message_received (bus, message, data);
2504
2505   /* Set new uri for playerbins and continue playback */
2506   if (l && (pipeline_type == 14 || pipeline_type == 16)) {
2507     stop_cb (NULL, NULL);
2508     l = g_list_next (l);
2509     if (l) {
2510       playerbin_set_uri (GST_ELEMENT (data), l->data);
2511       play_cb (NULL, NULL);
2512     }
2513   }
2514 }
2515
2516 static void
2517 msg_step_done (GstBus * bus, GstMessage * message, GstPipeline * data)
2518 {
2519   if (!shuttling)
2520     message_received (bus, message, data);
2521 }
2522
2523 static void
2524 connect_bus_signals (GstElement * pipeline)
2525 {
2526   GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
2527
2528 #ifdef HAVE_X
2529   /* handle prepare-xwindow-id element message synchronously */
2530   gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bus_sync_handler,
2531       pipeline);
2532 #endif
2533
2534   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
2535   gst_bus_enable_sync_message_emission (bus);
2536
2537   g_signal_connect (bus, "message::state-changed",
2538       (GCallback) msg_state_changed, pipeline);
2539   g_signal_connect (bus, "message::segment-done", (GCallback) msg_segment_done,
2540       pipeline);
2541   g_signal_connect (bus, "message::async-done", (GCallback) msg_async_done,
2542       pipeline);
2543
2544   g_signal_connect (bus, "message::new-clock", (GCallback) message_received,
2545       pipeline);
2546   g_signal_connect (bus, "message::clock-lost", (GCallback) msg_clock_lost,
2547       pipeline);
2548   g_signal_connect (bus, "message::error", (GCallback) message_received,
2549       pipeline);
2550   g_signal_connect (bus, "message::warning", (GCallback) message_received,
2551       pipeline);
2552   g_signal_connect (bus, "message::eos", (GCallback) msg_eos, pipeline);
2553   g_signal_connect (bus, "message::tag", (GCallback) message_received,
2554       pipeline);
2555   g_signal_connect (bus, "message::element", (GCallback) message_received,
2556       pipeline);
2557   g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
2558       pipeline);
2559   g_signal_connect (bus, "message::buffering", (GCallback) msg_buffering,
2560       pipeline);
2561 //  g_signal_connect (bus, "message::step-done", (GCallback) msg_step_done,
2562 //      pipeline);
2563   g_signal_connect (bus, "message::step-start", (GCallback) msg_step_done,
2564       pipeline);
2565   g_signal_connect (bus, "sync-message::step-done",
2566       (GCallback) msg_sync_step_done, pipeline);
2567
2568   gst_object_unref (bus);
2569 }
2570
2571 /* Return GList of paths described in location string */
2572 static GList *
2573 handle_wildcards (const gchar * location)
2574 {
2575   GList *res = NULL;
2576   gchar *path = g_path_get_dirname (location);
2577   gchar *pattern = g_path_get_basename (location);
2578   GPatternSpec *pspec = g_pattern_spec_new (pattern);
2579   GDir *dir = g_dir_open (path, 0, NULL);
2580   const gchar *name;
2581
2582   g_print ("matching %s from %s\n", pattern, path);
2583
2584   if (!dir) {
2585     g_print ("opening directory %s failed\n", path);
2586     goto out;
2587   }
2588
2589   while ((name = g_dir_read_name (dir)) != NULL) {
2590     if (g_pattern_match_string (pspec, name)) {
2591       res = g_list_append (res, g_strjoin ("/", path, name, NULL));
2592       g_print ("  found clip %s\n", name);
2593     }
2594   }
2595
2596   g_dir_close (dir);
2597 out:
2598   g_pattern_spec_free (pspec);
2599   g_free (pattern);
2600   g_free (path);
2601
2602   return res;
2603 }
2604
2605 static void
2606 delete_event_cb (void)
2607 {
2608   stop_cb (NULL, NULL);
2609   gtk_main_quit ();
2610 }
2611
2612 static void
2613 print_usage (int argc, char **argv)
2614 {
2615   gint i;
2616
2617   g_print ("usage: %s <type> <filename>\n", argv[0]);
2618   g_print ("   possible types:\n");
2619
2620   for (i = 0; i < NUM_TYPES; i++) {
2621     g_print ("     %d = %s\n", i, pipelines[i].name);
2622   }
2623 }
2624
2625 int
2626 main (int argc, char **argv)
2627 {
2628   GtkWidget *window, *hbox, *vbox, *panel, *expander, *pb2vbox, *boxes,
2629       *flagtable, *boxes2, *step;
2630   GtkWidget *play_button, *pause_button, *stop_button, *shot_button;
2631   GtkWidget *accurate_checkbox, *key_checkbox, *loop_checkbox, *flush_checkbox;
2632   GtkWidget *scrub_checkbox, *play_scrub_checkbox;
2633   GtkWidget *rate_label, *volume_label;
2634   GOptionEntry options[] = {
2635     {"stats", 's', 0, G_OPTION_ARG_NONE, &stats,
2636         "Show pad stats", NULL},
2637     {"elem", 'e', 0, G_OPTION_ARG_NONE, &elem_seek,
2638         "Seek on elements instead of pads", NULL},
2639     {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
2640         "Verbose properties", NULL},
2641     {NULL}
2642   };
2643   GOptionContext *ctx;
2644   GError *err = NULL;
2645
2646   if (!g_thread_supported ())
2647     g_thread_init (NULL);
2648
2649   ctx = g_option_context_new ("- test seeking in gsteamer");
2650   g_option_context_add_main_entries (ctx, options, NULL);
2651   g_option_context_add_group (ctx, gst_init_get_option_group ());
2652   g_option_context_add_group (ctx, gtk_get_option_group (TRUE));
2653
2654   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
2655     g_print ("Error initializing: %s\n", err->message);
2656     exit (1);
2657   }
2658
2659   GST_DEBUG_CATEGORY_INIT (seek_debug, "seek", 0, "seek example");
2660
2661   if (argc != 3) {
2662     print_usage (argc, argv);
2663     exit (-1);
2664   }
2665
2666   pipeline_type = atoi (argv[1]);
2667
2668   if (pipeline_type < 0 || pipeline_type >= NUM_TYPES) {
2669     print_usage (argc, argv);
2670     exit (-1);
2671   }
2672
2673   pipeline_spec = argv[2];
2674
2675   if (g_path_is_absolute (pipeline_spec) &&
2676       (g_strrstr (pipeline_spec, "*") != NULL ||
2677           g_strrstr (pipeline_spec, "?") != NULL)) {
2678     paths = handle_wildcards (pipeline_spec);
2679   } else {
2680     paths = g_list_prepend (paths, g_strdup (pipeline_spec));
2681   }
2682
2683   if (!paths) {
2684     g_print ("opening %s failed\n", pipeline_spec);
2685     exit (-1);
2686   }
2687
2688   l = paths;
2689
2690   pipeline = pipelines[pipeline_type].func ((gchar *) l->data);
2691   g_assert (pipeline);
2692
2693   /* initialize gui elements ... */
2694   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
2695   video_window = gtk_drawing_area_new ();
2696   g_signal_connect (video_window, "expose-event",
2697       G_CALLBACK (handle_expose_cb), NULL);
2698   g_signal_connect (video_window, "realize", G_CALLBACK (realize_cb), NULL);
2699   gtk_widget_set_double_buffered (video_window, FALSE);
2700
2701   statusbar = gtk_statusbar_new ();
2702   status_id = gtk_statusbar_get_context_id (GTK_STATUSBAR (statusbar), "seek");
2703   gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Stopped");
2704   hbox = gtk_hbox_new (FALSE, 0);
2705   vbox = gtk_vbox_new (FALSE, 0);
2706   flagtable = gtk_table_new (4, 2, FALSE);
2707   gtk_container_set_border_width (GTK_CONTAINER (vbox), 3);
2708
2709   /* media controls */
2710   play_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_PLAY);
2711   pause_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_PAUSE);
2712   stop_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_STOP);
2713
2714   /* seek flags */
2715   accurate_checkbox = gtk_check_button_new_with_label ("Accurate Seek");
2716   key_checkbox = gtk_check_button_new_with_label ("Key-unit Seek");
2717   loop_checkbox = gtk_check_button_new_with_label ("Loop");
2718   flush_checkbox = gtk_check_button_new_with_label ("Flush");
2719   scrub_checkbox = gtk_check_button_new_with_label ("Scrub");
2720   play_scrub_checkbox = gtk_check_button_new_with_label ("Play Scrub");
2721   skip_checkbox = gtk_check_button_new_with_label ("Play Skip");
2722   rate_spinbutton = gtk_spin_button_new_with_range (-100, 100, 0.1);
2723   gtk_spin_button_set_digits (GTK_SPIN_BUTTON (rate_spinbutton), 3);
2724   rate_label = gtk_label_new ("Rate");
2725
2726   gtk_widget_set_tooltip_text (accurate_checkbox,
2727       "accurate position is requested, this might be considerably slower for some formats");
2728   gtk_widget_set_tooltip_text (key_checkbox,
2729       "seek to the nearest keyframe. This might be faster but less accurate");
2730   gtk_widget_set_tooltip_text (loop_checkbox, "loop playback");
2731   gtk_widget_set_tooltip_text (flush_checkbox, "flush pipeline after seeking");
2732   gtk_widget_set_tooltip_text (rate_spinbutton, "define the playback rate, "
2733       "negative value trigger reverse playback");
2734   gtk_widget_set_tooltip_text (scrub_checkbox, "show images while seeking");
2735   gtk_widget_set_tooltip_text (play_scrub_checkbox, "play video while seeking");
2736   gtk_widget_set_tooltip_text (skip_checkbox,
2737       "Skip frames while playing at high frame rates");
2738
2739   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (flush_checkbox), TRUE);
2740   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (scrub_checkbox), TRUE);
2741
2742   gtk_spin_button_set_value (GTK_SPIN_BUTTON (rate_spinbutton), rate);
2743
2744   /* step expander */
2745   {
2746     GtkWidget *hbox;
2747
2748     step = gtk_expander_new ("step options");
2749     hbox = gtk_hbox_new (FALSE, 0);
2750
2751     format_combo = gtk_combo_box_text_new ();
2752     gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (format_combo),
2753         "frames");
2754     gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (format_combo),
2755         "time (ms)");
2756     gtk_combo_box_set_active (GTK_COMBO_BOX (format_combo), 0);
2757     gtk_box_pack_start (GTK_BOX (hbox), format_combo, FALSE, FALSE, 2);
2758
2759     step_amount_spinbutton = gtk_spin_button_new_with_range (1, 1000, 1);
2760     gtk_spin_button_set_digits (GTK_SPIN_BUTTON (step_amount_spinbutton), 0);
2761     gtk_spin_button_set_value (GTK_SPIN_BUTTON (step_amount_spinbutton), 1.0);
2762     gtk_box_pack_start (GTK_BOX (hbox), step_amount_spinbutton, FALSE, FALSE,
2763         2);
2764
2765     step_rate_spinbutton = gtk_spin_button_new_with_range (0.0, 100, 0.1);
2766     gtk_spin_button_set_digits (GTK_SPIN_BUTTON (step_rate_spinbutton), 3);
2767     gtk_spin_button_set_value (GTK_SPIN_BUTTON (step_rate_spinbutton), 1.0);
2768     gtk_box_pack_start (GTK_BOX (hbox), step_rate_spinbutton, FALSE, FALSE, 2);
2769
2770     step_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_FORWARD);
2771     gtk_button_set_label (GTK_BUTTON (step_button), "Step");
2772     gtk_box_pack_start (GTK_BOX (hbox), step_button, FALSE, FALSE, 2);
2773
2774     g_signal_connect (G_OBJECT (step_button), "clicked", G_CALLBACK (step_cb),
2775         pipeline);
2776
2777     /* shuttle scale */
2778     shuttle_checkbox = gtk_check_button_new_with_label ("Shuttle");
2779     gtk_box_pack_start (GTK_BOX (hbox), shuttle_checkbox, FALSE, FALSE, 2);
2780     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (shuttle_checkbox), FALSE);
2781     g_signal_connect (shuttle_checkbox, "toggled", G_CALLBACK (shuttle_toggled),
2782         pipeline);
2783
2784     shuttle_adjustment =
2785         GTK_ADJUSTMENT (gtk_adjustment_new (0.0, -3.00, 4.0, 0.1, 1.0, 1.0));
2786     shuttle_hscale = gtk_hscale_new (shuttle_adjustment);
2787     gtk_scale_set_digits (GTK_SCALE (shuttle_hscale), 2);
2788     gtk_scale_set_value_pos (GTK_SCALE (shuttle_hscale), GTK_POS_TOP);
2789     gtk_range_set_update_policy (GTK_RANGE (shuttle_hscale),
2790         GTK_UPDATE_CONTINUOUS);
2791     g_signal_connect (shuttle_hscale, "value_changed",
2792         G_CALLBACK (shuttle_value_changed), pipeline);
2793     g_signal_connect (shuttle_hscale, "format_value",
2794         G_CALLBACK (shuttle_format_value), pipeline);
2795
2796     gtk_box_pack_start (GTK_BOX (hbox), shuttle_hscale, TRUE, TRUE, 2);
2797
2798     gtk_container_add (GTK_CONTAINER (step), hbox);
2799   }
2800
2801   /* seek bar */
2802   adjustment =
2803       GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.00, N_GRAD, 0.1, 1.0, 1.0));
2804   hscale = gtk_hscale_new (adjustment);
2805   gtk_scale_set_digits (GTK_SCALE (hscale), 2);
2806   gtk_scale_set_value_pos (GTK_SCALE (hscale), GTK_POS_RIGHT);
2807   gtk_range_set_show_fill_level (GTK_RANGE (hscale), TRUE);
2808   gtk_range_set_fill_level (GTK_RANGE (hscale), N_GRAD);
2809   gtk_range_set_update_policy (GTK_RANGE (hscale), GTK_UPDATE_CONTINUOUS);
2810
2811   g_signal_connect (hscale, "button_press_event", G_CALLBACK (start_seek),
2812       pipeline);
2813   g_signal_connect (hscale, "button_release_event", G_CALLBACK (stop_seek),
2814       pipeline);
2815   g_signal_connect (hscale, "format_value", G_CALLBACK (format_value),
2816       pipeline);
2817
2818   if (pipeline_type == 16) {
2819     /* the playbin2 panel controls for the video/audio/subtitle tracks */
2820     panel = gtk_hbox_new (FALSE, 0);
2821     video_combo = gtk_combo_box_text_new ();
2822     audio_combo = gtk_combo_box_text_new ();
2823     text_combo = gtk_combo_box_text_new ();
2824     gtk_widget_set_sensitive (video_combo, FALSE);
2825     gtk_widget_set_sensitive (audio_combo, FALSE);
2826     gtk_widget_set_sensitive (text_combo, FALSE);
2827     gtk_box_pack_start (GTK_BOX (panel), video_combo, TRUE, TRUE, 2);
2828     gtk_box_pack_start (GTK_BOX (panel), audio_combo, TRUE, TRUE, 2);
2829     gtk_box_pack_start (GTK_BOX (panel), text_combo, TRUE, TRUE, 2);
2830     g_signal_connect (G_OBJECT (video_combo), "changed",
2831         G_CALLBACK (video_combo_cb), pipeline);
2832     g_signal_connect (G_OBJECT (audio_combo), "changed",
2833         G_CALLBACK (audio_combo_cb), pipeline);
2834     g_signal_connect (G_OBJECT (text_combo), "changed",
2835         G_CALLBACK (text_combo_cb), pipeline);
2836     /* playbin2 panel for flag checkboxes and volume/mute */
2837     boxes = gtk_hbox_new (FALSE, 0);
2838     vis_checkbox = gtk_check_button_new_with_label ("Vis");
2839     video_checkbox = gtk_check_button_new_with_label ("Video");
2840     audio_checkbox = gtk_check_button_new_with_label ("Audio");
2841     text_checkbox = gtk_check_button_new_with_label ("Text");
2842     mute_checkbox = gtk_check_button_new_with_label ("Mute");
2843     download_checkbox = gtk_check_button_new_with_label ("Download");
2844     buffer_checkbox = gtk_check_button_new_with_label ("Buffer");
2845     volume_label = gtk_label_new ("Volume");
2846     volume_spinbutton = gtk_spin_button_new_with_range (0, 10.0, 0.1);
2847     gtk_spin_button_set_value (GTK_SPIN_BUTTON (volume_spinbutton), 1.0);
2848     gtk_box_pack_start (GTK_BOX (boxes), video_checkbox, TRUE, TRUE, 2);
2849     gtk_box_pack_start (GTK_BOX (boxes), audio_checkbox, TRUE, TRUE, 2);
2850     gtk_box_pack_start (GTK_BOX (boxes), text_checkbox, TRUE, TRUE, 2);
2851     gtk_box_pack_start (GTK_BOX (boxes), vis_checkbox, TRUE, TRUE, 2);
2852     gtk_box_pack_start (GTK_BOX (boxes), mute_checkbox, TRUE, TRUE, 2);
2853     gtk_box_pack_start (GTK_BOX (boxes), download_checkbox, TRUE, TRUE, 2);
2854     gtk_box_pack_start (GTK_BOX (boxes), buffer_checkbox, TRUE, TRUE, 2);
2855     gtk_box_pack_start (GTK_BOX (boxes), volume_label, TRUE, TRUE, 2);
2856     gtk_box_pack_start (GTK_BOX (boxes), volume_spinbutton, TRUE, TRUE, 2);
2857     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (vis_checkbox), FALSE);
2858     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (audio_checkbox), TRUE);
2859     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (video_checkbox), TRUE);
2860     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (text_checkbox), TRUE);
2861     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (mute_checkbox), FALSE);
2862     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (download_checkbox), FALSE);
2863     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (buffer_checkbox), FALSE);
2864     g_signal_connect (G_OBJECT (vis_checkbox), "toggled",
2865         G_CALLBACK (vis_toggle_cb), pipeline);
2866     g_signal_connect (G_OBJECT (audio_checkbox), "toggled",
2867         G_CALLBACK (audio_toggle_cb), pipeline);
2868     g_signal_connect (G_OBJECT (video_checkbox), "toggled",
2869         G_CALLBACK (video_toggle_cb), pipeline);
2870     g_signal_connect (G_OBJECT (text_checkbox), "toggled",
2871         G_CALLBACK (text_toggle_cb), pipeline);
2872     g_signal_connect (G_OBJECT (mute_checkbox), "toggled",
2873         G_CALLBACK (mute_toggle_cb), pipeline);
2874     g_signal_connect (G_OBJECT (download_checkbox), "toggled",
2875         G_CALLBACK (download_toggle_cb), pipeline);
2876     g_signal_connect (G_OBJECT (buffer_checkbox), "toggled",
2877         G_CALLBACK (buffer_toggle_cb), pipeline);
2878     g_signal_connect (G_OBJECT (volume_spinbutton), "value_changed",
2879         G_CALLBACK (volume_spinbutton_changed_cb), pipeline);
2880     /* playbin2 panel for snapshot */
2881     boxes2 = gtk_hbox_new (FALSE, 0);
2882     shot_button = gtk_button_new_from_stock (GTK_STOCK_SAVE);
2883     gtk_widget_set_tooltip_text (shot_button,
2884         "save a screenshot .png in the current directory");
2885     g_signal_connect (G_OBJECT (shot_button), "clicked", G_CALLBACK (shot_cb),
2886         pipeline);
2887     vis_combo = gtk_combo_box_text_new ();
2888     g_signal_connect (G_OBJECT (vis_combo), "changed",
2889         G_CALLBACK (vis_combo_cb), pipeline);
2890     gtk_widget_set_sensitive (vis_combo, FALSE);
2891     gtk_box_pack_start (GTK_BOX (boxes2), shot_button, TRUE, TRUE, 2);
2892     gtk_box_pack_start (GTK_BOX (boxes2), vis_combo, TRUE, TRUE, 2);
2893
2894     /* fill the vis combo box and the array of factories */
2895     init_visualization_features ();
2896   } else {
2897     panel = boxes = boxes2 = NULL;
2898   }
2899
2900   /* do the packing stuff ... */
2901   gtk_window_set_default_size (GTK_WINDOW (window), 250, 96);
2902   /* FIXME: can we avoid this for audio only? */
2903   gtk_widget_set_size_request (GTK_WIDGET (video_window), -1,
2904       DEFAULT_VIDEO_HEIGHT);
2905   gtk_container_add (GTK_CONTAINER (window), vbox);
2906   gtk_box_pack_start (GTK_BOX (vbox), video_window, TRUE, TRUE, 2);
2907   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 2);
2908   gtk_box_pack_start (GTK_BOX (hbox), play_button, FALSE, FALSE, 2);
2909   gtk_box_pack_start (GTK_BOX (hbox), pause_button, FALSE, FALSE, 2);
2910   gtk_box_pack_start (GTK_BOX (hbox), stop_button, FALSE, FALSE, 2);
2911   gtk_box_pack_start (GTK_BOX (hbox), flagtable, FALSE, FALSE, 2);
2912   gtk_table_attach_defaults (GTK_TABLE (flagtable), accurate_checkbox, 0, 1, 0,
2913       1);
2914   gtk_table_attach_defaults (GTK_TABLE (flagtable), flush_checkbox, 1, 2, 0, 1);
2915   gtk_table_attach_defaults (GTK_TABLE (flagtable), loop_checkbox, 2, 3, 0, 1);
2916   gtk_table_attach_defaults (GTK_TABLE (flagtable), key_checkbox, 0, 1, 1, 2);
2917   gtk_table_attach_defaults (GTK_TABLE (flagtable), scrub_checkbox, 1, 2, 1, 2);
2918   gtk_table_attach_defaults (GTK_TABLE (flagtable), play_scrub_checkbox, 2, 3,
2919       1, 2);
2920   gtk_table_attach_defaults (GTK_TABLE (flagtable), skip_checkbox, 3, 4, 0, 1);
2921   gtk_table_attach_defaults (GTK_TABLE (flagtable), rate_label, 4, 5, 0, 1);
2922   gtk_table_attach_defaults (GTK_TABLE (flagtable), rate_spinbutton, 4, 5, 1,
2923       2);
2924   if (panel && boxes && boxes2) {
2925     expander = gtk_expander_new ("playbin2 options");
2926     pb2vbox = gtk_vbox_new (FALSE, 0);
2927     gtk_box_pack_start (GTK_BOX (pb2vbox), panel, FALSE, FALSE, 2);
2928     gtk_box_pack_start (GTK_BOX (pb2vbox), boxes, FALSE, FALSE, 2);
2929     gtk_box_pack_start (GTK_BOX (pb2vbox), boxes2, FALSE, FALSE, 2);
2930     gtk_container_add (GTK_CONTAINER (expander), pb2vbox);
2931     gtk_box_pack_start (GTK_BOX (vbox), expander, FALSE, FALSE, 2);
2932   }
2933   gtk_box_pack_start (GTK_BOX (vbox), step, FALSE, FALSE, 2);
2934   gtk_box_pack_start (GTK_BOX (vbox), hscale, FALSE, FALSE, 2);
2935   gtk_box_pack_start (GTK_BOX (vbox), statusbar, FALSE, FALSE, 2);
2936
2937   /* connect things ... */
2938   g_signal_connect (G_OBJECT (play_button), "clicked", G_CALLBACK (play_cb),
2939       pipeline);
2940   g_signal_connect (G_OBJECT (pause_button), "clicked", G_CALLBACK (pause_cb),
2941       pipeline);
2942   g_signal_connect (G_OBJECT (stop_button), "clicked", G_CALLBACK (stop_cb),
2943       pipeline);
2944   g_signal_connect (G_OBJECT (accurate_checkbox), "toggled",
2945       G_CALLBACK (accurate_toggle_cb), pipeline);
2946   g_signal_connect (G_OBJECT (key_checkbox), "toggled",
2947       G_CALLBACK (key_toggle_cb), pipeline);
2948   g_signal_connect (G_OBJECT (loop_checkbox), "toggled",
2949       G_CALLBACK (loop_toggle_cb), pipeline);
2950   g_signal_connect (G_OBJECT (flush_checkbox), "toggled",
2951       G_CALLBACK (flush_toggle_cb), pipeline);
2952   g_signal_connect (G_OBJECT (scrub_checkbox), "toggled",
2953       G_CALLBACK (scrub_toggle_cb), pipeline);
2954   g_signal_connect (G_OBJECT (play_scrub_checkbox), "toggled",
2955       G_CALLBACK (play_scrub_toggle_cb), pipeline);
2956   g_signal_connect (G_OBJECT (skip_checkbox), "toggled",
2957       G_CALLBACK (skip_toggle_cb), pipeline);
2958   g_signal_connect (G_OBJECT (rate_spinbutton), "value_changed",
2959       G_CALLBACK (rate_spinbutton_changed_cb), pipeline);
2960
2961   g_signal_connect (G_OBJECT (window), "delete-event", delete_event_cb, NULL);
2962
2963   /* show the gui. */
2964   gtk_widget_show_all (window);
2965
2966   /* realize window now so that the video window gets created and we can
2967    * obtain its XID before the pipeline is started up and the videosink
2968    * asks for the XID of the window to render onto */
2969   gtk_widget_realize (window);
2970
2971 #ifdef HAVE_X
2972   /* we should have the XID now */
2973   g_assert (embed_xid != 0);
2974 #endif
2975
2976   if (verbose) {
2977     g_signal_connect (pipeline, "deep_notify",
2978         G_CALLBACK (gst_object_default_deep_notify), NULL);
2979   }
2980
2981   connect_bus_signals (pipeline);
2982   gtk_main ();
2983
2984   g_print ("NULL pipeline\n");
2985   gst_element_set_state (pipeline, GST_STATE_NULL);
2986
2987   g_print ("free pipeline\n");
2988   gst_object_unref (pipeline);
2989
2990   g_list_foreach (paths, (GFunc) g_free, NULL);
2991   g_list_free (paths);
2992
2993   return 0;
2994 }