04522fd3bfe541e30b816ab2b748232e45735c1a
[platform/upstream/gstreamer.git] / gst / gstxml.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstxml.c: XML save/restore of pipelines
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /**
24  * SECTION:gstxml
25  * @short_description: XML save/restore operations of pipelines
26  *
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
31  * graph.
32  *
33  * #GstElement implementations need to override the #GstObjectClass.save_thyself()
34  * and #GstObjectClass.restore_thyself() virtual functions of #GstObject.
35  */
36
37 #include "gst_private.h"
38
39 #include "gstxml.h"
40 #include "gstmarshal.h"
41 #include "gstinfo.h"
42 #include "gstbin.h"
43
44 enum
45 {
46   OBJECT_LOADED,
47   LAST_SIGNAL
48 };
49
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);
53
54 static void gst_xml_object_loaded (GstObject * private, GstObject * object,
55     xmlNodePtr self, gpointer data);
56
57 static GstObjectClass *parent_class = NULL;
58 static guint gst_xml_signals[LAST_SIGNAL] = { 0 };
59
60 G_DEFINE_TYPE (GstXML, gst_xml, GST_TYPE_OBJECT);
61
62 static void
63 gst_xml_class_init (GstXMLClass * klass)
64 {
65   GObjectClass *gobject_class = (GObjectClass *) klass;
66
67   parent_class = g_type_class_peek_parent (klass);
68
69   gobject_class->dispose = gst_xml_dispose;
70
71   /* FIXME G_TYPE_POINTER should be GType of xmlNodePtr
72    * (ensonic) can't be fixed, as libxml does not use GObject (unfortunately)
73    */
74   /**
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
79    *
80    * Signals that a new object has been deserialized.
81    */
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,
86       G_TYPE_POINTER);
87
88 }
89
90 static void
91 gst_xml_init (GstXML * xml)
92 {
93   xml->topelements = NULL;
94 }
95
96 static void
97 gst_xml_dispose (GObject * object)
98 {
99   GstXML *xml = GST_XML (object);
100
101   g_list_foreach (xml->topelements, (GFunc) gst_object_unref, NULL);
102   g_list_free (xml->topelements);
103   xml->topelements = NULL;
104
105   G_OBJECT_CLASS (parent_class)->dispose (object);
106 }
107
108 /**
109  * gst_xml_new:
110  *
111  * Create a new GstXML parser object.
112  *
113  * Returns: a pointer to a new GstXML object.
114  */
115 GstXML *
116 gst_xml_new (void)
117 {
118   return GST_XML (g_object_newv (GST_TYPE_XML, 0, NULL));
119 }
120
121 /**
122  * gst_xml_write:
123  * @element: The element to write out
124  *
125  * Converts the given element into an XML presentation.
126  *
127  * Returns: a pointer to an XML document
128  */
129 xmlDocPtr
130 gst_xml_write (GstElement * element)
131 {
132   xmlDocPtr doc;
133   xmlNodePtr elementnode;
134   xmlNsPtr gst_ns;
135
136   doc = xmlNewDoc ((xmlChar *) "1.0");
137
138   doc->xmlRootNode = xmlNewDocNode (doc, NULL, (xmlChar *) "gstreamer", NULL);
139
140   gst_ns =
141       xmlNewNs (doc->xmlRootNode,
142       (xmlChar *) "http://gstreamer.net/gst-core/1.0/", (xmlChar *) "gst");
143
144   elementnode = xmlNewChild (doc->xmlRootNode, gst_ns, (xmlChar *) "element",
145       NULL);
146
147   gst_object_save_thyself (GST_OBJECT (element), elementnode);
148
149   return doc;
150 }
151
152 /**
153  * gst_xml_write_file:
154  * @element: The element to write out
155  * @out: an open file, like stdout
156  *
157  * Converts the given element into XML and writes the formatted XML to an open
158  * file.
159  *
160  * Returns: number of bytes written on success, -1 otherwise.
161  */
162 gint
163 gst_xml_write_file (GstElement * element, FILE * out)
164 {
165   xmlDocPtr cur;
166
167 #ifdef HAVE_LIBXML2
168   xmlOutputBufferPtr buf;
169 #endif
170   const char *encoding;
171   xmlCharEncodingHandlerPtr handler = NULL;
172   int indent;
173   gboolean ret;
174
175   cur = gst_xml_write (element);
176   if (!cur)
177     return -1;
178
179 #ifdef HAVE_LIBXML2
180   encoding = (const char *) cur->encoding;
181
182   if (encoding != NULL) {
183     xmlCharEncoding enc;
184
185     enc = xmlParseCharEncoding (encoding);
186
187     if (cur->charset != XML_CHAR_ENCODING_UTF8) {
188       xmlGenericError (xmlGenericErrorContext,
189           "xmlDocDump: document not in UTF8\n");
190       return -1;
191     }
192     if (enc != XML_CHAR_ENCODING_UTF8) {
193       handler = xmlFindCharEncodingHandler (encoding);
194       if (handler == NULL) {
195         xmlFree ((char *) cur->encoding);
196         cur->encoding = NULL;
197       }
198     }
199   }
200
201   buf = xmlOutputBufferCreateFile (out, handler);
202
203   indent = xmlIndentTreeOutput;
204   xmlIndentTreeOutput = 1;
205   ret = xmlSaveFormatFileTo (buf, cur, NULL, 1);
206   xmlIndentTreeOutput = indent;
207 #else
208   /* apparently this doesn't return anything in libxml1 */
209   xmlDocDump (out, cur);
210   ret = 1;
211 #endif
212
213   return ret;
214 }
215
216 /**
217  * gst_xml_parse_doc:
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
221  *
222  * Fills the GstXML object with the elements from the
223  * xmlDocPtr.
224  *
225  * Returns: TRUE on success, FALSE otherwise
226  */
227 gboolean
228 gst_xml_parse_doc (GstXML * xml, xmlDocPtr doc, const guchar * root)
229 {
230   xmlNodePtr field, cur;
231   xmlNsPtr ns;
232
233   cur = xmlDocGetRootElement (doc);
234   if (cur == NULL) {
235     g_warning ("gstxml: empty document\n");
236     return FALSE;
237   }
238   ns = xmlSearchNsByHref (doc, cur,
239       (xmlChar *) "http://gstreamer.net/gst-core/1.0/");
240   if (ns == NULL) {
241     g_warning ("gstxml: document of wrong type, core namespace not found\n");
242     return FALSE;
243   }
244   if (strcmp ((char *) cur->name, "gstreamer")) {
245     g_warning ("gstxml: XML file is in wrong format\n");
246     return FALSE;
247   }
248
249   gst_class_signal_connect (GST_OBJECT_CLASS (G_OBJECT_GET_CLASS (xml)),
250       "object_loaded", (gpointer) gst_xml_object_loaded, xml);
251
252   xml->ns = ns;
253
254   field = cur->xmlChildrenNode;
255
256   while (field) {
257     if (!strcmp ((char *) field->name, "element") && (field->ns == xml->ns)) {
258       GstElement *element;
259
260       element = gst_xml_make_element (field, NULL);
261
262       xml->topelements = g_list_prepend (xml->topelements, element);
263     }
264     field = field->next;
265   }
266
267   xml->topelements = g_list_reverse (xml->topelements);
268
269   return TRUE;
270 }
271
272 /* FIXME 0.9: Why guchar*? */
273 /**
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
278  *
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.
284  *
285  * Pass "-" as fname to read from stdin. You can also pass a URI
286  * of any format that libxml supports, including http.
287  *
288  * Returns: TRUE on success, FALSE otherwise
289  */
290 gboolean
291 gst_xml_parse_file (GstXML * xml, const guchar * fname, const guchar * root)
292 {
293   xmlDocPtr doc;
294   gboolean ret;
295
296   g_return_val_if_fail (fname != NULL, FALSE);
297
298   doc = xmlParseFile ((char *) fname);
299
300   if (!doc) {
301     g_warning ("gstxml: XML file \"%s\" could not be read\n", fname);
302     return FALSE;
303   }
304
305   ret = gst_xml_parse_doc (xml, doc, root);
306
307   xmlFreeDoc (doc);
308   return ret;
309 }
310
311 /* FIXME 0.9: guchar* */
312 /**
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
318  *
319  * Fills the GstXML object with the corresponding elements from
320  * an in memory XML buffer.
321  *
322  * Returns: TRUE on success
323  */
324 gboolean
325 gst_xml_parse_memory (GstXML * xml, guchar * buffer, guint size,
326     const gchar * root)
327 {
328   xmlDocPtr doc;
329   gboolean ret;
330
331   g_return_val_if_fail (buffer != NULL, FALSE);
332
333   doc = xmlParseMemory ((char *) buffer, size);
334
335   ret = gst_xml_parse_doc (xml, doc, (const xmlChar *) root);
336
337   xmlFreeDoc (doc);
338   return ret;
339 }
340
341 static void
342 gst_xml_object_loaded (GstObject * private, GstObject * object, xmlNodePtr self,
343     gpointer data)
344 {
345   GstXML *xml = GST_XML (data);
346
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);
349 }
350
351 /**
352  * gst_xml_get_topelements:
353  * @xml: The GstXML to get the elements from
354  *
355  * Retrieve a list of toplevel elements.
356  *
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.
361  */
362 GList *
363 gst_xml_get_topelements (GstXML * xml)
364 {
365   g_return_val_if_fail (xml != NULL, NULL);
366
367   return xml->topelements;
368 }
369
370 /* FIXME 0.11: why is the arg guchar* instead of gchar*? */
371 /**
372  * gst_xml_get_element:
373  * @xml: The GstXML to get the element from
374  * @name: The name of element to retrieve
375  *
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.
379  *
380  * Returns: a pointer to a new GstElement, caller owns returned reference.
381  */
382 GstElement *
383 gst_xml_get_element (GstXML * xml, const guchar * name)
384 {
385   GstElement *element;
386   GList *topelements;
387
388   g_return_val_if_fail (xml != NULL, NULL);
389   g_return_val_if_fail (name != NULL, NULL);
390
391   GST_DEBUG ("gstxml: getting element \"%s\"", name);
392
393   topelements = gst_xml_get_topelements (xml);
394
395   while (topelements) {
396     GstElement *top = GST_ELEMENT (topelements->data);
397
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));
401     } else {
402       if (GST_IS_BIN (top)) {
403         element = gst_bin_get_by_name (GST_BIN (top), (gchar *) name);
404
405         if (element)
406           return element;
407       }
408     }
409     topelements = g_list_next (topelements);
410   }
411   return NULL;
412 }
413
414 /**
415  * gst_xml_make_element:
416  * @cur: the xml node
417  * @parent: the parent of this object when it's loaded
418  *
419  * Load the element from the XML description
420  *
421  * Returns: the new element
422  */
423 GstElement *
424 gst_xml_make_element (xmlNodePtr cur, GstObject * parent)
425 {
426   xmlNodePtr children = cur->xmlChildrenNode;
427   GstElement *element;
428   gchar *name = NULL;
429   gchar *type = NULL;
430
431   /* first get the needed tags to construct the element */
432   while (children) {
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);
437     }
438     children = children->next;
439   }
440   g_return_val_if_fail (name != NULL, NULL);
441   g_return_val_if_fail (type != NULL, NULL);
442
443   GST_CAT_INFO (GST_CAT_XML, "loading \"%s\" of type \"%s\"", name, type);
444
445   element = gst_element_factory_make (type, name);
446
447   g_return_val_if_fail (element != NULL, NULL);
448
449   g_free (type);
450   g_free (name);
451
452   /* ne need to set the parent on this object bacause the pads */
453   /* will go through the hierarchy to link to their peers */
454   if (parent) {
455     if (GST_IS_BIN (parent)) {
456       gst_bin_add (GST_BIN (parent), element);
457     } else {
458       gst_object_set_parent (GST_OBJECT (element), parent);
459     }
460   }
461
462   gst_object_restore_thyself (GST_OBJECT (element), cur);
463
464   return element;
465 }