seek.c 37.5 KB
Newer Older
1
#include <stdlib.h>
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
2
3
4
5
6
#include <glib.h>
#include <gtk/gtk.h>
#include <gst/gst.h>
#include <string.h>

7
static GList *seekable_pads = NULL;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
8
static GList *rate_pads = NULL;
9
static GList *seekable_elements = NULL;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
10
11

static GstElement *pipeline;
Wim Taymans's avatar
Wim Taymans committed
12
13
static gint64 position;
static gint64 duration;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
14
static GtkAdjustment *adjustment;
15
static GtkWidget *hscale;
16
static gboolean stats = FALSE;
17
18
static gboolean elem_seek = FALSE;
static gboolean verbose = FALSE;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
19
20

static guint update_id;
21
22
static guint seek_timeout_id = 0;
static gulong changed_id;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
23

Wim Taymans's avatar
Wim Taymans committed
24
//#define SOURCE "filesrc"
25
#define SOURCE "gnomevfssrc"
26
27
#define ASINK "alsasink"
//#define ASINK "osssink"
Wim Taymans's avatar
Wim Taymans committed
28
29
30
#define VSINK "xvimagesink"
//#define VSINK "ximagesink"
//#define VSINK "aasink"
31
//#define VSINK "cacasink"
32

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
33
34
#define UPDATE_INTERVAL 500

35
/* number of milliseconds to play for after a seek */
Wim Taymans's avatar
Wim Taymans committed
36
37
//#define SCRUB_TIME 250
//#define SCRUB
38

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
39
#define THREAD
40
#define PAD_SEEK
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
41
42
43

typedef struct
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
44
45
46
  const gchar *padname;
  GstPad *target;
  GstElement *bin;
47
48
}
dyn_link;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
49

50
static GstElement *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
51
gst_element_factory_make_or_warn (gchar * type, gchar * name)
52
53
54
55
{
  GstElement *element = gst_element_factory_make (type, name);

  if (!element) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
56
    g_warning ("Failed to create element %s of type %s", name, type);
57
58
59
60
61
  }

  return element;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
62
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
63
dynamic_link (GstPadTemplate * templ, GstPad * newpad, gpointer data)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
64
{
65
  dyn_link *connect = (dyn_link *) data;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
66

67
68
69
70
  if (connect->padname == NULL ||
      !strcmp (gst_pad_get_name (newpad), connect->padname)) {
    if (connect->bin)
      gst_bin_add (GST_BIN (pipeline), connect->bin);
71
    gst_pad_link (newpad, connect->target);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
72

73
    //seekable_pads = g_list_prepend (seekable_pads, newpad);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
74
75
76
77
78
    rate_pads = g_list_prepend (rate_pads, newpad);
  }
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
79
80
setup_dynamic_link (GstElement * element, const gchar * padname,
    GstPad * target, GstElement * bin)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
81
{
82
  dyn_link *connect;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
83

84
  connect = g_new0 (dyn_link, 1);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
85
86
87
  connect->padname = g_strdup (padname);
  connect->target = target;
  connect->bin = bin;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
88

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
89
90
  g_signal_connect (G_OBJECT (element), "new_pad", G_CALLBACK (dynamic_link),
      connect);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
91
92
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
93
94
static GstElement *
make_mod_pipeline (const gchar * location)
95
96
97
98
{
  GstElement *pipeline;
  GstElement *src, *decoder, *audiosink;
  GstPad *seekable;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
99

100
101
  pipeline = gst_pipeline_new ("app");

102
103
  src = gst_element_factory_make_or_warn (SOURCE, "src");
  decoder = gst_element_factory_make_or_warn ("modplug", "decoder");
104
  audiosink = gst_element_factory_make_or_warn (ASINK, "sink");
105
106
107
108
109
110
111
112
  //g_object_set (G_OBJECT (audiosink), "sync", FALSE, NULL);

  g_object_set (G_OBJECT (src), "location", location, NULL);

  gst_bin_add (GST_BIN (pipeline), src);
  gst_bin_add (GST_BIN (pipeline), decoder);
  gst_bin_add (GST_BIN (pipeline), audiosink);

113
114
  gst_element_link (src, decoder);
  gst_element_link (decoder, audiosink);
115
116
117
118
119
120
121
122
123

  seekable = gst_element_get_pad (decoder, "src");
  seekable_pads = g_list_prepend (seekable_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (decoder, "sink"));

  return pipeline;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
124
125
static GstElement *
make_dv_pipeline (const gchar * location)
Wim Taymans's avatar
Wim Taymans committed
126
127
128
{
  GstElement *pipeline;
  GstElement *src, *decoder, *audiosink, *videosink;
129
  GstElement *a_queue, *v_queue;
Wim Taymans's avatar
Wim Taymans committed
130
  GstPad *seekable;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
131

Wim Taymans's avatar
Wim Taymans committed
132
133
134
135
  pipeline = gst_pipeline_new ("app");

  src = gst_element_factory_make_or_warn (SOURCE, "src");
  decoder = gst_element_factory_make_or_warn ("dvdec", "decoder");
136
137
138
  v_queue = gst_element_factory_make_or_warn ("queue", "v_queue");
  videosink = gst_element_factory_make_or_warn (VSINK, "v_sink");
  a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
139
  audiosink = gst_element_factory_make_or_warn (ASINK, "a_sink");
Wim Taymans's avatar
Wim Taymans committed
140
141
142
143
144

  g_object_set (G_OBJECT (src), "location", location, NULL);

  gst_bin_add (GST_BIN (pipeline), src);
  gst_bin_add (GST_BIN (pipeline), decoder);
145
  gst_bin_add (GST_BIN (pipeline), a_queue);
Wim Taymans's avatar
Wim Taymans committed
146
  gst_bin_add (GST_BIN (pipeline), audiosink);
147
  gst_bin_add (GST_BIN (pipeline), v_queue);
Wim Taymans's avatar
Wim Taymans committed
148
149
  gst_bin_add (GST_BIN (pipeline), videosink);

150
  gst_element_link (src, decoder);
151
152
153
154
  gst_element_link (decoder, a_queue);
  gst_element_link (a_queue, audiosink);
  gst_element_link (decoder, v_queue);
  gst_element_link (v_queue, videosink);
Wim Taymans's avatar
Wim Taymans committed
155
156
157
158

  seekable = gst_element_get_pad (decoder, "video");
  seekable_pads = g_list_prepend (seekable_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, seekable);
159

Wim Taymans's avatar
Wim Taymans committed
160
161
162
163
164
165
166
167
  seekable = gst_element_get_pad (decoder, "audio");
  seekable_pads = g_list_prepend (seekable_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (decoder, "sink"));

  return pipeline;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
168
169
static GstElement *
make_wav_pipeline (const gchar * location)
Wim Taymans's avatar
Wim Taymans committed
170
171
172
173
{
  GstElement *pipeline;
  GstElement *src, *decoder, *audiosink;
  GstPad *seekable;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
174

Wim Taymans's avatar
Wim Taymans committed
175
176
  pipeline = gst_pipeline_new ("app");

177
178
  src = gst_element_factory_make_or_warn (SOURCE, "src");
  decoder = gst_element_factory_make_or_warn ("wavparse", "decoder");
179
  audiosink = gst_element_factory_make_or_warn (ASINK, "sink");
Wim Taymans's avatar
Wim Taymans committed
180
181
182
183
184
185
186
187
  //g_object_set (G_OBJECT (audiosink), "sync", FALSE, NULL);

  g_object_set (G_OBJECT (src), "location", location, NULL);

  gst_bin_add (GST_BIN (pipeline), src);
  gst_bin_add (GST_BIN (pipeline), decoder);
  gst_bin_add (GST_BIN (pipeline), audiosink);

188
189
  gst_element_link (src, decoder);
  gst_element_link (decoder, audiosink);
Wim Taymans's avatar
Wim Taymans committed
190
191
192
193
194
195
196
197
198

  seekable = gst_element_get_pad (decoder, "src");
  seekable_pads = g_list_prepend (seekable_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (decoder, "sink"));

  return pipeline;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
199
200
static GstElement *
make_flac_pipeline (const gchar * location)
201
202
203
204
{
  GstElement *pipeline;
  GstElement *src, *decoder, *audiosink;
  GstPad *seekable;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
205

206
207
  pipeline = gst_pipeline_new ("app");

208
209
  src = gst_element_factory_make_or_warn (SOURCE, "src");
  decoder = gst_element_factory_make_or_warn ("flacdec", "decoder");
210
  audiosink = gst_element_factory_make_or_warn (ASINK, "sink");
Wim Taymans's avatar
Wim Taymans committed
211
  g_object_set (G_OBJECT (audiosink), "sync", FALSE, NULL);
212
213
214
215
216
217
218

  g_object_set (G_OBJECT (src), "location", location, NULL);

  gst_bin_add (GST_BIN (pipeline), src);
  gst_bin_add (GST_BIN (pipeline), decoder);
  gst_bin_add (GST_BIN (pipeline), audiosink);

219
220
  gst_element_link (src, decoder);
  gst_element_link (decoder, audiosink);
221
222
223
224
225
226
227
228
229

  seekable = gst_element_get_pad (decoder, "src");
  seekable_pads = g_list_prepend (seekable_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (decoder, "sink"));

  return pipeline;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
230
231
static GstElement *
make_sid_pipeline (const gchar * location)
232
233
234
235
{
  GstElement *pipeline;
  GstElement *src, *decoder, *audiosink;
  GstPad *seekable;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
236

237
238
  pipeline = gst_pipeline_new ("app");

239
240
  src = gst_element_factory_make_or_warn (SOURCE, "src");
  decoder = gst_element_factory_make_or_warn ("siddec", "decoder");
241
  audiosink = gst_element_factory_make_or_warn (ASINK, "sink");
242
243
244
245
246
247
248
249
  //g_object_set (G_OBJECT (audiosink), "sync", FALSE, NULL);

  g_object_set (G_OBJECT (src), "location", location, NULL);

  gst_bin_add (GST_BIN (pipeline), src);
  gst_bin_add (GST_BIN (pipeline), decoder);
  gst_bin_add (GST_BIN (pipeline), audiosink);

250
251
  gst_element_link (src, decoder);
  gst_element_link (decoder, audiosink);
252
253
254
255
256
257
258
259
260

  seekable = gst_element_get_pad (decoder, "src");
  seekable_pads = g_list_prepend (seekable_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (decoder, "sink"));

  return pipeline;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
261
262
static GstElement *
make_parse_pipeline (const gchar * location)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
263
264
265
266
{
  GstElement *pipeline;
  GstElement *src, *parser, *fakesink;
  GstPad *seekable;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
267

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
268
269
  pipeline = gst_pipeline_new ("app");

270
271
272
  src = gst_element_factory_make_or_warn (SOURCE, "src");
  parser = gst_element_factory_make_or_warn ("mpegparse", "parse");
  fakesink = gst_element_factory_make_or_warn ("fakesink", "sink");
Wim Taymans's avatar
Wim Taymans committed
273
274
  g_object_set (G_OBJECT (fakesink), "silent", TRUE, NULL);
  g_object_set (G_OBJECT (fakesink), "sync", TRUE, NULL);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
275
276
277
278
279
280
281

  g_object_set (G_OBJECT (src), "location", location, NULL);

  gst_bin_add (GST_BIN (pipeline), src);
  gst_bin_add (GST_BIN (pipeline), parser);
  gst_bin_add (GST_BIN (pipeline), fakesink);

282
283
  gst_element_link (src, parser);
  gst_element_link (parser, fakesink);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
284
285

  seekable = gst_element_get_pad (parser, "src");
286
  seekable_pads = g_list_prepend (seekable_pads, seekable);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
287
288
289
290
291
292
  rate_pads = g_list_prepend (rate_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (parser, "sink"));

  return pipeline;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
293
294
static GstElement *
make_vorbis_pipeline (const gchar * location)
295
{
296
297
  GstElement *pipeline, *audio_bin;
  GstElement *src, *demux, *decoder, *convert, *audiosink;
298
  GstPad *seekable;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
299

300
301
  pipeline = gst_pipeline_new ("app");

302
  src = gst_element_factory_make_or_warn (SOURCE, "src");
303
304
305
  demux = gst_element_factory_make_or_warn ("oggdemux", "demux");
  decoder = gst_element_factory_make_or_warn ("vorbisdec", "decoder");
  convert = gst_element_factory_make_or_warn ("audioconvert", "convert");
306
  audiosink = gst_element_factory_make_or_warn (ASINK, "sink");
Wim Taymans's avatar
Wim Taymans committed
307
  g_object_set (G_OBJECT (audiosink), "sync", TRUE, NULL);
308
309
310

  g_object_set (G_OBJECT (src), "location", location, NULL);

311
312
  audio_bin = gst_bin_new ("a_decoder_bin");

313
  gst_bin_add (GST_BIN (pipeline), src);
314
315
316
317
318
  gst_bin_add (GST_BIN (pipeline), demux);
  gst_bin_add (GST_BIN (audio_bin), decoder);
  gst_bin_add (GST_BIN (audio_bin), convert);
  gst_bin_add (GST_BIN (audio_bin), audiosink);
  gst_bin_add (GST_BIN (pipeline), audio_bin);
319

320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
  gst_element_link (src, demux);
  gst_element_link (decoder, convert);
  gst_element_link (convert, audiosink);

  setup_dynamic_link (demux, NULL, gst_element_get_pad (decoder, "sink"), NULL);

  seekable = gst_element_get_pad (decoder, "src");
  seekable_pads = g_list_prepend (seekable_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (decoder, "sink"));

  return pipeline;
}

static GstElement *
make_theora_pipeline (const gchar * location)
{
  GstElement *pipeline, *video_bin;
  GstElement *src, *demux, *decoder, *convert, *videosink;
  GstElement *queue;
  GstPad *seekable;

  pipeline = gst_pipeline_new ("app");

  src = gst_element_factory_make_or_warn (SOURCE, "src");
  demux = gst_element_factory_make_or_warn ("oggdemux", "demux");
  queue = gst_element_factory_make_or_warn ("queue", "queue");
  decoder = gst_element_factory_make_or_warn ("theoradec", "decoder");
  convert = gst_element_factory_make_or_warn ("ffmpegcolorspace", "convert");
Wim Taymans's avatar
Wim Taymans committed
349
  videosink = gst_element_factory_make_or_warn (VSINK, "sink");
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368

  g_object_set (G_OBJECT (src), "location", location, NULL);

  video_bin = gst_bin_new ("v_decoder_bin");

  gst_bin_add (GST_BIN (pipeline), src);
  gst_bin_add (GST_BIN (pipeline), demux);
  gst_bin_add (GST_BIN (pipeline), queue);
  gst_bin_add (GST_BIN (video_bin), decoder);
  gst_bin_add (GST_BIN (video_bin), convert);
  gst_bin_add (GST_BIN (video_bin), videosink);
  gst_bin_add (GST_BIN (pipeline), video_bin);

  gst_element_link (src, demux);
  gst_element_link (queue, decoder);
  gst_element_link (decoder, convert);
  gst_element_link (convert, videosink);

  setup_dynamic_link (demux, NULL, gst_element_get_pad (queue, "sink"), NULL);
369
370
371
372
373
374
375
376
377

  seekable = gst_element_get_pad (decoder, "src");
  seekable_pads = g_list_prepend (seekable_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (decoder, "sink"));

  return pipeline;
}

378
379
380
381
382
383
384
385
static GstElement *
make_vorbis_theora_pipeline (const gchar * location)
{
  GstElement *pipeline, *audio_bin, *video_bin;
  GstElement *src, *demux, *a_decoder, *a_convert, *v_decoder, *v_convert;
  GstElement *audiosink, *videosink;
  GstElement *a_queue, *v_queue;
  GstPad *seekable;
Wim Taymans's avatar
Wim Taymans committed
386
  GstPad *pad;
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402

  pipeline = gst_pipeline_new ("app");

  src = gst_element_factory_make_or_warn (SOURCE, "src");
  g_object_set (G_OBJECT (src), "location", location, NULL);

  demux = gst_element_factory_make_or_warn ("oggdemux", "demux");

  gst_bin_add (GST_BIN (pipeline), src);
  gst_bin_add (GST_BIN (pipeline), demux);
  gst_element_link (src, demux);

  audio_bin = gst_bin_new ("a_decoder_bin");
  a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
  a_decoder = gst_element_factory_make_or_warn ("vorbisdec", "a_dec");
  a_convert = gst_element_factory_make_or_warn ("audioconvert", "a_convert");
403
  audiosink = gst_element_factory_make_or_warn (ASINK, "a_sink");
404

405
  gst_bin_add (GST_BIN (pipeline), audio_bin);
406
407
408
409
410
411

  gst_bin_add (GST_BIN (audio_bin), a_queue);
  gst_bin_add (GST_BIN (audio_bin), a_decoder);
  gst_bin_add (GST_BIN (audio_bin), a_convert);
  gst_bin_add (GST_BIN (audio_bin), audiosink);

412
413
414
  gst_element_link (a_queue, a_decoder);
  gst_element_link (a_decoder, a_convert);
  gst_element_link (a_convert, audiosink);
415

Wim Taymans's avatar
Wim Taymans committed
416
417
418
  pad = gst_element_get_pad (a_queue, "sink");
  gst_element_add_pad (audio_bin, gst_ghost_pad_new ("sink", pad));
  gst_object_unref (GST_OBJECT_CAST (pad));
419
420
421

  setup_dynamic_link (demux, NULL, gst_element_get_pad (audio_bin, "sink"),
      NULL);
422
423
424
425
426
427

  video_bin = gst_bin_new ("v_decoder_bin");
  v_queue = gst_element_factory_make_or_warn ("queue", "v_queue");
  v_decoder = gst_element_factory_make_or_warn ("theoradec", "v_dec");
  v_convert =
      gst_element_factory_make_or_warn ("ffmpegcolorspace", "v_convert");
Wim Taymans's avatar
Wim Taymans committed
428
  videosink = gst_element_factory_make_or_warn (VSINK, "v_sink");
429
430

  gst_bin_add (GST_BIN (pipeline), video_bin);
431
432
433
434
435
436

  gst_bin_add (GST_BIN (video_bin), v_queue);
  gst_bin_add (GST_BIN (video_bin), v_decoder);
  gst_bin_add (GST_BIN (video_bin), v_convert);
  gst_bin_add (GST_BIN (video_bin), videosink);

437
  gst_element_link_many (v_queue, v_decoder, v_convert, videosink, NULL);
438

Wim Taymans's avatar
Wim Taymans committed
439
440
441
  pad = gst_element_get_pad (v_queue, "sink");
  gst_element_add_pad (video_bin, gst_ghost_pad_new ("sink", pad));
  gst_object_unref (GST_OBJECT_CAST (pad));
442
443
444

  setup_dynamic_link (demux, NULL, gst_element_get_pad (video_bin, "sink"),
      NULL);
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478

  seekable = gst_element_get_pad (a_decoder, "src");
  seekable_pads = g_list_prepend (seekable_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, seekable);
  rate_pads =
      g_list_prepend (rate_pads, gst_element_get_pad (a_decoder, "sink"));

  return pipeline;
}

static GstElement *
make_avi_msmpeg4v3_mp3_pipeline (const gchar * location)
{
  GstElement *pipeline, *audio_bin, *video_bin;
  GstElement *src, *demux, *a_decoder, *a_convert, *v_decoder, *v_convert;
  GstElement *audiosink, *videosink;
  GstElement *a_queue, *v_queue;
  GstPad *seekable;

  pipeline = gst_pipeline_new ("app");

  src = gst_element_factory_make_or_warn (SOURCE, "src");
  g_object_set (G_OBJECT (src), "location", location, NULL);

  demux = gst_element_factory_make_or_warn ("avidemux", "demux");

  gst_bin_add (GST_BIN (pipeline), src);
  gst_bin_add (GST_BIN (pipeline), demux);
  gst_element_link (src, demux);

  audio_bin = gst_bin_new ("a_decoder_bin");
  a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
  a_decoder = gst_element_factory_make_or_warn ("mad", "a_dec");
  a_convert = gst_element_factory_make_or_warn ("audioconvert", "a_convert");
479
  audiosink = gst_element_factory_make_or_warn (ASINK, "a_sink");
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498

  gst_element_link (a_queue, a_decoder);
  gst_element_link (a_decoder, a_convert);
  gst_element_link (a_convert, audiosink);

  gst_bin_add (GST_BIN (audio_bin), a_queue);
  gst_bin_add (GST_BIN (audio_bin), a_decoder);
  gst_bin_add (GST_BIN (audio_bin), a_convert);
  gst_bin_add (GST_BIN (audio_bin), audiosink);

  gst_bin_add (GST_BIN (pipeline), audio_bin);

  setup_dynamic_link (demux, NULL, gst_element_get_pad (a_queue, "sink"), NULL);

  video_bin = gst_bin_new ("v_decoder_bin");
  v_queue = gst_element_factory_make_or_warn ("queue", "v_queue");
  v_decoder = gst_element_factory_make_or_warn ("ffdec_msmpeg4", "v_dec");
  v_convert =
      gst_element_factory_make_or_warn ("ffmpegcolorspace", "v_convert");
Wim Taymans's avatar
Wim Taymans committed
499
  videosink = gst_element_factory_make_or_warn (VSINK, "v_sink");
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
  gst_element_link_many (v_queue, v_decoder, v_convert, videosink, NULL);

  gst_bin_add (GST_BIN (video_bin), v_queue);
  gst_bin_add (GST_BIN (video_bin), v_decoder);
  gst_bin_add (GST_BIN (video_bin), v_convert);
  gst_bin_add (GST_BIN (video_bin), videosink);

  gst_bin_add (GST_BIN (pipeline), video_bin);

  setup_dynamic_link (demux, NULL, gst_element_get_pad (v_queue, "sink"), NULL);

  seekable = gst_element_get_pad (a_decoder, "src");
  seekable_pads = g_list_prepend (seekable_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, seekable);
  rate_pads =
      g_list_prepend (rate_pads, gst_element_get_pad (a_decoder, "sink"));

  return pipeline;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
520
521
static GstElement *
make_mp3_pipeline (const gchar * location)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
522
523
{
  GstElement *pipeline;
524
  GstElement *src, *decoder, *osssink, *queue;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
525
  GstPad *seekable;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
526

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
527
528
  pipeline = gst_pipeline_new ("app");

529
530
531
  src = gst_element_factory_make_or_warn (SOURCE, "src");
  decoder = gst_element_factory_make_or_warn ("mad", "dec");
  queue = gst_element_factory_make_or_warn ("queue", "queue");
532
  osssink = gst_element_factory_make_or_warn (ASINK, "sink");
533

534
  seekable_elements = g_list_prepend (seekable_elements, osssink);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
535
536

  g_object_set (G_OBJECT (src), "location", location, NULL);
537
  g_object_set (G_OBJECT (osssink), "fragment", 0x00180008, NULL);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
538

539
540
  gst_bin_add (GST_BIN (pipeline), src);
  gst_bin_add (GST_BIN (pipeline), decoder);
541
542
  gst_bin_add (GST_BIN (pipeline), queue);
  gst_bin_add (GST_BIN (pipeline), osssink);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
543

544
545
  gst_element_link (src, decoder);
  gst_element_link (decoder, queue);
546
  gst_element_link (queue, osssink);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
547

548
549
  seekable = gst_element_get_pad (queue, "src");
  seekable_pads = g_list_prepend (seekable_pads, seekable);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
550
551
552
553
554
555
  rate_pads = g_list_prepend (rate_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (decoder, "sink"));

  return pipeline;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
556
557
static GstElement *
make_avi_pipeline (const gchar * location)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
558
559
560
{
  GstElement *pipeline, *audio_bin, *video_bin;
  GstElement *src, *demux, *a_decoder, *v_decoder, *audiosink, *videosink;
561
  GstElement *a_queue = NULL, *v_queue = NULL;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
562
  GstPad *seekable;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
563

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
564
565
  pipeline = gst_pipeline_new ("app");

566
  src = gst_element_factory_make_or_warn (SOURCE, "src");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
567
568
  g_object_set (G_OBJECT (src), "location", location, NULL);

569
  demux = gst_element_factory_make_or_warn ("avidemux", "demux");
570
  seekable_elements = g_list_prepend (seekable_elements, demux);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
571
572
573

  gst_bin_add (GST_BIN (pipeline), src);
  gst_bin_add (GST_BIN (pipeline), demux);
574
  gst_element_link (src, demux);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
575
576

  audio_bin = gst_bin_new ("a_decoder_bin");
577
  a_decoder = gst_element_factory_make_or_warn ("mad", "a_dec");
578
  audiosink = gst_element_factory_make_or_warn (ASINK, "a_sink");
579
  //g_object_set (G_OBJECT (audiosink), "fragment", 0x00180008, NULL);
580
  a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
581
582
  gst_element_link (a_decoder, a_queue);
  gst_element_link (a_queue, audiosink);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
583
  gst_bin_add (GST_BIN (audio_bin), a_decoder);
584
585
  gst_bin_add (GST_BIN (audio_bin), a_queue);
  gst_bin_add (GST_BIN (audio_bin), audiosink);
Wim Taymans's avatar
Wim Taymans committed
586
  gst_element_set_state (audio_bin, GST_STATE_PAUSED);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
587

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
588
  setup_dynamic_link (demux, "audio_00", gst_element_get_pad (a_decoder,
589
          "sink"), audio_bin);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
590
591

  seekable = gst_element_get_pad (a_queue, "src");
592
  seekable_pads = g_list_prepend (seekable_pads, seekable);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
593
  rate_pads = g_list_prepend (rate_pads, seekable);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
594
595
  rate_pads =
      g_list_prepend (rate_pads, gst_element_get_pad (a_decoder, "sink"));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
596
597

  video_bin = gst_bin_new ("v_decoder_bin");
598
  //v_decoder = gst_element_factory_make_or_warn ("identity", "v_dec");
Wim Taymans's avatar
Wim Taymans committed
599
  //v_decoder = gst_element_factory_make_or_warn ("windec", "v_dec");
Wim Taymans's avatar
Wim Taymans committed
600
  v_decoder = gst_element_factory_make_or_warn ("ffmpegdecall", "v_dec");
601
  videosink = gst_element_factory_make_or_warn ("ximagesink", "v_sink");
602
  //videosink = gst_element_factory_make_or_warn ("fakesink", "v_sink");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
603
  //g_object_set (G_OBJECT (videosink), "sync", TRUE, NULL);
604
  v_queue = gst_element_factory_make_or_warn ("queue", "v_queue");
Wim Taymans's avatar
Wim Taymans committed
605
  //g_object_set (G_OBJECT (v_queue), "max_level", 10, NULL);
606
607
  gst_element_link (v_decoder, v_queue);
  gst_element_link (v_queue, videosink);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
608
  gst_bin_add (GST_BIN (video_bin), v_decoder);
609
610
  gst_bin_add (GST_BIN (video_bin), v_queue);
  gst_bin_add (GST_BIN (video_bin), videosink);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
611

Wim Taymans's avatar
Wim Taymans committed
612
  gst_element_set_state (video_bin, GST_STATE_PAUSED);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
613

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
614
  setup_dynamic_link (demux, "video_00", gst_element_get_pad (v_decoder,
615
          "sink"), video_bin);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
616
617

  seekable = gst_element_get_pad (v_queue, "src");
618
  seekable_pads = g_list_prepend (seekable_pads, seekable);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
619
  rate_pads = g_list_prepend (rate_pads, seekable);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
620
621
  rate_pads =
      g_list_prepend (rate_pads, gst_element_get_pad (v_decoder, "sink"));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
622
623
624
625

  return pipeline;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
626
627
static GstElement *
make_mpeg_pipeline (const gchar * location)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
628
629
{
  GstElement *pipeline, *audio_bin, *video_bin;
630
631
  GstElement *src, *demux, *a_decoder, *v_decoder, *v_filter;
  GstElement *audiosink, *videosink;
632
  GstElement *a_queue, *v_queue;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
633
  GstPad *seekable;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
634

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
635
636
  pipeline = gst_pipeline_new ("app");

637
  src = gst_element_factory_make_or_warn (SOURCE, "src");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
638
639
  g_object_set (G_OBJECT (src), "location", location, NULL);

640
  demux = gst_element_factory_make_or_warn ("mpegdemux", "demux");
Wim Taymans's avatar
Wim Taymans committed
641
  g_object_set (G_OBJECT (demux), "sync", FALSE, NULL);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
642

643
644
  seekable_elements = g_list_prepend (seekable_elements, demux);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
645
646
  gst_bin_add (GST_BIN (pipeline), src);
  gst_bin_add (GST_BIN (pipeline), demux);
647
  gst_element_link (src, demux);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
648
649

  audio_bin = gst_bin_new ("a_decoder_bin");
650
651
  a_decoder = gst_element_factory_make_or_warn ("mad", "a_dec");
  a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
652
  audiosink = gst_element_factory_make_or_warn (ASINK, "a_sink");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
653
  g_object_set (G_OBJECT (audiosink), "fragment", 0x00180008, NULL);
654
655
  gst_element_link (a_decoder, a_queue);
  gst_element_link (a_queue, audiosink);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
656
  gst_bin_add (GST_BIN (audio_bin), a_decoder);
657
658
  gst_bin_add (GST_BIN (audio_bin), a_queue);
  gst_bin_add (GST_BIN (audio_bin), audiosink);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
659

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
660
  setup_dynamic_link (demux, "audio_00", gst_element_get_pad (a_decoder,
661
          "sink"), audio_bin);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
662
663

  seekable = gst_element_get_pad (a_queue, "src");
664
  seekable_pads = g_list_prepend (seekable_pads, seekable);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
665
  rate_pads = g_list_prepend (rate_pads, seekable);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
666
667
  rate_pads =
      g_list_prepend (rate_pads, gst_element_get_pad (a_decoder, "sink"));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
668
669

  video_bin = gst_bin_new ("v_decoder_bin");
670
  v_decoder = gst_element_factory_make_or_warn ("mpeg2dec", "v_dec");
Wim Taymans's avatar
Wim Taymans committed
671
  //g_object_set (G_OBJECT (video_thread), "priority", 2, NULL);
672
  v_queue = gst_element_factory_make_or_warn ("queue", "v_queue");
673
  v_filter = gst_element_factory_make_or_warn ("ffmpegcolorspace", "v_filter");
674
  videosink = gst_element_factory_make_or_warn ("ximagesink", "v_sink");
675
  gst_element_link_many (v_decoder, v_queue, v_filter, NULL);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
676

677
  gst_element_link (v_filter, videosink);
678
679
  gst_bin_add_many (GST_BIN (video_bin), v_decoder, NULL);
  gst_bin_add_many (GST_BIN (video_bin), v_queue, v_filter, videosink, NULL);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
680

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
681
  setup_dynamic_link (demux, "video_00", gst_element_get_pad (v_decoder,
682
          "sink"), video_bin);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
683
684

  seekable = gst_element_get_pad (v_queue, "src");
685
  seekable_pads = g_list_prepend (seekable_pads, seekable);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
686
  rate_pads = g_list_prepend (rate_pads, seekable);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
687
688
  rate_pads =
      g_list_prepend (rate_pads, gst_element_get_pad (v_decoder, "sink"));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
689
690
691
692

  return pipeline;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
693
694
static GstElement *
make_mpegnt_pipeline (const gchar * location)
Wim Taymans's avatar
Wim Taymans committed
695
696
697
698
{
  GstElement *pipeline, *audio_bin, *video_bin;
  GstElement *src, *demux, *a_decoder, *v_decoder, *v_filter;
  GstElement *audiosink, *videosink;
699
  GstElement *a_queue;
Wim Taymans's avatar
Wim Taymans committed
700
  GstPad *seekable;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
701

Wim Taymans's avatar
Wim Taymans committed
702
703
704
705
706
707
708
709
710
711
712
713
  pipeline = gst_pipeline_new ("app");

  src = gst_element_factory_make_or_warn (SOURCE, "src");
  g_object_set (G_OBJECT (src), "location", location, NULL);

  demux = gst_element_factory_make_or_warn ("mpegdemux", "demux");
  //g_object_set (G_OBJECT (demux), "sync", TRUE, NULL);

  seekable_elements = g_list_prepend (seekable_elements, demux);

  gst_bin_add (GST_BIN (pipeline), src);
  gst_bin_add (GST_BIN (pipeline), demux);
714
  gst_element_link (src, demux);
Wim Taymans's avatar
Wim Taymans committed
715
716
717
718

  audio_bin = gst_bin_new ("a_decoder_bin");
  a_decoder = gst_element_factory_make_or_warn ("mad", "a_dec");
  a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
719
  audiosink = gst_element_factory_make_or_warn (ASINK, "a_sink");
Wim Taymans's avatar
Wim Taymans committed
720
721
  //g_object_set (G_OBJECT (audiosink), "fragment", 0x00180008, NULL);
  g_object_set (G_OBJECT (audiosink), "sync", FALSE, NULL);
722
723
  gst_element_link (a_decoder, a_queue);
  gst_element_link (a_queue, audiosink);
Wim Taymans's avatar
Wim Taymans committed
724
  gst_bin_add (GST_BIN (audio_bin), a_decoder);
725
726
  gst_bin_add (GST_BIN (audio_bin), a_queue);
  gst_bin_add (GST_BIN (audio_bin), audiosink);
Wim Taymans's avatar
Wim Taymans committed
727

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
728
  setup_dynamic_link (demux, "audio_00", gst_element_get_pad (a_decoder,
729
          "sink"), audio_bin);
Wim Taymans's avatar
Wim Taymans committed
730
731
732
733

  seekable = gst_element_get_pad (a_queue, "src");
  seekable_pads = g_list_prepend (seekable_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, seekable);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
734
735
  rate_pads =
      g_list_prepend (rate_pads, gst_element_get_pad (a_decoder, "sink"));
Wim Taymans's avatar
Wim Taymans committed
736
737
738

  video_bin = gst_bin_new ("v_decoder_bin");
  v_decoder = gst_element_factory_make_or_warn ("mpeg2dec", "v_dec");
739
  v_filter = gst_element_factory_make_or_warn ("ffmpegcolorspace", "v_filter");
740
  videosink = gst_element_factory_make_or_warn ("ximagesink", "v_sink");
741
  gst_element_link_many (v_decoder, v_filter, videosink, NULL);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
742

Wim Taymans's avatar
Wim Taymans committed
743
744
  gst_bin_add_many (GST_BIN (video_bin), v_decoder, v_filter, videosink, NULL);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
745
  setup_dynamic_link (demux, "video_00", gst_element_get_pad (v_decoder,
746
          "sink"), video_bin);
Wim Taymans's avatar
Wim Taymans committed
747
748
749
750

  seekable = gst_element_get_pad (v_decoder, "src");
  seekable_pads = g_list_prepend (seekable_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, seekable);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
751
752
  rate_pads =
      g_list_prepend (rate_pads, gst_element_get_pad (v_decoder, "sink"));
Wim Taymans's avatar
Wim Taymans committed
753
754
755
756

  return pipeline;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
757
758
static GstElement *
make_playerbin_pipeline (const gchar * location)
Wim Taymans's avatar
Wim Taymans committed
759
{
760
  GstElement *player;
761
762
763
764

  player = gst_element_factory_make ("playbin", "player");
  g_assert (player);

765
  g_object_set (G_OBJECT (player), "uri", location, NULL);
766
767
768
769
770
771
772

  seekable_elements = g_list_prepend (seekable_elements, player);

  /* force element seeking on this pipeline */
  elem_seek = TRUE;

  return player;
Wim Taymans's avatar
Wim Taymans committed
773
774
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
775
776
static gchar *
format_value (GtkScale * scale, gdouble value)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
777
{
778
779
780
781
782
783
784
  gint64 real;
  gint64 seconds;
  gint64 subseconds;

  real = value * duration / 100;
  seconds = (gint64) real / GST_SECOND;
  subseconds = (gint64) real / (GST_SECOND / 100);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
785

786
787
  return g_strdup_printf ("%02" G_GINT64_FORMAT ":%02" G_GINT64_FORMAT ":%02"
      G_GINT64_FORMAT, seconds / 60, seconds % 60, subseconds % 100);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
788
789
790
791
792
793
}

typedef struct
{
  const gchar *name;
  const GstFormat format;
794
795
}
seek_format;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
796

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
797
798
799
800
801
802
static seek_format seek_formats[] = {
  {"tim", GST_FORMAT_TIME},
  {"byt", GST_FORMAT_BYTES},
  {"buf", GST_FORMAT_BUFFERS},
  {"def", GST_FORMAT_DEFAULT},
  {NULL, 0},
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
};

G_GNUC_UNUSED static void
query_rates (void)
{
  GList *walk = rate_pads;

  while (walk) {
    GstPad *pad = GST_PAD (walk->data);
    gint i = 0;

    g_print ("rate/sec  %8.8s: ", GST_PAD_NAME (pad));
    while (seek_formats[i].name) {
      gint64 value;
      GstFormat format;

      format = seek_formats[i].format;

Wim Taymans's avatar
Wim Taymans committed
821
822
      if (gst_pad_query_convert (pad, GST_FORMAT_TIME, GST_SECOND, &format,
              &value)) {
823
        g_print ("%s %13" G_GINT64_FORMAT " | ", seek_formats[i].name, value);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
824
      } else {
825
        g_print ("%s %13.13s | ", seek_formats[i].name, "*NA*");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
826
827
828
829
830
831
832
833
834
835
836
      }

      i++;
    }
    g_print (" %s:%s\n", GST_DEBUG_PAD_NAME (pad));

    walk = g_list_next (walk);
  }
}

G_GNUC_UNUSED static void
837
838
839
840
841
842
843
844
845
846
query_positions_elems ()
{
  GList *walk = seekable_elements;

  while (walk) {
    GstElement *element = GST_ELEMENT (walk->data);
    gint i = 0;

    g_print ("positions %8.8s: ", GST_ELEMENT_NAME (element));
    while (seek_formats[i].name) {
Wim Taymans's avatar
Wim Taymans committed
847
      gint64 position, total;
848
849
850
      GstFormat format;

      format = seek_formats[i].format;
Wim Taymans's avatar
Wim Taymans committed
851
852
853
854

      if (gst_element_query_position (element, &format, &position, &total)) {
        g_print ("%s %13" G_GINT64_FORMAT " / %13" G_GINT64_FORMAT " | ",
            seek_formats[i].name, position, total);
855
      } else {
Wim Taymans's avatar
Wim Taymans committed
856
857
        g_print ("%s %13.13s / %13.13s | ", seek_formats[i].name, "*NA*",
            "*NA*");
858
859
860
861
862
863
864
865
866
867
868
      }
      i++;
    }
    g_print (" %s\n", GST_ELEMENT_NAME (element));

    walk = g_list_next (walk);
  }
}

G_GNUC_UNUSED static void
query_positions_pads ()
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
869
{
870
871
872
873
874
875
876
877
878
  GList *walk = seekable_pads;

  while (walk) {
    GstPad *pad = GST_PAD (walk->data);
    gint i = 0;

    g_print ("positions %8.8s: ", GST_PAD_NAME (pad));
    while (seek_formats[i].name) {
      GstFormat format;
Wim Taymans's avatar
Wim Taymans committed
879
      gint64 position, total;
880
881

      format = seek_formats[i].format;
Wim Taymans's avatar
Wim Taymans committed
882
883
884
885

      if (gst_pad_query_position (pad, &format, &position, &total)) {
        g_print ("%s %13" G_GINT64_FORMAT " / %13" G_GINT64_FORMAT " | ",
            seek_formats[i].name, position, total);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
886
      } else {
Wim Taymans's avatar
Wim Taymans committed
887
888
        g_print ("%s %13.13s / %13.13s | ", seek_formats[i].name, "*NA*",
            "*NA*");
889
      }
Wim Taymans's avatar
Wim Taymans committed
890

891
      i++;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
892
    }
893
894
895
    g_print (" %s:%s\n", GST_DEBUG_PAD_NAME (pad));

    walk = g_list_next (walk);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
896
897
898
899
  }
}

static gboolean
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
900
update_scale (gpointer data)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
901
902
{
  GstClock *clock;
Wim Taymans's avatar
Wim Taymans committed
903
  GstFormat format;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
904

Wim Taymans's avatar
Wim Taymans committed
905
  position = 0;
906
  duration = 0;
907
  clock = gst_pipeline_get_clock (GST_PIPELINE (pipeline));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
908

Wim Taymans's avatar
Wim Taymans committed
909
910
  format = GST_FORMAT_TIME;

911
912
913
  if (elem_seek) {
    if (seekable_elements) {
      GstElement *element = GST_ELEMENT (seekable_elements->data);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
914

Wim Taymans's avatar
Wim Taymans committed
915
      gst_element_query_position (element, &format, &position, &duration);
916
917
918
919
920
    }
  } else {
    if (seekable_pads) {
      GstPad *pad = GST_PAD (seekable_pads->data);

Wim Taymans's avatar
Wim Taymans committed
921
      gst_pad_query_position (pad, &format, &position, &duration);
922
    }
923
  }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
924

925
  if (stats) {
926
    if (clock) {
David Schleef's avatar
David Schleef committed
927
928
      g_print ("clock:                  %13" G_GUINT64_FORMAT "  (%s)\n",
          position, gst_object_get_name (GST_OBJECT (clock)));
929
930
931
932
933
934
935
    }

    if (elem_seek) {
      query_positions_elems ();
    } else {
      query_positions_pads ();
    }
936
    query_rates ();
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
937
  }
938
939
  if (position >= duration)
    duration = position;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
940
941
942

  if (duration > 0) {
    gtk_adjustment_set_value (adjustment, position * 100.0 / duration);
943
    gtk_widget_queue_draw (hscale);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
944
945
  }

Wim Taymans's avatar
Wim Taymans committed
946
947
  gst_object_unref (clock);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
948
949
950
  return TRUE;
}

951
static void do_seek (GtkWidget * widget);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
952

953
#ifdef SCRUB
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
954
static gboolean
955
end_scrub (GtkWidget * widget)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
956
957
{
  gst_element_set_state (pipeline, GST_STATE_PAUSED);
958
  seek_timeout_id = 0;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
959
960
961

  return FALSE;
}
962
#endif
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
963

964
965
static void
do_seek (GtkWidget * widget)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
966
967
{
  gint64 real = gtk_range_get_value (GTK_RANGE (widget)) * duration / 100;
968
  gboolean res = FALSE;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
969
  GstEvent *s_event;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
970

971
972
  if (!elem_seek) {
    GList *walk = seekable_pads;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
973

974
975
    while (walk) {
      GstPad *seekable = GST_PAD (walk->data);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
976

977
978
      g_print ("seek to %" GST_TIME_FORMAT " on pad %s:%s\n",
          GST_TIME_ARGS (real), GST_DEBUG_PAD_NAME (seekable));
979
980
981
      s_event =
          gst_event_new_seek (GST_FORMAT_TIME | GST_SEEK_METHOD_SET |
          GST_SEEK_FLAG_FLUSH, real);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
982

983
      res = gst_pad_send_event (seekable, s_event);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
984

985
986
987
988
      walk = g_list_next (walk);
    }
  } else {
    GList *walk = seekable_elements;
989

990
991
    while (walk) {
      GstElement *seekable = GST_ELEMENT (walk->data);
992

993
994
995
      g_print ("seek to %" GST_TIME_FORMAT " on element %s\n",
          GST_TIME_ARGS (real), GST_ELEMENT_NAME (seekable));

996
997
998
      s_event =
          gst_event_new_seek (GST_FORMAT_TIME | GST_SEEK_METHOD_SET |
          GST_SEEK_FLAG_FLUSH, real);
999

1000
      res = gst_element_send_event (seekable, s_event);
1001

1002
1003
      walk = g_list_next (walk);
    }
1004
  }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1005

1006
  if (res)
Wim Taymans's avatar
Wim Taymans committed
1007
    gst_pipeline_set_new_stream_time (GST_PIPELINE (pipeline), 0);
1008
1009
  else
    g_print ("seek failed\n");
1010
1011
1012
1013
1014
}

static void
seek_cb (GtkWidget * widget)
{
1015
1016
1017
#ifndef SCRUB
  GTimeVal timeval;
#else
1018
1019
1020
1021
1022
1023
1024
1025
  /* If the timer hasn't expired yet, then the pipeline is running */
  if (seek_timeout_id != 0) {
    gst_element_set_state (pipeline, GST_STATE_PAUSED);
  }
#endif

  do_seek (widget);

1026
1027
1028
1029
1030
1031
#ifndef SCRUB
  /* wait for preroll */
  GST_TIME_TO_TIMEVAL (50 * GST_MSECOND, timeval);
  gst_element_get_state (pipeline, NULL, NULL, &timeval);
#endif

1032
#ifdef SCRUB
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1033
  gst_element_set_state (pipeline, GST_STATE_PLAYING);
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064

  if (seek_timeout_id == 0) {
    seek_timeout_id =
        gtk_timeout_add (SCRUB_TIME, (GSourceFunc) end_scrub, widget);
  }
#endif
}

static gboolean
start_seek (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
{
  gst_element_set_state (pipeline, GST_STATE_PAUSED);
  gtk_timeout_remove (update_id);

  if (changed_id == 0) {
    changed_id = gtk_signal_connect (GTK_OBJECT (hscale),
        "value_changed", G_CALLBACK (seek_cb), pipeline);
  }

  return FALSE;
}

static gboolean
stop_seek (GtkWidget * widget, gpointer user_data)
{
  g_signal_handler_disconnect (GTK_OBJECT (hscale), changed_id);
  changed_id = 0;
  if (seek_timeout_id