1 /* GStreamer Editing Services
2  * Copyright (C) <2013> Thibault Saunier <thibault.saunier@collabora.com>
3  *               <2013> Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 /**
22  * SECTION:gescontainer
23  * @title: GESContainer
24  * @short_description: Base Class for objects responsible for controlling other
25  * GESTimelineElement-s
26  */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 
31 #include "ges-container.h"
32 #include "ges.h"
33 #include "ges-internal.h"
34 
35 #include <string.h>
36 
37 GST_DEBUG_CATEGORY_STATIC (ges_container_debug);
38 #undef GST_CAT_DEFAULT
39 #define GST_CAT_DEFAULT ges_container_debug
40 
41 /* Mapping of relationship between a Container and the TimelineElements
42  * it controls
43  *
44  * NOTE : Does it make sense to make it public in the future ?
45  */
46 typedef struct
47 {
48   GESTimelineElement *child;
49 
50   GstClockTime start_offset;
51   GstClockTime duration_offset;
52   GstClockTime inpoint_offset;
53   gint32 priority_offset;
54 
55   guint start_notifyid;
56   guint duration_notifyid;
57   guint inpoint_notifyid;
58 } ChildMapping;
59 
60 enum
61 {
62   CHILD_ADDED_SIGNAL,
63   CHILD_REMOVED_SIGNAL,
64   LAST_SIGNAL
65 };
66 
67 static guint ges_container_signals[LAST_SIGNAL] = { 0 };
68 
69 struct _GESContainerPrivate
70 {
71   /*< public > */
72   GESLayer *layer;
73 
74   /*< private > */
75   /* Set to TRUE when the container is doing updates of track object
76    * properties so we don't end up in infinite property update loops
77    */
78   GHashTable *mappings;
79 
80   /* List of GESTimelineElement being in the "child-added" signal
81    * emission stage */
82   GList *adding_children;
83 
84   GList *copied_children;
85 };
86 
87 enum
88 {
89   PROP_0,
90   PROP_HEIGHT,
91   PROP_LAST
92 };
93 
94 static GParamSpec *properties[PROP_LAST];
95 
96 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GESContainer, ges_container,
97     GES_TYPE_TIMELINE_ELEMENT);
98 
99 /************************
100  *   Private  methods   *
101  ************************/
102 static void
_free_mapping(ChildMapping * mapping)103 _free_mapping (ChildMapping * mapping)
104 {
105   GESTimelineElement *child = mapping->child;
106 
107   /* Disconnect all notify listeners */
108   if (mapping->start_notifyid)
109     g_signal_handler_disconnect (child, mapping->start_notifyid);
110   if (mapping->duration_notifyid)
111     g_signal_handler_disconnect (child, mapping->duration_notifyid);
112   if (mapping->inpoint_notifyid)
113     g_signal_handler_disconnect (child, mapping->inpoint_notifyid);
114 
115   ges_timeline_element_set_parent (child, NULL);
116   g_slice_free (ChildMapping, mapping);
117 }
118 
119 static gint
compare_grouping_prio(GType * a,GType * b)120 compare_grouping_prio (GType * a, GType * b)
121 {
122   gint ret = 0;
123   GObjectClass *aclass = g_type_class_ref (*a);
124   GObjectClass *bclass = g_type_class_ref (*b);
125 
126   /* We want higher prios to be first */
127   if (GES_CONTAINER_CLASS (aclass)->grouping_priority <
128       GES_CONTAINER_CLASS (bclass)->grouping_priority)
129     ret = 1;
130   else if (GES_CONTAINER_CLASS (aclass)->grouping_priority >
131       GES_CONTAINER_CLASS (bclass)->grouping_priority)
132     ret = -1;
133 
134   g_type_class_unref (aclass);
135   g_type_class_unref (bclass);
136   return ret;
137 }
138 
139 static void
_resync_start_offsets(GESTimelineElement * child,ChildMapping * map,GESContainer * container)140 _resync_start_offsets (GESTimelineElement * child,
141     ChildMapping * map, GESContainer * container)
142 {
143   map->start_offset = _START (container) - _START (child);
144 }
145 
146 /*****************************************************
147  *                                                   *
148  * GESTimelineElement virtual methods implementation *
149  *                                                   *
150  *****************************************************/
151 static gboolean
_set_start(GESTimelineElement * element,GstClockTime start)152 _set_start (GESTimelineElement * element, GstClockTime start)
153 {
154   GList *tmp;
155   ChildMapping *map;
156   GESContainer *container = GES_CONTAINER (element);
157   GESContainerPrivate *priv = container->priv;
158 
159   GST_DEBUG_OBJECT (element, "Updating children offsets, (initiated_move: %"
160       GST_PTR_FORMAT ")", container->initiated_move);
161 
162   for (tmp = container->children; tmp; tmp = g_list_next (tmp)) {
163     GESTimelineElement *child = (GESTimelineElement *) tmp->data;
164 
165     map = g_hash_table_lookup (priv->mappings, child);
166     map->start_offset = start - _START (child);
167   }
168   container->children_control_mode = GES_CHILDREN_UPDATE;
169 
170   return TRUE;
171 }
172 
173 static gboolean
_set_inpoint(GESTimelineElement * element,GstClockTime inpoint)174 _set_inpoint (GESTimelineElement * element, GstClockTime inpoint)
175 {
176   GList *tmp;
177   GESContainer *container = GES_CONTAINER (element);
178 
179   for (tmp = container->children; tmp; tmp = g_list_next (tmp)) {
180     GESTimelineElement *child = (GESTimelineElement *) tmp->data;
181     ChildMapping *map = g_hash_table_lookup (container->priv->mappings, child);
182 
183     map->inpoint_offset = inpoint - _INPOINT (child);
184   }
185 
186   return TRUE;
187 }
188 
189 static gboolean
_set_duration(GESTimelineElement * element,GstClockTime duration)190 _set_duration (GESTimelineElement * element, GstClockTime duration)
191 {
192   GList *tmp;
193   GESContainer *container = GES_CONTAINER (element);
194   GESContainerPrivate *priv = container->priv;
195 
196   for (tmp = container->children; tmp; tmp = g_list_next (tmp)) {
197     GESTimelineElement *child = (GESTimelineElement *) tmp->data;
198     ChildMapping *map = g_hash_table_lookup (priv->mappings, child);
199 
200     map->duration_offset = duration - _DURATION (child);
201   }
202 
203   return TRUE;
204 }
205 
206 static void
_ges_container_add_child_properties(GESContainer * container,GESTimelineElement * child)207 _ges_container_add_child_properties (GESContainer * container,
208     GESTimelineElement * child)
209 {
210   guint n_props, i;
211 
212   GParamSpec **child_props =
213       ges_timeline_element_list_children_properties (child,
214       &n_props);
215 
216   for (i = 0; i < n_props; i++) {
217     GObject *prop_child;
218     gchar *prop_name = g_strdup_printf ("%s::%s",
219         g_type_name (child_props[i]->owner_type),
220         child_props[i]->name);
221 
222     if (ges_timeline_element_lookup_child (child, prop_name, &prop_child, NULL)) {
223       ges_timeline_element_add_child_property (GES_TIMELINE_ELEMENT (container),
224           child_props[i], prop_child);
225       gst_object_unref (prop_child);
226 
227     }
228     g_free (prop_name);
229     g_param_spec_unref (child_props[i]);
230   }
231 
232   g_free (child_props);
233 }
234 
235 static void
_ges_container_remove_child_properties(GESContainer * container,GESTimelineElement * child)236 _ges_container_remove_child_properties (GESContainer * container,
237     GESTimelineElement * child)
238 {
239   guint n_props, i;
240 
241   GParamSpec **child_props =
242       ges_timeline_element_list_children_properties (child,
243       &n_props);
244 
245   for (i = 0; i < n_props; i++) {
246     GObject *prop_child;
247     gchar *prop_name = g_strdup_printf ("%s::%s",
248         g_type_name (child_props[i]->owner_type),
249         child_props[i]->name);
250 
251     if (ges_timeline_element_lookup_child (child, prop_name, &prop_child, NULL)) {
252       ges_timeline_element_remove_child_property (GES_TIMELINE_ELEMENT
253           (container), child_props[i]);
254       gst_object_unref (prop_child);
255 
256     }
257 
258     g_free (prop_name);
259     g_param_spec_unref (child_props[i]);
260   }
261 
262   g_free (child_props);
263 }
264 
265 static GParamSpec **
_list_children_properties(GESTimelineElement * self,guint * n_properties)266 _list_children_properties (GESTimelineElement * self, guint * n_properties)
267 {
268   GList *tmp;
269 
270   for (tmp = GES_CONTAINER_CHILDREN (self); tmp; tmp = tmp->next)
271     _ges_container_add_child_properties (GES_CONTAINER (self), tmp->data);
272 
273   return
274       GES_TIMELINE_ELEMENT_CLASS
275       (ges_container_parent_class)->list_children_properties (self,
276       n_properties);
277 }
278 
279 static gboolean
_lookup_child(GESTimelineElement * self,const gchar * prop_name,GObject ** child,GParamSpec ** pspec)280 _lookup_child (GESTimelineElement * self, const gchar * prop_name,
281     GObject ** child, GParamSpec ** pspec)
282 {
283   GList *tmp;
284 
285   /* FIXME Implement a syntax to precisely get properties by path */
286   for (tmp = GES_CONTAINER_CHILDREN (self); tmp; tmp = tmp->next) {
287     if (ges_timeline_element_lookup_child (tmp->data, prop_name, child, pspec))
288       return TRUE;
289   }
290 
291   return FALSE;
292 }
293 
294 static GESTrackType
_get_track_types(GESTimelineElement * object)295 _get_track_types (GESTimelineElement * object)
296 {
297   GESTrackType types = GES_TRACK_TYPE_UNKNOWN;
298   GList *tmp, *children = ges_container_get_children (GES_CONTAINER (object),
299       TRUE);
300 
301   for (tmp = children; tmp; tmp = tmp->next) {
302     if (GES_IS_TRACK_ELEMENT (tmp->data)) {
303       types |= ges_timeline_element_get_track_types (tmp->data);
304     }
305   }
306 
307   g_list_free_full (children, gst_object_unref);
308 
309   return types ^ GES_TRACK_TYPE_UNKNOWN;
310 }
311 
312 static void
_deep_copy(GESTimelineElement * element,GESTimelineElement * copy)313 _deep_copy (GESTimelineElement * element, GESTimelineElement * copy)
314 {
315   GList *tmp;
316   GESContainer *self = GES_CONTAINER (element), *ccopy = GES_CONTAINER (copy);
317 
318   for (tmp = GES_CONTAINER_CHILDREN (element); tmp; tmp = tmp->next) {
319     ChildMapping *map;
320 
321     map =
322         g_slice_dup (ChildMapping, g_hash_table_lookup (self->priv->mappings,
323             tmp->data));
324     map->child = ges_timeline_element_copy (tmp->data, TRUE);
325     map->start_notifyid = 0;
326     map->inpoint_notifyid = 0;
327     map->duration_notifyid = 0;
328 
329     ccopy->priv->copied_children = g_list_prepend (ccopy->priv->copied_children,
330         map);
331   }
332 }
333 
334 static GESTimelineElement *
_paste(GESTimelineElement * element,GESTimelineElement * ref,GstClockTime paste_position)335 _paste (GESTimelineElement * element, GESTimelineElement * ref,
336     GstClockTime paste_position)
337 {
338   GList *tmp;
339   ChildMapping *map;
340   GESContainer *ncontainer =
341       GES_CONTAINER (ges_timeline_element_copy (element, FALSE));
342   GESContainer *self = GES_CONTAINER (element);
343 
344   for (tmp = self->priv->copied_children; tmp; tmp = tmp->next) {
345     GESTimelineElement *nchild;
346 
347     map = tmp->data;
348     nchild =
349         ges_timeline_element_paste (map->child,
350         paste_position - map->start_offset);
351 
352     if (!nchild) {
353       while (ncontainer->children)
354         ges_container_remove (ncontainer, ncontainer->children->data);
355 
356       g_object_unref (ncontainer);
357       return NULL;
358     }
359 
360     ges_timeline_element_set_timeline (GES_TIMELINE_ELEMENT (ncontainer),
361         GES_TIMELINE_ELEMENT_TIMELINE (ref));
362     ges_container_add (ncontainer, nchild);
363   }
364 
365   return GES_TIMELINE_ELEMENT (ncontainer);
366 }
367 
368 
369 /******************************************
370  *                                        *
371  * GObject virtual methods implementation *
372  *                                        *
373  ******************************************/
374 static void
_dispose(GObject * object)375 _dispose (GObject * object)
376 {
377   GList *tmp;
378   GESContainer *self = GES_CONTAINER (object);
379   GList *children;
380 
381   _ges_container_sort_children (self);
382   children = ges_container_get_children (self, FALSE);
383 
384   for (tmp = g_list_last (children); tmp; tmp = tmp->prev)
385     ges_container_remove (self, tmp->data);
386 
387   g_list_free_full (children, gst_object_unref);
388   self->children = NULL;
389 
390   G_OBJECT_CLASS (ges_container_parent_class)->dispose (object);
391 }
392 
393 static void
_finalize(GObject * object)394 _finalize (GObject * object)
395 {
396   GESContainer *self = GES_CONTAINER (object);
397 
398   g_list_free_full (self->priv->copied_children,
399       (GDestroyNotify) _free_mapping);
400 
401   if (self->priv->mappings)
402     g_hash_table_destroy (self->priv->mappings);
403 
404   G_OBJECT_CLASS (ges_container_parent_class)->finalize (object);
405 }
406 
407 static void
_get_property(GObject * container,guint property_id,GValue * value,GParamSpec * pspec)408 _get_property (GObject * container, guint property_id,
409     GValue * value, GParamSpec * pspec)
410 {
411   GESContainer *tobj = GES_CONTAINER (container);
412 
413   switch (property_id) {
414     case PROP_HEIGHT:
415       g_value_set_uint (value, tobj->height);
416       break;
417     default:
418       G_OBJECT_WARN_INVALID_PROPERTY_ID (container, property_id, pspec);
419   }
420 }
421 
422 static void
_set_property(GObject * container,guint property_id,const GValue * value,GParamSpec * pspec)423 _set_property (GObject * container, guint property_id,
424     const GValue * value, GParamSpec * pspec)
425 {
426   switch (property_id) {
427     default:
428       G_OBJECT_WARN_INVALID_PROPERTY_ID (container, property_id, pspec);
429   }
430 }
431 
432 static void
ges_container_class_init(GESContainerClass * klass)433 ges_container_class_init (GESContainerClass * klass)
434 {
435   GObjectClass *object_class = G_OBJECT_CLASS (klass);
436   GESTimelineElementClass *element_class = GES_TIMELINE_ELEMENT_CLASS (klass);
437 
438   GST_DEBUG_CATEGORY_INIT (ges_container_debug, "gescontainer",
439       GST_DEBUG_FG_YELLOW, "ges container");
440 
441   object_class->get_property = _get_property;
442   object_class->set_property = _set_property;
443   object_class->dispose = _dispose;
444   object_class->finalize = _finalize;
445 
446   /**
447    * GESContainer:height:
448    *
449    * The span of priorities which this container occupies.
450    */
451   properties[PROP_HEIGHT] = g_param_spec_uint ("height", "Height",
452       "The span of priorities this container occupies", 0, G_MAXUINT, 1,
453       G_PARAM_READABLE);
454   g_object_class_install_property (object_class, PROP_HEIGHT,
455       properties[PROP_HEIGHT]);
456 
457   /**
458    * GESContainer::child-added:
459    * @container: the #GESContainer
460    * @element: the #GESTimelineElement that was added.
461    *
462    * Will be emitted after a child was added to @container.
463    * Usually you should connect with #g_signal_connect_after
464    * as in the first emission stage, the signal emission might
465    * get stopped internally.
466    */
467   ges_container_signals[CHILD_ADDED_SIGNAL] =
468       g_signal_new ("child-added", G_TYPE_FROM_CLASS (klass),
469       G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESContainerClass, child_added),
470       NULL, NULL, g_cclosure_marshal_generic,
471       G_TYPE_NONE, 1, GES_TYPE_TIMELINE_ELEMENT);
472 
473   /**
474    * GESContainer::child-removed:
475    * @container: the #GESContainer
476    * @element: the #GESTimelineElement that was removed.
477    *
478    * Will be emitted after a child was removed from @container.
479    */
480   ges_container_signals[CHILD_REMOVED_SIGNAL] =
481       g_signal_new ("child-removed", G_TYPE_FROM_CLASS (klass),
482       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESContainerClass, child_removed),
483       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
484       GES_TYPE_TIMELINE_ELEMENT);
485 
486 
487   element_class->set_start = _set_start;
488   element_class->set_duration = _set_duration;
489   element_class->set_inpoint = _set_inpoint;
490   element_class->list_children_properties = _list_children_properties;
491   element_class->lookup_child = _lookup_child;
492   element_class->get_track_types = _get_track_types;
493   element_class->paste = _paste;
494   element_class->deep_copy = _deep_copy;
495 
496   /* No default implementations */
497   klass->remove_child = NULL;
498   klass->add_child = NULL;
499   klass->ungroup = NULL;
500   klass->group = NULL;
501   klass->grouping_priority = 0;
502   klass->edit = NULL;
503 }
504 
505 static void
ges_container_init(GESContainer * self)506 ges_container_init (GESContainer * self)
507 {
508   self->priv = ges_container_get_instance_private (self);
509 
510   /* FIXME, check why default was GST_SECOND? (before the existend of
511    * ges-container)
512    *
513    * _DURATION (self) = GST_SECOND; */
514   self->height = 1;             /* FIXME Why 1 and not 0? */
515   self->children = NULL;
516 
517   self->priv->mappings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
518       NULL, (GDestroyNotify) _free_mapping);
519 }
520 
521 /**********************************************
522  *                                            *
523  *    Property notifications from Children    *
524  *                                            *
525  **********************************************/
526 static void
_child_start_changed_cb(GESTimelineElement * child,GParamSpec * arg G_GNUC_UNUSED,GESContainer * container)527 _child_start_changed_cb (GESTimelineElement * child,
528     GParamSpec * arg G_GNUC_UNUSED, GESContainer * container)
529 {
530   ChildMapping *map;
531   GstClockTime start;
532 
533   GESContainerPrivate *priv = container->priv;
534   GESTimelineElement *element = GES_TIMELINE_ELEMENT (container);
535   GESChildrenControlMode pmode = container->children_control_mode;
536 
537   map = g_hash_table_lookup (priv->mappings, child);
538   g_assert (map);
539 
540   if (ELEMENT_FLAG_IS_SET (child, GES_TIMELINE_ELEMENT_SET_SIMPLE))
541     container->children_control_mode = GES_CHILDREN_UPDATE_ALL_VALUES;
542 
543   switch (container->children_control_mode) {
544     case GES_CHILDREN_IGNORE_NOTIFIES:
545       return;
546     case GES_CHILDREN_UPDATE_ALL_VALUES:
547       _ges_container_sort_children (container);
548       start = container->children ?
549           _START (container->children->data) : _START (container);
550 
551       if (start != _START (container)) {
552         _DURATION (container) = _END (container) - start;
553         _START (container) = start;
554 
555         GST_DEBUG_OBJECT (container, "Child move made us move %" GES_FORMAT,
556             GES_ARGS (container));
557 
558         g_object_notify (G_OBJECT (container), "start");
559       }
560 
561       /* Falltrough! */
562     case GES_CHILDREN_UPDATE_OFFSETS:
563       map->start_offset = _START (container) - _START (child);
564       break;
565 
566     case GES_CHILDREN_UPDATE:
567       /* We update all the children calling our set_start method */
568       container->initiated_move = child;
569       _set_start0 (element, _START (child) + map->start_offset);
570       container->initiated_move = NULL;
571       break;
572     default:
573       break;
574   }
575 
576   if (ELEMENT_FLAG_IS_SET (child, GES_TIMELINE_ELEMENT_SET_SIMPLE))
577     container->children_control_mode = pmode;
578 }
579 
580 static void
_child_inpoint_changed_cb(GESTimelineElement * child,GParamSpec * arg G_GNUC_UNUSED,GESContainer * container)581 _child_inpoint_changed_cb (GESTimelineElement * child,
582     GParamSpec * arg G_GNUC_UNUSED, GESContainer * container)
583 {
584   ChildMapping *map;
585 
586   GESContainerPrivate *priv = container->priv;
587   GESTimelineElement *element = GES_TIMELINE_ELEMENT (container);
588 
589   if (container->children_control_mode == GES_CHILDREN_IGNORE_NOTIFIES)
590     return;
591 
592   map = g_hash_table_lookup (priv->mappings, child);
593   g_assert (map);
594 
595   if (container->children_control_mode == GES_CHILDREN_UPDATE_OFFSETS
596       || ELEMENT_FLAG_IS_SET (child, GES_TIMELINE_ELEMENT_SET_SIMPLE)) {
597     map->inpoint_offset = _START (container) - _START (child);
598 
599     return;
600   }
601 
602   /* We update all the children calling our set_inpoint method */
603   container->initiated_move = child;
604   _set_inpoint0 (element, _INPOINT (child) + map->inpoint_offset);
605   container->initiated_move = NULL;
606 }
607 
608 static void
_child_duration_changed_cb(GESTimelineElement * child,GParamSpec * arg G_GNUC_UNUSED,GESContainer * container)609 _child_duration_changed_cb (GESTimelineElement * child,
610     GParamSpec * arg G_GNUC_UNUSED, GESContainer * container)
611 {
612   ChildMapping *map;
613 
614   GList *tmp;
615   GstClockTime end = 0;
616   GESContainerPrivate *priv = container->priv;
617   GESTimelineElement *element = GES_TIMELINE_ELEMENT (container);
618   GESChildrenControlMode pmode = container->children_control_mode;
619 
620   if (container->children_control_mode == GES_CHILDREN_IGNORE_NOTIFIES)
621     return;
622 
623   if (ELEMENT_FLAG_IS_SET (child, GES_TIMELINE_ELEMENT_SET_SIMPLE))
624     container->children_control_mode = GES_CHILDREN_UPDATE_ALL_VALUES;
625 
626   map = g_hash_table_lookup (priv->mappings, child);
627   g_assert (map);
628 
629   switch (container->children_control_mode) {
630     case GES_CHILDREN_IGNORE_NOTIFIES:
631       break;
632     case GES_CHILDREN_UPDATE_ALL_VALUES:
633       _ges_container_sort_children_by_end (container);
634 
635       for (tmp = container->children; tmp; tmp = tmp->next)
636         end = MAX (end, _END (tmp->data));
637 
638       if (end != _END (container)) {
639         _DURATION (container) = end - _START (container);
640         g_object_notify (G_OBJECT (container), "duration");
641       }
642       /* Falltrough */
643     case GES_CHILDREN_UPDATE_OFFSETS:
644       map->inpoint_offset = _START (container) - _START (child);
645       break;
646     case GES_CHILDREN_UPDATE:
647       /* We update all the children calling our set_duration method */
648       container->initiated_move = child;
649       _set_duration0 (element, _DURATION (child) + map->duration_offset);
650       container->initiated_move = NULL;
651       break;
652     default:
653       break;
654   }
655 
656   if (ELEMENT_FLAG_IS_SET (child, GES_TIMELINE_ELEMENT_SET_SIMPLE))
657     container->children_control_mode = pmode;
658 }
659 
660 /****************************************************
661  *                                                  *
662  *         Internal methods implementation          *
663  *                                                  *
664  ****************************************************/
665 
666 void
_ges_container_sort_children(GESContainer * container)667 _ges_container_sort_children (GESContainer * container)
668 {
669   container->children = g_list_sort (container->children,
670       (GCompareFunc) element_start_compare);
671 }
672 
673 void
_ges_container_sort_children_by_end(GESContainer * container)674 _ges_container_sort_children_by_end (GESContainer * container)
675 {
676   container->children = g_list_sort (container->children,
677       (GCompareFunc) element_end_compare);
678 }
679 
680 void
_ges_container_set_height(GESContainer * container,guint32 height)681 _ges_container_set_height (GESContainer * container, guint32 height)
682 {
683   if (container->height != height) {
684     container->height = height;
685     GST_DEBUG_OBJECT (container, "Updating height %i", container->height);
686     g_object_notify (G_OBJECT (container), "height");
687   }
688 }
689 
690 gint
_ges_container_get_priority_offset(GESContainer * container,GESTimelineElement * elem)691 _ges_container_get_priority_offset (GESContainer * container,
692     GESTimelineElement * elem)
693 {
694   ChildMapping *map = g_hash_table_lookup (container->priv->mappings, elem);
695 
696   g_return_val_if_fail (map, 0);
697 
698   return map->priority_offset;
699 }
700 
701 void
_ges_container_set_priority_offset(GESContainer * container,GESTimelineElement * elem,gint32 priority_offset)702 _ges_container_set_priority_offset (GESContainer * container,
703     GESTimelineElement * elem, gint32 priority_offset)
704 {
705   ChildMapping *map = g_hash_table_lookup (container->priv->mappings, elem);
706 
707   g_return_if_fail (map);
708 
709   map->priority_offset = priority_offset;
710 }
711 
712 /**********************************************
713  *                                            *
714  *            API implementation              *
715  *                                            *
716  **********************************************/
717 
718 /**
719  * ges_container_add:
720  * @container: a #GESContainer
721  * @child: the #GESTimelineElement
722  *
723  * Add the #GESTimelineElement to the container.
724  *
725  * Returns: %TRUE on success, %FALSE on failure.
726  */
727 gboolean
ges_container_add(GESContainer * container,GESTimelineElement * child)728 ges_container_add (GESContainer * container, GESTimelineElement * child)
729 {
730   ChildMapping *mapping;
731   gboolean notify_start = FALSE;
732   GESContainerClass *class;
733   GESContainerPrivate *priv;
734 
735   g_return_val_if_fail (GES_IS_CONTAINER (container), FALSE);
736   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (child), FALSE);
737   g_return_val_if_fail (GES_TIMELINE_ELEMENT_PARENT (child) == NULL, FALSE);
738 
739   class = GES_CONTAINER_GET_CLASS (container);
740   priv = container->priv;
741 
742   GST_DEBUG_OBJECT (container, "adding timeline element %" GST_PTR_FORMAT,
743       child);
744 
745   container->children_control_mode = GES_CHILDREN_IGNORE_NOTIFIES;
746   if (class->add_child) {
747     if (class->add_child (container, child) == FALSE) {
748       container->children_control_mode = GES_CHILDREN_UPDATE;
749       GST_WARNING_OBJECT (container, "Erreur adding child %p", child);
750       return FALSE;
751     }
752   }
753   container->children_control_mode = GES_CHILDREN_UPDATE;
754 
755   if (_START (container) > _START (child)) {
756     _START (container) = _START (child);
757 
758     g_hash_table_foreach (priv->mappings, (GHFunc) _resync_start_offsets,
759         container);
760     notify_start = TRUE;
761   }
762 
763   mapping = g_slice_new0 (ChildMapping);
764   mapping->child = gst_object_ref (child);
765   mapping->start_offset = _START (container) - _START (child);
766   mapping->duration_offset = _DURATION (container) - _DURATION (child);
767   mapping->inpoint_offset = _INPOINT (container) - _INPOINT (child);
768 
769   g_hash_table_insert (priv->mappings, child, mapping);
770 
771   container->children = g_list_prepend (container->children, child);
772 
773   _ges_container_sort_children (container);
774 
775   /* Listen to all property changes */
776   mapping->start_notifyid =
777       g_signal_connect (G_OBJECT (child), "notify::start",
778       G_CALLBACK (_child_start_changed_cb), container);
779   mapping->duration_notifyid =
780       g_signal_connect (G_OBJECT (child), "notify::duration",
781       G_CALLBACK (_child_duration_changed_cb), container);
782   mapping->inpoint_notifyid =
783       g_signal_connect (G_OBJECT (child), "notify::in-point",
784       G_CALLBACK (_child_inpoint_changed_cb), container);
785 
786   if (ges_timeline_element_set_parent (child, GES_TIMELINE_ELEMENT (container))
787       == FALSE) {
788     if (class->remove_child)
789       class->remove_child (container, child);
790 
791     g_hash_table_remove (priv->mappings, child);
792     container->children = g_list_remove (container->children, child);
793     _ges_container_sort_children (container);
794 
795     return FALSE;
796   }
797 
798   _ges_container_add_child_properties (container, child);
799 
800   priv->adding_children = g_list_prepend (priv->adding_children, child);
801   g_signal_emit (container, ges_container_signals[CHILD_ADDED_SIGNAL], 0,
802       child);
803   priv->adding_children = g_list_remove (priv->adding_children, child);
804 
805   if (notify_start)
806     g_object_notify (G_OBJECT (container), "start");
807 
808   return TRUE;
809 }
810 
811 /**
812  * ges_container_remove:
813  * @container: a #GESContainer
814  * @child: the #GESTimelineElement to release
815  *
816  * Release the @child from the control of @container.
817  *
818  * Returns: %TRUE if the @child was properly released, else %FALSE.
819  */
820 gboolean
ges_container_remove(GESContainer * container,GESTimelineElement * child)821 ges_container_remove (GESContainer * container, GESTimelineElement * child)
822 {
823   GESContainerClass *klass;
824   GESContainerPrivate *priv;
825 
826   g_return_val_if_fail (GES_IS_CONTAINER (container), FALSE);
827   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (child), FALSE);
828 
829   GST_DEBUG_OBJECT (container, "removing child: %" GST_PTR_FORMAT, child);
830 
831   klass = GES_CONTAINER_GET_CLASS (container);
832   priv = container->priv;
833 
834   if (!(g_hash_table_lookup (priv->mappings, child))) {
835     GST_WARNING_OBJECT (container, "Element isn't controlled by this "
836         "container");
837     return FALSE;
838   }
839 
840   if (klass->remove_child) {
841     if (klass->remove_child (container, child) == FALSE)
842       return FALSE;
843   }
844 
845   container->children = g_list_remove (container->children, child);
846   /* Let it live removing from our mappings */
847   g_hash_table_remove (priv->mappings, child);
848 
849   _ges_container_remove_child_properties (container, child);
850 
851   if (!g_list_find (container->priv->adding_children, child)) {
852     g_signal_emit (container, ges_container_signals[CHILD_REMOVED_SIGNAL], 0,
853         child);
854   } else {
855     GST_INFO_OBJECT (container, "Not emitting 'child-removed' signal as child"
856         " removal happend during 'child-added' signal emission");
857   }
858   gst_object_unref (child);
859 
860   return TRUE;
861 }
862 
863 static void
_get_children_recursively(GESContainer * container,GList ** children)864 _get_children_recursively (GESContainer * container, GList ** children)
865 {
866   GList *tmp;
867 
868   *children =
869       g_list_concat (*children, g_list_copy_deep (container->children,
870           (GCopyFunc) gst_object_ref, NULL));
871 
872   for (tmp = container->children; tmp; tmp = tmp->next) {
873     GESTimelineElement *element = tmp->data;
874 
875     if (GES_IS_CONTAINER (element))
876       _get_children_recursively (tmp->data, children);
877   }
878 }
879 
880 /**
881  * ges_container_get_children:
882  * @container: a #GESContainer
883  * @recursive:  Whether to recursively get children in @container
884  *
885  * Get the list of #GESTimelineElement contained in @container
886  * The user is responsible for unreffing the contained objects
887  * and freeing the list.
888  *
889  * Returns: (transfer full) (element-type GESTimelineElement): The list of
890  * timeline element contained in @container.
891  */
892 GList *
ges_container_get_children(GESContainer * container,gboolean recursive)893 ges_container_get_children (GESContainer * container, gboolean recursive)
894 {
895   GList *children = NULL;
896 
897   g_return_val_if_fail (GES_IS_CONTAINER (container), NULL);
898 
899   if (!recursive)
900     return g_list_copy_deep (container->children, (GCopyFunc) gst_object_ref,
901         NULL);
902 
903   _get_children_recursively (container, &children);
904   return children;
905 }
906 
907 /**
908  * ges_container_ungroup:
909  * @container: (transfer full): The #GESContainer to ungroup
910  * @recursive: Wether to recursively ungroup @container
911  *
912  * Ungroups the #GESTimelineElement contained in this GESContainer,
913  * creating new #GESContainer containing those #GESTimelineElement
914  * apropriately.
915  *
916  * Returns: (transfer full) (element-type GESContainer): The list of
917  * #GESContainer resulting from the ungrouping operation
918  * The user is responsible for unreffing the contained objects
919  * and freeing the list.
920  */
921 GList *
ges_container_ungroup(GESContainer * container,gboolean recursive)922 ges_container_ungroup (GESContainer * container, gboolean recursive)
923 {
924   GESContainerClass *klass;
925 
926   g_return_val_if_fail (GES_IS_CONTAINER (container), NULL);
927 
928   GST_DEBUG_OBJECT (container, "Ungrouping container %s recursively",
929       recursive ? "" : "not");
930 
931   klass = GES_CONTAINER_GET_CLASS (container);
932   if (klass->ungroup == NULL) {
933     GST_INFO_OBJECT (container, "No ungoup virtual method, doint nothing");
934     return NULL;
935   }
936 
937   return klass->ungroup (container, recursive);
938 }
939 
940 /**
941  * ges_container_group:
942  * @containers: (transfer none)(element-type GESContainer) (allow-none): The
943  * #GESContainer to group, they must all be in a same #GESTimeline
944  *
945  * Groups the #GESContainer-s provided in @containers. It creates a subclass
946  * of #GESContainer, depending on the containers provided in @containers.
947  * Basically, if all the containers in @containers should be contained in a same
948  * clip (all the #GESTrackElement they contain have the exact same
949  * start/inpoint/duration and are in the same layer), it will create a #GESClip
950  * otherwise a #GESGroup will be created
951  *
952  * Returns: (transfer none): The #GESContainer (subclass) resulting of the
953  * grouping
954  */
955 GESContainer *
ges_container_group(GList * containers)956 ges_container_group (GList * containers)
957 {
958   GList *tmp;
959   guint n_children;
960   GType *children_types;
961   GESTimelineElement *element;
962   GObjectClass *clip_class;
963 
964   guint i = 0;
965   GESContainer *ret = NULL;
966   GESTimeline *timeline = NULL;
967 
968   if (containers) {
969     element = GES_TIMELINE_ELEMENT (containers->data);
970     timeline = GES_TIMELINE_ELEMENT_TIMELINE (element);
971     g_return_val_if_fail (timeline, NULL);
972   }
973 
974   if (g_list_length (containers) == 1)
975     return containers->data;
976 
977   for (tmp = containers; tmp; tmp = tmp->next) {
978     g_return_val_if_fail (GES_IS_CONTAINER (tmp->data), NULL);
979     g_return_val_if_fail (GES_TIMELINE_ELEMENT_PARENT (tmp->data) == NULL,
980         NULL);
981     g_return_val_if_fail (GES_TIMELINE_ELEMENT_TIMELINE (tmp->data) == timeline,
982         NULL);
983   }
984 
985   children_types = g_type_children (GES_TYPE_CONTAINER, &n_children);
986   g_qsort_with_data (children_types, n_children, sizeof (GType),
987       (GCompareDataFunc) compare_grouping_prio, NULL);
988 
989   for (i = 0; i < n_children; i++) {
990     clip_class = g_type_class_peek (children_types[i]);
991     ret = GES_CONTAINER_CLASS (clip_class)->group (containers);
992 
993     if (ret)
994       break;
995   }
996 
997   g_free (children_types);
998   return ret;
999 }
1000 
1001 /**
1002  * ges_container_edit:
1003  * @container: the #GESClip to edit
1004  * @layers: (element-type GESLayer): The layers you want the edit to
1005  *  happen in, %NULL means that the edition is done in all the
1006  *  #GESLayers contained in the current timeline.
1007  * @new_layer_priority: The priority of the layer @container should land in.
1008  *  If the layer you're trying to move the container to doesn't exist, it will
1009  *  be created automatically. -1 means no move.
1010  * @mode: The #GESEditMode in which the editition will happen.
1011  * @edge: The #GESEdge the edit should happen on.
1012  * @position: The position at which to edit @container (in nanosecond)
1013  *
1014  * Edit @container in the different exisiting #GESEditMode modes. In the case of
1015  * slide, and roll, you need to specify a #GESEdge
1016  *
1017  * Returns: %TRUE if the container as been edited properly, %FALSE if an error
1018  * occured
1019  */
1020 gboolean
ges_container_edit(GESContainer * container,GList * layers,gint new_layer_priority,GESEditMode mode,GESEdge edge,guint64 position)1021 ges_container_edit (GESContainer * container, GList * layers,
1022     gint new_layer_priority, GESEditMode mode, GESEdge edge, guint64 position)
1023 {
1024   g_return_val_if_fail (GES_IS_CONTAINER (container), FALSE);
1025 
1026   if (G_UNLIKELY (GES_CONTAINER_GET_CLASS (container)->edit == NULL)) {
1027     GST_WARNING_OBJECT (container, "No edit vmethod implementation");
1028     return FALSE;
1029   }
1030 
1031   return GES_CONTAINER_GET_CLASS (container)->edit (container, layers,
1032       new_layer_priority, mode, edge, position);
1033 }
1034