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