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