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