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