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_INITIALLY_UNOWNED);
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: (transfer full): the #GESTimelineObject to add.
217 * Adds the given object to the layer. Sets the object's parent, and thus
218 * takes ownership of the object.
220 * An object can only be added to one layer.
222 * Returns: TRUE if the object was properly added to the layer, or FALSE
223 * if the @layer refuses to add the object.
227 ges_timeline_layer_add_object (GESTimelineLayer * layer,
228 GESTimelineObject * object)
230 GESTimelineLayer *tl_obj_layer;
232 GST_DEBUG ("layer:%p, object:%p", layer, object);
234 tl_obj_layer = ges_timeline_object_get_layer (object);
236 if (G_UNLIKELY (tl_obj_layer)) {
237 GST_WARNING ("TimelineObject %p already belongs to another layer");
238 g_object_unref (tl_obj_layer);
242 g_object_ref_sink (object);
244 /* Take a reference to the object and store it stored by start/priority */
245 layer->priv->objects_start =
246 g_slist_insert_sorted (layer->priv->objects_start, object,
247 (GCompareFunc) objects_start_compare);
249 /* Inform the object it's now in this layer */
250 ges_timeline_object_set_layer (object, layer);
252 GST_DEBUG ("current object priority : %d, layer min/max : %d/%d",
253 GES_TIMELINE_OBJECT_PRIORITY (object),
254 layer->min_gnl_priority, layer->max_gnl_priority);
256 /* Set the priority. */
257 if (GES_TIMELINE_OBJECT_PRIORITY (object) > (layer->max_gnl_priority)) {
258 ges_timeline_object_set_priority (object, layer->max_gnl_priority);
261 else if (GES_TIMELINE_OBJECT_PRIORITY (object) < (layer->min_gnl_priority)) {
262 ges_timeline_object_set_priority (object, layer->min_gnl_priority);
265 /* emit 'object-added' */
266 g_signal_emit (layer, ges_timeline_layer_signals[OBJECT_ADDED], 0, object);
272 * ges_timeline_layer_remove_object:
273 * @layer: a #GESTimelineLayer
274 * @object: the #GESTimelineObject to remove
276 * Removes the given @object from the @layer and unparents it.
277 * Unparenting it means the reference owned by @layer on the @object will be
278 * removed. If you wish to use the @object after this function, make sure you
279 * call g_object_ref() before removing it from the @layer.
281 * Returns: TRUE if the object could be removed, FALSE if the layer does
282 * not want to remove the object.
285 ges_timeline_layer_remove_object (GESTimelineLayer * layer,
286 GESTimelineObject * object)
288 GESTimelineLayer *tl_obj_layer;
290 g_return_val_if_fail (GES_IS_TIMELINE_LAYER (layer), FALSE);
291 g_return_val_if_fail (GES_IS_TIMELINE_OBJECT (object), FALSE);
293 GST_DEBUG ("layer:%p, object:%p", layer, object);
295 tl_obj_layer = ges_timeline_object_get_layer (object);
296 if (G_UNLIKELY (tl_obj_layer != layer)) {
297 GST_WARNING ("TimelineObject doesn't belong to this layer");
298 if (tl_obj_layer != NULL)
299 g_object_unref (tl_obj_layer);
302 g_object_unref (tl_obj_layer);
304 /* emit 'object-removed' */
305 g_signal_emit (layer, ges_timeline_layer_signals[OBJECT_REMOVED], 0, object);
307 /* inform the object it's no longer in a layer */
308 ges_timeline_object_set_layer (object, NULL);
310 /* Remove it from our list of controlled objects */
311 layer->priv->objects_start =
312 g_slist_remove (layer->priv->objects_start, object);
314 /* Remove our reference to the object */
315 g_object_unref (object);
321 * ges_timeline_layer_resync_priorities:
322 * @layer: a #GESTimelineLayer
324 * Resyncs the priorities of the objects controlled by @layer.
328 ges_timeline_layer_resync_priorities (GESTimelineLayer * layer)
332 /* TODO : Inhibit composition updates while doing this.
333 * Ideally we want to do it from an even higher level, but here will
334 * do in the meantime. */
336 /* TODO : This is the dumb version where we put everything linearly,
337 * will need to be adjusted for more complex usages (like with
339 for (tmp = layer->priv->objects_start; tmp; tmp = tmp->next) {
340 ges_timeline_object_set_priority ((GESTimelineObject *) tmp->data,
341 layer->min_gnl_priority);
348 * ges_timeline_layer_set_priority:
349 * @layer: a #GESTimelineLayer
350 * @priority: the priority to set
352 * Sets the layer to the given @priority. See the documentation of the
353 * priority property for more information.
356 ges_timeline_layer_set_priority (GESTimelineLayer * layer, guint priority)
358 g_return_if_fail (GES_IS_TIMELINE_LAYER (layer));
360 GST_DEBUG ("layer:%p, priority:%d", layer, priority);
362 if (priority != layer->priv->priority) {
363 layer->priv->priority = priority;
364 layer->min_gnl_priority = (priority * 10);
365 layer->max_gnl_priority = ((priority + 1) * 10) - 1;
367 /* FIXME : Update controlled object's gnl priority accordingly */
368 ges_timeline_layer_resync_priorities (layer);
373 * ges_timeline_layer_get_priority:
374 * @layer: a #GESTimelineLayer
376 * Returns: the priority of the @layer within the timeline.
379 ges_timeline_layer_get_priority (GESTimelineLayer * layer)
381 g_return_val_if_fail (GES_IS_TIMELINE_LAYER (layer), 0);
383 return layer->priv->priority;
387 * ges_timeline_layer_get_objects:
388 * @layer: a #GESTimelineLayer
390 * Get the timeline objects this layer contains.
392 * Returns: (transfer full) (element-type GESTimelineObject): a #GList of
393 * timeline objects. The user is responsible for
394 * unreffing the contained objects and freeing the list.
398 ges_timeline_layer_get_objects (GESTimelineLayer * layer)
402 GESTimelineLayerClass *klass;
404 g_return_val_if_fail (GES_IS_TIMELINE_LAYER (layer), NULL);
406 klass = GES_TIMELINE_LAYER_GET_CLASS (layer);
408 if (klass->get_objects) {
409 return klass->get_objects (layer);
412 for (tmp = layer->priv->objects_start; tmp; tmp = tmp->next) {
413 ret = g_list_prepend (ret, tmp->data);
414 g_object_ref (tmp->data);
417 ret = g_list_reverse (ret);