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