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