ef3fce4a74d18b9a9e80b782e56ba09c9a5b9a48
[platform/upstream/gstreamer.git] / tests / examples / seek / seek.c
1 /* GStreamer
2  *
3  * seek.c: seeking sample application
4  *
5  * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
6  *               2006 Stefan Kost <ensonic@users.sf.net>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <stdlib.h>
29 #include <math.h>
30 #include <glib.h>
31 #include <gtk/gtk.h>
32 #include <gst/gst.h>
33 #include <string.h>
34
35 #ifdef HAVE_X
36 #include <gdk/gdkx.h>
37 #endif
38 #ifdef G_OS_WIN32
39 #include <gdk/gdkwin32.h>
40 #endif
41
42 #include <gst/interfaces/xoverlay.h>
43
44 #if (!GTK_CHECK_VERSION(2, 23, 0) || GTK_CHECK_VERSION(2, 90, 0)) && !GTK_CHECK_VERSION(2, 91, 1)
45 #define gtk_combo_box_text_new gtk_combo_box_new_text
46 #define gtk_combo_box_text_append_text gtk_combo_box_append_text
47 #define gtk_combo_box_text_remove gtk_combo_box_remove_text
48 #define GTK_COMBO_BOX_TEXT GTK_COMBO_BOX
49 #endif
50
51 GST_DEBUG_CATEGORY_STATIC (seek_debug);
52 #define GST_CAT_DEFAULT (seek_debug)
53
54 #if !GTK_CHECK_VERSION (2, 17, 7)
55 static void
56 gtk_widget_get_allocation (GtkWidget * w, GtkAllocation * a)
57 {
58   *a = w->allocation;
59 }
60 #endif
61
62 /* configuration */
63
64 #define SOURCE "filesrc"
65
66 #define ASINK DEFAULT_AUDIOSINK
67 //#define ASINK "pulsesink"
68 //#define ASINK "alsasink"
69 //#define ASINK "osssink"
70 //#define ASINK "directsoundsink"
71
72 #ifdef G_OS_WIN32
73 #define VSINK "directdrawsink"
74 #else
75 #define VSINK "xvimagesink"
76 #endif
77 //#define VSINK "sdlvideosink"
78 //#define VSINK "ximagesink"
79 //#define VSINK "aasink"
80 //#define VSINK "cacasink"
81 //#define VSINK "dshowvideosink"
82 //#define VSINK "sdlvideosink"
83 //#define VSINK "glimagesink"
84
85 #define FILL_INTERVAL 100
86 //#define UPDATE_INTERVAL 500
87 //#define UPDATE_INTERVAL 100
88 #define UPDATE_INTERVAL 40
89
90 /* number of milliseconds to play for after a seek */
91 #define SCRUB_TIME 100
92
93 /* timeout for gst_element_get_state() after a seek */
94 #define SEEK_TIMEOUT 40 * GST_MSECOND
95
96 #define DEFAULT_VIDEO_HEIGHT 300
97
98 /* the state to go to when stop is pressed */
99 #define STOP_STATE      GST_STATE_READY
100
101 #define N_GRAD 1000.0
102
103 static GList *seekable_pads = NULL;
104 static GList *rate_pads = NULL;
105 static GList *seekable_elements = NULL;
106
107 static gboolean accurate_seek = FALSE;
108 static gboolean keyframe_seek = FALSE;
109 static gboolean loop_seek = FALSE;
110 static gboolean flush_seek = TRUE;
111 static gboolean scrub = TRUE;
112 static gboolean play_scrub = FALSE;
113 static gboolean skip_seek = FALSE;
114 static gdouble rate = 1.0;
115
116 static GstElement *pipeline;
117 static gint pipeline_type;
118 static const gchar *pipeline_spec;
119 static gint64 position = -1;
120 static gint64 duration = -1;
121 static GtkAdjustment *adjustment;
122 static GtkWidget *hscale, *statusbar;
123 static guint status_id = 0;
124 static gboolean stats = FALSE;
125 static gboolean elem_seek = FALSE;
126 static gboolean verbose = FALSE;
127
128 static gboolean is_live = FALSE;
129 static gboolean buffering = FALSE;
130 static GstBufferingMode mode;
131 static gint64 buffering_left;
132 static GstState state = GST_STATE_NULL;
133 static guint update_id = 0;
134 static guint seek_timeout_id = 0;
135 static gulong changed_id;
136 static guint fill_id = 0;
137
138 static gint n_video = 0, n_audio = 0, n_text = 0;
139 static gboolean need_streams = TRUE;
140 static GtkWidget *video_combo, *audio_combo, *text_combo, *vis_combo;
141 static GtkWidget *vis_checkbox, *video_checkbox, *audio_checkbox;
142 static GtkWidget *text_checkbox, *mute_checkbox, *volume_spinbutton;
143 static GtkWidget *skip_checkbox, *video_window, *download_checkbox;
144 static GtkWidget *buffer_checkbox, *rate_spinbutton;
145
146 static GStaticMutex state_mutex = G_STATIC_MUTEX_INIT;
147
148 static GtkWidget *format_combo, *step_amount_spinbutton, *step_rate_spinbutton;
149 static GtkWidget *shuttle_checkbox, *step_button;
150 static GtkWidget *shuttle_hscale;
151 static GtkAdjustment *shuttle_adjustment;
152
153 static GList *paths = NULL, *l = NULL;
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, "/") || !gst_uri_is_valid (location)) {
935     uri = gst_filename_to_uri (location, NULL);
936     g_print ("Setting URI: %s\n", uri);
937     g_object_set (G_OBJECT (player), "uri", uri, NULL);
938     g_free (uri);
939   } else {
940     g_print ("Setting URI: %s\n", location);
941     g_object_set (G_OBJECT (player), "uri", location, NULL);
942   }
943 }
944
945 static GstElement *
946 construct_playerbin (const gchar * name, const gchar * location)
947 {
948   GstElement *player;
949
950   player = gst_element_factory_make (name, "player");
951   g_assert (player);
952
953   playerbin_set_uri (player, location);
954
955   seekable_elements = g_list_prepend (seekable_elements, player);
956
957   /* force element seeking on this pipeline */
958   elem_seek = TRUE;
959
960   return player;
961 }
962
963 static GstElement *
964 make_playerbin_pipeline (const gchar * location)
965 {
966   return construct_playerbin ("playbin", location);
967 }
968
969 static GstElement *
970 make_playerbin2_pipeline (const gchar * location)
971 {
972   GstElement *pipeline = construct_playerbin ("playbin2", location);
973
974   /* FIXME: this is not triggered, playbin2 is not forwarding it from the sink */
975   g_signal_connect (pipeline, "notify::volume", G_CALLBACK (volume_notify_cb),
976       NULL);
977   return pipeline;
978 }
979
980 #ifndef GST_DISABLE_PARSE
981 static GstElement *
982 make_parselaunch_pipeline (const gchar * description)
983 {
984   GstElement *pipeline;
985   GError *error = NULL;
986
987   pipeline = gst_parse_launch (description, &error);
988
989   seekable_elements = g_list_prepend (seekable_elements, pipeline);
990
991   elem_seek = TRUE;
992
993   return pipeline;
994 }
995 #endif
996
997 typedef struct
998 {
999   const gchar *name;
1000   GstElement *(*func) (const gchar * location);
1001 }
1002 Pipeline;
1003
1004 static Pipeline pipelines[] = {
1005   {"mp3", make_mp3_pipeline},
1006   {"avi", make_avi_pipeline},
1007   {"mpeg1", make_mpeg_pipeline},
1008   {"mpegparse", make_parse_pipeline},
1009   {"vorbis", make_vorbis_pipeline},
1010   {"theora", make_theora_pipeline},
1011   {"ogg/v/t", make_vorbis_theora_pipeline},
1012   {"avi/msmpeg4v3/mp3", make_avi_msmpeg4v3_mp3_pipeline},
1013   {"sid", make_sid_pipeline},
1014   {"flac", make_flac_pipeline},
1015   {"wav", make_wav_pipeline},
1016   {"mod", make_mod_pipeline},
1017   {"dv", make_dv_pipeline},
1018   {"mpeg1nothreads", make_mpegnt_pipeline},
1019   {"playerbin", make_playerbin_pipeline},
1020 #ifndef GST_DISABLE_PARSE
1021   {"parse-launch", make_parselaunch_pipeline},
1022 #endif
1023   {"playerbin2", make_playerbin2_pipeline},
1024   {NULL, NULL},
1025 };
1026
1027 #define NUM_TYPES       ((sizeof (pipelines) / sizeof (Pipeline)) - 1)
1028
1029 /* ui callbacks and helpers */
1030
1031 static gchar *
1032 format_value (GtkScale * scale, gdouble value)
1033 {
1034   gint64 real;
1035   gint64 seconds;
1036   gint64 subseconds;
1037
1038   real = value * duration / N_GRAD;
1039   seconds = (gint64) real / GST_SECOND;
1040   subseconds = (gint64) real / (GST_SECOND / N_GRAD);
1041
1042   return g_strdup_printf ("%02" G_GINT64_FORMAT ":%02" G_GINT64_FORMAT ":%02"
1043       G_GINT64_FORMAT, seconds / 60, seconds % 60, subseconds % 100);
1044 }
1045
1046
1047 static gchar *
1048 shuttle_format_value (GtkScale * scale, gdouble value)
1049 {
1050   return g_strdup_printf ("%0.*g", gtk_scale_get_digits (scale), value);
1051 }
1052
1053 typedef struct
1054 {
1055   const gchar *name;
1056   const GstFormat format;
1057 }
1058 seek_format;
1059
1060 static seek_format seek_formats[] = {
1061   {"tim", GST_FORMAT_TIME},
1062   {"byt", GST_FORMAT_BYTES},
1063   {"buf", GST_FORMAT_BUFFERS},
1064   {"def", GST_FORMAT_DEFAULT},
1065   {NULL, 0},
1066 };
1067
1068 G_GNUC_UNUSED static void
1069 query_rates (void)
1070 {
1071   GList *walk = rate_pads;
1072
1073   while (walk) {
1074     GstPad *pad = GST_PAD (walk->data);
1075     gint i = 0;
1076
1077     g_print ("rate/sec  %8.8s: ", GST_PAD_NAME (pad));
1078     while (seek_formats[i].name) {
1079       gint64 value;
1080       GstFormat format;
1081
1082       format = seek_formats[i].format;
1083
1084       if (gst_pad_query_convert (pad, GST_FORMAT_TIME, GST_SECOND, &format,
1085               &value)) {
1086         g_print ("%s %13" G_GINT64_FORMAT " | ", seek_formats[i].name, value);
1087       } else {
1088         g_print ("%s %13.13s | ", seek_formats[i].name, "*NA*");
1089       }
1090
1091       i++;
1092     }
1093     g_print (" %s:%s\n", GST_DEBUG_PAD_NAME (pad));
1094
1095     walk = g_list_next (walk);
1096   }
1097 }
1098
1099 G_GNUC_UNUSED static void
1100 query_positions_elems (void)
1101 {
1102   GList *walk = seekable_elements;
1103
1104   while (walk) {
1105     GstElement *element = GST_ELEMENT (walk->data);
1106     gint i = 0;
1107
1108     g_print ("positions %8.8s: ", GST_ELEMENT_NAME (element));
1109     while (seek_formats[i].name) {
1110       gint64 position, total;
1111       GstFormat format;
1112
1113       format = seek_formats[i].format;
1114
1115       if (gst_element_query_position (element, &format, &position) &&
1116           gst_element_query_duration (element, &format, &total)) {
1117         g_print ("%s %13" G_GINT64_FORMAT " / %13" G_GINT64_FORMAT " | ",
1118             seek_formats[i].name, position, total);
1119       } else {
1120         g_print ("%s %13.13s / %13.13s | ", seek_formats[i].name, "*NA*",
1121             "*NA*");
1122       }
1123       i++;
1124     }
1125     g_print (" %s\n", GST_ELEMENT_NAME (element));
1126
1127     walk = g_list_next (walk);
1128   }
1129 }
1130
1131 G_GNUC_UNUSED static void
1132 query_positions_pads (void)
1133 {
1134   GList *walk = seekable_pads;
1135
1136   while (walk) {
1137     GstPad *pad = GST_PAD (walk->data);
1138     gint i = 0;
1139
1140     g_print ("positions %8.8s: ", GST_PAD_NAME (pad));
1141     while (seek_formats[i].name) {
1142       GstFormat format;
1143       gint64 position, total;
1144
1145       format = seek_formats[i].format;
1146
1147       if (gst_pad_query_position (pad, &format, &position) &&
1148           gst_pad_query_duration (pad, &format, &total)) {
1149         g_print ("%s %13" G_GINT64_FORMAT " / %13" G_GINT64_FORMAT " | ",
1150             seek_formats[i].name, position, total);
1151       } else {
1152         g_print ("%s %13.13s / %13.13s | ", seek_formats[i].name, "*NA*",
1153             "*NA*");
1154       }
1155
1156       i++;
1157     }
1158     g_print (" %s:%s\n", GST_DEBUG_PAD_NAME (pad));
1159
1160     walk = g_list_next (walk);
1161   }
1162 }
1163
1164 static gboolean start_seek (GtkWidget * widget, GdkEventButton * event,
1165     gpointer user_data);
1166 static gboolean stop_seek (GtkWidget * widget, GdkEventButton * event,
1167     gpointer user_data);
1168 static void seek_cb (GtkWidget * widget);
1169
1170 static void
1171 set_scale (gdouble value)
1172 {
1173   g_signal_handlers_block_by_func (hscale, (void *) start_seek,
1174       (void *) pipeline);
1175   g_signal_handlers_block_by_func (hscale, (void *) stop_seek,
1176       (void *) pipeline);
1177   g_signal_handlers_block_by_func (hscale, (void *) seek_cb, (void *) pipeline);
1178   gtk_adjustment_set_value (adjustment, value);
1179   g_signal_handlers_unblock_by_func (hscale, (void *) start_seek,
1180       (void *) pipeline);
1181   g_signal_handlers_unblock_by_func (hscale, (void *) stop_seek,
1182       (void *) pipeline);
1183   g_signal_handlers_unblock_by_func (hscale, (void *) seek_cb,
1184       (void *) pipeline);
1185   gtk_widget_queue_draw (hscale);
1186 }
1187
1188 static gboolean
1189 update_fill (gpointer data)
1190 {
1191   if (elem_seek) {
1192     if (seekable_elements) {
1193       GstElement *element = GST_ELEMENT (seekable_elements->data);
1194       GstQuery *query;
1195
1196       query = gst_query_new_buffering (GST_FORMAT_PERCENT);
1197       if (gst_element_query (element, query)) {
1198         gint64 start, stop, buffering_total;
1199         GstFormat format;
1200         gdouble fill;
1201         gboolean busy;
1202         gint percent;
1203         GstBufferingMode mode;
1204         gint avg_in, avg_out;
1205         gint64 buffering_left;
1206
1207         gst_query_parse_buffering_percent (query, &busy, &percent);
1208         gst_query_parse_buffering_range (query, &format, &start, &stop,
1209             &buffering_total);
1210         gst_query_parse_buffering_stats (query, &mode, &avg_in, &avg_out,
1211             &buffering_left);
1212
1213         /* note that we could start the playback when buffering_left < remaining
1214          * playback time */
1215         GST_DEBUG ("buffering total %" G_GINT64_FORMAT " ms, left %"
1216             G_GINT64_FORMAT " ms", buffering_total, buffering_left);
1217         GST_DEBUG ("start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT,
1218             start, stop);
1219
1220         if (stop != -1)
1221           fill = N_GRAD * stop / GST_FORMAT_PERCENT_MAX;
1222         else
1223           fill = N_GRAD;
1224
1225         gtk_range_set_fill_level (GTK_RANGE (hscale), fill);
1226       }
1227       gst_query_unref (query);
1228     }
1229   }
1230   return TRUE;
1231 }
1232
1233 static gboolean
1234 update_scale (gpointer data)
1235 {
1236   GstFormat format = GST_FORMAT_TIME;
1237
1238   //position = 0;
1239   //duration = 0;
1240
1241   if (elem_seek) {
1242     if (seekable_elements) {
1243       GstElement *element = GST_ELEMENT (seekable_elements->data);
1244
1245       gst_element_query_position (element, &format, &position);
1246       gst_element_query_duration (element, &format, &duration);
1247     }
1248   } else {
1249     if (seekable_pads) {
1250       GstPad *pad = GST_PAD (seekable_pads->data);
1251
1252       gst_pad_query_position (pad, &format, &position);
1253       gst_pad_query_duration (pad, &format, &duration);
1254     }
1255   }
1256
1257   if (stats) {
1258     if (elem_seek) {
1259       query_positions_elems ();
1260     } else {
1261       query_positions_pads ();
1262     }
1263     query_rates ();
1264   }
1265
1266   if (position >= duration)
1267     duration = position;
1268
1269   if (duration > 0) {
1270     set_scale (position * N_GRAD / duration);
1271   }
1272
1273   /* FIXME: see make_playerbin2_pipeline() and volume_notify_cb() */
1274   if (pipeline_type == 16) {
1275     g_object_notify (G_OBJECT (pipeline), "volume");
1276   }
1277
1278   return TRUE;
1279 }
1280
1281 static void do_seek (GtkWidget * widget);
1282 static void connect_bus_signals (GstElement * pipeline);
1283 static void set_update_scale (gboolean active);
1284 static void set_update_fill (gboolean active);
1285
1286 static gboolean
1287 end_scrub (GtkWidget * widget)
1288 {
1289   GST_DEBUG ("end scrub, PAUSE");
1290   gst_element_set_state (pipeline, GST_STATE_PAUSED);
1291   seek_timeout_id = 0;
1292
1293   return FALSE;
1294 }
1295
1296 static gboolean
1297 send_event (GstEvent * event)
1298 {
1299   gboolean res = FALSE;
1300
1301   if (!elem_seek) {
1302     GList *walk = seekable_pads;
1303
1304     while (walk) {
1305       GstPad *seekable = GST_PAD (walk->data);
1306
1307       GST_DEBUG ("send event on pad %s:%s", GST_DEBUG_PAD_NAME (seekable));
1308
1309       gst_event_ref (event);
1310       res = gst_pad_send_event (seekable, event);
1311
1312       walk = g_list_next (walk);
1313     }
1314   } else {
1315     GList *walk = seekable_elements;
1316
1317     while (walk) {
1318       GstElement *seekable = GST_ELEMENT (walk->data);
1319
1320       GST_DEBUG ("send event on element %s", GST_ELEMENT_NAME (seekable));
1321
1322       gst_event_ref (event);
1323       res = gst_element_send_event (seekable, event);
1324
1325       walk = g_list_next (walk);
1326     }
1327   }
1328   gst_event_unref (event);
1329   return res;
1330 }
1331
1332 static void
1333 do_seek (GtkWidget * widget)
1334 {
1335   gint64 real;
1336   gboolean res = FALSE;
1337   GstEvent *s_event;
1338   GstSeekFlags flags;
1339
1340   real = gtk_range_get_value (GTK_RANGE (widget)) * duration / N_GRAD;
1341
1342   GST_DEBUG ("value=%f, real=%" G_GINT64_FORMAT,
1343       gtk_range_get_value (GTK_RANGE (widget)), real);
1344
1345   flags = 0;
1346   if (flush_seek)
1347     flags |= GST_SEEK_FLAG_FLUSH;
1348   if (accurate_seek)
1349     flags |= GST_SEEK_FLAG_ACCURATE;
1350   if (keyframe_seek)
1351     flags |= GST_SEEK_FLAG_KEY_UNIT;
1352   if (loop_seek)
1353     flags |= GST_SEEK_FLAG_SEGMENT;
1354   if (skip_seek)
1355     flags |= GST_SEEK_FLAG_SKIP;
1356
1357   if (rate >= 0) {
1358     s_event = gst_event_new_seek (rate,
1359         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, real, GST_SEEK_TYPE_SET,
1360         GST_CLOCK_TIME_NONE);
1361     GST_DEBUG ("seek with rate %lf to %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT,
1362         rate, GST_TIME_ARGS (real), GST_TIME_ARGS (duration));
1363   } else {
1364     s_event = gst_event_new_seek (rate,
1365         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
1366         GST_SEEK_TYPE_SET, real);
1367     GST_DEBUG ("seek with rate %lf to %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT,
1368         rate, GST_TIME_ARGS (0), GST_TIME_ARGS (real));
1369   }
1370
1371   res = send_event (s_event);
1372
1373   if (res) {
1374     if (flush_seek) {
1375       gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL, SEEK_TIMEOUT);
1376     } else {
1377       set_update_scale (TRUE);
1378     }
1379   } else {
1380     g_print ("seek failed\n");
1381     set_update_scale (TRUE);
1382   }
1383 }
1384
1385 static void
1386 seek_cb (GtkWidget * widget)
1387 {
1388   /* If the timer hasn't expired yet, then the pipeline is running */
1389   if (play_scrub && seek_timeout_id != 0) {
1390     GST_DEBUG ("do scrub seek, PAUSED");
1391     gst_element_set_state (pipeline, GST_STATE_PAUSED);
1392   }
1393
1394   GST_DEBUG ("do seek");
1395   do_seek (widget);
1396
1397   if (play_scrub) {
1398     GST_DEBUG ("do scrub seek, PLAYING");
1399     gst_element_set_state (pipeline, GST_STATE_PLAYING);
1400
1401     if (seek_timeout_id == 0) {
1402       seek_timeout_id =
1403           g_timeout_add (SCRUB_TIME, (GSourceFunc) end_scrub, widget);
1404     }
1405   }
1406 }
1407
1408 static void
1409 set_update_fill (gboolean active)
1410 {
1411   GST_DEBUG ("fill scale is %d", active);
1412
1413   if (active) {
1414     if (fill_id == 0) {
1415       fill_id =
1416           g_timeout_add (FILL_INTERVAL, (GSourceFunc) update_fill, pipeline);
1417     }
1418   } else {
1419     if (fill_id) {
1420       g_source_remove (fill_id);
1421       fill_id = 0;
1422     }
1423   }
1424 }
1425
1426 static void
1427 set_update_scale (gboolean active)
1428 {
1429
1430   GST_DEBUG ("update scale is %d", active);
1431
1432   if (active) {
1433     if (update_id == 0) {
1434       update_id =
1435           g_timeout_add (UPDATE_INTERVAL, (GSourceFunc) update_scale, pipeline);
1436     }
1437   } else {
1438     if (update_id) {
1439       g_source_remove (update_id);
1440       update_id = 0;
1441     }
1442   }
1443 }
1444
1445 static gboolean
1446 start_seek (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
1447 {
1448   if (event->type != GDK_BUTTON_PRESS)
1449     return FALSE;
1450
1451   set_update_scale (FALSE);
1452
1453   if (state == GST_STATE_PLAYING && flush_seek && scrub) {
1454     GST_DEBUG ("start scrub seek, PAUSE");
1455     gst_element_set_state (pipeline, GST_STATE_PAUSED);
1456   }
1457
1458   if (changed_id == 0 && flush_seek && scrub) {
1459     changed_id =
1460         g_signal_connect (hscale, "value_changed", G_CALLBACK (seek_cb),
1461         pipeline);
1462   }
1463
1464   return FALSE;
1465 }
1466
1467 static gboolean
1468 stop_seek (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
1469 {
1470   if (changed_id) {
1471     g_signal_handler_disconnect (hscale, changed_id);
1472     changed_id = 0;
1473   }
1474
1475   if (!flush_seek || !scrub) {
1476     GST_DEBUG ("do final seek");
1477     do_seek (widget);
1478   }
1479
1480   if (seek_timeout_id != 0) {
1481     g_source_remove (seek_timeout_id);
1482     seek_timeout_id = 0;
1483     /* Still scrubbing, so the pipeline is playing, see if we need PAUSED
1484      * instead. */
1485     if (state == GST_STATE_PAUSED) {
1486       GST_DEBUG ("stop scrub seek, PAUSED");
1487       gst_element_set_state (pipeline, GST_STATE_PAUSED);
1488     }
1489   } else {
1490     if (state == GST_STATE_PLAYING) {
1491       GST_DEBUG ("stop scrub seek, PLAYING");
1492       gst_element_set_state (pipeline, GST_STATE_PLAYING);
1493     }
1494   }
1495
1496   return FALSE;
1497 }
1498
1499 static void
1500 play_cb (GtkButton * button, gpointer data)
1501 {
1502   GstStateChangeReturn ret;
1503
1504   if (state != GST_STATE_PLAYING) {
1505     g_print ("PLAY pipeline\n");
1506     gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
1507
1508     ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
1509     switch (ret) {
1510       case GST_STATE_CHANGE_FAILURE:
1511         goto failed;
1512       case GST_STATE_CHANGE_NO_PREROLL:
1513         is_live = TRUE;
1514         break;
1515       default:
1516         break;
1517     }
1518     state = GST_STATE_PLAYING;
1519     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Playing");
1520   }
1521
1522   return;
1523
1524 failed:
1525   {
1526     g_print ("PLAY failed\n");
1527     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Play failed");
1528   }
1529 }
1530
1531 static void
1532 pause_cb (GtkButton * button, gpointer data)
1533 {
1534   g_static_mutex_lock (&state_mutex);
1535   if (state != GST_STATE_PAUSED) {
1536     GstStateChangeReturn ret;
1537
1538     gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
1539     g_print ("PAUSE pipeline\n");
1540     ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
1541     switch (ret) {
1542       case GST_STATE_CHANGE_FAILURE:
1543         goto failed;
1544       case GST_STATE_CHANGE_NO_PREROLL:
1545         is_live = TRUE;
1546         break;
1547       default:
1548         break;
1549     }
1550
1551     state = GST_STATE_PAUSED;
1552     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Paused");
1553   }
1554   g_static_mutex_unlock (&state_mutex);
1555
1556   return;
1557
1558 failed:
1559   {
1560     g_static_mutex_unlock (&state_mutex);
1561     g_print ("PAUSE failed\n");
1562     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Pause failed");
1563   }
1564 }
1565
1566 static void
1567 stop_cb (GtkButton * button, gpointer data)
1568 {
1569   if (state != STOP_STATE) {
1570     GstStateChangeReturn ret;
1571
1572     g_print ("READY pipeline\n");
1573     gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
1574
1575     g_static_mutex_lock (&state_mutex);
1576     ret = gst_element_set_state (pipeline, STOP_STATE);
1577     if (ret == GST_STATE_CHANGE_FAILURE)
1578       goto failed;
1579
1580     state = STOP_STATE;
1581     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Stopped");
1582
1583     is_live = FALSE;
1584     buffering = FALSE;
1585     set_update_scale (FALSE);
1586     set_scale (0.0);
1587     set_update_fill (FALSE);
1588
1589     if (pipeline_type == 16)
1590       clear_streams (pipeline);
1591     g_static_mutex_unlock (&state_mutex);
1592
1593 #if 0
1594     /* if one uses parse_launch, play, stop and play again it fails as all the
1595      * pads after the demuxer can't be reconnected
1596      */
1597     if (!strcmp (pipelines[pipeline_type].name, "parse-launch")) {
1598       gst_element_set_state (pipeline, GST_STATE_NULL);
1599       gst_object_unref (pipeline);
1600
1601       g_list_free (seekable_elements);
1602       seekable_elements = NULL;
1603       g_list_free (seekable_pads);
1604       seekable_pads = NULL;
1605       g_list_free (rate_pads);
1606       rate_pads = NULL;
1607
1608       pipeline = pipelines[pipeline_type].func (pipeline_spec);
1609       g_assert (pipeline);
1610       gst_element_set_state (pipeline, STOP_STATE);
1611       connect_bus_signals (pipeline);
1612     }
1613 #endif
1614   }
1615   return;
1616
1617 failed:
1618   {
1619     g_static_mutex_unlock (&state_mutex);
1620     g_print ("STOP failed\n");
1621     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Stop failed");
1622   }
1623 }
1624
1625 static void
1626 accurate_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1627 {
1628   accurate_seek = gtk_toggle_button_get_active (button);
1629 }
1630
1631 static void
1632 key_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1633 {
1634   keyframe_seek = gtk_toggle_button_get_active (button);
1635 }
1636
1637 static void
1638 loop_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1639 {
1640   loop_seek = gtk_toggle_button_get_active (button);
1641   if (state == GST_STATE_PLAYING) {
1642     do_seek (hscale);
1643   }
1644 }
1645
1646 static void
1647 flush_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1648 {
1649   flush_seek = gtk_toggle_button_get_active (button);
1650 }
1651
1652 static void
1653 scrub_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1654 {
1655   scrub = gtk_toggle_button_get_active (button);
1656 }
1657
1658 static void
1659 play_scrub_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1660 {
1661   play_scrub = gtk_toggle_button_get_active (button);
1662 }
1663
1664 static void
1665 skip_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1666 {
1667   skip_seek = gtk_toggle_button_get_active (button);
1668   if (state == GST_STATE_PLAYING) {
1669     do_seek (hscale);
1670   }
1671 }
1672
1673 static void
1674 rate_spinbutton_changed_cb (GtkSpinButton * button, GstPipeline * pipeline)
1675 {
1676   gboolean res = FALSE;
1677   GstEvent *s_event;
1678   GstSeekFlags flags;
1679
1680   rate = gtk_spin_button_get_value (button);
1681
1682   GST_DEBUG ("rate changed to %lf", rate);
1683
1684   flags = 0;
1685   if (flush_seek)
1686     flags |= GST_SEEK_FLAG_FLUSH;
1687   if (loop_seek)
1688     flags |= GST_SEEK_FLAG_SEGMENT;
1689   if (accurate_seek)
1690     flags |= GST_SEEK_FLAG_ACCURATE;
1691   if (keyframe_seek)
1692     flags |= GST_SEEK_FLAG_KEY_UNIT;
1693   if (skip_seek)
1694     flags |= GST_SEEK_FLAG_SKIP;
1695
1696   if (rate >= 0.0) {
1697     s_event = gst_event_new_seek (rate,
1698         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, position,
1699         GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE);
1700   } else {
1701     s_event = gst_event_new_seek (rate,
1702         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
1703         GST_SEEK_TYPE_SET, position);
1704   }
1705
1706   res = send_event (s_event);
1707
1708   if (res) {
1709     if (flush_seek) {
1710       gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL, SEEK_TIMEOUT);
1711     }
1712   } else
1713     g_print ("seek failed\n");
1714 }
1715
1716 static void
1717 update_flag (GstPipeline * pipeline, gint num, gboolean state)
1718 {
1719   gint flags;
1720
1721   g_object_get (pipeline, "flags", &flags, NULL);
1722   if (state)
1723     flags |= (1 << num);
1724   else
1725     flags &= ~(1 << num);
1726   g_object_set (pipeline, "flags", flags, NULL);
1727 }
1728
1729 static void
1730 vis_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1731 {
1732   gboolean state;
1733
1734   state = gtk_toggle_button_get_active (button);
1735   update_flag (pipeline, 3, state);
1736   gtk_widget_set_sensitive (vis_combo, state);
1737 }
1738
1739 static void
1740 audio_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1741 {
1742   gboolean state;
1743
1744   state = gtk_toggle_button_get_active (button);
1745   update_flag (pipeline, 1, state);
1746   gtk_widget_set_sensitive (audio_combo, state);
1747 }
1748
1749 static void
1750 video_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1751 {
1752   gboolean state;
1753
1754   state = gtk_toggle_button_get_active (button);
1755   update_flag (pipeline, 0, state);
1756   gtk_widget_set_sensitive (video_combo, state);
1757 }
1758
1759 static void
1760 text_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1761 {
1762   gboolean state;
1763
1764   state = gtk_toggle_button_get_active (button);
1765   update_flag (pipeline, 2, state);
1766   gtk_widget_set_sensitive (text_combo, state);
1767 }
1768
1769 static void
1770 mute_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1771 {
1772   gboolean mute;
1773
1774   mute = gtk_toggle_button_get_active (button);
1775   g_object_set (pipeline, "mute", mute, NULL);
1776 }
1777
1778 static void
1779 download_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1780 {
1781   gboolean state;
1782
1783   state = gtk_toggle_button_get_active (button);
1784   update_flag (pipeline, 7, state);
1785 }
1786
1787 static void
1788 buffer_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1789 {
1790   gboolean state;
1791
1792   state = gtk_toggle_button_get_active (button);
1793   update_flag (pipeline, 8, state);
1794 }
1795
1796 static void
1797 clear_streams (GstElement * pipeline)
1798 {
1799   gint i;
1800
1801   /* remove previous info */
1802   for (i = 0; i < n_video; i++)
1803     gtk_combo_box_text_remove (GTK_COMBO_BOX_TEXT (video_combo), 0);
1804   for (i = 0; i < n_audio; i++)
1805     gtk_combo_box_text_remove (GTK_COMBO_BOX_TEXT (audio_combo), 0);
1806   for (i = 0; i < n_text; i++)
1807     gtk_combo_box_text_remove (GTK_COMBO_BOX_TEXT (text_combo), 0);
1808
1809   n_audio = n_video = n_text = 0;
1810   gtk_widget_set_sensitive (video_combo, FALSE);
1811   gtk_widget_set_sensitive (audio_combo, FALSE);
1812   gtk_widget_set_sensitive (text_combo, FALSE);
1813
1814   need_streams = TRUE;
1815 }
1816
1817 static void
1818 update_streams (GstPipeline * pipeline)
1819 {
1820   gint i;
1821
1822   if (pipeline_type == 16 && need_streams) {
1823     GstTagList *tags;
1824     gchar *name, *str;
1825     gint active_idx;
1826     gboolean state;
1827
1828     /* remove previous info */
1829     clear_streams (GST_ELEMENT_CAST (pipeline));
1830
1831     /* here we get and update the different streams detected by playbin2 */
1832     g_object_get (pipeline, "n-video", &n_video, NULL);
1833     g_object_get (pipeline, "n-audio", &n_audio, NULL);
1834     g_object_get (pipeline, "n-text", &n_text, NULL);
1835
1836     g_print ("video %d, audio %d, text %d\n", n_video, n_audio, n_text);
1837
1838     active_idx = 0;
1839     for (i = 0; i < n_video; i++) {
1840       g_signal_emit_by_name (pipeline, "get-video-tags", i, &tags);
1841       if (tags) {
1842         str = gst_structure_to_string ((GstStructure *) tags);
1843         g_print ("video %d: %s\n", i, str);
1844         g_free (str);
1845       }
1846       /* find good name for the label */
1847       name = g_strdup_printf ("video %d", i + 1);
1848       gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (video_combo), name);
1849       g_free (name);
1850     }
1851     state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (video_checkbox));
1852     gtk_widget_set_sensitive (video_combo, state && n_video > 0);
1853     gtk_combo_box_set_active (GTK_COMBO_BOX (video_combo), active_idx);
1854
1855     active_idx = 0;
1856     for (i = 0; i < n_audio; i++) {
1857       g_signal_emit_by_name (pipeline, "get-audio-tags", i, &tags);
1858       if (tags) {
1859         str = gst_structure_to_string ((GstStructure *) tags);
1860         g_print ("audio %d: %s\n", i, str);
1861         g_free (str);
1862       }
1863       /* find good name for the label */
1864       name = g_strdup_printf ("audio %d", i + 1);
1865       gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (audio_combo), name);
1866       g_free (name);
1867     }
1868     state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (audio_checkbox));
1869     gtk_widget_set_sensitive (audio_combo, state && n_audio > 0);
1870     gtk_combo_box_set_active (GTK_COMBO_BOX (audio_combo), active_idx);
1871
1872     active_idx = 0;
1873     for (i = 0; i < n_text; i++) {
1874       g_signal_emit_by_name (pipeline, "get-text-tags", i, &tags);
1875
1876       name = NULL;
1877       if (tags) {
1878         const GValue *value;
1879
1880         str = gst_structure_to_string ((GstStructure *) tags);
1881         g_print ("text %d: %s\n", i, str);
1882         g_free (str);
1883
1884         /* get the language code if we can */
1885         value = gst_tag_list_get_value_index (tags, GST_TAG_LANGUAGE_CODE, 0);
1886         if (value && G_VALUE_HOLDS_STRING (value)) {
1887           name = g_strdup_printf ("text %s", g_value_get_string (value));
1888         }
1889       }
1890       /* find good name for the label if we didn't use a tag */
1891       if (name == NULL)
1892         name = g_strdup_printf ("text %d", i + 1);
1893
1894       gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (text_combo), name);
1895       g_free (name);
1896     }
1897     state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (text_checkbox));
1898     gtk_widget_set_sensitive (text_combo, state && n_text > 0);
1899     gtk_combo_box_set_active (GTK_COMBO_BOX (text_combo), active_idx);
1900
1901     need_streams = FALSE;
1902   }
1903 }
1904
1905 static void
1906 video_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1907 {
1908   gint active;
1909
1910   active = gtk_combo_box_get_active (combo);
1911
1912   g_print ("setting current video track %d\n", active);
1913   g_object_set (pipeline, "current-video", active, NULL);
1914 }
1915
1916 static void
1917 audio_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1918 {
1919   gint active;
1920
1921   active = gtk_combo_box_get_active (combo);
1922
1923   g_print ("setting current audio track %d\n", active);
1924   g_object_set (pipeline, "current-audio", active, NULL);
1925 }
1926
1927 static void
1928 text_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1929 {
1930   gint active;
1931
1932   active = gtk_combo_box_get_active (combo);
1933
1934   g_print ("setting current text track %d\n", active);
1935   g_object_set (pipeline, "current-text", active, NULL);
1936 }
1937
1938 static gboolean
1939 filter_features (GstPluginFeature * feature, gpointer data)
1940 {
1941   GstElementFactory *f;
1942
1943   if (!GST_IS_ELEMENT_FACTORY (feature))
1944     return FALSE;
1945   f = GST_ELEMENT_FACTORY (feature);
1946   if (!g_strrstr (gst_element_factory_get_klass (f), "Visualization"))
1947     return FALSE;
1948
1949   return TRUE;
1950 }
1951
1952 static void
1953 init_visualization_features (void)
1954 {
1955   GList *list, *walk;
1956
1957   vis_entries = g_array_new (FALSE, FALSE, sizeof (VisEntry));
1958
1959   list = gst_registry_feature_filter (gst_registry_get_default (),
1960       filter_features, FALSE, NULL);
1961
1962   for (walk = list; walk; walk = g_list_next (walk)) {
1963     VisEntry entry;
1964     const gchar *name;
1965
1966     entry.factory = GST_ELEMENT_FACTORY (walk->data);
1967     name = gst_element_factory_get_longname (entry.factory);
1968
1969     g_array_append_val (vis_entries, entry);
1970     gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (vis_combo), name);
1971   }
1972   gtk_combo_box_set_active (GTK_COMBO_BOX (vis_combo), 0);
1973 }
1974
1975 static void
1976 vis_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1977 {
1978   guint index;
1979   VisEntry *entry;
1980   GstElement *element;
1981
1982   /* get the selected index and get the factory for this index */
1983   index = gtk_combo_box_get_active (GTK_COMBO_BOX (vis_combo));
1984   if (vis_entries->len > 0) {
1985     entry = &g_array_index (vis_entries, VisEntry, index);
1986
1987     /* create an instance of the element from the factory */
1988     element = gst_element_factory_create (entry->factory, NULL);
1989     if (!element)
1990       return;
1991
1992     /* set vis plugin for playbin2 */
1993     g_object_set (pipeline, "vis-plugin", element, NULL);
1994   }
1995 }
1996
1997 static void
1998 volume_spinbutton_changed_cb (GtkSpinButton * button, GstPipeline * pipeline)
1999 {
2000   gdouble volume;
2001
2002   volume = gtk_spin_button_get_value (button);
2003
2004   g_object_set (pipeline, "volume", volume, NULL);
2005 }
2006
2007 static void
2008 volume_notify_cb (GstElement * pipeline, GParamSpec * arg, gpointer user_dat)
2009 {
2010   gdouble cur_volume, new_volume;
2011
2012   g_object_get (pipeline, "volume", &new_volume, NULL);
2013   cur_volume = gtk_spin_button_get_value (GTK_SPIN_BUTTON (volume_spinbutton));
2014   if (fabs (cur_volume - new_volume) > 0.001) {
2015     g_signal_handlers_block_by_func (volume_spinbutton,
2016         volume_spinbutton_changed_cb, pipeline);
2017     gtk_spin_button_set_value (GTK_SPIN_BUTTON (volume_spinbutton), new_volume);
2018     g_signal_handlers_unblock_by_func (volume_spinbutton,
2019         volume_spinbutton_changed_cb, pipeline);
2020   }
2021 }
2022
2023 static void
2024 shot_cb (GtkButton * button, gpointer data)
2025 {
2026   GstBuffer *buffer;
2027   GstCaps *caps;
2028
2029   /* convert to our desired format (RGB24) */
2030   caps = gst_caps_new_simple ("video/x-raw-rgb",
2031       "bpp", G_TYPE_INT, 24, "depth", G_TYPE_INT, 24,
2032       /* Note: we don't ask for a specific width/height here, so that
2033        * videoscale can adjust dimensions from a non-1/1 pixel aspect
2034        * ratio to a 1/1 pixel-aspect-ratio */
2035       "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
2036       "endianness", G_TYPE_INT, G_BIG_ENDIAN,
2037       "red_mask", G_TYPE_INT, 0xff0000,
2038       "green_mask", G_TYPE_INT, 0x00ff00,
2039       "blue_mask", G_TYPE_INT, 0x0000ff, NULL);
2040
2041   /* convert the latest frame to the requested format */
2042   g_signal_emit_by_name (pipeline, "convert-frame", caps, &buffer);
2043   gst_caps_unref (caps);
2044
2045   if (buffer) {
2046     GstCaps *caps;
2047     GstStructure *s;
2048     gboolean res;
2049     gint width, height;
2050     GdkPixbuf *pixbuf;
2051     GError *error = NULL;
2052
2053     /* get the snapshot buffer format now. We set the caps on the appsink so
2054      * that it can only be an rgb buffer. The only thing we have not specified
2055      * on the caps is the height, which is dependant on the pixel-aspect-ratio
2056      * of the source material */
2057     caps = GST_BUFFER_CAPS (buffer);
2058     if (!caps) {
2059       g_warning ("could not get snapshot format\n");
2060       goto done;
2061     }
2062     s = gst_caps_get_structure (caps, 0);
2063
2064     /* we need to get the final caps on the buffer to get the size */
2065     res = gst_structure_get_int (s, "width", &width);
2066     res |= gst_structure_get_int (s, "height", &height);
2067     if (!res) {
2068       g_warning ("could not get snapshot dimension\n");
2069       goto done;
2070     }
2071
2072     /* create pixmap from buffer and save, gstreamer video buffers have a stride
2073      * that is rounded up to the nearest multiple of 4 */
2074     pixbuf = gdk_pixbuf_new_from_data (GST_BUFFER_DATA (buffer),
2075         GDK_COLORSPACE_RGB, FALSE, 8, width, height,
2076         GST_ROUND_UP_4 (width * 3), NULL, NULL);
2077
2078     /* save the pixbuf */
2079     gdk_pixbuf_save (pixbuf, "snapshot.png", "png", &error, NULL);
2080
2081   done:
2082     gst_buffer_unref (buffer);
2083   }
2084 }
2085
2086 /* called when the Step button is pressed */
2087 static void
2088 step_cb (GtkButton * button, gpointer data)
2089 {
2090   GstEvent *event;
2091   GstFormat format;
2092   guint64 amount;
2093   gdouble rate;
2094   gboolean flush, res;
2095   gint active;
2096
2097   active = gtk_combo_box_get_active (GTK_COMBO_BOX (format_combo));
2098   amount =
2099       gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
2100       (step_amount_spinbutton));
2101   rate = gtk_spin_button_get_value (GTK_SPIN_BUTTON (step_rate_spinbutton));
2102   flush = TRUE;
2103
2104   switch (active) {
2105     case 0:
2106       format = GST_FORMAT_BUFFERS;
2107       break;
2108     case 1:
2109       format = GST_FORMAT_TIME;
2110       amount *= GST_MSECOND;
2111       break;
2112     default:
2113       format = GST_FORMAT_UNDEFINED;
2114       break;
2115   }
2116
2117   event = gst_event_new_step (format, amount, rate, flush, FALSE);
2118
2119   res = send_event (event);
2120
2121   if (!res) {
2122     g_print ("Sending step event failed\n");
2123   }
2124 }
2125
2126 static void
2127 message_received (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
2128 {
2129   const GstStructure *s;
2130
2131   s = gst_message_get_structure (message);
2132   g_print ("message from \"%s\" (%s): ",
2133       GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
2134       gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
2135   if (s) {
2136     gchar *sstr;
2137
2138     sstr = gst_structure_to_string (s);
2139     g_print ("%s\n", sstr);
2140     g_free (sstr);
2141   } else {
2142     g_print ("no message details\n");
2143   }
2144 }
2145
2146 static gboolean shuttling = FALSE;
2147 static gdouble shuttle_rate = 0.0;
2148 static gdouble play_rate = 1.0;
2149
2150 static void
2151 do_shuttle (GstElement * element)
2152 {
2153   guint64 duration;
2154
2155   if (shuttling)
2156     duration = 40 * GST_MSECOND;
2157   else
2158     duration = -1;
2159
2160   gst_element_send_event (element,
2161       gst_event_new_step (GST_FORMAT_TIME, duration, shuttle_rate, FALSE,
2162           FALSE));
2163 }
2164
2165 static void
2166 msg_sync_step_done (GstBus * bus, GstMessage * message, GstElement * element)
2167 {
2168   GstFormat format;
2169   guint64 amount;
2170   gdouble rate;
2171   gboolean flush;
2172   gboolean intermediate;
2173   guint64 duration;
2174   gboolean eos;
2175
2176   gst_message_parse_step_done (message, &format, &amount, &rate, &flush,
2177       &intermediate, &duration, &eos);
2178
2179   if (eos) {
2180     g_print ("stepped till EOS\n");
2181     return;
2182   }
2183
2184   if (g_static_mutex_trylock (&state_mutex)) {
2185     if (shuttling)
2186       do_shuttle (element);
2187     g_static_mutex_unlock (&state_mutex);
2188   } else {
2189     /* ignore step messages that come while we are doing a state change */
2190     g_print ("state change is busy\n");
2191   }
2192 }
2193
2194 static void
2195 shuttle_toggled (GtkToggleButton * button, GstElement * element)
2196 {
2197   gboolean active;
2198
2199   active = gtk_toggle_button_get_active (button);
2200
2201   if (active != shuttling) {
2202     shuttling = active;
2203     g_print ("shuttling %s\n", shuttling ? "active" : "inactive");
2204     if (active) {
2205       shuttle_rate = 0.0;
2206       play_rate = 1.0;
2207       pause_cb (NULL, NULL);
2208       gst_element_get_state (element, NULL, NULL, -1);
2209     }
2210   }
2211 }
2212
2213 static void
2214 shuttle_rate_switch (GstElement * element)
2215 {
2216   GstSeekFlags flags;
2217   GstEvent *s_event;
2218   gboolean res;
2219
2220   if (state == GST_STATE_PLAYING) {
2221     /* pause when we need to */
2222     pause_cb (NULL, NULL);
2223     gst_element_get_state (element, NULL, NULL, -1);
2224   }
2225
2226   if (play_rate == 1.0)
2227     play_rate = -1.0;
2228   else
2229     play_rate = 1.0;
2230
2231   g_print ("rate changed to %lf %" GST_TIME_FORMAT "\n", play_rate,
2232       GST_TIME_ARGS (position));
2233
2234   flags = GST_SEEK_FLAG_FLUSH;
2235   flags |= GST_SEEK_FLAG_ACCURATE;
2236
2237   if (play_rate >= 0.0) {
2238     s_event = gst_event_new_seek (play_rate,
2239         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, position,
2240         GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE);
2241   } else {
2242     s_event = gst_event_new_seek (play_rate,
2243         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
2244         GST_SEEK_TYPE_SET, position);
2245   }
2246   res = send_event (s_event);
2247   if (res) {
2248     gst_element_get_state (element, NULL, NULL, SEEK_TIMEOUT);
2249   } else {
2250     g_print ("seek failed\n");
2251   }
2252 }
2253
2254 static void
2255 shuttle_value_changed (GtkRange * range, GstElement * element)
2256 {
2257   gdouble rate;
2258
2259   rate = gtk_adjustment_get_value (shuttle_adjustment);
2260
2261   if (rate == 0.0) {
2262     g_print ("rate 0.0, pause\n");
2263     pause_cb (NULL, NULL);
2264     gst_element_get_state (element, NULL, NULL, -1);
2265   } else {
2266     g_print ("rate changed %0.3g\n", rate);
2267
2268     if ((rate < 0.0 && play_rate > 0.0) || (rate > 0.0 && play_rate < 0.0)) {
2269       shuttle_rate_switch (element);
2270     }
2271
2272     shuttle_rate = ABS (rate);
2273     if (state != GST_STATE_PLAYING) {
2274       do_shuttle (element);
2275       play_cb (NULL, NULL);
2276     }
2277   }
2278 }
2279
2280 static void
2281 msg_async_done (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
2282 {
2283   GST_DEBUG ("async done");
2284   /* when we get ASYNC_DONE we can query position, duration and other
2285    * properties */
2286   update_scale (pipeline);
2287
2288   /* update the available streams */
2289   update_streams (pipeline);
2290 }
2291
2292 static void
2293 msg_state_changed (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
2294 {
2295   const GstStructure *s;
2296
2297   s = gst_message_get_structure (message);
2298
2299   /* We only care about state changed on the pipeline */
2300   if (s && GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
2301     GstState old, new, pending;
2302
2303     gst_message_parse_state_changed (message, &old, &new, &pending);
2304
2305     /* When state of the pipeline changes to paused or playing we start updating scale */
2306     if (new == GST_STATE_PLAYING) {
2307       set_update_scale (TRUE);
2308     } else {
2309       set_update_scale (FALSE);
2310     }
2311   }
2312 }
2313
2314 static void
2315 msg_segment_done (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
2316 {
2317   GstEvent *s_event;
2318   GstSeekFlags flags;
2319   gboolean res;
2320   GstFormat format;
2321
2322   GST_DEBUG ("position is %" GST_TIME_FORMAT, GST_TIME_ARGS (position));
2323   gst_message_parse_segment_done (message, &format, &position);
2324   GST_DEBUG ("end of segment at %" GST_TIME_FORMAT, GST_TIME_ARGS (position));
2325
2326   flags = 0;
2327   /* in the segment-done callback we never flush as this would not make sense
2328    * for seamless playback. */
2329   if (loop_seek)
2330     flags |= GST_SEEK_FLAG_SEGMENT;
2331   if (skip_seek)
2332     flags |= GST_SEEK_FLAG_SKIP;
2333
2334   s_event = gst_event_new_seek (rate,
2335       GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
2336       GST_SEEK_TYPE_SET, duration);
2337
2338   GST_DEBUG ("restart loop with rate %lf to 0 / %" GST_TIME_FORMAT,
2339       rate, GST_TIME_ARGS (duration));
2340
2341   res = send_event (s_event);
2342   if (!res)
2343     g_print ("segment seek failed\n");
2344 }
2345
2346 /* in stream buffering mode we PAUSE the pipeline until we receive a 100%
2347  * message */
2348 static void
2349 do_stream_buffering (gint percent)
2350 {
2351   gchar *bufstr;
2352
2353   gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
2354   bufstr = g_strdup_printf ("Buffering...%d", percent);
2355   gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, bufstr);
2356   g_free (bufstr);
2357
2358   if (percent == 100) {
2359     /* a 100% message means buffering is done */
2360     buffering = FALSE;
2361     /* if the desired state is playing, go back */
2362     if (state == GST_STATE_PLAYING) {
2363       /* no state management needed for live pipelines */
2364       if (!is_live) {
2365         fprintf (stderr, "Done buffering, setting pipeline to PLAYING ...\n");
2366         gst_element_set_state (pipeline, GST_STATE_PLAYING);
2367       }
2368       gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
2369       gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Playing");
2370     }
2371   } else {
2372     /* buffering busy */
2373     if (buffering == FALSE && state == GST_STATE_PLAYING) {
2374       /* we were not buffering but PLAYING, PAUSE  the pipeline. */
2375       if (!is_live) {
2376         fprintf (stderr, "Buffering, setting pipeline to PAUSED ...\n");
2377         gst_element_set_state (pipeline, GST_STATE_PAUSED);
2378       }
2379     }
2380     buffering = TRUE;
2381   }
2382 }
2383
2384 static void
2385 do_download_buffering (gint percent)
2386 {
2387   if (!buffering && percent < 100) {
2388     gchar *bufstr;
2389
2390     buffering = TRUE;
2391
2392     bufstr = g_strdup_printf ("Downloading...");
2393     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, bufstr);
2394     g_free (bufstr);
2395
2396     /* once we get a buffering message, we'll do the fill update */
2397     set_update_fill (TRUE);
2398
2399     if (state == GST_STATE_PLAYING && !is_live) {
2400       fprintf (stderr, "Downloading, setting pipeline to PAUSED ...\n");
2401       gst_element_set_state (pipeline, GST_STATE_PAUSED);
2402       /* user has to manually start the playback */
2403       state = GST_STATE_PAUSED;
2404     }
2405   }
2406 }
2407
2408 static void
2409 msg_buffering (GstBus * bus, GstMessage * message, GstPipeline * data)
2410 {
2411   gint percent;
2412
2413   gst_message_parse_buffering (message, &percent);
2414
2415   /* get more stats */
2416   gst_message_parse_buffering_stats (message, &mode, NULL, NULL,
2417       &buffering_left);
2418
2419   switch (mode) {
2420     case GST_BUFFERING_DOWNLOAD:
2421       do_download_buffering (percent);
2422       break;
2423     case GST_BUFFERING_LIVE:
2424       is_live = TRUE;
2425     case GST_BUFFERING_TIMESHIFT:
2426     case GST_BUFFERING_STREAM:
2427       do_stream_buffering (percent);
2428       break;
2429   }
2430 }
2431
2432 static void
2433 msg_clock_lost (GstBus * bus, GstMessage * message, GstPipeline * data)
2434 {
2435   g_print ("clock lost! PAUSE and PLAY to select a new clock\n");
2436   if (state == GST_STATE_PLAYING) {
2437     gst_element_set_state (pipeline, GST_STATE_PAUSED);
2438     gst_element_set_state (pipeline, GST_STATE_PLAYING);
2439   }
2440 }
2441
2442 #if defined (HAVE_X) || defined (G_OS_WIN32)
2443
2444 static gulong embed_xid = 0;
2445
2446 /* We set the xid here in response to the prepare-xwindow-id message via a
2447  * bus sync handler because we don't know the actual videosink used from the
2448  * start (as we don't know the pipeline, or bin elements such as autovideosink
2449  * or gconfvideosink may be used which create the actual videosink only once
2450  * the pipeline is started) */
2451 static GstBusSyncReply
2452 bus_sync_handler (GstBus * bus, GstMessage * message, GstPipeline * data)
2453 {
2454   if ((GST_MESSAGE_TYPE (message) == GST_MESSAGE_ELEMENT) &&
2455       gst_structure_has_name (message->structure, "prepare-xwindow-id")) {
2456     GstElement *element = GST_ELEMENT (GST_MESSAGE_SRC (message));
2457
2458     g_print ("got prepare-xwindow-id, setting XID %lu\n", embed_xid);
2459
2460     if (g_object_class_find_property (G_OBJECT_GET_CLASS (element),
2461             "force-aspect-ratio")) {
2462       g_object_set (element, "force-aspect-ratio", TRUE, NULL);
2463     }
2464
2465     /* Should have been initialised from main thread before (can't use
2466      * GDK_WINDOW_XID here with Gtk+ >= 2.18, because the sync handler will
2467      * be called from a streaming thread and GDK_WINDOW_XID maps to more than
2468      * a simple structure lookup with Gtk+ >= 2.18, where 'more' is stuff that
2469      * shouldn't be done from a non-GUI thread without explicit locking).  */
2470     g_assert (embed_xid != 0);
2471
2472     gst_x_overlay_set_window_handle (GST_X_OVERLAY (element), embed_xid);
2473   }
2474   return GST_BUS_PASS;
2475 }
2476 #endif
2477
2478 static gboolean
2479 handle_expose_cb (GtkWidget * widget, GdkEventExpose * event, gpointer data)
2480 {
2481   if (state < GST_STATE_PAUSED) {
2482     GtkAllocation allocation;
2483     GdkWindow *window = gtk_widget_get_window (widget);
2484     cairo_t *cr;
2485
2486     gtk_widget_get_allocation (widget, &allocation);
2487     cr = gdk_cairo_create (window);
2488     cairo_set_source_rgb (cr, 0, 0, 0);
2489     cairo_rectangle (cr, 0, 0, allocation.width, allocation.height);
2490     cairo_fill (cr);
2491     cairo_destroy (cr);
2492   }
2493   return FALSE;
2494 }
2495
2496 static void
2497 realize_cb (GtkWidget * widget, gpointer data)
2498 {
2499 #if GTK_CHECK_VERSION(2,18,0)
2500   {
2501     GdkWindow *window = gtk_widget_get_window (widget);
2502
2503     /* This is here just for pedagogical purposes, GDK_WINDOW_XID will call it
2504      * as well */
2505     if (!gdk_window_ensure_native (window))
2506       g_error ("Couldn't create native window needed for GstXOverlay!");
2507   }
2508 #endif
2509
2510 #if defined (HAVE_X) || defined (G_OS_WIN32)
2511   {
2512     GdkWindow *window = gtk_widget_get_window (video_window);
2513
2514 #ifdef G_OS_WIN32
2515     embed_xid = GDK_WINDOW_HWND (window);
2516 #else
2517     embed_xid = GDK_WINDOW_XID (window);
2518 #endif
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 int
2650 main (int argc, char **argv)
2651 {
2652   GtkWidget *window, *hbox, *vbox, *panel, *expander, *pb2vbox, *boxes,
2653       *flagtable, *boxes2, *step;
2654   GtkWidget *play_button, *pause_button, *stop_button, *shot_button;
2655   GtkWidget *accurate_checkbox, *key_checkbox, *loop_checkbox, *flush_checkbox;
2656   GtkWidget *scrub_checkbox, *play_scrub_checkbox;
2657   GtkWidget *rate_label, *volume_label;
2658   GOptionEntry options[] = {
2659     {"stats", 's', 0, G_OPTION_ARG_NONE, &stats,
2660         "Show pad stats", NULL},
2661     {"elem", 'e', 0, G_OPTION_ARG_NONE, &elem_seek,
2662         "Seek on elements instead of pads", NULL},
2663     {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
2664         "Verbose properties", NULL},
2665     {NULL}
2666   };
2667   GOptionContext *ctx;
2668   GError *err = NULL;
2669
2670   if (!g_thread_supported ())
2671     g_thread_init (NULL);
2672
2673   ctx = g_option_context_new ("- test seeking in gsteamer");
2674   g_option_context_add_main_entries (ctx, options, NULL);
2675   g_option_context_add_group (ctx, gst_init_get_option_group ());
2676   g_option_context_add_group (ctx, gtk_get_option_group (TRUE));
2677
2678   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
2679     g_print ("Error initializing: %s\n", err->message);
2680     exit (1);
2681   }
2682
2683   GST_DEBUG_CATEGORY_INIT (seek_debug, "seek", 0, "seek example");
2684
2685   if (argc != 3) {
2686     print_usage (argc, argv);
2687     exit (-1);
2688   }
2689
2690   pipeline_type = atoi (argv[1]);
2691
2692   if (pipeline_type < 0 || pipeline_type >= NUM_TYPES) {
2693     print_usage (argc, argv);
2694     exit (-1);
2695   }
2696
2697   pipeline_spec = argv[2];
2698
2699   if (g_path_is_absolute (pipeline_spec) &&
2700       (g_strrstr (pipeline_spec, "*") != NULL ||
2701           g_strrstr (pipeline_spec, "?") != NULL)) {
2702     paths = handle_wildcards (pipeline_spec);
2703   } else {
2704     paths = g_list_prepend (paths, g_strdup (pipeline_spec));
2705   }
2706
2707   if (!paths) {
2708     g_print ("opening %s failed\n", pipeline_spec);
2709     exit (-1);
2710   }
2711
2712   l = paths;
2713
2714   pipeline = pipelines[pipeline_type].func ((gchar *) l->data);
2715   g_assert (pipeline);
2716
2717   /* initialize gui elements ... */
2718   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
2719   video_window = gtk_drawing_area_new ();
2720   g_signal_connect (video_window, "expose-event",
2721       G_CALLBACK (handle_expose_cb), NULL);
2722   g_signal_connect (video_window, "realize", G_CALLBACK (realize_cb), NULL);
2723   gtk_widget_set_double_buffered (video_window, FALSE);
2724
2725   statusbar = gtk_statusbar_new ();
2726   status_id = gtk_statusbar_get_context_id (GTK_STATUSBAR (statusbar), "seek");
2727   gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Stopped");
2728   hbox = gtk_hbox_new (FALSE, 0);
2729   vbox = gtk_vbox_new (FALSE, 0);
2730   flagtable = gtk_table_new (4, 2, FALSE);
2731   gtk_container_set_border_width (GTK_CONTAINER (vbox), 3);
2732
2733   /* media controls */
2734   play_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_PLAY);
2735   pause_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_PAUSE);
2736   stop_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_STOP);
2737
2738   /* seek flags */
2739   accurate_checkbox = gtk_check_button_new_with_label ("Accurate Seek");
2740   key_checkbox = gtk_check_button_new_with_label ("Key-unit Seek");
2741   loop_checkbox = gtk_check_button_new_with_label ("Loop");
2742   flush_checkbox = gtk_check_button_new_with_label ("Flush");
2743   scrub_checkbox = gtk_check_button_new_with_label ("Scrub");
2744   play_scrub_checkbox = gtk_check_button_new_with_label ("Play Scrub");
2745   skip_checkbox = gtk_check_button_new_with_label ("Play Skip");
2746   rate_spinbutton = gtk_spin_button_new_with_range (-100, 100, 0.1);
2747   gtk_spin_button_set_digits (GTK_SPIN_BUTTON (rate_spinbutton), 3);
2748   rate_label = gtk_label_new ("Rate");
2749
2750   gtk_widget_set_tooltip_text (accurate_checkbox,
2751       "accurate position is requested, this might be considerably slower for some formats");
2752   gtk_widget_set_tooltip_text (key_checkbox,
2753       "seek to the nearest keyframe. This might be faster but less accurate");
2754   gtk_widget_set_tooltip_text (loop_checkbox, "loop playback");
2755   gtk_widget_set_tooltip_text (flush_checkbox, "flush pipeline after seeking");
2756   gtk_widget_set_tooltip_text (rate_spinbutton, "define the playback rate, "
2757       "negative value trigger reverse playback");
2758   gtk_widget_set_tooltip_text (scrub_checkbox, "show images while seeking");
2759   gtk_widget_set_tooltip_text (play_scrub_checkbox, "play video while seeking");
2760   gtk_widget_set_tooltip_text (skip_checkbox,
2761       "Skip frames while playing at high frame rates");
2762
2763   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (flush_checkbox), TRUE);
2764   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (scrub_checkbox), TRUE);
2765
2766   gtk_spin_button_set_value (GTK_SPIN_BUTTON (rate_spinbutton), rate);
2767
2768   /* step expander */
2769   {
2770     GtkWidget *hbox;
2771
2772     step = gtk_expander_new ("step options");
2773     hbox = gtk_hbox_new (FALSE, 0);
2774
2775     format_combo = gtk_combo_box_text_new ();
2776     gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (format_combo),
2777         "frames");
2778     gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (format_combo),
2779         "time (ms)");
2780     gtk_combo_box_set_active (GTK_COMBO_BOX (format_combo), 0);
2781     gtk_box_pack_start (GTK_BOX (hbox), format_combo, FALSE, FALSE, 2);
2782
2783     step_amount_spinbutton = gtk_spin_button_new_with_range (1, 1000, 1);
2784     gtk_spin_button_set_digits (GTK_SPIN_BUTTON (step_amount_spinbutton), 0);
2785     gtk_spin_button_set_value (GTK_SPIN_BUTTON (step_amount_spinbutton), 1.0);
2786     gtk_box_pack_start (GTK_BOX (hbox), step_amount_spinbutton, FALSE, FALSE,
2787         2);
2788
2789     step_rate_spinbutton = gtk_spin_button_new_with_range (0.0, 100, 0.1);
2790     gtk_spin_button_set_digits (GTK_SPIN_BUTTON (step_rate_spinbutton), 3);
2791     gtk_spin_button_set_value (GTK_SPIN_BUTTON (step_rate_spinbutton), 1.0);
2792     gtk_box_pack_start (GTK_BOX (hbox), step_rate_spinbutton, FALSE, FALSE, 2);
2793
2794     step_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_FORWARD);
2795     gtk_button_set_label (GTK_BUTTON (step_button), "Step");
2796     gtk_box_pack_start (GTK_BOX (hbox), step_button, FALSE, FALSE, 2);
2797
2798     g_signal_connect (G_OBJECT (step_button), "clicked", G_CALLBACK (step_cb),
2799         pipeline);
2800
2801     /* shuttle scale */
2802     shuttle_checkbox = gtk_check_button_new_with_label ("Shuttle");
2803     gtk_box_pack_start (GTK_BOX (hbox), shuttle_checkbox, FALSE, FALSE, 2);
2804     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (shuttle_checkbox), FALSE);
2805     g_signal_connect (shuttle_checkbox, "toggled", G_CALLBACK (shuttle_toggled),
2806         pipeline);
2807
2808     shuttle_adjustment =
2809         GTK_ADJUSTMENT (gtk_adjustment_new (0.0, -3.00, 4.0, 0.1, 1.0, 1.0));
2810     shuttle_hscale = gtk_hscale_new (shuttle_adjustment);
2811     gtk_scale_set_digits (GTK_SCALE (shuttle_hscale), 2);
2812     gtk_scale_set_value_pos (GTK_SCALE (shuttle_hscale), GTK_POS_TOP);
2813     g_signal_connect (shuttle_hscale, "value_changed",
2814         G_CALLBACK (shuttle_value_changed), pipeline);
2815     g_signal_connect (shuttle_hscale, "format_value",
2816         G_CALLBACK (shuttle_format_value), pipeline);
2817
2818     gtk_box_pack_start (GTK_BOX (hbox), shuttle_hscale, TRUE, TRUE, 2);
2819
2820     gtk_container_add (GTK_CONTAINER (step), hbox);
2821   }
2822
2823   /* seek bar */
2824   adjustment =
2825       GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.00, N_GRAD, 0.1, 1.0, 1.0));
2826   hscale = gtk_hscale_new (adjustment);
2827   gtk_scale_set_digits (GTK_SCALE (hscale), 2);
2828   gtk_scale_set_value_pos (GTK_SCALE (hscale), GTK_POS_RIGHT);
2829   gtk_range_set_show_fill_level (GTK_RANGE (hscale), TRUE);
2830   gtk_range_set_fill_level (GTK_RANGE (hscale), N_GRAD);
2831
2832   g_signal_connect (hscale, "button_press_event", G_CALLBACK (start_seek),
2833       pipeline);
2834   g_signal_connect (hscale, "button_release_event", G_CALLBACK (stop_seek),
2835       pipeline);
2836   g_signal_connect (hscale, "format_value", G_CALLBACK (format_value),
2837       pipeline);
2838
2839   if (pipeline_type == 16) {
2840     /* the playbin2 panel controls for the video/audio/subtitle tracks */
2841     panel = gtk_hbox_new (FALSE, 0);
2842     video_combo = gtk_combo_box_text_new ();
2843     audio_combo = gtk_combo_box_text_new ();
2844     text_combo = gtk_combo_box_text_new ();
2845     gtk_widget_set_sensitive (video_combo, FALSE);
2846     gtk_widget_set_sensitive (audio_combo, FALSE);
2847     gtk_widget_set_sensitive (text_combo, FALSE);
2848     gtk_box_pack_start (GTK_BOX (panel), video_combo, TRUE, TRUE, 2);
2849     gtk_box_pack_start (GTK_BOX (panel), audio_combo, TRUE, TRUE, 2);
2850     gtk_box_pack_start (GTK_BOX (panel), text_combo, TRUE, TRUE, 2);
2851     g_signal_connect (G_OBJECT (video_combo), "changed",
2852         G_CALLBACK (video_combo_cb), pipeline);
2853     g_signal_connect (G_OBJECT (audio_combo), "changed",
2854         G_CALLBACK (audio_combo_cb), pipeline);
2855     g_signal_connect (G_OBJECT (text_combo), "changed",
2856         G_CALLBACK (text_combo_cb), pipeline);
2857     /* playbin2 panel for flag checkboxes and volume/mute */
2858     boxes = gtk_hbox_new (FALSE, 0);
2859     vis_checkbox = gtk_check_button_new_with_label ("Vis");
2860     video_checkbox = gtk_check_button_new_with_label ("Video");
2861     audio_checkbox = gtk_check_button_new_with_label ("Audio");
2862     text_checkbox = gtk_check_button_new_with_label ("Text");
2863     mute_checkbox = gtk_check_button_new_with_label ("Mute");
2864     download_checkbox = gtk_check_button_new_with_label ("Download");
2865     buffer_checkbox = gtk_check_button_new_with_label ("Buffer");
2866     volume_label = gtk_label_new ("Volume");
2867     volume_spinbutton = gtk_spin_button_new_with_range (0, 10.0, 0.1);
2868     gtk_spin_button_set_value (GTK_SPIN_BUTTON (volume_spinbutton), 1.0);
2869     gtk_box_pack_start (GTK_BOX (boxes), video_checkbox, TRUE, TRUE, 2);
2870     gtk_box_pack_start (GTK_BOX (boxes), audio_checkbox, TRUE, TRUE, 2);
2871     gtk_box_pack_start (GTK_BOX (boxes), text_checkbox, TRUE, TRUE, 2);
2872     gtk_box_pack_start (GTK_BOX (boxes), vis_checkbox, TRUE, TRUE, 2);
2873     gtk_box_pack_start (GTK_BOX (boxes), mute_checkbox, TRUE, TRUE, 2);
2874     gtk_box_pack_start (GTK_BOX (boxes), download_checkbox, TRUE, TRUE, 2);
2875     gtk_box_pack_start (GTK_BOX (boxes), buffer_checkbox, TRUE, TRUE, 2);
2876     gtk_box_pack_start (GTK_BOX (boxes), volume_label, TRUE, TRUE, 2);
2877     gtk_box_pack_start (GTK_BOX (boxes), volume_spinbutton, TRUE, TRUE, 2);
2878     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (vis_checkbox), FALSE);
2879     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (audio_checkbox), TRUE);
2880     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (video_checkbox), TRUE);
2881     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (text_checkbox), TRUE);
2882     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (mute_checkbox), FALSE);
2883     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (download_checkbox), FALSE);
2884     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (buffer_checkbox), FALSE);
2885     g_signal_connect (G_OBJECT (vis_checkbox), "toggled",
2886         G_CALLBACK (vis_toggle_cb), pipeline);
2887     g_signal_connect (G_OBJECT (audio_checkbox), "toggled",
2888         G_CALLBACK (audio_toggle_cb), pipeline);
2889     g_signal_connect (G_OBJECT (video_checkbox), "toggled",
2890         G_CALLBACK (video_toggle_cb), pipeline);
2891     g_signal_connect (G_OBJECT (text_checkbox), "toggled",
2892         G_CALLBACK (text_toggle_cb), pipeline);
2893     g_signal_connect (G_OBJECT (mute_checkbox), "toggled",
2894         G_CALLBACK (mute_toggle_cb), pipeline);
2895     g_signal_connect (G_OBJECT (download_checkbox), "toggled",
2896         G_CALLBACK (download_toggle_cb), pipeline);
2897     g_signal_connect (G_OBJECT (buffer_checkbox), "toggled",
2898         G_CALLBACK (buffer_toggle_cb), pipeline);
2899     g_signal_connect (G_OBJECT (volume_spinbutton), "value_changed",
2900         G_CALLBACK (volume_spinbutton_changed_cb), pipeline);
2901     /* playbin2 panel for snapshot */
2902     boxes2 = gtk_hbox_new (FALSE, 0);
2903     shot_button = gtk_button_new_from_stock (GTK_STOCK_SAVE);
2904     gtk_widget_set_tooltip_text (shot_button,
2905         "save a screenshot .png in the current directory");
2906     g_signal_connect (G_OBJECT (shot_button), "clicked", G_CALLBACK (shot_cb),
2907         pipeline);
2908     vis_combo = gtk_combo_box_text_new ();
2909     g_signal_connect (G_OBJECT (vis_combo), "changed",
2910         G_CALLBACK (vis_combo_cb), pipeline);
2911     gtk_widget_set_sensitive (vis_combo, FALSE);
2912     gtk_box_pack_start (GTK_BOX (boxes2), shot_button, TRUE, TRUE, 2);
2913     gtk_box_pack_start (GTK_BOX (boxes2), vis_combo, TRUE, TRUE, 2);
2914
2915     /* fill the vis combo box and the array of factories */
2916     init_visualization_features ();
2917   } else {
2918     panel = boxes = boxes2 = NULL;
2919   }
2920
2921   /* do the packing stuff ... */
2922   gtk_window_set_default_size (GTK_WINDOW (window), 250, 96);
2923   /* FIXME: can we avoid this for audio only? */
2924   gtk_widget_set_size_request (GTK_WIDGET (video_window), -1,
2925       DEFAULT_VIDEO_HEIGHT);
2926   gtk_container_add (GTK_CONTAINER (window), vbox);
2927   gtk_box_pack_start (GTK_BOX (vbox), video_window, TRUE, TRUE, 2);
2928   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 2);
2929   gtk_box_pack_start (GTK_BOX (hbox), play_button, FALSE, FALSE, 2);
2930   gtk_box_pack_start (GTK_BOX (hbox), pause_button, FALSE, FALSE, 2);
2931   gtk_box_pack_start (GTK_BOX (hbox), stop_button, FALSE, FALSE, 2);
2932   gtk_box_pack_start (GTK_BOX (hbox), flagtable, FALSE, FALSE, 2);
2933   gtk_table_attach_defaults (GTK_TABLE (flagtable), accurate_checkbox, 0, 1, 0,
2934       1);
2935   gtk_table_attach_defaults (GTK_TABLE (flagtable), flush_checkbox, 1, 2, 0, 1);
2936   gtk_table_attach_defaults (GTK_TABLE (flagtable), loop_checkbox, 2, 3, 0, 1);
2937   gtk_table_attach_defaults (GTK_TABLE (flagtable), key_checkbox, 0, 1, 1, 2);
2938   gtk_table_attach_defaults (GTK_TABLE (flagtable), scrub_checkbox, 1, 2, 1, 2);
2939   gtk_table_attach_defaults (GTK_TABLE (flagtable), play_scrub_checkbox, 2, 3,
2940       1, 2);
2941   gtk_table_attach_defaults (GTK_TABLE (flagtable), skip_checkbox, 3, 4, 0, 1);
2942   gtk_table_attach_defaults (GTK_TABLE (flagtable), rate_label, 4, 5, 0, 1);
2943   gtk_table_attach_defaults (GTK_TABLE (flagtable), rate_spinbutton, 4, 5, 1,
2944       2);
2945   if (panel && boxes && boxes2) {
2946     expander = gtk_expander_new ("playbin2 options");
2947     pb2vbox = gtk_vbox_new (FALSE, 0);
2948     gtk_box_pack_start (GTK_BOX (pb2vbox), panel, FALSE, FALSE, 2);
2949     gtk_box_pack_start (GTK_BOX (pb2vbox), boxes, FALSE, FALSE, 2);
2950     gtk_box_pack_start (GTK_BOX (pb2vbox), boxes2, FALSE, FALSE, 2);
2951     gtk_container_add (GTK_CONTAINER (expander), pb2vbox);
2952     gtk_box_pack_start (GTK_BOX (vbox), expander, FALSE, FALSE, 2);
2953   }
2954   gtk_box_pack_start (GTK_BOX (vbox), step, FALSE, FALSE, 2);
2955   gtk_box_pack_start (GTK_BOX (vbox), hscale, FALSE, FALSE, 2);
2956   gtk_box_pack_start (GTK_BOX (vbox), statusbar, FALSE, FALSE, 2);
2957
2958   /* connect things ... */
2959   g_signal_connect (G_OBJECT (play_button), "clicked", G_CALLBACK (play_cb),
2960       pipeline);
2961   g_signal_connect (G_OBJECT (pause_button), "clicked", G_CALLBACK (pause_cb),
2962       pipeline);
2963   g_signal_connect (G_OBJECT (stop_button), "clicked", G_CALLBACK (stop_cb),
2964       pipeline);
2965   g_signal_connect (G_OBJECT (accurate_checkbox), "toggled",
2966       G_CALLBACK (accurate_toggle_cb), pipeline);
2967   g_signal_connect (G_OBJECT (key_checkbox), "toggled",
2968       G_CALLBACK (key_toggle_cb), pipeline);
2969   g_signal_connect (G_OBJECT (loop_checkbox), "toggled",
2970       G_CALLBACK (loop_toggle_cb), pipeline);
2971   g_signal_connect (G_OBJECT (flush_checkbox), "toggled",
2972       G_CALLBACK (flush_toggle_cb), pipeline);
2973   g_signal_connect (G_OBJECT (scrub_checkbox), "toggled",
2974       G_CALLBACK (scrub_toggle_cb), pipeline);
2975   g_signal_connect (G_OBJECT (play_scrub_checkbox), "toggled",
2976       G_CALLBACK (play_scrub_toggle_cb), pipeline);
2977   g_signal_connect (G_OBJECT (skip_checkbox), "toggled",
2978       G_CALLBACK (skip_toggle_cb), pipeline);
2979   g_signal_connect (G_OBJECT (rate_spinbutton), "value_changed",
2980       G_CALLBACK (rate_spinbutton_changed_cb), pipeline);
2981
2982   g_signal_connect (G_OBJECT (window), "delete-event", delete_event_cb, NULL);
2983
2984   /* show the gui. */
2985   gtk_widget_show_all (window);
2986
2987   /* realize window now so that the video window gets created and we can
2988    * obtain its XID before the pipeline is started up and the videosink
2989    * asks for the XID of the window to render onto */
2990   gtk_widget_realize (window);
2991
2992 #ifdef HAVE_X
2993   /* we should have the XID now */
2994   g_assert (embed_xid != 0);
2995 #endif
2996
2997   if (verbose) {
2998     g_signal_connect (pipeline, "deep_notify",
2999         G_CALLBACK (gst_object_default_deep_notify), NULL);
3000   }
3001
3002   connect_bus_signals (pipeline);
3003   gtk_main ();
3004
3005   g_print ("NULL pipeline\n");
3006   gst_element_set_state (pipeline, GST_STATE_NULL);
3007
3008   g_print ("free pipeline\n");
3009   gst_object_unref (pipeline);
3010
3011   g_list_foreach (paths, (GFunc) g_free, NULL);
3012   g_list_free (paths);
3013
3014   return 0;
3015 }