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