clip: Return TRUE when the the effect index does not change
[platform/upstream/gst-editing-services.git] / ges / ges-clip.c
1 /* GStreamer Editing Services
2  * Copyright (C) 2009 Edward Hervey <edward.hervey@collabora.co.uk>
3  *               2009 Nokia Corporation
4  *               2012 Collabora Ltd.
5  *                 Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 /**
24  * SECTION:gesclip
25  * @title: GESClip
26  * @short_description: Base Class for objects in a GESLayer
27  *
28  * A #GESClip is a 'natural' object which controls one or more
29  * #GESTrackElement(s) in one or more #GESTrack(s).
30  *
31  * Keeps a reference to the #GESTrackElement(s) it created and
32  * sets/updates their properties.
33  */
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 #include "ges-clip.h"
39 #include "ges.h"
40 #include "ges-internal.h"
41
42 #include <string.h>
43
44 static GList *ges_clip_create_track_elements_func (GESClip * clip,
45     GESTrackType type);
46 static gboolean _ripple (GESTimelineElement * element, GstClockTime start);
47 static gboolean _ripple_end (GESTimelineElement * element, GstClockTime end);
48 static gboolean _roll_start (GESTimelineElement * element, GstClockTime start);
49 static gboolean _roll_end (GESTimelineElement * element, GstClockTime end);
50 static gboolean _trim (GESTimelineElement * element, GstClockTime start);
51 static void _compute_height (GESContainer * container);
52
53 struct _GESClipPrivate
54 {
55   /*< public > */
56   GESLayer *layer;
57
58   /*< private > */
59   guint nb_effects;
60
61   GList *copied_track_elements;
62   GESLayer *copied_layer;
63
64   /* The formats supported by this Clip */
65   GESTrackType supportedformats;
66 };
67
68 typedef struct _CheckTrack
69 {
70   GESTrack *track;
71   GESTrackElement *source;
72 } CheckTrack;
73
74 enum
75 {
76   PROP_0,
77   PROP_LAYER,
78   PROP_SUPPORTED_FORMATS,
79   PROP_LAST
80 };
81
82 static GParamSpec *properties[PROP_LAST];
83
84 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GESClip, ges_clip, GES_TYPE_CONTAINER);
85
86 /****************************************************
87  *              Listen to our children              *
88  ****************************************************/
89
90 /* @min_priority: The absolute minimum priority a child of @container should have
91  * @max_priority: The absolute maximum priority a child of @container should have
92  */
93 static void
94 _get_priority_range (GESContainer * container, guint32 * min_priority,
95     guint32 * max_priority)
96 {
97   GESLayer *layer = GES_CLIP (container)->priv->layer;
98
99   if (layer) {
100     *min_priority = _PRIORITY (container) + layer->min_nle_priority;
101     *max_priority = layer->max_nle_priority;
102   } else {
103     *min_priority = _PRIORITY (container) + MIN_NLE_PRIO;
104     *max_priority = G_MAXUINT32;
105   }
106 }
107
108 static void
109 _child_priority_changed_cb (GESTimelineElement * child,
110     GParamSpec * arg G_GNUC_UNUSED, GESContainer * container)
111 {
112   guint32 min_prio, max_prio;
113
114   GST_DEBUG_OBJECT (container, "TimelineElement %p priority changed to %i",
115       child, _PRIORITY (child));
116
117   if (container->children_control_mode == GES_CHILDREN_IGNORE_NOTIFIES)
118     return;
119
120   /* Update mapping */
121   _get_priority_range (container, &min_prio, &max_prio);
122
123   _ges_container_set_priority_offset (container, child,
124       min_prio - _PRIORITY (child));
125 }
126
127 /*****************************************************
128  *                                                   *
129  * GESTimelineElement virtual methods implementation *
130  *                                                   *
131  *****************************************************/
132
133 static gboolean
134 _set_start (GESTimelineElement * element, GstClockTime start)
135 {
136   GList *tmp;
137   GESContainer *container = GES_CONTAINER (element);
138
139   GST_DEBUG_OBJECT (element, "Setting children start, (initiated_move: %"
140       GST_PTR_FORMAT ")", container->initiated_move);
141
142   element->start = start;
143   g_object_notify (G_OBJECT (element), "start");
144   container->children_control_mode = GES_CHILDREN_IGNORE_NOTIFIES;
145   for (tmp = container->children; tmp; tmp = g_list_next (tmp)) {
146     GESTimelineElement *child = (GESTimelineElement *) tmp->data;
147
148     _set_start0 (GES_TIMELINE_ELEMENT (child), start);
149   }
150   container->children_control_mode = GES_CHILDREN_UPDATE;
151
152   return FALSE;
153 }
154
155 static gboolean
156 _set_inpoint (GESTimelineElement * element, GstClockTime inpoint)
157 {
158   GList *tmp;
159   GESContainer *container = GES_CONTAINER (element);
160
161   container->children_control_mode = GES_CHILDREN_IGNORE_NOTIFIES;
162   for (tmp = container->children; tmp; tmp = g_list_next (tmp)) {
163     GESTimelineElement *child = (GESTimelineElement *) tmp->data;
164
165     if (child != container->initiated_move) {
166       _set_inpoint0 (child, inpoint);
167     }
168   }
169   container->children_control_mode = GES_CHILDREN_UPDATE;
170
171   return TRUE;
172 }
173
174 static gboolean
175 _set_duration (GESTimelineElement * element, GstClockTime duration)
176 {
177   GList *tmp;
178
179   GESContainer *container = GES_CONTAINER (element);
180
181   container->children_control_mode = GES_CHILDREN_IGNORE_NOTIFIES;
182   for (tmp = container->children; tmp; tmp = g_list_next (tmp)) {
183     GESTimelineElement *child = (GESTimelineElement *) tmp->data;
184
185     if (child != container->initiated_move)
186       _set_duration0 (GES_TIMELINE_ELEMENT (child), duration);
187   }
188   container->children_control_mode = GES_CHILDREN_UPDATE;
189
190   return TRUE;
191 }
192
193 static gboolean
194 _set_max_duration (GESTimelineElement * element, GstClockTime maxduration)
195 {
196   GList *tmp;
197
198   for (tmp = GES_CONTAINER (element)->children; tmp; tmp = g_list_next (tmp))
199     ges_timeline_element_set_max_duration (GES_TIMELINE_ELEMENT (tmp->data),
200         maxduration);
201
202   return TRUE;
203 }
204
205 static gboolean
206 _set_priority (GESTimelineElement * element, guint32 priority)
207 {
208   GList *tmp;
209   guint32 min_prio, max_prio;
210
211   GESContainer *container = GES_CONTAINER (element);
212
213   _get_priority_range (container, &min_prio, &max_prio);
214
215   container->children_control_mode = GES_CHILDREN_IGNORE_NOTIFIES;
216   for (tmp = container->children; tmp; tmp = g_list_next (tmp)) {
217     guint32 track_element_prio;
218     GESTimelineElement *child = (GESTimelineElement *) tmp->data;
219     gint off = _ges_container_get_priority_offset (container, child);
220
221
222     if (off >= LAYER_HEIGHT) {
223       GST_ERROR ("%s child %s as a priority offset %d >= LAYER_HEIGHT %d"
224           " ==> clamping it to 0", GES_TIMELINE_ELEMENT_NAME (element),
225           GES_TIMELINE_ELEMENT_NAME (child), off, LAYER_HEIGHT);
226       off = 0;
227     }
228
229     /* We need to remove our current priority from @min_prio
230      * as it is the absolute minimum priority @child could have
231      * before we set @container to @priority.
232      */
233     track_element_prio = min_prio - _PRIORITY (container) + priority - off;
234
235     if (track_element_prio > max_prio) {
236       GST_WARNING ("%p priority of %i, is outside of the its containing "
237           "layer space. (%d/%d) setting it to the maximum it can be",
238           container, priority, min_prio - _PRIORITY (container) + priority,
239           max_prio);
240
241       track_element_prio = max_prio;
242     }
243     _set_priority0 (child, track_element_prio);
244   }
245   container->children_control_mode = GES_CHILDREN_UPDATE;
246   _compute_height (container);
247
248   return TRUE;
249 }
250
251 static guint32
252 _get_layer_priority (GESTimelineElement * element)
253 {
254   GESClip *clip = GES_CLIP (element);
255
256   if (clip->priv->layer == NULL)
257     return GES_TIMELINE_ELEMENT_NO_LAYER_PRIORITY;
258
259   return ges_layer_get_priority (clip->priv->layer);
260 }
261
262 /****************************************************
263  *                                                  *
264  *  GESContainer virtual methods implementation     *
265  *                                                  *
266  ****************************************************/
267
268 static void
269 _compute_height (GESContainer * container)
270 {
271   GList *tmp;
272   guint32 min_prio = G_MAXUINT32, max_prio = 0;
273
274   if (container->children == NULL) {
275     /* FIXME Why not 0! */
276     _ges_container_set_height (container, 1);
277     return;
278   }
279
280   /* Go over all childs and check if height has changed */
281   for (tmp = container->children; tmp; tmp = tmp->next) {
282     guint tck_priority = _PRIORITY (tmp->data);
283
284     if (tck_priority < min_prio)
285       min_prio = tck_priority;
286     if (tck_priority > max_prio)
287       max_prio = tck_priority;
288   }
289
290   _ges_container_set_height (container, max_prio - min_prio + 1);
291 }
292
293 static gboolean
294 _add_child (GESContainer * container, GESTimelineElement * element)
295 {
296   GList *tmp;
297   guint max_prio, min_prio;
298   GESClipPrivate *priv = GES_CLIP (container)->priv;
299
300   g_return_val_if_fail (GES_IS_TRACK_ELEMENT (element), FALSE);
301
302   /* First make sure we work with a sorted list of GESTimelineElement-s */
303   _ges_container_sort_children (container);
304
305   /* If the TrackElement is an effect:
306    *  - We add it on top of the list of TrackEffect
307    *  - We put all TrackElements present in the Clip
308    *    which are not BaseEffect on top of them
309    * FIXME: Let the full control over priorities to the user
310    */
311   _get_priority_range (container, &min_prio, &max_prio);
312   if (GES_IS_BASE_EFFECT (element)) {
313     GESChildrenControlMode mode = container->children_control_mode;
314
315     GST_DEBUG_OBJECT (container, "Adding %ith effect: %" GST_PTR_FORMAT
316         " Priority %i", priv->nb_effects + 1, element,
317         min_prio + priv->nb_effects);
318
319     tmp = g_list_nth (GES_CONTAINER_CHILDREN (container), priv->nb_effects);
320     container->children_control_mode = GES_CHILDREN_UPDATE_OFFSETS;
321     for (; tmp; tmp = tmp->next) {
322       ges_timeline_element_set_priority (GES_TIMELINE_ELEMENT (tmp->data),
323           GES_TIMELINE_ELEMENT_PRIORITY (tmp->data) + 1);
324     }
325
326     _set_priority0 (element, min_prio + priv->nb_effects);
327     container->children_control_mode = mode;
328     priv->nb_effects++;
329   } else {
330     /* We add the track element on top of the effect list */
331     _set_priority0 (element, min_prio + priv->nb_effects);
332   }
333
334   /* We set the timing value of the child to ours, we avoid infinite loop
335    * making sure the container ignore notifies from the child */
336   container->children_control_mode = GES_CHILDREN_IGNORE_NOTIFIES;
337   _set_start0 (element, GES_TIMELINE_ELEMENT_START (container));
338   _set_inpoint0 (element, GES_TIMELINE_ELEMENT_INPOINT (container));
339   _set_duration0 (element, GES_TIMELINE_ELEMENT_DURATION (container));
340   container->children_control_mode = GES_CHILDREN_UPDATE;
341
342   return TRUE;
343 }
344
345 static gboolean
346 _remove_child (GESContainer * container, GESTimelineElement * element)
347 {
348   if (GES_IS_BASE_EFFECT (element)) {
349     GES_CLIP (container)->priv->nb_effects--;
350   }
351
352   GST_FIXME_OBJECT (container, "We should set other children prios");
353
354   return TRUE;
355 }
356
357 static void
358 _child_added (GESContainer * container, GESTimelineElement * element)
359 {
360   g_signal_connect (G_OBJECT (element), "notify::priority",
361       G_CALLBACK (_child_priority_changed_cb), container);
362
363   _child_priority_changed_cb (element, NULL, container);
364   _compute_height (container);
365 }
366
367 static void
368 _child_removed (GESContainer * container, GESTimelineElement * element)
369 {
370   g_signal_handlers_disconnect_by_func (element, _child_priority_changed_cb,
371       container);
372
373   if (GES_IS_BASE_EFFECT (element)) {
374     GList *tmp;
375     guint32 priority;
376     GESChildrenControlMode mode = container->children_control_mode;
377
378     GST_DEBUG_OBJECT (container, "Resyncing effects priority.");
379
380     container->children_control_mode = GES_CHILDREN_UPDATE_OFFSETS;
381     tmp = GES_CONTAINER_CHILDREN (container);
382     priority =
383         ges_timeline_element_get_priority (GES_TIMELINE_ELEMENT (element));
384     for (; tmp; tmp = tmp->next) {
385       if (ges_timeline_element_get_priority (GES_TIMELINE_ELEMENT (tmp->data)) >
386           priority) {
387         ges_timeline_element_set_priority (GES_TIMELINE_ELEMENT (tmp->data),
388             GES_TIMELINE_ELEMENT_PRIORITY (tmp->data) - 1);
389       }
390     }
391
392     container->children_control_mode = mode;
393   }
394
395   _compute_height (container);
396 }
397
398 static void
399 add_clip_to_list (gpointer key, gpointer clip, GList ** list)
400 {
401   *list = g_list_prepend (*list, gst_object_ref (clip));
402 }
403
404 static GList *
405 _ungroup (GESContainer * container, gboolean recursive)
406 {
407   GESClip *tmpclip;
408   GESTrackType track_type;
409   GESTrackElement *track_element;
410
411   gboolean first_obj = TRUE;
412   GList *tmp, *children, *ret = NULL;
413   GESClip *clip = GES_CLIP (container);
414   GESTimelineElement *element = GES_TIMELINE_ELEMENT (container);
415   GESLayer *layer = clip->priv->layer;
416   GHashTable *_tracktype_clip = g_hash_table_new (g_int_hash, g_int_equal);
417
418   /* If there is no TrackElement, just return @container in a list */
419   if (GES_CONTAINER_CHILDREN (container) == NULL) {
420     GST_DEBUG ("No TrackElement, simply returning");
421     return g_list_prepend (ret, container);
422   }
423
424   /* We need a copy of the current list of tracks */
425   children = ges_container_get_children (container, FALSE);
426   for (tmp = children; tmp; tmp = tmp->next) {
427     track_element = GES_TRACK_ELEMENT (tmp->data);
428     track_type = ges_track_element_get_track_type (track_element);
429
430     tmpclip = g_hash_table_lookup (_tracktype_clip, &track_type);
431     if (tmpclip == NULL) {
432       if (G_UNLIKELY (first_obj == TRUE)) {
433         tmpclip = clip;
434         first_obj = FALSE;
435       } else {
436         tmpclip = GES_CLIP (ges_timeline_element_copy (element, FALSE));
437         if (layer) {
438           /* Add new container to the same layer as @container */
439           ges_clip_set_moving_from_layer (tmpclip, TRUE);
440           ges_layer_add_clip (layer, tmpclip);
441           ges_clip_set_moving_from_layer (tmpclip, FALSE);
442         }
443       }
444
445       g_hash_table_insert (_tracktype_clip, &track_type, tmpclip);
446       ges_clip_set_supported_formats (tmpclip, track_type);
447     }
448
449     /* Move trackelement to the container it is supposed to land into */
450     if (tmpclip != clip) {
451       /* We need to bump the refcount to avoid the object to be destroyed */
452       gst_object_ref (track_element);
453       ges_container_remove (container, GES_TIMELINE_ELEMENT (track_element));
454       ges_container_add (GES_CONTAINER (tmpclip),
455           GES_TIMELINE_ELEMENT (track_element));
456       gst_object_unref (track_element);
457     }
458   }
459   g_list_free_full (children, gst_object_unref);
460   g_hash_table_foreach (_tracktype_clip, (GHFunc) add_clip_to_list, &ret);
461   g_hash_table_unref (_tracktype_clip);
462
463   return ret;
464 }
465
466 static GESContainer *
467 _group (GList * containers)
468 {
469   CheckTrack *tracks = NULL;
470   GESTimeline *timeline = NULL;
471   GESTrackType supported_formats;
472   GESLayer *layer = NULL;
473   GList *tmp, *tmpclip, *tmpelement;
474   GstClockTime start, inpoint, duration;
475
476   GESAsset *asset = NULL;
477   GESContainer *ret = NULL;
478   guint nb_tracks = 0, i = 0;
479
480   start = inpoint = duration = GST_CLOCK_TIME_NONE;
481
482   if (!containers)
483     return NULL;
484
485   /* First check if all the containers are clips, if they
486    * all have the same start/inpoint/duration and are in the same
487    * layer.
488    *
489    * We also need to make sure that all source have been created by the
490    * same asset, keep the information */
491   for (tmp = containers; tmp; tmp = tmp->next) {
492     GESClip *clip;
493     GESTimeline *tmptimeline;
494     GESContainer *tmpcontainer;
495     GESTimelineElement *element;
496
497     tmpcontainer = GES_CONTAINER (tmp->data);
498     element = GES_TIMELINE_ELEMENT (tmp->data);
499     if (GES_IS_CLIP (element) == FALSE) {
500       GST_DEBUG ("Can only work with clips");
501       goto done;
502     }
503     clip = GES_CLIP (tmp->data);
504     tmptimeline = GES_TIMELINE_ELEMENT_TIMELINE (element);
505     if (!timeline) {
506       GList *tmptrack;
507
508       start = _START (tmpcontainer);
509       inpoint = _INPOINT (tmpcontainer);
510       duration = _DURATION (tmpcontainer);
511       timeline = tmptimeline;
512       layer = clip->priv->layer;
513       nb_tracks = g_list_length (GES_TIMELINE_GET_TRACKS (timeline));
514       tracks = g_new0 (CheckTrack, nb_tracks);
515
516       for (tmptrack = GES_TIMELINE_GET_TRACKS (timeline); tmptrack;
517           tmptrack = tmptrack->next) {
518         tracks[i].track = tmptrack->data;
519         i++;
520       }
521     } else {
522       if (start != _START (tmpcontainer) ||
523           inpoint != _INPOINT (tmpcontainer) ||
524           duration != _DURATION (tmpcontainer) || clip->priv->layer != layer) {
525         GST_INFO ("All children must have the same start, inpoint, duration "
526             " and be in the same layer");
527
528         goto done;
529       } else {
530         GList *tmp2;
531
532         for (tmp2 = GES_CONTAINER_CHILDREN (tmp->data); tmp2; tmp2 = tmp2->next) {
533           GESTrackElement *track_element = GES_TRACK_ELEMENT (tmp2->data);
534
535           if (GES_IS_SOURCE (track_element)) {
536             guint i;
537
538             for (i = 0; i < nb_tracks; i++) {
539               if (tracks[i].track ==
540                   ges_track_element_get_track (track_element)) {
541                 if (tracks[i].source) {
542                   GST_INFO ("Can not link clips with various source for a "
543                       "same track");
544
545                   goto done;
546                 }
547                 tracks[i].source = track_element;
548                 break;
549               }
550             }
551           }
552         }
553       }
554     }
555   }
556
557
558   /* Then check that all sources have been created by the same asset,
559    * otherwise we can not group */
560   for (i = 0; i < nb_tracks; i++) {
561     if (tracks[i].source == NULL) {
562       GST_FIXME ("Check what to do here as we might end up having a mess");
563
564       continue;
565     }
566
567     /* FIXME Check what to do if we have source that have no assets */
568     if (!asset) {
569       asset =
570           ges_extractable_get_asset (GES_EXTRACTABLE
571           (ges_timeline_element_get_parent (GES_TIMELINE_ELEMENT (tracks
572                       [i].source))));
573       continue;
574     }
575     if (asset !=
576         ges_extractable_get_asset (GES_EXTRACTABLE
577             (ges_timeline_element_get_parent (GES_TIMELINE_ELEMENT (tracks
578                         [i].source))))) {
579       GST_INFO ("Can not link clips with source coming from different assets");
580
581       goto done;
582     }
583   }
584
585   /* And now pass all TrackElements to the first clip,
586    * and remove others from the layer (updating the supported formats) */
587   ret = containers->data;
588   supported_formats = GES_CLIP (ret)->priv->supportedformats;
589   for (tmpclip = containers->next; tmpclip; tmpclip = tmpclip->next) {
590     GESClip *cclip = tmpclip->data;
591     GList *children = ges_container_get_children (GES_CONTAINER (cclip), FALSE);
592
593     for (tmpelement = children; tmpelement; tmpelement = tmpelement->next) {
594       GESTimelineElement *celement = GES_TIMELINE_ELEMENT (tmpelement->data);
595
596       ges_container_remove (GES_CONTAINER (cclip), celement);
597       ges_container_add (ret, celement);
598
599       supported_formats = supported_formats |
600           ges_track_element_get_track_type (GES_TRACK_ELEMENT (celement));
601     }
602     g_list_free_full (children, gst_object_unref);
603
604     ges_layer_remove_clip (layer, tmpclip->data);
605   }
606
607   ges_clip_set_supported_formats (GES_CLIP (ret), supported_formats);
608
609 done:
610   if (tracks)
611     g_free (tracks);
612
613
614   return ret;
615
616 }
617
618 static gboolean
619 _edit (GESContainer * container, GList * layers,
620     gint new_layer_priority, GESEditMode mode, GESEdge edge, guint64 position)
621 {
622   GESTimeline *timeline = GES_TIMELINE_ELEMENT_TIMELINE (container);
623   GESTimelineElement *element = GES_TIMELINE_ELEMENT (container);
624
625   if (!G_UNLIKELY (GES_CONTAINER_CHILDREN (container))) {
626     GST_WARNING_OBJECT (container, "Trying to edit, but not containing"
627         "any TrackElement yet.");
628     return FALSE;
629   }
630
631   if (!timeline) {
632     GST_WARNING_OBJECT (container, "Trying to edit, but not in any"
633         "timeline.");
634     return FALSE;
635   }
636
637   switch (mode) {
638     case GES_EDIT_MODE_RIPPLE:
639       return timeline_ripple_object (timeline, element,
640           new_layer_priority <
641           0 ? GES_TIMELINE_ELEMENT_LAYER_PRIORITY (container) :
642           new_layer_priority, layers, edge, position);
643     case GES_EDIT_MODE_TRIM:
644       return timeline_trim_object (timeline, element,
645           new_layer_priority <
646           0 ? GES_TIMELINE_ELEMENT_LAYER_PRIORITY (container) :
647           new_layer_priority, layers, edge, position);
648     case GES_EDIT_MODE_NORMAL:
649       return timeline_move_object (timeline, element,
650           new_layer_priority <
651           0 ? GES_TIMELINE_ELEMENT_LAYER_PRIORITY (container) :
652           new_layer_priority, layers, edge, position);
653     case GES_EDIT_MODE_ROLL:
654       return timeline_roll_object (timeline, element, layers, edge, position);
655     case GES_EDIT_MODE_SLIDE:
656       GST_ERROR ("Sliding not implemented.");
657       return FALSE;
658   }
659   return FALSE;
660 }
661
662 static void
663 _deep_copy (GESTimelineElement * element, GESTimelineElement * copy)
664 {
665   GList *tmp;
666   GESClip *self = GES_CLIP (element), *ccopy = GES_CLIP (copy);
667
668   for (tmp = GES_CONTAINER_CHILDREN (element); tmp; tmp = tmp->next) {
669     ccopy->priv->copied_track_elements =
670         g_list_append (ccopy->priv->copied_track_elements,
671         ges_timeline_element_copy (tmp->data, TRUE));
672   }
673
674   if (self->priv->copied_layer)
675     ccopy->priv->copied_layer = g_object_ref (self->priv->copied_layer);
676   else if (self->priv->layer)
677     ccopy->priv->copied_layer = g_object_ref (self->priv->layer);
678 }
679
680 static GESTimelineElement *
681 _paste (GESTimelineElement * element, GESTimelineElement * ref,
682     GstClockTime paste_position)
683 {
684   GList *tmp;
685   GESClip *self = GES_CLIP (element);
686   GESClip *nclip = GES_CLIP (ges_timeline_element_copy (element, FALSE));
687
688   if (self->priv->copied_layer)
689     nclip->priv->copied_layer = g_object_ref (self->priv->copied_layer);
690
691   ges_timeline_element_set_start (GES_TIMELINE_ELEMENT (nclip), paste_position);
692   if (self->priv->copied_layer) {
693     if (!ges_layer_add_clip (self->priv->copied_layer, nclip)) {
694       GST_INFO ("%" GES_FORMAT " could not be pasted to %" GST_TIME_FORMAT,
695           GES_ARGS (element), GST_TIME_ARGS (paste_position));
696       gst_object_unref (nclip);
697
698       return NULL;
699     }
700
701   }
702
703   for (tmp = self->priv->copied_track_elements; tmp; tmp = tmp->next) {
704     GESTrackElement *new_trackelement, *trackelement =
705         GES_TRACK_ELEMENT (tmp->data);
706
707     new_trackelement =
708         GES_TRACK_ELEMENT (ges_timeline_element_copy (GES_TIMELINE_ELEMENT
709             (trackelement), FALSE));
710     if (new_trackelement == NULL) {
711       GST_WARNING_OBJECT (trackelement, "Could not create a copy");
712       continue;
713     }
714
715     ges_container_add (GES_CONTAINER (nclip),
716         GES_TIMELINE_ELEMENT (new_trackelement));
717
718     ges_track_element_copy_properties (GES_TIMELINE_ELEMENT (trackelement),
719         GES_TIMELINE_ELEMENT (new_trackelement));
720
721     ges_track_element_copy_bindings (trackelement, new_trackelement,
722         GST_CLOCK_TIME_NONE);
723   }
724
725   return GES_TIMELINE_ELEMENT (nclip);
726 }
727
728 static gboolean
729 _lookup_child (GESTimelineElement * self, const gchar * prop_name,
730     GObject ** child, GParamSpec ** pspec)
731 {
732   GList *tmp;
733
734   if (GES_TIMELINE_ELEMENT_CLASS (ges_clip_parent_class)->lookup_child (self,
735           prop_name, child, pspec))
736     return TRUE;
737
738   for (tmp = GES_CONTAINER_CHILDREN (self); tmp; tmp = tmp->next) {
739     if (ges_timeline_element_lookup_child (tmp->data, prop_name, child, pspec))
740       return TRUE;
741   }
742
743   return FALSE;
744 }
745
746 /****************************************************
747  *                                                  *
748  *    GObject virtual methods implementation        *
749  *                                                  *
750  ****************************************************/
751 static void
752 ges_clip_get_property (GObject * object, guint property_id,
753     GValue * value, GParamSpec * pspec)
754 {
755   GESClip *clip = GES_CLIP (object);
756
757   switch (property_id) {
758     case PROP_LAYER:
759       g_value_set_object (value, clip->priv->layer);
760       break;
761     case PROP_SUPPORTED_FORMATS:
762       g_value_set_flags (value, clip->priv->supportedformats);
763       break;
764     default:
765       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
766   }
767 }
768
769 static void
770 ges_clip_set_property (GObject * object, guint property_id,
771     const GValue * value, GParamSpec * pspec)
772 {
773   GESClip *clip = GES_CLIP (object);
774
775   switch (property_id) {
776     case PROP_SUPPORTED_FORMATS:
777       ges_clip_set_supported_formats (clip, g_value_get_flags (value));
778       break;
779     default:
780       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
781   }
782 }
783
784 static void
785 ges_clip_dispose (GObject * object)
786 {
787   GESClip *self = GES_CLIP (object);
788
789   g_list_free_full (self->priv->copied_track_elements, g_object_unref);
790   self->priv->copied_track_elements = NULL;
791   g_clear_object (&self->priv->copied_layer);
792
793   G_OBJECT_CLASS (ges_clip_parent_class)->dispose (object);
794 }
795
796
797 static void
798 ges_clip_class_init (GESClipClass * klass)
799 {
800   GObjectClass *object_class = G_OBJECT_CLASS (klass);
801   GESContainerClass *container_class = GES_CONTAINER_CLASS (klass);
802   GESTimelineElementClass *element_class = GES_TIMELINE_ELEMENT_CLASS (klass);
803
804   object_class->get_property = ges_clip_get_property;
805   object_class->set_property = ges_clip_set_property;
806   object_class->dispose = ges_clip_dispose;
807   klass->create_track_elements = ges_clip_create_track_elements_func;
808   klass->create_track_element = NULL;
809
810   /**
811    * GESClip:supported-formats:
812    *
813    * The formats supported by the clip.
814    */
815   properties[PROP_SUPPORTED_FORMATS] = g_param_spec_flags ("supported-formats",
816       "Supported formats", "Formats supported by the file",
817       GES_TYPE_TRACK_TYPE, GES_TRACK_TYPE_AUDIO | GES_TRACK_TYPE_VIDEO,
818       G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
819
820   g_object_class_install_property (object_class, PROP_SUPPORTED_FORMATS,
821       properties[PROP_SUPPORTED_FORMATS]);
822
823   /**
824    * GESClip:layer:
825    *
826    * The GESLayer where this clip is being used. If you want to connect to its
827    * notify signal you should connect to it with g_signal_connect_after as the
828    * signal emission can be stop in the first fase.
829    */
830   properties[PROP_LAYER] = g_param_spec_object ("layer", "Layer",
831       "The GESLayer where this clip is being used.",
832       GES_TYPE_LAYER, G_PARAM_READABLE);
833   g_object_class_install_property (object_class, PROP_LAYER,
834       properties[PROP_LAYER]);
835
836   element_class->ripple = _ripple;
837   element_class->ripple_end = _ripple_end;
838   element_class->roll_start = _roll_start;
839   element_class->roll_end = _roll_end;
840   element_class->trim = _trim;
841   element_class->set_start = _set_start;
842   element_class->set_duration = _set_duration;
843   element_class->set_inpoint = _set_inpoint;
844   element_class->set_priority = _set_priority;
845   element_class->set_max_duration = _set_max_duration;
846   element_class->paste = _paste;
847   element_class->deep_copy = _deep_copy;
848   element_class->lookup_child = _lookup_child;
849   element_class->get_layer_priority = _get_layer_priority;
850
851   container_class->add_child = _add_child;
852   container_class->remove_child = _remove_child;
853   container_class->child_removed = _child_removed;
854   container_class->child_added = _child_added;
855   container_class->ungroup = _ungroup;
856   container_class->group = _group;
857   container_class->grouping_priority = G_MAXUINT;
858   container_class->edit = _edit;
859 }
860
861 static void
862 ges_clip_init (GESClip * self)
863 {
864   self->priv = ges_clip_get_instance_private (self);
865   self->priv->layer = NULL;
866   self->priv->nb_effects = 0;
867 }
868
869 /**
870  * ges_clip_create_track_element:
871  * @clip: The origin #GESClip
872  * @type: The #GESTrackType to create a #GESTrackElement for.
873  *
874  * Creates a #GESTrackElement for the provided @type. The clip
875  * keep a reference to the newly created trackelement, you therefore need to
876  * call @ges_container_remove when you are done with it.
877  *
878  * Returns: (transfer none) (nullable): A #GESTrackElement. Returns NULL if
879  * the #GESTrackElement could not be created.
880  */
881 GESTrackElement *
882 ges_clip_create_track_element (GESClip * clip, GESTrackType type)
883 {
884   GESClipClass *class;
885   GESTrackElement *res;
886
887   g_return_val_if_fail (GES_IS_CLIP (clip), NULL);
888
889   GST_DEBUG_OBJECT (clip, "Creating track element for %s",
890       ges_track_type_name (type));
891   if (!(type & clip->priv->supportedformats)) {
892     GST_DEBUG_OBJECT (clip, "We don't support this track type %i", type);
893     return NULL;
894   }
895
896   class = GES_CLIP_GET_CLASS (clip);
897
898   if (G_UNLIKELY (class->create_track_element == NULL)) {
899     GST_ERROR ("No 'create_track_element' implementation available fo type %s",
900         G_OBJECT_TYPE_NAME (clip));
901     return NULL;
902   }
903
904   res = class->create_track_element (clip, type);
905   return res;
906
907 }
908
909 /**
910  * ges_clip_create_track_elements:
911  * @clip: The origin #GESClip
912  * @type: The #GESTrackType to create each #GESTrackElement for.
913  *
914  * Creates all #GESTrackElements supported by this clip for the track type.
915  *
916  * Returns: (element-type GESTrackElement) (transfer full): A #GList of
917  * newly created #GESTrackElement-s
918  */
919
920 GList *
921 ges_clip_create_track_elements (GESClip * clip, GESTrackType type)
922 {
923   GList *result = NULL, *tmp, *children;
924   GESClipClass *klass;
925   guint max_prio, min_prio;
926   gboolean readding_effects_only = TRUE;
927
928   g_return_val_if_fail (GES_IS_CLIP (clip), NULL);
929
930   klass = GES_CLIP_GET_CLASS (clip);
931
932   if (!(klass->create_track_elements)) {
933     GST_WARNING ("no GESClip::create_track_elements implentation");
934     return NULL;
935   }
936
937   GST_DEBUG_OBJECT (clip, "Creating TrackElements for type: %s",
938       ges_track_type_name (type));
939   children = ges_container_get_children (GES_CONTAINER (clip), TRUE);
940   for (tmp = children; tmp; tmp = tmp->next) {
941     GESTrackElement *child = GES_TRACK_ELEMENT (tmp->data);
942
943     if (!ges_track_element_get_track (child)
944         && ges_track_element_get_track_type (child) & type) {
945
946       GST_DEBUG_OBJECT (clip, "Removing for reusage: %" GST_PTR_FORMAT, child);
947       result = g_list_append (result, g_object_ref (child));
948       ges_container_remove (GES_CONTAINER (clip), tmp->data);
949       if (!GES_IS_BASE_EFFECT (child))
950         readding_effects_only = FALSE;
951     }
952   }
953   g_list_free_full (children, gst_object_unref);
954
955   if (readding_effects_only) {
956     result = g_list_concat (result, klass->create_track_elements (clip, type));
957   }
958
959   _get_priority_range (GES_CONTAINER (clip), &min_prio, &max_prio);
960   for (tmp = result; tmp; tmp = tmp->next) {
961     GESTimelineElement *elem = tmp->data;
962
963     _set_start0 (elem, GES_TIMELINE_ELEMENT_START (clip));
964     _set_inpoint0 (elem, GES_TIMELINE_ELEMENT_INPOINT (clip));
965     _set_duration0 (elem, GES_TIMELINE_ELEMENT_DURATION (clip));
966
967     if (GST_CLOCK_TIME_IS_VALID (GES_TIMELINE_ELEMENT_MAX_DURATION (clip)))
968       ges_timeline_element_set_max_duration (GES_TIMELINE_ELEMENT (elem),
969           GES_TIMELINE_ELEMENT_MAX_DURATION (clip));
970
971     _set_priority0 (elem, min_prio + clip->priv->nb_effects);
972
973     ges_container_add (GES_CONTAINER (clip), elem);
974   }
975
976   return result;
977 }
978
979 /*
980  * default implementation of GESClipClass::create_track_elements
981  */
982 GList *
983 ges_clip_create_track_elements_func (GESClip * clip, GESTrackType type)
984 {
985   GESTrackElement *result;
986
987   GST_DEBUG_OBJECT (clip, "Creating trackelement for track: %s",
988       ges_track_type_name (type));
989   result = ges_clip_create_track_element (clip, type);
990   if (!result) {
991     GST_DEBUG ("Did not create track element");
992     return NULL;
993   }
994
995   return g_list_append (NULL, result);
996 }
997
998 void
999 ges_clip_set_layer (GESClip * clip, GESLayer * layer)
1000 {
1001   if (layer == clip->priv->layer)
1002     return;
1003
1004   clip->priv->layer = layer;
1005
1006   GST_DEBUG ("clip:%p, layer:%p", clip, layer);
1007
1008   /* We do not want to notify the setting of layer = NULL when
1009    * it is actually the result of a move between layer (as we know
1010    * that it will be added to another layer right after, and this
1011    * is what imports here.) */
1012   if (!ELEMENT_FLAG_IS_SET (clip, GES_CLIP_IS_MOVING))
1013     g_object_notify_by_pspec (G_OBJECT (clip), properties[PROP_LAYER]);
1014 }
1015
1016 /**
1017  * ges_clip_set_moving_from_layer:
1018  * @clip: a #GESClip
1019  * @is_moving: %TRUE if you want to start moving @clip to another layer
1020  * %FALSE when you finished moving it.
1021  *
1022  * Sets the clip in a moving to layer state. You might rather use the
1023  * ges_clip_move_to_layer function to move #GESClip-s
1024  * from a layer to another.
1025  **/
1026 void
1027 ges_clip_set_moving_from_layer (GESClip * clip, gboolean is_moving)
1028 {
1029   g_return_if_fail (GES_IS_CLIP (clip));
1030
1031   if (is_moving)
1032     ELEMENT_SET_FLAG (clip, GES_CLIP_IS_MOVING);
1033   else
1034     ELEMENT_UNSET_FLAG (clip, GES_CLIP_IS_MOVING);
1035 }
1036
1037 /**
1038  * ges_clip_is_moving_from_layer:
1039  * @clip: a #GESClip
1040  *
1041  * Tells you if the clip is currently moving from a layer to another.
1042  * You might rather use the ges_clip_move_to_layer function to
1043  * move #GESClip-s from a layer to another.
1044  *
1045  * Returns: %TRUE if @clip is currently moving from its current layer
1046  * %FALSE otherwize
1047  **/
1048 gboolean
1049 ges_clip_is_moving_from_layer (GESClip * clip)
1050 {
1051   g_return_val_if_fail (GES_IS_CLIP (clip), FALSE);
1052
1053   return ELEMENT_FLAG_IS_SET (clip, GES_CLIP_IS_MOVING);
1054 }
1055
1056 /**
1057  * ges_clip_move_to_layer:
1058  * @clip: a #GESClip
1059  * @layer: the new #GESLayer
1060  *
1061  * Moves @clip to @layer. If @clip is not in any layer, it adds it to
1062  * @layer, else, it removes it from its current layer, and adds it to @layer.
1063  *
1064  * Returns: %TRUE if @clip could be moved %FALSE otherwize
1065  */
1066 gboolean
1067 ges_clip_move_to_layer (GESClip * clip, GESLayer * layer)
1068 {
1069   gboolean ret;
1070   GESLayer *current_layer;
1071
1072   g_return_val_if_fail (GES_IS_CLIP (clip), FALSE);
1073   g_return_val_if_fail (GES_IS_LAYER (layer), FALSE);
1074
1075   ELEMENT_SET_FLAG (clip, GES_CLIP_IS_MOVING);
1076   if (layer->timeline
1077       && !timeline_tree_can_move_element (timeline_get_tree (layer->timeline),
1078           GES_TIMELINE_ELEMENT (clip),
1079           ges_layer_get_priority (layer),
1080           GES_TIMELINE_ELEMENT_START (clip),
1081           GES_TIMELINE_ELEMENT_DURATION (clip), NULL)) {
1082     GST_INFO_OBJECT (layer, "Clip %" GES_FORMAT " can't move to layer %d",
1083         GES_ARGS (clip), ges_layer_get_priority (layer));
1084     ELEMENT_UNSET_FLAG (clip, GES_CLIP_IS_MOVING);
1085     return FALSE;
1086   }
1087
1088   current_layer = clip->priv->layer;
1089
1090   if (current_layer == NULL) {
1091     GST_DEBUG ("Not moving %p, only adding it to %p", clip, layer);
1092
1093     return ges_layer_add_clip (layer, clip);
1094   }
1095
1096   GST_DEBUG_OBJECT (clip, "moving to layer %p, priority: %d", layer,
1097       ges_layer_get_priority (layer));
1098
1099   gst_object_ref (clip);
1100   ret = ges_layer_remove_clip (current_layer, clip);
1101
1102   if (!ret) {
1103     ELEMENT_UNSET_FLAG (clip, GES_CLIP_IS_MOVING);
1104     gst_object_unref (clip);
1105     return FALSE;
1106   }
1107
1108   ret = ges_layer_add_clip (layer, clip);
1109   ELEMENT_UNSET_FLAG (clip, GES_CLIP_IS_MOVING);
1110
1111   gst_object_unref (clip);
1112   g_object_notify_by_pspec (G_OBJECT (clip), properties[PROP_LAYER]);
1113
1114
1115   return ret && (clip->priv->layer == layer);
1116 }
1117
1118 /**
1119  * ges_clip_find_track_element:
1120  * @clip: a #GESClip
1121  * @track: (allow-none): a #GESTrack or NULL
1122  * @type: a #GType indicating the type of track element you are looking
1123  * for or %G_TYPE_NONE if you do not care about the track type.
1124  *
1125  * Finds the #GESTrackElement controlled by @clip that is used in @track. You
1126  * may optionally specify a GType to further narrow search criteria.
1127  *
1128  * Note: If many objects match, then the one with the highest priority will be
1129  * returned.
1130  *
1131  * Returns: (transfer full) (nullable): The #GESTrackElement used by @track,
1132  * else %NULL. Unref after usage
1133  */
1134
1135 GESTrackElement *
1136 ges_clip_find_track_element (GESClip * clip, GESTrack * track, GType type)
1137 {
1138   GList *tmp;
1139   GESTrackElement *otmp;
1140
1141   GESTrackElement *ret = NULL;
1142
1143   g_return_val_if_fail (GES_IS_CLIP (clip), NULL);
1144   g_return_val_if_fail (!(track == NULL && type == G_TYPE_NONE), NULL);
1145
1146   for (tmp = GES_CONTAINER_CHILDREN (clip); tmp; tmp = g_list_next (tmp)) {
1147     otmp = (GESTrackElement *) tmp->data;
1148
1149     if ((type != G_TYPE_NONE) && !G_TYPE_CHECK_INSTANCE_TYPE (tmp->data, type))
1150       continue;
1151
1152     if ((track == NULL) || (ges_track_element_get_track (otmp) == track)) {
1153       ret = GES_TRACK_ELEMENT (tmp->data);
1154       gst_object_ref (ret);
1155       break;
1156     }
1157   }
1158
1159   return ret;
1160 }
1161
1162 /**
1163  * ges_clip_get_layer:
1164  * @clip: a #GESClip
1165  *
1166  * Get the #GESLayer to which this clip belongs.
1167  *
1168  * Returns: (transfer full) (nullable): The #GESLayer where this @clip is being
1169  * used, or %NULL if it is not used on any layer. The caller should unref it
1170  * usage.
1171  */
1172 GESLayer *
1173 ges_clip_get_layer (GESClip * clip)
1174 {
1175   g_return_val_if_fail (GES_IS_CLIP (clip), NULL);
1176
1177   if (clip->priv->layer != NULL)
1178     gst_object_ref (G_OBJECT (clip->priv->layer));
1179
1180   return clip->priv->layer;
1181 }
1182
1183 /**
1184  * ges_clip_get_top_effects:
1185  * @clip: The origin #GESClip
1186  *
1187  * Get effects applied on @clip
1188  *
1189  * Returns: (transfer full) (element-type GESTrackElement): a #GList of the
1190  * #GESBaseEffect that are applied on @clip order by ascendant priorities.
1191  * The refcount of the objects will be increased. The user will have to
1192  * unref each #GESBaseEffect and free the #GList.
1193  */
1194 GList *
1195 ges_clip_get_top_effects (GESClip * clip)
1196 {
1197   GList *tmp, *ret;
1198   guint i;
1199
1200   g_return_val_if_fail (GES_IS_CLIP (clip), NULL);
1201
1202   GST_DEBUG_OBJECT (clip, "Getting the %i top effects", clip->priv->nb_effects);
1203   ret = NULL;
1204
1205   for (tmp = GES_CONTAINER_CHILDREN (clip), i = 0;
1206       i < clip->priv->nb_effects; tmp = tmp->next, i++) {
1207     ret = g_list_append (ret, gst_object_ref (tmp->data));
1208   }
1209
1210   return g_list_sort (ret, (GCompareFunc) element_start_compare);
1211 }
1212
1213 /**
1214  * ges_clip_get_top_effect_index:
1215  * @clip: The origin #GESClip
1216  * @effect: The #GESBaseEffect we want to get the top index from
1217  *
1218  * Gets the index position of an effect.
1219  *
1220  * Returns: The top index of the effect, -1 if something went wrong.
1221  */
1222 gint
1223 ges_clip_get_top_effect_index (GESClip * clip, GESBaseEffect * effect)
1224 {
1225   guint max_prio, min_prio;
1226
1227   g_return_val_if_fail (GES_IS_CLIP (clip), -1);
1228   g_return_val_if_fail (GES_IS_BASE_EFFECT (effect), -1);
1229
1230   _get_priority_range (GES_CONTAINER (clip), &min_prio, &max_prio);
1231
1232   return GES_TIMELINE_ELEMENT_PRIORITY (effect) - min_prio;
1233 }
1234
1235 /* TODO 2.0 remove as it is Deprecated */
1236 gint
1237 ges_clip_get_top_effect_position (GESClip * clip, GESBaseEffect * effect)
1238 {
1239   return ges_clip_get_top_effect_index (clip, effect);
1240 }
1241
1242 /* TODO 2.0 remove as it is Deprecated */
1243 gboolean
1244 ges_clip_set_top_effect_priority (GESClip * clip,
1245     GESBaseEffect * effect, guint newpriority)
1246 {
1247   return ges_clip_set_top_effect_index (clip, effect, newpriority);
1248 }
1249
1250 /**
1251  * ges_clip_set_top_effect_index:
1252  * @clip: The origin #GESClip
1253  * @effect: The #GESBaseEffect to move
1254  * @newindex: the new index at which to move the @effect inside this
1255  * #GESClip
1256  *
1257  * This is a convenience method that lets you set the index of a top effect.
1258  *
1259  * Returns: %TRUE if @effect was successfuly moved, %FALSE otherwise.
1260  */
1261 gboolean
1262 ges_clip_set_top_effect_index (GESClip * clip, GESBaseEffect * effect,
1263     guint newindex)
1264 {
1265   gint inc;
1266   GList *tmp;
1267   guint current_prio, min_prio, max_prio;
1268   GESTrackElement *track_element;
1269
1270   g_return_val_if_fail (GES_IS_CLIP (clip), FALSE);
1271
1272   track_element = GES_TRACK_ELEMENT (effect);
1273   current_prio = _PRIORITY (track_element);
1274
1275   _get_priority_range (GES_CONTAINER (clip), &min_prio, &max_prio);
1276
1277   newindex = newindex + min_prio;
1278   /*  We don't change the priority */
1279   if (current_prio == newindex)
1280     return TRUE;
1281
1282   if (G_UNLIKELY (GES_CLIP (GES_TIMELINE_ELEMENT_PARENT (track_element)) !=
1283           clip))
1284     return FALSE;
1285
1286   if (newindex > (clip->priv->nb_effects - 1 + min_prio)) {
1287     GST_DEBUG ("You are trying to make %p not a top effect", effect);
1288     return FALSE;
1289   }
1290
1291   if (current_prio > clip->priv->nb_effects + min_prio) {
1292     GST_ERROR ("%p is not a top effect", effect);
1293     return FALSE;
1294   }
1295
1296   _ges_container_sort_children (GES_CONTAINER (clip));
1297   if (_PRIORITY (track_element) < newindex)
1298     inc = -1;
1299   else
1300     inc = +1;
1301
1302   GST_DEBUG_OBJECT (clip, "Setting top effect %" GST_PTR_FORMAT "priority: %i",
1303       effect, newindex);
1304
1305   for (tmp = GES_CONTAINER_CHILDREN (clip); tmp; tmp = tmp->next) {
1306     GESTrackElement *tmpo = GES_TRACK_ELEMENT (tmp->data);
1307     guint tck_priority = _PRIORITY (tmpo);
1308
1309     if (tmpo == track_element)
1310       continue;
1311
1312     if ((inc == +1 && tck_priority >= newindex) ||
1313         (inc == -1 && tck_priority <= newindex)) {
1314       _set_priority0 (GES_TIMELINE_ELEMENT (tmpo), tck_priority + inc);
1315     }
1316   }
1317   _set_priority0 (GES_TIMELINE_ELEMENT (track_element), newindex);
1318
1319   return TRUE;
1320 }
1321
1322 /**
1323  * ges_clip_split:
1324  * @clip: the #GESClip to split
1325  * @position: a #GstClockTime representing the timeline position at which to split
1326  *
1327  * The function modifies @clip, and creates another #GESClip so we have two
1328  * clips at the end, splitted at the time specified by @position, as a position
1329  * in the timeline (not in the clip to be split). For example, if
1330  * ges_clip_split is called on a 4-second clip playing from 0:01.00 until
1331  * 0:05.00, with a split position of 0:02.00, this will result in one clip of 1
1332  * second and one clip of 3 seconds, not in two clips of 2 seconds.
1333  *
1334  * The newly created clip will be added to the same layer as @clip is in. This
1335  * implies that @clip must be in a #GESLayer for the operation to be possible.
1336  *
1337  * This method supports clips playing at a different tempo than one second per
1338  * second. For example, splitting a clip with a #GESEffect 'pitch tempo=1.5'
1339  * four seconds after it starts, will set the inpoint of the new clip to six
1340  * seconds after that of the clip to split. For this, the rate-changing
1341  * property must be registered using @ges_effect_class_register_rate_property;
1342  * for the 'pitch' plugin, this is already done.
1343  *
1344  * Returns: (transfer none) (nullable): The newly created #GESClip resulting
1345  * from the splitting or %NULL if the clip can't be split.
1346  */
1347 GESClip *
1348 ges_clip_split (GESClip * clip, guint64 position)
1349 {
1350   GList *tmp;
1351   GESClip *new_object;
1352   GstClockTime start, inpoint, duration, old_duration, new_duration;
1353   gdouble media_duration_factor;
1354
1355   g_return_val_if_fail (GES_IS_CLIP (clip), NULL);
1356   g_return_val_if_fail (clip->priv->layer, NULL);
1357   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (position), NULL);
1358
1359   duration = _DURATION (clip);
1360   start = _START (clip);
1361   inpoint = _INPOINT (clip);
1362
1363   if (position >= start + duration || position <= start) {
1364     GST_WARNING_OBJECT (clip, "Can not split %" GST_TIME_FORMAT
1365         " out of boundaries", GST_TIME_ARGS (position));
1366     return NULL;
1367   }
1368
1369   GST_DEBUG_OBJECT (clip, "Spliting at %" GST_TIME_FORMAT,
1370       GST_TIME_ARGS (position));
1371
1372   /* Create the new Clip */
1373   new_object = GES_CLIP (ges_timeline_element_copy (GES_TIMELINE_ELEMENT (clip),
1374           FALSE));
1375
1376   GST_DEBUG_OBJECT (new_object, "New 'splitted' clip");
1377   /* Set new timing properties on the Clip */
1378   media_duration_factor =
1379       ges_timeline_element_get_media_duration_factor (GES_TIMELINE_ELEMENT
1380       (clip));
1381   new_duration = duration + start - position;
1382   old_duration = position - start;
1383   _set_start0 (GES_TIMELINE_ELEMENT (new_object), position);
1384   _set_inpoint0 (GES_TIMELINE_ELEMENT (new_object),
1385       inpoint + old_duration * media_duration_factor);
1386   _set_duration0 (GES_TIMELINE_ELEMENT (new_object), new_duration);
1387
1388   _DURATION (clip) = old_duration;
1389   g_object_notify (G_OBJECT (clip), "duration");
1390
1391   /* We do not want the timeline to create again TrackElement-s */
1392   ges_clip_set_moving_from_layer (new_object, TRUE);
1393   ges_layer_add_clip (clip->priv->layer, new_object);
1394   ges_clip_set_moving_from_layer (new_object, FALSE);
1395
1396   for (tmp = GES_CONTAINER_CHILDREN (clip); tmp; tmp = tmp->next) {
1397     GESTrackElement *new_trackelement, *trackelement =
1398         GES_TRACK_ELEMENT (tmp->data);
1399
1400     new_trackelement =
1401         GES_TRACK_ELEMENT (ges_timeline_element_copy (GES_TIMELINE_ELEMENT
1402             (trackelement), FALSE));
1403     if (new_trackelement == NULL) {
1404       GST_WARNING_OBJECT (trackelement, "Could not create a copy");
1405       continue;
1406     }
1407
1408     /* Set 'new' track element timing propeties */
1409     _set_start0 (GES_TIMELINE_ELEMENT (new_trackelement), position);
1410     _set_inpoint0 (GES_TIMELINE_ELEMENT (new_trackelement),
1411         inpoint + old_duration * media_duration_factor);
1412     _set_duration0 (GES_TIMELINE_ELEMENT (new_trackelement), new_duration);
1413
1414     ges_container_add (GES_CONTAINER (new_object),
1415         GES_TIMELINE_ELEMENT (new_trackelement));
1416     ges_track_element_copy_properties (GES_TIMELINE_ELEMENT (trackelement),
1417         GES_TIMELINE_ELEMENT (new_trackelement));
1418
1419     ges_track_element_copy_bindings (trackelement, new_trackelement,
1420         position - start + inpoint);
1421   }
1422
1423   ELEMENT_SET_FLAG (clip, GES_TIMELINE_ELEMENT_SET_SIMPLE);
1424   _DURATION (clip) = duration;
1425   _set_duration0 (GES_TIMELINE_ELEMENT (clip), old_duration);
1426   ELEMENT_UNSET_FLAG (clip, GES_TIMELINE_ELEMENT_SET_SIMPLE);
1427
1428   return new_object;
1429 }
1430
1431 /**
1432  * ges_clip_set_supported_formats:
1433  * @clip: the #GESClip to set supported formats on
1434  * @supportedformats: the #GESTrackType defining formats supported by @clip
1435  *
1436  * Sets the formats supported by the file.
1437  */
1438 void
1439 ges_clip_set_supported_formats (GESClip * clip, GESTrackType supportedformats)
1440 {
1441   g_return_if_fail (GES_IS_CLIP (clip));
1442
1443   clip->priv->supportedformats = supportedformats;
1444 }
1445
1446 /**
1447  * ges_clip_get_supported_formats:
1448  * @clip: the #GESClip
1449  *
1450  * Get the formats supported by @clip.
1451  *
1452  * Returns: The formats supported by @clip.
1453  */
1454 GESTrackType
1455 ges_clip_get_supported_formats (GESClip * clip)
1456 {
1457   g_return_val_if_fail (GES_IS_CLIP (clip), GES_TRACK_TYPE_UNKNOWN);
1458
1459   return clip->priv->supportedformats;
1460 }
1461
1462 gboolean
1463 _ripple (GESTimelineElement * element, GstClockTime start)
1464 {
1465   return ges_container_edit (GES_CONTAINER (element), NULL,
1466       ges_timeline_element_get_layer_priority (element),
1467       GES_EDIT_MODE_RIPPLE, GES_EDGE_NONE, start);
1468 }
1469
1470 static gboolean
1471 _ripple_end (GESTimelineElement * element, GstClockTime end)
1472 {
1473   return ges_container_edit (GES_CONTAINER (element), NULL,
1474       ges_timeline_element_get_layer_priority (element),
1475       GES_EDIT_MODE_RIPPLE, GES_EDGE_END, end);
1476 }
1477
1478 gboolean
1479 _roll_start (GESTimelineElement * element, GstClockTime start)
1480 {
1481   return ges_container_edit (GES_CONTAINER (element), NULL,
1482       ges_timeline_element_get_layer_priority (element),
1483       GES_EDIT_MODE_ROLL, GES_EDGE_START, start);
1484 }
1485
1486 gboolean
1487 _roll_end (GESTimelineElement * element, GstClockTime end)
1488 {
1489   return ges_container_edit (GES_CONTAINER (element), NULL,
1490       ges_timeline_element_get_layer_priority (element),
1491       GES_EDIT_MODE_ROLL, GES_EDGE_END, end);
1492 }
1493
1494 gboolean
1495 _trim (GESTimelineElement * element, GstClockTime start)
1496 {
1497   return ges_container_edit (GES_CONTAINER (element), NULL, -1,
1498       GES_EDIT_MODE_TRIM, GES_EDGE_START, start);
1499 }
1500
1501 /**
1502  * ges_clip_add_asset:
1503  * @clip: a #GESClip
1504  * @asset: a #GESAsset with #GES_TYPE_TRACK_ELEMENT as extractable_type
1505  *
1506  * Extracts a #GESTrackElement from @asset and adds it to the @clip.
1507  * Should only be called in order to add operations to a #GESClip,
1508  * ni other cases TrackElement are added automatically when adding the
1509  * #GESClip/#GESAsset to a layer.
1510  *
1511  * Takes a reference on @track_element.
1512  *
1513  * Returns: (transfer none)(allow-none): Created #GESTrackElement or NULL
1514  * if an error happened
1515  */
1516 GESTrackElement *
1517 ges_clip_add_asset (GESClip * clip, GESAsset * asset)
1518 {
1519   GESTrackElement *element;
1520
1521   g_return_val_if_fail (GES_IS_CLIP (clip), NULL);
1522   g_return_val_if_fail (GES_IS_ASSET (asset), NULL);
1523   g_return_val_if_fail (g_type_is_a (ges_asset_get_extractable_type
1524           (asset), GES_TYPE_TRACK_ELEMENT), NULL);
1525
1526   element = GES_TRACK_ELEMENT (ges_asset_extract (asset, NULL));
1527
1528   if (!ges_container_add (GES_CONTAINER (clip), GES_TIMELINE_ELEMENT (element)))
1529     return NULL;
1530
1531   return element;
1532
1533 }
1534
1535 /**
1536  * ges_clip_find_track_elements:
1537  * @clip: a #GESClip
1538  * @track: (allow-none): a #GESTrack or NULL
1539  * @track_type: a #GESTrackType indicating the type of tracks in which elements
1540  * should be searched.
1541  * @type: a #GType indicating the type of track element you are looking
1542  * for or %G_TYPE_NONE if you do not care about the track type.
1543  *
1544  * Finds all the #GESTrackElement controlled by @clip that is used in @track. You
1545  * may optionally specify a GType to further narrow search criteria.
1546  *
1547  * Returns: (transfer full) (element-type GESTrackElement): a #GList of the
1548  * #GESTrackElement contained in @clip.
1549  * The refcount of the objects will be increased. The user will have to
1550  * unref each #GESTrackElement and free the #GList.
1551  */
1552
1553 GList *
1554 ges_clip_find_track_elements (GESClip * clip, GESTrack * track,
1555     GESTrackType track_type, GType type)
1556 {
1557   GList *tmp;
1558   GESTrack *tmptrack;
1559   GESTrackElement *otmp;
1560   GESTrackElement *foundElement;
1561
1562   GList *ret = NULL;
1563
1564   g_return_val_if_fail (GES_IS_CLIP (clip), NULL);
1565   g_return_val_if_fail (!(track == NULL && type == G_TYPE_NONE &&
1566           track_type == GES_TRACK_TYPE_UNKNOWN), NULL);
1567
1568   for (tmp = GES_CONTAINER_CHILDREN (clip); tmp; tmp = g_list_next (tmp)) {
1569     otmp = (GESTrackElement *) tmp->data;
1570
1571     if ((type != G_TYPE_NONE) && !G_TYPE_CHECK_INSTANCE_TYPE (tmp->data, type))
1572       continue;
1573
1574     tmptrack = ges_track_element_get_track (otmp);
1575     if (((track != NULL && tmptrack == track)) ||
1576         (track_type != GES_TRACK_TYPE_UNKNOWN
1577             && ges_track_element_get_track_type (otmp) == track_type)) {
1578
1579       foundElement = GES_TRACK_ELEMENT (tmp->data);
1580
1581       ret = g_list_append (ret, gst_object_ref (foundElement));
1582     }
1583   }
1584
1585   return ret;
1586 }