1 /* GStreamer Editing Services
2 * Copyright (C) 2009 Edward Hervey <bilboed@bilboed.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * SECTION:ges-timeline
22 * @short_description: Multimedia timeline
24 * #GESTimeline is the central object for any multimedia timeline.
26 * Contains a list of #GESTimelineLayer which users should use to arrange the
27 * various timeline objects through time.
29 * The output type is determined by the #GESTimelineTrack that are set on
33 #include "gesmarshal.h"
34 #include "ges-internal.h"
35 #include "ges-timeline.h"
36 #include "ges-track.h"
37 #include "ges-timeline-layer.h"
41 G_DEFINE_TYPE (GESTimeline, ges_timeline, GST_TYPE_BIN);
43 /* private structure to contain our track-related information */
47 GESTimeline *timeline;
49 GstPad *pad; /* Pad from the track */
62 static guint ges_timeline_signals[LAST_SIGNAL] = { 0 };
64 gint custom_find_track (TrackPrivate * priv, GESTrack * track);
66 ges_timeline_get_property (GObject * object, guint property_id,
67 GValue * value, GParamSpec * pspec)
69 switch (property_id) {
71 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
76 ges_timeline_set_property (GObject * object, guint property_id,
77 const GValue * value, GParamSpec * pspec)
79 switch (property_id) {
81 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
86 ges_timeline_dispose (GObject * object)
88 G_OBJECT_CLASS (ges_timeline_parent_class)->dispose (object);
92 ges_timeline_finalize (GObject * object)
94 G_OBJECT_CLASS (ges_timeline_parent_class)->finalize (object);
98 ges_timeline_class_init (GESTimelineClass * klass)
100 GObjectClass *object_class = G_OBJECT_CLASS (klass);
102 object_class->get_property = ges_timeline_get_property;
103 object_class->set_property = ges_timeline_set_property;
104 object_class->dispose = ges_timeline_dispose;
105 object_class->finalize = ges_timeline_finalize;
114 ges_timeline_signals[TRACK_ADDED] =
115 g_signal_new ("track-added", G_TYPE_FROM_CLASS (klass),
116 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, track_added), NULL,
117 NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TRACK);
119 ges_timeline_signals[TRACK_REMOVED] =
120 g_signal_new ("track-removed", G_TYPE_FROM_CLASS (klass),
121 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, track_removed),
122 NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TRACK);
124 ges_timeline_signals[LAYER_ADDED] =
125 g_signal_new ("layer-added", G_TYPE_FROM_CLASS (klass),
126 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, layer_added), NULL,
127 NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TIMELINE_LAYER);
129 ges_timeline_signals[LAYER_REMOVED] =
130 g_signal_new ("layer-removed", G_TYPE_FROM_CLASS (klass),
131 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, layer_removed),
132 NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
133 GES_TYPE_TIMELINE_LAYER);
137 ges_timeline_init (GESTimeline * self)
144 ges_timeline_new (void)
146 return g_object_new (GES_TYPE_TIMELINE, NULL);
150 ges_timeline_load_from_uri (gchar * uri)
152 /* FIXME : IMPLEMENT */
157 ges_timeline_save (GESTimeline * timeline, gchar * uri)
159 /* FIXME : IMPLEMENT */
164 layer_object_added_cb (GESTimelineLayer * layer, GESTimelineObject * object,
165 GESTimeline * timeline)
169 GST_DEBUG ("New TimelineObject %p added to layer %p", object, layer);
171 for (tmp = timeline->tracks; tmp; tmp = g_list_next (tmp)) {
172 TrackPrivate *priv = (TrackPrivate *) tmp->data;
173 GESTrack *track = priv->track;
174 GESTrackObject *trobj;
176 GST_LOG ("Trying with track %p", track);
178 if (G_UNLIKELY (!(trobj =
179 ges_timeline_object_create_track_object (object, track)))) {
180 GST_WARNING ("Couldn't create TrackObject for TimelineObject");
184 GST_LOG ("Got new TrackObject %p, adding it to track", trobj);
185 ges_track_add_object (track, trobj);
193 layer_object_removed_cb (GESTimelineLayer * layer, GESTimelineObject * object,
194 GESTimeline * timeline)
198 GST_DEBUG ("TimelineObject %p removed from layer %p", object, layer);
200 /* Go over the object's track objects and figure out which one belongs to
201 * the list of tracks we control */
203 for (tmp = object->trackobjects; tmp; tmp = g_list_next (tmp)) {
204 GESTrackObject *trobj = (GESTrackObject *) tmp->data;
206 GST_DEBUG ("Trying to remove TrackObject %p", trobj);
207 if (G_LIKELY (g_list_find_custom (timeline->tracks,
208 (gconstpointer) trobj->track,
209 (GCompareFunc) custom_find_track))) {
210 GST_DEBUG ("Belongs to one of the tracks we control");
211 ges_track_remove_object (trobj->track, trobj);
213 ges_timeline_object_release_track_object (object, trobj);
221 * ges_timeline_add_layer:
222 * @timeline: a #GESTimeline
223 * @layer: the #GESTimelineLayer to add
225 * Add the layer to the timeline. The reference to the @layer will be stolen
228 * Returns: TRUE if the layer was properly added, else FALSE.
231 ges_timeline_add_layer (GESTimeline * timeline, GESTimelineLayer * layer)
233 GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
235 /* We can only add a layer that doesn't already belong to another timeline */
236 if (G_UNLIKELY (layer->timeline)) {
237 GST_WARNING ("Layer belongs to another timeline, can't add it");
241 /* Add to the list of layers, make sure we don't already control it */
242 if (G_UNLIKELY (g_list_find (timeline->layers, (gconstpointer) layer))) {
243 GST_WARNING ("Layer is already controlled by this timeline");
247 /* Reference is stolen */
248 timeline->layers = g_list_append (timeline->layers, layer);
250 /* Inform the layer that it belongs to a new timeline */
251 ges_timeline_layer_set_timeline (layer, timeline);
253 /* FIXME : GO OVER THE LIST OF ALREADY EXISTING TIMELINE OBJECTS IN THAT
254 * LAYER AND ADD THEM !!! */
256 /* Connect to 'object-added'/'object-removed' signal from the new layer */
257 g_signal_connect (layer, "object-added", G_CALLBACK (layer_object_added_cb),
259 g_signal_connect (layer, "object-removed",
260 G_CALLBACK (layer_object_removed_cb), timeline);
262 GST_DEBUG ("Done adding layer, emitting 'layer-added' signal");
263 g_signal_emit (timeline, ges_timeline_signals[LAYER_ADDED], 0, layer);
269 ges_timeline_remove_layer (GESTimeline * timeline, GESTimelineLayer * layer)
271 GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
273 if (G_UNLIKELY (!g_list_find (timeline->layers, layer))) {
274 GST_WARNING ("Layer doesn't belong to this timeline");
278 /* Disconnect signals */
279 GST_DEBUG ("Disconnecting signal callbacks");
280 g_signal_handlers_disconnect_by_func (layer, layer_object_added_cb, timeline);
281 g_signal_handlers_disconnect_by_func (layer, layer_object_removed_cb,
284 timeline->layers = g_list_remove (timeline->layers, layer);
286 ges_timeline_layer_set_timeline (layer, NULL);
288 g_signal_emit (timeline, ges_timeline_signals[LAYER_REMOVED], 0, layer);
290 g_object_unref (layer);
296 pad_added_cb (GESTrack * track, GstPad * pad, TrackPrivate * priv)
300 GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));
302 if (G_UNLIKELY (priv->pad)) {
303 GST_WARNING ("We are already controlling a pad for this track");
307 /* Remember the pad */
311 GST_DEBUG ("Ghosting pad and adding it to ourself");
312 padname = g_strdup_printf ("track_%p_src", track);
313 priv->ghostpad = gst_ghost_pad_new (padname, pad);
315 gst_pad_set_active (priv->ghostpad, TRUE);
316 gst_element_add_pad (GST_ELEMENT (priv->timeline), priv->ghostpad);
320 pad_removed_cb (GESTrack * track, GstPad * pad, TrackPrivate * priv)
322 GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));
324 if (G_UNLIKELY (priv->pad != pad)) {
325 GST_WARNING ("Not the pad we're controlling");
329 if (G_UNLIKELY (priv->ghostpad == NULL)) {
330 GST_WARNING ("We don't have a ghostpad for this pad !");
334 GST_DEBUG ("Removing ghostpad");
335 gst_pad_set_active (priv->ghostpad, FALSE);
336 gst_element_remove_pad (GST_ELEMENT (priv->timeline), priv->ghostpad);
337 gst_object_unref (priv->ghostpad);
338 priv->ghostpad = NULL;
343 custom_find_track (TrackPrivate * priv, GESTrack * track)
345 if (priv->track == track)
351 * ges_timeline_add_track:
352 * @timeline: a #GESTimeline
353 * @track: the #GESTrack to add
355 * Add a track to the timeline. The reference to the track will be stolen by the
358 * Returns: TRUE if the track was properly added, else FALSE.
362 ges_timeline_add_track (GESTimeline * timeline, GESTrack * track)
367 GST_DEBUG ("timeline:%p, track:%p", timeline, track);
369 /* make sure we don't already control it */
370 if (G_UNLIKELY ((tmp =
371 g_list_find_custom (timeline->tracks, (gconstpointer) track,
372 (GCompareFunc) custom_find_track)))) {
373 GST_WARNING ("Track is already controlled by this timeline");
377 /* Add the track to ourself (as a GstBin)
378 * Reference is stolen ! */
379 if (G_UNLIKELY (!gst_bin_add (GST_BIN (timeline), GST_ELEMENT (track)))) {
380 GST_WARNING ("Couldn't add track to ourself (GST)");
384 priv = g_new0 (TrackPrivate, 1);
385 priv->timeline = timeline;
388 /* Add the track to the list of tracks we track */
389 timeline->tracks = g_list_append (timeline->tracks, priv);
391 /* Listen to pad-added/-removed */
392 g_signal_connect (track, "pad-added", (GCallback) pad_added_cb, priv);
393 g_signal_connect (track, "pad-removed", (GCallback) pad_removed_cb, priv);
395 /* Inform the track that it's currently being used by ourself */
396 ges_track_set_timeline (track, timeline);
398 GST_DEBUG ("Done adding track, emitting 'track-added' signal");
400 /* emit 'track-added' */
401 g_signal_emit (timeline, ges_timeline_signals[TRACK_ADDED], 0, track);
407 * ges_timeline_remove_track:
408 * @timeline: a #GESTimeline
409 * @track: the #GESTrack to remove
411 * Remove the @track from the @timeline. The reference stolen when adding the
412 * @track will be removed. If you wish to use the @track after calling this
413 * function you must ensure that you have a reference to it.
415 * Returns: TRUE if the @track was properly removed, else FALSE.
418 ges_timeline_remove_track (GESTimeline * timeline, GESTrack * track)
423 GST_DEBUG ("timeline:%p, track:%p", timeline, track);
425 if (G_UNLIKELY (!(tmp =
426 g_list_find_custom (timeline->tracks, (gconstpointer) track,
427 (GCompareFunc) custom_find_track)))) {
428 GST_WARNING ("Track doesn't belong to this timeline");
433 timeline->tracks = g_list_remove (timeline->tracks, priv);
435 ges_track_set_timeline (track, NULL);
437 /* Remove ghost pad */
438 if (priv->ghostpad) {
439 GST_DEBUG ("Removing ghostpad");
440 gst_pad_set_active (priv->ghostpad, FALSE);
441 gst_ghost_pad_set_target ((GstGhostPad *) priv->ghostpad, NULL);
442 gst_element_remove_pad (GST_ELEMENT (timeline), priv->ghostpad);
445 /* Remove pad-added/-removed handlers */
446 g_signal_handlers_disconnect_by_func (track, pad_added_cb, priv);
447 g_signal_handlers_disconnect_by_func (track, pad_removed_cb, priv);
449 /* Signal track removal to all layers/objects */
450 g_signal_emit (timeline, ges_timeline_signals[TRACK_REMOVED], 0, track);
452 /* remove track from our bin */
453 if (G_UNLIKELY (!gst_bin_remove (GST_BIN (timeline), GST_ELEMENT (track)))) {
454 GST_WARNING ("Couldn't remove track to ourself (GST)");
464 * ges_timeline_get_track_for_pad:
465 * @timeline: The #GESTimeline
468 * Search the #GESTrack corresponding to the given @timeline's @pad.
470 * Returns: The corresponding #GESTrack if it is found, or #NULL if there is
475 ges_timeline_get_track_for_pad (GESTimeline * timeline, GstPad * pad)
479 for (tmp = timeline->tracks; tmp; tmp = g_list_next (tmp)) {
480 TrackPrivate *priv = (TrackPrivate *) tmp->data;
481 if (pad == priv->ghostpad)