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