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.
23 #include "gst_private.h"
26 #include "gstregistrypool.h"
30 /* Index signals and args */
42 static void gst_index_class_init (GstIndexClass *klass);
43 static void gst_index_init (GstIndex *index);
45 static void gst_index_set_property (GObject *object, guint prop_id,
46 const GValue *value, GParamSpec *pspec);
47 static void gst_index_get_property (GObject *object, guint prop_id,
48 GValue *value, GParamSpec *pspec);
50 static GstIndexGroup* gst_index_group_new (guint groupnum);
52 static gboolean gst_index_path_resolver (GstIndex *index, GstObject *writer,
53 gchar **writer_string, gpointer data);
54 static gboolean gst_index_gtype_resolver (GstIndex *index, GstObject *writer,
55 gchar **writer_string, gpointer data);
57 static GstObject *parent_class = NULL;
58 static guint gst_index_signals[LAST_SIGNAL] = { 0 };
62 GstIndexResolverMethod method;
63 GstIndexResolver resolver;
67 static const ResolverEntry resolvers[] =
69 { GST_INDEX_RESOLVER_CUSTOM, NULL, NULL },
70 { GST_INDEX_RESOLVER_GTYPE, gst_index_gtype_resolver, NULL },
71 { GST_INDEX_RESOLVER_PATH, gst_index_path_resolver, NULL },
74 #define GST_TYPE_INDEX_RESOLVER (gst_index_resolver_get_type())
76 gst_index_resolver_get_type (void)
78 static GType index_resolver_type = 0;
79 static GEnumValue index_resolver[] = {
80 { GST_INDEX_RESOLVER_CUSTOM, "GST_INDEX_RESOLVER_CUSTOM", "Use a custom resolver"},
81 { GST_INDEX_RESOLVER_GTYPE, "GST_INDEX_RESOLVER_GTYPE", "Resolve an object to its GType[.padname]"},
82 { GST_INDEX_RESOLVER_PATH, "GST_INDEX_RESOLVER_PATH", "Resolve an object to its path in the pipeline"},
85 if (!index_resolver_type) {
86 index_resolver_type = g_enum_register_static ("GstIndexResolver", index_resolver);
88 return index_resolver_type;
92 gst_index_entry_get_type (void)
94 static GType index_entry_type = 0;
96 if (!index_entry_type) {
97 index_entry_type = g_boxed_type_register_static ("GstIndexEntry",
98 (GBoxedCopyFunc) gst_index_entry_copy,
99 (GBoxedFreeFunc) gst_index_entry_free);
101 return index_entry_type;
106 gst_index_get_type (void) {
107 static GType index_type = 0;
110 static const GTypeInfo index_info = {
111 sizeof(GstIndexClass),
114 (GClassInitFunc)gst_index_class_init,
119 (GInstanceInitFunc)gst_index_init,
122 index_type = g_type_register_static(GST_TYPE_OBJECT, "GstIndex", &index_info, 0);
128 gst_index_class_init (GstIndexClass *klass)
130 GObjectClass *gobject_class;
131 GstElementClass *gstelement_class;
133 gobject_class = (GObjectClass*)klass;
134 gstelement_class = (GstElementClass*)klass;
136 parent_class = g_type_class_ref(GST_TYPE_OBJECT);
138 gst_index_signals[ENTRY_ADDED] =
139 g_signal_new ("entry_added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
140 G_STRUCT_OFFSET (GstIndexClass, entry_added), NULL, NULL,
141 gst_marshal_VOID__POINTER, G_TYPE_NONE, 1,
144 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_index_set_property);
145 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_index_get_property);
147 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_RESOLVER,
148 g_param_spec_enum ("resolver", "Resolver", "Select a predefined object to string mapper",
149 GST_TYPE_INDEX_RESOLVER, GST_INDEX_RESOLVER_PATH, G_PARAM_READWRITE));
153 gst_index_init (GstIndex *index)
155 index->curgroup = gst_index_group_new(0);
157 index->groups = g_list_prepend(NULL, index->curgroup);
159 index->writers = g_hash_table_new (NULL, NULL);
162 index->method = GST_INDEX_RESOLVER_PATH;
163 index->resolver = resolvers[index->method].resolver;
164 index->resolver_user_data = resolvers[index->method].user_data;
166 GST_FLAG_SET (index, GST_INDEX_WRITABLE);
167 GST_FLAG_SET (index, GST_INDEX_READABLE);
169 GST_DEBUG ( "created new index");
173 gst_index_set_property (GObject *object, guint prop_id,
174 const GValue *value, GParamSpec *pspec)
178 index = GST_INDEX (object);
182 index->method = g_value_get_enum (value);
183 index->resolver = resolvers[index->method].resolver;
184 index->resolver_user_data = resolvers[index->method].user_data;
187 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
193 gst_index_get_property (GObject *object, guint prop_id,
194 GValue *value, GParamSpec *pspec)
198 index = GST_INDEX (object);
202 g_value_set_enum (value, index->method);
205 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
210 static GstIndexGroup *
211 gst_index_group_new(guint groupnum)
213 GstIndexGroup *indexgroup = g_new(GstIndexGroup,1);
215 indexgroup->groupnum = groupnum;
216 indexgroup->entries = NULL;
217 indexgroup->certainty = GST_INDEX_UNKNOWN;
218 indexgroup->peergroup = -1;
220 GST_DEBUG ( "created new index group %d",groupnum);
228 * Create a new tileindex object
230 * Returns: a new index object
237 index = g_object_new (gst_index_get_type (), NULL);
244 * @index: the index to commit
245 * @id: the writer that commited the index
247 * Tell the index that the writer with the given id is done
248 * with this index and is not going to write any more entries
252 gst_index_commit (GstIndex *index, gint id)
254 GstIndexClass *iclass;
256 iclass = GST_INDEX_GET_CLASS (index);
259 iclass->commit (index, id);
264 * gst_index_get_group:
265 * @index: the index to get the current group from
267 * Get the id of the current group.
269 * Returns: the id of the current group.
272 gst_index_get_group(GstIndex *index)
274 return index->curgroup->groupnum;
278 * gst_index_new_group:
279 * @index: the index to create the new group in
281 * Create a new group for the given index. It will be
282 * set as the current group.
284 * Returns: the id of the newly created group.
287 gst_index_new_group(GstIndex *index)
289 index->curgroup = gst_index_group_new(++index->maxgroup);
290 index->groups = g_list_append(index->groups,index->curgroup);
291 GST_DEBUG ( "created new group %d in index",index->maxgroup);
292 return index->maxgroup;
296 * gst_index_set_group:
297 * @index: the index to set the new group in
298 * @groupnum: the groupnumber to set
300 * Set the current groupnumber to the given argument.
302 * Returns: TRUE if the operation succeeded, FALSE if the group
306 gst_index_set_group(GstIndex *index, gint groupnum)
309 GstIndexGroup *indexgroup;
311 /* first check for null change */
312 if (groupnum == index->curgroup->groupnum)
315 /* else search for the proper group */
316 list = index->groups;
318 indexgroup = (GstIndexGroup *)(list->data);
319 list = g_list_next(list);
320 if (indexgroup->groupnum == groupnum) {
321 index->curgroup = indexgroup;
322 GST_DEBUG ( "switched to index group %d", indexgroup->groupnum);
327 /* couldn't find the group in question */
328 GST_DEBUG ( "couldn't find index group %d",groupnum);
333 * gst_index_set_certainty:
334 * @index: the index to set the certainty on
335 * @certainty: the certainty to set
337 * Set the certainty of the given index.
340 gst_index_set_certainty(GstIndex *index, GstIndexCertainty certainty)
342 index->curgroup->certainty = certainty;
346 * gst_index_get_certainty:
347 * @index: the index to get the certainty of
349 * Get the certainty of the given index.
351 * Returns: the certainty of the index.
354 gst_index_get_certainty(GstIndex *index)
356 return index->curgroup->certainty;
360 * gst_index_set_filter:
361 * @index: the index to register the filter on
362 * @filter: the filter to register
363 * @user_data: data passed to the filter function
365 * Lets the app register a custom filter function so that
366 * it can select what entries should be stored in the index.
369 gst_index_set_filter (GstIndex *index,
370 GstIndexFilter filter, gpointer user_data)
372 g_return_if_fail (GST_IS_INDEX (index));
374 index->filter = filter;
375 index->filter_user_data = user_data;
379 * gst_index_set_resolver:
380 * @index: the index to register the resolver on
381 * @resolver: the resolver to register
382 * @user_data: data passed to the resolver function
384 * Lets the app register a custom function to map index
385 * ids to writer descriptions.
388 gst_index_set_resolver (GstIndex *index,
389 GstIndexResolver resolver, gpointer user_data)
391 g_return_if_fail (GST_IS_INDEX (index));
393 index->resolver = resolver;
394 index->resolver_user_data = user_data;
395 index->method = GST_INDEX_RESOLVER_CUSTOM;
399 * gst_index_entry_copy:
400 * @entry: the entry to copy
402 * Copies an entry and returns the result.
405 gst_index_entry_copy (GstIndexEntry *entry)
407 return g_memdup(entry, sizeof(*entry));
411 * gst_index_entry_free:
412 * @entry: the entry to free
414 * Free the memory used by the given entry.
417 gst_index_entry_free (GstIndexEntry *entry)
423 * gst_index_add_format:
424 * @index: the index to add the entry to
425 * @id: the id of the index writer
426 * @format: the format to add to the index
428 * Adds a format entry into the index. This function is
429 * used to map dynamic GstFormat ids to their original
432 * Returns: a pointer to the newly added entry in the index.
435 gst_index_add_format (GstIndex *index, gint id, GstFormat format)
437 GstIndexEntry *entry;
438 const GstFormatDefinition* def;
439 GstIndexClass *iclass;
441 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
442 g_return_val_if_fail (format != 0, NULL);
444 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
447 entry = g_new0 (GstIndexEntry, 1);
448 entry->type = GST_INDEX_ENTRY_FORMAT;
450 entry->data.format.format = format;
452 def = gst_format_get_details (format);
453 entry->data.format.key = def->nick;
455 iclass = GST_INDEX_GET_CLASS (index);
457 if (iclass->add_entry)
458 iclass->add_entry (index, entry);
460 g_signal_emit (G_OBJECT (index), gst_index_signals[ENTRY_ADDED], 0, entry);
467 * @index: the index to add the entry to
468 * @id: the id of the index writer
469 * @description: the description of the index writer
471 * Add an id entry into the index.
473 * Returns: a pointer to the newly added entry in the index.
476 gst_index_add_id (GstIndex *index, gint id, gchar *description)
478 GstIndexEntry *entry;
479 GstIndexClass *iclass;
481 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
482 g_return_val_if_fail (description != NULL, NULL);
484 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
487 entry = g_new0 (GstIndexEntry, 1);
488 entry->type = GST_INDEX_ENTRY_ID;
490 entry->data.id.description = description;
492 iclass = GST_INDEX_GET_CLASS (index);
494 if (iclass->add_entry)
495 iclass->add_entry (index, entry);
497 g_signal_emit (G_OBJECT (index), gst_index_signals[ENTRY_ADDED], 0, entry);
503 gst_index_path_resolver (GstIndex *index, GstObject *writer,
504 gchar **writer_string, gpointer data)
506 *writer_string = gst_object_get_path_string (writer);
512 gst_index_gtype_resolver (GstIndex *index, GstObject *writer,
513 gchar **writer_string, gpointer data)
515 if (GST_IS_PAD (writer)) {
516 GstElement *element = gst_pad_get_parent (GST_PAD (writer));
518 *writer_string = g_strdup_printf ("%s.%s",
519 g_type_name (G_OBJECT_TYPE (element)),
520 gst_object_get_name (writer));
523 *writer_string = g_strdup_printf ("%s", g_type_name (G_OBJECT_TYPE (writer)));
530 * gst_index_get_writer_id:
531 * @index: the index to get a unique write id for
532 * @writer: the GstObject to allocate an id for
533 * @id: a pointer to a gint to hold the id
535 * Before entries can be added to the index, a writer
536 * should obtain a unique id. The methods to add new entries
537 * to the index require this id as an argument.
539 * The application can implement a custom function to map the writer object
540 * to a string. That string will be used to register or look up an id
543 * Returns: TRUE if the writer would be mapped to an id.
546 gst_index_get_writer_id (GstIndex *index, GstObject *writer, gint *id)
548 gchar *writer_string = NULL;
549 GstIndexEntry *entry;
550 GstIndexClass *iclass;
551 gboolean success = FALSE;
553 g_return_val_if_fail (GST_IS_INDEX (index), FALSE);
554 g_return_val_if_fail (GST_IS_OBJECT (writer), FALSE);
555 g_return_val_if_fail (id, FALSE);
559 /* first try to get a previously cached id */
560 entry = g_hash_table_lookup (index->writers, writer);
563 iclass = GST_INDEX_GET_CLASS (index);
565 /* let the app make a string */
566 if (index->resolver) {
569 res = index->resolver (index, writer, &writer_string, index->resolver_user_data);
574 g_warning ("no resolver found");
578 /* if the index has a resolver, make it map this string to an id */
579 if (iclass->get_writer_id) {
580 success = iclass->get_writer_id (index, id, writer_string);
582 /* if the index could not resolve, we allocate one ourselves */
584 *id = ++index->last_id;
587 entry = gst_index_add_id (index, *id, writer_string);
589 /* index is probably not writable, make an entry anyway
590 * to keep it in our cache */
591 entry = g_new0 (GstIndexEntry, 1);
592 entry->type = GST_INDEX_ENTRY_ID;
594 entry->data.id.description = writer_string;
596 g_hash_table_insert (index->writers, writer, entry);
606 * gst_index_add_association:
607 * @index: the index to add the entry to
608 * @id: the id of the index writer
609 * @flags: optinal flags for this entry
610 * @format: the format of the value
612 * @...: other format/value pairs or 0 to end the list
614 * Associate given format/value pairs with eachother.
615 * Be sure to pass gint64 values to this functions varargs,
616 * you might want to use a gint64 cast to be sure.
618 * Returns: a pointer to the newly added entry in the index.
621 gst_index_add_association (GstIndex *index, gint id, GstAssocFlags flags,
622 GstFormat format, gint64 value, ...)
625 GstIndexAssociation *assoc;
626 GstIndexEntry *entry;
629 GstFormat cur_format;
630 volatile gint64 dummy;
631 GstIndexClass *iclass;
633 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
634 g_return_val_if_fail (format != 0, NULL);
636 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
639 va_start (args, value);
645 cur_format = va_arg (args, GstFormat);
647 dummy = va_arg (args, gint64);
651 /* make room for two assoc */
652 size = sizeof (GstIndexEntry) + (sizeof (GstIndexAssociation) * nassocs);
654 entry = g_malloc (size);
656 entry->type = GST_INDEX_ENTRY_ASSOCIATION;
658 entry->data.assoc.flags = flags;
659 assoc = (GstIndexAssociation *) (((guint8 *) entry) + sizeof (GstIndexEntry));
660 entry->data.assoc.assocs = assoc;
661 entry->data.assoc.nassocs = nassocs;
663 va_start (args, value);
665 assoc->format = format;
666 assoc->value = value;
670 format = va_arg (args, GstFormat);
672 value = va_arg (args, gint64);
676 iclass = GST_INDEX_GET_CLASS (index);
678 if (iclass->add_entry)
679 iclass->add_entry (index, entry);
681 g_signal_emit (G_OBJECT (index), gst_index_signals[ENTRY_ADDED], 0, entry);
687 * gst_index_add_object:
688 * @index: the index to add the object to
689 * @id: the id of the index writer
690 * @key: a key for the object
691 * @type: the GType of the object
692 * @object: a pointer to the object to add
694 * Add the given object to the index with the given key.
696 * Returns: a pointer to the newly added entry in the index.
699 gst_index_add_object (GstIndex *index, gint id, gchar *key,
700 GType type, gpointer object)
702 if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
709 gst_index_compare_func (gconstpointer a,
713 return (gint)a - (gint)b;
717 * gst_index_get_assoc_entry:
718 * @index: the index to search
719 * @id: the id of the index writer
720 * @method: The lookup method to use
721 * @flags: Flags for the entry
722 * @format: the format of the value
723 * @value: the value to find
725 * Finds the given format/value in the index
727 * Returns: the entry associated with the value or NULL if the
728 * value was not found.
731 gst_index_get_assoc_entry (GstIndex *index, gint id,
732 GstIndexLookupMethod method, GstAssocFlags flags,
733 GstFormat format, gint64 value)
735 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
740 return gst_index_get_assoc_entry_full (index, id, method, flags, format, value,
741 gst_index_compare_func, NULL);
745 * gst_index_get_assoc_entry_full:
746 * @index: the index to search
747 * @id: the id of the index writer
748 * @method: The lookup method to use
749 * @flags: Flags for the entry
750 * @format: the format of the value
751 * @value: the value to find
752 * @func: the function used to compare entries
753 * @user_data: user data passed to the compare function
755 * Finds the given format/value in the index with the given
756 * compare function and user_data.
758 * Returns: the entry associated with the value or NULL if the
759 * value was not found.
762 gst_index_get_assoc_entry_full (GstIndex *index, gint id,
763 GstIndexLookupMethod method, GstAssocFlags flags,
764 GstFormat format, gint64 value,
765 GCompareDataFunc func,
768 GstIndexClass *iclass;
770 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
775 iclass = GST_INDEX_GET_CLASS (index);
777 if (iclass->get_assoc_entry)
778 return iclass->get_assoc_entry (index, id, method, flags, format, value, func, user_data);
784 * gst_index_entry_assoc_map:
785 * @entry: the index to search
786 * @format: the format of the value the find
787 * @value: a pointer to store the value
789 * Gets alternative formats associated with the indexentry.
791 * Returns: TRUE if there was a value associated with the given
795 gst_index_entry_assoc_map (GstIndexEntry *entry,
796 GstFormat format, gint64 *value)
800 g_return_val_if_fail (entry != NULL, FALSE);
801 g_return_val_if_fail (value != NULL, FALSE);
803 for (i = 0; i < GST_INDEX_NASSOCS (entry); i++) {
804 if (GST_INDEX_ASSOC_FORMAT (entry, i) == format) {
805 *value = GST_INDEX_ASSOC_VALUE (entry, i);
813 static void gst_index_factory_class_init (GstIndexFactoryClass *klass);
814 static void gst_index_factory_init (GstIndexFactory *factory);
816 static GstPluginFeatureClass *factory_parent_class = NULL;
817 /* static guint gst_index_factory_signals[LAST_SIGNAL] = { 0 }; */
820 gst_index_factory_get_type (void)
822 static GType indexfactory_type = 0;
824 if (!indexfactory_type) {
825 static const GTypeInfo indexfactory_info = {
826 sizeof (GstIndexFactoryClass),
829 (GClassInitFunc) gst_index_factory_class_init,
832 sizeof(GstIndexFactory),
834 (GInstanceInitFunc) gst_index_factory_init,
837 indexfactory_type = g_type_register_static (GST_TYPE_PLUGIN_FEATURE,
838 "GstIndexFactory", &indexfactory_info, 0);
840 return indexfactory_type;
844 gst_index_factory_class_init (GstIndexFactoryClass *klass)
846 GObjectClass *gobject_class;
847 GstObjectClass *gstobject_class;
848 GstPluginFeatureClass *gstpluginfeature_class;
850 gobject_class = (GObjectClass*)klass;
851 gstobject_class = (GstObjectClass*)klass;
852 gstpluginfeature_class = (GstPluginFeatureClass*) klass;
854 factory_parent_class = g_type_class_ref (GST_TYPE_PLUGIN_FEATURE);
858 gst_index_factory_init (GstIndexFactory *factory)
863 * gst_index_factory_new:
864 * @name: name of indexfactory to create
865 * @longdesc: long description of indexfactory to create
866 * @type: the GType of the GstIndex element of this factory
868 * Create a new indexfactory with the given parameters
870 * Returns: a new #GstIndexFactory.
873 gst_index_factory_new (const gchar *name, const gchar *longdesc, GType type)
875 GstIndexFactory *factory;
877 g_return_val_if_fail(name != NULL, NULL);
878 factory = gst_index_factory_find (name);
880 factory = GST_INDEX_FACTORY (g_object_new (GST_TYPE_INDEX_FACTORY, NULL));
883 GST_PLUGIN_FEATURE_NAME (factory) = g_strdup (name);
884 if (factory->longdesc)
885 g_free (factory->longdesc);
886 factory->longdesc = g_strdup (longdesc);
887 factory->type = type;
893 * gst_index_factory_destroy:
894 * @factory: factory to destroy
896 * Removes the index from the global list.
899 gst_index_factory_destroy (GstIndexFactory *factory)
901 g_return_if_fail (factory != NULL);
903 /* we don't free the struct bacause someone might have a handle to it.. */
907 * gst_index_factory_find:
908 * @name: name of indexfactory to find
910 * Search for an indexfactory of the given name.
912 * Returns: #GstIndexFactory if found, NULL otherwise
915 gst_index_factory_find (const gchar *name)
917 GstPluginFeature *feature;
919 g_return_val_if_fail (name != NULL, NULL);
921 GST_DEBUG ("gstindex: find \"%s\"", name);
923 feature = gst_registry_pool_find_feature (name, GST_TYPE_INDEX_FACTORY);
925 return GST_INDEX_FACTORY (feature);
931 * gst_index_factory_create:
932 * @factory: the factory used to create the instance
934 * Create a new #GstIndex instance from the
935 * given indexfactory.
937 * Returns: A new #GstIndex instance.
940 gst_index_factory_create (GstIndexFactory *factory)
942 GstIndex *new = NULL;
944 g_return_val_if_fail (factory != NULL, NULL);
946 if (gst_plugin_feature_ensure_loaded (GST_PLUGIN_FEATURE (factory))) {
947 g_return_val_if_fail (factory->type != 0, NULL);
949 new = GST_INDEX (g_object_new(factory->type,NULL));
956 * gst_index_factory_make:
957 * @name: the name of the factory used to create the instance
959 * Create a new #GstIndex instance from the
960 * indexfactory with the given name.
962 * Returns: A new #GstIndex instance.
965 gst_index_factory_make (const gchar *name)
967 GstIndexFactory *factory;
969 g_return_val_if_fail (name != NULL, NULL);
971 factory = gst_index_factory_find (name);
976 return gst_index_factory_create (factory);