completion: Update to new gstreamer core helpers
[platform/upstream/gstreamer.git] / ges / ges-transition-clip.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., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * SECTION: gestransitionclip
23  * @title: GESTransitionClip
24  * @short_description: Transition from one clip to another in a GESLayer
25  *
26  * Creates an object that mixes together the two underlying objects, A and B.
27  * The A object is assumed to have a higher prioirity (lower number) than the
28  * B object. At the transition in point, only A will be visible, and by the
29  * end only B will be visible.
30  *
31  * The shape of the video transition depends on the value of the "vtype"
32  * property. The default value is "crossfade". For audio, only "crossfade" is
33  * supported.
34  *
35  * The ID of the ExtractableType is the nickname of the vtype property value. Note
36  * that this value can be changed after creation and the GESExtractable.asset value
37  * will be updated when needed.
38  */
39
40 #include <ges/ges.h>
41 #include "ges-internal.h"
42
43 struct _GESTransitionClipPrivate
44 {
45   GSList *video_transitions;
46
47   const gchar *vtype_name;
48 };
49
50 enum
51 {
52   PROP_VTYPE = 5,
53 };
54
55 static GESTrackElement *_create_track_element (GESClip
56     * self, GESTrackType type);
57 static void _child_added (GESContainer * container,
58     GESTimelineElement * element);
59 static void _child_removed (GESContainer * container,
60     GESTimelineElement * element);
61
62 /* Internal methods */
63 static void
64 ges_transition_clip_update_vtype_internal (GESClip *
65     self, GESVideoStandardTransitionType value, gboolean set_asset)
66 {
67   GSList *tmp;
68   guint index;
69   GEnumClass *enum_class;
70   const gchar *asset_id = NULL;
71   GESTransitionClip *trself = GES_TRANSITION_CLIP (self);
72
73   enum_class = g_type_class_peek (GES_VIDEO_STANDARD_TRANSITION_TYPE_TYPE);
74   for (index = 0; index < enum_class->n_values; index++) {
75     if (enum_class->values[index].value == value) {
76       asset_id = enum_class->values[index].value_nick;
77       break;
78     }
79   }
80
81   if (asset_id == NULL) {
82     GST_WARNING_OBJECT (self, "Wrong transition type value: %i can not set it",
83         value);
84
85     return;
86   }
87
88   for (tmp = trself->priv->video_transitions; tmp; tmp = tmp->next) {
89     if (!ges_video_transition_set_transition_type
90         (GES_VIDEO_TRANSITION (tmp->data), value))
91       return;
92   }
93
94   trself->vtype = value;
95   trself->priv->vtype_name = asset_id;
96
97   if (set_asset) {
98     /* We already checked the value, so we can be sure no error will accured */
99     ges_extractable_set_asset (GES_EXTRACTABLE (self),
100         ges_asset_request (GES_TYPE_TRANSITION_CLIP, asset_id, NULL));
101   }
102 }
103
104 /* GESExtractable interface overrides */
105 static GParameter *
106 extractable_get_parameters_from_id (const gchar * id, guint * n_params)
107 {
108   GEnumClass *enum_class =
109       g_type_class_peek (GES_VIDEO_STANDARD_TRANSITION_TYPE_TYPE);
110   GParameter *params = g_new0 (GParameter, 1);
111   GEnumValue *value = g_enum_get_value_by_nick (enum_class, id);
112
113   params[0].name = "vtype";
114   g_value_init (&params[0].value, GES_VIDEO_STANDARD_TRANSITION_TYPE_TYPE);
115   g_value_set_enum (&params[0].value, value->value);
116   *n_params = 1;
117
118   return params;
119 }
120
121 static gchar *
122 extractable_check_id (GType type, const gchar * id)
123 {
124   guint index;
125   GEnumClass *enum_class;
126   enum_class = g_type_class_peek (GES_VIDEO_STANDARD_TRANSITION_TYPE_TYPE);
127
128   for (index = 0; index < enum_class->n_values; index++) {
129     if (g_strcmp0 (enum_class->values[index].value_nick, id) == 0)
130       return g_strdup (id);
131   }
132
133   return NULL;
134 }
135
136 static gchar *
137 extractable_get_id (GESExtractable * self)
138 {
139   guint index;
140   GEnumClass *enum_class;
141   guint value = GES_TRANSITION_CLIP (self)->vtype;
142
143   enum_class = g_type_class_peek (GES_VIDEO_STANDARD_TRANSITION_TYPE_TYPE);
144   for (index = 0; index < enum_class->n_values; index++) {
145     if (enum_class->values[index].value == value)
146       return g_strdup (enum_class->values[index].value_nick);
147   }
148
149   return NULL;
150 }
151
152 static gboolean
153 extractable_set_asset (GESExtractable * self, GESAsset * asset)
154 {
155   GEnumClass *enum_class;
156   GESVideoStandardTransitionType value;
157   GESTransitionClip *trans = GES_TRANSITION_CLIP (self);
158   const gchar *vtype = ges_asset_get_id (asset);
159
160   if (!(ges_clip_get_supported_formats (GES_CLIP (self)) &
161           GES_TRACK_TYPE_VIDEO)) {
162     return FALSE;
163   }
164
165   /* Update the transition type if we actually changed it */
166   if (g_strcmp0 (vtype, trans->priv->vtype_name)) {
167     guint index;
168
169     value = GES_VIDEO_STANDARD_TRANSITION_TYPE_CROSSFADE;
170     enum_class = g_type_class_peek (GES_VIDEO_STANDARD_TRANSITION_TYPE_TYPE);
171
172     /* Find the in value in use */
173     for (index = 0; index < enum_class->n_values; index++) {
174       if (g_strcmp0 (enum_class->values[index].value_nick, vtype) == 0) {
175         value = enum_class->values[index].value;
176         break;
177       }
178     }
179     ges_transition_clip_update_vtype_internal (GES_CLIP (self), value, FALSE);
180   }
181
182   return TRUE;
183 }
184
185 static void
186 ges_extractable_interface_init (GESExtractableInterface * iface)
187 {
188   iface->check_id = (GESExtractableCheckId) extractable_check_id;
189   iface->get_id = extractable_get_id;
190   iface->get_parameters_from_id = extractable_get_parameters_from_id;
191   iface->can_update_asset = TRUE;
192   iface->set_asset_full = extractable_set_asset;
193 }
194
195 G_DEFINE_TYPE_WITH_CODE (GESTransitionClip,
196     ges_transition_clip, GES_TYPE_BASE_TRANSITION_CLIP,
197     G_IMPLEMENT_INTERFACE (GES_TYPE_EXTRACTABLE,
198         ges_extractable_interface_init));
199
200 static void
201 ges_transition_clip_get_property (GObject * object,
202     guint property_id, GValue * value, GParamSpec * pspec)
203 {
204   GESTransitionClip *self = GES_TRANSITION_CLIP (object);
205   switch (property_id) {
206     case PROP_VTYPE:
207       g_value_set_enum (value, self->vtype);
208       break;
209     default:
210       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
211   }
212 }
213
214 static void
215 ges_transition_clip_set_property (GObject * object,
216     guint property_id, const GValue * value, GParamSpec * pspec)
217 {
218   GESClip *self = GES_CLIP (object);
219
220   switch (property_id) {
221     case PROP_VTYPE:
222       ges_transition_clip_update_vtype_internal (self,
223           g_value_get_enum (value), TRUE);
224       break;
225     default:
226       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
227   }
228 }
229
230 static void
231 ges_transition_clip_class_init (GESTransitionClipClass * klass)
232 {
233   GObjectClass *object_class = G_OBJECT_CLASS (klass);
234   GESClipClass *timobj_class = GES_CLIP_CLASS (klass);
235   GESContainerClass *container_class = GES_CONTAINER_CLASS (klass);
236
237   g_type_class_add_private (klass, sizeof (GESTransitionClipPrivate));
238
239   object_class->get_property = ges_transition_clip_get_property;
240   object_class->set_property = ges_transition_clip_set_property;
241
242   /**
243    * GESTransitionClip:vtype:
244    *
245    * a #GESVideoStandardTransitionType representing the wipe to use
246    */
247   g_object_class_install_property (object_class, PROP_VTYPE,
248       g_param_spec_enum ("vtype", "VType",
249           "The SMPTE video wipe to use, or 0 for crossfade",
250           GES_VIDEO_STANDARD_TRANSITION_TYPE_TYPE,
251           GES_VIDEO_STANDARD_TRANSITION_TYPE_CROSSFADE,
252           G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
253
254   container_class->child_added = _child_added;
255   container_class->child_removed = _child_removed;
256
257   timobj_class->create_track_element = _create_track_element;
258 }
259
260 static void
261 ges_transition_clip_init (GESTransitionClip * self)
262 {
263
264   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
265       GES_TYPE_TRANSITION_CLIP, GESTransitionClipPrivate);
266
267   self->vtype = GES_VIDEO_STANDARD_TRANSITION_TYPE_NONE;
268   self->priv->vtype_name = NULL;
269 }
270
271 static void
272 _child_removed (GESContainer * container, GESTimelineElement * element)
273 {
274   GESTransitionClipPrivate *priv = GES_TRANSITION_CLIP (container)->priv;
275
276   /* If this is called, we should be sure the trackelement exists */
277   if (GES_IS_VIDEO_TRANSITION (element)) {
278     GST_DEBUG_OBJECT (container, "%" GST_PTR_FORMAT " removed", element);
279     priv->video_transitions = g_slist_remove (priv->video_transitions, element);
280     gst_object_unref (element);
281   }
282 }
283
284 static void
285 _child_added (GESContainer * container, GESTimelineElement * element)
286 {
287   GESTransitionClipPrivate *priv = GES_TRANSITION_CLIP (container)->priv;
288
289   if (GES_IS_VIDEO_TRANSITION (element)) {
290     GST_DEBUG_OBJECT (container, "%" GST_PTR_FORMAT " added", element);
291     priv->video_transitions =
292         g_slist_prepend (priv->video_transitions, gst_object_ref (element));
293   }
294 }
295
296 static GESTrackElement *
297 _create_track_element (GESClip * clip, GESTrackType type)
298 {
299   GESTransitionClip *transition = (GESTransitionClip *) clip;
300   GESTrackElement *res = NULL;
301   GESTrackType supportedformats;
302
303   GST_DEBUG ("Creating a GESTransition");
304
305   supportedformats = ges_clip_get_supported_formats (clip);
306   if (type == GES_TRACK_TYPE_VIDEO) {
307     if (supportedformats == GES_TRACK_TYPE_UNKNOWN ||
308         supportedformats & GES_TRACK_TYPE_VIDEO) {
309       GESVideoTransition *trans;
310
311       trans = ges_video_transition_new ();
312       ges_video_transition_set_transition_type (trans, transition->vtype);
313
314       res = GES_TRACK_ELEMENT (trans);
315     } else {
316       GST_DEBUG ("Not creating transition as video track not on"
317           " supportedformats");
318     }
319
320   } else if (type == GES_TRACK_TYPE_AUDIO) {
321
322     if (supportedformats == GES_TRACK_TYPE_UNKNOWN ||
323         supportedformats & GES_TRACK_TYPE_AUDIO)
324       res = GES_TRACK_ELEMENT (ges_audio_transition_new ());
325     else
326       GST_DEBUG ("Not creating transition as audio track"
327           " not on supportedformats");
328
329   } else
330     GST_WARNING ("Transitions don't handle this track type");
331
332   return res;
333 }
334
335 /**
336  * ges_transition_clip_new:
337  * @vtype: the type of transition to create
338  *
339  * Creates a new #GESTransitionClip.
340  *
341  * Returns: (transfer floating) (nullable): a newly created #GESTransitionClip,
342  * or %NULL if something went wrong.
343  */
344 GESTransitionClip *
345 ges_transition_clip_new (GESVideoStandardTransitionType vtype)
346 {
347   GEnumValue *value;
348   GEnumClass *klass;
349   GESTransitionClip *ret = NULL;
350
351   klass =
352       G_ENUM_CLASS (g_type_class_ref (GES_VIDEO_STANDARD_TRANSITION_TYPE_TYPE));
353   if (!klass) {
354     GST_ERROR ("Could not find the StandarTransitionType enum class");
355     return NULL;
356   }
357
358   value = g_enum_get_value (klass, vtype);
359   if (!value) {
360     GST_ERROR ("Could not find enum value for %i", vtype);
361     return NULL;
362   }
363
364   ret = ges_transition_clip_new_for_nick (((gchar *) value->value_nick));
365   g_type_class_unref (klass);
366
367   return ret;
368 }
369
370 /**
371  * ges_transition_clip_new_for_nick:
372  * @nick: a string representing the type of transition to create
373  *
374  * Creates a new #GESTransitionClip for the provided @nick.
375  *
376  * Returns: (transfer floating) (nullable): The newly created #GESTransitionClip,
377  * or %NULL if something went wrong
378  */
379
380 GESTransitionClip *
381 ges_transition_clip_new_for_nick (gchar * nick)
382 {
383   GESTransitionClip *ret = NULL;
384   GESAsset *asset = ges_asset_request (GES_TYPE_TRANSITION_CLIP, nick, NULL);
385
386   if (asset != NULL) {
387     ret = GES_TRANSITION_CLIP (ges_asset_extract (asset, NULL));
388
389     gst_object_unref (asset);
390   } else
391     GST_WARNING ("No asset found for nick: %s", nick);
392
393   return ret;
394 }