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