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.
25 * @short_description: XML save/restore operations of pipelines
27 * GStreamer pipelines can be saved to xml files using gst_xml_write_file().
28 * They can be loaded back using gst_xml_parse_doc() / gst_xml_parse_file() /
29 * gst_xml_parse_memory().
30 * Additionally one can load saved pipelines into the gst-editor to inspect the
33 * #GstElement implementations need to override gst_object_save_thyself() and
34 * gst_object_restore_thyself().
37 #include "gst_private.h"
40 #include "gstmarshal.h"
50 static void gst_xml_class_init (GstXMLClass * klass);
51 static void gst_xml_init (GstXML * xml);
53 static void gst_xml_object_loaded (GstObject * private, GstObject * object,
54 xmlNodePtr self, gpointer data);
56 static GstObjectClass *parent_class = NULL;
57 static guint gst_xml_signals[LAST_SIGNAL] = { 0 };
60 gst_xml_get_type (void)
62 static GType xml_type = 0;
64 if (G_UNLIKELY (xml_type == 0)) {
65 static const GTypeInfo xml_info = {
69 (GClassInitFunc) gst_xml_class_init,
74 (GInstanceInitFunc) gst_xml_init,
78 xml_type = g_type_register_static (GST_TYPE_OBJECT, "GstXML", &xml_info, 0);
84 gst_xml_class_init (GstXMLClass * klass)
86 GObjectClass *gobject_class;
88 gobject_class = (GObjectClass *) klass;
90 parent_class = g_type_class_peek_parent (klass);
92 /* FIXME G_TYPE_POINTER should be GType of xmlNodePtr
93 * (ensonic) can't be fixed, as libxml does not use GObject (unfortunately)
96 * GstXML::object-loaded:
97 * @xml: the xml persistence instance
98 * @object: the object that has been loaded
99 * @xml_node: the related xml_node pointer to the document tree
101 * Signals that a new object has been deserialized.
103 gst_xml_signals[OBJECT_LOADED] =
104 g_signal_new ("object-loaded", G_TYPE_FROM_CLASS (klass),
105 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstXMLClass, object_loaded), NULL,
106 NULL, gst_marshal_VOID__OBJECT_POINTER, G_TYPE_NONE, 2, GST_TYPE_OBJECT,
112 gst_xml_init (GstXML * xml)
114 xml->topelements = NULL;
120 * Create a new GstXML parser object.
122 * Returns: a pointer to a new GstXML object.
127 return GST_XML (g_object_new (GST_TYPE_XML, NULL));
132 * @element: The element to write out
134 * Converts the given element into an XML presentation.
136 * Returns: a pointer to an XML document
139 gst_xml_write (GstElement * element)
142 xmlNodePtr elementnode;
145 doc = xmlNewDoc ((xmlChar *) "1.0");
147 doc->xmlRootNode = xmlNewDocNode (doc, NULL, (xmlChar *) "gstreamer", NULL);
150 xmlNewNs (doc->xmlRootNode,
151 (xmlChar *) "http://gstreamer.net/gst-core/1.0/", (xmlChar *) "gst");
153 elementnode = xmlNewChild (doc->xmlRootNode, gst_ns, (xmlChar *) "element",
156 gst_object_save_thyself (GST_OBJECT (element), elementnode);
162 * gst_xml_write_file:
163 * @element: The element to write out
164 * @out: an open file, like stdout
166 * Converts the given element into XML and writes the formatted XML to an open
169 * Returns: number of bytes written on success, -1 otherwise.
172 gst_xml_write_file (GstElement * element, FILE * out)
177 xmlOutputBufferPtr buf;
179 const char *encoding;
180 xmlCharEncodingHandlerPtr handler = NULL;
184 cur = gst_xml_write (element);
189 encoding = (const char *) cur->encoding;
191 if (encoding != NULL) {
194 enc = xmlParseCharEncoding (encoding);
196 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
197 xmlGenericError (xmlGenericErrorContext,
198 "xmlDocDump: document not in UTF8\n");
201 if (enc != XML_CHAR_ENCODING_UTF8) {
202 handler = xmlFindCharEncodingHandler (encoding);
203 if (handler == NULL) {
204 xmlFree ((char *) cur->encoding);
205 cur->encoding = NULL;
210 buf = xmlOutputBufferCreateFile (out, handler);
212 indent = xmlIndentTreeOutput;
213 xmlIndentTreeOutput = 1;
214 ret = xmlSaveFormatFileTo (buf, cur, NULL, 1);
215 xmlIndentTreeOutput = indent;
217 /* apparently this doesn't return anything in libxml1 */
218 xmlDocDump (out, cur);
227 * @xml: a pointer to a GstXML object
228 * @doc: a pointer to an xml document to parse
229 * @root: The name of the root object to build
231 * Fills the GstXML object with the elements from the
234 * Returns: TRUE on success, FALSE otherwise
237 gst_xml_parse_doc (GstXML * xml, xmlDocPtr doc, const guchar * root)
239 xmlNodePtr field, cur;
242 cur = xmlDocGetRootElement (doc);
244 g_warning ("gstxml: empty document\n");
247 ns = xmlSearchNsByHref (doc, cur,
248 (xmlChar *) "http://gstreamer.net/gst-core/1.0/");
250 g_warning ("gstxml: document of wrong type, core namespace not found\n");
253 if (strcmp ((char *) cur->name, "gstreamer")) {
254 g_warning ("gstxml: XML file is in wrong format\n");
258 gst_class_signal_connect (GST_OBJECT_CLASS (G_OBJECT_GET_CLASS (xml)),
259 "object_loaded", gst_xml_object_loaded, xml);
263 field = cur->xmlChildrenNode;
266 if (!strcmp ((char *) field->name, "element") && (field->ns == xml->ns)) {
269 element = gst_xml_make_element (field, NULL);
271 xml->topelements = g_list_prepend (xml->topelements, element);
276 xml->topelements = g_list_reverse (xml->topelements);
281 /* FIXME 0.9: Why guchar*? */
283 * gst_xml_parse_file:
284 * @xml: a pointer to a GstXML object
285 * @fname: The filename with the xml description
286 * @root: The name of the root object to build
288 * Fills the GstXML object with the corresponding elements from
289 * the XML file fname. Optionally it will only build the element from
290 * the element node root (if it is not NULL). This feature is useful
291 * if you only want to build a specific element from an XML file
292 * but not the pipeline it is embedded in.
294 * Pass "-" as fname to read from stdin. You can also pass a URI
295 * of any format that libxml supports, including http.
297 * Returns: TRUE on success, FALSE otherwise
300 gst_xml_parse_file (GstXML * xml, const guchar * fname, const guchar * root)
304 g_return_val_if_fail (fname != NULL, FALSE);
306 doc = xmlParseFile ((char *) fname);
309 g_warning ("gstxml: XML file \"%s\" could not be read\n", fname);
313 return gst_xml_parse_doc (xml, doc, root);
316 /* FIXME 0.9: guchar* */
318 * gst_xml_parse_memory:
319 * @xml: a pointer to a GstXML object
320 * @buffer: a pointer to the in memory XML buffer
321 * @size: the size of the buffer
322 * @root: the name of the root objects to build
324 * Fills the GstXML object with the corresponding elements from
325 * an in memory XML buffer.
327 * Returns: TRUE on success
330 gst_xml_parse_memory (GstXML * xml, guchar * buffer, guint size,
335 g_return_val_if_fail (buffer != NULL, FALSE);
337 doc = xmlParseMemory ((char *) buffer, size);
339 return gst_xml_parse_doc (xml, doc, (const xmlChar *) root);
343 gst_xml_object_loaded (GstObject * private, GstObject * object, xmlNodePtr self,
346 GstXML *xml = GST_XML (data);
348 /* FIXME check that this element was created from the same xmlDocPtr... */
349 g_signal_emit (G_OBJECT (xml), gst_xml_signals[OBJECT_LOADED], 0, object,
354 * gst_xml_get_topelements:
355 * @xml: The GstXML to get the elements from
357 * Retrive a list of toplevel elements.
359 * Returns: a GList of elements
362 gst_xml_get_topelements (GstXML * xml)
364 g_return_val_if_fail (xml != NULL, NULL);
366 return xml->topelements;
369 /* FIXME 0.9: why is the arg guchar* instead of gchar*? */
371 * gst_xml_get_element:
372 * @xml: The GstXML to get the element from
373 * @name: The name of element to retreive
375 * This function is used to get a pointer to the GstElement corresponding
376 * to name in the pipeline description. You would use this if you have
377 * to do anything to the element after loading.
379 * Returns: a pointer to a new GstElement
382 gst_xml_get_element (GstXML * xml, const guchar * name)
387 g_return_val_if_fail (xml != NULL, NULL);
388 g_return_val_if_fail (name != NULL, NULL);
390 GST_DEBUG ("gstxml: getting element \"%s\"", name);
392 topelements = gst_xml_get_topelements (xml);
394 while (topelements) {
395 GstElement *top = GST_ELEMENT (topelements->data);
397 GST_DEBUG ("gstxml: getting element \"%s\"", name);
398 if (!strcmp (GST_ELEMENT_NAME (top), (char *) name)) {
401 if (GST_IS_BIN (top)) {
402 element = gst_bin_get_by_name (GST_BIN (top), (gchar *) name);
408 topelements = g_list_next (topelements);
414 * gst_xml_make_element:
416 * @parent: the parent of this object when it's loaded
418 * Load the element from the XML description
420 * Returns: the new element
423 gst_xml_make_element (xmlNodePtr cur, GstObject * parent)
425 xmlNodePtr children = cur->xmlChildrenNode;
430 /* first get the needed tags to construct the element */
432 if (!strcmp ((char *) children->name, "name")) {
433 name = (gchar *) xmlNodeGetContent (children);
434 } else if (!strcmp ((char *) children->name, "type")) {
435 type = (gchar *) xmlNodeGetContent (children);
437 children = children->next;
439 g_return_val_if_fail (name != NULL, NULL);
440 g_return_val_if_fail (type != NULL, NULL);
442 GST_CAT_INFO (GST_CAT_XML, "loading \"%s\" of type \"%s\"", name, type);
444 element = gst_element_factory_make (type, name);
446 g_return_val_if_fail (element != NULL, NULL);
451 /* ne need to set the parent on this object bacause the pads */
452 /* will go through the hierarchy to link to their peers */
454 gst_object_set_parent (GST_OBJECT (element), parent);
456 gst_object_restore_thyself (GST_OBJECT (element), cur);