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