ges-xml-formatter.c 54 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* Gstreamer Editing Services
 *
 * Copyright (C) <2012> Thibault Saunier <thibault.saunier@collabora.com>
 *
 * 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.
 */
20
21
22
23
#ifdef HAVE_CONFIG_H
#include "config.h"
#undef VERSION
#endif
24
25
26
27


/* TODO Determine error codes numbers */

28
#include <string.h>
29
#include <errno.h>
30
31
32
#include <locale.h>

#include "ges.h"
33
34
35
36
#include "ges-internal.h"

#define parent_class ges_xml_formatter_parent_class
#define API_VERSION 0
37
38
#define MINOR_VERSION 5
#define VERSION 0.5
39
40
41

#define COLLECT_STR_OPT (G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL)

42
#define _GET_PRIV(o) (((GESXmlFormatter*)o)->priv)
43
44
45
46
47
48
49

struct _GESXmlFormatterPrivate
{
  gboolean ges_opened;
  gboolean project_opened;

  GString *str;
50
51
52
53

  GHashTable *element_id;

  guint nbelements;
54
55

  guint min_version;
56
57
};

58
59
60
G_DEFINE_TYPE_WITH_PRIVATE (GESXmlFormatter, ges_xml_formatter,
    GES_TYPE_BASE_XML_FORMATTER);

61
62
63
64
65
static inline void
_parse_ges_element (GMarkupParseContext * context, const gchar * element_name,
    const gchar ** attribute_names, const gchar ** attribute_values,
    GESXmlFormatter * self, GError ** error)
{
66
  guint api_version;
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
  const gchar *version, *properties;

  gchar **split_version = NULL;

  if (g_strcmp0 (element_name, "ges")) {
    g_set_error (error, G_MARKUP_ERROR,
        G_MARKUP_ERROR_INVALID_CONTENT,
        "element '%s', Missing <ges> element'", element_name);
    return;
  }

  if (!g_markup_collect_attributes (element_name, attribute_names,
          attribute_values, error, G_MARKUP_COLLECT_STRING, "version", &version,
          COLLECT_STR_OPT, "properties", &properties,
          G_MARKUP_COLLECT_INVALID)) {
    return;
  }

  split_version = g_strsplit (version, ".", 2);
  if (split_version[1] == NULL)
    goto failed;

  errno = 0;
  api_version = g_ascii_strtoull (split_version[0], NULL, 10);
  if (errno || api_version != API_VERSION)
    goto stroull_failed;

94
95
  self->priv->min_version = g_ascii_strtoull (split_version[1], NULL, 10);
  if (self->priv->min_version > MINOR_VERSION)
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
    goto failed;

  _GET_PRIV (self)->ges_opened = TRUE;
  g_strfreev (split_version);
  return;

failed:
  g_set_error (error, G_MARKUP_ERROR,
      G_MARKUP_ERROR_INVALID_CONTENT,
      "element '%s', %s wrong version'", element_name, version);
  if (split_version)
    g_strfreev (split_version);

  return;

stroull_failed:
  GST_WARNING_OBJECT (self, "Error while strtoull: %s", g_strerror (errno));
  goto failed;
}

static inline void
_parse_project (GMarkupParseContext * context, const gchar * element_name,
    const gchar ** attribute_names, const gchar ** attribute_values,
    GESXmlFormatter * self, GError ** error)
{
  const gchar *metadatas = NULL, *properties;
  GESXmlFormatterPrivate *priv = _GET_PRIV (self);

  if (g_strcmp0 (element_name, "project")) {
    g_set_error (error, G_MARKUP_ERROR,
        G_MARKUP_ERROR_INVALID_CONTENT,
127
        "Found element '%s', Missing '<project>' element'", element_name);
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  } else {
    priv->project_opened = TRUE;
    if (!g_markup_collect_attributes (element_name, attribute_names,
            attribute_values, error,
            COLLECT_STR_OPT, "properties", &properties,
            COLLECT_STR_OPT, "metadatas", &metadatas, G_MARKUP_COLLECT_INVALID))
      return;

    if (GES_FORMATTER (self)->project && metadatas)
      ges_meta_container_add_metas_from_string (GES_META_CONTAINER
          (GES_FORMATTER (self)->project), metadatas);

  }
}

static inline void
_parse_encoding_profile (GMarkupParseContext * context,
    const gchar * element_name, const gchar ** attribute_names,
    const gchar ** attribute_values, GESXmlFormatter * self, GError ** error)
{
  GstCaps *capsformat = NULL;
149
150
151
  GstStructure *preset_properties = NULL;
  const gchar *name, *description, *type, *preset = NULL,
      *str_preset_properties = NULL, *preset_name = NULL, *format;
152
153
154
155
156
157
158

  if (!g_markup_collect_attributes (element_name, attribute_names,
          attribute_values, error,
          G_MARKUP_COLLECT_STRING, "name", &name,
          G_MARKUP_COLLECT_STRING, "description", &description,
          G_MARKUP_COLLECT_STRING, "type", &type,
          COLLECT_STR_OPT, "preset", &preset,
159
          COLLECT_STR_OPT, "preset-properties", &str_preset_properties,
160
161
162
163
164
165
166
          COLLECT_STR_OPT, "preset-name", &preset_name,
          COLLECT_STR_OPT, "format", &format, G_MARKUP_COLLECT_INVALID))
    return;

  if (format)
    capsformat = gst_caps_from_string (format);

167
168
169
170
171
172
173
174
175
  if (str_preset_properties) {
    preset_properties = gst_structure_from_string (str_preset_properties, NULL);
    if (preset_properties == NULL) {
      g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
          "element '%s', Wrong preset-properties format.", element_name);
      return;
    }
  }

176
  ges_base_xml_formatter_add_encoding_profile (GES_BASE_XML_FORMATTER (self),
177
178
      type, NULL, name, description, capsformat, preset, preset_properties,
      preset_name, 0, 0, NULL, 0, FALSE, NULL, TRUE, error);
179
180
181
182
183
184
185
}

static inline void
_parse_stream_profile (GMarkupParseContext * context,
    const gchar * element_name, const gchar ** attribute_names,
    const gchar ** attribute_values, GESXmlFormatter * self, GError ** error)
{
186
  gboolean variableframerate = FALSE, enabled = TRUE;
187
188
  guint id = 0, presence = 0, pass = 0;
  GstCaps *format_caps = NULL, *restriction_caps = NULL;
189
  GstStructure *preset_properties = NULL;
190
  const gchar *parent, *strid, *type, *strpresence, *format = NULL,
191
192
193
      *name = NULL, *description = NULL, *preset,
      *str_preset_properties = NULL, *preset_name = NULL, *restriction = NULL,
      *strpass = NULL, *strvariableframerate = NULL, *strenabled = NULL;
194
195
196
197
198
199
200
201
202
203
204
205

  /* FIXME Looks like there is a bug in that function, if we put the parent
   * at the beginning it set %NULL and not the real value... :/ */
  if (!g_markup_collect_attributes (element_name, attribute_names,
          attribute_values, error,
          G_MARKUP_COLLECT_STRING, "id", &strid,
          G_MARKUP_COLLECT_STRING, "type", &type,
          G_MARKUP_COLLECT_STRING, "presence", &strpresence,
          COLLECT_STR_OPT, "format", &format,
          COLLECT_STR_OPT, "name", &name,
          COLLECT_STR_OPT, "description", &description,
          COLLECT_STR_OPT, "preset", &preset,
206
          COLLECT_STR_OPT, "preset-properties", &str_preset_properties,
207
208
          COLLECT_STR_OPT, "preset-name", &preset_name,
          COLLECT_STR_OPT, "restriction", &restriction,
209
          COLLECT_STR_OPT, "pass", &strpass,
210
          COLLECT_STR_OPT, "variableframerate", &strvariableframerate,
211
          COLLECT_STR_OPT, "enabled", &strenabled,
212
213
214
215
216
217
218
219
220
221
222
223
224
225
          G_MARKUP_COLLECT_STRING, "parent", &parent, G_MARKUP_COLLECT_INVALID))
    return;

  errno = 0;
  id = g_ascii_strtoll (strid, NULL, 10);
  if (errno)
    goto convertion_failed;

  if (strpresence) {
    presence = g_ascii_strtoll (strpresence, NULL, 10);
    if (errno)
      goto convertion_failed;
  }

226
227
228
229
230
231
  if (str_preset_properties) {
    preset_properties = gst_structure_from_string (str_preset_properties, NULL);
    if (preset_properties == NULL)
      goto convertion_failed;
  }

232
233
234
235
236
237
238
239
240
241
242
243
  if (strpass) {
    pass = g_ascii_strtoll (strpass, NULL, 10);
    if (errno)
      goto convertion_failed;
  }

  if (strvariableframerate) {
    variableframerate = g_ascii_strtoll (strvariableframerate, NULL, 10);
    if (errno)
      goto convertion_failed;
  }

244
245
246
247
248
249
  if (strenabled) {
    enabled = g_ascii_strtoll (strenabled, NULL, 10);
    if (errno)
      goto convertion_failed;
  }

250
251
252
253
254
255
256
  if (format)
    format_caps = gst_caps_from_string (format);

  if (restriction)
    restriction_caps = gst_caps_from_string (restriction);

  ges_base_xml_formatter_add_encoding_profile (GES_BASE_XML_FORMATTER (self),
257
258
259
260
261
262
      type, parent, name, description, format_caps, preset, preset_properties,
      preset_name, id, presence, restriction_caps, pass, variableframerate,
      NULL, enabled, error);

  if (preset_properties)
    gst_structure_free (preset_properties);
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

  return;

convertion_failed:
  g_set_error (error, G_MARKUP_ERROR,
      G_MARKUP_ERROR_INVALID_CONTENT,
      "element '%s', Wrong property type, error: %s'", element_name,
      g_strerror (errno));
  return;
}

static inline void
_parse_timeline (GMarkupParseContext * context, const gchar * element_name,
    const gchar ** attribute_names, const gchar ** attribute_values,
    GESXmlFormatter * self, GError ** error)
{
  const gchar *metadatas = NULL, *properties = NULL;
  GESTimeline *timeline = GES_FORMATTER (self)->timeline;

  if (!g_markup_collect_attributes (element_name, attribute_names,
          attribute_values, error,
          COLLECT_STR_OPT, "properties", &properties,
          COLLECT_STR_OPT, "metadatas", &metadatas, G_MARKUP_COLLECT_INVALID))
    return;

  if (timeline == NULL)
    return;

291
292
  ges_base_xml_formatter_set_timeline_properties (GES_BASE_XML_FORMATTER (self),
      timeline, properties, metadatas);
293
294
295
296
297
298
299
300
301
}

static inline void
_parse_asset (GMarkupParseContext * context, const gchar * element_name,
    const gchar ** attribute_names, const gchar ** attribute_values,
    GESXmlFormatter * self, GError ** error)
{
  GType extractable_type;
  const gchar *id, *extractable_type_name, *metadatas = NULL, *properties =
302
      NULL, *proxy_id = NULL;
303
304
305
306
307
308

  if (!g_markup_collect_attributes (element_name, attribute_names,
          attribute_values, error, G_MARKUP_COLLECT_STRING, "id", &id,
          G_MARKUP_COLLECT_STRING, "extractable-type-name",
          &extractable_type_name,
          COLLECT_STR_OPT, "properties", &properties,
309
310
          COLLECT_STR_OPT, "metadatas", &metadatas,
          COLLECT_STR_OPT, "proxy-id", &proxy_id, G_MARKUP_COLLECT_INVALID))
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
    return;

  extractable_type = g_type_from_name (extractable_type_name);
  if (extractable_type == G_TYPE_NONE)
    g_set_error (error, G_MARKUP_ERROR,
        G_MARKUP_ERROR_INVALID_CONTENT,
        "element '%s' invalid extractable_type %s'",
        element_name, extractable_type_name);
  else if (!g_type_is_a (extractable_type, GES_TYPE_EXTRACTABLE))
    g_set_error (error, G_MARKUP_ERROR,
        G_MARKUP_ERROR_INVALID_CONTENT,
        "element '%s', %s not an extractable_type'",
        element_name, extractable_type_name);
  else {
    GstStructure *props = NULL;
    if (properties)
      props = gst_structure_from_string (properties, NULL);

    ges_base_xml_formatter_add_asset (GES_BASE_XML_FORMATTER (self), id,
330
        extractable_type, props, metadatas, proxy_id, error);
331
332
333
334
335
336
337
338
339
340
341
342
343
    if (props)
      gst_structure_free (props);
  }
}


static inline void
_parse_track (GMarkupParseContext * context, const gchar * element_name,
    const gchar ** attribute_names, const gchar ** attribute_values,
    GESXmlFormatter * self, GError ** error)
{
  GstCaps *caps;
  GESTrackType track_type;
344
  GstStructure *props = NULL;
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
  const gchar *strtrack_type, *strcaps, *strtrack_id, *metadatas =
      NULL, *properties = NULL;

  if (!g_markup_collect_attributes (element_name, attribute_names,
          attribute_values, error,
          G_MARKUP_COLLECT_STRING, "track-type", &strtrack_type,
          G_MARKUP_COLLECT_STRING, "track-id", &strtrack_id,
          COLLECT_STR_OPT, "properties", &properties,
          COLLECT_STR_OPT, "metadatas", &metadatas,
          G_MARKUP_COLLECT_STRING, "caps", &strcaps, G_MARKUP_COLLECT_INVALID))
    return;

  if ((caps = gst_caps_from_string (strcaps)) == NULL)
    goto wrong_caps;

  errno = 0;
  track_type = g_ascii_strtoll (strtrack_type, NULL, 10);
  if (errno)
    goto convertion_failed;

365
366
367
368
  if (properties) {
    props = gst_structure_from_string (properties, NULL);
  }

369
  ges_base_xml_formatter_add_track (GES_BASE_XML_FORMATTER (self), track_type,
370
      caps, strtrack_id, props, metadatas, error);
371

372
373
374
  if (props)
    gst_structure_free (props);

375
376
  gst_caps_unref (caps);

377
378
379
380
381
382
383
384
385
  return;

wrong_caps:
  g_set_error (error, G_MARKUP_ERROR,
      G_MARKUP_ERROR_INVALID_CONTENT,
      "element '%s', Can not create caps: %s'", element_name, strcaps);
  return;

convertion_failed:
386
  gst_caps_unref (caps);
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
  g_set_error (error, G_MARKUP_ERROR,
      G_MARKUP_ERROR_INVALID_CONTENT,
      "element '%s', Wrong property type, error: %s'", element_name,
      g_strerror (errno));
  return;

}

static inline void
_parse_layer (GMarkupParseContext * context, const gchar * element_name,
    const gchar ** attribute_names, const gchar ** attribute_values,
    GESXmlFormatter * self, GError ** error)
{
  GstStructure *props = NULL;
  guint priority;
  GType extractable_type = G_TYPE_NONE;
  const gchar *metadatas = NULL, *properties = NULL, *strprio = NULL,
      *extractable_type_name;

  if (!g_markup_collect_attributes (element_name, attribute_names,
          attribute_values, error,
          G_MARKUP_COLLECT_STRING, "priority", &strprio,
          COLLECT_STR_OPT, "extractable-type-name", &extractable_type_name,
          COLLECT_STR_OPT, "properties", &properties,
          COLLECT_STR_OPT, "metadatas", &metadatas, G_MARKUP_COLLECT_INVALID))
    return;

  if (extractable_type_name) {
    extractable_type = g_type_from_name (extractable_type_name);
    if (extractable_type == G_TYPE_NONE) {
      g_set_error (error, G_MARKUP_ERROR,
          G_MARKUP_ERROR_INVALID_CONTENT,
          "element '%s' invalid extractable_type %s'",
          element_name, extractable_type_name);

      return;
    } else if (!g_type_is_a (extractable_type, GES_TYPE_EXTRACTABLE)) {
      g_set_error (error, G_MARKUP_ERROR,
          G_MARKUP_ERROR_INVALID_CONTENT,
          "element '%s', %s not an extractable_type'",
          element_name, extractable_type_name);

      return;
    }
  }

  if (properties) {
    props = gst_structure_from_string (properties, NULL);
    if (props == NULL)
      goto wrong_properties;
  }

439
  errno = 0;
440
441
442
443
444
445
  priority = g_ascii_strtoll (strprio, NULL, 10);
  if (errno)
    goto convertion_failed;

  ges_base_xml_formatter_add_layer (GES_BASE_XML_FORMATTER (self),
      extractable_type, priority, props, metadatas, error);
446
447

done:
448
449
450
451
452
453
454
455
456
457
  if (props)
    gst_structure_free (props);

  return;

convertion_failed:
  g_set_error (error, G_MARKUP_ERROR,
      G_MARKUP_ERROR_INVALID_CONTENT,
      "element '%s', Wrong property type, error: %s'", element_name,
      g_strerror (errno));
458
  goto done;
459
460
461
462
463
464
465
466
467

wrong_properties:
  g_set_error (error, G_MARKUP_ERROR,
      G_MARKUP_ERROR_INVALID_CONTENT,
      "element '%s', wrong layer properties '%s', could no be deserialized",
      element_name, properties);
}

static inline void
468
_parse_clip (GMarkupParseContext * context,
469
470
471
472
    const gchar * element_name, const gchar ** attribute_names,
    const gchar ** attribute_values, GESXmlFormatter * self, GError ** error)
{
  GType type;
473
  GstStructure *props = NULL, *children_props = NULL;
474
475
476
477
478
  GESTrackType track_types;
  GstClockTime start, inpoint = 0, duration, layer_prio;

  const gchar *strid, *asset_id, *strstart, *strin, *strduration, *strrate,
      *strtrack_types, *strtype, *metadatas = NULL, *properties =
479
      NULL, *children_properties = NULL, *strlayer_prio;
480
481
482
483
484
485
486
487
488
489
490

  if (!g_markup_collect_attributes (element_name, attribute_names,
          attribute_values, error,
          G_MARKUP_COLLECT_STRING, "id", &strid,
          G_MARKUP_COLLECT_STRING, "type-name", &strtype,
          G_MARKUP_COLLECT_STRING, "start", &strstart,
          G_MARKUP_COLLECT_STRING, "duration", &strduration,
          G_MARKUP_COLLECT_STRING, "asset-id", &asset_id,
          G_MARKUP_COLLECT_STRING, "track-types", &strtrack_types,
          G_MARKUP_COLLECT_STRING, "layer-priority", &strlayer_prio,
          COLLECT_STR_OPT, "properties", &properties,
491
          COLLECT_STR_OPT, "children-properties", &children_properties,
492
493
494
495
496
497
          COLLECT_STR_OPT, "metadatas", &metadatas,
          COLLECT_STR_OPT, "rate", &strrate,
          COLLECT_STR_OPT, "inpoint", &strin, G_MARKUP_COLLECT_INVALID)) {
    return;
  }
  type = g_type_from_name (strtype);
498
  if (!g_type_is_a (type, GES_TYPE_CLIP))
499
500
    goto wrong_type;

501
  errno = 0;
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
  track_types = g_ascii_strtoll (strtrack_types, NULL, 10);
  if (errno)
    goto convertion_failed;

  layer_prio = g_ascii_strtoll (strlayer_prio, NULL, 10);
  if (errno)
    goto convertion_failed;

  if (strin) {
    inpoint = g_ascii_strtoull (strin, NULL, 10);
    if (errno)
      goto convertion_failed;
  }

  start = g_ascii_strtoull (strstart, NULL, 10);
  if (errno)
    goto convertion_failed;

  duration = g_ascii_strtoull (strduration, NULL, 10);
  if (errno)
    goto convertion_failed;

  if (properties) {
    props = gst_structure_from_string (properties, NULL);
    if (props == NULL)
      goto wrong_properties;
  }

530
531
532
533
534
535
  if (children_properties) {
    children_props = gst_structure_from_string (children_properties, NULL);
    if (children_props == NULL)
      goto wrong_children_properties;
  }

536
  ges_base_xml_formatter_add_clip (GES_BASE_XML_FORMATTER (self),
537
      strid, asset_id, type, start, inpoint, duration, layer_prio,
538
      track_types, props, children_props, metadatas, error);
539
540
541
542
543
544
545
546
  if (props)
    gst_structure_free (props);

  return;

wrong_properties:
  g_set_error (error, G_MARKUP_ERROR,
      G_MARKUP_ERROR_INVALID_CONTENT,
547
      "element '%s', Clip %s properties '%s', could no be deserialized",
548
549
550
      element_name, asset_id, properties);
  return;

551
552
553
554
555
556
557
wrong_children_properties:
  g_set_error (error, G_MARKUP_ERROR,
      G_MARKUP_ERROR_INVALID_CONTENT,
      "element '%s', Clip %s children properties '%s', could no be deserialized",
      element_name, asset_id, children_properties);
  return;

558
559
560
561
562
563
564
565
566
567
convertion_failed:
  g_set_error (error, G_MARKUP_ERROR,
      G_MARKUP_ERROR_INVALID_CONTENT,
      "element '%s', Wrong property type, error: %s'", element_name,
      g_strerror (errno));
  return;

wrong_type:
  g_set_error (error, G_MARKUP_ERROR,
      G_MARKUP_ERROR_INVALID_CONTENT,
568
      "element '%s', %s not a GESClip'", element_name, strtype);
569
570
}

571
572
573
574
575
576
static inline void
_parse_binding (GMarkupParseContext * context, const gchar * element_name,
    const gchar ** attribute_names, const gchar ** attribute_values,
    GESXmlFormatter * self, GError ** error)
{
  const gchar *type = NULL, *source_type = NULL, *timed_values =
577
      NULL, *property_name = NULL, *mode = NULL, *track_id = NULL;
578
579
580
581
582
583
584
585
586
587
  gchar **pairs, **tmp;
  gchar *pair;
  GSList *list = NULL;

  if (!g_markup_collect_attributes (element_name, attribute_names,
          attribute_values, error,
          G_MARKUP_COLLECT_STRING, "type", &type,
          G_MARKUP_COLLECT_STRING, "source_type", &source_type,
          G_MARKUP_COLLECT_STRING, "property", &property_name,
          G_MARKUP_COLLECT_STRING, "mode", &mode,
588
          G_MARKUP_COLLECT_STRING, "track_id", &track_id,
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
          G_MARKUP_COLLECT_STRING, "values", &timed_values,
          G_MARKUP_COLLECT_INVALID)) {
    return;
  }

  pairs = g_strsplit (timed_values, " ", 0);
  for (tmp = pairs; tmp != NULL; tmp += 1) {
    gchar **value_pair;

    pair = *tmp;
    if (pair == NULL)
      break;
    if (strlen (pair)) {
      GstTimedValue *value;

      value = g_slice_new (GstTimedValue);
      value_pair = g_strsplit (pair, ":", 0);
      value->timestamp = g_ascii_strtoull (value_pair[0], NULL, 10);
      value->value = g_ascii_strtod (value_pair[1], NULL);
      list = g_slist_append (list, value);
      g_strfreev (value_pair);
    }
  }

  g_strfreev (pairs);
614

615
616
617
  ges_base_xml_formatter_add_control_binding (GES_BASE_XML_FORMATTER (self),
      type,
      source_type,
618
      property_name, (gint) g_ascii_strtoll (mode, NULL, 10), track_id, list);
619
620
}

621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
static inline void
_parse_source (GMarkupParseContext * context, const gchar * element_name,
    const gchar ** attribute_names, const gchar ** attribute_values,
    GESXmlFormatter * self, GError ** error)
{
  GstStructure *children_props = NULL;
  const gchar *track_id = NULL, *children_properties = NULL;

  if (!g_markup_collect_attributes (element_name, attribute_names,
          attribute_values, error,
          G_MARKUP_COLLECT_STRING, "track-id", &track_id,
          COLLECT_STR_OPT, "children-properties", &children_properties,
          G_MARKUP_COLLECT_INVALID)) {
    return;
  }

  if (children_properties) {
    children_props = gst_structure_from_string (children_properties, NULL);
    if (children_props == NULL)
      goto wrong_children_properties;
  }

  ges_base_xml_formatter_add_source (GES_BASE_XML_FORMATTER (self), track_id,
      children_props);

  if (children_props)
    gst_structure_free (children_props);

  return;

wrong_children_properties:
  g_set_error (error, G_MARKUP_ERROR,
      G_MARKUP_ERROR_INVALID_CONTENT,
      "element '%s', children properties '%s', could no be deserialized",
      element_name, children_properties);
}

658
659
660
661
662
663
664
665
666
667
static inline void
_parse_effect (GMarkupParseContext * context, const gchar * element_name,
    const gchar ** attribute_names, const gchar ** attribute_values,
    GESXmlFormatter * self, GError ** error)
{
  GType type;

  GstStructure *children_props = NULL, *props = NULL;
  const gchar *asset_id = NULL, *strtype = NULL, *track_id =
      NULL, *metadatas = NULL, *properties = NULL, *track_type = NULL,
668
      *children_properties = NULL, *clip_id;
669
670
671
672
673

  if (!g_markup_collect_attributes (element_name, attribute_names,
          attribute_values, error,
          COLLECT_STR_OPT, "metadatas", &metadatas,
          G_MARKUP_COLLECT_STRING, "asset-id", &asset_id,
674
          G_MARKUP_COLLECT_STRING, "clip-id", &clip_id,
675
676
677
678
679
680
681
682
683
684
          G_MARKUP_COLLECT_STRING, "type-name", &strtype,
          G_MARKUP_COLLECT_STRING, "track-id", &track_id,
          COLLECT_STR_OPT, "children-properties", &children_properties,
          COLLECT_STR_OPT, "track-type", &track_type,
          COLLECT_STR_OPT, "properties", &properties,
          G_MARKUP_COLLECT_INVALID)) {
    return;
  }

  type = g_type_from_name (strtype);
685
  if (!g_type_is_a (type, GES_TYPE_BASE_EFFECT))
686
687
688
689
690
691
692
693
694
695
696
697
698
699
    goto wrong_type;

  if (children_properties) {
    children_props = gst_structure_from_string (children_properties, NULL);
    if (children_props == NULL)
      goto wrong_children_properties;
  }

  if (properties) {
    props = gst_structure_from_string (properties, NULL);
    if (props == NULL)
      goto wrong_properties;
  }

700
  ges_base_xml_formatter_add_track_element (GES_BASE_XML_FORMATTER (self),
701
      type, asset_id, track_id, clip_id, children_props, props, metadatas,
702
703
      error);

704
705
out:

706
707
708
709
710
711
712
713
714
715
716
717
  if (props)
    gst_structure_free (props);
  if (children_props)
    gst_structure_free (children_props);

  return;

wrong_properties:
  g_set_error (error, G_MARKUP_ERROR,
      G_MARKUP_ERROR_INVALID_CONTENT,
      "element '%s', Effect %s properties '%s', could no be deserialized",
      element_name, asset_id, properties);
718
  goto out;
719
720
721
722
723
724

wrong_children_properties:
  g_set_error (error, G_MARKUP_ERROR,
      G_MARKUP_ERROR_INVALID_CONTENT,
      "element '%s', Effect %s children properties '%s', could no be deserialized",
      element_name, asset_id, children_properties);
725
  goto out;
726
727
728
729

wrong_type:
  g_set_error (error, G_MARKUP_ERROR,
      G_MARKUP_ERROR_INVALID_CONTENT,
730
      "element '%s', %s not a GESBaseEffect'", element_name, strtype);
731
732
}

733
734
735
736
737
738

static inline void
_parse_group (GMarkupParseContext * context, const gchar * element_name,
    const gchar ** attribute_names, const gchar ** attribute_values,
    GESXmlFormatter * self, GError ** error)
{
739
  const gchar *id, *properties, *metadatas = NULL;
740
741
742
743
744

  if (!g_markup_collect_attributes (element_name, attribute_names,
          attribute_values, error,
          G_MARKUP_COLLECT_STRING, "id", &id,
          G_MARKUP_COLLECT_STRING, "properties", &properties,
745
          COLLECT_STR_OPT, "metadatas", &metadatas, G_MARKUP_COLLECT_INVALID)) {
746
747
748
749
    return;
  }

  ges_base_xml_formatter_add_group (GES_BASE_XML_FORMATTER (self), id,
750
      properties, metadatas);
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
}

static inline void
_parse_group_child (GMarkupParseContext * context, const gchar * element_name,
    const gchar ** attribute_names, const gchar ** attribute_values,
    GESXmlFormatter * self, GError ** error)
{
  const gchar *child_id, *name;

  if (!g_markup_collect_attributes (element_name, attribute_names,
          attribute_values, error,
          G_MARKUP_COLLECT_STRING, "id", &child_id,
          G_MARKUP_COLLECT_STRING, "name", &name, G_MARKUP_COLLECT_INVALID)) {
    return;
  }

  ges_base_xml_formatter_last_group_add_child (GES_BASE_XML_FORMATTER (self),
      child_id, name);
}

771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
static void
_parse_element_start (GMarkupParseContext * context, const gchar * element_name,
    const gchar ** attribute_names, const gchar ** attribute_values,
    gpointer self, GError ** error)
{
  GESXmlFormatterPrivate *priv = _GET_PRIV (self);

  if (!G_UNLIKELY (priv->ges_opened))
    _parse_ges_element (context, element_name, attribute_names,
        attribute_values, self, error);
  else if (!G_UNLIKELY (priv->project_opened))
    _parse_project (context, element_name, attribute_names, attribute_values,
        self, error);
  else if (g_strcmp0 (element_name, "encoding-profile") == 0)
    _parse_encoding_profile (context, element_name, attribute_names,
        attribute_values, self, error);
  else if (g_strcmp0 (element_name, "stream-profile") == 0)
    _parse_stream_profile (context, element_name, attribute_names,
        attribute_values, self, error);
  else if (g_strcmp0 (element_name, "timeline") == 0)
    _parse_timeline (context, element_name, attribute_names, attribute_values,
        self, error);
  else if (g_strcmp0 (element_name, "asset") == 0)
    _parse_asset (context, element_name, attribute_names, attribute_values,
        self, error);
  else if (g_strcmp0 (element_name, "track") == 0)
    _parse_track (context, element_name, attribute_names,
        attribute_values, self, error);
  else if (g_strcmp0 (element_name, "layer") == 0)
    _parse_layer (context, element_name, attribute_names,
        attribute_values, self, error);
802
803
  else if (g_strcmp0 (element_name, "clip") == 0)
    _parse_clip (context, element_name, attribute_names,
804
        attribute_values, self, error);
805
806
807
  else if (g_strcmp0 (element_name, "source") == 0)
    _parse_source (context, element_name, attribute_names,
        attribute_values, self, error);
808
809
810
  else if (g_strcmp0 (element_name, "effect") == 0)
    _parse_effect (context, element_name, attribute_names,
        attribute_values, self, error);
811
812
813
  else if (g_strcmp0 (element_name, "binding") == 0)
    _parse_binding (context, element_name, attribute_names,
        attribute_values, self, error);
814
815
816
817
818
819
  else if (g_strcmp0 (element_name, "group") == 0)
    _parse_group (context, element_name, attribute_names,
        attribute_values, self, error);
  else if (g_strcmp0 (element_name, "child") == 0)
    _parse_group_child (context, element_name, attribute_names,
        attribute_values, self, error);
820
821
822
823
824
825
826
827
828
  else
    GST_LOG_OBJECT (self, "Element %s not handled", element_name);
}

static void
_parse_element_end (GMarkupParseContext * context,
    const gchar * element_name, gpointer self, GError ** error)
{
  /*GESXmlFormatterPrivate *priv = _GET_PRIV (self); */
829
830
831
832
833
834
835
836
837
  if (g_strcmp0 (element_name, "ges") == 0 && GES_FORMATTER (self)->project) {
    gchar *version = g_strdup_printf ("%d.%d",
        API_VERSION, GES_XML_FORMATTER (self)->priv->min_version);

    ges_meta_container_set_string (GES_META_CONTAINER (GES_FORMATTER
            (self)->project), GES_META_FORMAT_VERSION, version);

    g_free (version);
  }
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
}

static void
_error_parsing (GMarkupParseContext * context, GError * error,
    gpointer user_data)
{
  GST_WARNING ("Error accured when parsing %s", error->message);
}

/***********************************************
 *                                             *
 *            Saving implementation            *
 *                                             *
 ***********************************************/

/* XML writting utils */
static inline void
855
append_escaped (GString * str, gchar * tmpstr)
856
{
857
858
  g_string_append (str, tmpstr);
  g_free (tmpstr);
859
860
861
862
863
}

static inline gboolean
_can_serialize_spec (GParamSpec * spec)
{
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
  if (!(spec->flags & G_PARAM_WRITABLE)) {
    GST_DEBUG ("%s from %s is not writable",
        spec->name, g_type_name (spec->owner_type));

    return FALSE;
  } else if (spec->flags & G_PARAM_CONSTRUCT_ONLY) {
    GST_DEBUG ("%s from %s is construct only",
        spec->name, g_type_name (spec->owner_type));

    return FALSE;
  } else if (spec->flags & GES_PARAM_NO_SERIALIZATION &&
      g_type_is_a (spec->owner_type, GES_TYPE_TIMELINE_ELEMENT)) {
    GST_DEBUG ("%s from %s is set as GES_PARAM_NO_SERIALIZATION",
        spec->name, g_type_name (spec->owner_type));

    return FALSE;
  } else if (g_type_is_a (G_PARAM_SPEC_VALUE_TYPE (spec), G_TYPE_OBJECT)) {
    GST_DEBUG ("%s from %s contains GObject, can't serialize that.",
        spec->name, g_type_name (spec->owner_type));

    return FALSE;
  } else if ((g_type_is_a (spec->owner_type, GST_TYPE_OBJECT) &&
          !g_strcmp0 (spec->name, "name"))) {

    GST_DEBUG ("We do not want to serialize the name of GstObjects.");
    return FALSE;
  } else if (G_PARAM_SPEC_VALUE_TYPE (spec) == G_TYPE_GTYPE) {
    GST_DEBUG ("%s from %s contains a GType, can't serialize.",
        spec->name, g_type_name (spec->owner_type));
    return FALSE;
  }

  return TRUE;
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
}

static inline void
_init_value_from_spec_for_serialization (GValue * value, GParamSpec * spec)
{

  if (g_type_is_a (spec->value_type, G_TYPE_ENUM) ||
      g_type_is_a (spec->value_type, G_TYPE_FLAGS))
    g_value_init (value, G_TYPE_INT);
  else
    g_value_init (value, spec->value_type);
}

static gchar *
_serialize_properties (GObject * object, const gchar * fieldname, ...)
{
  gchar *ret;
  guint n_props, j;
  GParamSpec *spec, **pspecs;
  GObjectClass *class = G_OBJECT_GET_CLASS (object);
  GstStructure *structure = gst_structure_new_empty ("properties");

  pspecs = g_object_class_list_properties (class, &n_props);
  for (j = 0; j < n_props; j++) {
    GValue val = { 0 };

    spec = pspecs[j];
924
925
    if (spec->value_type == GST_TYPE_CAPS) {
      GstCaps *caps;
926
      gchar *caps_str;
927
928

      g_object_get (object, spec->name, &caps, NULL);
929
930
931
      caps_str = gst_caps_to_string (caps);
      gst_structure_set (structure, spec->name, G_TYPE_STRING, caps_str, NULL);
      g_free (caps_str);
932
    } else if (_can_serialize_spec (spec)) {
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
      _init_value_from_spec_for_serialization (&val, spec);
      g_object_get_property (object, spec->name, &val);
      gst_structure_set_value (structure, spec->name, &val);
      g_value_unset (&val);
    }
  }
  g_free (pspecs);

  if (fieldname) {
    va_list varargs;
    va_start (varargs, fieldname);
    gst_structure_remove_fields_valist (structure, fieldname, varargs);
    va_end (varargs);
  }

  ret = gst_structure_to_string (structure);
  gst_structure_free (structure);

  return ret;
}

static inline void
955
_save_assets (GESXmlFormatter * self, GString * str, GESProject * project)
956
957
{
  char *properties, *metas;
958
  GESAsset *asset, *proxy;
959
960
961
962
963
964
965
  GList *assets, *tmp;

  assets = ges_project_list_assets (project, GES_TYPE_EXTRACTABLE);
  for (tmp = assets; tmp; tmp = tmp->next) {
    asset = GES_ASSET (tmp->data);
    properties = _serialize_properties (G_OBJECT (asset), NULL);
    metas = ges_meta_container_metas_to_string (GES_META_CONTAINER (asset));
966
967
    append_escaped (str,
        g_markup_printf_escaped
968
        ("      <asset id='%s' extractable-type-name='%s' properties='%s' metadatas='%s' ",
969
970
971
            ges_asset_get_id (asset),
            g_type_name (ges_asset_get_extractable_type (asset)), properties,
            metas));
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988

    /*TODO Save the whole list of proxies */
    proxy = ges_asset_get_proxy (asset);
    if (proxy) {
      append_escaped (str, g_markup_printf_escaped (" proxy-id='%s' ",
              ges_asset_get_id (proxy)));

      if (!g_list_find (assets, proxy)) {
        assets = g_list_append (assets, gst_object_ref (proxy));

        if (!tmp->next)
          tmp->next = g_list_last (assets);
      }

      self->priv->min_version = MAX (self->priv->min_version, 3);
    }
    g_string_append (str, "/>\n");
989
990
991
992
993
994
995
    g_free (properties);
    g_free (metas);
  }
  g_list_free_full (assets, gst_object_unref);
}

static inline void
996
_save_tracks (GESXmlFormatter * self, GString * str, GESTimeline * timeline)
997
998
999
1000
{
  gchar *strtmp, *metas;
  GESTrack *track;
  GList *tmp, *tracks;
1001
  char *properties;
1002
1003
1004
1005
1006
1007

  guint nb_tracks = 0;

  tracks = ges_timeline_get_tracks (timeline);
  for (tmp = tracks; tmp; tmp = tmp->next) {
    track = GES_TRACK (tmp->data);
1008
    properties = _serialize_properties (G_OBJECT (track), NULL);
1009
1010
    strtmp = gst_caps_to_string (ges_track_get_caps (track));
    metas = ges_meta_container_metas_to_string (GES_META_CONTAINER (track));
1011
1012
    append_escaped (str,
        g_markup_printf_escaped
1013
1014
        ("      <track caps='%s' track-type='%i' track-id='%i' properties='%s' metadatas='%s'/>\n",
            strtmp, track->type, nb_tracks++, properties, metas));
1015
1016
    g_free (strtmp);
    g_free (metas);
1017
    g_free (properties);
1018
1019
1020
1021
  }
  g_list_free_full (tracks, gst_object_unref);
}

1022
static inline void
1023
_save_children_properties (GString * str, GESTimelineElement * element)
1024
1025
1026
1027
{
  GstStructure *structure;
  GParamSpec **pspecs, *spec;
  guint i, n_props;
1028
  gchar *struct_str;
1029

1030
  pspecs = ges_timeline_element_list_children_properties (element, &n_props);
1031
1032
1033
1034
1035
1036
1037

  structure = gst_structure_new_empty ("properties");
  for (i = 0; i < n_props; i++) {
    GValue val = { 0 };
    spec = pspecs[i];

    if (_can_serialize_spec (spec)) {
1038
1039
1040
1041
      gchar *spec_name =
          g_strdup_printf ("%s::%s", g_type_name (spec->owner_type),
          spec->name);

1042
      _init_value_from_spec_for_serialization (&val, spec);
1043
      ges_timeline_element_get_child_property_by_pspec (element, spec, &val);
1044
1045
1046
      gst_structure_set_value (structure, spec_name, &val);

      g_free (spec_name);
1047
1048
1049
1050
1051
1052
      g_value_unset (&val);
    }
    g_param_spec_unref (spec);
  }
  g_free (pspecs);

1053
  struct_str = gst_structure_to_string (structure);
1054
  append_escaped (str,
1055
      g_markup_printf_escaped (" children-properties='%s'", struct_str));
1056
  gst_structure_free (structure);
1057
  g_free (struct_str);
1058
1059
}

1060
1061
/* TODO : Use this function for every track element with controllable properties */
static inline void
1062
_save_keyframes (GString * str, GESTrackElement * trackelement, gint index)
1063
1064
1065
1066
1067
{
  GHashTable *bindings_hashtable;
  GHashTableIter iter;
  gpointer key, value;

1068
1069
  bindings_hashtable =
      ges_track_element_get_all_control_bindings (trackelement);
1070
1071
1072
1073
1074
1075
1076

  g_hash_table_iter_init (&iter, bindings_hashtable);

  /* We iterate over the bindings, and save the timed values */
  while (g_hash_table_iter_next (&iter, &key, &value)) {
    if (GST_IS_DIRECT_CONTROL_BINDING ((GstControlBinding *) value)) {
      GstControlSource *source;
1077
      gboolean absolute = FALSE;
1078
1079
1080
1081
      GstDirectControlBinding *binding;

      binding = (GstDirectControlBinding *) value;

1082
1083
      g_object_get (binding, "control-source", &source,
          "absolute", &absolute, NULL);
1084
1085
1086
1087
1088

      if (GST_IS_INTERPOLATION_CONTROL_SOURCE (source)) {
        GList *timed_values, *tmp;
        GstInterpolationMode mode;

1089
1090
        append_escaped (str,
            g_markup_printf_escaped
1091
1092
1093
            ("            <binding type='%s' source_type='interpolation' property='%s'",
                absolute ? "direct-absolute" : "direct", (gchar *) key));

1094
        g_object_get (source, "mode", &mode, NULL);
1095
        append_escaped (str, g_markup_printf_escaped (" mode='%d'", mode));
1096
        append_escaped (str, g_markup_printf_escaped (" track_id='%d'", index));
1097
        append_escaped (str, g_markup_printf_escaped (" values ='"));
1098
1099
1100
1101
        timed_values =
            gst_timed_value_control_source_get_all
            (GST_TIMED_VALUE_CONTROL_SOURCE (source));
        for (tmp = timed_values; tmp; tmp = tmp->next) {
1102
          gchar strbuf[G_ASCII_DTOSTR_BUF_SIZE];
1103
1104
1105
          GstTimedValue *value;

          value = (GstTimedValue *) tmp->data;
1106
          append_escaped (str, g_markup_printf_escaped (" %" G_GUINT64_FORMAT
1107
1108
                  ":%s ", value->timestamp, g_ascii_dtostr (strbuf,
                      G_ASCII_DTOSTR_BUF_SIZE, value->value)));
1109
        }
1110
        append_escaped (str, g_markup_printf_escaped ("'/>\n"));
1111
1112
1113
      } else
        GST_DEBUG ("control source not in [interpolation]");
    } else
1114
      GST_DEBUG ("Binding type not in [direct, direct-absolute]");
1115
1116
1117
  }
}

1118
static inline void
1119
_save_effect (GString * str, guint clip_id, GESTrackElement * trackelement,
1120
1121
1122
1123
1124
    GESTimeline * timeline)
{
  GESTrack *tck;
  GList *tmp, *tracks;
  gchar *properties, *metas;
1125
  guint track_id = 0;
1126
  gboolean serialize;
1127
  gchar *extractable_id;
1128
1129
1130
1131
1132
1133
1134
1135

  g_object_get (trackelement, "serialize", &serialize, NULL);
  if (!serialize) {

    GST_DEBUG_OBJECT (trackelement, "Should not be serialized");

    return;
  }
1136

1137
  tck = ges_track_element_get_track (trackelement);
1138
  if (tck == NULL) {
1139
    GST_WARNING_OBJECT (trackelement, " Not in any track, can not save it");
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151

    return;
  }

  tracks = ges_timeline_get_tracks (timeline);
  for (tmp = tracks; tmp; tmp = tmp->next) {
    if (tmp->data == tck)
      break;
    track_id++;
  }
  g_list_free_full (tracks, gst_object_unref);

1152
  properties = _serialize_properties (G_OBJECT (trackelement), "start",
1153
1154
      "in-point", "duration", "locked", "max-duration", "name", "priority",
      NULL);
1155
1156
  metas =
      ges_meta_container_metas_to_string (GES_META_CONTAINER (trackelement));
1157
  extractable_id = ges_extractable_get_id (GES_EXTRACTABLE (trackelement));
1158
  append_escaped (str,
1159
      g_markup_printf_escaped ("          <effect asset-id='%s' clip-id='%u'"
1160
          " type-name='%s' track-type='%i' track-id='%i' properties='%s' metadatas='%s'",
1161
          extractable_id, clip_id,
1162
1163
          g_type_name (G_OBJECT_TYPE (trackelement)), tck->type, track_id,
          properties, metas));
1164
  g_free (extractable_id);
1165
1166
1167
  g_free (properties);
  g_free (metas);

1168
  _save_children_properties (str, GES_TIMELINE_ELEMENT (trackelement));
1169
  append_escaped (str, g_markup_printf_escaped (">\n"));
1170

1171
  _save_keyframes (str, trackelement, -1);
1172

1173
  append_escaped (str, g_markup_printf_escaped ("          </effect>\n"));
1174
1175
1176
}

static inline void
1177
_save_layers (GESXmlFormatter * self, GString * str, GESTimeline * timeline)
1178
1179
{
  gchar *properties, *metas;
1180
  GESLayer *layer;
1181
1182
  GESClip *clip;
  GList *tmplayer, *tmpclip, *clips;
1183
  GESXmlFormatterPrivate *priv = self->priv;
1184
1185
1186

  for (tmplayer = timeline->layers; tmplayer; tmplayer = tmplayer->next) {
    guint priority;
1187
    layer = GES_LAYER (tmplayer->data);
1188

1189
    priority = ges_layer_get_priority (layer);
1190
1191
    properties = _serialize_properties (G_OBJECT (layer), "priority", NULL);
    metas = ges_meta_container_metas_to_string (GES_META_CONTAINER (layer));
1192
1193
    append_escaped (str,
        g_markup_printf_escaped
1194
1195
        ("      <layer priority='%i' properties='%s' metadatas='%s'>\n",
            priority, properties, metas));
1196
1197
1198
    g_free (properties);
    g_free (metas);

1199
    clips = ges_layer_get_clips (layer);
1200
    for (tmpclip = clips; tmpclip; tmpclip = tmpclip->next) {
1201
      GList *effects, *tmpeffect;
1202
1203
      GList *tmptrackelement;
      GList *tracks;
1204
      gboolean serialize;
1205
      gchar *extractable_id;
1206

1207
      clip = GES_CLIP (tmpclip->data);
1208
1209
1210
1211
1212
1213
1214

      g_object_get (clip, "serialize", &serialize, NULL);
      if (!serialize) {
        GST_DEBUG_OBJECT (clip, "Should not be serialized");
        continue;
      }

1215
1216
      /* We escape all mandatrorry properties that are handled sparetely
       * and vtype for StandarTransition as it is the asset ID */
1217
      properties = _serialize_properties (G_OBJECT (clip),
1218
1219
          "supported-formats", "rate", "in-point", "start", "duration",
          "max-duration", "priority", "vtype", "uri", NULL);
1220
      extractable_id = ges_extractable_get_id (GES_EXTRACTABLE (clip));
1221
      metas = ges_meta_container_metas_to_string (GES_META_CONTAINER (clip));
1222
      append_escaped (str,
1223
          g_markup_printf_escaped ("        <clip id='%i' asset-id='%s'"
1224
1225
              " type-name='%s' layer-priority='%i' track-types='%i' start='%"
              G_GUINT64_FORMAT "' duration='%" G_GUINT64_FORMAT "' inpoint='%"
1226
              G_GUINT64_FORMAT "' rate='%d' properties='%s' metadatas='%s'",
1227
              priv->nbelements, extractable_id,
1228
1229
              g_type_name (G_OBJECT_TYPE (clip)), priority,
              ges_clip_get_supported_formats (clip), _START (clip),
1230
              _DURATION (clip), _INPOINT (clip), 0, properties, metas));
1231

1232
      if (GES_IS_TRANSITION_CLIP (clip)) {
1233
        _save_children_properties (str, GES_TIMELINE_ELEMENT (clip));
1234
1235
        self->priv->min_version = MAX (self->priv->min_version, 4);
      }
1236
1237
      g_string_append (str, ">\n");

1238
      g_free (extractable_id);
1239
1240
      g_free (properties);

1241
1242
1243
      g_hash_table_insert (self->priv->element_id, clip,
          GINT_TO_POINTER (priv->nbelements));

1244

1245
1246
1247
1248
1249
      /* Effects must always be serialized in the right priority order.
       * List order is guaranteed by the fact that ges_clip_get_top_effects
       * sorts the effects. */
      effects = ges_clip_get_top_effects (clip);
      for (tmpeffect = effects; tmpeffect; tmpeffect = tmpeffect->next) {
1250
1251
        _save_effect (str, priv->nbelements,
            GES_TRACK_ELEMENT (tmpeffect->data), timeline);
1252
      }
1253

1254

1255
1256
1257
1258
1259
      tracks = ges_timeline_get_tracks (timeline);

      for (tmptrackelement = GES_CONTAINER_CHILDREN (clip); tmptrackelement;
          tmptrackelement = tmptrackelement->next) {
        gint index;
1260
        gboolean serialize;
1261
1262
1263
1264

        if (!GES_IS_SOURCE (tmptrackelement->data))
          continue;

1265
1266
1267
1268
1269
1270
        g_object_get (tmptrackelement->data, "serialize", &serialize, NULL);
        if (!serialize) {
          GST_DEBUG_OBJECT (tmptrackelement->data, "Should not be serialized");
          continue;
        }

1271
1272
1273
        index =
            g_list_index (tracks,
            ges_track_element_get_track (tmptrackelement->data));
1274
1275
1276
1277
        append_escaped (str,
            g_markup_printf_escaped ("          <source track-id='%i'", index));
        _save_children_properties (str, tmptrackelement->data);
        append_escaped (str, g_markup_printf_escaped (">\n"));
1278
        _save_keyframes (str, tmptrackelement->data, index);
1279
        append_escaped (str, g_markup_printf_escaped ("          </source>\n"));
1280
1281
1282
1283
      }

      g_list_free_full (tracks, gst_object_unref);

1284
1285
      g_string_append (str, "        </clip>\n");

1286
      priv->nbelements++;
1287
    }
Edward Hervey's avatar
Edward Hervey committed
1288
    g_list_free_full (clips, (GDestroyNotify) gst_object_unref);
1289
    g_string_append (str, "      </layer>\n");
1290
1291
1292
  }
}

1293
1294
1295
1296
1297
static void
_save_group (GESXmlFormatter * self, GString * str, GList ** seen_groups,
    GESGroup * group)
{
  GList *tmp;
1298
  gboolean serialize;
1299
  gchar *properties, *metadatas;
1300

1301
1302
1303
1304
1305
1306
1307
1308
  g_object_get (group, "serialize", &serialize, NULL);
  if (!serialize) {

    GST_DEBUG_OBJECT (group, "Should not be serialized");

    return;
  }

1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
  if (g_list_find (*seen_groups, group)) {
    GST_DEBUG_OBJECT (group, "Already serialized");

    return;
  }

  *seen_groups = g_list_prepend (*seen_groups, group);
  for (tmp = GES_CONTAINER_CHILDREN (group); tmp; tmp = tmp->next) {
    if (GES_IS_GROUP (tmp->data)) {
      _save_group (self, str, seen_groups,
          GES_GROUP (GES_TIMELINE_ELEMENT (tmp->data)));
    }
  }

  properties = _serialize_properties (G_OBJECT (group), NULL);
1324
1325
1326
1327
1328
1329
1330

  metadatas = ges_meta_container_metas_to_string (GES_META_CONTAINER (group));
  self->priv->min_version = MAX (self->priv->min_version, 5);

  g_string_append_printf (str,
      "        <group id='%d' properties='%s' metadatas='%s'>\n",
      self->priv->nbelements, properties, metadatas);
1331
  g_free (properties);
1332
  g_free (metadatas);
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
  g_hash_table_insert (self->priv->element_id, group,
      GINT_TO_POINTER (self->priv->nbelements));
  self->priv->nbelements++;

  for (tmp = GES_CONTAINER_CHILDREN (group); tmp; tmp = tmp->next) {
    gint id = GPOINTER_TO_INT (g_hash_table_lookup (self->priv->element_id,
            tmp->data));

    g_string_append_printf (str, "          <child id='%d' name='%s'/>\n", id,
        GES_TIMELINE_ELEMENT_NAME (tmp->data));
  }
  g_string_append (str, "        </group>\n");
}

static void
_save_groups (GESXmlFormatter * self, GString * str, GESTimeline * timeline)
{
  GList *tmp;
  GList *seen_groups = NULL;

  g_string_append (str, "      <groups>\n");
1354
  for (tmp = ges_timeline_get_groups (timeline); tmp; tmp = tmp->next) {
1355
1356
    _save_group (self, str, &seen_groups, tmp->data);
  }
Edward Hervey's avatar
Edward Hervey committed
1357
  g_list_free (seen_groups);
1358
1359
  g_string_append (str, "      </groups>\n");
}
1360