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