ges-container.c 29.2 KB
Newer Older
Thibault Saunier's avatar
Thibault Saunier committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* GStreamer Editing Services
 * Copyright (C) <2013> Thibault Saunier <thibault.saunier@collabora.com>
 *               <2013> Collabora Ltd.
 *
 * 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., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/**
22
 * SECTION:gescontainer
23
 * @title: GESContainer
Thibault Saunier's avatar
Thibault Saunier committed
24 25 26
 * @short_description: Base Class for objects responsible for controlling other
 * GESTimelineElement-s
 */
27 28 29
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Thibault Saunier's avatar
Thibault Saunier committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

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

#include <string.h>

GST_DEBUG_CATEGORY_STATIC (ges_container_debug);
#undef GST_CAT_DEFAULT
#define GST_CAT_DEFAULT ges_container_debug

/* Mapping of relationship between a Container and the TimelineElements
 * it controls
 *
 * NOTE : Does it make sense to make it public in the future ?
 */
typedef struct
{
  GESTimelineElement *child;

  GstClockTime start_offset;
  GstClockTime duration_offset;
  GstClockTime inpoint_offset;
  gint32 priority_offset;

  guint start_notifyid;
  guint duration_notifyid;
  guint inpoint_notifyid;
} ChildMapping;

enum
{
  CHILD_ADDED_SIGNAL,
  CHILD_REMOVED_SIGNAL,
  LAST_SIGNAL
};

static guint ges_container_signals[LAST_SIGNAL] = { 0 };

struct _GESContainerPrivate
{
  /*< public > */
72
  GESLayer *layer;
Thibault Saunier's avatar
Thibault Saunier committed
73 74 75 76 77 78 79

  /*< private > */
  /* Set to TRUE when the container is doing updates of track object
   * properties so we don't end up in infinite property update loops
   */
  GHashTable *mappings;
  guint nb_effects;
80 81 82 83

  /* List of GESTimelineElement being in the "child-added" signal
   * emission stage */
  GList *adding_children;
84 85

  GList *copied_children;
Thibault Saunier's avatar
Thibault Saunier committed
86 87 88 89 90 91 92 93 94 95 96
};

enum
{
  PROP_0,
  PROP_HEIGHT,
  PROP_LAST
};

static GParamSpec *properties[PROP_LAST];

97 98 99
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GESContainer, ges_container,
    GES_TYPE_TIMELINE_ELEMENT);

Thibault Saunier's avatar
Thibault Saunier committed
100 101 102 103 104 105 106 107 108
/************************
 *   Private  methods   *
 ************************/
static void
_free_mapping (ChildMapping * mapping)
{
  GESTimelineElement *child = mapping->child;

  /* Disconnect all notify listeners */
109 110 111 112 113 114
  if (mapping->start_notifyid)
    g_signal_handler_disconnect (child, mapping->start_notifyid);
  if (mapping->duration_notifyid)
    g_signal_handler_disconnect (child, mapping->duration_notifyid);
  if (mapping->inpoint_notifyid)
    g_signal_handler_disconnect (child, mapping->inpoint_notifyid);
Thibault Saunier's avatar
Thibault Saunier committed
115 116 117 118 119

  ges_timeline_element_set_parent (child, NULL);
  g_slice_free (ChildMapping, mapping);
}

120 121 122 123 124 125 126
static gint
compare_grouping_prio (GType * a, GType * b)
{
  gint ret = 0;
  GObjectClass *aclass = g_type_class_ref (*a);
  GObjectClass *bclass = g_type_class_ref (*b);

127
  /* We want higher prios to be first */
128 129
  if (GES_CONTAINER_CLASS (aclass)->grouping_priority <
      GES_CONTAINER_CLASS (bclass)->grouping_priority)
130
    ret = 1;
131 132
  else if (GES_CONTAINER_CLASS (aclass)->grouping_priority >
      GES_CONTAINER_CLASS (bclass)->grouping_priority)
133
    ret = -1;
134 135 136 137 138 139

  g_type_class_unref (aclass);
  g_type_class_unref (bclass);
  return ret;
}

140 141 142 143 144 145 146
static void
_resync_start_offsets (GESTimelineElement * child,
    ChildMapping * map, GESContainer * container)
{
  map->start_offset = _START (container) - _START (child);
}

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
/*****************************************************
 *                                                   *
 * GESTimelineElement virtual methods implementation *
 *                                                   *
 *****************************************************/
static gboolean
_set_start (GESTimelineElement * element, GstClockTime start)
{
  GList *tmp;
  ChildMapping *map;
  GESContainer *container = GES_CONTAINER (element);
  GESContainerPrivate *priv = container->priv;

  GST_DEBUG_OBJECT (element, "Updating children offsets, (initiated_move: %"
      GST_PTR_FORMAT ")", container->initiated_move);

  for (tmp = container->children; tmp; tmp = g_list_next (tmp)) {
    GESTimelineElement *child = (GESTimelineElement *) tmp->data;

    map = g_hash_table_lookup (priv->mappings, child);
    map->start_offset = start - _START (child);
  }
169
  container->children_control_mode = GES_CHILDREN_UPDATE;
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

  return TRUE;
}

static gboolean
_set_inpoint (GESTimelineElement * element, GstClockTime inpoint)
{
  GList *tmp;
  GESContainer *container = GES_CONTAINER (element);

  for (tmp = container->children; tmp; tmp = g_list_next (tmp)) {
    GESTimelineElement *child = (GESTimelineElement *) tmp->data;
    ChildMapping *map = g_hash_table_lookup (container->priv->mappings, child);

    map->inpoint_offset = inpoint - _INPOINT (child);
  }

  return TRUE;
}

static gboolean
_set_duration (GESTimelineElement * element, GstClockTime duration)
{
  GList *tmp;
  GESContainer *container = GES_CONTAINER (element);
  GESContainerPrivate *priv = container->priv;

  for (tmp = container->children; tmp; tmp = g_list_next (tmp)) {
    GESTimelineElement *child = (GESTimelineElement *) tmp->data;
    ChildMapping *map = g_hash_table_lookup (priv->mappings, child);

    map->duration_offset = duration - _DURATION (child);
  }

  return TRUE;
}

207 208 209 210 211 212 213 214 215 216 217 218
static void
_ges_container_add_child_properties (GESContainer * container,
    GESTimelineElement * child)
{
  guint n_props, i;

  GParamSpec **child_props =
      ges_timeline_element_list_children_properties (child,
      &n_props);

  for (i = 0; i < n_props; i++) {
    GObject *prop_child;
219 220 221
    gchar *prop_name = g_strdup_printf ("%s::%s",
        g_type_name (child_props[i]->owner_type),
        child_props[i]->name);
222

223
    if (ges_timeline_element_lookup_child (child, prop_name, &prop_child, NULL)) {
224 225
      ges_timeline_element_add_child_property (GES_TIMELINE_ELEMENT (container),
          child_props[i], prop_child);
226 227
      gst_object_unref (prop_child);

228
    }
229
    g_free (prop_name);
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    g_param_spec_unref (child_props[i]);
  }

  g_free (child_props);
}

static void
_ges_container_remove_child_properties (GESContainer * container,
    GESTimelineElement * child)
{
  guint n_props, i;

  GParamSpec **child_props =
      ges_timeline_element_list_children_properties (child,
      &n_props);

  for (i = 0; i < n_props; i++) {
    GObject *prop_child;
248 249 250
    gchar *prop_name = g_strdup_printf ("%s::%s",
        g_type_name (child_props[i]->owner_type),
        child_props[i]->name);
251

252
    if (ges_timeline_element_lookup_child (child, prop_name, &prop_child, NULL)) {
253 254
      ges_timeline_element_remove_child_property (GES_TIMELINE_ELEMENT
          (container), child_props[i]);
255
      gst_object_unref (prop_child);
256 257 258

    }

259
    g_free (prop_name);
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    g_param_spec_unref (child_props[i]);
  }

  g_free (child_props);
}

static GParamSpec **
_list_children_properties (GESTimelineElement * self, guint * n_properties)
{
  GList *tmp;

  for (tmp = GES_CONTAINER_CHILDREN (self); tmp; tmp = tmp->next)
    _ges_container_add_child_properties (GES_CONTAINER (self), tmp->data);

  return
      GES_TIMELINE_ELEMENT_CLASS
      (ges_container_parent_class)->list_children_properties (self,
      n_properties);
}

static gboolean
_lookup_child (GESTimelineElement * self, const gchar * prop_name,
    GObject ** child, GParamSpec ** pspec)
{
  GList *tmp;

286
  /* FIXME Implement a syntax to precisely get properties by path */
287 288 289 290 291 292
  for (tmp = GES_CONTAINER_CHILDREN (self); tmp; tmp = tmp->next) {
    if (ges_timeline_element_lookup_child (tmp->data, prop_name, child, pspec))
      return TRUE;
  }

  return FALSE;
293 294
}

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
static GESTrackType
_get_track_types (GESTimelineElement * object)
{
  GESTrackType types = GES_TRACK_TYPE_UNKNOWN;
  GList *tmp, *children = ges_container_get_children (GES_CONTAINER (object),
      TRUE);

  for (tmp = children; tmp; tmp = tmp->next) {
    if (GES_IS_TRACK_ELEMENT (tmp->data)) {
      types |= ges_timeline_element_get_track_types (tmp->data);
    }
  }

  g_list_free_full (children, gst_object_unref);

  return types ^ GES_TRACK_TYPE_UNKNOWN;
}

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
static void
_deep_copy (GESTimelineElement * element, GESTimelineElement * copy)
{
  GList *tmp;
  GESContainer *self = GES_CONTAINER (element), *ccopy = GES_CONTAINER (copy);

  for (tmp = GES_CONTAINER_CHILDREN (element); tmp; tmp = tmp->next) {
    ChildMapping *map;

    map =
        g_slice_dup (ChildMapping, g_hash_table_lookup (self->priv->mappings,
            tmp->data));
    map->child = ges_timeline_element_copy (tmp->data, TRUE);
    map->start_notifyid = 0;
    map->inpoint_notifyid = 0;
    map->duration_notifyid = 0;

    ccopy->priv->copied_children = g_list_prepend (ccopy->priv->copied_children,
        map);
  }
}

static GESTimelineElement *
336 337 338 339
_paste (GESTimelineElement * element, GESTimelineElement * ref,
    GstClockTime paste_position)
{
  GList *tmp;
340 341 342
  ChildMapping *map;
  GESContainer *ncontainer =
      GES_CONTAINER (ges_timeline_element_copy (element, FALSE));
343 344
  GESContainer *self = GES_CONTAINER (element);

345 346
  for (tmp = self->priv->copied_children; tmp; tmp = tmp->next) {
    GESTimelineElement *nchild;
347

348 349 350 351 352
    map = tmp->data;
    nchild =
        ges_timeline_element_paste (map->child,
        paste_position - map->start_offset);
    ges_timeline_element_set_timeline (GES_TIMELINE_ELEMENT (ncontainer),
353
        GES_TIMELINE_ELEMENT_TIMELINE (ref));
354
    ges_container_add (ncontainer, nchild);
355 356
  }

357
  return GES_TIMELINE_ELEMENT (ncontainer);
358 359 360
}


Thibault Saunier's avatar
Thibault Saunier committed
361 362 363 364 365 366 367 368
/******************************************
 *                                        *
 * GObject virtual methods implementation *
 *                                        *
 ******************************************/
static void
_dispose (GObject * object)
{
369
  GList *tmp;
Thibault Saunier's avatar
Thibault Saunier committed
370
  GESContainer *self = GES_CONTAINER (object);
371
  GList *children;
372

373 374 375 376
  _ges_container_sort_children (self);
  children = ges_container_get_children (self, FALSE);

  for (tmp = g_list_last (children); tmp; tmp = tmp->prev)
377
    ges_container_remove (self, tmp->data);
Thibault Saunier's avatar
Thibault Saunier committed
378

379 380
  g_list_free_full (children, gst_object_unref);
  self->children = NULL;
381 382

  G_OBJECT_CLASS (ges_container_parent_class)->dispose (object);
Thibault Saunier's avatar
Thibault Saunier committed
383 384
}

385 386 387 388 389 390 391 392
static void
_finalize (GObject * object)
{
  GESContainer *self = GES_CONTAINER (object);

  g_list_free_full (self->priv->copied_children,
      (GDestroyNotify) _free_mapping);

393
  G_OBJECT_CLASS (ges_container_parent_class)->finalize (object);
394 395
}

Thibault Saunier's avatar
Thibault Saunier committed
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
static void
_get_property (GObject * container, guint property_id,
    GValue * value, GParamSpec * pspec)
{
  GESContainer *tobj = GES_CONTAINER (container);

  switch (property_id) {
    case PROP_HEIGHT:
      g_value_set_uint (value, tobj->height);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (container, property_id, pspec);
  }
}

static void
_set_property (GObject * container, guint property_id,
    const GValue * value, GParamSpec * pspec)
{
  switch (property_id) {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (container, property_id, pspec);
  }
}

static void
ges_container_class_init (GESContainerClass * klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
425
  GESTimelineElementClass *element_class = GES_TIMELINE_ELEMENT_CLASS (klass);
Thibault Saunier's avatar
Thibault Saunier committed
426 427 428 429 430 431 432

  GST_DEBUG_CATEGORY_INIT (ges_container_debug, "gescontainer",
      GST_DEBUG_FG_YELLOW, "ges container");

  object_class->get_property = _get_property;
  object_class->set_property = _set_property;
  object_class->dispose = _dispose;
433
  object_class->finalize = _finalize;
Thibault Saunier's avatar
Thibault Saunier committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451

  /**
   * GESContainer:height:
   *
   * The span of priorities which this container occupies.
   */
  properties[PROP_HEIGHT] = g_param_spec_uint ("height", "Height",
      "The span of priorities this container occupies", 0, G_MAXUINT, 1,
      G_PARAM_READABLE);
  g_object_class_install_property (object_class, PROP_HEIGHT,
      properties[PROP_HEIGHT]);

  /**
   * GESContainer::child-added:
   * @container: the #GESContainer
   * @element: the #GESTimelineElement that was added.
   *
   * Will be emitted after a child was added to @container.
452 453 454
   * Usually you should connect with #g_signal_connect_after
   * as in the first emission stage, the signal emission might
   * get stopped internally.
Thibault Saunier's avatar
Thibault Saunier committed
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
   */
  ges_container_signals[CHILD_ADDED_SIGNAL] =
      g_signal_new ("child-added", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESContainerClass, child_added),
      NULL, NULL, g_cclosure_marshal_generic,
      G_TYPE_NONE, 1, GES_TYPE_TIMELINE_ELEMENT);

  /**
   * GESContainer::child-removed:
   * @container: the #GESContainer
   * @element: the #GESTimelineElement that was removed.
   *
   * Will be emitted after a child was removed from @container.
   */
  ges_container_signals[CHILD_REMOVED_SIGNAL] =
      g_signal_new ("child-removed", G_TYPE_FROM_CLASS (klass),
471
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESContainerClass, child_removed),
Thibault Saunier's avatar
Thibault Saunier committed
472 473 474 475
      NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
      GES_TYPE_TIMELINE_ELEMENT);


476 477 478
  element_class->set_start = _set_start;
  element_class->set_duration = _set_duration;
  element_class->set_inpoint = _set_inpoint;
479 480
  element_class->list_children_properties = _list_children_properties;
  element_class->lookup_child = _lookup_child;
481
  element_class->get_track_types = _get_track_types;
482
  element_class->paste = _paste;
483
  element_class->deep_copy = _deep_copy;
484

Thibault Saunier's avatar
Thibault Saunier committed
485 486 487
  /* No default implementations */
  klass->remove_child = NULL;
  klass->add_child = NULL;
488
  klass->ungroup = NULL;
489
  klass->group = NULL;
490
  klass->grouping_priority = 0;
491
  klass->edit = NULL;
Thibault Saunier's avatar
Thibault Saunier committed
492 493 494 495 496
}

static void
ges_container_init (GESContainer * self)
{
497
  self->priv = ges_container_get_instance_private (self);
Thibault Saunier's avatar
Thibault Saunier committed
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519

  /* FIXME, check why default was GST_SECOND? (before the existend of
   * ges-container)
   *
   * _DURATION (self) = GST_SECOND; */
  self->height = 1;             /* FIXME Why 1 and not 0? */
  self->children = NULL;

  self->priv->mappings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
      NULL, (GDestroyNotify) _free_mapping);
}

/**********************************************
 *                                            *
 *    Property notifications from Children    *
 *                                            *
 **********************************************/
static void
_child_start_changed_cb (GESTimelineElement * child,
    GParamSpec * arg G_GNUC_UNUSED, GESContainer * container)
{
  ChildMapping *map;
520
  GstClockTime start;
Thibault Saunier's avatar
Thibault Saunier committed
521 522 523 524 525 526 527

  GESContainerPrivate *priv = container->priv;
  GESTimelineElement *element = GES_TIMELINE_ELEMENT (container);

  map = g_hash_table_lookup (priv->mappings, child);
  g_assert (map);

528 529 530 531 532 533 534
  switch (container->children_control_mode) {
    case GES_CHILDREN_IGNORE_NOTIFIES:
      return;
    case GES_CHILDREN_UPDATE_ALL_VALUES:
      _ges_container_sort_children (container);
      start = container->children ?
          _START (container->children->data) : _START (container);
535

536 537 538
      if (start != _START (container)) {
        _DURATION (container) = _END (container) - start;
        _START (container) = start;
Thibault Saunier's avatar
Thibault Saunier committed
539

540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
        GST_DEBUG_OBJECT (container, "Child move made us "
            "move to %" GST_TIME_FORMAT, GST_TIME_ARGS (_START (container)));

        g_object_notify (G_OBJECT (container), "start");
      }

      /* Falltrough! */
    case GES_CHILDREN_UPDATE_OFFSETS:
      map->start_offset = _START (container) - _START (child);
      break;

    case GES_CHILDREN_UPDATE:
      /* We update all the children calling our set_start method */
      container->initiated_move = child;
      _set_start0 (element, _START (child) + map->start_offset);
      container->initiated_move = NULL;
      break;
    default:
      break;
  }
Thibault Saunier's avatar
Thibault Saunier committed
560 561 562 563 564 565 566 567 568 569 570
}

static void
_child_inpoint_changed_cb (GESTimelineElement * child,
    GParamSpec * arg G_GNUC_UNUSED, GESContainer * container)
{
  ChildMapping *map;

  GESContainerPrivate *priv = container->priv;
  GESTimelineElement *element = GES_TIMELINE_ELEMENT (container);

571
  if (container->children_control_mode == GES_CHILDREN_IGNORE_NOTIFIES)
Thibault Saunier's avatar
Thibault Saunier committed
572 573 574 575 576
    return;

  map = g_hash_table_lookup (priv->mappings, child);
  g_assert (map);

577 578 579 580 581 582
  if (container->children_control_mode == GES_CHILDREN_UPDATE_OFFSETS) {
    map->inpoint_offset = _START (container) - _START (child);

    return;
  }

Thibault Saunier's avatar
Thibault Saunier committed
583
  /* We update all the children calling our set_inpoint method */
584
  container->initiated_move = child;
Thibault Saunier's avatar
Thibault Saunier committed
585
  _set_inpoint0 (element, _INPOINT (child) + map->inpoint_offset);
586
  container->initiated_move = NULL;
Thibault Saunier's avatar
Thibault Saunier committed
587 588 589 590 591 592 593 594
}

static void
_child_duration_changed_cb (GESTimelineElement * child,
    GParamSpec * arg G_GNUC_UNUSED, GESContainer * container)
{
  ChildMapping *map;

595 596
  GList *tmp;
  GstClockTime end = 0;
Thibault Saunier's avatar
Thibault Saunier committed
597 598 599
  GESContainerPrivate *priv = container->priv;
  GESTimelineElement *element = GES_TIMELINE_ELEMENT (container);

600
  if (container->children_control_mode == GES_CHILDREN_IGNORE_NOTIFIES)
Thibault Saunier's avatar
Thibault Saunier committed
601 602 603 604 605
    return;

  map = g_hash_table_lookup (priv->mappings, child);
  g_assert (map);

606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
  switch (container->children_control_mode) {
    case GES_CHILDREN_IGNORE_NOTIFIES:
      break;
    case GES_CHILDREN_UPDATE_ALL_VALUES:
      _ges_container_sort_children_by_end (container);

      for (tmp = container->children; tmp; tmp = tmp->next)
        end = MAX (end, _END (tmp->data));

      if (end != _END (container)) {
        _DURATION (container) = end - _START (container);
        g_object_notify (G_OBJECT (container), "duration");
      }
      /* Falltrough */
    case GES_CHILDREN_UPDATE_OFFSETS:
      map->inpoint_offset = _START (container) - _START (child);
      break;
    case GES_CHILDREN_UPDATE:
      /* We update all the children calling our set_duration method */
      container->initiated_move = child;
      _set_duration0 (element, _DURATION (child) + map->duration_offset);
      container->initiated_move = NULL;
      break;
    default:
      break;
631
  }
Thibault Saunier's avatar
Thibault Saunier committed
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
}

/****************************************************
 *                                                  *
 *         Internal methods implementation          *
 *                                                  *
 ****************************************************/

void
_ges_container_sort_children (GESContainer * container)
{
  container->children = g_list_sort (container->children,
      (GCompareFunc) element_start_compare);
}

647 648 649 650 651 652 653
void
_ges_container_sort_children_by_end (GESContainer * container)
{
  container->children = g_list_sort (container->children,
      (GCompareFunc) element_end_compare);
}

654 655 656 657 658 659 660 661
void
_ges_container_set_height (GESContainer * container, guint32 height)
{
  if (container->height != height) {
    container->height = height;
    GST_DEBUG_OBJECT (container, "Updating height %i", container->height);
    g_object_notify (G_OBJECT (container), "height");
  }
Thibault Saunier's avatar
Thibault Saunier committed
662 663
}

664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
gint
_ges_container_get_priority_offset (GESContainer * container,
    GESTimelineElement * elem)
{
  ChildMapping *map = g_hash_table_lookup (container->priv->mappings, elem);

  g_return_val_if_fail (map, 0);

  return map->priority_offset;
}

void
_ges_container_set_priority_offset (GESContainer * container,
    GESTimelineElement * elem, gint32 priority_offset)
{
  ChildMapping *map = g_hash_table_lookup (container->priv->mappings, elem);

  g_return_if_fail (map);

  map->priority_offset = priority_offset;
}

Thibault Saunier's avatar
Thibault Saunier committed
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
/**********************************************
 *                                            *
 *            API implementation              *
 *                                            *
 **********************************************/

/**
 * ges_container_add:
 * @container: a #GESContainer
 * @child: the #GESTimelineElement
 *
 * Add the #GESTimelineElement to the container.
 *
 * Returns: %TRUE on success, %FALSE on failure.
 */
gboolean
ges_container_add (GESContainer * container, GESTimelineElement * child)
{
  ChildMapping *mapping;
705
  gboolean notify_start = FALSE;
Thibault Saunier's avatar
Thibault Saunier committed
706 707 708 709 710
  GESContainerClass *class;
  GESContainerPrivate *priv;

  g_return_val_if_fail (GES_IS_CONTAINER (container), FALSE);
  g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (child), FALSE);
711
  g_return_val_if_fail (GES_TIMELINE_ELEMENT_PARENT (child) == NULL, FALSE);
Thibault Saunier's avatar
Thibault Saunier committed
712 713 714 715 716 717 718

  class = GES_CONTAINER_GET_CLASS (container);
  priv = container->priv;

  GST_DEBUG_OBJECT (container, "adding timeline element %" GST_PTR_FORMAT,
      child);

719
  container->children_control_mode = GES_CHILDREN_IGNORE_NOTIFIES;
Thibault Saunier's avatar
Thibault Saunier committed
720
  if (class->add_child) {
721
    if (class->add_child (container, child) == FALSE) {
722
      container->children_control_mode = GES_CHILDREN_UPDATE;
723
      GST_WARNING_OBJECT (container, "Erreur adding child %p", child);
Thibault Saunier's avatar
Thibault Saunier committed
724
      return FALSE;
725
    }
Thibault Saunier's avatar
Thibault Saunier committed
726
  }
727
  container->children_control_mode = GES_CHILDREN_UPDATE;
Thibault Saunier's avatar
Thibault Saunier committed
728

729 730 731 732 733 734 735 736
  if (_START (container) > _START (child)) {
    _START (container) = _START (child);

    g_hash_table_foreach (priv->mappings, (GHFunc) _resync_start_offsets,
        container);
    notify_start = TRUE;
  }

Thibault Saunier's avatar
Thibault Saunier committed
737
  mapping = g_slice_new0 (ChildMapping);
738
  mapping->child = gst_object_ref (child);
Thibault Saunier's avatar
Thibault Saunier committed
739 740 741 742 743 744 745 746
  mapping->start_offset = _START (container) - _START (child);
  mapping->duration_offset = _DURATION (container) - _DURATION (child);
  mapping->inpoint_offset = _INPOINT (container) - _INPOINT (child);

  g_hash_table_insert (priv->mappings, child, mapping);

  container->children = g_list_prepend (container->children, child);

747 748
  _ges_container_sort_children (container);

Thibault Saunier's avatar
Thibault Saunier committed
749 750 751 752 753 754 755 756
  /* Listen to all property changes */
  mapping->start_notifyid =
      g_signal_connect (G_OBJECT (child), "notify::start",
      G_CALLBACK (_child_start_changed_cb), container);
  mapping->duration_notifyid =
      g_signal_connect (G_OBJECT (child), "notify::duration",
      G_CALLBACK (_child_duration_changed_cb), container);
  mapping->inpoint_notifyid =
757
      g_signal_connect (G_OBJECT (child), "notify::in-point",
Thibault Saunier's avatar
Thibault Saunier committed
758 759 760 761
      G_CALLBACK (_child_inpoint_changed_cb), container);

  if (ges_timeline_element_set_parent (child, GES_TIMELINE_ELEMENT (container))
      == FALSE) {
762 763 764 765

    g_hash_table_remove (priv->mappings, child);
    container->children = g_list_remove (container->children, child);
    _ges_container_sort_children (container);
Thibault Saunier's avatar
Thibault Saunier committed
766 767 768 769

    return FALSE;
  }

770 771
  _ges_container_add_child_properties (container, child);

772
  priv->adding_children = g_list_prepend (priv->adding_children, child);
Thibault Saunier's avatar
Thibault Saunier committed
773 774
  g_signal_emit (container, ges_container_signals[CHILD_ADDED_SIGNAL], 0,
      child);
775
  priv->adding_children = g_list_remove (priv->adding_children, child);
Thibault Saunier's avatar
Thibault Saunier committed
776

777 778 779
  if (notify_start)
    g_object_notify (G_OBJECT (container), "start");

Thibault Saunier's avatar
Thibault Saunier committed
780 781 782 783 784 785
  return TRUE;
}

/**
 * ges_container_remove:
 * @container: a #GESContainer
Thibault Saunier's avatar
Thibault Saunier committed
786
 * @child: the #GESTimelineElement to release
Thibault Saunier's avatar
Thibault Saunier committed
787
 *
Thibault Saunier's avatar
Thibault Saunier committed
788
 * Release the @child from the control of @container.
Thibault Saunier's avatar
Thibault Saunier committed
789
 *
Thibault Saunier's avatar
Thibault Saunier committed
790
 * Returns: %TRUE if the @child was properly released, else %FALSE.
Thibault Saunier's avatar
Thibault Saunier committed
791 792
 */
gboolean
Thibault Saunier's avatar
Thibault Saunier committed
793
ges_container_remove (GESContainer * container, GESTimelineElement * child)
Thibault Saunier's avatar
Thibault Saunier committed
794 795 796 797 798
{
  GESContainerClass *klass;
  GESContainerPrivate *priv;

  g_return_val_if_fail (GES_IS_CONTAINER (container), FALSE);
Thibault Saunier's avatar
Thibault Saunier committed
799
  g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (child), FALSE);
Thibault Saunier's avatar
Thibault Saunier committed
800

Thibault Saunier's avatar
Thibault Saunier committed
801
  GST_DEBUG_OBJECT (container, "removing child: %" GST_PTR_FORMAT, child);
Thibault Saunier's avatar
Thibault Saunier committed
802 803 804 805

  klass = GES_CONTAINER_GET_CLASS (container);
  priv = container->priv;

Thibault Saunier's avatar
Thibault Saunier committed
806
  if (!(g_hash_table_lookup (priv->mappings, child))) {
Thibault Saunier's avatar
Thibault Saunier committed
807 808 809 810 811 812
    GST_WARNING_OBJECT (container, "Element isn't controlled by this "
        "container");
    return FALSE;
  }

  if (klass->remove_child) {
Thibault Saunier's avatar
Thibault Saunier committed
813
    if (klass->remove_child (container, child) == FALSE)
Thibault Saunier's avatar
Thibault Saunier committed
814 815 816
      return FALSE;
  }

Thibault Saunier's avatar
Thibault Saunier committed
817
  container->children = g_list_remove (container->children, child);
Thibault Saunier's avatar
Thibault Saunier committed
818
  /* Let it live removing from our mappings */
Thibault Saunier's avatar
Thibault Saunier committed
819
  g_hash_table_remove (priv->mappings, child);
Thibault Saunier's avatar
Thibault Saunier committed
820

821 822
  _ges_container_remove_child_properties (container, child);

823 824 825 826 827 828 829
  if (!g_list_find (container->priv->adding_children, child)) {
    g_signal_emit (container, ges_container_signals[CHILD_REMOVED_SIGNAL], 0,
        child);
  } else {
    GST_INFO_OBJECT (container, "Not emitting 'child-removed' signal as child"
        " removal happend during 'child-added' signal emission");
  }
830
  gst_object_unref (child);
Thibault Saunier's avatar
Thibault Saunier committed
831 832 833 834

  return TRUE;
}

835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
static void
_get_children_recursively (GESContainer * container, GList ** children)
{
  GList *tmp;

  *children =
      g_list_concat (*children, g_list_copy_deep (container->children,
          (GCopyFunc) gst_object_ref, NULL));

  for (tmp = container->children; tmp; tmp = tmp->next) {
    GESTimelineElement *element = tmp->data;

    if (GES_IS_CONTAINER (element))
      _get_children_recursively (tmp->data, children);
  }
}

Thibault Saunier's avatar
Thibault Saunier committed
852 853 854
/**
 * ges_container_get_children:
 * @container: a #GESContainer
855
 * @recursive:  Whether to recursively get children in @container
Thibault Saunier's avatar
Thibault Saunier committed
856 857 858 859 860 861 862 863 864
 *
 * Get the list of #GESTimelineElement contained in @container
 * The user is responsible for unreffing the contained objects
 * and freeing the list.
 *
 * Returns: (transfer full) (element-type GESTimelineElement): The list of
 * timeline element contained in @container.
 */
GList *
865
ges_container_get_children (GESContainer * container, gboolean recursive)
Thibault Saunier's avatar
Thibault Saunier committed
866
{
867 868
  GList *children = NULL;

Thibault Saunier's avatar
Thibault Saunier committed
869 870
  g_return_val_if_fail (GES_IS_CONTAINER (container), NULL);

871 872 873 874 875 876
  if (!recursive)
    return g_list_copy_deep (container->children, (GCopyFunc) gst_object_ref,
        NULL);

  _get_children_recursively (container, &children);
  return children;
Thibault Saunier's avatar
Thibault Saunier committed
877
}
878 879

/**
880
 * ges_container_ungroup:
881 882 883 884 885
 * @container: (transfer full): The #GESContainer to ungroup
 * @recursive: Wether to recursively ungroup @container
 *
 * Ungroups the #GESTimelineElement contained in this GESContainer,
 * creating new #GESContainer containing those #GESTimelineElement
886
 * apropriately.
887
 *
888
 * Returns: (transfer full) (element-type GESContainer): The list of
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
 * #GESContainer resulting from the ungrouping operation
 * The user is responsible for unreffing the contained objects
 * and freeing the list.
 */
GList *
ges_container_ungroup (GESContainer * container, gboolean recursive)
{
  GESContainerClass *klass;

  g_return_val_if_fail (GES_IS_CONTAINER (container), NULL);

  GST_DEBUG_OBJECT (container, "Ungrouping container %s recursively",
      recursive ? "" : "not");

  klass = GES_CONTAINER_GET_CLASS (container);
  if (klass->ungroup == NULL) {
    GST_INFO_OBJECT (container, "No ungoup virtual method, doint nothing");
    return NULL;
  }

  return klass->ungroup (container, recursive);
}
911 912 913

/**
 * ges_container_group:
914
 * @containers: (transfer none)(element-type GESContainer) (allow-none): The
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
 * #GESContainer to group, they must all be in a same #GESTimeline
 *
 * Groups the #GESContainer-s provided in @containers. It creates a subclass
 * of #GESContainer, depending on the containers provided in @containers.
 * Basically, if all the containers in @containers should be contained in a same
 * clip (all the #GESTrackElement they contain have the exact same
 * start/inpoint/duration and are in the same layer), it will create a #GESClip
 * otherwise a #GESGroup will be created
 *
 * Returns: (transfer none): The #GESContainer (subclass) resulting of the
 * grouping
 */
GESContainer *
ges_container_group (GList * containers)
{
  GList *tmp;
931 932
  guint n_children;
  GType *children_types;
933 934 935
  GESTimelineElement *element;
  GObjectClass *clip_class;

936 937
  guint i = 0;
  GESContainer *ret = NULL;
938
  GESTimeline *timeline = NULL;
939

940 941 942 943 944
  if (containers) {
    element = GES_TIMELINE_ELEMENT (containers->data);
    timeline = GES_TIMELINE_ELEMENT_TIMELINE (element);
    g_return_val_if_fail (timeline, NULL);
  }
945 946 947 948 949 950

  if (g_list_length (containers) == 1)
    return containers->data;

  for (tmp = containers; tmp; tmp = tmp->next) {
    g_return_val_if_fail (GES_IS_CONTAINER (tmp->data), NULL);
951 952 953 954
    g_return_val_if_fail (GES_TIMELINE_ELEMENT_PARENT (tmp->data) == NULL,
        NULL);
    g_return_val_if_fail (GES_TIMELINE_ELEMENT_TIMELINE (tmp->data) == timeline,
        NULL);
955 956
  }

957 958 959 960 961 962 963 964 965 966 967
  children_types = g_type_children (GES_TYPE_CONTAINER, &n_children);
  g_qsort_with_data (children_types, n_children, sizeof (GType),
      (GCompareDataFunc) compare_grouping_prio, NULL);

  for (i = 0; i < n_children; i++) {
    clip_class = g_type_class_peek (children_types[i]);
    ret = GES_CONTAINER_CLASS (clip_class)->group (containers);

    if (ret)
      break;
  }
968

969
  g_free (children_types);
970 971
  return ret;
}
972 973 974 975 976 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_container_edit:
 * @container: the #GESClip to edit
 * @layers: (element-type GESLayer): The layers you want the edit to
 *  happen in, %NULL means that the edition is done in all the
 *  #GESLayers contained in the current timeline.
 * @new_layer_priority: The priority of the layer @container should land in.
 *  If the layer you're trying to move the container to doesn't exist, it will
 *  be created automatically. -1 means no move.
 * @mode: The #GESEditMode in which the editition will happen.
 * @edge: The #GESEdge the edit should happen on.
 * @position: The position at which to edit @container (in nanosecond)
 *
 * Edit @container in the different exisiting #GESEditMode modes. In the case of
 * slide, and roll, you need to specify a #GESEdge
 *
 * Returns: %TRUE if the container as been edited properly, %FALSE if an error
 * occured
 */
gboolean
ges_container_edit (GESContainer * container, GList * layers,
    gint new_layer_priority, GESEditMode mode, GESEdge edge, guint64 position)
{
  g_return_val_if_fail (GES_IS_CONTAINER (container), FALSE);

  if (G_UNLIKELY (GES_CONTAINER_GET_CLASS (container)->edit == NULL)) {
    GST_WARNING_OBJECT (container, "No edit vmethod implementation");
    return FALSE;
  }

  return GES_CONTAINER_GET_CLASS (container)->edit (container, layers,
      new_layer_priority, mode, edge, position);
}