gst-inspect: Flush stdout before closing stdout FD
[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 #define DEFAULT_PAGER "less"
33
34 #include "tools.h"
35 #include <gst/gst_private.h>    /* for internal Factories */
36
37 #include <string.h>
38 #include <locale.h>
39 #include <glib/gprintf.h>
40 #ifdef G_OS_UNIX
41 #   include <unistd.h>
42 #   include <sys/wait.h>
43 #endif
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: %s: ", gst_plugin_get_name (plugin),
1294             gst_plugin_feature_get_name (feature));
1295         while (extensions[i]) {
1296           g_print ("%s%s", i > 0 ? ", " : "", extensions[i]);
1297           i++;
1298         }
1299         g_print ("\n");
1300       } else
1301         g_print ("  %s: %s: no extensions\n", gst_plugin_get_name (plugin),
1302             gst_plugin_feature_get_name (feature));
1303
1304       num_typefinders++;
1305     } else if (GST_IS_DEVICE_PROVIDER_FACTORY (feature)) {
1306       GstDeviceProviderFactory *factory;
1307
1308       factory = GST_DEVICE_PROVIDER_FACTORY (feature);
1309       n_print ("  %s: %s\n", GST_OBJECT_NAME (factory),
1310           gst_device_provider_factory_get_metadata (factory,
1311               GST_ELEMENT_METADATA_LONGNAME));
1312       num_devproviders++;
1313     } else if (GST_IS_TRACER_FACTORY (feature)) {
1314       n_print ("  %s (%s)\n", gst_object_get_name (GST_OBJECT (feature)),
1315           g_type_name (G_OBJECT_TYPE (feature)));
1316       num_tracers++;
1317     } else if (feature) {
1318       n_print ("  %s (%s)\n", gst_object_get_name (GST_OBJECT (feature)),
1319           g_type_name (G_OBJECT_TYPE (feature)));
1320       num_other++;
1321     }
1322     num_features++;
1323     features = g_list_next (features);
1324   }
1325
1326   gst_plugin_feature_list_free (origlist);
1327
1328   n_print ("\n");
1329   n_print ("  %d features:\n", num_features);
1330   if (num_elements > 0)
1331     n_print ("  +-- %d elements\n", num_elements);
1332   if (num_typefinders > 0)
1333     n_print ("  +-- %d typefinders\n", num_typefinders);
1334   if (num_devproviders > 0)
1335     n_print ("  +-- %d device providers\n", num_devproviders);
1336   if (num_tracers > 0)
1337     n_print ("  +-- %d tracers\n", num_tracers);
1338   if (num_other > 0)
1339     n_print ("  +-- %d other objects\n", num_other);
1340
1341   n_print ("\n");
1342 }
1343
1344 static int
1345 print_feature_info (const gchar * feature_name, gboolean print_all)
1346 {
1347   GstPluginFeature *feature;
1348   GstRegistry *registry = gst_registry_get ();
1349   int ret;
1350
1351   if ((feature = gst_registry_find_feature (registry, feature_name,
1352               GST_TYPE_ELEMENT_FACTORY))) {
1353     ret = print_element_info (feature, print_all);
1354     goto handled;
1355   }
1356   if ((feature = gst_registry_find_feature (registry, feature_name,
1357               GST_TYPE_TYPE_FIND_FACTORY))) {
1358     ret = print_typefind_info (feature, print_all);
1359     goto handled;
1360   }
1361   if ((feature = gst_registry_find_feature (registry, feature_name,
1362               GST_TYPE_TRACER_FACTORY))) {
1363     ret = print_tracer_info (feature, print_all);
1364     goto handled;
1365   }
1366
1367   /* TODO: handle DEVICE_PROVIDER_FACTORY */
1368
1369   return -1;
1370
1371 handled:
1372   gst_object_unref (feature);
1373   return ret;
1374 }
1375
1376 static int
1377 print_element_info (GstPluginFeature * feature, gboolean print_names)
1378 {
1379   GstElementFactory *factory;
1380   GstElement *element;
1381   GstPlugin *plugin;
1382   gint maxlevel = 0;
1383
1384   factory = GST_ELEMENT_FACTORY (gst_plugin_feature_load (feature));
1385   if (!factory) {
1386     g_print ("element plugin couldn't be loaded\n");
1387     return -1;
1388   }
1389
1390   element = gst_element_factory_create (factory, NULL);
1391   if (!element) {
1392     gst_object_unref (factory);
1393     g_print ("couldn't construct element for some reason\n");
1394     return -1;
1395   }
1396
1397   if (print_names)
1398     _name = g_strdup_printf ("%s: ", GST_OBJECT_NAME (factory));
1399   else
1400     _name = NULL;
1401
1402   print_factory_details_info (factory);
1403
1404   plugin = gst_plugin_feature_get_plugin (GST_PLUGIN_FEATURE (factory));
1405   if (plugin) {
1406     print_plugin_info (plugin);
1407     gst_object_unref (plugin);
1408   }
1409
1410   print_hierarchy (G_OBJECT_TYPE (element), 0, &maxlevel);
1411   print_interfaces (G_OBJECT_TYPE (element));
1412
1413   print_pad_templates_info (element, factory);
1414   print_clocking_info (element);
1415   print_uri_handler_info (element);
1416   print_pad_info (element);
1417   print_element_properties_info (element);
1418   print_signal_info (element);
1419   print_children_info (element);
1420   print_preset_list (element);
1421
1422   gst_object_unref (element);
1423   gst_object_unref (factory);
1424   g_free (_name);
1425   return 0;
1426 }
1427
1428 static int
1429 print_typefind_info (GstPluginFeature * feature, gboolean print_names)
1430 {
1431   GstTypeFindFactory *factory;
1432   GstPlugin *plugin;
1433   GstCaps *caps;
1434   GstRank rank;
1435   char s[20];
1436   const gchar *const *extensions;
1437
1438   factory = GST_TYPE_FIND_FACTORY (gst_plugin_feature_load (feature));
1439   if (!factory) {
1440     g_print ("typefind plugin couldn't be loaded\n");
1441     return -1;
1442   }
1443
1444   if (print_names)
1445     _name = g_strdup_printf ("%s: ", GST_OBJECT_NAME (factory));
1446   else
1447     _name = NULL;
1448
1449   n_print ("Factory Details:\n");
1450   rank = gst_plugin_feature_get_rank (feature);
1451   n_print ("  %-25s%s (%d)\n", "Rank", get_rank_name (s, rank), rank);
1452   n_print ("  %-25s%s\n", "Name", GST_OBJECT_NAME (factory));
1453   caps = gst_type_find_factory_get_caps (factory);
1454   if (caps) {
1455     gchar *caps_str = gst_caps_to_string (factory->caps);
1456
1457     n_print ("  %-25s%s\n", "Caps", caps_str);
1458     g_free (caps_str);
1459   }
1460   extensions = gst_type_find_factory_get_extensions (factory);
1461   if (extensions) {
1462     n_print ("  %-25s", "Extensions");
1463     print_typefind_extensions (extensions);
1464     n_print ("\n");
1465   }
1466   n_print ("\n");
1467
1468   plugin = gst_plugin_feature_get_plugin (GST_PLUGIN_FEATURE (factory));
1469   if (plugin) {
1470     print_plugin_info (plugin);
1471     gst_object_unref (plugin);
1472   }
1473
1474   gst_object_unref (factory);
1475   g_free (_name);
1476   return 0;
1477 }
1478
1479 static int
1480 print_tracer_info (GstPluginFeature * feature, gboolean print_names)
1481 {
1482   GstTracerFactory *factory;
1483   GstTracer *tracer;
1484   GstPlugin *plugin;
1485   gint maxlevel = 0;
1486
1487   factory = GST_TRACER_FACTORY (gst_plugin_feature_load (feature));
1488   if (!factory) {
1489     g_print ("tracer plugin couldn't be loaded\n");
1490     return -1;
1491   }
1492
1493   tracer = (GstTracer *) g_object_new (factory->type, NULL);
1494   if (!tracer) {
1495     gst_object_unref (factory);
1496     g_print ("couldn't construct tracer for some reason\n");
1497     return -1;
1498   }
1499
1500   if (print_names)
1501     _name = g_strdup_printf ("%s: ", GST_OBJECT_NAME (factory));
1502   else
1503     _name = NULL;
1504
1505   n_print ("Factory Details:\n");
1506   n_print ("  %-25s%s\n", "Name", GST_OBJECT_NAME (factory));
1507   n_print ("\n");
1508
1509   plugin = gst_plugin_feature_get_plugin (GST_PLUGIN_FEATURE (factory));
1510   if (plugin) {
1511     print_plugin_info (plugin);
1512     gst_object_unref (plugin);
1513   }
1514
1515   print_hierarchy (G_OBJECT_TYPE (tracer), 0, &maxlevel);
1516   print_interfaces (G_OBJECT_TYPE (tracer));
1517
1518   /* TODO: list what hooks it registers
1519    * - the data is available in gsttracerutils, we need to iterate the
1520    *   _priv_tracers hashtable for each probe and then check the list of hooks
1521    *  for each probe whether hook->tracer == tracer :/
1522    */
1523
1524   /* TODO: list what records it emits
1525    * - in class_init tracers can create GstTracerRecord instances
1526    * - those only get logged right now and there is no association with the
1527    *   tracer that created them
1528    * - we'd need to add them to GstTracerFactory
1529    *   gst_tracer_class_add_record (klass, record);
1530    *   - needs work in gstregistrychunks to (de)serialize specs
1531    *   - gst_tracer_register() would need to iterate the list of records and
1532    *     copy the record->spec into the factory
1533    */
1534
1535   gst_object_unref (tracer);
1536   gst_object_unref (factory);
1537   g_free (_name);
1538   return 0;
1539 }
1540
1541 static void
1542 print_plugin_automatic_install_info_codecs (GstElementFactory * factory)
1543 {
1544   GstPadDirection direction;
1545   const gchar *type_name;
1546   const gchar *klass;
1547   const GList *static_templates, *l;
1548   GstCaps *caps = NULL;
1549   guint i, num;
1550
1551   klass =
1552       gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_KLASS);
1553   g_return_if_fail (klass != NULL);
1554
1555   if (strstr (klass, "Demuxer") ||
1556       strstr (klass, "Decoder") ||
1557       strstr (klass, "Depay") || strstr (klass, "Parser")) {
1558     type_name = "decoder";
1559     direction = GST_PAD_SINK;
1560   } else if (strstr (klass, "Muxer") ||
1561       strstr (klass, "Encoder") || strstr (klass, "Pay")) {
1562     type_name = "encoder";
1563     direction = GST_PAD_SRC;
1564   } else {
1565     return;
1566   }
1567
1568   /* decoder/demuxer sink pads should always be static and there should only
1569    * be one, the same applies to encoders/muxers and source pads */
1570   static_templates = gst_element_factory_get_static_pad_templates (factory);
1571   for (l = static_templates; l != NULL; l = l->next) {
1572     GstStaticPadTemplate *tmpl = NULL;
1573
1574     tmpl = (GstStaticPadTemplate *) l->data;
1575     if (tmpl->direction == direction) {
1576       caps = gst_static_pad_template_get_caps (tmpl);
1577       break;
1578     }
1579   }
1580
1581   if (caps == NULL) {
1582     g_printerr ("Couldn't find static pad template for %s '%s'\n",
1583         type_name, GST_OBJECT_NAME (factory));
1584     return;
1585   }
1586
1587   caps = gst_caps_make_writable (caps);
1588   num = gst_caps_get_size (caps);
1589   for (i = 0; i < num; ++i) {
1590     GstStructure *s;
1591     gchar *s_str;
1592
1593     s = gst_caps_get_structure (caps, i);
1594     /* remove fields that are almost always just MIN-MAX of some sort
1595      * in order to make the caps look less messy */
1596     gst_structure_remove_field (s, "pixel-aspect-ratio");
1597     gst_structure_remove_field (s, "framerate");
1598     gst_structure_remove_field (s, "channels");
1599     gst_structure_remove_field (s, "width");
1600     gst_structure_remove_field (s, "height");
1601     gst_structure_remove_field (s, "rate");
1602     gst_structure_remove_field (s, "depth");
1603     gst_structure_remove_field (s, "clock-rate");
1604     s_str = gst_structure_to_string (s);
1605     g_print ("%s-%s\n", type_name, s_str);
1606     g_free (s_str);
1607   }
1608   gst_caps_unref (caps);
1609 }
1610
1611 static void
1612 print_plugin_automatic_install_info_protocols (GstElementFactory * factory)
1613 {
1614   const gchar *const *protocols;
1615
1616   protocols = gst_element_factory_get_uri_protocols (factory);
1617   if (protocols != NULL && *protocols != NULL) {
1618     switch (gst_element_factory_get_uri_type (factory)) {
1619       case GST_URI_SINK:
1620         while (*protocols != NULL) {
1621           g_print ("urisink-%s\n", *protocols);
1622           ++protocols;
1623         }
1624         break;
1625       case GST_URI_SRC:
1626         while (*protocols != NULL) {
1627           g_print ("urisource-%s\n", *protocols);
1628           ++protocols;
1629         }
1630         break;
1631       default:
1632         break;
1633     }
1634   }
1635 }
1636
1637 static void
1638 print_plugin_automatic_install_info (GstPlugin * plugin)
1639 {
1640   GList *features, *l;
1641
1642   /* not interested in typefind factories, only element factories */
1643   features = gst_registry_get_feature_list (gst_registry_get (),
1644       GST_TYPE_ELEMENT_FACTORY);
1645
1646   for (l = features; l != NULL; l = l->next) {
1647     GstPluginFeature *feature;
1648     GstPlugin *feature_plugin;
1649
1650     feature = GST_PLUGIN_FEATURE (l->data);
1651
1652     /* only interested in the ones that are in the plugin we just loaded */
1653     feature_plugin = gst_plugin_feature_get_plugin (feature);
1654     if (feature_plugin == plugin) {
1655       GstElementFactory *factory;
1656
1657       g_print ("element-%s\n", gst_plugin_feature_get_name (feature));
1658
1659       factory = GST_ELEMENT_FACTORY (feature);
1660       print_plugin_automatic_install_info_protocols (factory);
1661       print_plugin_automatic_install_info_codecs (factory);
1662     }
1663     if (feature_plugin)
1664       gst_object_unref (feature_plugin);
1665   }
1666
1667   g_list_foreach (features, (GFunc) gst_object_unref, NULL);
1668   g_list_free (features);
1669 }
1670
1671 static void
1672 print_all_plugin_automatic_install_info (void)
1673 {
1674   GList *plugins, *orig_plugins;
1675
1676   orig_plugins = plugins = gst_registry_get_plugin_list (gst_registry_get ());
1677   while (plugins) {
1678     GstPlugin *plugin;
1679
1680     plugin = (GstPlugin *) (plugins->data);
1681     plugins = g_list_next (plugins);
1682
1683     print_plugin_automatic_install_info (plugin);
1684   }
1685   gst_plugin_list_free (orig_plugins);
1686 }
1687
1688 #ifdef G_OS_UNIX
1689 static void
1690 redirect_stdout (void)
1691 {
1692   int pipefd[2];
1693   pid_t child_id;
1694
1695   if (pipe (pipefd) == -1) {
1696     g_printerr (_("Error creating pipe: %s\n"), g_strerror (errno));
1697     exit (-1);
1698   }
1699
1700   child_id = fork ();
1701   if (child_id == -1) {
1702     g_printerr (_("Error forking: %s\n"), g_strerror (errno));
1703     exit (-1);
1704   }
1705
1706   if (child_id == 0) {
1707     char **argv;
1708     const char *pager;
1709     int ret;
1710
1711     pager = g_getenv ("PAGER");
1712     if (pager == NULL)
1713       pager = DEFAULT_PAGER;
1714     argv = g_strsplit (pager, " ", 0);
1715
1716     /* child process */
1717     close (pipefd[1]);
1718     dup2 (pipefd[0], STDIN_FILENO);
1719     close (pipefd[0]);
1720
1721     ret = execvp (argv[0], argv);
1722     g_strfreev (argv);
1723     if (ret == -1) {
1724       /* No less? Let's just dump everything to stdout then */
1725       char buffer[1024];
1726
1727       do {
1728         int bytes_written;
1729
1730         if ((ret = read (STDIN_FILENO, buffer, sizeof (buffer))) == -1) {
1731           if (errno == EINTR || errno == EAGAIN) {
1732             continue;
1733           }
1734
1735           g_printerr (_("Error reading from console: %s\n"),
1736               g_strerror (errno));
1737           exit (-1);
1738         }
1739
1740         do {
1741           bytes_written = write (STDOUT_FILENO, buffer, ret);
1742         } while (bytes_written == -1 && (errno == EINTR || errno == EAGAIN));
1743
1744         if (bytes_written < 0) {
1745           g_printerr (_("Error writing to console: %s\n"), g_strerror (errno));
1746           exit (-1);
1747         }
1748       } while (ret > 0);
1749
1750       exit (0);
1751     }
1752   } else {
1753     close (pipefd[0]);
1754     dup2 (pipefd[1], STDOUT_FILENO);
1755     close (pipefd[1]);
1756     close (STDIN_FILENO);
1757   }
1758 }
1759 #endif
1760
1761 int
1762 main (int argc, char *argv[])
1763 {
1764   gboolean print_all = FALSE;
1765   gboolean do_print_blacklist = FALSE;
1766   gboolean plugin_name = FALSE;
1767   gboolean print_aii = FALSE;
1768   gboolean uri_handlers = FALSE;
1769   gboolean check_exists = FALSE;
1770   gchar *min_version = NULL;
1771   guint minver_maj = GST_VERSION_MAJOR;
1772   guint minver_min = GST_VERSION_MINOR;
1773   guint minver_micro = 0;
1774   gchar *types = NULL;
1775 #ifndef GST_DISABLE_OPTION_PARSING
1776   GOptionEntry options[] = {
1777     {"print-all", 'a', 0, G_OPTION_ARG_NONE, &print_all,
1778         N_("Print all elements"), NULL},
1779     {"print-blacklist", 'b', 0, G_OPTION_ARG_NONE, &do_print_blacklist,
1780         N_("Print list of blacklisted files"), NULL},
1781     {"print-plugin-auto-install-info", '\0', 0, G_OPTION_ARG_NONE, &print_aii,
1782         N_("Print a machine-parsable list of features the specified plugin "
1783               "or all plugins provide.\n                                       "
1784               "Useful in connection with external automatic plugin "
1785               "installation mechanisms"), NULL},
1786     {"plugin", '\0', 0, G_OPTION_ARG_NONE, &plugin_name,
1787         N_("List the plugin contents"), NULL},
1788     {"types", 't', 0, G_OPTION_ARG_STRING, &types,
1789         N_("A slashes ('/') separated list of types of elements (also known "
1790               "as klass) to list. (unordered)"), NULL},
1791     {"exists", '\0', 0, G_OPTION_ARG_NONE, &check_exists,
1792         N_("Check if the specified element or plugin exists"), NULL},
1793     {"atleast-version", '\0', 0, G_OPTION_ARG_STRING, &min_version,
1794         N_
1795           ("When checking if an element or plugin exists, also check that its "
1796               "version is at least the version specified"), NULL},
1797     {"uri-handlers", 'u', 0, G_OPTION_ARG_NONE, &uri_handlers,
1798           N_
1799           ("Print supported URI schemes, with the elements that implement them"),
1800         NULL},
1801     GST_TOOLS_GOPTION_VERSION,
1802     {NULL}
1803   };
1804   GOptionContext *ctx;
1805   GError *err = NULL;
1806 #endif
1807
1808   setlocale (LC_ALL, "");
1809
1810 #ifdef ENABLE_NLS
1811   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1812   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1813   textdomain (GETTEXT_PACKAGE);
1814 #endif
1815
1816   /* avoid glib warnings when inspecting deprecated properties */
1817   g_setenv ("G_ENABLE_DIAGNOSTIC", "0", FALSE);
1818
1819   g_set_prgname ("gst-inspect-" GST_API_VERSION);
1820
1821 #ifndef GST_DISABLE_OPTION_PARSING
1822   ctx = g_option_context_new ("[ELEMENT-NAME | PLUGIN-NAME]");
1823   g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
1824   g_option_context_add_group (ctx, gst_init_get_option_group ());
1825   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
1826     g_printerr ("Error initializing: %s\n", err->message);
1827     g_clear_error (&err);
1828     g_option_context_free (ctx);
1829     return -1;
1830   }
1831   g_option_context_free (ctx);
1832 #else
1833   gst_init (&argc, &argv);
1834 #endif
1835
1836 #ifdef G_OS_UNIX
1837   if (isatty (STDOUT_FILENO)) {
1838     redirect_stdout ();
1839   }
1840 #endif
1841
1842   gst_tools_print_version ();
1843
1844   if (print_all && argc > 1) {
1845     g_printerr ("-a requires no extra arguments\n");
1846     return -1;
1847   }
1848
1849   if (uri_handlers && argc > 1) {
1850     g_printerr ("-u requires no extra arguments\n");
1851     return -1;
1852   }
1853
1854   /* --atleast-version implies --exists */
1855   if (min_version != NULL) {
1856     if (sscanf (min_version, "%u.%u.%u", &minver_maj, &minver_min,
1857             &minver_micro) < 2) {
1858       g_printerr ("Can't parse version '%s' passed to --atleast-version\n",
1859           min_version);
1860       return -1;
1861     }
1862     check_exists = TRUE;
1863   }
1864
1865   if (check_exists) {
1866     int exit_code;
1867
1868     if (argc == 1) {
1869       g_printerr ("--exists requires an extra command line argument\n");
1870       exit_code = -1;
1871     } else {
1872       if (!plugin_name) {
1873         GstPluginFeature *feature;
1874
1875         feature = gst_registry_lookup_feature (gst_registry_get (), argv[1]);
1876         if (feature != NULL && gst_plugin_feature_check_version (feature,
1877                 minver_maj, minver_min, minver_micro)) {
1878           exit_code = 0;
1879         } else {
1880           exit_code = 1;
1881         }
1882
1883         if (feature)
1884           gst_object_unref (feature);
1885       } else {
1886         /* FIXME: support checking for plugins too */
1887         g_printerr ("Checking for plugins is not supported yet\n");
1888         exit_code = -1;
1889       }
1890     }
1891     return exit_code;
1892   }
1893
1894   /* if no arguments, print out list of elements */
1895   if (uri_handlers) {
1896     print_all_uri_handlers ();
1897   } else if (argc == 1 || print_all) {
1898     if (do_print_blacklist)
1899       print_blacklist ();
1900     else {
1901       if (print_aii)
1902         print_all_plugin_automatic_install_info ();
1903       else
1904         print_element_list (print_all, types);
1905     }
1906   } else {
1907     /* else we try to get a factory */
1908     const char *arg = argv[argc - 1];
1909     int retval = -1;
1910
1911     if (!plugin_name) {
1912       retval = print_feature_info (arg, print_all);
1913     }
1914
1915     /* otherwise check if it's a plugin */
1916     if (retval) {
1917       GstPlugin *plugin = gst_registry_find_plugin (gst_registry_get (), arg);
1918
1919       /* if there is such a plugin, print out info */
1920       if (plugin) {
1921         if (print_aii) {
1922           print_plugin_automatic_install_info (plugin);
1923         } else {
1924           print_plugin_info (plugin);
1925           print_plugin_features (plugin);
1926         }
1927       } else {
1928         GError *error = NULL;
1929
1930         if (g_file_test (arg, G_FILE_TEST_EXISTS)) {
1931           plugin = gst_plugin_load_file (arg, &error);
1932
1933           if (plugin) {
1934             if (print_aii) {
1935               print_plugin_automatic_install_info (plugin);
1936             } else {
1937               print_plugin_info (plugin);
1938               print_plugin_features (plugin);
1939             }
1940           } else {
1941             g_printerr (_("Could not load plugin file: %s\n"), error->message);
1942             g_clear_error (&error);
1943             return -1;
1944           }
1945         } else {
1946           g_printerr (_("No such element or plugin '%s'\n"), arg);
1947           return -1;
1948         }
1949       }
1950     }
1951   }
1952
1953 #ifdef G_OS_UNIX
1954   fflush (stdout);
1955   /* So that the pipe we create in redirect_stdout() is closed */
1956   close (STDOUT_FILENO);
1957   wait (NULL);
1958 #endif
1959
1960   return 0;
1961 }