gst-inspect: print doc urls for Rust plugins
[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 (parray->element_spec) {
748             n_print ("%sGstValueArray of GValues of type%s %s\"%s\"%s",
749                 PROP_VALUE_COLOR, RESET_COLOR, DATATYPE_COLOR,
750                 g_type_name (parray->element_spec->value_type), RESET_COLOR);
751           } else {
752             n_print ("%sGstValueArray of GValues%s", PROP_VALUE_COLOR,
753                 RESET_COLOR);
754           }
755         } else {
756           n_print ("%sUnknown type %ld%s %s\"%s\"%s", PROP_VALUE_COLOR,
757               (glong) param->value_type, RESET_COLOR, DATATYPE_COLOR,
758               g_type_name (param->value_type), RESET_COLOR);
759         }
760         break;
761     }
762     if (!readable)
763       g_print (" %sWrite only%s\n", PROP_VALUE_COLOR, RESET_COLOR);
764     else
765       g_print ("\n");
766
767     pop_indent_n (11);
768
769     g_value_reset (&value);
770   }
771   if (num_properties == 0)
772     n_print ("%snone%s\n", PROP_VALUE_COLOR, RESET_COLOR);
773
774   pop_indent ();
775
776   g_free (property_specs);
777 }
778
779 static void
780 print_element_properties_info (GstElement * element)
781 {
782   g_print ("\n");
783   print_object_properties_info (G_OBJECT (element),
784       G_OBJECT_GET_CLASS (element), "Element Properties");
785 }
786
787 static gint
788 gst_static_pad_compare_func (gconstpointer p1, gconstpointer p2)
789 {
790   GstStaticPadTemplate *pt1, *pt2;
791
792   pt1 = (GstStaticPadTemplate *) p1;
793   pt2 = (GstStaticPadTemplate *) p2;
794
795   return strcmp (pt1->name_template, pt2->name_template);
796 }
797
798
799 static void
800 print_pad_templates_info (GstElement * element, GstElementFactory * factory)
801 {
802   GList *pads;
803   GstStaticPadTemplate *padtemplate;
804   GstPadTemplate *tmpl;
805
806   n_print ("%sPad Templates%s:\n", HEADING_COLOR, RESET_COLOR);
807
808   push_indent ();
809
810   if (gst_element_factory_get_num_pad_templates (factory) == 0) {
811     n_print ("%snone%s\n", PROP_VALUE_COLOR, RESET_COLOR);
812     goto done;
813   }
814
815   pads = g_list_copy ((GList *)
816       gst_element_factory_get_static_pad_templates (factory));
817   pads = g_list_sort (pads, gst_static_pad_compare_func);
818   while (pads) {
819     padtemplate = (GstStaticPadTemplate *) (pads->data);
820     pads = g_list_next (pads);
821
822     if (padtemplate->direction == GST_PAD_SRC)
823       n_print ("%sSRC template%s: %s'%s'%s\n", PROP_NAME_COLOR, RESET_COLOR,
824           PROP_VALUE_COLOR, padtemplate->name_template, RESET_COLOR);
825     else if (padtemplate->direction == GST_PAD_SINK)
826       n_print ("%sSINK template%s: %s'%s'%s\n", PROP_NAME_COLOR, RESET_COLOR,
827           PROP_VALUE_COLOR, padtemplate->name_template, RESET_COLOR);
828     else
829       n_print ("%sUNKNOWN template%s: %s'%s'%s\n", PROP_NAME_COLOR, RESET_COLOR,
830           PROP_VALUE_COLOR, padtemplate->name_template, RESET_COLOR);
831
832     push_indent ();
833
834     if (padtemplate->presence == GST_PAD_ALWAYS)
835       n_print ("%sAvailability%s: %sAlways%s\n", PROP_NAME_COLOR, RESET_COLOR,
836           PROP_VALUE_COLOR, RESET_COLOR);
837     else if (padtemplate->presence == GST_PAD_SOMETIMES)
838       n_print ("%sAvailability%s: %sSometimes%s\n", PROP_NAME_COLOR,
839           RESET_COLOR, PROP_VALUE_COLOR, RESET_COLOR);
840     else if (padtemplate->presence == GST_PAD_REQUEST) {
841       n_print ("%sAvailability%s: %sOn request%s\n", PROP_NAME_COLOR,
842           RESET_COLOR, PROP_VALUE_COLOR, RESET_COLOR);
843     } else
844       n_print ("%sAvailability%s: %sUNKNOWN%s\n", PROP_NAME_COLOR, RESET_COLOR,
845           PROP_VALUE_COLOR, RESET_COLOR);
846
847     if (padtemplate->static_caps.string) {
848       GstCaps *caps = gst_static_caps_get (&padtemplate->static_caps);
849
850       n_print ("%sCapabilities%s:\n", PROP_NAME_COLOR, RESET_COLOR);
851
852       push_indent ();
853       print_caps (caps, "");    // FIXME
854       pop_indent ();
855
856       gst_caps_unref (caps);
857     }
858
859     tmpl = gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (element),
860         padtemplate->name_template);
861     if (tmpl != NULL) {
862       GType pad_type = GST_PAD_TEMPLATE_GTYPE (tmpl);
863
864       if (pad_type != G_TYPE_NONE && pad_type != GST_TYPE_PAD) {
865         gpointer pad_klass;
866
867         pad_klass = g_type_class_ref (pad_type);
868         n_print ("%sType%s: %s%s%s\n", PROP_NAME_COLOR, RESET_COLOR,
869             DATATYPE_COLOR, g_type_name (pad_type), RESET_COLOR);
870         print_object_properties_info (NULL, pad_klass, "Pad Properties");
871         g_type_class_unref (pad_klass);
872       }
873     }
874
875     pop_indent ();
876
877     if (pads != NULL)
878       n_print ("\n");
879   }
880   g_list_free (pads);
881 done:
882   pop_indent ();
883 }
884
885 static void
886 print_clocking_info (GstElement * element)
887 {
888   gboolean requires_clock, provides_clock;
889
890   requires_clock =
891       GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
892   provides_clock =
893       GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
894
895   if (!requires_clock && !provides_clock) {
896     n_print ("\n");
897     n_print ("%sElement has no clocking capabilities.%s\n", DESC_COLOR,
898         RESET_COLOR);
899     return;
900   }
901
902   n_print ("\n");
903   n_print ("%sClocking Interaction%s:\n", PROP_NAME_COLOR, RESET_COLOR);
904
905   push_indent ();
906
907   if (requires_clock) {
908     n_print ("%selement requires a clock%s\n", PROP_VALUE_COLOR, RESET_COLOR);
909   }
910
911   if (provides_clock) {
912     GstClock *clock;
913
914     clock = gst_element_get_clock (element);
915     if (clock) {
916       n_print ("%selement provides a clock%s: %s%s%s\n", PROP_VALUE_COLOR,
917           RESET_COLOR, DATATYPE_COLOR, GST_OBJECT_NAME (clock), RESET_COLOR);
918       gst_object_unref (clock);
919     } else
920       n_print ("%selement is supposed to provide a clock but returned NULL%s\n",
921           PROP_VALUE_COLOR, RESET_COLOR);
922   }
923
924   pop_indent ();
925 }
926
927 static void
928 print_uri_handler_info (GstElement * element)
929 {
930   if (GST_IS_URI_HANDLER (element)) {
931     const gchar *const *uri_protocols;
932     const gchar *uri_type;
933
934     if (gst_uri_handler_get_uri_type (GST_URI_HANDLER (element)) == GST_URI_SRC)
935       uri_type = "source";
936     else if (gst_uri_handler_get_uri_type (GST_URI_HANDLER (element)) ==
937         GST_URI_SINK)
938       uri_type = "sink";
939     else
940       uri_type = "unknown";
941
942     uri_protocols = gst_uri_handler_get_protocols (GST_URI_HANDLER (element));
943
944     n_print ("\n");
945     n_print ("%sURI handling capabilities:%s\n", HEADING_COLOR, RESET_COLOR);
946
947     push_indent ();
948
949     n_print ("%sElement can act as %s.%s\n", DESC_COLOR, uri_type, RESET_COLOR);
950
951     if (uri_protocols && *uri_protocols) {
952       n_print ("%sSupported URI protocols%s:\n", DESC_COLOR, RESET_COLOR);
953       push_indent ();
954       for (; *uri_protocols != NULL; uri_protocols++)
955         n_print ("%s%s%s\n", PROP_ATTR_VALUE_COLOR, *uri_protocols,
956             RESET_COLOR);
957       pop_indent ();
958     } else {
959       n_print ("%sNo supported URI protocols%s\n", PROP_VALUE_COLOR,
960           RESET_COLOR);
961     }
962
963     pop_indent ();
964   } else {
965     n_print ("%sElement has no URI handling capabilities.%s\n", DESC_COLOR,
966         RESET_COLOR);
967   }
968 }
969
970 static void
971 print_pad_info (GstElement * element)
972 {
973   const GList *pads;
974   GstPad *pad;
975
976   n_print ("\n");
977   n_print ("%sPads:%s\n", HEADING_COLOR, RESET_COLOR);
978
979   push_indent ();
980
981   if (!element->numpads) {
982     n_print ("%snone%s\n", PROP_VALUE_COLOR, RESET_COLOR);
983     goto done;
984   }
985
986   pads = element->pads;
987   while (pads) {
988     gchar *name;
989     GstCaps *caps;
990
991     pad = GST_PAD (pads->data);
992     pads = g_list_next (pads);
993
994     name = gst_pad_get_name (pad);
995     if (gst_pad_get_direction (pad) == GST_PAD_SRC)
996       n_print ("%sSRC%s: %s'%s'%s\n", PROP_NAME_COLOR, RESET_COLOR,
997           PROP_VALUE_COLOR, name, RESET_COLOR);
998     else if (gst_pad_get_direction (pad) == GST_PAD_SINK)
999       n_print ("%sSINK%s: %s'%s'%s\n", PROP_NAME_COLOR, RESET_COLOR,
1000           PROP_VALUE_COLOR, name, RESET_COLOR);
1001     else
1002       n_print ("%sUNKNOWN%s: %s'%s'%s\n", PROP_NAME_COLOR, RESET_COLOR,
1003           PROP_VALUE_COLOR, name, RESET_COLOR);
1004
1005     g_free (name);
1006
1007     if (pad->padtemplate) {
1008       push_indent ();
1009       n_print ("%sPad Template%s: %s'%s'%s\n", PROP_NAME_COLOR, RESET_COLOR,
1010           PROP_VALUE_COLOR, pad->padtemplate->name_template, RESET_COLOR);
1011       pop_indent ();
1012     }
1013
1014     caps = gst_pad_get_current_caps (pad);
1015     if (caps) {
1016       n_print ("%sCapabilities:%s\n", PROP_NAME_COLOR, RESET_COLOR);
1017       push_indent ();
1018       print_caps (caps, "");    // FIXME
1019       pop_indent ();
1020       gst_caps_unref (caps);
1021     }
1022   }
1023
1024 done:
1025   pop_indent ();
1026 }
1027
1028 static gboolean
1029 has_sometimes_template (GstElement * element)
1030 {
1031   GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
1032   GList *l;
1033
1034   for (l = klass->padtemplates; l != NULL; l = l->next) {
1035     if (GST_PAD_TEMPLATE (l->data)->presence == GST_PAD_SOMETIMES)
1036       return TRUE;
1037   }
1038
1039   return FALSE;
1040 }
1041
1042 static gboolean
1043 gtype_needs_ptr_marker (GType type)
1044 {
1045   if (type == G_TYPE_POINTER)
1046     return FALSE;
1047
1048   if (G_TYPE_FUNDAMENTAL (type) == G_TYPE_POINTER || G_TYPE_IS_BOXED (type)
1049       || G_TYPE_IS_OBJECT (type))
1050     return TRUE;
1051
1052   return FALSE;
1053 }
1054
1055 static void
1056 print_signal_info (GstElement * element)
1057 {
1058   /* Signals/Actions Block */
1059   guint *signals;
1060   guint nsignals;
1061   gint i = 0, j, k;
1062   GSignalQuery *query = NULL;
1063   GType type;
1064   GSList *found_signals, *l;
1065
1066   for (k = 0; k < 2; k++) {
1067     found_signals = NULL;
1068
1069     /* For elements that have sometimes pads, also list a few useful GstElement
1070      * signals. Put these first, so element-specific ones come later. */
1071     if (k == 0 && has_sometimes_template (element)) {
1072       query = g_new0 (GSignalQuery, 1);
1073       g_signal_query (g_signal_lookup ("pad-added", GST_TYPE_ELEMENT), query);
1074       found_signals = g_slist_append (found_signals, query);
1075       query = g_new0 (GSignalQuery, 1);
1076       g_signal_query (g_signal_lookup ("pad-removed", GST_TYPE_ELEMENT), query);
1077       found_signals = g_slist_append (found_signals, query);
1078       query = g_new0 (GSignalQuery, 1);
1079       g_signal_query (g_signal_lookup ("no-more-pads", GST_TYPE_ELEMENT),
1080           query);
1081       found_signals = g_slist_append (found_signals, query);
1082     }
1083
1084     for (type = G_OBJECT_TYPE (element); type; type = g_type_parent (type)) {
1085       if (type == GST_TYPE_ELEMENT || type == GST_TYPE_OBJECT)
1086         break;
1087
1088       if (type == GST_TYPE_BIN && G_OBJECT_TYPE (element) != GST_TYPE_BIN)
1089         continue;
1090
1091       signals = g_signal_list_ids (type, &nsignals);
1092       for (i = 0; i < nsignals; i++) {
1093         query = g_new0 (GSignalQuery, 1);
1094         g_signal_query (signals[i], query);
1095
1096         if ((k == 0 && !(query->signal_flags & G_SIGNAL_ACTION)) ||
1097             (k == 1 && (query->signal_flags & G_SIGNAL_ACTION)))
1098           found_signals = g_slist_append (found_signals, query);
1099         else
1100           g_free (query);
1101       }
1102       g_free (signals);
1103       signals = NULL;
1104     }
1105
1106     if (found_signals) {
1107       n_print ("\n");
1108       if (k == 0)
1109         n_print ("%sElement Signals%s:\n", HEADING_COLOR, RESET_COLOR);
1110       else
1111         n_print ("%sElement Actions%s:\n", HEADING_COLOR, RESET_COLOR);
1112     } else {
1113       continue;
1114     }
1115
1116     for (l = found_signals; l; l = l->next) {
1117       gchar *indent;
1118       const gchar *pmark;
1119       int indent_len;
1120
1121       query = (GSignalQuery *) l->data;
1122       indent_len = strlen (query->signal_name) +
1123           strlen (g_type_name (query->return_type)) + 24;
1124
1125       if (gtype_needs_ptr_marker (query->return_type)) {
1126         pmark = "* ";
1127         indent_len += 2;
1128       } else {
1129         pmark = " ";
1130       }
1131
1132       indent = g_new0 (gchar, indent_len + 1);
1133       memset (indent, ' ', indent_len);
1134
1135       n_print ("  %s\"%s\"%s :  %s%s%s%suser_function%s (%s%s%s* object%s",
1136           PROP_NAME_COLOR, query->signal_name, RESET_COLOR,
1137           DATATYPE_COLOR, g_type_name (query->return_type), PROP_VALUE_COLOR,
1138           pmark, RESET_COLOR, DATATYPE_COLOR, g_type_name (type),
1139           PROP_VALUE_COLOR, RESET_COLOR);
1140
1141       for (j = 0; j < query->n_params; j++) {
1142         const gchar *type_name, *asterisk;
1143
1144         type_name = g_type_name (query->param_types[j]);
1145         asterisk = gtype_needs_ptr_marker (query->param_types[j]) ? "*" : "";
1146
1147         g_print (",\n");
1148         n_print ("%s%s%s%s%s arg%d%s", indent, DATATYPE_COLOR, type_name,
1149             PROP_VALUE_COLOR, asterisk, j, RESET_COLOR);
1150       }
1151
1152       if (k == 0) {
1153         g_print (",\n");
1154         n_print ("%s%sgpointer %suser_data%s);\n", indent, DATATYPE_COLOR,
1155             PROP_VALUE_COLOR, RESET_COLOR);
1156       } else
1157         g_print (");\n");
1158
1159       g_free (indent);
1160     }
1161
1162     if (found_signals) {
1163       g_slist_foreach (found_signals, (GFunc) g_free, NULL);
1164       g_slist_free (found_signals);
1165     }
1166   }
1167 }
1168
1169 static void
1170 print_children_info (GstElement * element)
1171 {
1172   GList *children;
1173
1174   if (!GST_IS_BIN (element))
1175     return;
1176
1177   children = (GList *) GST_BIN (element)->children;
1178   if (children) {
1179     n_print ("\n");
1180     n_print ("%sChildren%s:\n", HEADING_COLOR, RESET_COLOR);
1181   }
1182
1183   while (children) {
1184     n_print ("  %s%s%s\n", DATATYPE_COLOR,
1185         GST_ELEMENT_NAME (GST_ELEMENT (children->data)), RESET_COLOR);
1186     children = g_list_next (children);
1187   }
1188 }
1189
1190 static void
1191 print_preset_list (GstElement * element)
1192 {
1193   gchar **presets, **preset;
1194
1195   if (!GST_IS_PRESET (element))
1196     return;
1197
1198   presets = gst_preset_get_preset_names (GST_PRESET (element));
1199   if (presets && *presets) {
1200     n_print ("\n");
1201     n_print ("%sPresets%s:\n", HEADING_COLOR, RESET_COLOR);
1202     for (preset = presets; *preset; preset++) {
1203       gchar *comment = NULL;
1204       n_print ("  \"%s\"", *preset);
1205
1206       if (gst_preset_get_meta (GST_PRESET (element), *preset, "comment",
1207               &comment) && comment)
1208         g_print (": %s", comment);
1209       g_free (comment);
1210       g_print ("\n");
1211     }
1212     g_strfreev (presets);
1213   }
1214 }
1215
1216 static gint
1217 gst_plugin_name_compare_func (gconstpointer p1, gconstpointer p2)
1218 {
1219   return strcmp (gst_plugin_get_name ((GstPlugin *) p1),
1220       gst_plugin_get_name ((GstPlugin *) p2));
1221 }
1222
1223 static gint
1224 gst_plugin_feature_name_compare_func (gconstpointer p1, gconstpointer p2)
1225 {
1226   return strcmp (GST_OBJECT_NAME (p1), GST_OBJECT_NAME (p2));
1227 }
1228
1229 static void
1230 print_blacklist (void)
1231 {
1232   GList *plugins, *cur;
1233   gint count = 0;
1234
1235   g_print ("%s%s%s\n", HEADING_COLOR, _("Blacklisted files:"), RESET_COLOR);
1236
1237   plugins = gst_registry_get_plugin_list (gst_registry_get ());
1238   if (sort_output == SORT_TYPE_NAME)
1239     plugins = g_list_sort (plugins, gst_plugin_name_compare_func);
1240
1241   for (cur = plugins; cur != NULL; cur = g_list_next (cur)) {
1242     GstPlugin *plugin = (GstPlugin *) (cur->data);
1243     if (GST_OBJECT_FLAG_IS_SET (plugin, GST_PLUGIN_FLAG_BLACKLISTED)) {
1244       g_print ("  %s\n", gst_plugin_get_name (plugin));
1245       count++;
1246     }
1247   }
1248
1249   g_print ("\n");
1250   g_print (_("%sTotal count%s: %s"), PROP_NAME_COLOR, RESET_COLOR,
1251       PROP_VALUE_COLOR);
1252   g_print (ngettext ("%d blacklisted file", "%d blacklisted files", count),
1253       count);
1254   g_print ("%s\n", RESET_COLOR);
1255   gst_plugin_list_free (plugins);
1256 }
1257
1258 static void
1259 print_typefind_extensions (const gchar * const *extensions, const gchar * color)
1260 {
1261   guint i = 0;
1262
1263   while (extensions[i]) {
1264     g_print ("%s%s%s%s", i > 0 ? ", " : "", color, extensions[i], RESET_COLOR);
1265     i++;
1266   }
1267 }
1268
1269 static void
1270 print_element_list (gboolean print_all, gchar * ftypes)
1271 {
1272   int plugincount = 0, featurecount = 0, blacklistcount = 0;
1273   GList *plugins, *orig_plugins;
1274   gchar **types = NULL;
1275
1276   if (ftypes) {
1277     gint i;
1278
1279     types = g_strsplit (ftypes, "/", -1);
1280     for (i = 0; types[i]; i++)
1281       *types[i] = g_ascii_toupper (*types[i]);
1282
1283   }
1284
1285   orig_plugins = plugins = gst_registry_get_plugin_list (gst_registry_get ());
1286   if (sort_output == SORT_TYPE_NAME)
1287     plugins = g_list_sort (plugins, gst_plugin_name_compare_func);
1288   while (plugins) {
1289     GList *features, *orig_features;
1290     GstPlugin *plugin;
1291
1292     plugin = (GstPlugin *) (plugins->data);
1293     plugins = g_list_next (plugins);
1294     plugincount++;
1295
1296     if (GST_OBJECT_FLAG_IS_SET (plugin, GST_PLUGIN_FLAG_BLACKLISTED)) {
1297       blacklistcount++;
1298       continue;
1299     }
1300
1301     orig_features = features =
1302         gst_registry_get_feature_list_by_plugin (gst_registry_get (),
1303         gst_plugin_get_name (plugin));
1304     if (sort_output == SORT_TYPE_NAME)
1305       features = g_list_sort (features, gst_plugin_feature_name_compare_func);
1306     while (features) {
1307       GstPluginFeature *feature;
1308
1309       if (G_UNLIKELY (features->data == NULL))
1310         goto next;
1311       feature = GST_PLUGIN_FEATURE (features->data);
1312       featurecount++;
1313
1314       if (GST_IS_ELEMENT_FACTORY (feature)) {
1315         const gchar *klass;
1316         GstElementFactory *factory;
1317
1318         factory = GST_ELEMENT_FACTORY (feature);
1319         if (types) {
1320           gint i;
1321           gboolean all_found = TRUE;
1322
1323           klass =
1324               gst_element_factory_get_metadata (factory,
1325               GST_ELEMENT_METADATA_KLASS);
1326           for (i = 0; types[i]; i++) {
1327             if (!strstr (klass, types[i])) {
1328               all_found = FALSE;
1329               break;
1330             }
1331           }
1332
1333           if (!all_found)
1334             goto next;
1335         }
1336         if (print_all)
1337           print_element_info (feature, TRUE);
1338         else
1339           g_print ("%s%s%s:  %s%s%s: %s%s%s\n", PLUGIN_NAME_COLOR,
1340               gst_plugin_get_name (plugin), RESET_COLOR, ELEMENT_NAME_COLOR,
1341               GST_OBJECT_NAME (factory), RESET_COLOR, ELEMENT_DETAIL_COLOR,
1342               gst_element_factory_get_metadata (factory,
1343                   GST_ELEMENT_METADATA_LONGNAME), RESET_COLOR);
1344       } else if (GST_IS_TYPE_FIND_FACTORY (feature)) {
1345         GstTypeFindFactory *factory;
1346         const gchar *const *extensions;
1347
1348         if (types)
1349           goto next;
1350         factory = GST_TYPE_FIND_FACTORY (feature);
1351         if (!print_all)
1352           g_print ("%s%s%s: %s%s%s: ", PLUGIN_NAME_COLOR,
1353               gst_plugin_get_name (plugin), RESET_COLOR, ELEMENT_NAME_COLOR,
1354               gst_plugin_feature_get_name (feature), RESET_COLOR);
1355
1356         extensions = gst_type_find_factory_get_extensions (factory);
1357         if (extensions != NULL) {
1358           if (!print_all) {
1359             print_typefind_extensions (extensions, ELEMENT_DETAIL_COLOR);
1360             g_print ("\n");
1361           }
1362         } else {
1363           if (!print_all)
1364             g_print ("%sno extensions%s\n", ELEMENT_DETAIL_COLOR, RESET_COLOR);
1365         }
1366       } else {
1367         if (types)
1368           goto next;
1369         if (!print_all)
1370           n_print ("%s%s%s:  %s%s%s (%s%s%s)\n", PLUGIN_NAME_COLOR,
1371               gst_plugin_get_name (plugin), RESET_COLOR, ELEMENT_NAME_COLOR,
1372               GST_OBJECT_NAME (feature), RESET_COLOR, ELEMENT_DETAIL_COLOR,
1373               g_type_name (G_OBJECT_TYPE (feature)), RESET_COLOR);
1374       }
1375
1376     next:
1377       features = g_list_next (features);
1378     }
1379
1380     gst_plugin_feature_list_free (orig_features);
1381   }
1382
1383   gst_plugin_list_free (orig_plugins);
1384   g_strfreev (types);
1385
1386   g_print ("\n");
1387   g_print (_("%sTotal count%s: %s"), PROP_NAME_COLOR, RESET_COLOR,
1388       PROP_VALUE_COLOR);
1389   g_print (ngettext ("%d plugin", "%d plugins", plugincount), plugincount);
1390   if (blacklistcount) {
1391     g_print (" (");
1392     g_print (ngettext ("%d blacklist entry", "%d blacklist entries",
1393             blacklistcount), blacklistcount);
1394     g_print (" not shown)");
1395   }
1396   g_print ("%s, %s", RESET_COLOR, PROP_VALUE_COLOR);
1397   g_print (ngettext ("%d feature", "%d features", featurecount), featurecount);
1398   g_print ("%s\n", RESET_COLOR);
1399 }
1400
1401 static void
1402 print_all_uri_handlers (void)
1403 {
1404   GList *plugins, *p, *features, *f;
1405
1406   plugins = gst_registry_get_plugin_list (gst_registry_get ());
1407   if (sort_output == SORT_TYPE_NAME)
1408     plugins = g_list_sort (plugins, gst_plugin_name_compare_func);
1409   for (p = plugins; p; p = p->next) {
1410     GstPlugin *plugin = (GstPlugin *) (p->data);
1411
1412     features =
1413         gst_registry_get_feature_list_by_plugin (gst_registry_get (),
1414         gst_plugin_get_name (plugin));
1415     if (sort_output == SORT_TYPE_NAME)
1416       features = g_list_sort (features, gst_plugin_feature_name_compare_func);
1417     for (f = features; f; f = f->next) {
1418       GstPluginFeature *feature = GST_PLUGIN_FEATURE (f->data);
1419
1420       if (GST_IS_ELEMENT_FACTORY (feature)) {
1421         GstElementFactory *factory;
1422         GstElement *element;
1423
1424         factory = GST_ELEMENT_FACTORY (gst_plugin_feature_load (feature));
1425         if (!factory) {
1426           g_print ("element plugin %s couldn't be loaded\n",
1427               gst_plugin_get_name (plugin));
1428           continue;
1429         }
1430
1431         element = gst_element_factory_create (factory, NULL);
1432         if (!element) {
1433           g_print ("couldn't construct element for %s for some reason\n",
1434               GST_OBJECT_NAME (factory));
1435           gst_object_unref (factory);
1436           continue;
1437         }
1438
1439         if (GST_IS_URI_HANDLER (element)) {
1440           const gchar *const *uri_protocols;
1441           const gchar *const *protocol;
1442           const gchar *dir;
1443
1444           switch (gst_uri_handler_get_uri_type (GST_URI_HANDLER (element))) {
1445             case GST_URI_SRC:
1446               dir = "read";
1447               break;
1448             case GST_URI_SINK:
1449               dir = "write";
1450               break;
1451             default:
1452               dir = "unknown";
1453               break;
1454           }
1455
1456           g_print ("%s%s%s (%s%s%s, %srank %u%s): ",
1457               FEATURE_NAME_COLOR,
1458               gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)),
1459               RESET_COLOR, FEATURE_DIR_COLOR, dir, RESET_COLOR,
1460               FEATURE_RANK_COLOR,
1461               gst_plugin_feature_get_rank (GST_PLUGIN_FEATURE (factory)),
1462               RESET_COLOR);
1463
1464           uri_protocols =
1465               gst_uri_handler_get_protocols (GST_URI_HANDLER (element));
1466           for (protocol = uri_protocols; *protocol != NULL; protocol++) {
1467             if (protocol != uri_protocols)
1468               g_print (", ");
1469             g_print ("%s%s%s", FEATURE_PROTO_COLOR, *protocol, RESET_COLOR);
1470           }
1471           g_print ("\n");
1472         }
1473
1474         gst_object_unref (element);
1475         gst_object_unref (factory);
1476       }
1477     }
1478
1479     gst_plugin_feature_list_free (features);
1480   }
1481
1482   gst_plugin_list_free (plugins);
1483 }
1484
1485 static void
1486 print_plugin_info (GstPlugin * plugin)
1487 {
1488   const gchar *plugin_name = gst_plugin_get_name (plugin);
1489   const gchar *release_date = gst_plugin_get_release_date_string (plugin);
1490   const gchar *filename = gst_plugin_get_filename (plugin);
1491   const gchar *module = gst_plugin_get_source (plugin);
1492   const gchar *origin = gst_plugin_get_origin (plugin);
1493
1494   n_print ("%sPlugin Details%s:\n", HEADING_COLOR, RESET_COLOR);
1495
1496   push_indent ();
1497
1498   n_print ("%s%-25s%s%s%s%s\n", PROP_NAME_COLOR, "Name", RESET_COLOR,
1499       PROP_VALUE_COLOR, plugin_name, RESET_COLOR);
1500   n_print ("%s%-25s%s%s%s%s\n", PROP_NAME_COLOR, "Description", RESET_COLOR,
1501       PROP_VALUE_COLOR, gst_plugin_get_description (plugin), RESET_COLOR);
1502   n_print ("%s%-25s%s%s%s%s\n", PROP_NAME_COLOR, "Filename", RESET_COLOR,
1503       PROP_VALUE_COLOR, (filename != NULL) ? filename : "(null)", RESET_COLOR);
1504   n_print ("%s%-25s%s%s%s%s\n", PROP_NAME_COLOR, "Version", RESET_COLOR,
1505       PROP_VALUE_COLOR, gst_plugin_get_version (plugin), RESET_COLOR);
1506   n_print ("%s%-25s%s%s%s%s\n", PROP_NAME_COLOR, "License", RESET_COLOR,
1507       PROP_VALUE_COLOR, gst_plugin_get_license (plugin), RESET_COLOR);
1508   n_print ("%s%-25s%s%s%s%s\n", PROP_NAME_COLOR, "Source module", RESET_COLOR,
1509       PROP_VALUE_COLOR, module, RESET_COLOR);
1510
1511   /* gst-plugins-rs has per-plugin module names so need to check origin there */
1512   if (g_strv_contains (gstreamer_modules, module)
1513       || (origin != NULL && g_str_has_suffix (origin, "/gst-plugins-rs"))) {
1514     n_print ("%s%-25s%s%s%s/%s/%s\n", PROP_NAME_COLOR, "Documentation",
1515         RESET_COLOR, PROP_VALUE_COLOR, GST_DOC_BASE_URL, plugin_name,
1516         RESET_COLOR);
1517   }
1518
1519   if (release_date != NULL) {
1520     const gchar *tz = "(UTC)";
1521     gchar *str, *sep;
1522
1523 /* may be: YYYY-MM-DD or YYYY-MM-DDTHH:MMZ */
1524 /* YYYY-MM-DDTHH:MMZ => YYYY-MM-DD HH:MM (UTC) */
1525     str = g_strdup (release_date);
1526     sep = strstr (str, "T");
1527     if (sep != NULL) {
1528       *sep = ' ';
1529       sep = strstr (sep + 1, "Z");
1530       if (sep != NULL)
1531         *sep = ' ';
1532     } else {
1533       tz = "";
1534     }
1535     n_print ("%s%-25s%s%s%s%s%s\n", PROP_NAME_COLOR, "Source release date",
1536         RESET_COLOR, PROP_VALUE_COLOR, str, tz, RESET_COLOR);
1537     g_free (str);
1538   }
1539   n_print ("%s%-25s%s%s%s%s\n", PROP_NAME_COLOR, "Binary package", RESET_COLOR,
1540       PROP_VALUE_COLOR, gst_plugin_get_package (plugin), RESET_COLOR);
1541   n_print ("%s%-25s%s%s%s%s\n", PROP_NAME_COLOR, "Origin URL", RESET_COLOR,
1542       PROP_VALUE_COLOR, gst_plugin_get_origin (plugin), RESET_COLOR);
1543
1544   pop_indent ();
1545
1546   n_print ("\n");
1547 }
1548
1549 static void
1550 print_plugin_features (GstPlugin * plugin)
1551 {
1552   GList *features, *origlist;
1553   gint num_features = 0;
1554   gint num_elements = 0;
1555   gint num_tracers = 0;
1556   gint num_typefinders = 0;
1557   gint num_devproviders = 0;
1558   gint num_other = 0;
1559
1560   origlist = features =
1561       gst_registry_get_feature_list_by_plugin (gst_registry_get (),
1562       gst_plugin_get_name (plugin));
1563   if (sort_output == SORT_TYPE_NAME)
1564     features = g_list_sort (features, gst_plugin_feature_name_compare_func);
1565   while (features) {
1566     GstPluginFeature *feature;
1567
1568     feature = GST_PLUGIN_FEATURE (features->data);
1569
1570     if (GST_IS_ELEMENT_FACTORY (feature)) {
1571       GstElementFactory *factory;
1572
1573       factory = GST_ELEMENT_FACTORY (feature);
1574       n_print ("  %s%s%s: %s%s%s\n", ELEMENT_NAME_COLOR,
1575           GST_OBJECT_NAME (factory), RESET_COLOR, ELEMENT_DETAIL_COLOR,
1576           gst_element_factory_get_metadata (factory,
1577               GST_ELEMENT_METADATA_LONGNAME), RESET_COLOR);
1578       num_elements++;
1579     } else if (GST_IS_TYPE_FIND_FACTORY (feature)) {
1580       GstTypeFindFactory *factory;
1581       const gchar *const *extensions;
1582
1583       factory = GST_TYPE_FIND_FACTORY (feature);
1584       extensions = gst_type_find_factory_get_extensions (factory);
1585       if (extensions) {
1586         g_print ("  %s%s%s: ", ELEMENT_NAME_COLOR,
1587             gst_plugin_feature_get_name (feature), RESET_COLOR);
1588         print_typefind_extensions (extensions, ELEMENT_DETAIL_COLOR);
1589         g_print ("\n");
1590       } else
1591         g_print ("  %s%s%s: %sno extensions%s\n", ELEMENT_NAME_COLOR,
1592             gst_plugin_feature_get_name (feature), RESET_COLOR,
1593             ELEMENT_DETAIL_COLOR, RESET_COLOR);
1594
1595       num_typefinders++;
1596     } else if (GST_IS_DEVICE_PROVIDER_FACTORY (feature)) {
1597       GstDeviceProviderFactory *factory;
1598
1599       factory = GST_DEVICE_PROVIDER_FACTORY (feature);
1600       n_print ("  %s%s%s: %s%s%s\n", ELEMENT_NAME_COLOR,
1601           GST_OBJECT_NAME (factory), RESET_COLOR, ELEMENT_DETAIL_COLOR,
1602           gst_device_provider_factory_get_metadata (factory,
1603               GST_ELEMENT_METADATA_LONGNAME), RESET_COLOR);
1604       num_devproviders++;
1605     } else if (GST_IS_TRACER_FACTORY (feature)) {
1606       n_print ("  %s%s%s (%s%s%s)\n", ELEMENT_NAME_COLOR,
1607           gst_object_get_name (GST_OBJECT (feature)), RESET_COLOR,
1608           DATATYPE_COLOR, g_type_name (G_OBJECT_TYPE (feature)), RESET_COLOR);
1609       num_tracers++;
1610     } else if (feature) {
1611       n_print ("  %s%s%s (%s%s%s)\n", ELEMENT_NAME_COLOR,
1612           gst_object_get_name (GST_OBJECT (feature)), RESET_COLOR,
1613           DATATYPE_COLOR, g_type_name (G_OBJECT_TYPE (feature)), RESET_COLOR);
1614       num_other++;
1615     }
1616     num_features++;
1617     features = g_list_next (features);
1618   }
1619
1620   gst_plugin_feature_list_free (origlist);
1621
1622   n_print ("\n");
1623   n_print ("  %s%d features%s:\n", HEADING_COLOR, num_features, RESET_COLOR);
1624   if (num_elements > 0)
1625     n_print ("  %s+--%s %s%d elements%s\n", CHILD_LINK_COLOR, RESET_COLOR,
1626         PLUGIN_FEATURE_COLOR, num_elements, RESET_COLOR);
1627   if (num_typefinders > 0)
1628     n_print ("  %s+--%s %s%d typefinders%s\n", CHILD_LINK_COLOR, RESET_COLOR,
1629         PLUGIN_FEATURE_COLOR, num_typefinders, RESET_COLOR);
1630   if (num_devproviders > 0)
1631     n_print ("  %s+--%s %s%d device providers%s\n", CHILD_LINK_COLOR,
1632         RESET_COLOR, PLUGIN_FEATURE_COLOR, num_devproviders, RESET_COLOR);
1633   if (num_tracers > 0)
1634     n_print ("  %s+--%s %s%d tracers%s\n", CHILD_LINK_COLOR, RESET_COLOR,
1635         PLUGIN_FEATURE_COLOR, num_tracers, RESET_COLOR);
1636   if (num_other > 0)
1637     n_print ("  %s+--%s %s%d other objects%s\n", CHILD_LINK_COLOR, RESET_COLOR,
1638         PLUGIN_FEATURE_COLOR, num_other, RESET_COLOR);
1639
1640   n_print ("\n");
1641 }
1642
1643 static int
1644 print_feature_info (const gchar * feature_name, gboolean print_all)
1645 {
1646   GstPluginFeature *feature;
1647   GstRegistry *registry = gst_registry_get ();
1648   int ret;
1649
1650   if ((feature = gst_registry_find_feature (registry, feature_name,
1651               GST_TYPE_ELEMENT_FACTORY))) {
1652     ret = print_element_info (feature, print_all);
1653     goto handled;
1654   }
1655   if ((feature = gst_registry_find_feature (registry, feature_name,
1656               GST_TYPE_TYPE_FIND_FACTORY))) {
1657     ret = print_typefind_info (feature, print_all);
1658     goto handled;
1659   }
1660   if ((feature = gst_registry_find_feature (registry, feature_name,
1661               GST_TYPE_TRACER_FACTORY))) {
1662     ret = print_tracer_info (feature, print_all);
1663     goto handled;
1664   }
1665
1666   /* TODO: handle DEVICE_PROVIDER_FACTORY */
1667
1668   return -1;
1669
1670 handled:
1671   gst_object_unref (feature);
1672   return ret;
1673 }
1674
1675 static int
1676 print_element_info (GstPluginFeature * feature, gboolean print_names)
1677 {
1678   GstElementFactory *factory;
1679   GstElement *element;
1680   GstPlugin *plugin;
1681   gint maxlevel = 0;
1682
1683   factory = GST_ELEMENT_FACTORY (gst_plugin_feature_load (feature));
1684   if (!factory) {
1685     g_print ("%selement plugin couldn't be loaded%s\n", DESC_COLOR,
1686         RESET_COLOR);
1687     return -1;
1688   }
1689
1690   element = gst_element_factory_create (factory, NULL);
1691   if (!element) {
1692     gst_object_unref (factory);
1693     g_print ("%scouldn't construct element for some reason%s\n", DESC_COLOR,
1694         RESET_COLOR);
1695     return -1;
1696   }
1697
1698   if (print_names)
1699     _name =
1700         g_strdup_printf ("%s%s%s: ", DATATYPE_COLOR, GST_OBJECT_NAME (factory),
1701         RESET_COLOR);
1702   else
1703     _name = NULL;
1704
1705   plugin = gst_plugin_feature_get_plugin (GST_PLUGIN_FEATURE (factory));
1706
1707   print_factory_details_info (factory, plugin);
1708
1709   if (plugin) {
1710     print_plugin_info (plugin);
1711     gst_object_unref (plugin);
1712   }
1713
1714   print_hierarchy (G_OBJECT_TYPE (element), 0, &maxlevel);
1715   print_interfaces (G_OBJECT_TYPE (element));
1716
1717   print_pad_templates_info (element, factory);
1718   print_clocking_info (element);
1719   print_uri_handler_info (element);
1720   print_pad_info (element);
1721   print_element_properties_info (element);
1722   print_signal_info (element);
1723   print_children_info (element);
1724   print_preset_list (element);
1725
1726   gst_object_unref (element);
1727   gst_object_unref (factory);
1728   g_free (_name);
1729   return 0;
1730 }
1731
1732 static int
1733 print_typefind_info (GstPluginFeature * feature, gboolean print_names)
1734 {
1735   GstTypeFindFactory *factory;
1736   GstPlugin *plugin;
1737   GstCaps *caps;
1738   GstRank rank;
1739   char s[40];
1740   const gchar *const *extensions;
1741
1742   factory = GST_TYPE_FIND_FACTORY (gst_plugin_feature_load (feature));
1743   if (!factory) {
1744     g_print ("%stypefind plugin couldn't be loaded%s\n", DESC_COLOR,
1745         RESET_COLOR);
1746     return -1;
1747   }
1748
1749   if (print_names)
1750     _name =
1751         g_strdup_printf ("%s%s%s: ", DATATYPE_COLOR, GST_OBJECT_NAME (factory),
1752         RESET_COLOR);
1753   else
1754     _name = NULL;
1755
1756   n_print ("%sFactory Details%s:\n", HEADING_COLOR, RESET_COLOR);
1757   rank = gst_plugin_feature_get_rank (feature);
1758   n_print ("  %s%-25s%s%s (%d)%s\n", PROP_NAME_COLOR, "Rank", PROP_VALUE_COLOR,
1759       get_rank_name (s, rank), rank, RESET_COLOR);
1760   n_print ("  %s%-25s%s%s%s\n", PROP_NAME_COLOR, "Name", PROP_VALUE_COLOR,
1761       GST_OBJECT_NAME (factory), RESET_COLOR);
1762   caps = gst_type_find_factory_get_caps (factory);
1763   if (caps) {
1764     gchar *caps_str = gst_caps_to_string (factory->caps);
1765
1766     n_print ("  %s%-25s%s%s%s\n", PROP_NAME_COLOR, "Caps", PROP_VALUE_COLOR,
1767         caps_str, RESET_COLOR);
1768     g_free (caps_str);
1769   }
1770   extensions = gst_type_find_factory_get_extensions (factory);
1771   if (extensions) {
1772     n_print ("  %s%-25s%s", PROP_NAME_COLOR, "Extensions", RESET_COLOR);
1773     print_typefind_extensions (extensions, PROP_VALUE_COLOR);
1774     n_print ("\n");
1775   }
1776   n_print ("\n");
1777
1778   plugin = gst_plugin_feature_get_plugin (GST_PLUGIN_FEATURE (factory));
1779   if (plugin) {
1780     print_plugin_info (plugin);
1781     gst_object_unref (plugin);
1782   }
1783
1784   gst_object_unref (factory);
1785   g_free (_name);
1786   return 0;
1787 }
1788
1789 static int
1790 print_tracer_info (GstPluginFeature * feature, gboolean print_names)
1791 {
1792   GstTracerFactory *factory;
1793   GstTracer *tracer;
1794   GstPlugin *plugin;
1795   gint maxlevel = 0;
1796
1797   factory = GST_TRACER_FACTORY (gst_plugin_feature_load (feature));
1798   if (!factory) {
1799     g_print ("%stracer plugin couldn't be loaded%s\n", DESC_COLOR, RESET_COLOR);
1800     return -1;
1801   }
1802
1803   tracer = (GstTracer *) g_object_new (factory->type, NULL);
1804   if (!tracer) {
1805     gst_object_unref (factory);
1806     g_print ("%scouldn't construct tracer for some reason%s\n", DESC_COLOR,
1807         RESET_COLOR);
1808     return -1;
1809   }
1810
1811   if (print_names)
1812     _name =
1813         g_strdup_printf ("%s%s%s: ", DATATYPE_COLOR, GST_OBJECT_NAME (factory),
1814         RESET_COLOR);
1815   else
1816     _name = NULL;
1817
1818   n_print ("%sFactory Details%s:\n", HEADING_COLOR, RESET_COLOR);
1819   n_print ("  %s%-25s%s%s%s\n", PROP_NAME_COLOR, "Name", PROP_VALUE_COLOR,
1820       GST_OBJECT_NAME (factory), RESET_COLOR);
1821   n_print ("\n");
1822
1823   plugin = gst_plugin_feature_get_plugin (GST_PLUGIN_FEATURE (factory));
1824   if (plugin) {
1825     print_plugin_info (plugin);
1826     gst_object_unref (plugin);
1827   }
1828
1829   print_hierarchy (G_OBJECT_TYPE (tracer), 0, &maxlevel);
1830   print_interfaces (G_OBJECT_TYPE (tracer));
1831
1832   /* TODO: list what hooks it registers
1833    * - the data is available in gsttracerutils, we need to iterate the
1834    *   _priv_tracers hashtable for each probe and then check the list of hooks
1835    *  for each probe whether hook->tracer == tracer :/
1836    */
1837
1838   /* TODO: list what records it emits
1839    * - in class_init tracers can create GstTracerRecord instances
1840    * - those only get logged right now and there is no association with the
1841    *   tracer that created them
1842    * - we'd need to add them to GstTracerFactory
1843    *   gst_tracer_class_add_record (klass, record);
1844    *   - needs work in gstregistrychunks to (de)serialize specs
1845    *   - gst_tracer_register() would need to iterate the list of records and
1846    *     copy the record->spec into the factory
1847    */
1848
1849   gst_object_unref (tracer);
1850   gst_object_unref (factory);
1851   g_free (_name);
1852   return 0;
1853 }
1854
1855 /* NOTE: Not coloring output from automatic install functions, as their output
1856  * is meant for machines, not humans.
1857  */
1858 static void
1859 print_plugin_automatic_install_info_codecs (GstElementFactory * factory)
1860 {
1861   GstPadDirection direction;
1862   const gchar *type_name;
1863   const gchar *klass;
1864   const GList *static_templates, *l;
1865   GstCaps *caps = NULL;
1866   guint i, num;
1867
1868   klass =
1869       gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_KLASS);
1870   g_return_if_fail (klass != NULL);
1871
1872   if (strstr (klass, "Demuxer") ||
1873       strstr (klass, "Decoder") ||
1874       strstr (klass, "Depay") || strstr (klass, "Parser")) {
1875     type_name = "decoder";
1876     direction = GST_PAD_SINK;
1877   } else if (strstr (klass, "Muxer") ||
1878       strstr (klass, "Encoder") || strstr (klass, "Pay")) {
1879     type_name = "encoder";
1880     direction = GST_PAD_SRC;
1881   } else {
1882     return;
1883   }
1884
1885   /* decoder/demuxer sink pads should always be static and there should only
1886    * be one, the same applies to encoders/muxers and source pads */
1887   static_templates = gst_element_factory_get_static_pad_templates (factory);
1888   for (l = static_templates; l != NULL; l = l->next) {
1889     GstStaticPadTemplate *tmpl = NULL;
1890
1891     tmpl = (GstStaticPadTemplate *) l->data;
1892     if (tmpl->direction == direction) {
1893       caps = gst_static_pad_template_get_caps (tmpl);
1894       break;
1895     }
1896   }
1897
1898   if (caps == NULL) {
1899     g_printerr ("Couldn't find static pad template for %s '%s'\n",
1900         type_name, GST_OBJECT_NAME (factory));
1901     return;
1902   }
1903
1904   caps = gst_caps_make_writable (caps);
1905   num = gst_caps_get_size (caps);
1906   for (i = 0; i < num; ++i) {
1907     GstStructure *s;
1908     gchar *s_str;
1909
1910     s = gst_caps_get_structure (caps, i);
1911     /* remove fields that are almost always just MIN-MAX of some sort
1912      * in order to make the caps look less messy */
1913     gst_structure_remove_field (s, "pixel-aspect-ratio");
1914     gst_structure_remove_field (s, "framerate");
1915     gst_structure_remove_field (s, "channels");
1916     gst_structure_remove_field (s, "width");
1917     gst_structure_remove_field (s, "height");
1918     gst_structure_remove_field (s, "rate");
1919     gst_structure_remove_field (s, "depth");
1920     gst_structure_remove_field (s, "clock-rate");
1921     s_str = gst_structure_to_string (s);
1922     g_print ("%s-%s\n", type_name, s_str);
1923     g_free (s_str);
1924   }
1925   gst_caps_unref (caps);
1926 }
1927
1928 static void
1929 print_plugin_automatic_install_info_protocols (GstElementFactory * factory)
1930 {
1931   const gchar *const *protocols;
1932
1933   protocols = gst_element_factory_get_uri_protocols (factory);
1934   if (protocols != NULL && *protocols != NULL) {
1935     switch (gst_element_factory_get_uri_type (factory)) {
1936       case GST_URI_SINK:
1937         while (*protocols != NULL) {
1938           g_print ("urisink-%s\n", *protocols);
1939           ++protocols;
1940         }
1941         break;
1942       case GST_URI_SRC:
1943         while (*protocols != NULL) {
1944           g_print ("urisource-%s\n", *protocols);
1945           ++protocols;
1946         }
1947         break;
1948       default:
1949         break;
1950     }
1951   }
1952 }
1953
1954 static void
1955 print_plugin_automatic_install_info (GstPlugin * plugin)
1956 {
1957   GList *features, *l;
1958
1959   /* not interested in typefind factories, only element factories */
1960   features = gst_registry_get_feature_list (gst_registry_get (),
1961       GST_TYPE_ELEMENT_FACTORY);
1962
1963   for (l = features; l != NULL; l = l->next) {
1964     GstPluginFeature *feature;
1965     GstPlugin *feature_plugin;
1966
1967     feature = GST_PLUGIN_FEATURE (l->data);
1968
1969     /* only interested in the ones that are in the plugin we just loaded */
1970     feature_plugin = gst_plugin_feature_get_plugin (feature);
1971     if (feature_plugin == plugin) {
1972       GstElementFactory *factory;
1973
1974       g_print ("element-%s\n", gst_plugin_feature_get_name (feature));
1975
1976       factory = GST_ELEMENT_FACTORY (feature);
1977       print_plugin_automatic_install_info_protocols (factory);
1978       print_plugin_automatic_install_info_codecs (factory);
1979     }
1980     if (feature_plugin)
1981       gst_object_unref (feature_plugin);
1982   }
1983
1984   g_list_foreach (features, (GFunc) gst_object_unref, NULL);
1985   g_list_free (features);
1986 }
1987
1988 static void
1989 print_all_plugin_automatic_install_info (void)
1990 {
1991   GList *plugins, *orig_plugins;
1992
1993   orig_plugins = plugins = gst_registry_get_plugin_list (gst_registry_get ());
1994   if (sort_output == SORT_TYPE_NAME)
1995     plugins = g_list_sort (plugins, gst_plugin_name_compare_func);
1996   while (plugins) {
1997     GstPlugin *plugin;
1998
1999     plugin = (GstPlugin *) (plugins->data);
2000     plugins = g_list_next (plugins);
2001
2002     print_plugin_automatic_install_info (plugin);
2003   }
2004   gst_plugin_list_free (orig_plugins);
2005 }
2006
2007 #ifdef G_OS_UNIX
2008 static gboolean
2009 redirect_stdout (void)
2010 {
2011   GError *error = NULL;
2012   gchar **argv;
2013   const gchar *pager, *less;
2014   gint stdin_fd;
2015   gchar **envp;
2016
2017   pager = g_getenv ("PAGER");
2018   if (pager == NULL)
2019     pager = DEFAULT_PAGER;
2020
2021   argv = g_strsplit (pager, " ", 0);
2022
2023   less = g_getenv ("GST_LESS");
2024   if (less == NULL)
2025     less = DEFAULT_LESS_OPTS;
2026
2027   envp = g_get_environ ();
2028   envp = g_environ_setenv (envp, "LESS", less, TRUE);
2029
2030   if (!g_spawn_async_with_pipes (NULL, argv, envp,
2031           G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
2032           NULL, NULL, &child_pid, &stdin_fd,
2033           /* pass null stdout/stderr to inherit our fds */
2034           NULL, NULL, &error)) {
2035     if (pager != DEFAULT_PAGER) {
2036       g_warning ("g_spawn_async_with_pipes() failed: %s\n",
2037           GST_STR_NULL (error->message));
2038     }
2039     g_strfreev (argv);
2040     g_strfreev (envp);
2041     g_clear_error (&error);
2042
2043     return FALSE;
2044   }
2045
2046   /* redirect our stdout to child stdin */
2047   dup2 (stdin_fd, STDOUT_FILENO);
2048   if (isatty (STDERR_FILENO))
2049     dup2 (stdin_fd, STDERR_FILENO);
2050   close (stdin_fd);
2051
2052   g_strfreev (argv);
2053   g_strfreev (envp);
2054
2055   return TRUE;
2056 }
2057
2058 static void
2059 child_exit_cb (GPid child_pid, gint status, gpointer user_data)
2060 {
2061   g_spawn_close_pid (child_pid);
2062   g_main_loop_quit (loop);
2063 }
2064 #endif
2065
2066 static gboolean
2067 _parse_sort_type (const gchar * option_name, const gchar * optarg,
2068     gpointer data, GError ** error)
2069 {
2070   if (!g_strcmp0 (optarg, "name")) {
2071     sort_output = SORT_TYPE_NAME;
2072     return TRUE;
2073   } else if (!g_strcmp0 (optarg, "none")) {
2074     sort_output = SORT_TYPE_NONE;
2075     return TRUE;
2076   }
2077
2078   return FALSE;
2079 }
2080
2081 int
2082 main (int argc, char *argv[])
2083 {
2084   gboolean print_all = FALSE;
2085   gboolean do_print_blacklist = FALSE;
2086   gboolean plugin_name = FALSE;
2087   gboolean print_aii = FALSE;
2088   gboolean uri_handlers = FALSE;
2089   gboolean check_exists = FALSE;
2090   gboolean color_always = FALSE;
2091   gchar *min_version = NULL;
2092   guint minver_maj = GST_VERSION_MAJOR;
2093   guint minver_min = GST_VERSION_MINOR;
2094   guint minver_micro = 0;
2095   gchar *types = NULL;
2096   const gchar *no_colors;
2097   int exit_code = 0;
2098 #ifndef GST_DISABLE_OPTION_PARSING
2099   GOptionEntry options[] = {
2100     {"print-all", 'a', 0, G_OPTION_ARG_NONE, &print_all,
2101         N_("Print all elements"), NULL},
2102     {"print-blacklist", 'b', 0, G_OPTION_ARG_NONE, &do_print_blacklist,
2103         N_("Print list of blacklisted files"), NULL},
2104     {"print-plugin-auto-install-info", '\0', 0, G_OPTION_ARG_NONE, &print_aii,
2105         N_("Print a machine-parsable list of features the specified plugin "
2106               "or all plugins provide.\n                                       "
2107               "Useful in connection with external automatic plugin "
2108               "installation mechanisms"), NULL},
2109     {"plugin", '\0', 0, G_OPTION_ARG_NONE, &plugin_name,
2110         N_("List the plugin contents"), NULL},
2111     {"types", 't', 0, G_OPTION_ARG_STRING, &types,
2112         N_("A slashes ('/') separated list of types of elements (also known "
2113               "as klass) to list. (unordered)"), NULL},
2114     {"exists", '\0', 0, G_OPTION_ARG_NONE, &check_exists,
2115         N_("Check if the specified element or plugin exists"), NULL},
2116     {"atleast-version", '\0', 0, G_OPTION_ARG_STRING, &min_version,
2117         N_
2118           ("When checking if an element or plugin exists, also check that its "
2119               "version is at least the version specified"), NULL},
2120     {"uri-handlers", 'u', 0, G_OPTION_ARG_NONE, &uri_handlers,
2121           N_
2122           ("Print supported URI schemes, with the elements that implement them"),
2123         NULL},
2124     {"no-colors", '\0', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,
2125           &colored_output,
2126           N_
2127           ("Disable colors in output. You can also achieve the same by setting "
2128               "'GST_INSPECT_NO_COLORS' environment variable to any value."),
2129         NULL},
2130     {"sort", '\0', G_OPTION_ARG_NONE, G_OPTION_ARG_CALLBACK, &_parse_sort_type,
2131           "Sort plugins and features. Sorting keys: name (default), none.",
2132         "<sort-key>"}
2133     ,
2134     {"color", 'C', 0, G_OPTION_ARG_NONE, &color_always,
2135           N_("Color output, even when not sending to a tty."),
2136         NULL},
2137     GST_TOOLS_GOPTION_VERSION,
2138     {NULL}
2139   };
2140   GOptionContext *ctx;
2141   GError *err = NULL;
2142 #endif
2143
2144   setlocale (LC_ALL, "");
2145
2146 #ifdef ENABLE_NLS
2147   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
2148   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2149   textdomain (GETTEXT_PACKAGE);
2150 #endif
2151
2152   /* avoid glib warnings when inspecting deprecated properties */
2153   g_setenv ("G_ENABLE_DIAGNOSTIC", "0", FALSE);
2154
2155   g_set_prgname ("gst-inspect-" GST_API_VERSION);
2156
2157 #ifndef GST_DISABLE_OPTION_PARSING
2158   ctx = g_option_context_new ("[ELEMENT-NAME | PLUGIN-NAME]");
2159   g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
2160   g_option_context_add_group (ctx, gst_init_get_option_group ());
2161   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
2162     g_printerr ("Error initializing: %s\n", err->message);
2163     g_clear_error (&err);
2164     g_option_context_free (ctx);
2165     return -1;
2166   }
2167   g_option_context_free (ctx);
2168 #else
2169   gst_init (&argc, &argv);
2170 #endif
2171
2172   gst_tools_print_version ();
2173
2174   if (print_all && argc > 1) {
2175     g_printerr ("-a requires no extra arguments\n");
2176     return -1;
2177   }
2178
2179   if (uri_handlers && argc > 1) {
2180     g_printerr ("-u requires no extra arguments\n");
2181     return -1;
2182   }
2183
2184   /* --atleast-version implies --exists */
2185   if (min_version != NULL) {
2186     if (sscanf (min_version, "%u.%u.%u", &minver_maj, &minver_min,
2187             &minver_micro) < 2) {
2188       g_printerr ("Can't parse version '%s' passed to --atleast-version\n",
2189           min_version);
2190       g_free (min_version);
2191       return -1;
2192     }
2193     g_free (min_version);
2194     check_exists = TRUE;
2195   }
2196
2197   if (check_exists) {
2198     if (argc == 1) {
2199       g_printerr ("--exists requires an extra command line argument\n");
2200       exit_code = -1;
2201     } else {
2202       if (!plugin_name) {
2203         GstPluginFeature *feature;
2204
2205         feature = gst_registry_lookup_feature (gst_registry_get (), argv[1]);
2206         if (feature != NULL && gst_plugin_feature_check_version (feature,
2207                 minver_maj, minver_min, minver_micro)) {
2208           exit_code = 0;
2209         } else {
2210           exit_code = 1;
2211         }
2212
2213         if (feature)
2214           gst_object_unref (feature);
2215       } else {
2216         /* FIXME: support checking for plugins too */
2217         g_printerr ("Checking for plugins is not supported yet\n");
2218         exit_code = -1;
2219       }
2220     }
2221     return exit_code;
2222   }
2223
2224   no_colors = g_getenv ("GST_INSPECT_NO_COLORS");
2225   /* We only support truecolor */
2226   colored_output &= (no_colors == NULL);
2227
2228 #ifdef G_OS_UNIX
2229   if (isatty (STDOUT_FILENO)) {
2230     if (redirect_stdout ())
2231       loop = g_main_loop_new (NULL, FALSE);
2232   } else {
2233     colored_output = (color_always) ? TRUE : FALSE;
2234   }
2235 #elif defined(G_OS_WIN32)
2236   {
2237     /* g_log_writer_supports_color is available since 2.50.0 */
2238     gint fd = _fileno (stdout);
2239     /* On Windows 10, g_log_writer_supports_color will also setup the console
2240      * so that it correctly interprets ANSI VT sequences if it's supported */
2241     if (!_isatty (fd) || !g_log_writer_supports_color (fd))
2242       colored_output = FALSE;
2243   }
2244 #endif
2245
2246   /* if no arguments, print out list of elements */
2247   if (uri_handlers) {
2248     print_all_uri_handlers ();
2249   } else if (argc == 1 || print_all) {
2250     if (do_print_blacklist)
2251       print_blacklist ();
2252     else {
2253       if (print_aii)
2254         print_all_plugin_automatic_install_info ();
2255       else
2256         print_element_list (print_all, types);
2257     }
2258   } else {
2259     /* else we try to get a factory */
2260     const char *arg = argv[argc - 1];
2261     int retval = -1;
2262
2263     if (!plugin_name) {
2264       retval = print_feature_info (arg, print_all);
2265     }
2266
2267     /* otherwise check if it's a plugin */
2268     if (retval) {
2269       GstPlugin *plugin = gst_registry_find_plugin (gst_registry_get (), arg);
2270
2271       /* if there is such a plugin, print out info */
2272       if (plugin) {
2273         if (print_aii) {
2274           print_plugin_automatic_install_info (plugin);
2275         } else {
2276           print_plugin_info (plugin);
2277           print_plugin_features (plugin);
2278         }
2279       } else {
2280         GError *error = NULL;
2281
2282         if (g_file_test (arg, G_FILE_TEST_EXISTS)) {
2283           plugin = gst_plugin_load_file (arg, &error);
2284
2285           if (plugin) {
2286             if (print_aii) {
2287               print_plugin_automatic_install_info (plugin);
2288             } else {
2289               print_plugin_info (plugin);
2290               print_plugin_features (plugin);
2291             }
2292           } else {
2293             g_printerr (_("Could not load plugin file: %s\n"), error->message);
2294             g_clear_error (&error);
2295             exit_code = -1;
2296             goto done;
2297           }
2298         } else {
2299           g_printerr (_("No such element or plugin '%s'\n"), arg);
2300           exit_code = -1;
2301           goto done;
2302         }
2303       }
2304     }
2305   }
2306
2307 done:
2308
2309 #ifdef G_OS_UNIX
2310   if (loop) {
2311     fflush (stdout);
2312     fflush (stderr);
2313     /* So that the pipe we create in redirect_stdout() is closed */
2314     close (STDOUT_FILENO);
2315     close (STDERR_FILENO);
2316     g_child_watch_add (child_pid, child_exit_cb, NULL);
2317     g_main_loop_run (loop);
2318     g_main_loop_unref (loop);
2319   }
2320 #endif
2321
2322   return exit_code;
2323 }