1 /* GStreamer
2 * Copyright (C) 2003 Erik Walthinsen <omega@cse.ogi.edu>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
20 #include <gst/gst.h>
22 #include <unistd.h>
23 #include <sys/mman.h>
24 #include <sys/stat.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <string.h>
29 #define GST_TYPE_FILE_INDEX \
30 (gst_file_index_get_type ())
31 #define GST_FILE_INDEX(obj) \
32 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_FILE_INDEX, GstFileIndex))
33 #define GST_FILE_INDEX_CLASS(klass) \
34 (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_FILE_INDEX, GstFileIndexClass))
35 #define GST_IS_FILE_INDEX(obj) \
36 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_FILE_INDEX))
37 #define GST_IS_FILE_INDEX_CLASS(obj) \
38 (GST_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_FILE_INDEX))
40 /*
41 * Object model:
42 *
43 * We build an index to each entry for each id.
44 *
45 *
46 * fileindex
47 * -----------------------------...
48 * ! !
49 * id1 id2
50 * !
51 * GArray
52 *
53 * The fileindex creates a FileIndexId object for each writer id, a
54 * Hashtable is kept to map the id to the FileIndexId
55 *
56 * The FileIndexId also keeps all the values in a sorted GArray.
57 *
58 * Finding a value for an id/format requires locating the correct GArray,
59 * then do a binary search to get the required value.
60 *
61 * Unlike gstmemindex: All formats are assumed to sort to the
62 * same order. All formats are assumed to be available from
63 * any entry.
64 */
66 /*
67 * Each array element is (32bits flags, nformats * 64bits)
68 */
69 typedef struct {
70 gint id;
71 gchar *id_desc;
72 gint nformats;
73 GstFormat *format;
74 GArray *array;
75 } GstFileIndexId;
77 typedef struct _GstFileIndex GstFileIndex;
78 typedef struct _GstFileIndexClass GstFileIndexClass;
80 #define ARRAY_ROW_SIZE(_ii) \
81 (sizeof (gint32) + (_ii)->nformats * sizeof (gint64))
82 #define ARRAY_TOTAL_SIZE(_ii) \
83 (_ii->array->len * ARRAY_ROW_SIZE(_ii))
85 // don't forget to convert to/from BE byte-order
86 #define ARRAY_ROW_FLAGS(_row) \
87 (*((gint32*) (_row)))
88 #define ARRAY_ROW_VALUE(_row,_vx) \
89 (*(gint64*) (((gchar*)(_row)) + sizeof (gint32) + (_vx) * sizeof (gint64)))
91 struct _GstFileIndex {
92 GstIndex parent;
94 gchar *location;
95 gboolean is_loaded;
96 GSList *unresolved;
97 gint next_id;
98 GHashTable *id_index;
100 GstIndexEntry *ret_entry; // hack to avoid leaking memory
101 };
103 struct _GstFileIndexClass {
104 GstIndexClass parent_class;
105 };
107 enum {
108 ARG_0,
109 ARG_LOCATION,
110 };
112 static void gst_file_index_class_init (GstFileIndexClass *klass);
113 static void gst_file_index_init (GstFileIndex *index);
114 static void gst_file_index_dispose (GObject *object);
116 static void
117 gst_file_index_set_property (GObject *object,
118 guint prop_id,
119 const GValue *value,
120 GParamSpec *pspec);
121 static void
122 gst_file_index_get_property (GObject *object,
123 guint prop_id,
124 GValue *value,
125 GParamSpec *pspec);
127 static gboolean
128 gst_file_index_get_writer_id (GstIndex *_index, gint *id, gchar *writer_string);
130 static void gst_file_index_commit (GstIndex *index, gint writer_id);
131 static void gst_file_index_add_entry (GstIndex *index, GstIndexEntry *entry);
132 static GstIndexEntry* gst_file_index_get_assoc_entry (GstIndex *index, gint id,
133 GstIndexLookupMethod method,
134 GstAssocFlags flags,
135 GstFormat format, gint64 value,
136 GCompareDataFunc func,
137 gpointer user_data);
139 #define CLASS(file_index) GST_FILE_INDEX_CLASS (G_OBJECT_GET_CLASS (file_index))
141 static GstIndex *parent_class = NULL;
143 GType
144 gst_file_index_get_type(void) {
145 static GType file_index_type = 0;
147 if (!file_index_type) {
148 static const GTypeInfo file_index_info = {
149 sizeof(GstFileIndexClass),
150 NULL,
151 NULL,
152 (GClassInitFunc)gst_file_index_class_init,
153 NULL,
154 NULL,
155 sizeof(GstFileIndex),
156 1,
157 (GInstanceInitFunc)gst_file_index_init,
158 NULL
159 };
160 file_index_type = g_type_register_static(GST_TYPE_INDEX, "GstFileIndex", &file_index_info, 0);
161 }
162 return file_index_type;
163 }
165 static void
166 gst_file_index_class_init (GstFileIndexClass *klass)
167 {
168 GObjectClass *gobject_class;
169 GstIndexClass *gstindex_class;
171 gobject_class = (GObjectClass*)klass;
172 gstindex_class = (GstIndexClass*)klass;
174 parent_class = g_type_class_ref(GST_TYPE_INDEX);
176 gobject_class->dispose = gst_file_index_dispose;
177 gobject_class->set_property = gst_file_index_set_property;
178 gobject_class->get_property = gst_file_index_get_property;
180 gstindex_class->add_entry = gst_file_index_add_entry;
181 gstindex_class->get_assoc_entry = gst_file_index_get_assoc_entry;
182 gstindex_class->commit = gst_file_index_commit;
183 gstindex_class->get_writer_id = gst_file_index_get_writer_id ;
185 g_object_class_install_property (gobject_class, ARG_LOCATION,
186 g_param_spec_string ("location", "File Location",
187 "Location of the index file",
188 NULL, G_PARAM_READWRITE));
189 }
191 static void
192 gst_file_index_init (GstFileIndex *index)
193 {
194 GST_DEBUG ( "created new file index");
196 index->id_index = g_hash_table_new (g_int_hash, g_int_equal);
197 }
199 static void
200 _file_index_id_free (GstFileIndexId *index_id, gboolean is_mmapped)
201 {
202 if (index_id->id_desc)
203 g_free (index_id->id_desc);
204 if (index_id->format)
205 g_free (index_id->format);
206 if (index_id->array) {
207 if (is_mmapped)
208 munmap (index_id->array->data, ARRAY_TOTAL_SIZE (index_id));
209 g_array_free (index_id->array, !is_mmapped);
210 }
211 g_free (index_id);
212 }
214 static gboolean
215 _id_index_free_helper (gpointer _key, GstFileIndexId *index_id,
216 GstFileIndex *index)
217 {
218 _file_index_id_free (index_id, index->is_loaded);
219 return TRUE;
220 }
222 static void
223 gst_file_index_dispose (GObject *object)
224 {
225 GstFileIndex *index = GST_FILE_INDEX (object);
227 if (index->location) {
228 g_free (index->location);
229 index->location = NULL;
230 }
232 {
233 GSList *elem;
234 for (elem = index->unresolved; elem; elem = g_slist_next (elem))
235 _file_index_id_free (elem->data, index->is_loaded);
236 g_slist_free (index->unresolved);
237 index->unresolved = NULL;
238 }
240 g_hash_table_foreach_steal (index->id_index,
241 (GHRFunc) _id_index_free_helper, index);
242 g_hash_table_destroy (index->id_index);
243 index->id_index = NULL;
245 gst_index_entry_free (index->ret_entry); // hack
247 G_OBJECT_CLASS (parent_class)->dispose (object);
248 }
250 static gboolean
251 gst_file_index_get_writer_id (GstIndex *_index,
252 gint *id, gchar *writer_string)
253 {
254 GstFileIndex *index = GST_FILE_INDEX (_index);
255 GSList *pending = index->unresolved;
256 gboolean match = FALSE;
257 GSList *elem;
259 if (!index->is_loaded)
260 return FALSE;
262 g_return_val_if_fail (id, FALSE);
263 g_return_val_if_fail (writer_string, FALSE);
265 index->unresolved = NULL;
267 for (elem = pending; elem; elem = g_slist_next (elem)) {
268 GstFileIndexId *ii = elem->data;
269 if (strcmp (ii->id_desc, writer_string) != 0) {
270 index->unresolved = g_slist_prepend (index->unresolved, ii);
271 continue;
272 }
274 if (match) {
275 g_warning ("Duplicate matches for writer '%s'", writer_string);
276 continue;
277 }
279 ii->id = *id = ++index->next_id;
280 g_hash_table_insert (index->id_index, &ii->id, ii);
281 match = TRUE;
283 //g_warning ("resolve %d %s", *id, writer_string);
284 }
286 g_slist_free (pending);
288 if (!match)
289 g_warning ("Can't resolve writer '%s'", writer_string);
291 return match;
292 }
294 static void
295 _fc_alloc_array (GstFileIndexId *id_index)
296 {
297 g_assert (!id_index->array);
298 id_index->array =
299 g_array_sized_new (FALSE, FALSE, ARRAY_ROW_SIZE (id_index), 0);
300 }
302 static void
303 gst_file_index_load (GstFileIndex *index)
304 {
305 xmlDocPtr doc;
306 xmlNodePtr root, part;
307 xmlChar *val;
309 g_assert (index->location);
310 g_return_if_fail (!index->is_loaded);
312 {
313 gchar *path = g_strdup_printf ("%s/gstindex.xml", index->location);
314 GError *err = NULL;
315 gchar *buf;
316 gsize len;
317 g_file_get_contents (path, &buf, &len, &err);
318 g_free (path);
319 if (err) g_error ("%s", err->message);
321 doc = xmlParseMemory (buf, len);
322 g_free (buf);
323 }
325 //xmlDocFormatDump (stderr, doc, TRUE);
327 root = doc->xmlRootNode;
328 if (strcmp (root->name, "gstfileindex") != 0)
329 g_error ("root node isn't a gstfileindex");
331 val = xmlGetProp (root, "version");
332 if (!val || atoi (val) != 1)
333 g_error ("version != 1");
334 free (val);
336 for (part = root->children; part; part = part->next) {
337 if (strcmp (part->name, "writers") == 0) {
338 xmlNodePtr writer;
339 for (writer = part->children; writer; writer = writer->next) {
340 xmlChar *datafile = xmlGetProp (writer, "datafile");
341 gchar *path = g_strdup_printf ("%s/%s", index->location, datafile);
342 int fd;
343 GstFileIndexId *id_index;
344 xmlNodePtr wpart;
345 xmlChar *entries_str;
346 gpointer array_data;
348 free (datafile);
350 fd = open (path, O_RDONLY);
351 g_free (path);
352 if (fd < 0) {
353 g_warning ("Can't open '%s': %s", path, strerror (errno));
354 continue;
355 }
357 id_index = g_new0 (GstFileIndexId, 1);
358 id_index->id_desc = xmlGetProp (writer, "id");
360 for (wpart = writer->children; wpart; wpart = wpart->next) {
361 if (strcmp (wpart->name, "formats") == 0) {
362 xmlChar *count_str = xmlGetProp (wpart, "count");
363 gint fx=0;
364 xmlNodePtr format;
366 id_index->nformats = atoi (count_str);
367 free (count_str);
369 id_index->format = g_new (GstFormat, id_index->nformats);
371 for (format = wpart->children;
372 format; format = format->next) {
373 xmlChar *nick = xmlGetProp (format, "nick");
374 GstFormat fmt = gst_format_get_by_nick (nick);
375 if (fmt == GST_FORMAT_UNDEFINED)
376 g_error ("format '%s' undefined", nick);
377 g_assert (fx < id_index->nformats);
378 id_index->format[fx++] = fmt;
379 free (nick);
380 }
381 } else
382 g_warning ("unknown wpart '%s'", wpart->name);
383 }
385 g_assert (id_index->nformats > 0);
386 _fc_alloc_array (id_index);
387 g_assert (id_index->array->data == NULL); // little bit risky
389 entries_str = xmlGetProp (writer, "entries");
390 id_index->array->len = atoi (entries_str);
391 free (entries_str);
393 array_data =
394 mmap (NULL, ARRAY_TOTAL_SIZE (id_index), PROT_READ, MAP_SHARED, fd, 0);
395 close (fd);
396 if (array_data == MAP_FAILED) {
397 g_error ("mmap %s failed: %s", path, strerror (errno));
398 continue;
399 }
401 id_index->array->data = array_data;
403 index->unresolved = g_slist_prepend (index->unresolved, id_index);
404 }
405 } else
406 g_warning ("unknown part '%s'", part->name);
407 }
409 xmlFreeDoc (doc);
411 GST_FLAG_UNSET (index, GST_INDEX_WRITABLE);
412 index->is_loaded = TRUE;
413 }
415 static void
416 gst_file_index_set_property (GObject *object,
417 guint prop_id,
418 const GValue *value,
419 GParamSpec *pspec)
420 {
421 GstFileIndex *index = GST_FILE_INDEX (object);
423 switch (prop_id) {
424 case ARG_LOCATION:
425 if (index->location)
426 g_free (index->location);
427 index->location = g_value_dup_string (value);
429 if (index->location && !g_hash_table_size (index->id_index))
430 gst_file_index_load (index);
431 break;
432 }
433 }
435 static void
436 gst_file_index_get_property (GObject *object,
437 guint prop_id,
438 GValue *value,
439 GParamSpec *pspec)
440 {
441 GstFileIndex *index = GST_FILE_INDEX (object);
443 switch (prop_id) {
444 case ARG_LOCATION:
445 g_value_set_string (value, index->location);
446 break;
447 }
448 }
450 static void
451 _file_index_id_save_xml (gpointer _key, GstFileIndexId *ii, xmlNodePtr writers)
452 {
453 const gint bufsize = 16;
454 gchar buf[bufsize];
455 xmlNodePtr writer;
456 xmlNodePtr formats;
457 gint xx;
459 writer = xmlNewChild (writers, NULL, "writer", NULL);
460 xmlSetProp (writer, "id", ii->id_desc);
461 g_snprintf (buf, bufsize, "%d", ii->array->len);
462 xmlSetProp (writer, "entries", buf);
463 g_snprintf (buf, bufsize, "%d", ii->id); // any unique number is OK
464 xmlSetProp (writer, "datafile", buf);
466 formats = xmlNewChild (writer, NULL, "formats", NULL);
467 g_snprintf (buf, bufsize, "%d", ii->nformats);
468 xmlSetProp (formats, "count", buf);
470 for (xx=0; xx < ii->nformats; xx++) {
471 xmlNodePtr format = xmlNewChild (formats, NULL, "format", NULL);
472 const GstFormatDefinition* def =
473 gst_format_get_details (ii->format[xx]);
474 xmlSetProp (format, "nick", def->nick);
475 }
476 }
478 //
479 // We must save the binary data in separate files because
480 // mmap wants getpagesize() alignment. If we append all
481 // the data to one file then we don't know the appropriate
482 // padding since the page size isn't fixed.
483 //
484 static void
485 _file_index_id_save_entries (gpointer *_key,
486 GstFileIndexId *ii,
487 gchar *prefix)
488 {
489 GError *err = NULL;
490 gchar *path = g_strdup_printf ("%s/%d", prefix, ii->id);
491 GIOChannel *chan =
492 g_io_channel_new_file (path, "w", &err);
493 g_free (path);
494 if (err) g_error ("%s", err->message);
496 g_io_channel_set_encoding (chan, NULL, &err);
497 if (err) g_error ("%s", err->message);
499 g_io_channel_write_chars (chan,
500 ii->array->data,
501 ARRAY_TOTAL_SIZE (ii),
502 NULL,
503 &err);
504 if (err) g_error ("%s", err->message);
506 g_io_channel_shutdown (chan, TRUE, &err);
507 if (err) g_error ("%s", err->message);
509 g_io_channel_unref (chan);
510 }
512 // We have to save the whole set of indexes into a single file
513 // so it doesn't make sense to commit only a single writer.
514 //
515 // i suggest:
516 //
517 // gst_index_commit (index, -1);
519 static void
520 gst_file_index_commit (GstIndex *_index, gint _writer_id)
521 {
522 GstFileIndex *index = GST_FILE_INDEX (_index);
523 xmlDocPtr doc;
524 xmlNodePtr writers;
525 GError *err = NULL;
526 gchar *path;
527 GIOChannel *tocfile;
529 g_return_if_fail (index->location);
530 g_return_if_fail (!index->is_loaded);
532 GST_FLAG_UNSET (index, GST_INDEX_WRITABLE);
534 doc = xmlNewDoc ("1.0");
535 doc->xmlRootNode = xmlNewDocNode (doc, NULL, "gstfileindex", NULL);
536 xmlSetProp (doc->xmlRootNode, "version", "1");
538 writers = xmlNewChild (doc->xmlRootNode, NULL, "writers", NULL);
539 g_hash_table_foreach (index->id_index,
540 (GHFunc) _file_index_id_save_xml, writers);
542 if (mkdir (index->location, 0777) &&
543 errno != EEXIST)
544 g_error ("mkdir %s: %s", index->location, strerror (errno));
546 path = g_strdup_printf ("%s/gstindex.xml", index->location);
547 tocfile =
548 g_io_channel_new_file (path, "w", &err);
549 g_free (path);
550 if (err) g_error ("%s", err->message);
552 g_io_channel_set_encoding (tocfile, NULL, &err);
553 if (err) g_error ("%s", err->message);
555 {
556 xmlChar *xmlmem;
557 int xmlsize;
558 xmlDocDumpMemory (doc, &xmlmem, &xmlsize);
559 g_io_channel_write_chars (tocfile, xmlmem, xmlsize, NULL, &err);
560 if (err) g_error ("%s", err->message);
561 xmlFreeDoc (doc);
562 free (xmlmem);
563 }
565 g_io_channel_shutdown (tocfile, TRUE, &err);
566 if (err) g_error ("%s", err->message);
568 g_io_channel_unref (tocfile);
570 g_hash_table_foreach (index->id_index,
571 (GHFunc) _file_index_id_save_entries,
572 index->location);
573 }
575 static void
576 gst_file_index_add_id (GstIndex *index, GstIndexEntry *entry)
577 {
578 GstFileIndex *fileindex = GST_FILE_INDEX (index);
579 GstFileIndexId *id_index;
581 id_index = g_hash_table_lookup (fileindex->id_index, &entry->id);
583 if (!id_index) {
584 id_index = g_new0 (GstFileIndexId, 1);
586 id_index->id = entry->id;
587 id_index->id_desc = g_strdup (entry->data.id.description);
589 // It would be useful to know the GType of the writer so
590 // we can try to cope with changes in the id_desc path.
592 g_hash_table_insert (fileindex->id_index, &id_index->id, id_index);
593 }
594 }
596 // This algorithm differs from libc bsearch in the handling
597 // of non-exact matches.
599 static gboolean
600 _fc_bsearch (GArray * ary,
601 gint stride,
602 gint * ret,
603 GCompareDataFunc compare,
604 gconstpointer sample,
605 gpointer user_data)
606 {
607 gint first, last;
608 gint mid;
609 gint midsize;
610 gint cmp;
611 gint tx;
613 g_return_val_if_fail (compare, FALSE);
615 if (!ary->len)
616 {
617 if (ret) *ret = 0;
618 return FALSE;
619 }
621 first = 0;
622 last = ary->len - 1;
624 midsize = last - first;
626 while (midsize > 1) {
627 mid = first + midsize / 2;
629 cmp = (*compare) (sample, ary->data + mid*stride, user_data);
631 if (cmp == 0)
632 {
633 // if there are multiple matches then scan for the first match
634 while (mid > 0 &&
635 (*compare) (sample,
636 ary->data + (mid - 1) * stride,
637 user_data) == 0)
638 --mid;
640 if (ret) *ret = mid;
641 return TRUE;
642 }
644 if (cmp < 0)
645 last = mid-1;
646 else
647 first = mid+1;
649 midsize = last - first;
650 }
652 for (tx = first; tx <= last; tx++)
653 {
654 cmp = (*compare) (sample, ary->data + tx*stride, user_data);
656 if (cmp < 0)
657 {
658 if (ret) *ret = tx;
659 return FALSE;
660 }
661 if (cmp == 0)
662 {
663 if (ret) *ret = tx;
664 return TRUE;
665 }
666 }
668 if (ret) *ret = last+1;
669 return FALSE;
670 }
672 static gint
673 file_index_compare (gconstpointer sample,
674 gconstpointer row,
675 gpointer user_data)
676 {
677 //GstFileIndexId *id_index = user_data;
678 const GstIndexAssociation *ca = sample;
679 gint64 val1 = ca->value;
680 gint64 val2_be = ARRAY_ROW_VALUE (row, ca->format);
681 gint64 val2 = GINT64_FROM_BE (val2_be);
682 gint64 diff = val2 - val1;
683 return (diff == 0 ? 0 : (diff < 0 ? 1 : -1));
684 }
686 static void
687 gst_file_index_add_association (GstIndex *index, GstIndexEntry *entry)
688 {
689 GstFileIndex *fileindex = GST_FILE_INDEX (index);
690 GstFileIndexId *id_index;
691 gint mx;
692 GstIndexAssociation sample;
693 gboolean exact;
695 id_index = g_hash_table_lookup (fileindex->id_index, &entry->id);
696 if (!id_index)
697 return;
699 if (!id_index->nformats) {
700 gint fx;
701 id_index->nformats = GST_INDEX_NASSOCS (entry);
702 //g_warning ("%d: formats = %d", entry->id, id_index->nformats);
703 id_index->format = g_new (GstFormat, id_index->nformats);
704 for (fx=0; fx < id_index->nformats; fx++)
705 id_index->format[fx] = GST_INDEX_ASSOC_FORMAT (entry, fx);
706 _fc_alloc_array (id_index);
707 } else {
708 /* only sanity checking */
709 if (id_index->nformats != GST_INDEX_NASSOCS (entry))
710 g_warning ("fileindex arity change %d -> %d",
711 id_index->nformats, GST_INDEX_NASSOCS (entry));
712 else {
713 gint fx;
714 for (fx=0; fx < id_index->nformats; fx++)
715 if (id_index->format[fx] != GST_INDEX_ASSOC_FORMAT (entry, fx))
716 g_warning ("fileindex format[%d] changed %d -> %d",
717 fx,
718 id_index->format[fx],
719 GST_INDEX_ASSOC_FORMAT (entry, fx));
720 }
721 }
723 /* this is a hack, we should use a private structure instead */
724 sample.format = 0;
725 sample.value = GST_INDEX_ASSOC_VALUE (entry, 0);
727 exact =
728 _fc_bsearch (id_index->array, ARRAY_ROW_SIZE (id_index),
729 &mx, file_index_compare,
730 &sample, id_index);
732 if (exact) {
733 // maybe overwrite instead?
734 // g_warning ("Ignoring duplicate index association at %lld",
735 // GST_INDEX_ASSOC_VALUE (entry, 0));
736 return;
737 }
739 {
740 gchar row_data[ARRAY_ROW_SIZE (id_index)];
741 gint fx;
743 gint32 flags_host = GST_INDEX_ASSOC_FLAGS (entry);
744 ARRAY_ROW_FLAGS (row_data) = GINT32_TO_BE (flags_host);
746 for (fx = 0; fx < id_index->nformats; fx++) {
747 gint64 val_host = GST_INDEX_ASSOC_VALUE (entry, fx);
748 ARRAY_ROW_VALUE (row_data, fx) = GINT64_TO_BE (val_host);
749 }
751 g_array_insert_vals (id_index->array, mx, row_data, 1);
752 }
753 }
755 /*
756 static void
757 show_entry (GstIndexEntry *entry)
758 {
759 switch (entry->type) {
760 case GST_INDEX_ENTRY_ID:
761 g_print ("id %d describes writer %s\n", entry->id,
762 GST_INDEX_ID_DESCRIPTION (entry));
763 break;
764 case GST_INDEX_ENTRY_FORMAT:
765 g_print ("%d: registered format %d for %s\n", entry->id,
766 GST_INDEX_FORMAT_FORMAT (entry),
767 GST_INDEX_FORMAT_KEY (entry));
768 break;
769 case GST_INDEX_ENTRY_ASSOCIATION:
770 {
771 gint i;
773 g_print ("%d: %08x ", entry->id, GST_INDEX_ASSOC_FLAGS (entry));
774 for (i = 0; i < GST_INDEX_NASSOCS (entry); i++) {
775 g_print ("%d %lld ", GST_INDEX_ASSOC_FORMAT (entry, i),
776 GST_INDEX_ASSOC_VALUE (entry, i));
777 }
778 g_print ("\n");
779 break;
780 }
781 default:
782 break;
783 }
784 }
785 */
787 static void
788 gst_file_index_add_entry (GstIndex *index, GstIndexEntry *entry)
789 {
790 GST_LOG_OBJECT (index, "adding this entry");
792 switch (entry->type){
793 case GST_INDEX_ENTRY_ID:
794 gst_file_index_add_id (index, entry);
795 break;
796 case GST_INDEX_ENTRY_ASSOCIATION:
797 gst_file_index_add_association (index, entry);
798 break;
799 case GST_INDEX_ENTRY_OBJECT:
800 g_error ("gst_file_index_add_object not implemented");
801 break;
802 case GST_INDEX_ENTRY_FORMAT:
803 g_warning ("gst_file_index_add_format not implemented");
804 break;
805 default:
806 break;
807 }
808 }
810 static GstIndexEntry*
811 gst_file_index_get_assoc_entry (GstIndex *index,
812 gint id,
813 GstIndexLookupMethod method,
814 GstAssocFlags flags,
815 GstFormat format,
816 gint64 value,
817 GCompareDataFunc _ignore_func,
818 gpointer _ignore_user_data)
819 {
820 GstFileIndex *fileindex = GST_FILE_INDEX (index);
821 GstFileIndexId *id_index;
822 gint formatx = -1;
823 gint fx;
824 GstIndexAssociation sample;
825 gint mx;
826 gboolean exact;
827 gpointer row_data;
828 GstIndexEntry *entry;
829 gint xx;
831 id_index = g_hash_table_lookup (fileindex->id_index, &id);
832 if (!id_index)
833 return NULL;
835 for (fx=0; fx < id_index->nformats; fx++)
836 if (id_index->format[fx] == format)
837 { formatx = fx; break; }
839 if (formatx == -1) {
840 g_warning ("index does not contain format %d", format);
841 return NULL;
842 }
844 /* this is a hack, we should use a private structure instead */
845 sample.format = formatx;
846 sample.value = value;
848 exact = _fc_bsearch (id_index->array, ARRAY_ROW_SIZE (id_index),
849 &mx, file_index_compare, &sample, id_index);
851 if (!exact) {
852 if (method == GST_INDEX_LOOKUP_EXACT)
853 return NULL;
854 else if (method == GST_INDEX_LOOKUP_BEFORE) {
855 if (mx == 0)
856 return NULL;
857 mx -= 1;
858 } else if (method == GST_INDEX_LOOKUP_AFTER) {
859 if (mx == id_index->array->len)
860 return NULL;
861 }
862 }
864 row_data = id_index->array->data + mx * ARRAY_ROW_SIZE (id_index);
866 // if exact then ignore flags (?)
867 if (method != GST_INDEX_LOOKUP_EXACT)
868 while ((GINT32_FROM_BE (ARRAY_ROW_FLAGS (row_data)) & flags) != flags) {
869 if (method == GST_INDEX_LOOKUP_BEFORE)
870 mx -= 1;
871 else if (method == GST_INDEX_LOOKUP_AFTER)
872 mx += 1;
873 if (mx < 0 || mx >= id_index->array->len)
874 return NULL;
875 row_data = id_index->array->data + mx * ARRAY_ROW_SIZE (id_index);
876 }
878 // entry memory management needs improvement
879 if (!fileindex->ret_entry)
880 fileindex->ret_entry = g_new0 (GstIndexEntry, 1);
881 entry = fileindex->ret_entry;
882 if (entry->data.assoc.assocs) {
883 g_free (entry->data.assoc.assocs);
884 entry->data.assoc.assocs = NULL;
885 }
887 entry->type = GST_INDEX_ENTRY_ASSOCIATION;
889 GST_INDEX_NASSOCS (entry) = id_index->nformats;
890 entry->data.assoc.assocs =
891 g_new (GstIndexAssociation, id_index->nformats);
893 {
894 gint32 flags_be = ARRAY_ROW_FLAGS (row_data);
895 GST_INDEX_ASSOC_FLAGS (entry) = GINT32_FROM_BE (flags_be);
897 for (xx=0; xx < id_index->nformats; xx++)
898 {
899 gint64 val_be = ARRAY_ROW_VALUE (row_data, xx);
900 GST_INDEX_ASSOC_FORMAT (entry, xx) = id_index->format[xx];
901 GST_INDEX_ASSOC_VALUE (entry, xx) = GINT64_FROM_BE (val_be);
902 }
903 }
905 return entry;
906 }
908 gboolean
909 gst_file_index_plugin_init (GModule *module, GstPlugin *plugin)
910 {
911 GstIndexFactory *factory;
913 gst_plugin_set_longname (plugin, "A file index");
915 factory = gst_index_factory_new ("fileindex",
916 "A index that stores entries in file",
917 gst_file_index_get_type());
919 if (factory != NULL) {
920 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
921 }
922 else {
923 g_warning ("could not register fileindex");
924 }
925 return TRUE;
926 }