2003-01-04 16:06:05 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2003 Erik Walthinsen <omega@cse.ogi.edu>
|
2003-08-15 13:39:14 +00:00
|
|
|
* 2003 Joshua N Pritikin <jpritikin@pobox.com>
|
2003-01-04 16:06:05 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2003-06-29 14:05:49 +00:00
|
|
|
#include <gst/gst.h>
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2003-06-29 14:05:49 +00:00
|
|
|
#include <string.h>
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
#define GST_TYPE_FILE_INDEX \
|
|
|
|
(gst_file_index_get_type ())
|
|
|
|
#define GST_FILE_INDEX(obj) \
|
|
|
|
(G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_FILE_INDEX, GstFileIndex))
|
|
|
|
#define GST_FILE_INDEX_CLASS(klass) \
|
|
|
|
(G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_FILE_INDEX, GstFileIndexClass))
|
|
|
|
#define GST_IS_FILE_INDEX(obj) \
|
|
|
|
(G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_FILE_INDEX))
|
|
|
|
#define GST_IS_FILE_INDEX_CLASS(obj) \
|
|
|
|
(GST_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_FILE_INDEX))
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-01-04 16:06:05 +00:00
|
|
|
/*
|
|
|
|
* Object model:
|
|
|
|
*
|
|
|
|
* We build an index to each entry for each id.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* fileindex
|
|
|
|
* -----------------------------...
|
|
|
|
* ! !
|
|
|
|
* id1 id2
|
|
|
|
* !
|
|
|
|
* GArray
|
|
|
|
*
|
|
|
|
* The fileindex creates a FileIndexId object for each writer id, a
|
|
|
|
* Hashtable is kept to map the id to the FileIndexId
|
|
|
|
*
|
|
|
|
* The FileIndexId also keeps all the values in a sorted GArray.
|
|
|
|
*
|
|
|
|
* Finding a value for an id/format requires locating the correct GArray,
|
|
|
|
* then do a binary search to get the required value.
|
|
|
|
*
|
|
|
|
* Unlike gstmemindex: All formats are assumed to sort to the
|
|
|
|
* same order. All formats are assumed to be available from
|
|
|
|
* any entry.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Each array element is (32bits flags, nformats * 64bits)
|
|
|
|
*/
|
2004-03-13 15:27:01 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
gint id;
|
|
|
|
gchar *id_desc;
|
|
|
|
gint nformats;
|
|
|
|
GstFormat *format;
|
|
|
|
GArray *array;
|
2004-03-15 19:27:17 +00:00
|
|
|
}
|
|
|
|
GstFileIndexId;
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
typedef struct _GstFileIndex GstFileIndex;
|
|
|
|
typedef struct _GstFileIndexClass GstFileIndexClass;
|
|
|
|
|
|
|
|
#define ARRAY_ROW_SIZE(_ii) \
|
|
|
|
(sizeof (gint32) + (_ii)->nformats * sizeof (gint64))
|
|
|
|
#define ARRAY_TOTAL_SIZE(_ii) \
|
|
|
|
(_ii->array->len * ARRAY_ROW_SIZE(_ii))
|
|
|
|
|
2003-07-25 03:13:24 +00:00
|
|
|
/* don't forget to convert to/from BE byte-order */
|
2003-01-04 16:06:05 +00:00
|
|
|
#define ARRAY_ROW_FLAGS(_row) \
|
|
|
|
(*((gint32*) (_row)))
|
|
|
|
#define ARRAY_ROW_VALUE(_row,_vx) \
|
|
|
|
(*(gint64*) (((gchar*)(_row)) + sizeof (gint32) + (_vx) * sizeof (gint64)))
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (DC);
|
2004-06-06 23:39:39 +00:00
|
|
|
#define GST_CAT_DEFAULT DC
|
2003-07-25 03:13:24 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
struct _GstFileIndex
|
|
|
|
{
|
|
|
|
GstIndex parent;
|
2003-01-04 16:06:05 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
gchar *location;
|
|
|
|
gboolean is_loaded;
|
|
|
|
GSList *unresolved;
|
|
|
|
gint next_id;
|
|
|
|
GHashTable *id_index;
|
2003-01-04 16:06:05 +00:00
|
|
|
|
2004-03-15 19:27:17 +00:00
|
|
|
GstIndexEntry *ret_entry; /* hack to avoid leaking memory */
|
2003-01-04 16:06:05 +00:00
|
|
|
};
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
struct _GstFileIndexClass
|
|
|
|
{
|
2003-01-04 16:06:05 +00:00
|
|
|
GstIndexClass parent_class;
|
|
|
|
};
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
enum
|
|
|
|
{
|
2003-01-04 16:06:05 +00:00
|
|
|
ARG_0,
|
|
|
|
ARG_LOCATION,
|
|
|
|
};
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
static void gst_file_index_class_init (GstFileIndexClass * klass);
|
|
|
|
static void gst_file_index_init (GstFileIndex * index);
|
|
|
|
static void gst_file_index_dispose (GObject * object);
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_file_index_set_property (GObject * object,
|
|
|
|
guint prop_id, const GValue * value, GParamSpec * pspec);
|
2003-01-04 16:06:05 +00:00
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_file_index_get_property (GObject * object,
|
|
|
|
guint prop_id, GValue * value, GParamSpec * pspec);
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
static gboolean
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_file_index_get_writer_id (GstIndex * _index, gint * id,
|
|
|
|
gchar * writer_string);
|
2003-01-04 16:06:05 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
static void gst_file_index_commit (GstIndex * index, gint writer_id);
|
|
|
|
static void gst_file_index_add_entry (GstIndex * index, GstIndexEntry * entry);
|
|
|
|
static GstIndexEntry *gst_file_index_get_assoc_entry (GstIndex * index, gint id,
|
|
|
|
GstIndexLookupMethod method,
|
|
|
|
GstAssocFlags flags,
|
|
|
|
GstFormat format, gint64 value, GCompareDataFunc func, gpointer user_data);
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
#define CLASS(file_index) GST_FILE_INDEX_CLASS (G_OBJECT_GET_CLASS (file_index))
|
|
|
|
|
|
|
|
static GstIndex *parent_class = NULL;
|
|
|
|
|
|
|
|
GType
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_file_index_get_type (void)
|
|
|
|
{
|
2003-01-04 16:06:05 +00:00
|
|
|
static GType file_index_type = 0;
|
|
|
|
|
|
|
|
if (!file_index_type) {
|
|
|
|
static const GTypeInfo file_index_info = {
|
2004-03-13 15:27:01 +00:00
|
|
|
sizeof (GstFileIndexClass),
|
2003-01-04 16:06:05 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2004-03-13 15:27:01 +00:00
|
|
|
(GClassInitFunc) gst_file_index_class_init,
|
2003-01-04 16:06:05 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2004-03-13 15:27:01 +00:00
|
|
|
sizeof (GstFileIndex),
|
2003-01-04 16:06:05 +00:00
|
|
|
1,
|
2004-03-13 15:27:01 +00:00
|
|
|
(GInstanceInitFunc) gst_file_index_init,
|
2003-01-04 16:06:05 +00:00
|
|
|
NULL
|
|
|
|
};
|
2004-03-15 19:27:17 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
file_index_type =
|
2004-03-15 19:27:17 +00:00
|
|
|
g_type_register_static (GST_TYPE_INDEX, "GstFileIndex",
|
|
|
|
&file_index_info, 0);
|
2003-01-04 16:06:05 +00:00
|
|
|
}
|
|
|
|
return file_index_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_file_index_class_init (GstFileIndexClass * klass)
|
2003-01-04 16:06:05 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstIndexClass *gstindex_class;
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
gstindex_class = (GstIndexClass *) klass;
|
2003-01-04 16:06:05 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
parent_class = g_type_class_ref (GST_TYPE_INDEX);
|
2003-01-04 16:06:05 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
gobject_class->dispose = gst_file_index_dispose;
|
|
|
|
gobject_class->set_property = gst_file_index_set_property;
|
|
|
|
gobject_class->get_property = gst_file_index_get_property;
|
2003-01-04 16:06:05 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
gstindex_class->add_entry = gst_file_index_add_entry;
|
2003-01-04 16:06:05 +00:00
|
|
|
gstindex_class->get_assoc_entry = gst_file_index_get_assoc_entry;
|
2004-03-13 15:27:01 +00:00
|
|
|
gstindex_class->commit = gst_file_index_commit;
|
|
|
|
gstindex_class->get_writer_id = gst_file_index_get_writer_id;
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, ARG_LOCATION,
|
2004-03-13 15:27:01 +00:00
|
|
|
g_param_spec_string ("location", "File Location",
|
2004-03-15 19:27:17 +00:00
|
|
|
"Location of the index file", NULL, G_PARAM_READWRITE));
|
2003-01-04 16:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_file_index_init (GstFileIndex * index)
|
2003-01-04 16:06:05 +00:00
|
|
|
{
|
2004-03-13 15:27:01 +00:00
|
|
|
GST_DEBUG ("created new file index");
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
index->id_index = g_hash_table_new (g_int_hash, g_int_equal);
|
|
|
|
}
|
|
|
|
|
2003-01-12 13:22:57 +00:00
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
_file_index_id_free (GstFileIndexId * index_id, gboolean is_mmapped)
|
2003-01-12 13:22:57 +00:00
|
|
|
{
|
|
|
|
if (index_id->id_desc)
|
|
|
|
g_free (index_id->id_desc);
|
|
|
|
if (index_id->format)
|
|
|
|
g_free (index_id->format);
|
|
|
|
if (index_id->array) {
|
|
|
|
if (is_mmapped)
|
2004-03-13 15:27:01 +00:00
|
|
|
munmap (index_id->array->data, ARRAY_TOTAL_SIZE (index_id));
|
2003-01-12 13:22:57 +00:00
|
|
|
g_array_free (index_id->array, !is_mmapped);
|
|
|
|
}
|
|
|
|
g_free (index_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2004-03-13 15:27:01 +00:00
|
|
|
_id_index_free_helper (gpointer _key, GstFileIndexId * index_id,
|
|
|
|
GstFileIndex * index)
|
2003-01-12 13:22:57 +00:00
|
|
|
{
|
|
|
|
_file_index_id_free (index_id, index->is_loaded);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2003-01-04 16:06:05 +00:00
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_file_index_dispose (GObject * object)
|
2003-01-04 16:06:05 +00:00
|
|
|
{
|
|
|
|
GstFileIndex *index = GST_FILE_INDEX (object);
|
|
|
|
|
2003-01-12 13:22:57 +00:00
|
|
|
if (index->location) {
|
2003-01-04 16:06:05 +00:00
|
|
|
g_free (index->location);
|
2003-01-12 13:22:57 +00:00
|
|
|
index->location = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
GSList *elem;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-01-12 13:22:57 +00:00
|
|
|
for (elem = index->unresolved; elem; elem = g_slist_next (elem))
|
|
|
|
_file_index_id_free (elem->data, index->is_loaded);
|
|
|
|
g_slist_free (index->unresolved);
|
|
|
|
index->unresolved = NULL;
|
|
|
|
}
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-01-12 13:22:57 +00:00
|
|
|
g_hash_table_foreach_steal (index->id_index,
|
2004-03-13 15:27:01 +00:00
|
|
|
(GHRFunc) _id_index_free_helper, index);
|
2003-01-12 13:22:57 +00:00
|
|
|
g_hash_table_destroy (index->id_index);
|
|
|
|
index->id_index = NULL;
|
2003-01-04 16:06:05 +00:00
|
|
|
|
2004-03-15 19:27:17 +00:00
|
|
|
gst_index_entry_free (index->ret_entry); /* hack */
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
struct fi_find_writer_context
|
|
|
|
{
|
2003-08-15 13:39:14 +00:00
|
|
|
const gchar *writer_string;
|
|
|
|
GstFileIndexId *ii;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
_fi_find_writer (gpointer key, gpointer val, gpointer data)
|
|
|
|
{
|
|
|
|
struct fi_find_writer_context *cx = data;
|
|
|
|
GstFileIndexId *ii = val;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-08-15 13:39:14 +00:00
|
|
|
if (strcmp (ii->id_desc, cx->writer_string) == 0)
|
|
|
|
cx->ii = ii;
|
|
|
|
}
|
|
|
|
|
2003-01-04 16:06:05 +00:00
|
|
|
static gboolean
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_file_index_get_writer_id (GstIndex * _index,
|
|
|
|
gint * id, gchar * writer_string)
|
2003-01-04 16:06:05 +00:00
|
|
|
{
|
|
|
|
GstFileIndex *index = GST_FILE_INDEX (_index);
|
|
|
|
GSList *pending = index->unresolved;
|
|
|
|
gboolean match = FALSE;
|
|
|
|
GSList *elem;
|
|
|
|
|
|
|
|
if (!index->is_loaded)
|
2003-01-05 16:51:46 +00:00
|
|
|
return FALSE;
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (id, FALSE);
|
|
|
|
g_return_val_if_fail (writer_string, FALSE);
|
|
|
|
|
|
|
|
index->unresolved = NULL;
|
|
|
|
|
|
|
|
for (elem = pending; elem; elem = g_slist_next (elem)) {
|
|
|
|
GstFileIndexId *ii = elem->data;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-01-04 21:57:28 +00:00
|
|
|
if (strcmp (ii->id_desc, writer_string) != 0) {
|
2003-01-04 16:06:05 +00:00
|
|
|
index->unresolved = g_slist_prepend (index->unresolved, ii);
|
|
|
|
continue;
|
|
|
|
}
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-01-04 16:06:05 +00:00
|
|
|
if (match) {
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_WARNING_OBJECT (index, "Duplicate matches for writer '%s'",
|
2004-03-15 19:27:17 +00:00
|
|
|
writer_string);
|
2003-01-04 16:06:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2003-01-05 22:58:42 +00:00
|
|
|
ii->id = *id = ++index->next_id;
|
|
|
|
g_hash_table_insert (index->id_index, &ii->id, ii);
|
2003-01-04 16:06:05 +00:00
|
|
|
match = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_slist_free (pending);
|
|
|
|
|
2003-08-15 13:39:14 +00:00
|
|
|
if (!match) {
|
|
|
|
struct fi_find_writer_context cx;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-08-15 13:39:14 +00:00
|
|
|
cx.writer_string = writer_string;
|
|
|
|
cx.ii = NULL;
|
|
|
|
g_hash_table_foreach (index->id_index, _fi_find_writer, &cx);
|
|
|
|
|
|
|
|
if (cx.ii) {
|
|
|
|
match = TRUE;
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_DEBUG_OBJECT (index, "Resolved writer '%s' again", writer_string);
|
2004-03-13 15:27:01 +00:00
|
|
|
} else
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_WARNING_OBJECT (index, "Can't resolve writer '%s'", writer_string);
|
2003-08-15 13:39:14 +00:00
|
|
|
}
|
2003-01-05 22:58:42 +00:00
|
|
|
|
2003-01-04 16:06:05 +00:00
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
_fc_alloc_array (GstFileIndexId * id_index)
|
2003-01-04 16:06:05 +00:00
|
|
|
{
|
|
|
|
g_assert (!id_index->array);
|
|
|
|
id_index->array =
|
2004-03-13 15:27:01 +00:00
|
|
|
g_array_sized_new (FALSE, FALSE, ARRAY_ROW_SIZE (id_index), 0);
|
2003-01-04 16:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_file_index_load (GstFileIndex * index)
|
2003-01-04 16:06:05 +00:00
|
|
|
{
|
|
|
|
xmlDocPtr doc;
|
|
|
|
xmlNodePtr root, part;
|
|
|
|
xmlChar *val;
|
|
|
|
|
|
|
|
g_assert (index->location);
|
|
|
|
g_return_if_fail (!index->is_loaded);
|
|
|
|
|
|
|
|
{
|
|
|
|
gchar *path = g_strdup_printf ("%s/gstindex.xml", index->location);
|
|
|
|
GError *err = NULL;
|
|
|
|
gchar *buf;
|
|
|
|
gsize len;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-01-04 16:06:05 +00:00
|
|
|
g_file_get_contents (path, &buf, &len, &err);
|
|
|
|
g_free (path);
|
2003-07-25 03:13:24 +00:00
|
|
|
if (err) {
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_ERROR_OBJECT (index, "%s", err->message);
|
2003-07-25 03:13:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
doc = xmlParseMemory (buf, len);
|
|
|
|
g_free (buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
//xmlDocFormatDump (stderr, doc, TRUE);
|
|
|
|
|
|
|
|
root = doc->xmlRootNode;
|
2003-07-25 03:13:24 +00:00
|
|
|
if (strcmp (root->name, "gstfileindex") != 0) {
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_ERROR_OBJECT (index, "root node isn't a gstfileindex");
|
2003-07-25 03:13:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-01-04 16:06:05 +00:00
|
|
|
val = xmlGetProp (root, "version");
|
2003-07-25 03:13:24 +00:00
|
|
|
if (!val || atoi (val) != 1) {
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_ERROR_OBJECT (index, "version != 1");
|
2003-07-25 03:13:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-01-04 16:06:05 +00:00
|
|
|
free (val);
|
|
|
|
|
|
|
|
for (part = root->children; part; part = part->next) {
|
|
|
|
if (strcmp (part->name, "writers") == 0) {
|
|
|
|
xmlNodePtr writer;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-01-04 16:06:05 +00:00
|
|
|
for (writer = part->children; writer; writer = writer->next) {
|
2004-03-15 19:27:17 +00:00
|
|
|
xmlChar *datafile = xmlGetProp (writer, "datafile");
|
|
|
|
gchar *path = g_strdup_printf ("%s/%s", index->location, datafile);
|
|
|
|
int fd;
|
|
|
|
GstFileIndexId *id_index;
|
|
|
|
xmlNodePtr wpart;
|
|
|
|
xmlChar *entries_str;
|
|
|
|
gpointer array_data;
|
|
|
|
|
|
|
|
free (datafile);
|
|
|
|
|
|
|
|
fd = open (path, O_RDONLY);
|
|
|
|
g_free (path);
|
|
|
|
if (fd < 0) {
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_ERROR_OBJECT (index,
|
2004-03-15 19:27:17 +00:00
|
|
|
"Can't open '%s': %s", path, strerror (errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
id_index = g_new0 (GstFileIndexId, 1);
|
|
|
|
id_index->id_desc = xmlGetProp (writer, "id");
|
|
|
|
|
|
|
|
for (wpart = writer->children; wpart; wpart = wpart->next) {
|
|
|
|
if (strcmp (wpart->name, "formats") == 0) {
|
|
|
|
xmlChar *count_str = xmlGetProp (wpart, "count");
|
|
|
|
gint fx = 0;
|
|
|
|
xmlNodePtr format;
|
|
|
|
|
|
|
|
id_index->nformats = atoi (count_str);
|
|
|
|
free (count_str);
|
|
|
|
|
|
|
|
id_index->format = g_new (GstFormat, id_index->nformats);
|
|
|
|
|
|
|
|
for (format = wpart->children; format; format = format->next) {
|
|
|
|
xmlChar *nick = xmlGetProp (format, "nick");
|
|
|
|
GstFormat fmt = gst_format_get_by_nick (nick);
|
|
|
|
|
|
|
|
if (fmt == GST_FORMAT_UNDEFINED)
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_ERROR_OBJECT (index, "format '%s' undefined", nick);
|
2004-03-15 19:27:17 +00:00
|
|
|
g_assert (fx < id_index->nformats);
|
|
|
|
id_index->format[fx++] = fmt;
|
|
|
|
free (nick);
|
|
|
|
}
|
|
|
|
} else
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_INFO_OBJECT (index, "unknown wpart '%s'", wpart->name);
|
2004-03-15 19:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_assert (id_index->nformats > 0);
|
|
|
|
_fc_alloc_array (id_index);
|
|
|
|
g_assert (id_index->array->data == NULL); /* little bit risky */
|
|
|
|
|
|
|
|
entries_str = xmlGetProp (writer, "entries");
|
|
|
|
id_index->array->len = atoi (entries_str);
|
|
|
|
free (entries_str);
|
|
|
|
|
|
|
|
array_data =
|
|
|
|
mmap (NULL, ARRAY_TOTAL_SIZE (id_index), PROT_READ, MAP_SHARED, fd,
|
|
|
|
0);
|
|
|
|
close (fd);
|
|
|
|
if (array_data == MAP_FAILED) {
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_ERROR_OBJECT (index,
|
2004-03-15 19:27:17 +00:00
|
|
|
"mmap %s failed: %s", path, strerror (errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
id_index->array->data = array_data;
|
|
|
|
|
|
|
|
index->unresolved = g_slist_prepend (index->unresolved, id_index);
|
2003-01-04 16:06:05 +00:00
|
|
|
}
|
|
|
|
} else
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_INFO_OBJECT (index, "unknown part '%s'", part->name);
|
2003-01-04 16:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
xmlFreeDoc (doc);
|
|
|
|
|
|
|
|
GST_FLAG_UNSET (index, GST_INDEX_WRITABLE);
|
|
|
|
index->is_loaded = TRUE;
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_LOG_OBJECT (index, "index %s loaded OK", index->location);
|
2003-01-04 16:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_file_index_set_property (GObject * object,
|
|
|
|
guint prop_id, const GValue * value, GParamSpec * pspec)
|
2003-01-04 16:06:05 +00:00
|
|
|
{
|
|
|
|
GstFileIndex *index = GST_FILE_INDEX (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
2004-03-13 15:27:01 +00:00
|
|
|
case ARG_LOCATION:
|
|
|
|
if (index->location)
|
2004-03-15 19:27:17 +00:00
|
|
|
g_free (index->location);
|
2004-03-13 15:27:01 +00:00
|
|
|
index->location = g_value_dup_string (value);
|
2003-01-04 16:06:05 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
if (index->location && !g_hash_table_size (index->id_index))
|
2004-03-15 19:27:17 +00:00
|
|
|
gst_file_index_load (index);
|
2004-03-13 15:27:01 +00:00
|
|
|
break;
|
2003-01-04 16:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_file_index_get_property (GObject * object,
|
|
|
|
guint prop_id, GValue * value, GParamSpec * pspec)
|
2003-01-04 16:06:05 +00:00
|
|
|
{
|
|
|
|
GstFileIndex *index = GST_FILE_INDEX (object);
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-01-04 16:06:05 +00:00
|
|
|
switch (prop_id) {
|
2004-03-13 15:27:01 +00:00
|
|
|
case ARG_LOCATION:
|
|
|
|
g_value_set_string (value, index->location);
|
|
|
|
break;
|
2003-01-04 16:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
_file_index_id_save_xml (gpointer _key, GstFileIndexId * ii, xmlNodePtr writers)
|
2003-01-04 16:06:05 +00:00
|
|
|
{
|
|
|
|
const gint bufsize = 16;
|
|
|
|
gchar buf[bufsize];
|
|
|
|
xmlNodePtr writer;
|
|
|
|
xmlNodePtr formats;
|
|
|
|
gint xx;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-07-25 03:13:24 +00:00
|
|
|
if (!ii->array) {
|
|
|
|
GST_INFO ("Index for %s is empty", ii->id_desc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-01-04 16:06:05 +00:00
|
|
|
writer = xmlNewChild (writers, NULL, "writer", NULL);
|
|
|
|
xmlSetProp (writer, "id", ii->id_desc);
|
|
|
|
g_snprintf (buf, bufsize, "%d", ii->array->len);
|
|
|
|
xmlSetProp (writer, "entries", buf);
|
2004-03-15 19:27:17 +00:00
|
|
|
g_snprintf (buf, bufsize, "%d", ii->id); /* any unique number is OK */
|
2003-01-04 16:06:05 +00:00
|
|
|
xmlSetProp (writer, "datafile", buf);
|
|
|
|
|
|
|
|
formats = xmlNewChild (writer, NULL, "formats", NULL);
|
|
|
|
g_snprintf (buf, bufsize, "%d", ii->nformats);
|
|
|
|
xmlSetProp (formats, "count", buf);
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
for (xx = 0; xx < ii->nformats; xx++) {
|
2003-01-04 16:06:05 +00:00
|
|
|
xmlNodePtr format = xmlNewChild (formats, NULL, "format", NULL);
|
2004-03-13 15:27:01 +00:00
|
|
|
const GstFormatDefinition *def = gst_format_get_details (ii->format[xx]);
|
|
|
|
|
2003-01-04 16:06:05 +00:00
|
|
|
xmlSetProp (format, "nick", def->nick);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-25 03:13:24 +00:00
|
|
|
/*
|
|
|
|
We must save the binary data in separate files because
|
|
|
|
mmap wants getpagesize() alignment. If we append all
|
|
|
|
the data to one file then we don't know the appropriate
|
|
|
|
padding since the page size isn't fixed.
|
|
|
|
*/
|
2003-01-04 16:06:05 +00:00
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
_file_index_id_save_entries (gpointer * _key,
|
|
|
|
GstFileIndexId * ii, gchar * prefix)
|
2003-01-04 16:06:05 +00:00
|
|
|
{
|
2003-08-01 16:17:46 +00:00
|
|
|
GError *err;
|
|
|
|
gchar *path;
|
|
|
|
GIOChannel *chan;
|
|
|
|
|
2003-07-25 03:13:24 +00:00
|
|
|
if (!ii->array)
|
|
|
|
return;
|
|
|
|
|
2003-08-01 16:17:46 +00:00
|
|
|
err = NULL;
|
|
|
|
path = g_strdup_printf ("%s/%d", prefix, ii->id);
|
2004-07-12 21:57:35 +00:00
|
|
|
chan = g_io_channel_new_file (path, "w", &err);
|
2003-01-04 16:06:05 +00:00
|
|
|
g_free (path);
|
2004-03-13 15:27:01 +00:00
|
|
|
if (err)
|
|
|
|
goto fail;
|
|
|
|
|
2003-01-04 16:06:05 +00:00
|
|
|
g_io_channel_set_encoding (chan, NULL, &err);
|
2004-03-13 15:27:01 +00:00
|
|
|
if (err)
|
|
|
|
goto fail;
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
g_io_channel_write_chars (chan,
|
2004-03-13 15:27:01 +00:00
|
|
|
ii->array->data, ARRAY_TOTAL_SIZE (ii), NULL, &err);
|
|
|
|
if (err)
|
|
|
|
goto fail;
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
g_io_channel_shutdown (chan, TRUE, &err);
|
2004-03-13 15:27:01 +00:00
|
|
|
if (err)
|
|
|
|
goto fail;
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
g_io_channel_unref (chan);
|
2003-07-25 03:13:24 +00:00
|
|
|
return;
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
fail:
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_ERROR ("%s", err->message);
|
2003-01-04 16:06:05 +00:00
|
|
|
}
|
|
|
|
|
2003-07-25 03:13:24 +00:00
|
|
|
/*
|
|
|
|
We have to save the whole set of indexes into a single file
|
|
|
|
so it doesn't make sense to commit only a single writer.
|
2003-01-04 16:06:05 +00:00
|
|
|
|
2003-07-25 03:13:24 +00:00
|
|
|
i suggest:
|
|
|
|
|
|
|
|
gst_index_commit (index, -1);
|
|
|
|
*/
|
2003-01-04 16:06:05 +00:00
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_file_index_commit (GstIndex * _index, gint _writer_id)
|
2003-01-04 16:06:05 +00:00
|
|
|
{
|
|
|
|
GstFileIndex *index = GST_FILE_INDEX (_index);
|
|
|
|
xmlDocPtr doc;
|
|
|
|
xmlNodePtr writers;
|
|
|
|
GError *err = NULL;
|
|
|
|
gchar *path;
|
|
|
|
GIOChannel *tocfile;
|
|
|
|
|
|
|
|
g_return_if_fail (index->location);
|
|
|
|
g_return_if_fail (!index->is_loaded);
|
|
|
|
|
|
|
|
GST_FLAG_UNSET (index, GST_INDEX_WRITABLE);
|
|
|
|
|
|
|
|
doc = xmlNewDoc ("1.0");
|
|
|
|
doc->xmlRootNode = xmlNewDocNode (doc, NULL, "gstfileindex", NULL);
|
|
|
|
xmlSetProp (doc->xmlRootNode, "version", "1");
|
|
|
|
|
|
|
|
writers = xmlNewChild (doc->xmlRootNode, NULL, "writers", NULL);
|
|
|
|
g_hash_table_foreach (index->id_index,
|
2004-03-13 15:27:01 +00:00
|
|
|
(GHFunc) _file_index_id_save_xml, writers);
|
2003-01-04 16:06:05 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
if (mkdir (index->location, 0777) && errno != EEXIST) {
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_ERROR_OBJECT (index, "mkdir %s: %s", index->location, strerror (errno));
|
2003-07-25 03:13:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
path = g_strdup_printf ("%s/gstindex.xml", index->location);
|
2004-07-12 21:57:35 +00:00
|
|
|
tocfile = g_io_channel_new_file (path, "w", &err);
|
2003-01-04 16:06:05 +00:00
|
|
|
g_free (path);
|
2003-07-25 03:13:24 +00:00
|
|
|
if (err) {
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_ERROR_OBJECT (index, "%s", err->message);
|
2003-07-25 03:13:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
g_io_channel_set_encoding (tocfile, NULL, &err);
|
2003-07-25 03:13:24 +00:00
|
|
|
if (err) {
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_ERROR_OBJECT (index, "%s", err->message);
|
2003-07-25 03:13:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
xmlChar *xmlmem;
|
|
|
|
int xmlsize;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-01-04 16:06:05 +00:00
|
|
|
xmlDocDumpMemory (doc, &xmlmem, &xmlsize);
|
|
|
|
g_io_channel_write_chars (tocfile, xmlmem, xmlsize, NULL, &err);
|
2003-07-25 03:13:24 +00:00
|
|
|
if (err) {
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_ERROR_OBJECT (index, "%s", err->message);
|
2003-07-25 03:13:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-01-04 16:06:05 +00:00
|
|
|
xmlFreeDoc (doc);
|
|
|
|
free (xmlmem);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_io_channel_shutdown (tocfile, TRUE, &err);
|
2003-07-25 03:13:24 +00:00
|
|
|
if (err) {
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_ERROR_OBJECT (index, "%s", err->message);
|
2003-07-25 03:13:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
g_io_channel_unref (tocfile);
|
|
|
|
|
|
|
|
g_hash_table_foreach (index->id_index,
|
2004-03-13 15:27:01 +00:00
|
|
|
(GHFunc) _file_index_id_save_entries, index->location);
|
2003-01-04 16:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_file_index_add_id (GstIndex * index, GstIndexEntry * entry)
|
2003-01-04 16:06:05 +00:00
|
|
|
{
|
|
|
|
GstFileIndex *fileindex = GST_FILE_INDEX (index);
|
|
|
|
GstFileIndexId *id_index;
|
|
|
|
|
|
|
|
id_index = g_hash_table_lookup (fileindex->id_index, &entry->id);
|
|
|
|
|
|
|
|
if (!id_index) {
|
|
|
|
id_index = g_new0 (GstFileIndexId, 1);
|
|
|
|
|
|
|
|
id_index->id = entry->id;
|
|
|
|
id_index->id_desc = g_strdup (entry->data.id.description);
|
|
|
|
|
2003-07-25 03:13:24 +00:00
|
|
|
/* It would be useful to know the GType of the writer so
|
|
|
|
we can try to cope with changes in the id_desc path. */
|
2003-01-04 16:06:05 +00:00
|
|
|
|
2003-01-05 22:58:42 +00:00
|
|
|
g_hash_table_insert (fileindex->id_index, &id_index->id, id_index);
|
2003-01-04 16:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-25 03:13:24 +00:00
|
|
|
/* This algorithm differs from libc bsearch in the handling
|
|
|
|
of non-exact matches. */
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
static gboolean
|
2004-03-13 15:27:01 +00:00
|
|
|
_fc_bsearch (GArray * ary,
|
|
|
|
gint stride,
|
|
|
|
gint * ret,
|
|
|
|
GCompareDataFunc compare, gconstpointer sample, gpointer user_data)
|
2003-01-04 16:06:05 +00:00
|
|
|
{
|
|
|
|
gint first, last;
|
|
|
|
gint mid;
|
|
|
|
gint midsize;
|
|
|
|
gint cmp;
|
|
|
|
gint tx;
|
|
|
|
|
|
|
|
g_return_val_if_fail (compare, FALSE);
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
if (!ary->len) {
|
|
|
|
if (ret)
|
|
|
|
*ret = 0;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
first = 0;
|
|
|
|
last = ary->len - 1;
|
|
|
|
|
|
|
|
midsize = last - first;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-01-04 16:06:05 +00:00
|
|
|
while (midsize > 1) {
|
|
|
|
mid = first + midsize / 2;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
|
|
|
cmp = (*compare) (sample, ary->data + mid * stride, user_data);
|
|
|
|
|
|
|
|
if (cmp == 0) {
|
|
|
|
/* if there are multiple matches then scan for the first match */
|
|
|
|
while (mid > 0 &&
|
2004-03-15 19:27:17 +00:00
|
|
|
(*compare) (sample, ary->data + (mid - 1) * stride, user_data) == 0)
|
|
|
|
--mid;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
|
|
|
if (ret)
|
2004-03-15 19:27:17 +00:00
|
|
|
*ret = mid;
|
2004-03-13 15:27:01 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2003-01-04 16:06:05 +00:00
|
|
|
if (cmp < 0)
|
2004-03-13 15:27:01 +00:00
|
|
|
last = mid - 1;
|
2003-01-04 16:06:05 +00:00
|
|
|
else
|
2004-03-13 15:27:01 +00:00
|
|
|
first = mid + 1;
|
|
|
|
|
2003-01-04 16:06:05 +00:00
|
|
|
midsize = last - first;
|
|
|
|
}
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
for (tx = first; tx <= last; tx++) {
|
|
|
|
cmp = (*compare) (sample, ary->data + tx * stride, user_data);
|
2003-01-04 16:06:05 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
if (cmp < 0) {
|
|
|
|
if (ret)
|
2004-03-15 19:27:17 +00:00
|
|
|
*ret = tx;
|
2004-03-13 15:27:01 +00:00
|
|
|
return FALSE;
|
2003-01-04 16:06:05 +00:00
|
|
|
}
|
2004-03-13 15:27:01 +00:00
|
|
|
if (cmp == 0) {
|
|
|
|
if (ret)
|
2004-03-15 19:27:17 +00:00
|
|
|
*ret = tx;
|
2004-03-13 15:27:01 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
2003-01-04 16:06:05 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
if (ret)
|
|
|
|
*ret = last + 1;
|
2003-01-04 16:06:05 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
2004-03-13 15:27:01 +00:00
|
|
|
file_index_compare (gconstpointer sample, gconstpointer row, gpointer user_data)
|
2003-01-04 16:06:05 +00:00
|
|
|
{
|
|
|
|
//GstFileIndexId *id_index = user_data;
|
|
|
|
const GstIndexAssociation *ca = sample;
|
|
|
|
gint64 val1 = ca->value;
|
2003-01-05 16:51:46 +00:00
|
|
|
gint64 val2_be = ARRAY_ROW_VALUE (row, ca->format);
|
|
|
|
gint64 val2 = GINT64_FROM_BE (val2_be);
|
2003-01-04 16:06:05 +00:00
|
|
|
gint64 diff = val2 - val1;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-01-05 16:51:46 +00:00
|
|
|
return (diff == 0 ? 0 : (diff < 0 ? 1 : -1));
|
2003-01-04 16:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_file_index_add_association (GstIndex * index, GstIndexEntry * entry)
|
2003-01-04 16:06:05 +00:00
|
|
|
{
|
|
|
|
GstFileIndex *fileindex = GST_FILE_INDEX (index);
|
|
|
|
GstFileIndexId *id_index;
|
|
|
|
gint mx;
|
|
|
|
GstIndexAssociation sample;
|
|
|
|
gboolean exact;
|
|
|
|
|
|
|
|
id_index = g_hash_table_lookup (fileindex->id_index, &entry->id);
|
|
|
|
if (!id_index)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!id_index->nformats) {
|
|
|
|
gint fx;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-01-04 16:06:05 +00:00
|
|
|
id_index->nformats = GST_INDEX_NASSOCS (entry);
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_LOG_OBJECT (fileindex, "creating %d formats for %d",
|
2004-03-15 19:27:17 +00:00
|
|
|
id_index->nformats, entry->id);
|
2003-01-04 16:06:05 +00:00
|
|
|
id_index->format = g_new (GstFormat, id_index->nformats);
|
2004-03-13 15:27:01 +00:00
|
|
|
for (fx = 0; fx < id_index->nformats; fx++)
|
2003-01-04 16:06:05 +00:00
|
|
|
id_index->format[fx] = GST_INDEX_ASSOC_FORMAT (entry, fx);
|
|
|
|
_fc_alloc_array (id_index);
|
|
|
|
} else {
|
|
|
|
/* only sanity checking */
|
|
|
|
if (id_index->nformats != GST_INDEX_NASSOCS (entry))
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_WARNING_OBJECT (fileindex, "arity change %d -> %d",
|
2004-03-15 19:27:17 +00:00
|
|
|
id_index->nformats, GST_INDEX_NASSOCS (entry));
|
2003-01-04 16:06:05 +00:00
|
|
|
else {
|
|
|
|
gint fx;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
|
|
|
for (fx = 0; fx < id_index->nformats; fx++)
|
2004-03-15 19:27:17 +00:00
|
|
|
if (id_index->format[fx] != GST_INDEX_ASSOC_FORMAT (entry, fx))
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_WARNING_OBJECT (fileindex, "format[%d] changed %d -> %d",
|
2004-03-15 19:27:17 +00:00
|
|
|
fx, id_index->format[fx], GST_INDEX_ASSOC_FORMAT (entry, fx));
|
2003-01-04 16:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is a hack, we should use a private structure instead */
|
|
|
|
sample.format = 0;
|
|
|
|
sample.value = GST_INDEX_ASSOC_VALUE (entry, 0);
|
|
|
|
|
|
|
|
exact =
|
2004-03-13 15:27:01 +00:00
|
|
|
_fc_bsearch (id_index->array, ARRAY_ROW_SIZE (id_index),
|
|
|
|
&mx, file_index_compare, &sample, id_index);
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
if (exact) {
|
2003-07-25 03:13:24 +00:00
|
|
|
/* maybe overwrite instead? */
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_DEBUG_OBJECT (index,
|
2004-03-15 19:27:17 +00:00
|
|
|
"Ignoring duplicate index association at %lld",
|
|
|
|
GST_INDEX_ASSOC_VALUE (entry, 0));
|
2003-01-04 16:06:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
gchar row_data[ARRAY_ROW_SIZE (id_index)];
|
2003-01-05 22:58:42 +00:00
|
|
|
gint fx;
|
2003-01-04 16:06:05 +00:00
|
|
|
|
2003-01-05 16:51:46 +00:00
|
|
|
gint32 flags_host = GST_INDEX_ASSOC_FLAGS (entry);
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-01-05 16:51:46 +00:00
|
|
|
ARRAY_ROW_FLAGS (row_data) = GINT32_TO_BE (flags_host);
|
2003-01-04 16:06:05 +00:00
|
|
|
|
2003-01-05 16:51:46 +00:00
|
|
|
for (fx = 0; fx < id_index->nformats; fx++) {
|
|
|
|
gint64 val_host = GST_INDEX_ASSOC_VALUE (entry, fx);
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-01-05 16:51:46 +00:00
|
|
|
ARRAY_ROW_VALUE (row_data, fx) = GINT64_TO_BE (val_host);
|
|
|
|
}
|
2003-01-04 16:06:05 +00:00
|
|
|
|
2003-01-05 22:58:42 +00:00
|
|
|
g_array_insert_vals (id_index->array, mx, row_data, 1);
|
2003-01-04 16:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
static void
|
|
|
|
show_entry (GstIndexEntry *entry)
|
|
|
|
{
|
|
|
|
switch (entry->type) {
|
|
|
|
case GST_INDEX_ENTRY_ID:
|
|
|
|
g_print ("id %d describes writer %s\n", entry->id,
|
|
|
|
GST_INDEX_ID_DESCRIPTION (entry));
|
|
|
|
break;
|
|
|
|
case GST_INDEX_ENTRY_FORMAT:
|
|
|
|
g_print ("%d: registered format %d for %s\n", entry->id,
|
|
|
|
GST_INDEX_FORMAT_FORMAT (entry),
|
|
|
|
GST_INDEX_FORMAT_KEY (entry));
|
|
|
|
break;
|
|
|
|
case GST_INDEX_ENTRY_ASSOCIATION:
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
g_print ("%d: %08x ", entry->id, GST_INDEX_ASSOC_FLAGS (entry));
|
|
|
|
for (i = 0; i < GST_INDEX_NASSOCS (entry); i++) {
|
|
|
|
g_print ("%d %lld ", GST_INDEX_ASSOC_FORMAT (entry, i),
|
|
|
|
GST_INDEX_ASSOC_VALUE (entry, i));
|
|
|
|
}
|
|
|
|
g_print ("\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_file_index_add_entry (GstIndex * index, GstIndexEntry * entry)
|
2003-01-04 16:06:05 +00:00
|
|
|
{
|
2003-06-29 14:05:49 +00:00
|
|
|
GST_LOG_OBJECT (index, "adding this entry");
|
2003-01-04 16:06:05 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
switch (entry->type) {
|
|
|
|
case GST_INDEX_ENTRY_ID:
|
|
|
|
gst_file_index_add_id (index, entry);
|
|
|
|
break;
|
|
|
|
case GST_INDEX_ENTRY_ASSOCIATION:
|
|
|
|
gst_file_index_add_association (index, entry);
|
|
|
|
break;
|
|
|
|
case GST_INDEX_ENTRY_OBJECT:
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_ERROR_OBJECT (index, "gst_file_index_add_object not implemented");
|
2004-03-13 15:27:01 +00:00
|
|
|
break;
|
|
|
|
case GST_INDEX_ENTRY_FORMAT:
|
|
|
|
/*
|
|
|
|
We infer the formats from the entry itself so this type of
|
|
|
|
GST_INDEX_ENTRY_* can probably go away.
|
2003-07-25 03:13:24 +00:00
|
|
|
*/
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_DEBUG_OBJECT (index, "gst_file_index_add_format not implemented");
|
2004-03-13 15:27:01 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2003-01-04 16:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
static GstIndexEntry *
|
|
|
|
gst_file_index_get_assoc_entry (GstIndex * index,
|
|
|
|
gint id,
|
|
|
|
GstIndexLookupMethod method,
|
|
|
|
GstAssocFlags flags,
|
|
|
|
GstFormat format,
|
|
|
|
gint64 value, GCompareDataFunc _ignore_func, gpointer _ignore_user_data)
|
2003-01-04 16:06:05 +00:00
|
|
|
{
|
|
|
|
GstFileIndex *fileindex = GST_FILE_INDEX (index);
|
|
|
|
GstFileIndexId *id_index;
|
|
|
|
gint formatx = -1;
|
|
|
|
gint fx;
|
|
|
|
GstIndexAssociation sample;
|
|
|
|
gint mx;
|
|
|
|
gboolean exact;
|
|
|
|
gpointer row_data;
|
|
|
|
GstIndexEntry *entry;
|
|
|
|
gint xx;
|
|
|
|
|
2003-07-25 03:13:24 +00:00
|
|
|
g_return_val_if_fail (id > 0, NULL);
|
|
|
|
|
2003-01-04 16:06:05 +00:00
|
|
|
id_index = g_hash_table_lookup (fileindex->id_index, &id);
|
2003-07-25 03:13:24 +00:00
|
|
|
if (!id_index) {
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_WARNING_OBJECT (fileindex, "writer %d unavailable", id);
|
2003-01-04 16:06:05 +00:00
|
|
|
return NULL;
|
2003-07-25 03:13:24 +00:00
|
|
|
}
|
2003-01-04 16:06:05 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
for (fx = 0; fx < id_index->nformats; fx++)
|
|
|
|
if (id_index->format[fx] == format) {
|
|
|
|
formatx = fx;
|
|
|
|
break;
|
|
|
|
}
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
if (formatx == -1) {
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_WARNING_OBJECT (fileindex, "format %d not available", format);
|
2003-01-04 16:06:05 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is a hack, we should use a private structure instead */
|
|
|
|
sample.format = formatx;
|
|
|
|
sample.value = value;
|
|
|
|
|
2003-01-05 16:51:46 +00:00
|
|
|
exact = _fc_bsearch (id_index->array, ARRAY_ROW_SIZE (id_index),
|
2004-03-13 15:27:01 +00:00
|
|
|
&mx, file_index_compare, &sample, id_index);
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
if (!exact) {
|
|
|
|
if (method == GST_INDEX_LOOKUP_EXACT)
|
|
|
|
return NULL;
|
|
|
|
else if (method == GST_INDEX_LOOKUP_BEFORE) {
|
|
|
|
if (mx == 0)
|
2004-03-15 19:27:17 +00:00
|
|
|
return NULL;
|
2003-01-04 16:06:05 +00:00
|
|
|
mx -= 1;
|
|
|
|
} else if (method == GST_INDEX_LOOKUP_AFTER) {
|
|
|
|
if (mx == id_index->array->len)
|
2004-03-15 19:27:17 +00:00
|
|
|
return NULL;
|
2003-01-04 16:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-05 16:51:46 +00:00
|
|
|
row_data = id_index->array->data + mx * ARRAY_ROW_SIZE (id_index);
|
2003-01-04 16:06:05 +00:00
|
|
|
|
2003-07-25 03:13:24 +00:00
|
|
|
/* if exact then ignore flags (?) */
|
2003-01-04 16:06:05 +00:00
|
|
|
if (method != GST_INDEX_LOOKUP_EXACT)
|
|
|
|
while ((GINT32_FROM_BE (ARRAY_ROW_FLAGS (row_data)) & flags) != flags) {
|
|
|
|
if (method == GST_INDEX_LOOKUP_BEFORE)
|
2004-03-15 19:27:17 +00:00
|
|
|
mx -= 1;
|
2003-01-04 16:06:05 +00:00
|
|
|
else if (method == GST_INDEX_LOOKUP_AFTER)
|
2004-03-15 19:27:17 +00:00
|
|
|
mx += 1;
|
2003-01-04 16:06:05 +00:00
|
|
|
if (mx < 0 || mx >= id_index->array->len)
|
2004-03-15 19:27:17 +00:00
|
|
|
return NULL;
|
2003-01-05 16:51:46 +00:00
|
|
|
row_data = id_index->array->data + mx * ARRAY_ROW_SIZE (id_index);
|
2003-01-04 16:06:05 +00:00
|
|
|
}
|
|
|
|
|
2003-07-25 03:13:24 +00:00
|
|
|
/* entry memory management needs improvement FIXME */
|
2003-01-04 16:06:05 +00:00
|
|
|
if (!fileindex->ret_entry)
|
|
|
|
fileindex->ret_entry = g_new0 (GstIndexEntry, 1);
|
|
|
|
entry = fileindex->ret_entry;
|
2003-01-05 22:58:42 +00:00
|
|
|
if (entry->data.assoc.assocs) {
|
2003-01-04 16:06:05 +00:00
|
|
|
g_free (entry->data.assoc.assocs);
|
2003-01-05 22:58:42 +00:00
|
|
|
entry->data.assoc.assocs = NULL;
|
|
|
|
}
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
entry->type = GST_INDEX_ENTRY_ASSOCIATION;
|
|
|
|
|
|
|
|
GST_INDEX_NASSOCS (entry) = id_index->nformats;
|
2004-03-13 15:27:01 +00:00
|
|
|
entry->data.assoc.assocs = g_new (GstIndexAssociation, id_index->nformats);
|
2003-01-04 16:06:05 +00:00
|
|
|
|
2003-01-05 16:51:46 +00:00
|
|
|
{
|
|
|
|
gint32 flags_be = ARRAY_ROW_FLAGS (row_data);
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-01-05 16:51:46 +00:00
|
|
|
GST_INDEX_ASSOC_FLAGS (entry) = GINT32_FROM_BE (flags_be);
|
2003-01-04 16:06:05 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
for (xx = 0; xx < id_index->nformats; xx++) {
|
|
|
|
gint64 val_be = ARRAY_ROW_VALUE (row_data, xx);
|
|
|
|
|
|
|
|
GST_INDEX_ASSOC_FORMAT (entry, xx) = id_index->format[xx];
|
|
|
|
GST_INDEX_ASSOC_VALUE (entry, xx) = GINT64_FROM_BE (val_be);
|
|
|
|
}
|
2003-01-05 16:51:46 +00:00
|
|
|
}
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2003-01-04 16:26:58 +00:00
|
|
|
gboolean
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_file_index_plugin_init (GstPlugin * plugin)
|
2003-01-04 16:06:05 +00:00
|
|
|
{
|
|
|
|
GstIndexFactory *factory;
|
|
|
|
|
|
|
|
factory = gst_index_factory_new ("fileindex",
|
2004-03-13 15:27:01 +00:00
|
|
|
"A index that stores entries in file", gst_file_index_get_type ());
|
2003-01-04 16:06:05 +00:00
|
|
|
|
|
|
|
if (factory != NULL) {
|
|
|
|
gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
|
2004-03-13 15:27:01 +00:00
|
|
|
} else {
|
2004-06-06 23:39:39 +00:00
|
|
|
return FALSE;
|
2003-01-04 16:06:05 +00:00
|
|
|
}
|
2004-06-06 23:39:39 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (DC, "GST_FILEINDEX", 0, NULL);
|
|
|
|
|
2003-01-04 16:06:05 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|