GESTimeline: Make sure added ghostpads are unique.
[platform/upstream/gstreamer.git] / ges / ges-timeline.c
1 /* GStreamer Editing Services
2  * Copyright (C) 2009 Edward Hervey <bilboed@bilboed.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
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"
25 #include "ges.h"
26
27 /**
28  * GESTimelinePipeline
29  *
30  * Top-level container for pipelines
31  * 
32  * Contains a list of TimelineLayer which users should use to arrange the
33  * various timeline objects.
34  *
35  */
36
37 G_DEFINE_TYPE (GESTimeline, ges_timeline, GST_TYPE_BIN);
38
39 /* private structure to contain our track-related information */
40
41 typedef struct
42 {
43   GESTimeline *timeline;
44   GESTrack *track;
45   GstPad *pad;                  /* Pad from the track */
46   GstPad *ghostpad;
47 } TrackPrivate;
48
49 enum
50 {
51   TRACK_ADDED,
52   TRACK_REMOVED,
53   LAYER_ADDED,
54   LAYER_REMOVED,
55   LAST_SIGNAL
56 };
57
58 static guint ges_timeline_signals[LAST_SIGNAL] = { 0 };
59
60 gint custom_find_track (TrackPrivate * priv, GESTrack * track);
61 static void
62 ges_timeline_get_property (GObject * object, guint property_id,
63     GValue * value, GParamSpec * pspec)
64 {
65   switch (property_id) {
66     default:
67       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
68   }
69 }
70
71 static void
72 ges_timeline_set_property (GObject * object, guint property_id,
73     const GValue * value, GParamSpec * pspec)
74 {
75   switch (property_id) {
76     default:
77       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
78   }
79 }
80
81 static void
82 ges_timeline_dispose (GObject * object)
83 {
84   G_OBJECT_CLASS (ges_timeline_parent_class)->dispose (object);
85 }
86
87 static void
88 ges_timeline_finalize (GObject * object)
89 {
90   G_OBJECT_CLASS (ges_timeline_parent_class)->finalize (object);
91 }
92
93 static void
94 ges_timeline_class_init (GESTimelineClass * klass)
95 {
96   GObjectClass *object_class = G_OBJECT_CLASS (klass);
97
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;
102
103   /* Signals
104    * 'track-added'
105    * 'track-removed'
106    * 'layer-added'
107    * 'layer-removed'
108    */
109
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);
114
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);
119
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);
124
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);
130 }
131
132 static void
133 ges_timeline_init (GESTimeline * self)
134 {
135   self->layers = NULL;
136   self->tracks = NULL;
137 }
138
139 GESTimeline *
140 ges_timeline_new (void)
141 {
142   return g_object_new (GES_TYPE_TIMELINE, NULL);
143 }
144
145 GESTimeline *
146 ges_timeline_load_from_uri (gchar * uri)
147 {
148   /* FIXME : IMPLEMENT */
149   return NULL;
150 }
151
152 gboolean
153 ges_timeline_save (GESTimeline * timeline, gchar * uri)
154 {
155   /* FIXME : IMPLEMENT */
156   return FALSE;
157 }
158
159 static void
160 layer_object_added_cb (GESTimelineLayer * layer, GESTimelineObject * object,
161     GESTimeline * timeline)
162 {
163   GList *tmp;
164
165   GST_DEBUG ("New TimelineObject %p added to layer %p", object, layer);
166
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;
171
172     GST_LOG ("Trying with track %p", track);
173
174     if (G_UNLIKELY (!(trobj =
175                 ges_timeline_object_create_track_object (object, track)))) {
176       GST_WARNING ("Couldn't create TrackObject for TimelineObject");
177       continue;
178     }
179
180     GST_LOG ("Got new TrackObject %p, adding it to track", trobj);
181     ges_track_add_object (track, trobj);
182   }
183
184   GST_DEBUG ("done");
185 }
186
187
188 static void
189 layer_object_removed_cb (GESTimelineLayer * layer, GESTimelineObject * object,
190     GESTimeline * timeline)
191 {
192   GList *tmp;
193
194   GST_DEBUG ("TimelineObject %p removed from layer %p", object, layer);
195
196   /* Go over the object's track objects and figure out which one belongs to
197    * the list of tracks we control */
198
199   for (tmp = object->trackobjects; tmp; tmp = g_list_next (tmp)) {
200     GESTrackObject *trobj = (GESTrackObject *) tmp->data;
201
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);
208
209       ges_timeline_object_release_track_object (object, trobj);
210     }
211   }
212
213   GST_DEBUG ("Done");
214 }
215
216
217 gboolean
218 ges_timeline_add_layer (GESTimeline * timeline, GESTimelineLayer * layer)
219 {
220   GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
221
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");
225     return FALSE;
226   }
227
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");
231     return FALSE;
232   }
233
234   /* Reference is taken */
235   timeline->layers = g_list_append (timeline->layers, g_object_ref (layer));
236
237   /* Inform the layer that it belongs to a new timeline */
238   ges_timeline_layer_set_timeline (layer, timeline);
239
240   /* FIXME : GO OVER THE LIST OF ALREADY EXISTING TIMELINE OBJECTS IN THAT
241    * LAYER AND ADD THEM !!! */
242
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),
245       timeline);
246   g_signal_connect (layer, "object-removed",
247       G_CALLBACK (layer_object_removed_cb), timeline);
248
249   GST_DEBUG ("Done adding layer, emitting 'layer-added' signal");
250   g_signal_emit (timeline, ges_timeline_signals[LAYER_ADDED], 0, layer);
251
252   return TRUE;
253 }
254
255 gboolean
256 ges_timeline_remove_layer (GESTimeline * timeline, GESTimelineLayer * layer)
257 {
258   GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);
259
260   if (G_UNLIKELY (!g_list_find (timeline->layers, layer))) {
261     GST_WARNING ("Layer doesn't belong to this timeline");
262     return FALSE;
263   }
264
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,
269       timeline);
270
271   timeline->layers = g_list_remove (timeline->layers, layer);
272
273   ges_timeline_layer_set_timeline (layer, NULL);
274
275   g_signal_emit (timeline, ges_timeline_signals[LAYER_REMOVED], 0, layer);
276
277   g_object_unref (layer);
278
279   return TRUE;
280 }
281
282 static void
283 pad_added_cb (GESTrack * track, GstPad * pad, TrackPrivate * priv)
284 {
285   gchar *padname;
286
287   GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));
288
289   if (G_UNLIKELY (priv->pad)) {
290     GST_WARNING ("We are already controlling a pad for this track");
291     return;
292   }
293
294   /* Remember the pad */
295   priv->pad = pad;
296
297   /* ghost it ! */
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);
301   g_free (padname);
302   gst_pad_set_active (priv->ghostpad, TRUE);
303   gst_element_add_pad (GST_ELEMENT (priv->timeline), priv->ghostpad);
304 }
305
306 static void
307 pad_removed_cb (GESTrack * track, GstPad * pad, TrackPrivate * priv)
308 {
309   GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));
310
311   if (G_UNLIKELY (priv->pad != pad)) {
312     GST_WARNING ("Not the pad we're controlling");
313     return;
314   }
315
316   if (G_UNLIKELY (priv->ghostpad == NULL)) {
317     GST_WARNING ("We don't have a ghostpad for this pad !");
318     return;
319   }
320
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;
326   priv->pad = NULL;
327 }
328
329 gint
330 custom_find_track (TrackPrivate * priv, GESTrack * track)
331 {
332   if (priv->track == track)
333     return 0;
334   return -1;
335 }
336
337 gboolean
338 ges_timeline_add_track (GESTimeline * timeline, GESTrack * track)
339 {
340   GList *tmp;
341   TrackPrivate *priv;
342
343   GST_DEBUG ("timeline:%p, track:%p", timeline, track);
344
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");
350     return FALSE;
351   }
352
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)");
357     return FALSE;
358   }
359
360   priv = g_new0 (TrackPrivate, 1);
361   priv->timeline = timeline;
362   priv->track = track;
363
364   /* Add the track to the list of tracks we track */
365   timeline->tracks = g_list_append (timeline->tracks, priv);
366
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);
370
371   /* Inform the track that it's currently being used by ourself */
372   ges_track_set_timeline (track, timeline);
373
374   GST_DEBUG ("Done adding track, emitting 'track-added' signal");
375
376   /* emit 'track-added' */
377   g_signal_emit (timeline, ges_timeline_signals[TRACK_ADDED], 0, track);
378
379   return TRUE;
380 }
381
382 gboolean
383 ges_timeline_remove_track (GESTimeline * timeline, GESTrack * track)
384 {
385   GList *tmp;
386   TrackPrivate *priv;
387
388   GST_DEBUG ("timeline:%p, track:%p", timeline, track);
389
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");
394     return FALSE;
395   }
396
397   priv = tmp->data;
398   timeline->tracks = g_list_remove (timeline->tracks, priv);
399
400   ges_track_set_timeline (track, NULL);
401
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);
408   }
409
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);
413
414   /* Signal track removal to all layers/objects */
415   g_signal_emit (timeline, ges_timeline_signals[TRACK_REMOVED], 0, track);
416
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)");
420     return FALSE;
421   }
422
423   g_free (priv);
424
425   return TRUE;
426 }
427
428 /**
429  * ges_timeline_get_track_for_pad:
430  * @timeline: The #GESTimeline
431  * @pad: The #GstPad
432  *
433  * Search the #GESTrack corresponding to the given @timeline's @pad.
434  *
435  * Returns: The corresponding #GESTrack if it is found, or #NULL if there is
436  * an error.
437  */
438
439 GESTrack *
440 ges_timeline_get_track_for_pad (GESTimeline * timeline, GstPad * pad)
441 {
442   GList *tmp;
443
444   for (tmp = timeline->tracks; tmp; tmp = g_list_next (tmp)) {
445     TrackPrivate *priv = (TrackPrivate *) tmp->data;
446     if (pad == priv->ghostpad)
447       return priv->track;
448   }
449
450   return NULL;
451 }