f96f8494d3843e37bb4d82506f4bf8ec2468ad60
[platform/upstream/gstreamer.git] / examples / seeking / seek.c
1 #include <stdlib.h>
2 #include <glib.h>
3 #include <gtk/gtk.h>
4 #include <gst/gst.h>
5 #include <string.h>
6
7 static GList *seekable_pads = NULL;
8 static GList *rate_pads = NULL;
9 static GList *seekable_elements = NULL;
10
11 static GstElement *pipeline;
12 static guint64 duration;
13 static GtkAdjustment *adjustment;
14 static gboolean stats = FALSE;
15
16 static guint update_id;
17
18 //#define SOURCE "gnomevfssrc"
19 #define SOURCE "filesrc"
20
21 #define UPDATE_INTERVAL 500
22
23 #define THREAD
24 #define PAD_SEEK
25
26 typedef struct
27 {
28   const gchar   *padname;
29   GstPad        *target;
30   GstElement    *bin;
31 } dyn_link;
32
33 static void
34 dynamic_link (GstPadTemplate *templ, GstPad *newpad, gpointer data)
35 {
36   dyn_link *connect = (dyn_link *) data;
37
38   if (!strcmp (gst_pad_get_name (newpad), connect->padname)) {
39     gst_element_set_state (pipeline, GST_STATE_PAUSED);
40     gst_bin_add (GST_BIN (pipeline), connect->bin);
41     gst_pad_link (newpad, connect->target);
42     gst_element_set_state (pipeline, GST_STATE_PLAYING);
43
44     seekable_pads = g_list_prepend (seekable_pads, newpad);
45     rate_pads = g_list_prepend (rate_pads, newpad);
46   }
47 }
48
49 static void
50 setup_dynamic_link (GstElement *element, const gchar *padname, GstPad *target, GstElement *bin)
51 {
52   dyn_link *connect;
53
54   connect = g_new0 (dyn_link, 1);
55   connect->padname      = g_strdup (padname);
56   connect->target       = target;
57   connect->bin          = bin;
58
59   g_signal_connect (G_OBJECT (element), "new_pad", G_CALLBACK (dynamic_link), connect);
60 }
61
62 static GstElement*
63 make_mod_pipeline (const gchar *location) 
64 {
65   GstElement *pipeline;
66   GstElement *src, *decoder, *audiosink;
67   GstPad *seekable;
68   
69   pipeline = gst_pipeline_new ("app");
70
71   src = gst_element_factory_make_or_warn (SOURCE, "src");
72   decoder = gst_element_factory_make_or_warn ("modplug", "decoder");
73   audiosink = gst_element_factory_make_or_warn ("osssink", "sink");
74   //g_object_set (G_OBJECT (audiosink), "sync", FALSE, NULL);
75
76   g_object_set (G_OBJECT (src), "location", location, NULL);
77
78   gst_bin_add (GST_BIN (pipeline), src);
79   gst_bin_add (GST_BIN (pipeline), decoder);
80   gst_bin_add (GST_BIN (pipeline), audiosink);
81
82   gst_element_link (src, decoder);
83   gst_element_link (decoder, audiosink);
84
85   seekable = gst_element_get_pad (decoder, "src");
86   seekable_pads = g_list_prepend (seekable_pads, seekable);
87   rate_pads = g_list_prepend (rate_pads, seekable);
88   rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (decoder, "sink"));
89
90   return pipeline;
91 }
92
93 static GstElement*
94 make_dv_pipeline (const gchar *location) 
95 {
96   GstElement *pipeline;
97   GstElement *src, *decoder, *audiosink, *videosink;
98   GstPad *seekable;
99   
100   pipeline = gst_pipeline_new ("app");
101
102   src = gst_element_factory_make_or_warn (SOURCE, "src");
103   decoder = gst_element_factory_make_or_warn ("dvdec", "decoder");
104   videosink = gst_element_factory_make_or_warn ("xvideosink", "v_sink");
105   audiosink = gst_element_factory_make_or_warn ("osssink", "a_sink");
106   //g_object_set (G_OBJECT (audiosink), "sync", FALSE, NULL);
107
108   g_object_set (G_OBJECT (src), "location", location, NULL);
109
110   gst_bin_add (GST_BIN (pipeline), src);
111   gst_bin_add (GST_BIN (pipeline), decoder);
112   gst_bin_add (GST_BIN (pipeline), audiosink);
113   gst_bin_add (GST_BIN (pipeline), videosink);
114
115   gst_element_link (src, decoder);
116   gst_element_link (decoder, audiosink);
117   gst_element_link (decoder, videosink);
118
119   seekable = gst_element_get_pad (decoder, "video");
120   seekable_pads = g_list_prepend (seekable_pads, seekable);
121   rate_pads = g_list_prepend (rate_pads, seekable);
122   seekable = gst_element_get_pad (decoder, "audio");
123   seekable_pads = g_list_prepend (seekable_pads, seekable);
124   rate_pads = g_list_prepend (rate_pads, seekable);
125   rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (decoder, "sink"));
126
127   return pipeline;
128 }
129
130 static GstElement*
131 make_wav_pipeline (const gchar *location) 
132 {
133   GstElement *pipeline;
134   GstElement *src, *decoder, *audiosink;
135   GstPad *seekable;
136   
137   pipeline = gst_pipeline_new ("app");
138
139   src = gst_element_factory_make_or_warn (SOURCE, "src");
140   decoder = gst_element_factory_make_or_warn ("wavparse", "decoder");
141   audiosink = gst_element_factory_make_or_warn ("osssink", "sink");
142   //g_object_set (G_OBJECT (audiosink), "sync", FALSE, NULL);
143
144   g_object_set (G_OBJECT (src), "location", location, NULL);
145
146   gst_bin_add (GST_BIN (pipeline), src);
147   gst_bin_add (GST_BIN (pipeline), decoder);
148   gst_bin_add (GST_BIN (pipeline), audiosink);
149
150   gst_element_link (src, decoder);
151   gst_element_link (decoder, audiosink);
152
153   seekable = gst_element_get_pad (decoder, "src");
154   seekable_pads = g_list_prepend (seekable_pads, seekable);
155   rate_pads = g_list_prepend (rate_pads, seekable);
156   rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (decoder, "sink"));
157
158   return pipeline;
159 }
160
161 static GstElement*
162 make_flac_pipeline (const gchar *location) 
163 {
164   GstElement *pipeline;
165   GstElement *src, *decoder, *audiosink;
166   GstPad *seekable;
167   
168   pipeline = gst_pipeline_new ("app");
169
170   src = gst_element_factory_make_or_warn (SOURCE, "src");
171   decoder = gst_element_factory_make_or_warn ("flacdec", "decoder");
172   audiosink = gst_element_factory_make_or_warn ("osssink", "sink");
173   //g_object_set (G_OBJECT (audiosink), "sync", FALSE, NULL);
174
175   g_object_set (G_OBJECT (src), "location", location, NULL);
176
177   gst_bin_add (GST_BIN (pipeline), src);
178   gst_bin_add (GST_BIN (pipeline), decoder);
179   gst_bin_add (GST_BIN (pipeline), audiosink);
180
181   gst_element_link (src, decoder);
182   gst_element_link (decoder, audiosink);
183
184   seekable = gst_element_get_pad (decoder, "src");
185   seekable_pads = g_list_prepend (seekable_pads, seekable);
186   rate_pads = g_list_prepend (rate_pads, seekable);
187   rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (decoder, "sink"));
188
189   return pipeline;
190 }
191
192 static GstElement*
193 make_sid_pipeline (const gchar *location) 
194 {
195   GstElement *pipeline;
196   GstElement *src, *decoder, *audiosink;
197   GstPad *seekable;
198   
199   pipeline = gst_pipeline_new ("app");
200
201   src = gst_element_factory_make_or_warn (SOURCE, "src");
202   decoder = gst_element_factory_make_or_warn ("siddec", "decoder");
203   audiosink = gst_element_factory_make_or_warn ("osssink", "sink");
204   //g_object_set (G_OBJECT (audiosink), "sync", FALSE, NULL);
205
206   g_object_set (G_OBJECT (src), "location", location, NULL);
207
208   gst_bin_add (GST_BIN (pipeline), src);
209   gst_bin_add (GST_BIN (pipeline), decoder);
210   gst_bin_add (GST_BIN (pipeline), audiosink);
211
212   gst_element_link (src, decoder);
213   gst_element_link (decoder, audiosink);
214
215   seekable = gst_element_get_pad (decoder, "src");
216   seekable_pads = g_list_prepend (seekable_pads, seekable);
217   rate_pads = g_list_prepend (rate_pads, seekable);
218   rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (decoder, "sink"));
219
220   return pipeline;
221 }
222
223 static GstElement*
224 make_parse_pipeline (const gchar *location) 
225 {
226   GstElement *pipeline;
227   GstElement *src, *parser, *fakesink;
228   GstPad *seekable;
229   
230   pipeline = gst_pipeline_new ("app");
231
232   src = gst_element_factory_make_or_warn (SOURCE, "src");
233   parser = gst_element_factory_make_or_warn ("mpegparse", "parse");
234   fakesink = gst_element_factory_make_or_warn ("fakesink", "sink");
235   g_object_set (G_OBJECT (fakesink), "silent", TRUE, NULL);
236   g_object_set (G_OBJECT (fakesink), "sync", TRUE, NULL);
237
238   g_object_set (G_OBJECT (src), "location", location, NULL);
239
240   gst_bin_add (GST_BIN (pipeline), src);
241   gst_bin_add (GST_BIN (pipeline), parser);
242   gst_bin_add (GST_BIN (pipeline), fakesink);
243
244   gst_element_link (src, parser);
245   gst_element_link (parser, fakesink);
246
247   seekable = gst_element_get_pad (parser, "src");
248   seekable_pads = g_list_prepend (seekable_pads, seekable);
249   rate_pads = g_list_prepend (rate_pads, seekable);
250   rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (parser, "sink"));
251
252   return pipeline;
253 }
254
255 static GstElement*
256 make_vorbis_pipeline (const gchar *location) 
257 {
258   GstElement *pipeline;
259   GstElement *src, *decoder, *audiosink;
260   GstPad *seekable;
261   
262   pipeline = gst_pipeline_new ("app");
263
264   src = gst_element_factory_make_or_warn (SOURCE, "src");
265   decoder = gst_element_factory_make_or_warn ("vorbisfile", "decoder");
266   audiosink = gst_element_factory_make_or_warn ("osssink", "sink");
267   g_object_set (G_OBJECT (audiosink), "sync", FALSE, NULL);
268
269   g_object_set (G_OBJECT (src), "location", location, NULL);
270
271   gst_bin_add (GST_BIN (pipeline), src);
272   gst_bin_add (GST_BIN (pipeline), decoder);
273   gst_bin_add (GST_BIN (pipeline), audiosink);
274
275   gst_element_link (src, decoder);
276   gst_element_link (decoder, audiosink);
277
278   seekable = gst_element_get_pad (decoder, "src");
279   seekable_pads = g_list_prepend (seekable_pads, seekable);
280   rate_pads = g_list_prepend (rate_pads, seekable);
281   rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (decoder, "sink"));
282
283   return pipeline;
284 }
285
286 static GstElement*
287 make_mp3_pipeline (const gchar *location) 
288 {
289   GstElement *pipeline;
290   GstElement *src, *decoder, *osssink, *queue, *audio_thread;
291   GstPad *seekable;
292   
293   pipeline = gst_pipeline_new ("app");
294
295   src = gst_element_factory_make_or_warn (SOURCE, "src");
296   decoder = gst_element_factory_make_or_warn ("mad", "dec");
297   queue = gst_element_factory_make_or_warn ("queue", "queue");
298   osssink = gst_element_factory_make_or_warn ("osssink", "sink");
299
300   audio_thread = gst_thread_new ("a_decoder_thread");
301
302   seekable_elements = g_list_prepend (seekable_elements, osssink);
303
304   g_object_set (G_OBJECT (src), "location", location, NULL);
305   g_object_set (G_OBJECT (osssink), "fragment", 0x00180008, NULL);
306
307   gst_bin_add (GST_BIN (pipeline), src);
308   gst_bin_add (GST_BIN (pipeline), decoder);
309   gst_bin_add (GST_BIN (audio_thread), queue);
310   gst_bin_add (GST_BIN (audio_thread), osssink);
311   gst_bin_add (GST_BIN (pipeline), audio_thread);
312
313   gst_element_link (src, decoder);
314   gst_element_link (decoder, queue);
315   gst_element_link (queue, osssink);
316
317   seekable = gst_element_get_pad (queue, "src");
318   seekable_pads = g_list_prepend (seekable_pads, seekable);
319   rate_pads = g_list_prepend (rate_pads, seekable);
320   rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (decoder, "sink"));
321
322   return pipeline;
323 }
324
325 static GstElement*
326 make_avi_pipeline (const gchar *location) 
327 {
328   GstElement *pipeline, *audio_bin, *video_bin;
329   GstElement *src, *demux, *a_decoder, *v_decoder, *audiosink, *videosink;
330   GstElement *a_queue = NULL, *audio_thread = NULL, *v_queue = NULL, *video_thread = NULL;
331   GstPad *seekable;
332   
333   pipeline = gst_pipeline_new ("app");
334
335   src = gst_element_factory_make_or_warn (SOURCE, "src");
336   g_object_set (G_OBJECT (src), "location", location, NULL);
337
338   demux = gst_element_factory_make_or_warn ("avidemux", "demux");
339   seekable_elements = g_list_prepend (seekable_elements, demux);
340
341   gst_bin_add (GST_BIN (pipeline), src);
342   gst_bin_add (GST_BIN (pipeline), demux);
343   gst_element_link (src, demux);
344
345   audio_bin = gst_bin_new ("a_decoder_bin");
346   a_decoder = gst_element_factory_make_or_warn ("mad", "a_dec");
347   audio_thread = gst_thread_new ("a_decoder_thread");
348   audiosink = gst_element_factory_make_or_warn ("osssink", "a_sink");
349   //g_object_set (G_OBJECT (audiosink), "fragment", 0x00180008, NULL);
350   a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
351   gst_element_link (a_decoder, a_queue);
352   gst_element_link (a_queue, audiosink);
353   gst_bin_add (GST_BIN (audio_bin), a_decoder);
354   gst_bin_add (GST_BIN (audio_bin), audio_thread);
355   gst_bin_add (GST_BIN (audio_thread), a_queue);
356   gst_bin_add (GST_BIN (audio_thread), audiosink);
357   gst_element_set_state (audio_bin, GST_STATE_PAUSED);
358
359   setup_dynamic_link (demux, "audio_00", gst_element_get_pad (a_decoder, "sink"), audio_bin);
360
361   seekable = gst_element_get_pad (a_queue, "src");
362   seekable_pads = g_list_prepend (seekable_pads, seekable);
363   rate_pads = g_list_prepend (rate_pads, seekable);
364   rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (a_decoder, "sink"));
365
366   video_bin = gst_bin_new ("v_decoder_bin");
367   //v_decoder = gst_element_factory_make_or_warn ("identity", "v_dec");
368   //v_decoder = gst_element_factory_make_or_warn ("windec", "v_dec");
369   v_decoder = gst_element_factory_make_or_warn ("ffdec_msmpeg4", "v_dec");
370   video_thread = gst_thread_new ("v_decoder_thread");
371   videosink = gst_element_factory_make_or_warn ("xvideosink", "v_sink");
372   //videosink = gst_element_factory_make_or_warn ("fakesink", "v_sink");
373   //g_object_set (G_OBJECT (videosink), "sync", TRUE, NULL);
374   v_queue = gst_element_factory_make_or_warn ("queue", "v_queue");
375   //g_object_set (G_OBJECT (v_queue), "max_level", 10, NULL);
376   gst_element_link (v_decoder, v_queue);
377   gst_element_link (v_queue, videosink);
378   gst_bin_add (GST_BIN (video_bin), v_decoder);
379   gst_bin_add (GST_BIN (video_bin), video_thread);
380   gst_bin_add (GST_BIN (video_thread), v_queue);
381   gst_bin_add (GST_BIN (video_thread), videosink);
382
383   gst_element_set_state (video_bin, GST_STATE_PAUSED);
384
385   setup_dynamic_link (demux, "video_00", gst_element_get_pad (v_decoder, "sink"), video_bin);
386
387   seekable = gst_element_get_pad (v_queue, "src");
388   seekable_pads = g_list_prepend (seekable_pads, seekable);
389   rate_pads = g_list_prepend (rate_pads, seekable);
390   rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (v_decoder, "sink"));
391
392   return pipeline;
393 }
394
395 static GstElement*
396 make_mpeg_pipeline (const gchar *location) 
397 {
398   GstElement *pipeline, *audio_bin, *video_bin;
399   GstElement *src, *demux, *a_decoder, *v_decoder, *v_filter;
400   GstElement *audiosink, *videosink;
401   GstElement *a_queue, *audio_thread, *v_queue, *video_thread;
402   GstPad *seekable;
403   
404   pipeline = gst_pipeline_new ("app");
405
406   src = gst_element_factory_make_or_warn (SOURCE, "src");
407   g_object_set (G_OBJECT (src), "location", location, NULL);
408
409   demux = gst_element_factory_make_or_warn ("mpegdemux", "demux");
410   g_object_set (G_OBJECT (demux), "sync", TRUE, NULL);
411
412   seekable_elements = g_list_prepend (seekable_elements, demux);
413
414   gst_bin_add (GST_BIN (pipeline), src);
415   gst_bin_add (GST_BIN (pipeline), demux);
416   gst_element_link (src, demux);
417
418   audio_bin = gst_bin_new ("a_decoder_bin");
419   a_decoder = gst_element_factory_make_or_warn ("mad", "a_dec");
420   audio_thread = gst_thread_new ("a_decoder_thread");
421   a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
422   audiosink = gst_element_factory_make_or_warn ("osssink", "a_sink");
423   g_object_set (G_OBJECT (audiosink), "fragment", 0x00180008, NULL);
424   gst_element_link (a_decoder, a_queue);
425   gst_element_link (a_queue, audiosink);
426   gst_bin_add (GST_BIN (audio_bin), a_decoder);
427   gst_bin_add (GST_BIN (audio_bin), audio_thread);
428   gst_bin_add (GST_BIN (audio_thread), a_queue);
429   gst_bin_add (GST_BIN (audio_thread), audiosink);
430
431   setup_dynamic_link (demux, "audio_00", gst_element_get_pad (a_decoder, "sink"), audio_bin);
432
433   seekable = gst_element_get_pad (a_queue, "src");
434   seekable_pads = g_list_prepend (seekable_pads, seekable);
435   rate_pads = g_list_prepend (rate_pads, seekable);
436   rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (a_decoder, "sink"));
437
438   video_bin = gst_bin_new ("v_decoder_bin");
439   v_decoder = gst_element_factory_make_or_warn ("mpeg2dec", "v_dec");
440   video_thread = gst_thread_new ("v_decoder_thread");
441   //g_object_set (G_OBJECT (video_thread), "priority", 2, NULL);
442   v_queue = gst_element_factory_make_or_warn ("queue", "v_queue");
443   v_filter = gst_element_factory_make_or_warn ("colorspace", "v_filter");
444   videosink = gst_element_factory_make_or_warn ("xvideosink", "v_sink");
445   gst_element_link_many (v_decoder, v_queue, v_filter, NULL);
446   
447   gst_element_link (v_filter, videosink);
448   gst_bin_add_many (GST_BIN (video_bin), v_decoder, video_thread, NULL);
449   gst_bin_add_many (GST_BIN (video_thread), v_queue, v_filter, videosink, NULL);
450
451   setup_dynamic_link (demux, "video_00", gst_element_get_pad (v_decoder, "sink"), video_bin);
452
453   seekable = gst_element_get_pad (v_queue, "src");
454   seekable_pads = g_list_prepend (seekable_pads, seekable);
455   rate_pads = g_list_prepend (rate_pads, seekable);
456   rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (v_decoder, "sink"));
457
458   return pipeline;
459 }
460
461 static GstElement*
462 make_mpegnt_pipeline (const gchar *location) 
463 {
464   GstElement *pipeline, *audio_bin, *video_bin;
465   GstElement *src, *demux, *a_decoder, *v_decoder, *v_filter;
466   GstElement *audiosink, *videosink;
467   GstElement *a_queue, *audio_thread;
468   GstPad *seekable;
469   
470   pipeline = gst_pipeline_new ("app");
471
472   src = gst_element_factory_make_or_warn (SOURCE, "src");
473   g_object_set (G_OBJECT (src), "location", location, NULL);
474
475   demux = gst_element_factory_make_or_warn ("mpegdemux", "demux");
476   //g_object_set (G_OBJECT (demux), "sync", TRUE, NULL);
477
478   seekable_elements = g_list_prepend (seekable_elements, 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_decoder = gst_element_factory_make_or_warn ("mad", "a_dec");
486   audio_thread = gst_thread_new ("a_decoder_thread");
487   a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
488   audiosink = gst_element_factory_make_or_warn ("osssink", "a_sink");
489   //g_object_set (G_OBJECT (audiosink), "fragment", 0x00180008, NULL);
490   g_object_set (G_OBJECT (audiosink), "sync", FALSE, NULL);
491   gst_element_link (a_decoder, a_queue);
492   gst_element_link (a_queue, audiosink);
493   gst_bin_add (GST_BIN (audio_bin), a_decoder);
494   gst_bin_add (GST_BIN (audio_bin), audio_thread);
495   gst_bin_add (GST_BIN (audio_thread), a_queue);
496   gst_bin_add (GST_BIN (audio_thread), audiosink);
497
498   setup_dynamic_link (demux, "audio_00", gst_element_get_pad (a_decoder, "sink"), audio_bin);
499
500   seekable = gst_element_get_pad (a_queue, "src");
501   seekable_pads = g_list_prepend (seekable_pads, seekable);
502   rate_pads = g_list_prepend (rate_pads, seekable);
503   rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (a_decoder, "sink"));
504
505   video_bin = gst_bin_new ("v_decoder_bin");
506   v_decoder = gst_element_factory_make_or_warn ("mpeg2dec", "v_dec");
507   v_filter = gst_element_factory_make_or_warn ("colorspace", "v_filter");
508   videosink = gst_element_factory_make_or_warn ("xvideosink", "v_sink");
509   gst_element_link_many (v_decoder, v_filter, videosink, NULL);
510   
511   gst_bin_add_many (GST_BIN (video_bin), v_decoder, v_filter, videosink, NULL);
512
513   setup_dynamic_link (demux, "video_00", gst_element_get_pad (v_decoder, "sink"), video_bin);
514
515   seekable = gst_element_get_pad (v_decoder, "src");
516   seekable_pads = g_list_prepend (seekable_pads, seekable);
517   rate_pads = g_list_prepend (rate_pads, seekable);
518   rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (v_decoder, "sink"));
519
520   return pipeline;
521 }
522
523 static gchar*
524 format_value (GtkScale *scale,
525               gdouble   value)
526 {
527   gint64 real;
528   gint64 seconds;
529   gint64 subseconds;
530
531   real = value * duration / 100;
532   seconds = (gint64) real / GST_SECOND;
533   subseconds = (gint64) real / (GST_SECOND / 100);
534
535   return g_strdup_printf ("%02lld:%02lld:%02lld",
536                           seconds/60, 
537                           seconds%60, 
538                           subseconds%100);
539 }
540
541 typedef struct
542 {
543   const gchar *name;
544   const GstFormat format;
545 } seek_format;
546
547 static seek_format seek_formats[] = 
548 {
549   { "tim",  GST_FORMAT_TIME    },
550   { "byt",  GST_FORMAT_BYTES   },
551   { "unt",  GST_FORMAT_UNITS   },
552   { "buf",  GST_FORMAT_BUFFERS },
553   { "def",  GST_FORMAT_DEFAULT },
554   { NULL, 0 }, 
555 };
556
557 G_GNUC_UNUSED static void
558 query_rates (void)
559 {
560   GList *walk = rate_pads;
561
562   while (walk) {
563     GstPad *pad = GST_PAD (walk->data);
564     gint i = 0;
565
566     g_print ("rate/sec  %8.8s: ", GST_PAD_NAME (pad));
567     while (seek_formats[i].name) {
568       gint64 value;
569       GstFormat format;
570
571       format = seek_formats[i].format;
572
573       if (gst_pad_convert (pad, GST_FORMAT_TIME, GST_SECOND, 
574                            &format, &value)) 
575       {
576         g_print ("%s %13lld | ", seek_formats[i].name, value);
577       }
578       else {
579         g_print ("%s %13.13s | ", seek_formats[i].name, "*NA*");
580       }
581
582       i++;
583     }
584     g_print (" %s:%s\n", GST_DEBUG_PAD_NAME (pad));
585
586     walk = g_list_next (walk);
587   }
588 }
589
590 G_GNUC_UNUSED static void
591 query_durations ()
592 {
593   GList *walk = seekable_pads;
594
595   while (walk) {
596     GstPad *pad = GST_PAD (walk->data);
597     gint i = 0;
598
599     g_print ("durations %8.8s: ", GST_PAD_NAME (pad));
600     while (seek_formats[i].name) {
601       gboolean res;
602       gint64 value;
603       GstFormat format;
604
605       format = seek_formats[i].format;
606       res = gst_pad_query (pad, GST_QUERY_TOTAL, &format, &value);
607       if (res) {
608         g_print ("%s %13lld | ", seek_formats[i].name, value);
609       }
610       else {
611         g_print ("%s %13.13s | ", seek_formats[i].name, "*NA*");
612       }
613       i++;
614     }
615     g_print (" %s:%s\n", GST_DEBUG_PAD_NAME (pad));
616   
617     walk = g_list_next (walk);
618   }
619 }
620
621 G_GNUC_UNUSED static void
622 query_positions ()
623 {
624   GList *walk = seekable_pads;
625
626   while (walk) {
627     GstPad *pad = GST_PAD (walk->data);
628     gint i = 0;
629
630     g_print ("positions %8.8s: ", GST_PAD_NAME (pad));
631     while (seek_formats[i].name) {
632       gboolean res;
633       gint64 value;
634       GstFormat format;
635
636       format = seek_formats[i].format;
637       res = gst_pad_query (pad, GST_QUERY_POSITION, &format, &value);
638       if (res) {
639         g_print ("%s %13lld | ", seek_formats[i].name, value);
640       }
641       else {
642         g_print ("%s %13.13s | ", seek_formats[i].name, "*NA*");
643       }
644       i++;
645     }
646     g_print (" %s:%s\n", GST_DEBUG_PAD_NAME (pad));
647
648     walk = g_list_next (walk);
649   }
650 }
651
652 static gboolean
653 update_scale (gpointer data) 
654 {
655   GstClock *clock;
656   guint64 position;
657   GstFormat format = GST_FORMAT_TIME;
658
659   duration = 0;
660   clock = gst_bin_get_clock (GST_BIN (pipeline));
661
662   if (seekable_pads) {
663     GstPad *pad = GST_PAD (seekable_pads->data);
664     gst_pad_query (pad, GST_QUERY_TOTAL, &format, &duration);
665   }
666   position = gst_clock_get_time (clock);
667
668   if (stats) {
669     g_print ("clock:                  %13llu  (%s)\n", position, gst_object_get_name (GST_OBJECT (clock)));
670     query_durations ();
671     query_positions ();
672     query_rates ();
673   }
674
675   if (duration > 0) {
676     gtk_adjustment_set_value (adjustment, position * 100.0 / duration);
677   }
678
679   return TRUE;
680 }
681
682 static gboolean
683 iterate (gpointer data)
684 {
685   gboolean res;
686
687   res = gst_bin_iterate (GST_BIN (data));
688   if (!res) {
689     gtk_timeout_remove (update_id);
690     g_print ("stopping iterations\n");
691   }
692   return res;
693 }
694
695 static gboolean
696 start_seek (GtkWidget *widget, GdkEventButton *event, gpointer user_data)
697 {
698   gst_element_set_state (pipeline, GST_STATE_PAUSED);
699   gtk_timeout_remove (update_id);
700
701   return FALSE;
702 }
703
704 static gboolean
705 stop_seek (GtkWidget *widget, GdkEventButton *event, gpointer user_data)
706 {
707   gint64 real = gtk_range_get_value (GTK_RANGE (widget)) * duration / 100;
708   gboolean res;
709   GstEvent *s_event;
710 #ifdef PAD_SEEK
711   GList *walk = seekable_pads;
712
713   while (walk) {
714     GstPad *seekable = GST_PAD (walk->data);
715
716     g_print ("seek to %lld on pad %s:%s\n", real, GST_DEBUG_PAD_NAME (seekable));
717     s_event = gst_event_new_seek (GST_FORMAT_TIME |
718                                   GST_SEEK_METHOD_SET |
719                                   GST_SEEK_FLAG_FLUSH, real);
720
721     res = gst_pad_send_event (seekable, s_event);
722
723     walk = g_list_next (walk);
724   }
725 #else
726   GList *walk = seekable_elements;
727
728   while (walk) {
729     GstElement *seekable = GST_ELEMENT (walk->data);
730
731     g_print ("seek to %lld on element %s\n", real, gst_element_get_name (seekable));
732     s_event = gst_event_new_seek (GST_FORMAT_TIME |
733                                   GST_SEEK_METHOD_SET |
734                                   GST_SEEK_FLAG_FLUSH, real);
735
736     res = gst_element_send_event (seekable, s_event);
737
738     walk = g_list_next (walk);
739   }
740 #endif
741
742   gst_element_set_state (pipeline, GST_STATE_PLAYING);
743   gtk_idle_add ((GtkFunction) iterate, pipeline);
744   update_id = gtk_timeout_add (UPDATE_INTERVAL, (GtkFunction) update_scale, pipeline);
745
746   return FALSE;
747 }
748
749 static void
750 play_cb (GtkButton * button, gpointer data)
751 {
752   if (gst_element_get_state (pipeline) != GST_STATE_PLAYING) {
753     gst_element_set_state (pipeline, GST_STATE_PLAYING);
754     gtk_idle_add ((GtkFunction) iterate, pipeline);
755     update_id = gtk_timeout_add (UPDATE_INTERVAL, (GtkFunction) update_scale, pipeline);
756   }
757 }
758
759 static void
760 pause_cb (GtkButton * button, gpointer data)
761 {
762   if (gst_element_get_state (pipeline) != GST_STATE_PAUSED) {
763     gst_element_set_state (pipeline, GST_STATE_PAUSED);
764     gtk_timeout_remove (update_id);
765   }
766 }
767
768 static void
769 stop_cb (GtkButton * button, gpointer data)
770 {
771   if (gst_element_get_state (pipeline) != GST_STATE_READY) {
772     gst_element_set_state (pipeline, GST_STATE_READY);
773     gtk_timeout_remove (update_id);
774   }
775 }
776
777 int
778 main (int argc, char **argv)
779 {
780   GtkWidget *window, *hbox, *vbox, 
781             *play_button, *pause_button, *stop_button, 
782             *hscale;
783   struct poptOption options[] = {
784     {"stats",  's',  POPT_ARG_NONE|POPT_ARGFLAG_STRIP,   &stats,   0,
785            "Show pad stats", NULL},
786     POPT_TABLEEND
787   };
788                 
789   gst_init_with_popt_table (&argc, &argv, options);
790   gtk_init (&argc, &argv);
791
792   if (argc != 3) {
793     g_print ("usage: %s <type 0=mp3 1=avi 2=mpeg1 3=mpegparse 4=vorbis 5=sid 6=flac 7=wav 8=mod 9=dv 10=mpeg1nothreads> <filename>\n", argv[0]);
794     exit (-1);
795   }
796
797   if (atoi (argv[1]) == 0) 
798     pipeline = make_mp3_pipeline (argv[2]);
799   else if (atoi (argv[1]) == 1) 
800     pipeline = make_avi_pipeline (argv[2]);
801   else if (atoi (argv[1]) == 2) 
802     pipeline = make_mpeg_pipeline (argv[2]);
803   else if (atoi (argv[1]) == 3)
804     pipeline = make_parse_pipeline (argv[2]);
805   else if (atoi (argv[1]) == 4) 
806     pipeline = make_vorbis_pipeline (argv[2]);
807   else if (atoi (argv[1]) == 5) 
808     pipeline = make_sid_pipeline (argv[2]);
809   else if (atoi (argv[1]) == 6) 
810     pipeline = make_flac_pipeline (argv[2]);
811   else if (atoi (argv[1]) == 7) 
812     pipeline = make_wav_pipeline (argv[2]);
813   else if (atoi (argv[1]) == 8) 
814     pipeline = make_mod_pipeline (argv[2]);
815   else if (atoi (argv[1]) == 9) 
816     pipeline = make_dv_pipeline (argv[2]);
817   else if (atoi (argv[1]) == 10) 
818     pipeline = make_mpegnt_pipeline (argv[2]);
819
820   /* initialize gui elements ... */
821   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
822   hbox = gtk_hbox_new (FALSE, 0);
823   vbox = gtk_vbox_new (FALSE, 0);
824   play_button = gtk_button_new_with_label ("play");
825   pause_button = gtk_button_new_with_label ("pause");
826   stop_button = gtk_button_new_with_label ("stop");
827
828   adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.00, 100.0, 0.1, 1.0, 1.0));
829   hscale = gtk_hscale_new (adjustment);
830   gtk_scale_set_digits (GTK_SCALE (hscale), 2);
831   gtk_range_set_update_policy (GTK_RANGE (hscale), GTK_UPDATE_CONTINUOUS);
832
833   gtk_signal_connect(GTK_OBJECT(hscale),
834                              "button_press_event", G_CALLBACK (start_seek), pipeline);
835   gtk_signal_connect(GTK_OBJECT(hscale),
836                              "button_release_event", G_CALLBACK (stop_seek), pipeline);
837   gtk_signal_connect(GTK_OBJECT(hscale),
838                              "format_value", G_CALLBACK (format_value), pipeline);
839
840   /* do the packing stuff ... */
841   gtk_window_set_default_size (GTK_WINDOW (window), 96, 96);
842   gtk_container_add (GTK_CONTAINER (window), vbox);
843   gtk_container_add (GTK_CONTAINER (vbox), hbox);
844   gtk_box_pack_start (GTK_BOX (hbox), play_button, FALSE, FALSE, 2);
845   gtk_box_pack_start (GTK_BOX (hbox), pause_button, FALSE, FALSE, 2);
846   gtk_box_pack_start (GTK_BOX (hbox), stop_button, FALSE, FALSE, 2);
847   gtk_box_pack_start (GTK_BOX (vbox), hscale, TRUE, TRUE, 2);
848
849   /* connect things ... */
850   g_signal_connect (G_OBJECT (play_button), "clicked", G_CALLBACK (play_cb), pipeline);
851   g_signal_connect (G_OBJECT (pause_button), "clicked", G_CALLBACK (pause_cb), pipeline);
852   g_signal_connect (G_OBJECT (stop_button), "clicked", G_CALLBACK (stop_cb), pipeline);
853   g_signal_connect (G_OBJECT (window), "delete_event", gtk_main_quit, NULL);
854
855   /* show the gui. */
856   gtk_widget_show_all (window);
857
858   g_signal_connect (pipeline, "deep_notify", G_CALLBACK (gst_element_default_deep_notify), NULL);
859   g_signal_connect (pipeline, "error", G_CALLBACK (gst_element_default_error), NULL);
860
861   gtk_main ();
862
863   gst_element_set_state (pipeline, GST_STATE_NULL);
864
865   //gst_object_unref (GST_OBJECT (pipeline));
866   gst_buffer_print_stats();
867   gst_event_print_stats();
868
869   //g_mem_chunk_info();
870
871   return 0;
872 }