2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstxml_registry.c: GstXMLRegistry object, support routines
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
29 #include <sys/types.h>
36 #include <gst/gst_private.h>
37 #include <gst/gstelement.h>
38 #include <gst/gsttypefind.h>
39 #include <gst/gstscheduler.h>
40 #include <gst/gsturi.h>
41 #include <gst/gstinfo.h>
43 #include "gstxmlregistry.h"
45 #define BLOCK_SIZE 1024*10
47 #define CLASS(registry) GST_XML_REGISTRY_CLASS (G_OBJECT_GET_CLASS (registry))
57 static void gst_xml_registry_class_init (GstXMLRegistryClass * klass);
58 static void gst_xml_registry_init (GstXMLRegistry * registry);
60 static void gst_xml_registry_set_property (GObject * object, guint prop_id,
61 const GValue * value, GParamSpec * pspec);
62 static void gst_xml_registry_get_property (GObject * object, guint prop_id,
63 GValue * value, GParamSpec * pspec);
65 static gboolean gst_xml_registry_load (GstRegistry * registry);
66 static gboolean gst_xml_registry_save (GstRegistry * registry);
67 static gboolean gst_xml_registry_rebuild (GstRegistry * registry);
69 static void gst_xml_registry_get_perms_func (GstXMLRegistry * registry);
70 static void gst_xml_registry_add_path_list_func (GstXMLRegistry * registry);
71 static gboolean gst_xml_registry_open_func (GstXMLRegistry * registry,
72 GstXMLRegistryMode mode);
73 static gboolean gst_xml_registry_load_func (GstXMLRegistry * registry,
74 gchar * data, gssize * size);
75 static gboolean gst_xml_registry_save_func (GstXMLRegistry * registry,
77 static gboolean gst_xml_registry_close_func (GstXMLRegistry * registry);
79 static GstRegistryReturn gst_xml_registry_load_plugin (GstRegistry * registry,
82 static void gst_xml_registry_start_element (GMarkupParseContext * context,
83 const gchar * element_name,
84 const gchar ** attribute_names,
85 const gchar ** attribute_values, gpointer user_data, GError ** error);
86 static void gst_xml_registry_end_element (GMarkupParseContext * context,
87 const gchar * element_name, gpointer user_data, GError ** error);
88 static void gst_xml_registry_text (GMarkupParseContext * context,
89 const gchar * text, gsize text_len, gpointer user_data, GError ** error);
90 static void gst_xml_registry_passthrough (GMarkupParseContext * context,
91 const gchar * passthrough_text,
92 gsize text_len, gpointer user_data, GError ** error);
93 static void gst_xml_registry_error (GMarkupParseContext * context,
94 GError * error, gpointer user_data);
97 static void gst_xml_registry_paths_start_element (GMarkupParseContext * context,
98 const gchar * element_name,
99 const gchar ** attribute_names,
100 const gchar ** attribute_values, gpointer user_data, GError ** error);
101 static void gst_xml_registry_paths_end_element (GMarkupParseContext * context,
102 const gchar * element_name, gpointer user_data, GError ** error);
103 static void gst_xml_registry_paths_text (GMarkupParseContext * context,
104 const gchar * text, gsize text_len, gpointer user_data, GError ** error);
106 static GstRegistryClass *parent_class = NULL;
108 /* static guint gst_xml_registry_signals[LAST_SIGNAL] = { 0 }; */
110 static const GMarkupParser gst_xml_registry_parser = {
111 gst_xml_registry_start_element,
112 gst_xml_registry_end_element,
113 gst_xml_registry_text,
114 gst_xml_registry_passthrough,
115 gst_xml_registry_error,
118 static const GMarkupParser gst_xml_registry_paths_parser = {
119 gst_xml_registry_paths_start_element,
120 gst_xml_registry_paths_end_element,
121 gst_xml_registry_paths_text,
128 gst_xml_registry_get_type (void)
130 static GType xml_registry_type = 0;
132 if (!xml_registry_type) {
133 static const GTypeInfo xml_registry_info = {
134 sizeof (GstXMLRegistryClass),
137 (GClassInitFunc) gst_xml_registry_class_init,
140 sizeof (GstXMLRegistry),
142 (GInstanceInitFunc) gst_xml_registry_init,
146 xml_registry_type = g_type_register_static (GST_TYPE_REGISTRY,
147 "GstXMLRegistry", &xml_registry_info, 0);
149 return xml_registry_type;
153 gst_xml_registry_class_init (GstXMLRegistryClass * klass)
155 GObjectClass *gobject_class;
156 GstRegistryClass *gstregistry_class;
157 GstXMLRegistryClass *gstxmlregistry_class;
159 gobject_class = (GObjectClass *) klass;
160 gstregistry_class = (GstRegistryClass *) klass;
161 gstxmlregistry_class = (GstXMLRegistryClass *) klass;
163 parent_class = g_type_class_ref (GST_TYPE_REGISTRY);
165 gobject_class->get_property =
166 GST_DEBUG_FUNCPTR (gst_xml_registry_get_property);
167 gobject_class->set_property =
168 GST_DEBUG_FUNCPTR (gst_xml_registry_set_property);
170 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LOCATION,
171 g_param_spec_string ("location", "Location",
172 "Location of the registry file", NULL, G_PARAM_READWRITE));
174 gstregistry_class->load = GST_DEBUG_FUNCPTR (gst_xml_registry_load);
175 gstregistry_class->save = GST_DEBUG_FUNCPTR (gst_xml_registry_save);
176 gstregistry_class->rebuild = GST_DEBUG_FUNCPTR (gst_xml_registry_rebuild);
178 gstregistry_class->load_plugin =
179 GST_DEBUG_FUNCPTR (gst_xml_registry_load_plugin);
181 gstxmlregistry_class->get_perms_func =
182 GST_DEBUG_FUNCPTR (gst_xml_registry_get_perms_func);
183 gstxmlregistry_class->add_path_list_func =
184 GST_DEBUG_FUNCPTR (gst_xml_registry_add_path_list_func);
185 gstxmlregistry_class->open_func =
186 GST_DEBUG_FUNCPTR (gst_xml_registry_open_func);
187 gstxmlregistry_class->load_func =
188 GST_DEBUG_FUNCPTR (gst_xml_registry_load_func);
189 gstxmlregistry_class->save_func =
190 GST_DEBUG_FUNCPTR (gst_xml_registry_save_func);
191 gstxmlregistry_class->close_func =
192 GST_DEBUG_FUNCPTR (gst_xml_registry_close_func);
196 gst_xml_registry_init (GstXMLRegistry * registry)
198 registry->location = NULL;
199 registry->context = NULL;
200 registry->state = GST_XML_REGISTRY_NONE;
201 registry->current_plugin = NULL;
202 registry->current_feature = NULL;
203 registry->open_tags = NULL;
207 * gst_xml_registry_new:
208 * @name: the name of the registry
209 * @location: the location of the registry file
211 * Create a new xml registry with the given name and location.
213 * Returns: a new GstXMLRegistry with the given name an location.
216 gst_xml_registry_new (const gchar * name, const gchar * location)
218 GstXMLRegistry *xmlregistry;
220 xmlregistry = GST_XML_REGISTRY (g_object_new (GST_TYPE_XML_REGISTRY, NULL));
222 g_object_set (G_OBJECT (xmlregistry), "location", location, NULL);
224 GST_REGISTRY (xmlregistry)->name = g_strdup (name);
226 return GST_REGISTRY (xmlregistry);
230 gst_xml_registry_set_property (GObject * object, guint prop_id,
231 const GValue * value, GParamSpec * pspec)
233 GstXMLRegistry *registry;
235 registry = GST_XML_REGISTRY (object);
239 if (registry->open) {
240 CLASS (object)->close_func (registry);
241 g_return_if_fail (registry->open == FALSE);
244 if (registry->location)
245 g_free (registry->location);
247 registry->location = g_strdup (g_value_get_string (value));
248 GST_REGISTRY (registry)->flags = 0x0;
250 if (CLASS (object)->get_perms_func)
251 CLASS (object)->get_perms_func (registry);
253 if (CLASS (object)->add_path_list_func)
254 CLASS (object)->add_path_list_func (registry);
257 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
263 gst_xml_registry_get_property (GObject * object, guint prop_id,
264 GValue * value, GParamSpec * pspec)
266 GstXMLRegistry *registry;
268 registry = GST_XML_REGISTRY (object);
272 g_value_set_string (value, g_strdup (registry->location));
275 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
280 /* this function returns the biggest of the path's mtime and ctime
281 * mtime is updated through an actual write (data)
282 * ctime is updated through changing inode information
283 * so this function returns the last time *anything* changed to this path
286 get_time (const char *path, gboolean * is_dir)
290 if (stat (path, &statbuf)) {
296 *is_dir = S_ISDIR (statbuf.st_mode);
298 if (statbuf.st_mtime > statbuf.st_ctime)
299 return statbuf.st_mtime;
300 return statbuf.st_ctime;
305 (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
308 make_dir (gchar * filename)
313 if (strrchr (filename, '/') == NULL)
316 dirname = g_strndup (filename, strrchr (filename, '/') - filename);
318 if (stat (dirname, &dirstat) == -1 && errno == ENOENT) {
319 if (mkdir (dirname, dirmode) != 0) {
320 if (make_dir (dirname) != TRUE) {
324 if (mkdir (dirname, dirmode) != 0)
335 gst_xml_registry_get_perms_func (GstXMLRegistry * registry)
339 /* if the dir does not exist, make it. if that can't be done, flags = 0x0.
340 if the file can be appended to, it's writable. if it can then be read,
342 After that check if it exists. */
344 if (make_dir (registry->location) != TRUE) {
345 /* we can't do anything with it, leave flags as 0x0 */
349 dirname = g_path_get_dirname (registry->location);
351 if (g_file_test (registry->location, G_FILE_TEST_EXISTS)) {
352 GST_REGISTRY (registry)->flags |= GST_REGISTRY_EXISTS;
355 if (!access (dirname, W_OK)) {
356 GST_REGISTRY (registry)->flags |= GST_REGISTRY_WRITABLE;
359 if (!access (dirname, R_OK)) {
360 GST_REGISTRY (registry)->flags |= GST_REGISTRY_READABLE;
367 gst_xml_registry_add_path_list_func (GstXMLRegistry * registry)
370 GMarkupParseContext *context;
373 GError *error = NULL;
375 context = g_markup_parse_context_new (&gst_xml_registry_paths_parser, 0,
378 if (!(reg = fopen (registry->location, "r"))) {
382 /* slightly allocate more as gmarkup reads too much */
383 text = g_malloc0 (BLOCK_SIZE + 32);
385 size = fread (text, 1, BLOCK_SIZE, reg);
388 g_markup_parse_context_parse (context, text, size, &error);
391 GST_ERROR ("parsing registry %s: %s\n",
392 registry->location, error->message);
396 if (registry->state == GST_XML_REGISTRY_PATHS_DONE)
399 size = fread (text, 1, BLOCK_SIZE, reg);
404 g_markup_parse_context_free (context);
413 plugin_times_older_than_recurse (gchar * path, time_t regtime)
416 struct dirent *dirent;
420 time_t pathtime = get_time (path, &is_dir);
422 if (pathtime > regtime) {
423 GST_CAT_INFO (GST_CAT_PLUGIN_LOADING,
424 "time for %s was %ld; more recent than registry time of %ld\n",
425 path, (long) pathtime, (long) regtime);
432 dir = opendir (path);
434 while ((dirent = readdir (dir))) {
435 /* don't want to recurse in place or backwards */
436 if (strcmp (dirent->d_name, ".") && strcmp (dirent->d_name, "..")) {
437 pluginname = g_strjoin ("/", path, dirent->d_name, NULL);
438 if (!plugin_times_older_than_recurse (pluginname, regtime)) {
452 plugin_times_older_than (GList * paths, time_t regtime)
454 /* return true iff regtime is more recent than the times of all the files
455 * in the plugin dirs.
459 GST_CAT_LOG (GST_CAT_PLUGIN_LOADING,
460 "comparing plugin times from %s with %ld",
461 (gchar *) paths->data, (long) regtime);
462 if (!plugin_times_older_than_recurse (paths->data, regtime))
464 paths = g_list_next (paths);
470 gst_xml_registry_open_func (GstXMLRegistry * registry, GstXMLRegistryMode mode)
472 GstRegistry *gst_registry;
475 gst_registry = GST_REGISTRY (registry);
476 paths = gst_registry->paths;
477 GST_CAT_DEBUG (GST_CAT_GST_INIT, "opening registry %s", registry->location);
479 g_return_val_if_fail (registry->open == FALSE, FALSE);
481 /* if it doesn't exist, first try to build it, and check if it worked
482 * if it's not readable, return false
483 * if it's out of date, rebuild it */
484 if (mode == GST_XML_REGISTRY_READ) {
485 if (!(gst_registry->flags & GST_REGISTRY_EXISTS)) {
486 /* if it's not writable, then don't bother */
487 if (!(gst_registry->flags & GST_REGISTRY_WRITABLE)) {
488 GST_CAT_INFO (GST_CAT_GST_INIT, "Registry isn't writable");
491 GST_CAT_INFO (GST_CAT_GST_INIT,
492 "Registry doesn't exist, trying to build...");
493 gst_registry_rebuild (gst_registry);
494 gst_registry_save (gst_registry);
495 /* FIXME: verify that the flags actually get updated ! */
496 if (!(gst_registry->flags & GST_REGISTRY_EXISTS)) {
500 /* at this point we know it exists */
501 g_return_val_if_fail (gst_registry->flags & GST_REGISTRY_READABLE, FALSE);
503 if (!plugin_times_older_than (paths, get_time (registry->location, NULL))) {
504 if (gst_registry->flags & GST_REGISTRY_WRITABLE) {
505 GST_CAT_INFO (GST_CAT_GST_INIT, "Registry out of date, rebuilding...");
507 gst_registry_rebuild (gst_registry);
509 gst_registry_save (gst_registry);
511 if (!plugin_times_older_than (paths, get_time (registry->location,
513 GST_CAT_INFO (GST_CAT_GST_INIT,
514 "Registry still out of date, something is wrong...");
518 GST_CAT_INFO (GST_CAT_GST_INIT,
519 "Can't write to this registry and it's out of date, ignoring it");
524 GST_CAT_DEBUG (GST_CAT_GST_INIT, "opening registry %s for reading",
526 registry->regfile = fopen (registry->location, "r");
527 } else if (mode == GST_XML_REGISTRY_WRITE) {
531 g_return_val_if_fail (gst_registry->flags & GST_REGISTRY_WRITABLE, FALSE);
533 tmploc = g_strconcat (registry->location, ".tmp", NULL);
535 GST_CAT_DEBUG (GST_CAT_GST_INIT, "opening registry %s for writing", tmploc);
537 if ((fd = open (tmploc, O_WRONLY | O_CREAT | O_EXCL, 0644)) < 0) {
543 registry->regfile = fdopen (fd, "w");
546 if (!registry->regfile)
549 registry->open = TRUE;
555 gst_xml_registry_load_func (GstXMLRegistry * registry, gchar * data,
558 *size = fread (data, 1, *size, registry->regfile);
564 gst_xml_registry_save_func (GstXMLRegistry * registry, gchar * format, ...)
568 va_start (var_args, format);
570 vfprintf (registry->regfile, format, var_args);
578 gst_xml_registry_close_func (GstXMLRegistry * registry)
582 GST_CAT_DEBUG (GST_CAT_GST_INIT, "closing registry %s", registry->location);
583 fclose (registry->regfile);
585 /* If we opened for writing, rename our temporary file. */
586 tmploc = g_strconcat (registry->location, ".tmp", NULL);
587 if (g_file_test (tmploc, G_FILE_TEST_EXISTS))
588 rename (tmploc, registry->location);
591 registry->open = FALSE;
597 gst_xml_registry_load (GstRegistry * registry)
599 GstXMLRegistry *xmlregistry;
602 GError *error = NULL;
606 xmlregistry = GST_XML_REGISTRY (registry);
608 timer = g_timer_new ();
610 xmlregistry->context =
611 g_markup_parse_context_new (&gst_xml_registry_parser, 0, registry, NULL);
613 if (!CLASS (xmlregistry)->open_func (xmlregistry, GST_XML_REGISTRY_READ)) {
614 g_timer_destroy (timer);
618 text = g_malloc0 (BLOCK_SIZE + 32);
621 CLASS (xmlregistry)->load_func (xmlregistry, text, &size);
624 g_markup_parse_context_parse (xmlregistry->context, text, size, &error);
627 GST_ERROR ("parsing registry: %s\n", error->message);
629 CLASS (xmlregistry)->close_func (xmlregistry);
630 g_timer_destroy (timer);
635 CLASS (xmlregistry)->load_func (xmlregistry, text, &size);
640 g_timer_stop (timer);
642 seconds = g_timer_elapsed (timer, NULL);
643 g_timer_destroy (timer);
645 GST_INFO ("loaded %s in %f seconds (%s)",
646 registry->name, seconds, xmlregistry->location);
648 CLASS (xmlregistry)->close_func (xmlregistry);
654 static GstRegistryReturn
655 gst_xml_registry_load_plugin (GstRegistry * registry, GstPlugin * plugin)
657 GError *error = NULL;
658 GstPlugin *loaded_plugin;
660 /* FIXME: add gerror support */
661 loaded_plugin = gst_plugin_load_file (plugin->filename, &error);
664 g_warning ("could not load plugin %s: %s", plugin->desc.name,
666 g_error_free (error);
668 return GST_REGISTRY_PLUGIN_LOAD_ERROR;
669 } else if (loaded_plugin != plugin) {
670 g_critical ("how to remove plugins?");
673 return GST_REGISTRY_OK;
677 gst_xml_registry_parse_plugin (GMarkupParseContext * context, const gchar * tag,
678 const gchar * text, gsize text_len, GstXMLRegistry * registry,
681 GstPlugin *plugin = registry->current_plugin;
683 if (!strcmp (tag, "name")) {
684 plugin->desc.name = g_strndup (text, text_len);
685 } else if (!strcmp (tag, "description")) {
686 plugin->desc.description = g_strndup (text, text_len);
687 } else if (!strcmp (tag, "filename")) {
688 plugin->filename = g_strndup (text, text_len);
689 } else if (!strcmp (tag, "version")) {
690 plugin->desc.version = g_strndup (text, text_len);
691 } else if (!strcmp (tag, "license")) {
692 plugin->desc.license = g_strndup (text, text_len);
693 } else if (!strcmp (tag, "package")) {
694 plugin->desc.package = g_strndup (text, text_len);
695 } else if (!strcmp (tag, "origin")) {
696 plugin->desc.origin = g_strndup (text, text_len);
703 add_to_char_array (gchar *** array, gchar * value)
706 gchar **old = *array;
709 /* expensive, but cycles are cheap... */
713 new = g_new0 (gchar *, i + 2);
724 gst_xml_registry_parse_element_factory (GMarkupParseContext * context,
725 const gchar * tag, const gchar * text, gsize text_len,
726 GstXMLRegistry * registry, GError ** error)
728 GstElementFactory *factory = GST_ELEMENT_FACTORY (registry->current_feature);
730 if (!strcmp (tag, "name")) {
731 gchar *name = g_strndup (text, text_len);
733 gst_plugin_feature_set_name (registry->current_feature, name);
735 } else if (!strcmp (tag, "longname")) {
736 g_free (factory->details.longname);
737 factory->details.longname = g_strndup (text, text_len);
738 } else if (!strcmp (tag, "class")) {
739 g_free (factory->details.klass);
740 factory->details.klass = g_strndup (text, text_len);
741 } else if (!strcmp (tag, "description")) {
742 g_free (factory->details.description);
743 factory->details.description = g_strndup (text, text_len);
744 } else if (!strcmp (tag, "author")) {
745 g_free (factory->details.author);
746 factory->details.author = g_strndup (text, text_len);
747 } else if (!strcmp (tag, "rank")) {
751 rank = strtol (text, &ret, 0);
752 if (ret == text + text_len) {
753 gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE (factory), rank);
755 } else if (!strcmp (tag, "uri_type")) {
756 if (strncasecmp (text, "sink", 4) == 0) {
757 factory->uri_type = GST_URI_SINK;
758 } else if (strncasecmp (text, "source", 5) == 0) {
759 factory->uri_type = GST_URI_SRC;
761 } else if (!strcmp (tag, "uri_protocol")) {
762 add_to_char_array (&factory->uri_protocols, g_strndup (text, text_len));
763 } else if (!strcmp (tag, "interface")) {
764 gchar *tmp = g_strndup (text, text_len);
766 __gst_element_factory_add_interface (factory, tmp);
774 gst_xml_registry_parse_type_find_factory (GMarkupParseContext * context,
775 const gchar * tag, const gchar * text, gsize text_len,
776 GstXMLRegistry * registry, GError ** error)
778 GstTypeFindFactory *factory =
779 GST_TYPE_FIND_FACTORY (registry->current_feature);
781 if (!strcmp (tag, "name")) {
782 registry->current_feature->name = g_strndup (text, text_len);
783 } else if (!strcmp (tag, "rank")) {
787 rank = strtol (text, &ret, 0);
788 if (ret == text + text_len) {
789 gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE (factory), rank);
793 else if (!strcmp (tag, "caps")) {
794 factory->caps = g_strndup (text, text_len);
796 else if (!strcmp (tag, "extension")) {
797 add_to_char_array (&factory->extensions, g_strndup (text, text_len));
804 gst_xml_registry_parse_scheduler_factory (GMarkupParseContext * context,
805 const gchar * tag, const gchar * text, gsize text_len,
806 GstXMLRegistry * registry, GError ** error)
808 GstSchedulerFactory *factory =
809 GST_SCHEDULER_FACTORY (registry->current_feature);
811 if (!strcmp (tag, "name")) {
812 registry->current_feature->name = g_strndup (text, text_len);
813 } else if (!strcmp (tag, "longdesc")) {
814 factory->longdesc = g_strndup (text, text_len);
820 gst_xml_registry_parse_index_factory (GMarkupParseContext * context,
821 const gchar * tag, const gchar * text, gsize text_len,
822 GstXMLRegistry * registry, GError ** error)
824 GstIndexFactory *factory = GST_INDEX_FACTORY (registry->current_feature);
826 if (!strcmp (tag, "name")) {
827 registry->current_feature->name = g_strndup (text, text_len);
828 } else if (!strcmp (tag, "longdesc")) {
829 factory->longdesc = g_strndup (text, text_len);
835 gst_xml_registry_parse_padtemplate (GMarkupParseContext * context,
836 const gchar * tag, const gchar * text, gsize text_len,
837 GstXMLRegistry * registry, GError ** error)
839 if (!strcmp (tag, "nametemplate")) {
840 registry->name_template = g_strndup (text, text_len);
841 } else if (!strcmp (tag, "direction")) {
842 if (!strncmp (text, "sink", text_len)) {
843 registry->direction = GST_PAD_SINK;
844 } else if (!strncmp (text, "src", text_len)) {
845 registry->direction = GST_PAD_SRC;
847 } else if (!strcmp (tag, "presence")) {
848 if (!strncmp (text, "always", text_len)) {
849 registry->presence = GST_PAD_ALWAYS;
850 } else if (!strncmp (text, "sometimes", text_len)) {
851 registry->presence = GST_PAD_SOMETIMES;
852 } else if (!strncmp (text, "request", text_len)) {
853 registry->presence = GST_PAD_REQUEST;
855 } else if (!strncmp (tag, "caps", 4)) {
858 s = g_strndup (text, text_len);
859 g_assert (registry->caps == NULL);
860 registry->caps = gst_caps_from_string (s);
861 if (registry->caps == NULL) {
862 g_critical ("Could not parse caps: length %d, content: %*s\n", text_len,
872 gst_xml_registry_start_element (GMarkupParseContext * context,
873 const gchar * element_name,
874 const gchar ** attribute_names,
875 const gchar ** attribute_values, gpointer user_data, GError ** error)
877 GstXMLRegistry *xmlregistry = GST_XML_REGISTRY (user_data);
879 xmlregistry->open_tags = g_list_prepend (xmlregistry->open_tags,
880 g_strdup (element_name));
882 switch (xmlregistry->state) {
883 case GST_XML_REGISTRY_NONE:
884 if (!strcmp (element_name, "GST-PluginRegistry")) {
885 xmlregistry->state = GST_XML_REGISTRY_TOP;
888 case GST_XML_REGISTRY_TOP:
889 if (!strncmp (element_name, "plugin", 6)) {
890 xmlregistry->state = GST_XML_REGISTRY_PLUGIN;
891 xmlregistry->parser = gst_xml_registry_parse_plugin;
892 xmlregistry->current_plugin = (GstPlugin *) g_new0 (GstPlugin, 1);
895 case GST_XML_REGISTRY_PLUGIN:
896 if (!strncmp (element_name, "feature", 7)) {
898 GstPluginFeature *feature = NULL;
900 xmlregistry->state = GST_XML_REGISTRY_FEATURE;
902 while (attribute_names[i]) {
903 if (!strncmp (attribute_names[i], "typename", 8)) {
905 GST_PLUGIN_FEATURE (g_object_new (g_type_from_name
906 (attribute_values[i]), NULL));
912 xmlregistry->current_feature = feature;
914 if (GST_IS_ELEMENT_FACTORY (feature)) {
915 GstElementFactory *factory = GST_ELEMENT_FACTORY (feature);
917 factory->padtemplates = NULL;
918 xmlregistry->parser = gst_xml_registry_parse_element_factory;
920 } else if (GST_IS_TYPE_FIND_FACTORY (feature)) {
921 xmlregistry->parser = gst_xml_registry_parse_type_find_factory;
922 } else if (GST_IS_SCHEDULER_FACTORY (feature)) {
923 xmlregistry->parser = gst_xml_registry_parse_scheduler_factory;
924 GST_SCHEDULER_FACTORY (feature)->type = 0;
925 } else if (GST_IS_INDEX_FACTORY (feature)) {
926 xmlregistry->parser = gst_xml_registry_parse_index_factory;
928 g_warning ("unknown feature type");
933 case GST_XML_REGISTRY_FEATURE:
934 if (!strncmp (element_name, "padtemplate", 11)) {
935 xmlregistry->state = GST_XML_REGISTRY_PADTEMPLATE;
936 xmlregistry->parser = gst_xml_registry_parse_padtemplate;
937 xmlregistry->name_template = NULL;
938 xmlregistry->direction = 0;
939 xmlregistry->presence = 0;
940 xmlregistry->caps = NULL;
949 gst_xml_registry_end_element (GMarkupParseContext * context,
950 const gchar * element_name, gpointer user_data, GError ** error)
952 GstXMLRegistry *xmlregistry = GST_XML_REGISTRY (user_data);
953 gchar *open_tag = (gchar *) xmlregistry->open_tags->data;
955 xmlregistry->open_tags = g_list_remove (xmlregistry->open_tags, open_tag);
958 switch (xmlregistry->state) {
959 case GST_XML_REGISTRY_TOP:
960 if (!strcmp (element_name, "GST-PluginRegistry")) {
961 xmlregistry->state = GST_XML_REGISTRY_NONE;
964 case GST_XML_REGISTRY_PLUGIN:
965 if (!strcmp (element_name, "plugin")) {
966 xmlregistry->state = GST_XML_REGISTRY_TOP;
967 xmlregistry->parser = NULL;
968 gst_registry_add_plugin (GST_REGISTRY (xmlregistry),
969 xmlregistry->current_plugin);
972 case GST_XML_REGISTRY_FEATURE:
973 if (!strcmp (element_name, "feature")) {
974 xmlregistry->state = GST_XML_REGISTRY_PLUGIN;
975 xmlregistry->parser = gst_xml_registry_parse_plugin;
976 gst_plugin_add_feature (xmlregistry->current_plugin,
977 xmlregistry->current_feature);
978 xmlregistry->current_feature = NULL;
981 case GST_XML_REGISTRY_PADTEMPLATE:
982 if (!strcmp (element_name, "padtemplate")) {
983 GstPadTemplate *template;
985 template = gst_pad_template_new (xmlregistry->name_template,
986 xmlregistry->direction, xmlregistry->presence, xmlregistry->caps);
988 g_free (xmlregistry->name_template);
989 xmlregistry->name_template = NULL;
990 xmlregistry->caps = NULL;
992 __gst_element_factory_add_pad_template (GST_ELEMENT_FACTORY
993 (xmlregistry->current_feature), template);
994 xmlregistry->state = GST_XML_REGISTRY_FEATURE;
995 xmlregistry->parser = gst_xml_registry_parse_element_factory;
1004 gst_xml_registry_text (GMarkupParseContext * context, const gchar * text,
1005 gsize text_len, gpointer user_data, GError ** error)
1007 GstXMLRegistry *xmlregistry = GST_XML_REGISTRY (user_data);
1010 if (xmlregistry->open_tags) {
1011 open_tag = (gchar *) xmlregistry->open_tags->data;
1013 if (!strcmp (open_tag, "plugin-path")) {
1014 //gst_plugin_add_path (g_strndup (text, text_len));
1015 } else if (xmlregistry->parser) {
1016 xmlregistry->parser (context, open_tag, text, text_len, xmlregistry,
1023 gst_xml_registry_passthrough (GMarkupParseContext * context,
1024 const gchar * passthrough_text, gsize text_len, gpointer user_data,
1030 gst_xml_registry_error (GMarkupParseContext * context, GError * error,
1033 GST_ERROR ("%s\n", error->message);
1037 gst_xml_registry_paths_start_element (GMarkupParseContext * context,
1038 const gchar * element_name,
1039 const gchar ** attribute_names,
1040 const gchar ** attribute_values, gpointer user_data, GError ** error)
1042 GstXMLRegistry *xmlregistry = GST_XML_REGISTRY (user_data);
1044 switch (xmlregistry->state) {
1045 case GST_XML_REGISTRY_NONE:
1046 if (!strcmp (element_name, "GST-PluginRegistry")) {
1047 xmlregistry->state = GST_XML_REGISTRY_TOP;
1050 case GST_XML_REGISTRY_TOP:
1051 if (!strcmp (element_name, "gst-registry-paths")) {
1052 xmlregistry->state = GST_XML_REGISTRY_PATHS;
1055 case GST_XML_REGISTRY_PATHS:
1056 if (!strcmp (element_name, "path")) {
1057 xmlregistry->state = GST_XML_REGISTRY_PATH;
1066 gst_xml_registry_paths_end_element (GMarkupParseContext * context,
1067 const gchar * element_name, gpointer user_data, GError ** error)
1069 GstXMLRegistry *xmlregistry = GST_XML_REGISTRY (user_data);
1071 switch (xmlregistry->state) {
1072 case GST_XML_REGISTRY_PATH:
1073 if (!strcmp (element_name, "path")) {
1074 xmlregistry->state = GST_XML_REGISTRY_PATHS;
1077 case GST_XML_REGISTRY_PATHS:
1078 if (!strcmp (element_name, "gst-plugin-paths")) {
1079 xmlregistry->state = GST_XML_REGISTRY_PATHS_DONE;
1088 gst_xml_registry_paths_text (GMarkupParseContext * context, const gchar * text,
1089 gsize text_len, gpointer user_data, GError ** error)
1091 GstXMLRegistry *xmlregistry = GST_XML_REGISTRY (user_data);
1093 if (xmlregistry->state == GST_XML_REGISTRY_PATH)
1094 gst_registry_add_path (GST_REGISTRY (xmlregistry), g_strndup (text,
1101 #define PUT_ESCAPED(tag,value) \
1103 const gchar *toconv = value; \
1105 gchar *v = g_markup_escape_text (toconv, strlen (toconv)); \
1106 CLASS (xmlregistry)->save_func (xmlregistry, "<%s>%s</%s>\n", tag, v, tag); \
1110 #define PUT_ESCAPED_INT(tag,value) \
1112 gchar *save = g_strdup_printf ("%ld", (glong) value); \
1113 CLASS (xmlregistry)->save_func (xmlregistry, "<%s>%s</%s>\n", tag, save, tag); \
1119 gst_xml_registry_save_caps (GstXMLRegistry * xmlregistry, const GstCaps * caps)
1121 char *s = gst_caps_to_string (caps);
1123 PUT_ESCAPED ("caps", s);
1129 gst_xml_registry_save_pad_template (GstXMLRegistry * xmlregistry,
1130 GstPadTemplate * template)
1134 PUT_ESCAPED ("nametemplate", template->name_template);
1135 CLASS (xmlregistry)->save_func (xmlregistry, "<direction>%s</direction>\n",
1136 (template->direction == GST_PAD_SINK ? "sink" : "src"));
1138 switch (template->presence) {
1139 case GST_PAD_ALWAYS:
1140 presence = "always";
1142 case GST_PAD_SOMETIMES:
1143 presence = "sometimes";
1145 case GST_PAD_REQUEST:
1146 presence = "request";
1149 presence = "unknown";
1152 CLASS (xmlregistry)->save_func (xmlregistry, "<presence>%s</presence>\n",
1155 if (GST_PAD_TEMPLATE_CAPS (template)) {
1156 gst_xml_registry_save_caps (xmlregistry, GST_PAD_TEMPLATE_CAPS (template));
1162 gst_xml_registry_save_feature (GstXMLRegistry * xmlregistry,
1163 GstPluginFeature * feature)
1165 PUT_ESCAPED ("name", feature->name);
1167 if (feature->rank > 0) {
1168 gint rank = feature->rank;
1170 CLASS (xmlregistry)->save_func (xmlregistry, "<rank>%d</rank>\n", rank);
1173 if (GST_IS_ELEMENT_FACTORY (feature)) {
1174 GstElementFactory *factory = GST_ELEMENT_FACTORY (feature);
1177 PUT_ESCAPED ("longname", factory->details.longname);
1178 PUT_ESCAPED ("class", factory->details.klass);
1179 PUT_ESCAPED ("description", factory->details.description);
1180 PUT_ESCAPED ("author", factory->details.author);
1182 walk = factory->padtemplates;
1185 GstPadTemplate *template = GST_PAD_TEMPLATE (walk->data);
1187 CLASS (xmlregistry)->save_func (xmlregistry, "<padtemplate>\n");
1188 gst_xml_registry_save_pad_template (xmlregistry, template);
1189 CLASS (xmlregistry)->save_func (xmlregistry, "</padtemplate>\n");
1191 walk = g_list_next (walk);
1194 walk = factory->interfaces;
1196 PUT_ESCAPED ("interface", (gchar *) walk->data);
1197 walk = g_list_next (walk);
1200 if (GST_URI_TYPE_IS_VALID (factory->uri_type)) {
1203 PUT_ESCAPED ("uri_type",
1204 factory->uri_type == GST_URI_SINK ? "sink" : "source");
1205 g_assert (factory->uri_protocols);
1206 protocol = factory->uri_protocols;
1208 PUT_ESCAPED ("uri_protocol", *protocol);
1212 } else if (GST_IS_TYPE_FIND_FACTORY (feature)) {
1213 GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (feature);
1216 if (factory->caps) {
1217 gst_xml_registry_save_caps (xmlregistry, factory->caps);
1219 if (factory->extensions) {
1220 while (factory->extensions[i]) {
1221 PUT_ESCAPED ("extension", factory->extensions[i]);
1225 } else if (GST_IS_SCHEDULER_FACTORY (feature)) {
1226 PUT_ESCAPED ("longdesc", GST_SCHEDULER_FACTORY (feature)->longdesc);
1227 } else if (GST_IS_INDEX_FACTORY (feature)) {
1228 PUT_ESCAPED ("longdesc", GST_INDEX_FACTORY (feature)->longdesc);
1234 gst_xml_registry_save_plugin (GstXMLRegistry * xmlregistry, GstPlugin * plugin)
1238 PUT_ESCAPED ("name", plugin->desc.name);
1239 PUT_ESCAPED ("description", plugin->desc.description);
1240 PUT_ESCAPED ("filename", plugin->filename);
1241 PUT_ESCAPED ("version", plugin->desc.version);
1242 PUT_ESCAPED ("license", plugin->desc.license);
1243 PUT_ESCAPED ("package", plugin->desc.package);
1244 PUT_ESCAPED ("origin", plugin->desc.origin);
1246 walk = plugin->features;
1249 GstPluginFeature *feature = GST_PLUGIN_FEATURE (walk->data);
1251 CLASS (xmlregistry)->save_func (xmlregistry, "<feature typename=\"%s\">\n",
1252 g_type_name (G_OBJECT_TYPE (feature)));
1253 gst_xml_registry_save_feature (xmlregistry, feature);
1254 CLASS (xmlregistry)->save_func (xmlregistry, "</feature>\n");
1256 walk = g_list_next (walk);
1263 gst_xml_registry_save (GstRegistry * registry)
1266 GstXMLRegistry *xmlregistry;
1268 g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
1269 g_return_val_if_fail (registry->flags & GST_REGISTRY_WRITABLE, FALSE);
1271 xmlregistry = GST_XML_REGISTRY (registry);
1273 if (!CLASS (xmlregistry)->open_func (xmlregistry, GST_XML_REGISTRY_WRITE)) {
1277 CLASS (xmlregistry)->save_func (xmlregistry, "<?xml version=\"1.0\"?>\n");
1278 CLASS (xmlregistry)->save_func (xmlregistry, "<GST-PluginRegistry>\n");
1280 walk = g_list_last (gst_registry_get_path_list (GST_REGISTRY (registry)));
1282 CLASS (xmlregistry)->save_func (xmlregistry, "<gst-plugin-paths>\n");
1284 CLASS (xmlregistry)->save_func (xmlregistry, "<path>");
1285 CLASS (xmlregistry)->save_func (xmlregistry, (gchar *) walk->data);
1286 CLASS (xmlregistry)->save_func (xmlregistry, "</path>\n");
1287 walk = g_list_previous (walk);
1289 CLASS (xmlregistry)->save_func (xmlregistry, "</gst-plugin-paths>\n");
1291 walk = g_list_last (registry->plugins);
1294 GstPlugin *plugin = GST_PLUGIN (walk->data);
1296 CLASS (xmlregistry)->save_func (xmlregistry, "<plugin>\n");
1297 gst_xml_registry_save_plugin (xmlregistry, plugin);
1298 CLASS (xmlregistry)->save_func (xmlregistry, "</plugin>\n");
1300 walk = g_list_previous (walk);
1302 CLASS (xmlregistry)->save_func (xmlregistry, "</GST-PluginRegistry>\n");
1304 CLASS (xmlregistry)->close_func (xmlregistry);
1310 gst_xml_registry_rebuild_recurse (GstXMLRegistry * registry,
1311 const gchar * directory)
1315 gint dr_len, sf_len;
1317 dir = g_dir_open (directory, 0, NULL);
1320 const gchar *dirent;
1322 while ((dirent = g_dir_read_name (dir))) {
1325 if (*dirent == '=') {
1326 /* =build, =inst, etc. -- automake distcheck directories */
1330 dirname = g_strjoin ("/", directory, dirent, NULL);
1332 g_list_concat (ret, gst_xml_registry_rebuild_recurse (registry,
1338 dr_len = strlen (directory);
1339 sf_len = strlen (G_MODULE_SUFFIX);
1340 if (dr_len >= sf_len &&
1341 strcmp (directory + dr_len - sf_len, G_MODULE_SUFFIX) == 0) {
1342 ret = g_list_prepend (ret, g_strdup (directory));
1350 gst_xml_registry_rebuild (GstRegistry * registry)
1352 GList *walk = NULL, *plugins = NULL, *prune = NULL;
1353 GError *error = NULL;
1356 GstXMLRegistry *xmlregistry = GST_XML_REGISTRY (registry);
1358 walk = registry->paths;
1361 gchar *path = (gchar *) walk->data;
1363 GST_CAT_INFO (GST_CAT_PLUGIN_LOADING,
1364 "Rebuilding registry %p in directory %s...", registry, path);
1366 plugins = g_list_concat (plugins,
1367 gst_xml_registry_rebuild_recurse (xmlregistry, path));
1369 walk = g_list_next (walk);
1372 plugins = g_list_reverse (plugins);
1375 length = g_list_length (plugins);
1379 g_assert (walk->data);
1380 plugin = gst_plugin_load_file ((gchar *) walk->data, NULL);
1382 prune = g_list_prepend (prune, walk->data);
1383 gst_registry_add_plugin (registry, plugin);
1386 walk = g_list_next (walk);
1391 plugins = g_list_remove (plugins, walk->data);
1392 g_free (walk->data);
1393 walk = g_list_next (walk);
1395 g_list_free (prune);
1397 } while (g_list_length (plugins) != length);
1401 if ((plugin = gst_plugin_load_file ((gchar *) walk->data, &error))) {
1402 g_warning ("Bizarre behavior: plugin %s actually loaded",
1403 (gchar *) walk->data);
1404 gst_registry_add_plugin (registry, plugin);
1406 GST_CAT_INFO (GST_CAT_PLUGIN_LOADING, "Plugin %s failed to load: %s",
1407 (gchar *) walk->data, error->message);
1409 g_free (walk->data);
1410 g_error_free (error);
1414 walk = g_list_next (walk);