GESTimeline: allow adding tracks after layers
[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
34 #include "gesmarshal.h"
35 #include "ges-internal.h"
36 #include "ges-timeline.h"
37 #include "ges-track.h"
38 #include "ges-timeline-layer.h"
39 #include "ges.h"
40
41
42 G_DEFINE_TYPE (GESTimeline, ges_timeline, GST_TYPE_BIN);
43
44 /* private structure to contain our track-related information */
45
46 typedef struct
47 {
48   GESTimeline *timeline;
49   GESTrack *track;
50   GstPad *pad;                  /* Pad from the track */
51   GstPad *ghostpad;
52 } TrackPrivate;
53
54 enum
55 {
56   TRACK_ADDED,
57   TRACK_REMOVED,
58   LAYER_ADDED,
59   LAYER_REMOVED,
60   LAST_SIGNAL
61 };
62
63 static GstBinClass *parent_class;
64
65 static guint ges_timeline_signals[LAST_SIGNAL] = { 0 };
66
67 gint custom_find_track (TrackPrivate * priv, GESTrack * track);
68 static GstStateChangeReturn
69 ges_timeline_change_state (GstElement * element, GstStateChange transition);
70 static void
71 discoverer_finished_cb (GstDiscoverer * discoverer, GESTimeline * timeline);
72 static void
73 discoverer_discovered_cb (GstDiscoverer * discoverer,
74     GstDiscovererInfo * info, GError * err, GESTimeline * timeline);
75
76 static void
77 ges_timeline_get_property (GObject * object, guint property_id,
78     GValue * value, GParamSpec * pspec)
79 {
80   switch (property_id) {
81     default:
82       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
83   }
84 }
85
86 static void
87 ges_timeline_set_property (GObject * object, guint property_id,
88     const GValue * value, GParamSpec * pspec)
89 {
90   switch (property_id) {
91     default:
92       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
93   }
94 }
95
96 static void
97 ges_timeline_dispose (GObject * object)
98 {
99   GESTimeline *timeline = GES_TIMELINE (object);
100
101   if (timeline->discoverer) {
102     gst_discoverer_stop (timeline->discoverer);
103     g_object_unref (timeline->discoverer);
104     timeline->discoverer = NULL;
105   }
106
107   while (timeline->layers) {
108     GESTimelineLayer *layer = (GESTimelineLayer *) timeline->layers->data;
109     ges_timeline_remove_layer (timeline, layer);
110   }
111
112   /* FIXME: it should be possible to remove tracks before removing
113    * layers, but at the moment this creates a problem because the track
114    * objects aren't notified that their gnlobjects have been destroyed.
115    */
116
117   while (timeline->tracks) {
118     TrackPrivate *priv = (TrackPrivate *) timeline->tracks->data;
119     ges_timeline_remove_track (timeline, priv->track);
120   }
121
122   G_OBJECT_CLASS (ges_timeline_parent_class)->dispose (object);
123 }
124
125 static void
126 ges_timeline_finalize (GObject * object)
127 {
128   G_OBJECT_CLASS (ges_timeline_parent_class)->finalize (object);
129 }
130
131 static void
132 ges_timeline_class_init (GESTimelineClass * klass)
133 {
134   GObjectClass *object_class = G_OBJECT_CLASS (klass);
135   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
136
137   parent_class = g_type_class_peek_parent (klass);
138
139   element_class->change_state = ges_timeline_change_state;
140
141   object_class->get_property = ges_timeline_get_property;
142   object_class->set_property = ges_timeline_set_property;
143   object_class->dispose = ges_timeline_dispose;
144   object_class->finalize = ges_timeline_finalize;
145
146   /**
147    * GESTimeline::track-added
148    * @timeline: the #GESTimeline
149    * @track: the #GESTrack that was added to the timeline
150    *
151    * Will be emitted after the track was added to the timeline.
152    */
153   ges_timeline_signals[TRACK_ADDED] =
154       g_signal_new ("track-added", G_TYPE_FROM_CLASS (klass),
155       G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, track_added), NULL,
156       NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TRACK);
157
158   /**
159    * GESTimeline::track-removed
160    * @timeline: the #GESTimeline
161    * @track: the #GESTrack that was removed from the timeline
162    *
163    * Will be emitted after the track was removed from the timeline.
164    */
165   ges_timeline_signals[TRACK_REMOVED] =
166       g_signal_new ("track-removed", G_TYPE_FROM_CLASS (klass),
167       G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, track_removed),
168       NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TRACK);
169
170   /**
171    * GESTimeline::layer-added
172    * @timeline: the #GESTimeline
173    * @layer: the #GESTimelineLayer that was added to the timeline
174    *
175    * Will be emitted after the layer was added to the timeline.
176    */
177   ges_timeline_signals[LAYER_ADDED] =
178       g_signal_new ("layer-added", G_TYPE_FROM_CLASS (klass),
179       G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, layer_added), NULL,
180       NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TIMELINE_LAYER);
181
182   /**
183    * GESTimeline::layer-removed
184    * @timeline: the #GESTimeline
185    * @layer: the #GESTimelineLayer that was removed from the timeline
186    *
187    * Will be emitted after the layer was removed from the timeline.
188    */
189   ges_timeline_signals[LAYER_REMOVED] =
190       g_signal_new ("layer-removed", G_TYPE_FROM_CLASS (klass),
191       G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, layer_removed),
192       NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
193       GES_TYPE_TIMELINE_LAYER);
194 }
195
196 static void
197 ges_timeline_init (GESTimeline * self)
198 {
199   self->layers = NULL;
200   self->tracks = NULL;
201
202   /* New discoverer with a 15s timeout */
203   self->discoverer = gst_discoverer_new (15 * GST_SECOND, NULL);
204   g_signal_connect (self->discoverer, "finished",
205       G_CALLBACK (discoverer_finished_cb), self);
206   g_signal_connect (self->discoverer, "discovered",
207       G_CALLBACK (discoverer_discovered_cb), self);
208   gst_discoverer_start (self->discoverer);
209 }
210
211 /**
212  * ges_timeline_new:
213  *
214  * Creates a new empty #GESTimeline.
215  *
216  * Returns: The new timeline.
217  */
218
219 GESTimeline *
220 ges_timeline_new (void)
221 {
222   return g_object_new (GES_TYPE_TIMELINE, NULL);
223 }
224
225 /**
226  * ges_timeline_load_from_uri:
227  * @uri: The URI to load from
228  *
229  * Creates a timeline from the contents of given uri.
230  *
231  * NOT_IMPLEMENTED !
232  *
233  * Returns: A new #GESTimeline if loading was successful, else NULL.
234  */
235
236 GESTimeline *
237 ges_timeline_load_from_uri (gchar * uri)
238 {
239   /* FIXME : IMPLEMENT */
240   return NULL;
241 }
242
243 /**
244  * ges_timeline_save:
245  * @timeline: a #GESTimeline
246  * @uri: The location to save to
247  *
248  * Saves the timeline to the given location
249  *
250  * NOT_IMPLEMENTED !
251  *
252  * Returns: TRUE if the timeline was successfully saved to the given location,
253  * else FALSE.
254  */
255
256 gboolean
257 ges_timeline_save (GESTimeline * timeline, gchar * uri)
258 {
259   /* FIXME : IMPLEMENT */
260   return FALSE;
261 }
262
263 static void
264 add_object_to_track (GESTimelineObject * object, GESTrack * track)
265 {
266   if (!ges_timeline_object_create_track_objects (object, track)) {
267     GST_WARNING ("error creating track objects");
268   }
269 }
270
271 static void
272 add_object_to_tracks (GESTimeline * timeline, GESTimelineObject * object)
273 {
274   GList *tmp;
275
276   for (tmp = timeline->tracks; tmp; tmp = g_list_next (tmp)) {
277     TrackPrivate *priv = (TrackPrivate *) tmp->data;
278     GESTrack *track = priv->track;
279
280     GST_LOG ("Trying with track %p", track);
281     add_object_to_track (object, track);
282   }
283 }
284
285
286 static void
287 do_async_start (GESTimeline * timeline)
288 {
289   GstMessage *message;
290   GList *tmp;
291
292   timeline->async_pending = TRUE;
293
294   /* Freeze state of tracks */
295   for (tmp = timeline->tracks; tmp; tmp = tmp->next) {
296     TrackPrivate *priv = (TrackPrivate *) tmp->data;
297     gst_element_set_locked_state ((GstElement *) priv->track, TRUE);
298   }
299
300   message = gst_message_new_async_start (GST_OBJECT_CAST (timeline), FALSE);
301   parent_class->handle_message (GST_BIN_CAST (timeline), message);
302 }
303
304 static void
305 do_async_done (GESTimeline * timeline)
306 {
307   GstMessage *message;
308
309   if (timeline->async_pending) {
310     GList *tmp;
311     /* Unfreeze state of tracks */
312     for (tmp = timeline->tracks; tmp; tmp = tmp->next) {
313       TrackPrivate *priv = (TrackPrivate *) tmp->data;
314       gst_element_set_locked_state ((GstElement *) priv->track, FALSE);
315       gst_element_sync_state_with_parent ((GstElement *) priv->track);
316     }
317
318     GST_DEBUG_OBJECT (timeline, "Emitting async-done");
319     message = gst_message_new_async_done (GST_OBJECT_CAST (timeline));
320     parent_class->handle_message (GST_BIN_CAST (timeline), message);
321
322     timeline->async_pending = FALSE;
323   }
324 }
325
326 static void
327 discoverer_finished_cb (GstDiscoverer * discoverer, GESTimeline * timeline)
328 {
329   do_async_done (timeline);
330 }
331
332 static void
333 discoverer_discovered_cb (GstDiscoverer * discoverer,
334     GstDiscovererInfo * info, GError * err, GESTimeline * timeline)
335 {
336   GList *tmp;
337   gboolean found = FALSE;
338   gboolean is_image = FALSE;
339   GESTimelineFileSource *tfs = NULL;
340   const gchar *uri = gst_discoverer_info_get_uri (info);
341
342   GST_DEBUG ("Discovered uri %s", uri);
343
344   /* Find corresponding TimelineFileSource in the sources */
345   for (tmp = timeline->pendingobjects; tmp; tmp = tmp->next) {
346     tfs = (GESTimelineFileSource *) tmp->data;
347
348     if (!g_strcmp0 (tfs->uri, uri)) {
349       found = TRUE;
350       break;
351     }
352   }
353
354   if (found) {
355     GList *stream_list;
356
357     /* Remove object from list */
358     timeline->pendingobjects =
359         g_list_delete_link (timeline->pendingobjects, tmp);
360
361     /* FIXME : Handle errors in discovery */
362     stream_list = gst_discoverer_info_get_stream_list (info);
363
364     /* Update timelinefilesource properties based on info */
365     for (tmp = stream_list; tmp; tmp = tmp->next) {
366       GstDiscovererStreamInfo *sinf = (GstDiscovererStreamInfo *) tmp->data;
367
368       if (GST_IS_DISCOVERER_AUDIO_INFO (sinf))
369         tfs->supportedformats |= GES_TRACK_TYPE_AUDIO;
370       else if (GST_IS_DISCOVERER_VIDEO_INFO (sinf)) {
371         tfs->supportedformats |= GES_TRACK_TYPE_VIDEO;
372         if (gst_discoverer_video_info_is_image ((GstDiscovererVideoInfo *)
373                 sinf)) {
374           tfs->supportedformats |= GES_TRACK_TYPE_AUDIO;
375           is_image = TRUE;
376         }
377       }
378     }
379
380     if (stream_list)
381       gst_discoverer_stream_info_list_free (stream_list);
382
383     if (is_image) {
384       /* don't set max-duration on still images */
385       g_object_set (tfs, "is_image", (gboolean) TRUE, NULL);
386     }
387
388     else {
389       g_object_set (tfs, "max-duration",
390           gst_discoverer_info_get_duration (info), NULL);
391     }
392
393     /* Continue the processing on tfs */
394     add_object_to_tracks (timeline, GES_TIMELINE_OBJECT (tfs));
395   }
396 }
397
398 static GstStateChangeReturn
399 ges_timeline_change_state (GstElement * element, GstStateChange transition)
400 {
401   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
402   GESTimeline *timeline = GES_TIMELINE (element);
403
404   switch (transition) {
405     case GST_STATE_CHANGE_READY_TO_PAUSED:
406       if (timeline->pendingobjects) {
407         do_async_start (timeline);
408         ret = GST_STATE_CHANGE_ASYNC;
409       }
410       break;
411     default:
412       break;
413   }
414
415   {
416     GstStateChangeReturn bret;
417
418     bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
419     if (G_UNLIKELY (bret == GST_STATE_CHANGE_NO_PREROLL)) {
420       do_async_done (timeline);
421       ret = bret;
422     }
423   }
424
425   switch (transition) {
426     case GST_STATE_CHANGE_PAUSED_TO_READY:
427       do_async_done (timeline);
428       break;
429     default:
430       break;
431   }
432
433   return ret;
434
435 }
436
437 static void
438 layer_object_added_cb (GESTimelineLayer * layer, GESTimelineObject * object,
439     GESTimeline * timeline)
440 {
441   GST_DEBUG ("New TimelineObject %p added to layer %p", object, layer);
442
443   if (GES_IS_TIMELINE_FILE_SOURCE (object)) {
444     GESTimelineFileSource *tfs = GES_TIMELINE_FILE_SOURCE (object);
445
446     /* Send the filesource to the discoverer if:
447      * * it doesn't have specified supported formats
448      * * OR it doesn't have a specified max-duration
449      * * OR it doesn't have a valid duration  */
450
451     if (tfs->supportedformats == GES_TRACK_TYPE_UNKNOWN ||
452         tfs->maxduration == GST_CLOCK_TIME_NONE || object->duration == 0) {
453       GST_LOG ("Incomplete TimelineFileSource, discovering it");
454       timeline->pendingobjects =
455           g_list_append (timeline->pendingobjects, object);
456       gst_discoverer_discover_uri_async (timeline->discoverer,
457           GES_TIMELINE_FILE_SOURCE (object)->uri);
458     } else
459       add_object_to_tracks (timeline, object);
460   } else {
461     add_object_to_tracks (timeline, object);
462   }
463
464   GST_DEBUG ("done");
465 }
466
467
468 static void
469 layer_object_removed_cb (GESTimelineLayer * layer, GESTimelineObject * object,
470     GESTimeline * timeline)
471 {
472   GList *tmp, *next;
473
474   GST_DEBUG ("TimelineObject %p removed from layer %p", object, layer);
475
476   /* Go over the object's track objects and figure out which one belongs to
477    * the list of tracks we control */
478
479   for (tmp = object->trackobjects; tmp; tmp = next) {
480     GESTrackObject *trobj = (GESTrackObject *) tmp->data;
481
482     next = g_list_next (tmp);
483
484     GST_DEBUG ("Trying to remove TrackObject %p", trobj);
485     if (G_LIKELY (g_list_find_custom (timeline->tracks,
486                 (gconstpointer) trobj->track,
487                 (GCompareFunc) custom_find_track))) {
488       GST_DEBUG ("Belongs to one of the tracks we control");
489       ges_track_remove_object (trobj->track, trobj);
490
491       ges_timeline_object_release_track_object (object, trobj);
492     }
493   }
494
495   GST_DEBUG ("Done");
496 }
497
498 /**
499  * ges_timeline_add_layer:
500  * @timeline: a #GESTimeline
501  * @layer: the #GESTimelineLayer to add
502  *
503  * Add the layer to the timeline. The reference to the @layer will be stolen
504  * by the @timeline.
505  *
506  * Returns: TRUE if the layer was properly added, else FALSE.
507  */
508 gboolean
509 ges_timeline_add_layer (GESTimeline * timeline, GESTimelineLayer * layer)
510 {
511   GList *objects, *tmp;
512
513   GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
514
515   /* We can only add a layer that doesn't already belong to another timeline */
516   if (G_UNLIKELY (layer->timeline)) {
517     GST_WARNING ("Layer belongs to another timeline, can't add it");
518     return FALSE;
519   }
520
521   /* Add to the list of layers, make sure we don't already control it */
522   if (G_UNLIKELY (g_list_find (timeline->layers, (gconstpointer) layer))) {
523     GST_WARNING ("Layer is already controlled by this timeline");
524     return FALSE;
525   }
526
527   /* Reference is stolen */
528   timeline->layers = g_list_append (timeline->layers, layer);
529
530   /* Inform the layer that it belongs to a new timeline */
531   ges_timeline_layer_set_timeline (layer, timeline);
532
533   /* Connect to 'object-added'/'object-removed' signal from the new layer */
534   g_signal_connect (layer, "object-added", G_CALLBACK (layer_object_added_cb),
535       timeline);
536   g_signal_connect (layer, "object-removed",
537       G_CALLBACK (layer_object_removed_cb), timeline);
538
539   GST_DEBUG ("Done adding layer, emitting 'layer-added' signal");
540   g_signal_emit (timeline, ges_timeline_signals[LAYER_ADDED], 0, layer);
541
542   /* add any existing timeline objects to the timeline */
543   objects = ges_timeline_layer_get_objects (layer);
544   for (tmp = objects; tmp; tmp = tmp->next) {
545     layer_object_added_cb (layer, tmp->data, timeline);
546     g_object_unref (tmp->data);
547     tmp->data = NULL;
548   }
549   g_list_free (objects);
550
551   return TRUE;
552 }
553
554 /**
555  * ges_timeline_remove_layer:
556  * @timeline: a #GESTimeline
557  * @layer: the #GESTimelineLayer to remove
558  *
559  * Removes the layer from the timeline. The reference that the @timeline holds on
560  * the layer will be dropped. If you wish to use the @layer after calling this
561  * method, you need to take a reference before calling.
562  *
563  * Returns: TRUE if the layer was properly removed, else FALSE.
564  */
565
566 gboolean
567 ges_timeline_remove_layer (GESTimeline * timeline, GESTimelineLayer * layer)
568 {
569   GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
570
571   if (G_UNLIKELY (!g_list_find (timeline->layers, layer))) {
572     GST_WARNING ("Layer doesn't belong to this timeline");
573     return FALSE;
574   }
575
576   /* Disconnect signals */
577   GST_DEBUG ("Disconnecting signal callbacks");
578   g_signal_handlers_disconnect_by_func (layer, layer_object_added_cb, timeline);
579   g_signal_handlers_disconnect_by_func (layer, layer_object_removed_cb,
580       timeline);
581
582   timeline->layers = g_list_remove (timeline->layers, layer);
583
584   ges_timeline_layer_set_timeline (layer, NULL);
585
586   g_signal_emit (timeline, ges_timeline_signals[LAYER_REMOVED], 0, layer);
587
588   g_object_unref (layer);
589
590   return TRUE;
591 }
592
593 static void
594 pad_added_cb (GESTrack * track, GstPad * pad, TrackPrivate * priv)
595 {
596   gchar *padname;
597
598
599   GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));
600
601   if (G_UNLIKELY (priv->pad)) {
602     GST_WARNING ("We are already controlling a pad for this track");
603     return;
604   }
605
606   /* Remember the pad */
607   priv->pad = pad;
608
609   /* ghost it ! */
610   GST_DEBUG ("Ghosting pad and adding it to ourself");
611   padname = g_strdup_printf ("track_%p_src", track);
612   priv->ghostpad = gst_ghost_pad_new (padname, pad);
613   g_free (padname);
614   gst_pad_set_active (priv->ghostpad, TRUE);
615   gst_element_add_pad (GST_ELEMENT (priv->timeline), priv->ghostpad);
616 }
617
618 static void
619 pad_removed_cb (GESTrack * track, GstPad * pad, TrackPrivate * priv)
620 {
621   GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));
622
623   if (G_UNLIKELY (priv->pad != pad)) {
624     GST_WARNING ("Not the pad we're controlling");
625     return;
626   }
627
628   if (G_UNLIKELY (priv->ghostpad == NULL)) {
629     GST_WARNING ("We don't have a ghostpad for this pad !");
630     return;
631   }
632
633   GST_DEBUG ("Removing ghostpad");
634   gst_pad_set_active (priv->ghostpad, FALSE);
635   gst_element_remove_pad (GST_ELEMENT (priv->timeline), priv->ghostpad);
636   priv->ghostpad = NULL;
637   priv->pad = NULL;
638 }
639
640 gint
641 custom_find_track (TrackPrivate * priv, GESTrack * track)
642 {
643   if (priv->track == track)
644     return 0;
645   return -1;
646 }
647
648 /**
649  * ges_timeline_add_track:
650  * @timeline: a #GESTimeline
651  * @track: the #GESTrack to add
652  *
653  * Add a track to the timeline. The reference to the track will be stolen by the
654  * pipeline.
655  *
656  * Returns: TRUE if the track was properly added, else FALSE.
657  */
658
659 gboolean
660 ges_timeline_add_track (GESTimeline * timeline, GESTrack * track)
661 {
662   TrackPrivate *priv;
663   GList *tmp;
664
665   GST_DEBUG ("timeline:%p, track:%p", timeline, track);
666
667   /* make sure we don't already control it */
668   if (G_UNLIKELY (g_list_find_custom (timeline->tracks, (gconstpointer) track,
669               (GCompareFunc) custom_find_track))) {
670     GST_WARNING ("Track is already controlled by this timeline");
671     return FALSE;
672   }
673
674   /* Add the track to ourself (as a GstBin) 
675    * Reference is stolen ! */
676   if (G_UNLIKELY (!gst_bin_add (GST_BIN (timeline), GST_ELEMENT (track)))) {
677     GST_WARNING ("Couldn't add track to ourself (GST)");
678     return FALSE;
679   }
680
681   priv = g_new0 (TrackPrivate, 1);
682   priv->timeline = timeline;
683   priv->track = track;
684
685   /* Add the track to the list of tracks we track */
686   timeline->tracks = g_list_append (timeline->tracks, priv);
687
688   /* Listen to pad-added/-removed */
689   g_signal_connect (track, "pad-added", (GCallback) pad_added_cb, priv);
690   g_signal_connect (track, "pad-removed", (GCallback) pad_removed_cb, priv);
691
692   /* Inform the track that it's currently being used by ourself */
693   ges_track_set_timeline (track, timeline);
694
695   GST_DEBUG ("Done adding track, emitting 'track-added' signal");
696
697   /* emit 'track-added' */
698   g_signal_emit (timeline, ges_timeline_signals[TRACK_ADDED], 0, track);
699
700   /* ensure that each existing timeline object has the opportunity to create a
701    * track object for this track*/
702
703   for (tmp = timeline->layers; tmp; tmp = tmp->next) {
704     GList *objects, *obj;
705     objects = ges_timeline_layer_get_objects (tmp->data);
706
707     for (obj = objects; obj; obj = obj->next) {
708       add_object_to_track (obj->data, track);
709       g_object_unref (obj->data);
710       obj->data = NULL;
711     }
712     g_list_free (objects);
713   }
714
715   return TRUE;
716 }
717
718 /**
719  * ges_timeline_remove_track:
720  * @timeline: a #GESTimeline
721  * @track: the #GESTrack to remove
722  *
723  * Remove the @track from the @timeline. The reference stolen when adding the
724  * @track will be removed. If you wish to use the @track after calling this
725  * function you must ensure that you have a reference to it.
726  *
727  * Returns: TRUE if the @track was properly removed, else FALSE.
728  */
729 gboolean
730 ges_timeline_remove_track (GESTimeline * timeline, GESTrack * track)
731 {
732   GList *tmp;
733   TrackPrivate *priv;
734
735   GST_DEBUG ("timeline:%p, track:%p", timeline, track);
736
737   if (G_UNLIKELY (!(tmp =
738               g_list_find_custom (timeline->tracks, (gconstpointer) track,
739                   (GCompareFunc) custom_find_track)))) {
740     GST_WARNING ("Track doesn't belong to this timeline");
741     return FALSE;
742   }
743
744   priv = tmp->data;
745   timeline->tracks = g_list_remove (timeline->tracks, priv);
746
747   ges_track_set_timeline (track, NULL);
748
749   /* Remove ghost pad */
750   if (priv->ghostpad) {
751     GST_DEBUG ("Removing ghostpad");
752     gst_pad_set_active (priv->ghostpad, FALSE);
753     gst_ghost_pad_set_target ((GstGhostPad *) priv->ghostpad, NULL);
754     gst_element_remove_pad (GST_ELEMENT (timeline), priv->ghostpad);
755   }
756
757   /* Remove pad-added/-removed handlers */
758   g_signal_handlers_disconnect_by_func (track, pad_added_cb, priv);
759   g_signal_handlers_disconnect_by_func (track, pad_removed_cb, priv);
760
761   /* Signal track removal to all layers/objects */
762   g_signal_emit (timeline, ges_timeline_signals[TRACK_REMOVED], 0, track);
763
764   /* remove track from our bin */
765   if (G_UNLIKELY (!gst_bin_remove (GST_BIN (timeline), GST_ELEMENT (track)))) {
766     GST_WARNING ("Couldn't remove track to ourself (GST)");
767     return FALSE;
768   }
769
770   g_free (priv);
771
772   return TRUE;
773 }
774
775 /**
776  * ges_timeline_get_track_for_pad:
777  * @timeline: The #GESTimeline
778  * @pad: The #GstPad
779  *
780  * Search the #GESTrack corresponding to the given @timeline's @pad.
781  *
782  * Returns: The corresponding #GESTrack if it is found, or #NULL if there is
783  * an error.
784  */
785
786 GESTrack *
787 ges_timeline_get_track_for_pad (GESTimeline * timeline, GstPad * pad)
788 {
789   GList *tmp;
790
791   for (tmp = timeline->tracks; tmp; tmp = g_list_next (tmp)) {
792     TrackPrivate *priv = (TrackPrivate *) tmp->data;
793     if (pad == priv->ghostpad)
794       return priv->track;
795   }
796
797   return NULL;
798 }
799
800 /**
801  * ges_timeline_get_tracks:
802  * @timeline: a #GESTimeline
803  *
804  * Returns the list of #GESTrack used by the Timeline.
805  *
806  * Returns: A list of #GESTrack. The caller should unref each track
807  * once he is done with them. */
808 GList *
809 ges_timeline_get_tracks (GESTimeline * timeline)
810 {
811   GList *tmp, *res = NULL;
812
813   for (tmp = timeline->tracks; tmp; tmp = g_list_next (tmp)) {
814     TrackPrivate *priv = (TrackPrivate *) tmp->data;
815     res = g_list_append (res, g_object_ref (priv->track));
816   }
817
818   return res;
819 }