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