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