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