diff --git a/gst/flv/Makefile.am b/gst/flv/Makefile.am index e8059cb4d7..d4124a917e 100644 --- a/gst/flv/Makefile.am +++ b/gst/flv/Makefile.am @@ -7,7 +7,9 @@ libgstflv_la_LDFLAGS = ${GST_PLUGIN_LDFLAGS} libgstflv_la_SOURCES = gstflvdemux.c gstflvmux.c libgstflv_la_LIBTOOLFLAGS = --tag=disable-static -noinst_HEADERS = gstflvdemux.h gstflvmux.h amfdefs.h +noinst_HEADERS = gstflvdemux.h gstflvmux.h amfdefs.h gstindex.h + +EXTRA_DIST = gstindex.c gstmemindex.c Android.mk: Makefile.am $(BUILT_SOURCES) androgenizer \ diff --git a/gst/flv/gstflvdemux.c b/gst/flv/gstflvdemux.c index b4e5669dd7..9921eb2aff 100644 --- a/gst/flv/gstflvdemux.c +++ b/gst/flv/gstflvdemux.c @@ -42,6 +42,13 @@ #include #include +/* FIXME: don't rely on own GstIndex */ +#include "gstindex.c" +#include "gstmemindex.c" +#define GST_ASSOCIATION_FLAG_NONE GST_INDEX_ASSOCIATION_FLAG_NONE +#define GST_ASSOCIATION_FLAG_KEY_UNIT GST_INDEX_ASSOCIATION_FLAG_KEY_UNIT +#define GST_ASSOCIATION_FLAG_DELTA_UNIT GST_INDEX_ASSOCIATION_FLAG_DELTA_UNIT + static GstStaticPadTemplate flv_sink_template = GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, @@ -3079,7 +3086,7 @@ gst_flv_demux_change_state (GstElement * element, GstStateChange transition) if (G_UNLIKELY (!demux->index)) { GST_DEBUG_OBJECT (demux, "no index provided creating our own"); - demux->index = gst_index_factory_make ("memindex"); + demux->index = g_object_new (gst_mem_index_get_type (), NULL); gst_index_get_writer_id (demux->index, GST_OBJECT (demux), &demux->index_id); @@ -3106,6 +3113,7 @@ gst_flv_demux_change_state (GstElement * element, GstStateChange transition) return ret; } +#if 0 static void gst_flv_demux_set_index (GstElement * element, GstIndex * index) { @@ -3142,6 +3150,7 @@ gst_flv_demux_get_index (GstElement * element) return result; } +#endif static void gst_flv_demux_dispose (GObject * object) @@ -3214,8 +3223,11 @@ gst_flv_demux_class_init (GstFlvDemuxClass * klass) gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_flv_demux_change_state); + +#if 0 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_flv_demux_set_index); gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_flv_demux_get_index); +#endif gst_element_class_add_pad_template (gstelement_class, gst_static_pad_template_get (&flv_sink_template)); diff --git a/gst/flv/gstflvdemux.h b/gst/flv/gstflvdemux.h index efbed316cf..c4684ed843 100644 --- a/gst/flv/gstflvdemux.h +++ b/gst/flv/gstflvdemux.h @@ -22,6 +22,7 @@ #include #include +#include "gstindex.h" G_BEGIN_DECLS #define GST_TYPE_FLV_DEMUX \ diff --git a/gst/flv/gstindex.c b/gst/flv/gstindex.c new file mode 100644 index 0000000000..c8a0b1da00 --- /dev/null +++ b/gst/flv/gstindex.c @@ -0,0 +1,1016 @@ +/* GStreamer + * Copyright (C) 2001 RidgeRun (http://www.ridgerun.com/) + * Written by Erik Walthinsen + * + * gstindex.c: Index for mappings and other data + * + * 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. + */ + +/** + * SECTION:gstindex + * @short_description: Generate indexes on objects + * @see_also: #GstIndexFactory + * + * GstIndex is used to generate a stream index of one or more elements + * in a pipeline. + * + * Elements will overload the set_index and get_index virtual methods in + * #GstElement. When streaming data, the element will add index entries if it + * has an index set. + * + * Each element that adds to the index will do that using a writer_id. The + * writer_id is obtained from gst_index_get_writer_id(). + * + * The application that wants to index the stream will create a new index object + * using gst_index_new() or gst_index_factory_make(). The index is assigned to a + * specific element, a bin or the whole pipeline. This will cause indexable + * elements to add entires to the index while playing. + */ + +/* FIXME: complete gobject annotations */ +/* FIXME-0.11: cleanup API + * - no one seems to use GstIndexGroup, GstIndexCertainty + * + * - the API for application to use the index is mostly missing + * - apps need to get a list of writers + * - apps need to be able to iterate over each writers index entry collection + * - gst_index_get_assoc_entry() should pass ownership + * - the GstIndexEntry structure is large and contains repetitive information + * - we want to allow Indexers to implement a saner storage and create + * GstIndexEntries on demand (the app has to free them), might even make + * sense to ask the app to provide a ptr and fill it. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +/* Index signals and args */ +enum +{ + ENTRY_ADDED, + LAST_SIGNAL +}; + +enum +{ + ARG_0, + ARG_RESOLVER + /* FILL ME */ +}; + +#if 0 +GST_DEBUG_CATEGORY_STATIC (index_debug); +#define GST_CAT_DEFAULT index_debug +#endif + +static void gst_index_finalize (GObject * object); + +static void gst_index_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec); +static void gst_index_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec); + +static GstIndexGroup *gst_index_group_new (guint groupnum); +static void gst_index_group_free (GstIndexGroup * group); + +static gboolean gst_index_path_resolver (GstIndex * index, GstObject * writer, + gchar ** writer_string, gpointer data); +static gboolean gst_index_gtype_resolver (GstIndex * index, GstObject * writer, + gchar ** writer_string, gpointer data); +static void gst_index_add_entry (GstIndex * index, GstIndexEntry * entry); + +static guint gst_index_signals[LAST_SIGNAL] = { 0 }; + +typedef struct +{ + GstIndexResolverMethod method; + GstIndexResolver resolver; + gpointer user_data; +} +ResolverEntry; + +static const ResolverEntry resolvers[] = { + {GST_INDEX_RESOLVER_CUSTOM, NULL, NULL}, + {GST_INDEX_RESOLVER_GTYPE, gst_index_gtype_resolver, NULL}, + {GST_INDEX_RESOLVER_PATH, gst_index_path_resolver, NULL}, +}; + +#define GST_TYPE_INDEX_RESOLVER (gst_index_resolver_get_type()) +static GType +gst_index_resolver_get_type (void) +{ + static GType index_resolver_type = 0; + static const GEnumValue index_resolver[] = { + {GST_INDEX_RESOLVER_CUSTOM, "GST_INDEX_RESOLVER_CUSTOM", "custom"}, + {GST_INDEX_RESOLVER_GTYPE, "GST_INDEX_RESOLVER_GTYPE", "gtype"}, + {GST_INDEX_RESOLVER_PATH, "GST_INDEX_RESOLVER_PATH", "path"}, + {0, NULL, NULL}, + }; + + if (!index_resolver_type) { + index_resolver_type = + g_enum_register_static ("GstIndexResolver", index_resolver); + } + return index_resolver_type; +} + +GType +gst_index_entry_get_type (void) +{ + static GType index_entry_type = 0; + + if (!index_entry_type) { + index_entry_type = g_boxed_type_register_static ("GstIndexEntry", + (GBoxedCopyFunc) gst_index_entry_copy, + (GBoxedFreeFunc) gst_index_entry_free); + } + return index_entry_type; +} + +#if 0 +#define _do_init \ +{ \ + GST_DEBUG_CATEGORY_INIT (index_debug, "GST_INDEX", GST_DEBUG_BOLD, \ + "Generic indexing support"); \ +} +#endif + +G_DEFINE_TYPE (GstIndex, gst_index, GST_TYPE_OBJECT); + +static void +gst_index_class_init (GstIndexClass * klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + + /** + * GstIndex::entry-added + * @gstindex: the object which received the signal. + * @arg1: The entry added to the index. + * + * Is emitted when a new entry is added to the index. + */ + gst_index_signals[ENTRY_ADDED] = + g_signal_new ("entry-added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (GstIndexClass, entry_added), NULL, NULL, + gst_marshal_VOID__BOXED, G_TYPE_NONE, 1, GST_TYPE_INDEX_ENTRY); + + gobject_class->set_property = gst_index_set_property; + gobject_class->get_property = gst_index_get_property; + gobject_class->finalize = gst_index_finalize; + + g_object_class_install_property (gobject_class, ARG_RESOLVER, + g_param_spec_enum ("resolver", "Resolver", + "Select a predefined object to string mapper", + GST_TYPE_INDEX_RESOLVER, GST_INDEX_RESOLVER_PATH, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); +} + +static void +gst_index_init (GstIndex * index) +{ + index->curgroup = gst_index_group_new (0); + index->maxgroup = 0; + index->groups = g_list_prepend (NULL, index->curgroup); + + index->writers = g_hash_table_new (NULL, NULL); + index->last_id = 0; + + index->method = GST_INDEX_RESOLVER_PATH; + index->resolver = resolvers[index->method].resolver; + index->resolver_user_data = resolvers[index->method].user_data; + + GST_OBJECT_FLAG_SET (index, GST_INDEX_WRITABLE); + GST_OBJECT_FLAG_SET (index, GST_INDEX_READABLE); + + GST_DEBUG ("created new index"); +} + +static void +gst_index_free_writer (gpointer key, gpointer value, gpointer user_data) +{ + GstIndexEntry *entry = (GstIndexEntry *) value; + + if (entry) { + gst_index_entry_free (entry); + } +} + +static void +gst_index_finalize (GObject * object) +{ + GstIndex *index = GST_INDEX (object); + + if (index->groups) { + g_list_foreach (index->groups, (GFunc) gst_index_group_free, NULL); + g_list_free (index->groups); + index->groups = NULL; + } + + if (index->writers) { + g_hash_table_foreach (index->writers, gst_index_free_writer, NULL); + g_hash_table_destroy (index->writers); + index->writers = NULL; + } + + if (index->filter_user_data && index->filter_user_data_destroy) + index->filter_user_data_destroy (index->filter_user_data); + + if (index->resolver_user_data && index->resolver_user_data_destroy) + index->resolver_user_data_destroy (index->resolver_user_data); + + G_OBJECT_CLASS (gst_index_parent_class)->finalize (object); +} + +static void +gst_index_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec) +{ + GstIndex *index; + + index = GST_INDEX (object); + + switch (prop_id) { + case ARG_RESOLVER: + index->method = (GstIndexResolverMethod) g_value_get_enum (value); + index->resolver = resolvers[index->method].resolver; + index->resolver_user_data = resolvers[index->method].user_data; + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_index_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec) +{ + GstIndex *index; + + index = GST_INDEX (object); + + switch (prop_id) { + case ARG_RESOLVER: + g_value_set_enum (value, index->method); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static GstIndexGroup * +gst_index_group_new (guint groupnum) +{ + GstIndexGroup *indexgroup = g_slice_new (GstIndexGroup); + + indexgroup->groupnum = groupnum; + indexgroup->entries = NULL; + indexgroup->certainty = GST_INDEX_UNKNOWN; + indexgroup->peergroup = -1; + + GST_DEBUG ("created new index group %d", groupnum); + + return indexgroup; +} + +static void +gst_index_group_free (GstIndexGroup * group) +{ + g_slice_free (GstIndexGroup, group); +} + +/* do not resurrect this, add a derived dummy index class instead */ +#if 0 +/** + * gst_index_new: + * + * Create a new dummy index object. Use gst_element_set_index() to assign that + * to an element or pipeline. This index is not storing anything, but will + * still emit e.g. the #GstIndex::entry-added signal. + * + * Returns: (transfer full): a new index object + */ +GstIndex * +gst_index_new (void) +{ + GstIndex *index; + + index = g_object_newv (gst_index_get_type (), 0, NULL); + + return index; +} +#endif + +/** + * gst_index_commit: + * @index: the index to commit + * @id: the writer that commited the index + * + * Tell the index that the writer with the given id is done + * with this index and is not going to write any more entries + * to it. + */ +void +gst_index_commit (GstIndex * index, gint id) +{ + GstIndexClass *iclass; + + iclass = GST_INDEX_GET_CLASS (index); + + if (iclass->commit) + iclass->commit (index, id); +} + +#if 0 +/** + * gst_index_get_group: + * @index: the index to get the current group from + * + * Get the id of the current group. + * + * Returns: the id of the current group. + */ +gint +gst_index_get_group (GstIndex * index) +{ + return index->curgroup->groupnum; +} + +/** + * gst_index_new_group: + * @index: the index to create the new group in + * + * Create a new group for the given index. It will be + * set as the current group. + * + * Returns: the id of the newly created group. + */ +gint +gst_index_new_group (GstIndex * index) +{ + index->curgroup = gst_index_group_new (++index->maxgroup); + index->groups = g_list_append (index->groups, index->curgroup); + GST_DEBUG ("created new group %d in index", index->maxgroup); + return index->maxgroup; +} + +/** + * gst_index_set_group: + * @index: the index to set the new group in + * @groupnum: the groupnumber to set + * + * Set the current groupnumber to the given argument. + * + * Returns: TRUE if the operation succeeded, FALSE if the group + * did not exist. + */ +gboolean +gst_index_set_group (GstIndex * index, gint groupnum) +{ + GList *list; + GstIndexGroup *indexgroup; + + /* first check for null change */ + if (groupnum == index->curgroup->groupnum) + return TRUE; + + /* else search for the proper group */ + list = index->groups; + while (list) { + indexgroup = (GstIndexGroup *) (list->data); + list = g_list_next (list); + if (indexgroup->groupnum == groupnum) { + index->curgroup = indexgroup; + GST_DEBUG ("switched to index group %d", indexgroup->groupnum); + return TRUE; + } + } + + /* couldn't find the group in question */ + GST_DEBUG ("couldn't find index group %d", groupnum); + return FALSE; +} +#endif + +#if 0 +/** + * gst_index_set_certainty: + * @index: the index to set the certainty on + * @certainty: the certainty to set + * + * Set the certainty of the given index. + */ +void +gst_index_set_certainty (GstIndex * index, GstIndexCertainty certainty) +{ + index->curgroup->certainty = certainty; +} + +/** + * gst_index_get_certainty: + * @index: the index to get the certainty of + * + * Get the certainty of the given index. + * + * Returns: the certainty of the index. + */ +GstIndexCertainty +gst_index_get_certainty (GstIndex * index) +{ + return index->curgroup->certainty; +} +#endif + +#if 0 +/** + * gst_index_set_filter: + * @index: the index to register the filter on + * @filter: the filter to register + * @user_data: data passed to the filter function + * + * Lets the app register a custom filter function so that + * it can select what entries should be stored in the index. + */ +void +gst_index_set_filter (GstIndex * index, + GstIndexFilter filter, gpointer user_data) +{ + g_return_if_fail (GST_IS_INDEX (index)); + + gst_index_set_filter_full (index, filter, user_data, NULL); +} + +/** + * gst_index_set_filter_full: + * @index: the index to register the filter on + * @filter: the filter to register + * @user_data: data passed to the filter function + * @user_data_destroy: function to call when @user_data is unset + * + * Lets the app register a custom filter function so that + * it can select what entries should be stored in the index. + */ +void +gst_index_set_filter_full (GstIndex * index, + GstIndexFilter filter, gpointer user_data, GDestroyNotify user_data_destroy) +{ + g_return_if_fail (GST_IS_INDEX (index)); + + if (index->filter_user_data && index->filter_user_data_destroy) + index->filter_user_data_destroy (index->filter_user_data); + + index->filter = filter; + index->filter_user_data = user_data; + index->filter_user_data_destroy = user_data_destroy; +} + +/** + * gst_index_set_resolver: + * @index: the index to register the resolver on + * @resolver: the resolver to register + * @user_data: data passed to the resolver function + * + * Lets the app register a custom function to map index + * ids to writer descriptions. + */ +void +gst_index_set_resolver (GstIndex * index, + GstIndexResolver resolver, gpointer user_data) +{ + gst_index_set_resolver_full (index, resolver, user_data, NULL); +} + +/** + * gst_index_set_resolver_full: + * @index: the index to register the resolver on + * @resolver: the resolver to register + * @user_data: data passed to the resolver function + * @user_data_destroy: destroy function for @user_data + * + * Lets the app register a custom function to map index + * ids to writer descriptions. + * + * Since: 0.10.18 + */ +void +gst_index_set_resolver_full (GstIndex * index, GstIndexResolver resolver, + gpointer user_data, GDestroyNotify user_data_destroy) +{ + g_return_if_fail (GST_IS_INDEX (index)); + + if (index->resolver_user_data && index->resolver_user_data_destroy) + index->resolver_user_data_destroy (index->resolver_user_data); + + index->resolver = resolver; + index->resolver_user_data = user_data; + index->resolver_user_data_destroy = user_data_destroy; + index->method = GST_INDEX_RESOLVER_CUSTOM; +} +#endif + +/** + * gst_index_entry_copy: + * @entry: the entry to copy + * + * Copies an entry and returns the result. + * + * Free-function: gst_index_entry_free + * + * Returns: (transfer full): a newly allocated #GstIndexEntry. + */ +GstIndexEntry * +gst_index_entry_copy (GstIndexEntry * entry) +{ + GstIndexEntry *new_entry = g_slice_new (GstIndexEntry); + + memcpy (new_entry, entry, sizeof (GstIndexEntry)); + return new_entry; +} + +/** + * gst_index_entry_free: + * @entry: (transfer full): the entry to free + * + * Free the memory used by the given entry. + */ +void +gst_index_entry_free (GstIndexEntry * entry) +{ + switch (entry->type) { + case GST_INDEX_ENTRY_ID: + if (entry->data.id.description) { + g_free (entry->data.id.description); + entry->data.id.description = NULL; + } + break; + case GST_INDEX_ENTRY_ASSOCIATION: + if (entry->data.assoc.assocs) { + g_free (entry->data.assoc.assocs); + entry->data.assoc.assocs = NULL; + } + break; + case GST_INDEX_ENTRY_OBJECT: + break; + case GST_INDEX_ENTRY_FORMAT: + break; + } + + g_slice_free (GstIndexEntry, entry); +} + +#if 0 +/** + * gst_index_add_format: + * @index: the index to add the entry to + * @id: the id of the index writer + * @format: the format to add to the index + * + * Adds a format entry into the index. This function is + * used to map dynamic GstFormat ids to their original + * format key. + * + * Free-function: gst_index_entry_free + * + * Returns: (transfer full): a pointer to the newly added entry in the index. + */ +GstIndexEntry * +gst_index_add_format (GstIndex * index, gint id, GstFormat format) +{ + GstIndexEntry *entry; + const GstFormatDefinition *def; + + g_return_val_if_fail (GST_IS_INDEX (index), NULL); + g_return_val_if_fail (format != 0, NULL); + + if (!GST_INDEX_IS_WRITABLE (index) || id == -1) + return NULL; + + entry = g_slice_new (GstIndexEntry); + entry->type = GST_INDEX_ENTRY_FORMAT; + entry->id = id; + entry->data.format.format = format; + + def = gst_format_get_details (format); + entry->data.format.key = def->nick; + + gst_index_add_entry (index, entry); + + return entry; +} +#endif + +/** + * gst_index_add_id: + * @index: the index to add the entry to + * @id: the id of the index writer + * @description: the description of the index writer + * + * Add an id entry into the index. + * + * Returns: a pointer to the newly added entry in the index. + */ +GstIndexEntry * +gst_index_add_id (GstIndex * index, gint id, gchar * description) +{ + GstIndexEntry *entry; + + g_return_val_if_fail (GST_IS_INDEX (index), NULL); + g_return_val_if_fail (description != NULL, NULL); + + if (!GST_INDEX_IS_WRITABLE (index) || id == -1) + return NULL; + + entry = g_slice_new (GstIndexEntry); + entry->type = GST_INDEX_ENTRY_ID; + entry->id = id; + entry->data.id.description = description; + + gst_index_add_entry (index, entry); + + return entry; +} + +static gboolean +gst_index_path_resolver (GstIndex * index, GstObject * writer, + gchar ** writer_string, gpointer data) +{ + *writer_string = gst_object_get_path_string (writer); + + return TRUE; +} + +static gboolean +gst_index_gtype_resolver (GstIndex * index, GstObject * writer, + gchar ** writer_string, gpointer data) +{ + g_return_val_if_fail (writer != NULL, FALSE); + + if (GST_IS_PAD (writer)) { + GstObject *element = gst_object_get_parent (GST_OBJECT (writer)); + gchar *name; + + name = gst_object_get_name (writer); + if (element) { + *writer_string = g_strdup_printf ("%s.%s", + G_OBJECT_TYPE_NAME (element), name); + gst_object_unref (element); + } else { + *writer_string = name; + name = NULL; + } + + g_free (name); + + } else { + *writer_string = g_strdup (G_OBJECT_TYPE_NAME (writer)); + } + + return TRUE; +} + +/** + * gst_index_get_writer_id: + * @index: the index to get a unique write id for + * @writer: the GstObject to allocate an id for + * @id: a pointer to a gint to hold the id + * + * Before entries can be added to the index, a writer + * should obtain a unique id. The methods to add new entries + * to the index require this id as an argument. + * + * The application can implement a custom function to map the writer object + * to a string. That string will be used to register or look up an id + * in the index. + * + * + * The caller must not hold @writer's #GST_OBJECT_LOCK, as the default + * resolver may call functions that take the object lock as well, and + * the lock is not recursive. + * + * + * Returns: TRUE if the writer would be mapped to an id. + */ +gboolean +gst_index_get_writer_id (GstIndex * index, GstObject * writer, gint * id) +{ + gchar *writer_string = NULL; + GstIndexEntry *entry; + GstIndexClass *iclass; + gboolean success = FALSE; + + g_return_val_if_fail (GST_IS_INDEX (index), FALSE); + g_return_val_if_fail (GST_IS_OBJECT (writer), FALSE); + g_return_val_if_fail (id, FALSE); + + *id = -1; + + /* first try to get a previously cached id */ + entry = g_hash_table_lookup (index->writers, writer); + if (entry == NULL) { + + iclass = GST_INDEX_GET_CLASS (index); + + /* let the app make a string */ + if (index->resolver) { + gboolean res; + + res = + index->resolver (index, writer, &writer_string, + index->resolver_user_data); + if (!res) + return FALSE; + } else { + g_warning ("no resolver found"); + return FALSE; + } + + /* if the index has a resolver, make it map this string to an id */ + if (iclass->get_writer_id) { + success = iclass->get_writer_id (index, id, writer_string); + } + /* if the index could not resolve, we allocate one ourselves */ + if (!success) { + *id = ++index->last_id; + } + + entry = gst_index_add_id (index, *id, writer_string); + if (!entry) { + /* index is probably not writable, make an entry anyway + * to keep it in our cache */ + entry = g_slice_new (GstIndexEntry); + entry->type = GST_INDEX_ENTRY_ID; + entry->id = *id; + entry->data.id.description = writer_string; + } + g_hash_table_insert (index->writers, writer, entry); + } else { + *id = entry->id; + } + + return TRUE; +} + +static void +gst_index_add_entry (GstIndex * index, GstIndexEntry * entry) +{ + GstIndexClass *iclass; + + iclass = GST_INDEX_GET_CLASS (index); + + if (iclass->add_entry) { + iclass->add_entry (index, entry); + } + + g_signal_emit (index, gst_index_signals[ENTRY_ADDED], 0, entry); +} + +/** + * gst_index_add_associationv: + * @index: the index to add the entry to + * @id: the id of the index writer + * @flags: optinal flags for this entry + * @n: number of associations + * @list: list of associations + * + * Associate given format/value pairs with each other. + * + * Returns: a pointer to the newly added entry in the index. + */ +GstIndexEntry * +gst_index_add_associationv (GstIndex * index, gint id, + GstIndexAssociationFlags flags, gint n, const GstIndexAssociation * list) +{ + GstIndexEntry *entry; + + g_return_val_if_fail (n > 0, NULL); + g_return_val_if_fail (list != NULL, NULL); + g_return_val_if_fail (GST_IS_INDEX (index), NULL); + + if (!GST_INDEX_IS_WRITABLE (index) || id == -1) + return NULL; + + entry = g_slice_new (GstIndexEntry); + + entry->type = GST_INDEX_ENTRY_ASSOCIATION; + entry->id = id; + entry->data.assoc.flags = flags; + entry->data.assoc.assocs = g_memdup (list, sizeof (GstIndexAssociation) * n); + entry->data.assoc.nassocs = n; + + gst_index_add_entry (index, entry); + + return entry; +} + +#if 0 +/** + * gst_index_add_association: + * @index: the index to add the entry to + * @id: the id of the index writer + * @flags: optinal flags for this entry + * @format: the format of the value + * @value: the value + * @...: other format/value pairs or 0 to end the list + * + * Associate given format/value pairs with each other. + * Be sure to pass gint64 values to this functions varargs, + * you might want to use a gint64 cast to be sure. + * + * Returns: a pointer to the newly added entry in the index. + */ +GstIndexEntry * +gst_index_add_association (GstIndex * index, gint id, + GstIndexAssociationFlags flags, GstFormat format, gint64 value, ...) +{ + va_list args; + GstIndexEntry *entry; + GstIndexAssociation *list; + gint n_assocs = 0; + GstFormat cur_format; + GArray *array; + + g_return_val_if_fail (GST_IS_INDEX (index), NULL); + g_return_val_if_fail (format != 0, NULL); + + if (!GST_INDEX_IS_WRITABLE (index) || id == -1) + return NULL; + + array = g_array_new (FALSE, FALSE, sizeof (GstIndexAssociation)); + + { + GstIndexAssociation a; + + a.format = format; + a.value = value; + n_assocs = 1; + g_array_append_val (array, a); + } + + va_start (args, value); + + while ((cur_format = va_arg (args, GstFormat))) { + GstIndexAssociation a; + + a.format = cur_format; + a.value = va_arg (args, gint64); + n_assocs++; + g_array_append_val (array, a); + } + + va_end (args); + + list = (GstIndexAssociation *) g_array_free (array, FALSE); + + entry = gst_index_add_associationv (index, id, flags, n_assocs, list); + g_free (list); + + return entry; +} + +/** + * gst_index_add_object: + * @index: the index to add the object to + * @id: the id of the index writer + * @key: a key for the object + * @type: the GType of the object + * @object: a pointer to the object to add + * + * Add the given object to the index with the given key. + * + * This function is not yet implemented. + * + * Returns: a pointer to the newly added entry in the index. + */ +GstIndexEntry * +gst_index_add_object (GstIndex * index, gint id, gchar * key, + GType type, gpointer object) +{ + if (!GST_INDEX_IS_WRITABLE (index) || id == -1) + return NULL; + + return NULL; +} +#endif + +static gint +gst_index_compare_func (gconstpointer a, gconstpointer b, gpointer user_data) +{ + if (a < b) + return -1; + if (a > b) + return 1; + return 0; +} + +/** + * gst_index_get_assoc_entry: + * @index: the index to search + * @id: the id of the index writer + * @method: The lookup method to use + * @flags: Flags for the entry + * @format: the format of the value + * @value: the value to find + * + * Finds the given format/value in the index + * + * Returns: the entry associated with the value or NULL if the + * value was not found. + */ +GstIndexEntry * +gst_index_get_assoc_entry (GstIndex * index, gint id, + GstIndexLookupMethod method, GstIndexAssociationFlags flags, + GstFormat format, gint64 value) +{ + g_return_val_if_fail (GST_IS_INDEX (index), NULL); + + if (id == -1) + return NULL; + + return gst_index_get_assoc_entry_full (index, id, method, flags, format, + value, gst_index_compare_func, NULL); +} + +/** + * gst_index_get_assoc_entry_full: + * @index: the index to search + * @id: the id of the index writer + * @method: The lookup method to use + * @flags: Flags for the entry + * @format: the format of the value + * @value: the value to find + * @func: the function used to compare entries + * @user_data: user data passed to the compare function + * + * Finds the given format/value in the index with the given + * compare function and user_data. + * + * Returns: the entry associated with the value or NULL if the + * value was not found. + */ +GstIndexEntry * +gst_index_get_assoc_entry_full (GstIndex * index, gint id, + GstIndexLookupMethod method, GstIndexAssociationFlags flags, + GstFormat format, gint64 value, GCompareDataFunc func, gpointer user_data) +{ + GstIndexClass *iclass; + + g_return_val_if_fail (GST_IS_INDEX (index), NULL); + + if (id == -1) + return NULL; + + iclass = GST_INDEX_GET_CLASS (index); + + if (iclass->get_assoc_entry) + return iclass->get_assoc_entry (index, id, method, flags, format, value, + func, user_data); + + return NULL; +} + +/** + * gst_index_entry_assoc_map: + * @entry: the index to search + * @format: the format of the value the find + * @value: a pointer to store the value + * + * Gets alternative formats associated with the indexentry. + * + * Returns: TRUE if there was a value associated with the given + * format. + */ +gboolean +gst_index_entry_assoc_map (GstIndexEntry * entry, + GstFormat format, gint64 * value) +{ + gint i; + + g_return_val_if_fail (entry != NULL, FALSE); + g_return_val_if_fail (value != NULL, FALSE); + + for (i = 0; i < GST_INDEX_NASSOCS (entry); i++) { + if (GST_INDEX_ASSOC_FORMAT (entry, i) == format) { + *value = GST_INDEX_ASSOC_VALUE (entry, i); + return TRUE; + } + } + return FALSE; +} diff --git a/gst/flv/gstindex.h b/gst/flv/gstindex.h new file mode 100644 index 0000000000..653aa18b71 --- /dev/null +++ b/gst/flv/gstindex.h @@ -0,0 +1,448 @@ +/* GStreamer + * Copyright (C) 1999,2000 Erik Walthinsen + * 2000 Wim Taymans + * + * gstindex.h: Header for GstIndex, base class to handle efficient + * storage or caching of seeking information. + * + * 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. + */ + +#ifndef __GST_INDEX_H__ +#define __GST_INDEX_H__ + +#include +#include +#include + +G_BEGIN_DECLS + +#define GST_TYPE_INDEX (gst_index_get_type ()) +#define GST_INDEX(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_INDEX, GstIndex)) +#define GST_IS_INDEX(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_INDEX)) +#define GST_INDEX_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_INDEX, GstIndexClass)) +#define GST_IS_INDEX_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_INDEX)) +#define GST_INDEX_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_INDEX, GstIndexClass)) + +#define GST_TYPE_INDEX_ENTRY (gst_index_entry_get_type()) + +typedef struct _GstIndexEntry GstIndexEntry; +typedef struct _GstIndexGroup GstIndexGroup; +typedef struct _GstIndex GstIndex; +typedef struct _GstIndexClass GstIndexClass; + +/** + * GstIndexCertainty: + * @GST_INDEX_UNKNOWN: accuracy is not known + * @GST_INDEX_CERTAIN: accuracy is perfect + * @GST_INDEX_FUZZY: accuracy is fuzzy + * + * The certainty of a group in the index. + */ +typedef enum { + GST_INDEX_UNKNOWN, + GST_INDEX_CERTAIN, + GST_INDEX_FUZZY +} GstIndexCertainty; + +/** + * GstIndexEntryType: + * @GST_INDEX_ENTRY_ID: This entry is an id that maps an index id to its owner object + * @GST_INDEX_ENTRY_ASSOCIATION: This entry is an association between formats + * @GST_INDEX_ENTRY_OBJECT: An object + * @GST_INDEX_ENTRY_FORMAT: A format definition + * + * The different types of entries in the index. + */ +typedef enum { + GST_INDEX_ENTRY_ID, + GST_INDEX_ENTRY_ASSOCIATION, + GST_INDEX_ENTRY_OBJECT, + GST_INDEX_ENTRY_FORMAT +} GstIndexEntryType; + +/** + * GstIndexLookupMethod: + * @GST_INDEX_LOOKUP_EXACT: There has to be an exact indexentry with the given format/value + * @GST_INDEX_LOOKUP_BEFORE: The exact entry or the one before it + * @GST_INDEX_LOOKUP_AFTER: The exact entry or the one after it + * + * Specify the method to find an index entry in the index. + */ +typedef enum { + GST_INDEX_LOOKUP_EXACT, + GST_INDEX_LOOKUP_BEFORE, + GST_INDEX_LOOKUP_AFTER +} GstIndexLookupMethod; + +/** + * GST_INDEX_NASSOCS: + * @entry: The entry to query + * + * Get the number of associations in the entry. + */ +#define GST_INDEX_NASSOCS(entry) ((entry)->data.assoc.nassocs) + +/** + * GST_INDEX_ASSOC_FLAGS: + * @entry: The entry to query + * + * Get the flags for this entry. + */ +#define GST_INDEX_ASSOC_FLAGS(entry) ((entry)->data.assoc.flags) + +/** + * GST_INDEX_ASSOC_FORMAT: + * @entry: The entry to query + * @i: The format index + * + * Get the i-th format of the entry. + */ +#define GST_INDEX_ASSOC_FORMAT(entry,i) ((entry)->data.assoc.assocs[(i)].format) + +/** + * GST_INDEX_ASSOC_VALUE: + * @entry: The entry to query + * @i: The value index + * + * Get the i-th value of the entry. + */ +#define GST_INDEX_ASSOC_VALUE(entry,i) ((entry)->data.assoc.assocs[(i)].value) + +typedef struct _GstIndexAssociation GstIndexAssociation; + +/** + * GstIndexAssociation: + * @format: the format of the association + * @value: the value of the association + * + * An association in an entry. + */ +struct _GstIndexAssociation { + GstFormat format; + gint64 value; +}; + +/** + * GstIndexAssociationFlags: + * @GST_INDEX_ASSOCIATION_FLAG_NONE: no extra flags + * @GST_INDEX_ASSOCIATION_FLAG_KEY_UNIT: the entry marks a key unit, a key unit is one + * that marks a place where one can randomly seek to. + * @GST_INDEX_ASSOCIATION_FLAG_DELTA_UNIT: the entry marks a delta unit, a delta unit + * is one that marks a place where one can relatively seek to. + * @GST_INDEX_ASSOCIATION_FLAG_LAST: extra user defined flags should start here. + * + * Flags for an association entry. + */ +typedef enum { + GST_INDEX_ASSOCIATION_FLAG_NONE = 0, + GST_INDEX_ASSOCIATION_FLAG_KEY_UNIT = (1 << 0), + GST_INDEX_ASSOCIATION_FLAG_DELTA_UNIT = (1 << 1), + + /* new flags should start here */ + GST_INDEX_ASSOCIATION_FLAG_LAST = (1 << 8) +} GstIndexAssociationFlags; + +/** + * GST_INDEX_FORMAT_FORMAT: + * @entry: The entry to query + * + * Get the format of the format entry + */ +#define GST_INDEX_FORMAT_FORMAT(entry) ((entry)->data.format.format) + +/** + * GST_INDEX_FORMAT_KEY: + * @entry: The entry to query + * + * Get the key of the format entry + */ +#define GST_INDEX_FORMAT_KEY(entry) ((entry)->data.format.key) + +/** + * GST_INDEX_ID_INVALID: + * + * Constant for an invalid index id + */ +#define GST_INDEX_ID_INVALID (-1) + +/** + * GST_INDEX_ID_DESCRIPTION: + * @entry: The entry to query + * + * Get the description of the id entry + */ +#define GST_INDEX_ID_DESCRIPTION(entry) ((entry)->data.id.description) + +/** + * GstIndexEntry: + * + * The basic element of an index. + */ +struct _GstIndexEntry { + /*< private >*/ + GstIndexEntryType type; + gint id; + + union { + struct { + gchar *description; + } id; + struct { + gint nassocs; + GstIndexAssociation + *assocs; + GstIndexAssociationFlags flags; + } assoc; + struct { + gchar *key; + GType type; + gpointer object; + } object; + struct { + GstFormat format; + const gchar *key; + } format; + } data; +}; + +/** + * GstIndexGroup: + * + * A group of related entries in an index. + */ + +struct _GstIndexGroup { + /*< private >*/ + /* unique ID of group in index */ + gint groupnum; + + /* list of entries */ + GList *entries; + + /* the certainty level of the group */ + GstIndexCertainty certainty; + + /* peer group that contains more certain entries */ + gint peergroup; +}; + +/** + * GstIndexFilter: + * @index: The index being queried + * @entry: The entry to be added. + * @user_data: User data passed to the function. + * + * Function to filter out entries in the index. + * + * Returns: This function should return %TRUE if the entry is to be added + * to the index, %FALSE otherwise. + * + */ +typedef gboolean (*GstIndexFilter) (GstIndex *index, + GstIndexEntry *entry, + gpointer user_data); +/** + * GstIndexResolverMethod: + * @GST_INDEX_RESOLVER_CUSTOM: Use a custom resolver + * @GST_INDEX_RESOLVER_GTYPE: Resolve based on the GType of the object + * @GST_INDEX_RESOLVER_PATH: Resolve on the path in graph + * + * The method used to resolve index writers + */ +typedef enum { + GST_INDEX_RESOLVER_CUSTOM, + GST_INDEX_RESOLVER_GTYPE, + GST_INDEX_RESOLVER_PATH +} GstIndexResolverMethod; + +/** + * GstIndexResolver: + * @index: the index being queried. + * @writer: The object that wants to write + * @writer_string: A description of the writer. + * @user_data: user_data as registered + * + * Function to resolve ids to writer descriptions. + * + * Returns: %TRUE if an id could be assigned to the writer. + */ +typedef gboolean (*GstIndexResolver) (GstIndex *index, + GstObject *writer, + gchar **writer_string, + gpointer user_data); + +/** + * GstIndexFlags: + * @GST_INDEX_WRITABLE: The index is writable + * @GST_INDEX_READABLE: The index is readable + * @GST_INDEX_FLAG_LAST: First flag that can be used by subclasses + * + * Flags for this index + */ +typedef enum { + GST_INDEX_WRITABLE = (GST_OBJECT_FLAG_LAST << 0), + GST_INDEX_READABLE = (GST_OBJECT_FLAG_LAST << 1), + + GST_INDEX_FLAG_LAST = (GST_OBJECT_FLAG_LAST << 8) +} GstIndexFlags; + +/** + * GST_INDEX_IS_READABLE: + * @obj: The index to check + * + * Check if the index can be read from + */ +#define GST_INDEX_IS_READABLE(obj) (GST_OBJECT_FLAG_IS_SET (obj, GST_INDEX_READABLE)) + +/** + * GST_INDEX_IS_WRITABLE: + * @obj: The index to check + * + * Check if the index can be written to + */ +#define GST_INDEX_IS_WRITABLE(obj) (GST_OBJECT_FLAG_IS_SET (obj, GST_INDEX_WRITABLE)) + +/** + * GstIndex: + * + * Opaque #GstIndex structure. + */ +struct _GstIndex { + GstObject object; + + /*< private >*/ + GList *groups; + GstIndexGroup *curgroup; + gint maxgroup; + + GstIndexResolverMethod method; + GstIndexResolver resolver; + gpointer resolver_user_data; + GDestroyNotify resolver_user_data_destroy; + + GstIndexFilter filter; + gpointer filter_user_data; + GDestroyNotify filter_user_data_destroy; + + GHashTable *writers; + gint last_id; + + /*< private >*/ + gpointer _gst_reserved[GST_PADDING]; +}; + +struct _GstIndexClass { + GstObjectClass parent_class; + + /*< protected >*/ + gboolean (*get_writer_id) (GstIndex *index, gint *id, gchar *writer); + + void (*commit) (GstIndex *index, gint id); + + /* abstract methods */ + void (*add_entry) (GstIndex *index, GstIndexEntry *entry); + + GstIndexEntry* (*get_assoc_entry) (GstIndex *index, gint id, + GstIndexLookupMethod method, GstIndexAssociationFlags flags, + GstFormat format, gint64 value, + GCompareDataFunc func, + gpointer user_data); + /* signals */ + void (*entry_added) (GstIndex *index, GstIndexEntry *entry); + + /*< private >*/ + gpointer _gst_reserved[GST_PADDING]; +}; + +static +GType gst_index_get_type (void); + +#if 0 +GstIndex* gst_index_new (void); +#endif +void gst_index_commit (GstIndex *index, gint id); + +#if 0 +gint gst_index_get_group (GstIndex *index); +gint gst_index_new_group (GstIndex *index); +gboolean gst_index_set_group (GstIndex *index, gint groupnum); + +void gst_index_set_certainty (GstIndex *index, + GstIndexCertainty certainty); +GstIndexCertainty gst_index_get_certainty (GstIndex *index); + +static +void gst_index_set_filter (GstIndex *index, + GstIndexFilter filter, gpointer user_data); +static +void gst_index_set_filter_full (GstIndex *index, + GstIndexFilter filter, gpointer user_data, + GDestroyNotify user_data_destroy); + +void gst_index_set_resolver (GstIndex *index, + GstIndexResolver resolver, gpointer user_data); +void gst_index_set_resolver_full (GstIndex *index, GstIndexResolver resolver, + gpointer user_data, + GDestroyNotify user_data_destroy); +#endif + +static +gboolean gst_index_get_writer_id (GstIndex *index, GstObject *writer, gint *id); + +#if 0 +GstIndexEntry* gst_index_add_format (GstIndex *index, gint id, GstFormat format); +#endif + +static +GstIndexEntry* gst_index_add_associationv (GstIndex * index, gint id, GstIndexAssociationFlags flags, + gint n, const GstIndexAssociation * list); +#if 0 +GstIndexEntry* gst_index_add_association (GstIndex *index, gint id, GstIndexAssociationFlags flags, + GstFormat format, gint64 value, ...) +GstIndexEntry* gst_index_add_object (GstIndex *index, gint id, gchar *key, + GType type, gpointer object); +#endif + +static +GstIndexEntry* gst_index_add_id (GstIndex *index, gint id, + gchar *description); + +static +GstIndexEntry* gst_index_get_assoc_entry (GstIndex *index, gint id, + GstIndexLookupMethod method, GstIndexAssociationFlags flags, + GstFormat format, gint64 value); +static +GstIndexEntry* gst_index_get_assoc_entry_full (GstIndex *index, gint id, + GstIndexLookupMethod method, GstIndexAssociationFlags flags, + GstFormat format, gint64 value, + GCompareDataFunc func, + gpointer user_data); + +/* working with index entries */ +static +GType gst_index_entry_get_type (void); +static +GstIndexEntry * gst_index_entry_copy (GstIndexEntry *entry); +static +void gst_index_entry_free (GstIndexEntry *entry); +static +gboolean gst_index_entry_assoc_map (GstIndexEntry *entry, + GstFormat format, gint64 *value); + +G_END_DECLS + +#endif /* __GST_INDEX_H__ */ diff --git a/gst/flv/gstmemindex.c b/gst/flv/gstmemindex.c new file mode 100644 index 0000000000..aaeab12d90 --- /dev/null +++ b/gst/flv/gstmemindex.c @@ -0,0 +1,430 @@ +/* GStreamer + * Copyright (C) <1999> Erik Walthinsen + * + * 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 + +#define GST_TYPE_MEM_INDEX \ + (gst_index_get_type ()) +#define GST_MEM_INDEX(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_MEM_INDEX, GstMemIndex)) +#define GST_MEM_INDEX_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_MEM_INDEX, GstMemIndexClass)) +#define GST_IS_MEM_INDEX(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_MEM_INDEX)) +#define GST_IS_MEM_INDEX_CLASS(klass) \ + (GST_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_MEM_INDEX)) + +/* + * Object model: + * + * All entries are simply added to a GList first. Then we build + * an index to each entry for each id/format + * + * + * memindex + * -----------------------------... + * ! ! + * id1 id2 + * ------------ + * ! ! + * format1 format2 + * ! ! + * GTree GTree + * + * + * The memindex creates a MemIndexId object for each writer id, a + * Hashtable is kept to map the id to the MemIndexId + * + * The MemIndexId keeps a MemIndexFormatIndex for each format the + * specific writer wants indexed. + * + * The MemIndexFormatIndex keeps all the values of the particular + * format in a GTree, The values of the GTree point back to the entry. + * + * Finding a value for an id/format requires locating the correct GTree, + * then do a lookup in the Tree to get the required value. + */ + +typedef struct +{ + GstFormat format; + gint offset; + GTree *tree; +} +GstMemIndexFormatIndex; + +typedef struct +{ + gint id; + GHashTable *format_index; +} +GstMemIndexId; + +typedef struct _GstMemIndex GstMemIndex; +typedef struct _GstMemIndexClass GstMemIndexClass; + +struct _GstMemIndex +{ + GstIndex parent; + + GList *associations; + + GHashTable *id_index; +}; + +struct _GstMemIndexClass +{ + GstIndexClass parent_class; +}; + +static void gst_mem_index_finalize (GObject * object); + +static void gst_mem_index_add_entry (GstIndex * index, GstIndexEntry * entry); +static GstIndexEntry *gst_mem_index_get_assoc_entry (GstIndex * index, gint id, + GstIndexLookupMethod method, GstIndexAssociationFlags flags, + GstFormat format, gint64 value, GCompareDataFunc func, gpointer user_data); + +#define CLASS(mem_index) GST_MEM_INDEX_CLASS (G_OBJECT_GET_CLASS (mem_index)) + +static GType gst_mem_index_get_type (void); + +G_DEFINE_TYPE (GstMemIndex, gst_mem_index, GST_TYPE_INDEX); + +static void +gst_mem_index_class_init (GstMemIndexClass * klass) +{ + GObjectClass *gobject_class; + GstIndexClass *gstindex_class; + + gobject_class = (GObjectClass *) klass; + gstindex_class = (GstIndexClass *) klass; + + gobject_class->finalize = gst_mem_index_finalize; + + gstindex_class->add_entry = GST_DEBUG_FUNCPTR (gst_mem_index_add_entry); + gstindex_class->get_assoc_entry = + GST_DEBUG_FUNCPTR (gst_mem_index_get_assoc_entry); +} + +static void +gst_mem_index_init (GstMemIndex * index) +{ + GST_DEBUG ("created new mem index"); + + index->associations = NULL; + index->id_index = g_hash_table_new (g_int_hash, g_int_equal); +} + +static void +gst_mem_index_free_format (gpointer key, gpointer value, gpointer user_data) +{ + GstMemIndexFormatIndex *index = (GstMemIndexFormatIndex *) value; + + if (index->tree) { + g_tree_destroy (index->tree); + } + + g_slice_free (GstMemIndexFormatIndex, index); +} + +static void +gst_mem_index_free_id (gpointer key, gpointer value, gpointer user_data) +{ + GstMemIndexId *id_index = (GstMemIndexId *) value; + + if (id_index->format_index) { + g_hash_table_foreach (id_index->format_index, gst_mem_index_free_format, + NULL); + g_hash_table_destroy (id_index->format_index); + id_index->format_index = NULL; + } + + g_slice_free (GstMemIndexId, id_index); +} + +static void +gst_mem_index_finalize (GObject * object) +{ + GstMemIndex *memindex = GST_MEM_INDEX (object); + + /* Delete the trees referencing the associations first */ + if (memindex->id_index) { + g_hash_table_foreach (memindex->id_index, gst_mem_index_free_id, NULL); + g_hash_table_destroy (memindex->id_index); + memindex->id_index = NULL; + } + + /* Then delete the associations themselves */ + if (memindex->associations) { + g_list_foreach (memindex->associations, (GFunc) gst_index_entry_free, NULL); + g_list_free (memindex->associations); + memindex->associations = NULL; + } + + G_OBJECT_CLASS (gst_mem_index_parent_class)->finalize (object); +} + +static void +gst_mem_index_add_id (GstIndex * index, GstIndexEntry * entry) +{ + GstMemIndex *memindex = GST_MEM_INDEX (index); + GstMemIndexId *id_index; + + id_index = g_hash_table_lookup (memindex->id_index, &entry->id); + + if (!id_index) { + id_index = g_slice_new0 (GstMemIndexId); + + id_index->id = entry->id; + id_index->format_index = g_hash_table_new (g_int_hash, g_int_equal); + g_hash_table_insert (memindex->id_index, &id_index->id, id_index); + } +} + +static gint +mem_index_compare (gconstpointer a, gconstpointer b, gpointer user_data) +{ + GstMemIndexFormatIndex *index = user_data; + gint64 val1, val2; + gint64 diff; + + val1 = GST_INDEX_ASSOC_VALUE (((GstIndexEntry *) a), index->offset); + val2 = GST_INDEX_ASSOC_VALUE (((GstIndexEntry *) b), index->offset); + + diff = (val2 - val1); + + return (diff == 0 ? 0 : (diff > 0 ? 1 : -1)); +} + +static void +gst_mem_index_index_format (GstMemIndexId * id_index, GstIndexEntry * entry, + gint assoc) +{ + GstMemIndexFormatIndex *index; + GstFormat *format; + + format = &GST_INDEX_ASSOC_FORMAT (entry, assoc); + + index = g_hash_table_lookup (id_index->format_index, format); + + if (!index) { + index = g_slice_new0 (GstMemIndexFormatIndex); + + index->format = *format; + index->offset = assoc; + index->tree = g_tree_new_with_data (mem_index_compare, index); + + g_hash_table_insert (id_index->format_index, &index->format, index); + } + + g_tree_insert (index->tree, entry, entry); +} + +static void +gst_mem_index_add_association (GstIndex * index, GstIndexEntry * entry) +{ + GstMemIndex *memindex = GST_MEM_INDEX (index); + GstMemIndexId *id_index; + + memindex->associations = g_list_prepend (memindex->associations, entry); + + id_index = g_hash_table_lookup (memindex->id_index, &entry->id); + if (id_index) { + gint i; + + for (i = 0; i < GST_INDEX_NASSOCS (entry); i++) { + gst_mem_index_index_format (id_index, entry, i); + } + } +} + +static void +gst_mem_index_add_object (GstIndex * index, GstIndexEntry * entry) +{ +} + +static void +gst_mem_index_add_format (GstIndex * index, GstIndexEntry * entry) +{ +} + +static void +gst_mem_index_add_entry (GstIndex * index, GstIndexEntry * entry) +{ + GST_LOG_OBJECT (index, "added this entry"); + + switch (entry->type) { + case GST_INDEX_ENTRY_ID: + gst_mem_index_add_id (index, entry); + break; + case GST_INDEX_ENTRY_ASSOCIATION: + gst_mem_index_add_association (index, entry); + break; + case GST_INDEX_ENTRY_OBJECT: + gst_mem_index_add_object (index, entry); + break; + case GST_INDEX_ENTRY_FORMAT: + gst_mem_index_add_format (index, entry); + break; + default: + break; + } +} + +typedef struct +{ + gint64 value; + GstMemIndexFormatIndex *index; + gboolean exact; + GstIndexEntry *lower; + gint64 low_diff; + GstIndexEntry *higher; + gint64 high_diff; +} +GstMemIndexSearchData; + +static gint +mem_index_search (gconstpointer a, gconstpointer b) +{ + GstMemIndexSearchData *data = (GstMemIndexSearchData *) b; + GstMemIndexFormatIndex *index = data->index; + gint64 val1, val2; + gint64 diff; + + val1 = GST_INDEX_ASSOC_VALUE (((GstIndexEntry *) a), index->offset); + val2 = data->value; + + diff = (val1 - val2); + if (diff == 0) + return 0; + + /* exact matching, don't update low/high */ + if (data->exact) + return (diff > 0 ? 1 : -1); + + if (diff < 0) { + if (diff > data->low_diff) { + data->low_diff = diff; + data->lower = (GstIndexEntry *) a; + } + diff = -1; + } else { + if (diff < data->high_diff) { + data->high_diff = diff; + data->higher = (GstIndexEntry *) a; + } + diff = 1; + } + + return diff; +} + +static GstIndexEntry * +gst_mem_index_get_assoc_entry (GstIndex * index, gint id, + GstIndexLookupMethod method, + GstIndexAssociationFlags flags, + GstFormat format, gint64 value, GCompareDataFunc func, gpointer user_data) +{ + GstMemIndex *memindex = GST_MEM_INDEX (index); + GstMemIndexId *id_index; + GstMemIndexFormatIndex *format_index; + GstIndexEntry *entry; + GstMemIndexSearchData data; + + id_index = g_hash_table_lookup (memindex->id_index, &id); + if (!id_index) + return NULL; + + format_index = g_hash_table_lookup (id_index->format_index, &format); + if (!format_index) + return NULL; + + data.value = value; + data.index = format_index; + data.exact = (method == GST_INDEX_LOOKUP_EXACT); + + /* setup data for low/high checks if we are not looking + * for an exact match */ + if (!data.exact) { + data.low_diff = G_MININT64; + data.lower = NULL; + data.high_diff = G_MAXINT64; + data.higher = NULL; + } + + entry = g_tree_search (format_index->tree, mem_index_search, &data); + + /* get the low/high values if we're not exact */ + if (entry == NULL && !data.exact) { + if (method == GST_INDEX_LOOKUP_BEFORE) + entry = data.lower; + else if (method == GST_INDEX_LOOKUP_AFTER) { + entry = data.higher; + } + } + + if (entry && ((GST_INDEX_ASSOC_FLAGS (entry) & flags) != flags)) { + if (method != GST_INDEX_LOOKUP_EXACT) { + GList *l_entry = g_list_find (memindex->associations, entry); + + entry = NULL; + + while (l_entry) { + entry = (GstIndexEntry *) l_entry->data; + + if (entry->id == id && (GST_INDEX_ASSOC_FLAGS (entry) & flags) == flags) + break; + + if (method == GST_INDEX_LOOKUP_BEFORE) + l_entry = g_list_next (l_entry); + else if (method == GST_INDEX_LOOKUP_AFTER) { + l_entry = g_list_previous (l_entry); + } + } + } else { + entry = NULL; + } + } + + return entry; +} + +#if 0 +gboolean +gst_mem_index_plugin_init (GstPlugin * plugin) +{ + GstIndexFactory *factory; + + factory = gst_index_factory_new ("memindex", + "A index that stores entries in memory", gst_mem_index_get_type ()); + + if (factory == NULL) { + g_warning ("failed to create memindex factory"); + return FALSE; + } + + GST_PLUGIN_FEATURE (factory)->plugin_name = plugin->desc.name; + GST_PLUGIN_FEATURE (factory)->loaded = TRUE; + + gst_registry_add_feature (gst_registry_get_default (), + GST_PLUGIN_FEATURE (factory)); + + return TRUE; +} +#endif