0a70c3b3e20188d17a8e3c8cde0b79889bb3222a
[platform/upstream/gstreamer.git] / gst / gstindex.c
1 /* GStreamer
2  * Copyright (C) 2001 RidgeRun (http://www.ridgerun.com/)
3  * Written by Erik Walthinsen <omega@ridgerun.com>
4  *
5  * gstindex.c: Index for mappings and other data
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /**
24  * SECTION:gstindex
25  * @short_description: Generate indexes on objects
26  * @see_also: #GstIndexFactory
27  *
28  * GstIndex is used to generate a stream index of one or more elements
29  * in a pipeline.
30  */
31
32 #include "gst_private.h"
33
34 #include "gstinfo.h"
35 #include "gstindex.h"
36 #include "gstindexfactory.h"
37 #include "gstmarshal.h"
38 #include "gstregistry.h"
39 /* for constructing an entry name */
40 #include "gstelement.h"
41 #include "gstpad.h"
42 #include "gstinfo.h"
43
44 /* Index signals and args */
45 enum
46 {
47   ENTRY_ADDED,
48   LAST_SIGNAL
49 };
50
51 enum
52 {
53   ARG_0,
54   ARG_RESOLVER
55       /* FILL ME */
56 };
57
58 GST_DEBUG_CATEGORY_STATIC (index_debug);
59 #define GST_CAT_DEFAULT index_debug
60
61 static void gst_index_class_init (GstIndexClass * klass);
62 static void gst_index_init (GstIndex * index);
63 static void gst_index_finalize (GObject * object);
64
65 static void gst_index_set_property (GObject * object, guint prop_id,
66     const GValue * value, GParamSpec * pspec);
67 static void gst_index_get_property (GObject * object, guint prop_id,
68     GValue * value, GParamSpec * pspec);
69
70 static GstIndexGroup *gst_index_group_new (guint groupnum);
71
72 static gboolean gst_index_path_resolver (GstIndex * index, GstObject * writer,
73     gchar ** writer_string, gpointer data);
74 static gboolean gst_index_gtype_resolver (GstIndex * index, GstObject * writer,
75     gchar ** writer_string, gpointer data);
76 static void gst_index_add_entry (GstIndex * index, GstIndexEntry * entry);
77
78 static GstObject *parent_class = NULL;
79 static guint gst_index_signals[LAST_SIGNAL] = { 0 };
80
81 typedef struct
82 {
83   GstIndexResolverMethod method;
84   GstIndexResolver resolver;
85   gpointer user_data;
86 }
87 ResolverEntry;
88
89 static const ResolverEntry resolvers[] = {
90   {GST_INDEX_RESOLVER_CUSTOM, NULL, NULL},
91   {GST_INDEX_RESOLVER_GTYPE, gst_index_gtype_resolver, NULL},
92   {GST_INDEX_RESOLVER_PATH, gst_index_path_resolver, NULL},
93 };
94
95 #define GST_TYPE_INDEX_RESOLVER (gst_index_resolver_get_type())
96 static GType
97 gst_index_resolver_get_type (void)
98 {
99   static GType index_resolver_type = 0;
100   static const GEnumValue index_resolver[] = {
101     {GST_INDEX_RESOLVER_CUSTOM, "GST_INDEX_RESOLVER_CUSTOM", "custom"},
102     {GST_INDEX_RESOLVER_GTYPE, "GST_INDEX_RESOLVER_GTYPE", "gtype"},
103     {GST_INDEX_RESOLVER_PATH, "GST_INDEX_RESOLVER_PATH", "path"},
104     {0, NULL, NULL},
105   };
106
107   if (!index_resolver_type) {
108     index_resolver_type =
109         g_enum_register_static ("GstIndexResolver", index_resolver);
110   }
111   return index_resolver_type;
112 }
113
114 GType
115 gst_index_entry_get_type (void)
116 {
117   static GType index_entry_type = 0;
118
119   if (!index_entry_type) {
120     index_entry_type = g_boxed_type_register_static ("GstIndexEntry",
121         (GBoxedCopyFunc) gst_index_entry_copy,
122         (GBoxedFreeFunc) gst_index_entry_free);
123   }
124   return index_entry_type;
125 }
126
127 #define _do_init \
128 { \
129   GST_DEBUG_CATEGORY_INIT (index_debug, "GST_INDEX", GST_DEBUG_BOLD, \
130       "Generic indexing support"); \
131 }
132
133 G_DEFINE_TYPE_WITH_CODE (GstIndex, gst_index, GST_TYPE_OBJECT, _do_init);
134
135 static void
136 gst_index_class_init (GstIndexClass * klass)
137 {
138   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
139
140   parent_class = g_type_class_peek_parent (klass);
141
142   /**
143    * GstIndex::entry-added
144    * @gstindex: the object which received the signal.
145    * @arg1: The entry added to the index.
146    *
147    * Is emitted when a new entry is added to the index.
148    */
149   gst_index_signals[ENTRY_ADDED] =
150       g_signal_new ("entry-added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
151       G_STRUCT_OFFSET (GstIndexClass, entry_added), NULL, NULL,
152       gst_marshal_VOID__BOXED, G_TYPE_NONE, 1, GST_TYPE_INDEX_ENTRY);
153
154   gobject_class->set_property = gst_index_set_property;
155   gobject_class->get_property = gst_index_get_property;
156   gobject_class->finalize = gst_index_finalize;
157
158   g_object_class_install_property (gobject_class, ARG_RESOLVER,
159       g_param_spec_enum ("resolver", "Resolver",
160           "Select a predefined object to string mapper",
161           GST_TYPE_INDEX_RESOLVER, GST_INDEX_RESOLVER_PATH,
162           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
163 }
164
165 static void
166 gst_index_init (GstIndex * index)
167 {
168   index->curgroup = gst_index_group_new (0);
169   index->maxgroup = 0;
170   index->groups = g_list_prepend (NULL, index->curgroup);
171
172   index->writers = g_hash_table_new (NULL, NULL);
173   index->last_id = 0;
174
175   index->method = GST_INDEX_RESOLVER_PATH;
176   index->resolver = resolvers[index->method].resolver;
177   index->resolver_user_data = resolvers[index->method].user_data;
178
179   GST_OBJECT_FLAG_SET (index, GST_INDEX_WRITABLE);
180   GST_OBJECT_FLAG_SET (index, GST_INDEX_READABLE);
181
182   GST_DEBUG ("created new index");
183 }
184
185 static void
186 gst_index_free_writer (gpointer key, gpointer value, gpointer user_data)
187 {
188   GstIndexEntry *entry = (GstIndexEntry *) value;
189
190   if (entry) {
191     gst_index_entry_free (entry);
192   }
193 }
194
195 static void
196 gst_index_finalize (GObject * object)
197 {
198   GstIndex *index = GST_INDEX (object);
199
200   if (index->groups) {
201     g_list_foreach (index->groups, (GFunc) g_free, NULL);
202     g_list_free (index->groups);
203     index->groups = NULL;
204   }
205
206   if (index->writers) {
207     g_hash_table_foreach (index->writers, gst_index_free_writer, NULL);
208     g_hash_table_destroy (index->writers);
209     index->writers = NULL;
210   }
211
212   if (index->filter_user_data && index->filter_user_data_destroy)
213     index->filter_user_data_destroy (index->filter_user_data);
214
215   if (index->resolver_user_data && index->resolver_user_data_destroy)
216     index->resolver_user_data_destroy (index->resolver_user_data);
217
218   G_OBJECT_CLASS (parent_class)->finalize (object);
219 }
220
221 static void
222 gst_index_set_property (GObject * object, guint prop_id,
223     const GValue * value, GParamSpec * pspec)
224 {
225   GstIndex *index;
226
227   index = GST_INDEX (object);
228
229   switch (prop_id) {
230     case ARG_RESOLVER:
231       index->method = g_value_get_enum (value);
232       index->resolver = resolvers[index->method].resolver;
233       index->resolver_user_data = resolvers[index->method].user_data;
234       break;
235     default:
236       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
237       break;
238   }
239 }
240
241 static void
242 gst_index_get_property (GObject * object, guint prop_id,
243     GValue * value, GParamSpec * pspec)
244 {
245   GstIndex *index;
246
247   index = GST_INDEX (object);
248
249   switch (prop_id) {
250     case ARG_RESOLVER:
251       g_value_set_enum (value, index->method);
252       break;
253     default:
254       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
255       break;
256   }
257 }
258
259 static GstIndexGroup *
260 gst_index_group_new (guint groupnum)
261 {
262   GstIndexGroup *indexgroup = g_new (GstIndexGroup, 1);
263
264   indexgroup->groupnum = groupnum;
265   indexgroup->entries = NULL;
266   indexgroup->certainty = GST_INDEX_UNKNOWN;
267   indexgroup->peergroup = -1;
268
269   GST_DEBUG ("created new index group %d", groupnum);
270
271   return indexgroup;
272 }
273
274 /**
275  * gst_index_new:
276  *
277  * Create a new tileindex object
278  *
279  * Returns: a new index object
280  */
281 GstIndex *
282 gst_index_new (void)
283 {
284   GstIndex *index;
285
286   index = g_object_newv (gst_index_get_type (), 0, NULL);
287
288   return index;
289 }
290
291 /**
292  * gst_index_commit:
293  * @index: the index to commit
294  * @id: the writer that commited the index
295  *
296  * Tell the index that the writer with the given id is done
297  * with this index and is not going to write any more entries
298  * to it.
299  */
300 void
301 gst_index_commit (GstIndex * index, gint id)
302 {
303   GstIndexClass *iclass;
304
305   iclass = GST_INDEX_GET_CLASS (index);
306
307   if (iclass->commit)
308     iclass->commit (index, id);
309 }
310
311
312 /**
313  * gst_index_get_group:
314  * @index: the index to get the current group from
315  *
316  * Get the id of the current group.
317  *
318  * Returns: the id of the current group.
319  */
320 gint
321 gst_index_get_group (GstIndex * index)
322 {
323   return index->curgroup->groupnum;
324 }
325
326 /**
327  * gst_index_new_group:
328  * @index: the index to create the new group in
329  *
330  * Create a new group for the given index. It will be
331  * set as the current group.
332  *
333  * Returns: the id of the newly created group.
334  */
335 gint
336 gst_index_new_group (GstIndex * index)
337 {
338   index->curgroup = gst_index_group_new (++index->maxgroup);
339   index->groups = g_list_append (index->groups, index->curgroup);
340   GST_DEBUG ("created new group %d in index", index->maxgroup);
341   return index->maxgroup;
342 }
343
344 /**
345  * gst_index_set_group:
346  * @index: the index to set the new group in
347  * @groupnum: the groupnumber to set
348  *
349  * Set the current groupnumber to the given argument.
350  *
351  * Returns: TRUE if the operation succeeded, FALSE if the group
352  * did not exist.
353  */
354 gboolean
355 gst_index_set_group (GstIndex * index, gint groupnum)
356 {
357   GList *list;
358   GstIndexGroup *indexgroup;
359
360   /* first check for null change */
361   if (groupnum == index->curgroup->groupnum)
362     return TRUE;
363
364   /* else search for the proper group */
365   list = index->groups;
366   while (list) {
367     indexgroup = (GstIndexGroup *) (list->data);
368     list = g_list_next (list);
369     if (indexgroup->groupnum == groupnum) {
370       index->curgroup = indexgroup;
371       GST_DEBUG ("switched to index group %d", indexgroup->groupnum);
372       return TRUE;
373     }
374   }
375
376   /* couldn't find the group in question */
377   GST_DEBUG ("couldn't find index group %d", groupnum);
378   return FALSE;
379 }
380
381 /**
382  * gst_index_set_certainty:
383  * @index: the index to set the certainty on
384  * @certainty: the certainty to set
385  *
386  * Set the certainty of the given index.
387  */
388 void
389 gst_index_set_certainty (GstIndex * index, GstIndexCertainty certainty)
390 {
391   index->curgroup->certainty = certainty;
392 }
393
394 /**
395  * gst_index_get_certainty:
396  * @index: the index to get the certainty of
397  *
398  * Get the certainty of the given index.
399  *
400  * Returns: the certainty of the index.
401  */
402 GstIndexCertainty
403 gst_index_get_certainty (GstIndex * index)
404 {
405   return index->curgroup->certainty;
406 }
407
408 /**
409  * gst_index_set_filter:
410  * @index: the index to register the filter on
411  * @filter: the filter to register
412  * @user_data: data passed to the filter function
413  *
414  * Lets the app register a custom filter function so that
415  * it can select what entries should be stored in the index.
416  */
417 void
418 gst_index_set_filter (GstIndex * index,
419     GstIndexFilter filter, gpointer user_data)
420 {
421   g_return_if_fail (GST_IS_INDEX (index));
422
423   gst_index_set_filter_full (index, filter, user_data, NULL);
424 }
425
426 /**
427  * gst_index_set_filter_full:
428  * @index: the index to register the filter on
429  * @filter: the filter to register
430  * @user_data: data passed to the filter function
431  * @user_data_destroy: function to call when @user_data is unset
432  *
433  * Lets the app register a custom filter function so that
434  * it can select what entries should be stored in the index.
435  */
436 void
437 gst_index_set_filter_full (GstIndex * index,
438     GstIndexFilter filter, gpointer user_data, GDestroyNotify user_data_destroy)
439 {
440   g_return_if_fail (GST_IS_INDEX (index));
441
442   if (index->filter_user_data && index->filter_user_data_destroy)
443     index->filter_user_data_destroy (index->filter_user_data);
444
445   index->filter = filter;
446   index->filter_user_data = user_data;
447   index->filter_user_data_destroy = user_data_destroy;
448 }
449
450 /**
451  * gst_index_set_resolver:
452  * @index: the index to register the resolver on
453  * @resolver: the resolver to register
454  * @user_data: data passed to the resolver function
455  *
456  * Lets the app register a custom function to map index
457  * ids to writer descriptions.
458  */
459 void
460 gst_index_set_resolver (GstIndex * index,
461     GstIndexResolver resolver, gpointer user_data)
462 {
463   gst_index_set_resolver_full (index, resolver, user_data, NULL);
464 }
465
466 /**
467  * gst_index_set_resolver_full:
468  * @index: the index to register the resolver on
469  * @resolver: the resolver to register
470  * @user_data: data passed to the resolver function
471  * @user_data_destroy: destroy function for @user_data
472  *
473  * Lets the app register a custom function to map index
474  * ids to writer descriptions.
475  *
476  * Since: 0.10.18
477  */
478 void
479 gst_index_set_resolver_full (GstIndex * index, GstIndexResolver resolver,
480     gpointer user_data, GDestroyNotify user_data_destroy)
481 {
482   g_return_if_fail (GST_IS_INDEX (index));
483
484   if (index->resolver_user_data && index->resolver_user_data_destroy)
485     index->resolver_user_data_destroy (index->resolver_user_data);
486
487   index->resolver = resolver;
488   index->resolver_user_data = user_data;
489   index->resolver_user_data_destroy = user_data_destroy;
490   index->method = GST_INDEX_RESOLVER_CUSTOM;
491 }
492
493 /**
494  * gst_index_entry_copy:
495  * @entry: the entry to copy
496  *
497  * Copies an entry and returns the result.
498  *
499  * Returns: a newly allocated #GstIndexEntry.
500  */
501 GstIndexEntry *
502 gst_index_entry_copy (GstIndexEntry * entry)
503 {
504   return g_memdup (entry, sizeof (*entry));
505 }
506
507 /**
508  * gst_index_entry_free:
509  * @entry: the entry to free
510  *
511  * Free the memory used by the given entry.
512  */
513 void
514 gst_index_entry_free (GstIndexEntry * entry)
515 {
516   switch (entry->type) {
517     case GST_INDEX_ENTRY_ID:
518       if (entry->data.id.description) {
519         g_free (entry->data.id.description);
520         entry->data.id.description = NULL;
521       }
522       break;
523     case GST_INDEX_ENTRY_ASSOCIATION:
524       if (entry->data.assoc.assocs) {
525         g_free (entry->data.assoc.assocs);
526         entry->data.assoc.assocs = NULL;
527       }
528       break;
529     case GST_INDEX_ENTRY_OBJECT:
530       break;
531     case GST_INDEX_ENTRY_FORMAT:
532       break;
533   }
534
535   g_free (entry);
536 }
537
538 /**
539  * gst_index_add_format:
540  * @index: the index to add the entry to
541  * @id: the id of the index writer
542  * @format: the format to add to the index
543  *
544  * Adds a format entry into the index. This function is
545  * used to map dynamic GstFormat ids to their original
546  * format key.
547  *
548  * Returns: a pointer to the newly added entry in the index.
549  */
550 GstIndexEntry *
551 gst_index_add_format (GstIndex * index, gint id, GstFormat format)
552 {
553   GstIndexEntry *entry;
554   const GstFormatDefinition *def;
555
556   g_return_val_if_fail (GST_IS_INDEX (index), NULL);
557   g_return_val_if_fail (format != 0, NULL);
558
559   if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
560     return NULL;
561
562   entry = g_new0 (GstIndexEntry, 1);
563   entry->type = GST_INDEX_ENTRY_FORMAT;
564   entry->id = id;
565   entry->data.format.format = format;
566
567   def = gst_format_get_details (format);
568   entry->data.format.key = def->nick;
569
570   gst_index_add_entry (index, entry);
571
572   return entry;
573 }
574
575 /**
576  * gst_index_add_id:
577  * @index: the index to add the entry to
578  * @id: the id of the index writer
579  * @description: the description of the index writer
580  *
581  * Add an id entry into the index.
582  *
583  * Returns: a pointer to the newly added entry in the index.
584  */
585 GstIndexEntry *
586 gst_index_add_id (GstIndex * index, gint id, gchar * description)
587 {
588   GstIndexEntry *entry;
589
590   g_return_val_if_fail (GST_IS_INDEX (index), NULL);
591   g_return_val_if_fail (description != NULL, NULL);
592
593   if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
594     return NULL;
595
596   entry = g_new0 (GstIndexEntry, 1);
597   entry->type = GST_INDEX_ENTRY_ID;
598   entry->id = id;
599   entry->data.id.description = description;
600
601   gst_index_add_entry (index, entry);
602
603   return entry;
604 }
605
606 static gboolean
607 gst_index_path_resolver (GstIndex * index, GstObject * writer,
608     gchar ** writer_string, gpointer data)
609 {
610   *writer_string = gst_object_get_path_string (writer);
611
612   return TRUE;
613 }
614
615 static gboolean
616 gst_index_gtype_resolver (GstIndex * index, GstObject * writer,
617     gchar ** writer_string, gpointer data)
618 {
619   g_return_val_if_fail (writer != NULL, FALSE);
620
621   if (GST_IS_PAD (writer)) {
622     GstElement *element =
623         (GstElement *) gst_object_get_parent (GST_OBJECT (writer));
624     gchar *name;
625
626     name = gst_object_get_name (writer);
627     *writer_string = g_strdup_printf ("%s.%s",
628         g_type_name (G_OBJECT_TYPE (element)), name);
629
630     gst_object_unref (element);
631     g_free (name);
632
633   } else {
634     *writer_string =
635         g_strdup_printf ("%s", g_type_name (G_OBJECT_TYPE (writer)));
636   }
637
638   return TRUE;
639 }
640
641 /**
642  * gst_index_get_writer_id:
643  * @index: the index to get a unique write id for
644  * @writer: the GstObject to allocate an id for
645  * @id: a pointer to a gint to hold the id
646  *
647  * Before entries can be added to the index, a writer
648  * should obtain a unique id. The methods to add new entries
649  * to the index require this id as an argument.
650  *
651  * The application can implement a custom function to map the writer object
652  * to a string. That string will be used to register or look up an id
653  * in the index.
654  *
655  * Returns: TRUE if the writer would be mapped to an id.
656  */
657 gboolean
658 gst_index_get_writer_id (GstIndex * index, GstObject * writer, gint * id)
659 {
660   gchar *writer_string = NULL;
661   GstIndexEntry *entry;
662   GstIndexClass *iclass;
663   gboolean success = FALSE;
664
665   g_return_val_if_fail (GST_IS_INDEX (index), FALSE);
666   g_return_val_if_fail (GST_IS_OBJECT (writer), FALSE);
667   g_return_val_if_fail (id, FALSE);
668
669   *id = -1;
670
671   /* first try to get a previously cached id */
672   entry = g_hash_table_lookup (index->writers, writer);
673   if (entry == NULL) {
674
675     iclass = GST_INDEX_GET_CLASS (index);
676
677     /* let the app make a string */
678     if (index->resolver) {
679       gboolean res;
680
681       res =
682           index->resolver (index, writer, &writer_string,
683           index->resolver_user_data);
684       if (!res)
685         return FALSE;
686     } else {
687       g_warning ("no resolver found");
688       return FALSE;
689     }
690
691     /* if the index has a resolver, make it map this string to an id */
692     if (iclass->get_writer_id) {
693       success = iclass->get_writer_id (index, id, writer_string);
694     }
695     /* if the index could not resolve, we allocate one ourselves */
696     if (!success) {
697       *id = ++index->last_id;
698     }
699
700     entry = gst_index_add_id (index, *id, writer_string);
701     if (!entry) {
702       /* index is probably not writable, make an entry anyway
703        * to keep it in our cache */
704       entry = g_new0 (GstIndexEntry, 1);
705       entry->type = GST_INDEX_ENTRY_ID;
706       entry->id = *id;
707       entry->data.id.description = writer_string;
708     }
709     g_hash_table_insert (index->writers, writer, entry);
710   } else {
711     *id = entry->id;
712   }
713
714   return TRUE;
715 }
716
717 static void
718 gst_index_add_entry (GstIndex * index, GstIndexEntry * entry)
719 {
720   GstIndexClass *iclass;
721
722   iclass = GST_INDEX_GET_CLASS (index);
723
724   if (iclass->add_entry) {
725     iclass->add_entry (index, entry);
726   }
727
728   g_signal_emit (index, gst_index_signals[ENTRY_ADDED], 0, entry);
729 }
730
731 /**
732  * gst_index_add_associationv:
733  * @index: the index to add the entry to
734  * @id: the id of the index writer
735  * @flags: optinal flags for this entry
736  * @n: number of associations
737  * @list: list of associations
738  *
739  * Associate given format/value pairs with each other.
740  *
741  * Returns: a pointer to the newly added entry in the index.
742  */
743 GstIndexEntry *
744 gst_index_add_associationv (GstIndex * index, gint id, GstAssocFlags flags,
745     gint n, const GstIndexAssociation * list)
746 {
747   GstIndexEntry *entry;
748
749   g_return_val_if_fail (n > 0, NULL);
750   g_return_val_if_fail (list != NULL, NULL);
751   g_return_val_if_fail (GST_IS_INDEX (index), NULL);
752
753   if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
754     return NULL;
755
756   entry = g_malloc (sizeof (GstIndexEntry));
757
758   entry->type = GST_INDEX_ENTRY_ASSOCIATION;
759   entry->id = id;
760   entry->data.assoc.flags = flags;
761   entry->data.assoc.assocs = g_memdup (list, sizeof (GstIndexAssociation) * n);
762   entry->data.assoc.nassocs = n;
763
764   gst_index_add_entry (index, entry);
765
766   return entry;
767 }
768
769 /**
770  * gst_index_add_association:
771  * @index: the index to add the entry to
772  * @id: the id of the index writer
773  * @flags: optinal flags for this entry
774  * @format: the format of the value
775  * @value: the value
776  * @...: other format/value pairs or 0 to end the list
777  *
778  * Associate given format/value pairs with each other.
779  * Be sure to pass gint64 values to this functions varargs,
780  * you might want to use a gint64 cast to be sure.
781  *
782  * Returns: a pointer to the newly added entry in the index.
783  */
784 GstIndexEntry *
785 gst_index_add_association (GstIndex * index, gint id, GstAssocFlags flags,
786     GstFormat format, gint64 value, ...)
787 {
788   va_list args;
789   GstIndexEntry *entry;
790   GstIndexAssociation *list;
791   gint n_assocs = 0;
792   GstFormat cur_format;
793   GArray *array;
794
795   g_return_val_if_fail (GST_IS_INDEX (index), NULL);
796   g_return_val_if_fail (format != 0, NULL);
797
798   if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
799     return NULL;
800
801   array = g_array_new (FALSE, FALSE, sizeof (GstIndexAssociation));
802
803   {
804     GstIndexAssociation a;
805
806     a.format = format;
807     a.value = value;
808     n_assocs = 1;
809     g_array_append_val (array, a);
810   }
811
812   va_start (args, value);
813
814   while ((cur_format = va_arg (args, GstFormat))) {
815     GstIndexAssociation a;
816
817     a.format = cur_format;
818     a.value = va_arg (args, gint64);
819     n_assocs++;
820     g_array_append_val (array, a);
821   }
822
823   va_end (args);
824
825   list = (GstIndexAssociation *) g_array_free (array, FALSE);
826
827   entry = gst_index_add_associationv (index, id, flags, n_assocs, list);
828   g_free (list);
829
830   return entry;
831 }
832
833 /**
834  * gst_index_add_object:
835  * @index: the index to add the object to
836  * @id: the id of the index writer
837  * @key: a key for the object
838  * @type: the GType of the object
839  * @object: a pointer to the object to add
840  *
841  * Add the given object to the index with the given key.
842  *
843  * This function is not yet implemented.
844  *
845  * Returns: a pointer to the newly added entry in the index.
846  */
847 GstIndexEntry *
848 gst_index_add_object (GstIndex * index, gint id, gchar * key,
849     GType type, gpointer object)
850 {
851   if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
852     return NULL;
853
854   return NULL;
855 }
856
857 static gint
858 gst_index_compare_func (gconstpointer a, gconstpointer b, gpointer user_data)
859 {
860   if (a < b)
861     return -1;
862   if (a > b)
863     return 1;
864   return 0;
865 }
866
867 /**
868  * gst_index_get_assoc_entry:
869  * @index: the index to search
870  * @id: the id of the index writer
871  * @method: The lookup method to use
872  * @flags: Flags for the entry
873  * @format: the format of the value
874  * @value: the value to find
875  *
876  * Finds the given format/value in the index
877  *
878  * Returns: the entry associated with the value or NULL if the
879  *   value was not found.
880  */
881 GstIndexEntry *
882 gst_index_get_assoc_entry (GstIndex * index, gint id,
883     GstIndexLookupMethod method, GstAssocFlags flags,
884     GstFormat format, gint64 value)
885 {
886   g_return_val_if_fail (GST_IS_INDEX (index), NULL);
887
888   if (id == -1)
889     return NULL;
890
891   return gst_index_get_assoc_entry_full (index, id, method, flags, format,
892       value, gst_index_compare_func, NULL);
893 }
894
895 /**
896  * gst_index_get_assoc_entry_full:
897  * @index: the index to search
898  * @id: the id of the index writer
899  * @method: The lookup method to use
900  * @flags: Flags for the entry
901  * @format: the format of the value
902  * @value: the value to find
903  * @func: the function used to compare entries
904  * @user_data: user data passed to the compare function
905  *
906  * Finds the given format/value in the index with the given
907  * compare function and user_data.
908  *
909  * Returns: the entry associated with the value or NULL if the
910  *   value was not found.
911  */
912 GstIndexEntry *
913 gst_index_get_assoc_entry_full (GstIndex * index, gint id,
914     GstIndexLookupMethod method, GstAssocFlags flags,
915     GstFormat format, gint64 value, GCompareDataFunc func, gpointer user_data)
916 {
917   GstIndexClass *iclass;
918
919   g_return_val_if_fail (GST_IS_INDEX (index), NULL);
920
921   if (id == -1)
922     return NULL;
923
924   iclass = GST_INDEX_GET_CLASS (index);
925
926   if (iclass->get_assoc_entry)
927     return iclass->get_assoc_entry (index, id, method, flags, format, value,
928         func, user_data);
929
930   return NULL;
931 }
932
933 /**
934  * gst_index_entry_assoc_map:
935  * @entry: the index to search
936  * @format: the format of the value the find
937  * @value: a pointer to store the value
938  *
939  * Gets alternative formats associated with the indexentry.
940  *
941  * Returns: TRUE if there was a value associated with the given
942  * format.
943  */
944 gboolean
945 gst_index_entry_assoc_map (GstIndexEntry * entry,
946     GstFormat format, gint64 * value)
947 {
948   gint i;
949
950   g_return_val_if_fail (entry != NULL, FALSE);
951   g_return_val_if_fail (value != NULL, FALSE);
952
953   for (i = 0; i < GST_INDEX_NASSOCS (entry); i++) {
954     if (GST_INDEX_ASSOC_FORMAT (entry, i) == format) {
955       *value = GST_INDEX_ASSOC_VALUE (entry, i);
956       return TRUE;
957     }
958   }
959   return FALSE;
960 }