2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 * 2005 David A. Schleef <ds@schleef.org>
6 * gstregistryxml.c: GstRegistry object, support routines
8 * This library is free software; you can redistribute it and/or
9 * modify it ulnder the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
30 #include <sys/types.h>
38 #include <gst/gst_private.h>
39 #include <gst/gstelement.h>
40 #include <gst/gsttypefind.h>
41 #include <gst/gsttypefindfactory.h>
42 #include <gst/gsturi.h>
43 #include <gst/gstinfo.h>
44 #include <gst/gstenumtypes.h>
45 #include <gst/gstregistry.h>
47 #include <libxml/xmlreader.h>
49 #include "glib-compat-private.h"
50 #include <glib/gstdio.h>
52 #define BLOCK_SIZE 1024*10
54 #define GST_CAT_DEFAULT GST_CAT_REGISTRY
56 #define CLASS(registry) GST_XML_REGISTRY_CLASS (G_OBJECT_GET_CLASS (registry))
59 gst_registry_save (GstRegistry * registry, gchar * format, ...)
66 va_start (var_args, format);
67 str = g_strdup_vprintf (format, var_args);
72 written = write (registry->cache_file, str, len);
78 GST_ERROR ("Failed to write registry to temporary file: %s",
88 add_to_char_array (gchar *** array, gchar * value)
94 /* expensive, but cycles are cheap... */
98 new = g_new0 (gchar *, i + 2);
108 /* read a string and copy it into the given location */
110 read_string (xmlTextReaderPtr reader, gchar ** write_to, gboolean allow_blank)
112 int depth = xmlTextReaderDepth (reader);
113 gboolean found = FALSE;
115 while (xmlTextReaderRead (reader) == 1) {
116 if (xmlTextReaderDepth (reader) == depth) {
117 if (allow_blank && !found &&
118 xmlTextReaderNodeType (reader) == XML_READER_TYPE_END_ELEMENT) {
119 /* Allow blank strings */
120 *write_to = g_strdup ("");
125 if (xmlTextReaderNodeType (reader) == XML_READER_TYPE_TEXT) {
128 *write_to = g_strdup ((gchar *) xmlTextReaderConstValue (reader));
136 read_uint (xmlTextReaderPtr reader, guint * write_to)
138 int depth = xmlTextReaderDepth (reader);
139 gboolean found = FALSE;
141 while (xmlTextReaderRead (reader) == 1) {
142 if (xmlTextReaderDepth (reader) == depth)
144 if (xmlTextReaderNodeType (reader) == XML_READER_TYPE_TEXT) {
149 GST_DEBUG ("failed to read uint, multiple text nodes");
152 s = (const gchar *) xmlTextReaderConstValue (reader);
153 *write_to = strtol (s, &ret, 0);
155 GST_DEBUG ("failed to read uint, text didn't convert to int");
161 GST_DEBUG ("failed to read uint, no text node");
166 read_enum (xmlTextReaderPtr reader, GType enum_type, guint * write_to)
168 int depth = xmlTextReaderDepth (reader);
169 gboolean found = FALSE;
173 while (xmlTextReaderRead (reader) == 1) {
174 if (xmlTextReaderDepth (reader) == depth)
176 if (xmlTextReaderNodeType (reader) == XML_READER_TYPE_TEXT) {
177 GEnumClass *enum_class;
182 enum_class = g_type_class_ref (enum_type);
186 g_enum_get_value_by_nick (enum_class,
187 (gchar *) xmlTextReaderConstValue (reader));
189 *write_to = value->value;
192 g_type_class_unref (enum_class);
198 static GstStaticPadTemplate *
199 load_pad_template (xmlTextReaderPtr reader)
202 int depth = xmlTextReaderDepth (reader);
203 gchar *name = NULL, *caps_str = NULL;
204 guint direction = 0, presence = 0;
206 while ((ret = xmlTextReaderRead (reader)) == 1) {
207 if (xmlTextReaderDepth (reader) == depth) {
208 GstStaticPadTemplate *template;
210 template = g_new0 (GstStaticPadTemplate, 1);
211 template->name_template = name;
212 template->presence = presence;
213 template->direction = direction;
214 template->static_caps.string = caps_str;
218 if (xmlTextReaderNodeType (reader) == XML_READER_TYPE_ELEMENT &&
219 xmlTextReaderDepth (reader) == depth + 1) {
220 const gchar *tag = (gchar *) xmlTextReaderConstName (reader);
222 if (g_str_equal (tag, "nametemplate")) {
223 read_string (reader, &name, FALSE);
224 } else if (g_str_equal (tag, "direction")) {
225 read_enum (reader, GST_TYPE_PAD_DIRECTION, &direction);
226 } else if (g_str_equal (tag, "presence")) {
227 read_enum (reader, GST_TYPE_PAD_PRESENCE, &presence);
228 } else if (!strncmp (tag, "caps", 4)) {
229 read_string (reader, &caps_str, FALSE);
239 static GstPluginFeature *
240 load_feature (xmlTextReaderPtr reader)
245 GstPluginFeature *feature;
248 depth = xmlTextReaderDepth (reader);
250 (gchar *) xmlTextReaderGetAttribute (reader, BAD_CAST "typename");
252 GST_DEBUG ("loading feature");
257 type = g_type_from_name (feature_name);
258 g_free (feature_name);
264 feature = g_object_new (type, NULL);
268 if (!GST_IS_PLUGIN_FEATURE (feature)) {
269 /* don't really know what it is */
270 if (GST_IS_OBJECT (feature))
271 gst_object_unref (feature);
273 g_object_unref (feature);
276 while ((ret = xmlTextReaderRead (reader)) == 1) {
277 if (xmlTextReaderDepth (reader) == depth) {
278 GST_DEBUG ("loaded feature %p with name %s", feature, feature->name);
281 if (xmlTextReaderNodeType (reader) == XML_READER_TYPE_ELEMENT &&
282 xmlTextReaderDepth (reader) == depth + 1) {
283 const gchar *tag = (gchar *) xmlTextReaderConstName (reader);
285 if (g_str_equal (tag, "name"))
286 read_string (reader, &feature->name, FALSE);
287 else if (g_str_equal (tag, "rank"))
288 read_uint (reader, &feature->rank);
290 if (GST_IS_ELEMENT_FACTORY (feature)) {
291 GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (feature);
293 if (g_str_equal (tag, "longname")) {
296 ret = read_string (reader, &factory->details.longname, TRUE);
297 GST_DEBUG ("longname ret=%d, name=%s",
298 ret, factory->details.longname);
299 } else if (g_str_equal (tag, "class")) {
300 read_string (reader, &factory->details.klass, TRUE);
301 } else if (g_str_equal (tag, "description")) {
302 read_string (reader, &factory->details.description, TRUE);
303 } else if (g_str_equal (tag, "author")) {
304 read_string (reader, &factory->details.author, TRUE);
305 } else if (g_str_equal (tag, "uri_type")) {
308 if (read_string (reader, &s, FALSE)) {
309 if (g_ascii_strncasecmp (s, "sink", 4) == 0) {
310 factory->uri_type = GST_URI_SINK;
311 } else if (g_ascii_strncasecmp (s, "source", 5) == 0) {
312 factory->uri_type = GST_URI_SRC;
316 } else if (g_str_equal (tag, "uri_protocol")) {
319 if (read_string (reader, &s, FALSE))
320 add_to_char_array (&factory->uri_protocols, s);
321 } else if (g_str_equal (tag, "interface")) {
324 if (read_string (reader, &s, FALSE)) {
325 __gst_element_factory_add_interface (factory, s);
326 /* add_interface strdup's s */
329 } else if (g_str_equal (tag, "padtemplate")) {
330 GstStaticPadTemplate *template = load_pad_template (reader);
333 GST_LOG ("adding template %s to factory %s",
334 GST_STR_NULL (GST_PAD_TEMPLATE_NAME_TEMPLATE (template)),
335 GST_PLUGIN_FEATURE_NAME (feature));
336 __gst_element_factory_add_static_pad_template (factory, template);
339 } else if (GST_IS_TYPE_FIND_FACTORY (feature)) {
340 GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (feature);
342 if (g_str_equal (tag, "extension")) {
345 if (read_string (reader, &s, TRUE))
346 add_to_char_array (&factory->extensions, s);
347 } else if (g_str_equal (tag, "caps")) {
350 if (read_string (reader, &s, FALSE)) {
351 factory->caps = gst_caps_from_string (s);
355 } else if (GST_IS_INDEX_FACTORY (feature)) {
356 GstIndexFactory *factory = GST_INDEX_FACTORY (feature);
358 if (g_str_equal (tag, "longdesc"))
359 read_string (reader, &factory->longdesc, TRUE);
364 GST_WARNING ("Error reading feature from registry: registry corrupt?");
369 load_plugin (xmlTextReaderPtr reader, GList ** feature_list)
374 *feature_list = NULL;
376 GST_DEBUG ("creating new plugin and parsing");
378 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
380 plugin->flags |= GST_PLUGIN_FLAG_CACHED;
381 while ((ret = xmlTextReaderRead (reader)) == 1) {
382 if (xmlTextReaderDepth (reader) == 1) {
385 if (xmlTextReaderNodeType (reader) == XML_READER_TYPE_ELEMENT &&
386 xmlTextReaderDepth (reader) == 2) {
387 const gchar *tag = (gchar *) xmlTextReaderConstName (reader);
389 if (g_str_equal (tag, "name")) {
392 ret = read_string (reader, &plugin->desc.name, FALSE);
393 GST_DEBUG ("name ret=%d, name=%s", ret, plugin->desc.name);
396 } else if (g_str_equal (tag, "description")) {
397 if (!read_string (reader, &plugin->desc.description, TRUE)) {
398 GST_DEBUG ("description field was invalid in registry");
401 GST_DEBUG ("description %s", plugin->desc.description);
402 } else if (g_str_equal (tag, "filename")) {
403 if (!read_string (reader, &plugin->filename, FALSE)) {
404 GST_DEBUG ("filename field was invalid in registry");
407 GST_DEBUG ("filename %s", plugin->filename);
408 plugin->basename = g_path_get_basename (plugin->filename);
409 } else if (g_str_equal (tag, "version")) {
410 if (!read_string (reader, &plugin->desc.version, TRUE)) {
411 GST_DEBUG ("version field was invalid in registry");
414 GST_DEBUG ("version %s", plugin->desc.version);
415 } else if (g_str_equal (tag, "license")) {
416 if (!read_string (reader, &plugin->desc.license, TRUE)) {
417 GST_DEBUG ("license field was invalid in registry");
420 GST_DEBUG ("license %s", plugin->desc.license);
421 } else if (g_str_equal (tag, "source")) {
422 if (!read_string (reader, &plugin->desc.source, TRUE)) {
423 GST_DEBUG ("source field was invalid in registry");
426 GST_DEBUG ("source %s", plugin->desc.source);
427 } else if (g_str_equal (tag, "package")) {
428 if (!read_string (reader, &plugin->desc.package, TRUE)) {
429 GST_DEBUG ("package field was invalid in registry");
432 GST_DEBUG ("package %s", plugin->desc.package);
433 } else if (g_str_equal (tag, "origin")) {
434 if (!read_string (reader, &plugin->desc.origin, TRUE)) {
435 GST_DEBUG ("failed to read origin");
438 } else if (g_str_equal (tag, "m32p")) {
441 if (!read_string (reader, &s, FALSE)) {
442 GST_DEBUG ("failed to read mtime");
445 plugin->file_mtime = strtol (s, NULL, 0);
446 GST_DEBUG ("mtime %d", (int) plugin->file_mtime);
448 } else if (g_str_equal (tag, "size")) {
451 if (read_uint (reader, &x)) {
452 plugin->file_size = x;
453 GST_DEBUG ("file_size %d", plugin->file_size);
455 GST_DEBUG ("failed to read size");
457 } else if (g_str_equal (tag, "feature")) {
458 GstPluginFeature *feature = load_feature (reader);
461 feature->plugin_name = g_strdup (plugin->desc.name);
462 *feature_list = g_list_prepend (*feature_list, feature);
465 GST_DEBUG ("unknown tag %s", tag);
469 gst_object_unref (plugin);
471 GST_DEBUG ("problem reading plugin");
477 * gst_registry_xml_read_cache:
478 * @registry: a #GstRegistry
479 * @location: a filename
481 * Read the contents of the XML cache file at location
482 * @location into @registry.
484 * Returns: TRUE on success.
487 gst_registry_xml_read_cache (GstRegistry * registry, const char *location)
489 GMappedFile *mapped = NULL;
492 xmlTextReaderPtr reader = NULL;
494 gboolean in_registry = FALSE;
497 /* make sure these types exist */
498 GST_TYPE_ELEMENT_FACTORY;
499 GST_TYPE_TYPE_FIND_FACTORY;
500 GST_TYPE_INDEX_FACTORY;
502 timer = g_timer_new ();
504 mapped = g_mapped_file_new (location, FALSE, NULL);
506 reader = xmlReaderForMemory (g_mapped_file_get_contents (mapped),
507 g_mapped_file_get_length (mapped), NULL, NULL, 0);
508 if (reader == NULL) {
509 g_mapped_file_free (mapped);
514 if (reader == NULL) {
515 file = fopen (location, "r");
517 g_timer_destroy (timer);
521 reader = xmlReaderForFd (fileno (file), NULL, NULL, 0);
524 g_timer_destroy (timer);
529 while ((ret = xmlTextReaderRead (reader)) == 1) {
530 if (xmlTextReaderDepth (reader) == 0) {
531 in_registry = xmlTextReaderNodeType (reader) == XML_READER_TYPE_ELEMENT &&
532 g_str_equal ("GST-PluginRegistry", xmlTextReaderConstName (reader));
533 } else if (in_registry) {
534 if (xmlTextReaderDepth (reader) == 1 &&
535 xmlTextReaderNodeType (reader) == XML_READER_TYPE_ELEMENT) {
536 const gchar *tag = (const gchar *) xmlTextReaderConstName (reader);
538 if (g_str_equal (tag, "plugin")) {
540 GstPlugin *plugin = load_plugin (reader, &feature_list);
545 GST_DEBUG ("adding plugin %s", plugin->desc.name);
546 gst_registry_add_plugin (registry, plugin);
547 for (g = feature_list; g; g = g_list_next (g)) {
548 gst_registry_add_feature (registry,
549 GST_PLUGIN_FEATURE_CAST (g->data));
551 g_list_free (feature_list);
557 xmlFreeTextReader (reader);
559 GST_ERROR ("parsing registry cache: %s", location);
561 g_mapped_file_free (mapped);
564 g_timer_destroy (timer);
568 g_timer_stop (timer);
569 seconds = g_timer_elapsed (timer, NULL);
570 g_timer_destroy (timer);
572 GST_INFO ("loaded %s in %f seconds", location, seconds);
575 g_mapped_file_free (mapped);
587 gst_registry_save_escaped (GstRegistry * registry, char *prefix, char *tag,
593 gchar *v = g_markup_escape_text (value, strlen (value));
595 ret = gst_registry_save (registry, "%s<%s>%s</%s>\n", prefix, tag, v, tag);
604 gst_registry_xml_save_caps (GstRegistry * registry, const GstCaps * caps)
606 /* we copy the caps here so we can simplify them before saving. This is a lot
607 * faster when loading them later on */
609 GstCaps *copy = gst_caps_copy (caps);
612 gst_caps_do_simplify (copy);
613 s = gst_caps_to_string (copy);
614 gst_caps_unref (copy);
616 ret = gst_registry_save_escaped (registry, " ", "caps", s);
622 gst_registry_xml_save_pad_template (GstRegistry * registry,
623 GstStaticPadTemplate * template)
627 if (!gst_registry_save_escaped (registry, " ", "nametemplate",
628 template->name_template))
631 if (!gst_registry_save (registry,
632 " <direction>%s</direction>\n",
633 (template->direction == GST_PAD_SINK ? "sink" : "src")))
636 switch (template->presence) {
640 case GST_PAD_SOMETIMES:
641 presence = "sometimes";
643 case GST_PAD_REQUEST:
644 presence = "request";
647 presence = "unknown";
650 if (!gst_registry_save (registry, " <presence>%s</presence>\n", presence))
653 if (template->static_caps.string) {
654 if (!gst_registry_save (registry, " <caps>%s</caps>\n",
655 template->static_caps.string))
662 gst_registry_xml_save_feature (GstRegistry * registry,
663 GstPluginFeature * feature)
665 if (!gst_registry_save_escaped (registry, " ", "name", feature->name))
668 if (feature->rank > 0) {
669 gint rank = feature->rank;
671 if (!gst_registry_save (registry, " <rank>%d</rank>\n", rank))
675 if (GST_IS_ELEMENT_FACTORY (feature)) {
676 GstElementFactory *factory = GST_ELEMENT_FACTORY (feature);
679 if (!gst_registry_save_escaped (registry, " ", "longname",
680 factory->details.longname))
682 if (!gst_registry_save_escaped (registry, " ", "class",
683 factory->details.klass))
685 if (!gst_registry_save_escaped (registry, " ", "description",
686 factory->details.description))
688 if (!gst_registry_save_escaped (registry, " ", "author",
689 factory->details.author))
692 walk = factory->staticpadtemplates;
695 GstStaticPadTemplate *template = walk->data;
697 if (!gst_registry_save (registry, " <padtemplate>\n"))
699 if (!gst_registry_xml_save_pad_template (registry, template))
701 if (!gst_registry_save (registry, " </padtemplate>\n"))
704 walk = g_list_next (walk);
707 walk = factory->interfaces;
709 if (!gst_registry_save_escaped (registry, " ", "interface",
710 (gchar *) walk->data))
712 walk = g_list_next (walk);
715 if (GST_URI_TYPE_IS_VALID (factory->uri_type)) {
718 if (!gst_registry_save_escaped (registry, " ", "uri_type",
719 factory->uri_type == GST_URI_SINK ? "sink" : "source"))
721 g_assert (factory->uri_protocols);
722 protocol = factory->uri_protocols;
724 if (!gst_registry_save_escaped (registry, " ", "uri_protocol",
730 } else if (GST_IS_TYPE_FIND_FACTORY (feature)) {
731 GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (feature);
735 if (!gst_registry_xml_save_caps (registry, factory->caps))
738 if (factory->extensions) {
739 while (factory->extensions[i]) {
740 if (!gst_registry_save_escaped (registry, " ", "extension",
741 factory->extensions[i]))
746 } else if (GST_IS_INDEX_FACTORY (feature)) {
747 if (!gst_registry_save_escaped (registry, " ", "longdesc",
748 GST_INDEX_FACTORY (feature)->longdesc))
755 gst_registry_xml_save_plugin (GstRegistry * registry, GstPlugin * plugin)
761 if (!gst_registry_save_escaped (registry, " ", "name", plugin->desc.name))
763 if (!gst_registry_save_escaped (registry, " ", "description",
764 plugin->desc.description))
766 if (!gst_registry_save_escaped (registry, " ", "filename", plugin->filename))
769 sprintf (s, "%d", (int) plugin->file_size);
770 if (!gst_registry_save_escaped (registry, " ", "size", s))
773 sprintf (s, "%d", (int) plugin->file_mtime);
774 if (!gst_registry_save_escaped (registry, " ", "m32p", s))
777 if (!gst_registry_save_escaped (registry, " ", "version",
778 plugin->desc.version))
780 if (!gst_registry_save_escaped (registry, " ", "license",
781 plugin->desc.license))
783 if (!gst_registry_save_escaped (registry, " ", "source", plugin->desc.source))
785 if (!gst_registry_save_escaped (registry, " ", "package",
786 plugin->desc.package))
788 if (!gst_registry_save_escaped (registry, " ", "origin", plugin->desc.origin))
791 list = gst_registry_get_feature_list_by_plugin (registry, plugin->desc.name);
793 for (walk = list; walk; walk = g_list_next (walk)) {
794 GstPluginFeature *feature = GST_PLUGIN_FEATURE (walk->data);
796 if (!gst_registry_save (registry,
797 " <feature typename=\"%s\">\n",
798 g_type_name (G_OBJECT_TYPE (feature))))
800 if (!gst_registry_xml_save_feature (registry, feature))
802 if (!gst_registry_save (registry, " </feature>\n"))
806 gst_plugin_feature_list_free (list);
810 gst_plugin_feature_list_free (list);
816 * gst_registry_xml_write_cache:
817 * @registry: a #GstRegistry
818 * @location: a filename
820 * Write @registry in an XML format at the location given by
821 * @location. Directories are automatically created.
823 * Returns: TRUE on success.
826 gst_registry_xml_write_cache (GstRegistry * registry, const char *location)
831 g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
833 tmp_location = g_strconcat (location, ".tmpXXXXXX", NULL);
834 registry->cache_file = g_mkstemp (tmp_location);
835 if (registry->cache_file == -1) {
838 /* oops, I bet the directory doesn't exist */
839 dir = g_path_get_dirname (location);
840 g_mkdir_with_parents (dir, 0777);
843 /* the previous g_mkstemp call overwrote the XXXXXX placeholder ... */
844 g_free (tmp_location);
845 tmp_location = g_strconcat (location, ".tmpXXXXXX", NULL);
846 registry->cache_file = g_mkstemp (tmp_location);
848 if (registry->cache_file == -1) {
849 GST_DEBUG ("g_mkstemp() failed: %s", g_strerror (errno));
850 g_free (tmp_location);
855 if (!gst_registry_save (registry, "<?xml version=\"1.0\"?>\n"))
857 if (!gst_registry_save (registry, "<GST-PluginRegistry>\n"))
861 for (walk = g_list_last (registry->plugins); walk;
862 walk = g_list_previous (walk)) {
863 GstPlugin *plugin = GST_PLUGIN_CAST (walk->data);
865 if (!plugin->filename)
868 if (plugin->flags & GST_PLUGIN_FLAG_CACHED) {
872 ret = g_stat (plugin->filename, &statbuf);
875 if (plugin->file_mtime != statbuf.st_mtime ||
876 plugin->file_size != statbuf.st_size) {
881 if (!gst_registry_save (registry, "<plugin>\n"))
883 if (!gst_registry_xml_save_plugin (registry, plugin))
885 if (!gst_registry_save (registry, "</plugin>\n"))
888 if (!gst_registry_save (registry, "</GST-PluginRegistry>\n"))
891 close (registry->cache_file);
893 if (g_file_test (tmp_location, G_FILE_TEST_EXISTS)) {
897 g_rename (tmp_location, location);
900 g_free (tmp_location);
905 close (registry->cache_file);
906 g_free (tmp_location);