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