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_class_init (GESTimelineLayerClass * klass)
109 GObjectClass *object_class = G_OBJECT_CLASS (klass);
111 g_type_class_add_private (klass, sizeof (GESTimelineLayerPrivate));
113 object_class->get_property = ges_timeline_layer_get_property;
114 object_class->set_property = ges_timeline_layer_set_property;
115 object_class->dispose = ges_timeline_layer_dispose;
118 * GESTimelineLayer:priority
120 * The priority of the layer in the #GESTimeline. 0 is the highest
121 * priority. Conceptually, a #GESTimeline is a stack of GESTimelineLayers,
122 * and the priority of the layer represents its position in the stack. Two
123 * layers should not have the same priority within a given GESTimeline.
125 g_object_class_install_property (object_class, PROP_PRIORITY,
126 g_param_spec_uint ("priority", "Priority",
127 "The priority of the layer", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
130 * GESTimelineLayer::object-added
131 * @layer: the #GESTimelineLayer
132 * @object: the #GESTimelineObject that was added.
134 * Will be emitted after the object was added to the layer.
136 ges_timeline_layer_signals[OBJECT_ADDED] =
137 g_signal_new ("object-added", G_TYPE_FROM_CLASS (klass),
138 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineLayerClass, object_added),
139 NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
140 GES_TYPE_TIMELINE_OBJECT);
143 * GESTimelineLayer::object-removed
144 * @layer: the #GESTimelineLayer
145 * @object: the #GESTimelineObject that was removed
147 * Will be emitted after the object was removed from the layer.
149 ges_timeline_layer_signals[OBJECT_REMOVED] =
150 g_signal_new ("object-removed", G_TYPE_FROM_CLASS (klass),
151 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineLayerClass,
152 object_removed), NULL, NULL, ges_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
153 GES_TYPE_TIMELINE_OBJECT);
158 ges_timeline_layer_init (GESTimelineLayer * self)
160 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
161 GES_TYPE_TIMELINE_LAYER, GESTimelineLayerPrivate);
163 /* TODO : Keep those 3 values in sync */
165 self->min_gnl_priority = 0;
166 self->max_gnl_priority = 9;
170 * ges_timeline_layer_new:
172 * Creates a new #GESTimelineLayer.
174 * Returns: A new #GESTimelineLayer
177 ges_timeline_layer_new (void)
179 return g_object_new (GES_TYPE_TIMELINE_LAYER, NULL);
183 ges_timeline_layer_set_timeline (GESTimelineLayer * layer,
184 GESTimeline * timeline)
186 GST_DEBUG ("layer:%p, timeline:%p", layer, timeline);
188 layer->timeline = timeline;
192 objects_start_compare (GESTimelineObject * a, GESTimelineObject * b)
194 if (a->start == b->start) {
195 if (a->priority < b->priority)
197 if (a->priority > b->priority)
201 if (a->start < b->start)
203 if (a->start > b->start)
209 * ges_timeline_layer_add_object:
210 * @layer: a #GESTimelineLayer
211 * @object: the #GESTimelineObject to add.
213 * Adds the object to the layer. The layer will steal a reference to the
216 * Returns: TRUE if the object was properly added to the layer, or FALSE
217 * if the @layer refused to add the object.
221 ges_timeline_layer_add_object (GESTimelineLayer * layer,
222 GESTimelineObject * object)
224 GESTimelineLayer *tl_obj_layer;
226 GST_DEBUG ("layer:%p, object:%p", layer, object);
228 tl_obj_layer = ges_timeline_object_get_layer (object);
229 if (G_UNLIKELY (tl_obj_layer)) {
230 GST_WARNING ("TimelineObject %p already belongs to another layer");
231 g_object_unref (tl_obj_layer);
235 /* Take a reference to the object and store it stored by start/priority */
236 layer->objects_start =
237 g_slist_insert_sorted (layer->objects_start, object,
238 (GCompareFunc) objects_start_compare);
240 /* Inform the object it's now in this layer */
241 ges_timeline_object_set_layer (object, layer);
243 GST_DEBUG ("current object priority : %d, layer min/max : %d/%d",
244 GES_TIMELINE_OBJECT_PRIORITY (object),
245 layer->min_gnl_priority, layer->max_gnl_priority);
247 /* Set the priority. */
248 if (GES_TIMELINE_OBJECT_PRIORITY (object) > (layer->max_gnl_priority)) {
249 ges_timeline_object_set_priority (object, layer->max_gnl_priority);
252 else if (GES_TIMELINE_OBJECT_PRIORITY (object) < (layer->min_gnl_priority)) {
253 ges_timeline_object_set_priority (object, layer->min_gnl_priority);
256 /* emit 'object-added' */
257 g_signal_emit (layer, ges_timeline_layer_signals[OBJECT_ADDED], 0, object);
263 * ges_timeline_layer_remove_object:
264 * @layer: a #GESTimelineLayer
265 * @object: the #GESTimelineObject to remove
267 * Removes the given @object from the @layer. The reference stolen by the @layer
268 * when the object was added will be removed. If you wish to use the object after
269 * this function, make sure you take an extra reference to the object before
270 * calling this function.
272 * Returns: TRUE if the object was properly remove, else FALSE.
275 ges_timeline_layer_remove_object (GESTimelineLayer * layer,
276 GESTimelineObject * object)
278 GESTimelineLayer *tl_obj_layer;
279 GST_DEBUG ("layer:%p, object:%p", layer, object);
281 tl_obj_layer = ges_timeline_object_get_layer (object);
282 if (G_UNLIKELY (tl_obj_layer != layer)) {
283 GST_WARNING ("TimelineObject doesn't belong to this layer");
284 if (tl_obj_layer != NULL)
285 g_object_unref (tl_obj_layer);
288 g_object_unref (tl_obj_layer);
290 /* emit 'object-removed' */
291 g_signal_emit (layer, ges_timeline_layer_signals[OBJECT_REMOVED], 0, object);
293 /* inform the object it's no longer in a layer */
294 ges_timeline_object_set_layer (object, NULL);
296 /* Remove it from our list of controlled objects */
297 layer->objects_start = g_slist_remove (layer->objects_start, object);
299 /* Remove our reference to the object */
300 g_object_unref (object);
306 * ges_timeline_layer_resync_priorities:
307 * @layer: a #GESTimelineLayer
309 * Resyncs the priorities of the objects controlled by @layer.
312 ges_timeline_layer_resync_priorities (GESTimelineLayer * layer)
316 /* TODO : Inhibit composition updates while doing this.
317 * Ideally we want to do it from an even higher level, but here will
318 * do in the meantime. */
320 /* TODO : This is the dumb version where we put everything linearly,
321 * will need to be adjusted for more complex usages (like with
323 for (tmp = layer->objects_start; tmp; tmp = tmp->next) {
324 ges_timeline_object_set_priority ((GESTimelineObject *) tmp->data,
325 layer->min_gnl_priority);
332 * ges_timeline_layer_set_priority:
333 * @layer: a #GESTimelineLayer
334 * @priority: the priority to set
336 * Sets the layer to the given @priority. See the documentation of
337 * the "priority" property for more information.
340 ges_timeline_layer_set_priority (GESTimelineLayer * layer, guint priority)
342 GST_DEBUG ("layer:%p, priority:%d", layer, priority);
344 if (priority != layer->priority) {
345 layer->priority = priority;
346 layer->min_gnl_priority = (priority * 10);
347 layer->max_gnl_priority = ((priority + 1) * 10) - 1;
349 /* FIXME : Update controlled object's gnl priority accordingly */
350 ges_timeline_layer_resync_priorities (layer);
355 * ges_timeline_layer_get_objects:
356 * @layer: a #GESTimelineLayer
358 * Get the timeline objects this layer contains.
360 * Returns: (transfer full) (element-type GESTimelineObject): a #GList of
361 * timeline objects. The user is responsible for
362 * unreffing the contained objects and freeing the list.
366 ges_timeline_layer_get_objects (GESTimelineLayer * layer)
370 GESTimelineLayerClass *klass;
372 klass = GES_TIMELINE_LAYER_GET_CLASS (layer);
374 if (klass->get_objects) {
375 return klass->get_objects (layer);
378 for (tmp = layer->objects_start; tmp; tmp = tmp->next) {
379 ret = g_list_prepend (ret, tmp->data);
380 g_object_ref (tmp->data);
383 ret = g_list_reverse (ret);