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.
24 * @short_description: Generate indexes on objects
25 * @see_also: #GstIndexFactory
27 * GstIndex is used to generate a stream index of one or more elements
31 #include "gst_private.h"
35 #include "gstindexfactory.h"
36 #include "gstmarshal.h"
37 #include "gstregistry.h"
38 /* for constructing an entry name */
39 #include "gstelement.h"
42 /* Index signals and args */
56 static void gst_index_class_init (GstIndexClass * klass);
57 static void gst_index_init (GstIndex * index);
59 static void gst_index_set_property (GObject * object, guint prop_id,
60 const GValue * value, GParamSpec * pspec);
61 static void gst_index_get_property (GObject * object, guint prop_id,
62 GValue * value, GParamSpec * pspec);
64 static GstIndexGroup *gst_index_group_new (guint groupnum);
66 static gboolean gst_index_path_resolver (GstIndex * index, GstObject * writer,
67 gchar ** writer_string, gpointer data);
68 static gboolean gst_index_gtype_resolver (GstIndex * index, GstObject * writer,
69 gchar ** writer_string, gpointer data);
70 static void gst_index_add_entry (GstIndex * index, GstIndexEntry * entry);
72 static GstObject *parent_class = NULL;
73 static guint gst_index_signals[LAST_SIGNAL] = { 0 };
77 GstIndexResolverMethod method;
78 GstIndexResolver resolver;
83 static const ResolverEntry resolvers[] = {
84 {GST_INDEX_RESOLVER_CUSTOM, NULL, NULL},
85 {GST_INDEX_RESOLVER_GTYPE, gst_index_gtype_resolver, NULL},
86 {GST_INDEX_RESOLVER_PATH, gst_index_path_resolver, NULL},
89 #define GST_TYPE_INDEX_RESOLVER (gst_index_resolver_get_type())
91 gst_index_resolver_get_type (void)
93 static GType index_resolver_type = 0;
94 static GEnumValue index_resolver[] = {
95 {GST_INDEX_RESOLVER_CUSTOM, "GST_INDEX_RESOLVER_CUSTOM",
96 "Use a custom resolver"},
97 {GST_INDEX_RESOLVER_GTYPE, "GST_INDEX_RESOLVER_GTYPE",
98 "Resolve an object to its GType[.padname]"},
99 {GST_INDEX_RESOLVER_PATH, "GST_INDEX_RESOLVER_PATH",
100 "Resolve an object to its path in the pipeline"},
104 if (!index_resolver_type) {
105 index_resolver_type =
106 g_enum_register_static ("GstIndexResolver", index_resolver);
108 return index_resolver_type;
112 gst_index_entry_get_type (void)
114 static GType index_entry_type = 0;
116 if (!index_entry_type) {
117 index_entry_type = g_boxed_type_register_static ("GstIndexEntry",
118 (GBoxedCopyFunc) gst_index_entry_copy,
119 (GBoxedFreeFunc) gst_index_entry_free);
121 return index_entry_type;
126 gst_index_get_type (void)
128 static GType index_type = 0;
131 static const GTypeInfo index_info = {
132 sizeof (GstIndexClass),
135 (GClassInitFunc) gst_index_class_init,
140 (GInstanceInitFunc) gst_index_init,
145 g_type_register_static (GST_TYPE_OBJECT, "GstIndex", &index_info, 0);
151 gst_index_class_init (GstIndexClass * klass)
153 GObjectClass *gobject_class;
154 GstElementClass *gstelement_class;
156 gobject_class = (GObjectClass *) klass;
157 gstelement_class = (GstElementClass *) klass;
159 parent_class = g_type_class_ref (GST_TYPE_OBJECT);
162 * GstIndex::entry-added
163 * @gstindex: the object which received the signal.
164 * @arg1: The entry added to the index.
166 * Is emitted when a new entry is added to the index.
168 gst_index_signals[ENTRY_ADDED] =
169 g_signal_new ("entry-added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
170 G_STRUCT_OFFSET (GstIndexClass, entry_added), NULL, NULL,
171 gst_marshal_VOID__BOXED, G_TYPE_NONE, 1, GST_TYPE_INDEX_ENTRY);
173 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_index_set_property);
174 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_index_get_property);
176 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_RESOLVER,
177 g_param_spec_enum ("resolver", "Resolver",
178 "Select a predefined object to string mapper",
179 GST_TYPE_INDEX_RESOLVER, GST_INDEX_RESOLVER_PATH, G_PARAM_READWRITE));
183 gst_index_init (GstIndex * index)
185 index->curgroup = gst_index_group_new (0);
187 index->groups = g_list_prepend (NULL, index->curgroup);
189 index->writers = g_hash_table_new (NULL, NULL);
192 index->method = GST_INDEX_RESOLVER_PATH;
193 index->resolver = resolvers[index->method].resolver;
194 index->resolver_user_data = resolvers[index->method].user_data;
196 GST_FLAG_SET (index, GST_INDEX_WRITABLE);
197 GST_FLAG_SET (index, GST_INDEX_READABLE);
199 GST_DEBUG ("created new index");
203 gst_index_set_property (GObject * object, guint prop_id,
204 const GValue * value, GParamSpec * pspec)
208 index = GST_INDEX (object);
212 index->method = g_value_get_enum (value);
213 index->resolver = resolvers[index->method].resolver;
214 index->resolver_user_data = resolvers[index->method].user_data;
217 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223 gst_index_get_property (GObject * object, guint prop_id,
224 GValue * value, GParamSpec * pspec)
228 index = GST_INDEX (object);
232 g_value_set_enum (value, index->method);
235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
240 static GstIndexGroup *
241 gst_index_group_new (guint groupnum)
243 GstIndexGroup *indexgroup = g_new (GstIndexGroup, 1);
245 indexgroup->groupnum = groupnum;
246 indexgroup->entries = NULL;
247 indexgroup->certainty = GST_INDEX_UNKNOWN;
248 indexgroup->peergroup = -1;
250 GST_DEBUG ("created new index group %d", groupnum);
258 * Create a new tileindex object
260 * Returns: a new index object
267 index = g_object_new (gst_index_get_type (), NULL);
274 * @index: the index to commit
275 * @id: the writer that commited the index
277 * Tell the index that the writer with the given id is done
278 * with this index and is not going to write any more entries
282 gst_index_commit (GstIndex * index, gint id)
284 GstIndexClass *iclass;
286 iclass = GST_INDEX_GET_CLASS (index);
289 iclass->commit (index, id);
294 * gst_index_get_group:
295 * @index: the index to get the current group from
297 * Get the id of the current group.
299 * Returns: the id of the current group.
302 gst_index_get_group (GstIndex * index)
304 return index->curgroup->groupnum;
308 * gst_index_new_group:
309 * @index: the index to create the new group in
311 * Create a new group for the given index. It will be
312 * set as the current group.
314 * Returns: the id of the newly created group.
317 gst_index_new_group (GstIndex * index)
319 index->curgroup = gst_index_group_new (++index->maxgroup);
320 index->groups = g_list_append (index->groups, index->curgroup);
321 GST_DEBUG ("created new group %d in index", index->maxgroup);
322 return index->maxgroup;
326 * gst_index_set_group:
327 * @index: the index to set the new group in
328 * @groupnum: the groupnumber to set
330 * Set the current groupnumber to the given argument.
332 * Returns: TRUE if the operation succeeded, FALSE if the group
336 gst_index_set_group (GstIndex * index, gint groupnum)
339 GstIndexGroup *indexgroup;
341 /* first check for null change */
342 if (groupnum == index->curgroup->groupnum)
345 /* else search for the proper group */
346 list = index->groups;
348 indexgroup = (GstIndexGroup *) (list->data);
349 list = g_list_next (list);
350 if (indexgroup->groupnum == groupnum) {
351 index->curgroup = indexgroup;
352 GST_DEBUG ("switched to index group %d", indexgroup->groupnum);
357 /* couldn't find the group in question */
358 GST_DEBUG ("couldn't find index group %d", groupnum);
363 * gst_index_set_certainty:
364 * @index: the index to set the certainty on
365 * @certainty: the certainty to set
367 * Set the certainty of the given index.
370 gst_index_set_certainty (GstIndex * index, GstIndexCertainty certainty)
372 index->curgroup->certainty = certainty;
376 * gst_index_get_certainty:
377 * @index: the index to get the certainty of
379 * Get the certainty of the given index.
381 * Returns: the certainty of the index.
384 gst_index_get_certainty (GstIndex * index)
386 return index->curgroup->certainty;
390 * gst_index_set_filter:
391 * @index: the index to register the filter on
392 * @filter: the filter to register
393 * @user_data: data passed to the filter function
395 * Lets the app register a custom filter function so that
396 * it can select what entries should be stored in the index.
399 gst_index_set_filter (GstIndex * index,
400 GstIndexFilter filter, gpointer user_data)
402 g_return_if_fail (GST_IS_INDEX (index));
404 index->filter = filter;
405 index->filter_user_data = user_data;
409 * gst_index_set_resolver:
410 * @index: the index to register the resolver on
411 * @resolver: the resolver to register
412 * @user_data: data passed to the resolver function
414 * Lets the app register a custom function to map index
415 * ids to writer descriptions.
418 gst_index_set_resolver (GstIndex * index,
419 GstIndexResolver resolver, gpointer user_data)
421 g_return_if_fail (GST_IS_INDEX (index));
423 index->resolver = resolver;
424 index->resolver_user_data = user_data;
425 index->method = GST_INDEX_RESOLVER_CUSTOM;
429 * gst_index_entry_copy:
430 * @entry: the entry to copy
432 * Copies an entry and returns the result.
434 * Returns: a newly allocated #GstIndexEntry.
437 gst_index_entry_copy (GstIndexEntry * entry)
439 return g_memdup (entry, sizeof (*entry));
443 * gst_index_entry_free:
444 * @entry: the entry to free
446 * Free the memory used by the given entry.
449 gst_index_entry_free (GstIndexEntry * entry)
455 * gst_index_add_format:
456 * @index: the index to add the entry to
457 * @id: the id of the index writer
458 * @format: the format to add to the index
460 * Adds a format entry into the index. This function is
461 * used to map dynamic GstFormat ids to their original
464 * Returns: a pointer to the newly added entry in the index.
467 gst_index_add_format (GstIndex * index, gint id, GstFormat format)
469 GstIndexEntry *entry;
470 const GstFormatDefinition *def;
472 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
473 g_return_val_if_fail (format != 0, NULL);
475 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
478 entry = g_new0 (GstIndexEntry, 1);
479 entry->type = GST_INDEX_ENTRY_FORMAT;
481 entry->data.format.format = format;
483 def = gst_format_get_details (format);
484 entry->data.format.key = def->nick;
486 gst_index_add_entry (index, entry);
493 * @index: the index to add the entry to
494 * @id: the id of the index writer
495 * @description: the description of the index writer
497 * Add an id entry into the index.
499 * Returns: a pointer to the newly added entry in the index.
502 gst_index_add_id (GstIndex * index, gint id, gchar * description)
504 GstIndexEntry *entry;
506 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
507 g_return_val_if_fail (description != NULL, NULL);
509 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
512 entry = g_new0 (GstIndexEntry, 1);
513 entry->type = GST_INDEX_ENTRY_ID;
515 entry->data.id.description = description;
517 gst_index_add_entry (index, entry);
523 gst_index_path_resolver (GstIndex * index, GstObject * writer,
524 gchar ** writer_string, gpointer data)
526 *writer_string = gst_object_get_path_string (writer);
532 gst_index_gtype_resolver (GstIndex * index, GstObject * writer,
533 gchar ** writer_string, gpointer data)
535 if (GST_IS_PAD (writer)) {
536 GstElement *element =
537 (GstElement *) gst_object_get_parent (GST_OBJECT (writer));
539 *writer_string = g_strdup_printf ("%s.%s",
540 g_type_name (G_OBJECT_TYPE (element)), gst_object_get_name (writer));
543 g_strdup_printf ("%s", g_type_name (G_OBJECT_TYPE (writer)));
550 * gst_index_get_writer_id:
551 * @index: the index to get a unique write id for
552 * @writer: the GstObject to allocate an id for
553 * @id: a pointer to a gint to hold the id
555 * Before entries can be added to the index, a writer
556 * should obtain a unique id. The methods to add new entries
557 * to the index require this id as an argument.
559 * The application can implement a custom function to map the writer object
560 * to a string. That string will be used to register or look up an id
563 * Returns: TRUE if the writer would be mapped to an id.
566 gst_index_get_writer_id (GstIndex * index, GstObject * writer, gint * id)
568 gchar *writer_string = NULL;
569 GstIndexEntry *entry;
570 GstIndexClass *iclass;
571 gboolean success = FALSE;
573 g_return_val_if_fail (GST_IS_INDEX (index), FALSE);
574 g_return_val_if_fail (GST_IS_OBJECT (writer), FALSE);
575 g_return_val_if_fail (id, FALSE);
579 /* first try to get a previously cached id */
580 entry = g_hash_table_lookup (index->writers, writer);
583 iclass = GST_INDEX_GET_CLASS (index);
585 /* let the app make a string */
586 if (index->resolver) {
590 index->resolver (index, writer, &writer_string,
591 index->resolver_user_data);
595 g_warning ("no resolver found");
599 /* if the index has a resolver, make it map this string to an id */
600 if (iclass->get_writer_id) {
601 success = iclass->get_writer_id (index, id, writer_string);
603 /* if the index could not resolve, we allocate one ourselves */
605 *id = ++index->last_id;
608 entry = gst_index_add_id (index, *id, writer_string);
610 /* index is probably not writable, make an entry anyway
611 * to keep it in our cache */
612 entry = g_new0 (GstIndexEntry, 1);
613 entry->type = GST_INDEX_ENTRY_ID;
615 entry->data.id.description = writer_string;
617 g_hash_table_insert (index->writers, writer, entry);
626 gst_index_add_entry (GstIndex * index, GstIndexEntry * entry)
628 GstIndexClass *iclass;
630 iclass = GST_INDEX_GET_CLASS (index);
632 if (iclass->add_entry) {
633 iclass->add_entry (index, entry);
636 g_signal_emit (G_OBJECT (index), gst_index_signals[ENTRY_ADDED], 0, entry);
640 * gst_index_add_associationv:
641 * @index: the index to add the entry to
642 * @id: the id of the index writer
643 * @flags: optinal flags for this entry
644 * @n: number of associations
645 * @list: list of associations
646 * @...: other format/value pairs or 0 to end the list
648 * Associate given format/value pairs with each other.
650 * Returns: a pointer to the newly added entry in the index.
653 gst_index_add_associationv (GstIndex * index, gint id, GstAssocFlags flags,
654 int n, const GstIndexAssociation * list)
656 GstIndexEntry *entry;
658 g_return_val_if_fail (n > 0, NULL);
659 g_return_val_if_fail (list != NULL, NULL);
660 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
662 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
665 entry = g_malloc (sizeof (GstIndexEntry));
667 entry->type = GST_INDEX_ENTRY_ASSOCIATION;
669 entry->data.assoc.flags = flags;
670 entry->data.assoc.assocs = g_memdup (list, sizeof (GstIndexAssociation) * n);
671 entry->data.assoc.nassocs = n;
673 gst_index_add_entry (index, entry);
679 * gst_index_add_association:
680 * @index: the index to add the entry to
681 * @id: the id of the index writer
682 * @flags: optinal flags for this entry
683 * @format: the format of the value
685 * @...: other format/value pairs or 0 to end the list
687 * Associate given format/value pairs with each other.
688 * Be sure to pass gint64 values to this functions varargs,
689 * you might want to use a gint64 cast to be sure.
691 * Returns: a pointer to the newly added entry in the index.
694 gst_index_add_association (GstIndex * index, gint id, GstAssocFlags flags,
695 GstFormat format, gint64 value, ...)
698 GstIndexEntry *entry;
699 GstIndexAssociation *list;
701 GstFormat cur_format;
704 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
705 g_return_val_if_fail (format != 0, NULL);
707 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
710 array = g_array_new (FALSE, FALSE, sizeof (GstIndexAssociation));
712 va_start (args, value);
717 GstIndexAssociation a;
720 cur_format = va_arg (args, GstFormat);
722 a.format = cur_format;
723 a.value = va_arg (args, gint64);
725 g_array_append_val (array, a);
730 list = (GstIndexAssociation *) g_array_free (array, FALSE);
732 entry = gst_index_add_associationv (index, id, flags, n_assocs, list);
739 * gst_index_add_object:
740 * @index: the index to add the object to
741 * @id: the id of the index writer
742 * @key: a key for the object
743 * @type: the GType of the object
744 * @object: a pointer to the object to add
746 * Add the given object to the index with the given key.
748 * Returns: a pointer to the newly added entry in the index.
751 gst_index_add_object (GstIndex * index, gint id, gchar * key,
752 GType type, gpointer object)
754 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
761 gst_index_compare_func (gconstpointer a, gconstpointer b, gpointer user_data)
771 * gst_index_get_assoc_entry:
772 * @index: the index to search
773 * @id: the id of the index writer
774 * @method: The lookup method to use
775 * @flags: Flags for the entry
776 * @format: the format of the value
777 * @value: the value to find
779 * Finds the given format/value in the index
781 * Returns: the entry associated with the value or NULL if the
782 * value was not found.
785 gst_index_get_assoc_entry (GstIndex * index, gint id,
786 GstIndexLookupMethod method, GstAssocFlags flags,
787 GstFormat format, gint64 value)
789 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
794 return gst_index_get_assoc_entry_full (index, id, method, flags, format,
795 value, gst_index_compare_func, NULL);
799 * gst_index_get_assoc_entry_full:
800 * @index: the index to search
801 * @id: the id of the index writer
802 * @method: The lookup method to use
803 * @flags: Flags for the entry
804 * @format: the format of the value
805 * @value: the value to find
806 * @func: the function used to compare entries
807 * @user_data: user data passed to the compare function
809 * Finds the given format/value in the index with the given
810 * compare function and user_data.
812 * Returns: the entry associated with the value or NULL if the
813 * value was not found.
816 gst_index_get_assoc_entry_full (GstIndex * index, gint id,
817 GstIndexLookupMethod method, GstAssocFlags flags,
818 GstFormat format, gint64 value, GCompareDataFunc func, gpointer user_data)
820 GstIndexClass *iclass;
822 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
827 iclass = GST_INDEX_GET_CLASS (index);
829 if (iclass->get_assoc_entry)
830 return iclass->get_assoc_entry (index, id, method, flags, format, value,
837 * gst_index_entry_assoc_map:
838 * @entry: the index to search
839 * @format: the format of the value the find
840 * @value: a pointer to store the value
842 * Gets alternative formats associated with the indexentry.
844 * Returns: TRUE if there was a value associated with the given
848 gst_index_entry_assoc_map (GstIndexEntry * entry,
849 GstFormat format, gint64 * value)
853 g_return_val_if_fail (entry != NULL, FALSE);
854 g_return_val_if_fail (value != NULL, FALSE);
856 for (i = 0; i < GST_INDEX_NASSOCS (entry); i++) {
857 if (GST_INDEX_ASSOC_FORMAT (entry, i) == format) {
858 *value = GST_INDEX_ASSOC_VALUE (entry, i);