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