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