misc: Fix various compiler warnings on MinGW
[platform/upstream/gstreamer.git] / tools / gst-inspect.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *               2000 Wim Taymans <wtay@chello.be>
4  *               2004 Thomas Vander Stichele <thomas@apestaart.org>
5  *               2018 Collabora Ltd.
6  *
7  * gst-inspect.c: tool to inspect the GStreamer registry
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #  include "config.h"
27 #endif
28
29 /* FIXME 2.0: suppress warnings for deprecated API such as GValueArray
30  * with newer GLib versions (>= 2.31.0) */
31 #define GLIB_DISABLE_DEPRECATION_WARNINGS
32
33 #include "tools.h"
34 #include <gst/gst_private.h>    /* for internal Factories */
35
36 #include <string.h>
37 #include <locale.h>
38 #include <glib/gprintf.h>
39 #ifdef G_OS_UNIX
40 #   include <unistd.h>
41 #   include <sys/wait.h>
42 #endif
43
44 #define DEFAULT_PAGER "less"
45
46 /* "R" : support color
47  * "X" : do not clear the screen when leaving the pager
48  * "F" : skip the pager if content fit into the screen
49  */
50 #define DEFAULT_LESS_OPTS "RXF"
51
52 gboolean colored_output = TRUE;
53
54 #ifdef G_OS_UNIX
55 GPid child_pid = -1;
56 #endif
57 GMainLoop *loop = NULL;
58
59 /* Console colors */
60
61 /* Escape values for colors */
62 #define BLUE      "\033[34m"
63 #define BRBLUE    "\033[94m"
64 #define BRCYAN    "\033[96m"
65 #define BRMAGENTA "\033[95m"
66 #define BRYELLOW  "\033[33m"
67 #define CYAN      "\033[36m"
68 #define GREEN     "\033[32m"
69 #define MAGENTA   "\033[35m"
70 #define YELLOW    "\033[33m"
71
72 /* General colors */
73 #define RESET_COLOR           (colored_output? "\033[0m": "")
74 #define HEADING_COLOR         (colored_output? BRYELLOW : "")
75 #define PROP_NAME_COLOR       (colored_output? BRBLUE : "")
76 #define PROP_VALUE_COLOR      (colored_output? RESET_COLOR: "")
77 #define PROP_ATTR_NAME_COLOR  (colored_output? BRYELLOW : "")
78 #define PROP_ATTR_VALUE_COLOR (colored_output? CYAN: "")
79 /* FIXME: find a good color that works on both dark & light bg. */
80 #define DESC_COLOR            (colored_output? RESET_COLOR: "")
81
82 /* Datatype-related colors */
83 #define DATATYPE_COLOR        (colored_output? GREEN : "")
84 #define CHILD_LINK_COLOR      (colored_output? BRMAGENTA : "")
85
86 /* Caps colors */
87 #define FIELD_NAME_COLOR      (colored_output? CYAN: "")
88 #define FIELD_VALUE_COLOR     (colored_output? BRBLUE : "")
89 #define CAPS_TYPE_COLOR       (colored_output? YELLOW : "")
90 #define STRUCT_NAME_COLOR     (colored_output? YELLOW : "")
91 #define CAPS_FEATURE_COLOR    (colored_output? GREEN : "")
92
93 /* Plugin listing colors */
94 #define PLUGIN_NAME_COLOR     (colored_output? BRBLUE : "")
95 #define ELEMENT_NAME_COLOR    (colored_output? GREEN : "")
96 /* FIXME: find a good color that works on both dark & light bg. */
97 #define ELEMENT_DETAIL_COLOR  (colored_output? RESET_COLOR : "")
98 #define PLUGIN_FEATURE_COLOR  (colored_output? BRBLUE: "")
99
100 /* Feature listing colors */
101 #define FEATURE_NAME_COLOR    (colored_output? GREEN : "")
102 #define FEATURE_DIR_COLOR     (colored_output? BRMAGENTA : "")
103 #define FEATURE_RANK_COLOR    (colored_output? CYAN : "")
104 #define FEATURE_PROTO_COLOR   (colored_output? BRYELLOW : "")
105
106 static char *_name = NULL;
107 static int indent = 0;
108
109 static int print_element_info (GstPluginFeature * feature,
110     gboolean print_names);
111 static int print_typefind_info (GstPluginFeature * feature,
112     gboolean print_names);
113 static int print_tracer_info (GstPluginFeature * feature, gboolean print_names);
114
115 #define push_indent() push_indent_n(1)
116 #define pop_indent() push_indent_n(-1)
117 #define pop_indent_n(n) push_indent_n(-n)
118
119 static void
120 push_indent_n (int n)
121 {
122   g_assert (n > 0 || indent > 0);
123   indent += n;
124 }
125
126 /* *INDENT-OFF* */
127 G_GNUC_PRINTF (1, 2)
128 /* *INDENT-ON* */
129
130 static void
131 n_print (const char *format, ...)
132 {
133   va_list args;
134   int i;
135
136   if (_name)
137     g_print ("%s", _name);
138
139   for (i = 0; i < indent; ++i)
140     g_print ("  ");
141
142   va_start (args, format);
143   g_vprintf (format, args);
144   va_end (args);
145 }
146
147 static gboolean
148 print_field (GQuark field, const GValue * value, gpointer pfx)
149 {
150   gchar *str = gst_value_serialize (value);
151
152   n_print ("%s  %s%15s%s: %s%s%s\n",
153       (gchar *) pfx, FIELD_NAME_COLOR, g_quark_to_string (field), RESET_COLOR,
154       FIELD_VALUE_COLOR, str, RESET_COLOR);
155   g_free (str);
156   return TRUE;
157 }
158
159 static void
160 print_caps (const GstCaps * caps, const gchar * pfx)
161 {
162   guint i;
163
164   g_return_if_fail (caps != NULL);
165
166   if (gst_caps_is_any (caps)) {
167     n_print ("%s%sANY%s\n", CAPS_TYPE_COLOR, pfx, RESET_COLOR);
168     return;
169   }
170   if (gst_caps_is_empty (caps)) {
171     n_print ("%s%sEMPTY%s\n", CAPS_TYPE_COLOR, pfx, RESET_COLOR);
172     return;
173   }
174
175   for (i = 0; i < gst_caps_get_size (caps); i++) {
176     GstStructure *structure = gst_caps_get_structure (caps, i);
177     GstCapsFeatures *features = gst_caps_get_features (caps, i);
178
179     if (features && (gst_caps_features_is_any (features) ||
180             !gst_caps_features_is_equal (features,
181                 GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY))) {
182       gchar *features_string = gst_caps_features_to_string (features);
183
184       n_print ("%s%s%s%s(%s%s%s)\n", pfx, STRUCT_NAME_COLOR,
185           gst_structure_get_name (structure), RESET_COLOR,
186           CAPS_FEATURE_COLOR, features_string, RESET_COLOR);
187       g_free (features_string);
188     } else {
189       n_print ("%s%s%s%s\n", pfx, STRUCT_NAME_COLOR,
190           gst_structure_get_name (structure), RESET_COLOR);
191     }
192     gst_structure_foreach (structure, print_field, (gpointer) pfx);
193   }
194 }
195
196 static const char *
197 get_rank_name (char *s, gint rank)
198 {
199   static const int ranks[4] = {
200     GST_RANK_NONE, GST_RANK_MARGINAL, GST_RANK_SECONDARY, GST_RANK_PRIMARY
201   };
202   static const char *rank_names[4] = { "none", "marginal", "secondary",
203     "primary"
204   };
205   int i;
206   int best_i;
207
208   best_i = 0;
209   for (i = 0; i < 4; i++) {
210     if (rank == ranks[i])
211       return rank_names[i];
212     if (abs (rank - ranks[i]) < abs (rank - ranks[best_i])) {
213       best_i = i;
214     }
215   }
216
217   sprintf (s, "%s %c %d", rank_names[best_i],
218       (rank - ranks[best_i] > 0) ? '+' : '-', abs (ranks[best_i] - rank));
219
220   return s;
221 }
222
223 static void
224 print_factory_details_info (GstElementFactory * factory)
225 {
226   gchar **keys, **k;
227   GstRank rank;
228   char s[20];
229
230   rank = gst_plugin_feature_get_rank (GST_PLUGIN_FEATURE (factory));
231   n_print ("%sFactory Details:%s\n", HEADING_COLOR, RESET_COLOR);
232
233   push_indent ();
234   n_print ("%s%-25s%s%s (%d)%s\n", PROP_NAME_COLOR, "Rank", PROP_VALUE_COLOR,
235       get_rank_name (s, rank), rank, RESET_COLOR);
236
237   keys = gst_element_factory_get_metadata_keys (factory);
238   if (keys != NULL) {
239     for (k = keys; *k != NULL; ++k) {
240       const gchar *val;
241       gchar *key = *k;
242
243       val = gst_element_factory_get_metadata (factory, key);
244       key[0] = g_ascii_toupper (key[0]);
245       n_print ("%s%-25s%s%s%s\n", PROP_NAME_COLOR, key, PROP_VALUE_COLOR, val,
246           RESET_COLOR);
247     }
248     g_strfreev (keys);
249   }
250   pop_indent ();
251   n_print ("\n");
252 }
253
254 static void
255 print_hierarchy (GType type, gint level, gint * maxlevel)
256 {
257   GType parent;
258   gint i;
259
260   parent = g_type_parent (type);
261
262   *maxlevel = *maxlevel + 1;
263   level++;
264
265   if (parent)
266     print_hierarchy (parent, level, maxlevel);
267
268   if (_name)
269     g_print ("%s%s%s", DATATYPE_COLOR, _name, RESET_COLOR);
270
271   for (i = 1; i < *maxlevel - level; i++)
272     g_print ("      ");
273   if (*maxlevel - level)
274     g_print (" %s+----%s", CHILD_LINK_COLOR, RESET_COLOR);
275
276   g_print ("%s%s%s\n", DATATYPE_COLOR, g_type_name (type), RESET_COLOR);
277
278   if (level == 1)
279     n_print ("\n");
280 }
281
282 static void
283 print_interfaces (GType type)
284 {
285   guint n_ifaces;
286   GType *iface, *ifaces = g_type_interfaces (type, &n_ifaces);
287
288   if (ifaces) {
289     if (n_ifaces) {
290       n_print (_("%sImplemented Interfaces%s:\n"), HEADING_COLOR, RESET_COLOR);
291       push_indent ();
292       iface = ifaces;
293       while (*iface) {
294         n_print ("%s%s%s\n", DATATYPE_COLOR, g_type_name (*iface), RESET_COLOR);
295         iface++;
296       }
297       pop_indent ();
298       n_print ("\n");
299     }
300     g_free (ifaces);
301   }
302 }
303
304 static gchar *
305 flags_to_string (GFlagsValue * vals, guint flags)
306 {
307   GString *s = NULL;
308   guint flags_left, i;
309
310   /* first look for an exact match and count the number of values */
311   for (i = 0; vals[i].value_name != NULL; ++i) {
312     if (vals[i].value == flags)
313       return g_strdup (vals[i].value_nick);
314   }
315
316   s = g_string_new (NULL);
317
318   /* we assume the values are sorted from lowest to highest value */
319   flags_left = flags;
320   while (i > 0) {
321     --i;
322     if (vals[i].value != 0 && (flags_left & vals[i].value) == vals[i].value) {
323       if (s->len > 0)
324         g_string_append_c (s, '+');
325       g_string_append (s, vals[i].value_nick);
326       flags_left -= vals[i].value;
327       if (flags_left == 0)
328         break;
329     }
330   }
331
332   if (s->len == 0)
333     g_string_assign (s, "(none)");
334
335   return g_string_free (s, FALSE);
336 }
337
338 #define KNOWN_PARAM_FLAGS \
339   (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY | \
340   G_PARAM_LAX_VALIDATION |  G_PARAM_STATIC_STRINGS | \
341   G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_DEPRECATED | \
342   GST_PARAM_CONTROLLABLE | GST_PARAM_MUTABLE_PLAYING | \
343   GST_PARAM_MUTABLE_PAUSED | GST_PARAM_MUTABLE_READY)
344
345 static int
346 sort_gparamspecs (GParamSpec ** a, GParamSpec ** b)
347 {
348   return g_strcmp0 (g_param_spec_get_name (*a), g_param_spec_get_name (*b));
349 }
350
351 /* obj will be NULL if we're printing properties of pad template pads */
352 static void
353 print_object_properties_info (GObject * obj, GObjectClass * obj_class,
354     const gchar * desc)
355 {
356   GParamSpec **property_specs;
357   guint num_properties, i;
358   gboolean readable;
359   gboolean first_flag;
360
361   property_specs = g_object_class_list_properties (obj_class, &num_properties);
362   g_qsort_with_data (property_specs, num_properties, sizeof (gpointer),
363       (GCompareDataFunc) sort_gparamspecs, NULL);
364
365   n_print ("%s%s%s:\n", HEADING_COLOR, desc, RESET_COLOR);
366
367   push_indent ();
368
369   for (i = 0; i < num_properties; i++) {
370     GValue value = { 0, };
371     GParamSpec *param = property_specs[i];
372     GType owner_type = param->owner_type;
373
374     /* We're printing pad properties */
375     if (obj == NULL && (owner_type == G_TYPE_OBJECT
376             || owner_type == GST_TYPE_OBJECT || owner_type == GST_TYPE_PAD))
377       continue;
378
379     g_value_init (&value, param->value_type);
380
381     n_print ("%s%-20s%s: %s%s%s\n", PROP_NAME_COLOR,
382         g_param_spec_get_name (param), RESET_COLOR, PROP_VALUE_COLOR,
383         g_param_spec_get_blurb (param), RESET_COLOR);
384
385     push_indent_n (11);
386
387     first_flag = TRUE;
388     n_print ("%sflags%s: ", PROP_ATTR_NAME_COLOR, RESET_COLOR);
389     readable = ! !(param->flags & G_PARAM_READABLE);
390     if (readable && obj != NULL) {
391       g_object_get_property (obj, param->name, &value);
392     } else {
393       /* if we can't read the property value, assume it's set to the default
394        * (which might not be entirely true for sub-classes, but that's an
395        * unlikely corner-case anyway) */
396       g_param_value_set_default (param, &value);
397     }
398     if (readable) {
399       g_print ("%s%s%s%s", (first_flag) ? "" : ", ", PROP_ATTR_VALUE_COLOR,
400           _("readable"), RESET_COLOR);
401       first_flag = FALSE;
402     }
403     if (param->flags & G_PARAM_WRITABLE) {
404       g_print ("%s%s%s%s", (first_flag) ? "" : ", ", PROP_ATTR_VALUE_COLOR,
405           _("writable"), RESET_COLOR);
406       first_flag = FALSE;
407     }
408     if (param->flags & G_PARAM_DEPRECATED) {
409       g_print ("%s%s%s%s", (first_flag) ? "" : ", ", PROP_ATTR_VALUE_COLOR,
410           _("deprecated"), RESET_COLOR);
411       first_flag = FALSE;
412     }
413     if (param->flags & GST_PARAM_CONTROLLABLE) {
414       g_print (", %s%s%s", PROP_ATTR_VALUE_COLOR, _("controllable"),
415           RESET_COLOR);
416       first_flag = FALSE;
417     }
418     if (param->flags & GST_PARAM_MUTABLE_PLAYING) {
419       g_print (", %s%s%s", PROP_ATTR_VALUE_COLOR,
420           _("changeable in NULL, READY, PAUSED or PLAYING state"), RESET_COLOR);
421     } else if (param->flags & GST_PARAM_MUTABLE_PAUSED) {
422       g_print (", %s%s%s", PROP_ATTR_VALUE_COLOR,
423           _("changeable only in NULL, READY or PAUSED state"), RESET_COLOR);
424     } else if (param->flags & GST_PARAM_MUTABLE_READY) {
425       g_print (", %s%s%s", PROP_ATTR_VALUE_COLOR,
426           _("changeable only in NULL or READY state"), RESET_COLOR);
427     }
428     if (param->flags & ~KNOWN_PARAM_FLAGS) {
429       g_print ("%s0x%s%0x%s", (first_flag) ? "" : ", ", PROP_ATTR_VALUE_COLOR,
430           param->flags & ~KNOWN_PARAM_FLAGS, RESET_COLOR);
431     }
432     g_print ("\n");
433
434     switch (G_VALUE_TYPE (&value)) {
435       case G_TYPE_STRING:
436       {
437         const char *string_val = g_value_get_string (&value);
438
439         n_print ("%sString%s. ", DATATYPE_COLOR, RESET_COLOR);
440
441         if (string_val == NULL)
442           g_print ("%sDefault%s: %snull%s", PROP_ATTR_NAME_COLOR, RESET_COLOR,
443               PROP_ATTR_VALUE_COLOR, RESET_COLOR);
444         else
445           g_print ("%sDefault%s: %s\"%s\"%s", PROP_ATTR_NAME_COLOR, RESET_COLOR,
446               PROP_ATTR_VALUE_COLOR, string_val, RESET_COLOR);
447         break;
448       }
449       case G_TYPE_BOOLEAN:
450       {
451         gboolean bool_val = g_value_get_boolean (&value);
452
453         n_print ("%sBoolean%s. %sDefault%s: %s%s%s", DATATYPE_COLOR,
454             RESET_COLOR, PROP_ATTR_NAME_COLOR, RESET_COLOR,
455             PROP_ATTR_VALUE_COLOR, bool_val ? "true" : "false", RESET_COLOR);
456         break;
457       }
458       case G_TYPE_ULONG:
459       {
460         GParamSpecULong *pulong = G_PARAM_SPEC_ULONG (param);
461
462         n_print
463             ("%sUnsigned Long%s. %sRange%s: %s%lu - %lu%s %sDefault%s: %s%lu%s ",
464             DATATYPE_COLOR, RESET_COLOR, PROP_ATTR_NAME_COLOR, RESET_COLOR,
465             PROP_ATTR_VALUE_COLOR, pulong->minimum, pulong->maximum,
466             RESET_COLOR, PROP_ATTR_NAME_COLOR, RESET_COLOR,
467             PROP_ATTR_VALUE_COLOR, g_value_get_ulong (&value), RESET_COLOR);
468
469         GST_ERROR ("%s: property '%s' of type ulong: consider changing to "
470             "uint/uint64", G_OBJECT_CLASS_NAME (obj_class),
471             g_param_spec_get_name (param));
472         break;
473       }
474       case G_TYPE_LONG:
475       {
476         GParamSpecLong *plong = G_PARAM_SPEC_LONG (param);
477
478         n_print ("%sLong%s. %sRange%s: %s%ld - %ld%s %sDefault%s: %s%ld%s ",
479             DATATYPE_COLOR, RESET_COLOR, PROP_ATTR_NAME_COLOR, RESET_COLOR,
480             PROP_ATTR_VALUE_COLOR, plong->minimum, plong->maximum, RESET_COLOR,
481             PROP_ATTR_NAME_COLOR, RESET_COLOR, PROP_ATTR_VALUE_COLOR,
482             g_value_get_long (&value), RESET_COLOR);
483
484         GST_ERROR ("%s: property '%s' of type long: consider changing to "
485             "int/int64", G_OBJECT_CLASS_NAME (obj_class),
486             g_param_spec_get_name (param));
487         break;
488       }
489       case G_TYPE_UINT:
490       {
491         GParamSpecUInt *puint = G_PARAM_SPEC_UINT (param);
492
493         n_print
494             ("%sUnsigned Integer%s. %sRange%s: %s%u - %u%s %sDefault%s: %s%u%s ",
495             DATATYPE_COLOR, RESET_COLOR, PROP_ATTR_NAME_COLOR, RESET_COLOR,
496             PROP_ATTR_VALUE_COLOR, puint->minimum, puint->maximum, RESET_COLOR,
497             PROP_ATTR_NAME_COLOR, RESET_COLOR, PROP_ATTR_VALUE_COLOR,
498             g_value_get_uint (&value), RESET_COLOR);
499         break;
500       }
501       case G_TYPE_INT:
502       {
503         GParamSpecInt *pint = G_PARAM_SPEC_INT (param);
504
505         n_print ("%sInteger%s. %sRange%s: %s%d - %d%s %sDefault%s: %s%d%s ",
506             DATATYPE_COLOR, RESET_COLOR, PROP_ATTR_NAME_COLOR, RESET_COLOR,
507             PROP_ATTR_VALUE_COLOR, pint->minimum, pint->maximum, RESET_COLOR,
508             PROP_ATTR_NAME_COLOR, RESET_COLOR, PROP_ATTR_VALUE_COLOR,
509             g_value_get_int (&value), RESET_COLOR);
510         break;
511       }
512       case G_TYPE_UINT64:
513       {
514         GParamSpecUInt64 *puint64 = G_PARAM_SPEC_UINT64 (param);
515
516         n_print ("%sUnsigned Integer64%s. %sRange%s: %s%" G_GUINT64_FORMAT " - "
517             "%" G_GUINT64_FORMAT "%s %sDefault%s: %s%" G_GUINT64_FORMAT "%s ",
518             DATATYPE_COLOR, RESET_COLOR, PROP_ATTR_NAME_COLOR, RESET_COLOR,
519             PROP_ATTR_VALUE_COLOR, puint64->minimum, puint64->maximum,
520             RESET_COLOR, PROP_ATTR_NAME_COLOR, RESET_COLOR,
521             PROP_ATTR_VALUE_COLOR, g_value_get_uint64 (&value), RESET_COLOR);
522         break;
523       }
524       case G_TYPE_INT64:
525       {
526         GParamSpecInt64 *pint64 = G_PARAM_SPEC_INT64 (param);
527
528         n_print ("%sInteger64%s. %sRange%s: %s%" G_GINT64_FORMAT " - %"
529             G_GINT64_FORMAT "%s %sDefault%s: %s%" G_GINT64_FORMAT "%s ",
530             DATATYPE_COLOR, RESET_COLOR, PROP_ATTR_NAME_COLOR, RESET_COLOR,
531             PROP_ATTR_VALUE_COLOR, pint64->minimum, pint64->maximum,
532             RESET_COLOR, PROP_ATTR_NAME_COLOR, RESET_COLOR,
533             PROP_ATTR_VALUE_COLOR, g_value_get_int64 (&value), RESET_COLOR);
534         break;
535       }
536       case G_TYPE_FLOAT:
537       {
538         GParamSpecFloat *pfloat = G_PARAM_SPEC_FLOAT (param);
539
540         n_print ("%sFloat%s. %sRange%s: %s%15.7g - %15.7g%s "
541             "%sDefault%s: %s%15.7g%s ", DATATYPE_COLOR, RESET_COLOR,
542             PROP_ATTR_NAME_COLOR, RESET_COLOR, PROP_ATTR_VALUE_COLOR,
543             pfloat->minimum, pfloat->maximum, RESET_COLOR, PROP_ATTR_NAME_COLOR,
544             RESET_COLOR, PROP_ATTR_VALUE_COLOR, g_value_get_float (&value),
545             RESET_COLOR);
546         break;
547       }
548       case G_TYPE_DOUBLE:
549       {
550         GParamSpecDouble *pdouble = G_PARAM_SPEC_DOUBLE (param);
551
552         n_print ("%sDouble%s. %sRange%s: %s%15.7g - %15.7g%s "
553             "%sDefault%s: %s%15.7g%s ", DATATYPE_COLOR, RESET_COLOR,
554             PROP_ATTR_NAME_COLOR, RESET_COLOR, PROP_ATTR_VALUE_COLOR,
555             pdouble->minimum, pdouble->maximum, RESET_COLOR,
556             PROP_ATTR_NAME_COLOR, RESET_COLOR, PROP_ATTR_VALUE_COLOR,
557             g_value_get_double (&value), RESET_COLOR);
558         break;
559       }
560       case G_TYPE_CHAR:
561       case G_TYPE_UCHAR:
562         GST_ERROR ("%s: property '%s' of type char: consider changing to "
563             "int/string", G_OBJECT_CLASS_NAME (obj_class),
564             g_param_spec_get_name (param));
565         /* fall through */
566       default:
567         if (param->value_type == GST_TYPE_CAPS) {
568           const GstCaps *caps = gst_value_get_caps (&value);
569
570           if (!caps)
571             n_print ("%sCaps%s (NULL)", DATATYPE_COLOR, RESET_COLOR);
572           else {
573             print_caps (caps, "                           ");
574           }
575         } else if (G_IS_PARAM_SPEC_ENUM (param)) {
576           GEnumValue *values;
577           guint j = 0;
578           gint enum_value;
579           const gchar *value_nick = "";
580
581           values = G_ENUM_CLASS (g_type_class_ref (param->value_type))->values;
582           enum_value = g_value_get_enum (&value);
583
584           while (values[j].value_name) {
585             if (values[j].value == enum_value)
586               value_nick = values[j].value_nick;
587             j++;
588           }
589
590           n_print ("%sEnum \"%s\"%s %sDefault%s: %s%d, \"%s\"%s",
591               DATATYPE_COLOR, g_type_name (G_VALUE_TYPE (&value)), RESET_COLOR,
592               PROP_ATTR_NAME_COLOR, RESET_COLOR, PROP_ATTR_VALUE_COLOR,
593               enum_value, value_nick, RESET_COLOR);
594
595           j = 0;
596           while (values[j].value_name) {
597             g_print ("\n");
598             n_print ("   %s(%d)%s: %s%-16s%s - %s%s%s",
599                 PROP_ATTR_NAME_COLOR, values[j].value, RESET_COLOR,
600                 PROP_ATTR_VALUE_COLOR, values[j].value_nick, RESET_COLOR,
601                 DESC_COLOR, values[j].value_name, RESET_COLOR);
602             j++;
603           }
604           /* g_type_class_unref (ec); */
605         } else if (G_IS_PARAM_SPEC_FLAGS (param)) {
606           GParamSpecFlags *pflags = G_PARAM_SPEC_FLAGS (param);
607           GFlagsValue *vals;
608           gchar *cur;
609
610           vals = pflags->flags_class->values;
611
612           cur = flags_to_string (vals, g_value_get_flags (&value));
613
614           n_print ("%sFlags \"%s\"%s %sDefault%s: %s0x%08x, \"%s\"%s",
615               DATATYPE_COLOR, g_type_name (G_VALUE_TYPE (&value)), RESET_COLOR,
616               PROP_ATTR_NAME_COLOR, RESET_COLOR, PROP_ATTR_VALUE_COLOR,
617               g_value_get_flags (&value), cur, RESET_COLOR);
618
619           while (vals[0].value_name) {
620             g_print ("\n");
621             n_print ("   %s(0x%08x)%s: %s%-16s%s - %s%s%s",
622                 PROP_ATTR_NAME_COLOR, vals[0].value, RESET_COLOR,
623                 PROP_ATTR_VALUE_COLOR, vals[0].value_nick, RESET_COLOR,
624                 DESC_COLOR, vals[0].value_name, RESET_COLOR);
625             ++vals;
626           }
627
628           g_free (cur);
629         } else if (G_IS_PARAM_SPEC_OBJECT (param)) {
630           n_print ("%sObject of type%s %s\"%s\"%s", PROP_VALUE_COLOR,
631               RESET_COLOR, DATATYPE_COLOR,
632               g_type_name (param->value_type), RESET_COLOR);
633         } else if (G_IS_PARAM_SPEC_BOXED (param)) {
634           n_print ("%sBoxed pointer of type%s %s\"%s\"%s", PROP_VALUE_COLOR,
635               RESET_COLOR, DATATYPE_COLOR,
636               g_type_name (param->value_type), RESET_COLOR);
637           if (param->value_type == GST_TYPE_STRUCTURE) {
638             const GstStructure *s = gst_value_get_structure (&value);
639             if (s)
640               gst_structure_foreach (s, print_field,
641                   (gpointer) "                           ");
642           }
643         } else if (G_IS_PARAM_SPEC_POINTER (param)) {
644           if (param->value_type != G_TYPE_POINTER) {
645             n_print ("%sPointer of type%s %s\"%s\"%s.", PROP_VALUE_COLOR,
646                 RESET_COLOR, DATATYPE_COLOR, g_type_name (param->value_type),
647                 RESET_COLOR);
648           } else {
649             n_print ("%sPointer.%s", PROP_VALUE_COLOR, RESET_COLOR);
650           }
651         } else if (param->value_type == G_TYPE_VALUE_ARRAY) {
652           GParamSpecValueArray *pvarray = G_PARAM_SPEC_VALUE_ARRAY (param);
653
654           if (pvarray->element_spec) {
655             n_print ("%sArray of GValues of type%s %s\"%s\"%s",
656                 PROP_VALUE_COLOR, RESET_COLOR, DATATYPE_COLOR,
657                 g_type_name (pvarray->element_spec->value_type), RESET_COLOR);
658           } else {
659             n_print ("%sArray of GValues%s", PROP_VALUE_COLOR, RESET_COLOR);
660           }
661         } else if (GST_IS_PARAM_SPEC_FRACTION (param)) {
662           GstParamSpecFraction *pfraction = GST_PARAM_SPEC_FRACTION (param);
663
664           n_print ("%sFraction%s. %sRange%s: %s%d/%d - %d/%d%s "
665               "%sDefault%s: %s%d/%d%s ", DATATYPE_COLOR, RESET_COLOR,
666               PROP_ATTR_NAME_COLOR, RESET_COLOR, PROP_ATTR_VALUE_COLOR,
667               pfraction->min_num, pfraction->min_den, pfraction->max_num,
668               pfraction->max_den, RESET_COLOR, PROP_ATTR_NAME_COLOR,
669               RESET_COLOR, PROP_ATTR_VALUE_COLOR,
670               gst_value_get_fraction_numerator (&value),
671               gst_value_get_fraction_denominator (&value), RESET_COLOR);
672         } else if (param->value_type == GST_TYPE_ARRAY) {
673           GstParamSpecArray *parray = GST_PARAM_SPEC_ARRAY_LIST (param);
674
675           if (parray->element_spec) {
676             n_print ("%sGstValueArray of GValues of type%s %s\"%s\"%s",
677                 PROP_VALUE_COLOR, RESET_COLOR, DATATYPE_COLOR,
678                 g_type_name (parray->element_spec->value_type), RESET_COLOR);
679           } else {
680             n_print ("%sGstValueArray of GValues%s", PROP_VALUE_COLOR,
681                 RESET_COLOR);
682           }
683         } else {
684           n_print ("%sUnknown type %ld%s %s\"%s\"%s", PROP_VALUE_COLOR,
685               (glong) param->value_type, RESET_COLOR, DATATYPE_COLOR,
686               g_type_name (param->value_type), RESET_COLOR);
687         }
688         break;
689     }
690     if (!readable)
691       g_print (" %sWrite only%s\n", PROP_VALUE_COLOR, RESET_COLOR);
692     else
693       g_print ("\n");
694
695     pop_indent_n (11);
696
697     g_value_reset (&value);
698   }
699   if (num_properties == 0)
700     n_print ("%snone%s\n", PROP_VALUE_COLOR, RESET_COLOR);
701
702   pop_indent ();
703
704   g_free (property_specs);
705 }
706
707 static void
708 print_element_properties_info (GstElement * element)
709 {
710   g_print ("\n");
711   print_object_properties_info (G_OBJECT (element),
712       G_OBJECT_GET_CLASS (element), "Element Properties");
713 }
714
715 static void
716 print_pad_templates_info (GstElement * element, GstElementFactory * factory)
717 {
718   const GList *pads;
719   GstStaticPadTemplate *padtemplate;
720   GstPadTemplate *tmpl;
721
722   n_print ("%sPad Templates%s:\n", HEADING_COLOR, RESET_COLOR);
723
724   push_indent ();
725
726   if (gst_element_factory_get_num_pad_templates (factory) == 0) {
727     n_print ("%snone%s\n", PROP_VALUE_COLOR, RESET_COLOR);
728     goto done;
729   }
730
731   pads = gst_element_factory_get_static_pad_templates (factory);
732   while (pads) {
733     padtemplate = (GstStaticPadTemplate *) (pads->data);
734     pads = g_list_next (pads);
735
736     if (padtemplate->direction == GST_PAD_SRC)
737       n_print ("%sSRC template%s: %s'%s'%s\n", PROP_NAME_COLOR, RESET_COLOR,
738           PROP_VALUE_COLOR, padtemplate->name_template, RESET_COLOR);
739     else if (padtemplate->direction == GST_PAD_SINK)
740       n_print ("%sSINK template%s: %s'%s'%s\n", PROP_NAME_COLOR, RESET_COLOR,
741           PROP_VALUE_COLOR, padtemplate->name_template, RESET_COLOR);
742     else
743       n_print ("%sUNKNOWN template%s: %s'%s'%s\n", PROP_NAME_COLOR, RESET_COLOR,
744           PROP_VALUE_COLOR, padtemplate->name_template, RESET_COLOR);
745
746     push_indent ();
747
748     if (padtemplate->presence == GST_PAD_ALWAYS)
749       n_print ("%sAvailability%s: %sAlways%s\n", PROP_NAME_COLOR, RESET_COLOR,
750           PROP_VALUE_COLOR, RESET_COLOR);
751     else if (padtemplate->presence == GST_PAD_SOMETIMES)
752       n_print ("%sAvailability%s: %sSometimes%s\n", PROP_NAME_COLOR,
753           RESET_COLOR, PROP_VALUE_COLOR, RESET_COLOR);
754     else if (padtemplate->presence == GST_PAD_REQUEST) {
755       n_print ("%sAvailability%s: %sOn request%s\n", PROP_NAME_COLOR,
756           RESET_COLOR, PROP_VALUE_COLOR, RESET_COLOR);
757     } else
758       n_print ("%sAvailability%s: %sUNKNOWN%s\n", PROP_NAME_COLOR, RESET_COLOR,
759           PROP_VALUE_COLOR, RESET_COLOR);
760
761     if (padtemplate->static_caps.string) {
762       GstCaps *caps = gst_static_caps_get (&padtemplate->static_caps);
763
764       n_print ("%sCapabilities%s:\n", PROP_NAME_COLOR, RESET_COLOR);
765
766       push_indent ();
767       print_caps (caps, "");    // FIXME
768       pop_indent ();
769
770       gst_caps_unref (caps);
771     }
772
773     tmpl = gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (element),
774         padtemplate->name_template);
775     if (tmpl != NULL) {
776       GType pad_type = GST_PAD_TEMPLATE_GTYPE (tmpl);
777
778       if (pad_type != G_TYPE_NONE && pad_type != GST_TYPE_PAD) {
779         gpointer pad_klass;
780
781         pad_klass = g_type_class_ref (pad_type);
782         n_print ("%sType%s: %s%s%s\n", PROP_NAME_COLOR, RESET_COLOR,
783             DATATYPE_COLOR, g_type_name (pad_type), RESET_COLOR);
784         print_object_properties_info (NULL, pad_klass, "Pad Properties");
785         g_type_class_unref (pad_klass);
786       }
787     }
788
789     pop_indent ();
790
791     if (pads != NULL)
792       n_print ("\n");
793   }
794
795 done:
796   pop_indent ();
797 }
798
799 static void
800 print_clocking_info (GstElement * element)
801 {
802   gboolean requires_clock, provides_clock;
803
804   requires_clock =
805       GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
806   provides_clock =
807       GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
808
809   if (!requires_clock && !provides_clock) {
810     n_print ("\n");
811     n_print ("%sElement has no clocking capabilities.%s\n", DESC_COLOR,
812         RESET_COLOR);
813     return;
814   }
815
816   n_print ("\n");
817   n_print ("%sClocking Interaction%s:\n", PROP_NAME_COLOR, RESET_COLOR);
818
819   push_indent ();
820
821   if (requires_clock) {
822     n_print ("%selement requires a clock%s\n", PROP_VALUE_COLOR, RESET_COLOR);
823   }
824
825   if (provides_clock) {
826     GstClock *clock;
827
828     clock = gst_element_get_clock (element);
829     if (clock) {
830       n_print ("%selement provides a clock%s: %s%s%s\n", PROP_VALUE_COLOR,
831           RESET_COLOR, DATATYPE_COLOR, GST_OBJECT_NAME (clock), RESET_COLOR);
832       gst_object_unref (clock);
833     } else
834       n_print ("%selement is supposed to provide a clock but returned NULL%s\n",
835           PROP_VALUE_COLOR, RESET_COLOR);
836   }
837
838   pop_indent ();
839 }
840
841 static void
842 print_uri_handler_info (GstElement * element)
843 {
844   if (GST_IS_URI_HANDLER (element)) {
845     const gchar *const *uri_protocols;
846     const gchar *uri_type;
847
848     if (gst_uri_handler_get_uri_type (GST_URI_HANDLER (element)) == GST_URI_SRC)
849       uri_type = "source";
850     else if (gst_uri_handler_get_uri_type (GST_URI_HANDLER (element)) ==
851         GST_URI_SINK)
852       uri_type = "sink";
853     else
854       uri_type = "unknown";
855
856     uri_protocols = gst_uri_handler_get_protocols (GST_URI_HANDLER (element));
857
858     n_print ("\n");
859     n_print ("%sURI handling capabilities:%s\n", HEADING_COLOR, RESET_COLOR);
860
861     push_indent ();
862
863     n_print ("%sElement can act as %s.%s\n", DESC_COLOR, uri_type, RESET_COLOR);
864
865     if (uri_protocols && *uri_protocols) {
866       n_print ("%sSupported URI protocols%s:\n", DESC_COLOR, RESET_COLOR);
867       push_indent ();
868       for (; *uri_protocols != NULL; uri_protocols++)
869         n_print ("%s%s%s\n", PROP_ATTR_VALUE_COLOR, *uri_protocols,
870             RESET_COLOR);
871       pop_indent ();
872     } else {
873       n_print ("%sNo supported URI protocols%s\n", PROP_VALUE_COLOR,
874           RESET_COLOR);
875     }
876
877     pop_indent ();
878   } else {
879     n_print ("%sElement has no URI handling capabilities.%s\n", DESC_COLOR,
880         RESET_COLOR);
881   }
882 }
883
884 static void
885 print_pad_info (GstElement * element)
886 {
887   const GList *pads;
888   GstPad *pad;
889
890   n_print ("\n");
891   n_print ("%sPads:%s\n", HEADING_COLOR, RESET_COLOR);
892
893   push_indent ();
894
895   if (!element->numpads) {
896     n_print ("%snone%s\n", PROP_VALUE_COLOR, RESET_COLOR);
897     goto done;
898   }
899
900   pads = element->pads;
901   while (pads) {
902     gchar *name;
903     GstCaps *caps;
904
905     pad = GST_PAD (pads->data);
906     pads = g_list_next (pads);
907
908     name = gst_pad_get_name (pad);
909     if (gst_pad_get_direction (pad) == GST_PAD_SRC)
910       n_print ("%sSRC%s: %s'%s'%s\n", PROP_NAME_COLOR, RESET_COLOR,
911           PROP_VALUE_COLOR, name, RESET_COLOR);
912     else if (gst_pad_get_direction (pad) == GST_PAD_SINK)
913       n_print ("%sSINK%s: %s'%s'%s\n", PROP_NAME_COLOR, RESET_COLOR,
914           PROP_VALUE_COLOR, name, RESET_COLOR);
915     else
916       n_print ("%sUNKNOWN%s: %s'%s'%s\n", PROP_NAME_COLOR, RESET_COLOR,
917           PROP_VALUE_COLOR, name, RESET_COLOR);
918
919     g_free (name);
920
921     if (pad->padtemplate) {
922       push_indent ();
923       n_print ("%sPad Template%s: %s'%s'%s\n", PROP_NAME_COLOR, RESET_COLOR,
924           PROP_VALUE_COLOR, pad->padtemplate->name_template, RESET_COLOR);
925       pop_indent ();
926     }
927
928     caps = gst_pad_get_current_caps (pad);
929     if (caps) {
930       n_print ("%sCapabilities:%s\n", PROP_NAME_COLOR, RESET_COLOR);
931       push_indent ();
932       print_caps (caps, "");    // FIXME
933       pop_indent ();
934       gst_caps_unref (caps);
935     }
936   }
937
938 done:
939   pop_indent ();
940 }
941
942 static gboolean
943 has_sometimes_template (GstElement * element)
944 {
945   GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
946   GList *l;
947
948   for (l = klass->padtemplates; l != NULL; l = l->next) {
949     if (GST_PAD_TEMPLATE (l->data)->presence == GST_PAD_SOMETIMES)
950       return TRUE;
951   }
952
953   return FALSE;
954 }
955
956 static gboolean
957 gtype_needs_ptr_marker (GType type)
958 {
959   if (type == G_TYPE_POINTER)
960     return FALSE;
961
962   if (G_TYPE_FUNDAMENTAL (type) == G_TYPE_POINTER || G_TYPE_IS_BOXED (type)
963       || G_TYPE_IS_OBJECT (type))
964     return TRUE;
965
966   return FALSE;
967 }
968
969 static void
970 print_signal_info (GstElement * element)
971 {
972   /* Signals/Actions Block */
973   guint *signals;
974   guint nsignals;
975   gint i = 0, j, k;
976   GSignalQuery *query = NULL;
977   GType type;
978   GSList *found_signals, *l;
979
980   for (k = 0; k < 2; k++) {
981     found_signals = NULL;
982
983     /* For elements that have sometimes pads, also list a few useful GstElement
984      * signals. Put these first, so element-specific ones come later. */
985     if (k == 0 && has_sometimes_template (element)) {
986       query = g_new0 (GSignalQuery, 1);
987       g_signal_query (g_signal_lookup ("pad-added", GST_TYPE_ELEMENT), query);
988       found_signals = g_slist_append (found_signals, query);
989       query = g_new0 (GSignalQuery, 1);
990       g_signal_query (g_signal_lookup ("pad-removed", GST_TYPE_ELEMENT), query);
991       found_signals = g_slist_append (found_signals, query);
992       query = g_new0 (GSignalQuery, 1);
993       g_signal_query (g_signal_lookup ("no-more-pads", GST_TYPE_ELEMENT),
994           query);
995       found_signals = g_slist_append (found_signals, query);
996     }
997
998     for (type = G_OBJECT_TYPE (element); type; type = g_type_parent (type)) {
999       if (type == GST_TYPE_ELEMENT || type == GST_TYPE_OBJECT)
1000         break;
1001
1002       if (type == GST_TYPE_BIN && G_OBJECT_TYPE (element) != GST_TYPE_BIN)
1003         continue;
1004
1005       signals = g_signal_list_ids (type, &nsignals);
1006       for (i = 0; i < nsignals; i++) {
1007         query = g_new0 (GSignalQuery, 1);
1008         g_signal_query (signals[i], query);
1009
1010         if ((k == 0 && !(query->signal_flags & G_SIGNAL_ACTION)) ||
1011             (k == 1 && (query->signal_flags & G_SIGNAL_ACTION)))
1012           found_signals = g_slist_append (found_signals, query);
1013         else
1014           g_free (query);
1015       }
1016       g_free (signals);
1017       signals = NULL;
1018     }
1019
1020     if (found_signals) {
1021       n_print ("\n");
1022       if (k == 0)
1023         n_print ("%sElement Signals%s:\n", HEADING_COLOR, RESET_COLOR);
1024       else
1025         n_print ("%sElement Actions%s:\n", HEADING_COLOR, RESET_COLOR);
1026     } else {
1027       continue;
1028     }
1029
1030     for (l = found_signals; l; l = l->next) {
1031       gchar *indent;
1032       const gchar *pmark;
1033       int indent_len;
1034
1035       query = (GSignalQuery *) l->data;
1036       indent_len = strlen (query->signal_name) +
1037           strlen (g_type_name (query->return_type)) + 24;
1038
1039       if (gtype_needs_ptr_marker (query->return_type)) {
1040         pmark = "* ";
1041         indent_len += 2;
1042       } else {
1043         pmark = " ";
1044       }
1045
1046       indent = g_new0 (gchar, indent_len + 1);
1047       memset (indent, ' ', indent_len);
1048
1049       n_print ("  %s\"%s\"%s :  %s%s%s%suser_function%s (%s%s%s* object%s",
1050           PROP_NAME_COLOR, query->signal_name, RESET_COLOR,
1051           DATATYPE_COLOR, g_type_name (query->return_type), PROP_VALUE_COLOR,
1052           pmark, RESET_COLOR, DATATYPE_COLOR, g_type_name (type),
1053           PROP_VALUE_COLOR, RESET_COLOR);
1054
1055       for (j = 0; j < query->n_params; j++) {
1056         const gchar *type_name, *asterisk;
1057
1058         type_name = g_type_name (query->param_types[j]);
1059         asterisk = gtype_needs_ptr_marker (query->param_types[j]) ? "*" : "";
1060
1061         g_print (",\n");
1062         n_print ("%s%s%s%s%s arg%d%s", indent, DATATYPE_COLOR, type_name,
1063             PROP_VALUE_COLOR, asterisk, j, RESET_COLOR);
1064       }
1065
1066       if (k == 0) {
1067         g_print (",\n");
1068         n_print ("%s%sgpointer %suser_data%s);\n", indent, DATATYPE_COLOR,
1069             PROP_VALUE_COLOR, RESET_COLOR);
1070       } else
1071         g_print (");\n");
1072
1073       g_free (indent);
1074     }
1075
1076     if (found_signals) {
1077       g_slist_foreach (found_signals, (GFunc) g_free, NULL);
1078       g_slist_free (found_signals);
1079     }
1080   }
1081 }
1082
1083 static void
1084 print_children_info (GstElement * element)
1085 {
1086   GList *children;
1087
1088   if (!GST_IS_BIN (element))
1089     return;
1090
1091   children = (GList *) GST_BIN (element)->children;
1092   if (children) {
1093     n_print ("\n");
1094     n_print ("%sChildren%s:\n", HEADING_COLOR, RESET_COLOR);
1095   }
1096
1097   while (children) {
1098     n_print ("  %s%s%s\n", DATATYPE_COLOR,
1099         GST_ELEMENT_NAME (GST_ELEMENT (children->data)), RESET_COLOR);
1100     children = g_list_next (children);
1101   }
1102 }
1103
1104 static void
1105 print_preset_list (GstElement * element)
1106 {
1107   gchar **presets, **preset;
1108
1109   if (!GST_IS_PRESET (element))
1110     return;
1111
1112   presets = gst_preset_get_preset_names (GST_PRESET (element));
1113   if (presets && *presets) {
1114     n_print ("\n");
1115     n_print ("%sPresets%s:\n", HEADING_COLOR, RESET_COLOR);
1116     for (preset = presets; *preset; preset++) {
1117       n_print ("  \"%s\"\n", *preset);
1118     }
1119     g_strfreev (presets);
1120   }
1121 }
1122
1123 static void
1124 print_blacklist (void)
1125 {
1126   GList *plugins, *cur;
1127   gint count = 0;
1128
1129   g_print ("%s%s%s\n", HEADING_COLOR, _("Blacklisted files:"), RESET_COLOR);
1130
1131   plugins = gst_registry_get_plugin_list (gst_registry_get ());
1132   for (cur = plugins; cur != NULL; cur = g_list_next (cur)) {
1133     GstPlugin *plugin = (GstPlugin *) (cur->data);
1134     if (GST_OBJECT_FLAG_IS_SET (plugin, GST_PLUGIN_FLAG_BLACKLISTED)) {
1135       g_print ("  %s\n", gst_plugin_get_name (plugin));
1136       count++;
1137     }
1138   }
1139
1140   g_print ("\n");
1141   g_print (_("%sTotal count%s: %s"), PROP_NAME_COLOR, RESET_COLOR,
1142       PROP_VALUE_COLOR);
1143   g_print (ngettext ("%d blacklisted file", "%d blacklisted files", count),
1144       count);
1145   g_print ("%s\n", RESET_COLOR);
1146   gst_plugin_list_free (plugins);
1147 }
1148
1149 static void
1150 print_typefind_extensions (const gchar * const *extensions, const gchar * color)
1151 {
1152   guint i = 0;
1153
1154   while (extensions[i]) {
1155     g_print ("%s%s%s%s", i > 0 ? ", " : "", color, extensions[i], RESET_COLOR);
1156     i++;
1157   }
1158 }
1159
1160 static void
1161 print_element_list (gboolean print_all, gchar * ftypes)
1162 {
1163   int plugincount = 0, featurecount = 0, blacklistcount = 0;
1164   GList *plugins, *orig_plugins;
1165   gchar **types = NULL;
1166
1167   if (ftypes) {
1168     gint i;
1169
1170     types = g_strsplit (ftypes, "/", -1);
1171     for (i = 0; types[i]; i++)
1172       *types[i] = g_ascii_toupper (*types[i]);
1173
1174   }
1175
1176   orig_plugins = plugins = gst_registry_get_plugin_list (gst_registry_get ());
1177   while (plugins) {
1178     GList *features, *orig_features;
1179     GstPlugin *plugin;
1180
1181     plugin = (GstPlugin *) (plugins->data);
1182     plugins = g_list_next (plugins);
1183     plugincount++;
1184
1185     if (GST_OBJECT_FLAG_IS_SET (plugin, GST_PLUGIN_FLAG_BLACKLISTED)) {
1186       blacklistcount++;
1187       continue;
1188     }
1189
1190     orig_features = features =
1191         gst_registry_get_feature_list_by_plugin (gst_registry_get (),
1192         gst_plugin_get_name (plugin));
1193     while (features) {
1194       GstPluginFeature *feature;
1195
1196       if (G_UNLIKELY (features->data == NULL))
1197         goto next;
1198       feature = GST_PLUGIN_FEATURE (features->data);
1199       featurecount++;
1200
1201       if (GST_IS_ELEMENT_FACTORY (feature)) {
1202         const gchar *klass;
1203         GstElementFactory *factory;
1204
1205         factory = GST_ELEMENT_FACTORY (feature);
1206         if (types) {
1207           gint i;
1208           gboolean all_found = TRUE;
1209
1210           klass =
1211               gst_element_factory_get_metadata (factory,
1212               GST_ELEMENT_METADATA_KLASS);
1213           for (i = 0; types[i]; i++) {
1214             if (!strstr (klass, types[i])) {
1215               all_found = FALSE;
1216               break;
1217             }
1218           }
1219
1220           if (!all_found)
1221             goto next;
1222         }
1223         if (print_all)
1224           print_element_info (feature, TRUE);
1225         else
1226           g_print ("%s%s%s:  %s%s%s: %s%s%s\n", PLUGIN_NAME_COLOR,
1227               gst_plugin_get_name (plugin), RESET_COLOR, ELEMENT_NAME_COLOR,
1228               GST_OBJECT_NAME (factory), RESET_COLOR, ELEMENT_DETAIL_COLOR,
1229               gst_element_factory_get_metadata (factory,
1230                   GST_ELEMENT_METADATA_LONGNAME), RESET_COLOR);
1231       } else if (GST_IS_TYPE_FIND_FACTORY (feature)) {
1232         GstTypeFindFactory *factory;
1233         const gchar *const *extensions;
1234
1235         if (types)
1236           goto next;
1237         factory = GST_TYPE_FIND_FACTORY (feature);
1238         if (!print_all)
1239           g_print ("%s%s%s: %s%s%s: ", PLUGIN_NAME_COLOR,
1240               gst_plugin_get_name (plugin), RESET_COLOR, ELEMENT_NAME_COLOR,
1241               gst_plugin_feature_get_name (feature), RESET_COLOR);
1242
1243         extensions = gst_type_find_factory_get_extensions (factory);
1244         if (extensions != NULL) {
1245           if (!print_all) {
1246             print_typefind_extensions (extensions, ELEMENT_DETAIL_COLOR);
1247             g_print ("\n");
1248           }
1249         } else {
1250           if (!print_all)
1251             g_print ("%sno extensions%s\n", ELEMENT_DETAIL_COLOR, RESET_COLOR);
1252         }
1253       } else {
1254         if (types)
1255           goto next;
1256         if (!print_all)
1257           n_print ("%s%s%s:  %s%s%s (%s%s%s)\n", PLUGIN_NAME_COLOR,
1258               gst_plugin_get_name (plugin), RESET_COLOR, ELEMENT_NAME_COLOR,
1259               GST_OBJECT_NAME (feature), RESET_COLOR, ELEMENT_DETAIL_COLOR,
1260               g_type_name (G_OBJECT_TYPE (feature)), RESET_COLOR);
1261       }
1262
1263     next:
1264       features = g_list_next (features);
1265     }
1266
1267     gst_plugin_feature_list_free (orig_features);
1268   }
1269
1270   gst_plugin_list_free (orig_plugins);
1271   g_strfreev (types);
1272
1273   g_print ("\n");
1274   g_print (_("%sTotal count%s: %s"), PROP_NAME_COLOR, RESET_COLOR,
1275       PROP_VALUE_COLOR);
1276   g_print (ngettext ("%d plugin", "%d plugins", plugincount), plugincount);
1277   if (blacklistcount) {
1278     g_print (" (");
1279     g_print (ngettext ("%d blacklist entry", "%d blacklist entries",
1280             blacklistcount), blacklistcount);
1281     g_print (" not shown)");
1282   }
1283   g_print ("%s, %s", RESET_COLOR, PROP_VALUE_COLOR);
1284   g_print (ngettext ("%d feature", "%d features", featurecount), featurecount);
1285   g_print ("%s\n", RESET_COLOR);
1286 }
1287
1288 static void
1289 print_all_uri_handlers (void)
1290 {
1291   GList *plugins, *p, *features, *f;
1292
1293   plugins = gst_registry_get_plugin_list (gst_registry_get ());
1294
1295   for (p = plugins; p; p = p->next) {
1296     GstPlugin *plugin = (GstPlugin *) (p->data);
1297
1298     features =
1299         gst_registry_get_feature_list_by_plugin (gst_registry_get (),
1300         gst_plugin_get_name (plugin));
1301
1302     for (f = features; f; f = f->next) {
1303       GstPluginFeature *feature = GST_PLUGIN_FEATURE (f->data);
1304
1305       if (GST_IS_ELEMENT_FACTORY (feature)) {
1306         GstElementFactory *factory;
1307         GstElement *element;
1308
1309         factory = GST_ELEMENT_FACTORY (gst_plugin_feature_load (feature));
1310         if (!factory) {
1311           g_print ("element plugin %s couldn't be loaded\n",
1312               gst_plugin_get_name (plugin));
1313           continue;
1314         }
1315
1316         element = gst_element_factory_create (factory, NULL);
1317         if (!element) {
1318           g_print ("couldn't construct element for %s for some reason\n",
1319               GST_OBJECT_NAME (factory));
1320           gst_object_unref (factory);
1321           continue;
1322         }
1323
1324         if (GST_IS_URI_HANDLER (element)) {
1325           const gchar *const *uri_protocols;
1326           const gchar *const *protocol;
1327           const gchar *dir;
1328
1329           switch (gst_uri_handler_get_uri_type (GST_URI_HANDLER (element))) {
1330             case GST_URI_SRC:
1331               dir = "read";
1332               break;
1333             case GST_URI_SINK:
1334               dir = "write";
1335               break;
1336             default:
1337               dir = "unknown";
1338               break;
1339           }
1340
1341           g_print ("%s%s%s (%s%s%s, %srank %u%s): ",
1342               FEATURE_NAME_COLOR,
1343               gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)),
1344               RESET_COLOR, FEATURE_DIR_COLOR, dir, RESET_COLOR,
1345               FEATURE_RANK_COLOR,
1346               gst_plugin_feature_get_rank (GST_PLUGIN_FEATURE (factory)),
1347               RESET_COLOR);
1348
1349           uri_protocols =
1350               gst_uri_handler_get_protocols (GST_URI_HANDLER (element));
1351           for (protocol = uri_protocols; *protocol != NULL; protocol++) {
1352             if (protocol != uri_protocols)
1353               g_print (", ");
1354             g_print ("%s%s%s", FEATURE_PROTO_COLOR, *protocol, RESET_COLOR);
1355           }
1356           g_print ("\n");
1357         }
1358
1359         gst_object_unref (element);
1360         gst_object_unref (factory);
1361       }
1362     }
1363
1364     gst_plugin_feature_list_free (features);
1365   }
1366
1367   gst_plugin_list_free (plugins);
1368 }
1369
1370 static void
1371 print_plugin_info (GstPlugin * plugin)
1372 {
1373   const gchar *release_date = gst_plugin_get_release_date_string (plugin);
1374   const gchar *filename = gst_plugin_get_filename (plugin);
1375
1376   n_print ("%sPlugin Details%s:\n", HEADING_COLOR, RESET_COLOR);
1377
1378   push_indent ();
1379
1380   n_print ("%s%-25s%s%s%s%s\n", PROP_NAME_COLOR, "Name", RESET_COLOR,
1381       PROP_VALUE_COLOR, gst_plugin_get_name (plugin), RESET_COLOR);
1382   n_print ("%s%-25s%s%s%s%s\n", PROP_NAME_COLOR, "Description", RESET_COLOR,
1383       PROP_VALUE_COLOR, gst_plugin_get_description (plugin), RESET_COLOR);
1384   n_print ("%s%-25s%s%s%s%s\n", PROP_NAME_COLOR, "Filename", RESET_COLOR,
1385       PROP_VALUE_COLOR, (filename != NULL) ? filename : "(null)", RESET_COLOR);
1386   n_print ("%s%-25s%s%s%s%s\n", PROP_NAME_COLOR, "Version", RESET_COLOR,
1387       PROP_VALUE_COLOR, gst_plugin_get_version (plugin), RESET_COLOR);
1388   n_print ("%s%-25s%s%s%s%s\n", PROP_NAME_COLOR, "License", RESET_COLOR,
1389       PROP_VALUE_COLOR, gst_plugin_get_license (plugin), RESET_COLOR);
1390   n_print ("%s%-25s%s%s%s%s\n", PROP_NAME_COLOR, "Source module", RESET_COLOR,
1391       PROP_VALUE_COLOR, gst_plugin_get_source (plugin), RESET_COLOR);
1392
1393   if (release_date != NULL) {
1394     const gchar *tz = "(UTC)";
1395     gchar *str, *sep;
1396
1397 /* may be: YYYY-MM-DD or YYYY-MM-DDTHH:MMZ */
1398 /* YYYY-MM-DDTHH:MMZ => YYYY-MM-DD HH:MM (UTC) */
1399     str = g_strdup (release_date);
1400     sep = strstr (str, "T");
1401     if (sep != NULL) {
1402       *sep = ' ';
1403       sep = strstr (sep + 1, "Z");
1404       if (sep != NULL)
1405         *sep = ' ';
1406     } else {
1407       tz = "";
1408     }
1409     n_print ("%s%-25s%s%s%s%s%s\n", PROP_NAME_COLOR, "Source release date",
1410         RESET_COLOR, PROP_VALUE_COLOR, str, tz, RESET_COLOR);
1411     g_free (str);
1412   }
1413   n_print ("%s%-25s%s%s%s%s\n", PROP_NAME_COLOR, "Binary package", RESET_COLOR,
1414       PROP_VALUE_COLOR, gst_plugin_get_package (plugin), RESET_COLOR);
1415   n_print ("%s%-25s%s%s%s%s\n", PROP_NAME_COLOR, "Origin URL", RESET_COLOR,
1416       PROP_VALUE_COLOR, gst_plugin_get_origin (plugin), RESET_COLOR);
1417
1418   pop_indent ();
1419
1420   n_print ("\n");
1421 }
1422
1423 static void
1424 print_plugin_features (GstPlugin * plugin)
1425 {
1426   GList *features, *origlist;
1427   gint num_features = 0;
1428   gint num_elements = 0;
1429   gint num_tracers = 0;
1430   gint num_typefinders = 0;
1431   gint num_devproviders = 0;
1432   gint num_other = 0;
1433
1434   origlist = features =
1435       gst_registry_get_feature_list_by_plugin (gst_registry_get (),
1436       gst_plugin_get_name (plugin));
1437
1438   while (features) {
1439     GstPluginFeature *feature;
1440
1441     feature = GST_PLUGIN_FEATURE (features->data);
1442
1443     if (GST_IS_ELEMENT_FACTORY (feature)) {
1444       GstElementFactory *factory;
1445
1446       factory = GST_ELEMENT_FACTORY (feature);
1447       n_print ("  %s%s%s: %s%s%s\n", ELEMENT_NAME_COLOR,
1448           GST_OBJECT_NAME (factory), RESET_COLOR, ELEMENT_DETAIL_COLOR,
1449           gst_element_factory_get_metadata (factory,
1450               GST_ELEMENT_METADATA_LONGNAME), RESET_COLOR);
1451       num_elements++;
1452     } else if (GST_IS_TYPE_FIND_FACTORY (feature)) {
1453       GstTypeFindFactory *factory;
1454       const gchar *const *extensions;
1455
1456       factory = GST_TYPE_FIND_FACTORY (feature);
1457       extensions = gst_type_find_factory_get_extensions (factory);
1458       if (extensions) {
1459         g_print ("  %s%s%s: ", ELEMENT_NAME_COLOR,
1460             gst_plugin_feature_get_name (feature), RESET_COLOR);
1461         print_typefind_extensions (extensions, ELEMENT_DETAIL_COLOR);
1462         g_print ("\n");
1463       } else
1464         g_print ("  %s%s%s: %sno extensions%s\n", ELEMENT_NAME_COLOR,
1465             gst_plugin_feature_get_name (feature), RESET_COLOR,
1466             ELEMENT_DETAIL_COLOR, RESET_COLOR);
1467
1468       num_typefinders++;
1469     } else if (GST_IS_DEVICE_PROVIDER_FACTORY (feature)) {
1470       GstDeviceProviderFactory *factory;
1471
1472       factory = GST_DEVICE_PROVIDER_FACTORY (feature);
1473       n_print ("  %s%s%s: %s%s%s\n", ELEMENT_NAME_COLOR,
1474           GST_OBJECT_NAME (factory), RESET_COLOR, ELEMENT_DETAIL_COLOR,
1475           gst_device_provider_factory_get_metadata (factory,
1476               GST_ELEMENT_METADATA_LONGNAME), RESET_COLOR);
1477       num_devproviders++;
1478     } else if (GST_IS_TRACER_FACTORY (feature)) {
1479       n_print ("  %s%s%s (%s%s%s)\n", ELEMENT_NAME_COLOR,
1480           gst_object_get_name (GST_OBJECT (feature)), RESET_COLOR,
1481           DATATYPE_COLOR, g_type_name (G_OBJECT_TYPE (feature)), RESET_COLOR);
1482       num_tracers++;
1483     } else if (feature) {
1484       n_print ("  %s%s%s (%s%s%s)\n", ELEMENT_NAME_COLOR,
1485           gst_object_get_name (GST_OBJECT (feature)), RESET_COLOR,
1486           DATATYPE_COLOR, g_type_name (G_OBJECT_TYPE (feature)), RESET_COLOR);
1487       num_other++;
1488     }
1489     num_features++;
1490     features = g_list_next (features);
1491   }
1492
1493   gst_plugin_feature_list_free (origlist);
1494
1495   n_print ("\n");
1496   n_print ("  %s%d features%s:\n", HEADING_COLOR, num_features, RESET_COLOR);
1497   if (num_elements > 0)
1498     n_print ("  %s+--%s %s%d elements%s\n", CHILD_LINK_COLOR, RESET_COLOR,
1499         PLUGIN_FEATURE_COLOR, num_elements, RESET_COLOR);
1500   if (num_typefinders > 0)
1501     n_print ("  %s+--%s %s%d typefinders%s\n", CHILD_LINK_COLOR, RESET_COLOR,
1502         PLUGIN_FEATURE_COLOR, num_typefinders, RESET_COLOR);
1503   if (num_devproviders > 0)
1504     n_print ("  %s+--%s %s%d device providers%s\n", CHILD_LINK_COLOR,
1505         RESET_COLOR, PLUGIN_FEATURE_COLOR, num_devproviders, RESET_COLOR);
1506   if (num_tracers > 0)
1507     n_print ("  %s+--%s %s%d tracers%s\n", CHILD_LINK_COLOR, RESET_COLOR,
1508         PLUGIN_FEATURE_COLOR, num_tracers, RESET_COLOR);
1509   if (num_other > 0)
1510     n_print ("  %s+--%s %s%d other objects%s\n", CHILD_LINK_COLOR, RESET_COLOR,
1511         PLUGIN_FEATURE_COLOR, num_other, RESET_COLOR);
1512
1513   n_print ("\n");
1514 }
1515
1516 static int
1517 print_feature_info (const gchar * feature_name, gboolean print_all)
1518 {
1519   GstPluginFeature *feature;
1520   GstRegistry *registry = gst_registry_get ();
1521   int ret;
1522
1523   if ((feature = gst_registry_find_feature (registry, feature_name,
1524               GST_TYPE_ELEMENT_FACTORY))) {
1525     ret = print_element_info (feature, print_all);
1526     goto handled;
1527   }
1528   if ((feature = gst_registry_find_feature (registry, feature_name,
1529               GST_TYPE_TYPE_FIND_FACTORY))) {
1530     ret = print_typefind_info (feature, print_all);
1531     goto handled;
1532   }
1533   if ((feature = gst_registry_find_feature (registry, feature_name,
1534               GST_TYPE_TRACER_FACTORY))) {
1535     ret = print_tracer_info (feature, print_all);
1536     goto handled;
1537   }
1538
1539   /* TODO: handle DEVICE_PROVIDER_FACTORY */
1540
1541   return -1;
1542
1543 handled:
1544   gst_object_unref (feature);
1545   return ret;
1546 }
1547
1548 static int
1549 print_element_info (GstPluginFeature * feature, gboolean print_names)
1550 {
1551   GstElementFactory *factory;
1552   GstElement *element;
1553   GstPlugin *plugin;
1554   gint maxlevel = 0;
1555
1556   factory = GST_ELEMENT_FACTORY (gst_plugin_feature_load (feature));
1557   if (!factory) {
1558     g_print ("%selement plugin couldn't be loaded%s\n", DESC_COLOR,
1559         RESET_COLOR);
1560     return -1;
1561   }
1562
1563   element = gst_element_factory_create (factory, NULL);
1564   if (!element) {
1565     gst_object_unref (factory);
1566     g_print ("%scouldn't construct element for some reason%s\n", DESC_COLOR,
1567         RESET_COLOR);
1568     return -1;
1569   }
1570
1571   if (print_names)
1572     _name =
1573         g_strdup_printf ("%s%s%s: ", DATATYPE_COLOR, GST_OBJECT_NAME (factory),
1574         RESET_COLOR);
1575   else
1576     _name = NULL;
1577
1578   print_factory_details_info (factory);
1579
1580   plugin = gst_plugin_feature_get_plugin (GST_PLUGIN_FEATURE (factory));
1581   if (plugin) {
1582     print_plugin_info (plugin);
1583     gst_object_unref (plugin);
1584   }
1585
1586   print_hierarchy (G_OBJECT_TYPE (element), 0, &maxlevel);
1587   print_interfaces (G_OBJECT_TYPE (element));
1588
1589   print_pad_templates_info (element, factory);
1590   print_clocking_info (element);
1591   print_uri_handler_info (element);
1592   print_pad_info (element);
1593   print_element_properties_info (element);
1594   print_signal_info (element);
1595   print_children_info (element);
1596   print_preset_list (element);
1597
1598   gst_object_unref (element);
1599   gst_object_unref (factory);
1600   g_free (_name);
1601   return 0;
1602 }
1603
1604 static int
1605 print_typefind_info (GstPluginFeature * feature, gboolean print_names)
1606 {
1607   GstTypeFindFactory *factory;
1608   GstPlugin *plugin;
1609   GstCaps *caps;
1610   GstRank rank;
1611   char s[20];
1612   const gchar *const *extensions;
1613
1614   factory = GST_TYPE_FIND_FACTORY (gst_plugin_feature_load (feature));
1615   if (!factory) {
1616     g_print ("%stypefind plugin couldn't be loaded%s\n", DESC_COLOR,
1617         RESET_COLOR);
1618     return -1;
1619   }
1620
1621   if (print_names)
1622     _name =
1623         g_strdup_printf ("%s%s%s: ", DATATYPE_COLOR, GST_OBJECT_NAME (factory),
1624         RESET_COLOR);
1625   else
1626     _name = NULL;
1627
1628   n_print ("%sFactory Details%s:\n", HEADING_COLOR, RESET_COLOR);
1629   rank = gst_plugin_feature_get_rank (feature);
1630   n_print ("  %s%-25s%s%s (%d)%s\n", PROP_NAME_COLOR, "Rank", PROP_VALUE_COLOR,
1631       get_rank_name (s, rank), rank, RESET_COLOR);
1632   n_print ("  %s%-25s%s%s%s\n", PROP_NAME_COLOR, "Name", PROP_VALUE_COLOR,
1633       GST_OBJECT_NAME (factory), RESET_COLOR);
1634   caps = gst_type_find_factory_get_caps (factory);
1635   if (caps) {
1636     gchar *caps_str = gst_caps_to_string (factory->caps);
1637
1638     n_print ("  %s%-25s%s%s%s\n", PROP_NAME_COLOR, "Caps", PROP_VALUE_COLOR,
1639         caps_str, RESET_COLOR);
1640     g_free (caps_str);
1641   }
1642   extensions = gst_type_find_factory_get_extensions (factory);
1643   if (extensions) {
1644     n_print ("  %s%-25s%s", PROP_NAME_COLOR, "Extensions", RESET_COLOR);
1645     print_typefind_extensions (extensions, PROP_VALUE_COLOR);
1646     n_print ("\n");
1647   }
1648   n_print ("\n");
1649
1650   plugin = gst_plugin_feature_get_plugin (GST_PLUGIN_FEATURE (factory));
1651   if (plugin) {
1652     print_plugin_info (plugin);
1653     gst_object_unref (plugin);
1654   }
1655
1656   gst_object_unref (factory);
1657   g_free (_name);
1658   return 0;
1659 }
1660
1661 static int
1662 print_tracer_info (GstPluginFeature * feature, gboolean print_names)
1663 {
1664   GstTracerFactory *factory;
1665   GstTracer *tracer;
1666   GstPlugin *plugin;
1667   gint maxlevel = 0;
1668
1669   factory = GST_TRACER_FACTORY (gst_plugin_feature_load (feature));
1670   if (!factory) {
1671     g_print ("%stracer plugin couldn't be loaded%s\n", DESC_COLOR, RESET_COLOR);
1672     return -1;
1673   }
1674
1675   tracer = (GstTracer *) g_object_new (factory->type, NULL);
1676   if (!tracer) {
1677     gst_object_unref (factory);
1678     g_print ("%scouldn't construct tracer for some reason%s\n", DESC_COLOR,
1679         RESET_COLOR);
1680     return -1;
1681   }
1682
1683   if (print_names)
1684     _name =
1685         g_strdup_printf ("%s%s%s: ", DATATYPE_COLOR, GST_OBJECT_NAME (factory),
1686         RESET_COLOR);
1687   else
1688     _name = NULL;
1689
1690   n_print ("%sFactory Details%s:\n", HEADING_COLOR, RESET_COLOR);
1691   n_print ("  %s%-25s%s%s%s\n", PROP_NAME_COLOR, "Name", PROP_VALUE_COLOR,
1692       GST_OBJECT_NAME (factory), RESET_COLOR);
1693   n_print ("\n");
1694
1695   plugin = gst_plugin_feature_get_plugin (GST_PLUGIN_FEATURE (factory));
1696   if (plugin) {
1697     print_plugin_info (plugin);
1698     gst_object_unref (plugin);
1699   }
1700
1701   print_hierarchy (G_OBJECT_TYPE (tracer), 0, &maxlevel);
1702   print_interfaces (G_OBJECT_TYPE (tracer));
1703
1704   /* TODO: list what hooks it registers
1705    * - the data is available in gsttracerutils, we need to iterate the
1706    *   _priv_tracers hashtable for each probe and then check the list of hooks
1707    *  for each probe whether hook->tracer == tracer :/
1708    */
1709
1710   /* TODO: list what records it emits
1711    * - in class_init tracers can create GstTracerRecord instances
1712    * - those only get logged right now and there is no association with the
1713    *   tracer that created them
1714    * - we'd need to add them to GstTracerFactory
1715    *   gst_tracer_class_add_record (klass, record);
1716    *   - needs work in gstregistrychunks to (de)serialize specs
1717    *   - gst_tracer_register() would need to iterate the list of records and
1718    *     copy the record->spec into the factory
1719    */
1720
1721   gst_object_unref (tracer);
1722   gst_object_unref (factory);
1723   g_free (_name);
1724   return 0;
1725 }
1726
1727 /* NOTE: Not coloring output from automatic install functions, as their output
1728  * is meant for machines, not humans.
1729  */
1730 static void
1731 print_plugin_automatic_install_info_codecs (GstElementFactory * factory)
1732 {
1733   GstPadDirection direction;
1734   const gchar *type_name;
1735   const gchar *klass;
1736   const GList *static_templates, *l;
1737   GstCaps *caps = NULL;
1738   guint i, num;
1739
1740   klass =
1741       gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_KLASS);
1742   g_return_if_fail (klass != NULL);
1743
1744   if (strstr (klass, "Demuxer") ||
1745       strstr (klass, "Decoder") ||
1746       strstr (klass, "Depay") || strstr (klass, "Parser")) {
1747     type_name = "decoder";
1748     direction = GST_PAD_SINK;
1749   } else if (strstr (klass, "Muxer") ||
1750       strstr (klass, "Encoder") || strstr (klass, "Pay")) {
1751     type_name = "encoder";
1752     direction = GST_PAD_SRC;
1753   } else {
1754     return;
1755   }
1756
1757   /* decoder/demuxer sink pads should always be static and there should only
1758    * be one, the same applies to encoders/muxers and source pads */
1759   static_templates = gst_element_factory_get_static_pad_templates (factory);
1760   for (l = static_templates; l != NULL; l = l->next) {
1761     GstStaticPadTemplate *tmpl = NULL;
1762
1763     tmpl = (GstStaticPadTemplate *) l->data;
1764     if (tmpl->direction == direction) {
1765       caps = gst_static_pad_template_get_caps (tmpl);
1766       break;
1767     }
1768   }
1769
1770   if (caps == NULL) {
1771     g_printerr ("Couldn't find static pad template for %s '%s'\n",
1772         type_name, GST_OBJECT_NAME (factory));
1773     return;
1774   }
1775
1776   caps = gst_caps_make_writable (caps);
1777   num = gst_caps_get_size (caps);
1778   for (i = 0; i < num; ++i) {
1779     GstStructure *s;
1780     gchar *s_str;
1781
1782     s = gst_caps_get_structure (caps, i);
1783     /* remove fields that are almost always just MIN-MAX of some sort
1784      * in order to make the caps look less messy */
1785     gst_structure_remove_field (s, "pixel-aspect-ratio");
1786     gst_structure_remove_field (s, "framerate");
1787     gst_structure_remove_field (s, "channels");
1788     gst_structure_remove_field (s, "width");
1789     gst_structure_remove_field (s, "height");
1790     gst_structure_remove_field (s, "rate");
1791     gst_structure_remove_field (s, "depth");
1792     gst_structure_remove_field (s, "clock-rate");
1793     s_str = gst_structure_to_string (s);
1794     g_print ("%s-%s\n", type_name, s_str);
1795     g_free (s_str);
1796   }
1797   gst_caps_unref (caps);
1798 }
1799
1800 static void
1801 print_plugin_automatic_install_info_protocols (GstElementFactory * factory)
1802 {
1803   const gchar *const *protocols;
1804
1805   protocols = gst_element_factory_get_uri_protocols (factory);
1806   if (protocols != NULL && *protocols != NULL) {
1807     switch (gst_element_factory_get_uri_type (factory)) {
1808       case GST_URI_SINK:
1809         while (*protocols != NULL) {
1810           g_print ("urisink-%s\n", *protocols);
1811           ++protocols;
1812         }
1813         break;
1814       case GST_URI_SRC:
1815         while (*protocols != NULL) {
1816           g_print ("urisource-%s\n", *protocols);
1817           ++protocols;
1818         }
1819         break;
1820       default:
1821         break;
1822     }
1823   }
1824 }
1825
1826 static void
1827 print_plugin_automatic_install_info (GstPlugin * plugin)
1828 {
1829   GList *features, *l;
1830
1831   /* not interested in typefind factories, only element factories */
1832   features = gst_registry_get_feature_list (gst_registry_get (),
1833       GST_TYPE_ELEMENT_FACTORY);
1834
1835   for (l = features; l != NULL; l = l->next) {
1836     GstPluginFeature *feature;
1837     GstPlugin *feature_plugin;
1838
1839     feature = GST_PLUGIN_FEATURE (l->data);
1840
1841     /* only interested in the ones that are in the plugin we just loaded */
1842     feature_plugin = gst_plugin_feature_get_plugin (feature);
1843     if (feature_plugin == plugin) {
1844       GstElementFactory *factory;
1845
1846       g_print ("element-%s\n", gst_plugin_feature_get_name (feature));
1847
1848       factory = GST_ELEMENT_FACTORY (feature);
1849       print_plugin_automatic_install_info_protocols (factory);
1850       print_plugin_automatic_install_info_codecs (factory);
1851     }
1852     if (feature_plugin)
1853       gst_object_unref (feature_plugin);
1854   }
1855
1856   g_list_foreach (features, (GFunc) gst_object_unref, NULL);
1857   g_list_free (features);
1858 }
1859
1860 static void
1861 print_all_plugin_automatic_install_info (void)
1862 {
1863   GList *plugins, *orig_plugins;
1864
1865   orig_plugins = plugins = gst_registry_get_plugin_list (gst_registry_get ());
1866   while (plugins) {
1867     GstPlugin *plugin;
1868
1869     plugin = (GstPlugin *) (plugins->data);
1870     plugins = g_list_next (plugins);
1871
1872     print_plugin_automatic_install_info (plugin);
1873   }
1874   gst_plugin_list_free (orig_plugins);
1875 }
1876
1877 #ifdef G_OS_UNIX
1878 static gboolean
1879 redirect_stdout (void)
1880 {
1881   GError *error = NULL;
1882   gchar **argv;
1883   const gchar *pager;
1884   gint stdin_fd;
1885   gchar **envp;
1886
1887   pager = g_getenv ("PAGER");
1888   if (pager == NULL)
1889     pager = DEFAULT_PAGER;
1890
1891   argv = g_strsplit (pager, " ", 0);
1892
1893   envp = g_get_environ ();
1894   envp = g_environ_setenv (envp, "LESS", DEFAULT_LESS_OPTS, TRUE);
1895
1896   if (!g_spawn_async_with_pipes (NULL, argv, envp,
1897           G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
1898           NULL, NULL, &child_pid, &stdin_fd,
1899           /* pass null stdout/stderr to inherit our fds */
1900           NULL, NULL, &error)) {
1901     g_warning ("g_spawn_async_with_pipes() failed: %s\n",
1902         GST_STR_NULL (error->message));
1903     g_strfreev (argv);
1904     g_strfreev (envp);
1905     g_clear_error (&error);
1906
1907     return FALSE;
1908   }
1909
1910   /* redirect our stdout to child stdin */
1911   dup2 (stdin_fd, STDOUT_FILENO);
1912   if (isatty (STDERR_FILENO))
1913     dup2 (stdin_fd, STDERR_FILENO);
1914   close (stdin_fd);
1915
1916   g_strfreev (argv);
1917   g_strfreev (envp);
1918
1919   return TRUE;
1920 }
1921
1922 static void
1923 child_exit_cb (GPid child_pid, gint status, gpointer user_data)
1924 {
1925   g_spawn_close_pid (child_pid);
1926   g_main_loop_quit (loop);
1927 }
1928 #endif
1929
1930 int
1931 main (int argc, char *argv[])
1932 {
1933   gboolean print_all = FALSE;
1934   gboolean do_print_blacklist = FALSE;
1935   gboolean plugin_name = FALSE;
1936   gboolean print_aii = FALSE;
1937   gboolean uri_handlers = FALSE;
1938   gboolean check_exists = FALSE;
1939   gchar *min_version = NULL;
1940   guint minver_maj = GST_VERSION_MAJOR;
1941   guint minver_min = GST_VERSION_MINOR;
1942   guint minver_micro = 0;
1943   gchar *types = NULL;
1944   const gchar *no_colors;
1945   int exit_code = 0;
1946 #ifndef GST_DISABLE_OPTION_PARSING
1947   GOptionEntry options[] = {
1948     {"print-all", 'a', 0, G_OPTION_ARG_NONE, &print_all,
1949         N_("Print all elements"), NULL},
1950     {"print-blacklist", 'b', 0, G_OPTION_ARG_NONE, &do_print_blacklist,
1951         N_("Print list of blacklisted files"), NULL},
1952     {"print-plugin-auto-install-info", '\0', 0, G_OPTION_ARG_NONE, &print_aii,
1953         N_("Print a machine-parsable list of features the specified plugin "
1954               "or all plugins provide.\n                                       "
1955               "Useful in connection with external automatic plugin "
1956               "installation mechanisms"), NULL},
1957     {"plugin", '\0', 0, G_OPTION_ARG_NONE, &plugin_name,
1958         N_("List the plugin contents"), NULL},
1959     {"types", 't', 0, G_OPTION_ARG_STRING, &types,
1960         N_("A slashes ('/') separated list of types of elements (also known "
1961               "as klass) to list. (unordered)"), NULL},
1962     {"exists", '\0', 0, G_OPTION_ARG_NONE, &check_exists,
1963         N_("Check if the specified element or plugin exists"), NULL},
1964     {"atleast-version", '\0', 0, G_OPTION_ARG_STRING, &min_version,
1965         N_
1966           ("When checking if an element or plugin exists, also check that its "
1967               "version is at least the version specified"), NULL},
1968     {"uri-handlers", 'u', 0, G_OPTION_ARG_NONE, &uri_handlers,
1969           N_
1970           ("Print supported URI schemes, with the elements that implement them"),
1971         NULL},
1972     {"no-colors", '\0', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,
1973           &colored_output,
1974           N_
1975           ("Disable colors in output. You can also achieve the same by setting"
1976               "'GST_INSPECT_NO_COLORS' environment variable to any value."),
1977         NULL},
1978     GST_TOOLS_GOPTION_VERSION,
1979     {NULL}
1980   };
1981   GOptionContext *ctx;
1982   GError *err = NULL;
1983 #endif
1984
1985   setlocale (LC_ALL, "");
1986
1987 #ifdef ENABLE_NLS
1988   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1989   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1990   textdomain (GETTEXT_PACKAGE);
1991 #endif
1992
1993   /* avoid glib warnings when inspecting deprecated properties */
1994   g_setenv ("G_ENABLE_DIAGNOSTIC", "0", FALSE);
1995
1996   g_set_prgname ("gst-inspect-" GST_API_VERSION);
1997
1998 #ifndef GST_DISABLE_OPTION_PARSING
1999   ctx = g_option_context_new ("[ELEMENT-NAME | PLUGIN-NAME]");
2000   g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
2001   g_option_context_add_group (ctx, gst_init_get_option_group ());
2002   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
2003     g_printerr ("Error initializing: %s\n", err->message);
2004     g_clear_error (&err);
2005     g_option_context_free (ctx);
2006     return -1;
2007   }
2008   g_option_context_free (ctx);
2009 #else
2010   gst_init (&argc, &argv);
2011 #endif
2012
2013   gst_tools_print_version ();
2014
2015   if (print_all && argc > 1) {
2016     g_printerr ("-a requires no extra arguments\n");
2017     return -1;
2018   }
2019
2020   if (uri_handlers && argc > 1) {
2021     g_printerr ("-u requires no extra arguments\n");
2022     return -1;
2023   }
2024
2025   /* --atleast-version implies --exists */
2026   if (min_version != NULL) {
2027     if (sscanf (min_version, "%u.%u.%u", &minver_maj, &minver_min,
2028             &minver_micro) < 2) {
2029       g_printerr ("Can't parse version '%s' passed to --atleast-version\n",
2030           min_version);
2031       return -1;
2032     }
2033     check_exists = TRUE;
2034   }
2035
2036   if (check_exists) {
2037     if (argc == 1) {
2038       g_printerr ("--exists requires an extra command line argument\n");
2039       exit_code = -1;
2040     } else {
2041       if (!plugin_name) {
2042         GstPluginFeature *feature;
2043
2044         feature = gst_registry_lookup_feature (gst_registry_get (), argv[1]);
2045         if (feature != NULL && gst_plugin_feature_check_version (feature,
2046                 minver_maj, minver_min, minver_micro)) {
2047           exit_code = 0;
2048         } else {
2049           exit_code = 1;
2050         }
2051
2052         if (feature)
2053           gst_object_unref (feature);
2054       } else {
2055         /* FIXME: support checking for plugins too */
2056         g_printerr ("Checking for plugins is not supported yet\n");
2057         exit_code = -1;
2058       }
2059     }
2060     return exit_code;
2061   }
2062
2063   no_colors = g_getenv ("GST_INSPECT_NO_COLORS");
2064   /* We only support truecolor */
2065   colored_output &= (no_colors == NULL);
2066
2067 #ifdef G_OS_UNIX
2068   if (isatty (STDOUT_FILENO)) {
2069     if (redirect_stdout ())
2070       loop = g_main_loop_new (NULL, FALSE);
2071   } else {
2072     colored_output = FALSE;
2073   }
2074 #elif defined(G_OS_WIN32)
2075   {
2076     gint fd = _fileno (stdout);
2077     /* On Windows 10, g_log_writer_supports_color will also setup the console
2078      * so that it correctly interprets ANSI VT sequences if it's supported */
2079     if (!_isatty (fd) || !g_log_writer_supports_color (fd))
2080       colored_output = FALSE;
2081   }
2082 #endif
2083
2084   /* if no arguments, print out list of elements */
2085   if (uri_handlers) {
2086     print_all_uri_handlers ();
2087   } else if (argc == 1 || print_all) {
2088     if (do_print_blacklist)
2089       print_blacklist ();
2090     else {
2091       if (print_aii)
2092         print_all_plugin_automatic_install_info ();
2093       else
2094         print_element_list (print_all, types);
2095     }
2096   } else {
2097     /* else we try to get a factory */
2098     const char *arg = argv[argc - 1];
2099     int retval = -1;
2100
2101     if (!plugin_name) {
2102       retval = print_feature_info (arg, print_all);
2103     }
2104
2105     /* otherwise check if it's a plugin */
2106     if (retval) {
2107       GstPlugin *plugin = gst_registry_find_plugin (gst_registry_get (), arg);
2108
2109       /* if there is such a plugin, print out info */
2110       if (plugin) {
2111         if (print_aii) {
2112           print_plugin_automatic_install_info (plugin);
2113         } else {
2114           print_plugin_info (plugin);
2115           print_plugin_features (plugin);
2116         }
2117       } else {
2118         GError *error = NULL;
2119
2120         if (g_file_test (arg, G_FILE_TEST_EXISTS)) {
2121           plugin = gst_plugin_load_file (arg, &error);
2122
2123           if (plugin) {
2124             if (print_aii) {
2125               print_plugin_automatic_install_info (plugin);
2126             } else {
2127               print_plugin_info (plugin);
2128               print_plugin_features (plugin);
2129             }
2130           } else {
2131             g_printerr (_("Could not load plugin file: %s\n"), error->message);
2132             g_clear_error (&error);
2133             exit_code = -1;
2134             goto done;
2135           }
2136         } else {
2137           g_printerr (_("No such element or plugin '%s'\n"), arg);
2138           exit_code = -1;
2139           goto done;
2140         }
2141       }
2142     }
2143   }
2144
2145 done:
2146
2147 #ifdef G_OS_UNIX
2148   if (loop) {
2149     fflush (stdout);
2150     fflush (stderr);
2151     /* So that the pipe we create in redirect_stdout() is closed */
2152     close (STDOUT_FILENO);
2153     close (STDERR_FILENO);
2154     g_child_watch_add (child_pid, child_exit_cb, NULL);
2155     g_main_loop_run (loop);
2156     g_main_loop_unref (loop);
2157   }
2158 #endif
2159
2160   return exit_code;
2161 }