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