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 #GESTimelineTrack 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;
106 G_OBJECT_CLASS (ges_timeline_parent_class)->dispose (object);
110 ges_timeline_finalize (GObject * object)
112 G_OBJECT_CLASS (ges_timeline_parent_class)->finalize (object);
116 ges_timeline_class_init (GESTimelineClass * klass)
118 GObjectClass *object_class = G_OBJECT_CLASS (klass);
119 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
121 parent_class = g_type_class_peek_parent (klass);
123 element_class->change_state = ges_timeline_change_state;
125 object_class->get_property = ges_timeline_get_property;
126 object_class->set_property = ges_timeline_set_property;
127 object_class->dispose = ges_timeline_dispose;
128 object_class->finalize = ges_timeline_finalize;
131 * GESTimeline::track-added
132 * @timeline: the #GESTimeline
133 * @track: the #GESTrack that was added to the timeline
135 * Will be emitted after the track was added to the timeline.
137 ges_timeline_signals[TRACK_ADDED] =
138 g_signal_new ("track-added", G_TYPE_FROM_CLASS (klass),
139 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, track_added), NULL,
140 NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TRACK);
143 * GESTimeline::track-removed
144 * @timeline: the #GESTimeline
145 * @track: the #GESTrack that was removed from the timeline
147 * Will be emitted after the track was removed from the timeline.
149 ges_timeline_signals[TRACK_REMOVED] =
150 g_signal_new ("track-removed", G_TYPE_FROM_CLASS (klass),
151 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, track_removed),
152 NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TRACK);
155 * GESTimeline::layer-added
156 * @timeline: the #GESTimeline
157 * @layer: the #GESTimelineLayer that was added to the timeline
159 * Will be emitted after the layer was added to the timeline.
161 ges_timeline_signals[LAYER_ADDED] =
162 g_signal_new ("layer-added", G_TYPE_FROM_CLASS (klass),
163 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, layer_added), NULL,
164 NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TIMELINE_LAYER);
167 * GESTimeline::layer-removed
168 * @timeline: the #GESTimeline
169 * @layer: the #GESTimelineLayer that was removed from the timeline
171 * Will be emitted after the layer was removed from the timeline.
173 ges_timeline_signals[LAYER_REMOVED] =
174 g_signal_new ("layer-removed", G_TYPE_FROM_CLASS (klass),
175 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, layer_removed),
176 NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
177 GES_TYPE_TIMELINE_LAYER);
181 ges_timeline_init (GESTimeline * self)
186 /* New discoverer with a 15s timeout */
187 self->discoverer = gst_discoverer_new (15 * GST_SECOND);
188 g_signal_connect (self->discoverer, "ready", G_CALLBACK (discoverer_ready_cb),
190 g_signal_connect (self->discoverer, "discovered",
191 G_CALLBACK (discoverer_discovered_cb), self);
192 gst_discoverer_start (self->discoverer);
198 * Creates a new empty #GESTimeline.
200 * Returns: The new timeline.
204 ges_timeline_new (void)
206 return g_object_new (GES_TYPE_TIMELINE, NULL);
210 * ges_timeline_load_from_uri:
211 * @uri: The URI to load from
213 * Creates a timeline from the contents of given uri.
217 * Returns: A new #GESTimeline if loading was successful, else NULL.
221 ges_timeline_load_from_uri (gchar * uri)
223 /* FIXME : IMPLEMENT */
229 * @timeline: a #GESTimeline
230 * @uri: The location to save to
232 * Saves the timeline to the given location
236 * Returns: TRUE if the timeline was successfully saved to the given location,
241 ges_timeline_save (GESTimeline * timeline, gchar * uri)
243 /* FIXME : IMPLEMENT */
248 add_object_to_tracks (GESTimeline * timeline, GESTimelineObject * object)
252 for (tmp = timeline->tracks; tmp; tmp = g_list_next (tmp)) {
253 TrackPrivate *priv = (TrackPrivate *) tmp->data;
254 GESTrack *track = priv->track;
255 GESTrackObject *trobj;
257 GST_LOG ("Trying with track %p", track);
259 if (G_UNLIKELY (!(trobj =
260 ges_timeline_object_create_track_object (object, track)))) {
261 GST_WARNING ("Couldn't create TrackObject for TimelineObject");
265 GST_LOG ("Got new TrackObject %p, adding it to track", trobj);
266 ges_track_add_object (track, trobj);
271 do_async_start (GESTimeline * timeline)
275 timeline->async_pending = TRUE;
277 message = gst_message_new_async_start (GST_OBJECT_CAST (timeline), FALSE);
278 parent_class->handle_message (GST_BIN_CAST (timeline), message);
282 do_async_done (GESTimeline * timeline)
286 if (timeline->async_pending) {
287 GST_DEBUG_OBJECT (timeline, "Emitting async-done");
288 message = gst_message_new_async_done (GST_OBJECT_CAST (timeline));
289 parent_class->handle_message (GST_BIN_CAST (timeline), message);
291 timeline->async_pending = FALSE;
296 discoverer_ready_cb (GstDiscoverer * discoverer, GESTimeline * timeline)
298 do_async_done (timeline);
302 discoverer_discovered_cb (GstDiscoverer * discoverer,
303 GstDiscovererInformation * info, GError * err, GESTimeline * timeline)
306 gboolean found = FALSE;
307 GESTimelineFileSource *tfs = NULL;
309 GST_DEBUG ("Discovered uri %s", info->uri);
311 /* Find corresponding TimelineFileSource in the sources */
312 for (tmp = timeline->pendingobjects; tmp; tmp = tmp->next) {
313 tfs = (GESTimelineFileSource *) tmp->data;
315 if (!g_strcmp0 (tfs->uri, info->uri)) {
322 /* Remove object from list */
323 timeline->pendingobjects =
324 g_list_delete_link (timeline->pendingobjects, tmp);
326 /* FIXME : Handle errors in discovery */
328 /* Update timelinefilesource properties based on info */
329 for (tmp = info->stream_list; tmp; tmp = tmp->next) {
330 GstStreamInformation *sinf = (GstStreamInformation *) tmp->data;
332 if (sinf->streamtype == GST_STREAM_AUDIO)
333 tfs->supportedformats |= GES_TRACK_TYPE_AUDIO;
334 else if ((sinf->streamtype == GST_STREAM_VIDEO) ||
335 (sinf->streamtype == GST_STREAM_IMAGE))
336 tfs->supportedformats |= GES_TRACK_TYPE_VIDEO;
339 g_object_set (tfs, "max-duration", info->duration, NULL);
341 /* Continue the processing on tfs */
342 add_object_to_tracks (timeline, GES_TIMELINE_OBJECT (tfs));
346 static GstStateChangeReturn
347 ges_timeline_change_state (GstElement * element, GstStateChange transition)
349 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
350 GESTimeline *timeline = GES_TIMELINE (element);
352 switch (transition) {
353 case GST_STATE_CHANGE_READY_TO_PAUSED:
354 if (timeline->pendingobjects) {
355 do_async_start (timeline);
356 ret = GST_STATE_CHANGE_ASYNC;
364 GstStateChangeReturn bret;
366 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
367 if (G_UNLIKELY (bret == GST_STATE_CHANGE_NO_PREROLL)) {
368 do_async_done (timeline);
373 switch (transition) {
374 case GST_STATE_CHANGE_PAUSED_TO_READY:
375 do_async_done (timeline);
386 layer_object_added_cb (GESTimelineLayer * layer, GESTimelineObject * object,
387 GESTimeline * timeline)
389 GST_DEBUG ("New TimelineObject %p added to layer %p", object, layer);
391 if (GES_IS_TIMELINE_FILE_SOURCE (object)) {
392 GESTimelineFileSource *tfs = GES_TIMELINE_FILE_SOURCE (object);
394 /* Send the filesource to the discoverer if:
395 * * it doesn't have specified supported formats
396 * * OR it doesn't have a specified max-duration
397 * * OR it doesn't have a valid duration */
399 if (tfs->supportedformats == GES_TRACK_TYPE_UNKNOWN ||
400 tfs->maxduration == GST_CLOCK_TIME_NONE || object->duration == 0) {
401 GST_LOG ("Incomplete TimelineFileSource, discovering it");
402 timeline->pendingobjects =
403 g_list_append (timeline->pendingobjects, object);
404 gst_discoverer_append_uri (timeline->discoverer,
405 GES_TIMELINE_FILE_SOURCE (object)->uri);
407 add_object_to_tracks (timeline, object);
409 add_object_to_tracks (timeline, object);
417 layer_object_removed_cb (GESTimelineLayer * layer, GESTimelineObject * object,
418 GESTimeline * timeline)
422 GST_DEBUG ("TimelineObject %p removed from layer %p", object, layer);
424 /* Go over the object's track objects and figure out which one belongs to
425 * the list of tracks we control */
427 for (tmp = object->trackobjects; tmp; tmp = next) {
428 GESTrackObject *trobj = (GESTrackObject *) tmp->data;
430 next = g_list_next (tmp);
432 GST_DEBUG ("Trying to remove TrackObject %p", trobj);
433 if (G_LIKELY (g_list_find_custom (timeline->tracks,
434 (gconstpointer) trobj->track,
435 (GCompareFunc) custom_find_track))) {
436 GST_DEBUG ("Belongs to one of the tracks we control");
437 ges_track_remove_object (trobj->track, trobj);
439 ges_timeline_object_release_track_object (object, trobj);
447 * ges_timeline_add_layer:
448 * @timeline: a #GESTimeline
449 * @layer: the #GESTimelineLayer to add
451 * Add the layer to the timeline. The reference to the @layer will be stolen
454 * Returns: TRUE if the layer was properly added, else FALSE.
457 ges_timeline_add_layer (GESTimeline * timeline, GESTimelineLayer * layer)
459 GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
461 /* We can only add a layer that doesn't already belong to another timeline */
462 if (G_UNLIKELY (layer->timeline)) {
463 GST_WARNING ("Layer belongs to another timeline, can't add it");
467 /* Add to the list of layers, make sure we don't already control it */
468 if (G_UNLIKELY (g_list_find (timeline->layers, (gconstpointer) layer))) {
469 GST_WARNING ("Layer is already controlled by this timeline");
473 /* Reference is stolen */
474 timeline->layers = g_list_append (timeline->layers, layer);
476 /* Inform the layer that it belongs to a new timeline */
477 ges_timeline_layer_set_timeline (layer, timeline);
479 /* FIXME : GO OVER THE LIST OF ALREADY EXISTING TIMELINE OBJECTS IN THAT
480 * LAYER AND ADD THEM !!! */
482 /* Connect to 'object-added'/'object-removed' signal from the new layer */
483 g_signal_connect (layer, "object-added", G_CALLBACK (layer_object_added_cb),
485 g_signal_connect (layer, "object-removed",
486 G_CALLBACK (layer_object_removed_cb), timeline);
488 GST_DEBUG ("Done adding layer, emitting 'layer-added' signal");
489 g_signal_emit (timeline, ges_timeline_signals[LAYER_ADDED], 0, layer);
495 * ges_timeline_remove_layer:
496 * @timeline: a #GESTimeline
497 * @layer: the #GESTimelineLayer to remove
499 * Removes the layer from the timeline. The reference that the @timeline holds on
500 * the layer will be dropped. If you wish to use the @layer after calling this
501 * method, you need to take a reference before calling.
503 * Returns: TRUE if the layer was properly removed, else FALSE.
507 ges_timeline_remove_layer (GESTimeline * timeline, GESTimelineLayer * layer)
509 GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
511 if (G_UNLIKELY (!g_list_find (timeline->layers, layer))) {
512 GST_WARNING ("Layer doesn't belong to this timeline");
516 /* Disconnect signals */
517 GST_DEBUG ("Disconnecting signal callbacks");
518 g_signal_handlers_disconnect_by_func (layer, layer_object_added_cb, timeline);
519 g_signal_handlers_disconnect_by_func (layer, layer_object_removed_cb,
522 timeline->layers = g_list_remove (timeline->layers, layer);
524 ges_timeline_layer_set_timeline (layer, NULL);
526 g_signal_emit (timeline, ges_timeline_signals[LAYER_REMOVED], 0, layer);
528 g_object_unref (layer);
534 pad_added_cb (GESTrack * track, GstPad * pad, TrackPrivate * priv)
538 GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));
540 if (G_UNLIKELY (priv->pad)) {
541 GST_WARNING ("We are already controlling a pad for this track");
545 /* Remember the pad */
549 GST_DEBUG ("Ghosting pad and adding it to ourself");
550 padname = g_strdup_printf ("track_%p_src", track);
551 priv->ghostpad = gst_ghost_pad_new (padname, pad);
553 gst_pad_set_active (priv->ghostpad, TRUE);
554 gst_element_add_pad (GST_ELEMENT (priv->timeline), priv->ghostpad);
558 pad_removed_cb (GESTrack * track, GstPad * pad, TrackPrivate * priv)
560 GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));
562 if (G_UNLIKELY (priv->pad != pad)) {
563 GST_WARNING ("Not the pad we're controlling");
567 if (G_UNLIKELY (priv->ghostpad == NULL)) {
568 GST_WARNING ("We don't have a ghostpad for this pad !");
572 GST_DEBUG ("Removing ghostpad");
573 gst_pad_set_active (priv->ghostpad, FALSE);
574 gst_element_remove_pad (GST_ELEMENT (priv->timeline), priv->ghostpad);
575 priv->ghostpad = NULL;
580 custom_find_track (TrackPrivate * priv, GESTrack * track)
582 if (priv->track == track)
588 * ges_timeline_add_track:
589 * @timeline: a #GESTimeline
590 * @track: the #GESTrack to add
592 * Add a track to the timeline. The reference to the track will be stolen by the
595 * Returns: TRUE if the track was properly added, else FALSE.
599 ges_timeline_add_track (GESTimeline * timeline, GESTrack * track)
604 GST_DEBUG ("timeline:%p, track:%p", timeline, track);
606 /* make sure we don't already control it */
607 if (G_UNLIKELY ((tmp =
608 g_list_find_custom (timeline->tracks, (gconstpointer) track,
609 (GCompareFunc) custom_find_track)))) {
610 GST_WARNING ("Track is already controlled by this timeline");
614 /* Add the track to ourself (as a GstBin)
615 * Reference is stolen ! */
616 if (G_UNLIKELY (!gst_bin_add (GST_BIN (timeline), GST_ELEMENT (track)))) {
617 GST_WARNING ("Couldn't add track to ourself (GST)");
621 priv = g_new0 (TrackPrivate, 1);
622 priv->timeline = timeline;
625 /* Add the track to the list of tracks we track */
626 timeline->tracks = g_list_append (timeline->tracks, priv);
628 /* Listen to pad-added/-removed */
629 g_signal_connect (track, "pad-added", (GCallback) pad_added_cb, priv);
630 g_signal_connect (track, "pad-removed", (GCallback) pad_removed_cb, priv);
632 /* Inform the track that it's currently being used by ourself */
633 ges_track_set_timeline (track, timeline);
635 GST_DEBUG ("Done adding track, emitting 'track-added' signal");
637 /* emit 'track-added' */
638 g_signal_emit (timeline, ges_timeline_signals[TRACK_ADDED], 0, track);
644 * ges_timeline_remove_track:
645 * @timeline: a #GESTimeline
646 * @track: the #GESTrack to remove
648 * Remove the @track from the @timeline. The reference stolen when adding the
649 * @track will be removed. If you wish to use the @track after calling this
650 * function you must ensure that you have a reference to it.
652 * Returns: TRUE if the @track was properly removed, else FALSE.
655 ges_timeline_remove_track (GESTimeline * timeline, GESTrack * track)
660 GST_DEBUG ("timeline:%p, track:%p", timeline, track);
662 if (G_UNLIKELY (!(tmp =
663 g_list_find_custom (timeline->tracks, (gconstpointer) track,
664 (GCompareFunc) custom_find_track)))) {
665 GST_WARNING ("Track doesn't belong to this timeline");
670 timeline->tracks = g_list_remove (timeline->tracks, priv);
672 ges_track_set_timeline (track, NULL);
674 /* Remove ghost pad */
675 if (priv->ghostpad) {
676 GST_DEBUG ("Removing ghostpad");
677 gst_pad_set_active (priv->ghostpad, FALSE);
678 gst_ghost_pad_set_target ((GstGhostPad *) priv->ghostpad, NULL);
679 gst_element_remove_pad (GST_ELEMENT (timeline), priv->ghostpad);
682 /* Remove pad-added/-removed handlers */
683 g_signal_handlers_disconnect_by_func (track, pad_added_cb, priv);
684 g_signal_handlers_disconnect_by_func (track, pad_removed_cb, priv);
686 /* Signal track removal to all layers/objects */
687 g_signal_emit (timeline, ges_timeline_signals[TRACK_REMOVED], 0, track);
689 /* remove track from our bin */
690 if (G_UNLIKELY (!gst_bin_remove (GST_BIN (timeline), GST_ELEMENT (track)))) {
691 GST_WARNING ("Couldn't remove track to ourself (GST)");
701 * ges_timeline_get_track_for_pad:
702 * @timeline: The #GESTimeline
705 * Search the #GESTrack corresponding to the given @timeline's @pad.
707 * Returns: The corresponding #GESTrack if it is found, or #NULL if there is
712 ges_timeline_get_track_for_pad (GESTimeline * timeline, GstPad * pad)
716 for (tmp = timeline->tracks; tmp; tmp = g_list_next (tmp)) {
717 TrackPrivate *priv = (TrackPrivate *) tmp->data;
718 if (pad == priv->ghostpad)
726 * ges_timeline_get_tracks:
727 * @timeline: a #GESTimeline
729 * Returns the list of #GESTrack used by the Timeline.
731 * Returns: A list of #GESTrack. The caller should unref each track
732 * once he is done with them. */
734 ges_timeline_get_tracks (GESTimeline * timeline)
736 GList *tmp, *res = NULL;
738 for (tmp = timeline->tracks; tmp; tmp = g_list_next (tmp)) {
739 TrackPrivate *priv = (TrackPrivate *) tmp->data;
740 res = g_list_append (res, g_object_ref (priv->track));