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