gst-inspect: Print GstValueArray properties nicely
[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 if (param->value_type == GST_TYPE_ARRAY) {
523           GstParamSpecArray *parray = GST_PARAM_SPEC_ARRAY_LIST (param);
524
525           if (parray->element_spec) {
526             n_print ("%-23.23s GstValueArray of GValues of type \"%s\"", "",
527                 g_type_name (parray->element_spec->value_type));
528           } else {
529             n_print ("%-23.23s GstValueArray of GValues", "");
530           }
531         } else {
532           n_print ("%-23.23s Unknown type %ld \"%s\"", "",
533               (glong) param->value_type, g_type_name (param->value_type));
534         }
535         break;
536     }
537     if (!readable)
538       g_print (" Write only\n");
539     else
540       g_print ("\n");
541
542     g_value_reset (&value);
543   }
544   if (num_properties == 0)
545     n_print ("  none\n");
546
547   g_free (property_specs);
548 }
549
550 static void
551 print_pad_templates_info (GstElement * element, GstElementFactory * factory)
552 {
553   const GList *pads;
554   GstStaticPadTemplate *padtemplate;
555
556   n_print ("Pad Templates:\n");
557   if (gst_element_factory_get_num_pad_templates (factory) == 0) {
558     n_print ("  none\n");
559     return;
560   }
561
562   pads = gst_element_factory_get_static_pad_templates (factory);
563   while (pads) {
564     padtemplate = (GstStaticPadTemplate *) (pads->data);
565     pads = g_list_next (pads);
566
567     if (padtemplate->direction == GST_PAD_SRC)
568       n_print ("  SRC template: '%s'\n", padtemplate->name_template);
569     else if (padtemplate->direction == GST_PAD_SINK)
570       n_print ("  SINK template: '%s'\n", padtemplate->name_template);
571     else
572       n_print ("  UNKNOWN!!! template: '%s'\n", padtemplate->name_template);
573
574     if (padtemplate->presence == GST_PAD_ALWAYS)
575       n_print ("    Availability: Always\n");
576     else if (padtemplate->presence == GST_PAD_SOMETIMES)
577       n_print ("    Availability: Sometimes\n");
578     else if (padtemplate->presence == GST_PAD_REQUEST) {
579       n_print ("    Availability: On request\n");
580     } else
581       n_print ("    Availability: UNKNOWN!!!\n");
582
583     if (padtemplate->static_caps.string) {
584       GstCaps *caps = gst_static_caps_get (&padtemplate->static_caps);
585
586       n_print ("    Capabilities:\n");
587       print_caps (caps, "      ");
588       gst_caps_unref (caps);
589     }
590
591     n_print ("\n");
592   }
593 }
594
595 static void
596 print_element_flag_info (GstElement * element)
597 {
598   gboolean have_flags = FALSE;
599
600   n_print ("\n");
601   n_print ("Element Flags:\n");
602
603   if (!have_flags)
604     n_print ("  no flags set\n");
605
606   if (GST_IS_BIN (element)) {
607     n_print ("\n");
608     n_print ("Bin Flags:\n");
609     if (!have_flags)
610       n_print ("  no flags set\n");
611   }
612 }
613
614 static void
615 print_implementation_info (GstElement * element)
616 {
617   GstElementClass *gstelement_class;
618
619   gstelement_class = GST_ELEMENT_CLASS (G_OBJECT_GET_CLASS (element));
620
621   n_print ("\n");
622   n_print ("Element Implementation:\n");
623
624   n_print ("  Has change_state() function: %s\n",
625       GST_DEBUG_FUNCPTR_NAME (gstelement_class->change_state));
626 }
627
628 static void
629 print_clocking_info (GstElement * element)
630 {
631   gboolean requires_clock, provides_clock;
632
633   requires_clock =
634       GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
635   provides_clock =
636       GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
637
638   if (!requires_clock && !provides_clock) {
639     n_print ("\n");
640     n_print ("Element has no clocking capabilities.\n");
641     return;
642   }
643
644   n_print ("\n");
645   n_print ("Clocking Interaction:\n");
646   if (requires_clock) {
647     n_print ("  element requires a clock\n");
648   }
649
650   if (provides_clock) {
651     GstClock *clock;
652
653     clock = gst_element_get_clock (element);
654     if (clock) {
655       n_print ("  element provides a clock: %s\n", GST_OBJECT_NAME (clock));
656       gst_object_unref (clock);
657     } else
658       n_print ("  element is supposed to provide a clock but returned NULL\n");
659   }
660 }
661
662 static void
663 print_uri_handler_info (GstElement * element)
664 {
665   if (GST_IS_URI_HANDLER (element)) {
666     const gchar *const *uri_protocols;
667     const gchar *uri_type;
668
669     if (gst_uri_handler_get_uri_type (GST_URI_HANDLER (element)) == GST_URI_SRC)
670       uri_type = "source";
671     else if (gst_uri_handler_get_uri_type (GST_URI_HANDLER (element)) ==
672         GST_URI_SINK)
673       uri_type = "sink";
674     else
675       uri_type = "unknown";
676
677     uri_protocols = gst_uri_handler_get_protocols (GST_URI_HANDLER (element));
678
679     n_print ("\n");
680     n_print ("URI handling capabilities:\n");
681     n_print ("  Element can act as %s.\n", uri_type);
682
683     if (uri_protocols && *uri_protocols) {
684       n_print ("  Supported URI protocols:\n");
685       for (; *uri_protocols != NULL; uri_protocols++)
686         n_print ("    %s\n", *uri_protocols);
687     } else {
688       n_print ("  No supported URI protocols\n");
689     }
690   } else {
691     n_print ("Element has no URI handling capabilities.\n");
692   }
693 }
694
695 static void
696 print_pad_info (GstElement * element)
697 {
698   const GList *pads;
699   GstPad *pad;
700
701   n_print ("\n");
702   n_print ("Pads:\n");
703
704   if (!element->numpads) {
705     n_print ("  none\n");
706     return;
707   }
708
709   pads = element->pads;
710   while (pads) {
711     gchar *name;
712     GstCaps *caps;
713
714     pad = GST_PAD (pads->data);
715     pads = g_list_next (pads);
716
717     name = gst_pad_get_name (pad);
718     if (gst_pad_get_direction (pad) == GST_PAD_SRC)
719       n_print ("  SRC: '%s'\n", name);
720     else if (gst_pad_get_direction (pad) == GST_PAD_SINK)
721       n_print ("  SINK: '%s'\n", name);
722     else
723       n_print ("  UNKNOWN!!!: '%s'\n", name);
724
725     g_free (name);
726
727     if (pad->padtemplate)
728       n_print ("    Pad Template: '%s'\n", pad->padtemplate->name_template);
729
730     caps = gst_pad_get_current_caps (pad);
731     if (caps) {
732       n_print ("    Capabilities:\n");
733       print_caps (caps, "      ");
734       gst_caps_unref (caps);
735     }
736   }
737 }
738
739 static gboolean
740 has_sometimes_template (GstElement * element)
741 {
742   GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
743   GList *l;
744
745   for (l = klass->padtemplates; l != NULL; l = l->next) {
746     if (GST_PAD_TEMPLATE (l->data)->presence == GST_PAD_SOMETIMES)
747       return TRUE;
748   }
749
750   return FALSE;
751 }
752
753 static gboolean
754 gtype_needs_ptr_marker (GType type)
755 {
756   if (type == G_TYPE_POINTER)
757     return FALSE;
758
759   if (G_TYPE_FUNDAMENTAL (type) == G_TYPE_POINTER || G_TYPE_IS_BOXED (type)
760       || G_TYPE_IS_OBJECT (type))
761     return TRUE;
762
763   return FALSE;
764 }
765
766 static void
767 print_signal_info (GstElement * element)
768 {
769   /* Signals/Actions Block */
770   guint *signals;
771   guint nsignals;
772   gint i = 0, j, k;
773   GSignalQuery *query = NULL;
774   GType type;
775   GSList *found_signals, *l;
776
777   for (k = 0; k < 2; k++) {
778     found_signals = NULL;
779
780     /* For elements that have sometimes pads, also list a few useful GstElement
781      * signals. Put these first, so element-specific ones come later. */
782     if (k == 0 && has_sometimes_template (element)) {
783       query = g_new0 (GSignalQuery, 1);
784       g_signal_query (g_signal_lookup ("pad-added", GST_TYPE_ELEMENT), query);
785       found_signals = g_slist_append (found_signals, query);
786       query = g_new0 (GSignalQuery, 1);
787       g_signal_query (g_signal_lookup ("pad-removed", GST_TYPE_ELEMENT), query);
788       found_signals = g_slist_append (found_signals, query);
789       query = g_new0 (GSignalQuery, 1);
790       g_signal_query (g_signal_lookup ("no-more-pads", GST_TYPE_ELEMENT),
791           query);
792       found_signals = g_slist_append (found_signals, query);
793     }
794
795     for (type = G_OBJECT_TYPE (element); type; type = g_type_parent (type)) {
796       if (type == GST_TYPE_ELEMENT || type == GST_TYPE_OBJECT)
797         break;
798
799       if (type == GST_TYPE_BIN && G_OBJECT_TYPE (element) != GST_TYPE_BIN)
800         continue;
801
802       signals = g_signal_list_ids (type, &nsignals);
803       for (i = 0; i < nsignals; i++) {
804         query = g_new0 (GSignalQuery, 1);
805         g_signal_query (signals[i], query);
806
807         if ((k == 0 && !(query->signal_flags & G_SIGNAL_ACTION)) ||
808             (k == 1 && (query->signal_flags & G_SIGNAL_ACTION)))
809           found_signals = g_slist_append (found_signals, query);
810         else
811           g_free (query);
812       }
813       g_free (signals);
814       signals = NULL;
815     }
816
817     if (found_signals) {
818       n_print ("\n");
819       if (k == 0)
820         n_print ("Element Signals:\n");
821       else
822         n_print ("Element Actions:\n");
823     } else {
824       continue;
825     }
826
827     for (l = found_signals; l; l = l->next) {
828       gchar *indent;
829       const gchar *pmark;
830       int indent_len;
831
832       query = (GSignalQuery *) l->data;
833       indent_len = strlen (query->signal_name) +
834           strlen (g_type_name (query->return_type)) + 24;
835
836       if (gtype_needs_ptr_marker (query->return_type)) {
837         pmark = "* ";
838         indent_len += 2;
839       } else {
840         pmark = "";
841       }
842
843       indent = g_new0 (gchar, indent_len + 1);
844       memset (indent, ' ', indent_len);
845
846       n_print ("  \"%s\" :  %s %suser_function (%s* object",
847           query->signal_name, g_type_name (query->return_type), pmark,
848           g_type_name (type));
849
850       for (j = 0; j < query->n_params; j++) {
851         const gchar *type_name, *asterisk;
852
853         type_name = g_type_name (query->param_types[j]);
854         asterisk = gtype_needs_ptr_marker (query->param_types[j]) ? "*" : "";
855
856         g_print (",\n");
857         n_print ("%s%s%s arg%d", indent, type_name, asterisk, j);
858       }
859
860       if (k == 0) {
861         g_print (",\n");
862         n_print ("%sgpointer user_data);\n", indent);
863       } else
864         g_print (");\n");
865
866       g_free (indent);
867     }
868
869     if (found_signals) {
870       g_slist_foreach (found_signals, (GFunc) g_free, NULL);
871       g_slist_free (found_signals);
872     }
873   }
874 }
875
876 static void
877 print_children_info (GstElement * element)
878 {
879   GList *children;
880
881   if (!GST_IS_BIN (element))
882     return;
883
884   children = (GList *) GST_BIN (element)->children;
885   if (children) {
886     n_print ("\n");
887     n_print ("Children:\n");
888   }
889
890   while (children) {
891     n_print ("  %s\n", GST_ELEMENT_NAME (GST_ELEMENT (children->data)));
892     children = g_list_next (children);
893   }
894 }
895
896 static void
897 print_preset_list (GstElement * element)
898 {
899   gchar **presets, **preset;
900
901   if (!GST_IS_PRESET (element))
902     return;
903
904   presets = gst_preset_get_preset_names (GST_PRESET (element));
905   if (presets && *presets) {
906     n_print ("\n");
907     n_print ("Presets:\n");
908     for (preset = presets; *preset; preset++) {
909       n_print ("  \"%s\"\n", *preset);
910     }
911     g_strfreev (presets);
912   }
913 }
914
915 static void
916 print_blacklist (void)
917 {
918   GList *plugins, *cur;
919   gint count = 0;
920
921   g_print ("%s\n", _("Blacklisted files:"));
922
923   plugins = gst_registry_get_plugin_list (gst_registry_get ());
924   for (cur = plugins; cur != NULL; cur = g_list_next (cur)) {
925     GstPlugin *plugin = (GstPlugin *) (cur->data);
926     if (GST_OBJECT_FLAG_IS_SET (plugin, GST_PLUGIN_FLAG_BLACKLISTED)) {
927       g_print ("  %s\n", gst_plugin_get_name (plugin));
928       count++;
929     }
930   }
931
932   g_print ("\n");
933   g_print (_("Total count: "));
934   g_print (ngettext ("%d blacklisted file", "%d blacklisted files", count),
935       count);
936   g_print ("\n");
937   gst_plugin_list_free (plugins);
938 }
939
940 static void
941 print_element_list (gboolean print_all, gchar * ftypes)
942 {
943   int plugincount = 0, featurecount = 0, blacklistcount = 0;
944   GList *plugins, *orig_plugins;
945   gchar **types = NULL;
946
947   if (ftypes) {
948     gint i;
949
950     types = g_strsplit (ftypes, "/", -1);
951     for (i = 0; types[i]; i++)
952       *types[i] = g_ascii_toupper (*types[i]);
953
954   }
955
956
957   orig_plugins = plugins = gst_registry_get_plugin_list (gst_registry_get ());
958   while (plugins) {
959     GList *features, *orig_features;
960     GstPlugin *plugin;
961
962     plugin = (GstPlugin *) (plugins->data);
963     plugins = g_list_next (plugins);
964     plugincount++;
965
966     if (GST_OBJECT_FLAG_IS_SET (plugin, GST_PLUGIN_FLAG_BLACKLISTED)) {
967       blacklistcount++;
968       continue;
969     }
970
971     orig_features = features =
972         gst_registry_get_feature_list_by_plugin (gst_registry_get (),
973         gst_plugin_get_name (plugin));
974     while (features) {
975       GstPluginFeature *feature;
976
977       if (G_UNLIKELY (features->data == NULL))
978         goto next;
979       feature = GST_PLUGIN_FEATURE (features->data);
980       featurecount++;
981
982       if (GST_IS_ELEMENT_FACTORY (feature)) {
983         const gchar *klass;
984         GstElementFactory *factory;
985
986         factory = GST_ELEMENT_FACTORY (feature);
987         if (types) {
988           gint i;
989           gboolean all_found = TRUE;
990
991           klass =
992               gst_element_factory_get_metadata (factory,
993               GST_ELEMENT_METADATA_KLASS);
994           for (i = 0; types[i]; i++) {
995             if (!strstr (klass, types[i])) {
996               all_found = FALSE;
997               break;
998             }
999           }
1000
1001           if (!all_found)
1002             goto next;
1003         }
1004         if (print_all)
1005           print_element_info (factory, TRUE);
1006         else
1007           g_print ("%s:  %s: %s\n", gst_plugin_get_name (plugin),
1008               GST_OBJECT_NAME (factory),
1009               gst_element_factory_get_metadata (factory,
1010                   GST_ELEMENT_METADATA_LONGNAME));
1011       } else if (GST_IS_TYPE_FIND_FACTORY (feature)) {
1012         GstTypeFindFactory *factory;
1013         const gchar *const *extensions;
1014
1015         if (types)
1016           goto next;
1017         factory = GST_TYPE_FIND_FACTORY (feature);
1018         if (!print_all)
1019           g_print ("%s: %s: ", gst_plugin_get_name (plugin),
1020               gst_plugin_feature_get_name (feature));
1021
1022         extensions = gst_type_find_factory_get_extensions (factory);
1023         if (extensions != NULL) {
1024           guint i = 0;
1025
1026           while (extensions[i]) {
1027             if (!print_all)
1028               g_print ("%s%s", i > 0 ? ", " : "", extensions[i]);
1029             i++;
1030           }
1031           if (!print_all)
1032             g_print ("\n");
1033         } else {
1034           if (!print_all)
1035             g_print ("no extensions\n");
1036         }
1037       } else {
1038         if (types)
1039           goto next;
1040         if (!print_all)
1041           n_print ("%s:  %s (%s)\n", gst_plugin_get_name (plugin),
1042               GST_OBJECT_NAME (feature), g_type_name (G_OBJECT_TYPE (feature)));
1043       }
1044
1045     next:
1046       features = g_list_next (features);
1047     }
1048
1049     gst_plugin_feature_list_free (orig_features);
1050   }
1051
1052   gst_plugin_list_free (orig_plugins);
1053   g_strfreev (types);
1054
1055   g_print ("\n");
1056   g_print (_("Total count: "));
1057   g_print (ngettext ("%d plugin", "%d plugins", plugincount), plugincount);
1058   if (blacklistcount) {
1059     g_print (" (");
1060     g_print (ngettext ("%d blacklist entry", "%d blacklist entries",
1061             blacklistcount), blacklistcount);
1062     g_print (" not shown)");
1063   }
1064   g_print (", ");
1065   g_print (ngettext ("%d feature", "%d features", featurecount), featurecount);
1066   g_print ("\n");
1067 }
1068
1069 static void
1070 print_all_uri_handlers (void)
1071 {
1072   GList *plugins, *p, *features, *f;
1073
1074   plugins = gst_registry_get_plugin_list (gst_registry_get ());
1075
1076   for (p = plugins; p; p = p->next) {
1077     GstPlugin *plugin = (GstPlugin *) (p->data);
1078
1079     features =
1080         gst_registry_get_feature_list_by_plugin (gst_registry_get (),
1081         gst_plugin_get_name (plugin));
1082
1083     for (f = features; f; f = f->next) {
1084       GstPluginFeature *feature = GST_PLUGIN_FEATURE (f->data);
1085
1086       if (GST_IS_ELEMENT_FACTORY (feature)) {
1087         GstElementFactory *factory;
1088         GstElement *element;
1089
1090         factory = GST_ELEMENT_FACTORY (gst_plugin_feature_load (feature));
1091         if (!factory) {
1092           g_print ("element plugin %s couldn't be loaded\n",
1093               gst_plugin_get_name (plugin));
1094           continue;
1095         }
1096
1097         element = gst_element_factory_create (factory, NULL);
1098         if (!element) {
1099           g_print ("couldn't construct element for %s for some reason\n",
1100               GST_OBJECT_NAME (factory));
1101           gst_object_unref (factory);
1102           continue;
1103         }
1104
1105         if (GST_IS_URI_HANDLER (element)) {
1106           const gchar *const *uri_protocols;
1107           const gchar *dir;
1108           gchar *joined;
1109
1110           switch (gst_uri_handler_get_uri_type (GST_URI_HANDLER (element))) {
1111             case GST_URI_SRC:
1112               dir = "read";
1113               break;
1114             case GST_URI_SINK:
1115               dir = "write";
1116               break;
1117             default:
1118               dir = "unknown";
1119               break;
1120           }
1121
1122           uri_protocols =
1123               gst_uri_handler_get_protocols (GST_URI_HANDLER (element));
1124           joined = g_strjoinv (", ", (gchar **) uri_protocols);
1125
1126           g_print ("%s (%s, rank %u): %s\n",
1127               gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)), dir,
1128               gst_plugin_feature_get_rank (GST_PLUGIN_FEATURE (factory)),
1129               joined);
1130
1131           g_free (joined);
1132         }
1133
1134         gst_object_unref (element);
1135         gst_object_unref (factory);
1136       }
1137     }
1138
1139     gst_plugin_feature_list_free (features);
1140   }
1141
1142   gst_plugin_list_free (plugins);
1143 }
1144
1145 static void
1146 print_plugin_info (GstPlugin * plugin)
1147 {
1148   const gchar *release_date = gst_plugin_get_release_date_string (plugin);
1149   const gchar *filename = gst_plugin_get_filename (plugin);
1150
1151   n_print ("Plugin Details:\n");
1152   n_print ("  %-25s%s\n", "Name", gst_plugin_get_name (plugin));
1153   n_print ("  %-25s%s\n", "Description", gst_plugin_get_description (plugin));
1154   n_print ("  %-25s%s\n", "Filename", (filename != NULL) ? filename : "(null)");
1155   n_print ("  %-25s%s\n", "Version", gst_plugin_get_version (plugin));
1156   n_print ("  %-25s%s\n", "License", gst_plugin_get_license (plugin));
1157   n_print ("  %-25s%s\n", "Source module", gst_plugin_get_source (plugin));
1158
1159   if (release_date != NULL) {
1160     const gchar *tz = "(UTC)";
1161     gchar *str, *sep;
1162
1163     /* may be: YYYY-MM-DD or YYYY-MM-DDTHH:MMZ */
1164     /* YYYY-MM-DDTHH:MMZ => YYYY-MM-DD HH:MM (UTC) */
1165     str = g_strdup (release_date);
1166     sep = strstr (str, "T");
1167     if (sep != NULL) {
1168       *sep = ' ';
1169       sep = strstr (sep + 1, "Z");
1170       if (sep != NULL)
1171         *sep = ' ';
1172     } else {
1173       tz = "";
1174     }
1175     n_print ("  %-25s%s%s\n", "Source release date", str, tz);
1176     g_free (str);
1177   }
1178   n_print ("  %-25s%s\n", "Binary package", gst_plugin_get_package (plugin));
1179   n_print ("  %-25s%s\n", "Origin URL", gst_plugin_get_origin (plugin));
1180   n_print ("\n");
1181 }
1182
1183 static void
1184 print_plugin_features (GstPlugin * plugin)
1185 {
1186   GList *features, *origlist;
1187   gint num_features = 0;
1188   gint num_elements = 0;
1189   gint num_tracers = 0;
1190   gint num_typefinders = 0;
1191   gint num_devproviders = 0;
1192   gint num_other = 0;
1193
1194   origlist = features =
1195       gst_registry_get_feature_list_by_plugin (gst_registry_get (),
1196       gst_plugin_get_name (plugin));
1197
1198   while (features) {
1199     GstPluginFeature *feature;
1200
1201     feature = GST_PLUGIN_FEATURE (features->data);
1202
1203     if (GST_IS_ELEMENT_FACTORY (feature)) {
1204       GstElementFactory *factory;
1205
1206       factory = GST_ELEMENT_FACTORY (feature);
1207       n_print ("  %s: %s\n", GST_OBJECT_NAME (factory),
1208           gst_element_factory_get_metadata (factory,
1209               GST_ELEMENT_METADATA_LONGNAME));
1210       num_elements++;
1211     } else if (GST_IS_TYPE_FIND_FACTORY (feature)) {
1212       GstTypeFindFactory *factory;
1213       const gchar *const *extensions;
1214
1215       factory = GST_TYPE_FIND_FACTORY (feature);
1216       extensions = gst_type_find_factory_get_extensions (factory);
1217       if (extensions) {
1218         guint i = 0;
1219
1220         g_print ("  %s: %s: ", gst_plugin_get_name (plugin),
1221             gst_plugin_feature_get_name (feature));
1222         while (extensions[i]) {
1223           g_print ("%s%s", i > 0 ? ", " : "", extensions[i]);
1224           i++;
1225         }
1226         g_print ("\n");
1227       } else
1228         g_print ("  %s: %s: no extensions\n", gst_plugin_get_name (plugin),
1229             gst_plugin_feature_get_name (feature));
1230
1231       num_typefinders++;
1232     } else if (GST_IS_DEVICE_PROVIDER_FACTORY (feature)) {
1233       GstDeviceProviderFactory *factory;
1234
1235       factory = GST_DEVICE_PROVIDER_FACTORY (feature);
1236       n_print ("  %s: %s\n", GST_OBJECT_NAME (factory),
1237           gst_device_provider_factory_get_metadata (factory,
1238               GST_ELEMENT_METADATA_LONGNAME));
1239       num_devproviders++;
1240     } else if (GST_IS_TRACER_FACTORY (feature)) {
1241       n_print ("  %s (%s)\n", gst_object_get_name (GST_OBJECT (feature)),
1242           g_type_name (G_OBJECT_TYPE (feature)));
1243       num_tracers++;
1244     } else if (feature) {
1245       n_print ("  %s (%s)\n", gst_object_get_name (GST_OBJECT (feature)),
1246           g_type_name (G_OBJECT_TYPE (feature)));
1247       num_other++;
1248     }
1249     num_features++;
1250     features = g_list_next (features);
1251   }
1252
1253   gst_plugin_feature_list_free (origlist);
1254
1255   n_print ("\n");
1256   n_print ("  %d features:\n", num_features);
1257   if (num_elements > 0)
1258     n_print ("  +-- %d elements\n", num_elements);
1259   if (num_typefinders > 0)
1260     n_print ("  +-- %d typefinders\n", num_typefinders);
1261   if (num_devproviders > 0)
1262     n_print ("  +-- %d device providers\n", num_devproviders);
1263   if (num_tracers > 0)
1264     n_print ("  +-- %d tracers\n", num_tracers);
1265   if (num_other > 0)
1266     n_print ("  +-- %d other objects\n", num_other);
1267
1268   n_print ("\n");
1269 }
1270
1271 static int
1272 print_element_features (const gchar * element_name)
1273 {
1274   GstPluginFeature *feature;
1275
1276   /* FIXME implement other pretty print function for these */
1277   feature = gst_registry_find_feature (gst_registry_get (), element_name,
1278       GST_TYPE_TYPE_FIND_FACTORY);
1279   if (feature) {
1280     n_print ("%s: a typefind function\n", element_name);
1281     return 0;
1282   }
1283   feature = gst_registry_find_feature (gst_registry_get (), element_name,
1284       GST_TYPE_TRACER_FACTORY);
1285   if (feature) {
1286     n_print ("%s: a tracer module\n", element_name);
1287     return 0;
1288   }
1289
1290   return -1;
1291 }
1292
1293 static int
1294 print_element_info (GstElementFactory * factory, gboolean print_names)
1295 {
1296   GstElement *element;
1297   GstPlugin *plugin;
1298   gint maxlevel = 0;
1299
1300   factory =
1301       GST_ELEMENT_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
1302           (factory)));
1303
1304   if (!factory) {
1305     g_print ("element plugin couldn't be loaded\n");
1306     return -1;
1307   }
1308
1309   element = gst_element_factory_create (factory, NULL);
1310   if (!element) {
1311     gst_object_unref (factory);
1312     g_print ("couldn't construct element for some reason\n");
1313     return -1;
1314   }
1315
1316   if (print_names)
1317     _name = g_strdup_printf ("%s: ", GST_OBJECT_NAME (factory));
1318   else
1319     _name = NULL;
1320
1321   print_factory_details_info (factory);
1322
1323   plugin = gst_plugin_feature_get_plugin (GST_PLUGIN_FEATURE (factory));
1324   if (plugin) {
1325     print_plugin_info (plugin);
1326     gst_object_unref (plugin);
1327   }
1328
1329   print_hierarchy (G_OBJECT_TYPE (element), 0, &maxlevel);
1330   print_interfaces (G_OBJECT_TYPE (element));
1331
1332   print_pad_templates_info (element, factory);
1333   print_element_flag_info (element);
1334   print_implementation_info (element);
1335   print_clocking_info (element);
1336   print_uri_handler_info (element);
1337   print_pad_info (element);
1338   print_element_properties_info (element);
1339   print_signal_info (element);
1340   print_children_info (element);
1341   print_preset_list (element);
1342
1343   gst_object_unref (element);
1344   gst_object_unref (factory);
1345   g_free (_name);
1346
1347   return 0;
1348 }
1349
1350
1351 static void
1352 print_plugin_automatic_install_info_codecs (GstElementFactory * factory)
1353 {
1354   GstPadDirection direction;
1355   const gchar *type_name;
1356   const gchar *klass;
1357   const GList *static_templates, *l;
1358   GstCaps *caps = NULL;
1359   guint i, num;
1360
1361   klass =
1362       gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_KLASS);
1363   g_return_if_fail (klass != NULL);
1364
1365   if (strstr (klass, "Demuxer") ||
1366       strstr (klass, "Decoder") ||
1367       strstr (klass, "Depay") || strstr (klass, "Parser")) {
1368     type_name = "decoder";
1369     direction = GST_PAD_SINK;
1370   } else if (strstr (klass, "Muxer") ||
1371       strstr (klass, "Encoder") || strstr (klass, "Pay")) {
1372     type_name = "encoder";
1373     direction = GST_PAD_SRC;
1374   } else {
1375     return;
1376   }
1377
1378   /* decoder/demuxer sink pads should always be static and there should only
1379    * be one, the same applies to encoders/muxers and source pads */
1380   static_templates = gst_element_factory_get_static_pad_templates (factory);
1381   for (l = static_templates; l != NULL; l = l->next) {
1382     GstStaticPadTemplate *tmpl = NULL;
1383
1384     tmpl = (GstStaticPadTemplate *) l->data;
1385     if (tmpl->direction == direction) {
1386       caps = gst_static_pad_template_get_caps (tmpl);
1387       break;
1388     }
1389   }
1390
1391   if (caps == NULL) {
1392     g_printerr ("Couldn't find static pad template for %s '%s'\n",
1393         type_name, GST_OBJECT_NAME (factory));
1394     return;
1395   }
1396
1397   caps = gst_caps_make_writable (caps);
1398   num = gst_caps_get_size (caps);
1399   for (i = 0; i < num; ++i) {
1400     GstStructure *s;
1401     gchar *s_str;
1402
1403     s = gst_caps_get_structure (caps, i);
1404     /* remove fields that are almost always just MIN-MAX of some sort
1405      * in order to make the caps look less messy */
1406     gst_structure_remove_field (s, "pixel-aspect-ratio");
1407     gst_structure_remove_field (s, "framerate");
1408     gst_structure_remove_field (s, "channels");
1409     gst_structure_remove_field (s, "width");
1410     gst_structure_remove_field (s, "height");
1411     gst_structure_remove_field (s, "rate");
1412     gst_structure_remove_field (s, "depth");
1413     gst_structure_remove_field (s, "clock-rate");
1414     s_str = gst_structure_to_string (s);
1415     g_print ("%s-%s\n", type_name, s_str);
1416     g_free (s_str);
1417   }
1418   gst_caps_unref (caps);
1419 }
1420
1421 static void
1422 print_plugin_automatic_install_info_protocols (GstElementFactory * factory)
1423 {
1424   const gchar *const *protocols;
1425
1426   protocols = gst_element_factory_get_uri_protocols (factory);
1427   if (protocols != NULL && *protocols != NULL) {
1428     switch (gst_element_factory_get_uri_type (factory)) {
1429       case GST_URI_SINK:
1430         while (*protocols != NULL) {
1431           g_print ("urisink-%s\n", *protocols);
1432           ++protocols;
1433         }
1434         break;
1435       case GST_URI_SRC:
1436         while (*protocols != NULL) {
1437           g_print ("urisource-%s\n", *protocols);
1438           ++protocols;
1439         }
1440         break;
1441       default:
1442         break;
1443     }
1444   }
1445 }
1446
1447 static void
1448 print_plugin_automatic_install_info (GstPlugin * plugin)
1449 {
1450   GList *features, *l;
1451
1452   /* not interested in typefind factories, only element factories */
1453   features = gst_registry_get_feature_list (gst_registry_get (),
1454       GST_TYPE_ELEMENT_FACTORY);
1455
1456   for (l = features; l != NULL; l = l->next) {
1457     GstPluginFeature *feature;
1458     GstPlugin *feature_plugin;
1459
1460     feature = GST_PLUGIN_FEATURE (l->data);
1461
1462     /* only interested in the ones that are in the plugin we just loaded */
1463     feature_plugin = gst_plugin_feature_get_plugin (feature);
1464     if (feature_plugin == plugin) {
1465       GstElementFactory *factory;
1466
1467       g_print ("element-%s\n", gst_plugin_feature_get_name (feature));
1468
1469       factory = GST_ELEMENT_FACTORY (feature);
1470       print_plugin_automatic_install_info_protocols (factory);
1471       print_plugin_automatic_install_info_codecs (factory);
1472     }
1473     if (feature_plugin)
1474       gst_object_unref (feature_plugin);
1475   }
1476
1477   g_list_foreach (features, (GFunc) gst_object_unref, NULL);
1478   g_list_free (features);
1479 }
1480
1481 static void
1482 print_all_plugin_automatic_install_info (void)
1483 {
1484   GList *plugins, *orig_plugins;
1485
1486   orig_plugins = plugins = gst_registry_get_plugin_list (gst_registry_get ());
1487   while (plugins) {
1488     GstPlugin *plugin;
1489
1490     plugin = (GstPlugin *) (plugins->data);
1491     plugins = g_list_next (plugins);
1492
1493     print_plugin_automatic_install_info (plugin);
1494   }
1495   gst_plugin_list_free (orig_plugins);
1496 }
1497
1498 int
1499 main (int argc, char *argv[])
1500 {
1501   gboolean print_all = FALSE;
1502   gboolean do_print_blacklist = FALSE;
1503   gboolean plugin_name = FALSE;
1504   gboolean print_aii = FALSE;
1505   gboolean uri_handlers = FALSE;
1506   gboolean check_exists = FALSE;
1507   gchar *min_version = NULL;
1508   guint minver_maj = GST_VERSION_MAJOR;
1509   guint minver_min = GST_VERSION_MINOR;
1510   guint minver_micro = 0;
1511   gchar *types = NULL;
1512 #ifndef GST_DISABLE_OPTION_PARSING
1513   GOptionEntry options[] = {
1514     {"print-all", 'a', 0, G_OPTION_ARG_NONE, &print_all,
1515         N_("Print all elements"), NULL},
1516     {"print-blacklist", 'b', 0, G_OPTION_ARG_NONE, &do_print_blacklist,
1517         N_("Print list of blacklisted files"), NULL},
1518     {"print-plugin-auto-install-info", '\0', 0, G_OPTION_ARG_NONE, &print_aii,
1519         N_("Print a machine-parsable list of features the specified plugin "
1520               "or all plugins provide.\n                                       "
1521               "Useful in connection with external automatic plugin "
1522               "installation mechanisms"), NULL},
1523     {"plugin", '\0', 0, G_OPTION_ARG_NONE, &plugin_name,
1524         N_("List the plugin contents"), NULL},
1525     {"types", 't', 0, G_OPTION_ARG_STRING, &types,
1526         N_("A slashes ('/') separated list of types of elements (also known "
1527               "as klass) to list. (unordered)"), NULL},
1528     {"exists", '\0', 0, G_OPTION_ARG_NONE, &check_exists,
1529         N_("Check if the specified element or plugin exists"), NULL},
1530     {"atleast-version", '\0', 0, G_OPTION_ARG_STRING, &min_version,
1531         N_
1532           ("When checking if an element or plugin exists, also check that its "
1533               "version is at least the version specified"), NULL},
1534     {"uri-handlers", 'u', 0, G_OPTION_ARG_NONE, &uri_handlers,
1535           N_
1536           ("Print supported URI schemes, with the elements that implement them"),
1537         NULL},
1538     GST_TOOLS_GOPTION_VERSION,
1539     {NULL}
1540   };
1541   GOptionContext *ctx;
1542   GError *err = NULL;
1543 #endif
1544
1545   setlocale (LC_ALL, "");
1546
1547 #ifdef ENABLE_NLS
1548   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1549   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1550   textdomain (GETTEXT_PACKAGE);
1551 #endif
1552
1553   /* avoid glib warnings when inspecting deprecated properties */
1554   g_setenv ("G_ENABLE_DIAGNOSTIC", "0", FALSE);
1555
1556   g_set_prgname ("gst-inspect-" GST_API_VERSION);
1557
1558 #ifndef GST_DISABLE_OPTION_PARSING
1559   ctx = g_option_context_new ("[ELEMENT-NAME | PLUGIN-NAME]");
1560   g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
1561   g_option_context_add_group (ctx, gst_init_get_option_group ());
1562   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
1563     g_printerr ("Error initializing: %s\n", err->message);
1564     g_clear_error (&err);
1565     g_option_context_free (ctx);
1566     return -1;
1567   }
1568   g_option_context_free (ctx);
1569 #else
1570   gst_init (&argc, &argv);
1571 #endif
1572
1573   gst_tools_print_version ();
1574
1575   if (print_all && argc > 1) {
1576     g_printerr ("-a requires no extra arguments\n");
1577     return -1;
1578   }
1579
1580   if (uri_handlers && argc > 1) {
1581     g_printerr ("-u requires no extra arguments\n");
1582     return -1;
1583   }
1584
1585   /* --atleast-version implies --exists */
1586   if (min_version != NULL) {
1587     if (sscanf (min_version, "%u.%u.%u", &minver_maj, &minver_min,
1588             &minver_micro) < 2) {
1589       g_printerr ("Can't parse version '%s' passed to --atleast-version\n",
1590           min_version);
1591       return -1;
1592     }
1593     check_exists = TRUE;
1594   }
1595
1596   if (check_exists) {
1597     int exit_code;
1598
1599     if (argc == 1) {
1600       g_printerr ("--exists requires an extra command line argument\n");
1601       exit_code = -1;
1602     } else {
1603       if (!plugin_name) {
1604         GstPluginFeature *feature;
1605
1606         feature = gst_registry_lookup_feature (gst_registry_get (), argv[1]);
1607         if (feature != NULL && gst_plugin_feature_check_version (feature,
1608                 minver_maj, minver_min, minver_micro)) {
1609           exit_code = 0;
1610         } else {
1611           exit_code = 1;
1612         }
1613
1614         if (feature)
1615           gst_object_unref (feature);
1616       } else {
1617         /* FIXME: support checking for plugins too */
1618         g_printerr ("Checking for plugins is not supported yet\n");
1619         exit_code = -1;
1620       }
1621     }
1622     return exit_code;
1623   }
1624
1625   /* if no arguments, print out list of elements */
1626   if (uri_handlers) {
1627     print_all_uri_handlers ();
1628   } else if (argc == 1 || print_all) {
1629     if (do_print_blacklist)
1630       print_blacklist ();
1631     else {
1632       if (print_aii)
1633         print_all_plugin_automatic_install_info ();
1634       else
1635         print_element_list (print_all, types);
1636     }
1637   } else {
1638     /* else we try to get a factory */
1639     GstElementFactory *factory;
1640     GstPlugin *plugin;
1641     const char *arg = argv[argc - 1];
1642     int retval;
1643
1644     if (!plugin_name) {
1645       factory = gst_element_factory_find (arg);
1646
1647       /* if there's a factory, print out the info */
1648       if (factory) {
1649         retval = print_element_info (factory, print_all);
1650         gst_object_unref (factory);
1651       } else {
1652         retval = print_element_features (arg);
1653       }
1654     } else {
1655       retval = -1;
1656     }
1657
1658     /* otherwise check if it's a plugin */
1659     if (retval) {
1660       plugin = gst_registry_find_plugin (gst_registry_get (), arg);
1661
1662       /* if there is such a plugin, print out info */
1663       if (plugin) {
1664         if (print_aii) {
1665           print_plugin_automatic_install_info (plugin);
1666         } else {
1667           print_plugin_info (plugin);
1668           print_plugin_features (plugin);
1669         }
1670       } else {
1671         GError *error = NULL;
1672
1673         if (g_file_test (arg, G_FILE_TEST_EXISTS)) {
1674           plugin = gst_plugin_load_file (arg, &error);
1675
1676           if (plugin) {
1677             if (print_aii) {
1678               print_plugin_automatic_install_info (plugin);
1679             } else {
1680               print_plugin_info (plugin);
1681               print_plugin_features (plugin);
1682             }
1683           } else {
1684             g_printerr (_("Could not load plugin file: %s\n"), error->message);
1685             g_clear_error (&error);
1686             return -1;
1687           }
1688         } else {
1689           g_printerr (_("No such element or plugin '%s'\n"), arg);
1690           return -1;
1691         }
1692       }
1693     }
1694   }
1695
1696   return 0;
1697 }