2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstxml.c: XML save/restore of pipelines
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.
23 #include "gst_private.h"
29 static void gst_xml_class_init (GstXMLClass *klass);
30 static void gst_xml_init (GstXML *xml);
32 static GstObjectClass *parent_class = NULL;
35 gst_xml_get_type(void)
37 static GtkType xml_type = 0;
40 static const GtkTypeInfo xml_info = {
43 sizeof(GstElementClass),
44 (GtkClassInitFunc)gst_xml_class_init,
45 (GtkObjectInitFunc)gst_xml_init,
48 (GtkClassInitFunc)NULL,
50 xml_type = gtk_type_unique (GST_TYPE_XML, &xml_info);
56 gst_xml_class_init (GstXMLClass *klass)
58 parent_class = gtk_type_class (GST_TYPE_OBJECT);
62 gst_xml_init(GstXML *xml)
68 * @element: The element to write out
70 * Converts the given element into an XML presentation.
72 * Returns: a pointer to an XML document
75 gst_xml_write (GstElement *element)
79 doc = xmlNewDoc ("1.0");
80 doc->xmlRootNode = xmlNewDocNode (doc, NULL, "GST-Pipeline", NULL);
82 gst_element_save_thyself (element, doc->xmlRootNode);
88 gst_xml_real_parse (xmlDocPtr doc, const guchar *root)
93 if (strcmp(doc->xmlRootNode->name, "GST-Pipeline")) {
94 g_warning("gstxml: XML file is in wrong format\n");
98 xml = GST_XML(gtk_type_new(GST_TYPE_XML));
100 xml->topelements = NULL;
102 field = doc->xmlRootNode->xmlChildrenNode;
105 if (!strcmp(field->name, "element")) {
108 xml->elements = g_hash_table_new(g_str_hash, g_str_equal);
110 element = gst_element_load_thyself(field, xml->elements);
112 g_hash_table_destroy (xml->elements);
114 xml->topelements = g_list_prepend (xml->topelements, element);
119 xml->topelements = g_list_reverse (xml->topelements);
126 * @fname: The filename with the xml description
127 * @root: The name of the root object to build
129 * Creates a new GstXML object (and the corresponding elements) from
130 * the XML file fname. Optionally it will only build the element from
131 * the element node root (if it is not NULL). This feature is useful
132 * if you only want to build a specific element from an XML file
133 * but not the pipeline it is embedded in. Note also that the XML parse
134 * tree is cached to speed up creating another GstXML object for
137 * Returns: a pointer to a new GstXML object
140 gst_xml_new (const guchar *fname, const guchar *root)
144 g_return_val_if_fail(fname != NULL, NULL);
146 doc = xmlParseFile(fname);
149 g_warning("gstxml: XML file \"%s\" could not be read\n", fname);
153 return gst_xml_real_parse (doc, root);
157 * gst_xml_new_from_memory:
158 * @buffer: a pointer to the in memory XML buffer
159 * @size: the size of the buffer
160 * @root: the name of the root objects to build
162 * Creates a new GstXML object (and the corresponding elements) from
163 * an in memory XML buffer.
165 * Returns: a pointer to a new GstXML object
168 gst_xml_new_from_memory (guchar *buffer, guint size, const gchar *root)
172 g_return_val_if_fail(buffer != NULL, NULL);
174 doc = xmlParseMemory (buffer, size);
176 return gst_xml_real_parse (doc, root);
180 * gst_xml_get_topelements:
181 * @xml: The GstXML to get the elements from
183 * Retrive a list of toplevel elements.
185 * Returns: a GList of elements
188 gst_xml_get_topelements (GstXML *xml)
190 g_return_val_if_fail (xml != NULL, NULL);
192 return xml->topelements;
196 * gst_xml_get_element:
197 * @xml: The GstXML to get the element from
198 * @name: The name of element to retreive
200 * This function is used to get a pointer to the GstElement corresponding
201 * to name in the pipeline description. You would use this if you have
202 * to do anything to the element after loading.
204 * Returns: a pointer to a new GstElement
207 gst_xml_get_element (GstXML *xml, const guchar *name)
212 g_return_val_if_fail(xml != NULL, NULL);
213 g_return_val_if_fail(name != NULL, NULL);
215 GST_DEBUG (0,"gstxml: getting element \"%s\"\n", name);
217 topelements = gst_xml_get_topelements (xml);
219 while (topelements) {
220 GstElement *top = GST_ELEMENT (topelements->data);
222 if (!strcmp (gst_element_get_name (top), name)) {
226 if (GST_IS_BIN (top)) {
227 element = gst_bin_get_by_name (GST_BIN (top), name);
233 topelements = g_list_next (topelements);