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