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