1 /* GStreamer Editing Services
2 * Copyright (C) 2009 Edward Hervey <edward.hervey@collabora.co.uk>
3 * 2009 Nokia Corporation
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.
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.
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.
23 * @short_description: Composition of objects
25 * Corresponds to one output format (i.e. audio OR video).
27 * Contains the compatible TrackObject(s).
29 * Wraps GNonLin's 'gnlcomposition' element.
32 #include "ges-internal.h"
33 #include "ges-track.h"
34 #include "ges-track-object.h"
36 G_DEFINE_TYPE (GESTrack, ges_track, GST_TYPE_BIN);
38 struct _GESTrackPrivate
41 GESTimeline *timeline;
46 GstElement *composition; /* The composition associated with this track */
47 GstPad *srcpad; /* The source GhostPad */
57 static void pad_added_cb (GstElement * element, GstPad * pad, GESTrack * track);
60 pad_removed_cb (GstElement * element, GstPad * pad, GESTrack * track);
63 ges_track_get_property (GObject * object, guint property_id,
64 GValue * value, GParamSpec * pspec)
66 GESTrack *track = GES_TRACK (object);
68 switch (property_id) {
70 gst_value_set_caps (value, track->priv->caps);
73 g_value_set_flags (value, track->type);
76 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
81 ges_track_set_property (GObject * object, guint property_id,
82 const GValue * value, GParamSpec * pspec)
84 GESTrack *track = GES_TRACK (object);
86 switch (property_id) {
88 ges_track_set_caps (track, gst_value_get_caps (value));
91 track->type = g_value_get_flags (value);
94 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
99 ges_track_dispose (GObject * object)
101 GESTrack *track = (GESTrack *) object;
102 GESTrackPrivate *priv = track->priv;
104 while (priv->trackobjects) {
105 GESTrackObject *trobj = GES_TRACK_OBJECT (priv->trackobjects->data);
106 ges_track_remove_object (track, trobj);
107 ges_timeline_object_release_track_object ((GESTimelineObject *)
108 ges_track_object_get_timeline_object (trobj), trobj);
111 if (priv->composition) {
112 gst_bin_remove (GST_BIN (object), priv->composition);
113 priv->composition = NULL;
117 gst_caps_unref (priv->caps);
121 G_OBJECT_CLASS (ges_track_parent_class)->dispose (object);
125 ges_track_finalize (GObject * object)
127 G_OBJECT_CLASS (ges_track_parent_class)->finalize (object);
131 ges_track_class_init (GESTrackClass * klass)
133 GObjectClass *object_class = G_OBJECT_CLASS (klass);
135 g_type_class_add_private (klass, sizeof (GESTrackPrivate));
137 object_class->get_property = ges_track_get_property;
138 object_class->set_property = ges_track_set_property;
139 object_class->dispose = ges_track_dispose;
140 object_class->finalize = ges_track_finalize;
145 * Caps used to filter/choose the output stream. This is generally set to
146 * a generic set of caps like 'video/x-raw-rgb;video/x-raw-yuv' for raw video.
148 * Default value: #GST_CAPS_ANY.
150 g_object_class_install_property (object_class, ARG_CAPS,
151 g_param_spec_boxed ("caps", "Caps",
152 "Caps used to filter/choose the output stream",
153 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
156 * GESTrack:track-type
158 * Type of stream the track outputs. This is used when creating the #GESTrack
159 * to specify in generic terms what type of content will be outputted.
161 * It also serves as a 'fast' way to check what type of data will be outputted
162 * from the #GESTrack without having to actually check the #GESTrack's caps
165 g_object_class_install_property (object_class, ARG_TYPE,
166 g_param_spec_flags ("track-type", "TrackType",
167 "Type of stream the track outputs",
168 GES_TYPE_TRACK_TYPE, GES_TRACK_TYPE_CUSTOM,
169 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
173 ges_track_init (GESTrack * self)
175 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
176 GES_TYPE_TRACK, GESTrackPrivate);
178 self->priv->composition = gst_element_factory_make ("gnlcomposition", NULL);
180 g_signal_connect (self->priv->composition, "pad-added",
181 (GCallback) pad_added_cb, self);
182 g_signal_connect (self->priv->composition, "pad-removed",
183 (GCallback) pad_removed_cb, self);
185 if (!gst_bin_add (GST_BIN (self), self->priv->composition))
186 GST_ERROR ("Couldn't add composition to bin !");
191 * @type: The type of track
192 * @caps: The caps to restrict the output of the track to.
194 * Creates a new #GESTrack with the given @type and @caps.
196 * The newly created track will steal a reference to the caps. If you wish to
197 * use those caps elsewhere, you will have to take an extra reference.
199 * Returns: A new #GESTrack.
202 ges_track_new (GESTrackType type, GstCaps * caps)
206 track = g_object_new (GES_TYPE_TRACK, "caps", caps, "track-type", type, NULL);
207 gst_caps_unref (caps);
213 * ges_track_video_raw_new:
215 * Creates a new #GESTrack of type #GES_TRACK_TYPE_VIDEO and with generic
216 * raw video caps ("video/x-raw-yuv;video/x-raw-rgb");
218 * Returns: A new #GESTrack.
221 ges_track_video_raw_new (void)
224 GstCaps *caps = gst_caps_from_string ("video/x-raw-yuv;video/x-raw-rgb");
226 track = ges_track_new (GES_TRACK_TYPE_VIDEO, caps);
232 * ges_track_audio_raw_new:
234 * Creates a new #GESTrack of type #GES_TRACK_TYPE_AUDIO and with generic
235 * raw audio caps ("audio/x-raw-int;audio/x-raw-float");
237 * Returns: A new #GESTrack.
240 ges_track_audio_raw_new (void)
243 GstCaps *caps = gst_caps_from_string ("audio/x-raw-int;audio/x-raw-float");
245 track = ges_track_new (GES_TRACK_TYPE_AUDIO, caps);
251 * ges_track_set_timeline:
252 * @track: a #GESTrack
253 * @timeline: a #GESTimeline
255 * Sets @timeline as the timeline controlling @track.
258 ges_track_set_timeline (GESTrack * track, GESTimeline * timeline)
260 GST_DEBUG ("track:%p, timeline:%p", track, timeline);
262 track->priv->timeline = timeline;
266 * ges_track_set_caps:
267 * @track: a #GESTrack
268 * @caps: the #GstCaps to set
270 * Sets the given @caps on the track.
273 ges_track_set_caps (GESTrack * track, const GstCaps * caps)
275 GESTrackPrivate *priv;
277 g_return_if_fail (GES_IS_TRACK (track));
278 g_return_if_fail (GST_IS_CAPS (caps));
280 GST_DEBUG ("track:%p, caps:%" GST_PTR_FORMAT, track, caps);
285 gst_caps_unref (priv->caps);
286 priv->caps = gst_caps_copy (caps);
288 g_object_set (priv->composition, "caps", caps, NULL);
289 /* FIXME : update all trackobjects ? */
293 * ges_track_add_object:
294 * @track: a #GESTrack
295 * @object: (transfer full): the #GESTrackObject to add
297 * Adds the given object to the track. Sets the object's controlling track,
298 * and thus takes ownership of the @object.
300 * An object can only be added to one track.
302 * Returns: #TRUE if the object was properly added. #FALSE if the track does not
303 * want to accept the object.
306 ges_track_add_object (GESTrack * track, GESTrackObject * object)
308 g_return_val_if_fail (GES_IS_TRACK (track), FALSE);
309 g_return_val_if_fail (GES_IS_TRACK_OBJECT (object), FALSE);
311 GST_DEBUG ("track:%p, object:%p", track, object);
313 if (G_UNLIKELY (ges_track_object_get_track (object) != NULL)) {
314 GST_WARNING ("Object already belongs to another track");
318 /* At this point, the track object shouldn't have any gnlobject since
319 * it hasn't been added to a track yet.
320 * FIXME : This check seems a bit obsolete */
321 if (G_UNLIKELY (ges_track_object_get_gnlobject (object) != NULL)) {
322 GST_ERROR ("TrackObject already controls a gnlobject !");
326 if (G_UNLIKELY (!ges_track_object_set_track (object, track))) {
327 GST_ERROR ("Couldn't properly add the object to the Track");
331 GST_DEBUG ("Adding object to ourself");
333 if (G_UNLIKELY (!gst_bin_add (GST_BIN (track->priv->composition),
334 ges_track_object_get_gnlobject (object)))) {
335 GST_WARNING ("Couldn't add object to the GnlComposition");
339 g_object_ref_sink (object);
341 track->priv->trackobjects = g_list_append (track->priv->trackobjects, object);
347 * ges_track_remove_object:
348 * @track: a #GESTrack
349 * @object: the #GESTrackObject to remove
351 * Removes the object from the track and unparents it.
352 * Unparenting it means the reference owned by @track on the @object will be
353 * removed. If you wish to use the @object after this function, make sure you
354 * call g_object_ref() before removing it from the @track.
356 * Returns: #TRUE if the object was removed, else #FALSE if the track
357 * could not remove the object (like if it didn't belong to the track).
360 ges_track_remove_object (GESTrack * track, GESTrackObject * object)
362 GESTrackPrivate *priv;
363 GstElement *gnlobject;
365 g_return_val_if_fail (GES_IS_TRACK (track), FALSE);
366 g_return_val_if_fail (GES_IS_TRACK_OBJECT (object), FALSE);
368 GST_DEBUG ("track:%p, object:%p", track, object);
372 if (G_UNLIKELY (ges_track_object_get_track (object) != track)) {
373 GST_WARNING ("Object belongs to another track");
377 if ((gnlobject = ges_track_object_get_gnlobject (object))) {
378 GST_DEBUG ("Removing GnlObject '%s' from composition '%s'",
379 GST_ELEMENT_NAME (gnlobject), GST_ELEMENT_NAME (priv->composition));
380 if (!gst_bin_remove (GST_BIN (priv->composition), gnlobject)) {
381 GST_WARNING ("Failed to remove gnlobject from composition");
386 ges_track_object_set_track (object, NULL);
387 priv->trackobjects = g_list_remove (priv->trackobjects, object);
389 g_object_unref (object);
395 pad_added_cb (GstElement * element, GstPad * pad, GESTrack * track)
397 GESTrackPrivate *priv = track->priv;
399 GST_DEBUG ("track:%p, pad %s:%s", track, GST_DEBUG_PAD_NAME (pad));
402 priv->srcpad = gst_ghost_pad_new ("src", pad);
404 gst_pad_set_active (priv->srcpad, TRUE);
406 gst_element_add_pad (GST_ELEMENT (track), priv->srcpad);
412 pad_removed_cb (GstElement * element, GstPad * pad, GESTrack * track)
414 GESTrackPrivate *priv = track->priv;
416 GST_DEBUG ("track:%p, pad %s:%s", track, GST_DEBUG_PAD_NAME (pad));
418 if (G_LIKELY (priv->srcpad)) {
419 gst_pad_set_active (priv->srcpad, FALSE);
420 gst_element_remove_pad (GST_ELEMENT (track), priv->srcpad);
428 * ges_track_get_caps:
429 * @track: a #GESTrack
431 * Get the #GstCaps this track is configured to output.
433 * Returns: The #GstCaps this track is configured to output.
436 ges_track_get_caps (GESTrack * track)
438 g_return_val_if_fail (GES_IS_TRACK (track), NULL);
440 return track->priv->caps;
444 * ges_track_get_timeline:
445 * @track: a #GESTrack
447 * Get the #GESTimeline this track belongs to. Can be %NULL.
449 * Returns: The #GESTimeline this track belongs to. Can be %NULL.
452 ges_track_get_timeline (GESTrack * track)
454 g_return_val_if_fail (GES_IS_TRACK (track), NULL);
456 return track->priv->timeline;