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