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"
35 #include "gesmarshal.h"
37 G_DEFINE_TYPE (GESTrack, ges_track, GST_TYPE_BIN);
39 struct _GESTrackPrivate
42 GESTimeline *timeline;
48 GstElement *composition; /* The composition associated with this track */
49 GstElement *background; /* The backgrond, handle the gaps in the track */
50 GstPad *srcpad; /* The source GhostPad */
67 static guint ges_track_signals[LAST_SIGNAL] = { 0 };
69 static GParamSpec *properties[ARG_LAST];
71 static void pad_added_cb (GstElement * element, GstPad * pad, GESTrack * track);
73 pad_removed_cb (GstElement * element, GstPad * pad, GESTrack * track);
74 static void composition_duration_cb (GstElement * composition, GParamSpec * arg
75 G_GNUC_UNUSED, GESTrack * obj);
77 sort_track_objects_cb (GESTrackObject * child,
78 GParamSpec * arg G_GNUC_UNUSED, GESTrack * track);
80 static void timeline_duration_cb (GESTimeline * timeline,
81 GParamSpec * arg G_GNUC_UNUSED, GESTrack * track);
84 ges_track_get_property (GObject * object, guint property_id,
85 GValue * value, GParamSpec * pspec)
87 GESTrack *track = GES_TRACK (object);
89 switch (property_id) {
91 gst_value_set_caps (value, track->priv->caps);
94 g_value_set_flags (value, track->type);
97 g_value_set_uint64 (value, track->priv->duration);
100 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
105 ges_track_set_property (GObject * object, guint property_id,
106 const GValue * value, GParamSpec * pspec)
108 GESTrack *track = GES_TRACK (object);
110 switch (property_id) {
112 ges_track_set_caps (track, gst_value_get_caps (value));
115 track->type = g_value_get_flags (value);
118 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
123 ges_track_dispose (GObject * object)
125 GESTrack *track = (GESTrack *) object;
126 GESTrackPrivate *priv = track->priv;
128 while (priv->trackobjects) {
129 GESTrackObject *trobj = GES_TRACK_OBJECT (priv->trackobjects->data);
130 ges_track_remove_object (track, trobj);
131 ges_timeline_object_release_track_object ((GESTimelineObject *)
132 ges_track_object_get_timeline_object (trobj), trobj);
135 if (priv->composition) {
136 gst_bin_remove (GST_BIN (object), priv->composition);
137 priv->composition = NULL;
141 gst_caps_unref (priv->caps);
145 G_OBJECT_CLASS (ges_track_parent_class)->dispose (object);
149 ges_track_constructed (GObject * object)
151 GObjectClass *parent_class;
152 GstElement *background = NULL;
153 GESTrack *self = GES_TRACK (object);
154 GESTrackPrivate *priv = self->priv;
156 if ((priv->background = gst_element_factory_make ("gnlsource", "background"))) {
157 g_object_set (priv->background, "priority", G_MAXUINT, NULL);
159 switch (self->type) {
160 case GES_TRACK_TYPE_VIDEO:
161 background = gst_element_factory_make ("videotestsrc", "background");
162 g_object_set (background, "pattern", 2, NULL);
164 case GES_TRACK_TYPE_AUDIO:
165 background = gst_element_factory_make ("audiotestsrc", "background");
166 g_object_set (background, "wave", 4, NULL);
173 if (!gst_bin_add (GST_BIN (priv->background), background))
174 GST_ERROR ("Couldn't add background");
176 if (!gst_bin_add (GST_BIN (priv->composition), priv->background))
177 GST_ERROR ("Couldn't add background");
183 parent_class = ges_track_parent_class;
184 if (parent_class->constructed)
185 parent_class->constructed (object);
187 G_OBJECT_CLASS (parent_class)->constructed (object);
191 ges_track_finalize (GObject * object)
193 G_OBJECT_CLASS (ges_track_parent_class)->finalize (object);
197 ges_track_class_init (GESTrackClass * klass)
199 GObjectClass *object_class = G_OBJECT_CLASS (klass);
201 g_type_class_add_private (klass, sizeof (GESTrackPrivate));
203 object_class->get_property = ges_track_get_property;
204 object_class->set_property = ges_track_set_property;
205 object_class->dispose = ges_track_dispose;
206 object_class->finalize = ges_track_finalize;
207 object_class->constructed = ges_track_constructed;
212 * Caps used to filter/choose the output stream. This is generally set to
213 * a generic set of caps like 'video/x-raw-rgb;video/x-raw-yuv' for raw video.
215 * Default value: #GST_CAPS_ANY.
217 properties[ARG_CAPS] = g_param_spec_boxed ("caps", "Caps",
218 "Caps used to filter/choose the output stream",
219 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
220 g_object_class_install_property (object_class, ARG_CAPS,
221 properties[ARG_CAPS]);
226 * Current duration of the track
230 properties[ARG_DURATION] = g_param_spec_uint64 ("duration", "Duration",
231 "The current duration of the track", 0, G_MAXUINT64, GST_SECOND,
233 g_object_class_install_property (object_class, ARG_DURATION,
234 properties[ARG_DURATION]);
237 * GESTrack:track-type
239 * Type of stream the track outputs. This is used when creating the #GESTrack
240 * to specify in generic terms what type of content will be outputted.
242 * It also serves as a 'fast' way to check what type of data will be outputted
243 * from the #GESTrack without having to actually check the #GESTrack's caps
246 properties[ARG_TYPE] = g_param_spec_flags ("track-type", "TrackType",
247 "Type of stream the track outputs",
248 GES_TYPE_TRACK_TYPE, GES_TRACK_TYPE_CUSTOM,
249 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
250 g_object_class_install_property (object_class, ARG_TYPE,
251 properties[ARG_TYPE]);
254 * GESTrack::track-object-added
255 * @object: the #GESTrack
256 * @effect: the #GESTrackObject that was added.
258 * Will be emitted after a track object was added to the track.
262 ges_track_signals[TRACK_OBJECT_ADDED] =
263 g_signal_new ("track-object-added", G_TYPE_FROM_CLASS (klass),
264 G_SIGNAL_RUN_FIRST, 0, NULL, NULL, ges_marshal_VOID__OBJECT,
265 G_TYPE_NONE, 1, GES_TYPE_TRACK_OBJECT);
268 * GESTrack::track-object-removed
269 * @object: the #GESTrack
270 * @effect: the #GESTrackObject that was removed.
272 * Will be emitted after a track object was removed from the track.
276 ges_track_signals[TRACK_OBJECT_REMOVED] =
277 g_signal_new ("track-object-removed", G_TYPE_FROM_CLASS (klass),
278 G_SIGNAL_RUN_FIRST, 0, NULL, NULL, ges_marshal_VOID__OBJECT,
279 G_TYPE_NONE, 1, GES_TYPE_TRACK_OBJECT);
283 ges_track_init (GESTrack * self)
285 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
286 GES_TYPE_TRACK, GESTrackPrivate);
288 self->priv->composition = gst_element_factory_make ("gnlcomposition", NULL);
289 self->priv->updating = TRUE;
291 g_signal_connect (G_OBJECT (self->priv->composition), "notify::duration",
292 G_CALLBACK (composition_duration_cb), self);
293 g_signal_connect (self->priv->composition, "pad-added",
294 (GCallback) pad_added_cb, self);
295 g_signal_connect (self->priv->composition, "pad-removed",
296 (GCallback) pad_removed_cb, self);
298 if (!gst_bin_add (GST_BIN (self), self->priv->composition))
299 GST_ERROR ("Couldn't add composition to bin !");
304 * @type: The type of track
305 * @caps: The caps to restrict the output of the track to.
307 * Creates a new #GESTrack with the given @type and @caps.
309 * The newly created track will steal a reference to the caps. If you wish to
310 * use those caps elsewhere, you will have to take an extra reference.
312 * Returns: A new #GESTrack.
315 ges_track_new (GESTrackType type, GstCaps * caps)
319 track = g_object_new (GES_TYPE_TRACK, "caps", caps, "track-type", type, NULL);
320 gst_caps_unref (caps);
326 * ges_track_video_raw_new:
328 * Creates a new #GESTrack of type #GES_TRACK_TYPE_VIDEO and with generic
329 * raw video caps ("video/x-raw-yuv;video/x-raw-rgb");
331 * Returns: A new #GESTrack.
334 ges_track_video_raw_new (void)
337 GstCaps *caps = gst_caps_from_string ("video/x-raw-yuv;video/x-raw-rgb");
339 track = ges_track_new (GES_TRACK_TYPE_VIDEO, caps);
345 * ges_track_audio_raw_new:
347 * Creates a new #GESTrack of type #GES_TRACK_TYPE_AUDIO and with generic
348 * raw audio caps ("audio/x-raw-int;audio/x-raw-float");
350 * Returns: A new #GESTrack.
353 ges_track_audio_raw_new (void)
356 GstCaps *caps = gst_caps_from_string ("audio/x-raw-int;audio/x-raw-float");
358 track = ges_track_new (GES_TRACK_TYPE_AUDIO, caps);
364 * ges_track_set_timeline:
365 * @track: a #GESTrack
366 * @timeline: a #GESTimeline
368 * Sets @timeline as the timeline controlling @track.
371 ges_track_set_timeline (GESTrack * track, GESTimeline * timeline)
373 GST_DEBUG ("track:%p, timeline:%p", track, timeline);
375 if (track->priv->timeline)
376 g_signal_handlers_disconnect_by_func (track,
377 timeline_duration_cb, track->priv->timeline);
380 g_signal_connect (G_OBJECT (timeline), "notify::duration",
381 G_CALLBACK (timeline_duration_cb), track);
383 track->priv->timeline = timeline;
387 * ges_track_set_caps:
388 * @track: a #GESTrack
389 * @caps: the #GstCaps to set
391 * Sets the given @caps on the track.
394 ges_track_set_caps (GESTrack * track, const GstCaps * caps)
396 GESTrackPrivate *priv;
398 g_return_if_fail (GES_IS_TRACK (track));
399 g_return_if_fail (GST_IS_CAPS (caps));
401 GST_DEBUG ("track:%p, caps:%" GST_PTR_FORMAT, track, caps);
406 gst_caps_unref (priv->caps);
407 priv->caps = gst_caps_copy (caps);
409 g_object_set (priv->composition, "caps", caps, NULL);
410 /* FIXME : update all trackobjects ? */
414 /* FIXME : put the compare function in the utils */
417 objects_start_compare (GESTrackObject * a, GESTrackObject * b)
419 if (a->start == b->start) {
420 if (a->priority < b->priority)
422 if (a->priority > b->priority)
426 if (a->start < b->start)
428 if (a->start > b->start)
434 * ges_track_add_object:
435 * @track: a #GESTrack
436 * @object: (transfer full): the #GESTrackObject to add
438 * Adds the given object to the track. Sets the object's controlling track,
439 * and thus takes ownership of the @object.
441 * An object can only be added to one track.
443 * Returns: #TRUE if the object was properly added. #FALSE if the track does not
444 * want to accept the object.
447 ges_track_add_object (GESTrack * track, GESTrackObject * object)
449 g_return_val_if_fail (GES_IS_TRACK (track), FALSE);
450 g_return_val_if_fail (GES_IS_TRACK_OBJECT (object), FALSE);
452 GST_DEBUG ("track:%p, object:%p", track, object);
454 if (G_UNLIKELY (ges_track_object_get_track (object) != NULL)) {
455 GST_WARNING ("Object already belongs to another track");
459 /* At this point, the track object shouldn't have any gnlobject since
460 * it hasn't been added to a track yet.
461 * FIXME : This check seems a bit obsolete */
462 if (G_UNLIKELY (ges_track_object_get_gnlobject (object) != NULL)) {
463 GST_ERROR ("TrackObject already controls a gnlobject !");
467 if (G_UNLIKELY (!ges_track_object_set_track (object, track))) {
468 GST_ERROR ("Couldn't properly add the object to the Track");
472 GST_DEBUG ("Adding object %s to ourself %s",
473 GST_OBJECT_NAME (ges_track_object_get_gnlobject (object)),
474 GST_OBJECT_NAME (track->priv->composition));
476 if (G_UNLIKELY (!gst_bin_add (GST_BIN (track->priv->composition),
477 ges_track_object_get_gnlobject (object)))) {
478 GST_WARNING ("Couldn't add object to the GnlComposition");
482 g_object_ref_sink (object);
483 track->priv->trackobjects =
484 g_list_insert_sorted (track->priv->trackobjects, object,
485 (GCompareFunc) objects_start_compare);
487 g_signal_emit (track, ges_track_signals[TRACK_OBJECT_ADDED], 0,
488 GES_TRACK_OBJECT (object));
490 g_signal_connect (GES_TRACK_OBJECT (object), "notify::start",
491 G_CALLBACK (sort_track_objects_cb), track);
493 g_signal_connect (GES_TRACK_OBJECT (object), "notify::priority",
494 G_CALLBACK (sort_track_objects_cb), track);
500 * ges_track_get_objects:
501 * @track: a #GESTrack
503 * Gets the #GESTrackObject contained in @track
505 * Returns: (transfer full) (element-type GESTrackObject): the list of
506 * #GESTrackObject present in the Track sorted by priority and start.
509 ges_track_get_objects (GESTrack * track)
514 g_return_val_if_fail (GES_IS_TRACK (track), NULL);
516 for (tmp = track->priv->trackobjects; tmp; tmp = tmp->next) {
517 ret = g_list_prepend (ret, tmp->data);
518 g_object_ref (tmp->data);
521 ret = g_list_reverse (ret);
526 * ges_track_remove_object:
527 * @track: a #GESTrack
528 * @object: the #GESTrackObject to remove
530 * Removes the object from the track and unparents it.
531 * Unparenting it means the reference owned by @track on the @object will be
532 * removed. If you wish to use the @object after this function, make sure you
533 * call g_object_ref() before removing it from the @track.
535 * Returns: #TRUE if the object was removed, else #FALSE if the track
536 * could not remove the object (like if it didn't belong to the track).
539 ges_track_remove_object (GESTrack * track, GESTrackObject * object)
541 GESTrackPrivate *priv;
542 GstElement *gnlobject;
544 g_return_val_if_fail (GES_IS_TRACK (track), FALSE);
545 g_return_val_if_fail (GES_IS_TRACK_OBJECT (object), FALSE);
547 GST_DEBUG ("track:%p, object:%p", track, object);
551 if (G_UNLIKELY (ges_track_object_get_track (object) != track)) {
552 GST_WARNING ("Object belongs to another track");
556 if ((gnlobject = ges_track_object_get_gnlobject (object))) {
557 GST_DEBUG ("Removing GnlObject '%s' from composition '%s'",
558 GST_ELEMENT_NAME (gnlobject), GST_ELEMENT_NAME (priv->composition));
559 /* We can't just set state of gnlobject to GST_STATE_NULL, because it will
560 * result in deadlock. Adding a ref to the gnlobj so we finalize it after
561 * removing it from the composition */
562 gst_object_ref (gnlobject);
563 if (!gst_bin_remove (GST_BIN (priv->composition), gnlobject)) {
564 GST_WARNING ("Failed to remove gnlobject from composition");
568 gst_element_set_state (gnlobject, GST_STATE_NULL);
569 /* Wait for the state change to actually happen */
570 gst_element_get_state (gnlobject, NULL, NULL, GST_CLOCK_TIME_NONE);
571 gst_object_unref (gnlobject);
574 g_signal_handlers_disconnect_by_func (object, sort_track_objects_cb, NULL);
576 ges_track_object_set_track (object, NULL);
578 g_signal_emit (track, ges_track_signals[TRACK_OBJECT_REMOVED], 0,
579 GES_TRACK_OBJECT (object));
581 priv->trackobjects = g_list_remove (priv->trackobjects, object);
583 g_object_unref (object);
589 pad_added_cb (GstElement * element, GstPad * pad, GESTrack * track)
591 GESTrackPrivate *priv = track->priv;
593 GST_DEBUG ("track:%p, pad %s:%s", track, GST_DEBUG_PAD_NAME (pad));
596 priv->srcpad = gst_ghost_pad_new ("src", pad);
598 gst_pad_set_active (priv->srcpad, TRUE);
600 gst_element_add_pad (GST_ELEMENT (track), priv->srcpad);
606 pad_removed_cb (GstElement * element, GstPad * pad, GESTrack * track)
608 GESTrackPrivate *priv = track->priv;
610 GST_DEBUG ("track:%p, pad %s:%s", track, GST_DEBUG_PAD_NAME (pad));
612 if (G_LIKELY (priv->srcpad)) {
613 gst_pad_set_active (priv->srcpad, FALSE);
614 gst_element_remove_pad (GST_ELEMENT (track), priv->srcpad);
622 composition_duration_cb (GstElement * composition,
623 GParamSpec * arg G_GNUC_UNUSED, GESTrack * obj)
627 g_object_get (composition, "duration", &duration, NULL);
630 if (obj->priv->duration != duration) {
631 GST_DEBUG ("composition duration : %" GST_TIME_FORMAT " current : %"
632 GST_TIME_FORMAT, GST_TIME_ARGS (duration),
633 GST_TIME_ARGS (obj->priv->duration));
635 obj->priv->duration = duration;
637 #if GLIB_CHECK_VERSION(2,26,0)
638 g_object_notify_by_pspec (G_OBJECT (obj), properties[ARG_DURATION]);
640 g_object_notify (G_OBJECT (obj), "duration");
646 sort_track_objects_cb (GESTrackObject * child,
647 GParamSpec * arg G_GNUC_UNUSED, GESTrack * track)
649 track->priv->trackobjects =
650 g_list_sort (track->priv->trackobjects,
651 (GCompareFunc) objects_start_compare);
655 timeline_duration_cb (GESTimeline * timeline,
656 GParamSpec * arg G_GNUC_UNUSED, GESTrack * track)
660 g_object_get (timeline, "duration", &duration, NULL);
661 g_object_set (GES_TRACK (track)->priv->background, "duration", duration,
664 GST_DEBUG_OBJECT (track, "Updating background duration to %" GST_TIME_FORMAT,
665 GST_TIME_ARGS (duration));
669 * ges_track_get_caps:
670 * @track: a #GESTrack
672 * Get the #GstCaps this track is configured to output.
674 * Returns: The #GstCaps this track is configured to output.
677 ges_track_get_caps (GESTrack * track)
679 g_return_val_if_fail (GES_IS_TRACK (track), NULL);
681 return track->priv->caps;
685 * ges_track_get_timeline:
686 * @track: a #GESTrack
688 * Get the #GESTimeline this track belongs to. Can be %NULL.
690 * Returns: The #GESTimeline this track belongs to. Can be %NULL.
693 ges_track_get_timeline (GESTrack * track)
695 g_return_val_if_fail (GES_IS_TRACK (track), NULL);
697 return track->priv->timeline;
701 * ges_track_enable_update:
702 * @track: a #GESTrack
703 * @enabled: Whether the track should update on every change or not.
705 * Control whether the track is updated for every change happening within.
707 * Users will want to use this method with %FALSE before doing lots of changes,
708 * and then call again with %TRUE for the changes to take effect in one go.
710 * Returns: %TRUE if the update status could be changed, else %FALSE.
713 ges_track_enable_update (GESTrack * track, gboolean enabled)
717 g_return_val_if_fail (GES_IS_TRACK (track), FALSE);
719 g_object_set (track->priv->composition, "update", enabled, NULL);
720 g_object_get (track->priv->composition, "update", &update, NULL);
722 track->priv->updating = update;
724 return update == enabled;
728 * ges_track_is_updating:
729 * @track: a #GESTrack
731 * Get whether the track is updated for every change happening within or not.
733 * Returns: %TRUE if @track is updating on every changes, else %FALSE.
736 ges_track_is_updating (GESTrack * track)
738 g_return_val_if_fail (GES_IS_TRACK (track), FALSE);
740 return track->priv->updating;