040a7f90b6973b5084b36df5b6df38fda90b9edf
[platform/upstream/gst-editing-services.git] / ges / ges-timeline-element.c
1 /* gst-editing-services
2  * Copyright (C) <2013> Thibault Saunier <thibault.saunier@collabora.com>
3  *               <2013> Collabora Ltd.
4  *
5  * gst-editing-services is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * gst-editing-services is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 /**
20  * SECTION:gestimelineelement
21  * @title: GESTimelineElement
22  * @short_description: Base Class for all elements that will be in a way or
23  * another inside a GESTimeline.
24  *
25  * The GESTimelineElement base class implements the notion of timing as well
26  * as priority. A GESTimelineElement can have a parent object which will be
27  * responsible for controlling its timing properties.
28  */
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include "ges-utils.h"
34 #include "ges-timeline-element.h"
35 #include "ges-extractable.h"
36 #include "ges-meta-container.h"
37 #include "ges-internal.h"
38 #include "ges-effect.h"
39
40 #include <string.h>
41 #include <gobject/gvaluecollector.h>
42
43 /* maps type name quark => count */
44 static GData *object_name_counts = NULL;
45
46 static void
47 extractable_set_asset (GESExtractable * extractable, GESAsset * asset)
48 {
49   GES_TIMELINE_ELEMENT (extractable)->asset = asset;
50 }
51
52 static void
53 ges_extractable_interface_init (GESExtractableInterface * iface)
54 {
55   iface->set_asset = extractable_set_asset;
56 }
57
58 enum
59 {
60   PROP_0,
61   PROP_PARENT,
62   PROP_TIMELINE,
63   PROP_START,
64   PROP_INPOINT,
65   PROP_DURATION,
66   PROP_MAX_DURATION,
67   PROP_PRIORITY,
68   PROP_NAME,
69   PROP_SERIALIZE,
70   PROP_LAST
71 };
72
73 enum
74 {
75   DEEP_NOTIFY,
76   LAST_SIGNAL
77 };
78
79 static guint ges_timeline_element_signals[LAST_SIGNAL] = { 0 };
80
81 static GParamSpec *properties[PROP_LAST] = { NULL, };
82
83 typedef struct
84 {
85   GObject *child;
86   gulong handler_id;
87 } ChildPropHandler;
88
89 struct _GESTimelineElementPrivate
90 {
91   gboolean serialize;
92
93   /* We keep a link between properties name and elements internally
94    * The hashtable should look like
95    * {GParamaSpec ---> child}*/
96   GHashTable *children_props;
97
98   GESTimelineElement *copied_from;
99
100   GESTimelineElementFlags flags;
101 };
102
103 typedef struct
104 {
105   GObject *child;
106   GParamSpec *arg;
107   GESTimelineElement *self;
108 } EmitDeepNotifyInIdleData;
109
110 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GESTimelineElement, ges_timeline_element,
111     G_TYPE_INITIALLY_UNOWNED, G_ADD_PRIVATE (GESTimelineElement)
112     G_IMPLEMENT_INTERFACE (GES_TYPE_EXTRACTABLE, ges_extractable_interface_init)
113     G_IMPLEMENT_INTERFACE (GES_TYPE_META_CONTAINER, NULL));
114
115 static void
116 _set_child_property (GESTimelineElement * self G_GNUC_UNUSED, GObject * child,
117     GParamSpec * pspec, GValue * value)
118 {
119   g_object_set_property (child, pspec->name, value);
120 }
121
122 static gboolean
123 _lookup_child (GESTimelineElement * self, const gchar * prop_name,
124     GObject ** child, GParamSpec ** pspec)
125 {
126   GHashTableIter iter;
127   gpointer key, value;
128   gchar **names, *name, *classename;
129   gboolean res;
130
131   classename = NULL;
132   res = FALSE;
133
134   names = g_strsplit (prop_name, "::", 2);
135   if (names[1] != NULL) {
136     classename = names[0];
137     name = names[1];
138   } else
139     name = names[0];
140
141   g_hash_table_iter_init (&iter, self->priv->children_props);
142   while (g_hash_table_iter_next (&iter, &key, &value)) {
143     if (g_strcmp0 (G_PARAM_SPEC (key)->name, name) == 0) {
144       ChildPropHandler *handler = (ChildPropHandler *) value;
145       if (classename == NULL ||
146           g_strcmp0 (G_OBJECT_TYPE_NAME (G_OBJECT (handler->child)),
147               classename) == 0 ||
148           g_strcmp0 (g_type_name (G_PARAM_SPEC (key)->owner_type),
149               classename) == 0) {
150         GST_DEBUG_OBJECT (self, "The %s property from %s has been found", name,
151             classename);
152         if (child)
153           *child = gst_object_ref (handler->child);
154
155         if (pspec)
156           *pspec = g_param_spec_ref (key);
157         res = TRUE;
158         break;
159       }
160     }
161   }
162   g_strfreev (names);
163
164   return res;
165 }
166
167 static GParamSpec **
168 default_list_children_properties (GESTimelineElement * self,
169     guint * n_properties)
170 {
171   GParamSpec **pspec, *spec;
172   GHashTableIter iter;
173   gpointer key, value;
174
175   guint i = 0;
176
177   *n_properties = g_hash_table_size (self->priv->children_props);
178   pspec = g_new (GParamSpec *, *n_properties);
179
180   g_hash_table_iter_init (&iter, self->priv->children_props);
181   while (g_hash_table_iter_next (&iter, &key, &value)) {
182     spec = G_PARAM_SPEC (key);
183     pspec[i] = g_param_spec_ref (spec);
184     i++;
185   }
186
187   return pspec;
188 }
189
190 static void
191 _get_property (GObject * object, guint property_id,
192     GValue * value, GParamSpec * pspec)
193 {
194   GESTimelineElement *self = GES_TIMELINE_ELEMENT (object);
195
196   switch (property_id) {
197     case PROP_PARENT:
198       g_value_take_object (value, self->parent);
199       break;
200     case PROP_TIMELINE:
201       g_value_take_object (value, self->timeline);
202       break;
203     case PROP_START:
204       g_value_set_uint64 (value, self->start);
205       break;
206     case PROP_INPOINT:
207       g_value_set_uint64 (value, self->inpoint);
208       break;
209     case PROP_DURATION:
210       g_value_set_uint64 (value, self->duration);
211       break;
212     case PROP_MAX_DURATION:
213       g_value_set_uint64 (value, self->maxduration);
214       break;
215     case PROP_PRIORITY:
216       g_value_set_uint (value, self->priority);
217       break;
218     case PROP_NAME:
219       g_value_take_string (value, ges_timeline_element_get_name (self));
220       break;
221     case PROP_SERIALIZE:
222       g_value_set_boolean (value, self->priv->serialize);
223       break;
224     default:
225       G_OBJECT_WARN_INVALID_PROPERTY_ID (self, property_id, pspec);
226   }
227 }
228
229 static void
230 _set_property (GObject * object, guint property_id,
231     const GValue * value, GParamSpec * pspec)
232 {
233   GESTimelineElement *self = GES_TIMELINE_ELEMENT (object);
234
235   switch (property_id) {
236     case PROP_PARENT:
237       ges_timeline_element_set_parent (self, g_value_get_object (value));
238       break;
239     case PROP_TIMELINE:
240       ges_timeline_element_set_timeline (self, g_value_get_object (value));
241       break;
242     case PROP_START:
243       ges_timeline_element_set_start (self, g_value_get_uint64 (value));
244       break;
245     case PROP_INPOINT:
246       ges_timeline_element_set_inpoint (self, g_value_get_uint64 (value));
247       break;
248     case PROP_DURATION:
249       ges_timeline_element_set_duration (self, g_value_get_uint64 (value));
250       break;
251     case PROP_PRIORITY:
252       ges_timeline_element_set_priority (self, g_value_get_uint (value));
253       break;
254     case PROP_MAX_DURATION:
255       ges_timeline_element_set_max_duration (self, g_value_get_uint64 (value));
256       break;
257     case PROP_NAME:
258       ges_timeline_element_set_name (self, g_value_get_string (value));
259       break;
260     case PROP_SERIALIZE:
261       self->priv->serialize = g_value_get_boolean (value);
262       break;
263     default:
264       G_OBJECT_WARN_INVALID_PROPERTY_ID (self, property_id, pspec);
265   }
266 }
267
268 static void
269 ges_timeline_element_dispose (GObject * object)
270 {
271   GESTimelineElement *self = GES_TIMELINE_ELEMENT (object);
272
273   if (self->priv->children_props) {
274     g_hash_table_unref (self->priv->children_props);
275     self->priv->children_props = NULL;
276   }
277
278   g_clear_object (&self->priv->copied_from);
279
280   G_OBJECT_CLASS (ges_timeline_element_parent_class)->dispose (object);
281 }
282
283 static void
284 ges_timeline_element_finalize (GObject * self)
285 {
286   GESTimelineElement *tle = GES_TIMELINE_ELEMENT (self);
287
288   g_free (tle->name);
289
290   G_OBJECT_CLASS (ges_timeline_element_parent_class)->finalize (self);
291 }
292
293 static void
294 _child_prop_handler_free (ChildPropHandler * handler)
295 {
296   g_object_freeze_notify (handler->child);
297   if (handler->handler_id)
298     g_signal_handler_disconnect (handler->child, handler->handler_id);
299   g_object_thaw_notify (handler->child);
300   gst_object_unref (handler->child);
301   g_slice_free (ChildPropHandler, handler);
302 }
303
304 static void
305 ges_timeline_element_init (GESTimelineElement * self)
306 {
307   self->priv = ges_timeline_element_get_instance_private (self);
308
309   self->priv->serialize = TRUE;
310
311   self->priv->children_props =
312       g_hash_table_new_full ((GHashFunc) ges_pspec_hash, ges_pspec_equal,
313       (GDestroyNotify) g_param_spec_unref,
314       (GDestroyNotify) _child_prop_handler_free);
315 }
316
317 static void
318 ges_timeline_element_class_init (GESTimelineElementClass * klass)
319 {
320   GObjectClass *object_class = G_OBJECT_CLASS (klass);
321
322   object_class->get_property = _get_property;
323   object_class->set_property = _set_property;
324
325   /**
326    * GESTimelineElement:parent:
327    *
328    * The parent container of the object
329    */
330   properties[PROP_PARENT] =
331       g_param_spec_object ("parent", "Parent",
332       "The parent container of the object", GES_TYPE_TIMELINE_ELEMENT,
333       G_PARAM_READWRITE);
334
335   /**
336    * GESTimelineElement:timeline:
337    *
338    * The timeline in which @element is
339    */
340   properties[PROP_TIMELINE] =
341       g_param_spec_object ("timeline", "Timeline",
342       "The timeline the object is in", GES_TYPE_TIMELINE, G_PARAM_READWRITE);
343
344   /**
345    * GESTimelineElement:start:
346    *
347    * The position of the object in its container (in nanoseconds).
348    */
349   properties[PROP_START] = g_param_spec_uint64 ("start", "Start",
350       "The position in the container", 0, G_MAXUINT64, 0, G_PARAM_READWRITE);
351
352   /**
353    * GESTimelineElement:in-point:
354    *
355    * The in-point at which this #GESTimelineElement will start outputting data
356    * from its contents (in nanoseconds).
357    *
358    * Ex : an in-point of 5 seconds means that the first outputted buffer will
359    * be the one located 5 seconds in the controlled resource.
360    */
361   properties[PROP_INPOINT] =
362       g_param_spec_uint64 ("in-point", "In-point", "The in-point", 0,
363       G_MAXUINT64, 0, G_PARAM_READWRITE);
364
365   /**
366    * GESTimelineElement:duration:
367    *
368    * The duration (in nanoseconds) which will be used in the container
369    */
370   properties[PROP_DURATION] =
371       g_param_spec_uint64 ("duration", "Duration", "The duration to use", 0,
372       G_MAXUINT64, GST_CLOCK_TIME_NONE, G_PARAM_READWRITE);
373
374   /**
375    * GESTimelineElement:max-duration:
376    *
377    * The maximum duration (in nanoseconds) of the #GESTimelineElement.
378    */
379   properties[PROP_MAX_DURATION] =
380       g_param_spec_uint64 ("max-duration", "Maximum duration",
381       "The maximum duration of the object", 0, G_MAXUINT64, GST_CLOCK_TIME_NONE,
382       G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
383
384   /**
385    * GESTimelineElement:priority:
386    *
387    * The priority of the object.
388    *
389    * Setting GESTimelineElement priorities is deprecated
390    * as all priority management is done by GES itself now.
391    */
392   properties[PROP_PRIORITY] = g_param_spec_uint ("priority", "Priority",
393       "The priority of the object", 0, G_MAXUINT, 0, G_PARAM_READWRITE);
394
395   /**
396    * GESTimelineElement:name:
397    *
398    * The name of the object
399    */
400   properties[PROP_NAME] =
401       g_param_spec_string ("name", "Name", "The name of the timeline object",
402       NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
403
404   /**
405    * GESTimelineElement:serialize:
406    *
407    * Whether the element should be serialized.
408    */
409   properties[PROP_SERIALIZE] = g_param_spec_boolean ("serialize", "Serialize",
410       "Whether the element should be serialized", TRUE,
411       G_PARAM_READWRITE | GES_PARAM_NO_SERIALIZATION);
412
413   g_object_class_install_properties (object_class, PROP_LAST, properties);
414
415   /**
416    * GESTimelineElement::deep-notify:
417    * @timeline_element: a #GESTtimelineElement
418    * @prop_object: the object that originated the signal
419    * @prop: the property that changed
420    *
421    * The deep notify signal is used to be notified of property changes of all
422    * the childs of @timeline_element
423    */
424   ges_timeline_element_signals[DEEP_NOTIFY] =
425       g_signal_new ("deep-notify", G_TYPE_FROM_CLASS (klass),
426       G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED |
427       G_SIGNAL_NO_HOOKS, 0, NULL, NULL, g_cclosure_marshal_generic,
428       G_TYPE_NONE, 2, G_TYPE_OBJECT, G_TYPE_PARAM);
429
430   object_class->dispose = ges_timeline_element_dispose;
431   object_class->finalize = ges_timeline_element_finalize;
432
433   klass->set_parent = NULL;
434   klass->set_start = NULL;
435   klass->set_inpoint = NULL;
436   klass->set_duration = NULL;
437   klass->set_max_duration = NULL;
438   klass->set_priority = NULL;
439
440   klass->ripple = NULL;
441   klass->ripple_end = NULL;
442   klass->roll_start = NULL;
443   klass->roll_end = NULL;
444   klass->trim = NULL;
445
446   klass->list_children_properties = default_list_children_properties;
447   klass->lookup_child = _lookup_child;
448   klass->set_child_property = _set_child_property;
449 }
450
451 static void
452 _set_name (GESTimelineElement * self, const gchar * wanted_name)
453 {
454   const gchar *type_name;
455   gchar *lowcase_type;
456   gint count;
457   GQuark q;
458   guint i, l;
459   gchar *name = NULL;
460
461   if (!object_name_counts) {
462     g_datalist_init (&object_name_counts);
463   }
464
465   q = g_type_qname (G_OBJECT_TYPE (self));
466   count = GPOINTER_TO_INT (g_datalist_id_get_data (&object_name_counts, q));
467
468   /* GstFooSink -> foosink<N> */
469   type_name = g_quark_to_string (q);
470   if (strncmp (type_name, "GES", 3) == 0)
471     type_name += 3;
472
473   lowcase_type = g_strdup (type_name);
474   l = strlen (lowcase_type);
475   for (i = 0; i < l; i++)
476     lowcase_type[i] = g_ascii_tolower (lowcase_type[i]);
477
478   if (wanted_name == NULL) {
479     /* give the 20th "uriclip" element and the first "uriclip2" (if needed in the future)
480      * different names */
481     l = strlen (type_name);
482     if (l > 0 && g_ascii_isdigit (type_name[l - 1])) {
483       name = g_strdup_printf ("%s-%d", lowcase_type, count++);
484     } else {
485       name = g_strdup_printf ("%s%d", lowcase_type, count++);
486     }
487   } else {
488     /* If the wanted name uses the same 'namespace' as default, make
489      * sure it does not badly interfere with our counting system */
490
491     if (g_str_has_prefix (wanted_name, lowcase_type)) {
492       guint64 tmpcount =
493           g_ascii_strtoull (&wanted_name[strlen (lowcase_type)], NULL, 10);
494
495       if (tmpcount > count) {
496         count = tmpcount + 1;
497         GST_DEBUG_OBJECT (self, "Using same naming %s but updated count to %i",
498             wanted_name, count);
499       } else if (tmpcount < count) {
500         name = g_strdup_printf ("%s%d", lowcase_type, count);
501         count++;
502         GST_DEBUG_OBJECT (self, "Name %s already allocated, giving: %s instead"
503             " New count is %i", wanted_name, name, count);
504       } else {
505         count++;
506         GST_DEBUG_OBJECT (self, "Perfect name, just bumping object count");
507       }
508     }
509
510     if (name == NULL)
511       name = g_strdup (wanted_name);
512   }
513
514   g_free (lowcase_type);
515   g_datalist_id_set_data (&object_name_counts, q, GINT_TO_POINTER (count));
516
517   g_free (self->name);
518   self->name = name;
519 }
520
521 /*********************************************
522  *            API implementation             *
523  *********************************************/
524
525 /**
526  * ges_timeline_element_set_parent:
527  * @self: a #GESTimelineElement
528  * @parent: new parent of self
529  *
530  * Sets the parent of @self to @parent. The parents needs to already
531  * own a hard reference on @self.
532  *
533  * Returns: %TRUE if @parent could be set or %FALSE when @self
534  * already had a parent or @self and @parent are the same.
535  */
536 gboolean
537 ges_timeline_element_set_parent (GESTimelineElement * self,
538     GESTimelineElement * parent)
539 {
540   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), FALSE);
541   g_return_val_if_fail (parent == NULL
542       || GES_IS_TIMELINE_ELEMENT (parent), FALSE);
543
544   if (self == parent) {
545     GST_INFO_OBJECT (self, "Trying to add %p in itself, not a good idea!",
546         self);
547     gst_object_ref_sink (self);
548     gst_object_unref (self);
549     return FALSE;
550   }
551
552   GST_DEBUG_OBJECT (self, "set parent to %" GST_PTR_FORMAT, parent);
553
554   if (self->parent != NULL && parent != NULL)
555     goto had_parent;
556
557   if (GES_TIMELINE_ELEMENT_GET_CLASS (self)->set_parent) {
558     if (!GES_TIMELINE_ELEMENT_GET_CLASS (self)->set_parent (self, parent))
559       return FALSE;
560   }
561
562   self->parent = parent;
563
564   g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_PARENT]);
565   return TRUE;
566
567   /* ERROR handling */
568 had_parent:
569   {
570     GST_WARNING_OBJECT (self, "set parent failed, object already had a parent");
571     gst_object_ref_sink (self);
572     gst_object_unref (self);
573     return FALSE;
574   }
575 }
576
577 /**
578  * ges_timeline_element_get_parent:
579  * @self: a #GESTimelineElement
580  *
581  * Returns the parent of @self. This function increases the refcount
582  * of the parent object so you should gst_object_unref() it after usage.
583  *
584  * Returns: (transfer full) (nullable): parent of @self, this can be %NULL if
585  * @self has no parent. unref after usage.
586  */
587 GESTimelineElement *
588 ges_timeline_element_get_parent (GESTimelineElement * self)
589 {
590   GESTimelineElement *result = NULL;
591
592   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), NULL);
593
594   result = self->parent;
595   if (G_LIKELY (result))
596     gst_object_ref (result);
597
598   return result;
599 }
600
601 /**
602  * ges_timeline_element_set_timeline:
603  * @self: a #GESTimelineElement
604  * @timeline: The #GESTimeline @self is in
605  *
606  * Sets the timeline of @self to @timeline.
607  *
608  * Returns: %TRUE if @timeline could be set or %FALSE when @timeline
609  * already had a timeline.
610  */
611 gboolean
612 ges_timeline_element_set_timeline (GESTimelineElement * self,
613     GESTimeline * timeline)
614 {
615   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), FALSE);
616   g_return_val_if_fail (timeline == NULL || GES_IS_TIMELINE (timeline), FALSE);
617
618   GST_DEBUG_OBJECT (self, "set timeline to %" GST_PTR_FORMAT, timeline);
619
620   if (timeline != NULL && G_UNLIKELY (self->timeline != NULL))
621     goto had_timeline;
622
623   if (timeline == NULL) {
624     if (self->timeline) {
625       if (!timeline_remove_element (self->timeline, self)) {
626         GST_INFO_OBJECT (self, "Could not remove from"
627             " currently set timeline %" GST_PTR_FORMAT, self->timeline);
628         return FALSE;
629       }
630     }
631   } else {
632     if (!timeline_add_element (timeline, self)) {
633       GST_INFO_OBJECT (self, "Could not add to timeline %" GST_PTR_FORMAT,
634           self);
635       return FALSE;
636     }
637   }
638
639   self->timeline = timeline;
640
641   g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TIMELINE]);
642   return TRUE;
643
644   /* ERROR handling */
645 had_timeline:
646   {
647     GST_DEBUG_OBJECT (self, "set timeline failed, object already had a "
648         "timeline");
649     return FALSE;
650   }
651 }
652
653 /**
654  * ges_timeline_element_get_timeline:
655  * @self: a #GESTimelineElement
656  *
657  * Returns the timeline of @self. This function increases the refcount
658  * of the timeline so you should gst_object_unref() it after usage.
659  *
660  * Returns: (transfer full) (nullable): timeline of @self, this can be %NULL if
661  * @self has no timeline. unref after usage.
662  */
663 GESTimeline *
664 ges_timeline_element_get_timeline (GESTimelineElement * self)
665 {
666   GESTimeline *result = NULL;
667
668   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), NULL);
669
670   result = self->timeline;
671   if (G_LIKELY (result))
672     gst_object_ref (result);
673
674   return result;
675 }
676
677 /**
678  * ges_timeline_element_set_start:
679  * @self: a #GESTimelineElement
680  * @start: the position in #GstClockTime
681  *
682  * Set the position of the object in its containing layer.
683  *
684  * Note that if the snapping-distance property of the timeline containing
685  * @self is set, @self will properly snap to the edges around @start.
686  *
687  * Returns: %TRUE if @start could be set.
688  */
689 gboolean
690 ges_timeline_element_set_start (GESTimelineElement * self, GstClockTime start)
691 {
692   GESTimelineElementClass *klass;
693   GESTimelineElement *toplevel_container, *parent;
694
695   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), FALSE);
696
697   klass = GES_TIMELINE_ELEMENT_GET_CLASS (self);
698
699   GST_DEBUG_OBJECT (self, "current start: %" GST_TIME_FORMAT
700       " new start: %" GST_TIME_FORMAT,
701       GST_TIME_ARGS (GES_TIMELINE_ELEMENT_START (self)), GST_TIME_ARGS (start));
702
703   toplevel_container = ges_timeline_element_get_toplevel_parent (self);
704   parent = self->parent;
705
706   /* FIXME This should not belong to GESTimelineElement */
707   if (toplevel_container &&
708       ((gint64) (_START (toplevel_container) + start - _START (self))) < 0 &&
709       parent
710       && GES_CONTAINER (parent)->children_control_mode == GES_CHILDREN_UPDATE) {
711     GST_INFO_OBJECT (self,
712         "Can not move the object as it would imply its "
713         "container to have a negative start value");
714
715     gst_object_unref (toplevel_container);
716     return FALSE;
717   }
718
719   gst_object_unref (toplevel_container);
720   if (klass->set_start) {
721     gboolean res = klass->set_start (self, start);
722     if (res) {
723       self->start = start;
724       g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_START]);
725     }
726
727     GST_DEBUG_OBJECT (self, "New start: %" GST_TIME_FORMAT,
728         GST_TIME_ARGS (GES_TIMELINE_ELEMENT_START (self)));
729     return res;
730   }
731
732   GST_WARNING_OBJECT (self, "No set_start virtual method implementation"
733       " on class %s. Can not set start %" GST_TIME_FORMAT,
734       G_OBJECT_CLASS_NAME (klass), GST_TIME_ARGS (start));
735   return FALSE;
736 }
737
738 /**
739  * ges_timeline_element_set_inpoint:
740  * @self: a #GESTimelineElement
741  * @inpoint: the in-point in #GstClockTime
742  *
743  * Set the in-point, that is the moment at which the @self will start
744  * outputting data from its contents.
745  *
746  * Returns: %TRUE if @inpoint could be set.
747  */
748 gboolean
749 ges_timeline_element_set_inpoint (GESTimelineElement * self,
750     GstClockTime inpoint)
751 {
752   GESTimelineElementClass *klass;
753
754   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), FALSE);
755
756   GST_DEBUG_OBJECT (self, "current inpoint: %" GST_TIME_FORMAT
757       " new inpoint: %" GST_TIME_FORMAT, GST_TIME_ARGS (inpoint),
758       GST_TIME_ARGS (GES_TIMELINE_ELEMENT_INPOINT (self)));
759
760   klass = GES_TIMELINE_ELEMENT_GET_CLASS (self);
761
762   if (klass->set_inpoint) {
763     gboolean res = klass->set_inpoint (self, inpoint);
764     if (res) {
765       self->inpoint = inpoint;
766       g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_INPOINT]);
767     }
768
769     return res;
770   }
771
772   GST_DEBUG_OBJECT (self, "No set_inpoint virtual method implementation"
773       " on class %s. Can not set inpoint %" GST_TIME_FORMAT,
774       G_OBJECT_CLASS_NAME (klass), GST_TIME_ARGS (inpoint));
775
776   return FALSE;
777 }
778
779 /**
780  * ges_timeline_element_set_max_duration:
781  * @self: a #GESTimelineElement
782  * @maxduration: the maximum duration in #GstClockTime
783  *
784  * Set the maximun duration of the object
785  *
786  * Returns: %TRUE if @maxduration could be set.
787  */
788 gboolean
789 ges_timeline_element_set_max_duration (GESTimelineElement * self,
790     GstClockTime maxduration)
791 {
792   GESTimelineElementClass *klass;
793
794   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), FALSE);
795
796   klass = GES_TIMELINE_ELEMENT_GET_CLASS (self);
797
798   GST_DEBUG_OBJECT (self, "current duration: %" GST_TIME_FORMAT
799       " new duration: %" GST_TIME_FORMAT,
800       GST_TIME_ARGS (GES_TIMELINE_ELEMENT_MAX_DURATION (self)),
801       GST_TIME_ARGS (maxduration));
802
803   if (klass->set_max_duration) {
804     if (klass->set_max_duration (self, maxduration) == FALSE)
805       return FALSE;
806   }
807
808   self->maxduration = maxduration;
809   g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MAX_DURATION]);
810   return TRUE;
811 }
812
813 /**
814  * ges_timeline_element_set_duration:
815  * @self: a #GESTimelineElement
816  * @duration: the duration in #GstClockTime
817  *
818  * Set the duration of the object
819  *
820  * Note that if the timeline snap-distance property of the timeline containing
821  * @self is set, @self will properly snap to its neighboors.
822  *
823  * Returns: %TRUE if @duration could be set.
824  */
825 gboolean
826 ges_timeline_element_set_duration (GESTimelineElement * self,
827     GstClockTime duration)
828 {
829   GESTimelineElementClass *klass;
830
831   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), FALSE);
832
833   klass = GES_TIMELINE_ELEMENT_GET_CLASS (self);
834
835   GST_DEBUG_OBJECT (self, "current duration: %" GST_TIME_FORMAT
836       " new duration: %" GST_TIME_FORMAT,
837       GST_TIME_ARGS (GES_TIMELINE_ELEMENT_DURATION (self)),
838       GST_TIME_ARGS (duration));
839
840   if (klass->set_duration) {
841     gboolean res = klass->set_duration (self, duration);
842     if (res) {
843       self->duration = duration;
844       g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_DURATION]);
845     }
846
847     return res;
848   }
849
850   GST_WARNING_OBJECT (self, "No set_duration virtual method implementation"
851       " on class %s. Can not set duration %" GST_TIME_FORMAT,
852       G_OBJECT_CLASS_NAME (klass), GST_TIME_ARGS (duration));
853   return FALSE;
854 }
855
856 /**
857  * ges_timeline_element_get_start:
858  * @self: a #GESTimelineElement
859  *
860  * Returns: The @start of @self
861  */
862 GstClockTime
863 ges_timeline_element_get_start (GESTimelineElement * self)
864 {
865   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), GST_CLOCK_TIME_NONE);
866
867   return self->start;
868 }
869
870 /**
871  * ges_timeline_element_get_inpoint:
872  * @self: a #GESTimelineElement
873  *
874  * Returns: The @inpoint of @self
875  */
876 GstClockTime
877 ges_timeline_element_get_inpoint (GESTimelineElement * self)
878 {
879   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), GST_CLOCK_TIME_NONE);
880
881   return self->inpoint;
882 }
883
884 /**
885  * ges_timeline_element_get_duration:
886  * @self: a #GESTimelineElement
887  *
888  * Returns: The @duration of @self
889  */
890 GstClockTime
891 ges_timeline_element_get_duration (GESTimelineElement * self)
892 {
893   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), GST_CLOCK_TIME_NONE);
894
895   return self->duration;
896 }
897
898 /**
899  * ges_timeline_element_get_max_duration:
900  * @self: a #GESTimelineElement
901  *
902  * Returns: The @maxduration of @self
903  */
904 GstClockTime
905 ges_timeline_element_get_max_duration (GESTimelineElement * self)
906 {
907   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), GST_CLOCK_TIME_NONE);
908
909   return self->maxduration;
910 }
911
912 /**
913  * ges_timeline_element_get_priority:
914  * @self: a #GESTimelineElement
915  *
916  * Returns: The @priority of @self
917  */
918 guint32
919 ges_timeline_element_get_priority (GESTimelineElement * self)
920 {
921   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), 0);
922
923   return self->priority;
924 }
925
926 /**
927  * ges_timeline_element_set_priority:
928  * @self: a #GESTimelineElement
929  * @priority: the priority
930  *
931  * Sets the priority of the object within the containing layer
932  *
933  * Deprecated: All priority management is done by GES itself now.
934  * To set #GESEffect priorities #ges_clip_set_top_effect_index should
935  * be used.
936  *
937  * Returns: %TRUE if @priority could be set.
938  */
939 gboolean
940 ges_timeline_element_set_priority (GESTimelineElement * self, guint32 priority)
941 {
942   GESTimelineElementClass *klass;
943
944   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), FALSE);
945
946   klass = GES_TIMELINE_ELEMENT_GET_CLASS (self);
947
948   GST_DEBUG_OBJECT (self, "current priority: %d new priority: %d",
949       self->priority, priority);
950
951   if (klass->set_priority) {
952     gboolean res = klass->set_priority (self, priority);
953     if (res) {
954       self->priority = priority;
955       g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_PRIORITY]);
956     }
957
958     return res;
959   }
960
961   GST_WARNING_OBJECT (self, "No set_priority virtual method implementation"
962       " on class %s. Can not set priority %d", G_OBJECT_CLASS_NAME (klass),
963       priority);
964   return FALSE;
965 }
966
967 /**
968  * ges_timeline_element_ripple:
969  * @self: The #GESTimelineElement to ripple.
970  * @start: The new start of @self in ripple mode.
971  *
972  * Edits @self in ripple mode. It allows you to modify the
973  * start of @self and move the following neighbours accordingly.
974  * This will change the overall timeline duration.
975  *
976  * Returns: %TRUE if the self as been rippled properly, %FALSE if an error
977  * occured
978  */
979 gboolean
980 ges_timeline_element_ripple (GESTimelineElement * self, GstClockTime start)
981 {
982   GESTimelineElementClass *klass;
983
984   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), FALSE);
985
986   klass = GES_TIMELINE_ELEMENT_GET_CLASS (self);
987
988   if (klass->ripple)
989     return klass->ripple (self, start);
990
991   GST_WARNING_OBJECT (self, "No ripple virtual method implementation"
992       " on class %s. Can not ripple to %" GST_TIME_FORMAT,
993       G_OBJECT_CLASS_NAME (klass), GST_TIME_ARGS (start));
994
995   return FALSE;
996 }
997
998 /**
999  * ges_timeline_element_ripple_end:
1000  * @self: The #GESTimelineElement to ripple.
1001  * @end: The new end (start + duration) of @self in ripple mode. It will
1002  *       basically only change the duration of @self.
1003  *
1004  * Edits @self in ripple mode. It allows you to modify the
1005  * duration of a @self and move the following neighbours accordingly.
1006  * This will change the overall timeline duration.
1007  *
1008  * Returns: %TRUE if the self as been rippled properly, %FALSE if an error
1009  * occured
1010  */
1011 gboolean
1012 ges_timeline_element_ripple_end (GESTimelineElement * self, GstClockTime end)
1013 {
1014   GESTimelineElementClass *klass;
1015
1016   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), FALSE);
1017
1018   klass = GES_TIMELINE_ELEMENT_GET_CLASS (self);
1019
1020   if (klass->ripple_end) {
1021     return klass->ripple_end (self, end);
1022   }
1023
1024   GST_WARNING_OBJECT (self, "No ripple virtual method implementation"
1025       " on class %s. Can not ripple end to %" GST_TIME_FORMAT,
1026       G_OBJECT_CLASS_NAME (klass), GST_TIME_ARGS (end));
1027
1028   return FALSE;
1029 }
1030
1031 /**
1032  * ges_timeline_element_roll_start:
1033  * @self: The #GESTimelineElement to roll
1034  * @start: The new start of @self in roll mode, it will also adapat
1035  * the in-point of @self according
1036  *
1037  * Edits @self in roll mode. It allows you to modify the
1038  * start and inpoint of a @self and "resize" (basicly change the duration
1039  * in this case) of the previous neighbours accordingly.
1040  * This will not change the overall timeline duration.
1041  *
1042  * Returns: %TRUE if the self as been roll properly, %FALSE if an error
1043  * occured
1044  */
1045 gboolean
1046 ges_timeline_element_roll_start (GESTimelineElement * self, GstClockTime start)
1047 {
1048   GESTimelineElementClass *klass;
1049
1050   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), FALSE);
1051
1052   klass = GES_TIMELINE_ELEMENT_GET_CLASS (self);
1053
1054   if (klass->roll_start) {
1055     return klass->roll_start (self, start);
1056   }
1057
1058   GST_WARNING_OBJECT (self, "No ripple virtual method implementation"
1059       " on class %s. Can not roll to %" GST_TIME_FORMAT,
1060       G_OBJECT_CLASS_NAME (klass), GST_TIME_ARGS (start));
1061
1062   return FALSE;
1063 }
1064
1065 /**
1066  * ges_timeline_element_roll_end:
1067  * @self: The #GESTimelineElement to roll.
1068  * @end: The new end (start + duration) of @self in roll mode
1069  *
1070  * Edits @self in roll mode. It allows you to modify the
1071  * duration of a @self and trim (basicly change the start + inpoint
1072  * in this case) the following neighbours accordingly.
1073  * This will not change the overall timeline duration.
1074  *
1075  * Returns: %TRUE if the self as been rolled properly, %FALSE if an error
1076  * occured
1077  */
1078 gboolean
1079 ges_timeline_element_roll_end (GESTimelineElement * self, GstClockTime end)
1080 {
1081   GESTimelineElementClass *klass;
1082
1083   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), FALSE);
1084
1085   klass = GES_TIMELINE_ELEMENT_GET_CLASS (self);
1086
1087   if (klass->roll_end)
1088     return klass->roll_end (self, end);
1089
1090   GST_WARNING_OBJECT (self, "No ripple virtual method implementation"
1091       " on class %s. Can not roll end to %" GST_TIME_FORMAT,
1092       G_OBJECT_CLASS_NAME (klass), GST_TIME_ARGS (end));
1093
1094   return FALSE;
1095 }
1096
1097 /**
1098  * ges_timeline_element_trim:
1099  * @self: The #GESTimelineElement to trim.
1100  * @start: The new start of @self in trim mode, will adapt the inpoint
1101  * of @self accordingly
1102  *
1103  * Edits @self in trim mode. It allows you to modify the
1104  * inpoint and start of @self.
1105  * This will not change the overall timeline duration.
1106  *
1107  * Note that to trim the end of an self you can just set its duration. The same way
1108  * as this method, it will take into account the snapping-distance property of the
1109  * timeline in which @self is.
1110  *
1111  * Returns: %TRUE if the self as been trimmed properly, %FALSE if an error
1112  * occured
1113  */
1114 gboolean
1115 ges_timeline_element_trim (GESTimelineElement * self, GstClockTime start)
1116 {
1117   GESTimelineElementClass *klass;
1118
1119   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), FALSE);
1120
1121   klass = GES_TIMELINE_ELEMENT_GET_CLASS (self);
1122
1123   if (klass->trim)
1124     return klass->trim (self, start);
1125
1126   GST_WARNING_OBJECT (self, "No ripple virtual method implementation"
1127       " on class %s. Can not trim to %" GST_TIME_FORMAT,
1128       G_OBJECT_CLASS_NAME (klass), GST_TIME_ARGS (start));
1129
1130   return FALSE;
1131 }
1132
1133 /**
1134  * ges_timeline_element_copy:
1135  * @self: The #GESTimelineElement to copy
1136  * @deep: whether we want to create the elements @self contains or not
1137  *
1138  * Copies @self
1139  *
1140  * Returns: (transfer floating): The newly create #GESTimelineElement, copied from @self
1141  */
1142 GESTimelineElement *
1143 ges_timeline_element_copy (GESTimelineElement * self, gboolean deep)
1144 {
1145   GESAsset *asset;
1146   GParameter *params;
1147   GParamSpec **specs;
1148   GESTimelineElementClass *klass;
1149   guint n, n_specs, n_params;
1150
1151   GESTimelineElement *ret = NULL;
1152
1153   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), FALSE);
1154
1155   klass = GES_TIMELINE_ELEMENT_GET_CLASS (self);
1156
1157   specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (self), &n_specs);
1158   params = g_new0 (GParameter, n_specs);
1159   n_params = 0;
1160
1161   for (n = 0; n < n_specs; ++n) {
1162     /* We do not want the timeline or the name to be copied */
1163     if (g_strcmp0 (specs[n]->name, "parent") &&
1164         g_strcmp0 (specs[n]->name, "timeline") &&
1165         g_strcmp0 (specs[n]->name, "name") &&
1166         (specs[n]->flags & G_PARAM_READWRITE) == G_PARAM_READWRITE) {
1167       params[n_params].name = g_intern_string (specs[n]->name);
1168       g_value_init (&params[n_params].value, specs[n]->value_type);
1169       g_object_get_property (G_OBJECT (self), specs[n]->name,
1170           &params[n_params].value);
1171       ++n_params;
1172     }
1173   }
1174
1175 #if GLIB_CHECK_VERSION(2, 53, 1)
1176   {
1177     gint i;
1178     GValue *values;
1179     const gchar **names;
1180     values = g_malloc0 (sizeof (GValue) * n_specs);
1181     names = g_malloc0 (sizeof (gchar *) * n_specs);
1182
1183     for (i = 0; i < n_params; i++) {
1184       values[i] = params[i].value;
1185       names[i] = params[i].name;
1186     }
1187
1188     ret =
1189         GES_TIMELINE_ELEMENT (g_object_new_with_properties (G_OBJECT_TYPE
1190             (self), n_params, names, values));
1191     g_free (names);
1192     g_free (values);
1193   }
1194 #else
1195   ret = g_object_newv (G_OBJECT_TYPE (self), n_params, params);
1196 #endif
1197
1198   while (n_params--)
1199     g_value_unset (&params[n_params].value);
1200
1201   g_free (specs);
1202   g_free (params);
1203
1204
1205   asset = ges_extractable_get_asset (GES_EXTRACTABLE (self));
1206   if (asset)
1207     ges_extractable_set_asset (GES_EXTRACTABLE (ret), asset);
1208   if (deep) {
1209     if (klass->deep_copy)
1210       klass->deep_copy (self, ret);
1211     else
1212       GST_WARNING_OBJECT (self, "No deep_copy virtual method implementation"
1213           " on class %s. Can not finish the copy", G_OBJECT_CLASS_NAME (klass));
1214   }
1215
1216   if (deep) {
1217     ret->priv->copied_from = gst_object_ref (self);
1218   }
1219
1220   return ret;
1221 }
1222
1223 /**
1224  * ges_timeline_element_get_toplevel_parent:
1225  * @self: The #GESTimelineElement to get the toplevel parent from
1226  *
1227  * Gets the toplevel #GESTimelineElement controlling @self
1228  *
1229  * Returns: (transfer full): The toplevel controlling parent of @self
1230  */
1231 GESTimelineElement *
1232 ges_timeline_element_get_toplevel_parent (GESTimelineElement * self)
1233 {
1234   GESTimelineElement *toplevel = self;
1235
1236   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), NULL);
1237
1238   while (GES_TIMELINE_ELEMENT_PARENT (toplevel))
1239     toplevel = GES_TIMELINE_ELEMENT_PARENT (toplevel);
1240
1241   return gst_object_ref (toplevel);
1242 }
1243
1244 /**
1245  * ges_timeline_element_get_name:
1246  * @self: a #GESTimelineElement
1247  *
1248  * Returns a copy of the name of @self.
1249  * Caller should g_free() the return value after usage.
1250  *
1251  * Returns: (transfer full): The name of @self
1252  */
1253 gchar *
1254 ges_timeline_element_get_name (GESTimelineElement * self)
1255 {
1256   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), NULL);
1257
1258   return g_strdup (self->name);
1259 }
1260
1261 /**
1262  * ges_timeline_element_set_name:
1263  * @self: a #GESTimelineElement
1264  * @name: (allow-none): The name @self should take (if avalaible<)
1265  *
1266  * Sets the name of object, or gives @self a guaranteed unique name (if name is NULL).
1267  * This function makes a copy of the provided name, so the caller retains ownership
1268  * of the name it sent.
1269  */
1270 gboolean
1271 ges_timeline_element_set_name (GESTimelineElement * self, const gchar * name)
1272 {
1273   gboolean result = TRUE, readd_to_timeline = FALSE;
1274
1275   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), FALSE);
1276
1277   if (name != NULL && !g_strcmp0 (name, self->name)) {
1278     GST_DEBUG_OBJECT (self, "Same name!");
1279     return TRUE;
1280   }
1281
1282   /* parented objects cannot be renamed */
1283   if (self->timeline != NULL && name) {
1284     GESTimelineElement *tmp = ges_timeline_get_element (self->timeline, name);
1285
1286     if (tmp) {
1287       gst_object_unref (tmp);
1288       goto had_timeline;
1289     }
1290
1291     timeline_remove_element (self->timeline, self);
1292     readd_to_timeline = TRUE;
1293   }
1294
1295   _set_name (self, name);
1296
1297   if (readd_to_timeline)
1298     timeline_add_element (self->timeline, self);
1299
1300   return result;
1301
1302   /* error */
1303 had_timeline:
1304   {
1305     GST_WARNING ("Object %s already in a timeline can't be renamed to %s",
1306         self->name, name);
1307     return FALSE;
1308   }
1309 }
1310
1311 static gboolean
1312 emit_deep_notify_in_idle (EmitDeepNotifyInIdleData * data)
1313 {
1314   g_signal_emit (data->self, ges_timeline_element_signals[DEEP_NOTIFY], 0,
1315       data->child, data->arg);
1316
1317   gst_object_unref (data->child);
1318   g_param_spec_unref (data->arg);
1319   gst_object_unref (data->self);
1320   g_slice_free (EmitDeepNotifyInIdleData, data);
1321
1322   return FALSE;
1323 }
1324
1325 static void
1326 child_prop_changed_cb (GObject * child, GParamSpec * arg
1327     G_GNUC_UNUSED, GESTimelineElement * self)
1328 {
1329   EmitDeepNotifyInIdleData *data;
1330
1331   /* Emit "deep-notify" right away if in main thread */
1332   if (g_main_context_acquire (g_main_context_default ())) {
1333     g_main_context_release (g_main_context_default ());
1334     g_signal_emit (self, ges_timeline_element_signals[DEEP_NOTIFY], 0,
1335         child, arg);
1336     return;
1337   }
1338
1339   data = g_slice_new (EmitDeepNotifyInIdleData);
1340
1341   data->child = gst_object_ref (child);
1342   data->arg = g_param_spec_ref (arg);
1343   data->self = gst_object_ref (self);
1344
1345   g_idle_add ((GSourceFunc) emit_deep_notify_in_idle, data);
1346 }
1347
1348 gboolean
1349 ges_timeline_element_add_child_property (GESTimelineElement * self,
1350     GParamSpec * pspec, GObject * child)
1351 {
1352   gchar *signame;
1353   ChildPropHandler *handler;
1354
1355   if (g_hash_table_contains (self->priv->children_props, pspec)) {
1356     GST_INFO_OBJECT (self, "Child property already exists: %s", pspec->name);
1357     return FALSE;
1358   }
1359
1360   GST_DEBUG_OBJECT (self, "Adding child property: %" GST_PTR_FORMAT "::%s",
1361       child, pspec->name);
1362
1363   signame = g_strconcat ("notify::", pspec->name, NULL);
1364   handler = (ChildPropHandler *) g_slice_new0 (ChildPropHandler);
1365   handler->child = gst_object_ref (child);
1366   handler->handler_id =
1367       g_signal_connect (child, signame, G_CALLBACK (child_prop_changed_cb),
1368       self);
1369   g_hash_table_insert (self->priv->children_props, g_param_spec_ref (pspec),
1370       handler);
1371
1372   g_free (signame);
1373
1374   return TRUE;
1375 }
1376
1377 /**
1378  * ges_timeline_element_get_child_property_by_pspec:
1379  * @self: a #GESTrackElement
1380  * @pspec: The #GParamSpec that specifies the property you want to get
1381  * @value: (out): return location for the value
1382  *
1383  * Gets a property of a child of @self.
1384  */
1385 void
1386 ges_timeline_element_get_child_property_by_pspec (GESTimelineElement * self,
1387     GParamSpec * pspec, GValue * value)
1388 {
1389   ChildPropHandler *handler;
1390
1391   g_return_if_fail (GES_IS_TIMELINE_ELEMENT (self));
1392
1393   handler = g_hash_table_lookup (self->priv->children_props, pspec);
1394   if (!handler)
1395     goto not_found;
1396
1397   g_object_get_property (G_OBJECT (handler->child), pspec->name, value);
1398
1399   return;
1400
1401 not_found:
1402   {
1403     GST_ERROR_OBJECT (self, "The %s property doesn't exist", pspec->name);
1404     return;
1405   }
1406 }
1407
1408 /**
1409  * ges_timeline_element_set_child_property_by_pspec:
1410  * @self: a #GESTimelineElement
1411  * @pspec: The #GParamSpec that specifies the property you want to set
1412  * @value: the value
1413  *
1414  * Sets a property of a child of @self.
1415  */
1416 void
1417 ges_timeline_element_set_child_property_by_pspec (GESTimelineElement * self,
1418     GParamSpec * pspec, const GValue * value)
1419 {
1420   ChildPropHandler *handler;
1421   GESTimelineElementClass *klass;
1422
1423   g_return_if_fail (GES_IS_TRACK_ELEMENT (self));
1424
1425   handler = g_hash_table_lookup (self->priv->children_props, pspec);
1426
1427   if (!handler)
1428     goto not_found;
1429
1430   klass = GES_TIMELINE_ELEMENT_GET_CLASS (self);
1431   g_assert (klass->set_child_property);
1432   klass->set_child_property (self, handler->child, pspec, (GValue *) value);
1433
1434   return;
1435
1436 not_found:
1437   {
1438     GST_ERROR ("The %s property doesn't exist", pspec->name);
1439     return;
1440   }
1441 }
1442
1443 /**
1444  * ges_timeline_element_set_child_property:
1445  * @self: The origin #GESTimelineElement
1446  * @property_name: The name of the property
1447  * @value: the value
1448  *
1449  * Sets a property of a child of @self
1450  *
1451  * Note that #ges_timeline_element_set_child_property is really
1452  * intended for language bindings, #ges_timeline_element_set_child_properties
1453  * is much more convenient for C programming.
1454  *
1455  * Returns: %TRUE if the property was set, %FALSE otherwize
1456  */
1457 gboolean
1458 ges_timeline_element_set_child_property (GESTimelineElement * self,
1459     const gchar * property_name, const GValue * value)
1460 {
1461   GParamSpec *pspec;
1462   GESTimelineElementClass *klass;
1463   GObject *child;
1464
1465   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), FALSE);
1466
1467   if (!ges_timeline_element_lookup_child (self, property_name, &child, &pspec))
1468     goto not_found;
1469
1470   klass = GES_TIMELINE_ELEMENT_GET_CLASS (self);
1471   g_assert (klass->set_child_property);
1472   klass->set_child_property (self, child, pspec, (GValue *) value);
1473
1474   gst_object_unref (child);
1475   g_param_spec_unref (pspec);
1476
1477   return TRUE;
1478
1479 not_found:
1480   {
1481     GST_WARNING_OBJECT (self, "The %s property doesn't exist", property_name);
1482
1483     return FALSE;
1484   }
1485 }
1486
1487 /**
1488 * ges_timeline_element_get_child_property:
1489 * @self: The origin #GESTimelineElement
1490 * @property_name: The name of the property
1491 * @value: (out): return location for the property value, it will
1492 * be initialized if it is initialized with 0
1493 *
1494 * In general, a copy is made of the property contents and
1495 * the caller is responsible for freeing the memory by calling
1496 * g_value_unset().
1497 *
1498 * Gets a property of a GstElement contained in @object.
1499 *
1500 * Note that #ges_timeline_element_get_child_property is really
1501 * intended for language bindings, #ges_timeline_element_get_child_properties
1502 * is much more convenient for C programming.
1503 *
1504 * Returns: %TRUE if the property was found, %FALSE otherwize
1505 */
1506 gboolean
1507 ges_timeline_element_get_child_property (GESTimelineElement * self,
1508     const gchar * property_name, GValue * value)
1509 {
1510   GParamSpec *pspec;
1511   GObject *child;
1512
1513   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), FALSE);
1514
1515   if (!ges_timeline_element_lookup_child (self, property_name, &child, &pspec))
1516     goto not_found;
1517
1518   if (G_VALUE_TYPE (value) == G_TYPE_INVALID)
1519     g_value_init (value, pspec->value_type);
1520
1521   g_object_get_property (child, pspec->name, value);
1522
1523   gst_object_unref (child);
1524   g_param_spec_unref (pspec);
1525
1526   return TRUE;
1527
1528 not_found:
1529   {
1530     GST_WARNING_OBJECT (self, "The %s property doesn't exist", property_name);
1531
1532     return FALSE;
1533   }
1534 }
1535
1536 /**
1537  * ges_timeline_element_lookup_child:
1538  * @self: object to lookup the property in
1539  * @prop_name: name of the property to look up. You can specify the name of the
1540  *     class as such: "ClassName::property-name", to guarantee that you get the
1541  *     proper GParamSpec in case various GstElement-s contain the same property
1542  *     name. If you don't do so, you will get the first element found, having
1543  *     this property and the and the corresponding GParamSpec.
1544  * @child: (out) (allow-none) (transfer full): pointer to a #GstElement that
1545  *     takes the real object to set property on
1546  * @pspec: (out) (allow-none) (transfer full): pointer to take the #GParamSpec
1547  *     describing the property
1548  *
1549  * Looks up which @element and @pspec would be effected by the given @name. If various
1550  * contained elements have this property name you will get the first one, unless you
1551  * specify the class name in @name.
1552  *
1553  * Returns: TRUE if @element and @pspec could be found. FALSE otherwise. In that
1554  * case the values for @pspec and @element are not modified. Unref @element after
1555  * usage.
1556  */
1557 gboolean
1558 ges_timeline_element_lookup_child (GESTimelineElement * self,
1559     const gchar * prop_name, GObject ** child, GParamSpec ** pspec)
1560 {
1561   GESTimelineElementClass *class;
1562
1563   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), FALSE);
1564   class = GES_TIMELINE_ELEMENT_GET_CLASS (self);
1565   g_return_val_if_fail (class->lookup_child, FALSE);
1566
1567   return class->lookup_child (self, prop_name, child, pspec);
1568 }
1569
1570 /**
1571  * ges_timeline_element_set_child_property_valist:
1572  * @self: The #GESTimelineElement parent object
1573  * @first_property_name: The name of the first property to set
1574  * @var_args: value for the first property, followed optionally by more
1575  * name/return location pairs, followed by NULL
1576  *
1577  * Sets a property of a child of @self. If there are various child elements
1578  * that have the same property name, you can distinguish them using the following
1579  * syntax: 'ClasseName::property_name' as property name. If you don't, the
1580  * corresponding property of the first element found will be set.
1581  */
1582 void
1583 ges_timeline_element_set_child_property_valist (GESTimelineElement * self,
1584     const gchar * first_property_name, va_list var_args)
1585 {
1586   const gchar *name;
1587   GParamSpec *pspec;
1588   GObject *child;
1589
1590   gchar *error = NULL;
1591   GValue value = { 0, };
1592
1593   g_return_if_fail (GES_IS_TIMELINE_ELEMENT (self));
1594
1595   name = first_property_name;
1596
1597   /* Note: This part is in big part copied from the gst_child_object_set_valist
1598    * method. */
1599
1600   /* iterate over pairs */
1601   while (name) {
1602     if (!ges_timeline_element_lookup_child (self, name, &child, &pspec))
1603       goto not_found;
1604
1605     G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
1606         G_VALUE_NOCOPY_CONTENTS, &error);
1607
1608     if (error)
1609       goto cant_copy;
1610
1611     g_object_set_property (child, pspec->name, &value);
1612
1613     gst_object_unref (child);
1614     g_param_spec_unref (pspec);
1615     g_value_unset (&value);
1616
1617     name = va_arg (var_args, gchar *);
1618   }
1619   return;
1620
1621 not_found:
1622   {
1623     GST_WARNING_OBJECT (self, "No property %s in OBJECT\n", name);
1624     return;
1625   }
1626 cant_copy:
1627   {
1628     GST_WARNING_OBJECT (self, "error copying value %s in %p: %s", pspec->name,
1629         self, error);
1630
1631     gst_object_unref (child);
1632     g_param_spec_unref (pspec);
1633     g_value_unset (&value);
1634     return;
1635   }
1636 }
1637
1638 /**
1639  * ges_timeline_element_set_child_properties:
1640  * @self: The #GESTimelineElement parent object
1641  * @first_property_name: The name of the first property to set
1642  * @...: value for the first property, followed optionally by more
1643  * name/return location pairs, followed by NULL
1644  *
1645  * Sets a property of a child of @self. If there are various child elements
1646  * that have the same property name, you can distinguish them using the following
1647  * syntax: 'ClasseName::property_name' as property name. If you don't, the
1648  * corresponding property of the first element found will be set.
1649  */
1650 void
1651 ges_timeline_element_set_child_properties (GESTimelineElement * self,
1652     const gchar * first_property_name, ...)
1653 {
1654   va_list var_args;
1655
1656   g_return_if_fail (GES_IS_TIMELINE_ELEMENT (self));
1657
1658   va_start (var_args, first_property_name);
1659   ges_timeline_element_set_child_property_valist (self, first_property_name,
1660       var_args);
1661   va_end (var_args);
1662 }
1663
1664 /**
1665  * ges_timeline_element_get_child_property_valist:
1666  * @self: The #GESTimelineElement parent object
1667  * @first_property_name: The name of the first property to get
1668  * @var_args: value for the first property, followed optionally by more
1669  * name/return location pairs, followed by NULL
1670  *
1671  * Gets a property of a child of @self. If there are various child elements
1672  * that have the same property name, you can distinguish them using the following
1673  * syntax: 'ClasseName::property_name' as property name. If you don't, the
1674  * corresponding property of the first element found will be set.
1675  */
1676 void
1677 ges_timeline_element_get_child_property_valist (GESTimelineElement * self,
1678     const gchar * first_property_name, va_list var_args)
1679 {
1680   const gchar *name;
1681   gchar *error = NULL;
1682   GValue value = { 0, };
1683   GParamSpec *pspec;
1684   GObject *child;
1685
1686   g_return_if_fail (GES_IS_TIMELINE_ELEMENT (self));
1687
1688   name = first_property_name;
1689
1690   /* This part is in big part copied from the gst_child_object_get_valist method */
1691   while (name) {
1692     if (!ges_timeline_element_lookup_child (self, name, &child, &pspec))
1693       goto not_found;
1694
1695     g_value_init (&value, pspec->value_type);
1696     g_object_get_property (child, pspec->name, &value);
1697     gst_object_unref (child);
1698     g_param_spec_unref (pspec);
1699
1700     G_VALUE_LCOPY (&value, var_args, 0, &error);
1701     if (error)
1702       goto cant_copy;
1703     g_value_unset (&value);
1704     name = va_arg (var_args, gchar *);
1705   }
1706   return;
1707
1708 not_found:
1709   {
1710     GST_WARNING_OBJECT (self, "no child property %s", name);
1711     return;
1712   }
1713 cant_copy:
1714   {
1715     GST_WARNING_OBJECT (self, "error copying value %s in %s", pspec->name,
1716         error);
1717
1718     g_value_unset (&value);
1719     return;
1720   }
1721 }
1722
1723 static gint
1724 compare_gparamspec (GParamSpec ** a, GParamSpec ** b, gpointer udata)
1725 {
1726   return g_strcmp0 ((*a)->name, (*b)->name);
1727 }
1728
1729
1730 /**
1731  * ges_timeline_element_list_children_properties:
1732  * @self: The #GESTimelineElement to get the list of children properties from
1733  * @n_properties: (out): return location for the length of the returned array
1734  *
1735  * Gets an array of #GParamSpec* for all configurable properties of the
1736  * children of @self.
1737  *
1738  * Returns: (transfer full) (array length=n_properties): an array of #GParamSpec* which should be freed after use or
1739  * %NULL if something went wrong
1740  */
1741 GParamSpec **
1742 ges_timeline_element_list_children_properties (GESTimelineElement * self,
1743     guint * n_properties)
1744 {
1745   GParamSpec **ret;
1746   GESTimelineElementClass *class;
1747
1748   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), NULL);
1749
1750   class = GES_TIMELINE_ELEMENT_GET_CLASS (self);
1751
1752   if (!class->list_children_properties) {
1753     GST_INFO_OBJECT (self, "No %s->list_children_properties implementation",
1754         G_OBJECT_TYPE_NAME (self));
1755
1756     *n_properties = 0;
1757     return NULL;
1758   }
1759
1760   ret = class->list_children_properties (self, n_properties);
1761   g_qsort_with_data (ret, *n_properties, sizeof (GParamSpec *),
1762       (GCompareDataFunc) compare_gparamspec, NULL);
1763
1764   return ret;
1765 }
1766
1767 /**
1768  * ges_timeline_element_get_child_properties:
1769  * @self: The origin #GESTimelineElement
1770  * @first_property_name: The name of the first property to get
1771  * @...: return location for the first property, followed optionally by more
1772  * name/return location pairs, followed by NULL
1773  *
1774  * Gets properties of a child of @self.
1775  */
1776 void
1777 ges_timeline_element_get_child_properties (GESTimelineElement * self,
1778     const gchar * first_property_name, ...)
1779 {
1780   va_list var_args;
1781
1782   g_return_if_fail (GES_IS_TIMELINE_ELEMENT (self));
1783
1784   va_start (var_args, first_property_name);
1785   ges_timeline_element_get_child_property_valist (self, first_property_name,
1786       var_args);
1787   va_end (var_args);
1788 }
1789
1790 gboolean
1791 ges_timeline_element_remove_child_property (GESTimelineElement * self,
1792     GParamSpec * pspec)
1793 {
1794   return g_hash_table_remove (self->priv->children_props, pspec);
1795 }
1796
1797 /**
1798  * ges_timeline_element_get_track_types:
1799  * @self: A #GESTimelineElement
1800  *
1801  * Gets all the TrackTypes @self will interact with
1802  *
1803  * Since: 1.6.0
1804  */
1805 GESTrackType
1806 ges_timeline_element_get_track_types (GESTimelineElement * self)
1807 {
1808   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), 0);
1809   g_return_val_if_fail (GES_TIMELINE_ELEMENT_GET_CLASS (self)->get_track_types,
1810       0);
1811
1812   return GES_TIMELINE_ELEMENT_GET_CLASS (self)->get_track_types (self);
1813 }
1814
1815 /**
1816  * ges_timeline_element_paste:
1817  * @self: The #GESTimelineElement to paste
1818  * @paste_position: The position in the timeline the element should
1819  * be copied to, meaning it will become the start of @self
1820  *
1821  * Paste @self inside the timeline. @self must have been created
1822  * using ges_timeline_element_copy with recurse=TRUE set,
1823  * otherwise it will fail.
1824  *
1825  * Returns: (transfer none): Paste @self copying the element
1826  *
1827  * Since: 1.6.0
1828  */
1829 GESTimelineElement *
1830 ges_timeline_element_paste (GESTimelineElement * self,
1831     GstClockTime paste_position)
1832 {
1833   GESTimelineElement *res;
1834   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self), FALSE);
1835
1836   if (!self->priv->copied_from) {
1837     GST_ERROR_OBJECT (self, "Is not being 'deeply' copied!");
1838
1839     return NULL;
1840   }
1841
1842   if (!GES_TIMELINE_ELEMENT_GET_CLASS (self)->paste) {
1843     GST_ERROR_OBJECT (self, "No paste vmethod implemented");
1844
1845     return NULL;
1846   }
1847
1848   res = GES_TIMELINE_ELEMENT_GET_CLASS (self)->paste (self,
1849       self->priv->copied_from, paste_position);
1850
1851   g_clear_object (&self->priv->copied_from);
1852
1853   return g_object_ref (res);
1854 }
1855
1856 /**
1857  * ges_timeline_element_get_layer_priority:
1858  * @self: A #GESTimelineElement
1859  *
1860  * Returns: The priority of the first layer the element is in (note that only
1861  * groups can span over several layers). %GES_TIMELINE_ELEMENT_NO_LAYER_PRIORITY
1862  * means that the element is not in a layer.
1863  */
1864 guint32
1865 ges_timeline_element_get_layer_priority (GESTimelineElement * self)
1866 {
1867   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (self),
1868       GES_TIMELINE_ELEMENT_NO_LAYER_PRIORITY);
1869
1870   if (!GES_TIMELINE_ELEMENT_GET_CLASS (self)->get_layer_priority)
1871     return self->priority;
1872
1873   return GES_TIMELINE_ELEMENT_GET_CLASS (self)->get_layer_priority (self);
1874 }
1875
1876 /* Internal */
1877 gdouble
1878 ges_timeline_element_get_media_duration_factor (GESTimelineElement * self)
1879 {
1880   gdouble media_duration_factor;
1881   GESEffectClass *class;
1882   GList *props;
1883
1884   media_duration_factor = 1.0;
1885
1886   class = GES_EFFECT_CLASS (g_type_class_ref (GES_TYPE_EFFECT));
1887
1888   for (props = class->rate_properties; props != NULL; props = props->next) {
1889     GObject *child;
1890     GParamSpec *pspec;
1891     if (ges_timeline_element_lookup_child (self, props->data, &child, &pspec)) {
1892       if (G_PARAM_SPEC_VALUE_TYPE (pspec) == G_TYPE_FLOAT) {
1893         gfloat rate_change;
1894         g_object_get (child, pspec->name, &rate_change, NULL);
1895         media_duration_factor *= rate_change;
1896       } else if (G_PARAM_SPEC_VALUE_TYPE (pspec) == G_TYPE_DOUBLE) {
1897         gdouble rate_change;
1898         g_object_get (child, pspec->name, &rate_change, NULL);
1899         media_duration_factor *= rate_change;
1900       } else {
1901         GST_WARNING_OBJECT (self,
1902             "Rate property %s in child %" GST_PTR_FORMAT
1903             " is of unsupported type %s", pspec->name, child,
1904             G_VALUE_TYPE_NAME (pspec->value_type));
1905       }
1906
1907       gst_object_unref (child);
1908       g_param_spec_unref (pspec);
1909
1910       GST_DEBUG_OBJECT (self,
1911           "Added rate changing property %s, set to value %lf",
1912           (const char *) props->data, media_duration_factor);
1913     }
1914   }
1915
1916   g_type_class_unref (class);
1917   return media_duration_factor;
1918 }
1919
1920 /* Internal */
1921 GESTimelineElement *
1922 ges_timeline_element_get_copied_from (GESTimelineElement * self)
1923 {
1924   GESTimelineElement *copied_from = self->priv->copied_from;
1925   self->priv->copied_from = NULL;
1926   return copied_from;
1927 }
1928
1929 GESTimelineElementFlags
1930 ges_timeline_element_flags (GESTimelineElement * self)
1931 {
1932   return self->priv->flags;
1933 }
1934
1935 void
1936 ges_timeline_element_set_flags (GESTimelineElement * self,
1937     GESTimelineElementFlags flags)
1938 {
1939   self->priv->flags = flags;
1940
1941 }