~ubuntu-branches/ubuntu/hardy/gstreamer0.10/hardy-updates

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wtay@chello.be>
 *
 * gstxml.c: XML save/restore of pipelines
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/**
 * SECTION:gstxml
 * @short_description: XML save/restore operations of pipelines
 *
 * GStreamer pipelines can be saved to xml files using gst_xml_write_file().
 * They can be loaded back using gst_xml_parse_doc() / gst_xml_parse_file() / 
 * gst_xml_parse_memory().
 * Additionally one can load saved pipelines into the gst-editor to inspect the
 * graph.
 *
 * #GstElement implementations need to override gst_object_save_thyself() and
 * gst_object_restore_thyself().
 */

#include "gst_private.h"

#include "gstxml.h"
#include "gstmarshal.h"
#include "gstinfo.h"
#include "gstbin.h"

enum
{
  OBJECT_LOADED,
  LAST_SIGNAL
};

static void gst_xml_class_init (GstXMLClass * klass);
static void gst_xml_init (GstXML * xml);
static void gst_xml_dispose (GObject * object);

static void gst_xml_object_loaded (GstObject * private, GstObject * object,
    xmlNodePtr self, gpointer data);

static GstObjectClass *parent_class = NULL;
static guint gst_xml_signals[LAST_SIGNAL] = { 0 };

GType
gst_xml_get_type (void)
{
  static GType xml_type = 0;

  if (G_UNLIKELY (xml_type == 0)) {
    static const GTypeInfo xml_info = {
      sizeof (GstXMLClass),
      NULL,
      NULL,
      (GClassInitFunc) gst_xml_class_init,
      NULL,
      NULL,
      sizeof (GstXML),
      0,
      (GInstanceInitFunc) gst_xml_init,
      NULL
    };

    xml_type = g_type_register_static (GST_TYPE_OBJECT, "GstXML", &xml_info, 0);
  }
  return xml_type;
}

static void
gst_xml_class_init (GstXMLClass * klass)
{
  GObjectClass *gobject_class;

  gobject_class = (GObjectClass *) klass;

  parent_class = g_type_class_peek_parent (klass);

  gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_xml_dispose);

  /* FIXME G_TYPE_POINTER should be GType of xmlNodePtr
   * (ensonic) can't be fixed, as libxml does not use GObject (unfortunately)
   */
  /**
   * GstXML::object-loaded:
   * @xml: the xml persistence instance
   * @object: the object that has been loaded
   * @xml_node: the related xml_node pointer to the document tree
   *
   * Signals that a new object has been deserialized.
   */
  gst_xml_signals[OBJECT_LOADED] =
      g_signal_new ("object-loaded", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstXMLClass, object_loaded), NULL,
      NULL, gst_marshal_VOID__OBJECT_POINTER, G_TYPE_NONE, 2, GST_TYPE_OBJECT,
      G_TYPE_POINTER);

}

static void
gst_xml_init (GstXML * xml)
{
  xml->topelements = NULL;
}

static void
gst_xml_dispose (GObject * object)
{
  GstXML *xml = GST_XML (object);

  g_list_foreach (xml->topelements, (GFunc) gst_object_unref, NULL);
  g_list_free (xml->topelements);
  xml->topelements = NULL;

  G_OBJECT_CLASS (parent_class)->dispose (object);
}

/**
 * gst_xml_new:
 *
 * Create a new GstXML parser object.
 *
 * Returns: a pointer to a new GstXML object.
 */
GstXML *
gst_xml_new (void)
{
  return GST_XML (g_object_new (GST_TYPE_XML, NULL));
}

/**
 * gst_xml_write:
 * @element: The element to write out
 *
 * Converts the given element into an XML presentation.
 *
 * Returns: a pointer to an XML document
 */
xmlDocPtr
gst_xml_write (GstElement * element)
{
  xmlDocPtr doc;
  xmlNodePtr elementnode;
  xmlNsPtr gst_ns;

  doc = xmlNewDoc ((xmlChar *) "1.0");

  doc->xmlRootNode = xmlNewDocNode (doc, NULL, (xmlChar *) "gstreamer", NULL);

  gst_ns =
      xmlNewNs (doc->xmlRootNode,
      (xmlChar *) "http://gstreamer.net/gst-core/1.0/", (xmlChar *) "gst");

  elementnode = xmlNewChild (doc->xmlRootNode, gst_ns, (xmlChar *) "element",
      NULL);

  gst_object_save_thyself (GST_OBJECT (element), elementnode);

  return doc;
}

/**
 * gst_xml_write_file:
 * @element: The element to write out
 * @out: an open file, like stdout
 *
 * Converts the given element into XML and writes the formatted XML to an open
 * file.
 *
 * Returns: number of bytes written on success, -1 otherwise.
 */
gint
gst_xml_write_file (GstElement * element, FILE * out)
{
  xmlDocPtr cur;

#ifdef HAVE_LIBXML2
  xmlOutputBufferPtr buf;
#endif
  const char *encoding;
  xmlCharEncodingHandlerPtr handler = NULL;
  int indent;
  gboolean ret;

  cur = gst_xml_write (element);
  if (!cur)
    return -1;

#ifdef HAVE_LIBXML2
  encoding = (const char *) cur->encoding;

  if (encoding != NULL) {
    xmlCharEncoding enc;

    enc = xmlParseCharEncoding (encoding);

    if (cur->charset != XML_CHAR_ENCODING_UTF8) {
      xmlGenericError (xmlGenericErrorContext,
          "xmlDocDump: document not in UTF8\n");
      return -1;
    }
    if (enc != XML_CHAR_ENCODING_UTF8) {
      handler = xmlFindCharEncodingHandler (encoding);
      if (handler == NULL) {
        xmlFree ((char *) cur->encoding);
        cur->encoding = NULL;
      }
    }
  }

  buf = xmlOutputBufferCreateFile (out, handler);

  indent = xmlIndentTreeOutput;
  xmlIndentTreeOutput = 1;
  ret = xmlSaveFormatFileTo (buf, cur, NULL, 1);
  xmlIndentTreeOutput = indent;
#else
  /* apparently this doesn't return anything in libxml1 */
  xmlDocDump (out, cur);
  ret = 1;
#endif

  return ret;
}

/**
 * gst_xml_parse_doc:
 * @xml: a pointer to a GstXML object
 * @doc: a pointer to an xml document to parse
 * @root: The name of the root object to build
 *
 * Fills the GstXML object with the elements from the
 * xmlDocPtr.
 *
 * Returns: TRUE on success, FALSE otherwise
 */
gboolean
gst_xml_parse_doc (GstXML * xml, xmlDocPtr doc, const guchar * root)
{
  xmlNodePtr field, cur;
  xmlNsPtr ns;

  cur = xmlDocGetRootElement (doc);
  if (cur == NULL) {
    g_warning ("gstxml: empty document\n");
    return FALSE;
  }
  ns = xmlSearchNsByHref (doc, cur,
      (xmlChar *) "http://gstreamer.net/gst-core/1.0/");
  if (ns == NULL) {
    g_warning ("gstxml: document of wrong type, core namespace not found\n");
    return FALSE;
  }
  if (strcmp ((char *) cur->name, "gstreamer")) {
    g_warning ("gstxml: XML file is in wrong format\n");
    return FALSE;
  }

  gst_class_signal_connect (GST_OBJECT_CLASS (G_OBJECT_GET_CLASS (xml)),
      "object_loaded", (gpointer) gst_xml_object_loaded, xml);

  xml->ns = ns;

  field = cur->xmlChildrenNode;

  while (field) {
    if (!strcmp ((char *) field->name, "element") && (field->ns == xml->ns)) {
      GstElement *element;

      element = gst_xml_make_element (field, NULL);

      xml->topelements = g_list_prepend (xml->topelements, element);
    }
    field = field->next;
  }

  xml->topelements = g_list_reverse (xml->topelements);

  return TRUE;
}

/* FIXME 0.9: Why guchar*? */
/**
 * gst_xml_parse_file:
 * @xml: a pointer to a GstXML object
 * @fname: The filename with the xml description
 * @root: The name of the root object to build
 *
 * Fills the GstXML object with the corresponding elements from
 * the XML file fname. Optionally it will only build the element from
 * the element node root (if it is not NULL). This feature is useful
 * if you only want to build a specific element from an XML file
 * but not the pipeline it is embedded in.
 *
 * Pass "-" as fname to read from stdin. You can also pass a URI
 * of any format that libxml supports, including http.
 *
 * Returns: TRUE on success, FALSE otherwise
 */
gboolean
gst_xml_parse_file (GstXML * xml, const guchar * fname, const guchar * root)
{
  xmlDocPtr doc;
  gboolean ret;

  g_return_val_if_fail (fname != NULL, FALSE);

  doc = xmlParseFile ((char *) fname);

  if (!doc) {
    g_warning ("gstxml: XML file \"%s\" could not be read\n", fname);
    return FALSE;
  }

  ret = gst_xml_parse_doc (xml, doc, root);

  xmlFreeDoc (doc);
  return ret;
}

/* FIXME 0.9: guchar* */
/**
 * gst_xml_parse_memory:
 * @xml: a pointer to a GstXML object
 * @buffer: a pointer to the in memory XML buffer
 * @size: the size of the buffer
 * @root: the name of the root objects to build
 *
 * Fills the GstXML object with the corresponding elements from
 * an in memory XML buffer.
 *
 * Returns: TRUE on success
 */
gboolean
gst_xml_parse_memory (GstXML * xml, guchar * buffer, guint size,
    const gchar * root)
{
  xmlDocPtr doc;
  gboolean ret;

  g_return_val_if_fail (buffer != NULL, FALSE);

  doc = xmlParseMemory ((char *) buffer, size);

  ret = gst_xml_parse_doc (xml, doc, (const xmlChar *) root);

  xmlFreeDoc (doc);
  return ret;
}

static void
gst_xml_object_loaded (GstObject * private, GstObject * object, xmlNodePtr self,
    gpointer data)
{
  GstXML *xml = GST_XML (data);

  /* FIXME check that this element was created from the same xmlDocPtr... */
  g_signal_emit (G_OBJECT (xml), gst_xml_signals[OBJECT_LOADED], 0, object,
      self);
}

/**
 * gst_xml_get_topelements:
 * @xml: The GstXML to get the elements from
 *
 * Retrieve a list of toplevel elements.
 *
 * Returns: a GList of top-level elements. The caller does not own a copy
 * of the list and must not free or modify the list. The caller also does not
 * own a reference to any of the elements in the list and should obtain its own
 * reference using gst_object_ref() if necessary.
 */
GList *
gst_xml_get_topelements (GstXML * xml)
{
  g_return_val_if_fail (xml != NULL, NULL);

  return xml->topelements;
}

/* FIXME 0.11: why is the arg guchar* instead of gchar*? */
/**
 * gst_xml_get_element:
 * @xml: The GstXML to get the element from
 * @name: The name of element to retrieve
 *
 * This function is used to get a pointer to the GstElement corresponding
 * to name in the pipeline description. You would use this if you have
 * to do anything to the element after loading.
 *
 * Returns: a pointer to a new GstElement, caller owns returned reference.
 */
GstElement *
gst_xml_get_element (GstXML * xml, const guchar * name)
{
  GstElement *element;
  GList *topelements;

  g_return_val_if_fail (xml != NULL, NULL);
  g_return_val_if_fail (name != NULL, NULL);

  GST_DEBUG ("gstxml: getting element \"%s\"", name);

  topelements = gst_xml_get_topelements (xml);

  while (topelements) {
    GstElement *top = GST_ELEMENT (topelements->data);

    GST_DEBUG ("gstxml: getting element \"%s\"", name);
    if (!strcmp (GST_ELEMENT_NAME (top), (char *) name)) {
      return GST_ELEMENT_CAST (gst_object_ref (top));
    } else {
      if (GST_IS_BIN (top)) {
        element = gst_bin_get_by_name (GST_BIN (top), (gchar *) name);

        if (element)
          return element;
      }
    }
    topelements = g_list_next (topelements);
  }
  return NULL;
}

/**
 * gst_xml_make_element:
 * @cur: the xml node
 * @parent: the parent of this object when it's loaded
 *
 * Load the element from the XML description
 *
 * Returns: the new element
 */
GstElement *
gst_xml_make_element (xmlNodePtr cur, GstObject * parent)
{
  xmlNodePtr children = cur->xmlChildrenNode;
  GstElement *element;
  gchar *name = NULL;
  gchar *type = NULL;

  /* first get the needed tags to construct the element */
  while (children) {
    if (!strcmp ((char *) children->name, "name")) {
      name = (gchar *) xmlNodeGetContent (children);
    } else if (!strcmp ((char *) children->name, "type")) {
      type = (gchar *) xmlNodeGetContent (children);
    }
    children = children->next;
  }
  g_return_val_if_fail (name != NULL, NULL);
  g_return_val_if_fail (type != NULL, NULL);

  GST_CAT_INFO (GST_CAT_XML, "loading \"%s\" of type \"%s\"", name, type);

  element = gst_element_factory_make (type, name);

  g_return_val_if_fail (element != NULL, NULL);

  g_free (type);
  g_free (name);

  /* ne need to set the parent on this object bacause the pads */
  /* will go through the hierarchy to link to their peers */
  if (parent) {
    if (GST_IS_BIN (parent)) {
      gst_bin_add (GST_BIN (parent), element);
    } else {
      gst_object_set_parent (GST_OBJECT (element), parent);
    }
  }

  gst_object_restore_thyself (GST_OBJECT (element), cur);

  return element;
}