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 /* TODO : Keep those 3 values in sync */
170 self->priv->priority = 0;
171 self->min_gnl_priority = 0;
172 self->max_gnl_priority = 9;
176 * ges_timeline_layer_new:
178 * Creates a new #GESTimelineLayer.
180 * Returns: A new #GESTimelineLayer
183 ges_timeline_layer_new (void)
185 return g_object_new (GES_TYPE_TIMELINE_LAYER, NULL);
189 ges_timeline_layer_set_timeline (GESTimelineLayer * layer,
190 GESTimeline * timeline)
192 GST_DEBUG ("layer:%p, timeline:%p", layer, timeline);
194 layer->timeline = timeline;
198 objects_start_compare (GESTimelineObject * a, GESTimelineObject * b)
200 if (a->start == b->start) {
201 if (a->priority < b->priority)
203 if (a->priority > b->priority)
207 if (a->start < b->start)
209 if (a->start > b->start)
215 * ges_timeline_layer_add_object:
216 * @layer: a #GESTimelineLayer
217 * @object: (transfer full): the #GESTimelineObject to add.
219 * Adds the given object to the layer. Sets the object's parent, and thus
220 * takes ownership of the object.
222 * An object can only be added to one layer.
224 * Returns: TRUE if the object was properly added to the layer, or FALSE
225 * if the @layer refuses to add the object.
229 ges_timeline_layer_add_object (GESTimelineLayer * layer,
230 GESTimelineObject * object)
232 GESTimelineLayer *tl_obj_layer;
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 if (GES_TIMELINE_OBJECT_PRIORITY (object) > (layer->max_gnl_priority)) {
260 ges_timeline_object_set_priority (object, layer->max_gnl_priority);
263 else if (GES_TIMELINE_OBJECT_PRIORITY (object) < (layer->min_gnl_priority)) {
264 ges_timeline_object_set_priority (object, layer->min_gnl_priority);
267 /* emit 'object-added' */
268 g_signal_emit (layer, ges_timeline_layer_signals[OBJECT_ADDED], 0, object);
274 * ges_timeline_layer_remove_object:
275 * @layer: a #GESTimelineLayer
276 * @object: the #GESTimelineObject to remove
278 * Removes the given @object from the @layer and unparents it.
279 * Unparenting it means the reference owned by @layer on the @object will be
280 * removed. If you wish to use the @object after this function, make sure you
281 * call g_object_ref() before removing it from the @layer.
283 * Returns: TRUE if the object could be removed, FALSE if the layer does
284 * not want to remove the object.
287 ges_timeline_layer_remove_object (GESTimelineLayer * layer,
288 GESTimelineObject * object)
290 GESTimelineLayer *tl_obj_layer;
292 g_return_val_if_fail (GES_IS_TIMELINE_LAYER (layer), FALSE);
293 g_return_val_if_fail (GES_IS_TIMELINE_OBJECT (object), FALSE);
295 GST_DEBUG ("layer:%p, object:%p", layer, object);
297 tl_obj_layer = ges_timeline_object_get_layer (object);
298 if (G_UNLIKELY (tl_obj_layer != layer)) {
299 GST_WARNING ("TimelineObject doesn't belong to this layer");
300 if (tl_obj_layer != NULL)
301 g_object_unref (tl_obj_layer);
304 g_object_unref (tl_obj_layer);
306 /* emit 'object-removed' */
307 g_signal_emit (layer, ges_timeline_layer_signals[OBJECT_REMOVED], 0, object);
309 /* inform the object it's no longer in a layer */
310 ges_timeline_object_set_layer (object, NULL);
312 /* Remove it from our list of controlled objects */
313 layer->priv->objects_start =
314 g_slist_remove (layer->priv->objects_start, object);
316 /* Remove our reference to the object */
317 g_object_unref (object);
323 * ges_timeline_layer_resync_priorities:
324 * @layer: a #GESTimelineLayer
326 * Resyncs the priorities of the objects controlled by @layer.
330 ges_timeline_layer_resync_priorities (GESTimelineLayer * layer)
334 /* TODO : Inhibit composition updates while doing this.
335 * Ideally we want to do it from an even higher level, but here will
336 * do in the meantime. */
338 /* TODO : This is the dumb version where we put everything linearly,
339 * will need to be adjusted for more complex usages (like with
341 for (tmp = layer->priv->objects_start; tmp; tmp = tmp->next) {
342 ges_timeline_object_set_priority ((GESTimelineObject *) tmp->data,
343 layer->min_gnl_priority);
350 * ges_timeline_layer_set_priority:
351 * @layer: a #GESTimelineLayer
352 * @priority: the priority to set
354 * Sets the layer to the given @priority. See the documentation of the
355 * priority property for more information.
358 ges_timeline_layer_set_priority (GESTimelineLayer * layer, guint priority)
360 g_return_if_fail (GES_IS_TIMELINE_LAYER (layer));
362 GST_DEBUG ("layer:%p, priority:%d", layer, priority);
364 if (priority != layer->priv->priority) {
365 layer->priv->priority = priority;
366 layer->min_gnl_priority = (priority * LAYER_HEIGHT);
367 layer->max_gnl_priority = ((priority + 1) * LAYER_HEIGHT) - 1;
369 /* FIXME : Update controlled object's gnl priority accordingly */
370 ges_timeline_layer_resync_priorities (layer);
375 * ges_timeline_layer_get_priority:
376 * @layer: a #GESTimelineLayer
378 * Get the priority of @layer within the timeline.
380 * Returns: The priority of the @layer within the timeline.
383 ges_timeline_layer_get_priority (GESTimelineLayer * layer)
385 g_return_val_if_fail (GES_IS_TIMELINE_LAYER (layer), 0);
387 return layer->priv->priority;
391 * ges_timeline_layer_get_objects:
392 * @layer: a #GESTimelineLayer
394 * Get the timeline objects this layer contains.
396 * Returns: (transfer full) (element-type GESTimelineObject): a #GList of
397 * timeline objects. The user is responsible for
398 * unreffing the contained objects and freeing the list.
402 ges_timeline_layer_get_objects (GESTimelineLayer * layer)
406 GESTimelineLayerClass *klass;
408 g_return_val_if_fail (GES_IS_TIMELINE_LAYER (layer), NULL);
410 klass = GES_TIMELINE_LAYER_GET_CLASS (layer);
412 if (klass->get_objects) {
413 return klass->get_objects (layer);
416 for (tmp = layer->priv->objects_start; tmp; tmp = tmp->next) {
417 ret = g_list_prepend (ret, tmp->data);
418 g_object_ref (tmp->data);
421 ret = g_list_reverse (ret);