Use g_memdup2() where available and add fallback for older GLib versions
[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 entires 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 #ifdef TIZEN_FEATURE_UPSTREAM
65 #include "gst/glib-compat-private.h"
66 #endif /* TIZEN_FEATURE_UPSTREAM */
67
68 /* Index signals and args */
69 enum
70 {
71   ENTRY_ADDED,
72   LAST_SIGNAL
73 };
74
75 enum
76 {
77   ARG_0,
78   ARG_RESOLVER
79       /* FILL ME */
80 };
81
82 #if 0
83 GST_DEBUG_CATEGORY_STATIC (index_debug);
84 #define GST_CAT_DEFAULT index_debug
85 #endif
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 guint gst_index_signals[LAST_SIGNAL] = { 0 };
104
105 typedef struct
106 {
107   GstIndexResolverMethod method;
108   GstIndexResolver resolver;
109   gpointer user_data;
110 }
111 ResolverEntry;
112
113 static const ResolverEntry resolvers[] = {
114   {GST_INDEX_RESOLVER_CUSTOM, NULL, NULL},
115   {GST_INDEX_RESOLVER_GTYPE, gst_index_gtype_resolver, NULL},
116   {GST_INDEX_RESOLVER_PATH, gst_index_path_resolver, NULL},
117 };
118
119 #define GST_TYPE_INDEX_RESOLVER (gst_index_resolver_get_type())
120 static GType
121 gst_index_resolver_get_type (void)
122 {
123   static GType index_resolver_type = 0;
124   static const GEnumValue index_resolver[] = {
125     {GST_INDEX_RESOLVER_CUSTOM, "GST_INDEX_RESOLVER_CUSTOM", "custom"},
126     {GST_INDEX_RESOLVER_GTYPE, "GST_INDEX_RESOLVER_GTYPE", "gtype"},
127     {GST_INDEX_RESOLVER_PATH, "GST_INDEX_RESOLVER_PATH", "path"},
128     {0, NULL, NULL},
129   };
130
131   if (!index_resolver_type) {
132     index_resolver_type =
133         g_enum_register_static ("GstIndexResolver", index_resolver);
134   }
135   return index_resolver_type;
136 }
137
138 G_DEFINE_BOXED_TYPE (GstIndexEntry, gst_index_entry,
139     (GBoxedCopyFunc) gst_index_entry_copy,
140     (GBoxedFreeFunc) gst_index_entry_free);
141
142 #if 0
143 #define _do_init \
144 { \
145   GST_DEBUG_CATEGORY_INIT (index_debug, "GST_INDEX", GST_DEBUG_BOLD, \
146       "Generic indexing support"); \
147 }
148 #endif
149
150 G_DEFINE_TYPE (GstIndex, gst_index, GST_TYPE_OBJECT);
151
152 static void
153 gst_index_class_init (GstIndexClass * klass)
154 {
155   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
156
157   /**
158    * GstIndex::entry-added
159    * @gstindex: the object which received the signal.
160    * @arg1: The entry added to the index.
161    *
162    * Is emitted when a new entry is added to the index.
163    */
164   gst_index_signals[ENTRY_ADDED] =
165       g_signal_new ("entry-added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
166       G_STRUCT_OFFSET (GstIndexClass, entry_added), NULL, NULL,
167       g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_INDEX_ENTRY);
168
169   gobject_class->set_property = gst_index_set_property;
170   gobject_class->get_property = gst_index_get_property;
171   gobject_class->finalize = gst_index_finalize;
172
173   g_object_class_install_property (gobject_class, ARG_RESOLVER,
174       g_param_spec_enum ("resolver", "Resolver",
175           "Select a predefined object to string mapper",
176           GST_TYPE_INDEX_RESOLVER, GST_INDEX_RESOLVER_PATH,
177           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
178 }
179
180 static void
181 gst_index_init (GstIndex * index)
182 {
183   index->curgroup = gst_index_group_new (0);
184   index->maxgroup = 0;
185   index->groups = g_list_prepend (NULL, index->curgroup);
186
187   index->writers = g_hash_table_new (NULL, NULL);
188   index->last_id = 0;
189
190   index->method = GST_INDEX_RESOLVER_PATH;
191   index->resolver = resolvers[index->method].resolver;
192   index->resolver_user_data = resolvers[index->method].user_data;
193
194   GST_OBJECT_FLAG_SET (index, GST_INDEX_WRITABLE);
195   GST_OBJECT_FLAG_SET (index, GST_INDEX_READABLE);
196
197   GST_DEBUG ("created new index");
198 }
199
200 static void
201 gst_index_free_writer (gpointer key, gpointer value, gpointer user_data)
202 {
203   GstIndexEntry *entry = (GstIndexEntry *) value;
204
205   if (entry) {
206     gst_index_entry_free (entry);
207   }
208 }
209
210 static void
211 gst_index_finalize (GObject * object)
212 {
213   GstIndex *index = GST_INDEX (object);
214
215   if (index->groups) {
216     g_list_foreach (index->groups, (GFunc) gst_index_group_free, NULL);
217     g_list_free (index->groups);
218     index->groups = NULL;
219   }
220
221   if (index->writers) {
222     g_hash_table_foreach (index->writers, gst_index_free_writer, NULL);
223     g_hash_table_destroy (index->writers);
224     index->writers = NULL;
225   }
226
227   if (index->filter_user_data && index->filter_user_data_destroy)
228     index->filter_user_data_destroy (index->filter_user_data);
229
230   if (index->resolver_user_data && index->resolver_user_data_destroy)
231     index->resolver_user_data_destroy (index->resolver_user_data);
232
233   G_OBJECT_CLASS (gst_index_parent_class)->finalize (object);
234 }
235
236 static void
237 gst_index_set_property (GObject * object, guint prop_id,
238     const GValue * value, GParamSpec * pspec)
239 {
240   GstIndex *index;
241
242   index = GST_INDEX (object);
243
244   switch (prop_id) {
245     case ARG_RESOLVER:
246       index->method = (GstIndexResolverMethod) g_value_get_enum (value);
247       index->resolver = resolvers[index->method].resolver;
248       index->resolver_user_data = resolvers[index->method].user_data;
249       break;
250     default:
251       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252       break;
253   }
254 }
255
256 static void
257 gst_index_get_property (GObject * object, guint prop_id,
258     GValue * value, GParamSpec * pspec)
259 {
260   GstIndex *index;
261
262   index = GST_INDEX (object);
263
264   switch (prop_id) {
265     case ARG_RESOLVER:
266       g_value_set_enum (value, index->method);
267       break;
268     default:
269       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
270       break;
271   }
272 }
273
274 static GstIndexGroup *
275 gst_index_group_new (guint groupnum)
276 {
277   GstIndexGroup *indexgroup = g_slice_new (GstIndexGroup);
278
279   indexgroup->groupnum = groupnum;
280   indexgroup->entries = NULL;
281   indexgroup->certainty = GST_INDEX_UNKNOWN;
282   indexgroup->peergroup = -1;
283
284   GST_DEBUG ("created new index group %d", groupnum);
285
286   return indexgroup;
287 }
288
289 static void
290 gst_index_group_free (GstIndexGroup * group)
291 {
292   g_slice_free (GstIndexGroup, group);
293 }
294
295 /* do not resurrect this, add a derived dummy index class instead */
296 #if 0
297 /**
298  * gst_index_new:
299  *
300  * Create a new dummy index object. Use gst_element_set_index() to assign that
301  * to an element or pipeline. This index is not storing anything, but will
302  * still emit e.g. the #GstIndex::entry-added signal.
303  *
304  * Returns: (transfer full): a new index object
305  */
306 GstIndex *
307 gst_index_new (void)
308 {
309   GstIndex *index;
310
311   index = g_object_new (gst_index_get_type (), NULL);
312
313   return index;
314 }
315
316 /**
317  * gst_index_commit:
318  * @index: the index to commit
319  * @id: the writer that committed the index
320  *
321  * Tell the index that the writer with the given id is done
322  * with this index and is not going to write any more entries
323  * to it.
324  */
325 void
326 gst_index_commit (GstIndex * index, gint id)
327 {
328   GstIndexClass *iclass;
329
330   iclass = GST_INDEX_GET_CLASS (index);
331
332   if (iclass->commit)
333     iclass->commit (index, id);
334 }
335
336 /**
337  * gst_index_get_group:
338  * @index: the index to get the current group from
339  *
340  * Get the id of the current group.
341  *
342  * Returns: the id of the current group.
343  */
344 gint
345 gst_index_get_group (GstIndex * index)
346 {
347   return index->curgroup->groupnum;
348 }
349
350 /**
351  * gst_index_new_group:
352  * @index: the index to create the new group in
353  *
354  * Create a new group for the given index. It will be
355  * set as the current group.
356  *
357  * Returns: the id of the newly created group.
358  */
359 gint
360 gst_index_new_group (GstIndex * index)
361 {
362   index->curgroup = gst_index_group_new (++index->maxgroup);
363   index->groups = g_list_append (index->groups, index->curgroup);
364   GST_DEBUG ("created new group %d in index", index->maxgroup);
365   return index->maxgroup;
366 }
367
368 /**
369  * gst_index_set_group:
370  * @index: the index to set the new group in
371  * @groupnum: the groupnumber to set
372  *
373  * Set the current groupnumber to the given argument.
374  *
375  * Returns: %TRUE if the operation succeeded, %FALSE if the group
376  * did not exist.
377  */
378 gboolean
379 gst_index_set_group (GstIndex * index, gint groupnum)
380 {
381   GList *list;
382   GstIndexGroup *indexgroup;
383
384   /* first check for null change */
385   if (groupnum == index->curgroup->groupnum)
386     return TRUE;
387
388   /* else search for the proper group */
389   list = index->groups;
390   while (list) {
391     indexgroup = (GstIndexGroup *) (list->data);
392     list = g_list_next (list);
393     if (indexgroup->groupnum == groupnum) {
394       index->curgroup = indexgroup;
395       GST_DEBUG ("switched to index group %d", indexgroup->groupnum);
396       return TRUE;
397     }
398   }
399
400   /* couldn't find the group in question */
401   GST_DEBUG ("couldn't find index group %d", groupnum);
402   return FALSE;
403 }
404 #endif
405
406 #if 0
407 /**
408  * gst_index_set_certainty:
409  * @index: the index to set the certainty on
410  * @certainty: the certainty to set
411  *
412  * Set the certainty of the given index.
413  */
414 void
415 gst_index_set_certainty (GstIndex * index, GstIndexCertainty certainty)
416 {
417   index->curgroup->certainty = certainty;
418 }
419
420 /**
421  * gst_index_get_certainty:
422  * @index: the index to get the certainty of
423  *
424  * Get the certainty of the given index.
425  *
426  * Returns: the certainty of the index.
427  */
428 GstIndexCertainty
429 gst_index_get_certainty (GstIndex * index)
430 {
431   return index->curgroup->certainty;
432 }
433 #endif
434
435 #if 0
436 /**
437  * gst_index_set_filter:
438  * @index: the index to register the filter on
439  * @filter: the filter to register
440  * @user_data: data passed to the filter function
441  *
442  * Lets the app register a custom filter function so that
443  * it can select what entries should be stored in the index.
444  */
445 void
446 gst_index_set_filter (GstIndex * index,
447     GstIndexFilter filter, gpointer user_data)
448 {
449   g_return_if_fail (GST_IS_INDEX (index));
450
451   gst_index_set_filter_full (index, filter, user_data, NULL);
452 }
453
454 /**
455  * gst_index_set_filter_full:
456  * @index: the index to register the filter on
457  * @filter: the filter to register
458  * @user_data: data passed to the filter function
459  * @user_data_destroy: function to call when @user_data is unset
460  *
461  * Lets the app register a custom filter function so that
462  * it can select what entries should be stored in the index.
463  */
464 void
465 gst_index_set_filter_full (GstIndex * index,
466     GstIndexFilter filter, gpointer user_data, GDestroyNotify user_data_destroy)
467 {
468   g_return_if_fail (GST_IS_INDEX (index));
469
470   if (index->filter_user_data && index->filter_user_data_destroy)
471     index->filter_user_data_destroy (index->filter_user_data);
472
473   index->filter = filter;
474   index->filter_user_data = user_data;
475   index->filter_user_data_destroy = user_data_destroy;
476 }
477
478 /**
479  * gst_index_set_resolver:
480  * @index: the index to register the resolver on
481  * @resolver: the resolver to register
482  * @user_data: data passed to the resolver function
483  *
484  * Lets the app register a custom function to map index
485  * ids to writer descriptions.
486  */
487 void
488 gst_index_set_resolver (GstIndex * index,
489     GstIndexResolver resolver, gpointer user_data)
490 {
491   gst_index_set_resolver_full (index, resolver, user_data, NULL);
492 }
493
494 /**
495  * gst_index_set_resolver_full:
496  * @index: the index to register the resolver on
497  * @resolver: the resolver to register
498  * @user_data: data passed to the resolver function
499  * @user_data_destroy: destroy function for @user_data
500  *
501  * Lets the app register a custom function to map index
502  * ids to writer descriptions.
503  */
504 void
505 gst_index_set_resolver_full (GstIndex * index, GstIndexResolver resolver,
506     gpointer user_data, GDestroyNotify user_data_destroy)
507 {
508   g_return_if_fail (GST_IS_INDEX (index));
509
510   if (index->resolver_user_data && index->resolver_user_data_destroy)
511     index->resolver_user_data_destroy (index->resolver_user_data);
512
513   index->resolver = resolver;
514   index->resolver_user_data = user_data;
515   index->resolver_user_data_destroy = user_data_destroy;
516   index->method = GST_INDEX_RESOLVER_CUSTOM;
517 }
518 #endif
519
520 /**
521  * gst_index_entry_copy:
522  * @entry: the entry to copy
523  *
524  * Copies an entry and returns the result.
525  *
526  * Free-function: gst_index_entry_free
527  *
528  * Returns: (transfer full): a newly allocated #GstIndexEntry.
529  */
530 GstIndexEntry *
531 gst_index_entry_copy (GstIndexEntry * entry)
532 {
533   GstIndexEntry *new_entry = g_slice_new (GstIndexEntry);
534
535   memcpy (new_entry, entry, sizeof (GstIndexEntry));
536   return new_entry;
537 }
538
539 /**
540  * gst_index_entry_free:
541  * @entry: (transfer full): the entry to free
542  *
543  * Free the memory used by the given entry.
544  */
545 void
546 gst_index_entry_free (GstIndexEntry * entry)
547 {
548   switch (entry->type) {
549     case GST_INDEX_ENTRY_ID:
550       if (entry->data.id.description) {
551         g_free (entry->data.id.description);
552         entry->data.id.description = NULL;
553       }
554       break;
555     case GST_INDEX_ENTRY_ASSOCIATION:
556       if (entry->data.assoc.assocs) {
557         g_free (entry->data.assoc.assocs);
558         entry->data.assoc.assocs = NULL;
559       }
560       break;
561     case GST_INDEX_ENTRY_OBJECT:
562       break;
563     case GST_INDEX_ENTRY_FORMAT:
564       break;
565   }
566
567   g_slice_free (GstIndexEntry, entry);
568 }
569
570 #if 0
571 /**
572  * gst_index_add_format:
573  * @index: the index to add the entry to
574  * @id: the id of the index writer
575  * @format: the format to add to the index
576  *
577  * Adds a format entry into the index. This function is
578  * used to map dynamic #GstFormat ids to their original
579  * format key.
580  *
581  * Free-function: gst_index_entry_free
582  *
583  * Returns: (transfer full): a pointer to the newly added entry in the index.
584  */
585 GstIndexEntry *
586 gst_index_add_format (GstIndex * index, gint id, GstFormat format)
587 {
588   GstIndexEntry *entry;
589   const GstFormatDefinition *def;
590
591   g_return_val_if_fail (GST_IS_INDEX (index), NULL);
592   g_return_val_if_fail (format != 0, NULL);
593
594   if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
595     return NULL;
596
597   entry = g_slice_new (GstIndexEntry);
598   entry->type = GST_INDEX_ENTRY_FORMAT;
599   entry->id = id;
600   entry->data.format.format = format;
601
602   def = gst_format_get_details (format);
603   entry->data.format.key = def->nick;
604
605   gst_index_add_entry (index, entry);
606
607   return entry;
608 }
609 #endif
610
611 /**
612  * gst_index_add_id:
613  * @index: the index to add the entry to
614  * @id: the id of the index writer
615  * @description: the description of the index writer
616  *
617  * Add an id entry into the index.
618  *
619  * Returns: a pointer to the newly added entry in the index.
620  */
621 GstIndexEntry *
622 gst_index_add_id (GstIndex * index, gint id, gchar * description)
623 {
624   GstIndexEntry *entry;
625
626   g_return_val_if_fail (GST_IS_INDEX (index), NULL);
627   g_return_val_if_fail (description != NULL, NULL);
628
629   if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
630     return NULL;
631
632   entry = g_slice_new (GstIndexEntry);
633   entry->type = GST_INDEX_ENTRY_ID;
634   entry->id = id;
635   entry->data.id.description = description;
636
637   gst_index_add_entry (index, entry);
638
639   return entry;
640 }
641
642 static gboolean
643 gst_index_path_resolver (GstIndex * index, GstObject * writer,
644     gchar ** writer_string, gpointer data)
645 {
646   *writer_string = gst_object_get_path_string (writer);
647
648   return TRUE;
649 }
650
651 static gboolean
652 gst_index_gtype_resolver (GstIndex * index, GstObject * writer,
653     gchar ** writer_string, gpointer data)
654 {
655   g_return_val_if_fail (writer != NULL, FALSE);
656
657   if (GST_IS_PAD (writer)) {
658     GstObject *element = gst_object_get_parent (GST_OBJECT (writer));
659     gchar *name;
660
661     name = gst_object_get_name (writer);
662     if (element) {
663       *writer_string = g_strdup_printf ("%s.%s",
664           G_OBJECT_TYPE_NAME (element), name);
665       gst_object_unref (element);
666     } else {
667       *writer_string = name;
668       name = NULL;
669     }
670
671     g_free (name);
672
673   } else {
674     *writer_string = g_strdup (G_OBJECT_TYPE_NAME (writer));
675   }
676
677   return TRUE;
678 }
679
680 /**
681  * gst_index_get_writer_id:
682  * @index: the index to get a unique write id for
683  * @writer: the #GstObject to allocate an id for
684  * @id: a pointer to a gint to hold the id
685  *
686  * Before entries can be added to the index, a writer
687  * should obtain a unique id. The methods to add new entries
688  * to the index require this id as an argument.
689  *
690  * The application can implement a custom function to map the writer object
691  * to a string. That string will be used to register or look up an id
692  * in the index.
693  *
694  * > The caller must not hold @writer's GST_OBJECT_LOCK(), as the default
695  * > resolver may call functions that take the object lock as well, and
696  * > the lock is not recursive.
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: optional flags for this entry
779  * @n: number of associations
780  * @list: (array length=n): 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 #ifdef TIZEN_FEATURE_UPSTREAM
805   entry->data.assoc.assocs = g_memdup2 (list, sizeof (GstIndexAssociation) * n);
806 #else /* TIZEN_FEATURE_UPSTREAM */
807   entry->data.assoc.assocs = g_memdup (list, sizeof (GstIndexAssociation) * n);
808 #endif /* TIZEN_FEATURE_UPSTREAM */
809   entry->data.assoc.nassocs = n;
810
811   gst_index_add_entry (index, entry);
812
813   return entry;
814 }
815
816 #if 0
817 /**
818  * gst_index_add_association:
819  * @index: the index to add the entry to
820  * @id: the id of the index writer
821  * @flags: optional flags for this entry
822  * @format: the format of the value
823  * @value: the value
824  * @...: other format/value pairs or 0 to end the list
825  *
826  * Associate given format/value pairs with each other.
827  * Be sure to pass gint64 values to this functions varargs,
828  * you might want to use a gint64 cast to be sure.
829  *
830  * Returns: a pointer to the newly added entry in the index.
831  */
832 GstIndexEntry *
833 gst_index_add_association (GstIndex * index, gint id,
834     GstIndexAssociationFlags flags, GstFormat format, gint64 value, ...)
835 {
836   va_list args;
837   GstIndexEntry *entry;
838   GstIndexAssociation *list;
839   gint n_assocs = 0;
840   GstFormat cur_format;
841   GArray *array;
842
843   g_return_val_if_fail (GST_IS_INDEX (index), NULL);
844   g_return_val_if_fail (format != 0, NULL);
845
846   if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
847     return NULL;
848
849   array = g_array_new (FALSE, FALSE, sizeof (GstIndexAssociation));
850
851   {
852     GstIndexAssociation a;
853
854     a.format = format;
855     a.value = value;
856     n_assocs = 1;
857     g_array_append_val (array, a);
858   }
859
860   va_start (args, value);
861
862   while ((cur_format = va_arg (args, GstFormat))) {
863     GstIndexAssociation a;
864
865     a.format = cur_format;
866     a.value = va_arg (args, gint64);
867     n_assocs++;
868     g_array_append_val (array, a);
869   }
870
871   va_end (args);
872
873   list = (GstIndexAssociation *) g_array_free (array, FALSE);
874
875   entry = gst_index_add_associationv (index, id, flags, n_assocs, list);
876   g_free (list);
877
878   return entry;
879 }
880
881 /**
882  * gst_index_add_object:
883  * @index: the index to add the object to
884  * @id: the id of the index writer
885  * @key: a key for the object
886  * @type: the GType of the object
887  * @object: a pointer to the object to add
888  *
889  * Add the given object to the index with the given key.
890  *
891  * This function is not yet implemented.
892  *
893  * Returns: a pointer to the newly added entry in the index.
894  */
895 GstIndexEntry *
896 gst_index_add_object (GstIndex * index, gint id, gchar * key,
897     GType type, gpointer object)
898 {
899   if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
900     return NULL;
901
902   return NULL;
903 }
904 #endif
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: (nullable): 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, GstIndexAssociationFlags 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: (nullable): 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, GstIndexAssociationFlags 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 }