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