Release 1.19.2
[platform/upstream/gst-editing-services.git] / ges / ges-effect-clip.c
1 /* GStreamer Editing Services
2  * Copyright (C) 2011 Thibault Saunier <thibault.saunier@collabora.co.uk>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /**
21  * SECTION: geseffectclip
22  * @title: GESEffectClip
23  * @short_description: An effect created by parse-launch style bin descriptions
24  * in a GESLayer
25  *
26  * The effect will be applied on the sources that have lower priorities
27  * (higher number) between the inpoint and the end of it.
28  *
29  * The asset ID of an effect clip is in the form:
30  *
31  * ```
32  *   "audio ! bin ! description || video ! bin ! description"
33  * ```
34  */
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <ges/ges.h>
40 #include "ges-internal.h"
41 #include "ges-types.h"
42
43 struct _GESEffectClipPrivate
44 {
45   gchar *video_bin_description;
46   gchar *audio_bin_description;
47 };
48
49 static void ges_extractable_interface_init (GESExtractableInterface * iface);
50 G_DEFINE_TYPE_WITH_CODE (GESEffectClip, ges_effect_clip,
51     GES_TYPE_BASE_EFFECT_CLIP, G_ADD_PRIVATE (GESEffectClip)
52     G_IMPLEMENT_INTERFACE (GES_TYPE_EXTRACTABLE,
53         ges_extractable_interface_init));
54
55 enum
56 {
57   PROP_0,
58   PROP_VIDEO_BIN_DESCRIPTION,
59   PROP_AUDIO_BIN_DESCRIPTION,
60 };
61
62 static void ges_effect_clip_finalize (GObject * object);
63 static GESTrackElement *_create_track_element (GESClip * self,
64     GESTrackType type);
65
66 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;       /* Start ignoring GParameter deprecation */
67 static GParameter *
68 extractable_get_parameters_from_id (const gchar * id, guint * n_params)
69 {
70   gchar *bin_desc;
71   GESTrackType ttype;
72   GParameter *params = g_new0 (GParameter, 2);
73   gchar **effects_desc = g_strsplit (id, "||", -1);
74   gint i;
75
76   *n_params = 0;
77
78   if (g_strv_length (effects_desc) > 2)
79     GST_ERROR ("EffectClip id %s contains too many effect descriptions", id);
80
81   for (i = 0; effects_desc[i] && i < 2; i++) {
82     bin_desc =
83         ges_effect_asset_id_get_type_and_bindesc (effects_desc[i], &ttype,
84         NULL);
85
86     if (ttype == GES_TRACK_TYPE_AUDIO) {
87       *n_params = *n_params + 1;
88       params[*n_params - 1].name = "audio-bin-description";
89     } else if (ttype == GES_TRACK_TYPE_VIDEO) {
90       *n_params = *n_params + 1;
91       params[i].name = "video-bin-description";
92     } else {
93       g_free (bin_desc);
94       GST_ERROR ("Could not find effect type for %s", effects_desc[i]);
95       continue;
96     }
97
98     g_value_init (&params[*n_params - 1].value, G_TYPE_STRING);
99     g_value_set_string (&params[*n_params - 1].value, bin_desc);
100     g_free (bin_desc);
101   }
102
103   g_strfreev (effects_desc);
104   return params;
105 }
106
107 G_GNUC_END_IGNORE_DEPRECATIONS; /* End ignoring GParameter deprecation */
108
109 static gchar *
110 extractable_check_id (GType type, const gchar * id, GError ** error)
111 {
112   return g_strdup (id);
113 }
114
115 static gchar *
116 extractable_get_id (GESExtractable * self)
117 {
118   GString *id = g_string_new (NULL);
119   GESEffectClipPrivate *priv = GES_EFFECT_CLIP (self)->priv;
120
121   if (priv->audio_bin_description)
122     g_string_append_printf (id, "audio %s ||", priv->audio_bin_description);
123   if (priv->video_bin_description)
124     g_string_append_printf (id, "video %s", priv->video_bin_description);
125
126   return g_string_free (id, FALSE);
127 }
128
129 static void
130 ges_extractable_interface_init (GESExtractableInterface * iface)
131 {
132   iface->asset_type = GES_TYPE_ASSET;
133   iface->check_id = extractable_check_id;
134   iface->get_parameters_from_id = extractable_get_parameters_from_id;
135   iface->get_id = extractable_get_id;
136 }
137
138
139 static void
140 ges_effect_clip_finalize (GObject * object)
141 {
142   GESEffectClipPrivate *priv = GES_EFFECT_CLIP (object)->priv;
143
144   g_free (priv->audio_bin_description);
145   g_free (priv->video_bin_description);
146
147   G_OBJECT_CLASS (ges_effect_clip_parent_class)->finalize (object);
148 }
149
150 static void
151 ges_effect_clip_get_property (GObject * object,
152     guint property_id, GValue * value, GParamSpec * pspec)
153 {
154   GESEffectClipPrivate *priv = GES_EFFECT_CLIP (object)->priv;
155
156   switch (property_id) {
157     case PROP_VIDEO_BIN_DESCRIPTION:
158       g_value_set_string (value, priv->video_bin_description);
159       break;
160     case PROP_AUDIO_BIN_DESCRIPTION:
161       g_value_set_string (value, priv->audio_bin_description);
162       break;
163     default:
164       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
165   }
166 }
167
168 static void
169 ges_effect_clip_set_property (GObject * object,
170     guint property_id, const GValue * value, GParamSpec * pspec)
171 {
172   GESEffectClip *self = GES_EFFECT_CLIP (object);
173
174   switch (property_id) {
175     case PROP_VIDEO_BIN_DESCRIPTION:
176       self->priv->video_bin_description = g_value_dup_string (value);
177       break;
178     case PROP_AUDIO_BIN_DESCRIPTION:
179       self->priv->audio_bin_description = g_value_dup_string (value);
180       break;
181     default:
182       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
183   }
184 }
185
186 static void
187 ges_effect_clip_class_init (GESEffectClipClass * klass)
188 {
189   GObjectClass *object_class = G_OBJECT_CLASS (klass);
190   GESClipClass *timobj_class = GES_CLIP_CLASS (klass);
191
192   object_class->get_property = ges_effect_clip_get_property;
193   object_class->set_property = ges_effect_clip_set_property;
194   object_class->finalize = ges_effect_clip_finalize;
195
196   /**
197    * GESEffectClip:video-bin-description:
198    *
199    * The description of the video track of the effect bin with a gst-launch-style
200    * pipeline description. This should be used for test purposes.
201    *
202    * Example: "videobalance saturation=1.5 hue=+0.5"
203    */
204   g_object_class_install_property (object_class, PROP_VIDEO_BIN_DESCRIPTION,
205       g_param_spec_string ("video-bin-description",
206           "Video bin description",
207           "Description of the video track of the effect",
208           NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
209
210   /**
211    * GESEffectClip:audio-bin-description:
212    *
213    * The description of the audio track of the effect bin with a gst-launch-style
214    * pipeline description. This should be used for test purposes.
215    *
216    * Example: "audiopanorama panorama=1.0"
217    */
218   g_object_class_install_property (object_class, PROP_AUDIO_BIN_DESCRIPTION,
219       g_param_spec_string ("audio-bin-description",
220           "bin description",
221           "Bin description of the audio track of the effect",
222           NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
223
224   timobj_class->create_track_element = _create_track_element;
225 }
226
227 static void
228 ges_effect_clip_init (GESEffectClip * self)
229 {
230   self->priv = ges_effect_clip_get_instance_private (self);
231 }
232
233 static GESTrackElement *
234 _create_track_element (GESClip * self, GESTrackType type)
235 {
236   const gchar *bin_description = NULL;
237   GESEffectClip *effect = GES_EFFECT_CLIP (self);
238
239   if (type == GES_TRACK_TYPE_VIDEO) {
240     bin_description = effect->priv->video_bin_description;
241   } else if (type == GES_TRACK_TYPE_AUDIO) {
242     bin_description = effect->priv->audio_bin_description;
243   }
244
245   if (bin_description)
246     return GES_TRACK_ELEMENT (ges_effect_new (bin_description));
247
248   GST_WARNING ("Effect doesn't handle this track type");
249   return NULL;
250 }
251
252 /**
253  * ges_effect_clip_new:
254  * @video_bin_description: The gst-launch like bin description of the effect
255  * @audio_bin_description: The gst-launch like bin description of the effect
256  *
257  * Creates a new #GESEffectClip from the description of the bin.
258  *
259  * Returns: (transfer floating) (nullable): a newly created #GESEffectClip, or
260  * %NULL if something went wrong.
261  */
262 GESEffectClip *
263 ges_effect_clip_new (const gchar * video_bin_description,
264     const gchar * audio_bin_description)
265 {
266   GESAsset *asset;
267   GESEffectClip *res;
268   GString *id = g_string_new (NULL);
269
270   if (audio_bin_description)
271     g_string_append_printf (id, "audio %s ||", audio_bin_description);
272   if (video_bin_description)
273     g_string_append_printf (id, "video %s", video_bin_description);
274
275   asset = ges_asset_request (GES_TYPE_EFFECT_CLIP, id->str, NULL);
276   res = GES_EFFECT_CLIP (ges_asset_extract (asset, NULL));
277   g_string_free (id, TRUE);
278   gst_object_unref (asset);
279
280   return res;
281 }