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 the #GstObjectClass.save_thyself()
34 * and #GstObjectClass.restore_thyself() virtual functions of #GstObject.
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);
52 static void gst_xml_dispose (GObject * object);
54 static void gst_xml_object_loaded (GstObject * private, GstObject * object,
55 xmlNodePtr self, gpointer data);
57 static GstObjectClass *parent_class = NULL;
58 static guint gst_xml_signals[LAST_SIGNAL] = { 0 };
60 G_DEFINE_TYPE (GstXML, gst_xml, GST_TYPE_OBJECT);
63 gst_xml_class_init (GstXMLClass * klass)
65 GObjectClass *gobject_class = (GObjectClass *) klass;
67 parent_class = g_type_class_peek_parent (klass);
69 gobject_class->dispose = gst_xml_dispose;
71 /* FIXME G_TYPE_POINTER should be GType of xmlNodePtr
72 * (ensonic) can't be fixed, as libxml does not use GObject (unfortunately)
75 * GstXML::object-loaded:
76 * @xml: the xml persistence instance
77 * @object: the object that has been loaded
78 * @xml_node: the related xml_node pointer to the document tree
80 * Signals that a new object has been deserialized.
82 gst_xml_signals[OBJECT_LOADED] =
83 g_signal_new ("object-loaded", G_TYPE_FROM_CLASS (klass),
84 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstXMLClass, object_loaded), NULL,
85 NULL, gst_marshal_VOID__OBJECT_POINTER, G_TYPE_NONE, 2, GST_TYPE_OBJECT,
91 gst_xml_init (GstXML * xml)
93 xml->topelements = NULL;
97 gst_xml_dispose (GObject * object)
99 GstXML *xml = GST_XML (object);
101 g_list_foreach (xml->topelements, (GFunc) gst_object_unref, NULL);
102 g_list_free (xml->topelements);
103 xml->topelements = NULL;
105 G_OBJECT_CLASS (parent_class)->dispose (object);
111 * Create a new GstXML parser object.
113 * Returns: a pointer to a new GstXML object.
118 return GST_XML (g_object_newv (GST_TYPE_XML, 0, NULL));
123 * @element: The element to write out
125 * Converts the given element into an XML presentation.
127 * Returns: a pointer to an XML document
130 gst_xml_write (GstElement * element)
133 xmlNodePtr elementnode;
136 doc = xmlNewDoc ((xmlChar *) "1.0");
138 doc->xmlRootNode = xmlNewDocNode (doc, NULL, (xmlChar *) "gstreamer", NULL);
141 xmlNewNs (doc->xmlRootNode,
142 (xmlChar *) "http://gstreamer.net/gst-core/1.0/", (xmlChar *) "gst");
144 elementnode = xmlNewChild (doc->xmlRootNode, gst_ns, (xmlChar *) "element",
147 gst_object_save_thyself (GST_OBJECT (element), elementnode);
153 * gst_xml_write_file:
154 * @element: The element to write out
155 * @out: an open file, like stdout
157 * Converts the given element into XML and writes the formatted XML to an open
160 * Returns: number of bytes written on success, -1 otherwise.
163 gst_xml_write_file (GstElement * element, FILE * out)
168 xmlOutputBufferPtr buf;
170 const char *encoding;
171 xmlCharEncodingHandlerPtr handler = NULL;
175 cur = gst_xml_write (element);
180 encoding = (const char *) cur->encoding;
182 if (encoding != NULL) {
185 enc = xmlParseCharEncoding (encoding);
187 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
188 xmlGenericError (xmlGenericErrorContext,
189 "xmlDocDump: document not in UTF8\n");
192 if (enc != XML_CHAR_ENCODING_UTF8) {
193 handler = xmlFindCharEncodingHandler (encoding);
194 if (handler == NULL) {
195 xmlFree ((char *) cur->encoding);
196 cur->encoding = NULL;
201 buf = xmlOutputBufferCreateFile (out, handler);
203 indent = xmlIndentTreeOutput;
204 xmlIndentTreeOutput = 1;
205 ret = xmlSaveFormatFileTo (buf, cur, NULL, 1);
206 xmlIndentTreeOutput = indent;
208 /* apparently this doesn't return anything in libxml1 */
209 xmlDocDump (out, cur);
218 * @xml: a pointer to a GstXML object
219 * @doc: a pointer to an xml document to parse
220 * @root: The name of the root object to build
222 * Fills the GstXML object with the elements from the
225 * Returns: TRUE on success, FALSE otherwise
228 gst_xml_parse_doc (GstXML * xml, xmlDocPtr doc, const guchar * root)
230 xmlNodePtr field, cur;
233 cur = xmlDocGetRootElement (doc);
235 g_warning ("gstxml: empty document\n");
238 ns = xmlSearchNsByHref (doc, cur,
239 (xmlChar *) "http://gstreamer.net/gst-core/1.0/");
241 g_warning ("gstxml: document of wrong type, core namespace not found\n");
244 if (strcmp ((char *) cur->name, "gstreamer")) {
245 g_warning ("gstxml: XML file is in wrong format\n");
249 gst_class_signal_connect (GST_OBJECT_CLASS (G_OBJECT_GET_CLASS (xml)),
250 "object_loaded", (gpointer) gst_xml_object_loaded, xml);
254 field = cur->xmlChildrenNode;
257 if (!strcmp ((char *) field->name, "element") && (field->ns == xml->ns)) {
260 element = gst_xml_make_element (field, NULL);
262 xml->topelements = g_list_prepend (xml->topelements, element);
267 xml->topelements = g_list_reverse (xml->topelements);
272 /* FIXME 0.9: Why guchar*? */
274 * gst_xml_parse_file:
275 * @xml: a pointer to a GstXML object
276 * @fname: The filename with the xml description
277 * @root: The name of the root object to build
279 * Fills the GstXML object with the corresponding elements from
280 * the XML file fname. Optionally it will only build the element from
281 * the element node root (if it is not NULL). This feature is useful
282 * if you only want to build a specific element from an XML file
283 * but not the pipeline it is embedded in.
285 * Pass "-" as fname to read from stdin. You can also pass a URI
286 * of any format that libxml supports, including http.
288 * Returns: TRUE on success, FALSE otherwise
291 gst_xml_parse_file (GstXML * xml, const guchar * fname, const guchar * root)
296 g_return_val_if_fail (fname != NULL, FALSE);
298 doc = xmlParseFile ((char *) fname);
301 g_warning ("gstxml: XML file \"%s\" could not be read\n", fname);
305 ret = gst_xml_parse_doc (xml, doc, root);
311 /* FIXME 0.9: guchar* */
313 * gst_xml_parse_memory:
314 * @xml: a pointer to a GstXML object
315 * @buffer: a pointer to the in memory XML buffer
316 * @size: the size of the buffer
317 * @root: the name of the root objects to build
319 * Fills the GstXML object with the corresponding elements from
320 * an in memory XML buffer.
322 * Returns: TRUE on success
325 gst_xml_parse_memory (GstXML * xml, guchar * buffer, guint size,
331 g_return_val_if_fail (buffer != NULL, FALSE);
333 doc = xmlParseMemory ((char *) buffer, size);
335 ret = gst_xml_parse_doc (xml, doc, (const xmlChar *) root);
342 gst_xml_object_loaded (GstObject * private, GstObject * object, xmlNodePtr self,
345 GstXML *xml = GST_XML (data);
347 /* FIXME check that this element was created from the same xmlDocPtr... */
348 g_signal_emit (xml, gst_xml_signals[OBJECT_LOADED], 0, object, self);
352 * gst_xml_get_topelements:
353 * @xml: The GstXML to get the elements from
355 * Retrieve a list of toplevel elements.
357 * Returns: a GList of top-level elements. The caller does not own a copy
358 * of the list and must not free or modify the list. The caller also does not
359 * own a reference to any of the elements in the list and should obtain its own
360 * reference using gst_object_ref() if necessary.
363 gst_xml_get_topelements (GstXML * xml)
365 g_return_val_if_fail (xml != NULL, NULL);
367 return xml->topelements;
370 /* FIXME 0.11: why is the arg guchar* instead of gchar*? */
372 * gst_xml_get_element:
373 * @xml: The GstXML to get the element from
374 * @name: The name of element to retrieve
376 * This function is used to get a pointer to the GstElement corresponding
377 * to name in the pipeline description. You would use this if you have
378 * to do anything to the element after loading.
380 * Returns: a pointer to a new GstElement, caller owns returned reference.
383 gst_xml_get_element (GstXML * xml, const guchar * name)
388 g_return_val_if_fail (xml != NULL, NULL);
389 g_return_val_if_fail (name != NULL, NULL);
391 GST_DEBUG ("gstxml: getting element \"%s\"", name);
393 topelements = gst_xml_get_topelements (xml);
395 while (topelements) {
396 GstElement *top = GST_ELEMENT (topelements->data);
398 GST_DEBUG ("gstxml: getting element \"%s\"", name);
399 if (!strcmp (GST_ELEMENT_NAME (top), (char *) name)) {
400 return GST_ELEMENT_CAST (gst_object_ref (top));
402 if (GST_IS_BIN (top)) {
403 element = gst_bin_get_by_name (GST_BIN (top), (gchar *) name);
409 topelements = g_list_next (topelements);
415 * gst_xml_make_element:
417 * @parent: the parent of this object when it's loaded
419 * Load the element from the XML description
421 * Returns: the new element
424 gst_xml_make_element (xmlNodePtr cur, GstObject * parent)
426 xmlNodePtr children = cur->xmlChildrenNode;
431 /* first get the needed tags to construct the element */
433 if (!strcmp ((char *) children->name, "name")) {
434 name = (gchar *) xmlNodeGetContent (children);
435 } else if (!strcmp ((char *) children->name, "type")) {
436 type = (gchar *) xmlNodeGetContent (children);
438 children = children->next;
440 g_return_val_if_fail (name != NULL, NULL);
441 g_return_val_if_fail (type != NULL, NULL);
443 GST_CAT_INFO (GST_CAT_XML, "loading \"%s\" of type \"%s\"", name, type);
445 element = gst_element_factory_make (type, name);
447 g_return_val_if_fail (element != NULL, NULL);
452 /* ne need to set the parent on this object bacause the pads */
453 /* will go through the hierarchy to link to their peers */
455 if (GST_IS_BIN (parent)) {
456 gst_bin_add (GST_BIN (parent), element);
458 gst_object_set_parent (GST_OBJECT (element), parent);
462 gst_object_restore_thyself (GST_OBJECT (element), cur);