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