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 G_DEFINE_TYPE (GESTimelineLayer, ges_timeline_layer, G_TYPE_OBJECT);
38 struct _GESTimelineLayerPrivate
41 GSList *objects_start; /* The TimelineObjects sorted by start and
44 guint32 priority; /* The priority of the layer within the
45 * containing timeline */
61 static guint ges_timeline_layer_signals[LAST_SIGNAL] = { 0 };
63 static gboolean ges_timeline_layer_resync_priorities (GESTimelineLayer * layer);
66 ges_timeline_layer_get_property (GObject * object, guint property_id,
67 GValue * value, GParamSpec * pspec)
69 GESTimelineLayer *layer = GES_TIMELINE_LAYER (object);
71 switch (property_id) {
73 g_value_set_uint (value, layer->priv->priority);
76 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
81 ges_timeline_layer_set_property (GObject * object, guint property_id,
82 const GValue * value, GParamSpec * pspec)
84 GESTimelineLayer *layer = GES_TIMELINE_LAYER (object);
86 switch (property_id) {
88 ges_timeline_layer_set_priority (layer, g_value_get_uint (value));
91 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
96 ges_timeline_layer_dispose (GObject * object)
98 GESTimelineLayer *layer = GES_TIMELINE_LAYER (object);
99 GESTimelineLayerPrivate *priv = layer->priv;
101 GST_DEBUG ("Disposing layer");
103 while (priv->objects_start)
104 ges_timeline_layer_remove_object (layer,
105 (GESTimelineObject *) priv->objects_start->data);
107 G_OBJECT_CLASS (ges_timeline_layer_parent_class)->dispose (object);
111 ges_timeline_layer_class_init (GESTimelineLayerClass * klass)
113 GObjectClass *object_class = G_OBJECT_CLASS (klass);
115 g_type_class_add_private (klass, sizeof (GESTimelineLayerPrivate));
117 object_class->get_property = ges_timeline_layer_get_property;
118 object_class->set_property = ges_timeline_layer_set_property;
119 object_class->dispose = ges_timeline_layer_dispose;
122 * GESTimelineLayer:priority
124 * The priority of the layer in the #GESTimeline. 0 is the highest
125 * priority. Conceptually, a #GESTimeline is a stack of GESTimelineLayers,
126 * and the priority of the layer represents its position in the stack. Two
127 * layers should not have the same priority within a given GESTimeline.
129 g_object_class_install_property (object_class, PROP_PRIORITY,
130 g_param_spec_uint ("priority", "Priority",
131 "The priority of the layer", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
134 * GESTimelineLayer::object-added
135 * @layer: the #GESTimelineLayer
136 * @object: the #GESTimelineObject that was added.
138 * Will be emitted after the object was added to the layer.
140 ges_timeline_layer_signals[OBJECT_ADDED] =
141 g_signal_new ("object-added", G_TYPE_FROM_CLASS (klass),
142 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineLayerClass, object_added),
143 NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
144 GES_TYPE_TIMELINE_OBJECT);
147 * GESTimelineLayer::object-removed
148 * @layer: the #GESTimelineLayer
149 * @object: the #GESTimelineObject that was removed
151 * Will be emitted after the object was removed from the layer.
153 ges_timeline_layer_signals[OBJECT_REMOVED] =
154 g_signal_new ("object-removed", G_TYPE_FROM_CLASS (klass),
155 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineLayerClass,
156 object_removed), NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
157 GES_TYPE_TIMELINE_OBJECT);
162 ges_timeline_layer_init (GESTimelineLayer * self)
164 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
165 GES_TYPE_TIMELINE_LAYER, GESTimelineLayerPrivate);
167 /* TODO : Keep those 3 values in sync */
168 self->priv->priority = 0;
169 self->min_gnl_priority = 0;
170 self->max_gnl_priority = 9;
174 * ges_timeline_layer_new:
176 * Creates a new #GESTimelineLayer.
178 * Returns: A new #GESTimelineLayer
181 ges_timeline_layer_new (void)
183 return g_object_new (GES_TYPE_TIMELINE_LAYER, NULL);
187 ges_timeline_layer_set_timeline (GESTimelineLayer * layer,
188 GESTimeline * timeline)
190 GST_DEBUG ("layer:%p, timeline:%p", layer, timeline);
192 layer->timeline = timeline;
196 objects_start_compare (GESTimelineObject * a, GESTimelineObject * b)
198 if (a->start == b->start) {
199 if (a->priority < b->priority)
201 if (a->priority > b->priority)
205 if (a->start < b->start)
207 if (a->start > b->start)
213 * ges_timeline_layer_add_object:
214 * @layer: a #GESTimelineLayer
215 * @object: the #GESTimelineObject to add.
217 * Adds the object to the layer. The layer will steal a reference to the
220 * Returns: TRUE if the object was properly added to the layer, or FALSE
221 * if the @layer refused to add the object.
225 ges_timeline_layer_add_object (GESTimelineLayer * layer,
226 GESTimelineObject * object)
228 GESTimelineLayer *tl_obj_layer;
230 GST_DEBUG ("layer:%p, object:%p", layer, object);
232 tl_obj_layer = ges_timeline_object_get_layer (object);
233 if (G_UNLIKELY (tl_obj_layer)) {
234 GST_WARNING ("TimelineObject %p already belongs to another layer");
235 g_object_unref (tl_obj_layer);
239 /* Take a reference to the object and store it stored by start/priority */
240 layer->priv->objects_start =
241 g_slist_insert_sorted (layer->priv->objects_start, object,
242 (GCompareFunc) objects_start_compare);
244 /* Inform the object it's now in this layer */
245 ges_timeline_object_set_layer (object, layer);
247 GST_DEBUG ("current object priority : %d, layer min/max : %d/%d",
248 GES_TIMELINE_OBJECT_PRIORITY (object),
249 layer->min_gnl_priority, layer->max_gnl_priority);
251 /* Set the priority. */
252 if (GES_TIMELINE_OBJECT_PRIORITY (object) > (layer->max_gnl_priority)) {
253 ges_timeline_object_set_priority (object, layer->max_gnl_priority);
256 else if (GES_TIMELINE_OBJECT_PRIORITY (object) < (layer->min_gnl_priority)) {
257 ges_timeline_object_set_priority (object, layer->min_gnl_priority);
260 /* emit 'object-added' */
261 g_signal_emit (layer, ges_timeline_layer_signals[OBJECT_ADDED], 0, object);
267 * ges_timeline_layer_remove_object:
268 * @layer: a #GESTimelineLayer
269 * @object: the #GESTimelineObject to remove
271 * Removes the given @object from the @layer. The reference stolen by the @layer
272 * when the object was added will be removed. If you wish to use the object after
273 * this function, make sure you take an extra reference to the object before
274 * calling this function.
276 * Returns: TRUE if the object was properly remove, else FALSE.
279 ges_timeline_layer_remove_object (GESTimelineLayer * layer,
280 GESTimelineObject * object)
282 GESTimelineLayer *tl_obj_layer;
283 GST_DEBUG ("layer:%p, object:%p", layer, object);
285 tl_obj_layer = ges_timeline_object_get_layer (object);
286 if (G_UNLIKELY (tl_obj_layer != layer)) {
287 GST_WARNING ("TimelineObject doesn't belong to this layer");
288 if (tl_obj_layer != NULL)
289 g_object_unref (tl_obj_layer);
292 g_object_unref (tl_obj_layer);
294 /* emit 'object-removed' */
295 g_signal_emit (layer, ges_timeline_layer_signals[OBJECT_REMOVED], 0, object);
297 /* inform the object it's no longer in a layer */
298 ges_timeline_object_set_layer (object, NULL);
300 /* Remove it from our list of controlled objects */
301 layer->priv->objects_start =
302 g_slist_remove (layer->priv->objects_start, object);
304 /* Remove our reference to the object */
305 g_object_unref (object);
311 * ges_timeline_layer_resync_priorities:
312 * @layer: a #GESTimelineLayer
314 * Resyncs the priorities of the objects controlled by @layer.
318 ges_timeline_layer_resync_priorities (GESTimelineLayer * layer)
322 /* TODO : Inhibit composition updates while doing this.
323 * Ideally we want to do it from an even higher level, but here will
324 * do in the meantime. */
326 /* TODO : This is the dumb version where we put everything linearly,
327 * will need to be adjusted for more complex usages (like with
329 for (tmp = layer->priv->objects_start; tmp; tmp = tmp->next) {
330 ges_timeline_object_set_priority ((GESTimelineObject *) tmp->data,
331 layer->min_gnl_priority);
338 * ges_timeline_layer_set_priority:
339 * @layer: a #GESTimelineLayer
340 * @priority: the priority to set
342 * Sets the layer to the given @priority. See the documentation of the
343 * priority property for more information.
346 ges_timeline_layer_set_priority (GESTimelineLayer * layer, guint priority)
348 g_return_if_fail (GES_IS_TIMELINE_LAYER (layer));
350 GST_DEBUG ("layer:%p, priority:%d", layer, priority);
352 if (priority != layer->priv->priority) {
353 layer->priv->priority = priority;
354 layer->min_gnl_priority = (priority * 10);
355 layer->max_gnl_priority = ((priority + 1) * 10) - 1;
357 /* FIXME : Update controlled object's gnl priority accordingly */
358 ges_timeline_layer_resync_priorities (layer);
363 * ges_timeline_layer_get_priority:
364 * @layer: a #GESTimelineLayer
366 * Returns: the priority of the @layer within the timeline.
369 ges_timeline_layer_get_priority (GESTimelineLayer * layer)
371 g_return_val_if_fail (GES_IS_TIMELINE_LAYER (layer), 0);
373 return layer->priv->priority;
377 * ges_timeline_layer_get_objects:
378 * @layer: a #GESTimelineLayer
380 * Get the timeline objects this layer contains.
382 * Returns: (transfer full) (element-type GESTimelineObject): a #GList of
383 * timeline objects. The user is responsible for
384 * unreffing the contained objects and freeing the list.
388 ges_timeline_layer_get_objects (GESTimelineLayer * layer)
392 GESTimelineLayerClass *klass;
394 g_return_val_if_fail (GES_IS_TIMELINE_LAYER (layer), NULL);
396 klass = GES_TIMELINE_LAYER_GET_CLASS (layer);
398 if (klass->get_objects) {
399 return klass->get_objects (layer);
402 for (tmp = layer->priv->objects_start; tmp; tmp = tmp->next) {
403 ret = g_list_prepend (ret, tmp->data);
404 g_object_ref (tmp->data);
407 ret = g_list_reverse (ret);