ges-base-xml-formatter.c 37.6 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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
23 24 25 26

#include "ges.h"
#include "ges-internal.h"

27 28 29 30
GST_DEBUG_CATEGORY_STATIC (base_xml_formatter);
#undef GST_CAT_DEFAULT
#define GST_CAT_DEFAULT base_xml_formatter

31 32
#define parent_class ges_base_xml_formatter_parent_class

33
#define _GET_PRIV(o) (((GESBaseXmlFormatter*) o)->priv)
34

35 36

static gboolean _loading_done_cb (GESFormatter * self);
37

38 39 40 41 42 43 44
typedef struct PendingGroup
{
  GESGroup *group;

  GList *pending_children;
} PendingGroup;

45 46
typedef struct LayerEntry
{
47
  GESLayer *layer;
48 49 50 51 52 53 54 55
  gboolean auto_trans;
} LayerEntry;

typedef struct PendingAsset
{
  GESFormatter *formatter;
  gchar *metadatas;
  GstStructure *properties;
56
  gchar *proxy_id;
57 58
  GType extractable_type;
  gchar *id;
59 60
} PendingAsset;

61 62 63 64 65 66 67 68 69 70 71 72
/* @STATE_CHECK_LOADABLE: Quickly check if XML is valid
 * @STATE_ASSETS: start loading all assets asynchronously
 * and setup all elements that are synchronously loadable (tracks, and layers basically).
 * @STATE_LOADING_CLIPS: adding clips and groups to the timeline
 */
typedef enum
{
  STATE_CHECK_LOADABLE,
  STATE_LOADING_ASSETS_AND_SYNC,
  STATE_LOADING_CLIPS,
} LoadingState;

73 74 75
struct _GESBaseXmlFormatterPrivate
{
  GMarkupParseContext *parsecontext;
76
  gsize xmlsize;
77
  LoadingState state;
78

79
  /* Clip.ID -> Clip */
80
  GHashTable *containers;
81 82 83 84 85 86 87 88 89

  /* ID -> track */
  GHashTable *tracks;

  /* layer.prio -> LayerEntry */
  GHashTable *layers;

  /* List of asset waited to be created */
  GList *pending_assets;
90

91 92
  GError *asset_error;

93 94
  /* current track element */
  GESTrackElement *current_track_element;
95 96

  GESClip *current_clip;
97 98

  gboolean timeline_auto_transition;
99 100

  GList *groups;
101 102
};

103 104 105
static void new_asset_cb (GESAsset * source, GAsyncResult * res,
    PendingAsset * passet);

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
static const gchar *
loading_state_name (LoadingState state)
{
  switch (state) {
    case STATE_CHECK_LOADABLE:
      return "check-loadable";
    case STATE_LOADING_ASSETS_AND_SYNC:
      return "loading-assets-and-sync";
    case STATE_LOADING_CLIPS:
      return "loading-clips";
  }

  return "??";
}

121

122 123 124 125 126 127 128
static void
_free_layer_entry (LayerEntry * entry)
{
  gst_object_unref (entry->layer);
  g_slice_free (LayerEntry, entry);
}

129 130 131 132 133
static void
_free_pending_group (PendingGroup * pgroup)
{
  if (pgroup->group)
    g_object_unref (pgroup->group);
134
  g_list_free_full (pgroup->pending_children, g_free);
135 136 137
  g_slice_free (PendingGroup, pgroup);
}

138 139
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GESBaseXmlFormatter,
    ges_base_xml_formatter, GES_TYPE_FORMATTER);
140 141 142
static gint
compare_assets_for_loading (PendingAsset * a, PendingAsset * b)
{
Thibault Saunier's avatar
Thibault Saunier committed
143 144 145 146 147 148
  if (a->extractable_type == GES_TYPE_TIMELINE)
    return -1;

  if (b->extractable_type == GES_TYPE_TIMELINE)
    return 1;

149 150 151 152 153 154 155 156
  if (a->proxy_id)
    return -1;

  if (b->proxy_id)
    return 1;

  return 0;
}
157

158
static GMarkupParseContext *
159
_parse (GESBaseXmlFormatter * self, GError ** error, LoadingState state)
160
{
161
  GError *err = NULL;
162 163 164
  GMarkupParseContext *parsecontext = NULL;
  GESBaseXmlFormatterClass *self_class =
      GES_BASE_XML_FORMATTER_GET_CLASS (self);
165
  GESBaseXmlFormatterPrivate *priv = _GET_PRIV (self);
166

Thibault Saunier's avatar
Thibault Saunier committed
167
  if (!self->xmlcontent || g_strcmp0 (self->xmlcontent, "") == 0) {
168
    err = g_error_new (GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_FAILED,
169
        "Nothing contained in the project file.");
170

171
    goto failed;
172
  }
173

174 175 176
  parsecontext = g_markup_parse_context_new (&self_class->content_parser,
      G_MARKUP_TREAT_CDATA_AS_TEXT, self, NULL);

177 178
  priv->state = state;
  GST_DEBUG_OBJECT (self, "Running %s pass", loading_state_name (state));
Thibault Saunier's avatar
Thibault Saunier committed
179
  if (!g_markup_parse_context_parse (parsecontext, self->xmlcontent,
180
          priv->xmlsize, &err))
181 182
    goto failed;

183 184 185
  if (!g_markup_parse_context_end_parse (parsecontext, &err))
    goto failed;

186 187 188 189
  if (priv->pending_assets) {
    GList *tmp;
    priv->pending_assets = g_list_sort (priv->pending_assets,
        (GCompareFunc) compare_assets_for_loading);
190

191 192
    for (tmp = priv->pending_assets; tmp; tmp = tmp->next) {
      PendingAsset *passet = tmp->data;
193

194 195 196 197 198 199 200 201
      ges_asset_request_async (passet->extractable_type, passet->id, NULL,
          (GAsyncReadyCallback) new_asset_cb, passet);
      ges_project_add_loading_asset (GES_FORMATTER (self)->project,
          passet->extractable_type, passet->id);
    }
  }

done:
202 203 204
  return parsecontext;

failed:
205
  GST_WARNING ("failed to load contents: %s", err->message);
206 207
  g_propagate_error (error, err);

208
  if (parsecontext) {
209
    g_markup_parse_context_free (parsecontext);
210 211
    parsecontext = NULL;
  }
212

213
  goto done;
214 215
}

216 217
static GMarkupParseContext *
_load_and_parse (GESBaseXmlFormatter * self, const gchar * uri, GError ** error,
218
    LoadingState state)
219 220 221 222 223 224
{
  GFile *file = NULL;
  GESBaseXmlFormatterPrivate *priv = _GET_PRIV (self);

  GError *err = NULL;

Thibault Saunier's avatar
Thibault Saunier committed
225 226
  GST_DEBUG_OBJECT (self, "loading xml from %s, %s", uri,
      loading_state_name (state));
227 228 229 230 231 232 233 234 235

  file = g_file_new_for_uri (uri);
  /* TODO Handle GCancellable */
  if (!g_file_query_exists (file, NULL)) {
    err = g_error_new (GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_FAILED,
        "Invalid URI: \"%s\"", uri);
    goto failed;
  }

Thibault Saunier's avatar
Thibault Saunier committed
236 237
  g_clear_pointer (&self->xmlcontent, g_free);
  if (!g_file_load_contents (file, NULL, &self->xmlcontent, &priv->xmlsize,
238 239
          NULL, &err))
    goto failed;
240
  g_object_unref (file);
241

242
  return _parse (self, error, state);
243 244

failed:
245
  g_object_unref (file);
246 247 248 249 250
  GST_WARNING ("failed to load contents from \"%s\"", uri);
  g_propagate_error (error, err);
  return NULL;
}

251 252 253 254 255 256 257
/***********************************************
 *                                             *
 * GESFormatter virtual methods implementation *
 *                                             *
 ***********************************************/

static gboolean
258 259
_can_load_uri (GESFormatter * dummy_formatter, const gchar * uri,
    GError ** error)
260 261
{
  GMarkupParseContext *ctx;
262
  GESBaseXmlFormatter *self = GES_BASE_XML_FORMATTER (dummy_formatter);
263

264
  ctx = _load_and_parse (self, uri, error, STATE_CHECK_LOADABLE);
265
  if (!ctx)
266
    return FALSE;
267 268

  g_markup_parse_context_free (ctx);
269
  return TRUE;
270 271 272 273 274 275 276 277
}

static gboolean
_load_from_uri (GESFormatter * self, GESTimeline * timeline, const gchar * uri,
    GError ** error)
{
  GESBaseXmlFormatterPrivate *priv = _GET_PRIV (self);

278
  GST_INFO_OBJECT (self, "Loading %s in %" GST_PTR_FORMAT, uri, timeline);
279 280
  ges_timeline_set_auto_transition (timeline, FALSE);

281
  priv->parsecontext =
282 283
      _load_and_parse (GES_BASE_XML_FORMATTER (self), uri, error,
      STATE_LOADING_ASSETS_AND_SYNC);
284 285 286 287

  if (!priv->parsecontext)
    return FALSE;

288
  if (priv->pending_assets == NULL)
289
    ges_idle_add ((GSourceFunc) _loading_done_cb, g_object_ref (self), NULL);
290

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
  return TRUE;
}

static gboolean
_save_to_uri (GESFormatter * formatter, GESTimeline * timeline,
    const gchar * uri, gboolean overwrite, GError ** error)
{
  GFile *file;
  gboolean ret;
  GString *str;
  GOutputStream *stream;
  GError *lerror = NULL;

  g_return_val_if_fail (formatter->project, FALSE);

  file = g_file_new_for_uri (uri);
  stream = G_OUTPUT_STREAM (g_file_create (file, G_FILE_CREATE_NONE, NULL,
          &lerror));
  if (stream == NULL) {
    if (overwrite && lerror->code == G_IO_ERROR_EXISTS) {
      g_clear_error (&lerror);
      stream = G_OUTPUT_STREAM (g_file_replace (file, NULL, FALSE,
              G_FILE_CREATE_NONE, NULL, &lerror));
    }

    if (stream == NULL)
      goto failed_opening_file;
  }

  str = GES_BASE_XML_FORMATTER_GET_CLASS (formatter)->save (formatter,
      timeline, error);

  if (str == NULL)
    goto serialization_failed;

  ret = g_output_stream_write_all (stream, str->str, str->len, NULL,
      NULL, &lerror);
  ret = g_output_stream_close (stream, NULL, &lerror);

  if (ret == FALSE)
    GST_WARNING_OBJECT (formatter, "Could not save %s because: %s", uri,
        lerror->message);

  g_string_free (str, TRUE);
335 336
  gst_object_unref (file);
  gst_object_unref (stream);
337 338 339 340 341 342 343

  if (lerror)
    g_propagate_error (error, lerror);

  return ret;

serialization_failed:
344
  gst_object_unref (file);
345 346

  g_output_stream_close (stream, NULL, NULL);
347
  gst_object_unref (stream);
348 349 350 351 352 353
  if (lerror)
    g_propagate_error (error, lerror);

  return FALSE;

failed_opening_file:
354
  gst_object_unref (file);
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375

  GST_WARNING_OBJECT (formatter, "Could not open %s because: %s", uri,
      lerror->message);

  if (lerror)
    g_propagate_error (error, lerror);

  return FALSE;
}

/***********************************************
 *                                             *
 *   GOBject virtual methods implementation    *
 *                                             *
 ***********************************************/

static void
_dispose (GObject * object)
{
  GESBaseXmlFormatterPrivate *priv = _GET_PRIV (object);

376 377 378
  g_clear_pointer (&priv->containers, g_hash_table_unref);
  g_clear_pointer (&priv->tracks, g_hash_table_unref);
  g_clear_pointer (&priv->layers, g_hash_table_unref);
379 380 381 382 383 384 385

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

static void
_finalize (GObject * object)
{
Thibault Saunier's avatar
Thibault Saunier committed
386
  GESBaseXmlFormatter *self = GES_BASE_XML_FORMATTER (object);
387 388 389 390
  GESBaseXmlFormatterPrivate *priv = _GET_PRIV (object);

  if (priv->parsecontext != NULL)
    g_markup_parse_context_free (priv->parsecontext);
Thibault Saunier's avatar
Thibault Saunier committed
391
  g_clear_pointer (&self->xmlcontent, g_free);
392

393 394 395
  g_list_free_full (priv->groups, (GDestroyNotify) _free_pending_group);
  priv->groups = NULL;

396 397 398 399 400 401
  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
ges_base_xml_formatter_init (GESBaseXmlFormatter * self)
{
402 403
  GESBaseXmlFormatterPrivate *priv;

404
  self->priv = ges_base_xml_formatter_get_instance_private (self);
405 406

  priv = self->priv;
407 408 409 410

  priv->parsecontext = NULL;
  priv->pending_assets = NULL;

411
  priv->containers = g_hash_table_new_full (g_str_hash,
412 413 414 415 416
      g_str_equal, g_free, gst_object_unref);
  priv->tracks = g_hash_table_new_full (g_str_hash,
      g_str_equal, g_free, gst_object_unref);
  priv->layers = g_hash_table_new_full (g_direct_hash,
      g_direct_equal, NULL, (GDestroyNotify) _free_layer_entry);
417
  priv->current_track_element = NULL;
418
  priv->current_clip = NULL;
419
  priv->timeline_auto_transition = FALSE;
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
}

static void
ges_base_xml_formatter_class_init (GESBaseXmlFormatterClass * self_class)
{
  GESFormatterClass *formatter_klass = GES_FORMATTER_CLASS (self_class);
  GObjectClass *object_class = G_OBJECT_CLASS (self_class);

  object_class->dispose = _dispose;
  object_class->finalize = _finalize;

  formatter_klass->can_load_uri = _can_load_uri;
  formatter_klass->load_from_uri = _load_from_uri;
  formatter_klass->save_to_uri = _save_to_uri;

  self_class->save = NULL;
436

437
  GST_DEBUG_CATEGORY_INIT (base_xml_formatter, "gesbasexmlformatter",
438
      GST_DEBUG_FG_BLUE | GST_DEBUG_BOLD, "Base XML Formatter");
439 440 441 442 443 444 445 446
}

/***********************************************
 *                                             *
 *             Private methods                 *
 *                                             *
 ***********************************************/

447 448 449 450 451 452 453 454 455 456

static GESTrackElement *
_get_element_by_track_id (GESBaseXmlFormatterPrivate * priv,
    const gchar * track_id, GESClip * clip)
{
  GESTrack *track = g_hash_table_lookup (priv->tracks, track_id);

  return ges_clip_find_track_element (clip, track, GES_TYPE_SOURCE);
}

457 458 459
static void
_set_auto_transition (gpointer prio, LayerEntry * entry, gpointer udata)
{
460
  ges_layer_set_auto_transition (entry->layer, entry->auto_trans);
461 462
}

463 464 465 466 467 468 469 470 471 472 473
static void
_add_all_groups (GESFormatter * self)
{
  GList *tmp;
  GESTimelineElement *child;
  GESBaseXmlFormatterPrivate *priv = GES_BASE_XML_FORMATTER (self)->priv;

  for (tmp = priv->groups; tmp; tmp = tmp->next) {
    GList *lchild;
    PendingGroup *pgroup = tmp->data;

474 475
    timeline_add_group (self->timeline, pgroup->group);

476 477 478 479 480 481 482 483 484
    for (lchild = ((PendingGroup *) tmp->data)->pending_children; lchild;
        lchild = lchild->next) {
      child = g_hash_table_lookup (priv->containers, lchild->data);

      GST_DEBUG_OBJECT (tmp->data, "Adding %s child %" GST_PTR_FORMAT " %s",
          (const gchar *) lchild->data, child,
          GES_TIMELINE_ELEMENT_NAME (child));
      ges_container_add (GES_CONTAINER (pgroup->group), child);
    }
485
    pgroup->group = NULL;
486
  }
487 488 489

  g_list_free_full (priv->groups, (GDestroyNotify) _free_pending_group);
  priv->groups = NULL;
490 491
}

492 493 494
static void
_loading_done (GESFormatter * self)
{
495
  GList *assets, *tmp;
496
  GError *error = NULL;
497
  GESBaseXmlFormatterPrivate *priv = GES_BASE_XML_FORMATTER (self)->priv;
498 499 500 501

  if (priv->parsecontext)
    g_markup_parse_context_free (priv->parsecontext);
  priv->parsecontext = NULL;
502 503 504 505 506 507
  /* Go over all assets and make sure that all proxies we were 'trying' to set are finally
   * properly set */
  assets = ges_project_list_assets (self->project, GES_TYPE_EXTRACTABLE);
  for (tmp = assets; tmp; tmp = tmp->next) {
    ges_asset_set_proxy (NULL, tmp->data);
  }
508
  g_list_free_full (assets, g_object_unref);
509

510 511 512 513 514 515
  if (priv->asset_error) {
    error = priv->asset_error;
    priv->asset_error = NULL;
  } else if (priv->state == STATE_LOADING_ASSETS_AND_SYNC) {
    GMarkupParseContext *context =
        _parse (GES_BASE_XML_FORMATTER (self), &error, STATE_LOADING_CLIPS);
516
    GST_INFO_OBJECT (self, "Assets cached... now loading the timeline.");
517 518 519

    if (context)
      g_markup_parse_context_free (context);
520
    g_assert (priv->pending_assets == NULL);
521 522 523 524 525 526
  }

  _add_all_groups (self);
  ges_timeline_set_auto_transition (self->timeline,
      priv->timeline_auto_transition);

527
  g_hash_table_foreach (priv->layers, (GHFunc) _set_auto_transition, NULL);
528 529
  ges_project_set_loaded (self->project, self, error);
  g_clear_error (&error);
530 531
}

532 533 534 535
static gboolean
_loading_done_cb (GESFormatter * self)
{
  _loading_done (self);
536
  gst_object_unref (self);
537 538 539 540

  return FALSE;
}

541
static gboolean
542
_set_child_property (GQuark field_id, const GValue * value,
543
    GESTimelineElement * tlelement)
544 545
{
  GParamSpec *pspec;
546
  GObject *object;
547

548
  /* FIXME: error handling? */
549 550
  if (!ges_timeline_element_lookup_child (tlelement,
          g_quark_to_string (field_id), &object, &pspec)) {
551 552
#ifndef GST_DISABLE_GST_DEBUG
    gchar *tmp = gst_value_serialize (value);
553
    GST_ERROR_OBJECT (tlelement, "Could not set %s=%s",
554 555 556
        g_quark_to_string (field_id), tmp);
    g_free (tmp);
#endif
557
    return TRUE;
558
  }
559

560
  g_object_set_property (G_OBJECT (object), pspec->name, value);
561
  g_param_spec_unref (pspec);
562
  gst_object_unref (object);
563
  return TRUE;
564 565
}

566
gboolean
567 568 569
set_property_foreach (GQuark field_id, const GValue * value, GObject * object)
{
  g_object_set_property (object, g_quark_to_string (field_id), value);
570
  return TRUE;
571 572
}

573
static inline GESClip *
574
_add_object_to_layer (GESBaseXmlFormatterPrivate * priv, const gchar * id,
575
    GESLayer * layer, GESAsset * asset, GstClockTime start,
576
    GstClockTime inpoint, GstClockTime duration,
577
    GESTrackType track_types, const gchar * metadatas,
578 579
    GstStructure * properties, GstStructure * children_properties,
    GError ** error)
580
{
581
  GESClip *clip = ges_layer_add_asset (layer,
582
      asset, start, inpoint, duration, track_types);
583

584
  if (clip == NULL) {
585 586 587 588
    g_set_error (error, GES_ERROR, GES_ERROR_FORMATTER_MALFORMED_INPUT_FILE,
        "Could not add clip %s [ %" GST_TIME_FORMAT ", ( %" GST_TIME_FORMAT
        ") - %" GST_TIME_FORMAT "]", id, GST_TIME_ARGS (start),
        GST_TIME_ARGS (inpoint), GST_TIME_ARGS (duration));
589 590 591 592 593

    return NULL;
  }

  if (metadatas)
594
    ges_meta_container_add_metas_from_string (GES_META_CONTAINER (clip),
595 596 597 598
        metadatas);

  if (properties)
    gst_structure_foreach (properties,
599
        (GstStructureForeachFunc) set_property_foreach, clip);
600

601 602 603 604
  if (children_properties)
    gst_structure_foreach (children_properties,
        (GstStructureForeachFunc) _set_child_property, clip);

605
  g_hash_table_insert (priv->containers, g_strdup (id), gst_object_ref (clip));
606
  return clip;
607 608 609
}

static void
610 611
_add_track_element (GESFormatter * self, GESClip * clip,
    GESTrackElement * trackelement, const gchar * track_id,
612 613 614 615 616 617
    GstStructure * children_properties, GstStructure * properties)
{
  GESBaseXmlFormatterPrivate *priv = _GET_PRIV (self);
  GESTrack *track = g_hash_table_lookup (priv->tracks, track_id);

  if (track == NULL) {
618
    GST_WARNING_OBJECT (self, "No track with id %s, can not add trackelement",
619
        track_id);
620
    gst_object_unref (trackelement);
621 622 623
    return;
  }

624 625
  GST_DEBUG_OBJECT (self, "Adding track_element: %" GST_PTR_FORMAT
      " To : %" GST_PTR_FORMAT, trackelement, clip);
626

Thibault Saunier's avatar
Thibault Saunier committed
627
  ges_container_add (GES_CONTAINER (clip), GES_TIMELINE_ELEMENT (trackelement));
628
  gst_structure_foreach (children_properties,
629
      (GstStructureForeachFunc) _set_child_property, trackelement);
630

631 632 633
  if (properties) {
    /* We do not serialize the priority anymore, and we should never have. */
    gst_structure_remove_field (properties, "priority");
634 635
    gst_structure_foreach (properties,
        (GstStructureForeachFunc) set_property_foreach, trackelement);
636
  }
637 638 639 640 641
}

static void
_free_pending_asset (GESBaseXmlFormatterPrivate * priv, PendingAsset * passet)
{
642
  g_free (passet->metadatas);
643
  g_free (passet->id);
644
  g_free (passet->proxy_id);
645 646 647 648
  if (passet->properties)
    gst_structure_free (passet->properties);

  priv->pending_assets = g_list_remove (priv->pending_assets, passet);
649
  g_slice_free (PendingAsset, passet);
650 651 652 653 654 655 656 657 658 659 660 661 662
}

static void
new_asset_cb (GESAsset * source, GAsyncResult * res, PendingAsset * passet)
{
  GError *error = NULL;
  gchar *possible_id = NULL;
  GESFormatter *self = passet->formatter;
  const gchar *id = ges_asset_get_id (source);
  GESBaseXmlFormatterPrivate *priv = _GET_PRIV (self);
  GESAsset *asset = ges_asset_request_finish (res, &error);

  if (error) {
663 664
    GST_INFO_OBJECT (self, "Error %s creating asset id: %s", error->message,
        id);
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682

    /* We set the metas on the Asset to give hints to the user */
    if (passet->metadatas)
      ges_meta_container_add_metas_from_string (GES_META_CONTAINER (source),
          passet->metadatas);
    if (passet->properties)
      gst_structure_foreach (passet->properties,
          (GstStructureForeachFunc) set_property_foreach, source);

    possible_id = ges_project_try_updating_id (GES_FORMATTER (self)->project,
        source, error);

    if (possible_id == NULL) {
      GST_WARNING_OBJECT (self, "Abandoning creation of asset %s with ID %s"
          "- Error: %s", g_type_name (G_OBJECT_TYPE (source)), id,
          error->message);

      _free_pending_asset (priv, passet);
683 684
      if (!priv->asset_error)
        priv->asset_error = g_error_copy (error);
685 686 687
      goto done;
    }

688
    /* We got a possible ID replacement for that asset, create it */
689 690
    ges_asset_request_async (ges_asset_get_extractable_type (source),
        possible_id, NULL, (GAsyncReadyCallback) new_asset_cb, passet);
691 692
    ges_project_add_loading_asset (GES_FORMATTER (self)->project,
        ges_asset_get_extractable_type (source), possible_id);
693 694 695 696

    goto done;
  }

697 698 699 700 701 702 703
  if (passet->proxy_id) {
    /* We set the URI to be used as a proxy,
     * this will finally be set as the proxy when we
     * are done loading all assets */
    ges_asset_try_proxy (asset, passet->proxy_id);
  }

704 705 706 707 708 709 710 711 712 713 714 715
  /* And now add to the project */
  ges_project_add_asset (self->project, asset);
  gst_object_unref (self);

  _free_pending_asset (priv, passet);

done:
  if (asset)
    gst_object_unref (asset);
  if (possible_id)
    g_free (possible_id);

716 717
  g_clear_error (&error);

718
  if (priv->pending_assets == NULL)
719 720 721
    _loading_done (self);
}

722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
GstElement *
get_element_for_encoding_profile (GstEncodingProfile * prof,
    GstElementFactoryListType type)
{
  GstEncodingProfile *prof_copy;
  GstElement *encodebin;
  GList *tmp;
  GstElement *element = NULL;

  prof_copy = gst_encoding_profile_copy (prof);

  gst_encoding_profile_set_presence (prof_copy, 1);
  gst_encoding_profile_set_preset (prof_copy, NULL);

  encodebin = gst_element_factory_make ("encodebin", NULL);
  g_object_set (encodebin, "profile", prof_copy, NULL);

  GST_OBJECT_LOCK (encodebin);
  for (tmp = GST_BIN (encodebin)->children; tmp; tmp = tmp->next) {
    GstElementFactory *factory;
    factory = gst_element_get_factory (GST_ELEMENT (tmp->data));

    if (factory && gst_element_factory_list_is_type (factory, type)) {
      element = GST_ELEMENT (tmp->data);
      gst_object_ref (element);
      break;
    }
  }
  GST_OBJECT_UNLOCK (encodebin);
  gst_object_unref (encodebin);

  gst_encoding_profile_unref (prof_copy);

  return element;
}

758 759 760 761
static GstEncodingProfile *
_create_profile (GESBaseXmlFormatter * self,
    const gchar * type, const gchar * parent, const gchar * name,
    const gchar * description, GstCaps * format, const gchar * preset,
762 763 764
    GstStructure * preset_properties, const gchar * preset_name, gint id,
    guint presence, GstCaps * restriction, guint pass,
    gboolean variableframerate, gboolean enabled)
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
{
  GstEncodingProfile *profile = NULL;

  if (!g_strcmp0 (type, "container")) {
    profile = GST_ENCODING_PROFILE (gst_encoding_container_profile_new (name,
            description, format, preset));
    gst_encoding_profile_set_preset_name (profile, preset_name);
  } else if (!g_strcmp0 (type, "video")) {
    GstEncodingVideoProfile *sprof = gst_encoding_video_profile_new (format,
        preset, restriction, presence);

    gst_encoding_video_profile_set_variableframerate (sprof, variableframerate);
    gst_encoding_video_profile_set_pass (sprof, pass);

    profile = GST_ENCODING_PROFILE (sprof);
  } else if (!g_strcmp0 (type, "audio")) {
    profile = GST_ENCODING_PROFILE (gst_encoding_audio_profile_new (format,
            preset, restriction, presence));
  } else {
    GST_ERROR_OBJECT (self, "Unknown profile format '%s'", type);

    return NULL;
  }

789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
  if (!g_strcmp0 (type, "video") || !g_strcmp0 (type, "audio")) {
    gst_encoding_profile_set_name (profile, name);
    gst_encoding_profile_set_enabled (profile, enabled);
    gst_encoding_profile_set_description (profile, description);
    gst_encoding_profile_set_preset_name (profile, preset_name);
  }

  if (preset && preset_properties) {
    GstElement *element;

    if (!g_strcmp0 (type, "container")) {
      element = get_element_for_encoding_profile (profile,
          GST_ELEMENT_FACTORY_TYPE_MUXER);
    } else {
      element = get_element_for_encoding_profile (profile,
          GST_ELEMENT_FACTORY_TYPE_ENCODER);
    }

    if (G_UNLIKELY (!element || !GST_IS_PRESET (element))) {
      GST_WARNING_OBJECT (element, "Element is not a GstPreset");
      goto done;
    }

    /* If the preset doesn't exist on the system, create it */
    if (!gst_preset_load_preset (GST_PRESET (element), preset)) {
      gst_structure_foreach (preset_properties,
          (GstStructureForeachFunc) set_property_foreach, element);

      if (!gst_preset_save_preset (GST_PRESET (element), preset)) {
        GST_WARNING_OBJECT (element, "Could not save preset %s", preset);
      }
    }

  done:
    if (element)
      gst_object_unref (element);
  }
826 827 828 829 830 831 832 833 834 835 836 837 838

  return profile;
}

/***********************************************
 *                                             *
 *              Public methods                 *
 *                                             *
 ***********************************************/

void
ges_base_xml_formatter_add_asset (GESBaseXmlFormatter * self,
    const gchar * id, GType extractable_type, GstStructure * properties,
839
    const gchar * metadatas, const gchar * proxy_id, GError ** error)
840 841 842 843
{
  PendingAsset *passet;
  GESBaseXmlFormatterPrivate *priv = _GET_PRIV (self);

844
  if (priv->state != STATE_LOADING_ASSETS_AND_SYNC) {
845
    GST_DEBUG_OBJECT (self, "Not parsing assets in %s state",
846
        loading_state_name (priv->state));
847 848 849 850

    return;
  }

851 852
  passet = g_slice_new0 (PendingAsset);
  passet->metadatas = g_strdup (metadatas);
853 854
  passet->id = g_strdup (id);
  passet->extractable_type = extractable_type;
855
  passet->proxy_id = g_strdup (proxy_id);
856 857 858 859 860 861 862
  passet->formatter = gst_object_ref (self);
  if (properties)
    passet->properties = gst_structure_copy (properties);
  priv->pending_assets = g_list_prepend (priv->pending_assets, passet);
}

void
863
ges_base_xml_formatter_add_clip (GESBaseXmlFormatter * self,
864
    const gchar * id, const char *asset_id, GType type, GstClockTime start,
865
    GstClockTime inpoint, GstClockTime duration,
866
    guint layer_prio, GESTrackType track_types, GstStructure * properties,
867
    GstStructure * children_properties,
868 869 870
    const gchar * metadatas, GError ** error)
{
  GESAsset *asset;
871
  GESClip *nclip;
872 873 874
  LayerEntry *entry;
  GESBaseXmlFormatterPrivate *priv = _GET_PRIV (self);

875 876 877
  if (priv->state != STATE_LOADING_CLIPS) {
    GST_DEBUG_OBJECT (self, "Not adding clip in %s state.",
        loading_state_name (priv->state));
878 879 880
    return;
  }

881 882
  entry = g_hash_table_lookup (priv->layers, GINT_TO_POINTER (layer_prio));
  if (entry == NULL) {
883
    g_set_error (error, GES_ERROR, GES_ERROR_FORMATTER_MALFORMED_INPUT_FILE,
884
        "We got a Clip in a layer"
885 886
        " that does not exist, something is wrong either in the project file or"
        " in %s", g_type_name (G_OBJECT_TYPE (self)));
887
    return;
888 889 890 891
  }

  /* We do not want the properties that are passed to layer-add_asset to be reset */
  if (properties)
892
    gst_structure_remove_fields (properties, "supported-formats",
893
        "inpoint", "start", "duration", NULL);
894 895

  asset = ges_asset_request (type, asset_id, NULL);
896 897
  if (!asset) {
    g_set_error (error, GES_ERROR, GES_ERROR_FORMATTER_MALFORMED_INPUT_FILE,
898 899
        "Clip references asset %s of type %s which was not present in the list of ressource,"
        " the file seems to be malformed.", asset_id, g_type_name (type));
900 901 902
    return;
  }

903
  nclip = _add_object_to_layer (priv, id, entry->layer,
904
      asset, start, inpoint, duration, track_types, metadatas, properties,
905
      children_properties, error);
906

907
  gst_object_unref (asset);
908
  if (!nclip)
909
    return;
910 911

  priv->current_clip = nclip;
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
void
ges_base_xml_formatter_set_timeline_properties (GESBaseXmlFormatter * self,
    GESTimeline * timeline, const gchar * properties, const gchar * metadatas)
{
  GESBaseXmlFormatterPrivate *priv = _GET_PRIV (self);
  gboolean auto_transition = FALSE;

  if (properties) {
    GstStructure *props = gst_structure_from_string (properties, NULL);

    if (props) {
      if (gst_structure_get_boolean (props, "auto-transition",
              &auto_transition))
        gst_structure_remove_field (props, "auto-transition");

      gst_structure_foreach (props,
          (GstStructureForeachFunc) set_property_foreach, timeline);
      gst_structure_free (props);
    }
  }

  if (metadatas) {
    ges_meta_container_add_metas_from_string (GES_META_CONTAINER (timeline),
        metadatas);
  };

  priv->timeline_auto_transition = auto_transition;
}

943 944 945 946 947 948 949
void
ges_base_xml_formatter_add_layer (GESBaseXmlFormatter * self,
    GType extractable_type, guint priority, GstStructure * properties,
    const gchar * metadatas, GError ** error)
{
  LayerEntry *entry;
  GESAsset *asset;
950
  GESLayer *layer;
951 952 953
  gboolean auto_transition = FALSE;
  GESBaseXmlFormatterPrivate *priv = _GET_PRIV (self);

954 955 956
  if (priv->state != STATE_LOADING_ASSETS_AND_SYNC) {
    GST_INFO_OBJECT (self, "Not loading layer in %s state.",
        loading_state_name (priv->state));
957 958 959
    return;
  }

960
  if (extractable_type == G_TYPE_NONE)
961
    layer = ges_layer_new ();
962 963 964 965 966 967 968 969 970 971 972
  else {
    asset = ges_asset_request (extractable_type, NULL, error);
    if (asset == NULL) {
      if (error && *error == NULL) {
        g_set_error (error, G_MARKUP_ERROR,
            G_MARKUP_ERROR_INVALID_CONTENT,
            "Layer type %s could not be created'",
            g_type_name (extractable_type));
        return;
      }
    }
973
    layer = GES_LAYER (ges_asset_extract (asset, error));
974 975
  }

976
  ges_layer_set_priority (layer, priority);
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
  ges_timeline_add_layer (GES_FORMATTER (self)->timeline, layer);
  if (properties) {
    if (gst_structure_get_boolean (properties, "auto-transition",
            &auto_transition))
      gst_structure_remove_field (properties, "auto-transition");

    gst_structure_foreach (properties,
        (GstStructureForeachFunc) set_property_foreach, layer);
  }

  if (metadatas)
    ges_meta_container_add_metas_from_string (GES_META_CONTAINER (layer),
        metadatas);

  entry = g_slice_new0 (LayerEntry);
  entry->layer = gst_object_ref (layer);
  entry->auto_trans = auto_transition;

  g_hash_table_insert (priv->layers, GINT_TO_POINTER (priority), entry);
}

void
ges_base_xml_formatter_add_track (GESBaseXmlFormatter * self,
    GESTrackType track_type, GstCaps * caps, const gchar * id,
    GstStructure * properties, const gchar * metadatas, GError ** error)
{
  GESTrack *track;
  GESBaseXmlFormatterPrivate *priv = _GET_PRIV (self);

1006 1007 1008
  if (priv->state != STATE_LOADING_ASSETS_AND_SYNC) {
    GST_INFO_OBJECT (self, "Not loading track in %s state.",
        loading_state_name (priv->state));
1009
    return;
1010
  }
1011 1012 1013

  track = ges_track_new (track_type, caps);
  ges_timeline_add_track (GES_FORMATTER (self)->timeline, track);
1014 1015 1016

  if (properties) {
    gchar *restriction;
1017
    GstCaps *restriction_caps;
1018 1019 1020

    gst_structure_get (properties, "restriction-caps", G_TYPE_STRING,
        &restriction, NULL);
1021 1022
    gst_structure_remove_fields (properties, "restriction-caps", "caps",
        "message-forward", NULL);
1023
    if (g_strcmp0 (restriction, "NULL")) {
1024 1025 1026
      restriction_caps = gst_caps_from_string (restriction);
      ges_track_set_restriction_caps (track, restriction_caps);
      gst_caps_unref (restriction_caps);
1027
    }
1028 1029
    gst_structure_foreach (properties,
        (GstStructureForeachFunc) set_property_foreach, track);
1030
    g_free (restriction);
1031
  }
1032 1033 1034 1035 1036 1037 1038

  g_hash_table_insert (priv->tracks, g_strdup (id), gst_object_ref (track));
  if (metadatas)
    ges_meta_container_add_metas_from_string (GES_META_CONTAINER (track),
        metadatas);
}

1039 1040 1041
void
ges_base_xml_formatter_add_control_binding (GESBaseXmlFormatter * self,
    const gchar * binding_type, const gchar * source_type,