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_BLOCKING) ? 'B' : 'b';
173 "%s %s_%s [color=black, fillcolor=\"%s\", label=\"%s\\n[%c][%s]\", height=\"0.2\", style=\"%s\"];\n",
174 spc, element_name, pad_name, color_name, GST_OBJECT_NAME (pad),
175 activation_mode[pad->mode], pad_flags, style_name);
178 "%s %s_%s [color=black, fillcolor=\"%s\", label=\"%s\", height=\"0.2\", style=\"%s\"];\n",
179 spc, element_name, pad_name, color_name, GST_OBJECT_NAME (pad),
187 debug_dump_element_pad (GstPad * pad, GstElement * element,
188 GstDebugGraphDetails details, FILE * out, const gint indent)
190 GstElement *target_element;
191 GstPad *target_pad, *tmp_pad;
194 gchar *target_element_name;
195 const gchar *color_name;
197 dir = gst_pad_get_direction (pad);
198 element_name = debug_dump_make_object_name (GST_OBJECT (element));
199 if (GST_IS_GHOST_PAD (pad)) {
201 (dir == GST_PAD_SRC) ? "#ffdddd" : ((dir ==
202 GST_PAD_SINK) ? "#ddddff" : "#ffffff");
203 /* output target-pad so that it belongs to this element */
204 if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad)))) {
205 if ((target_pad = gst_pad_get_peer (tmp_pad))) {
206 if ((target_element = gst_pad_get_parent_element (target_pad))) {
207 target_element_name =
208 debug_dump_make_object_name (GST_OBJECT (target_element));
210 target_element_name = g_strdup ("");
212 debug_dump_pad (target_pad, color_name, target_element_name, details,
214 g_free (target_element_name);
216 gst_object_unref (target_element);
217 gst_object_unref (target_pad);
219 gst_object_unref (tmp_pad);
223 (dir == GST_PAD_SRC) ? "#ffaaaa" : ((dir ==
224 GST_PAD_SINK) ? "#aaaaff" : "#cccccc");
227 debug_dump_pad (pad, color_name, element_name, details, out, indent);
228 g_free (element_name);
232 string_append_field (GQuark field, const GValue * value, gpointer ptr)
234 GString *str = (GString *) ptr;
235 gchar *value_str = gst_value_serialize (value);
236 gchar *esc_value_str;
238 /* some enums can become really long */
239 if (strlen (value_str) > 25) {
243 value_str[25] = '\0';
245 /* mirror any brackets and quotes */
246 if (value_str[0] == '<')
247 value_str[pos--] = '>';
248 if (value_str[0] == '[')
249 value_str[pos--] = ']';
250 if (value_str[0] == '(')
251 value_str[pos--] = ')';
252 if (value_str[0] == '{')
253 value_str[pos--] = '}';
254 if (value_str[0] == '"')
255 value_str[pos--] = '"';
257 value_str[pos--] = ' ';
259 value_str[pos--] = '.';
260 value_str[pos--] = '.';
261 value_str[pos--] = '.';
263 esc_value_str = g_strescape (value_str, NULL);
265 g_string_append_printf (str, " %18s: %s\\l", g_quark_to_string (field),
269 g_free (esc_value_str);
274 debug_dump_describe_caps (GstCaps * caps, GstDebugGraphDetails details)
278 if (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS) {
280 if (gst_caps_is_any (caps) || gst_caps_is_empty (caps)) {
281 media = gst_caps_to_string (caps);
288 for (i = 0; i < gst_caps_get_size (caps); i++) {
290 STRUCTURE_ESTIMATED_STRING_LEN (gst_caps_get_structure (caps, i));
293 str = g_string_sized_new (slen);
294 for (i = 0; i < gst_caps_get_size (caps); i++) {
295 GstStructure *structure = gst_caps_get_structure (caps, i);
297 g_string_append (str, gst_structure_get_name (structure));
298 g_string_append (str, "\\l");
300 gst_structure_foreach (structure, string_append_field, (gpointer) str);
303 media = g_string_free (str, FALSE);
307 if (GST_CAPS_IS_SIMPLE (caps))
309 g_strdup (gst_structure_get_name (gst_caps_get_structure (caps, 0)));
311 media = g_strdup ("*");
317 debug_dump_element_pad_link (GstPad * pad, GstElement * element,
318 GstDebugGraphDetails details, FILE * out, const gint indent)
320 GstElement *peer_element, *target_element;
321 GstPad *peer_pad, *target_pad, *tmp_pad;
322 GstCaps *caps, *peer_caps;
324 gchar *media_src = NULL, *media_sink = NULL;
325 gchar *pad_name, *element_name;
326 gchar *peer_pad_name, *peer_element_name;
327 gchar *target_pad_name, *target_element_name;
328 const gchar *spc = &spaces[MAX (sizeof (spaces) - (1 + indent * 2), 0)];
330 if ((peer_pad = gst_pad_get_peer (pad))) {
331 if ((details & GST_DEBUG_GRAPH_SHOW_MEDIA_TYPE) ||
332 (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS)
334 caps = gst_pad_get_negotiated_caps (pad);
336 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
337 peer_caps = gst_pad_get_negotiated_caps (peer_pad);
339 peer_caps = gst_caps_copy (gst_pad_get_pad_template_caps (peer_pad));
341 media = debug_dump_describe_caps (caps, details);
342 /* check if peer caps are different */
343 if (peer_caps && !gst_caps_is_equal (caps, peer_caps)) {
346 tmp = debug_dump_describe_caps (peer_caps, details);
347 if (gst_pad_get_direction (pad) == GST_PAD_SRC) {
356 gst_caps_unref (peer_caps);
357 gst_caps_unref (caps);
360 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
362 element_name = debug_dump_make_object_name (GST_OBJECT (element));
364 element_name = g_strdup ("");
366 peer_pad_name = debug_dump_make_object_name (GST_OBJECT (peer_pad));
367 if ((peer_element = gst_pad_get_parent_element (peer_pad))) {
369 debug_dump_make_object_name (GST_OBJECT (peer_element));
371 peer_element_name = g_strdup ("");
374 if (GST_IS_GHOST_PAD (pad)) {
375 if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad)))) {
376 if ((target_pad = gst_pad_get_peer (tmp_pad))) {
378 debug_dump_make_object_name (GST_OBJECT (target_pad));
379 if ((target_element = gst_pad_get_parent_element (target_pad))) {
380 target_element_name =
381 debug_dump_make_object_name (GST_OBJECT (target_element));
383 target_element_name = g_strdup ("");
385 /* src ghostpad relationship */
386 fprintf (out, "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
387 target_element_name, target_pad_name, element_name, pad_name);
389 g_free (target_pad_name);
390 g_free (target_element_name);
392 gst_object_unref (target_element);
393 gst_object_unref (target_pad);
395 gst_object_unref (tmp_pad);
398 if (GST_IS_GHOST_PAD (peer_pad)) {
399 if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (peer_pad)))) {
400 if ((target_pad = gst_pad_get_peer (tmp_pad))) {
402 debug_dump_make_object_name (GST_OBJECT (target_pad));
403 if ((target_element = gst_pad_get_parent_element (target_pad))) {
404 target_element_name =
405 debug_dump_make_object_name (GST_OBJECT (target_element));
407 target_element_name = g_strdup ("");
409 /* sink ghostpad relationship */
410 fprintf (out, "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
411 peer_element_name, peer_pad_name,
412 target_element_name, target_pad_name);
413 /* FIXME: we are missing links from the proxy pad
414 * theoretically we need to:
415 * pad=gst_object_ref(target_pad);
416 * goto line 280: if ((peer_pad = gst_pad_get_peer (pad)))
417 * as this would be ugly we need to refactor ...
419 debug_dump_element_pad_link (target_pad, target_element, details, out,
421 g_free (target_pad_name);
422 g_free (target_element_name);
424 gst_object_unref (target_element);
425 gst_object_unref (target_pad);
427 gst_object_unref (tmp_pad);
433 fprintf (out, "%s%s_%s -> %s_%s [label=\"%s\"]\n", spc,
434 element_name, pad_name, peer_element_name, peer_pad_name, media);
436 } else if (media_src && media_sink) {
437 /* dot has some issues with placement of head and taillabels,
438 * we need an empty label to make space */
439 fprintf (out, "%s%s_%s -> %s_%s [labeldistance=\"10\", labelangle=\"0\", "
441 "headlabel=\"%s\", taillabel=\"%s\"]\n",
442 spc, element_name, pad_name, peer_element_name, peer_pad_name,
443 media_src, media_sink);
447 fprintf (out, "%s%s_%s -> %s_%s\n", spc,
448 element_name, pad_name, peer_element_name, peer_pad_name);
452 g_free (element_name);
453 g_free (peer_pad_name);
454 g_free (peer_element_name);
456 gst_object_unref (peer_element);
457 gst_object_unref (peer_pad);
462 * debug_dump_element:
463 * @bin: the bin that should be analyzed
464 * @out: file to write to
465 * @indent: level of graph indentation
467 * Helper for _gst_debug_bin_to_dot_file() to recursively dump a pipeline.
470 debug_dump_element (GstBin * bin, GstDebugGraphDetails details, FILE * out,
473 GstIterator *element_iter, *pad_iter;
474 gboolean elements_done, pads_done;
475 GValue item = { 0, };
476 GValue item2 = { 0, };
480 guint src_pads, sink_pads;
482 gchar *state_name = NULL;
483 gchar *param_name = NULL;
484 const gchar *spc = &spaces[MAX (sizeof (spaces) - (1 + indent * 2), 0)];
486 element_iter = gst_bin_iterate_elements (bin);
487 elements_done = FALSE;
488 while (!elements_done) {
489 switch (gst_iterator_next (element_iter, &item)) {
490 case GST_ITERATOR_OK:
491 element = g_value_get_object (&item);
492 element_name = debug_dump_make_object_name (GST_OBJECT (element));
494 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
495 state_name = debug_dump_get_element_state (GST_ELEMENT (element));
497 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
498 param_name = debug_dump_get_element_params (GST_ELEMENT (element));
501 fprintf (out, "%ssubgraph cluster_%s {\n", spc, element_name);
502 fprintf (out, "%s fontname=\"Bitstream Vera Sans\";\n", spc);
503 fprintf (out, "%s fontsize=\"8\";\n", spc);
504 fprintf (out, "%s style=filled;\n", spc);
505 fprintf (out, "%s color=black;\n\n", spc);
506 fprintf (out, "%s label=\"%s\\n%s%s%s\";\n", spc,
507 G_OBJECT_TYPE_NAME (element), GST_OBJECT_NAME (element),
508 (state_name ? state_name : ""), (param_name ? param_name : "")
518 g_free (element_name);
520 src_pads = sink_pads = 0;
521 if ((pad_iter = gst_element_iterate_pads (element))) {
524 switch (gst_iterator_next (pad_iter, &item2)) {
525 case GST_ITERATOR_OK:
526 pad = g_value_get_object (&item2);
527 debug_dump_element_pad (pad, element, details, out, indent);
528 dir = gst_pad_get_direction (pad);
529 if (dir == GST_PAD_SRC)
531 else if (dir == GST_PAD_SINK)
533 g_value_reset (&item2);
535 case GST_ITERATOR_RESYNC:
536 gst_iterator_resync (pad_iter);
538 case GST_ITERATOR_ERROR:
539 case GST_ITERATOR_DONE:
544 g_value_unset (&item2);
545 gst_iterator_free (pad_iter);
547 if (GST_IS_BIN (element)) {
548 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
550 debug_dump_element (GST_BIN (element), details, out, indent + 1);
552 if (src_pads && !sink_pads)
553 fprintf (out, "%s fillcolor=\"#ffaaaa\";\n", spc);
554 else if (!src_pads && sink_pads)
555 fprintf (out, "%s fillcolor=\"#aaaaff\";\n", spc);
556 else if (src_pads && sink_pads)
557 fprintf (out, "%s fillcolor=\"#aaffaa\";\n", spc);
559 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
561 fprintf (out, "%s}\n\n", spc);
562 if ((pad_iter = gst_element_iterate_pads (element))) {
565 switch (gst_iterator_next (pad_iter, &item2)) {
566 case GST_ITERATOR_OK:
567 pad = g_value_get_object (&item2);
568 if (gst_pad_is_linked (pad)
569 && gst_pad_get_direction (pad) == GST_PAD_SRC) {
570 debug_dump_element_pad_link (pad, element, details, out,
573 g_value_reset (&item2);
575 case GST_ITERATOR_RESYNC:
576 gst_iterator_resync (pad_iter);
578 case GST_ITERATOR_ERROR:
579 case GST_ITERATOR_DONE:
584 g_value_unset (&item2);
585 gst_iterator_free (pad_iter);
587 g_value_reset (&item);
589 case GST_ITERATOR_RESYNC:
590 gst_iterator_resync (element_iter);
592 case GST_ITERATOR_ERROR:
593 case GST_ITERATOR_DONE:
594 elements_done = TRUE;
598 g_value_unset (&item);
599 gst_iterator_free (element_iter);
603 * _gst_debug_bin_to_dot_file:
604 * @bin: the top-level pipeline that should be analyzed
605 * @file_name: output base filename (e.g. "myplayer")
607 * To aid debugging applications one can use this method to write out the whole
608 * network of gstreamer elements that form the pipeline into an dot file.
609 * This file can be processed with graphviz to get an image.
610 * <informalexample><programlisting>
611 * dot -Tpng -oimage.png graph_lowlevel.dot
612 * </programlisting></informalexample>
615 _gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
616 const gchar * file_name)
618 gchar *full_file_name = NULL;
621 g_return_if_fail (GST_IS_BIN (bin));
623 if (G_LIKELY (priv_gst_dump_dot_dir == NULL))
627 file_name = g_get_application_name ();
629 file_name = "unnamed";
632 full_file_name = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.dot",
633 priv_gst_dump_dot_dir, file_name);
635 if ((out = fopen (full_file_name, "wb"))) {
636 gchar *state_name = NULL;
637 gchar *param_name = NULL;
639 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
640 state_name = debug_dump_get_element_state (GST_ELEMENT (bin));
642 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
643 param_name = debug_dump_get_element_params (GST_ELEMENT (bin));
648 "digraph pipeline {\n"
650 " fontname=\"sans\";\n"
651 " fontsize=\"10\";\n"
655 " label=\"<%s>\\n%s%s%s\";\n"
656 " node [style=filled, shape=box, fontsize=\"9\", fontname=\"sans\", margin=\"0.0,0.0\"];\n"
657 " edge [labelfontsize=\"6\", fontsize=\"9\", fontname=\"monospace\"];\n"
658 "\n", G_OBJECT_TYPE_NAME (bin), GST_OBJECT_NAME (bin),
659 (state_name ? state_name : ""), (param_name ? param_name : "")
666 debug_dump_element (bin, details, out, 1);
669 fprintf (out, "}\n");
671 GST_INFO ("wrote bin graph to : '%s'", full_file_name);
673 GST_WARNING ("Failed to open file '%s' for writing: %s", full_file_name,
676 g_free (full_file_name);
680 * _gst_debug_bin_to_dot_file_with_ts:
681 * @bin: the top-level pipeline that should be analyzed
682 * @file_name: output base filename (e.g. "myplayer")
684 * This works like _gst_debug_bin_to_dot_file(), but adds the current timestamp
685 * to the filename, so that it can be used to take multiple snapshots.
688 _gst_debug_bin_to_dot_file_with_ts (GstBin * bin, GstDebugGraphDetails details,
689 const gchar * file_name)
691 gchar *ts_file_name = NULL;
692 GstClockTime elapsed;
694 g_return_if_fail (GST_IS_BIN (bin));
697 file_name = g_get_application_name ();
699 file_name = "unnamed";
703 elapsed = GST_CLOCK_DIFF (_priv_gst_info_start_time,
704 gst_util_get_timestamp ());
706 /* we don't use GST_TIME_FORMAT as such filenames would fail on some
707 * filesystems like fat */
709 g_strdup_printf ("%u.%02u.%02u.%09u-%s", GST_TIME_ARGS (elapsed),
712 _gst_debug_bin_to_dot_file (bin, details, ts_file_name);
713 g_free (ts_file_name);
715 #else /* !GST_DISABLE_GST_DEBUG */
716 #ifndef GST_REMOVE_DISABLED
718 _gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
719 const gchar * file_name)
724 _gst_debug_bin_to_dot_file_with_ts (GstBin * bin, GstDebugGraphDetails details,
725 const gchar * file_name)
728 #endif /* GST_REMOVE_DISABLED */
729 #endif /* GST_DISABLE_GST_DEBUG */