ges-xml-formatter.c 67 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"
Thibault Saunier's avatar
Thibault Saunier committed
33
#include <glib/gstdio.h>
34 35 36 37
#include "ges-internal.h"

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

#define COLLECT_STR_OPT (G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL)

43
#define _GET_PRIV(o) (((GESXmlFormatter*)o)->priv)
44

Thibault Saunier's avatar
Thibault Saunier committed
45 46 47 48 49 50 51 52 53 54 55
typedef struct
{
  const gchar *id;
  gint start_line;
  gint start_char;
  gint fd;
  gchar *filename;
  GError *error;
  GMainLoop *ml;
} SubprojectData;

56 57 58 59 60 61
struct _GESXmlFormatterPrivate
{
  gboolean ges_opened;
  gboolean project_opened;

  GString *str;
62 63

  GHashTable *element_id;
Thibault Saunier's avatar
Thibault Saunier committed
64 65 66
  GHashTable *subprojects_map;
  SubprojectData *subproject;
  gint subproject_depth;
67 68

  guint nbelements;
69 70

  guint min_version;
71 72
};

Thibault Saunier's avatar
Thibault Saunier committed
73 74 75 76
G_LOCK_DEFINE_STATIC (uri_subprojects_map_lock);
/* { project_uri: { subproject_uri: new_suproject_uri}} */
static GHashTable *uri_subprojects_map = NULL;

77 78 79
G_DEFINE_TYPE_WITH_PRIVATE (GESXmlFormatter, ges_xml_formatter,
    GES_TYPE_BASE_XML_FORMATTER);

Thibault Saunier's avatar
Thibault Saunier committed
80 81 82
static GString *_save_project (GESFormatter * formatter, GString * str,
    GESProject * project, GESTimeline * timeline, GError ** error, guint depth);

83 84 85 86 87
static inline void
_parse_ges_element (GMarkupParseContext * context, const gchar * element_name,
    const gchar ** attribute_names, const gchar ** attribute_values,
    GESXmlFormatter * self, GError ** error)
{
88
  guint api_version;
89 90 91 92 93 94 95
  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,
Thibault Saunier's avatar
Thibault Saunier committed
96
        "Found element '%s', Missing '<ges>' element'", element_name);
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    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;

116 117
  self->priv->min_version = g_ascii_strtoull (split_version[1], NULL, 10);
  if (self->priv->min_version > MINOR_VERSION)
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
    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,
149
        "Found element '%s', Missing '<project>' element'", element_name);
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
  } 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;
171 172 173
  GstStructure *preset_properties = NULL;
  const gchar *name, *description, *type, *preset = NULL,
      *str_preset_properties = NULL, *preset_name = NULL, *format;
174 175 176 177 178 179 180

  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,
181
          COLLECT_STR_OPT, "preset-properties", &str_preset_properties,
182 183 184 185 186 187 188
          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);

189 190 191
  if (str_preset_properties) {
    preset_properties = gst_structure_from_string (str_preset_properties, NULL);
    if (preset_properties == NULL) {
192
      gst_caps_unref (capsformat);
193 194 195 196 197 198
      g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
          "element '%s', Wrong preset-properties format.", element_name);
      return;
    }
  }

199
  ges_base_xml_formatter_add_encoding_profile (GES_BASE_XML_FORMATTER (self),
200 201
      type, NULL, name, description, capsformat, preset, preset_properties,
      preset_name, 0, 0, NULL, 0, FALSE, NULL, TRUE, error);
202 203 204 205 206 207 208
}

static inline void
_parse_stream_profile (GMarkupParseContext * context,
    const gchar * element_name, const gchar ** attribute_names,
    const gchar ** attribute_values, GESXmlFormatter * self, GError ** error)
{
209
  gboolean variableframerate = FALSE, enabled = TRUE;
210 211
  guint id = 0, presence = 0, pass = 0;
  GstCaps *format_caps = NULL, *restriction_caps = NULL;
212
  GstStructure *preset_properties = NULL;
213
  const gchar *parent, *strid, *type, *strpresence, *format = NULL,
214 215 216
      *name = NULL, *description = NULL, *preset,
      *str_preset_properties = NULL, *preset_name = NULL, *restriction = NULL,
      *strpass = NULL, *strvariableframerate = NULL, *strenabled = NULL;
217 218 219 220 221 222 223 224 225 226 227 228

  /* 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,
229
          COLLECT_STR_OPT, "preset-properties", &str_preset_properties,
230 231
          COLLECT_STR_OPT, "preset-name", &preset_name,
          COLLECT_STR_OPT, "restriction", &restriction,
232
          COLLECT_STR_OPT, "pass", &strpass,
233
          COLLECT_STR_OPT, "variableframerate", &strvariableframerate,
234
          COLLECT_STR_OPT, "enabled", &strenabled,
235 236 237 238 239 240 241 242 243 244 245 246 247 248
          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;
  }

249 250 251 252 253 254
  if (str_preset_properties) {
    preset_properties = gst_structure_from_string (str_preset_properties, NULL);
    if (preset_properties == NULL)
      goto convertion_failed;
  }

255 256 257 258 259 260 261 262 263 264 265 266
  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;
  }

267 268 269 270 271 272
  if (strenabled) {
    enabled = g_ascii_strtoll (strenabled, NULL, 10);
    if (errno)
      goto convertion_failed;
  }

273 274 275 276 277 278 279
  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),
280 281 282 283 284 285
      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);
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

  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;

314 315
  ges_base_xml_formatter_set_timeline_properties (GES_BASE_XML_FORMATTER (self),
      timeline, properties, metadatas);
316 317 318 319 320 321 322 323 324
}

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 =
325
      NULL, *proxy_id = NULL;
Thibault Saunier's avatar
Thibault Saunier committed
326
  GESXmlFormatterPrivate *priv = _GET_PRIV (self);
327 328 329 330 331 332

  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,
333 334
          COLLECT_STR_OPT, "metadatas", &metadatas,
          COLLECT_STR_OPT, "proxy-id", &proxy_id, G_MARKUP_COLLECT_INVALID))
335 336 337
    return;

  extractable_type = g_type_from_name (extractable_type_name);
Thibault Saunier's avatar
Thibault Saunier committed
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
  if (extractable_type == GES_TYPE_TIMELINE) {
    SubprojectData *subproj_data = g_malloc0 (sizeof (SubprojectData));
    const gchar *nid;

    priv->subproject = subproj_data;
    G_LOCK (uri_subprojects_map_lock);
    nid = g_hash_table_lookup (priv->subprojects_map, id);
    G_UNLOCK (uri_subprojects_map_lock);

    if (!nid) {
      subproj_data->id = id;
      subproj_data->fd =
          g_file_open_tmp ("XXXXXX.xges", &subproj_data->filename, error);
      if (subproj_data->fd == -1) {
        GST_ERROR_OBJECT (self, "Could not create subproject file for %s", id);
        return;
      }
      g_markup_parse_context_get_position (context, &subproj_data->start_line,
          &subproj_data->start_char);
      id = gst_uri_construct ("file", subproj_data->filename);
      G_LOCK (uri_subprojects_map_lock);
      g_hash_table_insert (priv->subprojects_map, g_strdup (subproj_data->id),
          (gchar *) id);
      G_UNLOCK (uri_subprojects_map_lock);
      GST_INFO_OBJECT (self, "Serialized subproject %sis now at: %s",
          subproj_data->id, id);
    } else {
      GST_DEBUG_OBJECT (self, "Subproject already exists: %s -> %s", id, nid);
      id = nid;
      subproj_data->start_line = -1;
    }
  }

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
  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);

Thibault Saunier's avatar
Thibault Saunier committed
386 387 388 389 390 391 392 393 394 395
    if (extractable_type == GES_TYPE_URI_CLIP) {
      G_LOCK (uri_subprojects_map_lock);
      if (g_hash_table_contains (priv->subprojects_map, id)) {
        id = g_hash_table_lookup (priv->subprojects_map, id);

        GST_DEBUG_OBJECT (self, "Using subproject %s", id);
      }
      G_UNLOCK (uri_subprojects_map_lock);
    }

396
    ges_base_xml_formatter_add_asset (GES_BASE_XML_FORMATTER (self), id,
397
        extractable_type, props, metadatas, proxy_id, error);
398 399 400 401 402 403 404 405 406 407 408 409 410
    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;
411
  GstStructure *props = NULL;
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
  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;

432 433 434 435
  if (properties) {
    props = gst_structure_from_string (properties, NULL);
  }

436
  ges_base_xml_formatter_add_track (GES_BASE_XML_FORMATTER (self), track_type,
437
      caps, strtrack_id, props, metadatas, error);
438

439 440 441
  if (props)
    gst_structure_free (props);

442 443
  gst_caps_unref (caps);

444 445 446 447 448 449 450 451 452
  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:
453
  gst_caps_unref (caps);
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 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
  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;
  }

506
  errno = 0;
507 508 509 510 511 512
  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);
513 514

done:
515 516 517 518 519 520 521 522 523 524
  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));
525
  goto done;
526 527 528 529 530 531 532 533 534

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
535
_parse_clip (GMarkupParseContext * context,
536 537 538 539
    const gchar * element_name, const gchar ** attribute_names,
    const gchar ** attribute_values, GESXmlFormatter * self, GError ** error)
{
  GType type;
540
  GstStructure *props = NULL, *children_props = NULL;
541 542
  GESTrackType track_types;
  GstClockTime start, inpoint = 0, duration, layer_prio;
Thibault Saunier's avatar
Thibault Saunier committed
543
  GESXmlFormatterPrivate *priv = _GET_PRIV (self);
544 545 546

  const gchar *strid, *asset_id, *strstart, *strin, *strduration, *strrate,
      *strtrack_types, *strtype, *metadatas = NULL, *properties =
547
      NULL, *children_properties = NULL, *strlayer_prio;
548 549 550 551 552 553 554 555 556 557 558

  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,
559
          COLLECT_STR_OPT, "children-properties", &children_properties,
560 561 562 563 564 565
          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);
566
  if (!g_type_is_a (type, GES_TYPE_CLIP))
567 568
    goto wrong_type;

569
  errno = 0;
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
  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;
  }

598 599 600 601 602 603
  if (children_properties) {
    children_props = gst_structure_from_string (children_properties, NULL);
    if (children_props == NULL)
      goto wrong_children_properties;
  }

Thibault Saunier's avatar
Thibault Saunier committed
604 605 606 607 608 609
  G_LOCK (uri_subprojects_map_lock);
  if (g_hash_table_contains (priv->subprojects_map, asset_id)) {
    asset_id = g_hash_table_lookup (priv->subprojects_map, asset_id);
    GST_DEBUG_OBJECT (self, "Using subproject %s", asset_id);
  }
  G_UNLOCK (uri_subprojects_map_lock);
610
  ges_base_xml_formatter_add_clip (GES_BASE_XML_FORMATTER (self),
611
      strid, asset_id, type, start, inpoint, duration, layer_prio,
612
      track_types, props, children_props, metadatas, error);
613 614 615 616 617 618 619 620
  if (props)
    gst_structure_free (props);

  return;

wrong_properties:
  g_set_error (error, G_MARKUP_ERROR,
      G_MARKUP_ERROR_INVALID_CONTENT,
621
      "element '%s', Clip %s properties '%s', could no be deserialized",
622 623 624
      element_name, asset_id, properties);
  return;

625 626 627 628 629 630 631
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;

632 633 634 635 636 637 638 639 640 641
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,
642
      "element '%s', %s not a GESClip'", element_name, strtype);
643 644
}

645 646 647 648 649 650
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 =
651
      NULL, *property_name = NULL, *mode = NULL, *track_id = NULL;
652 653 654 655 656 657 658 659 660 661
  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,
662
          G_MARKUP_COLLECT_STRING, "track_id", &track_id,
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
          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);
688

689 690 691
  ges_base_xml_formatter_add_control_binding (GES_BASE_XML_FORMATTER (self),
      type,
      source_type,
692
      property_name, (gint) g_ascii_strtoll (mode, NULL, 10), track_id, list);
693 694
}

695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
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);
}

732 733 734 735 736 737 738 739 740 741
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,
742
      *children_properties = NULL, *clip_id;
743 744 745 746 747

  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,
748
          G_MARKUP_COLLECT_STRING, "clip-id", &clip_id,
749 750 751 752 753 754 755 756 757 758
          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);
759
  if (!g_type_is_a (type, GES_TYPE_BASE_EFFECT))
760 761 762 763 764 765 766 767 768 769 770 771 772 773
    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;
  }

774
  ges_base_xml_formatter_add_track_element (GES_BASE_XML_FORMATTER (self),
775
      type, asset_id, track_id, clip_id, children_props, props, metadatas,
776 777
      error);

778 779
out:

780 781 782 783 784 785 786 787 788 789 790 791
  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);
792
  goto out;
793 794 795 796 797 798

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);
799
  goto out;
800 801 802 803

wrong_type:
  g_set_error (error, G_MARKUP_ERROR,
      G_MARKUP_ERROR_INVALID_CONTENT,
804
      "element '%s', %s not a GESBaseEffect'", element_name, strtype);
805 806
}

807 808 809 810 811 812

static inline void
_parse_group (GMarkupParseContext * context, const gchar * element_name,
    const gchar ** attribute_names, const gchar ** attribute_values,
    GESXmlFormatter * self, GError ** error)
{
813
  const gchar *id, *properties, *metadatas = NULL;
814 815 816 817 818

  if (!g_markup_collect_attributes (element_name, attribute_names,
          attribute_values, error,
          G_MARKUP_COLLECT_STRING, "id", &id,
          G_MARKUP_COLLECT_STRING, "properties", &properties,
819
          COLLECT_STR_OPT, "metadatas", &metadatas, G_MARKUP_COLLECT_INVALID)) {
820 821 822 823
    return;
  }

  ges_base_xml_formatter_add_group (GES_BASE_XML_FORMATTER (self), id,
824
      properties, metadatas);
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
}

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);
}

845 846 847 848 849 850 851
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);

Thibault Saunier's avatar
Thibault Saunier committed
852 853 854 855 856 857 858 859
  if (priv->subproject) {
    if (g_strcmp0 (element_name, "ges") == 0) {
      priv->subproject_depth += 1;
    }
    return;
  }

  if (!G_UNLIKELY (priv->ges_opened)) {
860 861
    _parse_ges_element (context, element_name, attribute_names,
        attribute_values, self, error);
Thibault Saunier's avatar
Thibault Saunier committed
862
  } else if (!G_UNLIKELY (priv->project_opened))
863 864
    _parse_project (context, element_name, attribute_names, attribute_values,
        self, error);
Thibault Saunier's avatar
Thibault Saunier committed
865 866
  else if (g_strcmp0 (element_name, "ges") == 0) {
  } else if (g_strcmp0 (element_name, "encoding-profile") == 0)
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
    _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);
884 885
  else if (g_strcmp0 (element_name, "clip") == 0)
    _parse_clip (context, element_name, attribute_names,
886
        attribute_values, self, error);
887 888 889
  else if (g_strcmp0 (element_name, "source") == 0)
    _parse_source (context, element_name, attribute_names,
        attribute_values, self, error);
890 891 892
  else if (g_strcmp0 (element_name, "effect") == 0)
    _parse_effect (context, element_name, attribute_names,
        attribute_values, self, error);
893 894 895
  else if (g_strcmp0 (element_name, "binding") == 0)
    _parse_binding (context, element_name, attribute_names,
        attribute_values, self, error);
896 897 898 899 900 901
  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);
902 903 904 905
  else
    GST_LOG_OBJECT (self, "Element %s not handled", element_name);
}

Thibault Saunier's avatar
Thibault Saunier committed
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
static gboolean
_save_subproject_data (GESXmlFormatter * self, SubprojectData * subproj_data,
    gint subproject_end_line, gint subproject_end_char, GError ** error)
{
  gsize size;
  gint line = 1, i;
  gboolean res = FALSE;
  gsize start = 0, end = 0;
  gchar *subproject_content = NULL;
  gchar *xml = GES_BASE_XML_FORMATTER (self)->xmlcontent;

  for (i = 0; xml[i] != '\0'; i++) {
    if (!start && line == subproj_data->start_line) {
      i += subproj_data->start_char - 1;
      start = i;
    }

    if (line == subproject_end_line) {
      end = i + subproject_end_char - 1;
      break;
    }

    if (xml[i] == '\n')
      line++;
  }
  g_assert (start && end);
  size = (end - start);

  subproject_content = g_malloc (sizeof (gchar) * size);
  memcpy (subproject_content, &xml[start], end - start);
  subproject_content[end - start] = '\0';
  GST_INFO_OBJECT (self, "Saving subproject %s from %d:%d(%" G_GSIZE_FORMAT
      ") to %d:%d(%" G_GSIZE_FORMAT ")",
      subproj_data->id, subproj_data->start_line, subproj_data->start_char,
      start, subproject_end_line, subproject_end_char, end);

  res = g_file_set_contents (subproj_data->filename, subproject_content, -1,
      error);
  g_free (subproject_content);

  return res;
}

949 950 951 952
static void
_parse_element_end (GMarkupParseContext * context,
    const gchar * element_name, gpointer self, GError ** error)
{
Thibault Saunier's avatar
Thibault Saunier committed
953 954 955
  GESXmlFormatterPrivate *priv = _GET_PRIV (self);
  SubprojectData *subproj_data = priv->subproject;

956
  /*GESXmlFormatterPrivate *priv = _GET_PRIV (self); */
957

Thibault Saunier's avatar
Thibault Saunier committed
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
  if (!g_strcmp0 (element_name, "ges")) {
    gint subproject_end_line, subproject_end_char;

    if (priv->subproject_depth)
      priv->subproject_depth -= 1;

    if (!subproj_data) {
      if (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);
        _GET_PRIV (self)->ges_opened = FALSE;
      }
    } else if (subproj_data->start_line != -1 && !priv->subproject_depth) {
      g_markup_parse_context_get_position (context, &subproject_end_line,
          &subproject_end_char);
      _save_subproject_data (GES_XML_FORMATTER (self), subproj_data,
          subproject_end_line, subproject_end_char, error);

      subproj_data->filename = NULL;
      g_close (subproj_data->fd, error);
      subproj_data->id = NULL;
      subproj_data->start_line = 0;
      subproj_data->start_char = 0;
    }
987

Thibault Saunier's avatar
Thibault Saunier committed
988 989 990
    if (!priv->subproject_depth) {
      g_clear_pointer (&priv->subproject, g_free);
    }
991
  }
992 993 994 995 996 997
}

static void
_error_parsing (GMarkupParseContext * context, GError * error,
    gpointer user_data)
{
Thibault Saunier's avatar
Thibault Saunier committed
998
  GST_WARNING ("Error occurred when parsing %s", error->message);
999 1000 1001 1002 1003 1004 1005 1006 1007 1008
}

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

/* XML writting utils */
static inline void
Thibault Saunier's avatar
Thibault Saunier committed
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
string_add_indents (GString * str, guint depth, gboolean prepend)
{
  gint i;
  for (i = 0; i < depth; i++)
    prepend ? g_string_prepend (str, "  ") : g_string_append (str, "  ");
}

static inline void
string_append_with_depth (GString * str, const gchar * string, guint depth)
{
  string_add_indents (str, depth, FALSE);
  g_string_append (str, string);
}

static inline void
append_escaped (GString * str, gchar * tmpstr, guint depth)
1025
{
Thibault Saunier's avatar
Thibault Saunier committed
1026
  string_append_with_depth (str, tmpstr, depth);
1027
  g_free (tmpstr);
1028 1029 1030 1031 1032
}

static inline gboolean
_can_serialize_spec (GParamSpec * spec)
{
1033
  if (!(spec->flags & G_PARAM_WRITABLE)) {
1034
    GST_LOG ("%s from %s is not writable",
1035 1036 1037 1038
        spec->name, g_type_name (spec->owner_type));

    return FALSE;
  } else if (spec->flags & G_PARAM_CONSTRUCT_ONLY) {
1039
    GST_LOG ("%s from %s is construct only",
1040 1041 1042 1043 1044
        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)) {
1045
    GST_LOG ("%s from %s is set as GES_PARAM_NO_SERIALIZATION",
1046 1047 1048 1049
        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)) {
1050
    GST_LOG ("%s from %s contains GObject, can't serialize that.",
1051 1052 1053 1054 1055 1056
        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"))) {

1057
    GST_LOG ("We do not want to serialize the name of GstObjects.");
1058 1059
    return FALSE;
  } else if (G_PARAM_SPEC_VALUE_TYPE (spec) == G_TYPE_GTYPE) {
1060
    GST_LOG ("%s from %s contains a GType, can't serialize.",
1061 1062 1063 1064 1065
        spec->name, g_type_name (spec->owner_type));
    return FALSE;
  }

  return TRUE;
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
}

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];
1093 1094
    if (spec->value_type == GST_TYPE_CAPS) {
      GstCaps *caps;
1095
      gchar *caps_str;
1096 1097

      g_object_get (object, spec->name, &caps, NULL);
1098 1099
      caps_str = gst_caps_to_string (caps);
      gst_structure_set (structure, spec->name, G_TYPE_STRING, caps_str, NULL);
1100 1101
      if (caps)
        gst_caps_unref (caps);
1102
      g_free (caps_str);
1103
    } else if (_can_serialize_spec (spec)) {
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
      _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;
}

Thibault Saunier's avatar
Thibault Saunier committed
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
static void
project_loaded_cb (GESProject * project, GESTimeline * timeline,
    SubprojectData * data)
{
  g_main_loop_quit (data->ml);
}

static void
error_loading_asset_cb (GESProject * project, GError * err,
    const gchar * unused_id, GType extractable_type, SubprojectData * data)
{
  data->error = g_error_copy (err);
  g_main_loop_quit (data->ml);
}

static gboolean
_save_subproject (GESXmlFormatter * self, GString * str, GESProject * project,
    GESAsset * subproject, GError ** error, guint depth)
1143
{
Thibault Saunier's avatar
Thibault Saunier committed
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
  GString *substr;
  GESTimeline *timeline;
  gchar *properties, *metas;
  GESXmlFormatterPrivate *priv = self->priv;
  GMainContext *context = g_main_context_get_thread_default ();
  const gchar *id = ges_asset_get_id (subproject);
  SubprojectData data = { 0, };

  if (!g_strcmp0 (ges_asset_get_id (GES_ASSET (project)), id)) {
    g_set_error (error, G_MARKUP_ERROR,
        G_MARKUP_ERROR_INVALID_CONTENT,
        "Project %s trying to recurse into itself", id);
    return FALSE;
  }

  G_LOCK (uri_subprojects_map_lock);
  g_hash_table_insert (priv->subprojects_map, g_strdup (id), g_strdup (id));
  G_UNLOCK (uri_subprojects_map_lock);
  timeline = GES_TIMELINE (ges_asset_extract (subproject, error));
  if (!timeline) {
    return FALSE;
  }

  if (!context)
    context = g_main_context_default ();

  data.ml = g_main_loop_new (context, TRUE);
  g_signal_connect (subproject, "loaded", (GCallback) project_loaded_cb, &data);
  g_signal_connect (subproject, "error-loading-asset",
      (GCallback) project_loaded_cb, &data);
  g_main_loop_run (data.ml);

  g_signal_handlers_disconnect_by_func (subproject, project_loaded_cb, &data);
  g_signal_handlers_disconnect_by_func (subproject, error_loading_asset_cb,
      &data);
  if (data.error) {
    g_propagate_error (error, data.error);
    return FALSE;
  }

  subproject = ges_extractable_get_asset (GES_EXTRACTABLE (timeline));
  substr = g_string_new (NULL);
  properties = _serialize_properties (G_OBJECT (subproject), NULL);
  metas = ges_meta_container_metas_to_string (GES_META_CONTAINER (subproject));
  append_escaped (str,
      g_markup_printf_escaped
      ("      <asset id='%s' extractable-type-name='%s' properties='%s' metadatas='%s'>\n",
          ges_asset_get_id (subproject),
          g_type_name (ges_asset_get_extractable_type (subproject)), properties,
          metas), depth);

  depth += 4;
  GST_DEBUG_OBJECT (self, "Saving subproject %s (depth: %d)",
      ges_asset_get_id (subproject), depth / 4);
  if (!_save_project (GES_FORMATTER (self), substr, GES_PROJECT (subproject),
          timeline, error, depth)) {
    g_string_free (substr, TRUE);
    g_object_unref (subproject);
    goto err;
  }
  GST_DEBUG_OBJECT (self, "DONE Saving subproject %s",
      ges_asset_get_id (subproject));
  depth -= 4;

  g_string_append (str, substr->str);
  g_string_free (substr, TRUE);
  string_append_with_depth (str, "      </asset>\n", depth);

err:
  g_object_unref (subproject);

  return TRUE;
}

static gint
sort_assets (GESAsset * a, GESAsset * b)
{
  if (GES_IS_PROJECT (a))
    return -1;

  if (GES_IS_PROJECT (b))
    return 1;

  return 0;
}

static inline gboolean
_save_assets (GESXmlFormatter * self, GString * str, GESProject * project,
    GError ** error, guint depth)
{
  gchar *properties, *metas;
1235
  GESAsset *asset, *proxy;
1236
  GList *assets, *tmp;
Thibault Saunier's avatar
Thibault Saunier committed
1237 1238
  const gchar *id;
  GESXmlFormatterPrivate *priv = self->priv;
1239 1240

  assets = ges_project_list_assets (project, GES_TYPE_EXTRACTABLE);
Thibault Saunier's avatar
Thibault Saunier committed
1241 1242
  for (tmp = g_list_sort (assets, (GCompareFunc) sort_assets); tmp;
      tmp = tmp->next) {
1243
    asset = GES_ASSET (tmp->data);
Thibault Saunier's avatar
Thibault Saunier committed
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
    id = ges_asset_get_id (asset);

    if (GES_IS_PROJECT (asset)) {
      if (!_save_subproject (self, str, project, asset, error, depth))
        return FALSE;

      continue;
    }

    if (ges_asset_get_extractable_type (asset) == GES_TYPE_URI_CLIP) {
      G_LOCK (uri_subprojects_map_lock);
      if (g_hash_table_contains (priv->subprojects_map, id)) {
        id = g_hash_table_lookup (priv->subprojects_map, id);

        GST_DEBUG_OBJECT (self, "Using subproject %s", id);
      }
      G_UNLOCK (uri_subprojects_map_lock);
    }

1263 1264
    properties = _serialize_properties (G_OBJECT (asset), NULL);
    metas = ges_meta_container_metas_to_string (GES_META_CONTAINER (asset));
1265 1266
    append_escaped (str,
        g_markup_printf_escaped
1267
        ("      <asset id='%s' extractable-type-name='%s' properties='%s' metadatas='%s' ",
Thibault Saunier's avatar
Thibault Saunier committed
1268 1269
            id, g_type_name (ges_asset_get_extractable_type (asset)),
            properties, metas), depth);
1270 1271 1272 1273

    /*TODO Save the whole list of proxies */
    proxy = ges_asset_get_proxy (asset);
    if (proxy) {
Thibault Saunier's avatar
Thibault Saunier committed
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
      const gchar *proxy_id = ges_asset_get_id (proxy);

      if (ges_asset_get_extractable_type (asset) == GES_TYPE_URI_CLIP) {
        G_LOCK (uri_subprojects_map_lock);
        if (g_hash_table_contains (priv->subprojects_map, proxy_id)) {
          proxy_id = g_hash_table_lookup (priv->subprojects_map, proxy_id);

          GST_DEBUG_OBJECT (self, "Using subproject %s", id);
        }
        G_UNLOCK (uri_subprojects_map_lock);
      }
1285
      append_escaped (str, g_markup_printf_escaped (" proxy-id='%s' ",
Thibault Saunier's avatar
Thibault Saunier committed
1286
              proxy_id), depth);
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296

      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);
    }
Thibault Saunier's avatar
Thibault Saunier committed
1297

1298
    g_string_append (str, "/>\n");
1299 1300 1301
    g_free (properties);
    g_free (metas);
  }
Thibault Saunier's avatar
Thibault Saunier committed
1302

1303
  g_list_free_full (assets, gst_object_unref);
Thibault Saunier's avatar
Thibault Saunier committed
1304 1305

  return TRUE;
1306 1307 1308
}

static inline void
Thibault Saunier's avatar
Thibault Saunier committed
1309 1310
_save_tracks (GESXmlFormatter * self, GString * str, GESTimeline * timeline,
    guint depth)
1311 1312 1313 1314
{
  gchar *strtmp, *metas;
  GESTrack *track;
  GList *tmp, *tracks;