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