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"
43 /* Index signals and args */
57 static void gst_index_class_init (GstIndexClass * klass);
58 static void gst_index_init (GstIndex * index);
60 static void gst_index_set_property (GObject * object, guint prop_id,
61 const GValue * value, GParamSpec * pspec);
62 static void gst_index_get_property (GObject * object, guint prop_id,
63 GValue * value, GParamSpec * pspec);
65 static GstIndexGroup *gst_index_group_new (guint groupnum);
67 static gboolean gst_index_path_resolver (GstIndex * index, GstObject * writer,
68 gchar ** writer_string, gpointer data);
69 static gboolean gst_index_gtype_resolver (GstIndex * index, GstObject * writer,
70 gchar ** writer_string, gpointer data);
71 static void gst_index_add_entry (GstIndex * index, GstIndexEntry * entry);
73 static GstObject *parent_class = NULL;
74 static guint gst_index_signals[LAST_SIGNAL] = { 0 };
78 GstIndexResolverMethod method;
79 GstIndexResolver resolver;
84 static const ResolverEntry resolvers[] = {
85 {GST_INDEX_RESOLVER_CUSTOM, NULL, NULL},
86 {GST_INDEX_RESOLVER_GTYPE, gst_index_gtype_resolver, NULL},
87 {GST_INDEX_RESOLVER_PATH, gst_index_path_resolver, NULL},
90 #define GST_TYPE_INDEX_RESOLVER (gst_index_resolver_get_type())
92 gst_index_resolver_get_type (void)
94 static GType index_resolver_type = 0;
95 static const GEnumValue index_resolver[] = {
96 {GST_INDEX_RESOLVER_CUSTOM, "GST_INDEX_RESOLVER_CUSTOM",
97 "Use a custom resolver"},
98 {GST_INDEX_RESOLVER_GTYPE, "GST_INDEX_RESOLVER_GTYPE",
99 "Resolve an object to its GType[.padname]"},
100 {GST_INDEX_RESOLVER_PATH, "GST_INDEX_RESOLVER_PATH",
101 "Resolve an object to its path in the pipeline"},
105 if (!index_resolver_type) {
106 index_resolver_type =
107 g_enum_register_static ("GstIndexResolver", index_resolver);
109 return index_resolver_type;
113 gst_index_entry_get_type (void)
115 static GType index_entry_type = 0;
117 if (!index_entry_type) {
118 index_entry_type = g_boxed_type_register_static ("GstIndexEntry",
119 (GBoxedCopyFunc) gst_index_entry_copy,
120 (GBoxedFreeFunc) gst_index_entry_free);
122 return index_entry_type;
127 gst_index_get_type (void)
129 static GType index_type = 0;
132 static const GTypeInfo index_info = {
133 sizeof (GstIndexClass),
136 (GClassInitFunc) gst_index_class_init,
141 (GInstanceInitFunc) gst_index_init,
146 g_type_register_static (GST_TYPE_OBJECT, "GstIndex", &index_info, 0);
152 gst_index_class_init (GstIndexClass * klass)
154 GObjectClass *gobject_class;
156 gobject_class = G_OBJECT_CLASS (klass);
158 parent_class = g_type_class_peek_parent (klass);
161 * GstIndex::entry-added
162 * @gstindex: the object which received the signal.
163 * @arg1: The entry added to the index.
165 * Is emitted when a new entry is added to the index.
167 gst_index_signals[ENTRY_ADDED] =
168 g_signal_new ("entry-added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
169 G_STRUCT_OFFSET (GstIndexClass, entry_added), NULL, NULL,
170 gst_marshal_VOID__BOXED, G_TYPE_NONE, 1, GST_TYPE_INDEX_ENTRY);
172 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_index_set_property);
173 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_index_get_property);
175 g_object_class_install_property (gobject_class, ARG_RESOLVER,
176 g_param_spec_enum ("resolver", "Resolver",
177 "Select a predefined object to string mapper",
178 GST_TYPE_INDEX_RESOLVER, GST_INDEX_RESOLVER_PATH, G_PARAM_READWRITE));
182 gst_index_init (GstIndex * index)
184 index->curgroup = gst_index_group_new (0);
186 index->groups = g_list_prepend (NULL, index->curgroup);
188 index->writers = g_hash_table_new (NULL, NULL);
191 index->method = GST_INDEX_RESOLVER_PATH;
192 index->resolver = resolvers[index->method].resolver;
193 index->resolver_user_data = resolvers[index->method].user_data;
195 GST_OBJECT_FLAG_SET (index, GST_INDEX_WRITABLE);
196 GST_OBJECT_FLAG_SET (index, GST_INDEX_READABLE);
198 GST_DEBUG ("created new index");
202 gst_index_set_property (GObject * object, guint prop_id,
203 const GValue * value, GParamSpec * pspec)
207 index = GST_INDEX (object);
211 index->method = g_value_get_enum (value);
212 index->resolver = resolvers[index->method].resolver;
213 index->resolver_user_data = resolvers[index->method].user_data;
216 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
222 gst_index_get_property (GObject * object, guint prop_id,
223 GValue * value, GParamSpec * pspec)
227 index = GST_INDEX (object);
231 g_value_set_enum (value, index->method);
234 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
239 static GstIndexGroup *
240 gst_index_group_new (guint groupnum)
242 GstIndexGroup *indexgroup = g_new (GstIndexGroup, 1);
244 indexgroup->groupnum = groupnum;
245 indexgroup->entries = NULL;
246 indexgroup->certainty = GST_INDEX_UNKNOWN;
247 indexgroup->peergroup = -1;
249 GST_DEBUG ("created new index group %d", groupnum);
257 * Create a new tileindex object
259 * Returns: a new index object
266 index = g_object_new (gst_index_get_type (), NULL);
273 * @index: the index to commit
274 * @id: the writer that commited the index
276 * Tell the index that the writer with the given id is done
277 * with this index and is not going to write any more entries
281 gst_index_commit (GstIndex * index, gint id)
283 GstIndexClass *iclass;
285 iclass = GST_INDEX_GET_CLASS (index);
288 iclass->commit (index, id);
293 * gst_index_get_group:
294 * @index: the index to get the current group from
296 * Get the id of the current group.
298 * Returns: the id of the current group.
301 gst_index_get_group (GstIndex * index)
303 return index->curgroup->groupnum;
307 * gst_index_new_group:
308 * @index: the index to create the new group in
310 * Create a new group for the given index. It will be
311 * set as the current group.
313 * Returns: the id of the newly created group.
316 gst_index_new_group (GstIndex * index)
318 index->curgroup = gst_index_group_new (++index->maxgroup);
319 index->groups = g_list_append (index->groups, index->curgroup);
320 GST_DEBUG ("created new group %d in index", index->maxgroup);
321 return index->maxgroup;
325 * gst_index_set_group:
326 * @index: the index to set the new group in
327 * @groupnum: the groupnumber to set
329 * Set the current groupnumber to the given argument.
331 * Returns: TRUE if the operation succeeded, FALSE if the group
335 gst_index_set_group (GstIndex * index, gint groupnum)
338 GstIndexGroup *indexgroup;
340 /* first check for null change */
341 if (groupnum == index->curgroup->groupnum)
344 /* else search for the proper group */
345 list = index->groups;
347 indexgroup = (GstIndexGroup *) (list->data);
348 list = g_list_next (list);
349 if (indexgroup->groupnum == groupnum) {
350 index->curgroup = indexgroup;
351 GST_DEBUG ("switched to index group %d", indexgroup->groupnum);
356 /* couldn't find the group in question */
357 GST_DEBUG ("couldn't find index group %d", groupnum);
362 * gst_index_set_certainty:
363 * @index: the index to set the certainty on
364 * @certainty: the certainty to set
366 * Set the certainty of the given index.
369 gst_index_set_certainty (GstIndex * index, GstIndexCertainty certainty)
371 index->curgroup->certainty = certainty;
375 * gst_index_get_certainty:
376 * @index: the index to get the certainty of
378 * Get the certainty of the given index.
380 * Returns: the certainty of the index.
383 gst_index_get_certainty (GstIndex * index)
385 return index->curgroup->certainty;
389 * gst_index_set_filter:
390 * @index: the index to register the filter on
391 * @filter: the filter to register
392 * @user_data: data passed to the filter function
394 * Lets the app register a custom filter function so that
395 * it can select what entries should be stored in the index.
398 gst_index_set_filter (GstIndex * index,
399 GstIndexFilter filter, gpointer user_data)
401 g_return_if_fail (GST_IS_INDEX (index));
403 gst_index_set_filter_full (index, filter, user_data, NULL);
407 * gst_index_set_filter_full:
408 * @index: the index to register the filter on
409 * @filter: the filter to register
410 * @user_data: data passed to the filter function
411 * @user_data_destroy: function to call when @user_data is unset
413 * Lets the app register a custom filter function so that
414 * it can select what entries should be stored in the index.
417 gst_index_set_filter_full (GstIndex * index,
418 GstIndexFilter filter, gpointer user_data, GDestroyNotify user_data_destroy)
420 g_return_if_fail (GST_IS_INDEX (index));
422 if (index->filter_user_data && index->filter_user_data_destroy)
423 index->filter_user_data_destroy (index->filter_user_data);
425 index->filter = filter;
426 index->filter_user_data = user_data;
427 index->filter_user_data_destroy = user_data_destroy;
431 * gst_index_set_resolver:
432 * @index: the index to register the resolver on
433 * @resolver: the resolver to register
434 * @user_data: data passed to the resolver function
436 * Lets the app register a custom function to map index
437 * ids to writer descriptions.
440 gst_index_set_resolver (GstIndex * index,
441 GstIndexResolver resolver, gpointer user_data)
443 g_return_if_fail (GST_IS_INDEX (index));
445 index->resolver = resolver;
446 index->resolver_user_data = user_data;
447 index->method = GST_INDEX_RESOLVER_CUSTOM;
451 * gst_index_entry_copy:
452 * @entry: the entry to copy
454 * Copies an entry and returns the result.
456 * Returns: a newly allocated #GstIndexEntry.
459 gst_index_entry_copy (GstIndexEntry * entry)
461 return g_memdup (entry, sizeof (*entry));
465 * gst_index_entry_free:
466 * @entry: the entry to free
468 * Free the memory used by the given entry.
471 gst_index_entry_free (GstIndexEntry * entry)
477 * gst_index_add_format:
478 * @index: the index to add the entry to
479 * @id: the id of the index writer
480 * @format: the format to add to the index
482 * Adds a format entry into the index. This function is
483 * used to map dynamic GstFormat ids to their original
486 * Returns: a pointer to the newly added entry in the index.
489 gst_index_add_format (GstIndex * index, gint id, GstFormat format)
491 GstIndexEntry *entry;
492 const GstFormatDefinition *def;
494 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
495 g_return_val_if_fail (format != 0, NULL);
497 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
500 entry = g_new0 (GstIndexEntry, 1);
501 entry->type = GST_INDEX_ENTRY_FORMAT;
503 entry->data.format.format = format;
505 def = gst_format_get_details (format);
506 entry->data.format.key = def->nick;
508 gst_index_add_entry (index, entry);
515 * @index: the index to add the entry to
516 * @id: the id of the index writer
517 * @description: the description of the index writer
519 * Add an id entry into the index.
521 * Returns: a pointer to the newly added entry in the index.
524 gst_index_add_id (GstIndex * index, gint id, gchar * description)
526 GstIndexEntry *entry;
528 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
529 g_return_val_if_fail (description != NULL, NULL);
531 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
534 entry = g_new0 (GstIndexEntry, 1);
535 entry->type = GST_INDEX_ENTRY_ID;
537 entry->data.id.description = description;
539 gst_index_add_entry (index, entry);
545 gst_index_path_resolver (GstIndex * index, GstObject * writer,
546 gchar ** writer_string, gpointer data)
548 *writer_string = gst_object_get_path_string (writer);
554 gst_index_gtype_resolver (GstIndex * index, GstObject * writer,
555 gchar ** writer_string, gpointer data)
557 if (GST_IS_PAD (writer)) {
558 GstElement *element =
559 (GstElement *) gst_object_get_parent (GST_OBJECT (writer));
562 name = gst_object_get_name (writer);
563 *writer_string = g_strdup_printf ("%s.%s",
564 g_type_name (G_OBJECT_TYPE (element)), name);
566 gst_object_unref (element);
571 g_strdup_printf ("%s", g_type_name (G_OBJECT_TYPE (writer)));
578 * gst_index_get_writer_id:
579 * @index: the index to get a unique write id for
580 * @writer: the GstObject to allocate an id for
581 * @id: a pointer to a gint to hold the id
583 * Before entries can be added to the index, a writer
584 * should obtain a unique id. The methods to add new entries
585 * to the index require this id as an argument.
587 * The application can implement a custom function to map the writer object
588 * to a string. That string will be used to register or look up an id
591 * Returns: TRUE if the writer would be mapped to an id.
594 gst_index_get_writer_id (GstIndex * index, GstObject * writer, gint * id)
596 gchar *writer_string = NULL;
597 GstIndexEntry *entry;
598 GstIndexClass *iclass;
599 gboolean success = FALSE;
601 g_return_val_if_fail (GST_IS_INDEX (index), FALSE);
602 g_return_val_if_fail (GST_IS_OBJECT (writer), FALSE);
603 g_return_val_if_fail (id, FALSE);
607 /* first try to get a previously cached id */
608 entry = g_hash_table_lookup (index->writers, writer);
611 iclass = GST_INDEX_GET_CLASS (index);
613 /* let the app make a string */
614 if (index->resolver) {
618 index->resolver (index, writer, &writer_string,
619 index->resolver_user_data);
623 g_warning ("no resolver found");
627 /* if the index has a resolver, make it map this string to an id */
628 if (iclass->get_writer_id) {
629 success = iclass->get_writer_id (index, id, writer_string);
631 /* if the index could not resolve, we allocate one ourselves */
633 *id = ++index->last_id;
636 entry = gst_index_add_id (index, *id, writer_string);
638 /* index is probably not writable, make an entry anyway
639 * to keep it in our cache */
640 entry = g_new0 (GstIndexEntry, 1);
641 entry->type = GST_INDEX_ENTRY_ID;
643 entry->data.id.description = writer_string;
645 g_hash_table_insert (index->writers, writer, entry);
654 gst_index_add_entry (GstIndex * index, GstIndexEntry * entry)
656 GstIndexClass *iclass;
658 iclass = GST_INDEX_GET_CLASS (index);
660 if (iclass->add_entry) {
661 iclass->add_entry (index, entry);
664 g_signal_emit (G_OBJECT (index), gst_index_signals[ENTRY_ADDED], 0, entry);
668 * gst_index_add_associationv:
669 * @index: the index to add the entry to
670 * @id: the id of the index writer
671 * @flags: optinal flags for this entry
672 * @n: number of associations
673 * @list: list of associations
675 * Associate given format/value pairs with each other.
677 * Returns: a pointer to the newly added entry in the index.
680 gst_index_add_associationv (GstIndex * index, gint id, GstAssocFlags flags,
681 gint n, const GstIndexAssociation * list)
683 GstIndexEntry *entry;
685 g_return_val_if_fail (n > 0, NULL);
686 g_return_val_if_fail (list != NULL, NULL);
687 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
689 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
692 entry = g_malloc (sizeof (GstIndexEntry));
694 entry->type = GST_INDEX_ENTRY_ASSOCIATION;
696 entry->data.assoc.flags = flags;
697 entry->data.assoc.assocs = g_memdup (list, sizeof (GstIndexAssociation) * n);
698 entry->data.assoc.nassocs = n;
700 gst_index_add_entry (index, entry);
706 * gst_index_add_association:
707 * @index: the index to add the entry to
708 * @id: the id of the index writer
709 * @flags: optinal flags for this entry
710 * @format: the format of the value
712 * @...: other format/value pairs or 0 to end the list
714 * Associate given format/value pairs with each other.
715 * Be sure to pass gint64 values to this functions varargs,
716 * you might want to use a gint64 cast to be sure.
718 * Returns: a pointer to the newly added entry in the index.
721 gst_index_add_association (GstIndex * index, gint id, GstAssocFlags flags,
722 GstFormat format, gint64 value, ...)
725 GstIndexEntry *entry;
726 GstIndexAssociation *list;
728 GstFormat cur_format;
731 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
732 g_return_val_if_fail (format != 0, NULL);
734 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
737 array = g_array_new (FALSE, FALSE, sizeof (GstIndexAssociation));
739 va_start (args, value);
744 GstIndexAssociation a;
747 cur_format = va_arg (args, GstFormat);
749 a.format = cur_format;
750 a.value = va_arg (args, gint64);
752 g_array_append_val (array, a);
757 list = (GstIndexAssociation *) g_array_free (array, FALSE);
759 entry = gst_index_add_associationv (index, id, flags, n_assocs, list);
766 * gst_index_add_object:
767 * @index: the index to add the object to
768 * @id: the id of the index writer
769 * @key: a key for the object
770 * @type: the GType of the object
771 * @object: a pointer to the object to add
773 * Add the given object to the index with the given key.
775 * This function is not yet implemented.
777 * Returns: a pointer to the newly added entry in the index.
780 gst_index_add_object (GstIndex * index, gint id, gchar * key,
781 GType type, gpointer object)
783 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
790 gst_index_compare_func (gconstpointer a, gconstpointer b, gpointer user_data)
800 * gst_index_get_assoc_entry:
801 * @index: the index to search
802 * @id: the id of the index writer
803 * @method: The lookup method to use
804 * @flags: Flags for the entry
805 * @format: the format of the value
806 * @value: the value to find
808 * Finds the given format/value in the index
810 * Returns: the entry associated with the value or NULL if the
811 * value was not found.
814 gst_index_get_assoc_entry (GstIndex * index, gint id,
815 GstIndexLookupMethod method, GstAssocFlags flags,
816 GstFormat format, gint64 value)
818 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
823 return gst_index_get_assoc_entry_full (index, id, method, flags, format,
824 value, gst_index_compare_func, NULL);
828 * gst_index_get_assoc_entry_full:
829 * @index: the index to search
830 * @id: the id of the index writer
831 * @method: The lookup method to use
832 * @flags: Flags for the entry
833 * @format: the format of the value
834 * @value: the value to find
835 * @func: the function used to compare entries
836 * @user_data: user data passed to the compare function
838 * Finds the given format/value in the index with the given
839 * compare function and user_data.
841 * Returns: the entry associated with the value or NULL if the
842 * value was not found.
845 gst_index_get_assoc_entry_full (GstIndex * index, gint id,
846 GstIndexLookupMethod method, GstAssocFlags flags,
847 GstFormat format, gint64 value, GCompareDataFunc func, gpointer user_data)
849 GstIndexClass *iclass;
851 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
856 iclass = GST_INDEX_GET_CLASS (index);
858 if (iclass->get_assoc_entry)
859 return iclass->get_assoc_entry (index, id, method, flags, format, value,
866 * gst_index_entry_assoc_map:
867 * @entry: the index to search
868 * @format: the format of the value the find
869 * @value: a pointer to store the value
871 * Gets alternative formats associated with the indexentry.
873 * Returns: TRUE if there was a value associated with the given
877 gst_index_entry_assoc_map (GstIndexEntry * entry,
878 GstFormat format, gint64 * value)
882 g_return_val_if_fail (entry != NULL, FALSE);
883 g_return_val_if_fail (value != NULL, FALSE);
885 for (i = 0; i < GST_INDEX_NASSOCS (entry); i++) {
886 if (GST_INDEX_ASSOC_FORMAT (entry, i) == format) {
887 *value = GST_INDEX_ASSOC_VALUE (entry, i);