media-info.c 22.9 KB
Newer Older
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <gst/gst.h>
#include <string.h>
#include "media-info.h"

24
static gboolean _gst_media_info_debug = TRUE;
25 26 27 28 29 30 31 32

#ifdef G_HAVE_ISO_VARARGS

#define GMI_DEBUG(...) \
  { if (_gst_media_info_debug) { g_print ( __VA_ARGS__ ); }}

#elif defined(G_HAVE_GNUC_VARARGS)

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
33 34 35
#define GMI_DEBUG(format, args...) \
  { if (_gst_media_info_debug) { g_print ( format , ## args ); }}

36 37 38
#endif


Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
struct GstMediaInfoPriv
{
  GstElement *pipeline;

  GstElement *typefind;

  GstCaps *type;
  GstPad *decoder_pad;			/* pad for querying decoded caps */
  GstPad *source_pad;			/* pad for querying encoded caps */

  GstCaps *format;
  GstCaps *metadata;
  GstCaps *streaminfo;

  GstElement *decoder;			/* will be != NULL during collection */
  gchar *source_element;		/* type of element used as source */
  GstElement *source;

  GHashTable *decoders;			/* a table of decoder GstElements */

  GstMediaInfoStream *stream;		/* total stream properties */
  char *cache;				/* location of cache */
};

static void 	gst_media_info_class_init 	(GstMediaInfoClass *klass);
static void 	gst_media_info_instance_init 	(GstMediaInfo *info);

static void	gst_media_info_get_property     (GObject *object, guint prop_id,
			     			 GValue *value, GParamSpec *pspec);


static void	gst_media_info_reset 		(GstMediaInfo *info);

static void	deep_notify_callback 		(GObject *object, GstObject *origin,
		      				GParamSpec *pspec, GstMediaInfo *info);

/* helper structs bits */
static GstMediaInfoStream *
gst_media_info_stream_new (void)
{
  GstMediaInfoStream *stream;

  stream = g_malloc (sizeof (GstMediaInfoStream));

  stream->length_tracks = 0;
  stream->length_time = 0;
  stream->bitrate = 0;
  stream->seekable = FALSE;
  stream->path = NULL;
  stream->mime = NULL;
  stream->tracks = NULL;

  return stream;
}

static void
gst_media_info_stream_free (GstMediaInfoStream *stream)
{
  if (stream->mime) g_free (stream->mime);
  /* FIXME: free tracks */
  g_free (stream);
}

static GstMediaInfoTrack *
gst_media_info_track_new (void)
{
  GstMediaInfoTrack *track;

  track = g_malloc (sizeof (GstMediaInfoTrack));

  track->metadata = NULL;
  track->streaminfo = NULL;
  track->format = NULL;
  track->length_time = 0;
  track->con_streams = NULL;

  return track;
}
/* GObject-y bits */

/* signal stuff */
enum
{
  MEDIA_INFO_SIGNAL,
  LAST_SIGNAL
};

static guint gst_media_info_signals[LAST_SIGNAL] = { 0 };

/* GError stuff */
/*
enum
{
  MEDIA_INFO_ERROR_FILE
};
*/
/* GError quark stuff */
/*
static GQuark
gst_media_info_error_quark (void)
{
  static GQuark quark = 0;
  if (quark == 0)
    quark = g_quark_from_static_string ("gst-media-info-error-quark");
  return quark;
}
*/
/*
 * GObject type stuff 
 */

enum
{
  PROP_SOURCE
};

static GObjectClass *parent_class = NULL;

GType
gst_media_info_get_type (void)
{
  static GType gst_media_info_type = 0;
  if (!gst_media_info_type)
  {
    static const GTypeInfo gst_media_info_info = {
      sizeof (GstMediaInfoClass),
      (GBaseInitFunc) NULL,
      (GBaseFinalizeFunc) NULL,
      (GClassInitFunc) gst_media_info_class_init,
      NULL, NULL,
      sizeof (GstMediaInfo),
      0,
      (GInstanceInitFunc) gst_media_info_instance_init,
      NULL
    };
    gst_media_info_type = g_type_register_static (G_TYPE_OBJECT, 
		                                  "GstMediaInfo",
		                                  &gst_media_info_info, 0); 
  }
  return gst_media_info_type;
}

static void
gst_media_info_class_init (GstMediaInfoClass *klass)
{
  GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
  
  parent_class = g_type_class_ref (G_TYPE_OBJECT);
  //parent_class = g_type_class_peek_parent (klass);


  /*
  object_class->finalize = gst_media_info_finalize;
  object_class->dispose  = gst_media_info_dispose;
  */

  /*
  g_object_class->set_property = gst_media_info_set_property;
  */
  g_object_class->get_property = gst_media_info_get_property;

  klass->media_info_signal = NULL;

  gst_media_info_signals [MEDIA_INFO_SIGNAL] =
    g_signal_new ("media-info",
		  G_TYPE_FROM_CLASS (klass),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GstMediaInfoClass, media_info_signal),
		  NULL, NULL,
		  gst_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
}

static void
gst_media_info_instance_init (GstMediaInfo *info)
{
  GstElement *source;

  info->priv = g_new0 (GstMediaInfoPriv, 1);

  info->priv->pipeline = gst_pipeline_new ("media-info");

  info->priv->typefind = gst_element_factory_make ("typefind", "typefind");
  if (!GST_IS_ELEMENT (info->priv->typefind))
	  /* FIXME */
    g_error ("Cannot create typefind element !");

  /* ref it so it never goes away on removal out of bins */
  gst_object_ref (GST_OBJECT (info->priv->typefind));

  /* use gnomevfssrc by default */
  source = gst_element_factory_make ("gnomevfssrc", "source");
  if (GST_IS_ELEMENT (source))
  {
    info->priv->source = source;
    info->priv->source_element = g_strdup ("gnomevfssrc");
    gst_bin_add (GST_BIN (info->priv->pipeline), info->priv->source);
  }
  else
  {
    info->priv->source = NULL;
    info->priv->source_element = NULL;
  }
  info->priv->decoder = NULL;
  info->priv->type = NULL;
  info->priv->format = NULL;
  info->priv->metadata = NULL;

  /* clear result pointers */
  info->priv->stream = NULL;

  /* set up decoder hash table */
  info->priv->decoders = g_hash_table_new (g_str_hash, g_str_equal);

  /* attach notify handler */
  g_signal_connect (G_OBJECT (info->priv->pipeline), "deep_notify",
		    G_CALLBACK (deep_notify_callback), info);
}

/* get/set */
static void
gst_media_info_get_property (GObject *object, guint prop_id,
			     GValue *value, GParamSpec *pspec)
{
  GstMediaInfo *info = GST_MEDIA_INFO (object);

  switch (prop_id)
  {
    case PROP_SOURCE:
      g_value_set_string (value, info->priv->source_element);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

276 277 278 279 280 281 282 283 284 285
GstMediaInfo *
gst_media_info_new (const gchar *source_element)
{
  GstMediaInfo *info = g_object_new (GST_MEDIA_INFO_TYPE, NULL);
  if (source_element)
    g_object_set (G_OBJECT (info), "source", source_element);

  return info;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
/**
 * private functions 
 */

/* callbacks */
static void
have_type_callback (GstElement *typefind, GstCaps *type, GstMediaInfo *info)
{
  info->priv->type = type;
}

static void
deep_notify_callback (GObject *object, GstObject *origin,
		      GParamSpec *pspec, GstMediaInfo *info)
{
  GValue value = { 0, };

  if (strcmp (pspec->name, "metadata") == 0)
  {
    GMI_DEBUG("DEBUG: deep_notify: have metadata !\n");
    g_value_init (&value, pspec->value_type);
    g_object_get_property (G_OBJECT (origin), pspec->name, &value);
    info->priv->metadata = g_value_peek_pointer (&value);
  }
  else if (strcmp (pspec->name, "caps") == 0)
  {
    /* check if we're getting it from the source we want it from */
    if (GST_IS_PAD (origin) && GST_PAD (origin) == info->priv->decoder_pad)
    {
      GMI_DEBUG("DEBUG: deep_notify: have caps on decoder_pad !\n");
      g_value_init (&value, pspec->value_type);
      g_object_get_property (G_OBJECT (origin), pspec->name, &value);
      info->priv->format = g_value_peek_pointer (&value);
    }
    else GMI_DEBUG("DEBUG: igoring caps on object %s:%s\n",
		   gst_object_get_name (gst_object_get_parent (origin)),
		   gst_object_get_name (origin));
  }
  else if (strcmp (pspec->name, "offset") == 0)
  {
    /* we REALLY ignore offsets, we hate them */
  }
  else if (strcmp (pspec->name, "streaminfo") == 0)
  {
    GMI_DEBUG("DEBUG: deep_notify: have streaminfo !\n");
    g_value_init (&value, pspec->value_type);
    g_object_get_property (G_OBJECT (origin), pspec->name, &value);
    info->priv->streaminfo = g_value_peek_pointer (&value);
  }
   else GMI_DEBUG("DEBUG: ignoring notify of %s\n", pspec->name);
}

/* helpers */

/* reset info to a state where it can be used to query for media info
 * clear caps, metadata, and so on */
static void
gst_media_info_reset (GstMediaInfo *info)
{
  GstMediaInfoPriv *priv = info->priv;
  /* clear out some stuff */
  if (priv->format)
  {
    gst_caps_unref (priv->format);
    priv->format = NULL;
  }
  if (priv->metadata)
  {
    gst_caps_unref (priv->metadata);
    priv->metadata = NULL;
  }
  if (priv->stream)
  {
    g_free (priv->stream);
    priv->stream = NULL;
  }
}

/* seek to a track and reset metadata and streaminfo structs */
static gboolean
gst_media_info_seek_to_track (GstMediaInfo *info, long track)
{
  GstEvent *event;
  GstFormat track_format = 0;
  GstMediaInfoPriv *priv = info->priv;
  gboolean res;

  /* FIXME: consider more nicks as "track" */
  track_format = gst_format_get_by_nick ("logical_stream");
  if (track_format == 0) return FALSE;
  GMI_DEBUG("Track format: %d\n", track_format);
  
  if (gst_element_set_state (priv->pipeline, GST_STATE_PLAYING) 
		            == GST_STATE_FAILURE)
    g_warning ("Couldn't set to play");
  g_assert (GST_IS_PAD (info->priv->decoder_pad));
  event = gst_event_new_seek (track_format |
		              GST_SEEK_METHOD_SET |
			      GST_SEEK_FLAG_FLUSH,
			      track);
  res = gst_pad_send_event (info->priv->decoder_pad, event);
  g_assert (res);
  if (!res) 
  {
    g_warning ("seek to logical track failed");
    return FALSE;
  }
  /* clear structs because of the seek */
  if (priv->metadata)
  {
    gst_caps_unref (priv->metadata);
    priv->metadata = NULL;
  }
  if (priv->streaminfo)
  {
    gst_caps_unref (priv->streaminfo);
    priv->streaminfo = NULL;
  }
  return TRUE;
}

/* create a good decoder for this mime type */
/* FIXME: maybe make this more generic with a type, so it can be used
 * for taggers and other things as well */
GstElement *
gst_media_info_get_decoder (GstMediaInfo *info, const char *mime)
{
  GstElement *decoder;
  gchar *factory = NULL;

  /* check if we have an active codec element in the hash table for this */
  decoder = g_hash_table_lookup (info->priv->decoders, mime);
  if (decoder == NULL)
  {
    GMI_DEBUG("DEBUG: no decoder in table, inserting one\n");
    if (strcmp (mime, "application/x-ogg") == 0)
      factory = g_strdup ("vorbisfile");
423
    else if (strcmp (mime, "audio/x-mp3") == 0)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
      factory = g_strdup ("mad");
    else if (strcmp (mime, "audio/x-wav") == 0)
      factory = g_strdup ("wavparse");

    if (factory == NULL)
      return NULL;

    GMI_DEBUG("DEBUG: using factory %s\n", factory);
    decoder = gst_element_factory_make (factory, "decoder");
    g_free (factory);

    if (decoder)
    {
      g_hash_table_insert (info->priv->decoders, g_strdup (mime), decoder);
      /* ref it so we don't lose it when removing from bin */
      g_object_ref (GST_OBJECT (decoder));
    }
  }
 
  return decoder;
}

/* find the type of a file and store it in the caps of the info
 * FIXME: we might better return GstCaps instead of storing them
 * internally */
static void
gst_media_info_find_type (GstMediaInfo *info, const char *location)
{
  GstMediaInfoPriv *priv = info->priv;

  /* clear vars that need clearing */
  if (priv->type)
  {
    gst_caps_unref (priv->type);
    priv->type = NULL;
  }

  GMI_DEBUG("DEBUG: gst_media_info_find_type: start\n");
  /* find out type */
  /* FIXME: we could move caps for typefind out of struct and
   * just use it through this function only */

  gst_bin_add (GST_BIN (priv->pipeline), priv->typefind);
  g_object_set (G_OBJECT (priv->source), "location", location, NULL);
468 469
  if (!gst_element_link (priv->source, priv->typefind))
    g_warning ("Couldn't link source and typefind\n");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
470 471 472 473 474 475 476 477 478 479 480 481 482
  g_signal_connect (G_OBJECT (priv->typefind), "have-type",
		    G_CALLBACK (have_type_callback), info);
  if (gst_element_set_state (priv->pipeline, GST_STATE_PLAYING) 
		            == GST_STATE_FAILURE)
    g_warning ("Couldn't set to play");
  GMI_DEBUG("DEBUG: gst_media_info_find_type: iterating\n");
  while ((priv->type == NULL) && 
	 gst_bin_iterate (GST_BIN (priv->pipeline)))
    GMI_DEBUG("+");
  GMI_DEBUG("\n");

  /*clear up typefind */
  gst_element_set_state (priv->pipeline, GST_STATE_READY);
483
  gst_element_unlink (priv->source, priv->typefind);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
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 524 525 526 527 528 529 530 531 532
  gst_bin_remove (GST_BIN (priv->pipeline), priv->typefind);
}
/* get properties of complete physical stream 
 * and return them in pre-alloced stream struct */
static gboolean
gst_media_info_get_stream (GstMediaInfo *info, GstMediaInfoStream *stream)
{
  GstMediaInfoPriv *priv = info->priv;
  const GstFormat *formats;
  GstFormat track_format = 0;
  GstFormat format;
  gint64 value;
  gboolean res;
  glong bytes = 0;

  GMI_DEBUG("DEBUG:gst_media_info_get_stream start\n");

  if (gst_element_set_state (priv->pipeline, GST_STATE_PLAYING) 
		            == GST_STATE_FAILURE)
    g_warning ("Couldn't set to play");
  /* iterate until caps are found */
  /* FIXME: this should be done through the plugin sending some signal
   * that it is ready for queries */
  while (gst_bin_iterate (GST_BIN (priv->pipeline)) &&
	 priv->format == NULL)
    ;
  if (gst_element_set_state (priv->pipeline, GST_STATE_PAUSED) 
		            == GST_STATE_FAILURE)
    g_warning ("Couldn't set to paused");
  if (priv->format == NULL)
  {
    GMI_DEBUG("DEBUG: gst_media_info_get_stream: couldn't get caps !");
    return FALSE;
  }

  /* find a format that matches the "track" concept */
  /* FIXME: this is used in vorbis, but we might have to loop when
   * more codecs have tracks */
  track_format = gst_format_get_by_nick ("logical_stream");

  /* get supported formats on decoder pad */
  formats = gst_pad_get_formats (priv->decoder_pad);
  while (*formats)
  {
    const GstFormatDefinition *definition;

    format = *formats;

    g_assert (GST_IS_PAD (priv->decoder_pad));
Wim Taymans's avatar
Wim Taymans committed
533
    res = gst_pad_query (priv->decoder_pad, GST_QUERY_TOTAL,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
534 535 536 537 538 539 540 541 542 543 544
                         &format, &value);

    definition = gst_format_get_details (*formats);
    GMI_DEBUG("trying to figure out length for format %s\n", definition->nick);

    if (res)
    {
      switch (format)
      {
        case GST_FORMAT_TIME:
          stream->length_time = value;
545
          g_print ("  total %s: %" G_GINT64_FORMAT "\n", definition->nick, value);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
546 547 548 549 550 551 552
	  break;
	default:
	  /* separation is necessary because track_format doesn't resolve to
	   * int */
	  if (format == track_format)
	  {
	    stream->length_tracks = value;
553
            g_print ("  total %s: %" G_GINT64_FORMAT "\n", definition->nick, value);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
554 555 556 557 558 559 560 561 562 563 564 565 566 567
	  }
	  else
	    g_print ("warning: unhandled format %s\n", definition->nick);
      }
    }
    else
      GMI_DEBUG("query didn't return result for %s\n", definition->nick);

    formats++;
  }
  if (stream->length_tracks == 0) stream->length_tracks = 1;
  /* now get number of bytes from the sink pad to get the bitrate */
  format = GST_FORMAT_BYTES;
  g_assert (GST_IS_PAD (priv->source_pad));
Wim Taymans's avatar
Wim Taymans committed
568
  res = gst_pad_query (priv->source_pad, GST_QUERY_TOTAL,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
                       &format, &value);
  if (!res) g_warning ("Failed to query on sink pad !");
  bytes = value;

  if (bytes)
  {
    double seconds = stream->length_time / GST_SECOND;
    double bits = bytes * 8;
    stream->bitrate = (long) (bits / seconds);
  }
  return TRUE;
}

/* find metadata encoded in media */
GstCaps *
gst_media_info_find_metadata (GstMediaInfo *info)
{
  GstMediaInfoPriv *priv = info->priv;
  GstCaps *metadata;

  if (gst_element_set_state (priv->pipeline, GST_STATE_PLAYING) 
		            == GST_STATE_FAILURE)
    g_warning ("Couldn't set to play");
  GMI_DEBUG("DEBUG: gst_media_info_find_metadata: iterating\n");
  while ((priv->metadata == NULL) && 
	 gst_bin_iterate (GST_BIN (priv->pipeline)))
    GMI_DEBUG("+");
  GMI_DEBUG("\n");
  gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
  metadata = priv->metadata;
  priv->metadata = NULL;
  return metadata;
}

/* find streaminfo found by decoder */
GstCaps *
gst_media_info_find_streaminfo (GstMediaInfo *info)
{
  GstMediaInfoPriv *priv = info->priv;
  GstCaps *streaminfo;
  GstFormat format, track_format;

  if (gst_element_set_state (priv->pipeline, GST_STATE_PLAYING) 
		            == GST_STATE_FAILURE)
    g_warning ("Couldn't set to play");
  GMI_DEBUG("DEBUG: gst_media_info_find_streaminfo: iterating\n");
  while ((priv->streaminfo == NULL) && 
	 gst_bin_iterate (GST_BIN (priv->pipeline)))
    GMI_DEBUG("+");
  GMI_DEBUG("\n");
  gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
  streaminfo = priv->streaminfo;
  priv->streaminfo = NULL;

  /* now add total length to this, and maybe even bitrate ? FIXME */
  track_format = gst_format_get_by_nick ("logical_stream");
  if (track_format == 0) 
  {
    g_print ("FIXME: implement getting length of whole track\n");
  }
  else
  {
    /* which one are we at ? */
    long track_num;
    gint64 value_start, value_end;
    gboolean res;

Wim Taymans's avatar
Wim Taymans committed
636
    res = gst_pad_query (priv->decoder_pad, GST_QUERY_POSITION,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
		         &track_format, &value_start);
    if (res)
    {
      format = GST_FORMAT_TIME;
      track_num = value_start;
      GMI_DEBUG("DEBUG: we are currently at %ld\n", track_num);
      res = gst_pad_convert  (priv->decoder_pad,
		              track_format, track_num,
			      &format, &value_start);
      res &= gst_pad_convert (priv->decoder_pad,
			      track_format, track_num + 1,
                              &format, &value_end);
      if (res) 
      {
	GstPropsEntry *length;
        /* substract to get the length */
653 654
	GMI_DEBUG("DEBUG: start %" G_GINT64_FORMAT ", end %"
		  G_GINT64_FORMAT "\n", value_start, value_end);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
655
	value_end -= value_start;
656
	g_print ("DEBUG: length: %d\n", (int) value_end);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
	length = gst_props_entry_new ("length", GST_PROPS_INT ((int) value_end));
	gst_props_add_entry (gst_caps_get_props (streaminfo), length);
      }
    }
  }
  
  return streaminfo;
}

/* find format found by decoder */
GstCaps *
gst_media_info_find_format (GstMediaInfo *info)
{
  GstMediaInfoPriv *priv = info->priv;
  GstCaps *format;

  if (gst_element_set_state (priv->pipeline, GST_STATE_PLAYING) 
		            == GST_STATE_FAILURE)
    g_warning ("Couldn't set to play");
  GMI_DEBUG("DEBUG: gst_media_info_find_format: iterating\n");
  while ((priv->format == NULL) && 
	 gst_bin_iterate (GST_BIN (priv->pipeline)))
    GMI_DEBUG("+");
  GMI_DEBUG("\n");
  gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
  format = priv->format;
  priv->format = NULL;
  return format;
}

/* clear the decoder
 * (if it was set)
 */
static void
gst_media_info_clear_decoder (GstMediaInfo *info)
{
  if (info->priv->decoder)
  {
    /* clear up decoder */
	  /* FIXME: shouldn't need to set state here */
    gst_element_set_state (info->priv->pipeline, GST_STATE_READY);
698
    gst_element_unlink (info->priv->source, info->priv->decoder);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
    gst_bin_remove (GST_BIN (info->priv->pipeline), info->priv->decoder);
    info->priv->decoder = NULL;
  }
}

/* set the decoder to be used for decoding
 * install callback handlers
 */

static void
gst_media_info_set_decoder (GstMediaInfo *info, GstElement *decoder)
{
  GstMediaInfoPriv *priv = info->priv;

  /* set up pipeline and connect signal handlers */
  priv->decoder = decoder;
  gst_bin_add (GST_BIN (priv->pipeline), decoder);
716 717 718
  if (!gst_element_link (priv->source, decoder))
    g_warning ("Couldn't link source and decoder\n");
  /* FIXME: we should be link to ALL possible src pads */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
  if (!(priv->decoder_pad = gst_element_get_pad (decoder, "src")))
    g_warning ("Couldn't get decoder pad\n");
  if (!(priv->source_pad = gst_element_get_pad (priv->source, "src")))
    g_warning ("Couldn't get source pad\n");
}

/**
 * public methods 
 */
gboolean
gst_media_info_set_source (GstMediaInfo *info, const char *source)
{
  GstElement *src;
  src = gst_element_factory_make (source, "new-source");
  if  (!GST_IS_ELEMENT (src))
    return FALSE;

  if (info->priv->source)
  {
    /* this also unrefs the element */
    gst_bin_remove (GST_BIN (info->priv->pipeline), info->priv->source);
    if (info->priv->source_element)
    {
      g_free (info->priv->source_element);
      info->priv->source_element = NULL;
    }
  }
  g_object_set (G_OBJECT (src), "name", "source", NULL);
  gst_bin_add (GST_BIN (info->priv->pipeline), src);
  info->priv->source = src;
  info->priv->source_element = g_strdup (source);

  return TRUE;
}

GstMediaInfoStream *
gst_media_info_read (GstMediaInfo *info, const char *location, guint16 flags)
{
  GstMediaInfoPriv *priv = info->priv;
  GstElement *decoder = NULL;
  const gchar *mime;
  GstMediaInfoStream *stream = NULL;
  int i;
  
  GMI_DEBUG("DEBUG: gst_media_info_read: start\n");
  gst_media_info_reset (info);		/* reset all structs */
  /* find out type */
  /* FIXME: we could move caps for typefind out of struct and
   * just use it through this function only */
  gst_media_info_find_type (info, location);

  if (priv->type == NULL)
  {
    /* iteration ended, still don't have type, ugh */
    g_warning ("iteration ended, type not found !\n");
    return NULL;
  }
  stream = gst_media_info_stream_new ();
  mime = gst_caps_get_mime (priv->type);
  if (flags & GST_MEDIA_INFO_MIME)
    stream->mime = g_strdup (mime);
  stream->path = g_strdup (location);
  GMI_DEBUG("mime type: %s\n", mime);
  decoder = gst_media_info_get_decoder (info, mime);

 /* if it's NULL, then that's a sign we can't decode it */
  if (decoder == NULL)
  {
    g_warning ("Can't find a decoder for type %s\n", mime);
    gst_media_info_stream_free (stream);
    return NULL;
  }
  /* install this decoder in the pipeline */
  gst_media_info_set_decoder (info, decoder);

  /* collect total stream properties */
  gst_media_info_get_stream (info, stream);

  /* if we have multiple tracks, loop over them; if not, just get
   * metadata and return it */
  GMI_DEBUG("DEBUG: num tracks %ld\n", stream->length_tracks);
  for (i = 0; i < stream->length_tracks; ++i)
  {
    GstMediaInfoTrack *track = gst_media_info_track_new ();
    if (i > 0)
    {
      GMI_DEBUG("seeking to track %d\n", i);
      gst_media_info_seek_to_track (info, i);
    }
    if (flags & GST_MEDIA_INFO_METADATA)
      track->metadata = gst_media_info_find_metadata (info);
    if (flags & GST_MEDIA_INFO_STREAMINFO)
      track->streaminfo = gst_media_info_find_streaminfo (info);
    if (flags & GST_MEDIA_INFO_FORMAT)
      track->format = gst_media_info_find_format (info);
    stream->tracks = g_list_append (stream->tracks, track);
  }

  gst_media_info_clear_decoder (info);
  /* please return it */
  return stream;
}


/*
 * FIXME: reset ?
gboolean	gst_media_info_write	(GstMediaInfo *media_info,
                                         const char *location,
					 GstCaps *media_info);
					 */