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