2 * Copyright (C) 2006 Josep Torra <josep@fluendo.com>
3 * 2006 Mathieu Garcia <matthieu@fluendo.com>
4 * 2006,2007 Stefan Kost <ensonic@users.sf.net>
5 * 2008 Sebastian Dröge <slomo@circular-chaos.org>
7 * gstregistrybinary.c: GstRegistryBinary object, support routines
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 * - keep registry binary blob and reference strings
27 * - don't free/unmmap contents when leaving gst_registry_binary_read_cache()
28 * - free at gst_deinit() / _priv_gst_registry_cleanup() ?
30 * - GST_PLUGIN_FLAG_CONST
31 * - GstPluginFeature, GstIndexFactory, GstElementFactory
32 * - needs Flags (GST_PLUGIN_FEATURE_FLAG_CONST)
33 * - can we turn loaded into flag?
34 * - why do we collect a list of binary chunks and not write immediately
35 * - because we need to process subchunks, before we can set e.g. nr_of_items
37 * - need more robustness
38 * - don't parse beyond mem-block size
52 #if defined (_MSC_VER) && _MSC_VER >= 1400
56 #include <gst/gst_private.h>
57 #include <gst/gstconfig.h>
58 #include <gst/gstelement.h>
59 #include <gst/gsttypefind.h>
60 #include <gst/gsttypefindfactory.h>
61 #include <gst/gsturi.h>
62 #include <gst/gstinfo.h>
63 #include <gst/gstenumtypes.h>
64 #include <gst/gstpadtemplate.h>
66 #include <gst/gstregistrybinary.h>
68 #include <glib/gstdio.h> /* for g_stat(), g_mapped_file(), ... */
70 #include "glib-compat-private.h"
73 #define GST_CAT_DEFAULT GST_CAT_REGISTRY
77 #define unpack_element(_inptr, _outptr, _element) G_STMT_START{ \
78 _outptr = (_element *) _inptr; \
79 _inptr += sizeof (_element); \
82 #define unpack_const_string(_inptr, _outptr) G_STMT_START{\
83 _outptr = g_intern_string ((const gchar *)_inptr); \
84 _inptr += strlen(_outptr) + 1; \
87 #define unpack_string(_inptr, _outptr) G_STMT_START{\
88 _outptr = g_strdup ((gchar *)_inptr); \
89 _inptr += strlen(_outptr) + 1; \
92 #define ALIGNMENT (sizeof (void *))
93 #define alignment(_address) (gsize)_address%ALIGNMENT
94 #define align(_ptr) _ptr += (( alignment(_ptr) == 0) ? 0 : ALIGNMENT-alignment(_ptr))
99 /* On win32, we can't use g_mkstmp(), because of cross-DLL file I/O problems.
100 * So, we just create the entire binary registry in memory, then write it out
101 * with g_file_set_contents(), which creates a temporary file internally
104 typedef struct BinaryRegistryCache
106 const char *location;
109 } BinaryRegistryCache;
111 static BinaryRegistryCache *
112 gst_registry_binary_cache_init (GstRegistry * registry, const char *location)
114 BinaryRegistryCache *cache = g_new0 (BinaryRegistryCache, 1);
115 cache->location = location;
120 gst_registry_binary_cache_write (GstRegistry * registry,
121 BinaryRegistryCache * cache, unsigned long offset,
122 const void *data, int length)
124 cache->len = MAX (offset + length, cache->len);
125 cache->mem = g_realloc (cache->mem, cache->len);
127 memcpy (cache->mem + offset, data, length);
133 gst_registry_binary_cache_finish (GstRegistry * registry,
134 BinaryRegistryCache * cache, gboolean success)
137 GError *error = NULL;
138 if (!g_file_set_contents (cache->location, (const gchar *) cache->mem,
139 cache->len, &error)) {
140 /* Probably the directory didn't exist; create it */
142 dir = g_path_get_dirname (cache->location);
143 g_mkdir_with_parents (dir, 0777);
146 g_error_free (error);
149 if (!g_file_set_contents (cache->location, (const gchar *) cache->mem,
150 cache->len, &error)) {
151 GST_ERROR ("Failed to write to cache file: %s", error->message);
152 g_error_free (error);
163 typedef struct BinaryRegistryCache
165 const char *location;
167 unsigned long currentoffset;
168 } BinaryRegistryCache;
170 static BinaryRegistryCache *
171 gst_registry_binary_cache_init (GstRegistry * registry, const char *location)
173 BinaryRegistryCache *cache = g_new0 (BinaryRegistryCache, 1);
175 cache->location = location;
176 cache->tmp_location = g_strconcat (location, ".tmpXXXXXX", NULL);
177 registry->cache_file = g_mkstemp (cache->tmp_location);
178 if (registry->cache_file == -1) {
181 /* oops, I bet the directory doesn't exist */
182 dir = g_path_get_dirname (location);
183 g_mkdir_with_parents (dir, 0777);
186 /* the previous g_mkstemp call overwrote the XXXXXX placeholder ... */
187 g_free (cache->tmp_location);
188 cache->tmp_location = g_strconcat (location, ".tmpXXXXXX", NULL);
189 registry->cache_file = g_mkstemp (cache->tmp_location);
191 if (registry->cache_file == -1) {
192 GST_DEBUG ("g_mkstemp() failed: %s", g_strerror (errno));
193 g_free (cache->tmp_location);
203 gst_registry_binary_cache_write (GstRegistry * registry,
204 BinaryRegistryCache * cache, unsigned long offset,
205 const void *data, int length)
208 if (offset != cache->currentoffset) {
209 if (lseek (registry->cache_file, offset, SEEK_SET) != 0) {
210 GST_ERROR ("Seeking to new offset failed");
213 cache->currentoffset = offset;
216 written = write (registry->cache_file, data, length);
217 if (written != length) {
218 GST_ERROR ("Failed to write to cache file");
220 cache->currentoffset += written;
226 gst_registry_binary_cache_finish (GstRegistry * registry,
227 BinaryRegistryCache * cache, gboolean success)
229 if (close (registry->cache_file) < 0)
233 /* Only do the rename if we wrote the entire file successfully */
234 if (g_rename (cache->tmp_location, cache->location) < 0)
238 g_free (cache->tmp_location);
240 GST_INFO ("Wrote binary registry cache");
245 g_unlink (cache->tmp_location);
246 g_free (cache->tmp_location);
252 GST_ERROR ("close() failed: %s", g_strerror (errno));
253 goto fail_after_close;
257 GST_ERROR ("g_rename() failed: %s", g_strerror (errno));
258 goto fail_after_close;
264 * gst_registry_binary_write_chunk:
266 * Write from a memory location to the registry cache file
268 * Returns: %TRUE for success
270 inline static gboolean
271 gst_registry_binary_write_chunk (GstRegistry * registry,
272 BinaryRegistryCache * cache, const void *mem,
273 const gssize size, unsigned long *file_position, gboolean align)
275 gchar padder[ALIGNMENT] = { 0, };
278 /* Padding to insert the struct that requiere word alignment */
279 if ((align) && (alignment (*file_position) != 0)) {
280 padsize = ALIGNMENT - alignment (*file_position);
281 if (gst_registry_binary_cache_write (registry, cache, *file_position,
282 padder, padsize) != padsize) {
283 GST_ERROR ("Failed to write binary registry padder");
286 *file_position += padsize;
289 if (gst_registry_binary_cache_write (registry, cache, *file_position,
290 mem, size) != size) {
291 GST_ERROR ("Failed to write binary registry element");
295 *file_position += size;
302 * gst_registry_binary_initialize_magic:
304 * Initialize the GstBinaryRegistryMagic, setting both our magic number and
305 * gstreamer major/minor version
307 inline static gboolean
308 gst_registry_binary_initialize_magic (GstBinaryRegistryMagic * m)
310 memset (m, 0, sizeof (GstBinaryRegistryMagic));
312 if (!strncpy (m->magic, GST_MAGIC_BINARY_REGISTRY_STR,
313 GST_MAGIC_BINARY_REGISTRY_LEN)
314 || !strncpy (m->version, GST_MAGIC_BINARY_VERSION_STR,
315 GST_MAGIC_BINARY_VERSION_LEN)) {
316 GST_ERROR ("Failed to write magic to the registry magic structure");
325 * gst_registry_binary_save_const_string:
327 * Store a const string in a binary chunk.
329 * Returns: %TRUE for success
331 inline static gboolean
332 gst_registry_binary_save_const_string (GList ** list, const gchar * str)
334 GstBinaryChunk *chunk;
336 if (G_UNLIKELY (str == NULL)) {
337 GST_ERROR ("unexpected NULL string in plugin or plugin feature data");
341 chunk = g_malloc (sizeof (GstBinaryChunk));
342 chunk->data = (gpointer) str;
343 chunk->size = strlen ((gchar *) chunk->data) + 1;
344 chunk->flags = GST_BINARY_REGISTRY_FLAG_CONST;
345 chunk->align = FALSE;
346 *list = g_list_prepend (*list, chunk);
351 * gst_registry_binary_save_string:
353 * Store a string in a binary chunk.
355 * Returns: %TRUE for success
357 inline static gboolean
358 gst_registry_binary_save_string (GList ** list, gchar * str)
360 GstBinaryChunk *chunk;
362 chunk = g_malloc (sizeof (GstBinaryChunk));
364 chunk->size = strlen ((gchar *) chunk->data) + 1;
365 chunk->flags = GST_BINARY_REGISTRY_FLAG_NONE;
366 chunk->align = FALSE;
367 *list = g_list_prepend (*list, chunk);
373 * gst_registry_binary_save_data:
375 * Store some data in a binary chunk.
377 * Returns: the initialized chunk
379 inline static GstBinaryChunk *
380 gst_registry_binary_make_data (gpointer data, gulong size)
382 GstBinaryChunk *chunk;
384 chunk = g_malloc (sizeof (GstBinaryChunk));
387 chunk->flags = GST_BINARY_REGISTRY_FLAG_NONE;
394 * gst_registry_binary_save_pad_template:
396 * Store pad_templates in binary chunks.
398 * Returns: %TRUE for success
401 gst_registry_binary_save_pad_template (GList ** list,
402 GstStaticPadTemplate * template)
404 GstBinaryPadTemplate *pt;
407 pt = g_malloc0 (sizeof (GstBinaryPadTemplate));
408 chk = gst_registry_binary_make_data (pt, sizeof (GstBinaryPadTemplate));
410 pt->presence = template->presence;
411 pt->direction = template->direction;
413 /* pack pad template strings */
414 gst_registry_binary_save_const_string (list,
415 (gchar *) (template->static_caps.string));
416 gst_registry_binary_save_const_string (list, template->name_template);
418 *list = g_list_prepend (*list, chk);
425 * gst_registry_binary_save_feature:
427 * Store features in binary chunks.
429 * Returns: %TRUE for success
432 gst_registry_binary_save_feature (GList ** list, GstPluginFeature * feature)
434 const gchar *type_name = g_type_name (G_OBJECT_TYPE (feature));
435 GstBinaryPluginFeature *pf = NULL;
436 GstBinaryChunk *chk = NULL;
440 GST_ERROR ("NULL feature type_name, aborting.");
444 if (GST_IS_ELEMENT_FACTORY (feature)) {
445 GstBinaryElementFactory *ef;
446 GstElementFactory *factory = GST_ELEMENT_FACTORY (feature);
448 ef = g_malloc0 (sizeof (GstBinaryElementFactory));
449 chk = gst_registry_binary_make_data (ef, sizeof (GstBinaryElementFactory));
450 ef->npadtemplates = ef->ninterfaces = ef->nuriprotocols = 0;
451 pf = (GstBinaryPluginFeature *) ef;
453 /* save interfaces */
454 for (walk = factory->interfaces; walk;
455 walk = g_list_next (walk), ef->ninterfaces++) {
456 gst_registry_binary_save_const_string (list, (gchar *) walk->data);
458 GST_DEBUG ("Saved %d Interfaces", ef->ninterfaces);
460 if (GST_URI_TYPE_IS_VALID (factory->uri_type)) {
461 if (factory->uri_protocols && *factory->uri_protocols) {
462 GstBinaryChunk *subchk;
466 gst_registry_binary_make_data (&factory->uri_type,
467 sizeof (factory->uri_type));
468 subchk->flags = GST_BINARY_REGISTRY_FLAG_CONST;
470 protocol = factory->uri_protocols;
472 gst_registry_binary_save_const_string (list, *protocol++);
475 *list = g_list_prepend (*list, subchk);
476 GST_DEBUG ("Saved %d UriTypes", ef->nuriprotocols);
478 g_warning ("GStreamer feature '%s' is URI handler but does not provide"
479 " any protocols it can handle", feature->name);
483 /* save pad-templates */
484 for (walk = factory->staticpadtemplates; walk;
485 walk = g_list_next (walk), ef->npadtemplates++) {
486 GstStaticPadTemplate *template = walk->data;
488 if (!gst_registry_binary_save_pad_template (list, template)) {
489 GST_ERROR ("Can't fill pad template, aborting.");
494 /* pack element factory strings */
495 gst_registry_binary_save_const_string (list, factory->details.author);
496 gst_registry_binary_save_const_string (list, factory->details.description);
497 gst_registry_binary_save_const_string (list, factory->details.klass);
498 gst_registry_binary_save_const_string (list, factory->details.longname);
499 } else if (GST_IS_TYPE_FIND_FACTORY (feature)) {
500 GstBinaryTypeFindFactory *tff;
501 GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (feature);
504 /* we copy the caps here so we can simplify them before saving. This is a lot
505 * faster when loading them later on */
506 GstCaps *copy = gst_caps_copy (factory->caps);
508 tff = g_malloc0 (sizeof (GstBinaryTypeFindFactory));
510 gst_registry_binary_make_data (tff, sizeof (GstBinaryTypeFindFactory));
511 tff->nextensions = 0;
512 pf = (GstBinaryPluginFeature *) tff;
514 /* save extensions */
515 if (factory->extensions) {
516 while (factory->extensions[tff->nextensions]) {
517 gst_registry_binary_save_const_string (list,
518 factory->extensions[tff->nextensions++]);
522 gst_caps_do_simplify (copy);
523 str = gst_caps_to_string (copy);
524 gst_caps_unref (copy);
525 gst_registry_binary_save_string (list, str);
526 } else if (GST_IS_INDEX_FACTORY (feature)) {
527 GstIndexFactory *factory = GST_INDEX_FACTORY (feature);
529 pf = g_malloc0 (sizeof (GstBinaryPluginFeature));
530 chk = gst_registry_binary_make_data (pf, sizeof (GstBinaryPluginFeature));
531 pf->rank = feature->rank;
533 /* pack element factory strings */
534 gst_registry_binary_save_const_string (list, factory->longdesc);
536 GST_WARNING ("unhandled feature type '%s'", type_name);
540 pf->rank = feature->rank;
541 *list = g_list_prepend (*list, chk);
543 /* pack plugin feature strings */
544 gst_registry_binary_save_const_string (list, feature->name);
545 gst_registry_binary_save_const_string (list, (gchar *) type_name);
558 gst_registry_binary_save_plugin_dep (GList ** list, GstPluginDep * dep)
564 ed = g_new0 (GstBinaryDep, 1);
565 chk = gst_registry_binary_make_data (ed, sizeof (GstBinaryDep));
567 ed->flags = dep->flags;
572 ed->env_hash = dep->env_hash;
573 ed->stat_hash = dep->stat_hash;
575 for (s = dep->env_vars; s != NULL && *s != NULL; ++s, ++ed->n_env_vars)
576 gst_registry_binary_save_string (list, g_strdup (*s));
578 for (s = dep->paths; s != NULL && *s != NULL; ++s, ++ed->n_paths)
579 gst_registry_binary_save_string (list, g_strdup (*s));
581 for (s = dep->names; s != NULL && *s != NULL; ++s, ++ed->n_names)
582 gst_registry_binary_save_string (list, g_strdup (*s));
584 *list = g_list_prepend (*list, chk);
586 GST_LOG ("Saved external plugin dependency");
591 * gst_registry_binary_save_plugin:
593 * Adapt a GstPlugin to our GstBinaryPluginElement structure, and write it to
597 gst_registry_binary_save_plugin (GList ** list, GstRegistry * registry,
600 GstBinaryPluginElement *pe;
602 GList *plugin_features = NULL;
605 pe = g_malloc0 (sizeof (GstBinaryPluginElement));
606 chk = gst_registry_binary_make_data (pe, sizeof (GstBinaryPluginElement));
608 pe->file_size = plugin->file_size;
609 pe->file_mtime = plugin->file_mtime;
613 /* pack external deps */
614 for (walk = plugin->priv->deps; walk != NULL; walk = walk->next) {
615 if (!gst_registry_binary_save_plugin_dep (list, walk->data)) {
616 GST_ERROR ("Could not save external plugin dependency, aborting.");
622 /* pack plugin features */
624 gst_registry_get_feature_list_by_plugin (registry, plugin->desc.name);
625 for (walk = plugin_features; walk; walk = g_list_next (walk), pe->nfeatures++) {
626 GstPluginFeature *feature = GST_PLUGIN_FEATURE (walk->data);
628 if (!gst_registry_binary_save_feature (list, feature)) {
629 GST_ERROR ("Can't fill plugin feature, aborting.");
633 GST_DEBUG ("Save plugin '%s' with %d feature(s)", plugin->desc.name,
636 gst_plugin_feature_list_free (plugin_features);
638 /* pack plugin element strings */
639 gst_registry_binary_save_const_string (list, plugin->desc.origin);
640 gst_registry_binary_save_const_string (list, plugin->desc.package);
641 gst_registry_binary_save_const_string (list, plugin->desc.source);
642 gst_registry_binary_save_const_string (list, plugin->desc.license);
643 gst_registry_binary_save_const_string (list, plugin->desc.version);
644 gst_registry_binary_save_const_string (list, plugin->filename);
645 gst_registry_binary_save_const_string (list, plugin->desc.description);
646 gst_registry_binary_save_const_string (list, plugin->desc.name);
648 *list = g_list_prepend (*list, chk);
650 GST_DEBUG ("Found %d features in plugin \"%s\"", pe->nfeatures,
656 gst_plugin_feature_list_free (plugin_features);
663 * gst_registry_binary_write_cache:
664 * @registry: a #GstRegistry
665 * @location: a filename
667 * Write the @registry to a cache to file at given @location.
669 * Returns: %TRUE on success.
672 gst_registry_binary_write_cache (GstRegistry * registry, const char *location)
675 GstBinaryRegistryMagic magic;
676 GList *to_write = NULL;
677 unsigned long file_position = 0;
678 BinaryRegistryCache *cache;
680 GST_INFO ("Building binary registry cache image");
682 g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
684 if (!gst_registry_binary_initialize_magic (&magic))
687 /* iterate trough the list of plugins and fit them into binary structures */
688 for (walk = registry->plugins; walk; walk = g_list_next (walk)) {
689 GstPlugin *plugin = GST_PLUGIN (walk->data);
691 if (!plugin->filename)
694 if (plugin->flags & GST_PLUGIN_FLAG_CACHED) {
698 if ((ret = g_stat (plugin->filename, &statbuf)) < 0 ||
699 plugin->file_mtime != statbuf.st_mtime ||
700 plugin->file_size != statbuf.st_size)
704 if (!gst_registry_binary_save_plugin (&to_write, registry, plugin)) {
705 GST_ERROR ("Can't write binary plugin information for \"%s\"",
710 GST_INFO ("Writing binary registry cache");
712 cache = gst_registry_binary_cache_init (registry, location);
717 if (gst_registry_binary_cache_write (registry, cache, file_position,
718 &magic, sizeof (GstBinaryRegistryMagic)) !=
719 sizeof (GstBinaryRegistryMagic)) {
720 GST_ERROR ("Failed to write binary registry magic");
723 file_position += sizeof (GstBinaryRegistryMagic);
725 /* write out data chunks */
726 for (walk = to_write; walk; walk = g_list_next (walk)) {
727 GstBinaryChunk *cur = walk->data;
729 if (!gst_registry_binary_write_chunk (registry, cache, cur->data, cur->size,
730 &file_position, cur->align)) {
731 if (!(cur->flags & GST_BINARY_REGISTRY_FLAG_CONST))
737 if (!(cur->flags & GST_BINARY_REGISTRY_FLAG_CONST))
742 g_list_free (to_write);
744 if (!gst_registry_binary_cache_finish (registry, cache, TRUE))
752 for (walk = to_write; walk; walk = g_list_next (walk)) {
753 GstBinaryChunk *cur = walk->data;
755 if (!(cur->flags & GST_BINARY_REGISTRY_FLAG_CONST))
759 g_list_free (to_write);
762 (void) gst_registry_binary_cache_finish (registry, cache, FALSE);
772 /* Registry loading */
775 * gst_registry_binary_check_magic:
777 * Check GstBinaryRegistryMagic validity.
778 * Return < 0 if something is wrong, -2 means
779 * that just the version of the registry is out of
780 * date, -1 is a general failure.
783 gst_registry_binary_check_magic (gchar ** in, gsize size)
785 GstBinaryRegistryMagic *m;
788 GST_DEBUG ("Reading/casting for GstBinaryRegistryMagic at address %p", *in);
790 if (size < sizeof (GstBinaryRegistryMagic)) {
791 GST_WARNING ("Not enough data for binary registry magic structure");
794 unpack_element (*in, m, GstBinaryRegistryMagic);
796 if (strncmp (m->magic, GST_MAGIC_BINARY_REGISTRY_STR,
797 GST_MAGIC_BINARY_REGISTRY_LEN) != 0) {
799 ("Binary registry magic is different : %02x%02x%02x%02x != %02x%02x%02x%02x",
800 GST_MAGIC_BINARY_REGISTRY_STR[0] & 0xff,
801 GST_MAGIC_BINARY_REGISTRY_STR[1] & 0xff,
802 GST_MAGIC_BINARY_REGISTRY_STR[2] & 0xff,
803 GST_MAGIC_BINARY_REGISTRY_STR[3] & 0xff, m->magic[0] & 0xff,
804 m->magic[1] & 0xff, m->magic[2] & 0xff, m->magic[3] & 0xff);
807 if (strncmp (m->version, GST_MAGIC_BINARY_VERSION_STR,
808 GST_MAGIC_BINARY_VERSION_LEN)) {
809 GST_WARNING ("Binary registry magic version is different : %s != %s",
810 GST_MAGIC_BINARY_VERSION_STR, m->version);
819 * gst_registry_binary_load_pad_template:
821 * Make a new GstStaticPadTemplate from current GstBinaryPadTemplate structure
823 * Returns: new GstStaticPadTemplate
826 gst_registry_binary_load_pad_template (GstElementFactory * factory, gchar ** in)
828 GstBinaryPadTemplate *pt;
829 GstStaticPadTemplate *template;
832 GST_DEBUG ("Reading/casting for GstBinaryPadTemplate at address %p", *in);
833 unpack_element (*in, pt, GstBinaryPadTemplate);
835 template = g_new0 (GstStaticPadTemplate, 1);
836 template->presence = pt->presence;
837 template->direction = pt->direction;
839 /* unpack pad template strings */
840 unpack_const_string (*in, template->name_template);
841 unpack_string (*in, template->static_caps.string);
843 __gst_element_factory_add_static_pad_template (factory, template);
844 GST_DEBUG ("Added pad_template %s", template->name_template);
851 * gst_registry_binary_load_feature:
853 * Make a new GstPluginFeature from current binary plugin feature structure
855 * Returns: new GstPluginFeature
858 gst_registry_binary_load_feature (GstRegistry * registry, gchar ** in,
859 const gchar * plugin_name)
861 GstBinaryPluginFeature *pf = NULL;
862 GstPluginFeature *feature;
863 gchar *type_name = NULL, *str;
867 /* unpack plugin feature strings */
868 unpack_string (*in, type_name);
871 GST_ERROR ("No feature type name");
875 GST_DEBUG ("Plugin '%s' feature typename : '%s'", plugin_name, type_name);
877 if (!(type = g_type_from_name (type_name))) {
878 GST_ERROR ("Unknown type from typename '%s' for plugin '%s'", type_name,
883 if ((feature = g_object_new (type, NULL)) == NULL) {
884 GST_ERROR ("Can't create feature from type");
889 if (!GST_IS_PLUGIN_FEATURE (feature)) {
890 GST_ERROR ("typename : '%s' is not a plugin feature", type_name);
894 /* unpack more plugin feature strings */
895 unpack_string (*in, feature->name);
897 if (GST_IS_ELEMENT_FACTORY (feature)) {
898 GstBinaryElementFactory *ef;
899 GstElementFactory *factory = GST_ELEMENT_FACTORY (feature);
902 GST_LOG ("Reading/casting for GstBinaryElementFactory at address %p", *in);
903 unpack_element (*in, ef, GstBinaryElementFactory);
904 pf = (GstBinaryPluginFeature *) ef;
906 /* unpack element factory strings */
907 unpack_string (*in, factory->details.longname);
908 unpack_string (*in, factory->details.klass);
909 unpack_string (*in, factory->details.description);
910 unpack_string (*in, factory->details.author);
911 GST_DEBUG ("Element factory : '%s' with npadtemplates=%d",
912 factory->details.longname, ef->npadtemplates);
914 /* load pad templates */
915 for (i = 0; i < ef->npadtemplates; i++) {
916 if (!gst_registry_binary_load_pad_template (factory, in)) {
917 GST_ERROR ("Error while loading binary pad template");
923 if (ef->nuriprotocols) {
924 GST_DEBUG ("Reading %d UriTypes at address %p", ef->nuriprotocols, *in);
927 factory->uri_type = *((guint *) * in);
928 *in += sizeof (factory->uri_type);
929 //unpack_element(*in, &factory->uri_type, factory->uri_type);
931 factory->uri_protocols = g_new0 (gchar *, ef->nuriprotocols + 1);
932 for (i = 0; i < ef->nuriprotocols; i++) {
933 unpack_string (*in, str);
934 factory->uri_protocols[i] = str;
937 /* load interfaces */
938 GST_DEBUG ("Reading %d Interfaces at address %p", ef->ninterfaces, *in);
939 for (i = 0; i < ef->ninterfaces; i++) {
940 unpack_string (*in, str);
941 __gst_element_factory_add_interface (factory, str);
944 } else if (GST_IS_TYPE_FIND_FACTORY (feature)) {
945 GstBinaryTypeFindFactory *tff;
946 GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (feature);
949 GST_DEBUG ("Reading/casting for GstBinaryPluginFeature at address %p", *in);
950 unpack_element (*in, tff, GstBinaryTypeFindFactory);
951 pf = (GstBinaryPluginFeature *) tff;
954 unpack_string (*in, str);
955 factory->caps = gst_caps_from_string (str);
957 /* load extensions */
958 if (tff->nextensions) {
959 GST_DEBUG ("Reading %d Typefind extensions at address %p",
960 tff->nextensions, *in);
961 factory->extensions = g_new0 (gchar *, tff->nextensions + 1);
962 for (i = 0; i < tff->nextensions; i++) {
963 unpack_string (*in, str);
964 factory->extensions[i] = str;
967 } else if (GST_IS_INDEX_FACTORY (feature)) {
968 GstIndexFactory *factory = GST_INDEX_FACTORY (feature);
971 GST_DEBUG ("Reading/casting for GstBinaryPluginFeature at address %p", *in);
972 unpack_element (*in, pf, GstBinaryPluginFeature);
974 /* unpack index factory strings */
975 unpack_string (*in, factory->longdesc);
977 GST_WARNING ("unhandled factory type : %s", G_OBJECT_TYPE_NAME (feature));
981 feature->rank = pf->rank;
983 /* should already be the interned string, but better make sure */
984 feature->plugin_name = g_intern_string (plugin_name);
986 gst_registry_add_feature (registry, feature);
987 GST_DEBUG ("Added feature %s", feature->name);
995 if (GST_IS_OBJECT (feature))
996 gst_object_unref (feature);
998 g_object_unref (feature);
1003 gst_registry_binary_load_plugin_dep_strv (gchar ** in, guint n)
1010 arr = g_new0 (gchar *, n + 1);
1012 unpack_string (*in, arr[n - 1]);
1019 gst_registry_binary_load_plugin_dep (GstPlugin * plugin, gchar ** in)
1026 GST_LOG_OBJECT (plugin, "Unpacking GstBinaryDep from %p", *in);
1027 unpack_element (*in, d, GstBinaryDep);
1029 dep = g_new0 (GstPluginDep, 1);
1031 dep->env_hash = d->env_hash;
1032 dep->stat_hash = d->stat_hash;
1034 dep->flags = d->flags;
1036 dep->names = gst_registry_binary_load_plugin_dep_strv (in, d->n_names);
1037 dep->paths = gst_registry_binary_load_plugin_dep_strv (in, d->n_paths);
1038 dep->env_vars = gst_registry_binary_load_plugin_dep_strv (in, d->n_env_vars);
1040 plugin->priv->deps = g_list_append (plugin->priv->deps, dep);
1042 GST_DEBUG_OBJECT (plugin, "Loaded external plugin dependency from registry: "
1043 "env_hash: %08x, stat_hash: %08x", dep->env_hash, dep->stat_hash);
1044 for (s = dep->env_vars; s != NULL && *s != NULL; ++s)
1045 GST_LOG_OBJECT (plugin, " evar: %s", *s);
1046 for (s = dep->paths; s != NULL && *s != NULL; ++s)
1047 GST_LOG_OBJECT (plugin, " path: %s", *s);
1048 for (s = dep->names; s != NULL && *s != NULL; ++s)
1049 GST_LOG_OBJECT (plugin, " name: %s", *s);
1055 * gst_registry_binary_load_plugin:
1057 * Make a new GstPlugin from current GstBinaryPluginElement structure
1058 * and save it to the GstRegistry. Return an offset to the next
1059 * GstBinaryPluginElement structure.
1062 gst_registry_binary_load_plugin (GstRegistry * registry, gchar ** in)
1064 GstBinaryPluginElement *pe;
1065 GstPlugin *plugin = NULL;
1069 GST_LOG ("Reading/casting for GstBinaryPluginElement at address %p", *in);
1070 unpack_element (*in, pe, GstBinaryPluginElement);
1072 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
1074 /* TODO: also set GST_PLUGIN_FLAG_CONST */
1075 plugin->flags |= GST_PLUGIN_FLAG_CACHED;
1076 plugin->file_mtime = pe->file_mtime;
1077 plugin->file_size = pe->file_size;
1079 /* unpack plugin element strings */
1080 unpack_const_string (*in, plugin->desc.name);
1081 unpack_string (*in, plugin->desc.description);
1082 unpack_string (*in, plugin->filename);
1083 unpack_const_string (*in, plugin->desc.version);
1084 unpack_const_string (*in, plugin->desc.license);
1085 unpack_const_string (*in, plugin->desc.source);
1086 unpack_const_string (*in, plugin->desc.package);
1087 unpack_const_string (*in, plugin->desc.origin);
1088 GST_LOG ("read strings for name='%s'", plugin->desc.name);
1089 GST_LOG (" desc.description='%s'", plugin->desc.description);
1090 GST_LOG (" filename='%s'", plugin->filename);
1091 GST_LOG (" desc.version='%s'", plugin->desc.version);
1092 GST_LOG (" desc.license='%s'", plugin->desc.license);
1093 GST_LOG (" desc.source='%s'", plugin->desc.source);
1094 GST_LOG (" desc.package='%s'", plugin->desc.package);
1095 GST_LOG (" desc.origin='%s'", plugin->desc.origin);
1097 plugin->basename = g_path_get_basename (plugin->filename);
1099 /* Takes ownership of plugin */
1100 gst_registry_add_plugin (registry, plugin);
1101 GST_DEBUG ("Added plugin '%s' plugin with %d features from binary registry",
1102 plugin->desc.name, pe->nfeatures);
1104 /* Load plugin features */
1105 for (i = 0; i < pe->nfeatures; i++) {
1106 if (!gst_registry_binary_load_feature (registry, in, plugin->desc.name)) {
1107 GST_ERROR ("Error while loading binary feature");
1112 /* Load external plugin dependencies */
1113 for (i = 0; i < pe->n_deps; ++i) {
1114 if (!gst_registry_binary_load_plugin_dep (plugin, in)) {
1115 GST_ERROR_OBJECT (plugin, "Could not read external plugin dependency");
1129 * gst_registry_binary_read_cache:
1130 * @registry: a #GstRegistry
1131 * @location: a filename
1133 * Read the contents of the binary cache file at @location into @registry.
1135 * Returns: %TRUE on success.
1138 gst_registry_binary_read_cache (GstRegistry * registry, const char *location)
1140 GMappedFile *mapped = NULL;
1141 gchar *contents = NULL;
1145 gboolean res = FALSE;
1146 gint check_magic_result;
1147 #ifndef GST_DISABLE_GST_DEBUG
1148 GTimer *timer = NULL;
1152 /* make sure these types exist */
1153 GST_TYPE_ELEMENT_FACTORY;
1154 GST_TYPE_TYPE_FIND_FACTORY;
1155 GST_TYPE_INDEX_FACTORY;
1157 #ifndef GST_DISABLE_GST_DEBUG
1158 timer = g_timer_new ();
1161 mapped = g_mapped_file_new (location, FALSE, &err);
1163 GST_INFO ("Unable to mmap file %s : %s", location, err->message);
1167 g_file_get_contents (location, &contents, &size, &err);
1169 GST_INFO ("Unable to read file %s : %s", location, err->message);
1170 #ifndef GST_DISABLE_GST_DEBUG
1171 g_timer_destroy (timer);
1177 if ((contents = g_mapped_file_get_contents (mapped)) == NULL) {
1178 GST_ERROR ("Can't load file %s : %s", location, g_strerror (errno));
1181 /* check length for header */
1182 size = g_mapped_file_get_length (mapped);
1184 /* in is a cursor pointer, we initialize it with the begin of registry and is updated on each read */
1186 GST_DEBUG ("File data at address %p", in);
1187 if (size < sizeof (GstBinaryRegistryMagic)) {
1188 GST_ERROR ("No or broken registry header");
1191 /* check if header is valid */
1192 if ((check_magic_result = gst_registry_binary_check_magic (&in, size)) < 0) {
1194 if (check_magic_result == -1)
1196 ("Binary registry type not recognized (invalid magic) for file at %s",
1201 /* check if there are plugins in the file */
1203 if (!(((gsize) in + sizeof (GstBinaryPluginElement)) <
1204 (gsize) contents + size)) {
1205 GST_INFO ("No binary plugins structure to read");
1206 /* empty file, this is not an error */
1209 ((gsize) in + sizeof (GstBinaryPluginElement)) <
1210 (gsize) contents + size;) {
1211 GST_DEBUG ("reading binary registry %" G_GSIZE_FORMAT "(%x)/%"
1212 G_GSIZE_FORMAT, (gsize) in - (gsize) contents,
1213 (guint) ((gsize) in - (gsize) contents), size);
1214 if (!gst_registry_binary_load_plugin (registry, &in)) {
1215 GST_ERROR ("Problem while reading binary registry");
1221 #ifndef GST_DISABLE_GST_DEBUG
1222 g_timer_stop (timer);
1223 seconds = g_timer_elapsed (timer, NULL);
1226 GST_INFO ("loaded %s in %lf seconds", location, seconds);
1229 /* TODO: once we re-use the pointers to registry contents return here */
1232 #ifndef GST_DISABLE_GST_DEBUG
1233 g_timer_destroy (timer);
1236 g_mapped_file_free (mapped);
1244 /* FIXME 0.11: these are here for backwards compatibility */
1247 gst_registry_xml_read_cache (GstRegistry * registry, const char *location)
1253 gst_registry_xml_write_cache (GstRegistry * registry, const char *location)