2 * Copyright (C) 2007 Stefan Kost <ensonic@users.sf.net>
4 * gstdebugutils.c: debugging and analysis utillities
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 * edge [ constraint=false ];
23 * this creates strange graphs ("minlen=0" is better)
24 * try puting src/sink ghostpads for each bin into invisible clusters
26 * for more compact nodes, try
27 * - changing node-shape from box into record
28 * - use labels like : element [ label="{element | <src> src | <sink> sink}"]
29 * - point to record-connectors : element1:src -> element2:sink
32 #include "gst_private.h"
33 #include "gstdebugutils.h"
35 #ifndef GST_DISABLE_GST_DEBUG
44 #include "gstobject.h"
45 #include "gstghostpad.h"
50 /*** PIPELINE GRAPHS **********************************************************/
52 const gchar *priv_gst_dump_dot_dir; /* NULL *//* set from gst.c */
54 extern GstClockTime _priv_gst_info_start_time;
57 debug_dump_make_object_name (GstObject * element)
59 return g_strcanon (g_strdup_printf ("%s_%p", GST_OBJECT_NAME (element),
60 element), G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "_", '_');
64 debug_dump_get_element_state (GstElement * element)
66 gchar *state_name = NULL;
67 const gchar *state_icons = "~0-=>";
68 GstState state = 0, pending = 0;
70 gst_element_get_state (element, &state, &pending, 0);
71 if (pending == GST_STATE_VOID_PENDING) {
72 state_name = g_strdup_printf ("\\n[%c]", state_icons[state]);
74 state_name = g_strdup_printf ("\\n[%c] -> [%c]", state_icons[state],
75 state_icons[pending]);
81 debug_dump_get_element_params (GstElement * element)
83 gchar *param_name = NULL;
84 GParamSpec **properties, *property;
85 GValue value = { 0, };
86 guint i, number_of_properties;
87 gchar *tmp, *value_str;
89 /* get paramspecs and show non-default properties */
91 g_object_class_list_properties (G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS
92 (element)), &number_of_properties);
94 for (i = 0; i < number_of_properties; i++) {
95 property = properties[i];
97 /* ski some properties */
98 if (!(property->flags & G_PARAM_READABLE))
100 if (!strcmp (property->name, "name"))
103 g_value_init (&value, property->value_type);
104 g_object_get_property (G_OBJECT (element), property->name, &value);
105 if (!(g_param_value_defaults (property, &value))) {
106 tmp = g_strdup_value_contents (&value);
107 value_str = g_strescape (tmp, NULL);
111 param_name = g_strdup_printf ("%s\\n%s=%s",
112 tmp, property->name, value_str);
115 param_name = g_strdup_printf ("\\n%s=%s", property->name, value_str);
119 g_value_unset (&value);
127 debug_dump_element_pad (GstPad * pad, GstElement * element,
128 GstDebugGraphDetails details, FILE * out, const gint indent)
130 GstElement *target_element;
131 GstPad *target_pad, *tmp_pad;
133 GstPadTemplate *pad_templ;
134 GstPadPresence presence;
135 gchar *pad_name, *element_name;
136 gchar *target_pad_name, *target_element_name;
137 gchar *color_name, *style_name;
140 spc = g_malloc (1 + indent * 2);
141 memset (spc, 32, indent * 2);
142 spc[indent * 2] = '\0';
144 dir = gst_pad_get_direction (pad);
145 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
146 element_name = debug_dump_make_object_name (GST_OBJECT (element));
147 if (GST_IS_GHOST_PAD (pad)) {
149 (dir == GST_PAD_SRC) ? "#ffdddd" : ((dir ==
150 GST_PAD_SINK) ? "#ddddff" : "#ffffff");
151 /* output target-pad so that it belongs to this element */
152 if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad)))) {
153 if ((target_pad = gst_pad_get_peer (tmp_pad))) {
154 target_pad_name = debug_dump_make_object_name (GST_OBJECT (target_pad));
155 if ((target_element = gst_pad_get_parent_element (target_pad))) {
156 target_element_name =
157 debug_dump_make_object_name (GST_OBJECT (target_element));
159 target_element_name = "";
161 style_name = "filled,solid";
162 if ((pad_templ = gst_pad_get_pad_template (target_pad))) {
163 presence = GST_PAD_TEMPLATE_PRESENCE (pad_templ);
164 if (presence == GST_PAD_SOMETIMES) {
165 style_name = "filled,dotted";
166 } else if (presence == GST_PAD_REQUEST) {
167 style_name = "filled,dashed";
171 "%s %s_%s [color=black, fillcolor=\"%s\", label=\"%s\", height=\"0.2\", style=\"%s\"];\n",
172 spc, target_element_name, target_pad_name, color_name,
173 GST_OBJECT_NAME (target_pad), style_name);
174 g_free (target_pad_name);
175 if (target_element) {
176 g_free (target_element_name);
177 gst_object_unref (target_element);
179 gst_object_unref (target_pad);
181 gst_object_unref (tmp_pad);
185 (dir == GST_PAD_SRC) ? "#ffaaaa" : ((dir ==
186 GST_PAD_SINK) ? "#aaaaff" : "#cccccc");
189 style_name = "filled,solid";
190 if ((pad_templ = gst_pad_get_pad_template (pad))) {
191 presence = GST_PAD_TEMPLATE_PRESENCE (pad_templ);
192 if (presence == GST_PAD_SOMETIMES) {
193 style_name = "filled,dotted";
194 } else if (presence == GST_PAD_REQUEST) {
195 style_name = "filled,dashed";
199 "%s %s_%s [color=black, fillcolor=\"%s\", label=\"%s\", height=\"0.2\", style=\"%s\"];\n",
200 spc, element_name, pad_name, color_name, GST_OBJECT_NAME (pad),
204 g_free (element_name);
209 string_append_field (GQuark field, const GValue * value, gpointer ptr)
211 GString *str = (GString *) ptr;
212 gchar *value_str = gst_value_serialize (value);
214 g_string_append_printf (str, " %15s: %s\\l", g_quark_to_string (field),
222 debug_dump_describe_caps (GstCaps * caps, GstDebugGraphDetails details,
223 gboolean * need_free)
227 if (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS) {
229 if (gst_caps_is_any (caps) || gst_caps_is_empty (caps)) {
230 media = gst_caps_to_string (caps);
238 for (i = 0; i < gst_caps_get_size (caps); i++) {
240 STRUCTURE_ESTIMATED_STRING_LEN (gst_caps_get_structure (caps, i));
243 str = g_string_sized_new (slen);
244 for (i = 0; i < gst_caps_get_size (caps); i++) {
245 GstStructure *structure = gst_caps_get_structure (caps, i);
247 g_string_append (str, gst_structure_get_name (structure));
248 g_string_append (str, "\\l");
250 gst_structure_foreach (structure, string_append_field, (gpointer) str);
253 media = g_string_free (str, FALSE);
258 if (GST_CAPS_IS_SIMPLE (caps))
260 (gchar *) gst_structure_get_name (gst_caps_get_structure (caps, 0));
269 debug_dump_element_pad_link (GstPad * pad, GstElement * element,
270 GstDebugGraphDetails details, FILE * out, const gint indent)
272 GstElement *peer_element, *target_element;
273 GstPad *peer_pad, *target_pad, *tmp_pad;
274 GstCaps *caps, *peer_caps;
275 gboolean free_caps, free_peer_caps, free_media;
277 gchar *pad_name, *element_name;
278 gchar *peer_pad_name, *peer_element_name;
279 gchar *target_pad_name, *target_element_name;
282 spc = g_malloc (1 + indent * 2);
283 memset (spc, 32, indent * 2);
284 spc[indent * 2] = '\0';
286 if ((peer_pad = gst_pad_get_peer (pad))) {
288 if ((details & GST_DEBUG_GRAPH_SHOW_MEDIA_TYPE) ||
289 (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS)
291 if ((caps = gst_pad_get_negotiated_caps (pad))) {
295 if (!(caps = (GstCaps *)
296 gst_pad_get_pad_template_caps (pad))) {
297 /* this should not happen */
301 if ((peer_caps = gst_pad_get_negotiated_caps (peer_pad))) {
302 free_peer_caps = TRUE;
304 free_peer_caps = FALSE;
305 peer_caps = (GstCaps *) gst_pad_get_pad_template_caps (peer_pad);
308 media = debug_dump_describe_caps (caps, details, &free_media);
309 /* check if peer caps are different */
310 if (peer_caps && !gst_caps_is_equal (caps, peer_caps)) {
311 gchar *old_media = media;
315 tmp = debug_dump_describe_caps (peer_caps, details, &free_tmp);
316 if (gst_pad_get_direction (pad) == GST_PAD_SRC) {
317 media = g_strdup_printf ("%s\\n---\\n%s", media, tmp);
319 media = g_strdup_printf ("%s\\n---\\n%s", tmp, media);
328 gst_caps_unref (caps);
331 if (free_peer_caps && peer_caps) {
332 gst_caps_unref (peer_caps);
336 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
338 element_name = debug_dump_make_object_name (GST_OBJECT (element));
342 peer_pad_name = debug_dump_make_object_name (GST_OBJECT (peer_pad));
343 if ((peer_element = gst_pad_get_parent_element (peer_pad))) {
345 debug_dump_make_object_name (GST_OBJECT (peer_element));
347 peer_element_name = "";
350 if (GST_IS_GHOST_PAD (pad)) {
351 if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad)))) {
352 if ((target_pad = gst_pad_get_peer (tmp_pad))) {
354 debug_dump_make_object_name (GST_OBJECT (target_pad));
355 if ((target_element = gst_pad_get_parent_element (target_pad))) {
356 target_element_name =
357 debug_dump_make_object_name (GST_OBJECT (target_element));
359 target_element_name = "";
361 /* src ghostpad relationship */
362 fprintf (out, "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
363 target_element_name, target_pad_name, element_name, pad_name);
365 g_free (target_pad_name);
366 if (target_element) {
367 g_free (target_element_name);
368 gst_object_unref (target_element);
370 gst_object_unref (target_pad);
372 gst_object_unref (tmp_pad);
375 if (GST_IS_GHOST_PAD (peer_pad)) {
376 if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (peer_pad)))) {
377 if ((target_pad = gst_pad_get_peer (tmp_pad))) {
379 debug_dump_make_object_name (GST_OBJECT (target_pad));
380 if ((target_element = gst_pad_get_parent_element (target_pad))) {
381 target_element_name =
382 debug_dump_make_object_name (GST_OBJECT (target_element));
384 target_element_name = "";
386 /* sink ghostpad relationship */
387 fprintf (out, "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
388 peer_element_name, peer_pad_name,
389 target_element_name, target_pad_name);
390 /* FIXME: we are missing links from the proxy pad
391 * theoretically we need to:
392 * pad=gst_object_ref(target_pad);
393 * goto line 280: if ((peer_pad = gst_pad_get_peer (pad)))
394 * as this would e ugly we need to refactor ...
396 debug_dump_element_pad_link (target_pad, target_element, details, out,
398 g_free (target_pad_name);
399 if (target_element) {
400 g_free (target_element_name);
401 gst_object_unref (target_element);
403 gst_object_unref (target_pad);
405 gst_object_unref (tmp_pad);
411 fprintf (out, "%s%s_%s -> %s_%s [label=\"%s\"]\n", spc,
412 element_name, pad_name, peer_element_name, peer_pad_name, media);
417 fprintf (out, "%s%s_%s -> %s_%s\n", spc,
418 element_name, pad_name, peer_element_name, peer_pad_name);
423 g_free (element_name);
425 g_free (peer_pad_name);
427 g_free (peer_element_name);
428 gst_object_unref (peer_element);
430 gst_object_unref (peer_pad);
436 * debug_dump_element:
437 * @bin: the bin that should be analyzed
438 * @out: file to write to
439 * @indent: level of graph indentation
441 * Helper for _gst_debug_bin_to_dot_file() to recursively dump a pipeline.
444 debug_dump_element (GstBin * bin, GstDebugGraphDetails details, FILE * out,
447 GstIterator *element_iter, *pad_iter;
448 gboolean elements_done, pads_done;
452 guint src_pads, sink_pads;
454 gchar *state_name = NULL;
455 gchar *param_name = NULL;
458 spc = g_malloc (1 + indent * 2);
459 memset (spc, 32, indent * 2);
460 spc[indent * 2] = '\0';
462 element_iter = gst_bin_iterate_elements (bin);
463 elements_done = FALSE;
464 while (!elements_done) {
465 switch (gst_iterator_next (element_iter, (gpointer) & element)) {
466 case GST_ITERATOR_OK:
467 element_name = debug_dump_make_object_name (GST_OBJECT (element));
469 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
470 state_name = debug_dump_get_element_state (GST_ELEMENT (element));
472 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
473 param_name = debug_dump_get_element_params (GST_ELEMENT (element));
476 fprintf (out, "%ssubgraph cluster_%s {\n", spc, element_name);
477 fprintf (out, "%s fontname=\"Bitstream Vera Sans\";\n", spc);
478 fprintf (out, "%s fontsize=\"8\";\n", spc);
479 fprintf (out, "%s style=filled;\n", spc);
480 fprintf (out, "%s color=black;\n\n", spc);
481 fprintf (out, "%s label=\"%s\\n%s%s%s\";\n", spc,
482 G_OBJECT_TYPE_NAME (element), GST_OBJECT_NAME (element),
483 (state_name ? state_name : ""), (param_name ? param_name : "")
493 g_free (element_name);
495 src_pads = sink_pads = 0;
496 if ((pad_iter = gst_element_iterate_pads (element))) {
499 switch (gst_iterator_next (pad_iter, (gpointer) & pad)) {
500 case GST_ITERATOR_OK:
501 debug_dump_element_pad (pad, element, details, out, indent);
502 dir = gst_pad_get_direction (pad);
503 if (dir == GST_PAD_SRC)
505 else if (dir == GST_PAD_SINK)
507 gst_object_unref (pad);
509 case GST_ITERATOR_RESYNC:
510 gst_iterator_resync (pad_iter);
512 case GST_ITERATOR_ERROR:
513 case GST_ITERATOR_DONE:
518 gst_iterator_free (pad_iter);
520 if (GST_IS_BIN (element)) {
521 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
523 debug_dump_element (GST_BIN (element), details, out, indent + 1);
525 if (src_pads && !sink_pads)
526 fprintf (out, "%s fillcolor=\"#ffaaaa\";\n", spc);
527 else if (!src_pads && sink_pads)
528 fprintf (out, "%s fillcolor=\"#aaaaff\";\n", spc);
529 else if (src_pads && sink_pads)
530 fprintf (out, "%s fillcolor=\"#aaffaa\";\n", spc);
532 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
534 fprintf (out, "%s}\n\n", spc);
535 if ((pad_iter = gst_element_iterate_pads (element))) {
538 switch (gst_iterator_next (pad_iter, (gpointer) & pad)) {
539 case GST_ITERATOR_OK:
540 if (gst_pad_is_linked (pad)
541 && gst_pad_get_direction (pad) == GST_PAD_SRC) {
542 debug_dump_element_pad_link (pad, element, details, out,
545 gst_object_unref (pad);
547 case GST_ITERATOR_RESYNC:
548 gst_iterator_resync (pad_iter);
550 case GST_ITERATOR_ERROR:
551 case GST_ITERATOR_DONE:
556 gst_iterator_free (pad_iter);
558 gst_object_unref (element);
560 case GST_ITERATOR_RESYNC:
561 gst_iterator_resync (element_iter);
563 case GST_ITERATOR_ERROR:
564 case GST_ITERATOR_DONE:
565 elements_done = TRUE;
569 gst_iterator_free (element_iter);
574 * _gst_debug_bin_to_dot_file:
575 * @bin: the top-level pipeline that should be analyzed
576 * @file_name: output base filename (e.g. "myplayer")
578 * To aid debugging applications one can use this method to write out the whole
579 * network of gstreamer elements that form the pipeline into an dot file.
580 * This file can be processed with graphviz to get an image.
581 * <informalexample><programlisting>
582 * dot -Tpng -oimage.png graph_lowlevel.dot
583 * </programlisting></informalexample>
586 _gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
587 const gchar * file_name)
589 gchar *full_file_name = NULL;
592 g_return_if_fail (GST_IS_BIN (bin));
594 if (G_LIKELY (priv_gst_dump_dot_dir == NULL))
598 file_name = g_get_application_name ();
600 file_name = "unnamed";
603 full_file_name = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.dot",
604 priv_gst_dump_dot_dir, file_name);
606 if ((out = fopen (full_file_name, "wb"))) {
607 gchar *state_name = NULL;
608 gchar *param_name = NULL;
610 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
611 state_name = debug_dump_get_element_state (GST_ELEMENT (bin));
613 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
614 param_name = debug_dump_get_element_params (GST_ELEMENT (bin));
619 "digraph pipeline {\n"
621 " fontname=\"sans\";\n"
626 " label=\"<%s>\\n%s%s%s\";\n"
627 " node [style=filled, shape=box, fontsize=\"7\", fontname=\"sans\", margin=\"0.0,0.0\"];\n"
628 " edge [labelfontsize=\"7\", fontsize=\"7\", fontname=\"monospace\"];\n"
629 "\n", G_OBJECT_TYPE_NAME (bin), GST_OBJECT_NAME (bin),
630 (state_name ? state_name : ""), (param_name ? param_name : "")
637 debug_dump_element (bin, details, out, 1);
640 fprintf (out, "}\n");
642 GST_INFO ("wrote bin graph to : '%s'", full_file_name);
644 GST_WARNING ("Failed to open file '%s' for writing: %s", full_file_name,
647 g_free (full_file_name);
651 * _gst_debug_bin_to_dot_file_with_ts:
652 * @bin: the top-level pipeline that should be analyzed
653 * @file_name: output base filename (e.g. "myplayer")
655 * This works like _gst_debug_bin_to_dot_file(), but adds the current timestamp
656 * to the filename, so that it can be used to take multiple snapshots.
659 _gst_debug_bin_to_dot_file_with_ts (GstBin * bin, GstDebugGraphDetails details,
660 const gchar * file_name)
662 gchar *ts_file_name = NULL;
663 GstClockTime elapsed;
665 g_return_if_fail (GST_IS_BIN (bin));
668 file_name = g_get_application_name ();
670 file_name = "unnamed";
674 elapsed = GST_CLOCK_DIFF (_priv_gst_info_start_time,
675 gst_util_get_timestamp ());
677 /* we don't use GST_TIME_FORMAT as such filenames would fail on some
678 * filesystems like fat */
680 g_strdup_printf ("%u.%02u.%02u.%09u-%s", GST_TIME_ARGS (elapsed),
683 _gst_debug_bin_to_dot_file (bin, details, ts_file_name);
684 g_free (ts_file_name);
687 #endif /* GST_DISABLE_GST_DEBUG */