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.
22 * SECTION:ges-timeline-layer
23 * @short_description: Non-overlaping sequence of #GESTimelineObject
25 * Responsible for the ordering of the various contained TimelineObject(s). A
26 * timeline layer has a "priority" property, which is used to manage the
27 * priorities of individual TimelineObjects. Two layers should not have the
28 * same priority within a given timeline.
31 #include "ges-internal.h"
32 #include "gesmarshal.h"
33 #include "ges-timeline-layer.h"
35 #include "ges-timeline-source.h"
37 #define LAYER_HEIGHT 10
39 void track_object_added_cb (GESTimelineObject * object,
40 GESTrackObject * track_object, GESTimelineLayer * layer);
41 static void track_object_start_changed_cb (GESTrackObject * track_object,
42 GParamSpec * arg G_GNUC_UNUSED, GESTimelineObject * object);
43 void calculate_transition (GESTrackObject * track_object,
44 GESTimelineObject * object);
47 timeline_object_height_changed_cb (GESTimelineObject * obj,
48 GESTrackEffect * tr_eff, GESTimelineObject * second_obj);
50 G_DEFINE_TYPE (GESTimelineLayer, ges_timeline_layer, G_TYPE_INITIALLY_UNOWNED);
52 struct _GESTimelineLayerPrivate
55 GList *objects_start; /* The TimelineObjects sorted by start and
58 guint32 priority; /* The priority of the layer within the
59 * containing timeline */
61 gboolean auto_transition;
79 static guint ges_timeline_layer_signals[LAST_SIGNAL] = { 0 };
81 static gboolean ges_timeline_layer_resync_priorities (GESTimelineLayer * layer);
83 static GList *track_get_by_layer (GESTrackObject * track_object);
85 static void compare (GList * compared, GESTrackObject * track_object,
89 ges_timeline_layer_get_property (GObject * object, guint property_id,
90 GValue * value, GParamSpec * pspec)
92 GESTimelineLayer *layer = GES_TIMELINE_LAYER (object);
94 switch (property_id) {
96 g_value_set_uint (value, layer->priv->priority);
98 case PROP_AUTO_TRANSITION:
99 g_value_set_boolean (value, layer->priv->auto_transition);
102 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
107 ges_timeline_layer_set_property (GObject * object, guint property_id,
108 const GValue * value, GParamSpec * pspec)
110 GESTimelineLayer *layer = GES_TIMELINE_LAYER (object);
112 switch (property_id) {
114 ges_timeline_layer_set_priority (layer, g_value_get_uint (value));
116 case PROP_AUTO_TRANSITION:
117 ges_timeline_layer_set_auto_transition (layer,
118 g_value_get_boolean (value));
121 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
126 ges_timeline_layer_dispose (GObject * object)
128 GESTimelineLayer *layer = GES_TIMELINE_LAYER (object);
129 GESTimelineLayerPrivate *priv = layer->priv;
131 GST_DEBUG ("Disposing layer");
133 while (priv->objects_start)
134 ges_timeline_layer_remove_object (layer,
135 (GESTimelineObject *) priv->objects_start->data);
137 G_OBJECT_CLASS (ges_timeline_layer_parent_class)->dispose (object);
141 ges_timeline_layer_class_init (GESTimelineLayerClass * klass)
143 GObjectClass *object_class = G_OBJECT_CLASS (klass);
145 g_type_class_add_private (klass, sizeof (GESTimelineLayerPrivate));
147 object_class->get_property = ges_timeline_layer_get_property;
148 object_class->set_property = ges_timeline_layer_set_property;
149 object_class->dispose = ges_timeline_layer_dispose;
152 * GESTimelineLayer:priority
154 * The priority of the layer in the #GESTimeline. 0 is the highest
155 * priority. Conceptually, a #GESTimeline is a stack of GESTimelineLayers,
156 * and the priority of the layer represents its position in the stack. Two
157 * layers should not have the same priority within a given GESTimeline.
159 g_object_class_install_property (object_class, PROP_PRIORITY,
160 g_param_spec_uint ("priority", "Priority",
161 "The priority of the layer", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
164 * GESTimelineLayer:auto_transitioning
166 * Sets whether transitions are added automatically when timeline objects overlap
168 g_object_class_install_property (object_class, PROP_AUTO_TRANSITION,
169 g_param_spec_boolean ("auto-transition", "Auto-Transition",
170 "whether the transitions are added", FALSE, G_PARAM_READWRITE));
173 * GESTimelineLayer::object-added
174 * @layer: the #GESTimelineLayer
175 * @object: the #GESTimelineObject that was added.
177 * Will be emitted after the object was added to the layer.
179 ges_timeline_layer_signals[OBJECT_ADDED] =
180 g_signal_new ("object-added", G_TYPE_FROM_CLASS (klass),
181 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineLayerClass, object_added),
182 NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
183 GES_TYPE_TIMELINE_OBJECT);
186 * GESTimelineLayer::object-removed
187 * @layer: the #GESTimelineLayer
188 * @object: the #GESTimelineObject that was removed
190 * Will be emitted after the object was removed from the layer.
192 ges_timeline_layer_signals[OBJECT_REMOVED] =
193 g_signal_new ("object-removed", G_TYPE_FROM_CLASS (klass),
194 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineLayerClass,
195 object_removed), NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
196 GES_TYPE_TIMELINE_OBJECT);
200 ges_timeline_layer_init (GESTimelineLayer * self)
202 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
203 GES_TYPE_TIMELINE_LAYER, GESTimelineLayerPrivate);
205 self->priv->priority = 0;
206 self->priv->auto_transition = FALSE;
207 self->min_gnl_priority = 0;
208 self->max_gnl_priority = 9;
212 * ges_timeline_layer_new:
214 * Creates a new #GESTimelineLayer.
216 * Returns: A new #GESTimelineLayer
219 ges_timeline_layer_new (void)
221 return g_object_new (GES_TYPE_TIMELINE_LAYER, NULL);
225 ges_timeline_layer_set_timeline (GESTimelineLayer * layer,
226 GESTimeline * timeline)
228 GST_DEBUG ("layer:%p, timeline:%p", layer, timeline);
230 layer->timeline = timeline;
234 objects_start_compare (GESTimelineObject * a, GESTimelineObject * b)
236 if (a->start == b->start) {
237 if (a->priority < b->priority)
239 if (a->priority > b->priority)
243 if (a->start < b->start)
245 if (a->start > b->start)
251 * ges_timeline_layer_add_object:
252 * @layer: a #GESTimelineLayer
253 * @object: (transfer full): the #GESTimelineObject to add.
255 * Adds the given object to the layer. Sets the object's parent, and thus
256 * takes ownership of the object.
258 * An object can only be added to one layer.
260 * Returns: TRUE if the object was properly added to the layer, or FALSE
261 * if the @layer refuses to add the object.
265 track_get_by_layer (GESTrackObject * track_object)
268 GList *tck_objects_list = NULL, *tmp = NULL, *return_list = NULL;
269 GESTimelineLayer *layer;
270 GESTimelineObject *tl_obj;
271 gint priority, compared_priority;
273 track = ges_track_object_get_track (track_object);
274 tl_obj = ges_track_object_get_timeline_object (track_object);
275 layer = ges_timeline_object_get_layer (tl_obj);
276 priority = ges_timeline_layer_get_priority (layer);
278 tck_objects_list = ges_track_get_objects (track);
279 for (tmp = tck_objects_list; tmp; tmp = tmp->next) {
280 tl_obj = ges_track_object_get_timeline_object (tmp->data);
281 layer = ges_timeline_object_get_layer (tl_obj);
282 compared_priority = ges_timeline_layer_get_priority (layer);
283 if (compared_priority == priority) {
284 g_object_ref (tmp->data);
285 return_list = g_list_append (return_list, tmp->data);
289 for (tmp = tck_objects_list; tmp; tmp = tmp->next)
290 g_object_unref (tmp->data);
291 g_list_free (tck_objects_list);
296 ges_timeline_layer_add_object (GESTimelineLayer * layer,
297 GESTimelineObject * object)
299 GESTimelineLayer *tl_obj_layer;
300 guint32 maxprio, minprio, prio;
302 GST_DEBUG ("layer:%p, object:%p", layer, object);
304 tl_obj_layer = ges_timeline_object_get_layer (object);
306 if (G_UNLIKELY (tl_obj_layer)) {
307 GST_WARNING ("TimelineObject %p already belongs to another layer", object);
308 g_object_unref (tl_obj_layer);
312 g_object_ref_sink (object);
314 /* Take a reference to the object and store it stored by start/priority */
315 layer->priv->objects_start =
316 g_list_insert_sorted (layer->priv->objects_start, object,
317 (GCompareFunc) objects_start_compare);
319 if (layer->priv->auto_transition) {
320 if (GES_IS_TIMELINE_SOURCE (object)
321 && (ges_timeline_layer_get_priority (layer) != 99)) {
322 g_signal_connect (G_OBJECT (object), "track-object-added",
323 G_CALLBACK (track_object_added_cb), layer);
327 /* Inform the object it's now in this layer */
328 ges_timeline_object_set_layer (object, layer);
330 GST_DEBUG ("current object priority : %d, layer min/max : %d/%d",
331 GES_TIMELINE_OBJECT_PRIORITY (object),
332 layer->min_gnl_priority, layer->max_gnl_priority);
334 /* Set the priority. */
335 maxprio = layer->max_gnl_priority;
336 minprio = layer->min_gnl_priority;
337 prio = GES_TIMELINE_OBJECT_PRIORITY (object);
338 if (minprio + prio > (maxprio)) {
339 GST_WARNING ("%p is out of the layer %p space, setting its priority to "
340 "setting its priority %d to failthe maximum priority of the layer %d",
341 object, layer, prio, maxprio - minprio);
342 ges_timeline_object_set_priority (object, LAYER_HEIGHT - 1);
344 /* If the object has an acceptable priority, we just let it with its current
347 ges_timeline_layer_resync_priorities (layer);
349 /* emit 'object-added' */
350 g_signal_emit (layer, ges_timeline_layer_signals[OBJECT_ADDED], 0, object);
356 track_object_added_cb (GESTimelineObject * object,
357 GESTrackObject * track_object, GESTimelineLayer * layer)
359 if (GES_IS_TRACK_SOURCE (track_object)) {
360 g_signal_connect (G_OBJECT (track_object), "notify::start",
361 G_CALLBACK (track_object_start_changed_cb), object);
362 calculate_transition (track_object, object);
365 g_object_unref (layer);
370 timeline_object_height_changed_cb (GESTimelineObject * obj,
371 GESTrackEffect * tr_eff, GESTimelineObject * second_obj)
373 gint priority, height;
374 g_object_get (obj, "height", &height, "priority", &priority, NULL);
375 g_object_set (second_obj, "priority", priority + height, NULL);
379 track_object_start_changed_cb (GESTrackObject * track_object,
380 GParamSpec * arg G_GNUC_UNUSED, GESTimelineObject * object)
382 if (GES_IS_TRACK_SOURCE (track_object)) {
383 calculate_transition (track_object, object);
388 calculate_transition (GESTrackObject * track_object, GESTimelineObject * object)
390 GList *list, *cur, *compared, *compared_next, *tmp;
392 list = track_get_by_layer (track_object);
393 cur = g_list_find (list, track_object);
395 compared = cur->prev;
397 if (compared == NULL)
400 while (!GES_IS_TRACK_SOURCE (compared->data)) {
401 compared = compared->prev;
402 if (compared == NULL)
406 compare (compared, track_object, TRUE);
409 cur = g_list_find (list, track_object);
410 compared_next = cur->next;
412 if (compared_next == NULL)
415 while (!GES_IS_TRACK_SOURCE (compared_next->data)) {
416 compared_next = compared_next->next;
417 if (compared_next == NULL)
421 compare (compared_next, track_object, FALSE);
423 for (tmp = list; tmp; tmp = tmp->next)
424 g_object_unref (tmp->data);
430 compare (GList * compared, GESTrackObject * track_object, gboolean ahead)
433 gint64 start, duration, compared_start, compared_duration, end, compared_end,
434 tr_start, tr_duration;
435 GESTimelineStandardTransition *tr = NULL;
437 GESTimelineLayer *layer;
438 GESTimelineObject *object, *compared_object, *first_object, *second_object;
441 object = ges_track_object_get_timeline_object (track_object);
442 compared_object = ges_track_object_get_timeline_object (compared->data);
443 layer = ges_timeline_object_get_layer (object);
445 start = ges_track_object_get_start (track_object);
446 duration = ges_track_object_get_duration (track_object);
447 compared_start = ges_track_object_get_start (compared->data);
448 compared_duration = ges_track_object_get_duration (compared->data);
449 end = start + duration;
450 compared_end = compared_start + compared_duration;
453 for (tmp = compared->next; tmp; tmp = tmp->next) {
454 if GES_IS_TRACK_TRANSITION
456 g_object_get (tmp->data, "start", &tr_start, "duration", &tr_duration,
458 if (tr_start + tr_duration == compared_start + compared_duration) {
459 tr = GES_TIMELINE_STANDARD_TRANSITION
460 (ges_track_object_get_timeline_object (tmp->data));
466 if (compared_end <= start) {
468 ges_timeline_layer_remove_object (layer, GES_TIMELINE_OBJECT (tr));
469 g_object_get (compared_object, "priority", &priority, NULL);
470 g_object_set (object, "priority", priority, NULL);
473 } else if (start > compared_start && end < compared_end) {
475 ges_timeline_layer_remove_object (layer, GES_TIMELINE_OBJECT (tr));
477 if (start - compared_start < compared_end - end) {
478 ges_track_object_set_start (track_object, compared_end - duration + 1);
479 compare (compared, track_object, FALSE);
481 ges_track_object_set_start (track_object, compared_start - 1);
482 compare (compared, track_object, FALSE);
485 } else if (start <= compared_start) {
487 ges_timeline_layer_remove_object (layer, GES_TIMELINE_OBJECT (tr));
493 for (tmp = compared->prev; tmp; tmp = tmp->prev) {
494 if GES_IS_TRACK_TRANSITION
496 g_object_get (tmp->data, "start", &tr_start, "duration", &tr_duration,
498 if (tr_start == compared_start) {
499 tr = GES_TIMELINE_STANDARD_TRANSITION
500 (ges_track_object_get_timeline_object (tmp->data));
506 if (start + duration <= compared_start) {
508 ges_timeline_layer_remove_object (layer, GES_TIMELINE_OBJECT (tr));
509 g_object_get (object, "priority", &priority, NULL);
510 g_object_set (compared_object, "priority", priority, NULL);
513 } else if (start > compared_start) {
515 ges_timeline_layer_remove_object (layer, GES_TIMELINE_OBJECT (tr));
518 } else if (start < compared_start && end > compared_end) {
520 ges_timeline_layer_remove_object (layer, GES_TIMELINE_OBJECT (tr));
522 if (compared_start - start < end - compared_end) {
523 ges_track_object_set_start (track_object, compared_end - duration - 1);
524 compare (compared, track_object, TRUE);
526 ges_track_object_set_start (track_object, compared_start + 1);
534 tr = ges_timeline_standard_transition_new_for_nick ((gchar *) "crossfade");
535 track = ges_track_object_get_track (track_object);
537 ges_timeline_object_set_supported_formats (GES_TIMELINE_OBJECT (tr),
540 ges_timeline_layer_add_object (layer, GES_TIMELINE_OBJECT (tr));
543 first_object = ges_track_object_get_timeline_object (compared->data);
544 second_object = object;
546 second_object = ges_track_object_get_timeline_object (compared->data);
547 first_object = object;
550 g_object_get (first_object, "priority", &priority, "height", &height, NULL);
551 g_object_set (second_object, "priority", priority + height, NULL);
552 g_signal_connect (first_object, "notify::height",
553 (GCallback) timeline_object_height_changed_cb, second_object);
557 g_object_set (tr, "start", start, "duration",
558 compared_duration + compared_start - start, NULL);
560 g_object_set (tr, "start", compared_start, "duration",
561 start + duration - compared_start, NULL);
565 g_object_unref (track_object);
566 g_object_unref (layer);
571 * ges_timeline_layer_remove_object:
572 * @layer: a #GESTimelineLayer
573 * @object: the #GESTimelineObject to remove
575 * Removes the given @object from the @layer and unparents it.
576 * Unparenting it means the reference owned by @layer on the @object will be
577 * removed. If you wish to use the @object after this function, make sure you
578 * call g_object_ref() before removing it from the @layer.
580 * Returns: TRUE if the object could be removed, FALSE if the layer does
581 * not want to remove the object.
584 ges_timeline_layer_remove_object (GESTimelineLayer * layer,
585 GESTimelineObject * object)
587 GESTimelineLayer *tl_obj_layer;
589 g_return_val_if_fail (GES_IS_TIMELINE_LAYER (layer), FALSE);
590 g_return_val_if_fail (GES_IS_TIMELINE_OBJECT (object), FALSE);
592 GST_DEBUG ("layer:%p, object:%p", layer, object);
594 tl_obj_layer = ges_timeline_object_get_layer (object);
595 if (G_UNLIKELY (tl_obj_layer != layer)) {
596 GST_WARNING ("TimelineObject doesn't belong to this layer");
597 if (tl_obj_layer != NULL)
598 g_object_unref (tl_obj_layer);
601 g_object_unref (tl_obj_layer);
603 /* emit 'object-removed' */
604 g_signal_emit (layer, ges_timeline_layer_signals[OBJECT_REMOVED], 0, object);
606 /* inform the object it's no longer in a layer */
607 ges_timeline_object_set_layer (object, NULL);
609 /* Remove it from our list of controlled objects */
610 layer->priv->objects_start =
611 g_list_remove (layer->priv->objects_start, object);
613 /* Remove our reference to the object */
614 g_object_unref (object);
620 * ges_timeline_layer_resync_priorities:
621 * @layer: a #GESTimelineLayer
623 * Resyncs the priorities of the objects controlled by @layer.
627 ges_timeline_layer_resync_priorities (GESTimelineLayer * layer)
630 GESTimelineObject *obj;
632 GST_DEBUG ("Resync priorities of %p", layer);
634 /* TODO : Inhibit composition updates while doing this.
635 * Ideally we want to do it from an even higher level, but here will
636 * do in the meantime. */
638 for (tmp = layer->priv->objects_start; tmp; tmp = tmp->next) {
639 obj = GES_TIMELINE_OBJECT (tmp->data);
640 ges_timeline_object_set_priority (obj, GES_TIMELINE_OBJECT_PRIORITY (obj));
647 * ges_timeline_layer_set_priority:
648 * @layer: a #GESTimelineLayer
649 * @priority: the priority to set
651 * Sets the layer to the given @priority. See the documentation of the
652 * priority property for more information.
655 ges_timeline_layer_set_priority (GESTimelineLayer * layer, guint priority)
657 g_return_if_fail (GES_IS_TIMELINE_LAYER (layer));
659 GST_DEBUG ("layer:%p, priority:%d", layer, priority);
661 if (priority != layer->priv->priority) {
662 layer->priv->priority = priority;
663 layer->min_gnl_priority = (priority * LAYER_HEIGHT);
664 layer->max_gnl_priority = ((priority + 1) * LAYER_HEIGHT) - 1;
666 ges_timeline_layer_resync_priorities (layer);
671 * ges_timeline_layer_get_auto_transition:
672 * @layer: a #GESTimelineLayer
674 * Get the priority of @layer within the timeline.
676 * Returns: The priority of the @layer within the timeline.
679 ges_timeline_layer_get_auto_transition (GESTimelineLayer * layer)
681 g_return_val_if_fail (GES_IS_TIMELINE_LAYER (layer), 0);
683 return layer->priv->auto_transition;
687 * ges_timeline_layer_set_auto_transition:
688 * @layer: a #GESTimelineLayer
689 * @priority: whether the auto_transition is active
691 * Sets the layer to the given @auto_transition. See the documentation of the
692 * priority auto_transition for more information.
695 ges_timeline_layer_set_auto_transition (GESTimelineLayer * layer,
696 gboolean auto_transition)
698 g_return_if_fail (GES_IS_TIMELINE_LAYER (layer));
700 layer->priv->auto_transition = auto_transition;
704 * ges_timeline_layer_get_priority:
705 * @layer: a #GESTimelineLayer
707 * Get the priority of @layer within the timeline.
709 * Returns: The priority of the @layer within the timeline.
712 ges_timeline_layer_get_priority (GESTimelineLayer * layer)
714 g_return_val_if_fail (GES_IS_TIMELINE_LAYER (layer), 0);
716 return layer->priv->priority;
720 * ges_timeline_layer_get_objects:
721 * @layer: a #GESTimelineLayer
723 * Get the timeline objects this layer contains.
725 * Returns: (transfer full) (element-type GESTimelineObject): a #GList of
726 * timeline objects. The user is responsible for
727 * unreffing the contained objects and freeing the list.
731 ges_timeline_layer_get_objects (GESTimelineLayer * layer)
735 GESTimelineLayerClass *klass;
737 g_return_val_if_fail (GES_IS_TIMELINE_LAYER (layer), NULL);
739 klass = GES_TIMELINE_LAYER_GET_CLASS (layer);
741 if (klass->get_objects) {
742 return klass->get_objects (layer);
745 for (tmp = layer->priv->objects_start; tmp; tmp = tmp->next) {
746 ret = g_list_prepend (ret, tmp->data);
747 g_object_ref (tmp->data);
750 ret = g_list_reverse (ret);