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