1 /* GStreamer Editing Services
2 * Copyright (C) 2009 Edward Hervey <edward.hervey@collabora.co.uk>
3 * 2009 Nokia Corporation
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.
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.
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.
22 * SECTION:ges-timeline
23 * @short_description: Multimedia timeline
25 * #GESTimeline is the central object for any multimedia timeline.
27 * Contains a list of #GESTimelineLayer which users should use to arrange the
28 * various timeline objects through time.
30 * The output type is determined by the #GESTrack that are set on
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.
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"
47 G_DEFINE_TYPE (GESTimeline, ges_timeline, GST_TYPE_BIN);
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)))
56 struct _GESTimelinePrivate
58 GList *layers; /* A list of GESTimelineLayer sorted by priority */
59 GList *tracks; /* A list of private track data */
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;
67 /* Whether we are changing state asynchronously or not */
68 gboolean async_pending;
71 /* private structure to contain our track-related information */
75 GESTimeline *timeline;
77 GstPad *pad; /* Pad from the track */
90 static GstBinClass *parent_class;
92 static guint ges_timeline_signals[LAST_SIGNAL] = { 0 };
94 static gint custom_find_track (TrackPrivate * tr_priv, GESTrack * track);
95 static GstStateChangeReturn
96 ges_timeline_change_state (GstElement * element, GstStateChange transition);
98 discoverer_finished_cb (GstDiscoverer * discoverer, GESTimeline * timeline);
100 discoverer_discovered_cb (GstDiscoverer * discoverer,
101 GstDiscovererInfo * info, GError * err, GESTimeline * timeline);
104 ges_timeline_get_property (GObject * object, guint property_id,
105 GValue * value, GParamSpec * pspec)
107 switch (property_id) {
109 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
114 ges_timeline_set_property (GObject * object, guint property_id,
115 const GValue * value, GParamSpec * pspec)
117 switch (property_id) {
119 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
124 ges_timeline_dispose (GObject * object)
126 GESTimelinePrivate *priv = GES_TIMELINE (object)->priv;
128 if (priv->discoverer) {
129 gst_discoverer_stop (priv->discoverer);
130 g_object_unref (priv->discoverer);
131 priv->discoverer = NULL;
134 while (priv->layers) {
135 GESTimelineLayer *layer = (GESTimelineLayer *) priv->layers->data;
136 ges_timeline_remove_layer (GES_TIMELINE (object), layer);
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.
144 while (priv->tracks) {
145 TrackPrivate *tr_priv = (TrackPrivate *) priv->tracks->data;
146 ges_timeline_remove_track (GES_TIMELINE (object), tr_priv->track);
149 G_OBJECT_CLASS (ges_timeline_parent_class)->dispose (object);
153 ges_timeline_finalize (GObject * object)
155 GESTimeline *timeline = GES_TIMELINE (object);
157 g_mutex_free (timeline->priv->pendingobjects_lock);
159 G_OBJECT_CLASS (ges_timeline_parent_class)->finalize (object);
163 ges_timeline_class_init (GESTimelineClass * klass)
165 GObjectClass *object_class = G_OBJECT_CLASS (klass);
166 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
168 g_type_class_add_private (klass, sizeof (GESTimelinePrivate));
170 parent_class = g_type_class_peek_parent (klass);
172 element_class->change_state = ges_timeline_change_state;
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;
180 * GESTimeline::track-added
181 * @timeline: the #GESTimeline
182 * @track: the #GESTrack that was added to the timeline
184 * Will be emitted after the track was added to the timeline.
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);
192 * GESTimeline::track-removed
193 * @timeline: the #GESTimeline
194 * @track: the #GESTrack that was removed from the timeline
196 * Will be emitted after the track was removed from the timeline.
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);
204 * GESTimeline::layer-added
205 * @timeline: the #GESTimeline
206 * @layer: the #GESTimelineLayer that was added to the timeline
208 * Will be emitted after the layer was added to the timeline.
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);
216 * GESTimeline::layer-removed
217 * @timeline: the #GESTimeline
218 * @layer: the #GESTimelineLayer that was removed from the timeline
220 * Will be emitted after the layer was removed from the timeline.
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);
230 ges_timeline_init (GESTimeline * self)
233 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
234 GES_TYPE_TIMELINE, GESTimelinePrivate);
236 self->priv->layers = NULL;
237 self->priv->tracks = NULL;
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);
252 * Creates a new empty #GESTimeline.
254 * Returns: The new timeline.
258 ges_timeline_new (void)
260 return g_object_new (GES_TYPE_TIMELINE, NULL);
264 * ges_timeline_new_from_uri:
265 * @uri: the URI to load from
267 * Creates a timeline from the given URI.
269 * Returns: A new timeline if the uri was loaded successfully, or NULL if the
270 * uri could not be loaded
274 ges_timeline_new_from_uri (const gchar * uri)
278 /* FIXME : we should have a GError** argument so the user can know why
279 * it wasn't able to load the uri
282 ret = ges_timeline_new ();
284 if (!ges_timeline_load_from_uri (ret, uri)) {
285 g_object_unref (ret);
294 * ges_timeline_load_from_uri:
295 * @timeline: an empty #GESTimeline into which to load the formatter
296 * @uri: The URI to load from
298 * Loads the contents of URI into the given timeline.
300 * Returns: TRUE if the timeline was loaded successfully, or FALSE if the uri
301 * could not be loaded.
305 ges_timeline_load_from_uri (GESTimeline * timeline, const gchar * uri)
307 GESFormatter *p = NULL;
308 gboolean ret = FALSE;
310 /* FIXME : we should have a GError** argument so the user can know why
311 * it wasn't able to load the uri
314 if (!(p = ges_formatter_new_for_uri (uri))) {
315 GST_ERROR ("unsupported uri '%s'", uri);
319 if (!ges_formatter_load_from_uri (p, timeline, uri)) {
320 GST_ERROR ("error deserializing formatter");
333 * ges_timeline_save_to_uri:
334 * @timeline: a #GESTimeline
335 * @uri: The location to save to
337 * Saves the timeline to the given location
339 * Returns: TRUE if the timeline was successfully saved to the given location,
344 ges_timeline_save_to_uri (GESTimeline * timeline, const gchar * uri)
346 GESFormatter *p = NULL;
347 gboolean ret = FALSE;
349 /* FIXME : How will the user be able to chose the format he
350 * wishes to store to ? */
352 /* FIXME : How will we ensure a timeline loaded with a certain format
353 * will be saved with the same one by default ? We need to make this
354 * easy from an API perspective */
356 /* FIXME : we should have a GError** argument so the user can know why
357 * it wasn't able to save
360 if (!(p = ges_formatter_new_for_uri (uri))) {
361 GST_ERROR ("unsupported uri '%s'", uri);
365 if (!ges_formatter_save_to_uri (p, timeline, uri)) {
366 GST_ERROR ("error serializing formatter");
379 add_object_to_track (GESTimelineObject * object, GESTrack * track)
381 if (!ges_timeline_object_create_track_objects (object, track)) {
382 GST_WARNING ("error creating track objects");
387 add_object_to_tracks (GESTimeline * timeline, GESTimelineObject * object)
391 for (tmp = timeline->priv->tracks; tmp; tmp = g_list_next (tmp)) {
392 TrackPrivate *tr_priv = (TrackPrivate *) tmp->data;
393 GESTrack *track = tr_priv->track;
395 GST_LOG ("Trying with track %p", track);
396 add_object_to_track (object, track);
402 do_async_start (GESTimeline * timeline)
407 timeline->priv->async_pending = TRUE;
409 /* Freeze state of tracks */
410 for (tmp = timeline->priv->tracks; tmp; tmp = tmp->next) {
411 TrackPrivate *tr_priv = (TrackPrivate *) tmp->data;
412 gst_element_set_locked_state ((GstElement *) tr_priv->track, TRUE);
415 message = gst_message_new_async_start (GST_OBJECT_CAST (timeline), FALSE);
416 parent_class->handle_message (GST_BIN_CAST (timeline), message);
420 do_async_done (GESTimeline * timeline)
424 if (timeline->priv->async_pending) {
426 /* Unfreeze state of tracks */
427 for (tmp = timeline->priv->tracks; tmp; tmp = tmp->next) {
428 TrackPrivate *tr_priv = (TrackPrivate *) tmp->data;
429 gst_element_set_locked_state ((GstElement *) tr_priv->track, FALSE);
430 gst_element_sync_state_with_parent ((GstElement *) tr_priv->track);
433 GST_DEBUG_OBJECT (timeline, "Emitting async-done");
434 message = gst_message_new_async_done (GST_OBJECT_CAST (timeline));
435 parent_class->handle_message (GST_BIN_CAST (timeline), message);
437 timeline->priv->async_pending = FALSE;
442 discoverer_finished_cb (GstDiscoverer * discoverer, GESTimeline * timeline)
444 do_async_done (timeline);
448 discoverer_discovered_cb (GstDiscoverer * discoverer,
449 GstDiscovererInfo * info, GError * err, GESTimeline * timeline)
452 gboolean found = FALSE;
453 gboolean is_image = FALSE;
454 GESTimelineFileSource *tfs = NULL;
455 GESTimelinePrivate *priv = timeline->priv;
456 const gchar *uri = gst_discoverer_info_get_uri (info);
458 GST_DEBUG ("Discovered uri %s", uri);
460 GES_TIMELINE_PENDINGOBJS_LOCK (timeline);
462 /* Find corresponding TimelineFileSource in the sources */
463 for (tmp = priv->pendingobjects; tmp; tmp = tmp->next) {
464 tfs = (GESTimelineFileSource *) tmp->data;
466 if (!g_strcmp0 (ges_timeline_filesource_get_uri (tfs), uri)) {
474 GESTrackType tfs_supportedformats;
476 /* The timeline file source will be updated with discovered information
477 * so it needs to not be finalized during this process */
480 /* Remove object from list */
481 priv->pendingobjects = g_list_delete_link (priv->pendingobjects, tmp);
482 GES_TIMELINE_PENDINGOBJS_UNLOCK (timeline);
484 /* FIXME : Handle errors in discovery */
485 stream_list = gst_discoverer_info_get_stream_list (info);
487 /* Update timelinefilesource properties based on info */
488 for (tmp = stream_list; tmp; tmp = tmp->next) {
489 GstDiscovererStreamInfo *sinf = (GstDiscovererStreamInfo *) tmp->data;
491 tfs_supportedformats =
492 ges_timeline_filesource_get_supported_formats (tfs);
494 if (GST_IS_DISCOVERER_AUDIO_INFO (sinf)) {
495 tfs_supportedformats |= GES_TRACK_TYPE_AUDIO;
496 ges_timeline_filesource_set_supported_formats (tfs,
497 tfs_supportedformats);
498 } else if (GST_IS_DISCOVERER_VIDEO_INFO (sinf)) {
499 tfs_supportedformats |= GES_TRACK_TYPE_VIDEO;
500 ges_timeline_filesource_set_supported_formats (tfs,
501 tfs_supportedformats);
502 if (gst_discoverer_video_info_is_image ((GstDiscovererVideoInfo *)
504 tfs_supportedformats |= GES_TRACK_TYPE_AUDIO;
505 ges_timeline_filesource_set_supported_formats (tfs,
506 tfs_supportedformats);
513 gst_discoverer_stream_info_list_free (stream_list);
516 /* don't set max-duration on still images */
517 g_object_set (tfs, "is_image", (gboolean) TRUE, NULL);
521 g_object_set (tfs, "max-duration",
522 gst_discoverer_info_get_duration (info), NULL);
525 /* Continue the processing on tfs */
526 add_object_to_tracks (timeline, GES_TIMELINE_OBJECT (tfs));
528 /* Remove the ref as the timeline file source is no longer needed here */
529 g_object_unref (tfs);
531 GES_TIMELINE_PENDINGOBJS_UNLOCK (timeline);
535 static GstStateChangeReturn
536 ges_timeline_change_state (GstElement * element, GstStateChange transition)
538 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
539 GESTimeline *timeline = GES_TIMELINE (element);
541 switch (transition) {
542 case GST_STATE_CHANGE_READY_TO_PAUSED:
543 GES_TIMELINE_PENDINGOBJS_LOCK (timeline);
544 if (timeline->priv->pendingobjects) {
545 GES_TIMELINE_PENDINGOBJS_UNLOCK (timeline);
546 do_async_start (timeline);
547 ret = GST_STATE_CHANGE_ASYNC;
549 GES_TIMELINE_PENDINGOBJS_UNLOCK (timeline);
557 GstStateChangeReturn bret;
559 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
560 if (G_UNLIKELY (bret == GST_STATE_CHANGE_NO_PREROLL)) {
561 do_async_done (timeline);
566 switch (transition) {
567 case GST_STATE_CHANGE_PAUSED_TO_READY:
568 do_async_done (timeline);
579 layer_object_added_cb (GESTimelineLayer * layer, GESTimelineObject * object,
580 GESTimeline * timeline)
582 GST_DEBUG ("New TimelineObject %p added to layer %p", object, layer);
584 if (GES_IS_TIMELINE_FILE_SOURCE (object)) {
585 GESTimelineFileSource *tfs = GES_TIMELINE_FILE_SOURCE (object);
586 GESTrackType tfs_supportedformats =
587 ges_timeline_filesource_get_supported_formats (tfs);
588 guint64 tfs_maxdur = ges_timeline_filesource_get_max_duration (tfs);
589 const gchar *tfs_uri;
591 /* Send the filesource to the discoverer if:
592 * * it doesn't have specified supported formats
593 * * OR it doesn't have a specified max-duration
594 * * OR it doesn't have a valid duration */
596 if (tfs_supportedformats == GES_TRACK_TYPE_UNKNOWN ||
597 tfs_maxdur == GST_CLOCK_TIME_NONE || object->duration == 0) {
598 GST_LOG ("Incomplete TimelineFileSource, discovering it");
599 tfs_uri = ges_timeline_filesource_get_uri (tfs);
601 GES_TIMELINE_PENDINGOBJS_LOCK (timeline);
602 timeline->priv->pendingobjects =
603 g_list_append (timeline->priv->pendingobjects, object);
604 GES_TIMELINE_PENDINGOBJS_UNLOCK (timeline);
606 gst_discoverer_discover_uri_async (timeline->priv->discoverer, tfs_uri);
608 add_object_to_tracks (timeline, object);
610 add_object_to_tracks (timeline, object);
618 layer_object_removed_cb (GESTimelineLayer * layer, GESTimelineObject * object,
619 GESTimeline * timeline)
621 GList *tmp, *trackobjects;
623 GST_DEBUG ("TimelineObject %p removed from layer %p", object, layer);
625 /* Go over the object's track objects and figure out which one belongs to
626 * the list of tracks we control */
628 trackobjects = ges_timeline_object_get_track_objects (object);
629 for (tmp = trackobjects; tmp; tmp = tmp->next) {
630 GESTrackObject *trobj = (GESTrackObject *) tmp->data;
632 GST_DEBUG ("Trying to remove TrackObject %p", trobj);
633 if (G_LIKELY (g_list_find_custom (timeline->priv->tracks,
634 ges_track_object_get_track (trobj),
635 (GCompareFunc) custom_find_track))) {
636 GST_DEBUG ("Belongs to one of the tracks we control");
637 ges_track_remove_object (ges_track_object_get_track (trobj), trobj);
639 ges_timeline_object_release_track_object (object, trobj);
642 /* removing the reference added by _get_track_objects() */
643 g_object_unref (trobj);
645 g_list_free (trackobjects);
647 /* if the object is a timeline file source that has not yet been discovered,
648 * it no longer needs to be discovered so remove it from the pendingobjects
649 * list if it belongs to this layer */
650 if (GES_IS_TIMELINE_FILE_SOURCE (object)) {
651 GList *pendingobjects;
653 GES_TIMELINE_PENDINGOBJS_LOCK (timeline);
654 pendingobjects = timeline->priv->pendingobjects;
656 tmp = pendingobjects;
658 GList *next = tmp->next;
660 if (layer == (GESTimelineLayer *) ((GESTimelineObject *) tmp->data)->priv)
661 pendingobjects = g_list_delete_link (pendingobjects, tmp);
664 timeline->priv->pendingobjects = pendingobjects;
665 GES_TIMELINE_PENDINGOBJS_UNLOCK (timeline);
672 * ges_timeline_add_layer:
673 * @timeline: a #GESTimeline
674 * @layer: the #GESTimelineLayer to add
676 * Add the layer to the timeline. The reference to the @layer will be stolen
679 * Returns: TRUE if the layer was properly added, else FALSE.
682 ges_timeline_add_layer (GESTimeline * timeline, GESTimelineLayer * layer)
684 GList *objects, *tmp;
685 GESTimelinePrivate *priv = timeline->priv;
687 GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
689 /* We can only add a layer that doesn't already belong to another timeline */
690 if (G_UNLIKELY (layer->timeline)) {
691 GST_WARNING ("Layer belongs to another timeline, can't add it");
695 /* Add to the list of layers, make sure we don't already control it */
696 if (G_UNLIKELY (g_list_find (priv->layers, (gconstpointer) layer))) {
697 GST_WARNING ("Layer is already controlled by this timeline");
701 g_object_ref_sink (layer);
702 priv->layers = g_list_append (priv->layers, layer);
704 /* Inform the layer that it belongs to a new timeline */
705 ges_timeline_layer_set_timeline (layer, timeline);
707 /* Connect to 'object-added'/'object-removed' signal from the new layer */
708 g_signal_connect (layer, "object-added", G_CALLBACK (layer_object_added_cb),
710 g_signal_connect (layer, "object-removed",
711 G_CALLBACK (layer_object_removed_cb), timeline);
713 GST_DEBUG ("Done adding layer, emitting 'layer-added' signal");
714 g_signal_emit (timeline, ges_timeline_signals[LAYER_ADDED], 0, layer);
716 /* add any existing timeline objects to the timeline */
717 objects = ges_timeline_layer_get_objects (layer);
718 for (tmp = objects; tmp; tmp = tmp->next) {
719 layer_object_added_cb (layer, tmp->data, timeline);
720 g_object_unref (tmp->data);
723 g_list_free (objects);
729 * ges_timeline_remove_layer:
730 * @timeline: a #GESTimeline
731 * @layer: the #GESTimelineLayer to remove
733 * Removes the layer from the timeline. The reference that the @timeline holds on
734 * the layer will be dropped. If you wish to use the @layer after calling this
735 * method, you need to take a reference before calling.
737 * Returns: TRUE if the layer was properly removed, else FALSE.
741 ges_timeline_remove_layer (GESTimeline * timeline, GESTimelineLayer * layer)
743 GList *layer_objects, *tmp;
744 GESTimelinePrivate *priv = timeline->priv;
746 GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
748 if (G_UNLIKELY (!g_list_find (priv->layers, layer))) {
749 GST_WARNING ("Layer doesn't belong to this timeline");
753 /* remove objects from any private data structures */
755 layer_objects = ges_timeline_layer_get_objects (layer);
756 for (tmp = layer_objects; tmp; tmp = tmp->next) {
757 layer_object_removed_cb (layer, GES_TIMELINE_OBJECT (tmp->data), timeline);
758 g_object_unref (G_OBJECT (tmp->data));
761 g_list_free (layer_objects);
763 /* Disconnect signals */
764 GST_DEBUG ("Disconnecting signal callbacks");
765 g_signal_handlers_disconnect_by_func (layer, layer_object_added_cb, timeline);
766 g_signal_handlers_disconnect_by_func (layer, layer_object_removed_cb,
769 priv->layers = g_list_remove (priv->layers, layer);
771 ges_timeline_layer_set_timeline (layer, NULL);
773 g_signal_emit (timeline, ges_timeline_signals[LAYER_REMOVED], 0, layer);
775 g_object_unref (layer);
781 pad_added_cb (GESTrack * track, GstPad * pad, TrackPrivate * tr_priv)
786 GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));
788 if (G_UNLIKELY (tr_priv->pad)) {
789 GST_WARNING ("We are already controlling a pad for this track");
793 /* Remember the pad */
797 GST_DEBUG ("Ghosting pad and adding it to ourself");
798 padname = g_strdup_printf ("track_%p_src", track);
799 tr_priv->ghostpad = gst_ghost_pad_new (padname, pad);
801 gst_pad_set_active (tr_priv->ghostpad, TRUE);
802 gst_element_add_pad (GST_ELEMENT (tr_priv->timeline), tr_priv->ghostpad);
806 pad_removed_cb (GESTrack * track, GstPad * pad, TrackPrivate * tr_priv)
808 GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));
810 if (G_UNLIKELY (tr_priv->pad != pad)) {
811 GST_WARNING ("Not the pad we're controlling");
815 if (G_UNLIKELY (tr_priv->ghostpad == NULL)) {
816 GST_WARNING ("We don't have a ghostpad for this pad !");
820 GST_DEBUG ("Removing ghostpad");
821 gst_pad_set_active (tr_priv->ghostpad, FALSE);
822 gst_element_remove_pad (GST_ELEMENT (tr_priv->timeline), tr_priv->ghostpad);
823 tr_priv->ghostpad = NULL;
828 custom_find_track (TrackPrivate * tr_priv, GESTrack * track)
830 if (tr_priv->track == track)
836 * ges_timeline_add_track:
837 * @timeline: a #GESTimeline
838 * @track: the #GESTrack to add
840 * Add a track to the timeline. The reference to the track will be stolen by the
843 * Returns: TRUE if the track was properly added, else FALSE.
846 /* FIXME: create track objects for timeline objects which have already been
847 * added to existing layers.
851 ges_timeline_add_track (GESTimeline * timeline, GESTrack * track)
853 TrackPrivate *tr_priv;
854 GESTimelinePrivate *priv = timeline->priv;
857 GST_DEBUG ("timeline:%p, track:%p", timeline, track);
859 /* make sure we don't already control it */
860 if (G_UNLIKELY (g_list_find_custom (priv->tracks, (gconstpointer) track,
861 (GCompareFunc) custom_find_track))) {
862 GST_WARNING ("Track is already controlled by this timeline");
866 /* Add the track to ourself (as a GstBin)
867 * Reference is stolen ! */
868 if (G_UNLIKELY (!gst_bin_add (GST_BIN (timeline), GST_ELEMENT (track)))) {
869 GST_WARNING ("Couldn't add track to ourself (GST)");
873 tr_priv = g_new0 (TrackPrivate, 1);
874 tr_priv->timeline = timeline;
875 tr_priv->track = track;
877 /* Add the track to the list of tracks we track */
878 priv->tracks = g_list_append (priv->tracks, tr_priv);
880 /* Listen to pad-added/-removed */
881 g_signal_connect (track, "pad-added", (GCallback) pad_added_cb, tr_priv);
882 g_signal_connect (track, "pad-removed", (GCallback) pad_removed_cb, tr_priv);
884 /* Inform the track that it's currently being used by ourself */
885 ges_track_set_timeline (track, timeline);
887 GST_DEBUG ("Done adding track, emitting 'track-added' signal");
889 /* emit 'track-added' */
890 g_signal_emit (timeline, ges_timeline_signals[TRACK_ADDED], 0, track);
892 /* ensure that each existing timeline object has the opportunity to create a
893 * track object for this track*/
895 for (tmp = priv->layers; tmp; tmp = tmp->next) {
896 GList *objects, *obj;
897 objects = ges_timeline_layer_get_objects (tmp->data);
899 for (obj = objects; obj; obj = obj->next) {
900 add_object_to_track (obj->data, track);
901 g_object_unref (obj->data);
904 g_list_free (objects);
911 * ges_timeline_remove_track:
912 * @timeline: a #GESTimeline
913 * @track: the #GESTrack to remove
915 * Remove the @track from the @timeline. The reference stolen when adding the
916 * @track will be removed. If you wish to use the @track after calling this
917 * function you must ensure that you have a reference to it.
919 * Returns: TRUE if the @track was properly removed, else FALSE.
922 /* FIXME: release any track objects associated with this layer. currenly this
923 * will not happen if you remove the track before removing *all*
924 * timelineobjects which have a track object in this track.
928 ges_timeline_remove_track (GESTimeline * timeline, GESTrack * track)
931 TrackPrivate *tr_priv;
932 GESTimelinePrivate *priv = timeline->priv;
934 GST_DEBUG ("timeline:%p, track:%p", timeline, track);
936 if (G_UNLIKELY (!(tmp =
937 g_list_find_custom (priv->tracks, (gconstpointer) track,
938 (GCompareFunc) custom_find_track)))) {
939 GST_WARNING ("Track doesn't belong to this timeline");
944 priv->tracks = g_list_remove (priv->tracks, tr_priv);
946 ges_track_set_timeline (track, NULL);
948 /* Remove ghost pad */
949 if (tr_priv->ghostpad) {
950 GST_DEBUG ("Removing ghostpad");
951 gst_pad_set_active (tr_priv->ghostpad, FALSE);
952 gst_ghost_pad_set_target ((GstGhostPad *) tr_priv->ghostpad, NULL);
953 gst_element_remove_pad (GST_ELEMENT (timeline), tr_priv->ghostpad);
956 /* Remove pad-added/-removed handlers */
957 g_signal_handlers_disconnect_by_func (track, pad_added_cb, tr_priv);
958 g_signal_handlers_disconnect_by_func (track, pad_removed_cb, tr_priv);
960 /* Signal track removal to all layers/objects */
961 g_signal_emit (timeline, ges_timeline_signals[TRACK_REMOVED], 0, track);
963 /* remove track from our bin */
964 gst_object_ref (track);
965 if (G_UNLIKELY (!gst_bin_remove (GST_BIN (timeline), GST_ELEMENT (track)))) {
966 GST_WARNING ("Couldn't remove track to ourself (GST)");
967 gst_object_unref (track);
971 /* set track state to NULL */
973 gst_element_set_state (GST_ELEMENT (track), GST_STATE_NULL);
975 gst_object_unref (track);
983 * ges_timeline_get_track_for_pad:
984 * @timeline: The #GESTimeline
987 * Search the #GESTrack corresponding to the given @timeline's @pad.
989 * Returns: (transfer none): The corresponding #GESTrack if it is found,
990 * or %NULL if there is an error.
994 ges_timeline_get_track_for_pad (GESTimeline * timeline, GstPad * pad)
998 for (tmp = timeline->priv->tracks; tmp; tmp = g_list_next (tmp)) {
999 TrackPrivate *tr_priv = (TrackPrivate *) tmp->data;
1000 if (pad == tr_priv->ghostpad)
1001 return tr_priv->track;
1008 * ges_timeline_get_tracks:
1009 * @timeline: a #GESTimeline
1011 * Returns the list of #GESTrack used by the Timeline.
1013 * Returns: (transfer full) (element-type GESTrack): A list of #GESTrack.
1014 * The caller should unref each track once he is done with them.
1017 ges_timeline_get_tracks (GESTimeline * timeline)
1019 GList *tmp, *res = NULL;
1021 for (tmp = timeline->priv->tracks; tmp; tmp = g_list_next (tmp)) {
1022 TrackPrivate *tr_priv = (TrackPrivate *) tmp->data;
1023 res = g_list_append (res, g_object_ref (tr_priv->track));
1030 * ges_timeline_get_layers:
1031 * @timeline: a #GESTimeline
1033 * Get the list of #GESTimelineLayer present in the Timeline.
1035 * Returns: (transfer full) (element-type GESTimelineLayer): the list of
1036 * #GESTimelineLayer present in the Timeline.
1037 * The caller should unref each Layer once he is done with them.
1040 ges_timeline_get_layers (GESTimeline * timeline)
1042 GList *tmp, *res = NULL;
1044 for (tmp = timeline->priv->layers; tmp; tmp = g_list_next (tmp)) {
1045 res = g_list_append (res, g_object_ref (tmp->data));