Commit 8517f3b3 authored by Thomas Vander Stichele's avatar Thomas Vander Stichele
Browse files

first pass at making this work again needs quite a lot of cleanups

Original commit message from CVS:
first pass at making this work again
needs quite a lot of cleanups
parent c22c7c69
2004-03-06 Thomas Vander Stichele <thomas at apestaart dot org>
* gst-libs/gst/media-info/media-info-priv.c: (have_type_callback),
(deep_notify_callback), (tag_flag_score), (found_tag_callback),
(error_callback), (gmi_reset), (gmi_seek_to_track),
(gmi_get_decoder), (gmi_set_mime), (gmip_find_type_pre),
(gmip_find_type_post), (gmip_find_stream_post),
(gmip_find_track_streaminfo_post):
* gst-libs/gst/media-info/media-info-priv.h:
* gst-libs/gst/media-info/media-info-test.c: (print_tag),
(info_print), (main):
* gst-libs/gst/media-info/media-info.c:
(gst_media_info_error_create), (gst_media_info_error_element),
(gst_media_info_instance_init), (gst_media_info_get_property),
(gst_media_info_new), (gst_media_info_set_source),
(gst_media_info_read_idler), (gst_media_info_read):
* gst-libs/gst/media-info/media-info.h:
first pass at making this work again. This seems to work on
tagged ogg/vorbis and mp3 files.
2004-03-06 Benjamin Otte <otte@gnome.org>
* ext/mad/gstid3tag.c: (gst_id3_tag_chain):
......
......@@ -80,9 +80,36 @@ gmi_track_new (void)
static void
have_type_callback (GstElement *typefind, guint probability, GstCaps *type, GstMediaInfoPriv *priv)
{
g_print ("DEBUG: have_type: caps %p\n", type);
GstStructure *str;
const gchar *mime;
priv->type = gst_caps_copy (type);
/* FIXME: make sure we _free these somewhere */
str = gst_caps_get_structure (type, 0);
mime = gst_structure_get_name (str);
GST_DEBUG ("caps %p, mime %s", type, mime);
/* FIXME: this code doesn't yet work, test it later */
#ifdef DONTWORK
if (strcmp (mime, "application/x-id3") == 0)
{
/* dig a little deeper */
GST_DEBUG ("dealing with id3, digging deeper");
gst_element_set_state (priv->pipeline, GST_STATE_READY);
gst_element_unlink (priv->source, priv->typefind);
g_assert (priv->decontainer == NULL);
priv->decontainer = gst_element_factory_make ("id3tag", "decontainer");
gst_bin_add (GST_BIN (priv->pipeline), priv->decontainer);
if (priv->decontainer == NULL)
/* FIXME: signal error */
g_warning ("Couldn't create id3tag");
if (!gst_element_link_many (priv->source, priv->decontainer, priv->typefind, NULL));
g_warning ("Couldn't link in id3tag");
if (gst_element_set_state (priv->pipeline, GST_STATE_PLAYING)
== GST_STATE_FAILURE)
g_warning ("Couldn't set to playing");
}
#endif
}
void
......@@ -94,26 +121,21 @@ deep_notify_callback (GObject *object, GstObject *origin,
/* we only care about pad notifies */
if (!GST_IS_PAD (origin)) return;
/*
GST_DEBUG ("DEBUG: deep_notify: have notify of %s from object %s:%s !",
pspec->name, gst_element_get_name (gst_pad_get_parent (GST_PAD (origin))),
gst_object_get_name (origin));
if (strcmp (pspec->name, "metadata") == 0)
{
GST_DEBUG ("DEBUG: deep_notify: have metadata !");
g_value_init (&value, pspec->value_type);
g_object_get_property (G_OBJECT (origin), pspec->name, &value);
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) == priv->decoder_pad)
/* check if we're getting it from fakesink */
if (GST_IS_PAD (origin) && GST_PAD_PARENT (origin) == priv->fakesink)
{
GST_DEBUG ("DEBUG: deep_notify: have caps on decoder_pad !");
GST_DEBUG ("have caps on fakesink pad !");
g_value_init (&value, pspec->value_type);
g_object_get_property (G_OBJECT (origin), pspec->name, &value);
priv->format = g_value_peek_pointer (&value);
GST_DEBUG ("caps: %" GST_PTR_FORMAT, priv->format);
}
else GST_DEBUG ("ignoring caps on object %s:%s",
gst_object_get_name (gst_object_get_parent (origin)),
......@@ -123,14 +145,57 @@ deep_notify_callback (GObject *object, GstObject *origin,
{
/* we REALLY ignore offsets, we hate them */
}
else if (strcmp (pspec->name, "streaminfo") == 0)
//else GST_DEBUG ("ignoring notify of %s", pspec->name);
}
typedef struct {
guint meta;
guint encoded;
} TagFlagScore;
static void
tag_flag_score (const GstTagList *list, const gchar *tag, gpointer user_data)
{
TagFlagScore *score = (TagFlagScore *) user_data;
GstTagFlag flag;
flag = gst_tag_get_flag (tag);
if (flag == GST_TAG_FLAG_META) score->meta++;
if (flag == GST_TAG_FLAG_ENCODED) score->encoded++;
}
void
found_tag_callback (GObject *pipeline, GstElement *source, GstTagList *tags, GstMediaInfoPriv *priv)
{
TagFlagScore score;
score.meta = 0;
score.encoded = 0;
g_print ("FOUND TAG : found by element \"%s\".\n",
GST_STR_NULL (GST_ELEMENT_NAME (source)));
/* decide if it's likely to be metadata or streaminfo */
/* FIXME: this is a hack, there must be a better way,
but as long as elements can report both mixed we need to do this */
gst_tag_list_foreach (tags, tag_flag_score, &score);
if (score.meta > score.encoded)
{
GST_DEBUG ("found tags from decoder, adding them as metadata");
priv->metadata = gst_tag_list_copy (tags);
}
else
{
GST_DEBUG ("deep_notify: have streaminfo !");
g_value_init (&value, pspec->value_type);
g_object_get_property (G_OBJECT (origin), pspec->name, &value);
priv->streaminfo = g_value_peek_pointer (&value);
GST_DEBUG ("found tags, adding them as streaminfo");
priv->streaminfo = gst_tag_list_copy (tags);
}
else GST_DEBUG ("ignoring notify of %s", pspec->name);
}
void
error_callback (GObject *element, GstElement *source, GError *error, gchar *debug)
{
g_print ("ERROR: %s\n", error->message);
}
/* helpers */
......@@ -151,7 +216,7 @@ gmi_reset (GstMediaInfo *info)
if (priv->metadata)
{
GMI_DEBUG ("unreffing priv->metadata, error before this ?\n");
gst_caps_free (priv->metadata);
gst_tag_list_free (priv->metadata);
priv->metadata = NULL;
}
if (priv->stream)
......@@ -202,20 +267,22 @@ gmi_seek_to_track (GstMediaInfo *info, long track)
/* clear structs because of the seek */
if (priv->metadata)
{
gst_caps_free (priv->metadata);
gst_tag_list_free (priv->metadata);
priv->metadata = NULL;
}
if (priv->streaminfo)
{
gst_caps_free (priv->streaminfo);
gst_tag_list_free (priv->streaminfo);
priv->streaminfo = NULL;
}
return TRUE;
}
#ifdef REMOVEME
/* 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 */
/* FIXME: deprecated, we work with pipelines now */
GstElement *
gmi_get_decoder (GstMediaInfo *info, const char *mime)
{
......@@ -234,6 +301,7 @@ gmi_get_decoder (GstMediaInfo *info, const char *mime)
else if ((strcmp (mime, "audio/mpeg") == 0) ||
(strcmp (mime, "audio/x-mp3") == 0) ||
(strcmp (mime, "audio/mp3") == 0) ||
(strcmp (mime, "application/x-id3") == 0) ||
(strcmp (mime, "audio/x-id3") == 0))
factory = g_strdup ("mad");
else if (strcmp (mime, "application/x-flac") == 0)
......@@ -303,6 +371,72 @@ gmi_clear_decoder (GstMediaInfo *info)
info->priv->decoder = NULL;
}
}
#endif
/* set the mime type on the media info getter */
gboolean
gmi_set_mime (GstMediaInfo *info, const char *mime)
{
gchar *desc = NULL;
GError *error = NULL;
GstMediaInfoPriv *priv = info->priv;
/* FIXME: please figure out proper mp3 mimetypes */
if ((strcmp (mime, "application/x-ogg") == 0) ||
(strcmp (mime, "application/ogg") == 0))
desc = g_strdup_printf ("%s name=source ! oggdemux ! vorbisdec name=decoder ! audioconvert ! audio/x-raw-int ! fakesink name=sink", priv->source_name);
else if ((strcmp (mime, "audio/mpeg") == 0) ||
(strcmp (mime, "audio/x-mp3") == 0) ||
(strcmp (mime, "audio/mp3") == 0) ||
(strcmp (mime, "application/x-id3") == 0) ||
(strcmp (mime, "audio/x-id3") == 0))
desc = g_strdup_printf ("%s name=source ! id3tag ! mad name=decoder ! audio/x-raw-int ! fakesink name=sink", priv->source_name);
else if (strcmp (mime, "application/x-flac") == 0)
desc = g_strdup_printf ("%s name=source ! flac name=decoder ! audio/x-raw-int ! fakesink name=sink", priv->source_name);
else if (strcmp (mime, "audio/x-wav") == 0)
desc = g_strdup_printf ("%s ! wavparse name=decoder ! audio/x-raw-int ! fakesink name=sink", priv->source_name);
else if (strcmp (mime, "audio/x-mod") == 0 ||
strcmp (mime, "audio/x-s3m") == 0 ||
strcmp (mime, "audio/x-xm") == 0 ||
strcmp (mime, "audio/x-it") == 0)
desc = g_strdup_printf ("%s name=source ! modplug name=decoder ! audio/x-raw-int ! fakesink name=sink", priv->source_name);
GST_DEBUG ("using description %s", desc);
priv->pipeline_desc = desc;
priv->pipeline = gst_parse_launch (desc, &error);
if (error)
{
g_warning ("Error parsing pipeline description: %s\n", error->message);
g_error_free (error);
return FALSE;
}
/* get a bunch of elements from the bin */
priv->source = gst_bin_get_by_name (GST_BIN (priv->pipeline), "source");
g_assert (GST_IS_ELEMENT (priv->source));
g_object_set (G_OBJECT (priv->source), "location", priv->location, NULL);
priv->decoder = gst_bin_get_by_name (GST_BIN (priv->pipeline), "decoder");
g_assert (GST_IS_ELEMENT (priv->decoder));
priv->fakesink = gst_bin_get_by_name (GST_BIN (priv->pipeline), "sink");
g_assert (GST_IS_ELEMENT (priv->fakesink));
/* get the "source " source pad */
priv->source_pad = gst_element_get_pad (priv->source, "src");
g_assert (GST_IS_PAD (priv->source_pad));
/* get the "decoder" source pad */
priv->decoder_pad = gst_element_get_pad (priv->decoder, "src");
g_assert (GST_IS_PAD (priv->decoder_pad));
GST_DEBUG ("decoder pad: %s:%s", gst_object_get_name (gst_object_get_parent (GST_OBJECT (priv->decoder_pad))), gst_pad_get_name (priv->decoder_pad));
/* attach notify handler */
g_signal_connect (G_OBJECT (info->priv->pipeline), "deep_notify",
G_CALLBACK (deep_notify_callback), info->priv);
g_signal_connect (G_OBJECT (info->priv->pipeline), "found-tag", G_CALLBACK (found_tag_callback), info->priv);
g_signal_connect (G_OBJECT (info->priv->pipeline), "error",
G_CALLBACK (error_callback), info->priv);
return TRUE;
}
/****
* typefind functions
......@@ -321,6 +455,9 @@ gmip_find_type_pre (GstMediaInfoPriv *priv)
priv->type = NULL;
}
g_assert (priv);
g_assert (priv->source);
GST_DEBUG ("gmip_find_type_pre: start");
/* find out type */
/* FIXME: we could move caps for typefind out of struct and
......@@ -338,6 +475,7 @@ gmip_find_type_pre (GstMediaInfoPriv *priv)
g_warning ("Couldn't set to play");
return FALSE;
}
GST_DEBUG ("moving to STATE_TYPEFIND\n");
priv->state = GST_MEDIA_INFO_STATE_TYPEFIND;
return TRUE;
}
......@@ -348,7 +486,16 @@ gmip_find_type_post (GstMediaInfoPriv *priv)
{
/*clear up typefind */
gst_element_set_state (priv->pipeline, GST_STATE_READY);
gst_element_unlink (priv->source, priv->typefind);
if (priv->decontainer)
{
gst_element_unlink (priv->source, priv->decontainer);
gst_element_unlink (priv->decontainer, priv->typefind);
gst_bin_remove (GST_BIN (priv->pipeline), priv->decontainer);
}
else
{
gst_element_unlink (priv->source, priv->typefind);
}
gst_bin_remove (GST_BIN (priv->pipeline), priv->typefind);
if (priv->type == NULL)
......@@ -356,6 +503,7 @@ gmip_find_type_post (GstMediaInfoPriv *priv)
g_warning ("iteration ended, type not found !\n");
return FALSE;
}
GST_DEBUG ("moving to STATE_STREAM\n");
priv->state = GST_MEDIA_INFO_STATE_STREAM;
return TRUE;
}
......@@ -450,6 +598,7 @@ gmip_find_stream_post (GstMediaInfoPriv *priv)
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));
......@@ -465,6 +614,7 @@ gmip_find_stream_post (GstMediaInfoPriv *priv)
double bits = bytes * 8;
stream->bitrate = (long) (bits / seconds);
}
GST_DEBUG ("moving to STATE_METADATA\n");
priv->state = GST_MEDIA_INFO_STATE_METADATA; /* metadata of first track */
return TRUE;
}
......@@ -537,7 +687,7 @@ gmip_find_track_metadata (GstMediaInfoPriv *priv)
}
/* find streaminfo found by decoder and store in priv->streaminfo */
/* FIXME: this is an exact copy, so reuse this functioin instead */
/* FIXME: this is an exact copy, so reuse this function instead */
gboolean
gmip_find_track_streaminfo_pre (GstMediaInfoPriv *priv)
{
......@@ -590,8 +740,8 @@ gmip_find_track_streaminfo_post (GstMediaInfoPriv *priv)
value_end -= value_start;
/* FIXME: check units; this is in seconds */
gst_caps_set_simple (priv->streaminfo,
"length", G_TYPE_INT, (int) (value_end / 1E6), NULL);
gst_tag_list_add (priv->streaminfo, GST_TAG_MERGE_REPLACE,
GST_TAG_DURATION, (int) (value_end / 1E6), NULL);
}
}
}
......
......@@ -63,26 +63,25 @@ typedef enum
/* private structure */
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;
GstTagList *metadata;
gint metadata_iters;
GstCaps *streaminfo;
GstTagList *streaminfo;
GstElement *decoder; /* will be != NULL during collection */
GstElement *pipeline; /* will be != NULL during collection */
gchar *pipeline_desc; /* will be != NULL during collection */
GstElement *fakesink; /* so we can get caps from the
decoder sink pad */
gchar *source_element; /* type of element used as source */
gchar *source_name; /* type of element used as source */
GstElement *source;
GHashTable *decoders; /* a table of decoder GstElements */
GstPad *source_pad; /* pad for querying encoded caps */
GstElement *decoder;
GstPad *decoder_pad; /* pad for querying decoded caps */
GstElement *decontainer; /* element to typefind in containers */
GstMediaInfoState state; /* current state of state machine */
gchar *location; /* location set on the info object */
......@@ -92,6 +91,8 @@ struct GstMediaInfoPriv
GstMediaInfoStream *stream; /* total stream properties */
char *cache; /* location of cache */
GError *error; /* error for creation problems */
};
/* declarations */
......@@ -106,16 +107,15 @@ void gmi_reset (GstMediaInfo *info);
gboolean gmi_seek_to_track (GstMediaInfo *info,
long track);
GstElement * gmi_get_decoder (GstMediaInfo *info,
gboolean gmi_set_mime (GstMediaInfo *info,
const char *mime);
void gmi_set_decoder (GstMediaInfo *info,
GstElement *decoder);
void gmi_clear_decoder (GstMediaInfo *info);
void deep_notify_callback (GObject *object,
GstObject *origin,
GParamSpec *pspec,
GstMediaInfoPriv *priv);
void found_tag_callback (GObject *pipeline, GstElement *source, GstTagList *tags, GstMediaInfoPriv *priv);
void error_callback (GObject *element, GstElement *source, GError *error, gchar *debug);
gboolean gmip_find_type_pre (GstMediaInfoPriv *priv);
gboolean gmip_find_type_post (GstMediaInfoPriv *priv);
......
......@@ -4,6 +4,33 @@
#include <string.h>
#include "media-info.h"
static void
print_tag (const GstTagList *list, const gchar *tag, gpointer unused)
{
gint i, count;
count = gst_tag_list_get_tag_size (list, tag);
for (i = 0; i < count; i++) {
gchar *str;
if (gst_tag_get_type (tag) == G_TYPE_STRING) {
g_assert (gst_tag_list_get_string_index (list, tag, i, &str));
} else {
str = g_strdup_value_contents (
gst_tag_list_get_value_index (list, tag, i));
}
if (i == 0) {
g_print ("%15s: %s\n", gst_tag_get_nick (tag), str);
} else {
g_print (" : %s\n", str);
}
g_free (str);
}
}
static void
info_print (GstMediaInfoStream *stream)
{
......@@ -27,9 +54,12 @@ info_print (GstMediaInfoStream *stream)
g_print ("- track %d\n", i);
track = (GstMediaInfoTrack *) p->data;
g_print (" - metadata:\n");
g_print ("%s\n", gst_caps_to_string (track->metadata));
if (track->metadata)
gst_tag_list_foreach (track->metadata, print_tag, NULL);
else
g_print (" (none found)\n");
g_print (" - streaminfo:\n");
g_print ("%s\n", gst_caps_to_string (track->streaminfo));
gst_tag_list_foreach (track->streaminfo, print_tag, NULL);
g_print (" - format:\n");
g_print ("%s\n", gst_caps_to_string (track->format));
p = p->next;
......@@ -41,14 +71,29 @@ main (int argc, char *argv[])
{
GstMediaInfo *info;
GstMediaInfoStream *stream = NULL;
GError *error = NULL;
gint i;
g_assert (argc > 1);
gst_init (&argc, &argv);
gst_media_info_init ();
gst_init (&argc, &argv);
info = gst_media_info_new (&error);
if (error != NULL)
{
g_print ("Error creating media-info object: %s\n", error->message);
g_error_free (error);
return -1;
}
g_assert (G_IS_OBJECT (info));
if (!gst_media_info_set_source (info, "gnomevfssrc"))
{
g_print ("Could not set gnomevfssrc as a source\n");
return -1;
}
info = g_object_new (GST_MEDIA_INFO_TYPE, NULL);
g_print ("stream: %p, &stream: %p\n", stream, &stream);
for (i = 1; i < argc; ++i)
{
......
/* GStreamer
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
/* GStreamer media-info library
* Copyright (C) 2003,2004 Thomas Vander Stichele <thomas@apestaart.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
......@@ -48,15 +48,16 @@ enum
static guint gst_media_info_signals[LAST_SIGNAL] = { 0 };
/* GError stuff */
/*
* all GError stuff
*/
enum
{
MEDIA_INFO_ERROR_FILE
};
*/
/* GError quark stuff */
/*
static GQuark
gst_media_info_error_quark (void)
{
......@@ -65,7 +66,53 @@ gst_media_info_error_quark (void)
quark = g_quark_from_static_string ("gst-media-info-error-quark");
return quark;
}
*/
/* General GError creation */
static void
gst_media_info_error_create (GError **error, const gchar *message)
{
/* check if caller wanted an error reported */
if (error == NULL)
return;
*error = g_error_new (GST_MEDIA_INFO_ERROR, 0, message);
return;
}
/* GError creation when element is missing */
static void
gst_media_info_error_element (const gchar *element, GError **error)
{
gchar *message;
message = g_strdup_printf ("The %s element could not be found. "
"This element is essential for reading. "
"Please install the right plug-in and verify "
"that it works by running 'gst-inspect %s'",
element, element);
gst_media_info_error_create (error, message);
g_free (message);
return;
}
/* used from the instance_init function to report errors */
#define GST_MEDIA_INFO_MAKE_OR_ERROR(el, factory, name, error) \
G_STMT_START { \
el = gst_element_factory_make (factory, name); \
if (!GST_IS_ELEMENT (el)) \
{ \
gst_media_info_error_element (factory, error); \
return; \
} \
} G_STMT_END
#define GST_MEDIA_INFO_ERROR_RETURN(error, message) \
G_STMT_START { \
gst_media_info_error_create (error, message); \
return FALSE; \
} G_STMT_END
/*
* GObject type stuff
*/
......@@ -78,6 +125,8 @@ enum
static GObjectClass *parent_class = NULL;
GST_DEBUG_CATEGORY (gst_media_info_debug);
/* initialize the media-info library */
void
gst_media_info_init (void)
{
......@@ -145,53 +194,35 @@ gst_media_info_class_init (GstMediaInfoClass *klass)
static void
gst_media_info_instance_init (GstMediaInfo *info)
{
GstElement *source;
GError **error;
info->priv = g_new0 (GstMediaInfoPriv, 1);
error = &info->priv->error;
info->priv->pipeline = gst_pipeline_new ("media-info");
/* create the typefind element and make sure it stays around by reffing */
info->priv->typefind = gst_element_factory_make ("typefind", "typefind");
if (!GST_IS_ELEMENT (info->priv->typefind))
g_error ("Cannot create typefind element !");
GST_MEDIA_INFO_MAKE_OR_ERROR (info->priv->typefind, "typefind", "typefind", error);
gst_object_ref (GST_OBJECT (info->priv->typefind));
/* create the fakesink element and make sure it stays around by reffing */
info->priv->fakesink = gst_element_factory_make ("fakesink", "fakesink");
if (!GST_IS_ELEMENT (info->priv->fakesink))
g_error ("Cannot create fakesink element !");
GST_MEDIA_INFO_MAKE_OR_ERROR (info->priv->fakesink, "fakesink", "fakesink", error);
gst_object_ref (GST_OBJECT (info->priv->fakesink));
/* source element for media info reading */
info->priv->source = NULL;
info->priv->source_name = NULL;