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