2 * Copyright (C) 2001 RidgeRun (http://www.ridgerun.com/)
3 * Written by Erik Walthinsen <omega@ridgerun.com>
5 * gstindex.c: Index for mappings and other data
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
25 * @short_description: Generate indexes on objects
26 * @see_also: #GstIndexFactory
28 * GstIndex is used to generate a stream index of one or more elements
32 /* FIXME: complete gobject annotations */
34 #include "gst_private.h"
38 #include "gstindexfactory.h"
39 #include "gstmarshal.h"
40 #include "gstregistry.h"
41 /* for constructing an entry name */
42 #include "gstelement.h"
46 /* Index signals and args */
60 GST_DEBUG_CATEGORY_STATIC (index_debug);
61 #define GST_CAT_DEFAULT index_debug
63 static void gst_index_finalize (GObject * object);
65 static void gst_index_set_property (GObject * object, guint prop_id,
66 const GValue * value, GParamSpec * pspec);
67 static void gst_index_get_property (GObject * object, guint prop_id,
68 GValue * value, GParamSpec * pspec);
70 static GstIndexGroup *gst_index_group_new (guint groupnum);
71 static void gst_index_group_free (GstIndexGroup * group);
73 static gboolean gst_index_path_resolver (GstIndex * index, GstObject * writer,
74 gchar ** writer_string, gpointer data);
75 static gboolean gst_index_gtype_resolver (GstIndex * index, GstObject * writer,
76 gchar ** writer_string, gpointer data);
77 static void gst_index_add_entry (GstIndex * index, GstIndexEntry * entry);
79 static GstObject *parent_class = NULL;
80 static guint gst_index_signals[LAST_SIGNAL] = { 0 };
84 GstIndexResolverMethod method;
85 GstIndexResolver resolver;
90 static const ResolverEntry resolvers[] = {
91 {GST_INDEX_RESOLVER_CUSTOM, NULL, NULL},
92 {GST_INDEX_RESOLVER_GTYPE, gst_index_gtype_resolver, NULL},
93 {GST_INDEX_RESOLVER_PATH, gst_index_path_resolver, NULL},
96 #define GST_TYPE_INDEX_RESOLVER (gst_index_resolver_get_type())
98 gst_index_resolver_get_type (void)
100 static GType index_resolver_type = 0;
101 static const GEnumValue index_resolver[] = {
102 {GST_INDEX_RESOLVER_CUSTOM, "GST_INDEX_RESOLVER_CUSTOM", "custom"},
103 {GST_INDEX_RESOLVER_GTYPE, "GST_INDEX_RESOLVER_GTYPE", "gtype"},
104 {GST_INDEX_RESOLVER_PATH, "GST_INDEX_RESOLVER_PATH", "path"},
108 if (!index_resolver_type) {
109 index_resolver_type =
110 g_enum_register_static ("GstIndexResolver", index_resolver);
112 return index_resolver_type;
116 gst_index_entry_get_type (void)
118 static GType index_entry_type = 0;
120 if (!index_entry_type) {
121 index_entry_type = g_boxed_type_register_static ("GstIndexEntry",
122 (GBoxedCopyFunc) gst_index_entry_copy,
123 (GBoxedFreeFunc) gst_index_entry_free);
125 return index_entry_type;
130 GST_DEBUG_CATEGORY_INIT (index_debug, "GST_INDEX", GST_DEBUG_BOLD, \
131 "Generic indexing support"); \
134 G_DEFINE_TYPE_WITH_CODE (GstIndex, gst_index, GST_TYPE_OBJECT, _do_init);
137 gst_index_class_init (GstIndexClass * klass)
139 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
141 parent_class = g_type_class_peek_parent (klass);
144 * GstIndex::entry-added
145 * @gstindex: the object which received the signal.
146 * @arg1: The entry added to the index.
148 * Is emitted when a new entry is added to the index.
150 gst_index_signals[ENTRY_ADDED] =
151 g_signal_new ("entry-added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
152 G_STRUCT_OFFSET (GstIndexClass, entry_added), NULL, NULL,
153 gst_marshal_VOID__BOXED, G_TYPE_NONE, 1, GST_TYPE_INDEX_ENTRY);
155 gobject_class->set_property = gst_index_set_property;
156 gobject_class->get_property = gst_index_get_property;
157 gobject_class->finalize = gst_index_finalize;
159 g_object_class_install_property (gobject_class, ARG_RESOLVER,
160 g_param_spec_enum ("resolver", "Resolver",
161 "Select a predefined object to string mapper",
162 GST_TYPE_INDEX_RESOLVER, GST_INDEX_RESOLVER_PATH,
163 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
167 gst_index_init (GstIndex * index)
169 index->curgroup = gst_index_group_new (0);
171 index->groups = g_list_prepend (NULL, index->curgroup);
173 index->writers = g_hash_table_new (NULL, NULL);
176 index->method = GST_INDEX_RESOLVER_PATH;
177 index->resolver = resolvers[index->method].resolver;
178 index->resolver_user_data = resolvers[index->method].user_data;
180 GST_OBJECT_FLAG_SET (index, GST_INDEX_WRITABLE);
181 GST_OBJECT_FLAG_SET (index, GST_INDEX_READABLE);
183 GST_DEBUG ("created new index");
187 gst_index_free_writer (gpointer key, gpointer value, gpointer user_data)
189 GstIndexEntry *entry = (GstIndexEntry *) value;
192 gst_index_entry_free (entry);
197 gst_index_finalize (GObject * object)
199 GstIndex *index = GST_INDEX (object);
202 g_list_foreach (index->groups, (GFunc) gst_index_group_free, NULL);
203 g_list_free (index->groups);
204 index->groups = NULL;
207 if (index->writers) {
208 g_hash_table_foreach (index->writers, gst_index_free_writer, NULL);
209 g_hash_table_destroy (index->writers);
210 index->writers = NULL;
213 if (index->filter_user_data && index->filter_user_data_destroy)
214 index->filter_user_data_destroy (index->filter_user_data);
216 if (index->resolver_user_data && index->resolver_user_data_destroy)
217 index->resolver_user_data_destroy (index->resolver_user_data);
219 G_OBJECT_CLASS (parent_class)->finalize (object);
223 gst_index_set_property (GObject * object, guint prop_id,
224 const GValue * value, GParamSpec * pspec)
228 index = GST_INDEX (object);
232 index->method = g_value_get_enum (value);
233 index->resolver = resolvers[index->method].resolver;
234 index->resolver_user_data = resolvers[index->method].user_data;
237 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
243 gst_index_get_property (GObject * object, guint prop_id,
244 GValue * value, GParamSpec * pspec)
248 index = GST_INDEX (object);
252 g_value_set_enum (value, index->method);
255 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
260 static GstIndexGroup *
261 gst_index_group_new (guint groupnum)
263 GstIndexGroup *indexgroup = g_slice_new (GstIndexGroup);
265 indexgroup->groupnum = groupnum;
266 indexgroup->entries = NULL;
267 indexgroup->certainty = GST_INDEX_UNKNOWN;
268 indexgroup->peergroup = -1;
270 GST_DEBUG ("created new index group %d", groupnum);
276 gst_index_group_free (GstIndexGroup * group)
278 g_slice_free (GstIndexGroup, group);
284 * Create a new tileindex object
286 * Returns: (transfer full): a new index object
293 index = g_object_newv (gst_index_get_type (), 0, NULL);
300 * @index: the index to commit
301 * @id: the writer that commited the index
303 * Tell the index that the writer with the given id is done
304 * with this index and is not going to write any more entries
308 gst_index_commit (GstIndex * index, gint id)
310 GstIndexClass *iclass;
312 iclass = GST_INDEX_GET_CLASS (index);
315 iclass->commit (index, id);
320 * gst_index_get_group:
321 * @index: the index to get the current group from
323 * Get the id of the current group.
325 * Returns: the id of the current group.
328 gst_index_get_group (GstIndex * index)
330 return index->curgroup->groupnum;
334 * gst_index_new_group:
335 * @index: the index to create the new group in
337 * Create a new group for the given index. It will be
338 * set as the current group.
340 * Returns: the id of the newly created group.
343 gst_index_new_group (GstIndex * index)
345 index->curgroup = gst_index_group_new (++index->maxgroup);
346 index->groups = g_list_append (index->groups, index->curgroup);
347 GST_DEBUG ("created new group %d in index", index->maxgroup);
348 return index->maxgroup;
352 * gst_index_set_group:
353 * @index: the index to set the new group in
354 * @groupnum: the groupnumber to set
356 * Set the current groupnumber to the given argument.
358 * Returns: TRUE if the operation succeeded, FALSE if the group
362 gst_index_set_group (GstIndex * index, gint groupnum)
365 GstIndexGroup *indexgroup;
367 /* first check for null change */
368 if (groupnum == index->curgroup->groupnum)
371 /* else search for the proper group */
372 list = index->groups;
374 indexgroup = (GstIndexGroup *) (list->data);
375 list = g_list_next (list);
376 if (indexgroup->groupnum == groupnum) {
377 index->curgroup = indexgroup;
378 GST_DEBUG ("switched to index group %d", indexgroup->groupnum);
383 /* couldn't find the group in question */
384 GST_DEBUG ("couldn't find index group %d", groupnum);
389 * gst_index_set_certainty:
390 * @index: the index to set the certainty on
391 * @certainty: the certainty to set
393 * Set the certainty of the given index.
396 gst_index_set_certainty (GstIndex * index, GstIndexCertainty certainty)
398 index->curgroup->certainty = certainty;
402 * gst_index_get_certainty:
403 * @index: the index to get the certainty of
405 * Get the certainty of the given index.
407 * Returns: the certainty of the index.
410 gst_index_get_certainty (GstIndex * index)
412 return index->curgroup->certainty;
416 * gst_index_set_filter:
417 * @index: the index to register the filter on
418 * @filter: the filter to register
419 * @user_data: data passed to the filter function
421 * Lets the app register a custom filter function so that
422 * it can select what entries should be stored in the index.
425 gst_index_set_filter (GstIndex * index,
426 GstIndexFilter filter, gpointer user_data)
428 g_return_if_fail (GST_IS_INDEX (index));
430 gst_index_set_filter_full (index, filter, user_data, NULL);
434 * gst_index_set_filter_full:
435 * @index: the index to register the filter on
436 * @filter: the filter to register
437 * @user_data: data passed to the filter function
438 * @user_data_destroy: function to call when @user_data is unset
440 * Lets the app register a custom filter function so that
441 * it can select what entries should be stored in the index.
444 gst_index_set_filter_full (GstIndex * index,
445 GstIndexFilter filter, gpointer user_data, GDestroyNotify user_data_destroy)
447 g_return_if_fail (GST_IS_INDEX (index));
449 if (index->filter_user_data && index->filter_user_data_destroy)
450 index->filter_user_data_destroy (index->filter_user_data);
452 index->filter = filter;
453 index->filter_user_data = user_data;
454 index->filter_user_data_destroy = user_data_destroy;
458 * gst_index_set_resolver:
459 * @index: the index to register the resolver on
460 * @resolver: the resolver to register
461 * @user_data: data passed to the resolver function
463 * Lets the app register a custom function to map index
464 * ids to writer descriptions.
467 gst_index_set_resolver (GstIndex * index,
468 GstIndexResolver resolver, gpointer user_data)
470 gst_index_set_resolver_full (index, resolver, user_data, NULL);
474 * gst_index_set_resolver_full:
475 * @index: the index to register the resolver on
476 * @resolver: the resolver to register
477 * @user_data: data passed to the resolver function
478 * @user_data_destroy: destroy function for @user_data
480 * Lets the app register a custom function to map index
481 * ids to writer descriptions.
486 gst_index_set_resolver_full (GstIndex * index, GstIndexResolver resolver,
487 gpointer user_data, GDestroyNotify user_data_destroy)
489 g_return_if_fail (GST_IS_INDEX (index));
491 if (index->resolver_user_data && index->resolver_user_data_destroy)
492 index->resolver_user_data_destroy (index->resolver_user_data);
494 index->resolver = resolver;
495 index->resolver_user_data = user_data;
496 index->resolver_user_data_destroy = user_data_destroy;
497 index->method = GST_INDEX_RESOLVER_CUSTOM;
501 * gst_index_entry_copy:
502 * @entry: the entry to copy
504 * Copies an entry and returns the result.
506 * Free-function: gst_index_entry_free
508 * Returns: (transfer full): a newly allocated #GstIndexEntry.
511 gst_index_entry_copy (GstIndexEntry * entry)
513 GstIndexEntry *new_entry = g_slice_new (GstIndexEntry);
515 memcpy (new_entry, entry, sizeof (GstIndexEntry));
520 * gst_index_entry_free:
521 * @entry: (transfer full): the entry to free
523 * Free the memory used by the given entry.
526 gst_index_entry_free (GstIndexEntry * entry)
528 switch (entry->type) {
529 case GST_INDEX_ENTRY_ID:
530 if (entry->data.id.description) {
531 g_free (entry->data.id.description);
532 entry->data.id.description = NULL;
535 case GST_INDEX_ENTRY_ASSOCIATION:
536 if (entry->data.assoc.assocs) {
537 g_free (entry->data.assoc.assocs);
538 entry->data.assoc.assocs = NULL;
541 case GST_INDEX_ENTRY_OBJECT:
543 case GST_INDEX_ENTRY_FORMAT:
547 g_slice_free (GstIndexEntry, entry);
551 * gst_index_add_format:
552 * @index: the index to add the entry to
553 * @id: the id of the index writer
554 * @format: the format to add to the index
556 * Adds a format entry into the index. This function is
557 * used to map dynamic GstFormat ids to their original
560 * Free-function: gst_index_entry_free
562 * Returns: (transfer full): a pointer to the newly added entry in the index.
565 gst_index_add_format (GstIndex * index, gint id, GstFormat format)
567 GstIndexEntry *entry;
568 const GstFormatDefinition *def;
570 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
571 g_return_val_if_fail (format != 0, NULL);
573 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
576 entry = g_slice_new (GstIndexEntry);
577 entry->type = GST_INDEX_ENTRY_FORMAT;
579 entry->data.format.format = format;
581 def = gst_format_get_details (format);
582 entry->data.format.key = def->nick;
584 gst_index_add_entry (index, entry);
591 * @index: the index to add the entry to
592 * @id: the id of the index writer
593 * @description: the description of the index writer
595 * Add an id entry into the index.
597 * Returns: a pointer to the newly added entry in the index.
600 gst_index_add_id (GstIndex * index, gint id, gchar * description)
602 GstIndexEntry *entry;
604 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
605 g_return_val_if_fail (description != NULL, NULL);
607 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
610 entry = g_slice_new (GstIndexEntry);
611 entry->type = GST_INDEX_ENTRY_ID;
613 entry->data.id.description = description;
615 gst_index_add_entry (index, entry);
621 gst_index_path_resolver (GstIndex * index, GstObject * writer,
622 gchar ** writer_string, gpointer data)
624 *writer_string = gst_object_get_path_string (writer);
630 gst_index_gtype_resolver (GstIndex * index, GstObject * writer,
631 gchar ** writer_string, gpointer data)
633 g_return_val_if_fail (writer != NULL, FALSE);
635 if (GST_IS_PAD (writer)) {
636 GstElement *element =
637 (GstElement *) gst_object_get_parent (GST_OBJECT (writer));
640 name = gst_object_get_name (writer);
641 *writer_string = g_strdup_printf ("%s.%s",
642 g_type_name (G_OBJECT_TYPE (element)), name);
644 gst_object_unref (element);
649 g_strdup_printf ("%s", g_type_name (G_OBJECT_TYPE (writer)));
656 * gst_index_get_writer_id:
657 * @index: the index to get a unique write id for
658 * @writer: the GstObject to allocate an id for
659 * @id: a pointer to a gint to hold the id
661 * Before entries can be added to the index, a writer
662 * should obtain a unique id. The methods to add new entries
663 * to the index require this id as an argument.
665 * The application can implement a custom function to map the writer object
666 * to a string. That string will be used to register or look up an id
669 * Returns: TRUE if the writer would be mapped to an id.
672 gst_index_get_writer_id (GstIndex * index, GstObject * writer, gint * id)
674 gchar *writer_string = NULL;
675 GstIndexEntry *entry;
676 GstIndexClass *iclass;
677 gboolean success = FALSE;
679 g_return_val_if_fail (GST_IS_INDEX (index), FALSE);
680 g_return_val_if_fail (GST_IS_OBJECT (writer), FALSE);
681 g_return_val_if_fail (id, FALSE);
685 /* first try to get a previously cached id */
686 entry = g_hash_table_lookup (index->writers, writer);
689 iclass = GST_INDEX_GET_CLASS (index);
691 /* let the app make a string */
692 if (index->resolver) {
696 index->resolver (index, writer, &writer_string,
697 index->resolver_user_data);
701 g_warning ("no resolver found");
705 /* if the index has a resolver, make it map this string to an id */
706 if (iclass->get_writer_id) {
707 success = iclass->get_writer_id (index, id, writer_string);
709 /* if the index could not resolve, we allocate one ourselves */
711 *id = ++index->last_id;
714 entry = gst_index_add_id (index, *id, writer_string);
716 /* index is probably not writable, make an entry anyway
717 * to keep it in our cache */
718 entry = g_slice_new (GstIndexEntry);
719 entry->type = GST_INDEX_ENTRY_ID;
721 entry->data.id.description = writer_string;
723 g_hash_table_insert (index->writers, writer, entry);
732 gst_index_add_entry (GstIndex * index, GstIndexEntry * entry)
734 GstIndexClass *iclass;
736 iclass = GST_INDEX_GET_CLASS (index);
738 if (iclass->add_entry) {
739 iclass->add_entry (index, entry);
742 g_signal_emit (index, gst_index_signals[ENTRY_ADDED], 0, entry);
746 * gst_index_add_associationv:
747 * @index: the index to add the entry to
748 * @id: the id of the index writer
749 * @flags: optinal flags for this entry
750 * @n: number of associations
751 * @list: list of associations
753 * Associate given format/value pairs with each other.
755 * Returns: a pointer to the newly added entry in the index.
758 gst_index_add_associationv (GstIndex * index, gint id, GstAssocFlags flags,
759 gint n, const GstIndexAssociation * list)
761 GstIndexEntry *entry;
763 g_return_val_if_fail (n > 0, NULL);
764 g_return_val_if_fail (list != NULL, NULL);
765 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
767 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
770 entry = g_slice_new (GstIndexEntry);
772 entry->type = GST_INDEX_ENTRY_ASSOCIATION;
774 entry->data.assoc.flags = flags;
775 entry->data.assoc.assocs = g_memdup (list, sizeof (GstIndexAssociation) * n);
776 entry->data.assoc.nassocs = n;
778 gst_index_add_entry (index, entry);
784 * gst_index_add_association:
785 * @index: the index to add the entry to
786 * @id: the id of the index writer
787 * @flags: optinal flags for this entry
788 * @format: the format of the value
790 * @...: other format/value pairs or 0 to end the list
792 * Associate given format/value pairs with each other.
793 * Be sure to pass gint64 values to this functions varargs,
794 * you might want to use a gint64 cast to be sure.
796 * Returns: a pointer to the newly added entry in the index.
799 gst_index_add_association (GstIndex * index, gint id, GstAssocFlags flags,
800 GstFormat format, gint64 value, ...)
803 GstIndexEntry *entry;
804 GstIndexAssociation *list;
806 GstFormat cur_format;
809 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
810 g_return_val_if_fail (format != 0, NULL);
812 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
815 array = g_array_new (FALSE, FALSE, sizeof (GstIndexAssociation));
818 GstIndexAssociation a;
823 g_array_append_val (array, a);
826 va_start (args, value);
828 while ((cur_format = va_arg (args, GstFormat))) {
829 GstIndexAssociation a;
831 a.format = cur_format;
832 a.value = va_arg (args, gint64);
834 g_array_append_val (array, a);
839 list = (GstIndexAssociation *) g_array_free (array, FALSE);
841 entry = gst_index_add_associationv (index, id, flags, n_assocs, list);
848 * gst_index_add_object:
849 * @index: the index to add the object to
850 * @id: the id of the index writer
851 * @key: a key for the object
852 * @type: the GType of the object
853 * @object: a pointer to the object to add
855 * Add the given object to the index with the given key.
857 * This function is not yet implemented.
859 * Returns: a pointer to the newly added entry in the index.
862 gst_index_add_object (GstIndex * index, gint id, gchar * key,
863 GType type, gpointer object)
865 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
872 gst_index_compare_func (gconstpointer a, gconstpointer b, gpointer user_data)
882 * gst_index_get_assoc_entry:
883 * @index: the index to search
884 * @id: the id of the index writer
885 * @method: The lookup method to use
886 * @flags: Flags for the entry
887 * @format: the format of the value
888 * @value: the value to find
890 * Finds the given format/value in the index
892 * Returns: the entry associated with the value or NULL if the
893 * value was not found.
896 gst_index_get_assoc_entry (GstIndex * index, gint id,
897 GstIndexLookupMethod method, GstAssocFlags flags,
898 GstFormat format, gint64 value)
900 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
905 return gst_index_get_assoc_entry_full (index, id, method, flags, format,
906 value, gst_index_compare_func, NULL);
910 * gst_index_get_assoc_entry_full:
911 * @index: the index to search
912 * @id: the id of the index writer
913 * @method: The lookup method to use
914 * @flags: Flags for the entry
915 * @format: the format of the value
916 * @value: the value to find
917 * @func: the function used to compare entries
918 * @user_data: user data passed to the compare function
920 * Finds the given format/value in the index with the given
921 * compare function and user_data.
923 * Returns: the entry associated with the value or NULL if the
924 * value was not found.
927 gst_index_get_assoc_entry_full (GstIndex * index, gint id,
928 GstIndexLookupMethod method, GstAssocFlags flags,
929 GstFormat format, gint64 value, GCompareDataFunc func, gpointer user_data)
931 GstIndexClass *iclass;
933 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
938 iclass = GST_INDEX_GET_CLASS (index);
940 if (iclass->get_assoc_entry)
941 return iclass->get_assoc_entry (index, id, method, flags, format, value,
948 * gst_index_entry_assoc_map:
949 * @entry: the index to search
950 * @format: the format of the value the find
951 * @value: a pointer to store the value
953 * Gets alternative formats associated with the indexentry.
955 * Returns: TRUE if there was a value associated with the given
959 gst_index_entry_assoc_map (GstIndexEntry * entry,
960 GstFormat format, gint64 * value)
964 g_return_val_if_fail (entry != NULL, FALSE);
965 g_return_val_if_fail (value != NULL, FALSE);
967 for (i = 0; i < GST_INDEX_NASSOCS (entry); i++) {
968 if (GST_INDEX_ASSOC_FORMAT (entry, i) == format) {
969 *value = GST_INDEX_ASSOC_VALUE (entry, i);