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