clip: Fix layer managament when copying a clip that was pasted
[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   if (!self->priv->layer)
669     return;
670
671   for (tmp = GES_CONTAINER_CHILDREN (element); tmp; tmp = tmp->next) {
672     ccopy->priv->copied_track_elements =
673         g_list_append (ccopy->priv->copied_track_elements,
674         ges_timeline_element_copy (tmp->data, TRUE));
675   }
676
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
697       return NULL;
698     }
699
700   }
701
702   for (tmp = self->priv->copied_track_elements; tmp; tmp = tmp->next) {
703     GESTrackElement *new_trackelement, *trackelement =
704         GES_TRACK_ELEMENT (tmp->data);
705
706     new_trackelement =
707         GES_TRACK_ELEMENT (ges_timeline_element_copy (GES_TIMELINE_ELEMENT
708             (trackelement), FALSE));
709     if (new_trackelement == NULL) {
710       GST_WARNING_OBJECT (trackelement, "Could not create a copy");
711       continue;
712     }
713
714     ges_container_add (GES_CONTAINER (nclip),
715         GES_TIMELINE_ELEMENT (new_trackelement));
716
717     ges_track_element_copy_properties (GES_TIMELINE_ELEMENT (trackelement),
718         GES_TIMELINE_ELEMENT (new_trackelement));
719
720     ges_track_element_copy_bindings (trackelement, new_trackelement,
721         GST_CLOCK_TIME_NONE);
722   }
723
724   return GES_TIMELINE_ELEMENT (nclip);
725 }
726
727 static gboolean
728 _lookup_child (GESTimelineElement * self, const gchar * prop_name,
729     GObject ** child, GParamSpec ** pspec)
730 {
731   GList *tmp;
732
733   if (GES_TIMELINE_ELEMENT_CLASS (ges_clip_parent_class)->lookup_child (self,
734           prop_name, child, pspec))
735     return TRUE;
736
737   for (tmp = GES_CONTAINER_CHILDREN (self); tmp; tmp = tmp->next) {
738     if (ges_timeline_element_lookup_child (tmp->data, prop_name, child, pspec))
739       return TRUE;
740   }
741
742   return FALSE;
743 }
744
745 /****************************************************
746  *                                                  *
747  *    GObject virtual methods implementation        *
748  *                                                  *
749  ****************************************************/
750 static void
751 ges_clip_get_property (GObject * object, guint property_id,
752     GValue * value, GParamSpec * pspec)
753 {
754   GESClip *clip = GES_CLIP (object);
755
756   switch (property_id) {
757     case PROP_LAYER:
758       g_value_set_object (value, clip->priv->layer);
759       break;
760     case PROP_SUPPORTED_FORMATS:
761       g_value_set_flags (value, clip->priv->supportedformats);
762       break;
763     default:
764       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
765   }
766 }
767
768 static void
769 ges_clip_set_property (GObject * object, guint property_id,
770     const GValue * value, GParamSpec * pspec)
771 {
772   GESClip *clip = GES_CLIP (object);
773
774   switch (property_id) {
775     case PROP_SUPPORTED_FORMATS:
776       ges_clip_set_supported_formats (clip, g_value_get_flags (value));
777       break;
778     default:
779       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
780   }
781 }
782
783 static void
784 ges_clip_dispose (GObject * object)
785 {
786   GESClip *self = GES_CLIP (object);
787
788   g_list_free_full (self->priv->copied_track_elements, g_object_unref);
789   self->priv->copied_track_elements = NULL;
790   g_clear_object (&self->priv->copied_layer);
791
792   G_OBJECT_CLASS (ges_clip_parent_class)->dispose (object);
793 }
794
795
796 static void
797 ges_clip_class_init (GESClipClass * klass)
798 {
799   GObjectClass *object_class = G_OBJECT_CLASS (klass);
800   GESContainerClass *container_class = GES_CONTAINER_CLASS (klass);
801   GESTimelineElementClass *element_class = GES_TIMELINE_ELEMENT_CLASS (klass);
802
803   object_class->get_property = ges_clip_get_property;
804   object_class->set_property = ges_clip_set_property;
805   object_class->dispose = ges_clip_dispose;
806   klass->create_track_elements = ges_clip_create_track_elements_func;
807   klass->create_track_element = NULL;
808
809   /**
810    * GESClip:supported-formats:
811    *
812    * The formats supported by the clip.
813    */
814   properties[PROP_SUPPORTED_FORMATS] = g_param_spec_flags ("supported-formats",
815       "Supported formats", "Formats supported by the file",
816       GES_TYPE_TRACK_TYPE, GES_TRACK_TYPE_AUDIO | GES_TRACK_TYPE_VIDEO,
817       G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
818
819   g_object_class_install_property (object_class, PROP_SUPPORTED_FORMATS,
820       properties[PROP_SUPPORTED_FORMATS]);
821
822   /**
823    * GESClip:layer:
824    *
825    * The GESLayer where this clip is being used. If you want to connect to its
826    * notify signal you should connect to it with g_signal_connect_after as the
827    * signal emission can be stop in the first fase.
828    */
829   properties[PROP_LAYER] = g_param_spec_object ("layer", "Layer",
830       "The GESLayer where this clip is being used.",
831       GES_TYPE_LAYER, G_PARAM_READABLE);
832   g_object_class_install_property (object_class, PROP_LAYER,
833       properties[PROP_LAYER]);
834
835   element_class->ripple = _ripple;
836   element_class->ripple_end = _ripple_end;
837   element_class->roll_start = _roll_start;
838   element_class->roll_end = _roll_end;
839   element_class->trim = _trim;
840   element_class->set_start = _set_start;
841   element_class->set_duration = _set_duration;
842   element_class->set_inpoint = _set_inpoint;
843   element_class->set_priority = _set_priority;
844   element_class->set_max_duration = _set_max_duration;
845   element_class->paste = _paste;
846   element_class->deep_copy = _deep_copy;
847   element_class->lookup_child = _lookup_child;
848   element_class->get_layer_priority = _get_layer_priority;
849
850   container_class->add_child = _add_child;
851   container_class->remove_child = _remove_child;
852   container_class->child_removed = _child_removed;
853   container_class->child_added = _child_added;
854   container_class->ungroup = _ungroup;
855   container_class->group = _group;
856   container_class->grouping_priority = G_MAXUINT;
857   container_class->edit = _edit;
858 }
859
860 static void
861 ges_clip_init (GESClip * self)
862 {
863   self->priv = ges_clip_get_instance_private (self);
864   self->priv->layer = NULL;
865   self->priv->nb_effects = 0;
866 }
867
868 /**
869  * ges_clip_create_track_element:
870  * @clip: The origin #GESClip
871  * @type: The #GESTrackType to create a #GESTrackElement for.
872  *
873  * Creates a #GESTrackElement for the provided @type. The clip
874  * keep a reference to the newly created trackelement, you therefore need to
875  * call @ges_container_remove when you are done with it.
876  *
877  * Returns: (transfer none) (nullable): A #GESTrackElement. Returns NULL if
878  * the #GESTrackElement could not be created.
879  */
880 GESTrackElement *
881 ges_clip_create_track_element (GESClip * clip, GESTrackType type)
882 {
883   GESClipClass *class;
884   GESTrackElement *res;
885
886   g_return_val_if_fail (GES_IS_CLIP (clip), NULL);
887
888   GST_DEBUG_OBJECT (clip, "Creating track element for %s",
889       ges_track_type_name (type));
890   if (!(type & clip->priv->supportedformats)) {
891     GST_DEBUG_OBJECT (clip, "We don't support this track type %i", type);
892     return NULL;
893   }
894
895   class = GES_CLIP_GET_CLASS (clip);
896
897   if (G_UNLIKELY (class->create_track_element == NULL)) {
898     GST_ERROR ("No 'create_track_element' implementation available fo type %s",
899         G_OBJECT_TYPE_NAME (clip));
900     return NULL;
901   }
902
903   res = class->create_track_element (clip, type);
904   return res;
905
906 }
907
908 /**
909  * ges_clip_create_track_elements:
910  * @clip: The origin #GESClip
911  * @type: The #GESTrackType to create each #GESTrackElement for.
912  *
913  * Creates all #GESTrackElements supported by this clip for the track type.
914  *
915  * Returns: (element-type GESTrackElement) (transfer full): A #GList of
916  * newly created #GESTrackElement-s
917  */
918
919 GList *
920 ges_clip_create_track_elements (GESClip * clip, GESTrackType type)
921 {
922   GList *result = NULL, *tmp, *children;
923   GESClipClass *klass;
924   guint max_prio, min_prio;
925   gboolean readding_effects_only = TRUE;
926
927   g_return_val_if_fail (GES_IS_CLIP (clip), NULL);
928
929   klass = GES_CLIP_GET_CLASS (clip);
930
931   if (!(klass->create_track_elements)) {
932     GST_WARNING ("no GESClip::create_track_elements implentation");
933     return NULL;
934   }
935
936   GST_DEBUG_OBJECT (clip, "Creating TrackElements for type: %s",
937       ges_track_type_name (type));
938   children = ges_container_get_children (GES_CONTAINER (clip), TRUE);
939   for (tmp = children; tmp; tmp = tmp->next) {
940     GESTrackElement *child = GES_TRACK_ELEMENT (tmp->data);
941
942     if (!ges_track_element_get_track (child)
943         && ges_track_element_get_track_type (child) & type) {
944
945       GST_DEBUG_OBJECT (clip, "Removing for reusage: %" GST_PTR_FORMAT, child);
946       result = g_list_append (result, g_object_ref (child));
947       ges_container_remove (GES_CONTAINER (clip), tmp->data);
948       if (!GES_IS_BASE_EFFECT (child))
949         readding_effects_only = FALSE;
950     }
951   }
952   g_list_free_full (children, gst_object_unref);
953
954   if (readding_effects_only) {
955     result = g_list_concat (result, klass->create_track_elements (clip, type));
956   }
957
958   _get_priority_range (GES_CONTAINER (clip), &min_prio, &max_prio);
959   for (tmp = result; tmp; tmp = tmp->next) {
960     GESTimelineElement *elem = tmp->data;
961
962     _set_start0 (elem, GES_TIMELINE_ELEMENT_START (clip));
963     _set_inpoint0 (elem, GES_TIMELINE_ELEMENT_INPOINT (clip));
964     _set_duration0 (elem, GES_TIMELINE_ELEMENT_DURATION (clip));
965
966     if (GST_CLOCK_TIME_IS_VALID (GES_TIMELINE_ELEMENT_MAX_DURATION (clip)))
967       ges_timeline_element_set_max_duration (GES_TIMELINE_ELEMENT (elem),
968           GES_TIMELINE_ELEMENT_MAX_DURATION (clip));
969
970     _set_priority0 (elem, min_prio + clip->priv->nb_effects);
971
972     ges_container_add (GES_CONTAINER (clip), elem);
973   }
974
975   return result;
976 }
977
978 /*
979  * default implementation of GESClipClass::create_track_elements
980  */
981 GList *
982 ges_clip_create_track_elements_func (GESClip * clip, GESTrackType type)
983 {
984   GESTrackElement *result;
985
986   GST_DEBUG_OBJECT (clip, "Creating trackelement for track: %s",
987       ges_track_type_name (type));
988   result = ges_clip_create_track_element (clip, type);
989   if (!result) {
990     GST_DEBUG ("Did not create track element");
991     return NULL;
992   }
993
994   return g_list_append (NULL, result);
995 }
996
997 void
998 ges_clip_set_layer (GESClip * clip, GESLayer * layer)
999 {
1000   if (layer == clip->priv->layer)
1001     return;
1002
1003   clip->priv->layer = layer;
1004
1005   GST_DEBUG ("clip:%p, layer:%p", clip, layer);
1006
1007   /* We do not want to notify the setting of layer = NULL when
1008    * it is actually the result of a move between layer (as we know
1009    * that it will be added to another layer right after, and this
1010    * is what imports here.) */
1011   if (!ELEMENT_FLAG_IS_SET (clip, GES_CLIP_IS_MOVING))
1012     g_object_notify_by_pspec (G_OBJECT (clip), properties[PROP_LAYER]);
1013 }
1014
1015 /**
1016  * ges_clip_set_moving_from_layer:
1017  * @clip: a #GESClip
1018  * @is_moving: %TRUE if you want to start moving @clip to another layer
1019  * %FALSE when you finished moving it.
1020  *
1021  * Sets the clip in a moving to layer state. You might rather use the
1022  * ges_clip_move_to_layer function to move #GESClip-s
1023  * from a layer to another.
1024  **/
1025 void
1026 ges_clip_set_moving_from_layer (GESClip * clip, gboolean is_moving)
1027 {
1028   g_return_if_fail (GES_IS_CLIP (clip));
1029
1030   if (is_moving)
1031     ELEMENT_SET_FLAG (clip, GES_CLIP_IS_MOVING);
1032   else
1033     ELEMENT_UNSET_FLAG (clip, GES_CLIP_IS_MOVING);
1034 }
1035
1036 /**
1037  * ges_clip_is_moving_from_layer:
1038  * @clip: a #GESClip
1039  *
1040  * Tells you if the clip is currently moving from a layer to another.
1041  * You might rather use the ges_clip_move_to_layer function to
1042  * move #GESClip-s from a layer to another.
1043  *
1044  * Returns: %TRUE if @clip is currently moving from its current layer
1045  * %FALSE otherwize
1046  **/
1047 gboolean
1048 ges_clip_is_moving_from_layer (GESClip * clip)
1049 {
1050   g_return_val_if_fail (GES_IS_CLIP (clip), FALSE);
1051
1052   return ELEMENT_FLAG_IS_SET (clip, GES_CLIP_IS_MOVING);
1053 }
1054
1055 /**
1056  * ges_clip_move_to_layer:
1057  * @clip: a #GESClip
1058  * @layer: the new #GESLayer
1059  *
1060  * Moves @clip to @layer. If @clip is not in any layer, it adds it to
1061  * @layer, else, it removes it from its current layer, and adds it to @layer.
1062  *
1063  * Returns: %TRUE if @clip could be moved %FALSE otherwize
1064  */
1065 gboolean
1066 ges_clip_move_to_layer (GESClip * clip, GESLayer * layer)
1067 {
1068   gboolean ret;
1069   GESLayer *current_layer;
1070
1071   g_return_val_if_fail (GES_IS_CLIP (clip), FALSE);
1072   g_return_val_if_fail (GES_IS_LAYER (layer), FALSE);
1073
1074   ELEMENT_SET_FLAG (clip, GES_CLIP_IS_MOVING);
1075   if (layer->timeline
1076       && !timeline_tree_can_move_element (timeline_get_tree (layer->timeline),
1077           GES_TIMELINE_ELEMENT (clip),
1078           ges_layer_get_priority (layer),
1079           GES_TIMELINE_ELEMENT_START (clip),
1080           GES_TIMELINE_ELEMENT_DURATION (clip), NULL)) {
1081     GST_INFO_OBJECT (layer, "Clip %" GES_FORMAT " can't move to layer %d",
1082         GES_ARGS (clip), ges_layer_get_priority (layer));
1083     ELEMENT_UNSET_FLAG (clip, GES_CLIP_IS_MOVING);
1084     return FALSE;
1085   }
1086
1087   current_layer = clip->priv->layer;
1088
1089   if (current_layer == NULL) {
1090     GST_DEBUG ("Not moving %p, only adding it to %p", clip, layer);
1091
1092     return ges_layer_add_clip (layer, clip);
1093   }
1094
1095   GST_DEBUG_OBJECT (clip, "moving to layer %p, priority: %d", layer,
1096       ges_layer_get_priority (layer));
1097
1098   gst_object_ref (clip);
1099   ret = ges_layer_remove_clip (current_layer, clip);
1100
1101   if (!ret) {
1102     ELEMENT_UNSET_FLAG (clip, GES_CLIP_IS_MOVING);
1103     gst_object_unref (clip);
1104     return FALSE;
1105   }
1106
1107   ret = ges_layer_add_clip (layer, clip);
1108   ELEMENT_UNSET_FLAG (clip, GES_CLIP_IS_MOVING);
1109
1110   gst_object_unref (clip);
1111   g_object_notify_by_pspec (G_OBJECT (clip), properties[PROP_LAYER]);
1112
1113
1114   return ret && (clip->priv->layer == layer);
1115 }
1116
1117 /**
1118  * ges_clip_find_track_element:
1119  * @clip: a #GESClip
1120  * @track: (allow-none): a #GESTrack or NULL
1121  * @type: a #GType indicating the type of track element you are looking
1122  * for or %G_TYPE_NONE if you do not care about the track type.
1123  *
1124  * Finds the #GESTrackElement controlled by @clip that is used in @track. You
1125  * may optionally specify a GType to further narrow search criteria.
1126  *
1127  * Note: If many objects match, then the one with the highest priority will be
1128  * returned.
1129  *
1130  * Returns: (transfer full) (nullable): The #GESTrackElement used by @track,
1131  * else %NULL. Unref after usage
1132  */
1133
1134 GESTrackElement *
1135 ges_clip_find_track_element (GESClip * clip, GESTrack * track, GType type)
1136 {
1137   GList *tmp;
1138   GESTrackElement *otmp;
1139
1140   GESTrackElement *ret = NULL;
1141
1142   g_return_val_if_fail (GES_IS_CLIP (clip), NULL);
1143   g_return_val_if_fail (!(track == NULL && type == G_TYPE_NONE), NULL);
1144
1145   for (tmp = GES_CONTAINER_CHILDREN (clip); tmp; tmp = g_list_next (tmp)) {
1146     otmp = (GESTrackElement *) tmp->data;
1147
1148     if ((type != G_TYPE_NONE) && !G_TYPE_CHECK_INSTANCE_TYPE (tmp->data, type))
1149       continue;
1150
1151     if ((track == NULL) || (ges_track_element_get_track (otmp) == track)) {
1152       ret = GES_TRACK_ELEMENT (tmp->data);
1153       gst_object_ref (ret);
1154       break;
1155     }
1156   }
1157
1158   return ret;
1159 }
1160
1161 /**
1162  * ges_clip_get_layer:
1163  * @clip: a #GESClip
1164  *
1165  * Get the #GESLayer to which this clip belongs.
1166  *
1167  * Returns: (transfer full) (nullable): The #GESLayer where this @clip is being
1168  * used, or %NULL if it is not used on any layer. The caller should unref it
1169  * usage.
1170  */
1171 GESLayer *
1172 ges_clip_get_layer (GESClip * clip)
1173 {
1174   g_return_val_if_fail (GES_IS_CLIP (clip), NULL);
1175
1176   if (clip->priv->layer != NULL)
1177     gst_object_ref (G_OBJECT (clip->priv->layer));
1178
1179   return clip->priv->layer;
1180 }
1181
1182 /**
1183  * ges_clip_get_top_effects:
1184  * @clip: The origin #GESClip
1185  *
1186  * Get effects applied on @clip
1187  *
1188  * Returns: (transfer full) (element-type GESTrackElement): a #GList of the
1189  * #GESBaseEffect that are applied on @clip order by ascendant priorities.
1190  * The refcount of the objects will be increased. The user will have to
1191  * unref each #GESBaseEffect and free the #GList.
1192  */
1193 GList *
1194 ges_clip_get_top_effects (GESClip * clip)
1195 {
1196   GList *tmp, *ret;
1197   guint i;
1198
1199   g_return_val_if_fail (GES_IS_CLIP (clip), NULL);
1200
1201   GST_DEBUG_OBJECT (clip, "Getting the %i top effects", clip->priv->nb_effects);
1202   ret = NULL;
1203
1204   for (tmp = GES_CONTAINER_CHILDREN (clip), i = 0;
1205       i < clip->priv->nb_effects; tmp = tmp->next, i++) {
1206     ret = g_list_append (ret, gst_object_ref (tmp->data));
1207   }
1208
1209   return g_list_sort (ret, (GCompareFunc) element_start_compare);
1210 }
1211
1212 /**
1213  * ges_clip_get_top_effect_index:
1214  * @clip: The origin #GESClip
1215  * @effect: The #GESBaseEffect we want to get the top index from
1216  *
1217  * Gets the index position of an effect.
1218  *
1219  * Returns: The top index of the effect, -1 if something went wrong.
1220  */
1221 gint
1222 ges_clip_get_top_effect_index (GESClip * clip, GESBaseEffect * effect)
1223 {
1224   guint max_prio, min_prio;
1225
1226   g_return_val_if_fail (GES_IS_CLIP (clip), -1);
1227   g_return_val_if_fail (GES_IS_BASE_EFFECT (effect), -1);
1228
1229   _get_priority_range (GES_CONTAINER (clip), &min_prio, &max_prio);
1230
1231   return GES_TIMELINE_ELEMENT_PRIORITY (effect) - min_prio;
1232 }
1233
1234 /* TODO 2.0 remove as it is Deprecated */
1235 gint
1236 ges_clip_get_top_effect_position (GESClip * clip, GESBaseEffect * effect)
1237 {
1238   return ges_clip_get_top_effect_index (clip, effect);
1239 }
1240
1241 /* TODO 2.0 remove as it is Deprecated */
1242 gboolean
1243 ges_clip_set_top_effect_priority (GESClip * clip,
1244     GESBaseEffect * effect, guint newpriority)
1245 {
1246   return ges_clip_set_top_effect_index (clip, effect, newpriority);
1247 }
1248
1249 /**
1250  * ges_clip_set_top_effect_index:
1251  * @clip: The origin #GESClip
1252  * @effect: The #GESBaseEffect to move
1253  * @newindex: the new index at which to move the @effect inside this
1254  * #GESClip
1255  *
1256  * This is a convenience method that lets you set the index of a top effect.
1257  *
1258  * Returns: %TRUE if @effect was successfuly moved, %FALSE otherwise.
1259  */
1260 gboolean
1261 ges_clip_set_top_effect_index (GESClip * clip, GESBaseEffect * effect,
1262     guint newindex)
1263 {
1264   gint inc;
1265   GList *tmp;
1266   guint current_prio, min_prio, max_prio;
1267   GESTrackElement *track_element;
1268
1269   g_return_val_if_fail (GES_IS_CLIP (clip), FALSE);
1270
1271   track_element = GES_TRACK_ELEMENT (effect);
1272   if (G_UNLIKELY (GES_CLIP (GES_TIMELINE_ELEMENT_PARENT (track_element)) !=
1273           clip))
1274     return FALSE;
1275
1276   current_prio = _PRIORITY (track_element);
1277
1278   _get_priority_range (GES_CONTAINER (clip), &min_prio, &max_prio);
1279
1280   newindex = newindex + min_prio;
1281   /*  We don't change the priority */
1282   if (current_prio == newindex)
1283     return TRUE;
1284
1285   if (newindex > (clip->priv->nb_effects - 1 + min_prio)) {
1286     GST_DEBUG ("You are trying to make %p not a top effect", effect);
1287     return FALSE;
1288   }
1289
1290   if (current_prio > clip->priv->nb_effects + min_prio) {
1291     GST_ERROR ("%p is not a top effect", effect);
1292     return FALSE;
1293   }
1294
1295   _ges_container_sort_children (GES_CONTAINER (clip));
1296   if (_PRIORITY (track_element) < newindex)
1297     inc = -1;
1298   else
1299     inc = +1;
1300
1301   GST_DEBUG_OBJECT (clip, "Setting top effect %" GST_PTR_FORMAT "priority: %i",
1302       effect, newindex);
1303
1304   for (tmp = GES_CONTAINER_CHILDREN (clip); tmp; tmp = tmp->next) {
1305     GESTrackElement *tmpo = GES_TRACK_ELEMENT (tmp->data);
1306     guint tck_priority = _PRIORITY (tmpo);
1307
1308     if (tmpo == track_element)
1309       continue;
1310
1311     if ((inc == +1 && tck_priority >= newindex) ||
1312         (inc == -1 && tck_priority <= newindex)) {
1313       _set_priority0 (GES_TIMELINE_ELEMENT (tmpo), tck_priority + inc);
1314     }
1315   }
1316   _set_priority0 (GES_TIMELINE_ELEMENT (track_element), newindex);
1317
1318   return TRUE;
1319 }
1320
1321 /**
1322  * ges_clip_split:
1323  * @clip: the #GESClip to split
1324  * @position: a #GstClockTime representing the timeline position at which to split
1325  *
1326  * The function modifies @clip, and creates another #GESClip so we have two
1327  * clips at the end, splitted at the time specified by @position, as a position
1328  * in the timeline (not in the clip to be split). For example, if
1329  * ges_clip_split is called on a 4-second clip playing from 0:01.00 until
1330  * 0:05.00, with a split position of 0:02.00, this will result in one clip of 1
1331  * second and one clip of 3 seconds, not in two clips of 2 seconds.
1332  *
1333  * The newly created clip will be added to the same layer as @clip is in. This
1334  * implies that @clip must be in a #GESLayer for the operation to be possible.
1335  *
1336  * This method supports clips playing at a different tempo than one second per
1337  * second. For example, splitting a clip with a #GESEffect 'pitch tempo=1.5'
1338  * four seconds after it starts, will set the inpoint of the new clip to six
1339  * seconds after that of the clip to split. For this, the rate-changing
1340  * property must be registered using @ges_effect_class_register_rate_property;
1341  * for the 'pitch' plugin, this is already done.
1342  *
1343  * Returns: (transfer none) (nullable): The newly created #GESClip resulting
1344  * from the splitting or %NULL if the clip can't be split.
1345  */
1346 GESClip *
1347 ges_clip_split (GESClip * clip, guint64 position)
1348 {
1349   GList *tmp;
1350   GESClip *new_object;
1351   GstClockTime start, inpoint, duration, old_duration, new_duration;
1352   gdouble media_duration_factor;
1353
1354   g_return_val_if_fail (GES_IS_CLIP (clip), NULL);
1355   g_return_val_if_fail (clip->priv->layer, NULL);
1356   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (position), NULL);
1357
1358   duration = _DURATION (clip);
1359   start = _START (clip);
1360   inpoint = _INPOINT (clip);
1361
1362   if (position >= start + duration || position <= start) {
1363     GST_WARNING_OBJECT (clip, "Can not split %" GST_TIME_FORMAT
1364         " out of boundaries", GST_TIME_ARGS (position));
1365     return NULL;
1366   }
1367
1368   GST_DEBUG_OBJECT (clip, "Spliting at %" GST_TIME_FORMAT,
1369       GST_TIME_ARGS (position));
1370
1371   /* Create the new Clip */
1372   new_object = GES_CLIP (ges_timeline_element_copy (GES_TIMELINE_ELEMENT (clip),
1373           FALSE));
1374
1375   GST_DEBUG_OBJECT (new_object, "New 'splitted' clip");
1376   /* Set new timing properties on the Clip */
1377   media_duration_factor =
1378       ges_timeline_element_get_media_duration_factor (GES_TIMELINE_ELEMENT
1379       (clip));
1380   new_duration = duration + start - position;
1381   old_duration = position - start;
1382   _set_start0 (GES_TIMELINE_ELEMENT (new_object), position);
1383   _set_inpoint0 (GES_TIMELINE_ELEMENT (new_object),
1384       inpoint + old_duration * media_duration_factor);
1385   _set_duration0 (GES_TIMELINE_ELEMENT (new_object), new_duration);
1386
1387   _DURATION (clip) = old_duration;
1388   g_object_notify (G_OBJECT (clip), "duration");
1389
1390   /* We do not want the timeline to create again TrackElement-s */
1391   ges_clip_set_moving_from_layer (new_object, TRUE);
1392   ges_layer_add_clip (clip->priv->layer, new_object);
1393   ges_clip_set_moving_from_layer (new_object, FALSE);
1394
1395   for (tmp = GES_CONTAINER_CHILDREN (clip); tmp; tmp = tmp->next) {
1396     GESTrackElement *new_trackelement, *trackelement =
1397         GES_TRACK_ELEMENT (tmp->data);
1398
1399     new_trackelement =
1400         GES_TRACK_ELEMENT (ges_timeline_element_copy (GES_TIMELINE_ELEMENT
1401             (trackelement), FALSE));
1402     if (new_trackelement == NULL) {
1403       GST_WARNING_OBJECT (trackelement, "Could not create a copy");
1404       continue;
1405     }
1406
1407     /* Set 'new' track element timing propeties */
1408     _set_start0 (GES_TIMELINE_ELEMENT (new_trackelement), position);
1409     _set_inpoint0 (GES_TIMELINE_ELEMENT (new_trackelement),
1410         inpoint + old_duration * media_duration_factor);
1411     _set_duration0 (GES_TIMELINE_ELEMENT (new_trackelement), new_duration);
1412
1413     ges_container_add (GES_CONTAINER (new_object),
1414         GES_TIMELINE_ELEMENT (new_trackelement));
1415     ges_track_element_copy_properties (GES_TIMELINE_ELEMENT (trackelement),
1416         GES_TIMELINE_ELEMENT (new_trackelement));
1417
1418     ges_track_element_copy_bindings (trackelement, new_trackelement,
1419         position - start + inpoint);
1420   }
1421
1422   ELEMENT_SET_FLAG (clip, GES_TIMELINE_ELEMENT_SET_SIMPLE);
1423   _DURATION (clip) = duration;
1424   _set_duration0 (GES_TIMELINE_ELEMENT (clip), old_duration);
1425   ELEMENT_UNSET_FLAG (clip, GES_TIMELINE_ELEMENT_SET_SIMPLE);
1426
1427   return new_object;
1428 }
1429
1430 /**
1431  * ges_clip_set_supported_formats:
1432  * @clip: the #GESClip to set supported formats on
1433  * @supportedformats: the #GESTrackType defining formats supported by @clip
1434  *
1435  * Sets the formats supported by the file.
1436  */
1437 void
1438 ges_clip_set_supported_formats (GESClip * clip, GESTrackType supportedformats)
1439 {
1440   g_return_if_fail (GES_IS_CLIP (clip));
1441
1442   clip->priv->supportedformats = supportedformats;
1443 }
1444
1445 /**
1446  * ges_clip_get_supported_formats:
1447  * @clip: the #GESClip
1448  *
1449  * Get the formats supported by @clip.
1450  *
1451  * Returns: The formats supported by @clip.
1452  */
1453 GESTrackType
1454 ges_clip_get_supported_formats (GESClip * clip)
1455 {
1456   g_return_val_if_fail (GES_IS_CLIP (clip), GES_TRACK_TYPE_UNKNOWN);
1457
1458   return clip->priv->supportedformats;
1459 }
1460
1461 gboolean
1462 _ripple (GESTimelineElement * element, GstClockTime start)
1463 {
1464   return ges_container_edit (GES_CONTAINER (element), NULL,
1465       ges_timeline_element_get_layer_priority (element),
1466       GES_EDIT_MODE_RIPPLE, GES_EDGE_NONE, start);
1467 }
1468
1469 static gboolean
1470 _ripple_end (GESTimelineElement * element, GstClockTime end)
1471 {
1472   return ges_container_edit (GES_CONTAINER (element), NULL,
1473       ges_timeline_element_get_layer_priority (element),
1474       GES_EDIT_MODE_RIPPLE, GES_EDGE_END, end);
1475 }
1476
1477 gboolean
1478 _roll_start (GESTimelineElement * element, GstClockTime start)
1479 {
1480   return ges_container_edit (GES_CONTAINER (element), NULL,
1481       ges_timeline_element_get_layer_priority (element),
1482       GES_EDIT_MODE_ROLL, GES_EDGE_START, start);
1483 }
1484
1485 gboolean
1486 _roll_end (GESTimelineElement * element, GstClockTime end)
1487 {
1488   return ges_container_edit (GES_CONTAINER (element), NULL,
1489       ges_timeline_element_get_layer_priority (element),
1490       GES_EDIT_MODE_ROLL, GES_EDGE_END, end);
1491 }
1492
1493 gboolean
1494 _trim (GESTimelineElement * element, GstClockTime start)
1495 {
1496   return ges_container_edit (GES_CONTAINER (element), NULL, -1,
1497       GES_EDIT_MODE_TRIM, GES_EDGE_START, start);
1498 }
1499
1500 /**
1501  * ges_clip_add_asset:
1502  * @clip: a #GESClip
1503  * @asset: a #GESAsset with #GES_TYPE_TRACK_ELEMENT as extractable_type
1504  *
1505  * Extracts a #GESTrackElement from @asset and adds it to the @clip.
1506  * Should only be called in order to add operations to a #GESClip,
1507  * ni other cases TrackElement are added automatically when adding the
1508  * #GESClip/#GESAsset to a layer.
1509  *
1510  * Takes a reference on @track_element.
1511  *
1512  * Returns: (transfer none)(allow-none): Created #GESTrackElement or NULL
1513  * if an error happened
1514  */
1515 GESTrackElement *
1516 ges_clip_add_asset (GESClip * clip, GESAsset * asset)
1517 {
1518   GESTrackElement *element;
1519
1520   g_return_val_if_fail (GES_IS_CLIP (clip), NULL);
1521   g_return_val_if_fail (GES_IS_ASSET (asset), NULL);
1522   g_return_val_if_fail (g_type_is_a (ges_asset_get_extractable_type
1523           (asset), GES_TYPE_TRACK_ELEMENT), NULL);
1524
1525   element = GES_TRACK_ELEMENT (ges_asset_extract (asset, NULL));
1526
1527   if (!ges_container_add (GES_CONTAINER (clip), GES_TIMELINE_ELEMENT (element)))
1528     return NULL;
1529
1530   return element;
1531
1532 }
1533
1534 /**
1535  * ges_clip_find_track_elements:
1536  * @clip: a #GESClip
1537  * @track: (allow-none): a #GESTrack or NULL
1538  * @track_type: a #GESTrackType indicating the type of tracks in which elements
1539  * should be searched.
1540  * @type: a #GType indicating the type of track element you are looking
1541  * for or %G_TYPE_NONE if you do not care about the track type.
1542  *
1543  * Finds all the #GESTrackElement controlled by @clip that is used in @track. You
1544  * may optionally specify a GType to further narrow search criteria.
1545  *
1546  * Returns: (transfer full) (element-type GESTrackElement): a #GList of the
1547  * #GESTrackElement contained in @clip.
1548  * The refcount of the objects will be increased. The user will have to
1549  * unref each #GESTrackElement and free the #GList.
1550  */
1551
1552 GList *
1553 ges_clip_find_track_elements (GESClip * clip, GESTrack * track,
1554     GESTrackType track_type, GType type)
1555 {
1556   GList *tmp;
1557   GESTrack *tmptrack;
1558   GESTrackElement *otmp;
1559   GESTrackElement *foundElement;
1560
1561   GList *ret = NULL;
1562
1563   g_return_val_if_fail (GES_IS_CLIP (clip), NULL);
1564   g_return_val_if_fail (!(track == NULL && type == G_TYPE_NONE &&
1565           track_type == GES_TRACK_TYPE_UNKNOWN), NULL);
1566
1567   for (tmp = GES_CONTAINER_CHILDREN (clip); tmp; tmp = g_list_next (tmp)) {
1568     otmp = (GESTrackElement *) tmp->data;
1569
1570     if ((type != G_TYPE_NONE) && !G_TYPE_CHECK_INSTANCE_TYPE (tmp->data, type))
1571       continue;
1572
1573     tmptrack = ges_track_element_get_track (otmp);
1574     if (((track != NULL && tmptrack == track)) ||
1575         (track_type != GES_TRACK_TYPE_UNKNOWN
1576             && ges_track_element_get_track_type (otmp) == track_type)) {
1577
1578       foundElement = GES_TRACK_ELEMENT (tmp->data);
1579
1580       ret = g_list_append (ret, gst_object_ref (foundElement));
1581     }
1582   }
1583
1584   return ret;
1585 }