GESTrack: keep track of trackobjects and remove/release them in dispose
[platform/upstream/gstreamer.git] / ges / ges-track.c
1 /* GStreamer Editing Services
2  * Copyright (C) 2009 Edward Hervey <edward.hervey@collabora.co.uk>
3  *               2009 Nokia Corporation
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 /**
22  * SECTION:ges-track
23  * @short_description: Composition of objects
24  *
25  * Corresponds to one output format (i.e. audio OR video).
26  *
27  * Contains the compatible TrackObject(s).
28  *
29  * Wraps GNonLin's 'gnlcomposition' element.
30  */
31
32 #include "ges-internal.h"
33 #include "ges-track.h"
34 #include "ges-track-object.h"
35
36 G_DEFINE_TYPE (GESTrack, ges_track, GST_TYPE_BIN);
37
38 enum
39 {
40   ARG_0,
41   ARG_CAPS,
42   ARG_TYPE
43 };
44
45 static void pad_added_cb (GstElement * element, GstPad * pad, GESTrack * track);
46
47 static void
48 pad_removed_cb (GstElement * element, GstPad * pad, GESTrack * track);
49
50 static void
51 ges_track_get_property (GObject * object, guint property_id,
52     GValue * value, GParamSpec * pspec)
53 {
54   GESTrack *track = GES_TRACK (object);
55
56   switch (property_id) {
57     case ARG_CAPS:
58       gst_value_set_caps (value, track->caps);
59       break;
60     case ARG_TYPE:
61       g_value_set_flags (value, track->type);
62       break;
63     default:
64       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
65   }
66 }
67
68 static void
69 ges_track_set_property (GObject * object, guint property_id,
70     const GValue * value, GParamSpec * pspec)
71 {
72   GESTrack *track = GES_TRACK (object);
73
74   switch (property_id) {
75     case ARG_CAPS:
76       ges_track_set_caps (track, gst_value_get_caps (value));
77       break;
78     case ARG_TYPE:
79       track->type = g_value_get_flags (value);
80       break;
81     default:
82       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
83   }
84 }
85
86 static void
87 ges_track_dispose (GObject * object)
88 {
89   GESTrack *track = (GESTrack *) object;
90
91   while (track->trackobjects) {
92     GESTrackObject *trobj = GES_TRACK_OBJECT (track->trackobjects->data);
93     ges_track_remove_object (track, trobj);
94     ges_timeline_object_release_track_object (trobj->timelineobj, trobj);
95   }
96
97   if (track->composition) {
98     gst_bin_remove (GST_BIN (object), track->composition);
99     track->composition = NULL;
100   }
101
102   if (track->caps) {
103     gst_caps_unref (track->caps);
104     track->caps = NULL;
105   }
106
107   G_OBJECT_CLASS (ges_track_parent_class)->dispose (object);
108 }
109
110 static void
111 ges_track_finalize (GObject * object)
112 {
113   G_OBJECT_CLASS (ges_track_parent_class)->finalize (object);
114 }
115
116 static void
117 ges_track_class_init (GESTrackClass * klass)
118 {
119   GObjectClass *object_class = G_OBJECT_CLASS (klass);
120
121   object_class->get_property = ges_track_get_property;
122   object_class->set_property = ges_track_set_property;
123   object_class->dispose = ges_track_dispose;
124   object_class->finalize = ges_track_finalize;
125
126   /**
127    * GESTrack:caps
128    *
129    * Caps used to filter/choose the output stream. This is generally set to
130    * a generic set of caps like 'video/x-raw-rgb;video/x-raw-yuv' for raw video.
131    *
132    * Default value: #GST_CAPS_ANY.
133    */
134   g_object_class_install_property (object_class, ARG_CAPS,
135       g_param_spec_boxed ("caps", "Caps",
136           "Caps used to filter/choose the output stream",
137           GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
138
139   /**
140    * GESTrack:track-type
141    *
142    * Type of stream the track outputs. This is used when creating the #GESTrack
143    * to specify in generic terms what type of content will be outputted.
144    *
145    * It also serves as a 'fast' way to check what type of data will be outputted
146    * from the #GESTrack without having to actually check the #GESTrack's caps
147    * property.
148    */
149   g_object_class_install_property (object_class, ARG_TYPE,
150       g_param_spec_flags ("track-type", "TrackType",
151           "Type of stream the track outputs",
152           GES_TYPE_TRACK_TYPE, GES_TRACK_TYPE_CUSTOM,
153           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
154 }
155
156 static void
157 ges_track_init (GESTrack * self)
158 {
159   self->composition = gst_element_factory_make ("gnlcomposition", NULL);
160
161   g_signal_connect (self->composition, "pad-added", (GCallback) pad_added_cb,
162       self);
163   g_signal_connect (self->composition, "pad-removed",
164       (GCallback) pad_removed_cb, self);
165
166   if (!gst_bin_add (GST_BIN (self), self->composition))
167     GST_ERROR ("Couldn't add composition to bin !");
168 }
169
170 /**
171  * ges_track_new:
172  * @type: The type of track
173  * @caps: The caps to restrict the output of the track to.
174  *
175  * Creates a new #GESTrack with the given @type and @caps.
176  *
177  * The newly created track will steal a reference to the caps. If you wish to 
178  * use those caps elsewhere, you will have to take an extra reference.
179  *
180  * Returns: A new #GESTrack.
181  */
182 GESTrack *
183 ges_track_new (GESTrackType type, GstCaps * caps)
184 {
185   GESTrack *track;
186
187   track = g_object_new (GES_TYPE_TRACK, "caps", caps, "track-type", type, NULL);
188   gst_caps_unref (caps);
189
190   return track;
191 }
192
193 /**
194  * ges_track_video_raw_new:
195  *
196  * Creates a new #GESTrack of type #GES_TRACK_TYPE_VIDEO and with generic
197  * raw video caps ("video/x-raw-yuv;video/x-raw-rgb");
198  *
199  * Returns: A new #GESTrack.
200  */
201 GESTrack *
202 ges_track_video_raw_new (void)
203 {
204   GESTrack *track;
205   GstCaps *caps = gst_caps_from_string ("video/x-raw-yuv;video/x-raw-rgb");
206
207   track = ges_track_new (GES_TRACK_TYPE_VIDEO, caps);
208
209   return track;
210 }
211
212 /**
213  * ges_track_audio_raw_new:
214  *
215  * Creates a new #GESTrack of type #GES_TRACK_TYPE_AUDIO and with generic
216  * raw audio caps ("audio/x-raw-int;audio/x-raw-float");
217  *
218  * Returns: A new #GESTrack.
219  */
220 GESTrack *
221 ges_track_audio_raw_new (void)
222 {
223   GESTrack *track;
224   GstCaps *caps = gst_caps_from_string ("audio/x-raw-int;audio/x-raw-float");
225
226   track = ges_track_new (GES_TRACK_TYPE_AUDIO, caps);
227
228   return track;
229 }
230
231 void
232 ges_track_set_timeline (GESTrack * track, GESTimeline * timeline)
233 {
234   GST_DEBUG ("track:%p, timeline:%p", track, timeline);
235
236   track->timeline = timeline;
237 }
238
239 /**
240  * ges_track_set_caps:
241  * @track: a #GESTrack
242  * @caps: the #GstCaps to set
243  *
244  * Sets the given @caps on the track.
245  */
246 void
247 ges_track_set_caps (GESTrack * track, const GstCaps * caps)
248 {
249   GST_DEBUG ("track:%p, caps:%" GST_PTR_FORMAT, track, caps);
250
251   g_return_if_fail (GST_IS_CAPS (caps));
252
253   if (track->caps)
254     gst_caps_unref (track->caps);
255   track->caps = gst_caps_copy (caps);
256
257   g_object_set (track->composition, "caps", caps, NULL);
258   /* FIXME : update all trackobjects ? */
259 }
260
261 /**
262  * ges_track_add_object:
263  * @track: a #GESTrack
264  * @object: the #GESTrackObject to add
265  *
266  * Adds the given object to the track.
267  *
268  * Returns: #TRUE if the object was properly added. #FALSE if the track does not
269  * want to accept the object.
270  */
271 gboolean
272 ges_track_add_object (GESTrack * track, GESTrackObject * object)
273 {
274   GST_DEBUG ("track:%p, object:%p", track, object);
275
276   if (G_UNLIKELY (object->track != NULL)) {
277     GST_WARNING ("Object already belongs to another track");
278     return FALSE;
279   }
280
281   if (G_UNLIKELY (object->gnlobject != NULL)) {
282     GST_ERROR ("TrackObject doesn't have a gnlobject !");
283     return FALSE;
284   }
285
286   if (G_UNLIKELY (!ges_track_object_set_track (object, track))) {
287     GST_ERROR ("Couldn't properly add the object to the Track");
288     return FALSE;
289   }
290
291   GST_DEBUG ("Adding object to ourself");
292
293   /* make sure the object has a valid gnlobject ! */
294   if (G_UNLIKELY (!gst_bin_add (GST_BIN (track->composition),
295               object->gnlobject))) {
296     GST_WARNING ("Couldn't add object to the GnlComposition");
297     return FALSE;
298   }
299
300   track->trackobjects = g_list_append (track->trackobjects, object);
301
302   return TRUE;
303 }
304
305 /**
306  * ges_track_remove_object:
307  * @track: a #GESTrack
308  * @object: the #GESTrackObject to remove
309  *
310  * Removes the object from the track.
311  *
312  * Returns: #TRUE if the object was removed, else #FALSE if the track
313  * could not remove the object (like if it didn't belong to the track).
314  */
315 gboolean
316 ges_track_remove_object (GESTrack * track, GESTrackObject * object)
317 {
318   GST_DEBUG ("track:%p, object:%p", track, object);
319
320   if (G_UNLIKELY (object->track != track)) {
321     GST_WARNING ("Object belongs to another track");
322     return FALSE;
323   }
324
325   if (G_LIKELY (object->gnlobject != NULL)) {
326     GST_DEBUG ("Removing GnlObject from composition");
327     if (!gst_bin_remove (GST_BIN (track->composition), object->gnlobject)) {
328       GST_WARNING ("Failed to remove gnlobject from composition");
329       return FALSE;
330     }
331   }
332
333   ges_track_object_set_track (object, NULL);
334   track->trackobjects = g_list_remove (track->trackobjects, object);
335
336   return TRUE;
337 }
338
339 static void
340 pad_added_cb (GstElement * element, GstPad * pad, GESTrack * track)
341 {
342   GST_DEBUG ("track:%p, pad %s:%s", track, GST_DEBUG_PAD_NAME (pad));
343
344   /* ghost the pad */
345   track->srcpad = gst_ghost_pad_new ("src", pad);
346
347   gst_pad_set_active (track->srcpad, TRUE);
348
349   gst_element_add_pad (GST_ELEMENT (track), track->srcpad);
350
351   GST_DEBUG ("done");
352 }
353
354 static void
355 pad_removed_cb (GstElement * element, GstPad * pad, GESTrack * track)
356 {
357   GST_DEBUG ("track:%p, pad %s:%s", track, GST_DEBUG_PAD_NAME (pad));
358
359   if (G_LIKELY (track->srcpad)) {
360     gst_pad_set_active (track->srcpad, FALSE);
361     gst_element_remove_pad (GST_ELEMENT (track), track->srcpad);
362     track->srcpad = NULL;
363   }
364
365   GST_DEBUG ("done");
366 }