Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-good.git] / gst / flv / 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 /* do not resurrect this, add a derived dummy index class instead */
301 #if 0
302 /**
303  * gst_index_new:
304  *
305  * Create a new dummy index object. Use gst_element_set_index() to assign that
306  * to an element or pipeline. This index is not storing anything, but will
307  * still emit e.g. the #GstIndex::entry-added signal.
308  *
309  * Returns: (transfer full): a new index object
310  */
311 GstIndex *
312 gst_index_new (void)
313 {
314   GstIndex *index;
315
316   index = g_object_newv (gst_index_get_type (), 0, NULL);
317
318   return index;
319 }
320 #endif
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 #if 0
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 #endif
412
413 #if 0
414 /**
415  * gst_index_set_certainty:
416  * @index: the index to set the certainty on
417  * @certainty: the certainty to set
418  *
419  * Set the certainty of the given index.
420  */
421 void
422 gst_index_set_certainty (GstIndex * index, GstIndexCertainty certainty)
423 {
424   index->curgroup->certainty = certainty;
425 }
426
427 /**
428  * gst_index_get_certainty:
429  * @index: the index to get the certainty of
430  *
431  * Get the certainty of the given index.
432  *
433  * Returns: the certainty of the index.
434  */
435 GstIndexCertainty
436 gst_index_get_certainty (GstIndex * index)
437 {
438   return index->curgroup->certainty;
439 }
440 #endif
441
442 #if 0
443 /**
444  * gst_index_set_filter:
445  * @index: the index to register the filter on
446  * @filter: the filter to register
447  * @user_data: data passed to the filter function
448  *
449  * Lets the app register a custom filter function so that
450  * it can select what entries should be stored in the index.
451  */
452 void
453 gst_index_set_filter (GstIndex * index,
454     GstIndexFilter filter, gpointer user_data)
455 {
456   g_return_if_fail (GST_IS_INDEX (index));
457
458   gst_index_set_filter_full (index, filter, user_data, NULL);
459 }
460
461 /**
462  * gst_index_set_filter_full:
463  * @index: the index to register the filter on
464  * @filter: the filter to register
465  * @user_data: data passed to the filter function
466  * @user_data_destroy: function to call when @user_data is unset
467  *
468  * Lets the app register a custom filter function so that
469  * it can select what entries should be stored in the index.
470  */
471 void
472 gst_index_set_filter_full (GstIndex * index,
473     GstIndexFilter filter, gpointer user_data, GDestroyNotify user_data_destroy)
474 {
475   g_return_if_fail (GST_IS_INDEX (index));
476
477   if (index->filter_user_data && index->filter_user_data_destroy)
478     index->filter_user_data_destroy (index->filter_user_data);
479
480   index->filter = filter;
481   index->filter_user_data = user_data;
482   index->filter_user_data_destroy = user_data_destroy;
483 }
484
485 /**
486  * gst_index_set_resolver:
487  * @index: the index to register the resolver on
488  * @resolver: the resolver to register
489  * @user_data: data passed to the resolver function
490  *
491  * Lets the app register a custom function to map index
492  * ids to writer descriptions.
493  */
494 void
495 gst_index_set_resolver (GstIndex * index,
496     GstIndexResolver resolver, gpointer user_data)
497 {
498   gst_index_set_resolver_full (index, resolver, user_data, NULL);
499 }
500
501 /**
502  * gst_index_set_resolver_full:
503  * @index: the index to register the resolver on
504  * @resolver: the resolver to register
505  * @user_data: data passed to the resolver function
506  * @user_data_destroy: destroy function for @user_data
507  *
508  * Lets the app register a custom function to map index
509  * ids to writer descriptions.
510  *
511  * Since: 0.10.18
512  */
513 void
514 gst_index_set_resolver_full (GstIndex * index, GstIndexResolver resolver,
515     gpointer user_data, GDestroyNotify user_data_destroy)
516 {
517   g_return_if_fail (GST_IS_INDEX (index));
518
519   if (index->resolver_user_data && index->resolver_user_data_destroy)
520     index->resolver_user_data_destroy (index->resolver_user_data);
521
522   index->resolver = resolver;
523   index->resolver_user_data = user_data;
524   index->resolver_user_data_destroy = user_data_destroy;
525   index->method = GST_INDEX_RESOLVER_CUSTOM;
526 }
527 #endif
528
529 /**
530  * gst_index_entry_copy:
531  * @entry: the entry to copy
532  *
533  * Copies an entry and returns the result.
534  *
535  * Free-function: gst_index_entry_free
536  *
537  * Returns: (transfer full): a newly allocated #GstIndexEntry.
538  */
539 GstIndexEntry *
540 gst_index_entry_copy (GstIndexEntry * entry)
541 {
542   GstIndexEntry *new_entry = g_slice_new (GstIndexEntry);
543
544   memcpy (new_entry, entry, sizeof (GstIndexEntry));
545   return new_entry;
546 }
547
548 /**
549  * gst_index_entry_free:
550  * @entry: (transfer full): the entry to free
551  *
552  * Free the memory used by the given entry.
553  */
554 void
555 gst_index_entry_free (GstIndexEntry * entry)
556 {
557   switch (entry->type) {
558     case GST_INDEX_ENTRY_ID:
559       if (entry->data.id.description) {
560         g_free (entry->data.id.description);
561         entry->data.id.description = NULL;
562       }
563       break;
564     case GST_INDEX_ENTRY_ASSOCIATION:
565       if (entry->data.assoc.assocs) {
566         g_free (entry->data.assoc.assocs);
567         entry->data.assoc.assocs = NULL;
568       }
569       break;
570     case GST_INDEX_ENTRY_OBJECT:
571       break;
572     case GST_INDEX_ENTRY_FORMAT:
573       break;
574   }
575
576   g_slice_free (GstIndexEntry, entry);
577 }
578
579 #if 0
580 /**
581  * gst_index_add_format:
582  * @index: the index to add the entry to
583  * @id: the id of the index writer
584  * @format: the format to add to the index
585  *
586  * Adds a format entry into the index. This function is
587  * used to map dynamic GstFormat ids to their original
588  * format key.
589  *
590  * Free-function: gst_index_entry_free
591  *
592  * Returns: (transfer full): a pointer to the newly added entry in the index.
593  */
594 GstIndexEntry *
595 gst_index_add_format (GstIndex * index, gint id, GstFormat format)
596 {
597   GstIndexEntry *entry;
598   const GstFormatDefinition *def;
599
600   g_return_val_if_fail (GST_IS_INDEX (index), NULL);
601   g_return_val_if_fail (format != 0, NULL);
602
603   if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
604     return NULL;
605
606   entry = g_slice_new (GstIndexEntry);
607   entry->type = GST_INDEX_ENTRY_FORMAT;
608   entry->id = id;
609   entry->data.format.format = format;
610
611   def = gst_format_get_details (format);
612   entry->data.format.key = def->nick;
613
614   gst_index_add_entry (index, entry);
615
616   return entry;
617 }
618 #endif
619
620 /**
621  * gst_index_add_id:
622  * @index: the index to add the entry to
623  * @id: the id of the index writer
624  * @description: the description of the index writer
625  *
626  * Add an id entry into the index.
627  *
628  * Returns: a pointer to the newly added entry in the index.
629  */
630 GstIndexEntry *
631 gst_index_add_id (GstIndex * index, gint id, gchar * description)
632 {
633   GstIndexEntry *entry;
634
635   g_return_val_if_fail (GST_IS_INDEX (index), NULL);
636   g_return_val_if_fail (description != NULL, NULL);
637
638   if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
639     return NULL;
640
641   entry = g_slice_new (GstIndexEntry);
642   entry->type = GST_INDEX_ENTRY_ID;
643   entry->id = id;
644   entry->data.id.description = description;
645
646   gst_index_add_entry (index, entry);
647
648   return entry;
649 }
650
651 static gboolean
652 gst_index_path_resolver (GstIndex * index, GstObject * writer,
653     gchar ** writer_string, gpointer data)
654 {
655   *writer_string = gst_object_get_path_string (writer);
656
657   return TRUE;
658 }
659
660 static gboolean
661 gst_index_gtype_resolver (GstIndex * index, GstObject * writer,
662     gchar ** writer_string, gpointer data)
663 {
664   g_return_val_if_fail (writer != NULL, FALSE);
665
666   if (GST_IS_PAD (writer)) {
667     GstObject *element = gst_object_get_parent (GST_OBJECT (writer));
668     gchar *name;
669
670     name = gst_object_get_name (writer);
671     if (element) {
672       *writer_string = g_strdup_printf ("%s.%s",
673           G_OBJECT_TYPE_NAME (element), name);
674       gst_object_unref (element);
675     } else {
676       *writer_string = name;
677       name = NULL;
678     }
679
680     g_free (name);
681
682   } else {
683     *writer_string = g_strdup (G_OBJECT_TYPE_NAME (writer));
684   }
685
686   return TRUE;
687 }
688
689 /**
690  * gst_index_get_writer_id:
691  * @index: the index to get a unique write id for
692  * @writer: the GstObject to allocate an id for
693  * @id: a pointer to a gint to hold the id
694  *
695  * Before entries can be added to the index, a writer
696  * should obtain a unique id. The methods to add new entries
697  * to the index require this id as an argument.
698  *
699  * The application can implement a custom function to map the writer object
700  * to a string. That string will be used to register or look up an id
701  * in the index.
702  *
703  * <note>
704  * The caller must not hold @writer's #GST_OBJECT_LOCK, as the default
705  * resolver may call functions that take the object lock as well, and
706  * the lock is not recursive.
707  * </note>
708  *
709  * Returns: TRUE if the writer would be mapped to an id.
710  */
711 gboolean
712 gst_index_get_writer_id (GstIndex * index, GstObject * writer, gint * id)
713 {
714   gchar *writer_string = NULL;
715   GstIndexEntry *entry;
716   GstIndexClass *iclass;
717   gboolean success = FALSE;
718
719   g_return_val_if_fail (GST_IS_INDEX (index), FALSE);
720   g_return_val_if_fail (GST_IS_OBJECT (writer), FALSE);
721   g_return_val_if_fail (id, FALSE);
722
723   *id = -1;
724
725   /* first try to get a previously cached id */
726   entry = g_hash_table_lookup (index->writers, writer);
727   if (entry == NULL) {
728
729     iclass = GST_INDEX_GET_CLASS (index);
730
731     /* let the app make a string */
732     if (index->resolver) {
733       gboolean res;
734
735       res =
736           index->resolver (index, writer, &writer_string,
737           index->resolver_user_data);
738       if (!res)
739         return FALSE;
740     } else {
741       g_warning ("no resolver found");
742       return FALSE;
743     }
744
745     /* if the index has a resolver, make it map this string to an id */
746     if (iclass->get_writer_id) {
747       success = iclass->get_writer_id (index, id, writer_string);
748     }
749     /* if the index could not resolve, we allocate one ourselves */
750     if (!success) {
751       *id = ++index->last_id;
752     }
753
754     entry = gst_index_add_id (index, *id, writer_string);
755     if (!entry) {
756       /* index is probably not writable, make an entry anyway
757        * to keep it in our cache */
758       entry = g_slice_new (GstIndexEntry);
759       entry->type = GST_INDEX_ENTRY_ID;
760       entry->id = *id;
761       entry->data.id.description = writer_string;
762     }
763     g_hash_table_insert (index->writers, writer, entry);
764   } else {
765     *id = entry->id;
766   }
767
768   return TRUE;
769 }
770
771 static void
772 gst_index_add_entry (GstIndex * index, GstIndexEntry * entry)
773 {
774   GstIndexClass *iclass;
775
776   iclass = GST_INDEX_GET_CLASS (index);
777
778   if (iclass->add_entry) {
779     iclass->add_entry (index, entry);
780   }
781
782   g_signal_emit (index, gst_index_signals[ENTRY_ADDED], 0, entry);
783 }
784
785 /**
786  * gst_index_add_associationv:
787  * @index: the index to add the entry to
788  * @id: the id of the index writer
789  * @flags: optinal flags for this entry
790  * @n: number of associations
791  * @list: list of associations
792  *
793  * Associate given format/value pairs with each other.
794  *
795  * Returns: a pointer to the newly added entry in the index.
796  */
797 GstIndexEntry *
798 gst_index_add_associationv (GstIndex * index, gint id,
799     GstIndexAssociationFlags flags, gint n, const GstIndexAssociation * list)
800 {
801   GstIndexEntry *entry;
802
803   g_return_val_if_fail (n > 0, NULL);
804   g_return_val_if_fail (list != NULL, NULL);
805   g_return_val_if_fail (GST_IS_INDEX (index), NULL);
806
807   if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
808     return NULL;
809
810   entry = g_slice_new (GstIndexEntry);
811
812   entry->type = GST_INDEX_ENTRY_ASSOCIATION;
813   entry->id = id;
814   entry->data.assoc.flags = flags;
815   entry->data.assoc.assocs = g_memdup (list, sizeof (GstIndexAssociation) * n);
816   entry->data.assoc.nassocs = n;
817
818   gst_index_add_entry (index, entry);
819
820   return entry;
821 }
822
823 #if 0
824 /**
825  * gst_index_add_association:
826  * @index: the index to add the entry to
827  * @id: the id of the index writer
828  * @flags: optinal flags for this entry
829  * @format: the format of the value
830  * @value: the value
831  * @...: other format/value pairs or 0 to end the list
832  *
833  * Associate given format/value pairs with each other.
834  * Be sure to pass gint64 values to this functions varargs,
835  * you might want to use a gint64 cast to be sure.
836  *
837  * Returns: a pointer to the newly added entry in the index.
838  */
839 GstIndexEntry *
840 gst_index_add_association (GstIndex * index, gint id,
841     GstIndexAssociationFlags flags, GstFormat format, gint64 value, ...)
842 {
843   va_list args;
844   GstIndexEntry *entry;
845   GstIndexAssociation *list;
846   gint n_assocs = 0;
847   GstFormat cur_format;
848   GArray *array;
849
850   g_return_val_if_fail (GST_IS_INDEX (index), NULL);
851   g_return_val_if_fail (format != 0, NULL);
852
853   if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
854     return NULL;
855
856   array = g_array_new (FALSE, FALSE, sizeof (GstIndexAssociation));
857
858   {
859     GstIndexAssociation a;
860
861     a.format = format;
862     a.value = value;
863     n_assocs = 1;
864     g_array_append_val (array, a);
865   }
866
867   va_start (args, value);
868
869   while ((cur_format = va_arg (args, GstFormat))) {
870     GstIndexAssociation a;
871
872     a.format = cur_format;
873     a.value = va_arg (args, gint64);
874     n_assocs++;
875     g_array_append_val (array, a);
876   }
877
878   va_end (args);
879
880   list = (GstIndexAssociation *) g_array_free (array, FALSE);
881
882   entry = gst_index_add_associationv (index, id, flags, n_assocs, list);
883   g_free (list);
884
885   return entry;
886 }
887
888 /**
889  * gst_index_add_object:
890  * @index: the index to add the object to
891  * @id: the id of the index writer
892  * @key: a key for the object
893  * @type: the GType of the object
894  * @object: a pointer to the object to add
895  *
896  * Add the given object to the index with the given key.
897  *
898  * This function is not yet implemented.
899  *
900  * Returns: a pointer to the newly added entry in the index.
901  */
902 GstIndexEntry *
903 gst_index_add_object (GstIndex * index, gint id, gchar * key,
904     GType type, gpointer object)
905 {
906   if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
907     return NULL;
908
909   return NULL;
910 }
911 #endif
912
913 static gint
914 gst_index_compare_func (gconstpointer a, gconstpointer b, gpointer user_data)
915 {
916   if (a < b)
917     return -1;
918   if (a > b)
919     return 1;
920   return 0;
921 }
922
923 /**
924  * gst_index_get_assoc_entry:
925  * @index: the index to search
926  * @id: the id of the index writer
927  * @method: The lookup method to use
928  * @flags: Flags for the entry
929  * @format: the format of the value
930  * @value: the value to find
931  *
932  * Finds the given format/value in the index
933  *
934  * Returns: the entry associated with the value or NULL if the
935  *   value was not found.
936  */
937 GstIndexEntry *
938 gst_index_get_assoc_entry (GstIndex * index, gint id,
939     GstIndexLookupMethod method, GstIndexAssociationFlags flags,
940     GstFormat format, gint64 value)
941 {
942   g_return_val_if_fail (GST_IS_INDEX (index), NULL);
943
944   if (id == -1)
945     return NULL;
946
947   return gst_index_get_assoc_entry_full (index, id, method, flags, format,
948       value, gst_index_compare_func, NULL);
949 }
950
951 /**
952  * gst_index_get_assoc_entry_full:
953  * @index: the index to search
954  * @id: the id of the index writer
955  * @method: The lookup method to use
956  * @flags: Flags for the entry
957  * @format: the format of the value
958  * @value: the value to find
959  * @func: the function used to compare entries
960  * @user_data: user data passed to the compare function
961  *
962  * Finds the given format/value in the index with the given
963  * compare function and user_data.
964  *
965  * Returns: the entry associated with the value or NULL if the
966  *   value was not found.
967  */
968 GstIndexEntry *
969 gst_index_get_assoc_entry_full (GstIndex * index, gint id,
970     GstIndexLookupMethod method, GstIndexAssociationFlags flags,
971     GstFormat format, gint64 value, GCompareDataFunc func, gpointer user_data)
972 {
973   GstIndexClass *iclass;
974
975   g_return_val_if_fail (GST_IS_INDEX (index), NULL);
976
977   if (id == -1)
978     return NULL;
979
980   iclass = GST_INDEX_GET_CLASS (index);
981
982   if (iclass->get_assoc_entry)
983     return iclass->get_assoc_entry (index, id, method, flags, format, value,
984         func, user_data);
985
986   return NULL;
987 }
988
989 /**
990  * gst_index_entry_assoc_map:
991  * @entry: the index to search
992  * @format: the format of the value the find
993  * @value: a pointer to store the value
994  *
995  * Gets alternative formats associated with the indexentry.
996  *
997  * Returns: TRUE if there was a value associated with the given
998  * format.
999  */
1000 gboolean
1001 gst_index_entry_assoc_map (GstIndexEntry * entry,
1002     GstFormat format, gint64 * value)
1003 {
1004   gint i;
1005
1006   g_return_val_if_fail (entry != NULL, FALSE);
1007   g_return_val_if_fail (value != NULL, FALSE);
1008
1009   for (i = 0; i < GST_INDEX_NASSOCS (entry); i++) {
1010     if (GST_INDEX_ASSOC_FORMAT (entry, i) == format) {
1011       *value = GST_INDEX_ASSOC_VALUE (entry, i);
1012       return TRUE;
1013     }
1014   }
1015   return FALSE;
1016 }