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