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 #include "gst_private.h"
25 #include "gstregistry.h"
29 /* Index signals and args */
40 static void gst_index_class_init (GstIndexClass *klass);
41 static void gst_index_init (GstIndex *index);
43 #define CLASS(index) GST_INDEX_CLASS (G_OBJECT_GET_CLASS (index))
45 static GstObject *parent_class = NULL;
46 static guint gst_index_signals[LAST_SIGNAL] = { 0 };
49 gst_index_get_type(void) {
50 static GType index_type = 0;
53 static const GTypeInfo index_info = {
54 sizeof(GstIndexClass),
57 (GClassInitFunc)gst_index_class_init,
62 (GInstanceInitFunc)gst_index_init,
65 index_type = g_type_register_static(GST_TYPE_OBJECT, "GstIndex", &index_info, 0);
71 gst_index_class_init (GstIndexClass *klass)
73 GObjectClass *gobject_class;
74 GstElementClass *gstelement_class;
76 gobject_class = (GObjectClass*)klass;
77 gstelement_class = (GstElementClass*)klass;
79 parent_class = g_type_class_ref(GST_TYPE_OBJECT);
81 gst_index_signals[ENTRY_ADDED] =
82 g_signal_new ("entry_added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
83 G_STRUCT_OFFSET (GstIndexClass, entry_added), NULL, NULL,
84 gst_marshal_VOID__POINTER, G_TYPE_NONE, 1,
88 static GstIndexGroup *
89 gst_index_group_new(guint groupnum)
91 GstIndexGroup *indexgroup = g_new(GstIndexGroup,1);
93 indexgroup->groupnum = groupnum;
94 indexgroup->entries = NULL;
95 indexgroup->certainty = GST_INDEX_UNKNOWN;
96 indexgroup->peergroup = -1;
98 GST_DEBUG(0, "created new index group %d",groupnum);
104 gst_index_init (GstIndex *index)
106 index->curgroup = gst_index_group_new(0);
108 index->groups = g_list_prepend(NULL, index->curgroup);
110 index->writers = g_hash_table_new (NULL, NULL);
113 GST_DEBUG(0, "created new index");
119 * Create a new tileindex object
121 * Returns: a new index object
128 index = g_object_new (gst_index_get_type (), NULL);
134 * gst_index_get_group:
135 * @index: the index to get the current group from
137 * Get the id of the current group.
139 * Returns: the id of the current group.
142 gst_index_get_group(GstIndex *index)
144 return index->curgroup->groupnum;
148 * gst_index_new_group:
149 * @index: the index to create the new group in
151 * Create a new group for the given index. It will be
152 * set as the current group.
154 * Returns: the id of the newly created group.
157 gst_index_new_group(GstIndex *index)
159 index->curgroup = gst_index_group_new(++index->maxgroup);
160 index->groups = g_list_append(index->groups,index->curgroup);
161 GST_DEBUG(0, "created new group %d in index",index->maxgroup);
162 return index->maxgroup;
166 * gst_index_set_group:
167 * @index: the index to set the new group in
168 * @groupnum: the groupnumber to set
170 * Set the current groupnumber to the given argument.
172 * Returns: TRUE if the operation succeeded, FALSE if the group
176 gst_index_set_group(GstIndex *index, gint groupnum)
179 GstIndexGroup *indexgroup;
181 /* first check for null change */
182 if (groupnum == index->curgroup->groupnum)
185 /* else search for the proper group */
186 list = index->groups;
188 indexgroup = (GstIndexGroup *)(list->data);
189 list = g_list_next(list);
190 if (indexgroup->groupnum == groupnum) {
191 index->curgroup = indexgroup;
192 GST_DEBUG(0, "switched to index group %d", indexgroup->groupnum);
197 /* couldn't find the group in question */
198 GST_DEBUG(0, "couldn't find index group %d",groupnum);
203 * gst_index_set_certainty:
204 * @index: the index to set the certainty on
205 * @certainty: the certainty to set
207 * Set the certainty of the given index.
210 gst_index_set_certainty(GstIndex *index, GstIndexCertainty certainty)
212 index->curgroup->certainty = certainty;
216 * gst_index_get_certainty:
217 * @index: the index to get the certainty of
219 * Get the certainty of the given index.
221 * Returns: the certainty of the index.
224 gst_index_get_certainty(GstIndex *index)
226 return index->curgroup->certainty;
230 * gst_index_set_filter:
231 * @index: the index to register the filter on
232 * @filter: the filter to register
233 * @user_data: data passed to the filter function
235 * Lets the app register a custom filter function so that
236 * it can select what entries should be stored in the index.
239 gst_index_set_filter (GstIndex *index,
240 GstIndexFilter filter, gpointer user_data)
242 g_return_if_fail (GST_IS_INDEX (index));
244 index->filter = filter;
245 index->filter_user_data = user_data;
249 * gst_index_set_resolver:
250 * @index: the index to register the resolver on
251 * @resolver: the resolver to register
252 * @user_data: data passed to the resolver function
254 * Lets the app register a custom function to map index
255 * ids to writer descriptions.
258 gst_index_set_resolver (GstIndex *index,
259 GstIndexResolver resolver, gpointer user_data)
261 g_return_if_fail (GST_IS_INDEX (index));
263 index->resolver = resolver;
264 index->resolver_user_data = user_data;
268 * gst_index_entry_free:
269 * @entry: the entry to free
271 * Free the memory used by the given entry.
274 gst_index_entry_free (GstIndexEntry *entry)
280 * gst_index_add_format:
281 * @index: the index to add the entry to
282 * @id: the id of the index writer
283 * @format: the format to add to the index
285 * Adds a format entry into the index. This function is
286 * used to map dynamic GstFormat ids to their original
289 * Returns: a pointer to the newly added entry in the index.
292 gst_index_add_format (GstIndex *index, gint id, GstFormat format)
294 GstIndexEntry *entry;
295 const GstFormatDefinition* def;
297 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
298 g_return_val_if_fail (format != 0, NULL);
300 entry = g_new0 (GstIndexEntry, 1);
301 entry->type = GST_INDEX_ENTRY_FORMAT;
303 entry->data.format.format = format;
304 def = gst_format_get_details (format);
305 entry->data.format.key = def->nick;
307 if (CLASS (index)->add_entry)
308 CLASS (index)->add_entry (index, entry);
310 g_signal_emit (G_OBJECT (index), gst_index_signals[ENTRY_ADDED], 0, entry);
317 * @index: the index to add the entry to
318 * @id: the id of the index writer
319 * @description: the description of the index writer
321 * Add an id entry into the index.
323 * Returns: a pointer to the newly added entry in the index.
326 gst_index_add_id (GstIndex *index, gint id, gchar *description)
328 GstIndexEntry *entry;
330 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
331 g_return_val_if_fail (description != NULL, NULL);
333 entry = g_new0 (GstIndexEntry, 1);
334 entry->type = GST_INDEX_ENTRY_ID;
336 entry->data.id.description = description;
338 if (CLASS (index)->add_entry)
339 CLASS (index)->add_entry (index, entry);
341 g_signal_emit (G_OBJECT (index), gst_index_signals[ENTRY_ADDED], 0, entry);
347 * gst_index_get_writer_id:
348 * @index: the index to get a unique write id for
349 * @writer: the GstObject to allocate an id for
350 * @id: a pointer to a gint to hold the id
352 * Before entries can be added to the index, a writer
353 * should obtain a unique id. The methods to add new entries
354 * to the index require this id as an argument.
356 * The application or a GstIndex subclass can implement
357 * custom functions to map the writer object to an id.
359 * Returns: TRUE if the writer would be mapped to an id.
362 gst_index_get_writer_id (GstIndex *index, GstObject *writer, gint *id)
364 gchar *writer_string = NULL;
365 gboolean success = FALSE;
366 GstIndexEntry *entry;
368 g_return_val_if_fail (GST_IS_INDEX (index), FALSE);
369 g_return_val_if_fail (GST_IS_OBJECT (writer), FALSE);
370 g_return_val_if_fail (id, FALSE);
374 entry = g_hash_table_lookup (index->writers, writer);
376 *id = index->last_id;
378 writer_string = gst_object_get_path_string (writer);
380 gst_index_add_id (index, *id, writer_string);
382 g_hash_table_insert (index->writers, writer, entry);
385 if (CLASS (index)->resolve_writer) {
386 success = CLASS (index)->resolve_writer (index, writer, id, &writer_string);
389 if (index->resolver) {
390 success = index->resolver (index, writer, id, &writer_string, index->resolver_user_data);
397 * gst_index_add_association:
398 * @index: the index to add the entry to
399 * @id: the id of the index writer
400 * @flags: optinal flags for this entry
401 * @format: the format of the value
403 * @...: other format/value pairs or 0 to end the list
405 * Associate given format/value pairs with eachother.
406 * Be sure to pass gint64 values to this functions varargs,
407 * you might want to use a gint64 cast to be sure.
409 * Returns: a pointer to the newly added entry in the index.
412 gst_index_add_association (GstIndex *index, gint id, GstAssocFlags flags,
413 GstFormat format, gint64 value, ...)
416 GstIndexAssociation *assoc;
417 GstIndexEntry *entry;
420 GstFormat cur_format;
421 volatile gint64 dummy;
423 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
424 g_return_val_if_fail (format != 0, NULL);
426 va_start (args, value);
432 cur_format = va_arg (args, GstFormat);
434 dummy = va_arg (args, gint64);
438 /* make room for two assoc */
439 size = sizeof (GstIndexEntry) + (sizeof (GstIndexAssociation) * nassocs);
441 entry = g_malloc (size);
443 entry->type = GST_INDEX_ENTRY_ASSOCIATION;
445 entry->data.assoc.flags = flags;
446 assoc = (GstIndexAssociation *) (((guint8 *) entry) + sizeof (GstIndexEntry));
447 entry->data.assoc.assocs = assoc;
448 entry->data.assoc.nassocs = nassocs;
450 va_start (args, value);
452 assoc->format = format;
453 assoc->value = value;
457 format = va_arg (args, GstFormat);
459 value = va_arg (args, gint64);
463 if (CLASS (index)->add_entry)
464 CLASS (index)->add_entry (index, entry);
466 g_signal_emit (G_OBJECT (index), gst_index_signals[ENTRY_ADDED], 0, entry);
472 * gst_index_add_object:
473 * @index: the index to add the object to
474 * @id: the id of the index writer
475 * @key: a key for the object
476 * @type: the GType of the object
477 * @object: a pointer to the object to add
479 * Add the given object to the index with the given key.
481 * Returns: a pointer to the newly added entry in the index.
484 gst_index_add_object (GstIndex *index, gint id, gchar *key,
485 GType type, gpointer object)
491 gst_index_compare_func (gconstpointer a,
499 * gst_index_get_assoc_entry:
500 * @index: the index to search
501 * @id: the id of the index writer
502 * @method: The lookup method to use
503 * @format: the format of the value
504 * @value: the value to find
506 * Finds the given format/value in the index
508 * Returns: the entry associated with the value or NULL if the
509 * value was not found.
512 gst_index_get_assoc_entry (GstIndex *index, gint id,
513 GstIndexLookupMethod method,
514 GstFormat format, gint64 value)
516 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
518 return gst_index_get_assoc_entry_full (index, id, method, format, value,
519 gst_index_compare_func, NULL);
523 * gst_index_get_assoc_entry_full:
524 * @index: the index to search
525 * @id: the id of the index writer
526 * @method: The lookup method to use
527 * @format: the format of the value
528 * @value: the value to find
529 * @func: the function used to compare entries
530 * @user_data: user data passed to the compare function
532 * Finds the given format/value in the index with the given
533 * compare function and user_data.
535 * Returns: the entry associated with the value or NULL if the
536 * value was not found.
539 gst_index_get_assoc_entry_full (GstIndex *index, gint id,
540 GstIndexLookupMethod method,
541 GstFormat format, gint64 value,
542 GCompareDataFunc func,
545 g_return_val_if_fail (GST_IS_INDEX (index), NULL);
547 if (CLASS(index)->get_assoc_entry)
548 return CLASS (index)->get_assoc_entry (index, id, method, format, value, func, user_data);
554 * gst_index_entry_assoc_map:
555 * @entry: the index to search
556 * @format: the format of the value the find
557 * @value: a pointer to store the value
559 * Gets alternative formats associated with the indexentry.
561 * Returns: TRUE if there was a value associated with the given
565 gst_index_entry_assoc_map (GstIndexEntry *entry,
566 GstFormat format, gint64 *value)
570 g_return_val_if_fail (entry != NULL, FALSE);
571 g_return_val_if_fail (value != NULL, FALSE);
573 for (i = 0; i < GST_INDEX_NASSOCS (entry); i++) {
574 if (GST_INDEX_ASSOC_FORMAT (entry, i) == format) {
575 *value = GST_INDEX_ASSOC_VALUE (entry, i);
583 static void gst_index_factory_class_init (GstIndexFactoryClass *klass);
584 static void gst_index_factory_init (GstIndexFactory *factory);
586 static GstPluginFeatureClass *factory_parent_class = NULL;
587 /* static guint gst_index_factory_signals[LAST_SIGNAL] = { 0 }; */
590 gst_index_factory_get_type (void)
592 static GType indexfactory_type = 0;
594 if (!indexfactory_type) {
595 static const GTypeInfo indexfactory_info = {
596 sizeof (GstIndexFactoryClass),
599 (GClassInitFunc) gst_index_factory_class_init,
602 sizeof(GstIndexFactory),
604 (GInstanceInitFunc) gst_index_factory_init,
607 indexfactory_type = g_type_register_static (GST_TYPE_PLUGIN_FEATURE,
608 "GstIndexFactory", &indexfactory_info, 0);
610 return indexfactory_type;
614 gst_index_factory_class_init (GstIndexFactoryClass *klass)
616 GObjectClass *gobject_class;
617 GstObjectClass *gstobject_class;
618 GstPluginFeatureClass *gstpluginfeature_class;
620 gobject_class = (GObjectClass*)klass;
621 gstobject_class = (GstObjectClass*)klass;
622 gstpluginfeature_class = (GstPluginFeatureClass*) klass;
624 factory_parent_class = g_type_class_ref (GST_TYPE_PLUGIN_FEATURE);
628 gst_index_factory_init (GstIndexFactory *factory)
633 * gst_index_factory_new:
634 * @name: name of indexfactory to create
635 * @longdesc: long description of indexfactory to create
636 * @type: the GType of the GstIndex element of this factory
638 * Create a new indexfactory with the given parameters
640 * Returns: a new #GstIndexFactory.
643 gst_index_factory_new (const gchar *name, const gchar *longdesc, GType type)
645 GstIndexFactory *factory;
647 g_return_val_if_fail(name != NULL, NULL);
648 factory = gst_index_factory_find (name);
650 factory = GST_INDEX_FACTORY (g_object_new (GST_TYPE_INDEX_FACTORY, NULL));
653 GST_PLUGIN_FEATURE_NAME (factory) = g_strdup (name);
654 if (factory->longdesc)
655 g_free (factory->longdesc);
656 factory->longdesc = g_strdup (longdesc);
657 factory->type = type;
663 * gst_index_factory_destroy:
664 * @factory: factory to destroy
666 * Removes the index from the global list.
669 gst_index_factory_destroy (GstIndexFactory *factory)
671 g_return_if_fail (factory != NULL);
673 /* we don't free the struct bacause someone might have a handle to it.. */
677 * gst_index_factory_find:
678 * @name: name of indexfactory to find
680 * Search for an indexfactory of the given name.
682 * Returns: #GstIndexFactory if found, NULL otherwise
685 gst_index_factory_find (const gchar *name)
687 GstPluginFeature *feature;
689 g_return_val_if_fail (name != NULL, NULL);
691 GST_DEBUG (0,"gstindex: find \"%s\"", name);
693 feature = gst_registry_pool_find_feature (name, GST_TYPE_INDEX_FACTORY);
695 return GST_INDEX_FACTORY (feature);
701 * gst_index_factory_create:
702 * @factory: the factory used to create the instance
704 * Create a new #GstIndex instance from the
705 * given indexfactory.
707 * Returns: A new #GstIndex instance.
710 gst_index_factory_create (GstIndexFactory *factory)
712 GstIndex *new = NULL;
714 g_return_val_if_fail (factory != NULL, NULL);
716 if (gst_plugin_feature_ensure_loaded (GST_PLUGIN_FEATURE (factory))) {
717 g_return_val_if_fail (factory->type != 0, NULL);
719 new = GST_INDEX (g_object_new(factory->type,NULL));
726 * gst_index_factory_make:
727 * @name: the name of the factory used to create the instance
729 * Create a new #GstIndex instance from the
730 * indexfactory with the given name.
732 * Returns: A new #GstIndex instance.
735 gst_index_factory_make (const gchar *name)
737 GstIndexFactory *factory;
739 g_return_val_if_fail (name != NULL, NULL);
741 factory = gst_index_factory_find (name);
746 return gst_index_factory_create (factory);