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