- Removed unused locking from the cothreads
[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 #include "gst_private.h"
24
25 #include "gstxml.h"
26 #include "gstlog.h"
27 #include "gstbin.h"
28
29 enum {
30   OBJECT_LOADED,
31   LAST_SIGNAL
32 };
33
34 static void     gst_xml_class_init              (GstXMLClass *klass);
35 static void     gst_xml_init                    (GstXML *xml);
36
37 static void     gst_xml_object_loaded           (GstObject *private, GstObject *object, xmlNodePtr self, gpointer data);
38
39 static GstObjectClass *parent_class = NULL;
40 static guint gst_xml_signals[LAST_SIGNAL] = { 0 };
41
42 GType
43 gst_xml_get_type(void)
44 {
45   static GType xml_type = 0;
46
47   if (!xml_type) {
48     static const GTypeInfo xml_info = {
49       sizeof(GstXMLClass),
50       NULL,
51       NULL,
52       (GClassInitFunc)gst_xml_class_init,
53       NULL,
54       NULL,
55       sizeof(GstXML),
56       0,
57       (GInstanceInitFunc)gst_xml_init,
58       NULL
59     };
60     xml_type = g_type_register_static (GST_TYPE_OBJECT, "GstXML", &xml_info, 0);
61   }
62   return xml_type;
63 }
64
65 static void
66 gst_xml_class_init (GstXMLClass *klass)
67 {
68   GObjectClass *gobject_class;
69
70   gobject_class = (GObjectClass *)klass;
71
72   parent_class = g_type_class_ref (GST_TYPE_OBJECT);
73
74   gst_xml_signals[OBJECT_LOADED] =
75     g_signal_new ("object_loaded", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
76                     G_STRUCT_OFFSET (GstXMLClass, object_loaded), NULL, NULL,
77                     gst_marshal_VOID__OBJECT_POINTER, G_TYPE_NONE, 2,
78                     GST_TYPE_OBJECT, G_TYPE_POINTER);
79
80 }
81
82 static void
83 gst_xml_init(GstXML *xml)
84 {
85   xml->topelements = NULL;
86 }
87
88 /**
89  * gst_xml_new:
90  *
91  * Create a new GstXML parser object.
92  *
93  * Returns: a pointer to a new GstXML object.
94  */
95 GstXML*
96 gst_xml_new (void)
97 {
98   return GST_XML (g_object_new(GST_TYPE_XML,NULL));
99 }
100
101 /**
102  * gst_xml_write:
103  * @element: The element to write out
104  *
105  * Converts the given element into an XML presentation.
106  *
107  * Returns: a pointer to an XML document
108  */
109 xmlDocPtr
110 gst_xml_write (GstElement *element)
111 {
112   xmlDocPtr doc;
113   xmlNodePtr elementnode;
114   xmlNsPtr ns;
115
116   doc = xmlNewDoc ("1.0");
117
118   doc->xmlRootNode = xmlNewDocNode (doc, NULL, "gstreamer", NULL);
119   
120   ns = xmlNewNs (doc->xmlRootNode, "http://gstreamer.net/gst-core/1.0/", "gst");
121   
122   elementnode = xmlNewChild (doc->xmlRootNode, ns, "element", NULL);
123
124   gst_object_save_thyself (GST_OBJECT (element), elementnode);
125
126   return doc;
127 }
128
129 /**
130  * gst_xml_write_file:
131  * @element: The element to write out
132  * @out: an open file, like stdout
133  *
134  * Converts the given element into XML and writes the formatted XML to an open
135  * file.
136  *
137  * Returns: number of bytes written on success, -1 otherwise.
138  */
139 gint
140 gst_xml_write_file (GstElement *element, FILE *out)
141 {
142   xmlDocPtr cur;
143 #ifdef HAVE_LIBXML2
144   xmlOutputBufferPtr buf;
145 #endif
146   const char * encoding;
147   xmlCharEncodingHandlerPtr handler = NULL;
148   int indent;
149   gboolean ret;
150   
151   cur = gst_xml_write (element);
152   if (!cur) return -1;
153   
154 #ifdef HAVE_LIBXML2
155   encoding = (const char *) cur->encoding;
156   
157   if (encoding != NULL) {
158     xmlCharEncoding enc;
159     
160     enc = xmlParseCharEncoding (encoding);
161     
162     if (cur->charset != XML_CHAR_ENCODING_UTF8) {
163       xmlGenericError (xmlGenericErrorContext,
164                        "xmlDocDump: document not in UTF8\n");
165       return -1;
166     }
167     if (enc != XML_CHAR_ENCODING_UTF8) {
168       handler = xmlFindCharEncodingHandler (encoding);
169       if (handler == NULL) {
170         xmlFree ((char *) cur->encoding);
171         cur->encoding = NULL;
172       }
173     }
174   }
175   
176   buf = xmlOutputBufferCreateFile (out, handler);
177   
178   indent = xmlIndentTreeOutput;
179   xmlIndentTreeOutput = 1;
180   ret = xmlSaveFormatFileTo(buf, cur, NULL, 1);
181   xmlIndentTreeOutput = indent;
182 #else
183   /* apparently this doesn't return anything in libxml1 */
184   xmlDocDump (out, cur);
185   ret = 1;
186 #endif
187   
188   return ret;
189 }
190
191 /**
192  * gst_xml_parse_doc:
193  * @xml: a pointer to a GstXML object
194  * @doc: a pointer to an xml document to parse
195  * @root: The name of the root object to build
196  *
197  * Fills the GstXML object with the elements from the
198  * xmlDocPtr.
199  *
200  * Returns: TRUE on success, FALSE otherwise
201  */
202 gboolean
203 gst_xml_parse_doc (GstXML *xml, xmlDocPtr doc, const guchar *root)
204 {
205   xmlNodePtr field, cur;
206   xmlNsPtr ns;
207
208   cur = xmlDocGetRootElement(doc);
209   if (cur == NULL) {
210     g_warning("gstxml: empty document\n");
211     return FALSE ;
212   }
213   ns = xmlSearchNsByHref(doc, cur, "http://gstreamer.net/gst-core/1.0/");
214   if (ns == NULL) {
215     g_warning("gstxml: document of wrong type, core namespace not found\n");
216     return FALSE;
217   }
218   if (strcmp(cur->name, "gstreamer")) {
219     g_warning("gstxml: XML file is in wrong format\n");
220     return FALSE;
221   }
222
223   gst_class_signal_connect (GST_OBJECT_CLASS (G_OBJECT_GET_CLASS(xml)),
224                   "object_loaded", gst_xml_object_loaded, xml);
225
226   xml->ns = ns;
227
228   field = cur->xmlChildrenNode;
229
230   while (field) {
231     if (!strcmp(field->name, "element") && (field->ns == xml->ns)) {
232       GstElement *element;
233
234       element = gst_xml_make_element (field, NULL);
235
236       xml->topelements = g_list_prepend (xml->topelements, element);
237     }
238     field = field->next;
239   }
240
241   xml->topelements = g_list_reverse (xml->topelements);
242
243   return TRUE;
244 }
245
246 /**
247  * gst_xml_parse_file:
248  * @xml: a pointer to a GstXML object
249  * @fname: The filename with the xml description
250  * @root: The name of the root object to build
251  *
252  * Fills the GstXML object with the corresponding elements from
253  * the XML file fname. Optionally it will only build the element from
254  * the element node root (if it is not NULL). This feature is useful
255  * if you only want to build a specific element from an XML file
256  * but not the pipeline it is embedded in.
257  *
258  * Pass "-" as fname to read from stdin. You can also pass a URI
259  * of any format that libxml supports, including http.
260  *
261  * Returns: TRUE on success, FALSE otherwise
262  */
263 gboolean
264 gst_xml_parse_file (GstXML *xml, const guchar *fname, const guchar *root)
265 {
266   xmlDocPtr doc;
267
268   g_return_val_if_fail(fname != NULL, FALSE);
269
270   doc = xmlParseFile(fname);
271
272   if (!doc) {
273     g_warning("gstxml: XML file \"%s\" could not be read\n", fname);
274     return FALSE;
275   }
276
277   return gst_xml_parse_doc (xml, doc, root);
278 }
279
280 /**
281  * gst_xml_parse_memory:
282  * @xml: a pointer to a GstXML object
283  * @buffer: a pointer to the in memory XML buffer
284  * @size: the size of the buffer
285  * @root: the name of the root objects to build
286  *
287  * Fills the GstXML object with the corresponding elements from
288  * an in memory XML buffer.
289  *
290  * Returns: TRUE on success
291  */
292 gboolean
293 gst_xml_parse_memory (GstXML *xml, guchar *buffer, guint size, const gchar *root)
294 {
295   xmlDocPtr doc;
296
297   g_return_val_if_fail(buffer != NULL, FALSE);
298
299   doc = xmlParseMemory (buffer, size);
300
301   return gst_xml_parse_doc (xml, doc, root);
302 }
303
304 static void
305 gst_xml_object_loaded (GstObject *private, GstObject *object, xmlNodePtr self, gpointer data)
306 {
307   GstXML *xml = GST_XML (data);
308
309   /* FIXME check that this element was created from the same xmlDocPtr...*/
310   g_signal_emit (G_OBJECT (xml), gst_xml_signals[OBJECT_LOADED], 0, object, self);
311 }
312
313 /**
314  * gst_xml_get_topelements:
315  * @xml: The GstXML to get the elements from
316  *
317  * Retrive a list of toplevel elements.
318  *
319  * Returns: a GList of elements
320  */
321 GList*
322 gst_xml_get_topelements (GstXML *xml)
323 {
324   g_return_val_if_fail (xml != NULL, NULL);
325
326   return xml->topelements;
327 }
328
329 /**
330  * gst_xml_get_element:
331  * @xml: The GstXML to get the element from
332  * @name: The name of element to retreive
333  *
334  * This function is used to get a pointer to the GstElement corresponding
335  * to name in the pipeline description. You would use this if you have
336  * to do anything to the element after loading.
337  *
338  * Returns: a pointer to a new GstElement
339  */
340 GstElement*
341 gst_xml_get_element (GstXML *xml, const guchar *name)
342 {
343   GstElement *element;
344   GList *topelements;
345
346   g_return_val_if_fail(xml != NULL, NULL);
347   g_return_val_if_fail(name != NULL, NULL);
348
349   GST_DEBUG (0,"gstxml: getting element \"%s\"", name);
350
351   topelements = gst_xml_get_topelements (xml);
352
353   while (topelements) {
354     GstElement *top = GST_ELEMENT (topelements->data);
355
356     GST_DEBUG (0,"gstxml: getting element \"%s\"", name);
357     if (!strcmp (GST_ELEMENT_NAME (top), name)) {
358       return top;
359     }
360     else {
361       if (GST_IS_BIN (top)) {
362         element = gst_bin_get_by_name (GST_BIN (top), name);
363
364         if (element)
365           return element;
366       }
367     }
368     topelements = g_list_next (topelements);
369   }
370   return NULL;
371 }
372
373 /**
374  * gst_xml_make_element:
375  * @cur: the xml node
376  * @parent: the parent of this object when it's loaded
377  *
378  * Load the element from the XML description
379  *
380  * Returns: the new element
381  */
382 GstElement*
383 gst_xml_make_element (xmlNodePtr cur, GstObject *parent)
384 {
385   xmlNodePtr children = cur->xmlChildrenNode;
386   GstElement *element;
387   guchar *name = NULL;
388   guchar *type = NULL;
389
390   /* first get the needed tags to construct the element */
391   while (children) {
392     if (!strcmp (children->name, "name")) {
393       name = xmlNodeGetContent (children);
394     } else if (!strcmp (children->name, "type")) {
395       type = xmlNodeGetContent (children);
396     }
397     children = children->next;
398   }
399   g_return_val_if_fail (name != NULL, NULL);
400   g_return_val_if_fail (type != NULL, NULL);
401
402   GST_INFO (GST_CAT_XML,"loading \"%s\" of type \"%s\"", name, type);
403
404   element = gst_element_factory_make (type, name);
405
406   g_return_val_if_fail (element != NULL, NULL);
407
408   /* ne need to set the parent on this object bacause the pads */
409   /* will go through the hierarchy to connect to thier peers */
410   if (parent)
411     gst_object_set_parent (GST_OBJECT (element), parent);
412   
413   gst_object_restore_thyself (GST_OBJECT (element), cur);
414   
415   return element;
416 }