fix doc build fix autogen
[platform/upstream/gstreamer.git] / 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 #include "gst_private.h"
24
25 #include "gstinfo.h"
26 #include "gstregistrypool.h"
27 #include "gstpad.h"
28 #include "gstindex.h"
29
30 /* Index signals and args */
31 enum {
32   ENTRY_ADDED,
33   LAST_SIGNAL
34 };
35
36 enum {
37   ARG_0,
38   ARG_RESOLVER,
39   /* FILL ME */
40 };
41
42 static void             gst_index_class_init            (GstIndexClass *klass);
43 static void             gst_index_init                  (GstIndex *index);
44
45 static void             gst_index_set_property          (GObject *object, guint prop_id,
46                                                          const GValue *value, GParamSpec *pspec);
47 static void             gst_index_get_property          (GObject *object, guint prop_id, 
48                                                          GValue *value, GParamSpec *pspec);
49
50 static GstIndexGroup*   gst_index_group_new             (guint groupnum);
51
52 static gboolean         gst_index_path_resolver         (GstIndex *index, GstObject *writer,
53                                                          gchar **writer_string, gpointer data);
54 static gboolean         gst_index_gtype_resolver        (GstIndex *index, GstObject *writer,
55                                                          gchar **writer_string, gpointer data);
56
57 static GstObject *parent_class = NULL;
58 static guint gst_index_signals[LAST_SIGNAL] = { 0 };
59
60 typedef struct
61 {
62   GstIndexResolverMethod method;
63   GstIndexResolver       resolver;
64   gpointer               user_data;
65 } ResolverEntry;
66
67 static const ResolverEntry resolvers[] =
68 {
69   { GST_INDEX_RESOLVER_CUSTOM, NULL, NULL },
70   { GST_INDEX_RESOLVER_GTYPE,  gst_index_gtype_resolver, NULL },
71   { GST_INDEX_RESOLVER_PATH,   gst_index_path_resolver, NULL },
72 };
73
74 #define GST_TYPE_INDEX_RESOLVER (gst_index_resolver_get_type())
75 static GType
76 gst_index_resolver_get_type (void)
77 {
78   static GType index_resolver_type = 0;
79   static GEnumValue index_resolver[] = {
80     { GST_INDEX_RESOLVER_CUSTOM, "GST_INDEX_RESOLVER_CUSTOM",  "Use a custom resolver"},
81     { GST_INDEX_RESOLVER_GTYPE,  "GST_INDEX_RESOLVER_GTYPE",   "Resolve an object to its GType[.padname]"},
82     { GST_INDEX_RESOLVER_PATH,   "GST_INDEX_RESOLVER_PATH",    "Resolve an object to its path in the pipeline"},
83     {0, NULL, NULL},
84   };
85   if (!index_resolver_type) {
86     index_resolver_type = g_enum_register_static ("GstIndexResolver", index_resolver);
87   }
88   return index_resolver_type;
89 }
90
91 GType
92 gst_index_entry_get_type (void)
93 {
94   static GType index_entry_type = 0;
95
96   if (!index_entry_type) {
97     index_entry_type = g_boxed_type_register_static ("GstIndexEntry",
98         (GBoxedCopyFunc) gst_index_entry_copy,
99         (GBoxedFreeFunc) gst_index_entry_free);
100   }
101   return index_entry_type;
102 }
103
104
105 GType
106 gst_index_get_type (void) {
107   static GType index_type = 0;
108
109   if (!index_type) {
110     static const GTypeInfo index_info = {
111       sizeof(GstIndexClass),
112       NULL,
113       NULL,
114       (GClassInitFunc)gst_index_class_init,
115       NULL,
116       NULL,
117       sizeof(GstIndex),
118       1,
119       (GInstanceInitFunc)gst_index_init,
120       NULL
121     };
122     index_type = g_type_register_static(GST_TYPE_OBJECT, "GstIndex", &index_info, 0);
123   }
124   return index_type;
125 }
126
127 static void
128 gst_index_class_init (GstIndexClass *klass)
129 {
130   GObjectClass *gobject_class;
131   GstElementClass *gstelement_class;
132
133   gobject_class = (GObjectClass*)klass;
134   gstelement_class = (GstElementClass*)klass;
135
136   parent_class = g_type_class_ref(GST_TYPE_OBJECT);
137
138   gst_index_signals[ENTRY_ADDED] =
139     g_signal_new ("entry_added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
140                   G_STRUCT_OFFSET (GstIndexClass, entry_added), NULL, NULL,
141                   gst_marshal_VOID__POINTER, G_TYPE_NONE, 1,
142                   G_TYPE_POINTER);
143
144   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_index_set_property);
145   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_index_get_property);
146
147   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_RESOLVER,
148     g_param_spec_enum ("resolver", "Resolver", "Select a predefined object to string mapper",
149                        GST_TYPE_INDEX_RESOLVER, GST_INDEX_RESOLVER_PATH, G_PARAM_READWRITE));
150 }
151
152 static void
153 gst_index_init (GstIndex *index)
154 {
155   index->curgroup = gst_index_group_new(0);
156   index->maxgroup = 0;
157   index->groups = g_list_prepend(NULL, index->curgroup);
158
159   index->writers = g_hash_table_new (NULL, NULL);
160   index->last_id = 0;
161
162   index->method = GST_INDEX_RESOLVER_PATH;
163   index->resolver = resolvers[index->method].resolver;
164   index->resolver_user_data = resolvers[index->method].user_data;
165
166   GST_FLAG_SET (index, GST_INDEX_WRITABLE);
167   GST_FLAG_SET (index, GST_INDEX_READABLE);
168   
169   GST_DEBUG ( "created new index");
170 }
171
172 static void
173 gst_index_set_property (GObject *object, guint prop_id,
174                         const GValue *value, GParamSpec *pspec)
175 {
176   GstIndex *index;
177
178   index = GST_INDEX (object);
179
180   switch (prop_id) {
181     case ARG_RESOLVER:
182       index->method = g_value_get_enum (value);
183       index->resolver = resolvers[index->method].resolver;
184       index->resolver_user_data = resolvers[index->method].user_data;
185       break;
186     default:
187       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188       break;
189   }
190 }
191
192 static void
193 gst_index_get_property (GObject *object, guint prop_id, 
194                         GValue *value, GParamSpec *pspec)
195 {
196   GstIndex *index;
197
198   index = GST_INDEX (object);
199
200   switch (prop_id) {
201     case ARG_RESOLVER:
202       g_value_set_enum (value, index->method);
203       break;
204     default:
205       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
206       break;
207   }
208 }
209
210 static GstIndexGroup *
211 gst_index_group_new(guint groupnum)
212 {
213   GstIndexGroup *indexgroup = g_new(GstIndexGroup,1);
214
215   indexgroup->groupnum = groupnum;
216   indexgroup->entries = NULL;
217   indexgroup->certainty = GST_INDEX_UNKNOWN;
218   indexgroup->peergroup = -1;
219
220   GST_DEBUG ( "created new index group %d",groupnum);
221
222   return indexgroup;
223 }
224
225 /**
226  * gst_index_new:
227  *
228  * Create a new tileindex object
229  *
230  * Returns: a new index object
231  */
232 GstIndex*
233 gst_index_new (void)
234 {
235   GstIndex *index;
236
237   index = g_object_new (gst_index_get_type (), NULL);
238
239   return index;
240 }
241
242 /**
243  * gst_index_commit:
244  * @index: the index to commit
245  * @id: the writer that commited the index
246  *
247  * Tell the index that the writer with the given id is done
248  * with this index and is not going to write any more entries
249  * to it.
250  */
251 void
252 gst_index_commit (GstIndex *index, gint id)
253 {
254   GstIndexClass *iclass;
255
256   iclass = GST_INDEX_GET_CLASS (index);
257
258   if (iclass->commit)
259     iclass->commit (index, id);
260 }
261
262
263 /**
264  * gst_index_get_group:
265  * @index: the index to get the current group from
266  *
267  * Get the id of the current group.
268  *
269  * Returns: the id of the current group.
270  */
271 gint
272 gst_index_get_group(GstIndex *index)
273 {
274   return index->curgroup->groupnum;
275 }
276
277 /**
278  * gst_index_new_group:
279  * @index: the index to create the new group in
280  *
281  * Create a new group for the given index. It will be
282  * set as the current group.
283  *
284  * Returns: the id of the newly created group.
285  */
286 gint
287 gst_index_new_group(GstIndex *index)
288 {
289   index->curgroup = gst_index_group_new(++index->maxgroup);
290   index->groups = g_list_append(index->groups,index->curgroup);
291   GST_DEBUG ( "created new group %d in index",index->maxgroup);
292   return index->maxgroup;
293 }
294
295 /**
296  * gst_index_set_group:
297  * @index: the index to set the new group in
298  * @groupnum: the groupnumber to set
299  *
300  * Set the current groupnumber to the given argument.
301  *
302  * Returns: TRUE if the operation succeeded, FALSE if the group
303  * did not exist.
304  */
305 gboolean
306 gst_index_set_group(GstIndex *index, gint groupnum)
307 {
308   GList *list;
309   GstIndexGroup *indexgroup;
310
311   /* first check for null change */
312   if (groupnum == index->curgroup->groupnum)
313     return TRUE;
314
315   /* else search for the proper group */
316   list = index->groups;
317   while (list) {
318     indexgroup = (GstIndexGroup *)(list->data);
319     list = g_list_next(list);
320     if (indexgroup->groupnum == groupnum) {
321       index->curgroup = indexgroup;
322       GST_DEBUG ( "switched to index group %d", indexgroup->groupnum);
323       return TRUE;
324     }
325   }
326
327   /* couldn't find the group in question */
328   GST_DEBUG ( "couldn't find index group %d",groupnum);
329   return FALSE;
330 }
331
332 /**
333  * gst_index_set_certainty:
334  * @index: the index to set the certainty on
335  * @certainty: the certainty to set
336  *
337  * Set the certainty of the given index.
338  */
339 void
340 gst_index_set_certainty(GstIndex *index, GstIndexCertainty certainty)
341 {
342   index->curgroup->certainty = certainty;
343 }
344
345 /**
346  * gst_index_get_certainty:
347  * @index: the index to get the certainty of
348  *
349  * Get the certainty of the given index.
350  *
351  * Returns: the certainty of the index.
352  */
353 GstIndexCertainty
354 gst_index_get_certainty(GstIndex *index)
355 {
356   return index->curgroup->certainty;
357 }
358
359 /**
360  * gst_index_set_filter:
361  * @index: the index to register the filter on
362  * @filter: the filter to register
363  * @user_data: data passed to the filter function
364  *
365  * Lets the app register a custom filter function so that
366  * it can select what entries should be stored in the index.
367  */
368 void
369 gst_index_set_filter (GstIndex *index, 
370                       GstIndexFilter filter, gpointer user_data)
371 {
372   g_return_if_fail (GST_IS_INDEX (index));
373
374   index->filter = filter;
375   index->filter_user_data = user_data;
376 }
377
378 /**
379  * gst_index_set_resolver:
380  * @index: the index to register the resolver on
381  * @resolver: the resolver to register
382  * @user_data: data passed to the resolver function
383  *
384  * Lets the app register a custom function to map index
385  * ids to writer descriptions.
386  */
387 void
388 gst_index_set_resolver (GstIndex *index, 
389                         GstIndexResolver resolver, gpointer user_data)
390 {
391   g_return_if_fail (GST_IS_INDEX (index));
392
393   index->resolver = resolver;
394   index->resolver_user_data = user_data;
395   index->method = GST_INDEX_RESOLVER_CUSTOM;
396 }
397
398 /**
399  * gst_index_entry_copy:
400  * @entry: the entry to copy
401  *
402  * Copies an entry and returns the result.
403  */
404 GstIndexEntry *
405 gst_index_entry_copy (GstIndexEntry *entry)
406 {
407   return g_memdup(entry, sizeof(*entry));
408 }
409
410 /**
411  * gst_index_entry_free:
412  * @entry: the entry to free
413  *
414  * Free the memory used by the given entry.
415  */
416 void
417 gst_index_entry_free (GstIndexEntry *entry)
418 {
419   g_free (entry);
420 }
421
422 /**
423  * gst_index_add_format:
424  * @index: the index to add the entry to
425  * @id: the id of the index writer
426  * @format: the format to add to the index
427  *
428  * Adds a format entry into the index. This function is
429  * used to map dynamic GstFormat ids to their original
430  * format key.
431  *
432  * Returns: a pointer to the newly added entry in the index.
433  */
434 GstIndexEntry*
435 gst_index_add_format (GstIndex *index, gint id, GstFormat format)
436 {
437   GstIndexEntry *entry;
438   const GstFormatDefinition* def;
439   GstIndexClass *iclass;
440
441   g_return_val_if_fail (GST_IS_INDEX (index), NULL);
442   g_return_val_if_fail (format != 0, NULL);
443
444   if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
445     return NULL;
446   
447   entry = g_new0 (GstIndexEntry, 1);
448   entry->type = GST_INDEX_ENTRY_FORMAT;
449   entry->id = id;
450   entry->data.format.format = format;
451
452   def = gst_format_get_details (format);
453   entry->data.format.key = def->nick;
454
455   iclass = GST_INDEX_GET_CLASS (index);
456   
457   if (iclass->add_entry)
458     iclass->add_entry (index, entry);
459
460   g_signal_emit (G_OBJECT (index), gst_index_signals[ENTRY_ADDED], 0, entry);
461
462   return entry;
463 }
464
465 /**
466  * gst_index_add_id:
467  * @index: the index to add the entry to
468  * @id: the id of the index writer
469  * @description: the description of the index writer
470  *
471  * Add an id entry into the index.
472  *
473  * Returns: a pointer to the newly added entry in the index.
474  */
475 GstIndexEntry*
476 gst_index_add_id (GstIndex *index, gint id, gchar *description)
477 {
478   GstIndexEntry *entry;
479   GstIndexClass *iclass;
480
481   g_return_val_if_fail (GST_IS_INDEX (index), NULL);
482   g_return_val_if_fail (description != NULL, NULL);
483   
484   if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
485     return NULL;
486   
487   entry = g_new0 (GstIndexEntry, 1);
488   entry->type = GST_INDEX_ENTRY_ID;
489   entry->id = id;
490   entry->data.id.description = description;
491
492   iclass = GST_INDEX_GET_CLASS (index);
493
494   if (iclass->add_entry)
495     iclass->add_entry (index, entry);
496   
497   g_signal_emit (G_OBJECT (index), gst_index_signals[ENTRY_ADDED], 0, entry);
498
499   return entry;
500 }
501
502 static gboolean
503 gst_index_path_resolver (GstIndex *index, GstObject *writer,
504                          gchar **writer_string, gpointer data) 
505 {
506   *writer_string = gst_object_get_path_string (writer);
507
508   return TRUE;
509 }
510
511 static gboolean
512 gst_index_gtype_resolver (GstIndex *index, GstObject *writer,
513                           gchar **writer_string, gpointer data) 
514 {
515   if (GST_IS_PAD (writer)) {
516     GstElement *element = gst_pad_get_parent (GST_PAD (writer));
517
518     *writer_string = g_strdup_printf ("%s.%s", 
519                      g_type_name (G_OBJECT_TYPE (element)),
520                      gst_object_get_name (writer));
521   }
522   else {
523     *writer_string = g_strdup_printf ("%s", g_type_name (G_OBJECT_TYPE (writer)));
524   }
525
526   return TRUE;
527 }
528
529 /**
530  * gst_index_get_writer_id:
531  * @index: the index to get a unique write id for
532  * @writer: the GstObject to allocate an id for
533  * @id: a pointer to a gint to hold the id
534  *
535  * Before entries can be added to the index, a writer
536  * should obtain a unique id. The methods to add new entries
537  * to the index require this id as an argument. 
538  *
539  * The application can implement a custom function to map the writer object 
540  * to a string. That string will be used to register or look up an id
541  * in the index.
542  *
543  * Returns: TRUE if the writer would be mapped to an id.
544  */
545 gboolean 
546 gst_index_get_writer_id (GstIndex *index, GstObject *writer, gint *id)
547 {
548   gchar *writer_string = NULL;
549   GstIndexEntry *entry;
550   GstIndexClass *iclass;
551   gboolean success = FALSE;
552
553   g_return_val_if_fail (GST_IS_INDEX (index), FALSE);
554   g_return_val_if_fail (GST_IS_OBJECT (writer), FALSE);
555   g_return_val_if_fail (id, FALSE);
556
557   *id = -1;
558
559   /* first try to get a previously cached id */
560   entry = g_hash_table_lookup (index->writers, writer);
561   if (entry == NULL) { 
562
563     iclass = GST_INDEX_GET_CLASS (index);
564
565     /* let the app make a string */
566     if (index->resolver) {
567       gboolean res;
568
569       res = index->resolver (index, writer, &writer_string, index->resolver_user_data);
570       if (!res) 
571         return FALSE;
572     }
573     else {
574       g_warning ("no resolver found");
575       return FALSE;
576     }
577
578     /* if the index has a resolver, make it map this string to an id */
579     if (iclass->get_writer_id) {
580       success = iclass->get_writer_id (index, id, writer_string);
581     }
582     /* if the index could not resolve, we allocate one ourselves */
583     if (!success) {
584       *id = ++index->last_id;
585     }
586
587     entry = gst_index_add_id (index, *id, writer_string);
588     if (!entry) {
589       /* index is probably not writable, make an entry anyway
590        * to keep it in our cache */
591       entry = g_new0 (GstIndexEntry, 1);
592       entry->type = GST_INDEX_ENTRY_ID;
593       entry->id = *id;
594       entry->data.id.description = writer_string;
595     }
596     g_hash_table_insert (index->writers, writer, entry);
597   }
598   else {
599     *id = entry->id;
600   }
601
602   return TRUE;
603 }
604
605 /**
606  * gst_index_add_association:
607  * @index: the index to add the entry to
608  * @id: the id of the index writer
609  * @flags: optinal flags for this entry
610  * @format: the format of the value
611  * @value: the value 
612  * @...: other format/value pairs or 0 to end the list
613  *
614  * Associate given format/value pairs with eachother.
615  * Be sure to pass gint64 values to this functions varargs,
616  * you might want to use a gint64 cast to be sure.
617  *
618  * Returns: a pointer to the newly added entry in the index.
619  */
620 GstIndexEntry*
621 gst_index_add_association (GstIndex *index, gint id, GstAssocFlags flags, 
622                            GstFormat format, gint64 value, ...)
623 {
624   va_list args;
625   GstIndexAssociation *assoc;
626   GstIndexEntry *entry;
627   gulong size;
628   gint nassocs = 0;
629   GstFormat cur_format;
630   volatile gint64 dummy;
631   GstIndexClass *iclass;
632
633   g_return_val_if_fail (GST_IS_INDEX (index), NULL);
634   g_return_val_if_fail (format != 0, NULL);
635   
636   if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
637     return NULL;
638   
639   va_start (args, value);
640
641   cur_format = format;
642
643   while (cur_format) {
644     nassocs++;
645     cur_format = va_arg (args, GstFormat);
646     if (cur_format)
647       dummy = va_arg (args, gint64);
648   }
649   va_end (args);
650
651   /* make room for two assoc */
652   size = sizeof (GstIndexEntry) + (sizeof (GstIndexAssociation) * nassocs);
653
654   entry = g_malloc (size);
655
656   entry->type = GST_INDEX_ENTRY_ASSOCIATION;
657   entry->id = id;
658   entry->data.assoc.flags = flags;
659   assoc = (GstIndexAssociation *) (((guint8 *) entry) + sizeof (GstIndexEntry));
660   entry->data.assoc.assocs = assoc;
661   entry->data.assoc.nassocs = nassocs;
662
663   va_start (args, value);
664   while (format) {
665     assoc->format = format;
666     assoc->value = value;
667
668     assoc++;
669
670     format = va_arg (args, GstFormat);
671     if (format)
672       value = va_arg (args, gint64);
673   }
674   va_end (args);
675
676   iclass = GST_INDEX_GET_CLASS (index);
677
678   if (iclass->add_entry)
679     iclass->add_entry (index, entry);
680
681   g_signal_emit (G_OBJECT (index), gst_index_signals[ENTRY_ADDED], 0, entry);
682
683   return entry;
684 }
685
686 /**
687  * gst_index_add_object:
688  * @index: the index to add the object to
689  * @id: the id of the index writer
690  * @key: a key for the object
691  * @type: the GType of the object
692  * @object: a pointer to the object to add
693  *
694  * Add the given object to the index with the given key.
695  * 
696  * Returns: a pointer to the newly added entry in the index.
697  */
698 GstIndexEntry*
699 gst_index_add_object (GstIndex *index, gint id, gchar *key,
700                       GType type, gpointer object)
701 {
702   if (!GST_INDEX_IS_WRITABLE (index) || id == -1)
703     return NULL;
704   
705   return NULL;
706 }
707
708 static gint
709 gst_index_compare_func (gconstpointer a,
710                         gconstpointer b,
711                         gpointer user_data)
712 {
713   return a - b;  
714 }
715
716 /**
717  * gst_index_get_assoc_entry:
718  * @index: the index to search
719  * @id: the id of the index writer
720  * @method: The lookup method to use
721  * @flags: Flags for the entry
722  * @format: the format of the value
723  * @value: the value to find
724  *
725  * Finds the given format/value in the index
726  *
727  * Returns: the entry associated with the value or NULL if the
728  *   value was not found.
729  */
730 GstIndexEntry*
731 gst_index_get_assoc_entry (GstIndex *index, gint id,
732                            GstIndexLookupMethod method, GstAssocFlags flags,
733                            GstFormat format, gint64 value)
734 {
735   g_return_val_if_fail (GST_IS_INDEX (index), NULL);
736
737   if (id == -1)
738     return NULL;
739
740   return gst_index_get_assoc_entry_full (index, id, method, flags, format, value, 
741                                   gst_index_compare_func, NULL);
742 }
743
744 /**
745  * gst_index_get_assoc_entry_full:
746  * @index: the index to search
747  * @id: the id of the index writer
748  * @method: The lookup method to use
749  * @flags: Flags for the entry
750  * @format: the format of the value
751  * @value: the value to find
752  * @func: the function used to compare entries
753  * @user_data: user data passed to the compare function
754  *
755  * Finds the given format/value in the index with the given
756  * compare function and user_data.
757  *
758  * Returns: the entry associated with the value or NULL if the
759  *   value was not found.
760  */
761 GstIndexEntry*
762 gst_index_get_assoc_entry_full (GstIndex *index, gint id,
763                                 GstIndexLookupMethod method, GstAssocFlags flags,
764                                 GstFormat format, gint64 value,
765                                 GCompareDataFunc func,
766                                 gpointer user_data)
767 {
768   GstIndexClass *iclass;
769
770   g_return_val_if_fail (GST_IS_INDEX (index), NULL);
771
772   if (id == -1)
773     return NULL;
774
775   iclass = GST_INDEX_GET_CLASS (index);
776
777   if (iclass->get_assoc_entry)
778     return iclass->get_assoc_entry (index, id, method, flags, format, value, func, user_data);
779   
780   return NULL;
781 }
782
783 /**
784  * gst_index_entry_assoc_map:
785  * @entry: the index to search
786  * @format: the format of the value the find
787  * @value: a pointer to store the value
788  *
789  * Gets alternative formats associated with the indexentry.
790  *
791  * Returns: TRUE if there was a value associated with the given 
792  * format.
793  */
794 gboolean
795 gst_index_entry_assoc_map (GstIndexEntry *entry,
796                            GstFormat format, gint64 *value)
797 {
798   gint i;
799
800   g_return_val_if_fail (entry != NULL, FALSE);
801   g_return_val_if_fail (value != NULL, FALSE);
802
803   for (i = 0; i < GST_INDEX_NASSOCS (entry); i++) {
804      if (GST_INDEX_ASSOC_FORMAT (entry, i) == format) {
805        *value = GST_INDEX_ASSOC_VALUE (entry, i);
806        return TRUE;
807      }
808   }
809   return FALSE;
810 }
811
812
813 static void             gst_index_factory_class_init            (GstIndexFactoryClass *klass);
814 static void             gst_index_factory_init          (GstIndexFactory *factory);
815
816 static GstPluginFeatureClass *factory_parent_class = NULL;
817 /* static guint gst_index_factory_signals[LAST_SIGNAL] = { 0 }; */
818
819 GType 
820 gst_index_factory_get_type (void) 
821 {
822   static GType indexfactory_type = 0;
823
824   if (!indexfactory_type) {
825     static const GTypeInfo indexfactory_info = {
826       sizeof (GstIndexFactoryClass),
827       NULL,
828       NULL,
829       (GClassInitFunc) gst_index_factory_class_init,
830       NULL,
831       NULL,
832       sizeof(GstIndexFactory),
833       0,
834       (GInstanceInitFunc) gst_index_factory_init,
835       NULL
836     };
837     indexfactory_type = g_type_register_static (GST_TYPE_PLUGIN_FEATURE, 
838                                           "GstIndexFactory", &indexfactory_info, 0);
839   }
840   return indexfactory_type;
841 }
842
843 static void
844 gst_index_factory_class_init (GstIndexFactoryClass *klass)
845 {
846   GObjectClass *gobject_class;
847   GstObjectClass *gstobject_class;
848   GstPluginFeatureClass *gstpluginfeature_class;
849
850   gobject_class = (GObjectClass*)klass;
851   gstobject_class = (GstObjectClass*)klass;
852   gstpluginfeature_class = (GstPluginFeatureClass*) klass;
853
854   factory_parent_class = g_type_class_ref (GST_TYPE_PLUGIN_FEATURE);
855 }
856
857 static void
858 gst_index_factory_init (GstIndexFactory *factory)
859 {
860 }
861
862 /**
863  * gst_index_factory_new:
864  * @name: name of indexfactory to create
865  * @longdesc: long description of indexfactory to create
866  * @type: the GType of the GstIndex element of this factory
867  *
868  * Create a new indexfactory with the given parameters
869  *
870  * Returns: a new #GstIndexFactory.
871  */
872 GstIndexFactory*
873 gst_index_factory_new (const gchar *name, const gchar *longdesc, GType type)
874 {
875   GstIndexFactory *factory;
876
877   g_return_val_if_fail(name != NULL, NULL);
878   factory = gst_index_factory_find (name);
879   if (!factory) {
880     factory = GST_INDEX_FACTORY (g_object_new (GST_TYPE_INDEX_FACTORY, NULL));
881   }
882
883   GST_PLUGIN_FEATURE_NAME (factory) = g_strdup (name);
884   if (factory->longdesc)
885     g_free (factory->longdesc);
886   factory->longdesc = g_strdup (longdesc);
887   factory->type = type;
888
889   return factory;
890 }
891
892 /**
893  * gst_index_factory_destroy:
894  * @factory: factory to destroy
895  *
896  * Removes the index from the global list.
897  */
898 void
899 gst_index_factory_destroy (GstIndexFactory *factory)
900 {
901   g_return_if_fail (factory != NULL);
902
903   /* we don't free the struct bacause someone might  have a handle to it.. */
904 }
905
906 /**
907  * gst_index_factory_find:
908  * @name: name of indexfactory to find
909  *
910  * Search for an indexfactory of the given name.
911  *
912  * Returns: #GstIndexFactory if found, NULL otherwise
913  */
914 GstIndexFactory*
915 gst_index_factory_find (const gchar *name)
916 {
917   GstPluginFeature *feature;
918
919   g_return_val_if_fail (name != NULL, NULL);
920
921   GST_DEBUG ("gstindex: find \"%s\"", name);
922
923   feature = gst_registry_pool_find_feature (name, GST_TYPE_INDEX_FACTORY);
924   if (feature)
925     return GST_INDEX_FACTORY (feature);
926
927   return NULL;
928 }
929
930 /**
931  * gst_index_factory_create:
932  * @factory: the factory used to create the instance
933  *
934  * Create a new #GstIndex instance from the 
935  * given indexfactory.
936  *
937  * Returns: A new #GstIndex instance.
938  */
939 GstIndex*
940 gst_index_factory_create (GstIndexFactory *factory)
941 {
942   GstIndex *new = NULL;
943
944   g_return_val_if_fail (factory != NULL, NULL);
945
946   if (gst_plugin_feature_ensure_loaded (GST_PLUGIN_FEATURE (factory))) {
947     g_return_val_if_fail (factory->type != 0, NULL);
948
949     new = GST_INDEX (g_object_new(factory->type,NULL));
950   }
951
952   return new;
953 }
954
955 /**
956  * gst_index_factory_make:
957  * @name: the name of the factory used to create the instance
958  *
959  * Create a new #GstIndex instance from the 
960  * indexfactory with the given name.
961  *
962  * Returns: A new #GstIndex instance.
963  */
964 GstIndex*
965 gst_index_factory_make (const gchar *name)
966 {
967   GstIndexFactory *factory;
968
969   g_return_val_if_fail (name != NULL, NULL);
970
971   factory = gst_index_factory_find (name);
972
973   if (factory == NULL)
974     return NULL;
975
976   return gst_index_factory_create (factory);
977 }