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;
47 GstElement *composition; /* The composition associated with this track */
48 GstElement *background; /* The backgrond, handle the gaps in the track */
49 GstPad *srcpad; /* The source GhostPad */
64 static guint ges_track_signals[LAST_SIGNAL] = { 0 };
66 static GParamSpec *properties[ARG_LAST];
68 static void pad_added_cb (GstElement * element, GstPad * pad, GESTrack * track);
70 pad_removed_cb (GstElement * element, GstPad * pad, GESTrack * track);
71 static void composition_duration_cb (GstElement * composition, GParamSpec * arg
72 G_GNUC_UNUSED, GESTrack * obj);
74 sort_track_objects_cb (GESTrackObject * child,
75 GParamSpec * arg G_GNUC_UNUSED, GESTrack * track);
77 static void timeline_duration_cb (GESTimeline * timeline,
78 GParamSpec * arg G_GNUC_UNUSED, GESTrack * track);
81 ges_track_get_property (GObject * object, guint property_id,
82 GValue * value, GParamSpec * pspec)
84 GESTrack *track = GES_TRACK (object);
86 switch (property_id) {
88 gst_value_set_caps (value, track->priv->caps);
91 g_value_set_flags (value, track->type);
94 g_value_set_uint64 (value, track->priv->duration);
97 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
102 ges_track_set_property (GObject * object, guint property_id,
103 const GValue * value, GParamSpec * pspec)
105 GESTrack *track = GES_TRACK (object);
107 switch (property_id) {
109 ges_track_set_caps (track, gst_value_get_caps (value));
112 track->type = g_value_get_flags (value);
115 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
120 ges_track_dispose (GObject * object)
122 GESTrack *track = (GESTrack *) object;
123 GESTrackPrivate *priv = track->priv;
125 while (priv->trackobjects) {
126 GESTrackObject *trobj = GES_TRACK_OBJECT (priv->trackobjects->data);
127 ges_track_remove_object (track, trobj);
128 ges_timeline_object_release_track_object ((GESTimelineObject *)
129 ges_track_object_get_timeline_object (trobj), trobj);
132 if (priv->composition) {
133 gst_bin_remove (GST_BIN (object), priv->composition);
134 priv->composition = NULL;
138 gst_caps_unref (priv->caps);
142 G_OBJECT_CLASS (ges_track_parent_class)->dispose (object);
146 ges_track_constructed (GObject * object)
148 GObjectClass *parent_class;
149 GstElement *background = NULL;
150 GESTrack *self = GES_TRACK (object);
151 GESTrackPrivate *priv = self->priv;
153 if ((priv->background = gst_element_factory_make ("gnlsource", "background"))) {
154 g_object_set (priv->background, "priority", G_MAXUINT, NULL);
156 switch (self->type) {
157 case GES_TRACK_TYPE_VIDEO:
158 background = gst_element_factory_make ("videotestsrc", "background");
159 g_object_set (background, "pattern", 2, NULL);
161 case GES_TRACK_TYPE_AUDIO:
162 background = gst_element_factory_make ("audiotestsrc", "background");
163 g_object_set (background, "wave", 4, NULL);
170 if (!gst_bin_add (GST_BIN (priv->background), background))
171 GST_ERROR ("Couldn't add background");
173 if (!gst_bin_add (GST_BIN (priv->composition), priv->background))
174 GST_ERROR ("Couldn't add background");
180 parent_class = ges_track_parent_class;
181 if (parent_class->constructed)
182 parent_class->constructed (object);
184 G_OBJECT_CLASS (parent_class)->constructed (object);
188 ges_track_finalize (GObject * object)
190 G_OBJECT_CLASS (ges_track_parent_class)->finalize (object);
194 ges_track_class_init (GESTrackClass * klass)
196 GObjectClass *object_class = G_OBJECT_CLASS (klass);
198 g_type_class_add_private (klass, sizeof (GESTrackPrivate));
200 object_class->get_property = ges_track_get_property;
201 object_class->set_property = ges_track_set_property;
202 object_class->dispose = ges_track_dispose;
203 object_class->finalize = ges_track_finalize;
204 object_class->constructed = ges_track_constructed;
209 * Caps used to filter/choose the output stream. This is generally set to
210 * a generic set of caps like 'video/x-raw' for raw video.
212 * Default value: #GST_CAPS_ANY.
214 properties[ARG_CAPS] = g_param_spec_boxed ("caps", "Caps",
215 "Caps used to filter/choose the output stream",
216 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
217 g_object_class_install_property (object_class, ARG_CAPS,
218 properties[ARG_CAPS]);
223 * Current duration of the track
227 properties[ARG_DURATION] = g_param_spec_uint64 ("duration", "Duration",
228 "The current duration of the track", 0, G_MAXUINT64, GST_SECOND,
230 g_object_class_install_property (object_class, ARG_DURATION,
231 properties[ARG_DURATION]);
234 * GESTrack:track-type
236 * Type of stream the track outputs. This is used when creating the #GESTrack
237 * to specify in generic terms what type of content will be outputted.
239 * It also serves as a 'fast' way to check what type of data will be outputted
240 * from the #GESTrack without having to actually check the #GESTrack's caps
243 properties[ARG_TYPE] = g_param_spec_flags ("track-type", "TrackType",
244 "Type of stream the track outputs",
245 GES_TYPE_TRACK_TYPE, GES_TRACK_TYPE_CUSTOM,
246 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
247 g_object_class_install_property (object_class, ARG_TYPE,
248 properties[ARG_TYPE]);
251 * GESTrack::track-object-added
252 * @object: the #GESTrack
253 * @effect: the #GESTrackObject that was added.
255 * Will be emitted after a track object was added to the track.
259 ges_track_signals[TRACK_OBJECT_ADDED] =
260 g_signal_new ("track-object-added", G_TYPE_FROM_CLASS (klass),
261 G_SIGNAL_RUN_FIRST, 0, NULL, NULL, g_cclosure_marshal_generic,
262 G_TYPE_NONE, 1, GES_TYPE_TRACK_OBJECT);
265 * GESTrack::track-object-removed
266 * @object: the #GESTrack
267 * @effect: the #GESTrackObject that was removed.
269 * Will be emitted after a track object was removed from the track.
273 ges_track_signals[TRACK_OBJECT_REMOVED] =
274 g_signal_new ("track-object-removed", G_TYPE_FROM_CLASS (klass),
275 G_SIGNAL_RUN_FIRST, 0, NULL, NULL, g_cclosure_marshal_generic,
276 G_TYPE_NONE, 1, GES_TYPE_TRACK_OBJECT);
280 ges_track_init (GESTrack * self)
282 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
283 GES_TYPE_TRACK, GESTrackPrivate);
285 self->priv->composition = gst_element_factory_make ("gnlcomposition", NULL);
287 g_signal_connect (G_OBJECT (self->priv->composition), "notify::duration",
288 G_CALLBACK (composition_duration_cb), self);
289 g_signal_connect (self->priv->composition, "pad-added",
290 (GCallback) pad_added_cb, self);
291 g_signal_connect (self->priv->composition, "pad-removed",
292 (GCallback) pad_removed_cb, self);
294 if (!gst_bin_add (GST_BIN (self), self->priv->composition))
295 GST_ERROR ("Couldn't add composition to bin !");
300 * @type: The type of track
301 * @caps: (transfer full): The caps to restrict the output of the track to.
303 * Creates a new #GESTrack with the given @type and @caps.
305 * The newly created track will steal a reference to the caps. If you wish to
306 * use those caps elsewhere, you will have to take an extra reference.
308 * Returns: A new #GESTrack.
311 ges_track_new (GESTrackType type, GstCaps * caps)
315 track = g_object_new (GES_TYPE_TRACK, "caps", caps, "track-type", type, NULL);
316 gst_caps_unref (caps);
322 * ges_track_video_raw_new:
324 * Creates a new #GESTrack of type #GES_TRACK_TYPE_VIDEO and with generic
325 * raw video caps ("video/x-raw");
327 * Returns: A new #GESTrack.
330 ges_track_video_raw_new (void)
333 GstCaps *caps = gst_caps_new_empty_simple ("video/x-raw");
335 track = ges_track_new (GES_TRACK_TYPE_VIDEO, caps);
341 * ges_track_audio_raw_new:
343 * Creates a new #GESTrack of type #GES_TRACK_TYPE_AUDIO and with generic
344 * raw audio caps ("audio/x-raw");
346 * Returns: A new #GESTrack.
349 ges_track_audio_raw_new (void)
352 GstCaps *caps = gst_caps_new_empty_simple ("audio/x-raw");
354 track = ges_track_new (GES_TRACK_TYPE_AUDIO, caps);
360 * ges_track_set_timeline:
361 * @track: a #GESTrack
362 * @timeline: a #GESTimeline
364 * Sets @timeline as the timeline controlling @track.
367 ges_track_set_timeline (GESTrack * track, GESTimeline * timeline)
369 GST_DEBUG ("track:%p, timeline:%p", track, timeline);
371 if (track->priv->timeline)
372 g_signal_handlers_disconnect_by_func (track,
373 timeline_duration_cb, track->priv->timeline);
376 g_signal_connect (G_OBJECT (timeline), "notify::duration",
377 G_CALLBACK (timeline_duration_cb), track);
379 track->priv->timeline = timeline;
383 * ges_track_set_caps:
384 * @track: a #GESTrack
385 * @caps: the #GstCaps to set
387 * Sets the given @caps on the track.
390 ges_track_set_caps (GESTrack * track, const GstCaps * caps)
392 GESTrackPrivate *priv;
394 g_return_if_fail (GES_IS_TRACK (track));
395 g_return_if_fail (GST_IS_CAPS (caps));
397 GST_DEBUG ("track:%p, caps:%" GST_PTR_FORMAT, track, caps);
402 gst_caps_unref (priv->caps);
403 priv->caps = gst_caps_copy (caps);
405 g_object_set (priv->composition, "caps", caps, NULL);
406 /* FIXME : update all trackobjects ? */
410 /* FIXME : put the compare function in the utils */
413 objects_start_compare (GESTrackObject * a, GESTrackObject * b)
415 if (a->start == b->start) {
416 if (a->priority < b->priority)
418 if (a->priority > b->priority)
422 if (a->start < b->start)
424 if (a->start > b->start)
430 * ges_track_add_object:
431 * @track: a #GESTrack
432 * @object: (transfer full): the #GESTrackObject to add
434 * Adds the given object to the track. Sets the object's controlling track,
435 * and thus takes ownership of the @object.
437 * An object can only be added to one track.
439 * Returns: #TRUE if the object was properly added. #FALSE if the track does not
440 * want to accept the object.
443 ges_track_add_object (GESTrack * track, GESTrackObject * object)
445 g_return_val_if_fail (GES_IS_TRACK (track), FALSE);
446 g_return_val_if_fail (GES_IS_TRACK_OBJECT (object), FALSE);
448 GST_DEBUG ("track:%p, object:%p", track, object);
450 if (G_UNLIKELY (ges_track_object_get_track (object) != NULL)) {
451 GST_WARNING ("Object already belongs to another track");
455 /* At this point, the track object shouldn't have any gnlobject since
456 * it hasn't been added to a track yet.
457 * FIXME : This check seems a bit obsolete */
458 if (G_UNLIKELY (ges_track_object_get_gnlobject (object) != NULL)) {
459 GST_ERROR ("TrackObject already controls a gnlobject !");
463 if (G_UNLIKELY (!ges_track_object_set_track (object, track))) {
464 GST_ERROR ("Couldn't properly add the object to the Track");
468 GST_DEBUG ("Adding object %s to ourself %s",
469 GST_OBJECT_NAME (ges_track_object_get_gnlobject (object)),
470 GST_OBJECT_NAME (track->priv->composition));
472 if (G_UNLIKELY (!gst_bin_add (GST_BIN (track->priv->composition),
473 ges_track_object_get_gnlobject (object)))) {
474 GST_WARNING ("Couldn't add object to the GnlComposition");
478 g_object_ref_sink (object);
479 track->priv->trackobjects =
480 g_list_insert_sorted (track->priv->trackobjects, object,
481 (GCompareFunc) objects_start_compare);
483 g_signal_emit (track, ges_track_signals[TRACK_OBJECT_ADDED], 0,
484 GES_TRACK_OBJECT (object));
486 g_signal_connect (GES_TRACK_OBJECT (object), "notify::start",
487 G_CALLBACK (sort_track_objects_cb), track);
489 g_signal_connect (GES_TRACK_OBJECT (object), "notify::priority",
490 G_CALLBACK (sort_track_objects_cb), track);
496 * ges_track_get_objects:
497 * @track: a #GESTrack
499 * Gets the #GESTrackObject contained in @track
501 * Returns: (transfer full) (element-type GESTrackObject): the list of
502 * #GESTrackObject present in the Track sorted by priority and start.
505 ges_track_get_objects (GESTrack * track)
510 g_return_val_if_fail (GES_IS_TRACK (track), NULL);
512 for (tmp = track->priv->trackobjects; tmp; tmp = tmp->next) {
513 ret = g_list_prepend (ret, tmp->data);
514 g_object_ref (tmp->data);
517 ret = g_list_reverse (ret);
522 * ges_track_remove_object:
523 * @track: a #GESTrack
524 * @object: the #GESTrackObject to remove
526 * Removes the object from the track and unparents it.
527 * Unparenting it means the reference owned by @track on the @object will be
528 * removed. If you wish to use the @object after this function, make sure you
529 * call g_object_ref() before removing it from the @track.
531 * Returns: #TRUE if the object was removed, else #FALSE if the track
532 * could not remove the object (like if it didn't belong to the track).
535 ges_track_remove_object (GESTrack * track, GESTrackObject * object)
537 GESTrackPrivate *priv;
538 GstElement *gnlobject;
540 g_return_val_if_fail (GES_IS_TRACK (track), FALSE);
541 g_return_val_if_fail (GES_IS_TRACK_OBJECT (object), FALSE);
543 GST_DEBUG ("track:%p, object:%p", track, object);
547 if (G_UNLIKELY (ges_track_object_get_track (object) != track)) {
548 GST_WARNING ("Object belongs to another track");
552 if ((gnlobject = ges_track_object_get_gnlobject (object))) {
553 GST_DEBUG ("Removing GnlObject '%s' from composition '%s'",
554 GST_ELEMENT_NAME (gnlobject), GST_ELEMENT_NAME (priv->composition));
555 if (!gst_bin_remove (GST_BIN (priv->composition), gnlobject)) {
556 GST_WARNING ("Failed to remove gnlobject from composition");
561 g_signal_handlers_disconnect_by_func (object, sort_track_objects_cb, NULL);
563 ges_track_object_set_track (object, NULL);
565 g_signal_emit (track, ges_track_signals[TRACK_OBJECT_REMOVED], 0,
566 GES_TRACK_OBJECT (object));
568 priv->trackobjects = g_list_remove (priv->trackobjects, object);
570 g_object_unref (object);
576 pad_added_cb (GstElement * element, GstPad * pad, GESTrack * track)
578 GESTrackPrivate *priv = track->priv;
580 GST_DEBUG ("track:%p, pad %s:%s", track, GST_DEBUG_PAD_NAME (pad));
583 priv->srcpad = gst_ghost_pad_new ("src", pad);
585 gst_pad_set_active (priv->srcpad, TRUE);
587 gst_element_add_pad (GST_ELEMENT (track), priv->srcpad);
593 pad_removed_cb (GstElement * element, GstPad * pad, GESTrack * track)
595 GESTrackPrivate *priv = track->priv;
597 GST_DEBUG ("track:%p, pad %s:%s", track, GST_DEBUG_PAD_NAME (pad));
599 if (G_LIKELY (priv->srcpad)) {
600 gst_pad_set_active (priv->srcpad, FALSE);
601 gst_element_remove_pad (GST_ELEMENT (track), priv->srcpad);
609 composition_duration_cb (GstElement * composition,
610 GParamSpec * arg G_GNUC_UNUSED, GESTrack * obj)
614 g_object_get (composition, "duration", &duration, NULL);
617 if (obj->priv->duration != duration) {
618 GST_DEBUG ("composition duration : %" GST_TIME_FORMAT " current : %"
619 GST_TIME_FORMAT, GST_TIME_ARGS (duration),
620 GST_TIME_ARGS (obj->priv->duration));
622 obj->priv->duration = duration;
624 #if GLIB_CHECK_VERSION(2,26,0)
625 g_object_notify_by_pspec (G_OBJECT (obj), properties[ARG_DURATION]);
627 g_object_notify (G_OBJECT (obj), "duration");
633 sort_track_objects_cb (GESTrackObject * child,
634 GParamSpec * arg G_GNUC_UNUSED, GESTrack * track)
636 track->priv->trackobjects =
637 g_list_sort (track->priv->trackobjects,
638 (GCompareFunc) objects_start_compare);
642 timeline_duration_cb (GESTimeline * timeline,
643 GParamSpec * arg G_GNUC_UNUSED, GESTrack * track)
647 g_object_get (timeline, "duration", &duration, NULL);
648 g_object_set (GES_TRACK (track)->priv->background, "duration", duration,
651 GST_DEBUG_OBJECT (track, "Updating background duration to %" GST_TIME_FORMAT,
652 GST_TIME_ARGS (duration));
656 * ges_track_get_caps:
657 * @track: a #GESTrack
659 * Get the #GstCaps this track is configured to output.
661 * Returns: The #GstCaps this track is configured to output.
664 ges_track_get_caps (GESTrack * track)
666 g_return_val_if_fail (GES_IS_TRACK (track), NULL);
668 return track->priv->caps;
672 * ges_track_get_timeline:
673 * @track: a #GESTrack
675 * Get the #GESTimeline this track belongs to. Can be %NULL.
677 * Returns: The #GESTimeline this track belongs to. Can be %NULL.
680 ges_track_get_timeline (GESTrack * track)
682 g_return_val_if_fail (GES_IS_TRACK (track), NULL);
684 return track->priv->timeline;
688 * ges_track_enable_update:
689 * @track: a #GESTrack
690 * @enabled: Whether the track should update on every change or not.
692 * Control whether the track is updated for every change happening within.
694 * Users will want to use this method with %FALSE before doing lots of changes,
695 * and then call again with %TRUE for the changes to take effect in one go.
697 * Returns: %TRUE if the update status could be changed, else %FALSE.
700 ges_track_enable_update (GESTrack * track, gboolean enabled)
704 g_object_set (track->priv->composition, "update", enabled, NULL);
706 g_object_get (track->priv->composition, "update", &update, NULL);
708 if (update == enabled) {