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