GES : remove transitions when needed
[platform/upstream/gstreamer.git] / ges / ges-timeline.c
1 /* GStreamer Editing Services
2  * Copyright (C) 2009 Edward Hervey <edward.hervey@collabora.co.uk>
3  *               2009 Nokia Corporation
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * SECTION:ges-timeline
23  * @short_description: Multimedia timeline
24  *
25  * #GESTimeline is the central object for any multimedia timeline.
26  *
27  * Contains a list of #GESTimelineLayer which users should use to arrange the
28  * various timeline objects through time.
29  *
30  * The output type is determined by the #GESTrack that are set on
31  * the #GESTimeline.
32  *
33  * To save/load a timeline, you can use the ges_timeline_load_from_uri() and
34  * ges_timeline_save_to_uri() methods to use the default format. If you wish
35  * to specify the format to save/load the timeline from, please consult the
36  * documentation about #GESFormatter.
37  */
38
39 #include "gesmarshal.h"
40 #include "ges-internal.h"
41 #include "ges-timeline.h"
42 #include "ges-track.h"
43 #include "ges-timeline-layer.h"
44 #include "ges.h"
45
46 static void track_duration_cb (GstElement * track,
47     GParamSpec * arg G_GNUC_UNUSED, GESTimeline * timeline);
48
49 G_DEFINE_TYPE (GESTimeline, ges_timeline, GST_TYPE_BIN);
50
51 #define GES_TIMELINE_PENDINGOBJS_GET_LOCK(timeline) \
52   (GES_TIMELINE(timeline)->priv->pendingobjects_lock)
53 #define GES_TIMELINE_PENDINGOBJS_LOCK(timeline) \
54   (g_mutex_lock(GES_TIMELINE_PENDINGOBJS_GET_LOCK (timeline)))
55 #define GES_TIMELINE_PENDINGOBJS_UNLOCK(timeline) \
56   (g_mutex_unlock(GES_TIMELINE_PENDINGOBJS_GET_LOCK (timeline)))
57
58 struct _GESTimelinePrivate
59 {
60   GList *layers;                /* A list of GESTimelineLayer sorted by priority */
61   GList *tracks;                /* A list of private track data */
62
63   /* The duration of the timeline */
64   gint64 duration;
65
66   /* discoverer used for virgin sources */
67   GstDiscoverer *discoverer;
68   GList *pendingobjects;
69   /* lock to avoid discovery of objects that will be removed */
70   GMutex *pendingobjects_lock;
71
72   /* Whether we are changing state asynchronously or not */
73   gboolean async_pending;
74 };
75
76 /* private structure to contain our track-related information */
77
78 typedef struct
79 {
80   GESTimeline *timeline;
81   GESTrack *track;
82   GstPad *pad;                  /* Pad from the track */
83   GstPad *ghostpad;
84 } TrackPrivate;
85
86 enum
87 {
88   PROP_0,
89   PROP_DURATION,
90   PROP_LAST
91 };
92
93 static GParamSpec *properties[PROP_LAST];
94
95 enum
96 {
97   TRACK_ADDED,
98   TRACK_REMOVED,
99   LAYER_ADDED,
100   LAYER_REMOVED,
101   LAST_SIGNAL
102 };
103
104 static GstBinClass *parent_class;
105
106 static guint ges_timeline_signals[LAST_SIGNAL] = { 0 };
107
108 static gint custom_find_track (TrackPrivate * tr_priv, GESTrack * track);
109 static GstStateChangeReturn
110 ges_timeline_change_state (GstElement * element, GstStateChange transition);
111 static void
112 discoverer_finished_cb (GstDiscoverer * discoverer, GESTimeline * timeline);
113 static void
114 discoverer_discovered_cb (GstDiscoverer * discoverer,
115     GstDiscovererInfo * info, GError * err, GESTimeline * timeline);
116
117 void look_for_transition (GESTrackObject * track_object,
118     GESTimelineLayer * layer);
119 void track_object_removed_cb (GESTrack * track, GESTrackObject * track_object,
120     GESTimeline * timeline);
121
122 static void
123 ges_timeline_get_property (GObject * object, guint property_id,
124     GValue * value, GParamSpec * pspec)
125 {
126   GESTimeline *timeline = GES_TIMELINE (object);
127
128   switch (property_id) {
129     default:
130       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
131     case PROP_DURATION:
132       g_value_set_uint64 (value, timeline->priv->duration);
133       break;
134   }
135 }
136
137 static void
138 ges_timeline_set_property (GObject * object, guint property_id,
139     const GValue * value, GParamSpec * pspec)
140 {
141   switch (property_id) {
142     default:
143       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
144   }
145 }
146
147 static void
148 ges_timeline_dispose (GObject * object)
149 {
150   GESTimelinePrivate *priv = GES_TIMELINE (object)->priv;
151
152   if (priv->discoverer) {
153     gst_discoverer_stop (priv->discoverer);
154     g_object_unref (priv->discoverer);
155     priv->discoverer = NULL;
156   }
157
158   while (priv->layers) {
159     GESTimelineLayer *layer = (GESTimelineLayer *) priv->layers->data;
160     ges_timeline_remove_layer (GES_TIMELINE (object), layer);
161   }
162
163   /* FIXME: it should be possible to remove tracks before removing
164    * layers, but at the moment this creates a problem because the track
165    * objects aren't notified that their gnlobjects have been destroyed.
166    */
167
168   while (priv->tracks) {
169     TrackPrivate *tr_priv = (TrackPrivate *) priv->tracks->data;
170     ges_timeline_remove_track (GES_TIMELINE (object), tr_priv->track);
171   }
172
173   G_OBJECT_CLASS (ges_timeline_parent_class)->dispose (object);
174 }
175
176 static void
177 ges_timeline_finalize (GObject * object)
178 {
179   GESTimeline *timeline = GES_TIMELINE (object);
180
181   g_mutex_free (timeline->priv->pendingobjects_lock);
182
183   G_OBJECT_CLASS (ges_timeline_parent_class)->finalize (object);
184 }
185
186 static void
187 ges_timeline_class_init (GESTimelineClass * klass)
188 {
189   GObjectClass *object_class = G_OBJECT_CLASS (klass);
190   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
191
192   g_type_class_add_private (klass, sizeof (GESTimelinePrivate));
193
194   parent_class = g_type_class_peek_parent (klass);
195
196   element_class->change_state = ges_timeline_change_state;
197
198   object_class->get_property = ges_timeline_get_property;
199   object_class->set_property = ges_timeline_set_property;
200   object_class->dispose = ges_timeline_dispose;
201   object_class->finalize = ges_timeline_finalize;
202
203   /**
204    * GESTimelineObject:duration
205    *
206    * Current duration (in nanoseconds) of the #GESTimeline
207    *
208    * Default value: 0
209    */
210   properties[PROP_DURATION] =
211       g_param_spec_uint64 ("duration", "Duration",
212       "The duration of the timeline", 0, G_MAXUINT64,
213       GST_CLOCK_TIME_NONE, G_PARAM_READABLE);
214   g_object_class_install_property (object_class, PROP_DURATION,
215       properties[PROP_DURATION]);
216
217   /**
218    * GESTimeline::track-added
219    * @timeline: the #GESTimeline
220    * @track: the #GESTrack that was added to the timeline
221    *
222    * Will be emitted after the track was added to the timeline.
223    */
224   ges_timeline_signals[TRACK_ADDED] =
225       g_signal_new ("track-added", G_TYPE_FROM_CLASS (klass),
226       G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, track_added), NULL,
227       NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TRACK);
228
229   /**
230    * GESTimeline::track-removed
231    * @timeline: the #GESTimeline
232    * @track: the #GESTrack that was removed from the timeline
233    *
234    * Will be emitted after the track was removed from the timeline.
235    */
236   ges_timeline_signals[TRACK_REMOVED] =
237       g_signal_new ("track-removed", G_TYPE_FROM_CLASS (klass),
238       G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, track_removed),
239       NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TRACK);
240
241   /**
242    * GESTimeline::layer-added
243    * @timeline: the #GESTimeline
244    * @layer: the #GESTimelineLayer that was added to the timeline
245    *
246    * Will be emitted after the layer was added to the timeline.
247    */
248   ges_timeline_signals[LAYER_ADDED] =
249       g_signal_new ("layer-added", G_TYPE_FROM_CLASS (klass),
250       G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, layer_added), NULL,
251       NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TIMELINE_LAYER);
252
253   /**
254    * GESTimeline::layer-removed
255    * @timeline: the #GESTimeline
256    * @layer: the #GESTimelineLayer that was removed from the timeline
257    *
258    * Will be emitted after the layer was removed from the timeline.
259    */
260   ges_timeline_signals[LAYER_REMOVED] =
261       g_signal_new ("layer-removed", G_TYPE_FROM_CLASS (klass),
262       G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, layer_removed),
263       NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
264       GES_TYPE_TIMELINE_LAYER);
265 }
266
267 static void
268 ges_timeline_init (GESTimeline * self)
269 {
270
271   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
272       GES_TYPE_TIMELINE, GESTimelinePrivate);
273
274   self->priv->layers = NULL;
275   self->priv->tracks = NULL;
276   self->priv->duration = 0;
277
278   self->priv->pendingobjects_lock = g_mutex_new ();
279   /* New discoverer with a 15s timeout */
280   self->priv->discoverer = gst_discoverer_new (15 * GST_SECOND, NULL);
281   g_signal_connect (self->priv->discoverer, "finished",
282       G_CALLBACK (discoverer_finished_cb), self);
283   g_signal_connect (self->priv->discoverer, "discovered",
284       G_CALLBACK (discoverer_discovered_cb), self);
285   gst_discoverer_start (self->priv->discoverer);
286 }
287
288 static gint
289 sort_layers (gpointer a, gpointer b)
290 {
291   GESTimelineLayer *layer_a, *layer_b;
292   guint prio_a, prio_b;
293
294   layer_a = GES_TIMELINE_LAYER (a);
295   layer_b = GES_TIMELINE_LAYER (b);
296
297   prio_a = ges_timeline_layer_get_priority (layer_a);
298   prio_b = ges_timeline_layer_get_priority (layer_b);
299
300   if ((gint) prio_a > (guint) prio_b)
301     return 1;
302   if ((guint) prio_a < (guint) prio_b)
303     return -1;
304
305   return 0;
306 }
307
308 /**
309  * ges_timeline_new:
310  *
311  * Creates a new empty #GESTimeline.
312  *
313  * Returns: The new timeline.
314  */
315
316 GESTimeline *
317 ges_timeline_new (void)
318 {
319   return g_object_new (GES_TYPE_TIMELINE, NULL);
320 }
321
322 /**
323  * ges_timeline_new_from_uri:
324  * @uri: the URI to load from
325  *
326  * Creates a timeline from the given URI.
327  *
328  * Returns: A new timeline if the uri was loaded successfully, or NULL if the
329  * uri could not be loaded
330  */
331
332 GESTimeline *
333 ges_timeline_new_from_uri (const gchar * uri)
334 {
335   GESTimeline *ret;
336
337   /* FIXME : we should have a GError** argument so the user can know why
338    * it wasn't able to load the uri
339    */
340
341   ret = ges_timeline_new ();
342
343   if (!ges_timeline_load_from_uri (ret, uri)) {
344     g_object_unref (ret);
345     return NULL;
346   }
347
348   return ret;
349 }
350
351
352 /**
353  * ges_timeline_load_from_uri:
354  * @timeline: an empty #GESTimeline into which to load the formatter
355  * @uri: The URI to load from
356  *
357  * Loads the contents of URI into the given timeline.
358  *
359  * Returns: TRUE if the timeline was loaded successfully, or FALSE if the uri
360  * could not be loaded.
361  */
362
363 gboolean
364 ges_timeline_load_from_uri (GESTimeline * timeline, const gchar * uri)
365 {
366   GESFormatter *p = NULL;
367   gboolean ret = FALSE;
368
369   /* FIXME : we should have a GError** argument so the user can know why
370    * it wasn't able to load the uri
371    */
372
373   if (!(p = ges_formatter_new_for_uri (uri))) {
374     GST_ERROR ("unsupported uri '%s'", uri);
375     goto fail;
376   }
377
378   if (!ges_formatter_load_from_uri (p, timeline, uri)) {
379     GST_ERROR ("error deserializing formatter");
380     goto fail;
381   }
382
383   ret = TRUE;
384
385 fail:
386   if (p)
387     g_object_unref (p);
388   return ret;
389 }
390
391 /**
392  * ges_timeline_save_to_uri:
393  * @timeline: a #GESTimeline
394  * @uri: The location to save to
395  *
396  * Saves the timeline to the given location
397  *
398  * Returns: TRUE if the timeline was successfully saved to the given location,
399  * else FALSE.
400  */
401
402 gboolean
403 ges_timeline_save_to_uri (GESTimeline * timeline, const gchar * uri)
404 {
405   GESFormatter *p = NULL;
406   gboolean ret = FALSE;
407
408   /* FIXME : How will the user be able to chose the format he
409    * wishes to store to ? */
410
411   /* FIXME : How will we ensure a timeline loaded with a certain format
412    * will be saved with the same one by default ? We need to make this
413    * easy from an API perspective */
414
415   /* FIXME : we should have a GError** argument so the user can know why
416    * it wasn't able to save
417    */
418
419   if (!(p = ges_formatter_new_for_uri (uri))) {
420     GST_ERROR ("unsupported uri '%s'", uri);
421     goto fail;
422   }
423
424   if (!ges_formatter_save_to_uri (p, timeline, uri)) {
425     GST_ERROR ("error serializing formatter");
426     goto fail;
427   }
428
429   ret = TRUE;
430
431 fail:
432   if (p)
433     g_object_unref (p);
434   return ret;
435 }
436
437 static void
438 add_object_to_track (GESTimelineObject * object, GESTrack * track)
439 {
440   if (!ges_timeline_object_create_track_objects (object, track)) {
441     GST_WARNING ("error creating track objects");
442   }
443 }
444
445 static void
446 add_object_to_tracks (GESTimeline * timeline, GESTimelineObject * object)
447 {
448   GList *tmp;
449
450   for (tmp = timeline->priv->tracks; tmp; tmp = g_list_next (tmp)) {
451     TrackPrivate *tr_priv = (TrackPrivate *) tmp->data;
452     GESTrack *track = tr_priv->track;
453
454     GST_LOG ("Trying with track %p", track);
455     add_object_to_track (object, track);
456   }
457 }
458
459
460 static void
461 do_async_start (GESTimeline * timeline)
462 {
463   GstMessage *message;
464   GList *tmp;
465
466   timeline->priv->async_pending = TRUE;
467
468   /* Freeze state of tracks */
469   for (tmp = timeline->priv->tracks; tmp; tmp = tmp->next) {
470     TrackPrivate *tr_priv = (TrackPrivate *) tmp->data;
471     gst_element_set_locked_state ((GstElement *) tr_priv->track, TRUE);
472   }
473
474   message = gst_message_new_async_start (GST_OBJECT_CAST (timeline), FALSE);
475   parent_class->handle_message (GST_BIN_CAST (timeline), message);
476 }
477
478 static void
479 do_async_done (GESTimeline * timeline)
480 {
481   GstMessage *message;
482
483   if (timeline->priv->async_pending) {
484     GList *tmp;
485     /* Unfreeze state of tracks */
486     for (tmp = timeline->priv->tracks; tmp; tmp = tmp->next) {
487       TrackPrivate *tr_priv = (TrackPrivate *) tmp->data;
488       gst_element_set_locked_state ((GstElement *) tr_priv->track, FALSE);
489       gst_element_sync_state_with_parent ((GstElement *) tr_priv->track);
490     }
491
492     GST_DEBUG_OBJECT (timeline, "Emitting async-done");
493     message = gst_message_new_async_done (GST_OBJECT_CAST (timeline));
494     parent_class->handle_message (GST_BIN_CAST (timeline), message);
495
496     timeline->priv->async_pending = FALSE;
497   }
498 }
499
500 static void
501 discoverer_finished_cb (GstDiscoverer * discoverer, GESTimeline * timeline)
502 {
503   do_async_done (timeline);
504 }
505
506 static void
507 discoverer_discovered_cb (GstDiscoverer * discoverer,
508     GstDiscovererInfo * info, GError * err, GESTimeline * timeline)
509 {
510   GList *tmp;
511   gboolean found = FALSE;
512   gboolean is_image = FALSE;
513   GESTimelineFileSource *tfs = NULL;
514   GESTimelinePrivate *priv = timeline->priv;
515   const gchar *uri = gst_discoverer_info_get_uri (info);
516
517
518   if (err) {
519     GST_WARNING ("Error while discovering %s: %s", uri, err->message);
520     return;
521   } else
522     GST_DEBUG ("Discovered uri %s", uri);
523
524   GES_TIMELINE_PENDINGOBJS_LOCK (timeline);
525
526   /* Find corresponding TimelineFileSource in the sources */
527   for (tmp = priv->pendingobjects; tmp; tmp = tmp->next) {
528     tfs = (GESTimelineFileSource *) tmp->data;
529
530     if (!g_strcmp0 (ges_timeline_filesource_get_uri (tfs), uri)) {
531       found = TRUE;
532       break;
533     }
534   }
535
536   if (found) {
537     GList *stream_list;
538     GESTrackType tfs_supportedformats;
539
540     /* The timeline file source will be updated with discovered information
541      * so it needs to not be finalized during this process */
542     g_object_ref (tfs);
543
544     /* Remove object from list */
545     priv->pendingobjects = g_list_delete_link (priv->pendingobjects, tmp);
546     GES_TIMELINE_PENDINGOBJS_UNLOCK (timeline);
547
548     /* FIXME : Handle errors in discovery */
549     stream_list = gst_discoverer_info_get_stream_list (info);
550
551     tfs_supportedformats = ges_timeline_filesource_get_supported_formats (tfs);
552     if (tfs_supportedformats != GES_TRACK_TYPE_UNKNOWN)
553       goto check_image;
554
555     /* Update timelinefilesource properties based on info */
556     for (tmp = stream_list; tmp; tmp = tmp->next) {
557       GstDiscovererStreamInfo *sinf = (GstDiscovererStreamInfo *) tmp->data;
558
559       if (GST_IS_DISCOVERER_AUDIO_INFO (sinf)) {
560         tfs_supportedformats |= GES_TRACK_TYPE_AUDIO;
561         ges_timeline_filesource_set_supported_formats (tfs,
562             tfs_supportedformats);
563       } else if (GST_IS_DISCOVERER_VIDEO_INFO (sinf)) {
564         tfs_supportedformats |= GES_TRACK_TYPE_VIDEO;
565         ges_timeline_filesource_set_supported_formats (tfs,
566             tfs_supportedformats);
567         if (gst_discoverer_video_info_is_image ((GstDiscovererVideoInfo *)
568                 sinf)) {
569           tfs_supportedformats |= GES_TRACK_TYPE_AUDIO;
570           ges_timeline_filesource_set_supported_formats (tfs,
571               tfs_supportedformats);
572           is_image = TRUE;
573         }
574       }
575     }
576
577     if (stream_list)
578       gst_discoverer_stream_info_list_free (stream_list);
579
580   check_image:
581
582     if (is_image) {
583       /* don't set max-duration on still images */
584       g_object_set (tfs, "is_image", (gboolean) TRUE, NULL);
585     }
586
587     /* Continue the processing on tfs */
588     add_object_to_tracks (timeline, GES_TIMELINE_OBJECT (tfs));
589
590     if (!is_image) {
591       g_object_set (tfs, "max-duration",
592           gst_discoverer_info_get_duration (info), NULL);
593     }
594
595     /* Continue the processing on tfs */
596     add_object_to_tracks (timeline, GES_TIMELINE_OBJECT (tfs));
597
598     /* Remove the ref as the timeline file source is no longer needed here */
599     g_object_unref (tfs);
600   } else {
601     GES_TIMELINE_PENDINGOBJS_UNLOCK (timeline);
602   }
603 }
604
605 static GstStateChangeReturn
606 ges_timeline_change_state (GstElement * element, GstStateChange transition)
607 {
608   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
609   GESTimeline *timeline = GES_TIMELINE (element);
610
611   switch (transition) {
612     case GST_STATE_CHANGE_READY_TO_PAUSED:
613       GES_TIMELINE_PENDINGOBJS_LOCK (timeline);
614       if (timeline->priv->pendingobjects) {
615         GES_TIMELINE_PENDINGOBJS_UNLOCK (timeline);
616         do_async_start (timeline);
617         ret = GST_STATE_CHANGE_ASYNC;
618       } else {
619         GES_TIMELINE_PENDINGOBJS_UNLOCK (timeline);
620       }
621       break;
622     default:
623       break;
624   }
625
626   {
627     GstStateChangeReturn bret;
628
629     bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
630     if (G_UNLIKELY (bret == GST_STATE_CHANGE_NO_PREROLL)) {
631       do_async_done (timeline);
632       ret = bret;
633     }
634   }
635
636   switch (transition) {
637     case GST_STATE_CHANGE_PAUSED_TO_READY:
638       do_async_done (timeline);
639       break;
640     default:
641       break;
642   }
643
644   return ret;
645
646 }
647
648 static void
649 layer_object_added_cb (GESTimelineLayer * layer, GESTimelineObject * object,
650     GESTimeline * timeline)
651 {
652   if (ges_timeline_object_is_moving_from_layer (object)) {
653     GST_DEBUG ("TimelineObject %p is moving from a layer to another, not doing"
654         " anything on it", object);
655     return;
656   }
657
658   GST_DEBUG ("New TimelineObject %p added to layer %p", object, layer);
659
660   if (GES_IS_TIMELINE_FILE_SOURCE (object)) {
661     GESTimelineFileSource *tfs = GES_TIMELINE_FILE_SOURCE (object);
662     GESTrackType tfs_supportedformats =
663         ges_timeline_filesource_get_supported_formats (tfs);
664     guint64 tfs_maxdur = ges_timeline_filesource_get_max_duration (tfs);
665     const gchar *tfs_uri;
666
667     /* Send the filesource to the discoverer if:
668      * * it doesn't have specified supported formats
669      * * OR it doesn't have a specified max-duration
670      * * OR it doesn't have a valid duration  */
671
672     if (tfs_supportedformats == GES_TRACK_TYPE_UNKNOWN ||
673         tfs_maxdur == GST_CLOCK_TIME_NONE || object->duration == 0) {
674       GST_LOG ("Incomplete TimelineFileSource, discovering it");
675       tfs_uri = ges_timeline_filesource_get_uri (tfs);
676
677       GES_TIMELINE_PENDINGOBJS_LOCK (timeline);
678       timeline->priv->pendingobjects =
679           g_list_append (timeline->priv->pendingobjects, object);
680       GES_TIMELINE_PENDINGOBJS_UNLOCK (timeline);
681
682       gst_discoverer_discover_uri_async (timeline->priv->discoverer, tfs_uri);
683     } else
684       add_object_to_tracks (timeline, object);
685   } else {
686     add_object_to_tracks (timeline, object);
687   }
688
689   GST_DEBUG ("done");
690 }
691
692 static void
693 layer_priority_changed_cb (GESTimelineLayer * layer,
694     GParamSpec * arg G_GNUC_UNUSED, GESTimeline * timeline)
695 {
696   timeline->priv->layers = g_list_sort (timeline->priv->layers, (GCompareFunc)
697       sort_layers);
698 }
699
700 static void
701 layer_object_removed_cb (GESTimelineLayer * layer, GESTimelineObject * object,
702     GESTimeline * timeline)
703 {
704   GList *trackobjects, *tmp;
705
706   if (ges_timeline_object_is_moving_from_layer (object)) {
707     GST_DEBUG ("TimelineObject %p is moving from a layer to another, not doing"
708         " anything on it", object);
709     return;
710   }
711
712   GST_DEBUG ("TimelineObject %p removed from layer %p", object, layer);
713
714   /* Go over the object's track objects and figure out which one belongs to
715    * the list of tracks we control */
716
717   trackobjects = ges_timeline_object_get_track_objects (object);
718   for (tmp = trackobjects; tmp; tmp = tmp->next) {
719     GESTrackObject *trobj = (GESTrackObject *) tmp->data;
720
721     GST_DEBUG ("Trying to remove TrackObject %p", trobj);
722     if (G_LIKELY (g_list_find_custom (timeline->priv->tracks,
723                 ges_track_object_get_track (trobj),
724                 (GCompareFunc) custom_find_track))) {
725       GST_DEBUG ("Belongs to one of the tracks we control");
726       ges_track_remove_object (ges_track_object_get_track (trobj), trobj);
727
728       ges_timeline_object_release_track_object (object, trobj);
729     }
730     /* removing the reference added by _get_track_objects() */
731     g_object_unref (trobj);
732   }
733
734   g_list_free (trackobjects);
735
736   /* if the object is a timeline file source that has not yet been discovered,
737    * it no longer needs to be discovered so remove it from the pendingobjects
738    * list if it belongs to this layer */
739   if (GES_IS_TIMELINE_FILE_SOURCE (object)) {
740     GES_TIMELINE_PENDINGOBJS_LOCK (timeline);
741     timeline->priv->pendingobjects =
742         g_list_remove_all (timeline->priv->pendingobjects, object);
743     GES_TIMELINE_PENDINGOBJS_UNLOCK (timeline);
744   }
745
746   GST_DEBUG ("Done");
747 }
748
749 /**
750  * ges_timeline_append_layer:
751  * @timeline: a #GESTimeline
752  * @layer: the #GESTimelineLayer to add
753  *
754  * Convenience method to append @layer to @timeline which means that the
755  * priority of @layer is changed to correspond to the last layer of @timeline.
756  * The reference to the @layer will be stolen by @timeline.
757  *
758  * Returns: TRUE if the layer was properly added, else FALSE.
759  */
760 gboolean
761 ges_timeline_append_layer (GESTimeline * timeline, GESTimelineLayer * layer)
762 {
763   guint32 priority;
764   GESTimelinePrivate *priv = timeline->priv;
765
766   GST_DEBUG ("Appending layer to layer:%p, timeline:%p", timeline, layer);
767   priority = g_list_length (priv->layers);
768
769   ges_timeline_layer_set_priority (layer, priority);
770
771   return ges_timeline_add_layer (timeline, layer);
772 }
773
774 /**
775  * ges_timeline_add_layer:
776  * @timeline: a #GESTimeline
777  * @layer: the #GESTimelineLayer to add
778  *
779  * Add the layer to the timeline. The reference to the @layer will be stolen
780  * by the @timeline.
781  *
782  * Returns: TRUE if the layer was properly added, else FALSE.
783  */
784 gboolean
785 ges_timeline_add_layer (GESTimeline * timeline, GESTimelineLayer * layer)
786 {
787   GList *objects, *tmp;
788   GESTimelinePrivate *priv = timeline->priv;
789
790   GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
791
792   /* We can only add a layer that doesn't already belong to another timeline */
793   if (G_UNLIKELY (layer->timeline)) {
794     GST_WARNING ("Layer belongs to another timeline, can't add it");
795     return FALSE;
796   }
797
798   /* Add to the list of layers, make sure we don't already control it */
799   if (G_UNLIKELY (g_list_find (priv->layers, (gconstpointer) layer))) {
800     GST_WARNING ("Layer is already controlled by this timeline");
801     return FALSE;
802   }
803
804   g_object_ref_sink (layer);
805   priv->layers = g_list_insert_sorted (priv->layers, layer,
806       (GCompareFunc) sort_layers);
807
808   /* Inform the layer that it belongs to a new timeline */
809   ges_timeline_layer_set_timeline (layer, timeline);
810
811   /* Connect to 'object-added'/'object-removed' signal from the new layer */
812   g_signal_connect (layer, "object-added", G_CALLBACK (layer_object_added_cb),
813       timeline);
814   g_signal_connect (layer, "object-removed",
815       G_CALLBACK (layer_object_removed_cb), timeline);
816   g_signal_connect (layer, "notify::priority",
817       G_CALLBACK (layer_priority_changed_cb), timeline);
818
819   GST_DEBUG ("Done adding layer, emitting 'layer-added' signal");
820   g_signal_emit (timeline, ges_timeline_signals[LAYER_ADDED], 0, layer);
821
822   /* add any existing timeline objects to the timeline */
823   objects = ges_timeline_layer_get_objects (layer);
824   for (tmp = objects; tmp; tmp = tmp->next) {
825     layer_object_added_cb (layer, tmp->data, timeline);
826     g_object_unref (tmp->data);
827     tmp->data = NULL;
828   }
829   g_list_free (objects);
830
831   return TRUE;
832 }
833
834 /**
835  * ges_timeline_remove_layer:
836  * @timeline: a #GESTimeline
837  * @layer: the #GESTimelineLayer to remove
838  *
839  * Removes the layer from the timeline. The reference that the @timeline holds on
840  * the layer will be dropped. If you wish to use the @layer after calling this
841  * method, you need to take a reference before calling.
842  *
843  * Returns: TRUE if the layer was properly removed, else FALSE.
844  */
845
846 gboolean
847 ges_timeline_remove_layer (GESTimeline * timeline, GESTimelineLayer * layer)
848 {
849   GList *layer_objects, *tmp;
850   GESTimelinePrivate *priv = timeline->priv;
851
852   GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
853
854   if (G_UNLIKELY (!g_list_find (priv->layers, layer))) {
855     GST_WARNING ("Layer doesn't belong to this timeline");
856     return FALSE;
857   }
858
859   /* remove objects from any private data structures */
860
861   layer_objects = ges_timeline_layer_get_objects (layer);
862   for (tmp = layer_objects; tmp; tmp = tmp->next) {
863     layer_object_removed_cb (layer, GES_TIMELINE_OBJECT (tmp->data), timeline);
864     g_object_unref (G_OBJECT (tmp->data));
865     tmp->data = NULL;
866   }
867   g_list_free (layer_objects);
868
869   /* Disconnect signals */
870   GST_DEBUG ("Disconnecting signal callbacks");
871   g_signal_handlers_disconnect_by_func (layer, layer_object_added_cb, timeline);
872   g_signal_handlers_disconnect_by_func (layer, layer_object_removed_cb,
873       timeline);
874
875   priv->layers = g_list_remove (priv->layers, layer);
876
877   ges_timeline_layer_set_timeline (layer, NULL);
878
879   g_signal_emit (timeline, ges_timeline_signals[LAYER_REMOVED], 0, layer);
880
881   g_object_unref (layer);
882
883   return TRUE;
884 }
885
886 static void
887 pad_added_cb (GESTrack * track, GstPad * pad, TrackPrivate * tr_priv)
888 {
889   gchar *padname;
890
891
892   GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));
893
894   if (G_UNLIKELY (tr_priv->pad)) {
895     GST_WARNING ("We are already controlling a pad for this track");
896     return;
897   }
898
899   /* Remember the pad */
900   tr_priv->pad = pad;
901
902   /* ghost it ! */
903   GST_DEBUG ("Ghosting pad and adding it to ourself");
904   padname = g_strdup_printf ("track_%p_src", track);
905   tr_priv->ghostpad = gst_ghost_pad_new (padname, pad);
906   g_free (padname);
907   gst_pad_set_active (tr_priv->ghostpad, TRUE);
908   gst_element_add_pad (GST_ELEMENT (tr_priv->timeline), tr_priv->ghostpad);
909 }
910
911 static void
912 pad_removed_cb (GESTrack * track, GstPad * pad, TrackPrivate * tr_priv)
913 {
914   GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));
915
916   if (G_UNLIKELY (tr_priv->pad != pad)) {
917     GST_WARNING ("Not the pad we're controlling");
918     return;
919   }
920
921   if (G_UNLIKELY (tr_priv->ghostpad == NULL)) {
922     GST_WARNING ("We don't have a ghostpad for this pad !");
923     return;
924   }
925
926   GST_DEBUG ("Removing ghostpad");
927   gst_pad_set_active (tr_priv->ghostpad, FALSE);
928   gst_element_remove_pad (GST_ELEMENT (tr_priv->timeline), tr_priv->ghostpad);
929   tr_priv->ghostpad = NULL;
930   tr_priv->pad = NULL;
931 }
932
933 static gint
934 custom_find_track (TrackPrivate * tr_priv, GESTrack * track)
935 {
936   if (tr_priv->track == track)
937     return 0;
938   return -1;
939 }
940
941 /**
942  * ges_timeline_add_track:
943  * @timeline: a #GESTimeline
944  * @track: the #GESTrack to add
945  *
946  * Add a track to the timeline. The reference to the track will be stolen by the
947  * pipeline.
948  *
949  * Returns: TRUE if the track was properly added, else FALSE.
950  */
951
952 /* FIXME: create track objects for timeline objects which have already been
953  * added to existing layers.
954  */
955
956 gboolean
957 ges_timeline_add_track (GESTimeline * timeline, GESTrack * track)
958 {
959   TrackPrivate *tr_priv;
960   GESTimelinePrivate *priv = timeline->priv;
961   GList *tmp;
962
963   GST_DEBUG ("timeline:%p, track:%p", timeline, track);
964
965   /* make sure we don't already control it */
966   if (G_UNLIKELY (g_list_find_custom (priv->tracks, (gconstpointer) track,
967               (GCompareFunc) custom_find_track))) {
968     GST_WARNING ("Track is already controlled by this timeline");
969     return FALSE;
970   }
971
972   /* Add the track to ourself (as a GstBin)
973    * Reference is stolen ! */
974   if (G_UNLIKELY (!gst_bin_add (GST_BIN (timeline), GST_ELEMENT (track)))) {
975     GST_WARNING ("Couldn't add track to ourself (GST)");
976     return FALSE;
977   }
978
979   tr_priv = g_new0 (TrackPrivate, 1);
980   tr_priv->timeline = timeline;
981   tr_priv->track = track;
982
983   /* Add the track to the list of tracks we track */
984   priv->tracks = g_list_append (priv->tracks, tr_priv);
985
986   /* Listen to pad-added/-removed */
987   g_signal_connect (track, "pad-added", (GCallback) pad_added_cb, tr_priv);
988   g_signal_connect (track, "pad-removed", (GCallback) pad_removed_cb, tr_priv);
989
990   /* Inform the track that it's currently being used by ourself */
991   ges_track_set_timeline (track, timeline);
992
993   GST_DEBUG ("Done adding track, emitting 'track-added' signal");
994
995   /* emit 'track-added' */
996   g_signal_emit (timeline, ges_timeline_signals[TRACK_ADDED], 0, track);
997
998   /* ensure that each existing timeline object has the opportunity to create a
999    * track object for this track*/
1000
1001   for (tmp = priv->layers; tmp; tmp = tmp->next) {
1002     GList *objects, *obj;
1003     objects = ges_timeline_layer_get_objects (tmp->data);
1004
1005     for (obj = objects; obj; obj = obj->next) {
1006       add_object_to_track (obj->data, track);
1007       g_object_unref (obj->data);
1008       obj->data = NULL;
1009     }
1010     g_list_free (objects);
1011   }
1012
1013   /* We connect to the duration change notify, so we can update
1014    * our duration accordingly */
1015   g_signal_connect (G_OBJECT (track), "notify::duration",
1016       G_CALLBACK (track_duration_cb), timeline);
1017   track_duration_cb (GST_ELEMENT (track), NULL, timeline);
1018
1019   return TRUE;
1020 }
1021
1022 /**
1023  * ges_timeline_remove_track:
1024  * @timeline: a #GESTimeline
1025  * @track: the #GESTrack to remove
1026  *
1027  * Remove the @track from the @timeline. The reference stolen when adding the
1028  * @track will be removed. If you wish to use the @track after calling this
1029  * function you must ensure that you have a reference to it.
1030  *
1031  * Returns: TRUE if the @track was properly removed, else FALSE.
1032  */
1033
1034 /* FIXME: release any track objects associated with this layer. currenly this
1035  * will not happen if you remove the track before removing *all*
1036  * timelineobjects which have a track object in this track.
1037  */
1038
1039 gboolean
1040 ges_timeline_remove_track (GESTimeline * timeline, GESTrack * track)
1041 {
1042   GList *tmp;
1043   TrackPrivate *tr_priv;
1044   GESTimelinePrivate *priv = timeline->priv;
1045
1046   GST_DEBUG ("timeline:%p, track:%p", timeline, track);
1047
1048   if (G_UNLIKELY (!(tmp =
1049               g_list_find_custom (priv->tracks, (gconstpointer) track,
1050                   (GCompareFunc) custom_find_track)))) {
1051     GST_WARNING ("Track doesn't belong to this timeline");
1052     return FALSE;
1053   }
1054
1055   tr_priv = tmp->data;
1056   priv->tracks = g_list_remove (priv->tracks, tr_priv);
1057
1058   ges_track_set_timeline (track, NULL);
1059
1060   /* Remove ghost pad */
1061   if (tr_priv->ghostpad) {
1062     GST_DEBUG ("Removing ghostpad");
1063     gst_pad_set_active (tr_priv->ghostpad, FALSE);
1064     gst_ghost_pad_set_target ((GstGhostPad *) tr_priv->ghostpad, NULL);
1065     gst_element_remove_pad (GST_ELEMENT (timeline), tr_priv->ghostpad);
1066   }
1067
1068   /* Remove pad-added/-removed handlers */
1069   g_signal_handlers_disconnect_by_func (track, pad_added_cb, tr_priv);
1070   g_signal_handlers_disconnect_by_func (track, pad_removed_cb, tr_priv);
1071   g_signal_handlers_disconnect_by_func (track, track_duration_cb,
1072       tr_priv->track);
1073
1074   /* Signal track removal to all layers/objects */
1075   g_signal_emit (timeline, ges_timeline_signals[TRACK_REMOVED], 0, track);
1076
1077   /* remove track from our bin */
1078   gst_object_ref (track);
1079   if (G_UNLIKELY (!gst_bin_remove (GST_BIN (timeline), GST_ELEMENT (track)))) {
1080     GST_WARNING ("Couldn't remove track to ourself (GST)");
1081     gst_object_unref (track);
1082     return FALSE;
1083   }
1084
1085   /* set track state to NULL */
1086
1087   gst_element_set_state (GST_ELEMENT (track), GST_STATE_NULL);
1088
1089   gst_object_unref (track);
1090
1091   g_free (tr_priv);
1092
1093   return TRUE;
1094 }
1095
1096 /**
1097  * ges_timeline_get_track_for_pad:
1098  * @timeline: The #GESTimeline
1099  * @pad: The #GstPad
1100  *
1101  * Search the #GESTrack corresponding to the given @timeline's @pad.
1102  *
1103  * Returns: (transfer none): The corresponding #GESTrack if it is found,
1104  * or %NULL if there is an error.
1105  */
1106
1107 GESTrack *
1108 ges_timeline_get_track_for_pad (GESTimeline * timeline, GstPad * pad)
1109 {
1110   GList *tmp;
1111
1112   for (tmp = timeline->priv->tracks; tmp; tmp = g_list_next (tmp)) {
1113     TrackPrivate *tr_priv = (TrackPrivate *) tmp->data;
1114     if (pad == tr_priv->ghostpad)
1115       return tr_priv->track;
1116   }
1117
1118   return NULL;
1119 }
1120
1121 /**
1122  * ges_timeline_get_tracks:
1123  * @timeline: a #GESTimeline
1124  *
1125  * Returns the list of #GESTrack used by the Timeline.
1126  *
1127  * Returns: (transfer full) (element-type GESTrack): A list of #GESTrack.
1128  * The caller should unref each track once he is done with them.
1129  */
1130 GList *
1131 ges_timeline_get_tracks (GESTimeline * timeline)
1132 {
1133   GList *tmp, *res = NULL;
1134
1135   for (tmp = timeline->priv->tracks; tmp; tmp = g_list_next (tmp)) {
1136     TrackPrivate *tr_priv = (TrackPrivate *) tmp->data;
1137     res = g_list_append (res, g_object_ref (tr_priv->track));
1138   }
1139
1140   return res;
1141 }
1142
1143 /**
1144  * ges_timeline_get_layers:
1145  * @timeline: a #GESTimeline
1146  *
1147  * Get the list of #GESTimelineLayer present in the Timeline.
1148  *
1149  * Returns: (transfer full) (element-type GESTimelineLayer): the list of
1150  * #GESTimelineLayer present in the Timeline sorted by priority.
1151  * The caller should unref each Layer once he is done with them.
1152  */
1153 GList *
1154 ges_timeline_get_layers (GESTimeline * timeline)
1155 {
1156   GList *tmp, *res = NULL;
1157
1158   for (tmp = timeline->priv->layers; tmp; tmp = g_list_next (tmp)) {
1159     res = g_list_insert_sorted (res, g_object_ref (tmp->data),
1160         (GCompareFunc) sort_layers);
1161   }
1162
1163   return res;
1164 }
1165
1166 /**
1167  * ges_timeline_enable_update:
1168  * @timeline: a #GESTimeline
1169  * @enabled: TRUE if the timeline must be updated, FALSE otherwise.
1170  *
1171  * Calls the enable_update function of the tracks contained by the timeline.
1172  *
1173  * Returns: True if success, FALSE otherwise.
1174  */
1175 gboolean
1176 ges_timeline_enable_update (GESTimeline * timeline, gboolean enabled)
1177 {
1178   GList *tmp, *tracks;
1179   gboolean res = TRUE;
1180
1181   GST_DEBUG_OBJECT (timeline, "%s updates", enabled ? "Enabling" : "Disabling");
1182
1183   tracks = ges_timeline_get_tracks (timeline);
1184
1185   for (tmp = tracks; tmp; tmp = tmp->next) {
1186     if (!ges_track_enable_update (tmp->data, enabled)) {
1187       res = FALSE;
1188     }
1189   }
1190
1191   g_list_free (tracks);
1192
1193   return res;
1194 }
1195
1196 static void
1197 track_duration_cb (GstElement * track,
1198     GParamSpec * arg G_GNUC_UNUSED, GESTimeline * timeline)
1199 {
1200   guint64 duration, max_duration = 0;
1201   GList *tmp;
1202
1203   for (tmp = timeline->priv->tracks; tmp; tmp = g_list_next (tmp)) {
1204     TrackPrivate *tr_priv = (TrackPrivate *) tmp->data;
1205     g_object_get (tr_priv->track, "duration", &duration, NULL);
1206     GST_DEBUG ("track duration : %" GST_TIME_FORMAT, GST_TIME_ARGS (duration));
1207     max_duration = MAX (duration, max_duration);
1208   }
1209
1210   if (timeline->priv->duration != max_duration) {
1211     GST_DEBUG ("track duration : %" GST_TIME_FORMAT " current : %"
1212         GST_TIME_FORMAT, GST_TIME_ARGS (max_duration),
1213         GST_TIME_ARGS (timeline->priv->duration));
1214
1215     timeline->priv->duration = max_duration;
1216
1217 #if GLIB_CHECK_VERSION(2,26,0)
1218     g_object_notify_by_pspec (G_OBJECT (timeline), properties[PROP_DURATION]);
1219 #else
1220     g_object_notify (G_OBJECT (timeline), "duration");
1221 #endif
1222   }
1223 }