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