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