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 #include "gst_private.h"
36 #include "gstindexfactory.h"
37 #include "gstmarshal.h"
38 #include "gstregistry.h"
39 /* for constructing an entry name */
40 #include "gstelement.h"
44 /* Index signals and args */
58 GST_DEBUG_CATEGORY_STATIC (index_debug);
59 #define GST_CAT_DEFAULT index_debug
61 static void gst_index_finalize (GObject * object);
63 static void gst_index_set_property (GObject * object, guint prop_id,
64 const GValue * value, GParamSpec * pspec);
65 static void gst_index_get_property (GObject * object, guint prop_id,
66 GValue * value, GParamSpec * pspec);
68 static GstIndexGroup *gst_index_group_new (guint groupnum);
69 static void gst_index_group_free (GstIndexGroup * group);
71 static gboolean gst_index_path_resolver (GstIndex * index, GstObject * writer,
72 gchar ** writer_string, gpointer data);
73 static gboolean gst_index_gtype_resolver (GstIndex * index, GstObject * writer,
74 gchar ** writer_string, gpointer data);
75 static void gst_index_add_entry (GstIndex * index, GstIndexEntry * entry);
77 static GstObject *parent_class = NULL;
78 static guint gst_index_signals[LAST_SIGNAL] = { 0 };
82 GstIndexResolverMethod method;
83 GstIndexResolver resolver;
88 static const ResolverEntry resolvers[] = {
89 {GST_INDEX_RESOLVER_CUSTOM, NULL, NULL},
90 {GST_INDEX_RESOLVER_GTYPE, gst_index_gtype_resolver, NULL},
91 {GST_INDEX_RESOLVER_PATH, gst_index_path_resolver, NULL},
94 #define GST_TYPE_INDEX_RESOLVER (gst_index_resolver_get_type())
96 gst_index_resolver_get_type (void)
98 static GType index_resolver_type = 0;
99 static const GEnumValue index_resolver[] = {
100 {GST_INDEX_RESOLVER_CUSTOM, "GST_INDEX_RESOLVER_CUSTOM", "custom"},
101 {GST_INDEX_RESOLVER_GTYPE, "GST_INDEX_RESOLVER_GTYPE", "gtype"},
102 {GST_INDEX_RESOLVER_PATH, "GST_INDEX_RESOLVER_PATH", "path"},
106 if (!index_resolver_type) {
107 index_resolver_type =
108 g_enum_register_static ("GstIndexResolver", index_resolver);
110 return index_resolver_type;
114 gst_index_entry_get_type (void)
116 static GType index_entry_type = 0;
118 if (!index_entry_type) {
119 index_entry_type = g_boxed_type_register_static ("GstIndexEntry",
120 (GBoxedCopyFunc) gst_index_entry_copy,
121 (GBoxedFreeFunc) gst_index_entry_free);
123 return index_entry_type;
128 GST_DEBUG_CATEGORY_INIT (index_debug, "GST_INDEX", GST_DEBUG_BOLD, \
129 "Generic indexing support"); \
132 G_DEFINE_TYPE_WITH_CODE (GstIndex, gst_index, GST_TYPE_OBJECT, _do_init);
135 gst_index_class_init (GstIndexClass * klass)
137 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
139 parent_class = g_type_class_peek_parent (klass);
142 * GstIndex::entry-added
143 * @gstindex: the object which received the signal.
144 * @arg1: The entry added to the index.
146 * Is emitted when a new entry is added to the index.
148 gst_index_signals[ENTRY_ADDED] =
149 g_signal_new ("entry-added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
150 G_STRUCT_OFFSET (GstIndexClass, entry_added), NULL, NULL,
151 gst_marshal_VOID__BOXED, G_TYPE_NONE, 1, GST_TYPE_INDEX_ENTRY);
153 gobject_class->set_property = gst_index_set_property;
154 gobject_class->get_property = gst_index_get_property;
155 gobject_class->finalize = gst_index_finalize;
157 g_object_class_install_property (gobject_class, ARG_RESOLVER,
158 g_param_spec_enum ("resolver", "Resolver",
159 "Select a predefined object to string mapper",
160 GST_TYPE_INDEX_RESOLVER, GST_INDEX_RESOLVER_PATH,
161 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
165 gst_index_init (GstIndex * index)
167 index->curgroup = gst_index_group_new (0);
169 index->groups = g_list_prepend (NULL, index->curgroup);
171 index->writers = g_hash_table_new (NULL, NULL);
174 index->method = GST_INDEX_RESOLVER_PATH;
175 index->resolver = resolvers[index->method].resolver;
176 index->resolver_user_data = resolvers[index->method].user_data;
178 GST_OBJECT_FLAG_SET (index, GST_INDEX_WRITABLE);
179 GST_OBJECT_FLAG_SET (index, GST_INDEX_READABLE);
181 GST_DEBUG ("created new index");
185 gst_index_free_writer (gpointer key, gpointer value, gpointer user_data)
187 GstIndexEntry *entry = (GstIndexEntry *) value;
190 gst_index_entry_free (entry);
195 gst_index_finalize (GObject * object)
197 GstIndex *index = GST_INDEX (object);
200 g_list_foreach (index->groups, (GFunc) gst_index_group_free, NULL);
201 g_list_free (index->groups);
202 index->groups = NULL;
205 if (index->writers) {
206 g_hash_table_foreach (index->writers, gst_index_free_writer, NULL);
207 g_hash_table_destroy (index->writers);
208 index->writers = NULL;
211 if (index->filter_user_data && index->filter_user_data_destroy)
212 index->filter_user_data_destroy (index->filter_user_data);
214 if (index->resolver_user_data && index->resolver_user_data_destroy)
215 index->resolver_user_data_destroy (index->resolver_user_data);
217 G_OBJECT_CLASS (parent_class)->finalize (object);
221 gst_index_set_property (GObject * object, guint prop_id,
222 const GValue * value, GParamSpec * pspec)
226 index = GST_INDEX (object);
230 index->method = g_value_get_enum (value);
231 index->resolver = resolvers[index->method].resolver;
232 index->resolver_user_data = resolvers[index->method].user_data;
235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241 gst_index_get_property (GObject * object, guint prop_id,
242 GValue * value, GParamSpec * pspec)
246 index = GST_INDEX (object);
250 g_value_set_enum (value, index->method);
253 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
258 static GstIndexGroup *
259 gst_index_group_new (guint groupnum)
261 GstIndexGroup *indexgroup = g_slice_new (GstIndexGroup);
263 indexgroup->groupnum = groupnum;
264 indexgroup->entries = NULL;
265 indexgroup->certainty = GST_INDEX_UNKNOWN;
266 indexgroup->peergroup = -1;
268 GST_DEBUG ("created new index group %d", groupnum);
274 gst_index_group_free (GstIndexGroup * group)
276 g_slice_free (GstIndexGroup, group);
282 * Create a new tileindex object
284 * Returns: a new index object
291 index = g_object_newv (gst_index_get_type (), 0, NULL);
298 * @index: the index to commit
299 * @id: the writer that commited the index
301 * Tell the index that the writer with the given id is done
302 * with this index and is not going to write any more entries
306 gst_index_commit (GstIndex * index, gint id)
308 GstIndexClass *iclass;
310 iclass = GST_INDEX_GET_CLASS (index);
313 iclass->commit (index, id);
318 * gst_index_get_group:
319 * @index: the index to get the current group from
321 * Get the id of the current group.
323 * Returns: the id of the current group.
326 gst_index_get_group (GstIndex * index)
328 return index->curgroup->groupnum;
332 * gst_index_new_group:
333 * @index: the index to create the new group in
335 * Create a new group for the given index. It will be
336 * set as the current group.
338 * Returns: the id of the newly created group.
341 gst_index_new_group (GstIndex * index)
343 index->curgroup = gst_index_group_new (++index->maxgroup);
344 index->groups = g_list_append (index->groups, index->curgroup);
345 GST_DEBUG ("created new group %d in index", index->maxgroup);
346 return index->maxgroup;
350 * gst_index_set_group:
351 * @index: the index to set the new group in
352 * @groupnum: the groupnumber to set
354 * Set the current groupnumber to the given argument.
356 * Returns: TRUE if the operation succeeded, FALSE if the group
360 gst_index_set_group (GstIndex * index, gint groupnum)
363 GstIndexGroup *indexgroup;
365 /* first check for null change */
366 if (groupnum == index->curgroup->groupnum)
369 /* else search for the proper group */
370 list = index->groups;
372 indexgroup = (GstIndexGroup *) (list->data);
373 list = g_list_next (list);
374 if (indexgroup->groupnum == groupnum) {
375 index->curgroup = indexgroup;
376 GST_DEBUG ("switched to index group %d", indexgroup->groupnum);
381 /* couldn't find the group in question */
382 GST_DEBUG ("couldn't find index group %d", groupnum);
387 * gst_index_set_certainty:
388 * @index: the index to set the certainty on
389 * @certainty: the certainty to set
391 * Set the certainty of the given index.
394 gst_index_set_certainty (GstIndex * index, GstIndexCertainty certainty)
396 index->curgroup->certainty = certainty;
400 * gst_index_get_certainty:
401 * @index: the index to get the certainty of
403 * Get the certainty of the given index.
405 * Returns: the certainty of the index.
408 gst_index_get_certainty (GstIndex * index)
410 return index->curgroup->certainty;
414 * gst_index_set_filter:
415 * @index: the index to register the filter on
416 * @filter: the filter to register
417 * @user_data: data passed to the filter function
419 * Lets the app register a custom filter function so that
420 * it can select what entries should be stored in the index.
423 gst_index_set_filter (GstIndex * index,
424 GstIndexFilter filter, gpointer user_data)
426 g_return_if_fail (GST_IS_INDEX (index));
428 gst_index_set_filter_full (index, filter, user_data, NULL);
432 * gst_index_set_filter_full:
433 * @index: the index to register the filter on
434 * @filter: the filter to register
435 * @user_data: data passed to the filter function
436 * @user_data_destroy: function to call when @user_data is unset
438 * Lets the app register a custom filter function so that
439 * it can select what entries should be stored in the index.
442 gst_index_set_filter_full (GstIndex * index,
443 GstIndexFilter filter, gpointer user_data, GDestroyNotify user_data_destroy)
445 g_return_if_fail (GST_IS_INDEX (index));
447 if (index->filter_user_data && index->filter_user_data_destroy)
448 index->filter_user_data_destroy (index->filter_user_data);
450 index->filter = filter;
451 index->filter_user_data = user_data;
452 index->filter_user_data_destroy = user_data_destroy;
456 * gst_index_set_resolver:
457 * @index: the index to register the resolver on
458 * @resolver: the resolver to register
459 * @user_data: data passed to the resolver function
461 * Lets the app register a custom function to map index
462 * ids to writer descriptions.
465 gst_index_set_resolver (GstIndex * index,
466 GstIndexResolver resolver, gpointer user_data)
468 gst_index_set_resolver_full (index, resolver, user_data, NULL);
472 * gst_index_set_resolver_full:
473 * @index: the index to register the resolver on
474 * @resolver: the resolver to register
475 * @user_data: data passed to the resolver function
476 * @user_data_destroy: destroy function for @user_data
478 * Lets the app register a custom function to map index
479 * ids to writer descriptions.
484 gst_index_set_resolver_full (GstIndex * index, GstIndexResolver resolver,
485 gpointer user_data, GDestroyNotify user_data_destroy)
487 g_return_if_fail (GST_IS_INDEX (index));
489 if (index->resolver_user_data && index->resolver_user_data_destroy)
490 index->resolver_user_data_destroy (index->resolver_user_data);
492 index->resolver = resolver;
493 index->resolver_user_data = user_data;
494 index->resolver_user_data_destroy = user_data_destroy;
495 index->method = GST_INDEX_RESOLVER_CUSTOM;
499 * gst_index_entry_copy:
500 * @entry: the entry to copy
502 * Copies an entry and returns the result.
504 * Returns: a newly allocated #GstIndexEntry.
507 gst_index_entry_copy (GstIndexEntry * entry)
509 GstIndexEntry *new_entry = g_slice_new (GstIndexEntry);
511 memcpy (new_entry, entry, sizeof (GstIndexEntry));
516 * gst_index_entry_free:
517 * @entry: the entry to free
519 * Free the memory used by the given entry.
522 gst_index_entry_free (GstIndexEntry * entry)
524 switch (entry->type) {
525 case GST_INDEX_ENTRY_ID:
526 if (entry->data.id.description) {
527 g_free (entry->data.id.description);
528 entry->data.id.description = NULL;
531 case GST_INDEX_ENTRY_ASSOCIATION:
532 if (entry->data.assoc.assocs) {
533 g_free (entry->data.assoc.assocs);
534 entry->data.assoc.assocs = NULL;
537 case GST_INDEX_ENTRY_OBJECT:
539 case GST_INDEX_ENTRY_FORMAT:
543 g_slice_free (GstIndexEntry, entry);
547 * gst_index_add_format:
548 * @index: the index to add the entry to
549 * @id: the id of the index writer
550 * @format: the format to add to the index
552 * Adds a format entry into the index. This function is
553 * used to map dynamic GstFormat ids to their original
556 * Returns: a pointer to the newly added entry in the index.
559 gst_index_add_format (GstIndex * index, gint id, GstFormat format)
561 GstIndexEntry *entry;
562 const GstFormatDefinition *def;
564 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
565 g_return_val_if_fail (format != 0, NULL);
567 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
570 entry = g_slice_new (GstIndexEntry);
571 entry->type = GST_INDEX_ENTRY_FORMAT;
573 entry->data.format.format = format;
575 def = gst_format_get_details (format);
576 entry->data.format.key = def->nick;
578 gst_index_add_entry (index, entry);
585 * @index: the index to add the entry to
586 * @id: the id of the index writer
587 * @description: the description of the index writer
589 * Add an id entry into the index.
591 * Returns: a pointer to the newly added entry in the index.
594 gst_index_add_id (GstIndex * index, gint id, gchar * description)
596 GstIndexEntry *entry;
598 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
599 g_return_val_if_fail (description != NULL, NULL);
601 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
604 entry = g_slice_new (GstIndexEntry);
605 entry->type = GST_INDEX_ENTRY_ID;
607 entry->data.id.description = description;
609 gst_index_add_entry (index, entry);
615 gst_index_path_resolver (GstIndex * index, GstObject * writer,
616 gchar ** writer_string, gpointer data)
618 *writer_string = gst_object_get_path_string (writer);
624 gst_index_gtype_resolver (GstIndex * index, GstObject * writer,
625 gchar ** writer_string, gpointer data)
627 g_return_val_if_fail (writer != NULL, FALSE);
629 if (GST_IS_PAD (writer)) {
630 GstElement *element =
631 (GstElement *) gst_object_get_parent (GST_OBJECT (writer));
634 name = gst_object_get_name (writer);
635 *writer_string = g_strdup_printf ("%s.%s",
636 g_type_name (G_OBJECT_TYPE (element)), name);
638 gst_object_unref (element);
643 g_strdup_printf ("%s", g_type_name (G_OBJECT_TYPE (writer)));
650 * gst_index_get_writer_id:
651 * @index: the index to get a unique write id for
652 * @writer: the GstObject to allocate an id for
653 * @id: a pointer to a gint to hold the id
655 * Before entries can be added to the index, a writer
656 * should obtain a unique id. The methods to add new entries
657 * to the index require this id as an argument.
659 * The application can implement a custom function to map the writer object
660 * to a string. That string will be used to register or look up an id
663 * Returns: TRUE if the writer would be mapped to an id.
666 gst_index_get_writer_id (GstIndex * index, GstObject * writer, gint * id)
668 gchar *writer_string = NULL;
669 GstIndexEntry *entry;
670 GstIndexClass *iclass;
671 gboolean success = FALSE;
673 g_return_val_if_fail (GST_IS_INDEX (index), FALSE);
674 g_return_val_if_fail (GST_IS_OBJECT (writer), FALSE);
675 g_return_val_if_fail (id, FALSE);
679 /* first try to get a previously cached id */
680 entry = g_hash_table_lookup (index->writers, writer);
683 iclass = GST_INDEX_GET_CLASS (index);
685 /* let the app make a string */
686 if (index->resolver) {
690 index->resolver (index, writer, &writer_string,
691 index->resolver_user_data);
695 g_warning ("no resolver found");
699 /* if the index has a resolver, make it map this string to an id */
700 if (iclass->get_writer_id) {
701 success = iclass->get_writer_id (index, id, writer_string);
703 /* if the index could not resolve, we allocate one ourselves */
705 *id = ++index->last_id;
708 entry = gst_index_add_id (index, *id, writer_string);
710 /* index is probably not writable, make an entry anyway
711 * to keep it in our cache */
712 entry = g_slice_new (GstIndexEntry);
713 entry->type = GST_INDEX_ENTRY_ID;
715 entry->data.id.description = writer_string;
717 g_hash_table_insert (index->writers, writer, entry);
726 gst_index_add_entry (GstIndex * index, GstIndexEntry * entry)
728 GstIndexClass *iclass;
730 iclass = GST_INDEX_GET_CLASS (index);
732 if (iclass->add_entry) {
733 iclass->add_entry (index, entry);
736 g_signal_emit (index, gst_index_signals[ENTRY_ADDED], 0, entry);
740 * gst_index_add_associationv:
741 * @index: the index to add the entry to
742 * @id: the id of the index writer
743 * @flags: optinal flags for this entry
744 * @n: number of associations
745 * @list: list of associations
747 * Associate given format/value pairs with each other.
749 * Returns: a pointer to the newly added entry in the index.
752 gst_index_add_associationv (GstIndex * index, gint id, GstAssocFlags flags,
753 gint n, const GstIndexAssociation * list)
755 GstIndexEntry *entry;
757 g_return_val_if_fail (n > 0, NULL);
758 g_return_val_if_fail (list != NULL, NULL);
759 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
761 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
764 entry = g_slice_new (GstIndexEntry);
766 entry->type = GST_INDEX_ENTRY_ASSOCIATION;
768 entry->data.assoc.flags = flags;
769 entry->data.assoc.assocs = g_memdup (list, sizeof (GstIndexAssociation) * n);
770 entry->data.assoc.nassocs = n;
772 gst_index_add_entry (index, entry);
778 * gst_index_add_association:
779 * @index: the index to add the entry to
780 * @id: the id of the index writer
781 * @flags: optinal flags for this entry
782 * @format: the format of the value
784 * @...: other format/value pairs or 0 to end the list
786 * Associate given format/value pairs with each other.
787 * Be sure to pass gint64 values to this functions varargs,
788 * you might want to use a gint64 cast to be sure.
790 * Returns: a pointer to the newly added entry in the index.
793 gst_index_add_association (GstIndex * index, gint id, GstAssocFlags flags,
794 GstFormat format, gint64 value, ...)
797 GstIndexEntry *entry;
798 GstIndexAssociation *list;
800 GstFormat cur_format;
803 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
804 g_return_val_if_fail (format != 0, NULL);
806 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
809 array = g_array_new (FALSE, FALSE, sizeof (GstIndexAssociation));
812 GstIndexAssociation a;
817 g_array_append_val (array, a);
820 va_start (args, value);
822 while ((cur_format = va_arg (args, GstFormat))) {
823 GstIndexAssociation a;
825 a.format = cur_format;
826 a.value = va_arg (args, gint64);
828 g_array_append_val (array, a);
833 list = (GstIndexAssociation *) g_array_free (array, FALSE);
835 entry = gst_index_add_associationv (index, id, flags, n_assocs, list);
842 * gst_index_add_object:
843 * @index: the index to add the object to
844 * @id: the id of the index writer
845 * @key: a key for the object
846 * @type: the GType of the object
847 * @object: a pointer to the object to add
849 * Add the given object to the index with the given key.
851 * This function is not yet implemented.
853 * Returns: a pointer to the newly added entry in the index.
856 gst_index_add_object (GstIndex * index, gint id, gchar * key,
857 GType type, gpointer object)
859 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
866 gst_index_compare_func (gconstpointer a, gconstpointer b, gpointer user_data)
876 * gst_index_get_assoc_entry:
877 * @index: the index to search
878 * @id: the id of the index writer
879 * @method: The lookup method to use
880 * @flags: Flags for the entry
881 * @format: the format of the value
882 * @value: the value to find
884 * Finds the given format/value in the index
886 * Returns: the entry associated with the value or NULL if the
887 * value was not found.
890 gst_index_get_assoc_entry (GstIndex * index, gint id,
891 GstIndexLookupMethod method, GstAssocFlags flags,
892 GstFormat format, gint64 value)
894 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
899 return gst_index_get_assoc_entry_full (index, id, method, flags, format,
900 value, gst_index_compare_func, NULL);
904 * gst_index_get_assoc_entry_full:
905 * @index: the index to search
906 * @id: the id of the index writer
907 * @method: The lookup method to use
908 * @flags: Flags for the entry
909 * @format: the format of the value
910 * @value: the value to find
911 * @func: the function used to compare entries
912 * @user_data: user data passed to the compare function
914 * Finds the given format/value in the index with the given
915 * compare function and user_data.
917 * Returns: the entry associated with the value or NULL if the
918 * value was not found.
921 gst_index_get_assoc_entry_full (GstIndex * index, gint id,
922 GstIndexLookupMethod method, GstAssocFlags flags,
923 GstFormat format, gint64 value, GCompareDataFunc func, gpointer user_data)
925 GstIndexClass *iclass;
927 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
932 iclass = GST_INDEX_GET_CLASS (index);
934 if (iclass->get_assoc_entry)
935 return iclass->get_assoc_entry (index, id, method, flags, format, value,
942 * gst_index_entry_assoc_map:
943 * @entry: the index to search
944 * @format: the format of the value the find
945 * @value: a pointer to store the value
947 * Gets alternative formats associated with the indexentry.
949 * Returns: TRUE if there was a value associated with the given
953 gst_index_entry_assoc_map (GstIndexEntry * entry,
954 GstFormat format, gint64 * value)
958 g_return_val_if_fail (entry != NULL, FALSE);
959 g_return_val_if_fail (value != NULL, FALSE);
961 for (i = 0; i < GST_INDEX_NASSOCS (entry); i++) {
962 if (GST_INDEX_ASSOC_FORMAT (entry, i) == format) {
963 *value = GST_INDEX_ASSOC_VALUE (entry, i);