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