Reimplement the timeline editing API
[platform/upstream/gst-editing-services.git] / ges / ges-timeline.c
1 /* GStreamer Editing Services
2  * Copyright (C) 2009 Edward Hervey <edward.hervey@collabora.co.uk>
3  *               2009 Nokia Corporation
4  *               2012 Thibault Saunier <tsaunier@gnome.org>
5  *               2012 Collabora Ltd.
6  *                 Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
7  *               2019 Igalia S.L
8  *                 Author: Thibault Saunier <tsaunier@igalia.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23  * Boston, MA 02110-1301, USA.
24  */
25
26 /**
27  * SECTION:gestimeline
28  * @title: GESTimeline
29  * @short_description: Multimedia timeline
30  *
31  * #GESTimeline is the central object for any multimedia timeline.
32  *
33  * Contains a list of #GESLayer which users should use to arrange the
34  * various clips through time.
35  *
36  * The output type is determined by the #GESTrack that are set on
37  * the #GESTimeline.
38  *
39  * To save/load a timeline, you can use the ges_timeline_load_from_uri() and
40  * ges_timeline_save_to_uri() methods to use the default format. If you wish
41  *
42  * Note that any change you make in the timeline will not actually be taken
43  * into account until you call the #ges_timeline_commit method.
44  */
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 #include "ges-internal.h"
50 #include "ges-project.h"
51 #include "ges-container.h"
52 #include "ges-timeline.h"
53 #include "ges-timeline-tree.h"
54 #include "ges-track.h"
55 #include "ges-layer.h"
56 #include "ges-auto-transition.h"
57 #include "ges.h"
58
59
60 static GPtrArray *select_tracks_for_object_default (GESTimeline * timeline,
61     GESClip * clip, GESTrackElement * tr_obj, gpointer user_data);
62 static void ges_extractable_interface_init (GESExtractableInterface * iface);
63 static void ges_meta_container_interface_init
64     (GESMetaContainerInterface * iface);
65
66 GST_DEBUG_CATEGORY_STATIC (ges_timeline_debug);
67 #undef GST_CAT_DEFAULT
68 #define GST_CAT_DEFAULT ges_timeline_debug
69
70 /* lock to protect dynamic callbacks, like pad-added */
71 #define DYN_LOCK(timeline) (&GES_TIMELINE (timeline)->priv->dyn_mutex)
72 #define LOCK_DYN(timeline) G_STMT_START {                       \
73     GST_LOG_OBJECT (timeline, "Getting dynamic lock from %p", \
74         g_thread_self());                                       \
75     g_rec_mutex_lock (DYN_LOCK (timeline));                     \
76     GST_LOG_OBJECT (timeline, "Got Dynamic lock from %p",     \
77         g_thread_self());         \
78   } G_STMT_END
79
80 #define UNLOCK_DYN(timeline) G_STMT_START {                         \
81     GST_LOG_OBJECT (timeline, "Unlocking dynamic lock from %p", \
82         g_thread_self());                                         \
83     g_rec_mutex_unlock (DYN_LOCK (timeline));                     \
84     GST_LOG_OBJECT (timeline, "Unlocked Dynamic lock from %p",  \
85         g_thread_self());         \
86   } G_STMT_END
87
88 #define CHECK_THREAD(timeline) g_assert(timeline->priv->valid_thread == g_thread_self())
89
90 struct _GESTimelinePrivate
91 {
92   GNode *tree;
93
94   /* The duration of the timeline */
95   gint64 duration;
96
97   /* The auto-transition of the timeline */
98   gboolean auto_transition;
99
100   /* Timeline edition modes and snapping management */
101   guint64 snapping_distance;
102
103   GRecMutex dyn_mutex;
104   GList *priv_tracks;
105
106   /* Avoid sorting layers when we are actually resyncing them ourself */
107   gboolean resyncing_layers;
108   GList *auto_transitions;
109
110   /* Last snapping  properties */
111   GstClockTime last_snap_ts;
112   GESTrackElement *last_snaped1;
113   GESTrackElement *last_snaped2;
114
115   /* This variable is set to %TRUE when it makes sense to update the transitions,
116    * and %FALSE otherwize */
117   gboolean needs_transitions_update;
118
119   /* While we are creating and adding the TrackElements for a clip, we need to
120    * ignore the child-added signal */
121   GESClip *ignore_track_element_added;
122   GList *groups;
123
124   guint stream_start_group_id;
125
126   GHashTable *all_elements;
127
128   /* With GST_OBJECT_LOCK */
129   guint expected_async_done;
130   /* With GST_OBJECT_LOCK */
131   guint expected_commited;
132
133   /* For ges_timeline_commit_sync */
134   GMutex commited_lock;
135   GCond commited_cond;
136
137   GThread *valid_thread;
138 };
139
140 /* private structure to contain our track-related information */
141
142 typedef struct
143 {
144   GESTimeline *timeline;
145   GESTrack *track;
146   GstPad *pad;                  /* Pad from the track */
147   GstPad *ghostpad;
148
149   gulong probe_id;
150 } TrackPrivate;
151
152 enum
153 {
154   PROP_0,
155   PROP_DURATION,
156   PROP_AUTO_TRANSITION,
157   PROP_SNAPPING_DISTANCE,
158   PROP_UPDATE,
159   PROP_LAST
160 };
161
162 static GParamSpec *properties[PROP_LAST];
163
164 enum
165 {
166   TRACK_ADDED,
167   TRACK_REMOVED,
168   LAYER_ADDED,
169   LAYER_REMOVED,
170   GROUP_ADDED,
171   GROUP_REMOVED,
172   SNAPING_STARTED,
173   SNAPING_ENDED,
174   SELECT_TRACKS_FOR_OBJECT,
175   COMMITED,
176   LAST_SIGNAL
177 };
178
179 G_DEFINE_TYPE_WITH_CODE (GESTimeline, ges_timeline, GST_TYPE_BIN,
180     G_ADD_PRIVATE (GESTimeline)
181     G_IMPLEMENT_INTERFACE (GES_TYPE_EXTRACTABLE, ges_extractable_interface_init)
182     G_IMPLEMENT_INTERFACE (GES_TYPE_META_CONTAINER,
183         ges_meta_container_interface_init));
184
185 static GstBinClass *parent_class;
186
187 static guint ges_timeline_signals[LAST_SIGNAL] = { 0 };
188
189 static gint custom_find_track (TrackPrivate * tr_priv, GESTrack * track);
190
191 static guint nb_assets = 0;
192
193 /* GESExtractable implementation */
194 static gchar *
195 extractable_check_id (GType type, const gchar * id)
196 {
197   gchar *res;
198
199   if (id == NULL)
200     res = g_strdup_printf ("%s-%i", "project", nb_assets);
201   else
202     res = g_strdup (id);
203
204   nb_assets++;
205
206   return res;
207 }
208
209 static gchar *
210 extractable_get_id (GESExtractable * self)
211 {
212   GESAsset *asset;
213
214   if (!(asset = ges_extractable_get_asset (self)))
215     return NULL;
216
217   return g_strdup (ges_asset_get_id (asset));
218 }
219
220 static void
221 ges_extractable_interface_init (GESExtractableInterface * iface)
222 {
223   iface->asset_type = GES_TYPE_PROJECT;
224   iface->check_id = (GESExtractableCheckId) extractable_check_id;
225   iface->get_id = extractable_get_id;
226 }
227
228 static void
229 ges_meta_container_interface_init (GESMetaContainerInterface * iface)
230 {
231 }
232
233 /* GObject Standard vmethods*/
234 static void
235 ges_timeline_get_property (GObject * object, guint property_id,
236     GValue * value, GParamSpec * pspec)
237 {
238   GESTimeline *timeline = GES_TIMELINE (object);
239
240   switch (property_id) {
241     case PROP_DURATION:
242       g_value_set_uint64 (value, timeline->priv->duration);
243       break;
244     case PROP_AUTO_TRANSITION:
245       g_value_set_boolean (value, timeline->priv->auto_transition);
246       break;
247     case PROP_SNAPPING_DISTANCE:
248       g_value_set_uint64 (value, timeline->priv->snapping_distance);
249       break;
250     default:
251       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
252   }
253 }
254
255 static void
256 ges_timeline_set_property (GObject * object, guint property_id,
257     const GValue * value, GParamSpec * pspec)
258 {
259   GESTimeline *timeline = GES_TIMELINE (object);
260
261   switch (property_id) {
262     case PROP_AUTO_TRANSITION:
263       ges_timeline_set_auto_transition (timeline, g_value_get_boolean (value));
264       break;
265     case PROP_SNAPPING_DISTANCE:
266       timeline->priv->snapping_distance = g_value_get_uint64 (value);
267       break;
268     default:
269       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
270   }
271 }
272
273 static void
274 ges_timeline_dispose (GObject * object)
275 {
276   GESTimeline *tl = GES_TIMELINE (object);
277   GESTimelinePrivate *priv = tl->priv;
278   GList *tmp, *groups;
279
280   while (tl->layers) {
281     GESLayer *layer = (GESLayer *) tl->layers->data;
282     ges_timeline_remove_layer (GES_TIMELINE (object), layer);
283   }
284
285   /* FIXME: it should be possible to remove tracks before removing
286    * layers, but at the moment this creates a problem because the track
287    * objects aren't notified that their nleobjects have been destroyed.
288    */
289
290   while (tl->tracks)
291     ges_timeline_remove_track (GES_TIMELINE (object), tl->tracks->data);
292
293   groups = g_list_copy (priv->groups);
294   for (tmp = groups; tmp; tmp = tmp->next) {
295     GList *elems = ges_container_ungroup (tmp->data, FALSE);
296
297     g_list_free_full (elems, gst_object_unref);
298   }
299   g_list_free (priv->groups);
300   g_list_free (groups);
301
302   g_list_free_full (priv->auto_transitions, gst_object_unref);
303
304   g_hash_table_unref (priv->all_elements);
305
306   G_OBJECT_CLASS (ges_timeline_parent_class)->dispose (object);
307 }
308
309 static void
310 ges_timeline_finalize (GObject * object)
311 {
312   GESTimeline *tl = GES_TIMELINE (object);
313
314   g_rec_mutex_clear (&tl->priv->dyn_mutex);
315   g_node_destroy (tl->priv->tree);
316
317   G_OBJECT_CLASS (ges_timeline_parent_class)->finalize (object);
318 }
319
320
321
322 static void
323 ges_timeline_handle_message (GstBin * bin, GstMessage * message)
324 {
325   GESTimeline *timeline = GES_TIMELINE (bin);
326
327   if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ELEMENT) {
328     GstMessage *amessage = NULL;
329     const GstStructure *mstructure = gst_message_get_structure (message);
330
331     if (gst_structure_has_name (mstructure, "NleCompositionStartUpdate")) {
332       if (g_strcmp0 (gst_structure_get_string (mstructure, "reason"), "Seek")) {
333         GST_INFO_OBJECT (timeline,
334             "A composition is starting an update because of %s"
335             " not considering async", gst_structure_get_string (mstructure,
336                 "reason"));
337
338         goto forward;
339       }
340
341       GST_OBJECT_LOCK (timeline);
342       if (timeline->priv->expected_async_done == 0) {
343         amessage = gst_message_new_async_start (GST_OBJECT_CAST (bin));
344         timeline->priv->expected_async_done = g_list_length (timeline->tracks);
345         GST_INFO_OBJECT (timeline, "Posting ASYNC_START %s",
346             gst_structure_get_string (mstructure, "reason"));
347       }
348       GST_OBJECT_UNLOCK (timeline);
349
350     } else if (gst_structure_has_name (mstructure, "NleCompositionUpdateDone")) {
351       if (g_strcmp0 (gst_structure_get_string (mstructure, "reason"), "Seek")) {
352         GST_INFO_OBJECT (timeline,
353             "A composition is done updating because of %s"
354             " not considering async", gst_structure_get_string (mstructure,
355                 "reason"));
356
357         goto forward;
358       }
359
360       GST_OBJECT_LOCK (timeline);
361       timeline->priv->expected_async_done -= 1;
362       if (timeline->priv->expected_async_done == 0) {
363         amessage = gst_message_new_async_done (GST_OBJECT_CAST (bin),
364             GST_CLOCK_TIME_NONE);
365         GST_INFO_OBJECT (timeline, "Posting ASYNC_DONE %s",
366             gst_structure_get_string (mstructure, "reason"));
367       }
368       GST_OBJECT_UNLOCK (timeline);
369     }
370
371     if (amessage)
372       gst_element_post_message (GST_ELEMENT_CAST (bin), amessage);
373   }
374
375 forward:
376   gst_element_post_message (GST_ELEMENT_CAST (bin), message);
377 }
378
379 /* we collect the first result */
380 static gboolean
381 _gst_array_accumulator (GSignalInvocationHint * ihint,
382     GValue * return_accu, const GValue * handler_return, gpointer dummy)
383 {
384   gpointer array;
385
386   array = g_value_get_boxed (handler_return);
387   if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
388     g_value_set_boxed (return_accu, array);
389
390   return FALSE;
391 }
392
393 static void
394 ges_timeline_class_init (GESTimelineClass * klass)
395 {
396   GObjectClass *object_class = G_OBJECT_CLASS (klass);
397   GstBinClass *bin_class = GST_BIN_CLASS (klass);
398
399   GST_DEBUG_CATEGORY_INIT (ges_timeline_debug, "gestimeline",
400       GST_DEBUG_FG_YELLOW, "ges timeline");
401   timeline_tree_init_debug ();
402
403   parent_class = g_type_class_peek_parent (klass);
404
405   object_class->get_property = ges_timeline_get_property;
406   object_class->set_property = ges_timeline_set_property;
407   object_class->dispose = ges_timeline_dispose;
408   object_class->finalize = ges_timeline_finalize;
409
410   bin_class->handle_message = GST_DEBUG_FUNCPTR (ges_timeline_handle_message);
411
412   /**
413    * GESTimeline:duration:
414    *
415    * Current duration (in nanoseconds) of the #GESTimeline
416    */
417   properties[PROP_DURATION] =
418       g_param_spec_uint64 ("duration", "Duration",
419       "The duration of the timeline", 0, G_MAXUINT64,
420       GST_CLOCK_TIME_NONE, G_PARAM_READABLE);
421   g_object_class_install_property (object_class, PROP_DURATION,
422       properties[PROP_DURATION]);
423
424   /**
425    * GESTimeline:auto-transition:
426    *
427    * Sets whether transitions are added automagically when clips overlap.
428    */
429   g_object_class_install_property (object_class, PROP_AUTO_TRANSITION,
430       g_param_spec_boolean ("auto-transition", "Auto-Transition",
431           "whether the transitions are added", FALSE, G_PARAM_READWRITE));
432
433   /**
434    * GESTimeline:snapping-distance:
435    *
436    * Distance (in nanoseconds) from which a moving object will snap
437    * with it neighboors. 0 means no snapping.
438    */
439   properties[PROP_SNAPPING_DISTANCE] =
440       g_param_spec_uint64 ("snapping-distance", "Snapping distance",
441       "Distance from which moving an object will snap with neighboors", 0,
442       G_MAXUINT64, 0, G_PARAM_READWRITE);
443   g_object_class_install_property (object_class, PROP_SNAPPING_DISTANCE,
444       properties[PROP_SNAPPING_DISTANCE]);
445
446   /**
447    * GESTimeline::track-added:
448    * @timeline: the #GESTimeline
449    * @track: the #GESTrack that was added to the timeline
450    *
451    * Will be emitted after the track was added to the timeline.
452    */
453   ges_timeline_signals[TRACK_ADDED] =
454       g_signal_new ("track-added", G_TYPE_FROM_CLASS (klass),
455       G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, track_added), NULL,
456       NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GES_TYPE_TRACK);
457
458   /**
459    * GESTimeline::track-removed:
460    * @timeline: the #GESTimeline
461    * @track: the #GESTrack that was removed from the timeline
462    *
463    * Will be emitted after the track was removed from the timeline.
464    */
465   ges_timeline_signals[TRACK_REMOVED] =
466       g_signal_new ("track-removed", G_TYPE_FROM_CLASS (klass),
467       G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, track_removed),
468       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GES_TYPE_TRACK);
469
470   /**
471    * GESTimeline::layer-added:
472    * @timeline: the #GESTimeline
473    * @layer: the #GESLayer that was added to the timeline
474    *
475    * Will be emitted after a new layer is added to the timeline.
476    */
477   ges_timeline_signals[LAYER_ADDED] =
478       g_signal_new ("layer-added", G_TYPE_FROM_CLASS (klass),
479       G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, layer_added), NULL,
480       NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GES_TYPE_LAYER);
481
482   /**
483    * GESTimeline::layer-removed:
484    * @timeline: the #GESTimeline
485    * @layer: the #GESLayer that was removed from the timeline
486    *
487    * Will be emitted after the layer was removed from the timeline.
488    */
489   ges_timeline_signals[LAYER_REMOVED] =
490       g_signal_new ("layer-removed", G_TYPE_FROM_CLASS (klass),
491       G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, layer_removed),
492       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GES_TYPE_LAYER);
493
494   /**
495    * GESTimeline::group-added
496    * @timeline: the #GESTimeline
497    * @group: the #GESGroup
498    *
499    * Will be emitted after a new group is added to to the timeline.
500    */
501   ges_timeline_signals[GROUP_ADDED] =
502       g_signal_new ("group-added", G_TYPE_FROM_CLASS (klass),
503       G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, group_added), NULL,
504       NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GES_TYPE_GROUP);
505
506   /**
507    * GESTimeline::group-removed
508    * @timeline: the #GESTimeline
509    * @group: the #GESGroup
510    * @children: (element-type GES.Container) (transfer container): a list of #GESContainer
511    *
512    * Will be emitted after a group has been removed from the timeline.
513    */
514   ges_timeline_signals[GROUP_REMOVED] =
515       g_signal_new ("group-removed", G_TYPE_FROM_CLASS (klass),
516       G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, group_removed),
517       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, GES_TYPE_GROUP,
518       G_TYPE_PTR_ARRAY);
519
520   /**
521    * GESTimeline::snapping-started:
522    * @timeline: the #GESTimeline
523    * @obj1: the first #GESTrackElement that was snapping.
524    * @obj2: the second #GESTrackElement that was snapping.
525    * @position: the position where the two objects finally snapping.
526    *
527    * Will be emitted when the 2 #GESTrackElement first snapped
528    */
529   ges_timeline_signals[SNAPING_STARTED] =
530       g_signal_new ("snapping-started", G_TYPE_FROM_CLASS (klass),
531       G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL,
532       G_TYPE_NONE, 3, GES_TYPE_TRACK_ELEMENT, GES_TYPE_TRACK_ELEMENT,
533       G_TYPE_UINT64);
534
535   /**
536    * GESTimeline::snapping-ended:
537    * @timeline: the #GESTimeline
538    * @obj1: the first #GESTrackElement that was snapping.
539    * @obj2: the second #GESTrackElement that was snapping.
540    * @position: the position where the two objects finally snapping.
541    *
542    * Will be emitted when the 2 #GESTrackElement ended to snap
543    */
544   ges_timeline_signals[SNAPING_ENDED] =
545       g_signal_new ("snapping-ended", G_TYPE_FROM_CLASS (klass),
546       G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL,
547       G_TYPE_NONE, 3, GES_TYPE_TRACK_ELEMENT, GES_TYPE_TRACK_ELEMENT,
548       G_TYPE_UINT64);
549
550   /**
551    * GESTimeline::select-tracks-for-object:
552    * @timeline: the #GESTimeline
553    * @clip: The #GESClip on which @track_element will land
554    * @track_element: The #GESTrackElement for which to choose the tracks it should land into
555    *
556    * Returns: (transfer full) (element-type GESTrack): a #GPtrArray of #GESTrack-s where that object should be added
557    */
558   ges_timeline_signals[SELECT_TRACKS_FOR_OBJECT] =
559       g_signal_new ("select-tracks-for-object", G_TYPE_FROM_CLASS (klass),
560       G_SIGNAL_RUN_LAST, 0, _gst_array_accumulator, NULL, NULL,
561       G_TYPE_PTR_ARRAY, 2, GES_TYPE_CLIP, GES_TYPE_TRACK_ELEMENT);
562
563   /**
564    * GESTimeline::commited:
565    * @timeline: the #GESTimeline
566    *
567    * This signal will be emitted once the changes initiated by #ges_timeline_commit
568    * have been executed in the backend. Use #ges_timeline_commit_sync if you
569    * don't need to do anything in the meantime.
570    */
571   ges_timeline_signals[COMMITED] =
572       g_signal_new ("commited", G_TYPE_FROM_CLASS (klass),
573       G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
574 }
575
576 static void
577 ges_timeline_init (GESTimeline * self)
578 {
579   GESTimelinePrivate *priv = self->priv;
580
581   self->priv = ges_timeline_get_instance_private (self);
582   self->priv->tree = g_node_new (self);
583
584   priv = self->priv;
585   self->layers = NULL;
586   self->tracks = NULL;
587   self->priv->duration = 0;
588   self->priv->auto_transition = FALSE;
589   priv->snapping_distance = 0;
590   priv->expected_async_done = 0;
591   priv->expected_commited = 0;
592
593   self->priv->last_snap_ts = GST_CLOCK_TIME_NONE;
594
595   priv->priv_tracks = NULL;
596   priv->needs_transitions_update = TRUE;
597
598   priv->all_elements =
599       g_hash_table_new_full (g_str_hash, g_str_equal, g_free, gst_object_unref);
600
601   priv->stream_start_group_id = -1;
602
603   g_signal_connect_after (self, "select-tracks-for-object",
604       G_CALLBACK (select_tracks_for_object_default), NULL);
605
606   g_rec_mutex_init (&priv->dyn_mutex);
607   g_mutex_init (&priv->commited_lock);
608   priv->valid_thread = g_thread_self ();
609 }
610
611 /* Private methods */
612
613 static inline GESContainer *
614 get_toplevel_container (gpointer element)
615 {
616   GESTimelineElement *ret =
617       ges_timeline_element_get_toplevel_parent ((GESTimelineElement
618           *) (element));
619
620   /*  We own a ref to the elements ourself */
621   gst_object_unref (ret);
622   return (GESContainer *) ret;
623 }
624
625 /* Sorting utils*/
626 static gint
627 sort_layers (gpointer a, gpointer b)
628 {
629   GESLayer *layer_a, *layer_b;
630   guint prio_a, prio_b;
631
632   layer_a = GES_LAYER (a);
633   layer_b = GES_LAYER (b);
634
635   prio_a = ges_layer_get_priority (layer_a);
636   prio_b = ges_layer_get_priority (layer_b);
637
638   if ((gint) prio_a > (guint) prio_b)
639     return 1;
640   if ((guint) prio_a < (guint) prio_b)
641     return -1;
642
643   return 0;
644 }
645
646 static void
647 _resync_layers (GESTimeline * timeline)
648 {
649   GList *tmp;
650   gint i = 0;
651
652   timeline->priv->resyncing_layers = TRUE;
653   for (tmp = timeline->layers; tmp; tmp = tmp->next) {
654     layer_set_priority (tmp->data, i, TRUE);
655     i++;
656   }
657   timeline->priv->resyncing_layers = FALSE;
658 }
659
660 void
661 timeline_update_duration (GESTimeline * timeline)
662 {
663   GstClockTime duration = timeline_tree_get_duration (timeline->priv->tree);
664
665   if (timeline->priv->duration != duration) {
666     GST_DEBUG ("track duration : %" GST_TIME_FORMAT " current : %"
667         GST_TIME_FORMAT, GST_TIME_ARGS (duration),
668         GST_TIME_ARGS (timeline->priv->duration));
669
670     timeline->priv->duration = duration;
671
672     g_object_notify_by_pspec (G_OBJECT (timeline), properties[PROP_DURATION]);
673   }
674 }
675
676 static gint
677 custom_find_track (TrackPrivate * tr_priv, GESTrack * track)
678 {
679   if (tr_priv->track == track)
680     return 0;
681   return -1;
682 }
683
684 static void
685 _destroy_auto_transition_cb (GESAutoTransition * auto_transition,
686     GESTimeline * timeline)
687 {
688   GESTimelinePrivate *priv = timeline->priv;
689   GESClip *transition = auto_transition->transition_clip;
690   GESLayer *layer = ges_clip_get_layer (transition);
691
692   ges_layer_remove_clip (layer, transition);
693   g_signal_handlers_disconnect_by_func (auto_transition,
694       _destroy_auto_transition_cb, timeline);
695
696   priv->auto_transitions =
697       g_list_remove (priv->auto_transitions, auto_transition);
698   gst_object_unref (auto_transition);
699 }
700
701 GESAutoTransition *
702 ges_timeline_create_transition (GESTimeline * timeline,
703     GESTrackElement * previous, GESTrackElement * next, GESClip * transition,
704     GESLayer * layer, guint64 start, guint64 duration)
705 {
706   GESAsset *asset;
707   GESAutoTransition *auto_transition;
708
709   if (transition == NULL) {
710     /* TODO make it possible to specify a Transition asset in the API */
711     asset = ges_asset_request (GES_TYPE_TRANSITION_CLIP, "crossfade", NULL);
712     transition =
713         ges_layer_add_asset (layer, asset, start, 0, duration,
714         ges_track_element_get_track_type (next));
715   } else {
716     GST_DEBUG_OBJECT (timeline,
717         "Reusing already existing transition: %" GST_PTR_FORMAT, transition);
718   }
719
720   /* We know there is only 1 TrackElement */
721   auto_transition =
722       ges_auto_transition_new (GES_CONTAINER_CHILDREN (transition)->data,
723       previous, next);
724
725   g_signal_connect (auto_transition, "destroy-me",
726       G_CALLBACK (_destroy_auto_transition_cb), timeline);
727
728   timeline->priv->auto_transitions =
729       g_list_prepend (timeline->priv->auto_transitions, auto_transition);
730
731   return auto_transition;
732 }
733
734 GESAutoTransition *
735 ges_timeline_find_auto_transition (GESTimeline * timeline,
736     GESTrackElement * prev, GESTrackElement * next,
737     GstClockTime transition_duration)
738 {
739   GList *tmp;
740
741   for (tmp = timeline->priv->auto_transitions; tmp; tmp = tmp->next) {
742     GESAutoTransition *auto_trans = (GESAutoTransition *) tmp->data;
743
744     /* We already have a transition linked to one of the elements we want to
745      * find a transition for */
746     if (auto_trans->previous_source == prev || auto_trans->next_source == next) {
747       if (auto_trans->previous_source != prev
748           || auto_trans->next_source != next) {
749         GST_ERROR_OBJECT (timeline, "Failed creating auto transition, "
750             " trying to have 3 clips overlapping, rolling back");
751       }
752
753       return auto_trans;
754     }
755   }
756
757   return NULL;
758 }
759
760 static GESAutoTransition *
761 _create_auto_transition_from_transitions (GESTimeline * timeline,
762     GESTrackElement * prev, GESTrackElement * next,
763     GstClockTime transition_duration)
764 {
765   GList *tmp, *elements;
766   GESLayer *layer;
767   guint32 layer_prio = GES_TIMELINE_ELEMENT_LAYER_PRIORITY (prev);
768   GESTrack *track;
769   GESAutoTransition *auto_transition =
770       ges_timeline_find_auto_transition (timeline, prev, next,
771       transition_duration);
772
773   if (auto_transition)
774     return auto_transition;
775
776   layer = ges_timeline_get_layer (timeline, layer_prio);
777   track = ges_track_element_get_track (prev);
778   elements = ges_track_get_elements (track);
779   for (tmp = elements; tmp; tmp = tmp->next) {
780     GESTrackElement *maybe_transition = tmp->data;
781
782     if (ges_timeline_element_get_layer_priority (tmp->data) != layer_prio)
783       continue;
784
785     if (_START (maybe_transition) > _START (next))
786       break;
787     else if (_START (maybe_transition) != _START (next) ||
788         _DURATION (maybe_transition) != transition_duration)
789       continue;
790     else if (GES_IS_TRANSITION (maybe_transition)) {
791       /* Use that transition */
792       /* TODO We should make sure that the transition contains only
793        * TrackElement-s in @track and if it is not the case properly unlink the
794        * object to use it */
795       auto_transition = ges_timeline_create_transition (timeline, prev, next,
796           GES_CLIP (GES_TIMELINE_ELEMENT_PARENT (maybe_transition)), layer,
797           _START (next), transition_duration);
798
799       break;
800     }
801   }
802   gst_object_unref (layer);
803   g_list_free_full (elements, gst_object_unref);
804
805   return auto_transition;
806 }
807
808 void
809 ges_timeline_emit_snapping (GESTimeline * timeline, GESTimelineElement * elem1,
810     GESTimelineElement * elem2, GstClockTime snap_time)
811 {
812   GESTimelinePrivate *priv = timeline->priv;
813   GstClockTime last_snap_ts = timeline->priv->last_snap_ts;
814
815   if (!GST_CLOCK_TIME_IS_VALID (snap_time)) {
816     if (priv->last_snaped1 != NULL && priv->last_snaped2 != NULL) {
817       g_signal_emit (timeline, ges_timeline_signals[SNAPING_ENDED], 0,
818           priv->last_snaped1, priv->last_snaped2, last_snap_ts);
819       priv->last_snaped1 = NULL;
820       priv->last_snaped2 = NULL;
821       priv->last_snap_ts = GST_CLOCK_TIME_NONE;
822     }
823
824     return;
825   }
826
827   g_assert (elem1 != elem2);
828   if (GES_IS_CLIP (elem1)) {
829     g_assert (GES_CONTAINER_CHILDREN (elem1));
830     elem1 = GES_CONTAINER_CHILDREN (elem1)->data;
831   }
832
833   if (GES_IS_CLIP (elem2)) {
834     g_assert (GES_CONTAINER_CHILDREN (elem2));
835     elem2 = GES_CONTAINER_CHILDREN (elem2)->data;
836   }
837
838   if (last_snap_ts != snap_time) {
839     g_signal_emit (timeline, ges_timeline_signals[SNAPING_ENDED], 0,
840         priv->last_snaped1, priv->last_snaped2, (last_snap_ts));
841
842     /* We want the snap start signal to be emited anyway */
843     timeline->priv->last_snap_ts = GST_CLOCK_TIME_NONE;
844   }
845
846   if (!GST_CLOCK_TIME_IS_VALID (timeline->priv->last_snap_ts)) {
847     priv->last_snaped1 = (GESTrackElement *) elem1;
848     priv->last_snaped2 = (GESTrackElement *) elem2;
849     timeline->priv->last_snap_ts = snap_time;
850
851     g_signal_emit (timeline, ges_timeline_signals[SNAPING_STARTED], 0,
852         elem1, elem2, snap_time);
853   }
854
855 }
856
857 gboolean
858 ges_timeline_trim_object_simple (GESTimeline * timeline,
859     GESTimelineElement * element, guint32 new_layer_priority,
860     GList * layers, GESEdge edge, guint64 position, gboolean snapping)
861 {
862
863   return timeline_trim_object (timeline, element, new_layer_priority, layers,
864       edge, position);
865 }
866
867 gboolean
868 timeline_ripple_object (GESTimeline * timeline, GESTimelineElement * obj,
869     gint new_layer_priority, GList * layers, GESEdge edge, guint64 position)
870 {
871   gboolean res = TRUE;
872   guint64 new_duration;
873   GstClockTimeDiff diff;
874
875   switch (edge) {
876     case GES_EDGE_NONE:
877       GST_DEBUG ("Simply rippling");
878       diff = GST_CLOCK_DIFF (position, _START (obj));
879
880       timeline->priv->needs_transitions_update = FALSE;
881       res = timeline_tree_ripple (timeline->priv->tree,
882           (gint64) GES_TIMELINE_ELEMENT_LAYER_PRIORITY (obj) -
883           (gint64) new_layer_priority, diff, obj,
884           GES_EDGE_NONE, timeline->priv->snapping_distance);
885       timeline->priv->needs_transitions_update = TRUE;
886
887       break;
888     case GES_EDGE_END:
889       GST_DEBUG ("Rippling end");
890
891       timeline->priv->needs_transitions_update = FALSE;
892       new_duration =
893           CLAMP (position - obj->start, 0, obj->maxduration - obj->inpoint);
894       res =
895           timeline_tree_ripple (timeline->priv->tree,
896           (gint64) GES_TIMELINE_ELEMENT_LAYER_PRIORITY (obj) -
897           (gint64) new_layer_priority,
898           _DURATION (obj) - new_duration, obj,
899           GES_EDGE_END, timeline->priv->snapping_distance);
900       timeline->priv->needs_transitions_update = TRUE;
901
902       GST_DEBUG ("Done Rippling end");
903       break;
904     case GES_EDGE_START:
905       GST_INFO ("Ripple start doesn't make sense, trimming instead");
906       if (!timeline_trim_object (timeline, obj, -1, layers, edge, position))
907         goto error;
908       break;
909     default:
910       GST_DEBUG ("Can not ripple edge: %i", edge);
911
912       break;
913   }
914
915   return res;
916
917 error:
918
919   return FALSE;
920 }
921
922 gboolean
923 timeline_slide_object (GESTimeline * timeline, GESTrackElement * obj,
924     GList * layers, GESEdge edge, guint64 position)
925 {
926
927   /* FIXME implement me! */
928   GST_FIXME_OBJECT (timeline, "Slide mode editing not implemented yet");
929
930   return FALSE;
931 }
932
933 static gboolean
934 _trim_transition (GESTimeline * timeline, GESTimelineElement * element,
935     GESEdge edge, GstClockTime position)
936 {
937   GList *tmp;
938   GESLayer *layer = ges_timeline_get_layer (timeline,
939       GES_TIMELINE_ELEMENT_LAYER_PRIORITY (element));
940
941   if (!ges_layer_get_auto_transition (layer))
942     goto fail;
943
944   gst_object_unref (layer);
945   for (tmp = timeline->priv->auto_transitions; tmp; tmp = tmp->next) {
946     GESAutoTransition *auto_transition = tmp->data;
947
948     if (GES_TIMELINE_ELEMENT (auto_transition->transition) == element ||
949         GES_TIMELINE_ELEMENT (auto_transition->transition_clip) == element) {
950       /* Trimming an auto transition means trimming its neighboors */
951       if (!auto_transition->positioning) {
952         if (edge == GES_EDGE_END) {
953           ges_container_edit (GES_CONTAINER (auto_transition->previous_clip),
954               NULL, -1, GES_EDIT_MODE_TRIM, GES_EDGE_END, position);
955         } else {
956           ges_container_edit (GES_CONTAINER (auto_transition->next_clip),
957               NULL, -1, GES_EDIT_MODE_TRIM, GES_EDGE_START, position);
958         }
959
960         return TRUE;
961       }
962
963       return FALSE;
964     }
965   }
966
967   return FALSE;
968
969 fail:
970   gst_object_unref (layer);
971   return FALSE;
972 }
973
974
975 gboolean
976 timeline_trim_object (GESTimeline * timeline, GESTimelineElement * object,
977     guint32 new_layer_priority, GList * layers, GESEdge edge, guint64 position)
978 {
979   if ((GES_IS_TRANSITION (object) || GES_IS_TRANSITION_CLIP (object)) &&
980       !ELEMENT_FLAG_IS_SET (object, GES_TIMELINE_ELEMENT_SET_SIMPLE)) {
981     return _trim_transition (timeline, object, edge, position);
982   }
983
984   return timeline_tree_trim (timeline->priv->tree,
985       GES_TIMELINE_ELEMENT (object), new_layer_priority > 0 ? (gint64)
986       ges_timeline_element_get_layer_priority (GES_TIMELINE_ELEMENT (object)) -
987       new_layer_priority : 0, edge == GES_EDGE_END ? GST_CLOCK_DIFF (position,
988           _START (object) + _DURATION (object)) : GST_CLOCK_DIFF (position,
989           GES_TIMELINE_ELEMENT_START (object)), edge,
990       timeline->priv->snapping_distance);
991 }
992
993 gboolean
994 timeline_roll_object (GESTimeline * timeline, GESTimelineElement * element,
995     GList * layers, GESEdge edge, guint64 position)
996 {
997   return timeline_tree_roll (timeline->priv->tree,
998       element,
999       (edge == GES_EDGE_END) ?
1000       GST_CLOCK_DIFF (position, _END (element)) :
1001       GST_CLOCK_DIFF (position, _START (element)),
1002       edge, timeline->priv->snapping_distance);
1003 }
1004
1005 gboolean
1006 timeline_move_object (GESTimeline * timeline, GESTimelineElement * object,
1007     guint32 new_layer_priority, GList * layers, GESEdge edge, guint64 position)
1008 {
1009   gboolean ret = FALSE;
1010   GstClockTimeDiff offset = edge == GES_EDGE_END ?
1011       GST_CLOCK_DIFF (position, _START (object) + _DURATION (object)) :
1012       GST_CLOCK_DIFF (position, GES_TIMELINE_ELEMENT_START (object));
1013
1014   ret = timeline_tree_move (timeline->priv->tree,
1015       GES_TIMELINE_ELEMENT (object), new_layer_priority < 0 ? 0 : (gint64)
1016       ges_timeline_element_get_layer_priority (GES_TIMELINE_ELEMENT (object)) -
1017       new_layer_priority, offset, edge, timeline->priv->snapping_distance);
1018
1019   return ret;
1020 }
1021
1022 gboolean
1023 ges_timeline_move_object_simple (GESTimeline * timeline,
1024     GESTimelineElement * element, GList * layers, GESEdge edge,
1025     guint64 position)
1026 {
1027   return timeline_move_object (timeline, element,
1028       ges_timeline_element_get_layer_priority (element), NULL, edge, position);
1029 }
1030
1031 void
1032 timeline_add_group (GESTimeline * timeline, GESGroup * group)
1033 {
1034   GST_DEBUG_OBJECT (timeline, "Adding group %" GST_PTR_FORMAT, group);
1035
1036   timeline->priv->groups = g_list_prepend (timeline->priv->groups,
1037       gst_object_ref_sink (group));
1038
1039   ges_timeline_element_set_timeline (GES_TIMELINE_ELEMENT (group), timeline);
1040 }
1041
1042 void
1043 timeline_update_transition (GESTimeline * timeline)
1044 {
1045   GList *tmp, *auto_transs;
1046
1047   auto_transs = g_list_copy (timeline->priv->auto_transitions);
1048   for (tmp = auto_transs; tmp; tmp = tmp->next)
1049     ges_auto_transition_update (tmp->data);
1050   g_list_free (auto_transs);
1051 }
1052
1053 /**
1054  * timeline_emit_group_added:
1055  * @timeline: a #GESTimeline
1056  * @group: group that was added
1057  *
1058  * Emit group-added signal.
1059  */
1060 void
1061 timeline_emit_group_added (GESTimeline * timeline, GESGroup * group)
1062 {
1063   g_signal_emit (timeline, ges_timeline_signals[GROUP_ADDED], 0, group);
1064 }
1065
1066 /**
1067  * timeline_emit_group_removed:
1068  * @timeline: a #GESTimeline
1069  * @group: group that was removed
1070  *
1071  * Emit group-removed signal.
1072  */
1073 void
1074 timeline_emit_group_removed (GESTimeline * timeline, GESGroup * group,
1075     GPtrArray * array)
1076 {
1077   g_signal_emit (timeline, ges_timeline_signals[GROUP_REMOVED], 0, group,
1078       array);
1079 }
1080
1081 void
1082 timeline_remove_group (GESTimeline * timeline, GESGroup * group)
1083 {
1084   GST_DEBUG_OBJECT (timeline, "Removing group %" GST_PTR_FORMAT, group);
1085
1086   timeline->priv->groups = g_list_remove (timeline->priv->groups, group);
1087
1088   ges_timeline_element_set_timeline (GES_TIMELINE_ELEMENT (group), NULL);
1089   gst_object_unref (group);
1090 }
1091
1092 static GPtrArray *
1093 select_tracks_for_object_default (GESTimeline * timeline,
1094     GESClip * clip, GESTrackElement * tr_object, gpointer user_data)
1095 {
1096   GPtrArray *result;
1097   GList *tmp;
1098
1099   result = g_ptr_array_new ();
1100
1101   for (tmp = timeline->tracks; tmp; tmp = tmp->next) {
1102     GESTrack *track = GES_TRACK (tmp->data);
1103
1104     if ((track->type & ges_track_element_get_track_type (tr_object))) {
1105       gst_object_ref (track);
1106       g_ptr_array_add (result, track);
1107     }
1108   }
1109
1110   return result;
1111 }
1112
1113 static void
1114 add_object_to_tracks (GESTimeline * timeline, GESClip * clip, GESTrack * track)
1115 {
1116   gint i;
1117   GList *tmp, *list;
1118   GESTrackType types, visited_type = GES_TRACK_TYPE_UNKNOWN;
1119
1120   GST_DEBUG_OBJECT (timeline, "Creating %" GST_PTR_FORMAT
1121       " trackelements and adding them to our tracks", clip);
1122
1123   types = ges_clip_get_supported_formats (clip);
1124   if (track) {
1125     if ((types & track->type) == 0)
1126       return;
1127     types = track->type;
1128   }
1129
1130   for (i = 0, tmp = timeline->tracks; tmp; tmp = tmp->next, i++) {
1131     GESTrack *track = GES_TRACK (tmp->data);
1132
1133     if (((track->type & types) == 0 || (track->type & visited_type)))
1134       continue;
1135
1136     list = ges_clip_create_track_elements (clip, track->type);
1137     g_list_free (list);
1138   }
1139 }
1140
1141 static void
1142 layer_auto_transition_changed_cb (GESLayer * layer,
1143     GParamSpec * arg G_GNUC_UNUSED, GESTimeline * timeline)
1144 {
1145   GList *tmp, *clips;
1146
1147   timeline_tree_create_transitions (timeline->priv->tree,
1148       _create_auto_transition_from_transitions);
1149   clips = ges_layer_get_clips (layer);
1150   for (tmp = clips; tmp; tmp = tmp->next) {
1151     if (GES_IS_TRANSITION_CLIP (tmp->data)) {
1152       GList *tmpautotrans;
1153       gboolean found = FALSE;
1154
1155       for (tmpautotrans = timeline->priv->auto_transitions; tmpautotrans;
1156           tmpautotrans = tmpautotrans->next) {
1157         if (GES_AUTO_TRANSITION (tmpautotrans->data)->transition_clip ==
1158             tmp->data) {
1159           found = TRUE;
1160           break;
1161         }
1162       }
1163
1164       if (!found) {
1165         GST_ERROR_OBJECT (timeline,
1166             "Transition %s could not be wrapped into an auto transition"
1167             " REMOVING it", GES_TIMELINE_ELEMENT_NAME (tmp->data));
1168
1169         ges_layer_remove_clip (layer, tmp->data);
1170       }
1171     }
1172   }
1173   g_list_free_full (clips, gst_object_unref);
1174 }
1175
1176 static void
1177 clip_track_element_added_cb (GESClip * clip,
1178     GESTrackElement * track_element, GESTimeline * timeline)
1179 {
1180   guint i;
1181   GESTrack *track;
1182   gboolean is_source;
1183   GPtrArray *tracks = NULL;
1184   GESTrackElement *existing_src = NULL;
1185
1186   if (timeline->priv->ignore_track_element_added == clip) {
1187     GST_DEBUG_OBJECT (timeline, "Ignoring element added (%" GST_PTR_FORMAT
1188         " in %" GST_PTR_FORMAT, track_element, clip);
1189
1190     return;
1191   }
1192
1193   if (ges_track_element_get_track (track_element)) {
1194     GST_WARNING_OBJECT (track_element, "Already in a track");
1195
1196     return;
1197   }
1198
1199   g_signal_emit (G_OBJECT (timeline),
1200       ges_timeline_signals[SELECT_TRACKS_FOR_OBJECT], 0, clip, track_element,
1201       &tracks);
1202
1203   if (!tracks || tracks->len == 0) {
1204     GST_WARNING_OBJECT (timeline, "Got no Track to add %p (type %s), removing"
1205         " from clip (stopping 'child-added' signal emission).",
1206         track_element, ges_track_type_name (ges_track_element_get_track_type
1207             (track_element)));
1208
1209     if (tracks)
1210       g_ptr_array_unref (tracks);
1211
1212     g_signal_stop_emission_by_name (clip, "child-added");
1213     ges_container_remove (GES_CONTAINER (clip),
1214         GES_TIMELINE_ELEMENT (track_element));
1215
1216     return;
1217   }
1218
1219   /* We add the current element to the first track */
1220   track = g_ptr_array_index (tracks, 0);
1221
1222   is_source = g_type_is_a (G_OBJECT_TYPE (track_element), GES_TYPE_SOURCE);
1223   if (is_source)
1224     existing_src = ges_clip_find_track_element (clip, track, GES_TYPE_SOURCE);
1225
1226   if (existing_src == NULL) {
1227     if (!ges_track_add_element (track, track_element)) {
1228       GST_WARNING_OBJECT (clip, "Failed to add track element to track");
1229       ges_container_remove (GES_CONTAINER (clip),
1230           GES_TIMELINE_ELEMENT (track_element));
1231       g_ptr_array_unref (tracks);
1232       return;
1233     }
1234   } else {
1235     GST_INFO_OBJECT (clip, "Already had a Source Element in %" GST_PTR_FORMAT
1236         " of type %s, removing new one. (stopping 'child-added' emission)",
1237         track, G_OBJECT_TYPE_NAME (track_element));
1238     g_signal_stop_emission_by_name (clip, "child-added");
1239     ges_container_remove (GES_CONTAINER (clip),
1240         GES_TIMELINE_ELEMENT (track_element));
1241   }
1242   gst_object_unref (track);
1243   g_clear_object (&existing_src);
1244
1245   /* And create copies to add to other tracks */
1246   timeline->priv->ignore_track_element_added = clip;
1247   for (i = 1; i < tracks->len; i++) {
1248     GESTrack *track;
1249     GESTrackElement *track_element_copy;
1250
1251     track = g_ptr_array_index (tracks, i);
1252     if (is_source)
1253       existing_src = ges_clip_find_track_element (clip, track, GES_TYPE_SOURCE);
1254     if (existing_src == NULL) {
1255       ges_container_remove (GES_CONTAINER (clip),
1256           GES_TIMELINE_ELEMENT (track_element));
1257       gst_object_unref (track);
1258       g_ptr_array_unref (tracks);
1259       continue;
1260     } else {
1261       GST_INFO_OBJECT (clip, "Already had a Source Element in %" GST_PTR_FORMAT
1262           " of type %s, removing new one. (stopping 'child-added' emission)",
1263           track, G_OBJECT_TYPE_NAME (track_element));
1264       g_signal_stop_emission_by_name (clip, "child-added");
1265       ges_container_remove (GES_CONTAINER (clip),
1266           GES_TIMELINE_ELEMENT (track_element));
1267     }
1268     g_clear_object (&existing_src);
1269
1270     track_element_copy =
1271         GES_TRACK_ELEMENT (ges_timeline_element_copy (GES_TIMELINE_ELEMENT
1272             (track_element), TRUE));
1273
1274     GST_LOG_OBJECT (timeline, "Trying to add %p to track %p",
1275         track_element_copy, track);
1276
1277     if (!ges_container_add (GES_CONTAINER (clip),
1278             GES_TIMELINE_ELEMENT (track_element_copy))) {
1279       GST_WARNING_OBJECT (clip, "Failed to add track element to clip");
1280       gst_object_unref (track_element_copy);
1281       g_ptr_array_unref (tracks);
1282       return;
1283     }
1284
1285     if (!ges_track_add_element (track, track_element_copy)) {
1286       GST_WARNING_OBJECT (clip, "Failed to add track element to track");
1287       ges_container_remove (GES_CONTAINER (clip),
1288           GES_TIMELINE_ELEMENT (track_element_copy));
1289       gst_object_unref (track_element_copy);
1290       g_ptr_array_unref (tracks);
1291       return;
1292     }
1293
1294     gst_object_unref (track);
1295   }
1296   timeline->priv->ignore_track_element_added = NULL;
1297   g_ptr_array_unref (tracks);
1298   if (GES_IS_SOURCE (track_element))
1299     timeline_tree_create_transitions (timeline->priv->tree,
1300         ges_timeline_find_auto_transition);
1301 }
1302
1303 static void
1304 clip_track_element_removed_cb (GESClip * clip,
1305     GESTrackElement * track_element, GESTimeline * timeline)
1306 {
1307   GESTrack *track = ges_track_element_get_track (track_element);
1308
1309   if (track)
1310     ges_track_remove_element (track, track_element);
1311 }
1312
1313 static void
1314 layer_object_added_cb (GESLayer * layer, GESClip * clip, GESTimeline * timeline)
1315 {
1316   GESProject *project;
1317
1318   /* We make sure not to be connected twice */
1319   g_signal_handlers_disconnect_by_func (clip, clip_track_element_added_cb,
1320       timeline);
1321   g_signal_handlers_disconnect_by_func (clip, clip_track_element_removed_cb,
1322       timeline);
1323
1324   /* And we connect to the object */
1325   g_signal_connect (clip, "child-added",
1326       G_CALLBACK (clip_track_element_added_cb), timeline);
1327   g_signal_connect (clip, "child-removed",
1328       G_CALLBACK (clip_track_element_removed_cb), timeline);
1329
1330   if (ges_clip_is_moving_from_layer (clip)) {
1331     GST_DEBUG ("Clip %p moving from one layer to another, not creating "
1332         "TrackElement", clip);
1333     timeline_tree_create_transitions (timeline->priv->tree,
1334         ges_timeline_find_auto_transition);
1335     return;
1336   }
1337
1338   add_object_to_tracks (timeline, clip, NULL);
1339
1340   GST_DEBUG ("Making sure that the asset is in our project");
1341   project =
1342       GES_PROJECT (ges_extractable_get_asset (GES_EXTRACTABLE (timeline)));
1343   ges_project_add_asset (project,
1344       ges_extractable_get_asset (GES_EXTRACTABLE (clip)));
1345
1346   GST_DEBUG ("Done");
1347 }
1348
1349 static void
1350 layer_priority_changed_cb (GESLayer * layer,
1351     GParamSpec * arg G_GNUC_UNUSED, GESTimeline * timeline)
1352 {
1353   if (timeline->priv->resyncing_layers)
1354     return;
1355
1356   timeline->layers = g_list_sort (timeline->layers, (GCompareFunc)
1357       sort_layers);
1358 }
1359
1360 static void
1361 layer_object_removed_cb (GESLayer * layer, GESClip * clip,
1362     GESTimeline * timeline)
1363 {
1364   GList *trackelements, *tmp;
1365
1366   if (ges_clip_is_moving_from_layer (clip)) {
1367     GST_DEBUG ("Clip %p is moving from a layer to another, not doing"
1368         " anything on it", clip);
1369     return;
1370   }
1371
1372   GST_DEBUG ("Clip %p removed from layer %p", clip, layer);
1373
1374   /* Go over the clip's track element and figure out which one belongs to
1375    * the list of tracks we control */
1376
1377   trackelements = ges_container_get_children (GES_CONTAINER (clip), FALSE);
1378   for (tmp = trackelements; tmp; tmp = tmp->next) {
1379     GESTrackElement *track_element = (GESTrackElement *) tmp->data;
1380     GESTrack *track = ges_track_element_get_track (track_element);
1381
1382     if (!track)
1383       continue;
1384
1385     GST_DEBUG_OBJECT (timeline, "Trying to remove TrackElement %p",
1386         track_element);
1387
1388     /* FIXME Check if we should actually check that we control the
1389      * track in the new management of TrackElement context */
1390     LOCK_DYN (timeline);
1391     if (G_LIKELY (g_list_find_custom (timeline->priv->priv_tracks, track,
1392                 (GCompareFunc) custom_find_track) || track == NULL)) {
1393       GST_DEBUG ("Belongs to one of the tracks we control");
1394
1395       ges_track_remove_element (track, track_element);
1396     }
1397     UNLOCK_DYN (timeline);
1398   }
1399   g_signal_handlers_disconnect_by_func (clip, clip_track_element_added_cb,
1400       timeline);
1401   g_signal_handlers_disconnect_by_func (clip, clip_track_element_removed_cb,
1402       timeline);
1403
1404   g_list_free_full (trackelements, gst_object_unref);
1405
1406   GST_DEBUG ("Done");
1407 }
1408
1409 static void
1410 trackelement_start_changed_cb (GESTrackElement * child,
1411     GParamSpec * arg G_GNUC_UNUSED, GESTimeline * timeline)
1412 {
1413   timeline_update_duration (timeline);
1414 }
1415
1416 static void
1417 track_element_added_cb (GESTrack * track, GESTrackElement * track_element,
1418     GESTimeline * timeline)
1419 {
1420   /* Auto transition should be updated before we receive the signal */
1421   g_signal_connect_after (GES_TRACK_ELEMENT (track_element), "notify::start",
1422       G_CALLBACK (trackelement_start_changed_cb), timeline);
1423 }
1424
1425 static void
1426 track_element_removed_cb (GESTrack * track,
1427     GESTrackElement * track_element, GESTimeline * timeline)
1428 {
1429   /* Disconnect all signal handlers */
1430   g_signal_handlers_disconnect_by_func (track_element,
1431       trackelement_start_changed_cb, timeline);
1432 }
1433
1434 static GstPadProbeReturn
1435 _pad_probe_cb (GstPad * mixer_pad, GstPadProbeInfo * info,
1436     GESTimeline * timeline)
1437 {
1438   GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
1439   if (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START) {
1440     LOCK_DYN (timeline);
1441     if (timeline->priv->stream_start_group_id == -1) {
1442       if (!gst_event_parse_group_id (event,
1443               &timeline->priv->stream_start_group_id))
1444         timeline->priv->stream_start_group_id = gst_util_group_id_next ();
1445     }
1446
1447     info->data = gst_event_make_writable (event);
1448     gst_event_set_group_id (GST_PAD_PROBE_INFO_EVENT (info),
1449         timeline->priv->stream_start_group_id);
1450     UNLOCK_DYN (timeline);
1451
1452     return GST_PAD_PROBE_REMOVE;
1453   }
1454
1455   return GST_PAD_PROBE_OK;
1456 }
1457
1458 static void
1459 _ghost_track_srcpad (TrackPrivate * tr_priv)
1460 {
1461   GstPad *pad;
1462   gchar *padname;
1463   gboolean no_more;
1464   GList *tmp;
1465   GESTrack *track = tr_priv->track;
1466
1467   pad = gst_element_get_static_pad (GST_ELEMENT (track), "src");
1468
1469   GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));
1470
1471   /* Remember the pad */
1472   LOCK_DYN (tr_priv->timeline);
1473   GST_OBJECT_LOCK (track);
1474   tr_priv->pad = pad;
1475
1476   no_more = TRUE;
1477   for (tmp = tr_priv->timeline->priv->priv_tracks; tmp; tmp = g_list_next (tmp)) {
1478     TrackPrivate *tr_priv = (TrackPrivate *) tmp->data;
1479
1480     if (!tr_priv->pad) {
1481       GST_LOG ("Found track without pad %p", tr_priv->track);
1482       no_more = FALSE;
1483     }
1484   }
1485   GST_OBJECT_UNLOCK (track);
1486
1487   /* ghost it ! */
1488   GST_DEBUG ("Ghosting pad and adding it to ourself");
1489   padname = g_strdup_printf ("track_%p_src", track);
1490   tr_priv->ghostpad = gst_ghost_pad_new (padname, pad);
1491   g_free (padname);
1492   gst_pad_set_active (tr_priv->ghostpad, TRUE);
1493   gst_element_add_pad (GST_ELEMENT (tr_priv->timeline), tr_priv->ghostpad);
1494
1495   if (no_more) {
1496     GST_DEBUG ("Signaling no-more-pads");
1497     gst_element_no_more_pads (GST_ELEMENT (tr_priv->timeline));
1498   }
1499
1500   tr_priv->probe_id = gst_pad_add_probe (pad,
1501       GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
1502       (GstPadProbeCallback) _pad_probe_cb, tr_priv->timeline, NULL);
1503
1504   UNLOCK_DYN (tr_priv->timeline);
1505 }
1506
1507 gboolean
1508 timeline_add_element (GESTimeline * timeline, GESTimelineElement * element)
1509 {
1510   GESTimelineElement *same_name =
1511       g_hash_table_lookup (timeline->priv->all_elements,
1512       element->name);
1513
1514   GST_DEBUG_OBJECT (timeline, "Adding element: %s", element->name);
1515   if (same_name) {
1516     GST_ERROR_OBJECT (timeline, "%s Already in the timeline %" GST_PTR_FORMAT,
1517         element->name, same_name);
1518     return FALSE;
1519   }
1520
1521   g_hash_table_insert (timeline->priv->all_elements,
1522       ges_timeline_element_get_name (element), gst_object_ref (element));
1523
1524   timeline_tree_track_element (timeline->priv->tree, element);
1525
1526   return TRUE;
1527 }
1528
1529 gboolean
1530 timeline_remove_element (GESTimeline * timeline, GESTimelineElement * element)
1531 {
1532   if (g_hash_table_remove (timeline->priv->all_elements, element->name)) {
1533     timeline_tree_stop_tracking_element (timeline->priv->tree, element);
1534
1535     return TRUE;
1536   }
1537
1538   return FALSE;
1539 }
1540
1541 void
1542 timeline_fill_gaps (GESTimeline * timeline)
1543 {
1544   GList *tmp;
1545
1546   for (tmp = timeline->tracks; tmp; tmp = tmp->next) {
1547     track_resort_and_fill_gaps (tmp->data);
1548   }
1549 }
1550
1551 GNode *
1552 timeline_get_tree (GESTimeline * timeline)
1553 {
1554   return timeline->priv->tree;
1555 }
1556
1557 /**** API *****/
1558 /**
1559  * ges_timeline_new:
1560  *
1561  * Creates a new empty #GESTimeline.
1562  *
1563  * Returns: (transfer floating): The new timeline.
1564  */
1565
1566 GESTimeline *
1567 ges_timeline_new (void)
1568 {
1569   GESProject *project = ges_project_new (NULL);
1570   GESExtractable *timeline = g_object_new (GES_TYPE_TIMELINE, NULL);
1571
1572   ges_extractable_set_asset (timeline, GES_ASSET (project));
1573   gst_object_unref (project);
1574
1575   return GES_TIMELINE (timeline);
1576 }
1577
1578 /**
1579  * ges_timeline_new_from_uri:
1580  * @uri: the URI to load from
1581  * @error: (out) (allow-none): An error to be set in case something wrong happens or %NULL
1582  *
1583  * Creates a timeline from the given URI.
1584  *
1585  * Returns: (transfer floating) (nullable): A new timeline if the uri was loaded
1586  * successfully, or %NULL if the uri could not be loaded.
1587  */
1588 GESTimeline *
1589 ges_timeline_new_from_uri (const gchar * uri, GError ** error)
1590 {
1591   GESTimeline *ret;
1592   GESProject *project = ges_project_new (uri);
1593
1594   ret = GES_TIMELINE (ges_asset_extract (GES_ASSET (project), error));
1595   gst_object_unref (project);
1596
1597   return ret;
1598 }
1599
1600 /**
1601  * ges_timeline_load_from_uri:
1602  * @timeline: an empty #GESTimeline into which to load the formatter
1603  * @uri: The URI to load from
1604  * @error: (out) (allow-none): An error to be set in case something wrong happens or %NULL
1605  *
1606  * Loads the contents of URI into the given timeline.
1607  *
1608  * Returns: %TRUE if the timeline was loaded successfully, or %FALSE if the uri
1609  * could not be loaded.
1610  */
1611 gboolean
1612 ges_timeline_load_from_uri (GESTimeline * timeline, const gchar * uri,
1613     GError ** error)
1614 {
1615   GESProject *project;
1616   gboolean ret = FALSE;
1617
1618   g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
1619   g_return_val_if_fail ((ges_extractable_get_asset (GES_EXTRACTABLE
1620               (timeline)) == NULL), FALSE);
1621
1622   project = ges_project_new (uri);
1623   ret = ges_project_load (project, timeline, error);
1624   gst_object_unref (project);
1625
1626   return ret;
1627 }
1628
1629 /**
1630  * ges_timeline_save_to_uri:
1631  * @timeline: a #GESTimeline
1632  * @uri: The location to save to
1633  * @formatter_asset: (allow-none): The formatter asset to use or %NULL. If %NULL,
1634  * will try to save in the same format as the one from which the timeline as been loaded
1635  * or default to the formatter with highest rank
1636  * @overwrite: %TRUE to overwrite file if it exists
1637  * @error: (out) (allow-none): An error to be set in case something wrong happens or %NULL
1638  *
1639  * Saves the timeline to the given location
1640  *
1641  * Returns: %TRUE if the timeline was successfully saved to the given location,
1642  * else %FALSE.
1643  */
1644 gboolean
1645 ges_timeline_save_to_uri (GESTimeline * timeline, const gchar * uri,
1646     GESAsset * formatter_asset, gboolean overwrite, GError ** error)
1647 {
1648   GESProject *project;
1649
1650   gboolean ret, created_proj = FALSE;
1651
1652   g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
1653   project =
1654       GES_PROJECT (ges_extractable_get_asset (GES_EXTRACTABLE (timeline)));
1655
1656   if (project == NULL) {
1657     project = ges_project_new (NULL);
1658     created_proj = TRUE;
1659   }
1660
1661   ret = ges_project_save (project, timeline, uri, formatter_asset, overwrite,
1662       error);
1663
1664   if (created_proj)
1665     gst_object_unref (project);
1666
1667   return ret;
1668 }
1669
1670 /**
1671  * ges_timeline_get_groups:
1672  * @timeline: a #GESTimeline
1673  *
1674  * Get the list of #GESGroup present in the Timeline.
1675  *
1676  * Returns: (transfer none) (element-type GESGroup): the list of
1677  * #GESGroup that contain clips present in the timeline's layers.
1678  * Must not be changed.
1679  */
1680 GList *
1681 ges_timeline_get_groups (GESTimeline * timeline)
1682 {
1683   g_return_val_if_fail (GES_IS_TIMELINE (timeline), NULL);
1684   CHECK_THREAD (timeline);
1685
1686   return timeline->priv->groups;
1687 }
1688
1689 /**
1690  * ges_timeline_append_layer:
1691  * @timeline: a #GESTimeline
1692  *
1693  * Append a newly created #GESLayer to @timeline
1694  * Note that you do not own any reference to the returned layer.
1695  *
1696  * Returns: (transfer none): The newly created #GESLayer, or the last (empty)
1697  * #GESLayer of @timeline.
1698  */
1699 GESLayer *
1700 ges_timeline_append_layer (GESTimeline * timeline)
1701 {
1702   guint32 priority;
1703   GESLayer *layer;
1704
1705   g_return_val_if_fail (GES_IS_TIMELINE (timeline), NULL);
1706   CHECK_THREAD (timeline);
1707
1708   layer = ges_layer_new ();
1709   priority = g_list_length (timeline->layers);
1710   ges_layer_set_priority (layer, priority);
1711
1712   ges_timeline_add_layer (timeline, layer);
1713
1714   return layer;
1715 }
1716
1717 /**
1718  * ges_timeline_add_layer:
1719  * @timeline: a #GESTimeline
1720  * @layer: (transfer floating): the #GESLayer to add
1721  *
1722  * Add the layer to the timeline. The reference to the @layer will be stolen
1723  * by the @timeline.
1724  *
1725  * Returns: %TRUE if the layer was properly added, else %FALSE.
1726  */
1727 gboolean
1728 ges_timeline_add_layer (GESTimeline * timeline, GESLayer * layer)
1729 {
1730   gboolean auto_transition;
1731   GList *objects, *tmp;
1732
1733   g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
1734   g_return_val_if_fail (GES_IS_LAYER (layer), FALSE);
1735   CHECK_THREAD (timeline);
1736
1737   GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
1738
1739   /* We can only add a layer that doesn't already belong to another timeline */
1740   if (G_UNLIKELY (layer->timeline)) {
1741     GST_WARNING ("Layer belongs to another timeline, can't add it");
1742     gst_object_ref_sink (layer);
1743     gst_object_unref (layer);
1744     return FALSE;
1745   }
1746
1747   /* Add to the list of layers, make sure we don't already control it */
1748   if (G_UNLIKELY (g_list_find (timeline->layers, (gconstpointer) layer))) {
1749     GST_WARNING ("Layer is already controlled by this timeline");
1750     gst_object_ref_sink (layer);
1751     gst_object_unref (layer);
1752     return FALSE;
1753   }
1754
1755   auto_transition = ges_layer_get_auto_transition (layer);
1756
1757   /* If the user doesn't explicitely set layer auto_transition, then set our */
1758   if (!auto_transition) {
1759     auto_transition = ges_timeline_get_auto_transition (timeline);
1760     ges_layer_set_auto_transition (layer, auto_transition);
1761   }
1762
1763   gst_object_ref_sink (layer);
1764   timeline->layers = g_list_insert_sorted (timeline->layers, layer,
1765       (GCompareFunc) sort_layers);
1766
1767   /* Inform the layer that it belongs to a new timeline */
1768   ges_layer_set_timeline (layer, timeline);
1769
1770   /* Connect to 'clip-added'/'clip-removed' signal from the new layer */
1771   g_signal_connect_after (layer, "clip-added",
1772       G_CALLBACK (layer_object_added_cb), timeline);
1773   g_signal_connect_after (layer, "clip-removed",
1774       G_CALLBACK (layer_object_removed_cb), timeline);
1775   g_signal_connect (layer, "notify::priority",
1776       G_CALLBACK (layer_priority_changed_cb), timeline);
1777   g_signal_connect (layer, "notify::auto-transition",
1778       G_CALLBACK (layer_auto_transition_changed_cb), timeline);
1779
1780   GST_DEBUG ("Done adding layer, emitting 'layer-added' signal");
1781   g_signal_emit (timeline, ges_timeline_signals[LAYER_ADDED], 0, layer);
1782
1783   /* add any existing clips to the timeline */
1784   objects = ges_layer_get_clips (layer);
1785   for (tmp = objects; tmp; tmp = tmp->next) {
1786     layer_object_added_cb (layer, tmp->data, timeline);
1787     gst_object_unref (tmp->data);
1788     tmp->data = NULL;
1789   }
1790   g_list_free (objects);
1791
1792   return TRUE;
1793 }
1794
1795 /**
1796  * ges_timeline_remove_layer:
1797  * @timeline: a #GESTimeline
1798  * @layer: the #GESLayer to remove
1799  *
1800  * Removes the layer from the timeline. The reference that the @timeline holds on
1801  * the layer will be dropped. If you wish to use the @layer after calling this
1802  * method, you need to take a reference before calling.
1803  *
1804  * Returns: %TRUE if the layer was properly removed, else %FALSE.
1805  */
1806
1807 gboolean
1808 ges_timeline_remove_layer (GESTimeline * timeline, GESLayer * layer)
1809 {
1810   GList *layer_objects, *tmp;
1811
1812   g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
1813   g_return_val_if_fail (GES_IS_LAYER (layer), FALSE);
1814   CHECK_THREAD (timeline);
1815
1816   GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
1817
1818   if (G_UNLIKELY (!g_list_find (timeline->layers, layer))) {
1819     GST_WARNING ("Layer doesn't belong to this timeline");
1820     return FALSE;
1821   }
1822
1823   /* remove objects from any private data structures */
1824
1825   layer_objects = ges_layer_get_clips (layer);
1826   for (tmp = layer_objects; tmp; tmp = tmp->next) {
1827     layer_object_removed_cb (layer, GES_CLIP (tmp->data), timeline);
1828     gst_object_unref (G_OBJECT (tmp->data));
1829     tmp->data = NULL;
1830   }
1831   g_list_free (layer_objects);
1832
1833   /* Disconnect signals */
1834   GST_DEBUG ("Disconnecting signal callbacks");
1835   g_signal_handlers_disconnect_by_func (layer, layer_object_added_cb, timeline);
1836   g_signal_handlers_disconnect_by_func (layer, layer_object_removed_cb,
1837       timeline);
1838   g_signal_handlers_disconnect_by_func (layer, layer_priority_changed_cb,
1839       timeline);
1840   g_signal_handlers_disconnect_by_func (layer,
1841       layer_auto_transition_changed_cb, timeline);
1842
1843   timeline->layers = g_list_remove (timeline->layers, layer);
1844   ges_layer_set_timeline (layer, NULL);
1845
1846   g_signal_emit (timeline, ges_timeline_signals[LAYER_REMOVED], 0, layer);
1847
1848   gst_object_unref (layer);
1849
1850   return TRUE;
1851 }
1852
1853 /**
1854  * ges_timeline_add_track:
1855  * @timeline: a #GESTimeline
1856  * @track: (transfer full): the #GESTrack to add
1857  *
1858  * Add a track to the timeline. The reference to the track will be stolen by the
1859  * pipeline.
1860  *
1861  * Returns: %TRUE if the track was properly added, else %FALSE.
1862  */
1863
1864 /* FIXME: create track elements for clips which have already been
1865  * added to existing layers.
1866  */
1867
1868 gboolean
1869 ges_timeline_add_track (GESTimeline * timeline, GESTrack * track)
1870 {
1871   TrackPrivate *tr_priv;
1872   GList *tmp;
1873
1874   g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
1875   g_return_val_if_fail (GES_IS_TRACK (track), FALSE);
1876   CHECK_THREAD (timeline);
1877
1878   GST_DEBUG ("timeline:%p, track:%p", timeline, track);
1879
1880   /* make sure we don't already control it */
1881   if (G_UNLIKELY (g_list_find (timeline->tracks, (gconstpointer) track))) {
1882     GST_WARNING ("Track is already controlled by this timeline");
1883     return FALSE;
1884   }
1885
1886   /* Add the track to ourself (as a GstBin)
1887    * Reference is stolen ! */
1888   if (G_UNLIKELY (!gst_bin_add (GST_BIN (timeline), GST_ELEMENT (track)))) {
1889     GST_WARNING ("Couldn't add track to ourself (GST)");
1890     return FALSE;
1891   }
1892
1893   tr_priv = g_new0 (TrackPrivate, 1);
1894   tr_priv->timeline = timeline;
1895   tr_priv->track = track;
1896
1897   /* Add the track to the list of tracks we track */
1898   LOCK_DYN (timeline);
1899   timeline->priv->priv_tracks = g_list_append (timeline->priv->priv_tracks,
1900       tr_priv);
1901   UNLOCK_DYN (timeline);
1902   timeline->tracks = g_list_append (timeline->tracks, track);
1903
1904   /* Inform the track that it's currently being used by ourself */
1905   ges_track_set_timeline (track, timeline);
1906
1907   GST_DEBUG ("Done adding track, emitting 'track-added' signal");
1908
1909   _ghost_track_srcpad (tr_priv);
1910
1911   /* emit 'track-added' */
1912   g_signal_emit (timeline, ges_timeline_signals[TRACK_ADDED], 0, track);
1913
1914   /* ensure that each existing clip has the opportunity to create a
1915    * track element for this track*/
1916
1917   /* We connect to the object for the timeline editing mode management */
1918   g_signal_connect (G_OBJECT (track), "track-element-added",
1919       G_CALLBACK (track_element_added_cb), timeline);
1920   g_signal_connect (G_OBJECT (track), "track-element-removed",
1921       G_CALLBACK (track_element_removed_cb), timeline);
1922
1923   for (tmp = timeline->layers; tmp; tmp = tmp->next) {
1924     GList *objects, *obj;
1925     objects = ges_layer_get_clips (tmp->data);
1926
1927     for (obj = objects; obj; obj = obj->next) {
1928       GESClip *clip = obj->data;
1929
1930       add_object_to_tracks (timeline, clip, track);
1931       gst_object_unref (clip);
1932     }
1933     g_list_free (objects);
1934   }
1935
1936   /* FIXME Check if we should rollback if we can't sync state */
1937   gst_element_sync_state_with_parent (GST_ELEMENT (track));
1938   g_object_set (track, "message-forward", TRUE, NULL);
1939
1940   return TRUE;
1941 }
1942
1943 /**
1944  * ges_timeline_remove_track:
1945  * @timeline: a #GESTimeline
1946  * @track: the #GESTrack to remove
1947  *
1948  * Remove the @track from the @timeline. The reference stolen when adding the
1949  * @track will be removed. If you wish to use the @track after calling this
1950  * function you must ensure that you have a reference to it.
1951  *
1952  * Returns: %TRUE if the @track was properly removed, else %FALSE.
1953  */
1954
1955 /* FIXME: release any track elements associated with this layer. currenly this
1956  * will not happen if you remove the track before removing *all*
1957  * clips which have a track element in this track.
1958  */
1959
1960 gboolean
1961 ges_timeline_remove_track (GESTimeline * timeline, GESTrack * track)
1962 {
1963   GList *tmp;
1964   TrackPrivate *tr_priv;
1965   GESTimelinePrivate *priv;
1966
1967   g_return_val_if_fail (GES_IS_TRACK (track), FALSE);
1968   g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
1969   CHECK_THREAD (timeline);
1970
1971   GST_DEBUG ("timeline:%p, track:%p", timeline, track);
1972
1973   priv = timeline->priv;
1974   LOCK_DYN (timeline);
1975   if (G_UNLIKELY (!(tmp = g_list_find_custom (priv->priv_tracks,
1976                   track, (GCompareFunc) custom_find_track)))) {
1977     GST_WARNING ("Track doesn't belong to this timeline");
1978     UNLOCK_DYN (timeline);
1979     return FALSE;
1980   }
1981
1982   tr_priv = tmp->data;
1983   gst_object_unref (tr_priv->pad);
1984   priv->priv_tracks = g_list_remove (priv->priv_tracks, tr_priv);
1985   UNLOCK_DYN (timeline);
1986   timeline->tracks = g_list_remove (timeline->tracks, track);
1987
1988   ges_track_set_timeline (track, NULL);
1989
1990   /* Remove ghost pad */
1991   if (tr_priv->ghostpad) {
1992     GST_DEBUG ("Removing ghostpad");
1993     gst_pad_set_active (tr_priv->ghostpad, FALSE);
1994     gst_ghost_pad_set_target ((GstGhostPad *) tr_priv->ghostpad, NULL);
1995     gst_element_remove_pad (GST_ELEMENT (timeline), tr_priv->ghostpad);
1996   }
1997
1998   /* Remove pad-added/-removed handlers */
1999   g_signal_handlers_disconnect_by_func (track, track_element_added_cb,
2000       timeline);
2001   g_signal_handlers_disconnect_by_func (track, track_element_removed_cb,
2002       timeline);
2003
2004   /* Signal track removal to all layers/objects */
2005   g_signal_emit (timeline, ges_timeline_signals[TRACK_REMOVED], 0, track);
2006
2007   /* remove track from our bin */
2008   gst_object_ref (track);
2009   if (G_UNLIKELY (!gst_bin_remove (GST_BIN (timeline), GST_ELEMENT (track)))) {
2010     GST_WARNING ("Couldn't remove track to ourself (GST)");
2011     gst_object_unref (track);
2012     return FALSE;
2013   }
2014
2015   /* set track state to NULL */
2016   gst_element_set_state (GST_ELEMENT (track), GST_STATE_NULL);
2017
2018   gst_object_unref (track);
2019
2020   g_free (tr_priv);
2021
2022   return TRUE;
2023 }
2024
2025 /**
2026  * ges_timeline_get_track_for_pad:
2027  * @timeline: The #GESTimeline
2028  * @pad: The #GstPad
2029  *
2030  * Search the #GESTrack corresponding to the given @timeline's @pad.
2031  *
2032  * Returns: (transfer none) (nullable): The corresponding #GESTrack if it is
2033  * found, or %NULL if there is an error.
2034  */
2035
2036 GESTrack *
2037 ges_timeline_get_track_for_pad (GESTimeline * timeline, GstPad * pad)
2038 {
2039   GList *tmp;
2040
2041   g_return_val_if_fail (GES_IS_TIMELINE (timeline), NULL);
2042
2043   LOCK_DYN (timeline);
2044   for (tmp = timeline->priv->priv_tracks; tmp; tmp = g_list_next (tmp)) {
2045     TrackPrivate *tr_priv = (TrackPrivate *) tmp->data;
2046     if (pad == tr_priv->ghostpad) {
2047       UNLOCK_DYN (timeline);
2048       return tr_priv->track;
2049     }
2050   }
2051   UNLOCK_DYN (timeline);
2052
2053   return NULL;
2054 }
2055
2056 /**
2057  * ges_timeline_get_pad_for_track:
2058  * @timeline: The #GESTimeline
2059  * @track: The #GESTrack
2060  *
2061  * Search the #GstPad corresponding to the given @timeline's @track.
2062  *
2063  * Returns: (transfer none) (nullable): The corresponding #GstPad if it is
2064  * found, or %NULL if there is an error.
2065  */
2066
2067 GstPad *
2068 ges_timeline_get_pad_for_track (GESTimeline * timeline, GESTrack * track)
2069 {
2070   GList *tmp;
2071
2072   LOCK_DYN (timeline);
2073   for (tmp = timeline->priv->priv_tracks; tmp; tmp = g_list_next (tmp)) {
2074     TrackPrivate *tr_priv = (TrackPrivate *) tmp->data;
2075
2076     if (track == tr_priv->track) {
2077       if (tr_priv->ghostpad)
2078         gst_object_ref (tr_priv->ghostpad);
2079
2080       UNLOCK_DYN (timeline);
2081       return tr_priv->ghostpad;
2082     }
2083   }
2084   UNLOCK_DYN (timeline);
2085
2086   return NULL;
2087 }
2088
2089 /**
2090  * ges_timeline_get_tracks:
2091  * @timeline: a #GESTimeline
2092  *
2093  * Returns the list of #GESTrack used by the Timeline.
2094  *
2095  * Returns: (transfer full) (element-type GESTrack): A list of #GESTrack.
2096  * The caller should unref each track once he is done with them.
2097  */
2098 GList *
2099 ges_timeline_get_tracks (GESTimeline * timeline)
2100 {
2101   g_return_val_if_fail (GES_IS_TIMELINE (timeline), NULL);
2102   CHECK_THREAD (timeline);
2103
2104   return g_list_copy_deep (timeline->tracks, (GCopyFunc) gst_object_ref, NULL);
2105 }
2106
2107 /**
2108  * ges_timeline_get_layers:
2109  * @timeline: a #GESTimeline
2110  *
2111  * Get the list of #GESLayer present in the Timeline.
2112  *
2113  * Returns: (transfer full) (element-type GESLayer): the list of
2114  * #GESLayer present in the Timeline sorted by priority.
2115  * The caller should unref each Layer once he is done with them.
2116  */
2117 GList *
2118 ges_timeline_get_layers (GESTimeline * timeline)
2119 {
2120   GList *tmp, *res = NULL;
2121
2122   g_return_val_if_fail (GES_IS_TIMELINE (timeline), NULL);
2123   CHECK_THREAD (timeline);
2124
2125   for (tmp = timeline->layers; tmp; tmp = g_list_next (tmp)) {
2126     res = g_list_insert_sorted (res, gst_object_ref (tmp->data),
2127         (GCompareFunc) sort_layers);
2128   }
2129
2130   return res;
2131 }
2132
2133 static void
2134 track_commited_cb (GESTrack * track, GESTimeline * timeline)
2135 {
2136   gboolean emit_commited = FALSE;
2137   GST_OBJECT_LOCK (timeline);
2138   timeline->priv->expected_commited -= 1;
2139   if (timeline->priv->expected_commited == 0)
2140     emit_commited = TRUE;
2141   g_signal_handlers_disconnect_by_func (track, track_commited_cb, timeline);
2142   GST_OBJECT_UNLOCK (timeline);
2143
2144   if (emit_commited) {
2145     g_signal_emit (timeline, ges_timeline_signals[COMMITED], 0);
2146   }
2147 }
2148
2149 /* Must be called with the timeline's DYN_LOCK */
2150 static gboolean
2151 ges_timeline_commit_unlocked (GESTimeline * timeline)
2152 {
2153   GList *tmp;
2154   gboolean res = TRUE;
2155
2156   GST_DEBUG_OBJECT (timeline, "commiting changes");
2157
2158   timeline_tree_create_transitions (timeline->priv->tree,
2159       ges_timeline_find_auto_transition);
2160   for (tmp = timeline->layers; tmp; tmp = tmp->next) {
2161     GESLayer *layer = tmp->data;
2162
2163     /* Ensure clip priorities are correct after an edit */
2164     ges_layer_resync_priorities (layer);
2165   }
2166
2167   timeline->priv->expected_commited =
2168       g_list_length (timeline->priv->priv_tracks);
2169
2170   if (timeline->priv->expected_commited == 0) {
2171     g_signal_emit (timeline, ges_timeline_signals[COMMITED], 0);
2172   } else {
2173     for (tmp = timeline->tracks; tmp; tmp = tmp->next) {
2174       g_signal_connect (tmp->data, "commited", G_CALLBACK (track_commited_cb),
2175           timeline);
2176       if (!ges_track_commit (GES_TRACK (tmp->data)))
2177         res = FALSE;
2178     }
2179   }
2180
2181   return res;
2182 }
2183
2184 /**
2185  * ges_timeline_commit:
2186  * @timeline: a #GESTimeline
2187  *
2188  * Commit all the pending changes of the clips contained in the
2189  * @timeline.
2190  *
2191  * When changes happen in a timeline, they are not
2192  * directly executed in the non-linear engine. Call this method once you are
2193  * done with a set of changes and want it to be executed.
2194  *
2195  * The #GESTimeline::commited signal will be emitted when the (possibly updated)
2196  * #GstPipeline is ready to output data again, except if the state of the
2197  * timeline was #GST_STATE_READY or #GST_STATE_NULL.
2198  *
2199  * Note that all the pending changes will automatically be executed when the
2200  * timeline goes from #GST_STATE_READY to #GST_STATE_PAUSED, which usually is
2201  * triggered by corresponding state changes in a containing #GESPipeline.
2202  *
2203  * You should not try to change the state of the timeline, seek it or add
2204  * tracks to it during a commit operation, that is between a call to this
2205  * function and after receiving the #GESTimeline::commited signal.
2206  *
2207  * See #ges_timeline_commit_sync if you don't want to bother with waiting
2208  * for the signal.
2209  *
2210  * Returns: %TRUE if pending changes were commited or %FALSE if nothing needed
2211  * to be commited
2212  */
2213 gboolean
2214 ges_timeline_commit (GESTimeline * timeline)
2215 {
2216   gboolean ret;
2217
2218   g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
2219
2220   LOCK_DYN (timeline);
2221   ret = ges_timeline_commit_unlocked (timeline);
2222   UNLOCK_DYN (timeline);
2223
2224   ges_timeline_emit_snapping (timeline, NULL, NULL, GST_CLOCK_TIME_NONE);
2225   return ret;
2226 }
2227
2228 static void
2229 commited_cb (GESTimeline * timeline)
2230 {
2231   g_mutex_lock (&timeline->priv->commited_lock);
2232   g_cond_signal (&timeline->priv->commited_cond);
2233   g_mutex_unlock (&timeline->priv->commited_lock);
2234 }
2235
2236 /**
2237  * ges_timeline_commit_sync:
2238  * @timeline: a #GESTimeline
2239  *
2240  * Commit all the pending changes of the #GESClips contained in the
2241  * @timeline.
2242  *
2243  * Will return once the update is complete, that is when the
2244  * (possibly updated) #GstPipeline is ready to output data again, or if the
2245  * state of the timeline was #GST_STATE_READY or #GST_STATE_NULL.
2246  *
2247  * This function will wait for any pending state change of the timeline by
2248  * calling #gst_element_get_state with a #GST_CLOCK_TIME_NONE timeout, you
2249  * should not try to change the state from another thread before this function
2250  * has returned.
2251  *
2252  * See #ges_timeline_commit for more information.
2253  *
2254  * Returns: %TRUE if pending changes were commited or %FALSE if nothing needed
2255  * to be commited
2256  */
2257 gboolean
2258 ges_timeline_commit_sync (GESTimeline * timeline)
2259 {
2260   gboolean ret;
2261   gboolean wait_for_signal;
2262
2263   g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
2264
2265   /* Let's make sure our state is stable */
2266   gst_element_get_state (GST_ELEMENT (timeline), NULL, NULL,
2267       GST_CLOCK_TIME_NONE);
2268
2269   /* Let's make sure no track gets added between now and the actual commiting */
2270   LOCK_DYN (timeline);
2271   wait_for_signal = g_list_length (timeline->priv->priv_tracks) > 0
2272       && GST_STATE (timeline) >= GST_STATE_PAUSED;
2273
2274   if (!wait_for_signal) {
2275     ret = ges_timeline_commit_unlocked (timeline);
2276   } else {
2277     gulong handler_id =
2278         g_signal_connect (timeline, "commited", (GCallback) commited_cb, NULL);
2279
2280     g_mutex_lock (&timeline->priv->commited_lock);
2281
2282     ret = ges_timeline_commit_unlocked (timeline);
2283     g_cond_wait (&timeline->priv->commited_cond,
2284         &timeline->priv->commited_lock);
2285     g_mutex_unlock (&timeline->priv->commited_lock);
2286     g_signal_handler_disconnect (timeline, handler_id);
2287   }
2288
2289   UNLOCK_DYN (timeline);
2290
2291   return ret;
2292 }
2293
2294 /**
2295  * ges_timeline_get_duration:
2296  * @timeline: a #GESTimeline
2297  *
2298  * Get the current duration of @timeline
2299  *
2300  * Returns: The current duration of @timeline
2301  */
2302 GstClockTime
2303 ges_timeline_get_duration (GESTimeline * timeline)
2304 {
2305   g_return_val_if_fail (GES_IS_TIMELINE (timeline), GST_CLOCK_TIME_NONE);
2306   CHECK_THREAD (timeline);
2307
2308   return timeline->priv->duration;
2309 }
2310
2311 /**
2312  * ges_timeline_get_auto_transition:
2313  * @timeline: a #GESTimeline
2314  *
2315  * Gets whether transitions are automatically added when objects
2316  * overlap or not.
2317  *
2318  * Returns: %TRUE if transitions are automatically added, else %FALSE.
2319  */
2320 gboolean
2321 ges_timeline_get_auto_transition (GESTimeline * timeline)
2322 {
2323   g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
2324   CHECK_THREAD (timeline);
2325
2326   return timeline->priv->auto_transition;
2327 }
2328
2329 /**
2330  * ges_timeline_set_auto_transition:
2331  * @timeline: a #GESLayer
2332  * @auto_transition: whether the auto_transition is active
2333  *
2334  * Sets the layer to the given @auto_transition. See the documentation of the
2335  * property auto_transition for more information.
2336  */
2337 void
2338 ges_timeline_set_auto_transition (GESTimeline * timeline,
2339     gboolean auto_transition)
2340 {
2341   GList *layers;
2342   GESLayer *layer;
2343
2344   g_return_if_fail (GES_IS_TIMELINE (timeline));
2345   CHECK_THREAD (timeline);
2346
2347   timeline->priv->auto_transition = auto_transition;
2348   g_object_notify (G_OBJECT (timeline), "auto-transition");
2349
2350   layers = timeline->layers;
2351   for (; layers; layers = layers->next) {
2352     layer = layers->data;
2353     ges_layer_set_auto_transition (layer, auto_transition);
2354   }
2355 }
2356
2357 /**
2358  * ges_timeline_get_snapping_distance:
2359  * @timeline: a #GESTimeline
2360  *
2361  * Gets the configured snapping distance of the timeline. See
2362  * the documentation of the property snapping_distance for more
2363  * information.
2364  *
2365  * Returns: The @snapping_distance property of the timeline
2366  */
2367 GstClockTime
2368 ges_timeline_get_snapping_distance (GESTimeline * timeline)
2369 {
2370   g_return_val_if_fail (GES_IS_TIMELINE (timeline), GST_CLOCK_TIME_NONE);
2371   CHECK_THREAD (timeline);
2372
2373   return timeline->priv->snapping_distance;
2374
2375 }
2376
2377 /**
2378  * ges_timeline_set_snapping_distance:
2379  * @timeline: a #GESLayer
2380  * @snapping_distance: whether the snapping_distance is active
2381  *
2382  * Sets the @snapping_distance of the timeline. See the documentation of the
2383  * property snapping_distance for more information.
2384  */
2385 void
2386 ges_timeline_set_snapping_distance (GESTimeline * timeline,
2387     GstClockTime snapping_distance)
2388 {
2389   g_return_if_fail (GES_IS_TIMELINE (timeline));
2390   CHECK_THREAD (timeline);
2391
2392   timeline->priv->snapping_distance = snapping_distance;
2393 }
2394
2395 /**
2396  * ges_timeline_get_element:
2397  * @timeline: a #GESTimeline
2398  *
2399  * Gets a #GESTimelineElement contained in the timeline
2400  *
2401  * Returns: (transfer full) (nullable): The #GESTimelineElement or %NULL if
2402  * not found.
2403  */
2404 GESTimelineElement *
2405 ges_timeline_get_element (GESTimeline * timeline, const gchar * name)
2406 {
2407   GESTimelineElement *ret;
2408
2409   g_return_val_if_fail (GES_IS_TIMELINE (timeline), NULL);
2410   CHECK_THREAD (timeline);
2411
2412   ret = g_hash_table_lookup (timeline->priv->all_elements, name);
2413
2414   if (ret)
2415     return gst_object_ref (ret);
2416
2417 #ifndef GST_DISABLE_GST_DEBUG
2418   {
2419     GList *element_names, *tmp;
2420     element_names = g_hash_table_get_keys (timeline->priv->all_elements);
2421
2422     GST_INFO_OBJECT (timeline, "Does not contain element %s", name);
2423
2424     for (tmp = element_names; tmp; tmp = tmp->next) {
2425       GST_DEBUG_OBJECT (timeline, "Containes: %s", (gchar *) tmp->data);
2426     }
2427     g_list_free (element_names);
2428   }
2429 #endif
2430
2431   return NULL;
2432 }
2433
2434 /**
2435  * ges_timeline_is_empty:
2436  * @timeline: a #GESTimeline
2437  *
2438  * Check whether a #GESTimeline is empty or not
2439  *
2440  * Returns: %TRUE if the timeline is empty %FALSE otherwize
2441  */
2442 gboolean
2443 ges_timeline_is_empty (GESTimeline * timeline)
2444 {
2445   GHashTableIter iter;
2446   gpointer key, value;
2447
2448   g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
2449   CHECK_THREAD (timeline);
2450
2451   if (g_hash_table_size (timeline->priv->all_elements) == 0)
2452     return TRUE;
2453
2454   g_hash_table_iter_init (&iter, timeline->priv->all_elements);
2455   while (g_hash_table_iter_next (&iter, &key, &value)) {
2456     if (GES_IS_SOURCE (value) &&
2457         ges_track_element_is_active (GES_TRACK_ELEMENT (value)))
2458       return FALSE;
2459   }
2460
2461   return TRUE;
2462 }
2463
2464 /**
2465  * ges_timeline_get_layer:
2466  * @timeline: The #GESTimeline to retrive a layer from
2467  * @priority: The priority of the layer to find
2468  *
2469  * Retrieve the layer with @priority as a priority
2470  *
2471  * Returns: (transfer full) (nullable): A #GESLayer or %NULL if no layer with
2472  * @priority was found
2473  *
2474  * Since 1.6
2475  */
2476 GESLayer *
2477 ges_timeline_get_layer (GESTimeline * timeline, guint priority)
2478 {
2479   GList *tmp;
2480   GESLayer *layer = NULL;
2481
2482   g_return_val_if_fail (GES_IS_TIMELINE (timeline), NULL);
2483   CHECK_THREAD (timeline);
2484
2485   for (tmp = timeline->layers; tmp; tmp = tmp->next) {
2486     GESLayer *tmp_layer = GES_LAYER (tmp->data);
2487     guint tmp_priority;
2488
2489     g_object_get (tmp_layer, "priority", &tmp_priority, NULL);
2490     if (tmp_priority == priority) {
2491       layer = gst_object_ref (tmp_layer);
2492       break;
2493     }
2494   }
2495
2496   return layer;
2497 }
2498
2499 /**
2500  * ges_timeline_paste_element:
2501  * @timeline: The #GESTimeline onto which the #GESTimelineElement should be pasted
2502  * @element: The #GESTimelineElement to paste
2503  * @position: The position in the timeline the element should
2504  * be pasted to, meaning it will become the start of @element
2505  * @layer_priority: The #GESLayer to which the element should be pasted to.
2506  * -1 means paste to the same layer from which the @element has been copied from.
2507  *
2508  * Paste @element inside the timeline. @element must have been
2509  * created using ges_timeline_element_copy with deep=TRUE set,
2510  * i.e. it must be a deep copy, otherwise it will fail.
2511  *
2512  * Returns: (transfer none): Shallow copy of the @element pasted
2513  */
2514 GESTimelineElement *
2515 ges_timeline_paste_element (GESTimeline * timeline,
2516     GESTimelineElement * element, GstClockTime position, gint layer_priority)
2517 {
2518   GESTimelineElement *res, *copied_from;
2519   GESTimelineElementClass *element_class;
2520
2521   g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
2522   g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (element), FALSE);
2523   CHECK_THREAD (timeline);
2524
2525   element_class = GES_TIMELINE_ELEMENT_GET_CLASS (element);
2526   copied_from = ges_timeline_element_get_copied_from (element);
2527
2528   if (!copied_from) {
2529     GST_ERROR_OBJECT (element, "Is not being 'deeply' copied!");
2530
2531     return NULL;
2532   }
2533
2534   if (!element_class->paste) {
2535     GST_ERROR_OBJECT (element, "No paste vmethod implemented");
2536
2537     return NULL;
2538   }
2539
2540   /*
2541    * Currently the API only supports pasting onto the same layer from which
2542    * the @element has been copied from, i.e., @layer_priority needs to be -1.
2543    */
2544   if (layer_priority != -1) {
2545     GST_WARNING_OBJECT (timeline,
2546         "Only -1 value for layer priority is supported");
2547   }
2548
2549   res = element_class->paste (element, copied_from, position);
2550
2551   g_clear_object (&copied_from);
2552
2553   return g_object_ref (res);
2554 }
2555
2556 /**
2557  * ges_timeline_move_layer:
2558  * @timeline: The timeline in which @layer must be
2559  * @layer: The layer to move at @new_layer_priority
2560  * @new_layer_priority: The index at which @layer should land
2561  *
2562  * Moves @layer at @new_layer_priority meaning that @layer
2563  * we land at that position in the stack of layers inside
2564  * the timeline. If @new_layer_priority is superior than the number
2565  * of layers present in the time, it will move to the end of the
2566  * stack of layers.
2567  */
2568 gboolean
2569 ges_timeline_move_layer (GESTimeline * timeline, GESLayer * layer,
2570     guint new_layer_priority)
2571 {
2572   gint current_priority;
2573
2574   g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
2575   g_return_val_if_fail (GES_IS_LAYER (layer), FALSE);
2576   g_return_val_if_fail (ges_layer_get_timeline (layer) == timeline, FALSE);
2577   CHECK_THREAD (timeline);
2578
2579   current_priority = ges_layer_get_priority (layer);
2580
2581   if (new_layer_priority == current_priority) {
2582     GST_DEBUG_OBJECT (timeline,
2583         "Nothing to do for %" GST_PTR_FORMAT ", same priorities", layer);
2584
2585     return TRUE;
2586   }
2587
2588   timeline->layers = g_list_remove (timeline->layers, layer);
2589   timeline->layers = g_list_insert (timeline->layers, layer,
2590       (gint) new_layer_priority);
2591
2592   _resync_layers (timeline);
2593
2594   return TRUE;
2595 }