seek.c 27.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;
12
static guint64 duration;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
13
static GtkAdjustment *adjustment;
14
static gboolean stats = FALSE;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
15 16 17

static guint update_id;

18 19
//#define SOURCE "gnomevfssrc"
#define SOURCE "filesrc"
20

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
21 22 23
#define UPDATE_INTERVAL 500

#define THREAD
24
#define PAD_SEEK
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

typedef struct
{
  const gchar 	*padname;
  GstPad 	*target;
  GstElement 	*bin;
} dyn_connect;

static void
dynamic_connect (GstPadTemplate *templ, GstPad *newpad, gpointer data)
{
  dyn_connect *connect = (dyn_connect *) data;

  if (!strcmp (gst_pad_get_name (newpad), connect->padname)) {
    gst_element_set_state (pipeline, GST_STATE_PAUSED);
    gst_bin_add (GST_BIN (pipeline), connect->bin);
    gst_pad_connect (newpad, connect->target);
    gst_element_set_state (pipeline, GST_STATE_PLAYING);

44
    seekable_pads = g_list_prepend (seekable_pads, newpad);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    rate_pads = g_list_prepend (rate_pads, newpad);
  }
}

static void
setup_dynamic_connection (GstElement *element, const gchar *padname, GstPad *target, GstElement *bin)
{
  dyn_connect *connect;

  connect = g_new0 (dyn_connect, 1);
  connect->padname 	= g_strdup (padname);
  connect->target 	= target;
  connect->bin 		= bin;

  g_signal_connect (G_OBJECT (element), "new_pad", G_CALLBACK (dynamic_connect), connect);
}

62 63 64 65 66 67 68 69 70
static GstElement*
make_mod_pipeline (const gchar *location) 
{
  GstElement *pipeline;
  GstElement *src, *decoder, *audiosink;
  GstPad *seekable;
  
  pipeline = gst_pipeline_new ("app");

71 72 73
  src = gst_element_factory_make_or_warn (SOURCE, "src");
  decoder = gst_element_factory_make_or_warn ("modplug", "decoder");
  audiosink = gst_element_factory_make_or_warn ("osssink", "sink");
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  //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);

  gst_element_connect (src, decoder);
  gst_element_connect (decoder, audiosink);

  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;
}

Wim Taymans's avatar
Wim Taymans committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
static GstElement*
make_dv_pipeline (const gchar *location) 
{
  GstElement *pipeline;
  GstElement *src, *decoder, *audiosink, *videosink;
  GstPad *seekable;
  
  pipeline = gst_pipeline_new ("app");

  src = gst_element_factory_make_or_warn (SOURCE, "src");
  decoder = gst_element_factory_make_or_warn ("dvdec", "decoder");
  videosink = gst_element_factory_make_or_warn ("xvideosink", "v_sink");
  audiosink = gst_element_factory_make_or_warn ("osssink", "a_sink");
  //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);
  gst_bin_add (GST_BIN (pipeline), videosink);

  gst_element_connect (src, decoder);
  gst_element_connect (decoder, audiosink);
  gst_element_connect (decoder, videosink);

  seekable = gst_element_get_pad (decoder, "video");
  seekable_pads = g_list_prepend (seekable_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, seekable);
  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;
}

Wim Taymans's avatar
Wim Taymans committed
130 131 132 133 134 135 136 137 138
static GstElement*
make_wav_pipeline (const gchar *location) 
{
  GstElement *pipeline;
  GstElement *src, *decoder, *audiosink;
  GstPad *seekable;
  
  pipeline = gst_pipeline_new ("app");

139 140 141
  src = gst_element_factory_make_or_warn (SOURCE, "src");
  decoder = gst_element_factory_make_or_warn ("wavparse", "decoder");
  audiosink = gst_element_factory_make_or_warn ("osssink", "sink");
Wim Taymans's avatar
Wim Taymans committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  //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);

  gst_element_connect (src, decoder);
  gst_element_connect (decoder, audiosink);

  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;
}

161 162 163 164 165 166 167 168 169
static GstElement*
make_flac_pipeline (const gchar *location) 
{
  GstElement *pipeline;
  GstElement *src, *decoder, *audiosink;
  GstPad *seekable;
  
  pipeline = gst_pipeline_new ("app");

170 171 172
  src = gst_element_factory_make_or_warn (SOURCE, "src");
  decoder = gst_element_factory_make_or_warn ("flacdec", "decoder");
  audiosink = gst_element_factory_make_or_warn ("osssink", "sink");
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  //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);

  gst_element_connect (src, decoder);
  gst_element_connect (decoder, audiosink);

  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_sid_pipeline (const gchar *location) 
{
  GstElement *pipeline;
  GstElement *src, *decoder, *audiosink;
  GstPad *seekable;
  
  pipeline = gst_pipeline_new ("app");

201 202 203
  src = gst_element_factory_make_or_warn (SOURCE, "src");
  decoder = gst_element_factory_make_or_warn ("siddec", "decoder");
  audiosink = gst_element_factory_make_or_warn ("osssink", "sink");
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  //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);

  gst_element_connect (src, decoder);
  gst_element_connect (decoder, audiosink);

  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
223 224 225 226 227 228 229 230 231
static GstElement*
make_parse_pipeline (const gchar *location) 
{
  GstElement *pipeline;
  GstElement *src, *parser, *fakesink;
  GstPad *seekable;
  
  pipeline = gst_pipeline_new ("app");

232 233 234
  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");
235
  g_object_set (G_OBJECT (fakesink), "sync", FALSE, NULL);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
236 237 238 239 240 241 242 243 244 245 246

  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);

  gst_element_connect (src, parser);
  gst_element_connect (parser, fakesink);

  seekable = gst_element_get_pad (parser, "src");
247
  seekable_pads = g_list_prepend (seekable_pads, seekable);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
248 249 250 251 252 253
  rate_pads = g_list_prepend (rate_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (parser, "sink"));

  return pipeline;
}

254 255 256 257 258 259 260 261 262
static GstElement*
make_vorbis_pipeline (const gchar *location) 
{
  GstElement *pipeline;
  GstElement *src, *decoder, *audiosink;
  GstPad *seekable;
  
  pipeline = gst_pipeline_new ("app");

263 264 265
  src = gst_element_factory_make_or_warn (SOURCE, "src");
  decoder = gst_element_factory_make_or_warn ("vorbisfile", "decoder");
  audiosink = gst_element_factory_make_or_warn ("osssink", "sink");
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
  //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);

  gst_element_connect (src, decoder);
  gst_element_connect (decoder, audiosink);

  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
285 286 287 288
static GstElement*
make_mp3_pipeline (const gchar *location) 
{
  GstElement *pipeline;
289
  GstElement *src, *decoder, *osssink, *queue, *audio_thread;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
290 291 292 293
  GstPad *seekable;
  
  pipeline = gst_pipeline_new ("app");

294 295 296 297
  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");
  osssink = gst_element_factory_make_or_warn ("osssink", "sink");
298 299 300 301

  audio_thread = gst_thread_new ("a_decoder_thread");

  seekable_elements = g_list_prepend (seekable_elements, osssink);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
302 303 304 305

  g_object_set (G_OBJECT (src), "location", location, NULL);
  g_object_set (G_OBJECT (osssink), "fragment", 0x00180008, NULL);

306 307 308 309 310
  gst_bin_add (GST_BIN (pipeline), src);
  gst_bin_add (GST_BIN (pipeline), decoder);
  gst_bin_add (GST_BIN (audio_thread), queue);
  gst_bin_add (GST_BIN (audio_thread), osssink);
  gst_bin_add (GST_BIN (pipeline), audio_thread);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
311

312 313 314
  gst_element_connect (src, decoder);
  gst_element_connect (decoder, queue);
  gst_element_connect (queue, osssink);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
315

316 317
  seekable = gst_element_get_pad (queue, "src");
  seekable_pads = g_list_prepend (seekable_pads, seekable);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
  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_avi_pipeline (const gchar *location) 
{
  GstElement *pipeline, *audio_bin, *video_bin;
  GstElement *src, *demux, *a_decoder, *v_decoder, *audiosink, *videosink;
  GstElement *a_queue = NULL, *audio_thread = NULL, *v_queue = NULL, *video_thread = NULL;
  GstPad *seekable;
  
  pipeline = gst_pipeline_new ("app");

334
  src = gst_element_factory_make_or_warn (SOURCE, "src");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
335 336
  g_object_set (G_OBJECT (src), "location", location, NULL);

337
  demux = gst_element_factory_make_or_warn ("avidemux", "demux");
338
  seekable_elements = g_list_prepend (seekable_elements, demux);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
339 340 341 342 343 344

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

  audio_bin = gst_bin_new ("a_decoder_bin");
345
  a_decoder = gst_element_factory_make_or_warn ("mad", "a_dec");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
346
  audio_thread = gst_thread_new ("a_decoder_thread");
347
  audiosink = gst_element_factory_make_or_warn ("osssink", "a_sink");
348
  //g_object_set (G_OBJECT (audiosink), "fragment", 0x00180008, NULL);
349
  a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
350 351 352 353 354 355
  gst_element_connect (a_decoder, a_queue);
  gst_element_connect (a_queue, audiosink);
  gst_bin_add (GST_BIN (audio_bin), a_decoder);
  gst_bin_add (GST_BIN (audio_bin), audio_thread);
  gst_bin_add (GST_BIN (audio_thread), a_queue);
  gst_bin_add (GST_BIN (audio_thread), audiosink);
Wim Taymans's avatar
Wim Taymans committed
356
  gst_element_set_state (audio_bin, GST_STATE_PAUSED);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
357 358 359 360

  setup_dynamic_connection (demux, "audio_00", gst_element_get_pad (a_decoder, "sink"), audio_bin);

  seekable = gst_element_get_pad (a_queue, "src");
361
  seekable_pads = g_list_prepend (seekable_pads, seekable);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
362 363 364 365
  rate_pads = g_list_prepend (rate_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (a_decoder, "sink"));

  video_bin = gst_bin_new ("v_decoder_bin");
366 367
  //v_decoder = gst_element_factory_make_or_warn ("identity", "v_dec");
  v_decoder = gst_element_factory_make_or_warn ("windec", "v_dec");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
368
  video_thread = gst_thread_new ("v_decoder_thread");
369 370
  videosink = gst_element_factory_make_or_warn ("xvideosink", "v_sink");
  //videosink = gst_element_factory_make_or_warn ("fakesink", "v_sink");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
371
  //g_object_set (G_OBJECT (videosink), "sync", TRUE, NULL);
372
  v_queue = gst_element_factory_make_or_warn ("queue", "v_queue");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
373 374 375 376 377 378 379 380
  g_object_set (G_OBJECT (v_queue), "max_level", 10, NULL);
  gst_element_connect (v_decoder, v_queue);
  gst_element_connect (v_queue, videosink);
  gst_bin_add (GST_BIN (video_bin), v_decoder);
  gst_bin_add (GST_BIN (video_bin), video_thread);
  gst_bin_add (GST_BIN (video_thread), v_queue);
  gst_bin_add (GST_BIN (video_thread), videosink);

Wim Taymans's avatar
Wim Taymans committed
381
  gst_element_set_state (video_bin, GST_STATE_PAUSED);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
382 383 384 385

  setup_dynamic_connection (demux, "video_00", gst_element_get_pad (v_decoder, "sink"), video_bin);

  seekable = gst_element_get_pad (v_queue, "src");
386
  seekable_pads = g_list_prepend (seekable_pads, seekable);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
387 388 389 390 391 392 393 394 395 396
  rate_pads = g_list_prepend (rate_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (v_decoder, "sink"));

  return pipeline;
}

static GstElement*
make_mpeg_pipeline (const gchar *location) 
{
  GstElement *pipeline, *audio_bin, *video_bin;
397 398
  GstElement *src, *demux, *a_decoder, *v_decoder, *v_filter;
  GstElement *audiosink, *videosink;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
399 400 401 402 403
  GstElement *a_queue, *audio_thread, *v_queue, *video_thread;
  GstPad *seekable;
  
  pipeline = gst_pipeline_new ("app");

404
  src = gst_element_factory_make_or_warn (SOURCE, "src");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
405 406
  g_object_set (G_OBJECT (src), "location", location, NULL);

407
  demux = gst_element_factory_make_or_warn ("mpegdemux", "demux");
408 409
  //g_object_set (G_OBJECT (demux), "sync", FALSE, NULL);
  g_object_set (G_OBJECT (demux), "sync", TRUE, NULL);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
410

411 412
  seekable_elements = g_list_prepend (seekable_elements, demux);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
413 414 415 416 417
  gst_bin_add (GST_BIN (pipeline), src);
  gst_bin_add (GST_BIN (pipeline), demux);
  gst_element_connect (src, demux);

  audio_bin = gst_bin_new ("a_decoder_bin");
418
  a_decoder = gst_element_factory_make_or_warn ("mad", "a_dec");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
419
  audio_thread = gst_thread_new ("a_decoder_thread");
420 421
  a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
  audiosink = gst_element_factory_make_or_warn ("osssink", "a_sink");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
422 423 424 425 426 427 428 429 430 431 432
  g_object_set (G_OBJECT (audiosink), "fragment", 0x00180008, NULL);
  gst_element_connect (a_decoder, a_queue);
  gst_element_connect (a_queue, audiosink);
  gst_bin_add (GST_BIN (audio_bin), a_decoder);
  gst_bin_add (GST_BIN (audio_bin), audio_thread);
  gst_bin_add (GST_BIN (audio_thread), a_queue);
  gst_bin_add (GST_BIN (audio_thread), audiosink);

  setup_dynamic_connection (demux, "audio_00", gst_element_get_pad (a_decoder, "sink"), audio_bin);

  seekable = gst_element_get_pad (a_queue, "src");
433
  seekable_pads = g_list_prepend (seekable_pads, seekable);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
434 435 436 437
  rate_pads = g_list_prepend (rate_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (a_decoder, "sink"));

  video_bin = gst_bin_new ("v_decoder_bin");
438
  v_decoder = gst_element_factory_make_or_warn ("mpeg2dec", "v_dec");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
439
  video_thread = gst_thread_new ("v_decoder_thread");
440
  g_object_set (G_OBJECT (video_thread), "schedpolicy", 2, NULL);
441 442 443
  v_queue = gst_element_factory_make_or_warn ("queue", "v_queue");
  v_filter = gst_element_factory_make_or_warn ("colorspace", "v_filter");
  videosink = gst_element_factory_make_or_warn ("xvideosink", "v_sink");
444 445 446
  gst_element_connect_many (v_decoder, v_queue, v_filter, NULL);
  
  gst_element_connect_pads (v_filter, "src", videosink, "sink");
447
  gst_bin_add_many (GST_BIN (video_bin), v_decoder, video_thread, NULL);
448
  gst_bin_add_many (GST_BIN (video_thread), v_queue, v_filter, videosink, NULL);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
449 450 451 452

  setup_dynamic_connection (demux, "video_00", gst_element_get_pad (v_decoder, "sink"), video_bin);

  seekable = gst_element_get_pad (v_queue, "src");
453
  seekable_pads = g_list_prepend (seekable_pads, seekable);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
454 455 456 457 458 459
  rate_pads = g_list_prepend (rate_pads, seekable);
  rate_pads = g_list_prepend (rate_pads, gst_element_get_pad (v_decoder, "sink"));

  return pipeline;
}

Wim Taymans's avatar
Wim Taymans committed
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
static GstElement*
make_mpegnt_pipeline (const gchar *location) 
{
  GstElement *pipeline, *audio_bin, *video_bin;
  GstElement *src, *demux, *a_decoder, *v_decoder, *v_filter;
  GstElement *audiosink, *videosink;
  GstElement *a_queue, *audio_thread;
  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 ("mpegdemux", "demux");
  //g_object_set (G_OBJECT (demux), "sync", FALSE, NULL);
  //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);
  gst_element_connect (src, demux);

  audio_bin = gst_bin_new ("a_decoder_bin");
  a_decoder = gst_element_factory_make_or_warn ("mad", "a_dec");
  audio_thread = gst_thread_new ("a_decoder_thread");
  a_queue = gst_element_factory_make_or_warn ("queue", "a_queue");
  audiosink = gst_element_factory_make_or_warn ("osssink", "a_sink");
  //g_object_set (G_OBJECT (audiosink), "fragment", 0x00180008, NULL);
  g_object_set (G_OBJECT (audiosink), "sync", FALSE, NULL);
  gst_element_connect (a_decoder, a_queue);
  gst_element_connect (a_queue, audiosink);
  gst_bin_add (GST_BIN (audio_bin), a_decoder);
  gst_bin_add (GST_BIN (audio_bin), audio_thread);
  gst_bin_add (GST_BIN (audio_thread), a_queue);
  gst_bin_add (GST_BIN (audio_thread), audiosink);

  setup_dynamic_connection (demux, "audio_00", gst_element_get_pad (a_decoder, "sink"), audio_bin);

  seekable = gst_element_get_pad (a_queue, "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"));

  video_bin = gst_bin_new ("v_decoder_bin");
  v_decoder = gst_element_factory_make_or_warn ("mpeg2dec", "v_dec");
  v_filter = gst_element_factory_make_or_warn ("colorspace", "v_filter");
  videosink = gst_element_factory_make_or_warn ("xvideosink", "v_sink");
  gst_element_connect_many (v_decoder, v_filter, NULL);
  
  gst_element_connect_pads (v_filter, "src", videosink, "sink");
  gst_bin_add_many (GST_BIN (video_bin), v_decoder, v_filter, videosink, NULL);

  setup_dynamic_connection (demux, "video_00", gst_element_get_pad (v_decoder, "sink"), video_bin);

  seekable = gst_element_get_pad (v_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 (v_decoder, "sink"));

  return pipeline;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
524 525 526 527
static gchar*
format_value (GtkScale *scale,
	      gdouble   value)
{
528 529 530 531 532 533 534
  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
535

536
  return g_strdup_printf ("%02lld:%02lld:%02lld",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
                          seconds/60, 
			  seconds%60, 
			  subseconds%100);
}

typedef struct
{
  const gchar *name;
  const GstFormat format;
} seek_format;

static seek_format seek_formats[] = 
{
  { "tim",  GST_FORMAT_TIME    },
  { "byt",  GST_FORMAT_BYTES   },
552
  { "unt",  GST_FORMAT_UNITS   },
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
  { "buf",  GST_FORMAT_BUFFERS },
  { "def",  GST_FORMAT_DEFAULT },
  { NULL, 0 }, 
};

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;

      if (gst_pad_convert (pad, GST_FORMAT_TIME, GST_SECOND, 
		           &format, &value)) 
      {
577
        g_print ("%s %13lld | ", seek_formats[i].name, value);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
578 579
      }
      else {
580
        g_print ("%s %13.13s | ", seek_formats[i].name, "*NA*");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
581 582 583 584 585 586 587 588 589 590 591
      }

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

    walk = g_list_next (walk);
  }
}

G_GNUC_UNUSED static void
592
query_durations ()
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
593
{
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
  GList *walk = seekable_pads;

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

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

      format = seek_formats[i].format;
      res = gst_pad_query (pad, GST_PAD_QUERY_TOTAL, &format, &value);
      if (res) {
        g_print ("%s %13lld | ", seek_formats[i].name, value);
      }
      else {
        g_print ("%s %13.13s | ", seek_formats[i].name, "*NA*");
      }
      i++;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
615
    }
616 617 618
    g_print (" %s:%s\n", GST_DEBUG_PAD_NAME (pad));
  
    walk = g_list_next (walk);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
619 620 621 622
  }
}

G_GNUC_UNUSED static void
623
query_positions ()
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
624
{
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
  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) {
      gboolean res;
      gint64 value;
      GstFormat format;

      format = seek_formats[i].format;
      res = gst_pad_query (pad, GST_PAD_QUERY_POSITION, &format, &value);
      if (res) {
        g_print ("%s %13lld | ", seek_formats[i].name, value);
      }
      else {
        g_print ("%s %13.13s | ", seek_formats[i].name, "*NA*");
      }
      i++;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
646
    }
647 648 649
    g_print (" %s:%s\n", GST_DEBUG_PAD_NAME (pad));

    walk = g_list_next (walk);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
650 651 652 653 654 655 656
  }
}

static gboolean
update_scale (gpointer data) 
{
  GstClock *clock;
657 658
  guint64 position;
  GstFormat format = GST_FORMAT_TIME;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
659

660
  duration = 0;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
661 662
  clock = gst_bin_get_clock (GST_BIN (pipeline));

663 664 665 666 667
  if (seekable_pads) {
    GstPad *pad = GST_PAD (seekable_pads->data);
    gst_pad_query (pad, GST_PAD_QUERY_TOTAL, &format, &duration);
  }
  position = gst_clock_get_time (clock);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
668

669 670 671 672 673
  if (stats) {
    g_print ("clock:                  %13llu  (%s)\n", position, gst_object_get_name (GST_OBJECT (clock)));
    query_durations ();
    query_positions ();
    query_rates ();
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
  }

  if (duration > 0) {
    gtk_adjustment_set_value (adjustment, position * 100.0 / duration);
  }

  return TRUE;
}

static gboolean
iterate (gpointer data)
{
  gboolean res;

  res = gst_bin_iterate (GST_BIN (data));
  if (!res) {
    gtk_timeout_remove (update_id);
    g_print ("stopping iterations\n");
  }
  return res;
}

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

  return FALSE;
}

static gboolean
stop_seek (GtkWidget *widget, GdkEventButton *event, gpointer user_data)
{
  gint64 real = gtk_range_get_value (GTK_RANGE (widget)) * duration / 100;
  gboolean res;
  GstEvent *s_event;
711 712
#ifdef PAD_SEEK
  GList *walk = seekable_pads;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
713 714 715 716 717 718 719 720 721 722 723 724 725

  while (walk) {
    GstPad *seekable = GST_PAD (walk->data);

    g_print ("seek to %lld on pad %s:%s\n", real, GST_DEBUG_PAD_NAME (seekable));
    s_event = gst_event_new_seek (GST_FORMAT_TIME |
			   	  GST_SEEK_METHOD_SET |
				  GST_SEEK_FLAG_FLUSH, real);

    res = gst_pad_send_event (seekable, s_event);

    walk = g_list_next (walk);
  }
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
#else
  GList *walk = seekable_elements;

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

    g_print ("seek to %lld on element %s\n", real, gst_element_get_name (seekable));
    s_event = gst_event_new_seek (GST_FORMAT_TIME |
			   	  GST_SEEK_METHOD_SET |
				  GST_SEEK_FLAG_FLUSH, real);

    res = gst_element_send_event (seekable, s_event);

    walk = g_list_next (walk);
  }
#endif
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
742 743 744 745 746 747 748 749 750 751 752

  gst_element_set_state (pipeline, GST_STATE_PLAYING);
  gtk_idle_add ((GtkFunction) iterate, pipeline);
  update_id = gtk_timeout_add (UPDATE_INTERVAL, (GtkFunction) update_scale, pipeline);

  return FALSE;
}

static void
play_cb (GtkButton * button, gpointer data)
{
753 754 755 756 757
  if (gst_element_get_state (pipeline) != GST_STATE_PLAYING) {
    gst_element_set_state (pipeline, GST_STATE_PLAYING);
    gtk_idle_add ((GtkFunction) iterate, pipeline);
    update_id = gtk_timeout_add (UPDATE_INTERVAL, (GtkFunction) update_scale, pipeline);
  }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
758 759 760 761 762
}

static void
pause_cb (GtkButton * button, gpointer data)
{
763 764 765 766
  if (gst_element_get_state (pipeline) != GST_STATE_PAUSED) {
    gst_element_set_state (pipeline, GST_STATE_PAUSED);
    gtk_timeout_remove (update_id);
  }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
767 768 769 770 771
}

static void
stop_cb (GtkButton * button, gpointer data)
{
772 773 774 775
  if (gst_element_get_state (pipeline) != GST_STATE_READY) {
    gst_element_set_state (pipeline, GST_STATE_READY);
    gtk_timeout_remove (update_id);
  }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
776 777 778 779 780 781 782 783
}

int
main (int argc, char **argv)
{
  GtkWidget *window, *hbox, *vbox, 
            *play_button, *pause_button, *stop_button, 
	    *hscale;
784 785 786 787 788 789 790
  struct poptOption options[] = {
    {"stats",  's',  POPT_ARG_NONE|POPT_ARGFLAG_STRIP,   &stats,   0,
           "Show pad stats", NULL},
    POPT_TABLEEND
  };
	        
  gst_init_with_popt_table (&argc, &argv, options);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
791 792 793
  gtk_init (&argc, &argv);

  if (argc != 3) {
Wim Taymans's avatar
Wim Taymans committed
794
    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]);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
795 796 797 798 799 800 801 802 803
    exit (-1);
  }

  if (atoi (argv[1]) == 0) 
    pipeline = make_mp3_pipeline (argv[2]);
  else if (atoi (argv[1]) == 1) 
    pipeline = make_avi_pipeline (argv[2]);
  else if (atoi (argv[1]) == 2) 
    pipeline = make_mpeg_pipeline (argv[2]);
804
  else if (atoi (argv[1]) == 3)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
805
    pipeline = make_parse_pipeline (argv[2]);
806 807 808 809 810 811
  else if (atoi (argv[1]) == 4) 
    pipeline = make_vorbis_pipeline (argv[2]);
  else if (atoi (argv[1]) == 5) 
    pipeline = make_sid_pipeline (argv[2]);
  else if (atoi (argv[1]) == 6) 
    pipeline = make_flac_pipeline (argv[2]);
Wim Taymans's avatar
Wim Taymans committed
812 813
  else if (atoi (argv[1]) == 7) 
    pipeline = make_wav_pipeline (argv[2]);
814 815
  else if (atoi (argv[1]) == 8) 
    pipeline = make_mod_pipeline (argv[2]);
Wim Taymans's avatar
Wim Taymans committed
816 817
  else if (atoi (argv[1]) == 9) 
    pipeline = make_dv_pipeline (argv[2]);
Wim Taymans's avatar
Wim Taymans committed
818 819
  else if (atoi (argv[1]) == 10) 
    pipeline = make_mpegnt_pipeline (argv[2]);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860

  /* initialize gui elements ... */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  hbox = gtk_hbox_new (FALSE, 0);
  vbox = gtk_vbox_new (FALSE, 0);
  play_button = gtk_button_new_with_label ("play");
  pause_button = gtk_button_new_with_label ("pause");
  stop_button = gtk_button_new_with_label ("stop");

  adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.00, 100.0, 0.1, 1.0, 1.0));
  hscale = gtk_hscale_new (adjustment);
  gtk_scale_set_digits (GTK_SCALE (hscale), 2);
  gtk_range_set_update_policy (GTK_RANGE (hscale), GTK_UPDATE_CONTINUOUS);

  gtk_signal_connect(GTK_OBJECT(hscale),
                             "button_press_event", G_CALLBACK (start_seek), pipeline);
  gtk_signal_connect(GTK_OBJECT(hscale),
                             "button_release_event", G_CALLBACK (stop_seek), pipeline);
  gtk_signal_connect(GTK_OBJECT(hscale),
                             "format_value", G_CALLBACK (format_value), pipeline);

  /* do the packing stuff ... */
  gtk_window_set_default_size (GTK_WINDOW (window), 96, 96);
  gtk_container_add (GTK_CONTAINER (window), vbox);
  gtk_container_add (GTK_CONTAINER (vbox), hbox);
  gtk_box_pack_start (GTK_BOX (hbox), play_button, FALSE, FALSE, 2);
  gtk_box_pack_start (GTK_BOX (hbox), pause_button, FALSE, FALSE, 2);
  gtk_box_pack_start (GTK_BOX (hbox), stop_button, FALSE, FALSE, 2);
  gtk_box_pack_start (GTK_BOX (vbox), hscale, TRUE, TRUE, 2);

  /* connect things ... */
  g_signal_connect (G_OBJECT (play_button), "clicked", G_CALLBACK (play_cb), pipeline);
  g_signal_connect (G_OBJECT (pause_button), "clicked", G_CALLBACK (pause_cb), pipeline);
  g_signal_connect (G_OBJECT (stop_button), "clicked", G_CALLBACK (stop_cb), pipeline);
  g_signal_connect (G_OBJECT (window), "delete_event", gtk_main_quit, NULL);

  /* show the gui. */
  gtk_widget_show_all (window);

  gtk_main ();

861 862 863 864
  //gst_object_unref (GST_OBJECT (pipeline));
  gst_buffer_print_stats();
  gst_event_print_stats();

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
865 866 867 868
  //g_mem_chunk_info();

  return 0;
}