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