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