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
57 static guint ges_timeline_layer_signals[LAST_SIGNAL] = { 0 };
59 static gboolean ges_timeline_layer_resync_priorities (GESTimelineLayer * layer);
62 ges_timeline_layer_get_property (GObject * object, guint property_id,
63 GValue * value, GParamSpec * pspec)
65 GESTimelineLayer *layer = GES_TIMELINE_LAYER (object);
67 switch (property_id) {
69 g_value_set_uint (value, layer->priority);
72 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
77 ges_timeline_layer_set_property (GObject * object, guint property_id,
78 const GValue * value, GParamSpec * pspec)
80 GESTimelineLayer *layer = GES_TIMELINE_LAYER (object);
82 switch (property_id) {
84 ges_timeline_layer_set_priority (layer, g_value_get_uint (value));
87 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
92 ges_timeline_layer_dispose (GObject * object)
94 GESTimelineLayer *layer = GES_TIMELINE_LAYER (object);
96 GST_DEBUG ("Disposing layer");
98 while (layer->objects_start) {
99 GESTimelineObject *obj = (GESTimelineObject *) layer->objects_start->data;
100 ges_timeline_layer_remove_object (layer, obj);
103 G_OBJECT_CLASS (ges_timeline_layer_parent_class)->dispose (object);
107 ges_timeline_layer_finalize (GObject * object)
109 G_OBJECT_CLASS (ges_timeline_layer_parent_class)->finalize (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;
122 object_class->finalize = ges_timeline_layer_finalize;
125 * GESTimelineLayer:priority
127 * The priority of the layer in the #GESTimeline. 0 is the highest
128 * priority. Conceptually, a #GESTimeline is a stack of GESTimelineLayers,
129 * and the priority of the layer represents its position in the stack. Two
130 * layers should not have the same priority within a given GESTimeline.
132 g_object_class_install_property (object_class, PROP_PRIORITY,
133 g_param_spec_uint ("priority", "Priority",
134 "The priority of the layer", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
137 * GESTimelineLayer::object-added
138 * @layer: the #GESTimelineLayer
139 * @object: the #GESTimelineObject that was added.
141 * Will be emitted after the object was added to the layer.
143 ges_timeline_layer_signals[OBJECT_ADDED] =
144 g_signal_new ("object-added", G_TYPE_FROM_CLASS (klass),
145 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineLayerClass, object_added),
146 NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
147 GES_TYPE_TIMELINE_OBJECT);
150 * GESTimelineLayer::object-removed
151 * @layer: the #GESTimelineLayer
152 * @object: the #GESTimelineObject that was removed
154 * Will be emitted after the object was removed from the layer.
156 ges_timeline_layer_signals[OBJECT_REMOVED] =
157 g_signal_new ("object-removed", G_TYPE_FROM_CLASS (klass),
158 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineLayerClass,
159 object_removed), NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
160 GES_TYPE_TIMELINE_OBJECT);
165 ges_timeline_layer_init (GESTimelineLayer * self)
167 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
168 GES_TYPE_TIMELINE_LAYER, GESTimelineLayerPrivate);
170 /* TODO : Keep those 3 values in sync */
172 self->min_gnl_priority = 0;
173 self->max_gnl_priority = 9;
177 * ges_timeline_layer_new:
179 * Creates a new #GESTimelineLayer.
181 * Returns: A new #GESTimelineLayer
184 ges_timeline_layer_new (void)
186 return g_object_new (GES_TYPE_TIMELINE_LAYER, NULL);
190 ges_timeline_layer_set_timeline (GESTimelineLayer * layer,
191 GESTimeline * timeline)
193 GST_DEBUG ("layer:%p, timeline:%p", layer, timeline);
195 layer->timeline = timeline;
199 objects_start_compare (GESTimelineObject * a, GESTimelineObject * b)
201 if (a->start == b->start) {
202 if (a->priority < b->priority)
204 if (a->priority > b->priority)
208 if (a->start < b->start)
210 if (a->start > b->start)
216 * ges_timeline_layer_add_object:
217 * @layer: a #GESTimelineLayer
218 * @object: the #GESTimelineObject to add.
220 * Adds the object to the layer. The layer will steal a reference to the
223 * Returns: TRUE if the object was properly added to the layer, or FALSE
224 * if the @layer refused to add the object.
228 ges_timeline_layer_add_object (GESTimelineLayer * layer,
229 GESTimelineObject * object)
231 GST_DEBUG ("layer:%p, object:%p", layer, object);
233 if (G_UNLIKELY (object->layer)) {
234 GST_WARNING ("TimelineObject %p already belongs to another layer");
238 /* Take a reference to the object and store it stored by start/priority */
239 layer->objects_start =
240 g_slist_insert_sorted (layer->objects_start, object,
241 (GCompareFunc) objects_start_compare);
243 /* Inform the object it's now in this layer */
244 ges_timeline_object_set_layer (object, layer);
246 GST_DEBUG ("current object priority : %d, layer min/max : %d/%d",
247 GES_TIMELINE_OBJECT_PRIORITY (object),
248 layer->min_gnl_priority, layer->max_gnl_priority);
250 /* Set the priority. */
251 if (GES_TIMELINE_OBJECT_PRIORITY (object) > (layer->max_gnl_priority)) {
252 ges_timeline_object_set_priority (object, layer->max_gnl_priority);
255 else if (GES_TIMELINE_OBJECT_PRIORITY (object) < (layer->min_gnl_priority)) {
256 ges_timeline_object_set_priority (object, layer->min_gnl_priority);
259 /* emit 'object-added' */
260 g_signal_emit (layer, ges_timeline_layer_signals[OBJECT_ADDED], 0, object);
266 * ges_timeline_layer_remove_object:
267 * @layer: a #GESTimelineLayer
268 * @object: the #GESTimelineObject to remove
270 * Removes the given @object from the @layer. The reference stolen by the @layer
271 * when the object was added will be removed. If you wish to use the object after
272 * this function, make sure you take an extra reference to the object before
273 * calling this function.
275 * Returns: TRUE if the object was properly remove, else FALSE.
278 ges_timeline_layer_remove_object (GESTimelineLayer * layer,
279 GESTimelineObject * object)
281 GST_DEBUG ("layer:%p, object:%p", layer, object);
283 if (G_UNLIKELY (object->layer != layer)) {
284 GST_WARNING ("TimelineObject doesn't belong to this layer");
288 /* emit 'object-removed' */
289 g_signal_emit (layer, ges_timeline_layer_signals[OBJECT_REMOVED], 0, object);
291 /* inform the object it's no longer in a layer */
292 ges_timeline_object_set_layer (object, NULL);
294 /* Remove it from our list of controlled objects */
295 layer->objects_start = g_slist_remove (layer->objects_start, object);
297 /* Remove our reference to the object */
298 g_object_unref (object);
304 * ges_timeline_layer_resync_priorities:
305 * @layer: a #GESTimelineLayer
307 * Resyncs the priorities of the objects controlled by @layer.
310 ges_timeline_layer_resync_priorities (GESTimelineLayer * layer)
314 /* TODO : Inhibit composition updates while doing this.
315 * Ideally we want to do it from an even higher level, but here will
316 * do in the meantime. */
318 /* TODO : This is the dumb version where we put everything linearly,
319 * will need to be adjusted for more complex usages (like with
321 for (tmp = layer->objects_start; tmp; tmp = tmp->next) {
322 ges_timeline_object_set_priority ((GESTimelineObject *) tmp->data,
323 layer->min_gnl_priority);
330 * ges_timeline_layer_set_priority:
331 * @layer: a #GESTimelineLayer
332 * @priority: the priority to set
334 * Sets the layer to the given @priority. See the documentation of
335 * the "priority" property for more information.
338 ges_timeline_layer_set_priority (GESTimelineLayer * layer, guint priority)
340 GST_DEBUG ("layer:%p, priority:%d", layer, priority);
342 if (priority != layer->priority) {
343 layer->priority = priority;
344 layer->min_gnl_priority = (priority * 10);
345 layer->max_gnl_priority = ((priority + 1) * 10) - 1;
347 /* FIXME : Update controlled object's gnl priority accordingly */
348 ges_timeline_layer_resync_priorities (layer);
353 * ges_timeline_layer_get_objects:
354 * @layer: a #GESTimelineLayer
356 * Get the timeline objects this layer contains.
358 * Returns: a #GList of timeline objects. The user is responsible for
359 * unreffing the contained objects and freeing the list.
363 ges_timeline_layer_get_objects (GESTimelineLayer * layer)
367 GESTimelineLayerClass *klass;
369 klass = GES_TIMELINE_LAYER_GET_CLASS (layer);
371 if (klass->get_objects) {
372 return klass->get_objects (layer);
375 for (tmp = layer->objects_start; tmp; tmp = tmp->next) {
376 ret = g_list_prepend (ret, tmp->data);
377 g_object_ref (tmp->data);
380 ret = g_list_reverse (ret);