tests/examples/seek/seek.c: Initialise error to NULL as we should.
[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 #include <stdlib.h>
24 #include <glib.h>
25 #include <gtk/gtk.h>
26 #include <gst/gst.h>
27 #include <string.h>
28
29 GST_DEBUG_CATEGORY_STATIC (seek_debug);
30 #define GST_CAT_DEFAULT (seek_debug)
31
32 /* configuration */
33
34 //#define SOURCE "filesrc"
35 #define SOURCE "gnomevfssrc"
36
37 #define ASINK "alsasink"
38 //#define ASINK "osssink"
39
40 #define VSINK "xvimagesink"
41 //#define VSINK "sdlvideosink"
42 //#define VSINK "ximagesink"
43 //#define VSINK "aasink"
44 //#define VSINK "cacasink"
45
46 #define FILL_INTERVAL 100
47 //#define UPDATE_INTERVAL 500
48 //#define UPDATE_INTERVAL 100
49 #define UPDATE_INTERVAL 10
50
51 /* number of milliseconds to play for after a seek */
52 #define SCRUB_TIME 100
53
54 /* timeout for gst_element_get_state() after a seek */
55 #define SEEK_TIMEOUT 40 * GST_MSECOND
56
57
58 static GList *seekable_pads = NULL;
59 static GList *rate_pads = NULL;
60 static GList *seekable_elements = NULL;
61
62 static gboolean accurate_seek = FALSE;
63 static gboolean keyframe_seek = FALSE;
64 static gboolean loop_seek = FALSE;
65 static gboolean flush_seek = TRUE;
66 static gboolean scrub = TRUE;
67 static gboolean play_scrub = FALSE;
68 static gdouble rate = 1.0;
69
70 static GstElement *pipeline;
71 static gint pipeline_type;
72 static const gchar *pipeline_spec;
73 static gint64 position = -1;
74 static gint64 duration = -1;
75 static GtkAdjustment *adjustment;
76 static GtkWidget *hscale, *statusbar;
77 static guint status_id = 0;
78 static gboolean stats = FALSE;
79 static gboolean elem_seek = FALSE;
80 static gboolean verbose = FALSE;
81
82 static gboolean is_live = FALSE;
83 static gboolean buffering = FALSE;
84 static GstBufferingMode mode;
85 static gint64 buffering_left;
86 static GstState state = GST_STATE_NULL;
87 static guint update_id = 0;
88 static guint seek_timeout_id = 0;
89 static gulong changed_id;
90 static guint fill_id = 0;
91
92 static gint n_video = 0, n_audio = 0, n_text = 0;
93 static gboolean need_streams = TRUE;
94 static GtkWidget *video_combo, *audio_combo, *text_combo, *vis_combo;
95 static GtkWidget *vis_checkbox, *video_checkbox, *audio_checkbox;
96 static GtkWidget *text_checkbox, *mute_checkbox, *volume_spinbutton;
97
98 /* we keep an array of the visualisation entries so that we can easily switch
99  * with the combo box index. */
100 typedef struct
101 {
102   GstElementFactory *factory;
103 } VisEntry;
104
105 static GArray *vis_entries;
106
107 static void clear_streams (GstElement * pipeline);
108
109 /* pipeline construction */
110
111 typedef struct
112 {
113   const gchar *padname;
114   GstPad *target;
115   GstElement *bin;
116 }
117 dyn_link;
118
119 static GstElement *
120 gst_element_factory_make_or_warn (gchar * type, gchar * name)
121 {
122   GstElement *element = gst_element_factory_make (type, name);
123
124   if (!element) {
125     g_warning ("Failed to create element %s of type %s", name, type);
126   }
127
128   return element;
129 }
130
131 static void
132 dynamic_link (GstPadTemplate * templ, GstPad * newpad, gpointer data)
133 {
134   gchar *padname;
135   dyn_link *connect = (dyn_link *) data;
136
137   padname = gst_pad_get_name (newpad);
138
139   if (connect->padname == NULL || !strcmp (padname, connect->padname)) {
140     if (connect->bin)
141       gst_bin_add (GST_BIN (pipeline), connect->bin);
142     gst_pad_link (newpad, connect->target);
143
144     //seekable_pads = g_list_prepend (seekable_pads, newpad);
145     rate_pads = g_list_prepend (rate_pads, newpad);
146   }
147   g_free (padname);
148 }
149
150 static void
151 setup_dynamic_link (GstElement * element, const gchar * padname,
152     GstPad * target, GstElement * bin)
153 {
154   dyn_link *connect;
155
156   connect = g_new0 (dyn_link, 1);
157   connect->padname = g_strdup (padname);
158   connect->target = target;
159   connect->bin = bin;
160
161   g_signal_connect (G_OBJECT (element), "pad-added", G_CALLBACK (dynamic_link),
162       connect);
163 }
164
165 static GstElement *
166 make_mod_pipeline (const gchar * location)
167 {
168   GstElement *pipeline;
169   GstElement *src, *decoder, *audiosink;
170   GstPad *seekable;
171
172   pipeline = gst_pipeline_new ("app");
173
174   src = gst_element_factory_make_or_warn (SOURCE, "src");
175   decoder = gst_element_factory_make_or_warn ("modplug", "decoder");
176   audiosink = gst_element_factory_make_or_warn (ASINK, "sink");
177   //g_object_set (G_OBJECT (audiosink), "sync", FALSE, NULL);
178
179   g_object_set (G_OBJECT (src), "location", location, NULL);
180
181   gst_bin_add (GST_BIN (pipeline), src);
182   gst_bin_add (GST_BIN (pipeline), decoder);
183   gst_bin_add (GST_BIN (pipeline), audiosink);
184
185   gst_element_link (src, decoder);
186   gst_element_link (decoder, audiosink);
187
188   seekable = gst_element_get_static_pad (decoder, "src");
189   seekable_pads = g_list_prepend (seekable_pads, seekable);
190   rate_pads = g_list_prepend (rate_pads, seekable);
191   rate_pads =
192       g_list_prepend (rate_pads, gst_element_get_static_pad (decoder, "sink"));
193
194   return pipeline;
195 }
196
197 static GstElement *
198 make_dv_pipeline (const gchar * location)
199 {
200   GstElement *pipeline;
201   GstElement *src, *demux, *decoder, *audiosink, *videosink;
202   GstElement *a_queue, *v_queue;
203   GstPad *seekable;
204
205   pipeline = gst_pipeline_new ("app");
206
207   src = gst_element_factory_make_or_warn (SOURCE, "src");
208   demux = gst_element_factory_make_or_warn ("dvdemux", "demuxer");
209   v_queue = gst_element_factory_make_or_warn ("queue", "v_queue");
210   decoder = gst_element_factory_make_or_warn ("ffdec_dvvideo", "decoder");
211   videosink = gst_element_factory_make_or_warn (VSINK, "v_sink");
212   a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
213   audiosink = gst_element_factory_make_or_warn ("alsasink", "a_sink");
214
215   g_object_set (G_OBJECT (src), "location", location, NULL);
216
217   gst_bin_add (GST_BIN (pipeline), src);
218   gst_bin_add (GST_BIN (pipeline), demux);
219   gst_bin_add (GST_BIN (pipeline), a_queue);
220   gst_bin_add (GST_BIN (pipeline), audiosink);
221   gst_bin_add (GST_BIN (pipeline), v_queue);
222   gst_bin_add (GST_BIN (pipeline), decoder);
223   gst_bin_add (GST_BIN (pipeline), videosink);
224
225   gst_element_link (src, demux);
226   gst_element_link (a_queue, audiosink);
227   gst_element_link (v_queue, decoder);
228   gst_element_link (decoder, videosink);
229
230   setup_dynamic_link (demux, "video", gst_element_get_static_pad (v_queue,
231           "sink"), NULL);
232   setup_dynamic_link (demux, "audio", gst_element_get_static_pad (a_queue,
233           "sink"), NULL);
234
235   seekable = gst_element_get_static_pad (decoder, "src");
236   seekable_pads = g_list_prepend (seekable_pads, seekable);
237   rate_pads = g_list_prepend (rate_pads, seekable);
238
239   return pipeline;
240 }
241
242 static GstElement *
243 make_wav_pipeline (const gchar * location)
244 {
245   GstElement *pipeline;
246   GstElement *src, *decoder, *audiosink;
247
248   pipeline = gst_pipeline_new ("app");
249
250   src = gst_element_factory_make_or_warn (SOURCE, "src");
251   decoder = gst_element_factory_make_or_warn ("wavparse", "decoder");
252   audiosink = gst_element_factory_make_or_warn (ASINK, "sink");
253
254   g_object_set (G_OBJECT (src), "location", location, NULL);
255
256   gst_bin_add (GST_BIN (pipeline), src);
257   gst_bin_add (GST_BIN (pipeline), decoder);
258   gst_bin_add (GST_BIN (pipeline), audiosink);
259
260   gst_element_link (src, decoder);
261
262   setup_dynamic_link (decoder, "src", gst_element_get_static_pad (audiosink,
263           "sink"), NULL);
264
265   seekable_elements = g_list_prepend (seekable_elements, audiosink);
266
267   /* force element seeking on this pipeline */
268   elem_seek = TRUE;
269
270   return pipeline;
271 }
272
273 static GstElement *
274 make_flac_pipeline (const gchar * location)
275 {
276   GstElement *pipeline;
277   GstElement *src, *decoder, *audiosink;
278   GstPad *seekable;
279
280   pipeline = gst_pipeline_new ("app");
281
282   src = gst_element_factory_make_or_warn (SOURCE, "src");
283   decoder = gst_element_factory_make_or_warn ("flacdec", "decoder");
284   audiosink = gst_element_factory_make_or_warn (ASINK, "sink");
285   g_object_set (G_OBJECT (audiosink), "sync", FALSE, NULL);
286
287   g_object_set (G_OBJECT (src), "location", location, NULL);
288
289   gst_bin_add (GST_BIN (pipeline), src);
290   gst_bin_add (GST_BIN (pipeline), decoder);
291   gst_bin_add (GST_BIN (pipeline), audiosink);
292
293   gst_element_link (src, decoder);
294   gst_element_link (decoder, audiosink);
295
296   seekable = gst_element_get_static_pad (decoder, "src");
297   seekable_pads = g_list_prepend (seekable_pads, seekable);
298   rate_pads = g_list_prepend (rate_pads, seekable);
299   rate_pads =
300       g_list_prepend (rate_pads, gst_element_get_static_pad (decoder, "sink"));
301
302   return pipeline;
303 }
304
305 static GstElement *
306 make_sid_pipeline (const gchar * location)
307 {
308   GstElement *pipeline;
309   GstElement *src, *decoder, *audiosink;
310   GstPad *seekable;
311
312   pipeline = gst_pipeline_new ("app");
313
314   src = gst_element_factory_make_or_warn (SOURCE, "src");
315   decoder = gst_element_factory_make_or_warn ("siddec", "decoder");
316   audiosink = gst_element_factory_make_or_warn (ASINK, "sink");
317   //g_object_set (G_OBJECT (audiosink), "sync", FALSE, NULL);
318
319   g_object_set (G_OBJECT (src), "location", location, NULL);
320
321   gst_bin_add (GST_BIN (pipeline), src);
322   gst_bin_add (GST_BIN (pipeline), decoder);
323   gst_bin_add (GST_BIN (pipeline), audiosink);
324
325   gst_element_link (src, decoder);
326   gst_element_link (decoder, audiosink);
327
328   seekable = gst_element_get_static_pad (decoder, "src");
329   seekable_pads = g_list_prepend (seekable_pads, seekable);
330   rate_pads = g_list_prepend (rate_pads, seekable);
331   rate_pads =
332       g_list_prepend (rate_pads, gst_element_get_static_pad (decoder, "sink"));
333
334   return pipeline;
335 }
336
337 static GstElement *
338 make_parse_pipeline (const gchar * location)
339 {
340   GstElement *pipeline;
341   GstElement *src, *parser, *fakesink;
342   GstPad *seekable;
343
344   pipeline = gst_pipeline_new ("app");
345
346   src = gst_element_factory_make_or_warn (SOURCE, "src");
347   parser = gst_element_factory_make_or_warn ("mpegparse", "parse");
348   fakesink = gst_element_factory_make_or_warn ("fakesink", "sink");
349   g_object_set (G_OBJECT (fakesink), "silent", TRUE, NULL);
350   g_object_set (G_OBJECT (fakesink), "sync", TRUE, NULL);
351
352   g_object_set (G_OBJECT (src), "location", location, NULL);
353
354   gst_bin_add (GST_BIN (pipeline), src);
355   gst_bin_add (GST_BIN (pipeline), parser);
356   gst_bin_add (GST_BIN (pipeline), fakesink);
357
358   gst_element_link (src, parser);
359   gst_element_link (parser, fakesink);
360
361   seekable = gst_element_get_static_pad (parser, "src");
362   seekable_pads = g_list_prepend (seekable_pads, seekable);
363   rate_pads = g_list_prepend (rate_pads, seekable);
364   rate_pads =
365       g_list_prepend (rate_pads, gst_element_get_static_pad (parser, "sink"));
366
367   return pipeline;
368 }
369
370 static GstElement *
371 make_vorbis_pipeline (const gchar * location)
372 {
373   GstElement *pipeline, *audio_bin;
374   GstElement *src, *demux, *decoder, *convert, *audiosink;
375   GstPad *pad, *seekable;
376
377   pipeline = gst_pipeline_new ("app");
378
379   src = gst_element_factory_make_or_warn (SOURCE, "src");
380   demux = gst_element_factory_make_or_warn ("oggdemux", "demux");
381   decoder = gst_element_factory_make_or_warn ("vorbisdec", "decoder");
382   convert = gst_element_factory_make_or_warn ("audioconvert", "convert");
383   audiosink = gst_element_factory_make_or_warn (ASINK, "sink");
384   g_object_set (G_OBJECT (audiosink), "sync", TRUE, NULL);
385
386   g_object_set (G_OBJECT (src), "location", location, NULL);
387
388   audio_bin = gst_bin_new ("a_decoder_bin");
389
390   gst_bin_add (GST_BIN (pipeline), src);
391   gst_bin_add (GST_BIN (pipeline), demux);
392   gst_bin_add (GST_BIN (audio_bin), decoder);
393   gst_bin_add (GST_BIN (audio_bin), convert);
394   gst_bin_add (GST_BIN (audio_bin), audiosink);
395   gst_bin_add (GST_BIN (pipeline), audio_bin);
396
397   gst_element_link (src, demux);
398   gst_element_link (decoder, convert);
399   gst_element_link (convert, audiosink);
400
401   pad = gst_element_get_static_pad (decoder, "sink");
402   gst_element_add_pad (audio_bin, gst_ghost_pad_new ("sink", pad));
403   gst_object_unref (pad);
404
405   setup_dynamic_link (demux, NULL, gst_element_get_static_pad (audio_bin,
406           "sink"), NULL);
407
408   seekable = gst_element_get_static_pad (decoder, "src");
409   seekable_pads = g_list_prepend (seekable_pads, seekable);
410   rate_pads = g_list_prepend (rate_pads, seekable);
411   rate_pads =
412       g_list_prepend (rate_pads, gst_element_get_static_pad (decoder, "sink"));
413
414   return pipeline;
415 }
416
417 static GstElement *
418 make_theora_pipeline (const gchar * location)
419 {
420   GstElement *pipeline, *video_bin;
421   GstElement *src, *demux, *decoder, *convert, *videosink;
422   GstPad *pad, *seekable;
423
424   pipeline = gst_pipeline_new ("app");
425
426   src = gst_element_factory_make_or_warn (SOURCE, "src");
427   demux = gst_element_factory_make_or_warn ("oggdemux", "demux");
428   decoder = gst_element_factory_make_or_warn ("theoradec", "decoder");
429   convert = gst_element_factory_make_or_warn ("ffmpegcolorspace", "convert");
430   videosink = gst_element_factory_make_or_warn (VSINK, "sink");
431
432   g_object_set (G_OBJECT (src), "location", location, NULL);
433
434   video_bin = gst_bin_new ("v_decoder_bin");
435
436   gst_bin_add (GST_BIN (pipeline), src);
437   gst_bin_add (GST_BIN (pipeline), demux);
438   gst_bin_add (GST_BIN (video_bin), decoder);
439   gst_bin_add (GST_BIN (video_bin), convert);
440   gst_bin_add (GST_BIN (video_bin), videosink);
441   gst_bin_add (GST_BIN (pipeline), video_bin);
442
443   gst_element_link (src, demux);
444   gst_element_link (decoder, convert);
445   gst_element_link (convert, videosink);
446
447   pad = gst_element_get_static_pad (decoder, "sink");
448   gst_element_add_pad (video_bin, gst_ghost_pad_new ("sink", pad));
449   gst_object_unref (pad);
450
451   setup_dynamic_link (demux, NULL, gst_element_get_static_pad (video_bin,
452           "sink"), NULL);
453
454   seekable = gst_element_get_static_pad (decoder, "src");
455   seekable_pads = g_list_prepend (seekable_pads, seekable);
456   rate_pads = g_list_prepend (rate_pads, seekable);
457   rate_pads =
458       g_list_prepend (rate_pads, gst_element_get_static_pad (decoder, "sink"));
459
460   return pipeline;
461 }
462
463 static GstElement *
464 make_vorbis_theora_pipeline (const gchar * location)
465 {
466   GstElement *pipeline, *audio_bin, *video_bin;
467   GstElement *src, *demux, *a_decoder, *a_convert, *v_decoder, *v_convert;
468   GstElement *audiosink, *videosink;
469   GstElement *a_queue, *v_queue, *v_scale;
470   GstPad *seekable;
471   GstPad *pad;
472
473   pipeline = gst_pipeline_new ("app");
474
475   src = gst_element_factory_make_or_warn (SOURCE, "src");
476   g_object_set (G_OBJECT (src), "location", location, NULL);
477
478   demux = gst_element_factory_make_or_warn ("oggdemux", "demux");
479
480   gst_bin_add (GST_BIN (pipeline), src);
481   gst_bin_add (GST_BIN (pipeline), demux);
482   gst_element_link (src, demux);
483
484   audio_bin = gst_bin_new ("a_decoder_bin");
485   a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
486   a_decoder = gst_element_factory_make_or_warn ("vorbisdec", "a_dec");
487   a_convert = gst_element_factory_make_or_warn ("audioconvert", "a_convert");
488   audiosink = gst_element_factory_make_or_warn (ASINK, "a_sink");
489
490   gst_bin_add (GST_BIN (pipeline), audio_bin);
491
492   gst_bin_add (GST_BIN (audio_bin), a_queue);
493   gst_bin_add (GST_BIN (audio_bin), a_decoder);
494   gst_bin_add (GST_BIN (audio_bin), a_convert);
495   gst_bin_add (GST_BIN (audio_bin), audiosink);
496
497   gst_element_link (a_queue, a_decoder);
498   gst_element_link (a_decoder, a_convert);
499   gst_element_link (a_convert, audiosink);
500
501   pad = gst_element_get_static_pad (a_queue, "sink");
502   gst_element_add_pad (audio_bin, gst_ghost_pad_new ("sink", pad));
503   gst_object_unref (pad);
504
505   setup_dynamic_link (demux, NULL, gst_element_get_static_pad (audio_bin,
506           "sink"), NULL);
507
508   video_bin = gst_bin_new ("v_decoder_bin");
509   v_queue = gst_element_factory_make_or_warn ("queue", "v_queue");
510   v_decoder = gst_element_factory_make_or_warn ("theoradec", "v_dec");
511   v_convert =
512       gst_element_factory_make_or_warn ("ffmpegcolorspace", "v_convert");
513   v_scale = gst_element_factory_make_or_warn ("videoscale", "v_scale");
514   videosink = gst_element_factory_make_or_warn (VSINK, "v_sink");
515
516   gst_bin_add (GST_BIN (pipeline), video_bin);
517
518   gst_bin_add (GST_BIN (video_bin), v_queue);
519   gst_bin_add (GST_BIN (video_bin), v_decoder);
520   gst_bin_add (GST_BIN (video_bin), v_convert);
521   gst_bin_add (GST_BIN (video_bin), v_scale);
522   gst_bin_add (GST_BIN (video_bin), videosink);
523
524   gst_element_link_many (v_queue, v_decoder, v_convert, v_scale, videosink,
525       NULL);
526
527   pad = gst_element_get_static_pad (v_queue, "sink");
528   gst_element_add_pad (video_bin, gst_ghost_pad_new ("sink", pad));
529   gst_object_unref (pad);
530
531   setup_dynamic_link (demux, NULL, gst_element_get_static_pad (video_bin,
532           "sink"), NULL);
533
534   seekable = gst_element_get_static_pad (a_decoder, "src");
535   seekable_pads = g_list_prepend (seekable_pads, seekable);
536   rate_pads = g_list_prepend (rate_pads, seekable);
537   rate_pads =
538       g_list_prepend (rate_pads, gst_element_get_static_pad (a_decoder,
539           "sink"));
540
541   return pipeline;
542 }
543
544 static GstElement *
545 make_avi_msmpeg4v3_mp3_pipeline (const gchar * location)
546 {
547   GstElement *pipeline, *audio_bin, *video_bin;
548   GstElement *src, *demux, *a_decoder, *a_convert, *v_decoder, *v_convert;
549   GstElement *audiosink, *videosink;
550   GstElement *a_queue, *v_queue;
551   GstPad *seekable, *pad;
552
553   pipeline = gst_pipeline_new ("app");
554
555   src = gst_element_factory_make_or_warn (SOURCE, "src");
556   g_object_set (G_OBJECT (src), "location", location, NULL);
557
558   demux = gst_element_factory_make_or_warn ("avidemux", "demux");
559
560   gst_bin_add (GST_BIN (pipeline), src);
561   gst_bin_add (GST_BIN (pipeline), demux);
562   gst_element_link (src, demux);
563
564   audio_bin = gst_bin_new ("a_decoder_bin");
565   a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
566   a_decoder = gst_element_factory_make_or_warn ("mad", "a_dec");
567   a_convert = gst_element_factory_make_or_warn ("audioconvert", "a_convert");
568   audiosink = gst_element_factory_make_or_warn (ASINK, "a_sink");
569
570   gst_bin_add (GST_BIN (audio_bin), a_queue);
571   gst_bin_add (GST_BIN (audio_bin), a_decoder);
572   gst_bin_add (GST_BIN (audio_bin), a_convert);
573   gst_bin_add (GST_BIN (audio_bin), audiosink);
574
575   gst_element_link (a_queue, a_decoder);
576   gst_element_link (a_decoder, a_convert);
577   gst_element_link (a_convert, audiosink);
578
579   gst_bin_add (GST_BIN (pipeline), audio_bin);
580
581   pad = gst_element_get_static_pad (a_queue, "sink");
582   gst_element_add_pad (audio_bin, gst_ghost_pad_new ("sink", pad));
583   gst_object_unref (pad);
584
585   setup_dynamic_link (demux, NULL, gst_element_get_static_pad (audio_bin,
586           "sink"), NULL);
587
588   video_bin = gst_bin_new ("v_decoder_bin");
589   v_queue = gst_element_factory_make_or_warn ("queue", "v_queue");
590   v_decoder = gst_element_factory_make_or_warn ("ffdec_msmpeg4", "v_dec");
591   v_convert =
592       gst_element_factory_make_or_warn ("ffmpegcolorspace", "v_convert");
593   videosink = gst_element_factory_make_or_warn (VSINK, "v_sink");
594
595   gst_bin_add (GST_BIN (video_bin), v_queue);
596   gst_bin_add (GST_BIN (video_bin), v_decoder);
597   gst_bin_add (GST_BIN (video_bin), v_convert);
598   gst_bin_add (GST_BIN (video_bin), videosink);
599
600   gst_element_link_many (v_queue, v_decoder, v_convert, videosink, NULL);
601
602   gst_bin_add (GST_BIN (pipeline), video_bin);
603
604   pad = gst_element_get_static_pad (v_queue, "sink");
605   gst_element_add_pad (video_bin, gst_ghost_pad_new ("sink", pad));
606   gst_object_unref (pad);
607
608   setup_dynamic_link (demux, NULL, gst_element_get_static_pad (video_bin,
609           "sink"), NULL);
610
611   seekable = gst_element_get_static_pad (a_decoder, "src");
612   seekable_pads = g_list_prepend (seekable_pads, seekable);
613   rate_pads = g_list_prepend (rate_pads, seekable);
614   rate_pads =
615       g_list_prepend (rate_pads, gst_element_get_static_pad (a_decoder,
616           "sink"));
617
618   return pipeline;
619 }
620
621 static GstElement *
622 make_mp3_pipeline (const gchar * location)
623 {
624   GstElement *pipeline;
625   GstElement *src, *decoder, *osssink, *queue;
626   GstPad *seekable;
627
628   pipeline = gst_pipeline_new ("app");
629
630   src = gst_element_factory_make_or_warn (SOURCE, "src");
631   decoder = gst_element_factory_make_or_warn ("mad", "dec");
632   queue = gst_element_factory_make_or_warn ("queue", "queue");
633   osssink = gst_element_factory_make_or_warn (ASINK, "sink");
634
635   seekable_elements = g_list_prepend (seekable_elements, osssink);
636
637   g_object_set (G_OBJECT (src), "location", location, NULL);
638   //g_object_set (G_OBJECT (osssink), "fragment", 0x00180008, NULL);
639
640   gst_bin_add (GST_BIN (pipeline), src);
641   gst_bin_add (GST_BIN (pipeline), decoder);
642   gst_bin_add (GST_BIN (pipeline), queue);
643   gst_bin_add (GST_BIN (pipeline), osssink);
644
645   gst_element_link (src, decoder);
646   gst_element_link (decoder, queue);
647   gst_element_link (queue, osssink);
648
649   seekable = gst_element_get_static_pad (queue, "src");
650   seekable_pads = g_list_prepend (seekable_pads, seekable);
651   rate_pads = g_list_prepend (rate_pads, seekable);
652   rate_pads =
653       g_list_prepend (rate_pads, gst_element_get_static_pad (decoder, "sink"));
654
655   return pipeline;
656 }
657
658 static GstElement *
659 make_avi_pipeline (const gchar * location)
660 {
661   GstElement *pipeline, *audio_bin, *video_bin;
662   GstElement *src, *demux, *a_decoder, *v_decoder, *audiosink, *videosink;
663   GstElement *a_queue = NULL, *v_queue = NULL;
664   GstPad *seekable;
665
666   pipeline = gst_pipeline_new ("app");
667
668   src = gst_element_factory_make_or_warn (SOURCE, "src");
669   g_object_set (G_OBJECT (src), "location", location, NULL);
670
671   demux = gst_element_factory_make_or_warn ("avidemux", "demux");
672   seekable_elements = g_list_prepend (seekable_elements, demux);
673
674   gst_bin_add (GST_BIN (pipeline), src);
675   gst_bin_add (GST_BIN (pipeline), demux);
676   gst_element_link (src, demux);
677
678   audio_bin = gst_bin_new ("a_decoder_bin");
679   a_decoder = gst_element_factory_make_or_warn ("mad", "a_dec");
680   audiosink = gst_element_factory_make_or_warn (ASINK, "a_sink");
681   a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
682   gst_element_link (a_decoder, a_queue);
683   gst_element_link (a_queue, audiosink);
684   gst_bin_add (GST_BIN (audio_bin), a_decoder);
685   gst_bin_add (GST_BIN (audio_bin), a_queue);
686   gst_bin_add (GST_BIN (audio_bin), audiosink);
687   gst_element_set_state (audio_bin, GST_STATE_PAUSED);
688
689   setup_dynamic_link (demux, "audio_00", gst_element_get_static_pad (a_decoder,
690           "sink"), audio_bin);
691
692   seekable = gst_element_get_static_pad (a_queue, "src");
693   seekable_pads = g_list_prepend (seekable_pads, seekable);
694   rate_pads = g_list_prepend (rate_pads, seekable);
695   rate_pads =
696       g_list_prepend (rate_pads, gst_element_get_static_pad (a_decoder,
697           "sink"));
698
699   video_bin = gst_bin_new ("v_decoder_bin");
700   v_decoder = gst_element_factory_make_or_warn ("ffmpegdecall", "v_dec");
701   videosink = gst_element_factory_make_or_warn (VSINK, "v_sink");
702   v_queue = gst_element_factory_make_or_warn ("queue", "v_queue");
703   gst_element_link (v_decoder, v_queue);
704   gst_element_link (v_queue, videosink);
705   gst_bin_add (GST_BIN (video_bin), v_decoder);
706   gst_bin_add (GST_BIN (video_bin), v_queue);
707   gst_bin_add (GST_BIN (video_bin), videosink);
708
709   gst_element_set_state (video_bin, GST_STATE_PAUSED);
710
711   setup_dynamic_link (demux, "video_00", gst_element_get_static_pad (v_decoder,
712           "sink"), video_bin);
713
714   seekable = gst_element_get_static_pad (v_queue, "src");
715   seekable_pads = g_list_prepend (seekable_pads, seekable);
716   rate_pads = g_list_prepend (rate_pads, seekable);
717   rate_pads =
718       g_list_prepend (rate_pads, gst_element_get_static_pad (v_decoder,
719           "sink"));
720
721   return pipeline;
722 }
723
724 static GstElement *
725 make_mpeg_pipeline (const gchar * location)
726 {
727   GstElement *pipeline, *audio_bin, *video_bin;
728   GstElement *src, *demux, *a_decoder, *v_decoder, *v_filter;
729   GstElement *audiosink, *videosink;
730   GstElement *a_queue, *v_queue;
731   GstPad *seekable;
732   GstPad *pad;
733
734   pipeline = gst_pipeline_new ("app");
735
736   src = gst_element_factory_make_or_warn (SOURCE, "src");
737   g_object_set (G_OBJECT (src), "location", location, NULL);
738
739   //demux = gst_element_factory_make_or_warn ("mpegdemux", "demux");
740   demux = gst_element_factory_make_or_warn ("flupsdemux", "demux");
741
742   gst_bin_add (GST_BIN (pipeline), src);
743   gst_bin_add (GST_BIN (pipeline), demux);
744   gst_element_link (src, demux);
745
746   audio_bin = gst_bin_new ("a_decoder_bin");
747   a_decoder = gst_element_factory_make_or_warn ("mad", "a_dec");
748   a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
749   audiosink = gst_element_factory_make_or_warn (ASINK, "a_sink");
750   gst_bin_add (GST_BIN (audio_bin), a_decoder);
751   gst_bin_add (GST_BIN (audio_bin), a_queue);
752   gst_bin_add (GST_BIN (audio_bin), audiosink);
753
754   gst_element_link (a_decoder, a_queue);
755   gst_element_link (a_queue, audiosink);
756
757   gst_bin_add (GST_BIN (pipeline), audio_bin);
758
759   pad = gst_element_get_static_pad (a_decoder, "sink");
760   gst_element_add_pad (audio_bin, gst_ghost_pad_new ("sink", pad));
761   gst_object_unref (pad);
762
763   setup_dynamic_link (demux, "audio_c0", gst_element_get_static_pad (audio_bin,
764           "sink"), NULL);
765
766   video_bin = gst_bin_new ("v_decoder_bin");
767   v_decoder = gst_element_factory_make_or_warn ("mpeg2dec", "v_dec");
768   v_queue = gst_element_factory_make_or_warn ("queue", "v_queue");
769   v_filter = gst_element_factory_make_or_warn ("ffmpegcolorspace", "v_filter");
770   videosink = gst_element_factory_make_or_warn (VSINK, "v_sink");
771
772   gst_bin_add (GST_BIN (video_bin), v_decoder);
773   gst_bin_add (GST_BIN (video_bin), v_queue);
774   gst_bin_add (GST_BIN (video_bin), v_filter);
775   gst_bin_add (GST_BIN (video_bin), videosink);
776
777   gst_element_link (v_decoder, v_queue);
778   gst_element_link (v_queue, v_filter);
779   gst_element_link (v_filter, videosink);
780
781   gst_bin_add (GST_BIN (pipeline), video_bin);
782
783   pad = gst_element_get_static_pad (v_decoder, "sink");
784   gst_element_add_pad (video_bin, gst_ghost_pad_new ("sink", pad));
785   gst_object_unref (pad);
786
787   setup_dynamic_link (demux, "video_e0", gst_element_get_static_pad (video_bin,
788           "sink"), NULL);
789
790   seekable = gst_element_get_static_pad (v_filter, "src");
791   seekable_pads = g_list_prepend (seekable_pads, seekable);
792   rate_pads = g_list_prepend (rate_pads, seekable);
793   rate_pads =
794       g_list_prepend (rate_pads, gst_element_get_static_pad (v_decoder,
795           "sink"));
796
797   return pipeline;
798 }
799
800 static GstElement *
801 make_mpegnt_pipeline (const gchar * location)
802 {
803   GstElement *pipeline, *audio_bin, *video_bin;
804   GstElement *src, *demux, *a_decoder, *v_decoder, *v_filter;
805   GstElement *audiosink, *videosink;
806   GstElement *a_queue;
807   GstPad *seekable;
808
809   pipeline = gst_pipeline_new ("app");
810
811   src = gst_element_factory_make_or_warn (SOURCE, "src");
812   g_object_set (G_OBJECT (src), "location", location, NULL);
813
814   demux = gst_element_factory_make_or_warn ("mpegdemux", "demux");
815   //g_object_set (G_OBJECT (demux), "sync", TRUE, NULL);
816
817   seekable_elements = g_list_prepend (seekable_elements, demux);
818
819   gst_bin_add (GST_BIN (pipeline), src);
820   gst_bin_add (GST_BIN (pipeline), demux);
821   gst_element_link (src, demux);
822
823   audio_bin = gst_bin_new ("a_decoder_bin");
824   a_decoder = gst_element_factory_make_or_warn ("mad", "a_dec");
825   a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
826   audiosink = gst_element_factory_make_or_warn (ASINK, "a_sink");
827   //g_object_set (G_OBJECT (audiosink), "fragment", 0x00180008, NULL);
828   g_object_set (G_OBJECT (audiosink), "sync", FALSE, NULL);
829   gst_element_link (a_decoder, a_queue);
830   gst_element_link (a_queue, audiosink);
831   gst_bin_add (GST_BIN (audio_bin), a_decoder);
832   gst_bin_add (GST_BIN (audio_bin), a_queue);
833   gst_bin_add (GST_BIN (audio_bin), audiosink);
834
835   setup_dynamic_link (demux, "audio_00", gst_element_get_static_pad (a_decoder,
836           "sink"), audio_bin);
837
838   seekable = gst_element_get_static_pad (a_queue, "src");
839   seekable_pads = g_list_prepend (seekable_pads, seekable);
840   rate_pads = g_list_prepend (rate_pads, seekable);
841   rate_pads =
842       g_list_prepend (rate_pads, gst_element_get_static_pad (a_decoder,
843           "sink"));
844
845   video_bin = gst_bin_new ("v_decoder_bin");
846   v_decoder = gst_element_factory_make_or_warn ("mpeg2dec", "v_dec");
847   v_filter = gst_element_factory_make_or_warn ("ffmpegcolorspace", "v_filter");
848   videosink = gst_element_factory_make_or_warn (VSINK, "v_sink");
849   gst_element_link_many (v_decoder, v_filter, videosink, NULL);
850
851   gst_bin_add_many (GST_BIN (video_bin), v_decoder, v_filter, videosink, NULL);
852
853   setup_dynamic_link (demux, "video_00", gst_element_get_static_pad (v_decoder,
854           "sink"), video_bin);
855
856   seekable = gst_element_get_static_pad (v_decoder, "src");
857   seekable_pads = g_list_prepend (seekable_pads, seekable);
858   rate_pads = g_list_prepend (rate_pads, seekable);
859   rate_pads =
860       g_list_prepend (rate_pads, gst_element_get_static_pad (v_decoder,
861           "sink"));
862
863   return pipeline;
864 }
865
866 static GstElement *
867 make_playerbin_pipeline (const gchar * location)
868 {
869   GstElement *player;
870
871   player = gst_element_factory_make ("playbin", "player");
872   g_assert (player);
873
874   g_object_set (G_OBJECT (player), "uri", location, NULL);
875
876   seekable_elements = g_list_prepend (seekable_elements, player);
877
878   /* force element seeking on this pipeline */
879   elem_seek = TRUE;
880
881   return player;
882 }
883
884 static GstElement *
885 make_playerbin2_pipeline (const gchar * location)
886 {
887   GstElement *player;
888
889   player = gst_element_factory_make ("playbin2", "player");
890   g_assert (player);
891
892   g_object_set (G_OBJECT (player), "uri", location, NULL);
893
894   seekable_elements = g_list_prepend (seekable_elements, player);
895
896   /* force element seeking on this pipeline */
897   elem_seek = TRUE;
898
899   return player;
900 }
901
902 #ifndef GST_DISABLE_PARSE
903 static GstElement *
904 make_parselaunch_pipeline (const gchar * description)
905 {
906   GstElement *pipeline;
907   GError *error = NULL;
908
909   pipeline = gst_parse_launch (description, &error);
910
911   seekable_elements = g_list_prepend (seekable_elements, pipeline);
912
913   elem_seek = TRUE;
914
915   return pipeline;
916 }
917 #endif
918
919 typedef struct
920 {
921   gchar *name;
922   GstElement *(*func) (const gchar * location);
923 }
924 Pipeline;
925
926 static Pipeline pipelines[] = {
927   {"mp3", make_mp3_pipeline},
928   {"avi", make_avi_pipeline},
929   {"mpeg1", make_mpeg_pipeline},
930   {"mpegparse", make_parse_pipeline},
931   {"vorbis", make_vorbis_pipeline},
932   {"theora", make_theora_pipeline},
933   {"ogg/v/t", make_vorbis_theora_pipeline},
934   {"avi/msmpeg4v3/mp3", make_avi_msmpeg4v3_mp3_pipeline},
935   {"sid", make_sid_pipeline},
936   {"flac", make_flac_pipeline},
937   {"wav", make_wav_pipeline},
938   {"mod", make_mod_pipeline},
939   {"dv", make_dv_pipeline},
940   {"mpeg1nothreads", make_mpegnt_pipeline},
941   {"playerbin", make_playerbin_pipeline},
942 #ifndef GST_DISABLE_PARSE
943   {"parse-launch", make_parselaunch_pipeline},
944 #endif
945   {"playerbin2", make_playerbin2_pipeline},
946   {NULL, NULL},
947 };
948
949 #define NUM_TYPES       ((sizeof (pipelines) / sizeof (Pipeline)) - 1)
950
951 /* ui callbacks and helpers */
952
953 static gchar *
954 format_value (GtkScale * scale, gdouble value)
955 {
956   gint64 real;
957   gint64 seconds;
958   gint64 subseconds;
959
960   real = value * duration / 100;
961   seconds = (gint64) real / GST_SECOND;
962   subseconds = (gint64) real / (GST_SECOND / 100);
963
964   return g_strdup_printf ("%02" G_GINT64_FORMAT ":%02" G_GINT64_FORMAT ":%02"
965       G_GINT64_FORMAT, seconds / 60, seconds % 60, subseconds % 100);
966 }
967
968 typedef struct
969 {
970   const gchar *name;
971   const GstFormat format;
972 }
973 seek_format;
974
975 static seek_format seek_formats[] = {
976   {"tim", GST_FORMAT_TIME},
977   {"byt", GST_FORMAT_BYTES},
978   {"buf", GST_FORMAT_BUFFERS},
979   {"def", GST_FORMAT_DEFAULT},
980   {NULL, 0},
981 };
982
983 G_GNUC_UNUSED static void
984 query_rates (void)
985 {
986   GList *walk = rate_pads;
987
988   while (walk) {
989     GstPad *pad = GST_PAD (walk->data);
990     gint i = 0;
991
992     g_print ("rate/sec  %8.8s: ", GST_PAD_NAME (pad));
993     while (seek_formats[i].name) {
994       gint64 value;
995       GstFormat format;
996
997       format = seek_formats[i].format;
998
999       if (gst_pad_query_convert (pad, GST_FORMAT_TIME, GST_SECOND, &format,
1000               &value)) {
1001         g_print ("%s %13" G_GINT64_FORMAT " | ", seek_formats[i].name, value);
1002       } else {
1003         g_print ("%s %13.13s | ", seek_formats[i].name, "*NA*");
1004       }
1005
1006       i++;
1007     }
1008     g_print (" %s:%s\n", GST_DEBUG_PAD_NAME (pad));
1009
1010     walk = g_list_next (walk);
1011   }
1012 }
1013
1014 G_GNUC_UNUSED static void
1015 query_positions_elems (void)
1016 {
1017   GList *walk = seekable_elements;
1018
1019   while (walk) {
1020     GstElement *element = GST_ELEMENT (walk->data);
1021     gint i = 0;
1022
1023     g_print ("positions %8.8s: ", GST_ELEMENT_NAME (element));
1024     while (seek_formats[i].name) {
1025       gint64 position, total;
1026       GstFormat format;
1027
1028       format = seek_formats[i].format;
1029
1030       if (gst_element_query_position (element, &format, &position) &&
1031           gst_element_query_duration (element, &format, &total)) {
1032         g_print ("%s %13" G_GINT64_FORMAT " / %13" G_GINT64_FORMAT " | ",
1033             seek_formats[i].name, position, total);
1034       } else {
1035         g_print ("%s %13.13s / %13.13s | ", seek_formats[i].name, "*NA*",
1036             "*NA*");
1037       }
1038       i++;
1039     }
1040     g_print (" %s\n", GST_ELEMENT_NAME (element));
1041
1042     walk = g_list_next (walk);
1043   }
1044 }
1045
1046 G_GNUC_UNUSED static void
1047 query_positions_pads (void)
1048 {
1049   GList *walk = seekable_pads;
1050
1051   while (walk) {
1052     GstPad *pad = GST_PAD (walk->data);
1053     gint i = 0;
1054
1055     g_print ("positions %8.8s: ", GST_PAD_NAME (pad));
1056     while (seek_formats[i].name) {
1057       GstFormat format;
1058       gint64 position, total;
1059
1060       format = seek_formats[i].format;
1061
1062       if (gst_pad_query_position (pad, &format, &position) &&
1063           gst_pad_query_duration (pad, &format, &total)) {
1064         g_print ("%s %13" G_GINT64_FORMAT " / %13" G_GINT64_FORMAT " | ",
1065             seek_formats[i].name, position, total);
1066       } else {
1067         g_print ("%s %13.13s / %13.13s | ", seek_formats[i].name, "*NA*",
1068             "*NA*");
1069       }
1070
1071       i++;
1072     }
1073     g_print (" %s:%s\n", GST_DEBUG_PAD_NAME (pad));
1074
1075     walk = g_list_next (walk);
1076   }
1077 }
1078
1079 static gboolean start_seek (GtkWidget * widget, GdkEventButton * event,
1080     gpointer user_data);
1081 static gboolean stop_seek (GtkWidget * widget, GdkEventButton * event,
1082     gpointer user_data);
1083 static void seek_cb (GtkWidget * widget);
1084
1085 static void
1086 set_scale (gdouble value)
1087 {
1088   g_signal_handlers_block_by_func (hscale, (void *) start_seek,
1089       (void *) pipeline);
1090   g_signal_handlers_block_by_func (hscale, (void *) stop_seek,
1091       (void *) pipeline);
1092   g_signal_handlers_block_by_func (hscale, (void *) seek_cb, (void *) pipeline);
1093   gtk_adjustment_set_value (adjustment, value);
1094   g_signal_handlers_unblock_by_func (hscale, (void *) start_seek,
1095       (void *) pipeline);
1096   g_signal_handlers_unblock_by_func (hscale, (void *) stop_seek,
1097       (void *) pipeline);
1098   g_signal_handlers_unblock_by_func (hscale, (void *) seek_cb,
1099       (void *) pipeline);
1100   gtk_widget_queue_draw (hscale);
1101 }
1102
1103 static gboolean
1104 update_fill (gpointer data)
1105 {
1106   if (elem_seek) {
1107     if (seekable_elements) {
1108       GstElement *element = GST_ELEMENT (seekable_elements->data);
1109       GstQuery *query;
1110
1111       query = gst_query_new_buffering (GST_FORMAT_PERCENT);
1112       if (gst_element_query (element, query)) {
1113         gint64 start, stop;
1114         GstFormat format;
1115         gdouble fill;
1116         gboolean busy;
1117         gint percent;
1118
1119         gst_query_parse_buffering_percent (query, &busy, &percent);
1120
1121         if (buffering && !busy) {
1122           /* if we were buffering but not anymore, start playing */
1123           if (state == GST_STATE_PLAYING && !is_live) {
1124             fprintf (stderr, "setting pipeline to PLAYING ...\n");
1125             gst_element_set_state (pipeline, GST_STATE_PLAYING);
1126             gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
1127             gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id,
1128                 "Playing");
1129           }
1130           state = GST_STATE_PAUSED;
1131           buffering = FALSE;
1132         }
1133
1134         gst_query_parse_buffering_range (query, &format, &start, &stop, NULL);
1135
1136         GST_DEBUG ("start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT,
1137             start, stop);
1138
1139         if (stop != -1)
1140           fill = 100.0 * stop / GST_FORMAT_PERCENT_MAX;
1141         else
1142           fill = 100.0;
1143
1144         gtk_range_set_fill_level (GTK_RANGE (hscale), fill);
1145       }
1146       gst_query_unref (query);
1147     }
1148   }
1149   return TRUE;
1150 }
1151
1152 static gboolean
1153 update_scale (gpointer data)
1154 {
1155   GstFormat format = GST_FORMAT_TIME;
1156
1157   //position = 0;
1158   //duration = 0;
1159
1160   if (elem_seek) {
1161     if (seekable_elements) {
1162       GstElement *element = GST_ELEMENT (seekable_elements->data);
1163
1164       gst_element_query_position (element, &format, &position);
1165       gst_element_query_duration (element, &format, &duration);
1166     }
1167   } else {
1168     if (seekable_pads) {
1169       GstPad *pad = GST_PAD (seekable_pads->data);
1170
1171       gst_pad_query_position (pad, &format, &position);
1172       gst_pad_query_duration (pad, &format, &duration);
1173     }
1174   }
1175
1176   if (stats) {
1177     if (elem_seek) {
1178       query_positions_elems ();
1179     } else {
1180       query_positions_pads ();
1181     }
1182     query_rates ();
1183   }
1184   if (position >= duration)
1185     duration = position;
1186
1187   if (duration > 0) {
1188     set_scale (position * 100.0 / duration);
1189   }
1190
1191   return TRUE;
1192 }
1193
1194 static void do_seek (GtkWidget * widget);
1195 static void connect_bus_signals (GstElement * pipeline);
1196 static void set_update_scale (gboolean active);
1197 static void set_update_fill (gboolean active);
1198
1199 static gboolean
1200 end_scrub (GtkWidget * widget)
1201 {
1202   GST_DEBUG ("end scrub, PAUSE");
1203   gst_element_set_state (pipeline, GST_STATE_PAUSED);
1204   seek_timeout_id = 0;
1205
1206   return FALSE;
1207 }
1208
1209 static gboolean
1210 send_event (GstEvent * event)
1211 {
1212   gboolean res = FALSE;
1213
1214   if (!elem_seek) {
1215     GList *walk = seekable_pads;
1216
1217     while (walk) {
1218       GstPad *seekable = GST_PAD (walk->data);
1219
1220       GST_DEBUG ("send event on pad %s:%s", GST_DEBUG_PAD_NAME (seekable));
1221
1222       gst_event_ref (event);
1223       res = gst_pad_send_event (seekable, event);
1224
1225       walk = g_list_next (walk);
1226     }
1227   } else {
1228     GList *walk = seekable_elements;
1229
1230     while (walk) {
1231       GstElement *seekable = GST_ELEMENT (walk->data);
1232
1233       GST_DEBUG ("send event on element %s", GST_ELEMENT_NAME (seekable));
1234
1235       gst_event_ref (event);
1236       res = gst_element_send_event (seekable, event);
1237
1238       walk = g_list_next (walk);
1239     }
1240   }
1241   gst_event_unref (event);
1242   return res;
1243 }
1244
1245 static void
1246 do_seek (GtkWidget * widget)
1247 {
1248   gint64 real;
1249   gboolean res = FALSE;
1250   GstEvent *s_event;
1251   GstSeekFlags flags;
1252
1253   real = gtk_range_get_value (GTK_RANGE (widget)) * duration / 100;
1254
1255   flags = 0;
1256   if (flush_seek)
1257     flags |= GST_SEEK_FLAG_FLUSH;
1258   if (accurate_seek)
1259     flags |= GST_SEEK_FLAG_ACCURATE;
1260   if (keyframe_seek)
1261     flags |= GST_SEEK_FLAG_KEY_UNIT;
1262   if (loop_seek)
1263     flags |= GST_SEEK_FLAG_SEGMENT;
1264
1265   if (rate >= 0) {
1266     s_event = gst_event_new_seek (rate,
1267         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, real, GST_SEEK_TYPE_SET, -1);
1268     GST_DEBUG ("seek with rate %lf to %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT,
1269         rate, GST_TIME_ARGS (real), GST_TIME_ARGS (duration));
1270   } else {
1271     s_event = gst_event_new_seek (rate,
1272         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
1273         GST_SEEK_TYPE_SET, real);
1274     GST_DEBUG ("seek with rate %lf to %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT,
1275         rate, GST_TIME_ARGS (0), GST_TIME_ARGS (real));
1276   }
1277
1278   res = send_event (s_event);
1279
1280   if (res) {
1281     if (flush_seek) {
1282       gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL, SEEK_TIMEOUT);
1283     } else {
1284       set_update_scale (TRUE);
1285     }
1286   } else {
1287     g_print ("seek failed\n");
1288     set_update_scale (TRUE);
1289   }
1290 }
1291
1292 static void
1293 seek_cb (GtkWidget * widget)
1294 {
1295   /* If the timer hasn't expired yet, then the pipeline is running */
1296   if (play_scrub && seek_timeout_id != 0) {
1297     GST_DEBUG ("do scrub seek, PAUSED");
1298     gst_element_set_state (pipeline, GST_STATE_PAUSED);
1299   }
1300
1301   GST_DEBUG ("do seek");
1302   do_seek (widget);
1303
1304   if (play_scrub) {
1305     GST_DEBUG ("do scrub seek, PLAYING");
1306     gst_element_set_state (pipeline, GST_STATE_PLAYING);
1307
1308     if (seek_timeout_id == 0) {
1309       seek_timeout_id =
1310           g_timeout_add (SCRUB_TIME, (GSourceFunc) end_scrub, widget);
1311     }
1312   }
1313 }
1314
1315 static void
1316 set_update_fill (gboolean active)
1317 {
1318   GST_DEBUG ("fill scale is %d", active);
1319
1320   if (active) {
1321     if (fill_id == 0) {
1322       fill_id =
1323           g_timeout_add (FILL_INTERVAL, (GtkFunction) update_fill, pipeline);
1324     }
1325   } else {
1326     if (fill_id) {
1327       g_source_remove (fill_id);
1328       fill_id = 0;
1329     }
1330   }
1331 }
1332
1333 static void
1334 set_update_scale (gboolean active)
1335 {
1336
1337   GST_DEBUG ("update scale is %d", active);
1338
1339   if (active) {
1340     if (update_id == 0) {
1341       update_id =
1342           g_timeout_add (UPDATE_INTERVAL, (GtkFunction) update_scale, pipeline);
1343     }
1344   } else {
1345     if (update_id) {
1346       g_source_remove (update_id);
1347       update_id = 0;
1348     }
1349   }
1350 }
1351
1352 static gboolean
1353 start_seek (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
1354 {
1355   if (event->type != GDK_BUTTON_PRESS)
1356     return FALSE;
1357
1358   set_update_scale (FALSE);
1359
1360   if (state == GST_STATE_PLAYING && flush_seek && scrub) {
1361     GST_DEBUG ("start scrub seek, PAUSE");
1362     gst_element_set_state (pipeline, GST_STATE_PAUSED);
1363   }
1364
1365   if (changed_id == 0 && flush_seek && scrub) {
1366     changed_id = gtk_signal_connect (GTK_OBJECT (hscale),
1367         "value_changed", G_CALLBACK (seek_cb), pipeline);
1368   }
1369
1370   return FALSE;
1371 }
1372
1373 static gboolean
1374 stop_seek (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
1375 {
1376   if (changed_id) {
1377     g_signal_handler_disconnect (GTK_OBJECT (hscale), changed_id);
1378     changed_id = 0;
1379   }
1380
1381   if (!flush_seek || !scrub) {
1382     GST_DEBUG ("do final seek");
1383     do_seek (widget);
1384   }
1385
1386   if (seek_timeout_id != 0) {
1387     g_source_remove (seek_timeout_id);
1388     seek_timeout_id = 0;
1389     /* Still scrubbing, so the pipeline is playing, see if we need PAUSED
1390      * instead. */
1391     if (state == GST_STATE_PAUSED) {
1392       GST_DEBUG ("stop scrub seek, PAUSED");
1393       gst_element_set_state (pipeline, GST_STATE_PAUSED);
1394     }
1395   } else {
1396     if (state == GST_STATE_PLAYING) {
1397       GST_DEBUG ("stop scrub seek, PLAYING");
1398       gst_element_set_state (pipeline, GST_STATE_PLAYING);
1399     }
1400   }
1401
1402   return FALSE;
1403 }
1404
1405 static void
1406 play_cb (GtkButton * button, gpointer data)
1407 {
1408   GstStateChangeReturn ret;
1409
1410   if (state != GST_STATE_PLAYING) {
1411     g_print ("PLAY pipeline\n");
1412     gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
1413
1414     ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
1415     switch (ret) {
1416       case GST_STATE_CHANGE_FAILURE:
1417         goto failed;
1418       case GST_STATE_CHANGE_NO_PREROLL:
1419         is_live = TRUE;
1420         break;
1421       default:
1422         break;
1423     }
1424     state = GST_STATE_PLAYING;
1425     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Playing");
1426   }
1427   return;
1428
1429 failed:
1430   {
1431     g_print ("PLAY failed\n");
1432     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Play failed");
1433   }
1434 }
1435
1436 static void
1437 pause_cb (GtkButton * button, gpointer data)
1438 {
1439   if (state != GST_STATE_PAUSED) {
1440     GstStateChangeReturn ret;
1441
1442     gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
1443     g_print ("PAUSE pipeline\n");
1444     ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
1445     switch (ret) {
1446       case GST_STATE_CHANGE_FAILURE:
1447         goto failed;
1448       case GST_STATE_CHANGE_NO_PREROLL:
1449         is_live = TRUE;
1450         break;
1451       default:
1452         break;
1453     }
1454
1455     state = GST_STATE_PAUSED;
1456     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Paused");
1457   }
1458   return;
1459
1460 failed:
1461   {
1462     g_print ("PAUSE failed\n");
1463     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Pause failed");
1464   }
1465 }
1466
1467 static void
1468 stop_cb (GtkButton * button, gpointer data)
1469 {
1470   if (state != GST_STATE_READY) {
1471     GstStateChangeReturn ret;
1472
1473     g_print ("READY pipeline\n");
1474     gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
1475
1476     ret = gst_element_set_state (pipeline, GST_STATE_READY);
1477     if (ret == GST_STATE_CHANGE_FAILURE)
1478       goto failed;
1479
1480     state = GST_STATE_READY;
1481     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Stopped");
1482
1483     is_live = FALSE;
1484     buffering = FALSE;
1485     set_update_scale (FALSE);
1486     set_scale (0.0);
1487     set_update_fill (FALSE);
1488
1489     if (pipeline_type == 16)
1490       clear_streams (pipeline);
1491
1492     /* if one uses parse_launch, play, stop and play again it fails as all the
1493      * pads after the demuxer can't be reconnected
1494      */
1495     if (!strcmp (pipelines[pipeline_type].name, "parse-launch")) {
1496       gst_element_set_state (pipeline, GST_STATE_NULL);
1497       gst_object_unref (pipeline);
1498
1499       pipeline = pipelines[pipeline_type].func (pipeline_spec);
1500       g_assert (pipeline);
1501       gst_element_set_state (pipeline, GST_STATE_READY);
1502       connect_bus_signals (pipeline);
1503     }
1504   }
1505   return;
1506
1507 failed:
1508   {
1509     g_print ("STOP failed\n");
1510     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Stop failed");
1511   }
1512 }
1513
1514 static void
1515 accurate_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1516 {
1517   accurate_seek = gtk_toggle_button_get_active (button);
1518 }
1519
1520 static void
1521 key_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1522 {
1523   keyframe_seek = gtk_toggle_button_get_active (button);
1524 }
1525
1526 static void
1527 loop_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1528 {
1529   loop_seek = gtk_toggle_button_get_active (button);
1530   if (state == GST_STATE_PLAYING) {
1531     do_seek (hscale);
1532   }
1533 }
1534
1535 static void
1536 flush_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1537 {
1538   flush_seek = gtk_toggle_button_get_active (button);
1539 }
1540
1541 static void
1542 scrub_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1543 {
1544   scrub = gtk_toggle_button_get_active (button);
1545 }
1546
1547 static void
1548 play_scrub_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1549 {
1550   play_scrub = gtk_toggle_button_get_active (button);
1551 }
1552
1553 static void
1554 rate_spinbutton_changed_cb (GtkSpinButton * button, GstPipeline * pipeline)
1555 {
1556   gboolean res = FALSE;
1557   GstEvent *s_event;
1558   GstSeekFlags flags;
1559
1560   rate = gtk_spin_button_get_value (button);
1561
1562   flags = 0;
1563   if (flush_seek)
1564     flags |= GST_SEEK_FLAG_FLUSH;
1565   if (loop_seek)
1566     flags |= GST_SEEK_FLAG_SEGMENT;
1567   if (accurate_seek)
1568     flags |= GST_SEEK_FLAG_ACCURATE;
1569   if (keyframe_seek)
1570     flags |= GST_SEEK_FLAG_KEY_UNIT;
1571
1572   if (rate >= 0) {
1573     s_event = gst_event_new_seek (rate,
1574         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, position,
1575         GST_SEEK_TYPE_SET, -1);
1576   } else {
1577     s_event = gst_event_new_seek (rate,
1578         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
1579         GST_SEEK_TYPE_SET, position);
1580   }
1581
1582   GST_DEBUG ("rate changed to %lf", rate);
1583
1584   res = send_event (s_event);
1585
1586   if (res) {
1587     if (flush_seek) {
1588       gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL, SEEK_TIMEOUT);
1589     }
1590   } else
1591     g_print ("seek failed\n");
1592 }
1593
1594 static void
1595 update_flag (GstPipeline * pipeline, gint num, gboolean state)
1596 {
1597   gint flags;
1598
1599   g_object_get (pipeline, "flags", &flags, NULL);
1600   if (state)
1601     flags |= (1 << num);
1602   else
1603     flags &= ~(1 << num);
1604   g_object_set (pipeline, "flags", flags, NULL);
1605 }
1606
1607 static void
1608 vis_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1609 {
1610   gboolean state;
1611
1612   state = gtk_toggle_button_get_active (button);
1613   update_flag (pipeline, 3, state);
1614   gtk_widget_set_sensitive (vis_combo, state);
1615 }
1616
1617 static void
1618 audio_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1619 {
1620   gboolean state;
1621
1622   state = gtk_toggle_button_get_active (button);
1623   update_flag (pipeline, 1, state);
1624   gtk_widget_set_sensitive (audio_combo, state);
1625 }
1626
1627 static void
1628 video_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1629 {
1630   gboolean state;
1631
1632   state = gtk_toggle_button_get_active (button);
1633   update_flag (pipeline, 0, state);
1634   gtk_widget_set_sensitive (video_combo, state);
1635 }
1636
1637 static void
1638 text_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1639 {
1640   gboolean state;
1641
1642   state = gtk_toggle_button_get_active (button);
1643   update_flag (pipeline, 2, state);
1644   gtk_widget_set_sensitive (text_combo, state);
1645 }
1646
1647 static void
1648 mute_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
1649 {
1650   gboolean mute;
1651
1652   mute = gtk_toggle_button_get_active (button);
1653   g_object_set (pipeline, "mute", mute, NULL);
1654 }
1655
1656 static void
1657 clear_streams (GstElement * pipeline)
1658 {
1659   gint i;
1660
1661   /* remove previous info */
1662   for (i = 0; i < n_video; i++)
1663     gtk_combo_box_remove_text (GTK_COMBO_BOX (video_combo), 0);
1664   for (i = 0; i < n_audio; i++)
1665     gtk_combo_box_remove_text (GTK_COMBO_BOX (audio_combo), 0);
1666   for (i = 0; i < n_text; i++)
1667     gtk_combo_box_remove_text (GTK_COMBO_BOX (text_combo), 0);
1668
1669   n_audio = n_video = n_text = 0;
1670   gtk_widget_set_sensitive (video_combo, FALSE);
1671   gtk_widget_set_sensitive (audio_combo, FALSE);
1672   gtk_widget_set_sensitive (text_combo, FALSE);
1673
1674   need_streams = TRUE;
1675 }
1676
1677 static void
1678 update_streams (GstPipeline * pipeline)
1679 {
1680   gint i;
1681
1682   if (pipeline_type == 16 && need_streams) {
1683     GstTagList *tags;
1684     gchar *name;
1685     gint active_idx;
1686     gboolean state;
1687
1688     /* remove previous info */
1689     clear_streams (GST_ELEMENT_CAST (pipeline));
1690
1691     /* here we get and update the different streams detected by playbin2 */
1692     g_object_get (pipeline, "n-video", &n_video, NULL);
1693     g_object_get (pipeline, "n-audio", &n_audio, NULL);
1694     g_object_get (pipeline, "n-text", &n_text, NULL);
1695
1696     g_print ("video %d, audio %d, text %d\n", n_video, n_audio, n_text);
1697
1698     active_idx = 0;
1699     for (i = 0; i < n_video; i++) {
1700       g_signal_emit_by_name (pipeline, "get-video-tags", i, &tags);
1701       /* find good name for the label */
1702       name = g_strdup_printf ("video %d", i + 1);
1703       gtk_combo_box_append_text (GTK_COMBO_BOX (video_combo), name);
1704       g_free (name);
1705     }
1706     state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (video_checkbox));
1707     gtk_widget_set_sensitive (video_combo, state && n_video > 0);
1708     gtk_combo_box_set_active (GTK_COMBO_BOX (video_combo), active_idx);
1709
1710     active_idx = 0;
1711     for (i = 0; i < n_audio; i++) {
1712       g_signal_emit_by_name (pipeline, "get-audio-tags", i, &tags);
1713       /* find good name for the label */
1714       name = g_strdup_printf ("audio %d", i + 1);
1715       gtk_combo_box_append_text (GTK_COMBO_BOX (audio_combo), name);
1716       g_free (name);
1717     }
1718     state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (audio_checkbox));
1719     gtk_widget_set_sensitive (audio_combo, state && n_audio > 0);
1720     gtk_combo_box_set_active (GTK_COMBO_BOX (audio_combo), active_idx);
1721
1722     active_idx = 0;
1723     for (i = 0; i < n_text; i++) {
1724       g_signal_emit_by_name (pipeline, "get-text-tags", i, &tags);
1725       name = NULL;
1726       if (tags) {
1727         const GValue *value;
1728
1729         /* get the language code if we can */
1730         value = gst_tag_list_get_value_index (tags, GST_TAG_LANGUAGE_CODE, 0);
1731         if (value && G_VALUE_HOLDS_STRING (value)) {
1732           name = g_strdup_printf ("text %s", g_value_get_string (value));
1733         }
1734       }
1735       /* find good name for the label if we didn't use a tag */
1736       if (name == NULL)
1737         name = g_strdup_printf ("text %d", i + 1);
1738
1739       gtk_combo_box_append_text (GTK_COMBO_BOX (text_combo), name);
1740       g_free (name);
1741     }
1742     state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (text_checkbox));
1743     gtk_widget_set_sensitive (text_combo, state && n_text > 0);
1744     gtk_combo_box_set_active (GTK_COMBO_BOX (text_combo), active_idx);
1745
1746     need_streams = FALSE;
1747   }
1748 }
1749
1750 static void
1751 video_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1752 {
1753   g_object_set (pipeline, "current-video", gtk_combo_box_get_active (combo),
1754       NULL);
1755 }
1756
1757 static void
1758 audio_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1759 {
1760   g_object_set (pipeline, "current-audio", gtk_combo_box_get_active (combo),
1761       NULL);
1762 }
1763
1764 static void
1765 text_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1766 {
1767   g_object_set (pipeline, "current-text", gtk_combo_box_get_active (combo),
1768       NULL);
1769 }
1770
1771 static gboolean
1772 filter_features (GstPluginFeature * feature, gpointer data)
1773 {
1774   GstElementFactory *f;
1775
1776   if (!GST_IS_ELEMENT_FACTORY (feature))
1777     return FALSE;
1778   f = GST_ELEMENT_FACTORY (feature);
1779   if (!g_strrstr (gst_element_factory_get_klass (f), "Visualization"))
1780     return FALSE;
1781
1782   return TRUE;
1783 }
1784
1785 static void
1786 init_visualization_features (void)
1787 {
1788   GList *list, *walk;
1789
1790   vis_entries = g_array_new (FALSE, FALSE, sizeof (VisEntry));
1791
1792   list = gst_registry_feature_filter (gst_registry_get_default (),
1793       filter_features, FALSE, NULL);
1794
1795   for (walk = list; walk; walk = g_list_next (walk)) {
1796     VisEntry entry;
1797     const gchar *name;
1798
1799     entry.factory = GST_ELEMENT_FACTORY (walk->data);
1800     name = gst_element_factory_get_longname (entry.factory);
1801
1802     g_array_append_val (vis_entries, entry);
1803     gtk_combo_box_append_text (GTK_COMBO_BOX (vis_combo), name);
1804   }
1805   gtk_combo_box_set_active (GTK_COMBO_BOX (vis_combo), 0);
1806 }
1807
1808 static void
1809 vis_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1810 {
1811   guint index;
1812   VisEntry *entry;
1813   GstElement *element;
1814
1815   /* get the selected index and get the factory for this index */
1816   index = gtk_combo_box_get_active (GTK_COMBO_BOX (vis_combo));
1817   entry = &g_array_index (vis_entries, VisEntry, index);
1818   /* create an instance of the element from the factory */
1819   element = gst_element_factory_create (entry->factory, NULL);
1820   if (!element)
1821     return;
1822
1823   /* set vis plugin for playbin2 */
1824   g_object_set (pipeline, "vis-plugin", element, NULL);
1825 }
1826
1827 static void
1828 volume_spinbutton_changed_cb (GtkSpinButton * button, GstPipeline * pipeline)
1829 {
1830   gdouble volume;
1831
1832   volume = gtk_spin_button_get_value (button);
1833
1834   g_object_set (pipeline, "volume", volume, NULL);
1835 }
1836
1837 static void
1838 shot_cb (GtkButton * button, gpointer data)
1839 {
1840   GstBuffer *buffer;
1841   GstCaps *caps;
1842
1843   /* convert to our desired format (RGB24) */
1844   caps = gst_caps_new_simple ("video/x-raw-rgb",
1845       "bpp", G_TYPE_INT, 24, "depth", G_TYPE_INT, 24,
1846       /* Note: we don't ask for a specific width/height here, so that
1847        * videoscale can adjust dimensions from a non-1/1 pixel aspect
1848        * ratio to a 1/1 pixel-aspect-ratio */
1849       "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
1850       "endianness", G_TYPE_INT, G_BIG_ENDIAN,
1851       "red_mask", G_TYPE_INT, 0xff0000,
1852       "green_mask", G_TYPE_INT, 0x00ff00,
1853       "blue_mask", G_TYPE_INT, 0x0000ff, NULL);
1854
1855   /* convert the latest frame to the requested format */
1856   g_signal_emit_by_name (pipeline, "convert-frame", caps, &buffer);
1857   gst_caps_unref (caps);
1858
1859   if (buffer) {
1860     GstCaps *caps;
1861     GstStructure *s;
1862     gboolean res;
1863     gint width, height;
1864     GdkPixbuf *pixbuf;
1865     GError *error = NULL;
1866
1867     /* get the snapshot buffer format now. We set the caps on the appsink so
1868      * that it can only be an rgb buffer. The only thing we have not specified
1869      * on the caps is the height, which is dependant on the pixel-aspect-ratio
1870      * of the source material */
1871     caps = GST_BUFFER_CAPS (buffer);
1872     if (!caps) {
1873       g_warning ("could not get snapshot format\n");
1874       goto done;
1875     }
1876     s = gst_caps_get_structure (caps, 0);
1877
1878     /* we need to get the final caps on the buffer to get the size */
1879     res = gst_structure_get_int (s, "width", &width);
1880     res |= gst_structure_get_int (s, "height", &height);
1881     if (!res) {
1882       g_warning ("could not get snapshot dimension\n");
1883       goto done;
1884     }
1885
1886     /* create pixmap from buffer and save, gstreamer video buffers have a stride
1887      * that is rounded up to the nearest multiple of 4 */
1888     pixbuf = gdk_pixbuf_new_from_data (GST_BUFFER_DATA (buffer),
1889         GDK_COLORSPACE_RGB, FALSE, 8, width, height,
1890         GST_ROUND_UP_4 (width * 3), NULL, NULL);
1891
1892     /* save the pixbuf */
1893     gdk_pixbuf_save (pixbuf, "snapshot.png", "png", &error, NULL);
1894
1895   done:
1896     gst_buffer_unref (buffer);
1897   }
1898 }
1899
1900 static void
1901 message_received (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
1902 {
1903   const GstStructure *s;
1904
1905   s = gst_message_get_structure (message);
1906   g_print ("message from \"%s\" (%s): ",
1907       GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
1908       gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
1909   if (s) {
1910     gchar *sstr;
1911
1912     sstr = gst_structure_to_string (s);
1913     g_print ("%s\n", sstr);
1914     g_free (sstr);
1915   } else {
1916     g_print ("no message details\n");
1917   }
1918 }
1919
1920 static void
1921 msg_async_done (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
1922 {
1923   GST_DEBUG ("async done");
1924   /* when we get ASYNC_DONE we can query position, duration and other
1925    * properties */
1926   update_scale (pipeline);
1927
1928   /* update the available streams */
1929   update_streams (pipeline);
1930 }
1931
1932 static void
1933 msg_state_changed (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
1934 {
1935   const GstStructure *s;
1936
1937   s = gst_message_get_structure (message);
1938
1939   /* We only care about state changed on the pipeline */
1940   if (s && GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
1941     GstState old, new, pending;
1942
1943     gst_message_parse_state_changed (message, &old, &new, &pending);
1944
1945     /* When state of the pipeline changes to paused or playing we start updating scale */
1946     if (new == GST_STATE_PLAYING) {
1947       set_update_scale (TRUE);
1948     } else {
1949       set_update_scale (FALSE);
1950     }
1951   }
1952 }
1953
1954 static void
1955 msg_segment_done (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
1956 {
1957   GstEvent *s_event;
1958   GstSeekFlags flags;
1959   gboolean res;
1960   GstFormat format;
1961
1962   GST_DEBUG ("position is %" GST_TIME_FORMAT, GST_TIME_ARGS (position));
1963   gst_message_parse_segment_done (message, &format, &position);
1964   GST_DEBUG ("end of segment at %" GST_TIME_FORMAT, GST_TIME_ARGS (position));
1965
1966   flags = 0;
1967   /* in the segment-done callback we never flush as this would not make sense
1968    * for seamless playback. */
1969   if (loop_seek)
1970     flags = GST_SEEK_FLAG_SEGMENT;
1971
1972   s_event = gst_event_new_seek (rate,
1973       GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
1974       GST_SEEK_TYPE_SET, duration);
1975
1976   GST_DEBUG ("restart loop with rate %lf to 0 / %" GST_TIME_FORMAT,
1977       rate, GST_TIME_ARGS (duration));
1978
1979   res = send_event (s_event);
1980   if (!res)
1981     g_print ("segment seek failed\n");
1982 }
1983
1984 /* in stream buffering mode we PAUSE the pipeline until we receive a 100%
1985  * message */
1986 static void
1987 do_stream_buffering (gint percent)
1988 {
1989   gchar *bufstr;
1990
1991   gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
1992   bufstr = g_strdup_printf ("Buffering...%d", percent);
1993   gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, bufstr);
1994   g_free (bufstr);
1995
1996   if (percent == 100) {
1997     /* a 100% message means buffering is done */
1998     buffering = FALSE;
1999     /* if the desired state is playing, go back */
2000     if (state == GST_STATE_PLAYING) {
2001       /* no state management needed for live pipelines */
2002       if (!is_live) {
2003         fprintf (stderr, "Done buffering, setting pipeline to PLAYING ...\n");
2004         gst_element_set_state (pipeline, GST_STATE_PLAYING);
2005       }
2006       gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
2007       gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Playing");
2008     }
2009   } else {
2010     /* buffering busy */
2011     if (buffering == FALSE && state == GST_STATE_PLAYING) {
2012       /* we were not buffering but PLAYING, PAUSE  the pipeline. */
2013       if (!is_live) {
2014         fprintf (stderr, "Buffering, setting pipeline to PAUSED ...\n");
2015         gst_element_set_state (pipeline, GST_STATE_PAUSED);
2016       }
2017     }
2018     buffering = TRUE;
2019   }
2020 }
2021
2022 static void
2023 do_download_buffering (gint percent)
2024 {
2025   if (!buffering && percent < 100) {
2026     gchar *bufstr;
2027
2028     buffering = TRUE;
2029
2030     bufstr = g_strdup_printf ("Downloading...");
2031     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, bufstr);
2032     g_free (bufstr);
2033
2034     /* once we get a buffering message, we'll do the fill update */
2035     set_update_fill (TRUE);
2036
2037     if (state == GST_STATE_PLAYING && !is_live) {
2038       fprintf (stderr, "Downloading, setting pipeline to PAUSED ...\n");
2039       gst_element_set_state (pipeline, GST_STATE_PAUSED);
2040     }
2041   }
2042 }
2043
2044 static void
2045 msg_buffering (GstBus * bus, GstMessage * message, GstPipeline * data)
2046 {
2047   gint percent;
2048
2049   gst_message_parse_buffering (message, &percent);
2050
2051   /* get more stats */
2052   gst_message_parse_buffering_stats (message, &mode, NULL, NULL,
2053       &buffering_left);
2054
2055   switch (mode) {
2056     case GST_BUFFERING_DOWNLOAD:
2057       do_download_buffering (percent);
2058       break;
2059     case GST_BUFFERING_LIVE:
2060     case GST_BUFFERING_TIMESHIFT:
2061     case GST_BUFFERING_STREAM:
2062       do_stream_buffering (percent);
2063       break;
2064   }
2065 }
2066
2067 static void
2068 connect_bus_signals (GstElement * pipeline)
2069 {
2070   GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
2071
2072   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
2073
2074   g_signal_connect (bus, "message::state-changed",
2075       (GCallback) msg_state_changed, pipeline);
2076   g_signal_connect (bus, "message::segment-done", (GCallback) msg_segment_done,
2077       pipeline);
2078   g_signal_connect (bus, "message::async-done", (GCallback) msg_async_done,
2079       pipeline);
2080
2081   g_signal_connect (bus, "message::new-clock", (GCallback) message_received,
2082       pipeline);
2083   g_signal_connect (bus, "message::error", (GCallback) message_received,
2084       pipeline);
2085   g_signal_connect (bus, "message::warning", (GCallback) message_received,
2086       pipeline);
2087   g_signal_connect (bus, "message::eos", (GCallback) message_received,
2088       pipeline);
2089   g_signal_connect (bus, "message::tag", (GCallback) message_received,
2090       pipeline);
2091   g_signal_connect (bus, "message::element", (GCallback) message_received,
2092       pipeline);
2093   g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
2094       pipeline);
2095   g_signal_connect (bus, "message::buffering", (GCallback) msg_buffering,
2096       pipeline);
2097
2098   gst_object_unref (bus);
2099 }
2100
2101 static void
2102 print_usage (int argc, char **argv)
2103 {
2104   gint i;
2105
2106   g_print ("usage: %s <type> <filename>\n", argv[0]);
2107   g_print ("   possible types:\n");
2108
2109   for (i = 0; i < NUM_TYPES; i++) {
2110     g_print ("     %d = %s\n", i, pipelines[i].name);
2111   }
2112 }
2113
2114 int
2115 main (int argc, char **argv)
2116 {
2117   GtkWidget *window, *hbox, *vbox, *panel, *boxes, *flagtable, *boxes2;
2118   GtkWidget *play_button, *pause_button, *stop_button, *shot_button;
2119   GtkWidget *accurate_checkbox, *key_checkbox, *loop_checkbox, *flush_checkbox;
2120   GtkWidget *scrub_checkbox, *play_scrub_checkbox, *rate_spinbutton;
2121   GtkWidget *rate_label;
2122   GtkTooltips *tips;
2123   GOptionEntry options[] = {
2124     {"stats", 's', 0, G_OPTION_ARG_NONE, &stats,
2125         "Show pad stats", NULL},
2126     {"elem", 'e', 0, G_OPTION_ARG_NONE, &elem_seek,
2127         "Seek on elements instead of pads", NULL},
2128     {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
2129         "Verbose properties", NULL},
2130     {NULL}
2131   };
2132   GOptionContext *ctx;
2133   GError *err = NULL;
2134
2135   if (!g_thread_supported ())
2136     g_thread_init (NULL);
2137
2138   ctx = g_option_context_new ("- test seeking in gsteamer");
2139   g_option_context_add_main_entries (ctx, options, NULL);
2140   g_option_context_add_group (ctx, gst_init_get_option_group ());
2141
2142   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
2143     g_print ("Error initializing: %s\n", err->message);
2144     exit (1);
2145   }
2146
2147   GST_DEBUG_CATEGORY_INIT (seek_debug, "seek", 0, "seek example");
2148
2149   gtk_init (&argc, &argv);
2150
2151   if (argc != 3) {
2152     print_usage (argc, argv);
2153     exit (-1);
2154   }
2155
2156   pipeline_type = atoi (argv[1]);
2157
2158   if (pipeline_type < 0 || pipeline_type >= NUM_TYPES) {
2159     print_usage (argc, argv);
2160     exit (-1);
2161   }
2162
2163   pipeline_spec = argv[2];
2164
2165   pipeline = pipelines[pipeline_type].func (pipeline_spec);
2166   g_assert (pipeline);
2167
2168   /* initialize gui elements ... */
2169   tips = gtk_tooltips_new ();
2170   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
2171   statusbar = gtk_statusbar_new ();
2172   status_id = gtk_statusbar_get_context_id (GTK_STATUSBAR (statusbar), "seek");
2173   gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Stopped");
2174   hbox = gtk_hbox_new (FALSE, 0);
2175   vbox = gtk_vbox_new (FALSE, 0);
2176   flagtable = gtk_table_new (4, 2, FALSE);
2177   gtk_container_set_border_width (GTK_CONTAINER (vbox), 3);
2178
2179   /* media controls */
2180   play_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_PLAY);
2181   pause_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_PAUSE);
2182   stop_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_STOP);
2183
2184   /* seek flags */
2185   accurate_checkbox = gtk_check_button_new_with_label ("Accurate Seek");
2186   key_checkbox = gtk_check_button_new_with_label ("Key-unit Seek");
2187   loop_checkbox = gtk_check_button_new_with_label ("Loop");
2188   flush_checkbox = gtk_check_button_new_with_label ("Flush");
2189   scrub_checkbox = gtk_check_button_new_with_label ("Scrub");
2190   play_scrub_checkbox = gtk_check_button_new_with_label ("Play Scrub");
2191   rate_spinbutton = gtk_spin_button_new_with_range (-100, 100, 0.1);
2192   gtk_spin_button_set_digits (GTK_SPIN_BUTTON (rate_spinbutton), 3);
2193   rate_label = gtk_label_new ("Rate");
2194
2195   gtk_tooltips_set_tip (tips, accurate_checkbox,
2196       "accurate position is requested, this might be considerably slower for some formats",
2197       NULL);
2198   gtk_tooltips_set_tip (tips, key_checkbox,
2199       "seek to the nearest keyframe. This might be faster but less accurate",
2200       NULL);
2201   gtk_tooltips_set_tip (tips, loop_checkbox, "loop playback", NULL);
2202   gtk_tooltips_set_tip (tips, flush_checkbox, "flush pipeline after seeking",
2203       NULL);
2204   gtk_tooltips_set_tip (tips, rate_spinbutton, "define the playback rate, "
2205       "negative value trigger reverse playback", NULL);
2206   gtk_tooltips_set_tip (tips, scrub_checkbox, "show images while seeking",
2207       NULL);
2208   gtk_tooltips_set_tip (tips, play_scrub_checkbox, "play video while seeking",
2209       NULL);
2210
2211   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (flush_checkbox), TRUE);
2212   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (scrub_checkbox), TRUE);
2213
2214   gtk_spin_button_set_value (GTK_SPIN_BUTTON (rate_spinbutton), rate);
2215
2216   /* seek bar */
2217   adjustment =
2218       GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.00, 100.0, 0.1, 1.0, 1.0));
2219   hscale = gtk_hscale_new (adjustment);
2220   gtk_scale_set_digits (GTK_SCALE (hscale), 2);
2221 #if GTK_CHECK_VERSION(2,12,0)
2222   gtk_range_set_show_fill_level (GTK_RANGE (hscale), TRUE);
2223   gtk_range_set_fill_level (GTK_RANGE (hscale), 100.0);
2224 #endif
2225   gtk_range_set_update_policy (GTK_RANGE (hscale), GTK_UPDATE_CONTINUOUS);
2226
2227   gtk_signal_connect (GTK_OBJECT (hscale),
2228       "button_press_event", G_CALLBACK (start_seek), pipeline);
2229   gtk_signal_connect (GTK_OBJECT (hscale),
2230       "button_release_event", G_CALLBACK (stop_seek), pipeline);
2231   gtk_signal_connect (GTK_OBJECT (hscale),
2232       "format_value", G_CALLBACK (format_value), pipeline);
2233
2234   if (pipeline_type == 16) {
2235     /* the playbin2 panel controls for the video/audio/subtitle tracks */
2236     panel = gtk_hbox_new (FALSE, 0);
2237     video_combo = gtk_combo_box_new_text ();
2238     audio_combo = gtk_combo_box_new_text ();
2239     text_combo = gtk_combo_box_new_text ();
2240     gtk_widget_set_sensitive (video_combo, FALSE);
2241     gtk_widget_set_sensitive (audio_combo, FALSE);
2242     gtk_widget_set_sensitive (text_combo, FALSE);
2243     gtk_box_pack_start (GTK_BOX (panel), video_combo, TRUE, TRUE, 2);
2244     gtk_box_pack_start (GTK_BOX (panel), audio_combo, TRUE, TRUE, 2);
2245     gtk_box_pack_start (GTK_BOX (panel), text_combo, TRUE, TRUE, 2);
2246     g_signal_connect (G_OBJECT (video_combo), "changed",
2247         G_CALLBACK (video_combo_cb), pipeline);
2248     g_signal_connect (G_OBJECT (audio_combo), "changed",
2249         G_CALLBACK (audio_combo_cb), pipeline);
2250     g_signal_connect (G_OBJECT (text_combo), "changed",
2251         G_CALLBACK (text_combo_cb), pipeline);
2252     /* playbin2 panel for flag checkboxes and volume/mute */
2253     boxes = gtk_hbox_new (FALSE, 0);
2254     vis_checkbox = gtk_check_button_new_with_label ("Vis");
2255     video_checkbox = gtk_check_button_new_with_label ("Video");
2256     audio_checkbox = gtk_check_button_new_with_label ("Audio");
2257     text_checkbox = gtk_check_button_new_with_label ("Text");
2258     mute_checkbox = gtk_check_button_new_with_label ("Mute");
2259     volume_spinbutton = gtk_spin_button_new_with_range (0, 10.0, 0.1);
2260     gtk_spin_button_set_value (GTK_SPIN_BUTTON (volume_spinbutton), 1.0);
2261     gtk_box_pack_start (GTK_BOX (boxes), video_checkbox, TRUE, TRUE, 2);
2262     gtk_box_pack_start (GTK_BOX (boxes), audio_checkbox, TRUE, TRUE, 2);
2263     gtk_box_pack_start (GTK_BOX (boxes), text_checkbox, TRUE, TRUE, 2);
2264     gtk_box_pack_start (GTK_BOX (boxes), vis_checkbox, TRUE, TRUE, 2);
2265     gtk_box_pack_start (GTK_BOX (boxes), mute_checkbox, TRUE, TRUE, 2);
2266     gtk_box_pack_start (GTK_BOX (boxes), volume_spinbutton, TRUE, TRUE, 2);
2267     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (vis_checkbox), FALSE);
2268     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (audio_checkbox), TRUE);
2269     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (video_checkbox), TRUE);
2270     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (text_checkbox), TRUE);
2271     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (mute_checkbox), FALSE);
2272     g_signal_connect (G_OBJECT (vis_checkbox), "toggled",
2273         G_CALLBACK (vis_toggle_cb), pipeline);
2274     g_signal_connect (G_OBJECT (audio_checkbox), "toggled",
2275         G_CALLBACK (audio_toggle_cb), pipeline);
2276     g_signal_connect (G_OBJECT (video_checkbox), "toggled",
2277         G_CALLBACK (video_toggle_cb), pipeline);
2278     g_signal_connect (G_OBJECT (text_checkbox), "toggled",
2279         G_CALLBACK (text_toggle_cb), pipeline);
2280     g_signal_connect (G_OBJECT (mute_checkbox), "toggled",
2281         G_CALLBACK (mute_toggle_cb), pipeline);
2282     g_signal_connect (G_OBJECT (volume_spinbutton), "value_changed",
2283         G_CALLBACK (volume_spinbutton_changed_cb), pipeline);
2284     /* playbin2 panel for snapshot */
2285     boxes2 = gtk_hbox_new (FALSE, 0);
2286     shot_button = gtk_button_new_from_stock (GTK_STOCK_SAVE);
2287     gtk_tooltips_set_tip (tips, shot_button,
2288         "save a screenshot .png in the current directory", NULL);
2289     g_signal_connect (G_OBJECT (shot_button), "clicked", G_CALLBACK (shot_cb),
2290         pipeline);
2291     vis_combo = gtk_combo_box_new_text ();
2292     g_signal_connect (G_OBJECT (vis_combo), "changed",
2293         G_CALLBACK (vis_combo_cb), pipeline);
2294     gtk_widget_set_sensitive (vis_combo, FALSE);
2295     gtk_box_pack_start (GTK_BOX (boxes2), shot_button, TRUE, TRUE, 2);
2296     gtk_box_pack_start (GTK_BOX (boxes2), vis_combo, TRUE, TRUE, 2);
2297
2298     /* fill the vis combo box and the array of factories */
2299     init_visualization_features ();
2300   } else {
2301     panel = boxes = boxes2 = NULL;
2302   }
2303
2304   /* do the packing stuff ... */
2305   gtk_window_set_default_size (GTK_WINDOW (window), 250, 96);
2306   gtk_container_add (GTK_CONTAINER (window), vbox);
2307   gtk_container_add (GTK_CONTAINER (vbox), hbox);
2308   gtk_box_pack_start (GTK_BOX (hbox), play_button, FALSE, FALSE, 2);
2309   gtk_box_pack_start (GTK_BOX (hbox), pause_button, FALSE, FALSE, 2);
2310   gtk_box_pack_start (GTK_BOX (hbox), stop_button, FALSE, FALSE, 2);
2311   gtk_box_pack_start (GTK_BOX (hbox), flagtable, FALSE, FALSE, 2);
2312   gtk_table_attach_defaults (GTK_TABLE (flagtable), accurate_checkbox, 0, 1, 0,
2313       1);
2314   gtk_table_attach_defaults (GTK_TABLE (flagtable), flush_checkbox, 1, 2, 0, 1);
2315   gtk_table_attach_defaults (GTK_TABLE (flagtable), loop_checkbox, 2, 3, 0, 1);
2316   gtk_table_attach_defaults (GTK_TABLE (flagtable), key_checkbox, 0, 1, 1, 2);
2317   gtk_table_attach_defaults (GTK_TABLE (flagtable), scrub_checkbox, 1, 2, 1, 2);
2318   gtk_table_attach_defaults (GTK_TABLE (flagtable), play_scrub_checkbox, 2, 3,
2319       1, 2);
2320   gtk_table_attach_defaults (GTK_TABLE (flagtable), rate_label, 3, 4, 0, 1);
2321   gtk_table_attach_defaults (GTK_TABLE (flagtable), rate_spinbutton, 3, 4, 1,
2322       2);
2323   if (panel && boxes && boxes2) {
2324     gtk_box_pack_start (GTK_BOX (vbox), panel, TRUE, TRUE, 2);
2325     gtk_box_pack_start (GTK_BOX (vbox), boxes, TRUE, TRUE, 2);
2326     gtk_box_pack_start (GTK_BOX (vbox), boxes2, TRUE, TRUE, 2);
2327   }
2328   gtk_box_pack_start (GTK_BOX (vbox), hscale, TRUE, TRUE, 2);
2329   gtk_box_pack_start (GTK_BOX (vbox), statusbar, TRUE, TRUE, 2);
2330
2331   /* connect things ... */
2332   g_signal_connect (G_OBJECT (play_button), "clicked", G_CALLBACK (play_cb),
2333       pipeline);
2334   g_signal_connect (G_OBJECT (pause_button), "clicked", G_CALLBACK (pause_cb),
2335       pipeline);
2336   g_signal_connect (G_OBJECT (stop_button), "clicked", G_CALLBACK (stop_cb),
2337       pipeline);
2338   g_signal_connect (G_OBJECT (accurate_checkbox), "toggled",
2339       G_CALLBACK (accurate_toggle_cb), pipeline);
2340   g_signal_connect (G_OBJECT (key_checkbox), "toggled",
2341       G_CALLBACK (key_toggle_cb), pipeline);
2342   g_signal_connect (G_OBJECT (loop_checkbox), "toggled",
2343       G_CALLBACK (loop_toggle_cb), pipeline);
2344   g_signal_connect (G_OBJECT (flush_checkbox), "toggled",
2345       G_CALLBACK (flush_toggle_cb), pipeline);
2346   g_signal_connect (G_OBJECT (scrub_checkbox), "toggled",
2347       G_CALLBACK (scrub_toggle_cb), pipeline);
2348   g_signal_connect (G_OBJECT (play_scrub_checkbox), "toggled",
2349       G_CALLBACK (play_scrub_toggle_cb), pipeline);
2350   g_signal_connect (G_OBJECT (rate_spinbutton), "value_changed",
2351       G_CALLBACK (rate_spinbutton_changed_cb), pipeline);
2352
2353   g_signal_connect (G_OBJECT (window), "delete-event", gtk_main_quit, NULL);
2354
2355   /* show the gui. */
2356   gtk_widget_show_all (window);
2357
2358   if (verbose) {
2359     g_signal_connect (pipeline, "deep_notify",
2360         G_CALLBACK (gst_object_default_deep_notify), NULL);
2361   }
2362
2363   connect_bus_signals (pipeline);
2364   gtk_main ();
2365
2366   g_print ("NULL pipeline\n");
2367   gst_element_set_state (pipeline, GST_STATE_NULL);
2368
2369   g_print ("free pipeline\n");
2370   gst_object_unref (pipeline);
2371
2372   return 0;
2373 }