documentation: fixed a heap o' typos
[platform/upstream/gstreamer.git] / tools / utils.c
1 #include <string.h>
2
3 #include "utils.h"
4 #include <gst/pbutils/descriptions.h>
5
6 void
7 print (GstDebugColorFlags c, gboolean err, gboolean nline, const gchar * format,
8     va_list var_args)
9 {
10   GString *str = g_string_new (NULL);
11   GstDebugColorMode color_mode;
12   gchar *color = NULL;
13   const gchar *clear = NULL;
14
15   color_mode = gst_debug_get_color_mode ();
16 #ifdef G_OS_WIN32
17   if (color_mode == GST_DEBUG_COLOR_MODE_UNIX) {
18 #else
19   if (color_mode != GST_DEBUG_COLOR_MODE_OFF) {
20 #endif
21     clear = "\033[00m";
22     color = gst_debug_construct_term_color (c);
23   }
24
25   if (color) {
26     g_string_append (str, color);
27     g_free (color);
28   }
29
30   g_string_append_vprintf (str, format, var_args);
31
32   if (nline)
33     g_string_append_c (str, '\n');
34
35   if (clear)
36     g_string_append (str, clear);
37
38   if (err)
39     g_printerr ("%s", str->str);
40   else
41     g_print ("%s", str->str);
42
43   g_string_free (str, TRUE);
44 }
45
46 void
47 ok (const gchar * format, ...)
48 {
49   va_list var_args;
50
51   va_start (var_args, format);
52   print (GST_DEBUG_FG_GREEN, FALSE, TRUE, format, var_args);
53   va_end (var_args);
54 }
55
56 void
57 warn (const gchar * format, ...)
58 {
59   va_list var_args;
60
61   va_start (var_args, format);
62   print (GST_DEBUG_FG_YELLOW, TRUE, TRUE, format, var_args);
63   va_end (var_args);
64 }
65
66 void
67 error (const gchar * format, ...)
68 {
69   va_list var_args;
70
71   va_start (var_args, format);
72   print (GST_DEBUG_FG_RED, TRUE, TRUE, format, var_args);
73   va_end (var_args);
74 }
75
76 gchar *
77 ensure_uri (const gchar * location)
78 {
79   if (gst_uri_is_valid (location))
80     return g_strdup (location);
81   else
82     return gst_filename_to_uri (location, NULL);
83 }
84
85 gchar *
86 get_file_extension (gchar * uri)
87 {
88   size_t len;
89   gint find;
90
91   len = strlen (uri);
92   find = len - 1;
93
94   while (find >= 0) {
95     if (uri[find] == '.')
96       break;
97     find--;
98   }
99
100   if (find < 0)
101     return NULL;
102
103   return &uri[find + 1];
104 }
105
106 GList *
107 get_usable_profiles (GstEncodingTarget * target)
108 {
109   GList *tmpprof, *usable_profiles = NULL;
110
111   for (tmpprof = (GList *) gst_encoding_target_get_profiles (target);
112       tmpprof; tmpprof = tmpprof->next) {
113     GstEncodingProfile *profile = tmpprof->data;
114     GstElement *tmpencodebin = gst_element_factory_make ("encodebin", NULL);
115
116     gst_encoding_profile_set_presence (profile, 1);
117     if (GST_IS_ENCODING_CONTAINER_PROFILE (profile)) {
118       GList *tmpsubprof;
119       for (tmpsubprof = (GList *)
120           gst_encoding_container_profile_get_profiles
121           (GST_ENCODING_CONTAINER_PROFILE (profile)); tmpsubprof;
122           tmpsubprof = tmpsubprof->next)
123         gst_encoding_profile_set_presence (tmpsubprof->data, 1);
124     }
125
126     g_object_set (tmpencodebin, "profile", gst_object_ref (profile), NULL);
127     GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (tmpencodebin),
128         GST_DEBUG_GRAPH_SHOW_ALL, gst_encoding_profile_get_name (profile));
129
130     /* The profile could be expended */
131     if (GST_BIN (tmpencodebin)->children)
132       usable_profiles = g_list_prepend (usable_profiles, profile);
133
134     gst_object_unref (tmpencodebin);
135   }
136
137   return usable_profiles;
138 }
139
140 GstEncodingProfile *
141 create_encoding_profile (const gchar * pname)
142 {
143   GstEncodingProfile *profile;
144   GValue value = G_VALUE_INIT;
145
146   g_value_init (&value, GST_TYPE_ENCODING_PROFILE);
147
148   if (!gst_value_deserialize (&value, pname)) {
149     g_value_reset (&value);
150
151     return NULL;
152   }
153
154   profile = g_value_dup_object (&value);
155   g_value_reset (&value);
156
157   return profile;
158 }
159
160 static const gchar *
161 get_profile_type (GstEncodingProfile * profile)
162 {
163   if (GST_IS_ENCODING_CONTAINER_PROFILE (profile))
164     return "Container";
165   else if (GST_IS_ENCODING_AUDIO_PROFILE (profile))
166     return "Audio";
167   else if (GST_IS_ENCODING_VIDEO_PROFILE (profile))
168     return "Video";
169   else
170     return "Unknown";
171 }
172
173 static void
174 print_profile (GstEncodingProfile * profile, const gchar * prefix)
175 {
176   const gchar *name = gst_encoding_profile_get_name (profile);
177   const gchar *desc = gst_encoding_profile_get_description (profile);
178   GstCaps *format = gst_encoding_profile_get_format (profile);
179   gchar *capsdesc;
180
181   if (gst_caps_is_fixed (format))
182     capsdesc = gst_pb_utils_get_codec_description (format);
183   else
184     capsdesc = gst_caps_to_string (format);
185
186   g_print ("%s%s: %s%s%s%s%s%s\n", prefix, get_profile_type (profile),
187       name ? name : capsdesc, desc ? ": " : "", desc ? desc : "",
188       name ? " (" : "", name ? capsdesc : "", name ? ")" : "");
189
190   g_free (capsdesc);
191 }
192
193 void
194 describe_encoding_profile (GstEncodingProfile * profile)
195 {
196   g_return_if_fail (GST_IS_ENCODING_PROFILE (profile));
197
198   print_profile (profile, "  ");
199   if (GST_IS_ENCODING_CONTAINER_PROFILE (profile)) {
200     const GList *tmp;
201
202     for (tmp =
203         gst_encoding_container_profile_get_profiles
204         (GST_ENCODING_CONTAINER_PROFILE (profile)); tmp; tmp = tmp->next)
205       print_profile (tmp->data, "    - ");
206   }
207
208 }