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