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