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"
46 static void track_duration_cb (GstElement * track,
47 GParamSpec * arg G_GNUC_UNUSED, GESTimeline * timeline);
49 G_DEFINE_TYPE (GESTimeline, ges_timeline, GST_TYPE_BIN);
51 #define GES_TIMELINE_PENDINGOBJS_GET_LOCK(timeline) \
52 (GES_TIMELINE(timeline)->priv->pendingobjects_lock)
53 #define GES_TIMELINE_PENDINGOBJS_LOCK(timeline) \
54 (g_mutex_lock(GES_TIMELINE_PENDINGOBJS_GET_LOCK (timeline)))
55 #define GES_TIMELINE_PENDINGOBJS_UNLOCK(timeline) \
56 (g_mutex_unlock(GES_TIMELINE_PENDINGOBJS_GET_LOCK (timeline)))
58 struct _GESTimelinePrivate
60 GList *layers; /* A list of GESTimelineLayer sorted by priority */
61 GList *tracks; /* A list of private track data */
63 /* The duration of the timeline */
66 /* discoverer used for virgin sources */
67 GstDiscoverer *discoverer;
68 GList *pendingobjects;
69 /* lock to avoid discovery of objects that will be removed */
70 GMutex *pendingobjects_lock;
72 /* Whether we are changing state asynchronously or not */
73 gboolean async_pending;
76 /* private structure to contain our track-related information */
80 GESTimeline *timeline;
82 GstPad *pad; /* Pad from the track */
93 static GParamSpec *properties[PROP_LAST];
104 static GstBinClass *parent_class;
106 static guint ges_timeline_signals[LAST_SIGNAL] = { 0 };
108 static gint custom_find_track (TrackPrivate * tr_priv, GESTrack * track);
109 static GstStateChangeReturn
110 ges_timeline_change_state (GstElement * element, GstStateChange transition);
112 discoverer_finished_cb (GstDiscoverer * discoverer, GESTimeline * timeline);
114 discoverer_discovered_cb (GstDiscoverer * discoverer,
115 GstDiscovererInfo * info, GError * err, GESTimeline * timeline);
118 ges_timeline_get_property (GObject * object, guint property_id,
119 GValue * value, GParamSpec * pspec)
121 GESTimeline *timeline = GES_TIMELINE (object);
123 switch (property_id) {
125 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
127 g_value_set_uint64 (value, timeline->priv->duration);
133 ges_timeline_set_property (GObject * object, guint property_id,
134 const GValue * value, GParamSpec * pspec)
136 switch (property_id) {
138 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
143 ges_timeline_dispose (GObject * object)
145 GESTimelinePrivate *priv = GES_TIMELINE (object)->priv;
147 if (priv->discoverer) {
148 gst_discoverer_stop (priv->discoverer);
149 g_object_unref (priv->discoverer);
150 priv->discoverer = NULL;
153 while (priv->layers) {
154 GESTimelineLayer *layer = (GESTimelineLayer *) priv->layers->data;
155 ges_timeline_remove_layer (GES_TIMELINE (object), layer);
158 /* FIXME: it should be possible to remove tracks before removing
159 * layers, but at the moment this creates a problem because the track
160 * objects aren't notified that their gnlobjects have been destroyed.
163 while (priv->tracks) {
164 TrackPrivate *tr_priv = (TrackPrivate *) priv->tracks->data;
165 ges_timeline_remove_track (GES_TIMELINE (object), tr_priv->track);
168 G_OBJECT_CLASS (ges_timeline_parent_class)->dispose (object);
172 ges_timeline_finalize (GObject * object)
174 GESTimeline *timeline = GES_TIMELINE (object);
176 g_mutex_free (timeline->priv->pendingobjects_lock);
178 G_OBJECT_CLASS (ges_timeline_parent_class)->finalize (object);
182 ges_timeline_class_init (GESTimelineClass * klass)
184 GObjectClass *object_class = G_OBJECT_CLASS (klass);
185 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
187 g_type_class_add_private (klass, sizeof (GESTimelinePrivate));
189 parent_class = g_type_class_peek_parent (klass);
191 element_class->change_state = ges_timeline_change_state;
193 object_class->get_property = ges_timeline_get_property;
194 object_class->set_property = ges_timeline_set_property;
195 object_class->dispose = ges_timeline_dispose;
196 object_class->finalize = ges_timeline_finalize;
199 * GESTimelineObject:duration
201 * Current duration (in nanoseconds) of the #GESTimeline
205 properties[PROP_DURATION] =
206 g_param_spec_uint64 ("duration", "Duration",
207 "The duration of the timeline", 0, G_MAXUINT64,
208 GST_CLOCK_TIME_NONE, G_PARAM_READABLE);
209 g_object_class_install_property (object_class, PROP_DURATION,
210 properties[PROP_DURATION]);
213 * GESTimeline::track-added
214 * @timeline: the #GESTimeline
215 * @track: the #GESTrack that was added to the timeline
217 * Will be emitted after the track was added to the timeline.
219 ges_timeline_signals[TRACK_ADDED] =
220 g_signal_new ("track-added", G_TYPE_FROM_CLASS (klass),
221 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, track_added), NULL,
222 NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TRACK);
225 * GESTimeline::track-removed
226 * @timeline: the #GESTimeline
227 * @track: the #GESTrack that was removed from the timeline
229 * Will be emitted after the track was removed from the timeline.
231 ges_timeline_signals[TRACK_REMOVED] =
232 g_signal_new ("track-removed", G_TYPE_FROM_CLASS (klass),
233 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, track_removed),
234 NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TRACK);
237 * GESTimeline::layer-added
238 * @timeline: the #GESTimeline
239 * @layer: the #GESTimelineLayer that was added to the timeline
241 * Will be emitted after the layer was added to the timeline.
243 ges_timeline_signals[LAYER_ADDED] =
244 g_signal_new ("layer-added", G_TYPE_FROM_CLASS (klass),
245 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, layer_added), NULL,
246 NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TIMELINE_LAYER);
249 * GESTimeline::layer-removed
250 * @timeline: the #GESTimeline
251 * @layer: the #GESTimelineLayer that was removed from the timeline
253 * Will be emitted after the layer was removed from the timeline.
255 ges_timeline_signals[LAYER_REMOVED] =
256 g_signal_new ("layer-removed", G_TYPE_FROM_CLASS (klass),
257 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, layer_removed),
258 NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
259 GES_TYPE_TIMELINE_LAYER);
263 ges_timeline_init (GESTimeline * self)
266 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
267 GES_TYPE_TIMELINE, GESTimelinePrivate);
269 self->priv->layers = NULL;
270 self->priv->tracks = NULL;
271 self->priv->duration = 0;
273 self->priv->pendingobjects_lock = g_mutex_new ();
274 /* New discoverer with a 15s timeout */
275 self->priv->discoverer = gst_discoverer_new (15 * GST_SECOND, NULL);
276 g_signal_connect (self->priv->discoverer, "finished",
277 G_CALLBACK (discoverer_finished_cb), self);
278 g_signal_connect (self->priv->discoverer, "discovered",
279 G_CALLBACK (discoverer_discovered_cb), self);
280 gst_discoverer_start (self->priv->discoverer);
284 sort_layers (gpointer a, gpointer b)
286 GESTimelineLayer *layer_a, *layer_b;
287 guint prio_a, prio_b;
289 layer_a = GES_TIMELINE_LAYER (a);
290 layer_b = GES_TIMELINE_LAYER (b);
292 prio_a = ges_timeline_layer_get_priority (layer_a);
293 prio_b = ges_timeline_layer_get_priority (layer_b);
295 if ((gint) prio_a > (guint) prio_b)
297 if ((guint) prio_a < (guint) prio_b)
306 * Creates a new empty #GESTimeline.
308 * Returns: The new timeline.
312 ges_timeline_new (void)
314 return g_object_new (GES_TYPE_TIMELINE, NULL);
318 * ges_timeline_new_from_uri:
319 * @uri: the URI to load from
321 * Creates a timeline from the given URI.
323 * Returns: A new timeline if the uri was loaded successfully, or NULL if the
324 * uri could not be loaded
328 ges_timeline_new_from_uri (const gchar * uri)
332 /* FIXME : we should have a GError** argument so the user can know why
333 * it wasn't able to load the uri
336 ret = ges_timeline_new ();
338 if (!ges_timeline_load_from_uri (ret, uri)) {
339 g_object_unref (ret);
348 * ges_timeline_load_from_uri:
349 * @timeline: an empty #GESTimeline into which to load the formatter
350 * @uri: The URI to load from
352 * Loads the contents of URI into the given timeline.
354 * Returns: TRUE if the timeline was loaded successfully, or FALSE if the uri
355 * could not be loaded.
359 ges_timeline_load_from_uri (GESTimeline * timeline, const gchar * uri)
361 GESFormatter *p = NULL;
362 gboolean ret = FALSE;
364 /* FIXME : we should have a GError** argument so the user can know why
365 * it wasn't able to load the uri
368 if (!(p = ges_formatter_new_for_uri (uri))) {
369 GST_ERROR ("unsupported uri '%s'", uri);
373 if (!ges_formatter_load_from_uri (p, timeline, uri)) {
374 GST_ERROR ("error deserializing formatter");
387 * ges_timeline_save_to_uri:
388 * @timeline: a #GESTimeline
389 * @uri: The location to save to
391 * Saves the timeline to the given location
393 * Returns: TRUE if the timeline was successfully saved to the given location,
398 ges_timeline_save_to_uri (GESTimeline * timeline, const gchar * uri)
400 GESFormatter *p = NULL;
401 gboolean ret = FALSE;
403 /* FIXME : How will the user be able to chose the format he
404 * wishes to store to ? */
406 /* FIXME : How will we ensure a timeline loaded with a certain format
407 * will be saved with the same one by default ? We need to make this
408 * easy from an API perspective */
410 /* FIXME : we should have a GError** argument so the user can know why
411 * it wasn't able to save
414 if (!(p = ges_formatter_new_for_uri (uri))) {
415 GST_ERROR ("unsupported uri '%s'", uri);
419 if (!ges_formatter_save_to_uri (p, timeline, uri)) {
420 GST_ERROR ("error serializing formatter");
433 add_object_to_track (GESTimelineObject * object, GESTrack * track)
435 if (!ges_timeline_object_create_track_objects (object, track)) {
436 GST_WARNING ("error creating track objects");
441 add_object_to_tracks (GESTimeline * timeline, GESTimelineObject * object)
445 for (tmp = timeline->priv->tracks; tmp; tmp = g_list_next (tmp)) {
446 TrackPrivate *tr_priv = (TrackPrivate *) tmp->data;
447 GESTrack *track = tr_priv->track;
449 GST_LOG ("Trying with track %p", track);
450 add_object_to_track (object, track);
456 do_async_start (GESTimeline * timeline)
461 timeline->priv->async_pending = TRUE;
463 /* Freeze state of tracks */
464 for (tmp = timeline->priv->tracks; tmp; tmp = tmp->next) {
465 TrackPrivate *tr_priv = (TrackPrivate *) tmp->data;
466 gst_element_set_locked_state ((GstElement *) tr_priv->track, TRUE);
469 message = gst_message_new_async_start (GST_OBJECT_CAST (timeline), FALSE);
470 parent_class->handle_message (GST_BIN_CAST (timeline), message);
474 do_async_done (GESTimeline * timeline)
478 if (timeline->priv->async_pending) {
480 /* Unfreeze state of tracks */
481 for (tmp = timeline->priv->tracks; tmp; tmp = tmp->next) {
482 TrackPrivate *tr_priv = (TrackPrivate *) tmp->data;
483 gst_element_set_locked_state ((GstElement *) tr_priv->track, FALSE);
484 gst_element_sync_state_with_parent ((GstElement *) tr_priv->track);
487 GST_DEBUG_OBJECT (timeline, "Emitting async-done");
488 message = gst_message_new_async_done (GST_OBJECT_CAST (timeline));
489 parent_class->handle_message (GST_BIN_CAST (timeline), message);
491 timeline->priv->async_pending = FALSE;
496 discoverer_finished_cb (GstDiscoverer * discoverer, GESTimeline * timeline)
498 do_async_done (timeline);
502 discoverer_discovered_cb (GstDiscoverer * discoverer,
503 GstDiscovererInfo * info, GError * err, GESTimeline * timeline)
506 gboolean found = FALSE;
507 gboolean is_image = FALSE;
508 GESTimelineFileSource *tfs = NULL;
509 GESTimelinePrivate *priv = timeline->priv;
510 const gchar *uri = gst_discoverer_info_get_uri (info);
514 GST_WARNING ("Error while discovering %s: %s", uri, err->message);
517 GST_DEBUG ("Discovered uri %s", uri);
519 GES_TIMELINE_PENDINGOBJS_LOCK (timeline);
521 /* Find corresponding TimelineFileSource in the sources */
522 for (tmp = priv->pendingobjects; tmp; tmp = tmp->next) {
523 tfs = (GESTimelineFileSource *) tmp->data;
525 if (!g_strcmp0 (ges_timeline_filesource_get_uri (tfs), uri)) {
533 GESTrackType tfs_supportedformats;
535 /* The timeline file source will be updated with discovered information
536 * so it needs to not be finalized during this process */
539 /* Remove object from list */
540 priv->pendingobjects = g_list_delete_link (priv->pendingobjects, tmp);
541 GES_TIMELINE_PENDINGOBJS_UNLOCK (timeline);
543 /* FIXME : Handle errors in discovery */
544 stream_list = gst_discoverer_info_get_stream_list (info);
546 tfs_supportedformats = ges_timeline_filesource_get_supported_formats (tfs);
547 if (tfs_supportedformats != GES_TRACK_TYPE_UNKNOWN)
550 /* Update timelinefilesource properties based on info */
551 for (tmp = stream_list; tmp; tmp = tmp->next) {
552 GstDiscovererStreamInfo *sinf = (GstDiscovererStreamInfo *) tmp->data;
554 if (GST_IS_DISCOVERER_AUDIO_INFO (sinf)) {
555 tfs_supportedformats |= GES_TRACK_TYPE_AUDIO;
556 ges_timeline_filesource_set_supported_formats (tfs,
557 tfs_supportedformats);
558 } else if (GST_IS_DISCOVERER_VIDEO_INFO (sinf)) {
559 tfs_supportedformats |= GES_TRACK_TYPE_VIDEO;
560 ges_timeline_filesource_set_supported_formats (tfs,
561 tfs_supportedformats);
562 if (gst_discoverer_video_info_is_image ((GstDiscovererVideoInfo *)
564 tfs_supportedformats |= GES_TRACK_TYPE_AUDIO;
565 ges_timeline_filesource_set_supported_formats (tfs,
566 tfs_supportedformats);
573 gst_discoverer_stream_info_list_free (stream_list);
578 /* don't set max-duration on still images */
579 g_object_set (tfs, "is_image", (gboolean) TRUE, NULL);
582 /* Continue the processing on tfs */
583 add_object_to_tracks (timeline, GES_TIMELINE_OBJECT (tfs));
586 g_object_set (tfs, "max-duration",
587 gst_discoverer_info_get_duration (info), NULL);
590 /* Continue the processing on tfs */
591 add_object_to_tracks (timeline, GES_TIMELINE_OBJECT (tfs));
593 /* Remove the ref as the timeline file source is no longer needed here */
594 g_object_unref (tfs);
596 GES_TIMELINE_PENDINGOBJS_UNLOCK (timeline);
600 static GstStateChangeReturn
601 ges_timeline_change_state (GstElement * element, GstStateChange transition)
603 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
604 GESTimeline *timeline = GES_TIMELINE (element);
606 switch (transition) {
607 case GST_STATE_CHANGE_READY_TO_PAUSED:
608 GES_TIMELINE_PENDINGOBJS_LOCK (timeline);
609 if (timeline->priv->pendingobjects) {
610 GES_TIMELINE_PENDINGOBJS_UNLOCK (timeline);
611 do_async_start (timeline);
612 ret = GST_STATE_CHANGE_ASYNC;
614 GES_TIMELINE_PENDINGOBJS_UNLOCK (timeline);
622 GstStateChangeReturn bret;
624 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
625 if (G_UNLIKELY (bret == GST_STATE_CHANGE_NO_PREROLL)) {
626 do_async_done (timeline);
631 switch (transition) {
632 case GST_STATE_CHANGE_PAUSED_TO_READY:
633 do_async_done (timeline);
644 layer_object_added_cb (GESTimelineLayer * layer, GESTimelineObject * object,
645 GESTimeline * timeline)
647 if (ges_timeline_object_is_moving_from_layer (object)) {
648 GST_DEBUG ("TimelineObject %p is moving from a layer to another, not doing"
649 " anything on it", object);
653 GST_DEBUG ("New TimelineObject %p added to layer %p", object, layer);
655 if (GES_IS_TIMELINE_FILE_SOURCE (object)) {
656 GESTimelineFileSource *tfs = GES_TIMELINE_FILE_SOURCE (object);
657 GESTrackType tfs_supportedformats =
658 ges_timeline_filesource_get_supported_formats (tfs);
659 guint64 tfs_maxdur = ges_timeline_filesource_get_max_duration (tfs);
660 const gchar *tfs_uri;
662 /* Send the filesource to the discoverer if:
663 * * it doesn't have specified supported formats
664 * * OR it doesn't have a specified max-duration
665 * * OR it doesn't have a valid duration */
667 if (tfs_supportedformats == GES_TRACK_TYPE_UNKNOWN ||
668 tfs_maxdur == GST_CLOCK_TIME_NONE || object->duration == 0) {
669 GST_LOG ("Incomplete TimelineFileSource, discovering it");
670 tfs_uri = ges_timeline_filesource_get_uri (tfs);
672 GES_TIMELINE_PENDINGOBJS_LOCK (timeline);
673 timeline->priv->pendingobjects =
674 g_list_append (timeline->priv->pendingobjects, object);
675 GES_TIMELINE_PENDINGOBJS_UNLOCK (timeline);
677 gst_discoverer_discover_uri_async (timeline->priv->discoverer, tfs_uri);
679 add_object_to_tracks (timeline, object);
681 add_object_to_tracks (timeline, object);
688 layer_priority_changed_cb (GESTimelineLayer * layer,
689 GParamSpec * arg G_GNUC_UNUSED, GESTimeline * timeline)
691 timeline->priv->layers = g_list_sort (timeline->priv->layers, (GCompareFunc)
696 layer_object_removed_cb (GESTimelineLayer * layer, GESTimelineObject * object,
697 GESTimeline * timeline)
699 GList *tmp, *trackobjects;
701 if (ges_timeline_object_is_moving_from_layer (object)) {
702 GST_DEBUG ("TimelineObject %p is moving from a layer to another, not doing"
703 " anything on it", object);
707 GST_DEBUG ("TimelineObject %p removed from layer %p", object, layer);
709 /* Go over the object's track objects and figure out which one belongs to
710 * the list of tracks we control */
712 trackobjects = ges_timeline_object_get_track_objects (object);
713 for (tmp = trackobjects; tmp; tmp = tmp->next) {
714 GESTrackObject *trobj = (GESTrackObject *) tmp->data;
716 GST_DEBUG ("Trying to remove TrackObject %p", trobj);
717 if (G_LIKELY (g_list_find_custom (timeline->priv->tracks,
718 ges_track_object_get_track (trobj),
719 (GCompareFunc) custom_find_track))) {
720 GST_DEBUG ("Belongs to one of the tracks we control");
721 ges_track_remove_object (ges_track_object_get_track (trobj), trobj);
723 ges_timeline_object_release_track_object (object, trobj);
726 /* removing the reference added by _get_track_objects() */
727 g_object_unref (trobj);
729 g_list_free (trackobjects);
731 /* if the object is a timeline file source that has not yet been discovered,
732 * it no longer needs to be discovered so remove it from the pendingobjects
733 * list if it belongs to this layer */
734 if (GES_IS_TIMELINE_FILE_SOURCE (object)) {
735 GES_TIMELINE_PENDINGOBJS_LOCK (timeline);
736 timeline->priv->pendingobjects =
737 g_list_remove_all (timeline->priv->pendingobjects, object);
738 GES_TIMELINE_PENDINGOBJS_UNLOCK (timeline);
745 * ges_timeline_append_layer:
746 * @timeline: a #GESTimeline
747 * @layer: the #GESTimelineLayer to add
749 * Convenience method to append @layer to @timeline which means that the
750 * priority of @layer is changed to correspond to the last layer of @timeline.
751 * The reference to the @layer will be stolen by @timeline.
753 * Returns: TRUE if the layer was properly added, else FALSE.
756 ges_timeline_append_layer (GESTimeline * timeline, GESTimelineLayer * layer)
759 GESTimelinePrivate *priv = timeline->priv;
761 GST_DEBUG ("Appending layer to layer:%p, timeline:%p", timeline, layer);
762 priority = g_list_length (priv->layers);
764 ges_timeline_layer_set_priority (layer, priority);
766 return ges_timeline_add_layer (timeline, layer);
770 * ges_timeline_add_layer:
771 * @timeline: a #GESTimeline
772 * @layer: the #GESTimelineLayer to add
774 * Add the layer to the timeline. The reference to the @layer will be stolen
777 * Returns: TRUE if the layer was properly added, else FALSE.
780 ges_timeline_add_layer (GESTimeline * timeline, GESTimelineLayer * layer)
782 GList *objects, *tmp;
783 GESTimelinePrivate *priv = timeline->priv;
785 GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
787 /* We can only add a layer that doesn't already belong to another timeline */
788 if (G_UNLIKELY (layer->timeline)) {
789 GST_WARNING ("Layer belongs to another timeline, can't add it");
793 /* Add to the list of layers, make sure we don't already control it */
794 if (G_UNLIKELY (g_list_find (priv->layers, (gconstpointer) layer))) {
795 GST_WARNING ("Layer is already controlled by this timeline");
799 g_object_ref_sink (layer);
800 priv->layers = g_list_insert_sorted (priv->layers, layer,
801 (GCompareFunc) sort_layers);
803 /* Inform the layer that it belongs to a new timeline */
804 ges_timeline_layer_set_timeline (layer, timeline);
806 /* Connect to 'object-added'/'object-removed' signal from the new layer */
807 g_signal_connect (layer, "object-added", G_CALLBACK (layer_object_added_cb),
809 g_signal_connect (layer, "object-removed",
810 G_CALLBACK (layer_object_removed_cb), timeline);
811 g_signal_connect (layer, "notify::priority",
812 G_CALLBACK (layer_priority_changed_cb), timeline);
814 GST_DEBUG ("Done adding layer, emitting 'layer-added' signal");
815 g_signal_emit (timeline, ges_timeline_signals[LAYER_ADDED], 0, layer);
817 /* add any existing timeline objects to the timeline */
818 objects = ges_timeline_layer_get_objects (layer);
819 for (tmp = objects; tmp; tmp = tmp->next) {
820 layer_object_added_cb (layer, tmp->data, timeline);
821 g_object_unref (tmp->data);
824 g_list_free (objects);
830 * ges_timeline_remove_layer:
831 * @timeline: a #GESTimeline
832 * @layer: the #GESTimelineLayer to remove
834 * Removes the layer from the timeline. The reference that the @timeline holds on
835 * the layer will be dropped. If you wish to use the @layer after calling this
836 * method, you need to take a reference before calling.
838 * Returns: TRUE if the layer was properly removed, else FALSE.
842 ges_timeline_remove_layer (GESTimeline * timeline, GESTimelineLayer * layer)
844 GList *layer_objects, *tmp;
845 GESTimelinePrivate *priv = timeline->priv;
847 GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
849 if (G_UNLIKELY (!g_list_find (priv->layers, layer))) {
850 GST_WARNING ("Layer doesn't belong to this timeline");
854 /* remove objects from any private data structures */
856 layer_objects = ges_timeline_layer_get_objects (layer);
857 for (tmp = layer_objects; tmp; tmp = tmp->next) {
858 layer_object_removed_cb (layer, GES_TIMELINE_OBJECT (tmp->data), timeline);
859 g_object_unref (G_OBJECT (tmp->data));
862 g_list_free (layer_objects);
864 /* Disconnect signals */
865 GST_DEBUG ("Disconnecting signal callbacks");
866 g_signal_handlers_disconnect_by_func (layer, layer_object_added_cb, timeline);
867 g_signal_handlers_disconnect_by_func (layer, layer_object_removed_cb,
870 priv->layers = g_list_remove (priv->layers, layer);
872 ges_timeline_layer_set_timeline (layer, NULL);
874 g_signal_emit (timeline, ges_timeline_signals[LAYER_REMOVED], 0, layer);
876 g_object_unref (layer);
882 pad_added_cb (GESTrack * track, GstPad * pad, TrackPrivate * tr_priv)
887 GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));
889 if (G_UNLIKELY (tr_priv->pad)) {
890 GST_WARNING ("We are already controlling a pad for this track");
894 /* Remember the pad */
898 GST_DEBUG ("Ghosting pad and adding it to ourself");
899 padname = g_strdup_printf ("track_%p_src", track);
900 tr_priv->ghostpad = gst_ghost_pad_new (padname, pad);
902 gst_pad_set_active (tr_priv->ghostpad, TRUE);
903 gst_element_add_pad (GST_ELEMENT (tr_priv->timeline), tr_priv->ghostpad);
907 pad_removed_cb (GESTrack * track, GstPad * pad, TrackPrivate * tr_priv)
909 GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));
911 if (G_UNLIKELY (tr_priv->pad != pad)) {
912 GST_WARNING ("Not the pad we're controlling");
916 if (G_UNLIKELY (tr_priv->ghostpad == NULL)) {
917 GST_WARNING ("We don't have a ghostpad for this pad !");
921 GST_DEBUG ("Removing ghostpad");
922 gst_pad_set_active (tr_priv->ghostpad, FALSE);
923 gst_element_remove_pad (GST_ELEMENT (tr_priv->timeline), tr_priv->ghostpad);
924 tr_priv->ghostpad = NULL;
929 custom_find_track (TrackPrivate * tr_priv, GESTrack * track)
931 if (tr_priv->track == track)
937 * ges_timeline_add_track:
938 * @timeline: a #GESTimeline
939 * @track: the #GESTrack to add
941 * Add a track to the timeline. The reference to the track will be stolen by the
944 * Returns: TRUE if the track was properly added, else FALSE.
947 /* FIXME: create track objects for timeline objects which have already been
948 * added to existing layers.
952 ges_timeline_add_track (GESTimeline * timeline, GESTrack * track)
954 TrackPrivate *tr_priv;
955 GESTimelinePrivate *priv = timeline->priv;
958 GST_DEBUG ("timeline:%p, track:%p", timeline, track);
960 /* make sure we don't already control it */
961 if (G_UNLIKELY (g_list_find_custom (priv->tracks, (gconstpointer) track,
962 (GCompareFunc) custom_find_track))) {
963 GST_WARNING ("Track is already controlled by this timeline");
967 /* Add the track to ourself (as a GstBin)
968 * Reference is stolen ! */
969 if (G_UNLIKELY (!gst_bin_add (GST_BIN (timeline), GST_ELEMENT (track)))) {
970 GST_WARNING ("Couldn't add track to ourself (GST)");
974 tr_priv = g_new0 (TrackPrivate, 1);
975 tr_priv->timeline = timeline;
976 tr_priv->track = track;
978 /* Add the track to the list of tracks we track */
979 priv->tracks = g_list_append (priv->tracks, tr_priv);
981 /* Listen to pad-added/-removed */
982 g_signal_connect (track, "pad-added", (GCallback) pad_added_cb, tr_priv);
983 g_signal_connect (track, "pad-removed", (GCallback) pad_removed_cb, tr_priv);
985 /* Inform the track that it's currently being used by ourself */
986 ges_track_set_timeline (track, timeline);
988 GST_DEBUG ("Done adding track, emitting 'track-added' signal");
990 /* emit 'track-added' */
991 g_signal_emit (timeline, ges_timeline_signals[TRACK_ADDED], 0, track);
993 /* ensure that each existing timeline object has the opportunity to create a
994 * track object for this track*/
996 for (tmp = priv->layers; tmp; tmp = tmp->next) {
997 GList *objects, *obj;
998 objects = ges_timeline_layer_get_objects (tmp->data);
1000 for (obj = objects; obj; obj = obj->next) {
1001 add_object_to_track (obj->data, track);
1002 g_object_unref (obj->data);
1005 g_list_free (objects);
1008 /* We connect to the duration change notify, so we can update
1009 * our duration accordingly */
1010 g_signal_connect (G_OBJECT (track), "notify::duration",
1011 G_CALLBACK (track_duration_cb), timeline);
1012 track_duration_cb (GST_ELEMENT (track), NULL, timeline);
1018 * ges_timeline_remove_track:
1019 * @timeline: a #GESTimeline
1020 * @track: the #GESTrack to remove
1022 * Remove the @track from the @timeline. The reference stolen when adding the
1023 * @track will be removed. If you wish to use the @track after calling this
1024 * function you must ensure that you have a reference to it.
1026 * Returns: TRUE if the @track was properly removed, else FALSE.
1029 /* FIXME: release any track objects associated with this layer. currenly this
1030 * will not happen if you remove the track before removing *all*
1031 * timelineobjects which have a track object in this track.
1035 ges_timeline_remove_track (GESTimeline * timeline, GESTrack * track)
1038 TrackPrivate *tr_priv;
1039 GESTimelinePrivate *priv = timeline->priv;
1041 GST_DEBUG ("timeline:%p, track:%p", timeline, track);
1043 if (G_UNLIKELY (!(tmp =
1044 g_list_find_custom (priv->tracks, (gconstpointer) track,
1045 (GCompareFunc) custom_find_track)))) {
1046 GST_WARNING ("Track doesn't belong to this timeline");
1050 tr_priv = tmp->data;
1051 priv->tracks = g_list_remove (priv->tracks, tr_priv);
1053 ges_track_set_timeline (track, NULL);
1055 /* Remove ghost pad */
1056 if (tr_priv->ghostpad) {
1057 GST_DEBUG ("Removing ghostpad");
1058 gst_pad_set_active (tr_priv->ghostpad, FALSE);
1059 gst_ghost_pad_set_target ((GstGhostPad *) tr_priv->ghostpad, NULL);
1060 gst_element_remove_pad (GST_ELEMENT (timeline), tr_priv->ghostpad);
1063 /* Remove pad-added/-removed handlers */
1064 g_signal_handlers_disconnect_by_func (track, pad_added_cb, tr_priv);
1065 g_signal_handlers_disconnect_by_func (track, pad_removed_cb, tr_priv);
1066 g_signal_handlers_disconnect_by_func (track, track_duration_cb,
1069 /* Signal track removal to all layers/objects */
1070 g_signal_emit (timeline, ges_timeline_signals[TRACK_REMOVED], 0, track);
1072 /* remove track from our bin */
1073 gst_object_ref (track);
1074 if (G_UNLIKELY (!gst_bin_remove (GST_BIN (timeline), GST_ELEMENT (track)))) {
1075 GST_WARNING ("Couldn't remove track to ourself (GST)");
1076 gst_object_unref (track);
1080 /* set track state to NULL */
1082 gst_element_set_state (GST_ELEMENT (track), GST_STATE_NULL);
1084 gst_object_unref (track);
1092 * ges_timeline_get_track_for_pad:
1093 * @timeline: The #GESTimeline
1096 * Search the #GESTrack corresponding to the given @timeline's @pad.
1098 * Returns: (transfer none): The corresponding #GESTrack if it is found,
1099 * or %NULL if there is an error.
1103 ges_timeline_get_track_for_pad (GESTimeline * timeline, GstPad * pad)
1107 for (tmp = timeline->priv->tracks; tmp; tmp = g_list_next (tmp)) {
1108 TrackPrivate *tr_priv = (TrackPrivate *) tmp->data;
1109 if (pad == tr_priv->ghostpad)
1110 return tr_priv->track;
1117 * ges_timeline_get_tracks:
1118 * @timeline: a #GESTimeline
1120 * Returns the list of #GESTrack used by the Timeline.
1122 * Returns: (transfer full) (element-type GESTrack): A list of #GESTrack.
1123 * The caller should unref each track once he is done with them.
1126 ges_timeline_get_tracks (GESTimeline * timeline)
1128 GList *tmp, *res = NULL;
1130 for (tmp = timeline->priv->tracks; tmp; tmp = g_list_next (tmp)) {
1131 TrackPrivate *tr_priv = (TrackPrivate *) tmp->data;
1132 res = g_list_append (res, g_object_ref (tr_priv->track));
1139 * ges_timeline_get_layers:
1140 * @timeline: a #GESTimeline
1142 * Get the list of #GESTimelineLayer present in the Timeline.
1144 * Returns: (transfer full) (element-type GESTimelineLayer): the list of
1145 * #GESTimelineLayer present in the Timeline sorted by priority.
1146 * The caller should unref each Layer once he is done with them.
1149 ges_timeline_get_layers (GESTimeline * timeline)
1151 GList *tmp, *res = NULL;
1153 for (tmp = timeline->priv->layers; tmp; tmp = g_list_next (tmp)) {
1154 res = g_list_insert_sorted (res, g_object_ref (tmp->data),
1155 (GCompareFunc) sort_layers);
1162 * ges_timeline_enable_update:
1163 * @timeline: a #GESTimeline
1164 * @enabled: TRUE if the timeline must be updated, FALSE otherwise.
1166 * Calls the enable_update function of the tracks contained by the timeline.
1168 * Returns: True if success, FALSE otherwise.
1171 ges_timeline_enable_update (GESTimeline * timeline, gboolean enabled)
1173 GList *tmp, *tracks;
1174 gboolean res = TRUE;
1176 GST_DEBUG_OBJECT (timeline, "%s updates", enabled ? "Enabling" : "Disabling");
1178 tracks = ges_timeline_get_tracks (timeline);
1180 for (tmp = tracks; tmp; tmp = tmp->next) {
1181 if (!ges_track_enable_update (tmp->data, enabled)) {
1186 g_list_free (tracks);
1192 track_duration_cb (GstElement * track,
1193 GParamSpec * arg G_GNUC_UNUSED, GESTimeline * timeline)
1195 guint64 duration, max_duration = 0;
1198 for (tmp = timeline->priv->tracks; tmp; tmp = g_list_next (tmp)) {
1199 TrackPrivate *tr_priv = (TrackPrivate *) tmp->data;
1200 g_object_get (tr_priv->track, "duration", &duration, NULL);
1201 GST_DEBUG ("track duration : %" GST_TIME_FORMAT, GST_TIME_ARGS (duration));
1202 max_duration = MAX (duration, max_duration);
1205 if (timeline->priv->duration != max_duration) {
1206 GST_DEBUG ("track duration : %" GST_TIME_FORMAT " current : %"
1207 GST_TIME_FORMAT, GST_TIME_ARGS (max_duration),
1208 GST_TIME_ARGS (timeline->priv->duration));
1210 timeline->priv->duration = max_duration;
1212 #if GLIB_CHECK_VERSION(2,26,0)
1213 g_object_notify_by_pspec (G_OBJECT (timeline), properties[PROP_DURATION]);
1215 g_object_notify (G_OBJECT (timeline), "duration");