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
30 * - we use head/tail labels for pad-caps right now
31 * - this does not work well, as dot seems to not look at their size when
33 * - we could add the caps to the pad itself, then we should use one line per
34 * caps (simple caps = one line)
37 #include "gst_private.h"
38 #include "gstdebugutils.h"
40 #ifndef GST_DISABLE_GST_DEBUG
49 #include "gstobject.h"
50 #include "gstghostpad.h"
55 /*** PIPELINE GRAPHS **********************************************************/
57 const gchar *priv_gst_dump_dot_dir; /* NULL *//* set from gst.c */
59 const gchar spaces[] = {
66 extern GstClockTime _priv_gst_info_start_time;
69 debug_dump_make_object_name (GstObject * element)
71 return g_strcanon (g_strdup_printf ("%s_%p", GST_OBJECT_NAME (element),
72 element), G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "_", '_');
76 debug_dump_get_element_state (GstElement * element)
78 gchar *state_name = NULL;
79 const gchar *state_icons = "~0-=>";
80 GstState state = 0, pending = 0;
82 gst_element_get_state (element, &state, &pending, 0);
83 if (pending == GST_STATE_VOID_PENDING) {
84 state_name = g_strdup_printf ("\\n[%c]", state_icons[state]);
86 state_name = g_strdup_printf ("\\n[%c] -> [%c]", state_icons[state],
87 state_icons[pending]);
93 debug_dump_get_element_params (GstElement * element)
95 gchar *param_name = NULL;
96 GParamSpec **properties, *property;
97 GValue value = { 0, };
98 guint i, number_of_properties;
99 gchar *tmp, *value_str;
101 /* get paramspecs and show non-default properties */
103 g_object_class_list_properties (G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS
104 (element)), &number_of_properties);
106 for (i = 0; i < number_of_properties; i++) {
107 property = properties[i];
109 /* ski some properties */
110 if (!(property->flags & G_PARAM_READABLE))
112 if (!strcmp (property->name, "name"))
115 g_value_init (&value, property->value_type);
116 g_object_get_property (G_OBJECT (element), property->name, &value);
117 if (!(g_param_value_defaults (property, &value))) {
118 tmp = g_strdup_value_contents (&value);
119 value_str = g_strescape (tmp, NULL);
123 param_name = g_strdup_printf ("%s\\n%s=%s",
124 tmp, property->name, value_str);
127 param_name = g_strdup_printf ("\\n%s=%s", property->name, value_str);
131 g_value_unset (&value);
139 debug_dump_pad (GstPad * pad, const gchar * color_name,
140 const gchar * element_name, GstDebugGraphDetails details, FILE * out,
143 GstPadTemplate *pad_templ;
144 GstPadPresence presence;
146 const gchar *style_name;
147 const gchar *spc = &spaces[MAX (sizeof (spaces) - (1 + indent * 2), 0)];
149 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
151 /* pad availability */
152 style_name = "filled,solid";
153 if ((pad_templ = gst_pad_get_pad_template (pad))) {
154 presence = GST_PAD_TEMPLATE_PRESENCE (pad_templ);
155 if (presence == GST_PAD_SOMETIMES) {
156 style_name = "filled,dotted";
157 } else if (presence == GST_PAD_REQUEST) {
158 style_name = "filled,dashed";
161 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
163 const gchar *activation_mode = "-><";
165 /* check if pad flags */
166 pad_flags[0] = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKED) ? 'B' : 'b';
167 pad_flags[1] = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLUSHING) ? 'F' : 'f';
168 pad_flags[2] = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_IN_GETCAPS) ? 'G' : 'g';
169 pad_flags[3] = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_IN_SETCAPS) ? 'S' : 's';
170 pad_flags[4] = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKING) ? 'B' : 'b';
174 "%s %s_%s [color=black, fillcolor=\"%s\", label=\"%s\\n[%c][%s]\", height=\"0.2\", style=\"%s\"];\n",
175 spc, element_name, pad_name, color_name, GST_OBJECT_NAME (pad),
176 activation_mode[pad->mode], pad_flags, style_name);
179 "%s %s_%s [color=black, fillcolor=\"%s\", label=\"%s\", height=\"0.2\", style=\"%s\"];\n",
180 spc, element_name, pad_name, color_name, GST_OBJECT_NAME (pad),
188 debug_dump_element_pad (GstPad * pad, GstElement * element,
189 GstDebugGraphDetails details, FILE * out, const gint indent)
191 GstElement *target_element;
192 GstPad *target_pad, *tmp_pad;
195 gchar *target_element_name;
196 const gchar *color_name;
198 dir = gst_pad_get_direction (pad);
199 element_name = debug_dump_make_object_name (GST_OBJECT (element));
200 if (GST_IS_GHOST_PAD (pad)) {
202 (dir == GST_PAD_SRC) ? "#ffdddd" : ((dir ==
203 GST_PAD_SINK) ? "#ddddff" : "#ffffff");
204 /* output target-pad so that it belongs to this element */
205 if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad)))) {
206 if ((target_pad = gst_pad_get_peer (tmp_pad))) {
207 if ((target_element = gst_pad_get_parent_element (target_pad))) {
208 target_element_name =
209 debug_dump_make_object_name (GST_OBJECT (target_element));
211 target_element_name = g_strdup ("");
213 debug_dump_pad (target_pad, color_name, target_element_name, details,
215 g_free (target_element_name);
217 gst_object_unref (target_element);
218 gst_object_unref (target_pad);
220 gst_object_unref (tmp_pad);
224 (dir == GST_PAD_SRC) ? "#ffaaaa" : ((dir ==
225 GST_PAD_SINK) ? "#aaaaff" : "#cccccc");
228 debug_dump_pad (pad, color_name, element_name, details, out, indent);
229 g_free (element_name);
233 string_append_field (GQuark field, const GValue * value, gpointer ptr)
235 GString *str = (GString *) ptr;
236 gchar *value_str = gst_value_serialize (value);
237 gchar *esc_value_str;
239 /* some enums can become really long */
240 if (strlen (value_str) > 25) {
244 value_str[25] = '\0';
246 /* mirror any brackets and quotes */
247 if (value_str[0] == '<')
248 value_str[pos--] = '>';
249 if (value_str[0] == '[')
250 value_str[pos--] = ']';
251 if (value_str[0] == '(')
252 value_str[pos--] = ')';
253 if (value_str[0] == '{')
254 value_str[pos--] = '}';
255 if (value_str[0] == '"')
256 value_str[pos--] = '"';
258 value_str[pos--] = ' ';
260 value_str[pos--] = '.';
261 value_str[pos--] = '.';
262 value_str[pos--] = '.';
264 esc_value_str = g_strescape (value_str, NULL);
266 g_string_append_printf (str, " %18s: %s\\l", g_quark_to_string (field),
270 g_free (esc_value_str);
275 debug_dump_describe_caps (GstCaps * caps, GstDebugGraphDetails details)
279 if (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS) {
281 if (gst_caps_is_any (caps) || gst_caps_is_empty (caps)) {
282 media = gst_caps_to_string (caps);
289 for (i = 0; i < gst_caps_get_size (caps); i++) {
291 STRUCTURE_ESTIMATED_STRING_LEN (gst_caps_get_structure (caps, i));
294 str = g_string_sized_new (slen);
295 for (i = 0; i < gst_caps_get_size (caps); i++) {
296 GstStructure *structure = gst_caps_get_structure (caps, i);
298 g_string_append (str, gst_structure_get_name (structure));
299 g_string_append (str, "\\l");
301 gst_structure_foreach (structure, string_append_field, (gpointer) str);
304 media = g_string_free (str, FALSE);
308 if (GST_CAPS_IS_SIMPLE (caps))
310 g_strdup (gst_structure_get_name (gst_caps_get_structure (caps, 0)));
312 media = g_strdup ("*");
318 debug_dump_element_pad_link (GstPad * pad, GstElement * element,
319 GstDebugGraphDetails details, FILE * out, const gint indent)
321 GstElement *peer_element, *target_element;
322 GstPad *peer_pad, *target_pad, *tmp_pad;
323 GstCaps *caps, *peer_caps;
325 gchar *media_src = NULL, *media_sink = NULL;
326 gchar *pad_name, *element_name;
327 gchar *peer_pad_name, *peer_element_name;
328 gchar *target_pad_name, *target_element_name;
329 const gchar *spc = &spaces[MAX (sizeof (spaces) - (1 + indent * 2), 0)];
331 if ((peer_pad = gst_pad_get_peer (pad))) {
332 if ((details & GST_DEBUG_GRAPH_SHOW_MEDIA_TYPE) ||
333 (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS)
335 caps = gst_pad_get_negotiated_caps (pad);
337 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
338 peer_caps = gst_pad_get_negotiated_caps (peer_pad);
340 peer_caps = gst_caps_copy (gst_pad_get_pad_template_caps (peer_pad));
342 media = debug_dump_describe_caps (caps, details);
343 /* check if peer caps are different */
344 if (peer_caps && !gst_caps_is_equal (caps, peer_caps)) {
347 tmp = debug_dump_describe_caps (peer_caps, details);
348 if (gst_pad_get_direction (pad) == GST_PAD_SRC) {
357 gst_caps_unref (peer_caps);
358 gst_caps_unref (caps);
361 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
363 element_name = debug_dump_make_object_name (GST_OBJECT (element));
365 element_name = g_strdup ("");
367 peer_pad_name = debug_dump_make_object_name (GST_OBJECT (peer_pad));
368 if ((peer_element = gst_pad_get_parent_element (peer_pad))) {
370 debug_dump_make_object_name (GST_OBJECT (peer_element));
372 peer_element_name = g_strdup ("");
375 if (GST_IS_GHOST_PAD (pad)) {
376 if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (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 = g_strdup ("");
386 /* src ghostpad relationship */
387 fprintf (out, "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
388 target_element_name, target_pad_name, element_name, pad_name);
390 g_free (target_pad_name);
391 g_free (target_element_name);
393 gst_object_unref (target_element);
394 gst_object_unref (target_pad);
396 gst_object_unref (tmp_pad);
399 if (GST_IS_GHOST_PAD (peer_pad)) {
400 if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (peer_pad)))) {
401 if ((target_pad = gst_pad_get_peer (tmp_pad))) {
403 debug_dump_make_object_name (GST_OBJECT (target_pad));
404 if ((target_element = gst_pad_get_parent_element (target_pad))) {
405 target_element_name =
406 debug_dump_make_object_name (GST_OBJECT (target_element));
408 target_element_name = g_strdup ("");
410 /* sink ghostpad relationship */
411 fprintf (out, "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
412 peer_element_name, peer_pad_name,
413 target_element_name, target_pad_name);
414 /* FIXME: we are missing links from the proxy pad
415 * theoretically we need to:
416 * pad=gst_object_ref(target_pad);
417 * goto line 280: if ((peer_pad = gst_pad_get_peer (pad)))
418 * as this would be ugly we need to refactor ...
420 debug_dump_element_pad_link (target_pad, target_element, details, out,
422 g_free (target_pad_name);
423 g_free (target_element_name);
425 gst_object_unref (target_element);
426 gst_object_unref (target_pad);
428 gst_object_unref (tmp_pad);
434 fprintf (out, "%s%s_%s -> %s_%s [label=\"%s\"]\n", spc,
435 element_name, pad_name, peer_element_name, peer_pad_name, media);
437 } else if (media_src && media_sink) {
438 /* dot has some issues with placement of head and taillabels,
439 * we need an empty label to make space */
440 fprintf (out, "%s%s_%s -> %s_%s [labeldistance=\"10\", labelangle=\"0\", "
442 "headlabel=\"%s\", taillabel=\"%s\"]\n",
443 spc, element_name, pad_name, peer_element_name, peer_pad_name,
444 media_src, media_sink);
448 fprintf (out, "%s%s_%s -> %s_%s\n", spc,
449 element_name, pad_name, peer_element_name, peer_pad_name);
453 g_free (element_name);
454 g_free (peer_pad_name);
455 g_free (peer_element_name);
457 gst_object_unref (peer_element);
458 gst_object_unref (peer_pad);
463 * debug_dump_element:
464 * @bin: the bin that should be analyzed
465 * @out: file to write to
466 * @indent: level of graph indentation
468 * Helper for _gst_debug_bin_to_dot_file() to recursively dump a pipeline.
471 debug_dump_element (GstBin * bin, GstDebugGraphDetails details, FILE * out,
474 GstIterator *element_iter, *pad_iter;
475 gboolean elements_done, pads_done;
476 GValue item = { 0, };
477 GValue item2 = { 0, };
481 guint src_pads, sink_pads;
483 gchar *state_name = NULL;
484 gchar *param_name = NULL;
485 const gchar *spc = &spaces[MAX (sizeof (spaces) - (1 + indent * 2), 0)];
487 element_iter = gst_bin_iterate_elements (bin);
488 elements_done = FALSE;
489 while (!elements_done) {
490 switch (gst_iterator_next (element_iter, &item)) {
491 case GST_ITERATOR_OK:
492 element = g_value_get_object (&item);
493 element_name = debug_dump_make_object_name (GST_OBJECT (element));
495 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
496 state_name = debug_dump_get_element_state (GST_ELEMENT (element));
498 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
499 param_name = debug_dump_get_element_params (GST_ELEMENT (element));
502 fprintf (out, "%ssubgraph cluster_%s {\n", spc, element_name);
503 fprintf (out, "%s fontname=\"Bitstream Vera Sans\";\n", spc);
504 fprintf (out, "%s fontsize=\"8\";\n", spc);
505 fprintf (out, "%s style=filled;\n", spc);
506 fprintf (out, "%s color=black;\n\n", spc);
507 fprintf (out, "%s label=\"%s\\n%s%s%s\";\n", spc,
508 G_OBJECT_TYPE_NAME (element), GST_OBJECT_NAME (element),
509 (state_name ? state_name : ""), (param_name ? param_name : "")
519 g_free (element_name);
521 src_pads = sink_pads = 0;
522 if ((pad_iter = gst_element_iterate_pads (element))) {
525 switch (gst_iterator_next (pad_iter, &item2)) {
526 case GST_ITERATOR_OK:
527 pad = g_value_get_object (&item2);
528 debug_dump_element_pad (pad, element, details, out, indent);
529 dir = gst_pad_get_direction (pad);
530 if (dir == GST_PAD_SRC)
532 else if (dir == GST_PAD_SINK)
534 g_value_reset (&item2);
536 case GST_ITERATOR_RESYNC:
537 gst_iterator_resync (pad_iter);
539 case GST_ITERATOR_ERROR:
540 case GST_ITERATOR_DONE:
545 g_value_unset (&item2);
546 gst_iterator_free (pad_iter);
548 if (GST_IS_BIN (element)) {
549 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
551 debug_dump_element (GST_BIN (element), details, out, indent + 1);
553 if (src_pads && !sink_pads)
554 fprintf (out, "%s fillcolor=\"#ffaaaa\";\n", spc);
555 else if (!src_pads && sink_pads)
556 fprintf (out, "%s fillcolor=\"#aaaaff\";\n", spc);
557 else if (src_pads && sink_pads)
558 fprintf (out, "%s fillcolor=\"#aaffaa\";\n", spc);
560 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
562 fprintf (out, "%s}\n\n", spc);
563 if ((pad_iter = gst_element_iterate_pads (element))) {
566 switch (gst_iterator_next (pad_iter, &item2)) {
567 case GST_ITERATOR_OK:
568 pad = g_value_get_object (&item2);
569 if (gst_pad_is_linked (pad)
570 && gst_pad_get_direction (pad) == GST_PAD_SRC) {
571 debug_dump_element_pad_link (pad, element, details, out,
574 g_value_reset (&item2);
576 case GST_ITERATOR_RESYNC:
577 gst_iterator_resync (pad_iter);
579 case GST_ITERATOR_ERROR:
580 case GST_ITERATOR_DONE:
585 g_value_unset (&item2);
586 gst_iterator_free (pad_iter);
588 g_value_reset (&item);
590 case GST_ITERATOR_RESYNC:
591 gst_iterator_resync (element_iter);
593 case GST_ITERATOR_ERROR:
594 case GST_ITERATOR_DONE:
595 elements_done = TRUE;
599 g_value_unset (&item);
600 gst_iterator_free (element_iter);
604 * _gst_debug_bin_to_dot_file:
605 * @bin: the top-level pipeline that should be analyzed
606 * @file_name: output base filename (e.g. "myplayer")
608 * To aid debugging applications one can use this method to write out the whole
609 * network of gstreamer elements that form the pipeline into an dot file.
610 * This file can be processed with graphviz to get an image.
611 * <informalexample><programlisting>
612 * dot -Tpng -oimage.png graph_lowlevel.dot
613 * </programlisting></informalexample>
616 _gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
617 const gchar * file_name)
619 gchar *full_file_name = NULL;
622 g_return_if_fail (GST_IS_BIN (bin));
624 if (G_LIKELY (priv_gst_dump_dot_dir == NULL))
628 file_name = g_get_application_name ();
630 file_name = "unnamed";
633 full_file_name = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.dot",
634 priv_gst_dump_dot_dir, file_name);
636 if ((out = fopen (full_file_name, "wb"))) {
637 gchar *state_name = NULL;
638 gchar *param_name = NULL;
640 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
641 state_name = debug_dump_get_element_state (GST_ELEMENT (bin));
643 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
644 param_name = debug_dump_get_element_params (GST_ELEMENT (bin));
649 "digraph pipeline {\n"
651 " fontname=\"sans\";\n"
652 " fontsize=\"10\";\n"
656 " label=\"<%s>\\n%s%s%s\";\n"
657 " node [style=filled, shape=box, fontsize=\"9\", fontname=\"sans\", margin=\"0.0,0.0\"];\n"
658 " edge [labelfontsize=\"6\", fontsize=\"9\", fontname=\"monospace\"];\n"
659 "\n", G_OBJECT_TYPE_NAME (bin), GST_OBJECT_NAME (bin),
660 (state_name ? state_name : ""), (param_name ? param_name : "")
667 debug_dump_element (bin, details, out, 1);
670 fprintf (out, "}\n");
672 GST_INFO ("wrote bin graph to : '%s'", full_file_name);
674 GST_WARNING ("Failed to open file '%s' for writing: %s", full_file_name,
677 g_free (full_file_name);
681 * _gst_debug_bin_to_dot_file_with_ts:
682 * @bin: the top-level pipeline that should be analyzed
683 * @file_name: output base filename (e.g. "myplayer")
685 * This works like _gst_debug_bin_to_dot_file(), but adds the current timestamp
686 * to the filename, so that it can be used to take multiple snapshots.
689 _gst_debug_bin_to_dot_file_with_ts (GstBin * bin, GstDebugGraphDetails details,
690 const gchar * file_name)
692 gchar *ts_file_name = NULL;
693 GstClockTime elapsed;
695 g_return_if_fail (GST_IS_BIN (bin));
698 file_name = g_get_application_name ();
700 file_name = "unnamed";
704 elapsed = GST_CLOCK_DIFF (_priv_gst_info_start_time,
705 gst_util_get_timestamp ());
707 /* we don't use GST_TIME_FORMAT as such filenames would fail on some
708 * filesystems like fat */
710 g_strdup_printf ("%u.%02u.%02u.%09u-%s", GST_TIME_ARGS (elapsed),
713 _gst_debug_bin_to_dot_file (bin, details, ts_file_name);
714 g_free (ts_file_name);
716 #else /* !GST_DISABLE_GST_DEBUG */
717 #ifndef GST_REMOVE_DISABLED
719 _gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
720 const gchar * file_name)
725 _gst_debug_bin_to_dot_file_with_ts (GstBin * bin, GstDebugGraphDetails details,
726 const gchar * file_name)
729 #endif /* GST_REMOVE_DISABLED */
730 #endif /* GST_DISABLE_GST_DEBUG */