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 */
66 static guint ges_track_signals[LAST_SIGNAL] = { 0 };
68 static GParamSpec *properties[ARG_LAST];
70 static void pad_added_cb (GstElement * element, GstPad * pad, GESTrack * track);
72 pad_removed_cb (GstElement * element, GstPad * pad, GESTrack * track);
73 static void composition_duration_cb (GstElement * composition, GParamSpec * arg
74 G_GNUC_UNUSED, GESTrack * obj);
76 sort_track_objects_cb (GESTrackObject * child,
77 GParamSpec * arg G_GNUC_UNUSED, GESTrack * track);
79 static void timeline_duration_cb (GESTimeline * timeline,
80 GParamSpec * arg G_GNUC_UNUSED, GESTrack * track);
83 ges_track_get_property (GObject * object, guint property_id,
84 GValue * value, GParamSpec * pspec)
86 GESTrack *track = GES_TRACK (object);
88 switch (property_id) {
90 gst_value_set_caps (value, track->priv->caps);
93 g_value_set_flags (value, track->type);
96 g_value_set_uint64 (value, track->priv->duration);
99 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
104 ges_track_set_property (GObject * object, guint property_id,
105 const GValue * value, GParamSpec * pspec)
107 GESTrack *track = GES_TRACK (object);
109 switch (property_id) {
111 ges_track_set_caps (track, gst_value_get_caps (value));
114 track->type = g_value_get_flags (value);
117 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
122 ges_track_dispose (GObject * object)
124 GESTrack *track = (GESTrack *) object;
125 GESTrackPrivate *priv = track->priv;
127 while (priv->trackobjects) {
128 GESTrackObject *trobj = GES_TRACK_OBJECT (priv->trackobjects->data);
129 ges_track_remove_object (track, trobj);
130 ges_timeline_object_release_track_object ((GESTimelineObject *)
131 ges_track_object_get_timeline_object (trobj), trobj);
134 if (priv->composition) {
135 gst_bin_remove (GST_BIN (object), priv->composition);
136 priv->composition = NULL;
140 gst_caps_unref (priv->caps);
144 G_OBJECT_CLASS (ges_track_parent_class)->dispose (object);
148 ges_track_constructed (GObject * object)
150 GObjectClass *parent_class;
151 GstElement *background = NULL;
152 GESTrack *self = GES_TRACK (object);
153 GESTrackPrivate *priv = self->priv;
155 if ((priv->background = gst_element_factory_make ("gnlsource", "background"))) {
156 g_object_set (priv->background, "priority", G_MAXUINT, NULL);
158 switch (self->type) {
159 case GES_TRACK_TYPE_VIDEO:
160 background = gst_element_factory_make ("videotestsrc", "background");
161 g_object_set (background, "pattern", 2, NULL);
163 case GES_TRACK_TYPE_AUDIO:
164 background = gst_element_factory_make ("audiotestsrc", "background");
165 g_object_set (background, "wave", 4, NULL);
172 if (!gst_bin_add (GST_BIN (priv->background), background))
173 GST_ERROR ("Couldn't add background");
175 if (!gst_bin_add (GST_BIN (priv->composition), priv->background))
176 GST_ERROR ("Couldn't add background");
182 parent_class = ges_track_parent_class;
183 if (parent_class->constructed)
184 parent_class->constructed (object);
186 G_OBJECT_CLASS (parent_class)->constructed (object);
190 ges_track_finalize (GObject * object)
192 G_OBJECT_CLASS (ges_track_parent_class)->finalize (object);
196 ges_track_class_init (GESTrackClass * klass)
198 GObjectClass *object_class = G_OBJECT_CLASS (klass);
200 g_type_class_add_private (klass, sizeof (GESTrackPrivate));
202 object_class->get_property = ges_track_get_property;
203 object_class->set_property = ges_track_set_property;
204 object_class->dispose = ges_track_dispose;
205 object_class->finalize = ges_track_finalize;
206 object_class->constructed = ges_track_constructed;
211 * Caps used to filter/choose the output stream. This is generally set to
212 * a generic set of caps like 'video/x-raw' for raw video.
214 * Default value: #GST_CAPS_ANY.
216 properties[ARG_CAPS] = g_param_spec_boxed ("caps", "Caps",
217 "Caps used to filter/choose the output stream",
218 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
219 g_object_class_install_property (object_class, ARG_CAPS,
220 properties[ARG_CAPS]);
225 * Current duration of the track
229 properties[ARG_DURATION] = g_param_spec_uint64 ("duration", "Duration",
230 "The current duration of the track", 0, G_MAXUINT64, GST_SECOND,
232 g_object_class_install_property (object_class, ARG_DURATION,
233 properties[ARG_DURATION]);
236 * GESTrack:track-type
238 * Type of stream the track outputs. This is used when creating the #GESTrack
239 * to specify in generic terms what type of content will be outputted.
241 * It also serves as a 'fast' way to check what type of data will be outputted
242 * from the #GESTrack without having to actually check the #GESTrack's caps
245 properties[ARG_TYPE] = g_param_spec_flags ("track-type", "TrackType",
246 "Type of stream the track outputs",
247 GES_TYPE_TRACK_TYPE, GES_TRACK_TYPE_CUSTOM,
248 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
249 g_object_class_install_property (object_class, ARG_TYPE,
250 properties[ARG_TYPE]);
253 * GESTrack::track-object-added
254 * @object: the #GESTrack
255 * @effect: the #GESTrackObject that was added.
257 * Will be emitted after a track object was added to the track.
261 ges_track_signals[TRACK_OBJECT_ADDED] =
262 g_signal_new ("track-object-added", G_TYPE_FROM_CLASS (klass),
263 G_SIGNAL_RUN_FIRST, 0, NULL, NULL, g_cclosure_marshal_generic,
264 G_TYPE_NONE, 1, GES_TYPE_TRACK_OBJECT);
267 * GESTrack::track-object-removed
268 * @object: the #GESTrack
269 * @effect: the #GESTrackObject that was removed.
271 * Will be emitted after a track object was removed from the track.
275 ges_track_signals[TRACK_OBJECT_REMOVED] =
276 g_signal_new ("track-object-removed", G_TYPE_FROM_CLASS (klass),
277 G_SIGNAL_RUN_FIRST, 0, NULL, NULL, g_cclosure_marshal_generic,
278 G_TYPE_NONE, 1, GES_TYPE_TRACK_OBJECT);
282 ges_track_init (GESTrack * self)
284 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
285 GES_TYPE_TRACK, GESTrackPrivate);
287 self->priv->composition = gst_element_factory_make ("gnlcomposition", NULL);
288 self->priv->updating = TRUE;
290 g_signal_connect (G_OBJECT (self->priv->composition), "notify::duration",
291 G_CALLBACK (composition_duration_cb), self);
292 g_signal_connect (self->priv->composition, "pad-added",
293 (GCallback) pad_added_cb, self);
294 g_signal_connect (self->priv->composition, "pad-removed",
295 (GCallback) pad_removed_cb, self);
297 if (!gst_bin_add (GST_BIN (self), self->priv->composition))
298 GST_ERROR ("Couldn't add composition to bin !");
303 * @type: The type of track
304 * @caps: (transfer full): The caps to restrict the output of the track to.
306 * Creates a new #GESTrack with the given @type and @caps.
308 * The newly created track will steal a reference to the caps. If you wish to
309 * use those caps elsewhere, you will have to take an extra reference.
311 * Returns: A new #GESTrack.
314 ges_track_new (GESTrackType type, GstCaps * caps)
318 track = g_object_new (GES_TYPE_TRACK, "caps", caps, "track-type", type, NULL);
319 gst_caps_unref (caps);
325 * ges_track_video_raw_new:
327 * Creates a new #GESTrack of type #GES_TRACK_TYPE_VIDEO and with generic
328 * raw video caps ("video/x-raw");
330 * Returns: A new #GESTrack.
333 ges_track_video_raw_new (void)
336 GstCaps *caps = gst_caps_new_empty_simple ("video/x-raw");
338 track = ges_track_new (GES_TRACK_TYPE_VIDEO, caps);
344 * ges_track_audio_raw_new:
346 * Creates a new #GESTrack of type #GES_TRACK_TYPE_AUDIO and with generic
347 * raw audio caps ("audio/x-raw");
349 * Returns: A new #GESTrack.
352 ges_track_audio_raw_new (void)
355 GstCaps *caps = gst_caps_new_empty_simple ("audio/x-raw");
357 track = ges_track_new (GES_TRACK_TYPE_AUDIO, caps);
363 * ges_track_set_timeline:
364 * @track: a #GESTrack
365 * @timeline: a #GESTimeline
367 * Sets @timeline as the timeline controlling @track.
370 ges_track_set_timeline (GESTrack * track, GESTimeline * timeline)
372 GST_DEBUG ("track:%p, timeline:%p", track, timeline);
374 if (track->priv->timeline)
375 g_signal_handlers_disconnect_by_func (track,
376 timeline_duration_cb, track->priv->timeline);
379 g_signal_connect (G_OBJECT (timeline), "notify::duration",
380 G_CALLBACK (timeline_duration_cb), track);
382 track->priv->timeline = timeline;
386 * ges_track_set_caps:
387 * @track: a #GESTrack
388 * @caps: the #GstCaps to set
390 * Sets the given @caps on the track.
393 ges_track_set_caps (GESTrack * track, const GstCaps * caps)
395 GESTrackPrivate *priv;
397 g_return_if_fail (GES_IS_TRACK (track));
398 g_return_if_fail (GST_IS_CAPS (caps));
400 GST_DEBUG ("track:%p, caps:%" GST_PTR_FORMAT, track, caps);
405 gst_caps_unref (priv->caps);
406 priv->caps = gst_caps_copy (caps);
408 g_object_set (priv->composition, "caps", caps, NULL);
409 /* FIXME : update all trackobjects ? */
413 /* FIXME : put the compare function in the utils */
416 objects_start_compare (GESTrackObject * a, GESTrackObject * b)
418 if (a->start == b->start) {
419 if (a->priority < b->priority)
421 if (a->priority > b->priority)
425 if (a->start < b->start)
427 if (a->start > b->start)
433 * ges_track_add_object:
434 * @track: a #GESTrack
435 * @object: (transfer full): the #GESTrackObject to add
437 * Adds the given object to the track. Sets the object's controlling track,
438 * and thus takes ownership of the @object.
440 * An object can only be added to one track.
442 * Returns: #TRUE if the object was properly added. #FALSE if the track does not
443 * want to accept the object.
446 ges_track_add_object (GESTrack * track, GESTrackObject * object)
448 g_return_val_if_fail (GES_IS_TRACK (track), FALSE);
449 g_return_val_if_fail (GES_IS_TRACK_OBJECT (object), FALSE);
451 GST_DEBUG ("track:%p, object:%p", track, object);
453 if (G_UNLIKELY (ges_track_object_get_track (object) != NULL)) {
454 GST_WARNING ("Object already belongs to another track");
458 /* At this point, the track object shouldn't have any gnlobject since
459 * it hasn't been added to a track yet.
460 * FIXME : This check seems a bit obsolete */
461 if (G_UNLIKELY (ges_track_object_get_gnlobject (object) != NULL)) {
462 GST_ERROR ("TrackObject already controls a gnlobject !");
466 if (G_UNLIKELY (!ges_track_object_set_track (object, track))) {
467 GST_ERROR ("Couldn't properly add the object to the Track");
471 GST_DEBUG ("Adding object %s to ourself %s",
472 GST_OBJECT_NAME (ges_track_object_get_gnlobject (object)),
473 GST_OBJECT_NAME (track->priv->composition));
475 if (G_UNLIKELY (!gst_bin_add (GST_BIN (track->priv->composition),
476 ges_track_object_get_gnlobject (object)))) {
477 GST_WARNING ("Couldn't add object to the GnlComposition");
481 g_object_ref_sink (object);
482 track->priv->trackobjects =
483 g_list_insert_sorted (track->priv->trackobjects, object,
484 (GCompareFunc) objects_start_compare);
486 g_signal_emit (track, ges_track_signals[TRACK_OBJECT_ADDED], 0,
487 GES_TRACK_OBJECT (object));
489 g_signal_connect (GES_TRACK_OBJECT (object), "notify::start",
490 G_CALLBACK (sort_track_objects_cb), track);
492 g_signal_connect (GES_TRACK_OBJECT (object), "notify::priority",
493 G_CALLBACK (sort_track_objects_cb), track);
499 * ges_track_get_objects:
500 * @track: a #GESTrack
502 * Gets the #GESTrackObject contained in @track
504 * Returns: (transfer full) (element-type GESTrackObject): the list of
505 * #GESTrackObject present in the Track sorted by priority and start.
508 ges_track_get_objects (GESTrack * track)
513 g_return_val_if_fail (GES_IS_TRACK (track), NULL);
515 for (tmp = track->priv->trackobjects; tmp; tmp = tmp->next) {
516 ret = g_list_prepend (ret, tmp->data);
517 g_object_ref (tmp->data);
520 ret = g_list_reverse (ret);
525 * ges_track_remove_object:
526 * @track: a #GESTrack
527 * @object: the #GESTrackObject to remove
529 * Removes the object from the track and unparents it.
530 * Unparenting it means the reference owned by @track on the @object will be
531 * removed. If you wish to use the @object after this function, make sure you
532 * call g_object_ref() before removing it from the @track.
534 * Returns: #TRUE if the object was removed, else #FALSE if the track
535 * could not remove the object (like if it didn't belong to the track).
538 ges_track_remove_object (GESTrack * track, GESTrackObject * object)
540 GESTrackPrivate *priv;
541 GstElement *gnlobject;
543 g_return_val_if_fail (GES_IS_TRACK (track), FALSE);
544 g_return_val_if_fail (GES_IS_TRACK_OBJECT (object), FALSE);
546 GST_DEBUG ("track:%p, object:%p", track, object);
550 if (G_UNLIKELY (ges_track_object_get_track (object) != track)) {
551 GST_WARNING ("Object belongs to another track");
555 if ((gnlobject = ges_track_object_get_gnlobject (object))) {
556 GST_DEBUG ("Removing GnlObject '%s' from composition '%s'",
557 GST_ELEMENT_NAME (gnlobject), GST_ELEMENT_NAME (priv->composition));
558 /* We can't just set state of gnlobject to GST_STATE_NULL, because it will
559 * result in deadlock. Adding a ref to the gnlobj so we finalize it after
560 * removing it from the composition */
561 gst_object_ref (gnlobject);
562 if (!gst_bin_remove (GST_BIN (priv->composition), gnlobject)) {
563 GST_WARNING ("Failed to remove gnlobject from composition");
567 gst_element_set_state (gnlobject, GST_STATE_NULL);
568 /* Wait for the state change to actually happen */
569 gst_element_get_state (gnlobject, NULL, NULL, GST_CLOCK_TIME_NONE);
570 gst_object_unref (gnlobject);
573 g_signal_handlers_disconnect_by_func (object, sort_track_objects_cb, NULL);
575 ges_track_object_set_track (object, NULL);
577 g_signal_emit (track, ges_track_signals[TRACK_OBJECT_REMOVED], 0,
578 GES_TRACK_OBJECT (object));
580 priv->trackobjects = g_list_remove (priv->trackobjects, object);
582 g_object_unref (object);
588 pad_added_cb (GstElement * element, GstPad * pad, GESTrack * track)
590 GESTrackPrivate *priv = track->priv;
592 GST_DEBUG ("track:%p, pad %s:%s", track, GST_DEBUG_PAD_NAME (pad));
595 priv->srcpad = gst_ghost_pad_new ("src", pad);
597 gst_pad_set_active (priv->srcpad, TRUE);
599 gst_element_add_pad (GST_ELEMENT (track), priv->srcpad);
605 pad_removed_cb (GstElement * element, GstPad * pad, GESTrack * track)
607 GESTrackPrivate *priv = track->priv;
609 GST_DEBUG ("track:%p, pad %s:%s", track, GST_DEBUG_PAD_NAME (pad));
611 if (G_LIKELY (priv->srcpad)) {
612 gst_pad_set_active (priv->srcpad, FALSE);
613 gst_element_remove_pad (GST_ELEMENT (track), priv->srcpad);
621 composition_duration_cb (GstElement * composition,
622 GParamSpec * arg G_GNUC_UNUSED, GESTrack * obj)
626 g_object_get (composition, "duration", &duration, NULL);
629 if (obj->priv->duration != duration) {
630 GST_DEBUG ("composition duration : %" GST_TIME_FORMAT " current : %"
631 GST_TIME_FORMAT, GST_TIME_ARGS (duration),
632 GST_TIME_ARGS (obj->priv->duration));
634 obj->priv->duration = duration;
636 #if GLIB_CHECK_VERSION(2,26,0)
637 g_object_notify_by_pspec (G_OBJECT (obj), properties[ARG_DURATION]);
639 g_object_notify (G_OBJECT (obj), "duration");
645 sort_track_objects_cb (GESTrackObject * child,
646 GParamSpec * arg G_GNUC_UNUSED, GESTrack * track)
648 track->priv->trackobjects =
649 g_list_sort (track->priv->trackobjects,
650 (GCompareFunc) objects_start_compare);
654 timeline_duration_cb (GESTimeline * timeline,
655 GParamSpec * arg G_GNUC_UNUSED, GESTrack * track)
659 g_object_get (timeline, "duration", &duration, NULL);
660 g_object_set (GES_TRACK (track)->priv->background, "duration", duration,
663 GST_DEBUG_OBJECT (track, "Updating background duration to %" GST_TIME_FORMAT,
664 GST_TIME_ARGS (duration));
668 * ges_track_get_caps:
669 * @track: a #GESTrack
671 * Get the #GstCaps this track is configured to output.
673 * Returns: The #GstCaps this track is configured to output.
676 ges_track_get_caps (GESTrack * track)
678 g_return_val_if_fail (GES_IS_TRACK (track), NULL);
680 return track->priv->caps;
684 * ges_track_get_timeline:
685 * @track: a #GESTrack
687 * Get the #GESTimeline this track belongs to. Can be %NULL.
689 * Returns: The #GESTimeline this track belongs to. Can be %NULL.
692 ges_track_get_timeline (GESTrack * track)
694 g_return_val_if_fail (GES_IS_TRACK (track), NULL);
696 return track->priv->timeline;
700 * ges_track_enable_update:
701 * @track: a #GESTrack
702 * @enabled: Whether the track should update on every change or not.
704 * Control whether the track is updated for every change happening within.
706 * Users will want to use this method with %FALSE before doing lots of changes,
707 * and then call again with %TRUE for the changes to take effect in one go.
709 * Returns: %TRUE if the update status could be changed, else %FALSE.
712 ges_track_enable_update (GESTrack * track, gboolean enabled)
716 g_return_val_if_fail (GES_IS_TRACK (track), FALSE);
718 g_object_set (track->priv->composition, "update", enabled, NULL);
719 g_object_get (track->priv->composition, "update", &update, NULL);
721 track->priv->updating = update;
723 return update == enabled;
727 * ges_track_is_updating:
728 * @track: a #GESTrack
730 * Get whether the track is updated for every change happening within or not.
732 * Returns: %TRUE if @track is updating on every changes, else %FALSE.
735 ges_track_is_updating (GESTrack * track)
737 g_return_val_if_fail (GES_IS_TRACK (track), FALSE);
739 return track->priv->updating;