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