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