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_ready_cb (GstDiscoverer * discoverer, GESTimeline * timeline);
73 discoverer_discovered_cb (GstDiscoverer * discoverer,
74 GstDiscovererInformation * 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->tracks) {
108 TrackPrivate *priv = (TrackPrivate *) timeline->tracks->data;
109 ges_timeline_remove_track (timeline, priv->track);
112 while (timeline->layers) {
113 GESTimelineLayer *layer = (GESTimelineLayer *) timeline->layers->data;
114 ges_timeline_remove_layer (timeline, layer);
117 G_OBJECT_CLASS (ges_timeline_parent_class)->dispose (object);
121 ges_timeline_finalize (GObject * object)
123 G_OBJECT_CLASS (ges_timeline_parent_class)->finalize (object);
127 ges_timeline_class_init (GESTimelineClass * klass)
129 GObjectClass *object_class = G_OBJECT_CLASS (klass);
130 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
132 parent_class = g_type_class_peek_parent (klass);
134 element_class->change_state = ges_timeline_change_state;
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;
142 * GESTimeline::track-added
143 * @timeline: the #GESTimeline
144 * @track: the #GESTrack that was added to the timeline
146 * Will be emitted after the track was added to the timeline.
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);
154 * GESTimeline::track-removed
155 * @timeline: the #GESTimeline
156 * @track: the #GESTrack that was removed from the timeline
158 * Will be emitted after the track was removed from the timeline.
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);
166 * GESTimeline::layer-added
167 * @timeline: the #GESTimeline
168 * @layer: the #GESTimelineLayer that was added to the timeline
170 * Will be emitted after the layer was added to the timeline.
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);
178 * GESTimeline::layer-removed
179 * @timeline: the #GESTimeline
180 * @layer: the #GESTimelineLayer that was removed from the timeline
182 * Will be emitted after the layer was removed from the timeline.
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);
192 ges_timeline_init (GESTimeline * self)
197 /* New discoverer with a 15s timeout */
198 self->discoverer = gst_discoverer_new (15 * GST_SECOND);
199 g_signal_connect (self->discoverer, "ready", G_CALLBACK (discoverer_ready_cb),
201 g_signal_connect (self->discoverer, "discovered",
202 G_CALLBACK (discoverer_discovered_cb), self);
203 gst_discoverer_start (self->discoverer);
209 * Creates a new empty #GESTimeline.
211 * Returns: The new timeline.
215 ges_timeline_new (void)
217 return g_object_new (GES_TYPE_TIMELINE, NULL);
221 * ges_timeline_load_from_uri:
222 * @uri: The URI to load from
224 * Creates a timeline from the contents of given uri.
228 * Returns: A new #GESTimeline if loading was successful, else NULL.
232 ges_timeline_load_from_uri (gchar * uri)
234 /* FIXME : IMPLEMENT */
240 * @timeline: a #GESTimeline
241 * @uri: The location to save to
243 * Saves the timeline to the given location
247 * Returns: TRUE if the timeline was successfully saved to the given location,
252 ges_timeline_save (GESTimeline * timeline, gchar * uri)
254 /* FIXME : IMPLEMENT */
259 add_object_to_tracks (GESTimeline * timeline, GESTimelineObject * object)
263 for (tmp = timeline->tracks; tmp; tmp = g_list_next (tmp)) {
264 TrackPrivate *priv = (TrackPrivate *) tmp->data;
265 GESTrack *track = priv->track;
266 GESTrackObject *trobj;
268 GST_LOG ("Trying with track %p", track);
270 if (G_UNLIKELY (!(trobj =
271 ges_timeline_object_create_track_object (object, track)))) {
272 GST_WARNING ("Couldn't create TrackObject for TimelineObject");
276 GST_LOG ("Got new TrackObject %p, adding it to track", trobj);
277 ges_track_add_object (track, trobj);
282 do_async_start (GESTimeline * timeline)
287 timeline->async_pending = TRUE;
289 /* Freeze state of tracks */
290 for (tmp = timeline->tracks; tmp; tmp = tmp->next) {
291 TrackPrivate *priv = (TrackPrivate *) tmp->data;
292 gst_element_set_locked_state ((GstElement *) priv->track, TRUE);
295 message = gst_message_new_async_start (GST_OBJECT_CAST (timeline), FALSE);
296 parent_class->handle_message (GST_BIN_CAST (timeline), message);
300 do_async_done (GESTimeline * timeline)
304 if (timeline->async_pending) {
306 /* Unfreeze state of tracks */
307 for (tmp = timeline->tracks; tmp; tmp = tmp->next) {
308 TrackPrivate *priv = (TrackPrivate *) tmp->data;
309 gst_element_set_locked_state ((GstElement *) priv->track, FALSE);
310 gst_element_sync_state_with_parent ((GstElement *) priv->track);
313 GST_DEBUG_OBJECT (timeline, "Emitting async-done");
314 message = gst_message_new_async_done (GST_OBJECT_CAST (timeline));
315 parent_class->handle_message (GST_BIN_CAST (timeline), message);
317 timeline->async_pending = FALSE;
322 discoverer_ready_cb (GstDiscoverer * discoverer, GESTimeline * timeline)
324 do_async_done (timeline);
328 discoverer_discovered_cb (GstDiscoverer * discoverer,
329 GstDiscovererInformation * info, GError * err, GESTimeline * timeline)
332 gboolean found = FALSE;
333 gboolean is_image = FALSE;
334 GESTimelineFileSource *tfs = NULL;
336 GST_DEBUG ("Discovered uri %s", info->uri);
338 /* Find corresponding TimelineFileSource in the sources */
339 for (tmp = timeline->pendingobjects; tmp; tmp = tmp->next) {
340 tfs = (GESTimelineFileSource *) tmp->data;
342 if (!g_strcmp0 (tfs->uri, info->uri)) {
349 /* Remove object from list */
350 timeline->pendingobjects =
351 g_list_delete_link (timeline->pendingobjects, tmp);
353 /* FIXME : Handle errors in discovery */
355 /* Update timelinefilesource properties based on info */
356 for (tmp = info->stream_list; tmp; tmp = tmp->next) {
357 GstStreamInformation *sinf = (GstStreamInformation *) tmp->data;
359 if (sinf->streamtype == GST_STREAM_AUDIO)
360 tfs->supportedformats |= GES_TRACK_TYPE_AUDIO;
361 else if (sinf->streamtype == GST_STREAM_VIDEO)
362 tfs->supportedformats |= GES_TRACK_TYPE_VIDEO;
363 else if (sinf->streamtype == GST_STREAM_IMAGE) {
364 tfs->supportedformats |= GES_TRACK_TYPE_VIDEO | GES_TRACK_TYPE_AUDIO;
370 /* don't set max-duration on still images */
371 g_object_set (tfs, "is_image", (gboolean) TRUE, NULL);
375 g_object_set (tfs, "max-duration", (guint64) info->duration, NULL);
378 /* Continue the processing on tfs */
379 add_object_to_tracks (timeline, GES_TIMELINE_OBJECT (tfs));
383 static GstStateChangeReturn
384 ges_timeline_change_state (GstElement * element, GstStateChange transition)
386 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
387 GESTimeline *timeline = GES_TIMELINE (element);
389 switch (transition) {
390 case GST_STATE_CHANGE_READY_TO_PAUSED:
391 if (timeline->pendingobjects) {
392 do_async_start (timeline);
393 ret = GST_STATE_CHANGE_ASYNC;
401 GstStateChangeReturn bret;
403 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
404 if (G_UNLIKELY (bret == GST_STATE_CHANGE_NO_PREROLL)) {
405 do_async_done (timeline);
410 switch (transition) {
411 case GST_STATE_CHANGE_PAUSED_TO_READY:
412 do_async_done (timeline);
423 layer_object_added_cb (GESTimelineLayer * layer, GESTimelineObject * object,
424 GESTimeline * timeline)
426 GST_DEBUG ("New TimelineObject %p added to layer %p", object, layer);
428 if (GES_IS_TIMELINE_FILE_SOURCE (object)) {
429 GESTimelineFileSource *tfs = GES_TIMELINE_FILE_SOURCE (object);
431 /* Send the filesource to the discoverer if:
432 * * it doesn't have specified supported formats
433 * * OR it doesn't have a specified max-duration
434 * * OR it doesn't have a valid duration */
436 if (tfs->supportedformats == GES_TRACK_TYPE_UNKNOWN ||
437 tfs->maxduration == GST_CLOCK_TIME_NONE || object->duration == 0) {
438 GST_LOG ("Incomplete TimelineFileSource, discovering it");
439 timeline->pendingobjects =
440 g_list_append (timeline->pendingobjects, object);
441 gst_discoverer_append_uri (timeline->discoverer,
442 GES_TIMELINE_FILE_SOURCE (object)->uri);
444 add_object_to_tracks (timeline, object);
446 add_object_to_tracks (timeline, object);
454 layer_object_removed_cb (GESTimelineLayer * layer, GESTimelineObject * object,
455 GESTimeline * timeline)
459 GST_DEBUG ("TimelineObject %p removed from layer %p", object, layer);
461 /* Go over the object's track objects and figure out which one belongs to
462 * the list of tracks we control */
464 for (tmp = object->trackobjects; tmp; tmp = next) {
465 GESTrackObject *trobj = (GESTrackObject *) tmp->data;
467 next = g_list_next (tmp);
469 GST_DEBUG ("Trying to remove TrackObject %p", trobj);
470 if (G_LIKELY (g_list_find_custom (timeline->tracks,
471 (gconstpointer) trobj->track,
472 (GCompareFunc) custom_find_track))) {
473 GST_DEBUG ("Belongs to one of the tracks we control");
474 ges_track_remove_object (trobj->track, trobj);
476 ges_timeline_object_release_track_object (object, trobj);
484 * ges_timeline_add_layer:
485 * @timeline: a #GESTimeline
486 * @layer: the #GESTimelineLayer to add
488 * Add the layer to the timeline. The reference to the @layer will be stolen
491 * Returns: TRUE if the layer was properly added, else FALSE.
494 ges_timeline_add_layer (GESTimeline * timeline, GESTimelineLayer * layer)
496 GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
498 /* We can only add a layer that doesn't already belong to another timeline */
499 if (G_UNLIKELY (layer->timeline)) {
500 GST_WARNING ("Layer belongs to another timeline, can't add it");
504 /* Add to the list of layers, make sure we don't already control it */
505 if (G_UNLIKELY (g_list_find (timeline->layers, (gconstpointer) layer))) {
506 GST_WARNING ("Layer is already controlled by this timeline");
510 /* Reference is stolen */
511 timeline->layers = g_list_append (timeline->layers, layer);
513 /* Inform the layer that it belongs to a new timeline */
514 ges_timeline_layer_set_timeline (layer, timeline);
516 /* FIXME : GO OVER THE LIST OF ALREADY EXISTING TIMELINE OBJECTS IN THAT
517 * LAYER AND ADD THEM !!! */
519 /* Connect to 'object-added'/'object-removed' signal from the new layer */
520 g_signal_connect (layer, "object-added", G_CALLBACK (layer_object_added_cb),
522 g_signal_connect (layer, "object-removed",
523 G_CALLBACK (layer_object_removed_cb), timeline);
525 GST_DEBUG ("Done adding layer, emitting 'layer-added' signal");
526 g_signal_emit (timeline, ges_timeline_signals[LAYER_ADDED], 0, layer);
532 * ges_timeline_remove_layer:
533 * @timeline: a #GESTimeline
534 * @layer: the #GESTimelineLayer to remove
536 * Removes the layer from the timeline. The reference that the @timeline holds on
537 * the layer will be dropped. If you wish to use the @layer after calling this
538 * method, you need to take a reference before calling.
540 * Returns: TRUE if the layer was properly removed, else FALSE.
544 ges_timeline_remove_layer (GESTimeline * timeline, GESTimelineLayer * layer)
546 GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
548 if (G_UNLIKELY (!g_list_find (timeline->layers, layer))) {
549 GST_WARNING ("Layer doesn't belong to this timeline");
553 /* Disconnect signals */
554 GST_DEBUG ("Disconnecting signal callbacks");
555 g_signal_handlers_disconnect_by_func (layer, layer_object_added_cb, timeline);
556 g_signal_handlers_disconnect_by_func (layer, layer_object_removed_cb,
559 timeline->layers = g_list_remove (timeline->layers, layer);
561 ges_timeline_layer_set_timeline (layer, NULL);
563 g_signal_emit (timeline, ges_timeline_signals[LAYER_REMOVED], 0, layer);
565 g_object_unref (layer);
571 pad_added_cb (GESTrack * track, GstPad * pad, TrackPrivate * priv)
575 GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));
577 if (G_UNLIKELY (priv->pad)) {
578 GST_WARNING ("We are already controlling a pad for this track");
582 /* Remember the pad */
586 GST_DEBUG ("Ghosting pad and adding it to ourself");
587 padname = g_strdup_printf ("track_%p_src", track);
588 priv->ghostpad = gst_ghost_pad_new (padname, pad);
590 gst_pad_set_active (priv->ghostpad, TRUE);
591 gst_element_add_pad (GST_ELEMENT (priv->timeline), priv->ghostpad);
595 pad_removed_cb (GESTrack * track, GstPad * pad, TrackPrivate * priv)
597 GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));
599 if (G_UNLIKELY (priv->pad != pad)) {
600 GST_WARNING ("Not the pad we're controlling");
604 if (G_UNLIKELY (priv->ghostpad == NULL)) {
605 GST_WARNING ("We don't have a ghostpad for this pad !");
609 GST_DEBUG ("Removing ghostpad");
610 gst_pad_set_active (priv->ghostpad, FALSE);
611 gst_element_remove_pad (GST_ELEMENT (priv->timeline), priv->ghostpad);
612 priv->ghostpad = NULL;
617 custom_find_track (TrackPrivate * priv, GESTrack * track)
619 if (priv->track == track)
625 * ges_timeline_add_track:
626 * @timeline: a #GESTimeline
627 * @track: the #GESTrack to add
629 * Add a track to the timeline. The reference to the track will be stolen by the
632 * Returns: TRUE if the track was properly added, else FALSE.
636 ges_timeline_add_track (GESTimeline * timeline, GESTrack * track)
641 GST_DEBUG ("timeline:%p, track:%p", timeline, track);
643 /* make sure we don't already control it */
644 if (G_UNLIKELY ((tmp =
645 g_list_find_custom (timeline->tracks, (gconstpointer) track,
646 (GCompareFunc) custom_find_track)))) {
647 GST_WARNING ("Track is already controlled by this timeline");
651 /* Add the track to ourself (as a GstBin)
652 * Reference is stolen ! */
653 if (G_UNLIKELY (!gst_bin_add (GST_BIN (timeline), GST_ELEMENT (track)))) {
654 GST_WARNING ("Couldn't add track to ourself (GST)");
658 priv = g_new0 (TrackPrivate, 1);
659 priv->timeline = timeline;
662 /* Add the track to the list of tracks we track */
663 timeline->tracks = g_list_append (timeline->tracks, priv);
665 /* Listen to pad-added/-removed */
666 g_signal_connect (track, "pad-added", (GCallback) pad_added_cb, priv);
667 g_signal_connect (track, "pad-removed", (GCallback) pad_removed_cb, priv);
669 /* Inform the track that it's currently being used by ourself */
670 ges_track_set_timeline (track, timeline);
672 GST_DEBUG ("Done adding track, emitting 'track-added' signal");
674 /* emit 'track-added' */
675 g_signal_emit (timeline, ges_timeline_signals[TRACK_ADDED], 0, track);
681 * ges_timeline_remove_track:
682 * @timeline: a #GESTimeline
683 * @track: the #GESTrack to remove
685 * Remove the @track from the @timeline. The reference stolen when adding the
686 * @track will be removed. If you wish to use the @track after calling this
687 * function you must ensure that you have a reference to it.
689 * Returns: TRUE if the @track was properly removed, else FALSE.
692 ges_timeline_remove_track (GESTimeline * timeline, GESTrack * track)
697 GST_DEBUG ("timeline:%p, track:%p", timeline, track);
699 if (G_UNLIKELY (!(tmp =
700 g_list_find_custom (timeline->tracks, (gconstpointer) track,
701 (GCompareFunc) custom_find_track)))) {
702 GST_WARNING ("Track doesn't belong to this timeline");
707 timeline->tracks = g_list_remove (timeline->tracks, priv);
709 ges_track_set_timeline (track, NULL);
711 /* Remove ghost pad */
712 if (priv->ghostpad) {
713 GST_DEBUG ("Removing ghostpad");
714 gst_pad_set_active (priv->ghostpad, FALSE);
715 gst_ghost_pad_set_target ((GstGhostPad *) priv->ghostpad, NULL);
716 gst_element_remove_pad (GST_ELEMENT (timeline), priv->ghostpad);
719 /* Remove pad-added/-removed handlers */
720 g_signal_handlers_disconnect_by_func (track, pad_added_cb, priv);
721 g_signal_handlers_disconnect_by_func (track, pad_removed_cb, priv);
723 /* Signal track removal to all layers/objects */
724 g_signal_emit (timeline, ges_timeline_signals[TRACK_REMOVED], 0, track);
726 /* remove track from our bin */
727 if (G_UNLIKELY (!gst_bin_remove (GST_BIN (timeline), GST_ELEMENT (track)))) {
728 GST_WARNING ("Couldn't remove track to ourself (GST)");
738 * ges_timeline_get_track_for_pad:
739 * @timeline: The #GESTimeline
742 * Search the #GESTrack corresponding to the given @timeline's @pad.
744 * Returns: The corresponding #GESTrack if it is found, or #NULL if there is
749 ges_timeline_get_track_for_pad (GESTimeline * timeline, GstPad * pad)
753 for (tmp = timeline->tracks; tmp; tmp = g_list_next (tmp)) {
754 TrackPrivate *priv = (TrackPrivate *) tmp->data;
755 if (pad == priv->ghostpad)
763 * ges_timeline_get_tracks:
764 * @timeline: a #GESTimeline
766 * Returns the list of #GESTrack used by the Timeline.
768 * Returns: A list of #GESTrack. The caller should unref each track
769 * once he is done with them. */
771 ges_timeline_get_tracks (GESTimeline * timeline)
773 GList *tmp, *res = NULL;
775 for (tmp = timeline->tracks; tmp; tmp = g_list_next (tmp)) {
776 TrackPrivate *priv = (TrackPrivate *) tmp->data;
777 res = g_list_append (res, g_object_ref (priv->track));