2815ad23456c67504dec0c1713f07dc3f2c2992e
[platform/upstream/gst-editing-services.git] / tools / utils.c
1 /* GStreamer Editing Services
2  * Copyright (C) 2015 Mathieu Duponchelle <mathieu.duponchelle@opencreed.com>
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 #include <stdlib.h>
21 #include <glib/gprintf.h>
22 #include <string.h>
23 #include <gst/gst.h>
24 #include "utils.h"
25 #include "../ges/ges-internal.h"
26
27 #undef GST_CAT_DEFAULT
28
29 /* Copy of GST_ASCII_IS_STRING */
30 #define ASCII_IS_STRING(c) (g_ascii_isalnum((c)) || ((c) == '_') || \
31     ((c) == '-') || ((c) == '+') || ((c) == '/') || ((c) == ':') || \
32     ((c) == '.'))
33
34 /* g_free after usage */
35 static gchar *
36 _sanitize_argument (gchar * arg, const gchar * prev_arg)
37 {
38   gboolean expect_equal = !(arg[0] == '+' || g_str_has_prefix (arg, "set-")
39       || prev_arg == NULL || prev_arg[0] == '+'
40       || g_str_has_prefix (prev_arg, "set-"));
41   gboolean need_wrap = FALSE;
42   gchar *first_equal = NULL;
43   gchar *wrap_start;
44   gchar *new_string, *tmp_string;
45   gsize num_escape;
46
47   for (tmp_string = arg; *tmp_string != '\0'; tmp_string++) {
48     if (expect_equal && first_equal == NULL && *tmp_string == '=') {
49       first_equal = tmp_string;
50       /* if this is the first equal, then don't count it as necessarily
51        * needing a wrap */
52     } else if (!ASCII_IS_STRING (*tmp_string)) {
53       need_wrap = TRUE;
54       break;
55     }
56   }
57
58   if (!need_wrap)
59     return g_strdup (arg);
60
61   if (first_equal)
62     wrap_start = first_equal + 1;
63   else
64     wrap_start = arg;
65
66   /* need to escape any '"' or '\\' to correctly parse in as a structure */
67   num_escape = 0;
68   for (tmp_string = wrap_start; *tmp_string != '\0'; tmp_string++) {
69     if (*tmp_string == '"' || *tmp_string == '\\')
70       num_escape++;
71   }
72
73   tmp_string = new_string =
74       g_malloc (sizeof (gchar) * (strlen (arg) + num_escape + 3));
75
76   while (arg != wrap_start)
77     *(tmp_string++) = *(arg++);
78   (*tmp_string++) = '"';
79
80   while (*arg != '\0') {
81     if (*arg == '"' || *arg == '\\')
82       (*tmp_string++) = '\\';
83     *(tmp_string++) = *(arg++);
84   }
85   *(tmp_string++) = '"';
86   *tmp_string = '\0';
87
88   return new_string;
89 }
90
91 gchar *
92 sanitize_timeline_description (gchar ** args, GESLauncherParsedOptions * opts)
93 {
94   gint i;
95   gchar *prev_arg = NULL;
96   GString *track_def;
97   GString *timeline_str;
98
99   gchar *string = g_strdup (" ");
100
101   for (i = 1; args[i]; i++) {
102     gchar *new_string;
103     gchar *sanitized = _sanitize_argument (args[i], prev_arg);
104
105     new_string = g_strconcat (string, " ", sanitized, NULL);
106
107     g_free (sanitized);
108     g_free (string);
109     string = new_string;
110     prev_arg = args[i];
111   }
112
113   if (strstr (string, "+track")) {
114     gchar *res = g_strconcat ("ges:", string, NULL);
115     g_free (string);
116
117     return res;
118   }
119
120   timeline_str = g_string_new (string);
121   g_free (string);
122
123   if (opts->track_types & GES_TRACK_TYPE_VIDEO) {
124     track_def = g_string_new (" +track video ");
125
126     if (opts->video_track_caps)
127       g_string_append_printf (track_def, " restrictions=[%s] ",
128           opts->video_track_caps);
129
130     g_string_prepend (timeline_str, track_def->str);
131     g_string_free (track_def, TRUE);
132   }
133
134   if (opts->track_types & GES_TRACK_TYPE_AUDIO) {
135     track_def = g_string_new (" +track audio ");
136
137     if (opts->audio_track_caps)
138       g_string_append_printf (track_def, " restrictions=[%s] ",
139           opts->audio_track_caps);
140
141     g_string_prepend (timeline_str, track_def->str);
142     g_string_free (track_def, TRUE);
143   }
144
145   g_string_prepend (timeline_str, "ges:");
146
147   return g_string_free (timeline_str, FALSE);
148 }
149
150 gboolean
151 get_flags_from_string (GType type, const gchar * str_flags, guint * flags)
152 {
153   GValue value = G_VALUE_INIT;
154   g_value_init (&value, type);
155
156   if (!gst_value_deserialize (&value, str_flags)) {
157     g_value_unset (&value);
158
159     return FALSE;
160   }
161
162   *flags = g_value_get_flags (&value);
163   g_value_unset (&value);
164
165   return TRUE;
166 }
167
168 gchar *
169 ensure_uri (const gchar * location)
170 {
171   if (gst_uri_is_valid (location))
172     return g_strdup (location);
173   else
174     return gst_filename_to_uri (location, NULL);
175 }
176
177 GstEncodingProfile *
178 parse_encoding_profile (const gchar * format)
179 {
180   GstEncodingProfile *profile;
181   GValue value = G_VALUE_INIT;
182
183   g_value_init (&value, GST_TYPE_ENCODING_PROFILE);
184
185   if (!gst_value_deserialize (&value, format)) {
186     g_value_reset (&value);
187
188     return NULL;
189   }
190
191   profile = g_value_dup_object (&value);
192   g_value_reset (&value);
193
194   return profile;
195 }
196
197 void
198 print_enum (GType enum_type)
199 {
200   GEnumClass *enum_class = G_ENUM_CLASS (g_type_class_ref (enum_type));
201   guint i;
202
203   for (i = 0; i < enum_class->n_values; i++) {
204     g_printf ("%s\n", enum_class->values[i].value_nick);
205   }
206
207   g_type_class_unref (enum_class);
208 }
209
210 void
211 ges_print (GstDebugColorFlags c, gboolean err, gboolean nline,
212     const gchar * format, va_list var_args)
213 {
214   GString *str = g_string_new (NULL);
215   GstDebugColorMode color_mode;
216   gchar *color = NULL;
217   const gchar *clear = NULL;
218
219   color_mode = gst_debug_get_color_mode ();
220 #ifdef G_OS_WIN32
221   if (color_mode == GST_DEBUG_COLOR_MODE_UNIX) {
222 #else
223   if (color_mode != GST_DEBUG_COLOR_MODE_OFF) {
224 #endif
225     clear = "\033[00m";
226     color = gst_debug_construct_term_color (c);
227   }
228
229   if (color) {
230     g_string_append (str, color);
231     g_free (color);
232   }
233
234   g_string_append_vprintf (str, format, var_args);
235
236   if (nline)
237     g_string_append_c (str, '\n');
238
239   if (clear)
240     g_string_append (str, clear);
241
242   if (err)
243     g_printerr ("%s", str->str);
244   else
245     g_print ("%s", str->str);
246
247   g_string_free (str, TRUE);
248 }
249
250 void
251 ges_ok (const gchar * format, ...)
252 {
253   va_list var_args;
254
255   va_start (var_args, format);
256   ges_print (GST_DEBUG_FG_GREEN, FALSE, TRUE, format, var_args);
257   va_end (var_args);
258 }
259
260 void
261 ges_warn (const gchar * format, ...)
262 {
263   va_list var_args;
264
265   va_start (var_args, format);
266   ges_print (GST_DEBUG_FG_YELLOW, TRUE, TRUE, format, var_args);
267   va_end (var_args);
268 }
269
270 void
271 ges_printerr (const gchar * format, ...)
272 {
273   va_list var_args;
274
275   va_start (var_args, format);
276   ges_print (GST_DEBUG_FG_RED, TRUE, TRUE, format, var_args);
277   va_end (var_args);
278 }
279
280 gchar *
281 get_file_extension (gchar * uri)
282 {
283   size_t len;
284   gint find;
285
286   len = strlen (uri);
287   find = len - 1;
288
289   while (find >= 0) {
290     if (uri[find] == '.')
291       break;
292     find--;
293   }
294
295   if (find <= 0)
296     return NULL;
297
298   return g_strdup (&uri[find + 1]);
299 }
300
301 static const gchar *
302 get_type_icon (gpointer obj)
303 {
304   if (GST_IS_ENCODING_AUDIO_PROFILE (obj) || GST_IS_DISCOVERER_AUDIO_INFO (obj))
305     return "♫";
306   else if (GST_IS_ENCODING_VIDEO_PROFILE (obj)
307       || GST_IS_DISCOVERER_VIDEO_INFO (obj))
308     return "▶";
309   else if (GST_IS_ENCODING_CONTAINER_PROFILE (obj)
310       || GST_IS_DISCOVERER_CONTAINER_INFO (obj))
311     return "∋";
312   else
313     return "";
314 }
315
316 static void
317 print_profile (GstEncodingProfile * profile, const gchar * prefix)
318 {
319   const gchar *name = gst_encoding_profile_get_name (profile);
320   const gchar *desc = gst_encoding_profile_get_description (profile);
321   GstCaps *format = gst_encoding_profile_get_format (profile);
322   gchar *capsdesc = NULL;
323
324   if (gst_caps_is_fixed (format))
325     capsdesc = gst_pb_utils_get_codec_description (format);
326   if (!capsdesc)
327     capsdesc = gst_caps_to_string (format);
328
329   if (GST_IS_ENCODING_CONTAINER_PROFILE (profile)) {
330     g_print ("%s> %s %s: %s%s%s%s\n", prefix,
331         get_type_icon (profile),
332         capsdesc, name ? name : "",
333         desc ? " (" : "", desc ? desc : "", desc ? ")" : "");
334
335   } else {
336     g_print ("%s%s %s%s%s%s%s%s", prefix, get_type_icon (profile),
337         name ? name : capsdesc, desc ? ": " : "", desc ? desc : "",
338         name ? " (" : "", name ? capsdesc : "", name ? ")" : "");
339
340     if (GST_IS_ENCODING_VIDEO_PROFILE (profile)) {
341       GstCaps *caps = gst_encoding_profile_get_restriction (profile);
342
343       if (!caps && gst_caps_is_fixed (format))
344         caps = gst_caps_ref (format);
345
346       if (caps) {
347         GstVideoInfo info;
348
349         if (gst_video_info_from_caps (&info, caps)) {
350           g_print (" (%dx%d", info.width, info.height);
351           if (info.fps_n)
352             g_print ("@%d/%dfps", info.fps_n, info.fps_d);
353           g_print (")");
354         }
355         gst_caps_unref (caps);
356       }
357     } else if (GST_IS_ENCODING_AUDIO_PROFILE (profile)) {
358       GstCaps *caps = gst_encoding_profile_get_restriction (profile);
359
360       if (!caps && gst_caps_is_fixed (format))
361         caps = gst_caps_ref (format);
362
363       if (caps) {
364         GstAudioInfo info;
365
366         if (gst_caps_is_fixed (caps) && gst_audio_info_from_caps (&info, caps))
367           g_print (" (%d channels @ %dhz)", info.channels, info.rate);
368         gst_caps_unref (caps);
369       }
370     }
371
372
373     g_print ("\n");
374   }
375
376   gst_caps_unref (format);
377
378   g_free (capsdesc);
379 }
380
381 void
382 describe_encoding_profile (GstEncodingProfile * profile)
383 {
384   g_return_if_fail (GST_IS_ENCODING_PROFILE (profile));
385
386   print_profile (profile, "     ");
387   if (GST_IS_ENCODING_CONTAINER_PROFILE (profile)) {
388     const GList *tmp;
389
390     for (tmp =
391         gst_encoding_container_profile_get_profiles
392         (GST_ENCODING_CONTAINER_PROFILE (profile)); tmp; tmp = tmp->next)
393       print_profile (tmp->data, "       - ");
394   }
395 }
396
397 static void
398 describe_stream_info (GstDiscovererStreamInfo * sinfo, GString * desc)
399 {
400   gchar *capsdesc;
401   GstCaps *caps;
402
403   caps = gst_discoverer_stream_info_get_caps (sinfo);
404   capsdesc = gst_pb_utils_get_codec_description (caps);
405   if (!capsdesc)
406     capsdesc = gst_caps_to_string (caps);
407   gst_caps_unref (caps);
408
409   g_string_append_printf (desc, "%s%s%s", desc->len ? ", " : "",
410       get_type_icon (sinfo), capsdesc);
411
412   if (GST_IS_DISCOVERER_CONTAINER_INFO (sinfo)) {
413     GList *tmp, *streams;
414
415     streams =
416         gst_discoverer_container_info_get_streams (GST_DISCOVERER_CONTAINER_INFO
417         (sinfo));
418     for (tmp = streams; tmp; tmp = tmp->next)
419       describe_stream_info (tmp->data, desc);
420     gst_discoverer_stream_info_list_free (streams);
421   }
422 }
423
424 static gchar *
425 describe_discoverer (GstDiscovererInfo * info)
426 {
427   GString *desc = g_string_new (NULL);
428   GstDiscovererStreamInfo *sinfo = gst_discoverer_info_get_stream_info (info);
429
430   describe_stream_info (sinfo, desc);
431   gst_discoverer_stream_info_unref (sinfo);
432
433   return g_string_free (desc, FALSE);
434 }
435
436 void
437 print_timeline (GESTimeline * timeline)
438 {
439   GList *layer, *clip, *clips;
440
441   if (!timeline->layers)
442     return;
443
444   g_print ("\nTimeline description:\n");
445   g_print ("====================\n\n");
446   for (layer = timeline->layers; layer; layer = layer->next) {
447     clips = ges_layer_get_clips (layer->data);
448
449     if (!clips)
450       continue;
451
452     g_printerr ("  layer %d: \n", ges_layer_get_priority (layer->data));
453     g_printerr ("  --------\n");
454     for (clip = clips; clip; clip = clip->next) {
455       gchar *name;
456
457       if (GES_IS_URI_CLIP (clip->data)) {
458         GESUriClipAsset *asset =
459             GES_URI_CLIP_ASSET (ges_extractable_get_asset (clip->data));
460         gchar *asset_desc =
461             describe_discoverer (ges_uri_clip_asset_get_info (asset));
462
463         name = g_strdup_printf ("Clip from: '%s' [%s]",
464             ges_asset_get_id (GES_ASSET (asset)), asset_desc);
465         g_free (asset_desc);
466       } else {
467         name = g_strdup (GES_TIMELINE_ELEMENT_NAME (clip->data));
468       }
469       g_print ("    - %s\n        start=%" GST_TIME_FORMAT,
470           name, GST_TIME_ARGS (GES_TIMELINE_ELEMENT_START (clip->data)));
471       g_free (name);
472       if (GES_TIMELINE_ELEMENT_INPOINT (clip->data))
473         g_print (" inpoint=%" GST_TIME_FORMAT,
474             GST_TIME_ARGS (GES_TIMELINE_ELEMENT_INPOINT (clip->data)));
475       g_print (" duration=%" GST_TIME_FORMAT "\n",
476           GST_TIME_ARGS (GES_TIMELINE_ELEMENT_END (clip->data)));
477     }
478     if (layer->next)
479       g_printerr ("\n");
480
481     g_list_free_full (clips, gst_object_unref);
482   }
483
484   g_print ("\n");
485 }