GESTimelineObject: add private structure
[platform/upstream/gstreamer.git] / ges / ges-timeline-layer.c
1 /* GStreamer Editing Services
2  * Copyright (C) 2009 Edward Hervey <edward.hervey@collabora.co.uk>
3  *               2009 Nokia Corporation
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 /**
22  * SECTION:ges-timeline-layer
23  * @short_description: Non-overlaping sequence of #GESTimelineObject
24  *
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.
29  */
30
31 #include "ges-internal.h"
32 #include "gesmarshal.h"
33 #include "ges-timeline-layer.h"
34 #include "ges.h"
35
36 G_DEFINE_TYPE (GESTimelineLayer, ges_timeline_layer, G_TYPE_OBJECT);
37
38 struct _GESTimelineLayerPrivate
39 {
40   /*  Dummy variable */
41   void *nothing;
42 };
43
44 enum
45 {
46   PROP_0,
47   PROP_PRIORITY,
48 };
49
50 enum
51 {
52   OBJECT_ADDED,
53   OBJECT_REMOVED,
54   LAST_SIGNAL
55 };
56
57 static guint ges_timeline_layer_signals[LAST_SIGNAL] = { 0 };
58
59 static gboolean ges_timeline_layer_resync_priorities (GESTimelineLayer * layer);
60
61 static void
62 ges_timeline_layer_get_property (GObject * object, guint property_id,
63     GValue * value, GParamSpec * pspec)
64 {
65   GESTimelineLayer *layer = GES_TIMELINE_LAYER (object);
66
67   switch (property_id) {
68     case PROP_PRIORITY:
69       g_value_set_uint (value, layer->priority);
70       break;
71     default:
72       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
73   }
74 }
75
76 static void
77 ges_timeline_layer_set_property (GObject * object, guint property_id,
78     const GValue * value, GParamSpec * pspec)
79 {
80   GESTimelineLayer *layer = GES_TIMELINE_LAYER (object);
81
82   switch (property_id) {
83     case PROP_PRIORITY:
84       ges_timeline_layer_set_priority (layer, g_value_get_uint (value));
85       break;
86     default:
87       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
88   }
89 }
90
91 static void
92 ges_timeline_layer_dispose (GObject * object)
93 {
94   GESTimelineLayer *layer = GES_TIMELINE_LAYER (object);
95
96   GST_DEBUG ("Disposing layer");
97
98   while (layer->objects_start) {
99     GESTimelineObject *obj = (GESTimelineObject *) layer->objects_start->data;
100     ges_timeline_layer_remove_object (layer, obj);
101   }
102
103   G_OBJECT_CLASS (ges_timeline_layer_parent_class)->dispose (object);
104 }
105
106 static void
107 ges_timeline_layer_finalize (GObject * object)
108 {
109   G_OBJECT_CLASS (ges_timeline_layer_parent_class)->finalize (object);
110 }
111
112 static void
113 ges_timeline_layer_class_init (GESTimelineLayerClass * klass)
114 {
115   GObjectClass *object_class = G_OBJECT_CLASS (klass);
116
117   g_type_class_add_private (klass, sizeof (GESTimelineLayerPrivate));
118
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;
123
124   /**
125    * GESTimelineLayer:priority
126    *
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.
131    */
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));
135
136   /**
137    * GESTimelineLayer::object-added
138    * @layer: the #GESTimelineLayer
139    * @object: the #GESTimelineObject that was added.
140    *
141    * Will be emitted after the object was added to the layer.
142    */
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);
148
149   /**
150    * GESTimelineLayer::object-removed
151    * @layer: the #GESTimelineLayer
152    * @object: the #GESTimelineObject that was removed
153    *
154    * Will be emitted after the object was removed from the layer.
155    */
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);
161
162 }
163
164 static void
165 ges_timeline_layer_init (GESTimelineLayer * self)
166 {
167   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
168       GES_TYPE_TIMELINE_LAYER, GESTimelineLayerPrivate);
169
170   /* TODO : Keep those 3 values in sync */
171   self->priority = 0;
172   self->min_gnl_priority = 0;
173   self->max_gnl_priority = 9;
174 }
175
176 /**
177  * ges_timeline_layer_new:
178  *
179  * Creates a new #GESTimelineLayer.
180  *
181  * Returns: A new #GESTimelineLayer
182  */
183 GESTimelineLayer *
184 ges_timeline_layer_new (void)
185 {
186   return g_object_new (GES_TYPE_TIMELINE_LAYER, NULL);
187 }
188
189 void
190 ges_timeline_layer_set_timeline (GESTimelineLayer * layer,
191     GESTimeline * timeline)
192 {
193   GST_DEBUG ("layer:%p, timeline:%p", layer, timeline);
194
195   layer->timeline = timeline;
196 }
197
198 static gint
199 objects_start_compare (GESTimelineObject * a, GESTimelineObject * b)
200 {
201   if (a->start == b->start) {
202     if (a->priority < b->priority)
203       return -1;
204     if (a->priority > b->priority)
205       return 1;
206     return 0;
207   }
208   if (a->start < b->start)
209     return -1;
210   if (a->start > b->start)
211     return 1;
212   return 0;
213 }
214
215 /**
216  * ges_timeline_layer_add_object:
217  * @layer: a #GESTimelineLayer
218  * @object: the #GESTimelineObject to add.
219  *
220  * Adds the object to the layer. The layer will steal a reference to the
221  * provided object.
222  *
223  * Returns: TRUE if the object was properly added to the layer, or FALSE
224  * if the @layer refused to add the object.
225  */
226
227 gboolean
228 ges_timeline_layer_add_object (GESTimelineLayer * layer,
229     GESTimelineObject * object)
230 {
231   GESTimelineLayer *tl_obj_layer;
232
233   GST_DEBUG ("layer:%p, object:%p", layer, object);
234
235   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);
239     return FALSE;
240   }
241
242   /* Take a reference to the object and store it stored by start/priority */
243   layer->objects_start =
244       g_slist_insert_sorted (layer->objects_start, object,
245       (GCompareFunc) objects_start_compare);
246
247   /* Inform the object it's now in this layer */
248   ges_timeline_object_set_layer (object, layer);
249
250   GST_DEBUG ("current object priority : %d, layer min/max : %d/%d",
251       GES_TIMELINE_OBJECT_PRIORITY (object),
252       layer->min_gnl_priority, layer->max_gnl_priority);
253
254   /* Set the priority. */
255   if (GES_TIMELINE_OBJECT_PRIORITY (object) > (layer->max_gnl_priority)) {
256     ges_timeline_object_set_priority (object, layer->max_gnl_priority);
257   }
258
259   else if (GES_TIMELINE_OBJECT_PRIORITY (object) < (layer->min_gnl_priority)) {
260     ges_timeline_object_set_priority (object, layer->min_gnl_priority);
261   }
262
263   /* emit 'object-added' */
264   g_signal_emit (layer, ges_timeline_layer_signals[OBJECT_ADDED], 0, object);
265
266   return TRUE;
267 }
268
269 /**
270  * ges_timeline_layer_remove_object:
271  * @layer: a #GESTimelineLayer
272  * @object: the #GESTimelineObject to remove
273  *
274  * Removes the given @object from the @layer. The reference stolen by the @layer
275  * when the object was added will be removed. If you wish to use the object after
276  * this function, make sure you take an extra reference to the object before
277  * calling this function.
278  *
279  * Returns: TRUE if the object was properly remove, else FALSE.
280  */
281 gboolean
282 ges_timeline_layer_remove_object (GESTimelineLayer * layer,
283     GESTimelineObject * object)
284 {
285   GESTimelineLayer *tl_obj_layer;
286   GST_DEBUG ("layer:%p, object:%p", layer, object);
287
288   tl_obj_layer = ges_timeline_object_get_layer (object);
289   if (G_UNLIKELY (tl_obj_layer != layer)) {
290     GST_WARNING ("TimelineObject doesn't belong to this layer");
291     if (tl_obj_layer != NULL)
292       g_object_unref (tl_obj_layer);
293     return FALSE;
294   }
295   g_object_unref (tl_obj_layer);
296
297   /* emit 'object-removed' */
298   g_signal_emit (layer, ges_timeline_layer_signals[OBJECT_REMOVED], 0, object);
299
300   /* inform the object it's no longer in a layer */
301   ges_timeline_object_set_layer (object, NULL);
302
303   /* Remove it from our list of controlled objects */
304   layer->objects_start = g_slist_remove (layer->objects_start, object);
305
306   /* Remove our reference to the object */
307   g_object_unref (object);
308
309   return TRUE;
310 }
311
312 /**
313  * ges_timeline_layer_resync_priorities:
314  * @layer: a #GESTimelineLayer
315  *
316  * Resyncs the priorities of the objects controlled by @layer.
317  * This method */
318 gboolean
319 ges_timeline_layer_resync_priorities (GESTimelineLayer * layer)
320 {
321   GSList *tmp;
322
323   /* TODO : Inhibit composition updates while doing this.
324    * Ideally we want to do it from an even higher level, but here will
325    * do in the meantime. */
326
327   /* TODO : This is the dumb version where we put everything linearly,
328    * will need to be adjusted for more complex usages (like with
329    * transitions).  */
330   for (tmp = layer->objects_start; tmp; tmp = tmp->next) {
331     ges_timeline_object_set_priority ((GESTimelineObject *) tmp->data,
332         layer->min_gnl_priority);
333   }
334
335   return TRUE;
336 }
337
338 /**
339  * ges_timeline_layer_set_priority:
340  * @layer: a #GESTimelineLayer
341  * @priority: the priority to set
342  *
343  * Sets the layer to the given @priority. See the documentation of
344  * the "priority" property for more information.
345  */
346 void
347 ges_timeline_layer_set_priority (GESTimelineLayer * layer, guint priority)
348 {
349   GST_DEBUG ("layer:%p, priority:%d", layer, priority);
350
351   if (priority != layer->priority) {
352     layer->priority = priority;
353     layer->min_gnl_priority = (priority * 10);
354     layer->max_gnl_priority = ((priority + 1) * 10) - 1;
355
356     /* FIXME : Update controlled object's gnl priority accordingly */
357     ges_timeline_layer_resync_priorities (layer);
358   }
359 }
360
361 /**
362  * ges_timeline_layer_get_objects:
363  * @layer: a #GESTimelineLayer
364  *
365  * Get the timeline objects this layer contains.
366  *
367  * Returns: a #GList of timeline objects. The user is responsible for
368  * unreffing the contained objects and freeing the list.
369  */
370
371 GList *
372 ges_timeline_layer_get_objects (GESTimelineLayer * layer)
373 {
374   GList *ret = NULL;
375   GSList *tmp;
376   GESTimelineLayerClass *klass;
377
378   klass = GES_TIMELINE_LAYER_GET_CLASS (layer);
379
380   if (klass->get_objects) {
381     return klass->get_objects (layer);
382   }
383
384   for (tmp = layer->objects_start; tmp; tmp = tmp->next) {
385     ret = g_list_prepend (ret, tmp->data);
386     g_object_ref (tmp->data);
387   }
388
389   ret = g_list_reverse (ret);
390   return ret;
391 }