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