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"
36 #define LAYER_HEIGHT 10
38 G_DEFINE_TYPE (GESTimelineLayer, ges_timeline_layer, G_TYPE_INITIALLY_UNOWNED);
40 struct _GESTimelineLayerPrivate
43 GSList *objects_start; /* The TimelineObjects sorted by start and
46 guint32 priority; /* The priority of the layer within the
47 * containing timeline */
63 static guint ges_timeline_layer_signals[LAST_SIGNAL] = { 0 };
65 static gboolean ges_timeline_layer_resync_priorities (GESTimelineLayer * layer);
68 ges_timeline_layer_get_property (GObject * object, guint property_id,
69 GValue * value, GParamSpec * pspec)
71 GESTimelineLayer *layer = GES_TIMELINE_LAYER (object);
73 switch (property_id) {
75 g_value_set_uint (value, layer->priv->priority);
78 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
83 ges_timeline_layer_set_property (GObject * object, guint property_id,
84 const GValue * value, GParamSpec * pspec)
86 GESTimelineLayer *layer = GES_TIMELINE_LAYER (object);
88 switch (property_id) {
90 ges_timeline_layer_set_priority (layer, g_value_get_uint (value));
93 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
98 ges_timeline_layer_dispose (GObject * object)
100 GESTimelineLayer *layer = GES_TIMELINE_LAYER (object);
101 GESTimelineLayerPrivate *priv = layer->priv;
103 GST_DEBUG ("Disposing layer");
105 while (priv->objects_start)
106 ges_timeline_layer_remove_object (layer,
107 (GESTimelineObject *) priv->objects_start->data);
109 G_OBJECT_CLASS (ges_timeline_layer_parent_class)->dispose (object);
113 ges_timeline_layer_class_init (GESTimelineLayerClass * klass)
115 GObjectClass *object_class = G_OBJECT_CLASS (klass);
117 g_type_class_add_private (klass, sizeof (GESTimelineLayerPrivate));
119 object_class->get_property = ges_timeline_layer_get_property;
120 object_class->set_property = ges_timeline_layer_set_property;
121 object_class->dispose = ges_timeline_layer_dispose;
124 * GESTimelineLayer:priority
126 * The priority of the layer in the #GESTimeline. 0 is the highest
127 * priority. Conceptually, a #GESTimeline is a stack of GESTimelineLayers,
128 * and the priority of the layer represents its position in the stack. Two
129 * layers should not have the same priority within a given GESTimeline.
131 g_object_class_install_property (object_class, PROP_PRIORITY,
132 g_param_spec_uint ("priority", "Priority",
133 "The priority of the layer", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
136 * GESTimelineLayer::object-added
137 * @layer: the #GESTimelineLayer
138 * @object: the #GESTimelineObject that was added.
140 * Will be emitted after the object was added to the layer.
142 ges_timeline_layer_signals[OBJECT_ADDED] =
143 g_signal_new ("object-added", G_TYPE_FROM_CLASS (klass),
144 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineLayerClass, object_added),
145 NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
146 GES_TYPE_TIMELINE_OBJECT);
149 * GESTimelineLayer::object-removed
150 * @layer: the #GESTimelineLayer
151 * @object: the #GESTimelineObject that was removed
153 * Will be emitted after the object was removed from the layer.
155 ges_timeline_layer_signals[OBJECT_REMOVED] =
156 g_signal_new ("object-removed", G_TYPE_FROM_CLASS (klass),
157 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineLayerClass,
158 object_removed), NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
159 GES_TYPE_TIMELINE_OBJECT);
164 ges_timeline_layer_init (GESTimelineLayer * self)
166 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
167 GES_TYPE_TIMELINE_LAYER, GESTimelineLayerPrivate);
169 self->priv->priority = 0;
170 self->min_gnl_priority = 0;
171 self->max_gnl_priority = 9;
175 * ges_timeline_layer_new:
177 * Creates a new #GESTimelineLayer.
179 * Returns: A new #GESTimelineLayer
182 ges_timeline_layer_new (void)
184 return g_object_new (GES_TYPE_TIMELINE_LAYER, NULL);
188 ges_timeline_layer_set_timeline (GESTimelineLayer * layer,
189 GESTimeline * timeline)
191 GST_DEBUG ("layer:%p, timeline:%p", layer, timeline);
193 layer->timeline = timeline;
197 objects_start_compare (GESTimelineObject * a, GESTimelineObject * b)
199 if (a->start == b->start) {
200 if (a->priority < b->priority)
202 if (a->priority > b->priority)
206 if (a->start < b->start)
208 if (a->start > b->start)
214 * ges_timeline_layer_add_object:
215 * @layer: a #GESTimelineLayer
216 * @object: (transfer full): the #GESTimelineObject to add.
218 * Adds the given object to the layer. Sets the object's parent, and thus
219 * takes ownership of the object.
221 * An object can only be added to one layer.
223 * Returns: TRUE if the object was properly added to the layer, or FALSE
224 * if the @layer refuses to add the object.
228 ges_timeline_layer_add_object (GESTimelineLayer * layer,
229 GESTimelineObject * object)
231 GESTimelineLayer *tl_obj_layer;
232 guint32 maxprio, minprio, prio;
234 GST_DEBUG ("layer:%p, object:%p", layer, object);
236 tl_obj_layer = ges_timeline_object_get_layer (object);
238 if (G_UNLIKELY (tl_obj_layer)) {
239 GST_WARNING ("TimelineObject %p already belongs to another layer", object);
240 g_object_unref (tl_obj_layer);
244 g_object_ref_sink (object);
246 /* Take a reference to the object and store it stored by start/priority */
247 layer->priv->objects_start =
248 g_slist_insert_sorted (layer->priv->objects_start, object,
249 (GCompareFunc) objects_start_compare);
251 /* Inform the object it's now in this layer */
252 ges_timeline_object_set_layer (object, layer);
254 GST_DEBUG ("current object priority : %d, layer min/max : %d/%d",
255 GES_TIMELINE_OBJECT_PRIORITY (object),
256 layer->min_gnl_priority, layer->max_gnl_priority);
258 /* Set the priority. */
259 maxprio = layer->max_gnl_priority;
260 minprio = layer->min_gnl_priority;
261 prio = GES_TIMELINE_OBJECT_PRIORITY (object);
262 if (minprio + prio > (maxprio)) {
263 GST_WARNING ("%p is out of the layer %p space, setting its priority to "
264 "setting its priority %d to the maximum priority of the layer %d",
265 object, layer, prio, maxprio - minprio);
266 ges_timeline_object_set_priority (object, LAYER_HEIGHT - 1);
268 /* If the object has an acceptable priority, we just let it with its current
271 ges_timeline_layer_resync_priorities (layer);
273 /* emit 'object-added' */
274 g_signal_emit (layer, ges_timeline_layer_signals[OBJECT_ADDED], 0, object);
280 * ges_timeline_layer_remove_object:
281 * @layer: a #GESTimelineLayer
282 * @object: the #GESTimelineObject to remove
284 * Removes the given @object from the @layer and unparents it.
285 * Unparenting it means the reference owned by @layer on the @object will be
286 * removed. If you wish to use the @object after this function, make sure you
287 * call g_object_ref() before removing it from the @layer.
289 * Returns: TRUE if the object could be removed, FALSE if the layer does
290 * not want to remove the object.
293 ges_timeline_layer_remove_object (GESTimelineLayer * layer,
294 GESTimelineObject * object)
296 GESTimelineLayer *tl_obj_layer;
298 g_return_val_if_fail (GES_IS_TIMELINE_LAYER (layer), FALSE);
299 g_return_val_if_fail (GES_IS_TIMELINE_OBJECT (object), FALSE);
301 GST_DEBUG ("layer:%p, object:%p", layer, object);
303 tl_obj_layer = ges_timeline_object_get_layer (object);
304 if (G_UNLIKELY (tl_obj_layer != layer)) {
305 GST_WARNING ("TimelineObject doesn't belong to this layer");
306 if (tl_obj_layer != NULL)
307 g_object_unref (tl_obj_layer);
310 g_object_unref (tl_obj_layer);
312 /* emit 'object-removed' */
313 g_signal_emit (layer, ges_timeline_layer_signals[OBJECT_REMOVED], 0, object);
315 /* inform the object it's no longer in a layer */
316 ges_timeline_object_set_layer (object, NULL);
318 /* Remove it from our list of controlled objects */
319 layer->priv->objects_start =
320 g_slist_remove (layer->priv->objects_start, object);
322 /* Remove our reference to the object */
323 g_object_unref (object);
329 * ges_timeline_layer_resync_priorities:
330 * @layer: a #GESTimelineLayer
332 * Resyncs the priorities of the objects controlled by @layer.
336 ges_timeline_layer_resync_priorities (GESTimelineLayer * layer)
339 GESTimelineObject *obj;
341 GST_DEBUG ("Resync priorities of %p", layer);
343 /* TODO : Inhibit composition updates while doing this.
344 * Ideally we want to do it from an even higher level, but here will
345 * do in the meantime. */
347 for (tmp = layer->priv->objects_start; tmp; tmp = tmp->next) {
348 obj = GES_TIMELINE_OBJECT (tmp->data);
349 ges_timeline_object_set_priority (obj, GES_TIMELINE_OBJECT_PRIORITY (obj));
356 * ges_timeline_layer_set_priority:
357 * @layer: a #GESTimelineLayer
358 * @priority: the priority to set
360 * Sets the layer to the given @priority. See the documentation of the
361 * priority property for more information.
364 ges_timeline_layer_set_priority (GESTimelineLayer * layer, guint priority)
366 g_return_if_fail (GES_IS_TIMELINE_LAYER (layer));
368 GST_DEBUG ("layer:%p, priority:%d", layer, priority);
370 if (priority != layer->priv->priority) {
371 layer->priv->priority = priority;
372 layer->min_gnl_priority = (priority * LAYER_HEIGHT);
373 layer->max_gnl_priority = ((priority + 1) * LAYER_HEIGHT) - 1;
375 ges_timeline_layer_resync_priorities (layer);
380 * ges_timeline_layer_get_priority:
381 * @layer: a #GESTimelineLayer
383 * Get the priority of @layer within the timeline.
385 * Returns: The priority of the @layer within the timeline.
388 ges_timeline_layer_get_priority (GESTimelineLayer * layer)
390 g_return_val_if_fail (GES_IS_TIMELINE_LAYER (layer), 0);
392 return layer->priv->priority;
396 * ges_timeline_layer_get_objects:
397 * @layer: a #GESTimelineLayer
399 * Get the timeline objects this layer contains.
401 * Returns: (transfer full) (element-type GESTimelineObject): a #GList of
402 * timeline objects. The user is responsible for
403 * unreffing the contained objects and freeing the list.
407 ges_timeline_layer_get_objects (GESTimelineLayer * layer)
411 GESTimelineLayerClass *klass;
413 g_return_val_if_fail (GES_IS_TIMELINE_LAYER (layer), NULL);
415 klass = GES_TIMELINE_LAYER_GET_CLASS (layer);
417 if (klass->get_objects) {
418 return klass->get_objects (layer);
421 for (tmp = layer->priv->objects_start; tmp; tmp = tmp->next) {
422 ret = g_list_prepend (ret, tmp->data);
423 g_object_ref (tmp->data);
426 ret = g_list_reverse (ret);