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.
20 #include "gesmarshal.h"
21 #include "ges-internal.h"
22 #include "ges-timeline.h"
23 #include "ges-track.h"
24 #include "ges-timeline-layer.h"
30 * Top-level container for pipelines
32 * Contains a list of TimelineLayer which users should use to arrange the
33 * various timeline objects.
37 G_DEFINE_TYPE (GESTimeline, ges_timeline, GST_TYPE_BIN);
39 /* private structure to contain our track-related information */
43 GESTimeline *timeline;
45 GstPad *pad; /* Pad from the track */
58 static guint ges_timeline_signals[LAST_SIGNAL] = { 0 };
60 gint custom_find_track (TrackPrivate * priv, GESTrack * track);
62 ges_timeline_get_property (GObject * object, guint property_id,
63 GValue * value, GParamSpec * pspec)
65 switch (property_id) {
67 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
72 ges_timeline_set_property (GObject * object, guint property_id,
73 const GValue * value, GParamSpec * pspec)
75 switch (property_id) {
77 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
82 ges_timeline_dispose (GObject * object)
84 G_OBJECT_CLASS (ges_timeline_parent_class)->dispose (object);
88 ges_timeline_finalize (GObject * object)
90 G_OBJECT_CLASS (ges_timeline_parent_class)->finalize (object);
94 ges_timeline_class_init (GESTimelineClass * klass)
96 GObjectClass *object_class = G_OBJECT_CLASS (klass);
98 object_class->get_property = ges_timeline_get_property;
99 object_class->set_property = ges_timeline_set_property;
100 object_class->dispose = ges_timeline_dispose;
101 object_class->finalize = ges_timeline_finalize;
110 ges_timeline_signals[TRACK_ADDED] =
111 g_signal_new ("track-added", G_TYPE_FROM_CLASS (klass),
112 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, track_added), NULL,
113 NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TRACK);
115 ges_timeline_signals[TRACK_REMOVED] =
116 g_signal_new ("track-removed", G_TYPE_FROM_CLASS (klass),
117 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, track_removed),
118 NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TRACK);
120 ges_timeline_signals[LAYER_ADDED] =
121 g_signal_new ("layer-added", G_TYPE_FROM_CLASS (klass),
122 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, layer_added), NULL,
123 NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GES_TYPE_TIMELINE_LAYER);
125 ges_timeline_signals[LAYER_REMOVED] =
126 g_signal_new ("layer-removed", G_TYPE_FROM_CLASS (klass),
127 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, layer_removed),
128 NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
129 GES_TYPE_TIMELINE_LAYER);
133 ges_timeline_init (GESTimeline * self)
140 ges_timeline_new (void)
142 return g_object_new (GES_TYPE_TIMELINE, NULL);
146 ges_timeline_load_from_uri (gchar * uri)
148 /* FIXME : IMPLEMENT */
153 ges_timeline_save (GESTimeline * timeline, gchar * uri)
155 /* FIXME : IMPLEMENT */
160 layer_object_added_cb (GESTimelineLayer * layer, GESTimelineObject * object,
161 GESTimeline * timeline)
165 GST_DEBUG ("New TimelineObject %p added to layer %p", object, layer);
167 for (tmp = timeline->tracks; tmp; tmp = g_list_next (tmp)) {
168 TrackPrivate *priv = (TrackPrivate *) tmp->data;
169 GESTrack *track = priv->track;
170 GESTrackObject *trobj;
172 GST_LOG ("Trying with track %p", track);
174 if (G_UNLIKELY (!(trobj =
175 ges_timeline_object_create_track_object (object, track)))) {
176 GST_WARNING ("Couldn't create TrackObject for TimelineObject");
180 GST_LOG ("Got new TrackObject %p, adding it to track", trobj);
181 ges_track_add_object (track, trobj);
189 layer_object_removed_cb (GESTimelineLayer * layer, GESTimelineObject * object,
190 GESTimeline * timeline)
194 GST_DEBUG ("TimelineObject %p removed from layer %p", object, layer);
196 /* Go over the object's track objects and figure out which one belongs to
197 * the list of tracks we control */
199 for (tmp = object->trackobjects; tmp; tmp = g_list_next (tmp)) {
200 GESTrackObject *trobj = (GESTrackObject *) tmp->data;
202 GST_DEBUG ("Trying to remove TrackObject %p", trobj);
203 if (G_LIKELY (g_list_find_custom (timeline->tracks,
204 (gconstpointer) trobj->track,
205 (GCompareFunc) custom_find_track))) {
206 GST_DEBUG ("Belongs to one of the tracks we control");
207 ges_track_remove_object (trobj->track, trobj);
209 ges_timeline_object_release_track_object (object, trobj);
218 ges_timeline_add_layer (GESTimeline * timeline, GESTimelineLayer * layer)
220 GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
222 /* We can only add a layer that doesn't already belong to another timeline */
223 if (G_UNLIKELY (layer->timeline)) {
224 GST_WARNING ("Layer belongs to another timeline, can't add it");
228 /* Add to the list of layers, make sure we don't already control it */
229 if (G_UNLIKELY (g_list_find (timeline->layers, (gconstpointer) layer))) {
230 GST_WARNING ("Layer is already controlled by this timeline");
234 /* Reference is taken */
235 timeline->layers = g_list_append (timeline->layers, g_object_ref (layer));
237 /* Inform the layer that it belongs to a new timeline */
238 ges_timeline_layer_set_timeline (layer, timeline);
240 /* FIXME : GO OVER THE LIST OF ALREADY EXISTING TIMELINE OBJECTS IN THAT
241 * LAYER AND ADD THEM !!! */
243 /* Connect to 'object-added'/'object-removed' signal from the new layer */
244 g_signal_connect (layer, "object-added", G_CALLBACK (layer_object_added_cb),
246 g_signal_connect (layer, "object-removed",
247 G_CALLBACK (layer_object_removed_cb), timeline);
249 GST_DEBUG ("Done adding layer, emitting 'layer-added' signal");
250 g_signal_emit (timeline, ges_timeline_signals[LAYER_ADDED], 0, layer);
256 ges_timeline_remove_layer (GESTimeline * timeline, GESTimelineLayer * layer)
258 GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
260 if (G_UNLIKELY (!g_list_find (timeline->layers, layer))) {
261 GST_WARNING ("Layer doesn't belong to this timeline");
265 /* Disconnect signals */
266 GST_DEBUG ("Disconnecting signal callbacks");
267 g_signal_handlers_disconnect_by_func (layer, layer_object_added_cb, timeline);
268 g_signal_handlers_disconnect_by_func (layer, layer_object_removed_cb,
271 timeline->layers = g_list_remove (timeline->layers, layer);
273 ges_timeline_layer_set_timeline (layer, NULL);
275 g_signal_emit (timeline, ges_timeline_signals[LAYER_REMOVED], 0, layer);
277 g_object_unref (layer);
283 pad_added_cb (GESTrack * track, GstPad * pad, TrackPrivate * priv)
287 GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));
289 if (G_UNLIKELY (priv->pad)) {
290 GST_WARNING ("We are already controlling a pad for this track");
294 /* Remember the pad */
298 GST_DEBUG ("Ghosting pad and adding it to ourself");
299 padname = g_strdup_printf ("track_%p_src", track);
300 priv->ghostpad = gst_ghost_pad_new (padname, pad);
302 gst_pad_set_active (priv->ghostpad, TRUE);
303 gst_element_add_pad (GST_ELEMENT (priv->timeline), priv->ghostpad);
307 pad_removed_cb (GESTrack * track, GstPad * pad, TrackPrivate * priv)
309 GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));
311 if (G_UNLIKELY (priv->pad != pad)) {
312 GST_WARNING ("Not the pad we're controlling");
316 if (G_UNLIKELY (priv->ghostpad == NULL)) {
317 GST_WARNING ("We don't have a ghostpad for this pad !");
321 GST_DEBUG ("Removing ghostpad");
322 gst_pad_set_active (priv->ghostpad, FALSE);
323 gst_element_remove_pad (GST_ELEMENT (priv->timeline), priv->ghostpad);
324 gst_object_unref (priv->ghostpad);
325 priv->ghostpad = NULL;
330 custom_find_track (TrackPrivate * priv, GESTrack * track)
332 if (priv->track == track)
338 ges_timeline_add_track (GESTimeline * timeline, GESTrack * track)
343 GST_DEBUG ("timeline:%p, track:%p", timeline, track);
345 /* make sure we don't already control it */
346 if (G_UNLIKELY ((tmp =
347 g_list_find_custom (timeline->tracks, (gconstpointer) track,
348 (GCompareFunc) custom_find_track)))) {
349 GST_WARNING ("Track is already controlled by this timeline");
353 /* Add the track to ourself (as a GstBin)
354 * Reference is taken ! */
355 if (G_UNLIKELY (!gst_bin_add (GST_BIN (timeline), GST_ELEMENT (track)))) {
356 GST_WARNING ("Couldn't add track to ourself (GST)");
360 priv = g_new0 (TrackPrivate, 1);
361 priv->timeline = timeline;
364 /* Add the track to the list of tracks we track */
365 timeline->tracks = g_list_append (timeline->tracks, priv);
367 /* Listen to pad-added/-removed */
368 g_signal_connect (track, "pad-added", (GCallback) pad_added_cb, priv);
369 g_signal_connect (track, "pad-removed", (GCallback) pad_removed_cb, priv);
371 /* Inform the track that it's currently being used by ourself */
372 ges_track_set_timeline (track, timeline);
374 GST_DEBUG ("Done adding track, emitting 'track-added' signal");
376 /* emit 'track-added' */
377 g_signal_emit (timeline, ges_timeline_signals[TRACK_ADDED], 0, track);
383 ges_timeline_remove_track (GESTimeline * timeline, GESTrack * track)
388 GST_DEBUG ("timeline:%p, track:%p", timeline, track);
390 if (G_UNLIKELY (!(tmp =
391 g_list_find_custom (timeline->tracks, (gconstpointer) track,
392 (GCompareFunc) custom_find_track)))) {
393 GST_WARNING ("Track doesn't belong to this timeline");
398 timeline->tracks = g_list_remove (timeline->tracks, priv);
400 ges_track_set_timeline (track, NULL);
402 /* Remove ghost pad */
403 if (priv->ghostpad) {
404 GST_DEBUG ("Removing ghostpad");
405 gst_pad_set_active (priv->ghostpad, FALSE);
406 gst_ghost_pad_set_target ((GstGhostPad *) priv->ghostpad, NULL);
407 gst_element_remove_pad (GST_ELEMENT (timeline), priv->ghostpad);
410 /* Remove pad-added/-removed handlers */
411 g_signal_handlers_disconnect_by_func (track, pad_added_cb, priv);
412 g_signal_handlers_disconnect_by_func (track, pad_removed_cb, priv);
414 /* Signal track removal to all layers/objects */
415 g_signal_emit (timeline, ges_timeline_signals[TRACK_REMOVED], 0, track);
417 /* remove track from our bin */
418 if (G_UNLIKELY (!gst_bin_remove (GST_BIN (timeline), GST_ELEMENT (track)))) {
419 GST_WARNING ("Couldn't remove track to ourself (GST)");
429 * ges_timeline_get_track_for_pad:
430 * @timeline: The #GESTimeline
433 * Search the #GESTrack corresponding to the given @timeline's @pad.
435 * Returns: The corresponding #GESTrack if it is found, or #NULL if there is
440 ges_timeline_get_track_for_pad (GESTimeline * timeline, GstPad * pad)
444 for (tmp = timeline->tracks; tmp; tmp = g_list_next (tmp)) {
445 TrackPrivate *priv = (TrackPrivate *) tmp->data;
446 if (pad == priv->ghostpad)