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