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