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 guint ges_timeline_signals[LAST_SIGNAL] = { 0 };
65 gint custom_find_track (TrackPrivate * priv, GESTrack * track);
67 ges_timeline_get_property (GObject * object, guint property_id,
68 GValue * value, GParamSpec * pspec)
70 switch (property_id) {
72 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
77 ges_timeline_set_property (GObject * object, guint property_id,
78 const GValue * value, GParamSpec * pspec)
80 switch (property_id) {
82 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
87 ges_timeline_dispose (GObject * object)
89 G_OBJECT_CLASS (ges_timeline_parent_class)->dispose (object);
93 ges_timeline_finalize (GObject * object)
95 G_OBJECT_CLASS (ges_timeline_parent_class)->finalize (object);
99 ges_timeline_class_init (GESTimelineClass * klass)
101 GObjectClass *object_class = G_OBJECT_CLASS (klass);
103 object_class->get_property = ges_timeline_get_property;
104 object_class->set_property = ges_timeline_set_property;
105 object_class->dispose = ges_timeline_dispose;
106 object_class->finalize = ges_timeline_finalize;
110 * GESTimeline::track-added
111 * @timeline: the #GESTimeline
112 * @track: the #GESTrack that was added to the timeline
114 * Will be emitted after the track was added to the timeline.
116 ges_timeline_signals[TRACK_ADDED] =
117 g_signal_new ("track-added", G_TYPE_FROM_CLASS (klass),
118 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, track_added), NULL,
119 NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TRACK);
122 * GESTimeline::track-removed
123 * @timeline: the #GESTimeline
124 * @track: the #GESTrack that was removed from the timeline
126 * Will be emitted after the track was removed from the timeline.
128 ges_timeline_signals[TRACK_REMOVED] =
129 g_signal_new ("track-removed", G_TYPE_FROM_CLASS (klass),
130 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, track_removed),
131 NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TRACK);
134 * GESTimeline::layer-added
135 * @timeline: the #GESTimeline
136 * @layer: the #GESTimelineLayer that was added to the timeline
138 * Will be emitted after the layer was added to the timeline.
140 ges_timeline_signals[LAYER_ADDED] =
141 g_signal_new ("layer-added", G_TYPE_FROM_CLASS (klass),
142 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, layer_added), NULL,
143 NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TIMELINE_LAYER);
146 * GESTimeline::layer-removed
147 * @timeline: the #GESTimeline
148 * @layer: the #GESTimelineLayer that was removed from the timeline
150 * Will be emitted after the layer was removed from the timeline.
152 ges_timeline_signals[LAYER_REMOVED] =
153 g_signal_new ("layer-removed", G_TYPE_FROM_CLASS (klass),
154 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, layer_removed),
155 NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
156 GES_TYPE_TIMELINE_LAYER);
160 ges_timeline_init (GESTimeline * self)
169 * Creates a new empty #GESTimeline.
171 * Returns: The new timeline.
175 ges_timeline_new (void)
177 return g_object_new (GES_TYPE_TIMELINE, NULL);
181 * ges_timeline_load_from_uri:
182 * @uri: The URI to load from
184 * Creates a timeline from the contents of given uri.
188 * Returns: A new #GESTimeline if loading was successful, else NULL.
192 ges_timeline_load_from_uri (gchar * uri)
194 /* FIXME : IMPLEMENT */
200 * @timeline: a #GESTimeline
201 * @uri: The location to save to
203 * Saves the timeline to the given location
207 * Returns: TRUE if the timeline was successfully saved to the given location,
212 ges_timeline_save (GESTimeline * timeline, gchar * uri)
214 /* FIXME : IMPLEMENT */
219 layer_object_added_cb (GESTimelineLayer * layer, GESTimelineObject * object,
220 GESTimeline * timeline)
224 GST_DEBUG ("New TimelineObject %p added to layer %p", object, layer);
226 for (tmp = timeline->tracks; tmp; tmp = g_list_next (tmp)) {
227 TrackPrivate *priv = (TrackPrivate *) tmp->data;
228 GESTrack *track = priv->track;
229 GESTrackObject *trobj;
231 GST_LOG ("Trying with track %p", track);
233 if (G_UNLIKELY (!(trobj =
234 ges_timeline_object_create_track_object (object, track)))) {
235 GST_WARNING ("Couldn't create TrackObject for TimelineObject");
239 GST_LOG ("Got new TrackObject %p, adding it to track", trobj);
240 ges_track_add_object (track, trobj);
248 layer_object_removed_cb (GESTimelineLayer * layer, GESTimelineObject * object,
249 GESTimeline * timeline)
253 GST_DEBUG ("TimelineObject %p removed from layer %p", object, layer);
255 /* Go over the object's track objects and figure out which one belongs to
256 * the list of tracks we control */
258 for (tmp = object->trackobjects; tmp; tmp = next) {
259 GESTrackObject *trobj = (GESTrackObject *) tmp->data;
261 next = g_list_next (tmp);
263 GST_DEBUG ("Trying to remove TrackObject %p", trobj);
264 if (G_LIKELY (g_list_find_custom (timeline->tracks,
265 (gconstpointer) trobj->track,
266 (GCompareFunc) custom_find_track))) {
267 GST_DEBUG ("Belongs to one of the tracks we control");
268 ges_track_remove_object (trobj->track, trobj);
270 ges_timeline_object_release_track_object (object, trobj);
278 * ges_timeline_add_layer:
279 * @timeline: a #GESTimeline
280 * @layer: the #GESTimelineLayer to add
282 * Add the layer to the timeline. The reference to the @layer will be stolen
285 * Returns: TRUE if the layer was properly added, else FALSE.
288 ges_timeline_add_layer (GESTimeline * timeline, GESTimelineLayer * layer)
290 GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
292 /* We can only add a layer that doesn't already belong to another timeline */
293 if (G_UNLIKELY (layer->timeline)) {
294 GST_WARNING ("Layer belongs to another timeline, can't add it");
298 /* Add to the list of layers, make sure we don't already control it */
299 if (G_UNLIKELY (g_list_find (timeline->layers, (gconstpointer) layer))) {
300 GST_WARNING ("Layer is already controlled by this timeline");
304 /* Reference is stolen */
305 timeline->layers = g_list_append (timeline->layers, layer);
307 /* Inform the layer that it belongs to a new timeline */
308 ges_timeline_layer_set_timeline (layer, timeline);
310 /* FIXME : GO OVER THE LIST OF ALREADY EXISTING TIMELINE OBJECTS IN THAT
311 * LAYER AND ADD THEM !!! */
313 /* Connect to 'object-added'/'object-removed' signal from the new layer */
314 g_signal_connect (layer, "object-added", G_CALLBACK (layer_object_added_cb),
316 g_signal_connect (layer, "object-removed",
317 G_CALLBACK (layer_object_removed_cb), timeline);
319 GST_DEBUG ("Done adding layer, emitting 'layer-added' signal");
320 g_signal_emit (timeline, ges_timeline_signals[LAYER_ADDED], 0, layer);
326 * ges_timeline_remove_layer:
327 * @timeline: a #GESTimeline
328 * @layer: the #GESTimelineLayer to remove
330 * Removes the layer from the timeline. The reference that the @timeline holds on
331 * the layer will be dropped. If you wish to use the @layer after calling this
332 * method, you need to take a reference before calling.
334 * Returns: TRUE if the layer was properly removed, else FALSE.
338 ges_timeline_remove_layer (GESTimeline * timeline, GESTimelineLayer * layer)
340 GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
342 if (G_UNLIKELY (!g_list_find (timeline->layers, layer))) {
343 GST_WARNING ("Layer doesn't belong to this timeline");
347 /* Disconnect signals */
348 GST_DEBUG ("Disconnecting signal callbacks");
349 g_signal_handlers_disconnect_by_func (layer, layer_object_added_cb, timeline);
350 g_signal_handlers_disconnect_by_func (layer, layer_object_removed_cb,
353 timeline->layers = g_list_remove (timeline->layers, layer);
355 ges_timeline_layer_set_timeline (layer, NULL);
357 g_signal_emit (timeline, ges_timeline_signals[LAYER_REMOVED], 0, layer);
359 g_object_unref (layer);
365 pad_added_cb (GESTrack * track, GstPad * pad, TrackPrivate * priv)
369 GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));
371 if (G_UNLIKELY (priv->pad)) {
372 GST_WARNING ("We are already controlling a pad for this track");
376 /* Remember the pad */
380 GST_DEBUG ("Ghosting pad and adding it to ourself");
381 padname = g_strdup_printf ("track_%p_src", track);
382 priv->ghostpad = gst_ghost_pad_new (padname, pad);
384 gst_pad_set_active (priv->ghostpad, TRUE);
385 gst_element_add_pad (GST_ELEMENT (priv->timeline), priv->ghostpad);
389 pad_removed_cb (GESTrack * track, GstPad * pad, TrackPrivate * priv)
391 GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));
393 if (G_UNLIKELY (priv->pad != pad)) {
394 GST_WARNING ("Not the pad we're controlling");
398 if (G_UNLIKELY (priv->ghostpad == NULL)) {
399 GST_WARNING ("We don't have a ghostpad for this pad !");
403 GST_DEBUG ("Removing ghostpad");
404 gst_pad_set_active (priv->ghostpad, FALSE);
405 gst_element_remove_pad (GST_ELEMENT (priv->timeline), priv->ghostpad);
406 gst_object_unref (priv->ghostpad);
407 priv->ghostpad = NULL;
412 custom_find_track (TrackPrivate * priv, GESTrack * track)
414 if (priv->track == track)
420 * ges_timeline_add_track:
421 * @timeline: a #GESTimeline
422 * @track: the #GESTrack to add
424 * Add a track to the timeline. The reference to the track will be stolen by the
427 * Returns: TRUE if the track was properly added, else FALSE.
431 ges_timeline_add_track (GESTimeline * timeline, GESTrack * track)
436 GST_DEBUG ("timeline:%p, track:%p", timeline, track);
438 /* make sure we don't already control it */
439 if (G_UNLIKELY ((tmp =
440 g_list_find_custom (timeline->tracks, (gconstpointer) track,
441 (GCompareFunc) custom_find_track)))) {
442 GST_WARNING ("Track is already controlled by this timeline");
446 /* Add the track to ourself (as a GstBin)
447 * Reference is stolen ! */
448 if (G_UNLIKELY (!gst_bin_add (GST_BIN (timeline), GST_ELEMENT (track)))) {
449 GST_WARNING ("Couldn't add track to ourself (GST)");
453 priv = g_new0 (TrackPrivate, 1);
454 priv->timeline = timeline;
457 /* Add the track to the list of tracks we track */
458 timeline->tracks = g_list_append (timeline->tracks, priv);
460 /* Listen to pad-added/-removed */
461 g_signal_connect (track, "pad-added", (GCallback) pad_added_cb, priv);
462 g_signal_connect (track, "pad-removed", (GCallback) pad_removed_cb, priv);
464 /* Inform the track that it's currently being used by ourself */
465 ges_track_set_timeline (track, timeline);
467 GST_DEBUG ("Done adding track, emitting 'track-added' signal");
469 /* emit 'track-added' */
470 g_signal_emit (timeline, ges_timeline_signals[TRACK_ADDED], 0, track);
476 * ges_timeline_remove_track:
477 * @timeline: a #GESTimeline
478 * @track: the #GESTrack to remove
480 * Remove the @track from the @timeline. The reference stolen when adding the
481 * @track will be removed. If you wish to use the @track after calling this
482 * function you must ensure that you have a reference to it.
484 * Returns: TRUE if the @track was properly removed, else FALSE.
487 ges_timeline_remove_track (GESTimeline * timeline, GESTrack * track)
492 GST_DEBUG ("timeline:%p, track:%p", timeline, track);
494 if (G_UNLIKELY (!(tmp =
495 g_list_find_custom (timeline->tracks, (gconstpointer) track,
496 (GCompareFunc) custom_find_track)))) {
497 GST_WARNING ("Track doesn't belong to this timeline");
502 timeline->tracks = g_list_remove (timeline->tracks, priv);
504 ges_track_set_timeline (track, NULL);
506 /* Remove ghost pad */
507 if (priv->ghostpad) {
508 GST_DEBUG ("Removing ghostpad");
509 gst_pad_set_active (priv->ghostpad, FALSE);
510 gst_ghost_pad_set_target ((GstGhostPad *) priv->ghostpad, NULL);
511 gst_element_remove_pad (GST_ELEMENT (timeline), priv->ghostpad);
514 /* Remove pad-added/-removed handlers */
515 g_signal_handlers_disconnect_by_func (track, pad_added_cb, priv);
516 g_signal_handlers_disconnect_by_func (track, pad_removed_cb, priv);
518 /* Signal track removal to all layers/objects */
519 g_signal_emit (timeline, ges_timeline_signals[TRACK_REMOVED], 0, track);
521 /* remove track from our bin */
522 if (G_UNLIKELY (!gst_bin_remove (GST_BIN (timeline), GST_ELEMENT (track)))) {
523 GST_WARNING ("Couldn't remove track to ourself (GST)");
533 * ges_timeline_get_track_for_pad:
534 * @timeline: The #GESTimeline
537 * Search the #GESTrack corresponding to the given @timeline's @pad.
539 * Returns: The corresponding #GESTrack if it is found, or #NULL if there is
544 ges_timeline_get_track_for_pad (GESTimeline * timeline, GstPad * pad)
548 for (tmp = timeline->tracks; tmp; tmp = g_list_next (tmp)) {
549 TrackPrivate *priv = (TrackPrivate *) tmp->data;
550 if (pad == priv->ghostpad)
558 * ges_timeline_get_tracks:
559 * @timeline: a #GESTimeline
561 * Returns the list of #GESTrack used by the Timeline.
563 * Returns: A list of #GESTrack. The caller should unref each track
564 * once he is done with them. */
566 ges_timeline_get_tracks (GESTimeline * timeline)
568 GList *tmp, *res = NULL;
570 for (tmp = timeline->tracks; tmp; tmp = g_list_next (tmp)) {
571 TrackPrivate *priv = (TrackPrivate *) tmp->data;
572 res = g_list_append (res, g_object_ref (priv->track));