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