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);
51 static guint ges_timeline_layer_signals[LAST_SIGNAL] = { 0 };
53 static gboolean ges_timeline_layer_resync_priorities (GESTimelineLayer * layer);
56 ges_timeline_layer_get_property (GObject * object, guint property_id,
57 GValue * value, GParamSpec * pspec)
59 GESTimelineLayer *layer = GES_TIMELINE_LAYER (object);
61 switch (property_id) {
63 g_value_set_uint (value, layer->priority);
66 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
71 ges_timeline_layer_set_property (GObject * object, guint property_id,
72 const GValue * value, GParamSpec * pspec)
74 GESTimelineLayer *layer = GES_TIMELINE_LAYER (object);
76 switch (property_id) {
78 ges_timeline_layer_set_priority (layer, g_value_get_uint (value));
81 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
86 ges_timeline_layer_dispose (GObject * object)
88 GESTimelineLayer *layer = GES_TIMELINE_LAYER (object);
90 GST_DEBUG ("Disposing layer");
92 while (layer->objects_start) {
93 GESTimelineObject *obj = (GESTimelineObject *) layer->objects_start->data;
94 ges_timeline_layer_remove_object (layer, obj);
97 G_OBJECT_CLASS (ges_timeline_layer_parent_class)->dispose (object);
101 ges_timeline_layer_finalize (GObject * object)
103 G_OBJECT_CLASS (ges_timeline_layer_parent_class)->finalize (object);
107 ges_timeline_layer_class_init (GESTimelineLayerClass * klass)
109 GObjectClass *object_class = G_OBJECT_CLASS (klass);
111 object_class->get_property = ges_timeline_layer_get_property;
112 object_class->set_property = ges_timeline_layer_set_property;
113 object_class->dispose = ges_timeline_layer_dispose;
114 object_class->finalize = ges_timeline_layer_finalize;
117 * GESTimelineLayer:priority
119 * The priority of the layer in the #GESTimeline. 0 is the highest
120 * priority. Conceptually, a #GESTimeline is a stack of GESTimelineLayers,
121 * and the priority of the layer represents its position in the stack. Two
122 * layers should not have the same priority within a given GESTimeline.
124 g_object_class_install_property (object_class, PROP_PRIORITY,
125 g_param_spec_uint ("priority", "Priority",
126 "The priority of the layer", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
129 * GESTimelineLayer::object-added
130 * @layer: the #GESTimelineLayer
131 * @object: the #GESTimelineObject that was added.
133 * Will be emitted after the object was added to the layer.
135 ges_timeline_layer_signals[OBJECT_ADDED] =
136 g_signal_new ("object-added", G_TYPE_FROM_CLASS (klass),
137 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineLayerClass, object_added),
138 NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
139 GES_TYPE_TIMELINE_OBJECT);
142 * GESTimelineLayer::object-removed
143 * @layer: the #GESTimelineLayer
144 * @object: the #GESTimelineObject that was removed
146 * Will be emitted after the object was removed from the layer.
148 ges_timeline_layer_signals[OBJECT_REMOVED] =
149 g_signal_new ("object-removed", G_TYPE_FROM_CLASS (klass),
150 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineLayerClass,
151 object_removed), NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
152 GES_TYPE_TIMELINE_OBJECT);
157 ges_timeline_layer_init (GESTimelineLayer * self)
159 /* TODO : Keep those 3 values in sync */
161 self->min_gnl_priority = 0;
162 self->max_gnl_priority = 9;
166 * ges_timeline_layer_new:
168 * Creates a new #GESTimelineLayer.
170 * Returns: A new #GESTimelineLayer
173 ges_timeline_layer_new (void)
175 return g_object_new (GES_TYPE_TIMELINE_LAYER, NULL);
179 ges_timeline_layer_set_timeline (GESTimelineLayer * layer,
180 GESTimeline * timeline)
182 GST_DEBUG ("layer:%p, timeline:%p", layer, timeline);
184 layer->timeline = timeline;
188 objects_start_compare (GESTimelineObject * a, GESTimelineObject * b)
190 if (a->start == b->start) {
191 if (a->priority < b->priority)
193 if (a->priority > b->priority)
197 if (a->start < b->start)
199 if (a->start > b->start)
205 * ges_timeline_layer_add_object:
206 * @layer: a #GESTimelineLayer
207 * @object: the #GESTimelineObject to add.
209 * Adds the object to the layer. The layer will steal a reference to the
212 * Returns: TRUE if the object was properly added to the layer, or FALSE
213 * if the @layer refused to add the object.
217 ges_timeline_layer_add_object (GESTimelineLayer * layer,
218 GESTimelineObject * object)
220 GST_DEBUG ("layer:%p, object:%p", layer, object);
222 if (G_UNLIKELY (object->layer)) {
223 GST_WARNING ("TimelineObject %p already belongs to another layer");
227 /* Take a reference to the object and store it stored by start/priority */
228 layer->objects_start =
229 g_slist_insert_sorted (layer->objects_start, object,
230 (GCompareFunc) objects_start_compare);
232 /* Inform the object it's now in this layer */
233 ges_timeline_object_set_layer (object, layer);
235 /* Set the priority. */
236 if (GES_TIMELINE_OBJECT_PRIORITY (object) > (layer->max_gnl_priority)) {
237 ges_timeline_object_set_priority (object, layer->max_gnl_priority);
240 else if (GES_TIMELINE_OBJECT_PRIORITY (object) < (layer->min_gnl_priority)) {
241 ges_timeline_object_set_priority (object, layer->min_gnl_priority);
244 /* emit 'object-added' */
245 g_signal_emit (layer, ges_timeline_layer_signals[OBJECT_ADDED], 0, object);
251 * ges_timeline_layer_remove_object:
252 * @layer: a #GESTimelineLayer
253 * @object: the #GESTimelineObject to remove
255 * Removes the given @object from the @layer. The reference stolen by the @layer
256 * when the object was added will be removed. If you wish to use the object after
257 * this function, make sure you take an extra reference to the object before
258 * calling this function.
260 * Returns: TRUE if the object was properly remove, else FALSE.
263 ges_timeline_layer_remove_object (GESTimelineLayer * layer,
264 GESTimelineObject * object)
266 GST_DEBUG ("layer:%p, object:%p", layer, object);
268 if (G_UNLIKELY (object->layer != layer)) {
269 GST_WARNING ("TimelineObject doesn't belong to this layer");
273 /* emit 'object-removed' */
274 g_signal_emit (layer, ges_timeline_layer_signals[OBJECT_REMOVED], 0, object);
276 /* inform the object it's no longer in a layer */
277 ges_timeline_object_set_layer (object, NULL);
279 /* Remove it from our list of controlled objects */
280 layer->objects_start = g_slist_remove (layer->objects_start, object);
282 /* Remove our reference to the object */
283 g_object_unref (object);
289 * ges_timeline_layer_resync_priorities:
290 * @layer: a #GESTimelineLayer
292 * Resyncs the priorities of the objects controlled by @layer.
295 ges_timeline_layer_resync_priorities (GESTimelineLayer * layer)
299 /* TODO : Inhibit composition updates while doing this.
300 * Ideally we want to do it from an even higher level, but here will
301 * do in the meantime. */
303 /* TODO : This is the dumb version where we put everything linearly,
304 * will need to be adjusted for more complex usages (like with
306 for (tmp = layer->objects_start; tmp; tmp = tmp->next) {
307 ges_timeline_object_set_priority ((GESTimelineObject *) tmp->data,
308 layer->min_gnl_priority);
315 ges_timeline_layer_set_priority (GESTimelineLayer * layer, guint priority)
317 GST_DEBUG ("layer:%p, priority:%d", layer, priority);
319 if (priority != layer->priority) {
320 layer->priority = priority;
321 layer->min_gnl_priority = (priority * 10);
322 layer->max_gnl_priority = ((priority + 1) * 10) - 1;
324 /* FIXME : Update controlled object's gnl priority accordingly */
325 ges_timeline_layer_resync_priorities (layer);