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
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"
42 G_DEFINE_TYPE (GESTimeline, ges_timeline, GST_TYPE_BIN);
44 /* private structure to contain our track-related information */
48 GESTimeline *timeline;
50 GstPad *pad; /* Pad from the track */
63 static GstBinClass *parent_class;
65 static guint ges_timeline_signals[LAST_SIGNAL] = { 0 };
67 gint custom_find_track (TrackPrivate * priv, GESTrack * track);
68 static GstStateChangeReturn
69 ges_timeline_change_state (GstElement * element, GstStateChange transition);
71 discoverer_finished_cb (GstDiscoverer * discoverer, GESTimeline * timeline);
73 discoverer_discovered_cb (GstDiscoverer * discoverer,
74 GstDiscovererInfo * info, GError * err, GESTimeline * timeline);
77 ges_timeline_get_property (GObject * object, guint property_id,
78 GValue * value, GParamSpec * pspec)
80 switch (property_id) {
82 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
87 ges_timeline_set_property (GObject * object, guint property_id,
88 const GValue * value, GParamSpec * pspec)
90 switch (property_id) {
92 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
97 ges_timeline_dispose (GObject * object)
99 GESTimeline *timeline = GES_TIMELINE (object);
101 if (timeline->discoverer) {
102 gst_discoverer_stop (timeline->discoverer);
103 g_object_unref (timeline->discoverer);
104 timeline->discoverer = NULL;
107 while (timeline->layers) {
108 GESTimelineLayer *layer = (GESTimelineLayer *) timeline->layers->data;
109 ges_timeline_remove_layer (timeline, layer);
112 /* FIXME: it should be possible to remove tracks before removing
113 * layers, but at the moment this creates a problem because the track
114 * objects aren't notified that their gnlobjects have been destroyed.
117 while (timeline->tracks) {
118 TrackPrivate *priv = (TrackPrivate *) timeline->tracks->data;
119 ges_timeline_remove_track (timeline, priv->track);
122 G_OBJECT_CLASS (ges_timeline_parent_class)->dispose (object);
126 ges_timeline_finalize (GObject * object)
128 G_OBJECT_CLASS (ges_timeline_parent_class)->finalize (object);
132 ges_timeline_class_init (GESTimelineClass * klass)
134 GObjectClass *object_class = G_OBJECT_CLASS (klass);
135 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
137 parent_class = g_type_class_peek_parent (klass);
139 element_class->change_state = ges_timeline_change_state;
141 object_class->get_property = ges_timeline_get_property;
142 object_class->set_property = ges_timeline_set_property;
143 object_class->dispose = ges_timeline_dispose;
144 object_class->finalize = ges_timeline_finalize;
147 * GESTimeline::track-added
148 * @timeline: the #GESTimeline
149 * @track: the #GESTrack that was added to the timeline
151 * Will be emitted after the track was added to the timeline.
153 ges_timeline_signals[TRACK_ADDED] =
154 g_signal_new ("track-added", G_TYPE_FROM_CLASS (klass),
155 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, track_added), NULL,
156 NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TRACK);
159 * GESTimeline::track-removed
160 * @timeline: the #GESTimeline
161 * @track: the #GESTrack that was removed from the timeline
163 * Will be emitted after the track was removed from the timeline.
165 ges_timeline_signals[TRACK_REMOVED] =
166 g_signal_new ("track-removed", G_TYPE_FROM_CLASS (klass),
167 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, track_removed),
168 NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TRACK);
171 * GESTimeline::layer-added
172 * @timeline: the #GESTimeline
173 * @layer: the #GESTimelineLayer that was added to the timeline
175 * Will be emitted after the layer was added to the timeline.
177 ges_timeline_signals[LAYER_ADDED] =
178 g_signal_new ("layer-added", G_TYPE_FROM_CLASS (klass),
179 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, layer_added), NULL,
180 NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TIMELINE_LAYER);
183 * GESTimeline::layer-removed
184 * @timeline: the #GESTimeline
185 * @layer: the #GESTimelineLayer that was removed from the timeline
187 * Will be emitted after the layer was removed from the timeline.
189 ges_timeline_signals[LAYER_REMOVED] =
190 g_signal_new ("layer-removed", G_TYPE_FROM_CLASS (klass),
191 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, layer_removed),
192 NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
193 GES_TYPE_TIMELINE_LAYER);
197 ges_timeline_init (GESTimeline * self)
202 /* New discoverer with a 15s timeout */
203 self->discoverer = gst_discoverer_new (15 * GST_SECOND, NULL);
204 g_signal_connect (self->discoverer, "finished",
205 G_CALLBACK (discoverer_finished_cb), self);
206 g_signal_connect (self->discoverer, "discovered",
207 G_CALLBACK (discoverer_discovered_cb), self);
208 gst_discoverer_start (self->discoverer);
214 * Creates a new empty #GESTimeline.
216 * Returns: The new timeline.
220 ges_timeline_new (void)
222 return g_object_new (GES_TYPE_TIMELINE, NULL);
226 * ges_timeline_load_from_uri:
227 * @uri: The URI to load from
229 * Creates a timeline from the contents of given uri.
233 * Returns: A new #GESTimeline if loading was successful, else NULL.
237 ges_timeline_load_from_uri (gchar * uri)
239 /* FIXME : IMPLEMENT */
245 * @timeline: a #GESTimeline
246 * @uri: The location to save to
248 * Saves the timeline to the given location
252 * Returns: TRUE if the timeline was successfully saved to the given location,
257 ges_timeline_save (GESTimeline * timeline, gchar * uri)
259 /* FIXME : IMPLEMENT */
264 add_object_to_tracks (GESTimeline * timeline, GESTimelineObject * object)
268 for (tmp = timeline->tracks; tmp; tmp = g_list_next (tmp)) {
269 TrackPrivate *priv = (TrackPrivate *) tmp->data;
270 GESTrack *track = priv->track;
272 GST_LOG ("Trying with track %p", track);
273 if (!ges_timeline_object_create_track_objects (object, track)) {
274 GST_WARNING ("error creating track objects");
280 do_async_start (GESTimeline * timeline)
285 timeline->async_pending = TRUE;
287 /* Freeze state of tracks */
288 for (tmp = timeline->tracks; tmp; tmp = tmp->next) {
289 TrackPrivate *priv = (TrackPrivate *) tmp->data;
290 gst_element_set_locked_state ((GstElement *) priv->track, TRUE);
293 message = gst_message_new_async_start (GST_OBJECT_CAST (timeline), FALSE);
294 parent_class->handle_message (GST_BIN_CAST (timeline), message);
298 do_async_done (GESTimeline * timeline)
302 if (timeline->async_pending) {
304 /* Unfreeze state of tracks */
305 for (tmp = timeline->tracks; tmp; tmp = tmp->next) {
306 TrackPrivate *priv = (TrackPrivate *) tmp->data;
307 gst_element_set_locked_state ((GstElement *) priv->track, FALSE);
308 gst_element_sync_state_with_parent ((GstElement *) priv->track);
311 GST_DEBUG_OBJECT (timeline, "Emitting async-done");
312 message = gst_message_new_async_done (GST_OBJECT_CAST (timeline));
313 parent_class->handle_message (GST_BIN_CAST (timeline), message);
315 timeline->async_pending = FALSE;
320 discoverer_finished_cb (GstDiscoverer * discoverer, GESTimeline * timeline)
322 do_async_done (timeline);
326 discoverer_discovered_cb (GstDiscoverer * discoverer,
327 GstDiscovererInfo * info, GError * err, GESTimeline * timeline)
330 gboolean found = FALSE;
331 gboolean is_image = FALSE;
332 GESTimelineFileSource *tfs = NULL;
333 const gchar *uri = gst_discoverer_info_get_uri (info);
335 GST_DEBUG ("Discovered uri %s", uri);
337 /* Find corresponding TimelineFileSource in the sources */
338 for (tmp = timeline->pendingobjects; tmp; tmp = tmp->next) {
339 tfs = (GESTimelineFileSource *) tmp->data;
341 if (!g_strcmp0 (tfs->uri, uri)) {
350 /* Remove object from list */
351 timeline->pendingobjects =
352 g_list_delete_link (timeline->pendingobjects, tmp);
354 /* FIXME : Handle errors in discovery */
355 stream_list = gst_discoverer_info_get_stream_list (info);
357 /* Update timelinefilesource properties based on info */
358 for (tmp = stream_list; tmp; tmp = tmp->next) {
359 GstDiscovererStreamInfo *sinf = (GstDiscovererStreamInfo *) tmp->data;
361 if (GST_IS_DISCOVERER_AUDIO_INFO (sinf))
362 tfs->supportedformats |= GES_TRACK_TYPE_AUDIO;
363 else if (GST_IS_DISCOVERER_VIDEO_INFO (sinf)) {
364 tfs->supportedformats |= GES_TRACK_TYPE_VIDEO;
365 if (gst_discoverer_video_info_is_image ((GstDiscovererVideoInfo *)
367 tfs->supportedformats |= GES_TRACK_TYPE_AUDIO;
374 gst_discoverer_stream_info_list_free (stream_list);
377 /* don't set max-duration on still images */
378 g_object_set (tfs, "is_image", (gboolean) TRUE, NULL);
382 g_object_set (tfs, "max-duration",
383 gst_discoverer_info_get_duration (info), NULL);
386 /* Continue the processing on tfs */
387 add_object_to_tracks (timeline, GES_TIMELINE_OBJECT (tfs));
391 static GstStateChangeReturn
392 ges_timeline_change_state (GstElement * element, GstStateChange transition)
394 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
395 GESTimeline *timeline = GES_TIMELINE (element);
397 switch (transition) {
398 case GST_STATE_CHANGE_READY_TO_PAUSED:
399 if (timeline->pendingobjects) {
400 do_async_start (timeline);
401 ret = GST_STATE_CHANGE_ASYNC;
409 GstStateChangeReturn bret;
411 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
412 if (G_UNLIKELY (bret == GST_STATE_CHANGE_NO_PREROLL)) {
413 do_async_done (timeline);
418 switch (transition) {
419 case GST_STATE_CHANGE_PAUSED_TO_READY:
420 do_async_done (timeline);
431 layer_object_added_cb (GESTimelineLayer * layer, GESTimelineObject * object,
432 GESTimeline * timeline)
434 GST_DEBUG ("New TimelineObject %p added to layer %p", object, layer);
436 if (GES_IS_TIMELINE_FILE_SOURCE (object)) {
437 GESTimelineFileSource *tfs = GES_TIMELINE_FILE_SOURCE (object);
439 /* Send the filesource to the discoverer if:
440 * * it doesn't have specified supported formats
441 * * OR it doesn't have a specified max-duration
442 * * OR it doesn't have a valid duration */
444 if (tfs->supportedformats == GES_TRACK_TYPE_UNKNOWN ||
445 tfs->maxduration == GST_CLOCK_TIME_NONE || object->duration == 0) {
446 GST_LOG ("Incomplete TimelineFileSource, discovering it");
447 timeline->pendingobjects =
448 g_list_append (timeline->pendingobjects, object);
449 gst_discoverer_discover_uri_async (timeline->discoverer,
450 GES_TIMELINE_FILE_SOURCE (object)->uri);
452 add_object_to_tracks (timeline, object);
454 add_object_to_tracks (timeline, object);
462 layer_object_removed_cb (GESTimelineLayer * layer, GESTimelineObject * object,
463 GESTimeline * timeline)
467 GST_DEBUG ("TimelineObject %p removed from layer %p", object, layer);
469 /* Go over the object's track objects and figure out which one belongs to
470 * the list of tracks we control */
472 for (tmp = object->trackobjects; tmp; tmp = next) {
473 GESTrackObject *trobj = (GESTrackObject *) tmp->data;
475 next = g_list_next (tmp);
477 GST_DEBUG ("Trying to remove TrackObject %p", trobj);
478 if (G_LIKELY (g_list_find_custom (timeline->tracks,
479 (gconstpointer) trobj->track,
480 (GCompareFunc) custom_find_track))) {
481 GST_DEBUG ("Belongs to one of the tracks we control");
482 ges_track_remove_object (trobj->track, trobj);
484 ges_timeline_object_release_track_object (object, trobj);
492 * ges_timeline_add_layer:
493 * @timeline: a #GESTimeline
494 * @layer: the #GESTimelineLayer to add
496 * Add the layer to the timeline. The reference to the @layer will be stolen
499 * Returns: TRUE if the layer was properly added, else FALSE.
502 ges_timeline_add_layer (GESTimeline * timeline, GESTimelineLayer * layer)
504 GList *objects, *tmp;
506 GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
508 /* We can only add a layer that doesn't already belong to another timeline */
509 if (G_UNLIKELY (layer->timeline)) {
510 GST_WARNING ("Layer belongs to another timeline, can't add it");
514 /* Add to the list of layers, make sure we don't already control it */
515 if (G_UNLIKELY (g_list_find (timeline->layers, (gconstpointer) layer))) {
516 GST_WARNING ("Layer is already controlled by this timeline");
520 /* Reference is stolen */
521 timeline->layers = g_list_append (timeline->layers, layer);
523 /* Inform the layer that it belongs to a new timeline */
524 ges_timeline_layer_set_timeline (layer, timeline);
526 /* Connect to 'object-added'/'object-removed' signal from the new layer */
527 g_signal_connect (layer, "object-added", G_CALLBACK (layer_object_added_cb),
529 g_signal_connect (layer, "object-removed",
530 G_CALLBACK (layer_object_removed_cb), timeline);
532 GST_DEBUG ("Done adding layer, emitting 'layer-added' signal");
533 g_signal_emit (timeline, ges_timeline_signals[LAYER_ADDED], 0, layer);
535 /* add any existing timeline objects to the timeline */
536 objects = ges_timeline_layer_get_objects (layer);
537 for (tmp = objects; tmp; tmp = tmp->next) {
538 layer_object_added_cb (layer, tmp->data, timeline);
539 g_object_unref (tmp->data);
542 g_list_free (objects);
548 * ges_timeline_remove_layer:
549 * @timeline: a #GESTimeline
550 * @layer: the #GESTimelineLayer to remove
552 * Removes the layer from the timeline. The reference that the @timeline holds on
553 * the layer will be dropped. If you wish to use the @layer after calling this
554 * method, you need to take a reference before calling.
556 * Returns: TRUE if the layer was properly removed, else FALSE.
560 ges_timeline_remove_layer (GESTimeline * timeline, GESTimelineLayer * layer)
562 GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
564 if (G_UNLIKELY (!g_list_find (timeline->layers, layer))) {
565 GST_WARNING ("Layer doesn't belong to this timeline");
569 /* Disconnect signals */
570 GST_DEBUG ("Disconnecting signal callbacks");
571 g_signal_handlers_disconnect_by_func (layer, layer_object_added_cb, timeline);
572 g_signal_handlers_disconnect_by_func (layer, layer_object_removed_cb,
575 timeline->layers = g_list_remove (timeline->layers, layer);
577 ges_timeline_layer_set_timeline (layer, NULL);
579 g_signal_emit (timeline, ges_timeline_signals[LAYER_REMOVED], 0, layer);
581 g_object_unref (layer);
587 pad_added_cb (GESTrack * track, GstPad * pad, TrackPrivate * priv)
591 GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));
593 if (G_UNLIKELY (priv->pad)) {
594 GST_WARNING ("We are already controlling a pad for this track");
598 /* Remember the pad */
602 GST_DEBUG ("Ghosting pad and adding it to ourself");
603 padname = g_strdup_printf ("track_%p_src", track);
604 priv->ghostpad = gst_ghost_pad_new (padname, pad);
606 gst_pad_set_active (priv->ghostpad, TRUE);
607 gst_element_add_pad (GST_ELEMENT (priv->timeline), priv->ghostpad);
611 pad_removed_cb (GESTrack * track, GstPad * pad, TrackPrivate * priv)
613 GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));
615 if (G_UNLIKELY (priv->pad != pad)) {
616 GST_WARNING ("Not the pad we're controlling");
620 if (G_UNLIKELY (priv->ghostpad == NULL)) {
621 GST_WARNING ("We don't have a ghostpad for this pad !");
625 GST_DEBUG ("Removing ghostpad");
626 gst_pad_set_active (priv->ghostpad, FALSE);
627 gst_element_remove_pad (GST_ELEMENT (priv->timeline), priv->ghostpad);
628 priv->ghostpad = NULL;
633 custom_find_track (TrackPrivate * priv, GESTrack * track)
635 if (priv->track == track)
641 * ges_timeline_add_track:
642 * @timeline: a #GESTimeline
643 * @track: the #GESTrack to add
645 * Add a track to the timeline. The reference to the track will be stolen by the
648 * Returns: TRUE if the track was properly added, else FALSE.
652 ges_timeline_add_track (GESTimeline * timeline, GESTrack * track)
656 GST_DEBUG ("timeline:%p, track:%p", timeline, track);
658 /* make sure we don't already control it */
659 if (G_UNLIKELY (g_list_find_custom (timeline->tracks, (gconstpointer) track,
660 (GCompareFunc) custom_find_track))) {
661 GST_WARNING ("Track is already controlled by this timeline");
665 /* Add the track to ourself (as a GstBin)
666 * Reference is stolen ! */
667 if (G_UNLIKELY (!gst_bin_add (GST_BIN (timeline), GST_ELEMENT (track)))) {
668 GST_WARNING ("Couldn't add track to ourself (GST)");
672 priv = g_new0 (TrackPrivate, 1);
673 priv->timeline = timeline;
676 /* Add the track to the list of tracks we track */
677 timeline->tracks = g_list_append (timeline->tracks, priv);
679 /* Listen to pad-added/-removed */
680 g_signal_connect (track, "pad-added", (GCallback) pad_added_cb, priv);
681 g_signal_connect (track, "pad-removed", (GCallback) pad_removed_cb, priv);
683 /* Inform the track that it's currently being used by ourself */
684 ges_track_set_timeline (track, timeline);
686 GST_DEBUG ("Done adding track, emitting 'track-added' signal");
688 /* emit 'track-added' */
689 g_signal_emit (timeline, ges_timeline_signals[TRACK_ADDED], 0, track);
695 * ges_timeline_remove_track:
696 * @timeline: a #GESTimeline
697 * @track: the #GESTrack to remove
699 * Remove the @track from the @timeline. The reference stolen when adding the
700 * @track will be removed. If you wish to use the @track after calling this
701 * function you must ensure that you have a reference to it.
703 * Returns: TRUE if the @track was properly removed, else FALSE.
706 ges_timeline_remove_track (GESTimeline * timeline, GESTrack * track)
711 GST_DEBUG ("timeline:%p, track:%p", timeline, track);
713 if (G_UNLIKELY (!(tmp =
714 g_list_find_custom (timeline->tracks, (gconstpointer) track,
715 (GCompareFunc) custom_find_track)))) {
716 GST_WARNING ("Track doesn't belong to this timeline");
721 timeline->tracks = g_list_remove (timeline->tracks, priv);
723 ges_track_set_timeline (track, NULL);
725 /* Remove ghost pad */
726 if (priv->ghostpad) {
727 GST_DEBUG ("Removing ghostpad");
728 gst_pad_set_active (priv->ghostpad, FALSE);
729 gst_ghost_pad_set_target ((GstGhostPad *) priv->ghostpad, NULL);
730 gst_element_remove_pad (GST_ELEMENT (timeline), priv->ghostpad);
733 /* Remove pad-added/-removed handlers */
734 g_signal_handlers_disconnect_by_func (track, pad_added_cb, priv);
735 g_signal_handlers_disconnect_by_func (track, pad_removed_cb, priv);
737 /* Signal track removal to all layers/objects */
738 g_signal_emit (timeline, ges_timeline_signals[TRACK_REMOVED], 0, track);
740 /* remove track from our bin */
741 if (G_UNLIKELY (!gst_bin_remove (GST_BIN (timeline), GST_ELEMENT (track)))) {
742 GST_WARNING ("Couldn't remove track to ourself (GST)");
752 * ges_timeline_get_track_for_pad:
753 * @timeline: The #GESTimeline
756 * Search the #GESTrack corresponding to the given @timeline's @pad.
758 * Returns: The corresponding #GESTrack if it is found, or #NULL if there is
763 ges_timeline_get_track_for_pad (GESTimeline * timeline, GstPad * pad)
767 for (tmp = timeline->tracks; tmp; tmp = g_list_next (tmp)) {
768 TrackPrivate *priv = (TrackPrivate *) tmp->data;
769 if (pad == priv->ghostpad)
777 * ges_timeline_get_tracks:
778 * @timeline: a #GESTimeline
780 * Returns the list of #GESTrack used by the Timeline.
782 * Returns: A list of #GESTrack. The caller should unref each track
783 * once he is done with them. */
785 ges_timeline_get_tracks (GESTimeline * timeline)
787 GList *tmp, *res = NULL;
789 for (tmp = timeline->tracks; tmp; tmp = g_list_next (tmp)) {
790 TrackPrivate *priv = (TrackPrivate *) tmp->data;
791 res = g_list_append (res, g_object_ref (priv->track));