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