ges: Add API to get the TrackObject-s contained in a Track
[platform/upstream/gstreamer.git] / ges / ges-track.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-track
23  * @short_description: Composition of objects
24  *
25  * Corresponds to one output format (i.e. audio OR video).
26  *
27  * Contains the compatible TrackObject(s).
28  *
29  * Wraps GNonLin's 'gnlcomposition' element.
30  */
31
32 #include "ges-internal.h"
33 #include "ges-track.h"
34 #include "ges-track-object.h"
35 #include "gesmarshal.h"
36
37 G_DEFINE_TYPE (GESTrack, ges_track, GST_TYPE_BIN);
38
39 struct _GESTrackPrivate
40 {
41   /*< private > */
42   GESTimeline *timeline;
43   GList *trackobjects;
44   guint64 duration;
45
46   GstCaps *caps;
47
48   GstElement *composition;      /* The composition associated with this track */
49   GstPad *srcpad;               /* The source GhostPad */
50 };
51
52 enum
53 {
54   ARG_0,
55   ARG_CAPS,
56   ARG_TYPE,
57   ARG_DURATION,
58   ARG_LAST,
59   TRACK_OBJECT_ADDED,
60   LAST_SIGNAL
61 };
62
63 static guint ges_track_signals[LAST_SIGNAL] = { 0 };
64
65 static GParamSpec *properties[ARG_LAST];
66
67 static void pad_added_cb (GstElement * element, GstPad * pad, GESTrack * track);
68 static void
69 pad_removed_cb (GstElement * element, GstPad * pad, GESTrack * track);
70 static void composition_duration_cb (GstElement * composition, GParamSpec * arg
71     G_GNUC_UNUSED, GESTrack * obj);
72
73 static void
74 ges_track_get_property (GObject * object, guint property_id,
75     GValue * value, GParamSpec * pspec)
76 {
77   GESTrack *track = GES_TRACK (object);
78
79   switch (property_id) {
80     case ARG_CAPS:
81       gst_value_set_caps (value, track->priv->caps);
82       break;
83     case ARG_TYPE:
84       g_value_set_flags (value, track->type);
85       break;
86     case ARG_DURATION:
87       g_value_set_uint64 (value, track->priv->duration);
88       break;
89     default:
90       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
91   }
92 }
93
94 static void
95 ges_track_set_property (GObject * object, guint property_id,
96     const GValue * value, GParamSpec * pspec)
97 {
98   GESTrack *track = GES_TRACK (object);
99
100   switch (property_id) {
101     case ARG_CAPS:
102       ges_track_set_caps (track, gst_value_get_caps (value));
103       break;
104     case ARG_TYPE:
105       track->type = g_value_get_flags (value);
106       break;
107     default:
108       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
109   }
110 }
111
112 static void
113 ges_track_dispose (GObject * object)
114 {
115   GESTrack *track = (GESTrack *) object;
116   GESTrackPrivate *priv = track->priv;
117
118   while (priv->trackobjects) {
119     GESTrackObject *trobj = GES_TRACK_OBJECT (priv->trackobjects->data);
120     ges_track_remove_object (track, trobj);
121     ges_timeline_object_release_track_object ((GESTimelineObject *)
122         ges_track_object_get_timeline_object (trobj), trobj);
123   }
124
125   if (priv->composition) {
126     gst_bin_remove (GST_BIN (object), priv->composition);
127     priv->composition = NULL;
128   }
129
130   if (priv->caps) {
131     gst_caps_unref (priv->caps);
132     priv->caps = NULL;
133   }
134
135   G_OBJECT_CLASS (ges_track_parent_class)->dispose (object);
136 }
137
138 static void
139 ges_track_finalize (GObject * object)
140 {
141   G_OBJECT_CLASS (ges_track_parent_class)->finalize (object);
142 }
143
144 static void
145 ges_track_class_init (GESTrackClass * klass)
146 {
147   GObjectClass *object_class = G_OBJECT_CLASS (klass);
148
149   g_type_class_add_private (klass, sizeof (GESTrackPrivate));
150
151   object_class->get_property = ges_track_get_property;
152   object_class->set_property = ges_track_set_property;
153   object_class->dispose = ges_track_dispose;
154   object_class->finalize = ges_track_finalize;
155
156   /**
157    * GESTrack:caps
158    *
159    * Caps used to filter/choose the output stream. This is generally set to
160    * a generic set of caps like 'video/x-raw-rgb;video/x-raw-yuv' for raw video.
161    *
162    * Default value: #GST_CAPS_ANY.
163    */
164   properties[ARG_CAPS] = g_param_spec_boxed ("caps", "Caps",
165       "Caps used to filter/choose the output stream",
166       GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
167   g_object_class_install_property (object_class, ARG_CAPS,
168       properties[ARG_CAPS]);
169
170   /**
171    * GESTrack:duration
172    *
173    * Current duration of the track
174    *
175    * Default value: O
176    */
177   properties[ARG_DURATION] = g_param_spec_uint64 ("duration", "Duration",
178       "The current duration of the track", 0, G_MAXUINT64, GST_SECOND,
179       G_PARAM_READABLE);
180   g_object_class_install_property (object_class, ARG_DURATION,
181       properties[ARG_DURATION]);
182
183   /**
184    * GESTrack:track-type
185    *
186    * Type of stream the track outputs. This is used when creating the #GESTrack
187    * to specify in generic terms what type of content will be outputted.
188    *
189    * It also serves as a 'fast' way to check what type of data will be outputted
190    * from the #GESTrack without having to actually check the #GESTrack's caps
191    * property.
192    */
193   properties[ARG_TYPE] = g_param_spec_flags ("track-type", "TrackType",
194       "Type of stream the track outputs",
195       GES_TYPE_TRACK_TYPE, GES_TRACK_TYPE_CUSTOM,
196       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
197   g_object_class_install_property (object_class, ARG_TYPE,
198       properties[ARG_TYPE]);
199
200   /**
201    * GESTrack::track-object-added
202    * @object: the #GESTrack
203    * @effect: the #GESTrackObject that was added.
204    *
205    * Will be emitted after a track object was added to the track.
206    *
207    * Since: 0.10.2
208    */
209   ges_track_signals[TRACK_OBJECT_ADDED] =
210       g_signal_new ("track-object-added", G_TYPE_FROM_CLASS (klass),
211       G_SIGNAL_RUN_FIRST, 0, NULL, NULL, ges_marshal_VOID__OBJECT,
212       G_TYPE_NONE, 1, GES_TYPE_TRACK_OBJECT);
213
214 }
215
216 static void
217 ges_track_init (GESTrack * self)
218 {
219   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
220       GES_TYPE_TRACK, GESTrackPrivate);
221
222   self->priv->composition = gst_element_factory_make ("gnlcomposition", NULL);
223
224   g_signal_connect (G_OBJECT (self->priv->composition), "notify::duration",
225       G_CALLBACK (composition_duration_cb), self);
226   g_signal_connect (self->priv->composition, "pad-added",
227       (GCallback) pad_added_cb, self);
228   g_signal_connect (self->priv->composition, "pad-removed",
229       (GCallback) pad_removed_cb, self);
230
231   if (!gst_bin_add (GST_BIN (self), self->priv->composition))
232     GST_ERROR ("Couldn't add composition to bin !");
233 }
234
235 /**
236  * ges_track_new:
237  * @type: The type of track
238  * @caps: The caps to restrict the output of the track to.
239  *
240  * Creates a new #GESTrack with the given @type and @caps.
241  *
242  * The newly created track will steal a reference to the caps. If you wish to 
243  * use those caps elsewhere, you will have to take an extra reference.
244  *
245  * Returns: A new #GESTrack.
246  */
247 GESTrack *
248 ges_track_new (GESTrackType type, GstCaps * caps)
249 {
250   GESTrack *track;
251
252   track = g_object_new (GES_TYPE_TRACK, "caps", caps, "track-type", type, NULL);
253   gst_caps_unref (caps);
254
255   return track;
256 }
257
258 /**
259  * ges_track_video_raw_new:
260  *
261  * Creates a new #GESTrack of type #GES_TRACK_TYPE_VIDEO and with generic
262  * raw video caps ("video/x-raw-yuv;video/x-raw-rgb");
263  *
264  * Returns: A new #GESTrack.
265  */
266 GESTrack *
267 ges_track_video_raw_new (void)
268 {
269   GESTrack *track;
270   GstCaps *caps = gst_caps_from_string ("video/x-raw-yuv;video/x-raw-rgb");
271
272   track = ges_track_new (GES_TRACK_TYPE_VIDEO, caps);
273
274   return track;
275 }
276
277 /**
278  * ges_track_audio_raw_new:
279  *
280  * Creates a new #GESTrack of type #GES_TRACK_TYPE_AUDIO and with generic
281  * raw audio caps ("audio/x-raw-int;audio/x-raw-float");
282  *
283  * Returns: A new #GESTrack.
284  */
285 GESTrack *
286 ges_track_audio_raw_new (void)
287 {
288   GESTrack *track;
289   GstCaps *caps = gst_caps_from_string ("audio/x-raw-int;audio/x-raw-float");
290
291   track = ges_track_new (GES_TRACK_TYPE_AUDIO, caps);
292
293   return track;
294 }
295
296 /**
297  * ges_track_set_timeline:
298  * @track: a #GESTrack
299  * @timeline: a #GESTimeline
300  *
301  * Sets @timeline as the timeline controlling @track.
302  */
303 void
304 ges_track_set_timeline (GESTrack * track, GESTimeline * timeline)
305 {
306   GST_DEBUG ("track:%p, timeline:%p", track, timeline);
307
308   track->priv->timeline = timeline;
309 }
310
311 /**
312  * ges_track_set_caps:
313  * @track: a #GESTrack
314  * @caps: the #GstCaps to set
315  *
316  * Sets the given @caps on the track.
317  */
318 void
319 ges_track_set_caps (GESTrack * track, const GstCaps * caps)
320 {
321   GESTrackPrivate *priv;
322
323   g_return_if_fail (GES_IS_TRACK (track));
324   g_return_if_fail (GST_IS_CAPS (caps));
325
326   GST_DEBUG ("track:%p, caps:%" GST_PTR_FORMAT, track, caps);
327
328   priv = track->priv;
329
330   if (priv->caps)
331     gst_caps_unref (priv->caps);
332   priv->caps = gst_caps_copy (caps);
333
334   g_object_set (priv->composition, "caps", caps, NULL);
335   /* FIXME : update all trackobjects ? */
336 }
337
338
339 /* FIXME : put the compare function in the utils */
340
341 static gint
342 objects_start_compare (GESTrackObject * a, GESTrackObject * b)
343 {
344   if (a->start == b->start) {
345     if (a->priority < b->priority)
346       return -1;
347     if (a->priority > b->priority)
348       return 1;
349     return 0;
350   }
351   if (a->start < b->start)
352     return -1;
353   if (a->start > b->start)
354     return 1;
355   return 0;
356 }
357
358 /**
359  * ges_track_add_object:
360  * @track: a #GESTrack
361  * @object: (transfer full): the #GESTrackObject to add
362  *
363  * Adds the given object to the track. Sets the object's controlling track,
364  * and thus takes ownership of the @object.
365  *
366  * An object can only be added to one track.
367  *
368  * Returns: #TRUE if the object was properly added. #FALSE if the track does not
369  * want to accept the object.
370  */
371 gboolean
372 ges_track_add_object (GESTrack * track, GESTrackObject * object)
373 {
374   g_return_val_if_fail (GES_IS_TRACK (track), FALSE);
375   g_return_val_if_fail (GES_IS_TRACK_OBJECT (object), FALSE);
376
377   GST_DEBUG ("track:%p, object:%p", track, object);
378
379   if (G_UNLIKELY (ges_track_object_get_track (object) != NULL)) {
380     GST_WARNING ("Object already belongs to another track");
381     return FALSE;
382   }
383
384   /* At this point, the track object shouldn't have any gnlobject since
385    * it hasn't been added to a track yet.
386    * FIXME : This check seems a bit obsolete */
387   if (G_UNLIKELY (ges_track_object_get_gnlobject (object) != NULL)) {
388     GST_ERROR ("TrackObject already controls a gnlobject !");
389     return FALSE;
390   }
391
392   if (G_UNLIKELY (!ges_track_object_set_track (object, track))) {
393     GST_ERROR ("Couldn't properly add the object to the Track");
394     return FALSE;
395   }
396
397   GST_DEBUG ("Adding object to ourself");
398
399   if (G_UNLIKELY (!gst_bin_add (GST_BIN (track->priv->composition),
400               ges_track_object_get_gnlobject (object)))) {
401     GST_WARNING ("Couldn't add object to the GnlComposition");
402     return FALSE;
403   }
404
405   g_object_ref_sink (object);
406   track->priv->trackobjects =
407       g_list_insert_sorted (track->priv->trackobjects, object,
408       (GCompareFunc) objects_start_compare);
409   g_signal_emit (track, ges_track_signals[TRACK_OBJECT_ADDED], 0,
410       GES_TRACK_OBJECT (object));
411
412   return TRUE;
413 }
414
415 GList *
416 ges_track_get_objects (GESTrack * track)
417 {
418   GList *ret = NULL;
419   GList *tmp;
420
421   g_return_val_if_fail (GES_IS_TRACK (track), NULL);
422
423   for (tmp = track->priv->trackobjects; tmp; tmp = tmp->next) {
424     ret = g_list_prepend (ret, tmp->data);
425     g_object_ref (tmp->data);
426   }
427
428   ret = g_list_reverse (ret);
429   return ret;
430 }
431
432 /**
433  * ges_track_remove_object:
434  * @track: a #GESTrack
435  * @object: the #GESTrackObject to remove
436  *
437  * Removes the object from the track and unparents it.
438  * Unparenting it means the reference owned by @track on the @object will be
439  * removed. If you wish to use the @object after this function, make sure you
440  * call g_object_ref() before removing it from the @track.
441  *
442  * Returns: #TRUE if the object was removed, else #FALSE if the track
443  * could not remove the object (like if it didn't belong to the track).
444  */
445 gboolean
446 ges_track_remove_object (GESTrack * track, GESTrackObject * object)
447 {
448   GESTrackPrivate *priv;
449   GstElement *gnlobject;
450
451   g_return_val_if_fail (GES_IS_TRACK (track), FALSE);
452   g_return_val_if_fail (GES_IS_TRACK_OBJECT (object), FALSE);
453
454   GST_DEBUG ("track:%p, object:%p", track, object);
455
456   priv = track->priv;
457
458   if (G_UNLIKELY (ges_track_object_get_track (object) != track)) {
459     GST_WARNING ("Object belongs to another track");
460     return FALSE;
461   }
462
463   if ((gnlobject = ges_track_object_get_gnlobject (object))) {
464     GST_DEBUG ("Removing GnlObject '%s' from composition '%s'",
465         GST_ELEMENT_NAME (gnlobject), GST_ELEMENT_NAME (priv->composition));
466     if (!gst_bin_remove (GST_BIN (priv->composition), gnlobject)) {
467       GST_WARNING ("Failed to remove gnlobject from composition");
468       return FALSE;
469     }
470   }
471
472   ges_track_object_set_track (object, NULL);
473   priv->trackobjects = g_list_remove (priv->trackobjects, object);
474
475   g_object_unref (object);
476
477   return TRUE;
478 }
479
480 static void
481 pad_added_cb (GstElement * element, GstPad * pad, GESTrack * track)
482 {
483   GESTrackPrivate *priv = track->priv;
484
485   GST_DEBUG ("track:%p, pad %s:%s", track, GST_DEBUG_PAD_NAME (pad));
486
487   /* ghost the pad */
488   priv->srcpad = gst_ghost_pad_new ("src", pad);
489
490   gst_pad_set_active (priv->srcpad, TRUE);
491
492   gst_element_add_pad (GST_ELEMENT (track), priv->srcpad);
493
494   GST_DEBUG ("done");
495 }
496
497 static void
498 pad_removed_cb (GstElement * element, GstPad * pad, GESTrack * track)
499 {
500   GESTrackPrivate *priv = track->priv;
501
502   GST_DEBUG ("track:%p, pad %s:%s", track, GST_DEBUG_PAD_NAME (pad));
503
504   if (G_LIKELY (priv->srcpad)) {
505     gst_pad_set_active (priv->srcpad, FALSE);
506     gst_element_remove_pad (GST_ELEMENT (track), priv->srcpad);
507     priv->srcpad = NULL;
508   }
509
510   GST_DEBUG ("done");
511 }
512
513 static void
514 composition_duration_cb (GstElement * composition,
515     GParamSpec * arg G_GNUC_UNUSED, GESTrack * obj)
516 {
517   guint64 duration;
518
519   g_object_get (composition, "duration", &duration, NULL);
520
521
522   if (obj->priv->duration != duration) {
523     GST_DEBUG ("composition duration : %" GST_TIME_FORMAT " current : %"
524         GST_TIME_FORMAT, GST_TIME_ARGS (duration),
525         GST_TIME_ARGS (obj->priv->duration));
526
527     obj->priv->duration = duration;
528
529 #if GLIB_CHECK_VERSION(2,26,0)
530     g_object_notify_by_pspec (G_OBJECT (obj), properties[ARG_DURATION]);
531 #else
532     g_object_notify (G_OBJECT (obj), "duration");
533 #endif
534   }
535 }
536
537 /**
538  * ges_track_get_caps:
539  * @track: a #GESTrack
540  *
541  * Get the #GstCaps this track is configured to output.
542  *
543  * Returns: The #GstCaps this track is configured to output.
544  */
545 const GstCaps *
546 ges_track_get_caps (GESTrack * track)
547 {
548   g_return_val_if_fail (GES_IS_TRACK (track), NULL);
549
550   return track->priv->caps;
551 }
552
553 /**
554  * ges_track_get_timeline:
555  * @track: a #GESTrack
556  *
557  * Get the #GESTimeline this track belongs to. Can be %NULL.
558  *
559  * Returns: The #GESTimeline this track belongs to. Can be %NULL.
560  */
561 const GESTimeline *
562 ges_track_get_timeline (GESTrack * track)
563 {
564   g_return_val_if_fail (GES_IS_TRACK (track), NULL);
565
566   return track->priv->timeline;
567 }