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 * obj)
71 return g_strcanon (g_strdup_printf ("%s_%p", GST_OBJECT_NAME (obj), obj),
72 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 = GST_STATE_VOID_PENDING, pending = GST_STATE_VOID_PENDING;
82 gst_element_get_state (element, &state, &pending, 0);
83 if (pending == GST_STATE_VOID_PENDING) {
84 gboolean is_locked = gst_element_is_locked_state (element);
85 state_name = g_strdup_printf ("\\n[%c]%s", state_icons[state],
86 (is_locked ? "(locked)" : ""));
88 state_name = g_strdup_printf ("\\n[%c] -> [%c]", state_icons[state],
89 state_icons[pending]);
95 debug_dump_get_element_params (GstElement * element)
97 gchar *param_name = NULL;
98 GParamSpec **properties, *property;
99 GValue value = { 0, };
100 guint i, number_of_properties;
101 gchar *tmp, *value_str;
103 /* get paramspecs and show non-default properties */
105 g_object_class_list_properties (G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS
106 (element)), &number_of_properties);
108 for (i = 0; i < number_of_properties; i++) {
109 property = properties[i];
111 /* ski some properties */
112 if (!(property->flags & G_PARAM_READABLE))
114 if (!strcmp (property->name, "name"))
117 g_value_init (&value, property->value_type);
118 g_object_get_property (G_OBJECT (element), property->name, &value);
119 if (!(g_param_value_defaults (property, &value))) {
120 tmp = g_strdup_value_contents (&value);
121 value_str = g_strescape (tmp, NULL);
125 param_name = g_strdup_printf ("%s\\n%s=%s",
126 tmp, property->name, value_str);
129 param_name = g_strdup_printf ("\\n%s=%s", property->name, value_str);
133 g_value_unset (&value);
141 debug_dump_pad (GstPad * pad, const gchar * color_name,
142 const gchar * element_name, GstDebugGraphDetails details, FILE * out,
145 GstPadTemplate *pad_templ;
146 GstPadPresence presence;
148 const gchar *style_name;
149 const gchar *spc = &spaces[MAX (sizeof (spaces) - (1 + indent * 2), 0)];
151 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
153 /* pad availability */
154 style_name = "filled,solid";
155 if ((pad_templ = gst_pad_get_pad_template (pad))) {
156 presence = GST_PAD_TEMPLATE_PRESENCE (pad_templ);
157 if (presence == GST_PAD_SOMETIMES) {
158 style_name = "filled,dotted";
159 } else if (presence == GST_PAD_REQUEST) {
160 style_name = "filled,dashed";
163 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
165 const gchar *activation_mode = "-><";
167 /* check if pad flags */
168 pad_flags[0] = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKED) ? 'B' : 'b';
169 pad_flags[1] = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLUSHING) ? 'F' : 'f';
170 pad_flags[2] = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_IN_GETCAPS) ? 'G' : 'g';
171 pad_flags[3] = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKING) ? 'B' : 'b';
175 "%s %s_%s [color=black, fillcolor=\"%s\", label=\"%s\\n[%c][%s]\", height=\"0.2\", style=\"%s\"];\n",
176 spc, element_name, pad_name, color_name, GST_OBJECT_NAME (pad),
177 activation_mode[pad->mode], pad_flags, style_name);
180 "%s %s_%s [color=black, fillcolor=\"%s\", label=\"%s\", height=\"0.2\", style=\"%s\"];\n",
181 spc, element_name, pad_name, color_name, GST_OBJECT_NAME (pad),
189 debug_dump_element_pad (GstPad * pad, GstElement * element,
190 GstDebugGraphDetails details, FILE * out, const gint indent)
192 GstElement *target_element;
193 GstPad *target_pad, *tmp_pad;
196 gchar *target_element_name;
197 const gchar *color_name;
199 dir = gst_pad_get_direction (pad);
200 element_name = debug_dump_make_object_name (GST_OBJECT (element));
201 if (GST_IS_GHOST_PAD (pad)) {
203 (dir == GST_PAD_SRC) ? "#ffdddd" : ((dir ==
204 GST_PAD_SINK) ? "#ddddff" : "#ffffff");
205 /* output target-pad so that it belongs to this element */
206 if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad)))) {
207 if ((target_pad = gst_pad_get_peer (tmp_pad))) {
208 gchar *pad_name, *target_pad_name;
209 const gchar *spc = &spaces[MAX (sizeof (spaces) - (1 + indent * 2), 0)];
211 if ((target_element = gst_pad_get_parent_element (target_pad))) {
212 target_element_name =
213 debug_dump_make_object_name (GST_OBJECT (target_element));
215 target_element_name = g_strdup ("");
217 debug_dump_pad (target_pad, color_name, target_element_name, details,
219 /* src ghostpad relationship */
220 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
221 target_pad_name = debug_dump_make_object_name (GST_OBJECT (target_pad));
222 if (dir == GST_PAD_SRC) {
223 fprintf (out, "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
224 target_element_name, target_pad_name, element_name, pad_name);
226 fprintf (out, "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
227 element_name, pad_name, target_element_name, target_pad_name);
229 g_free (target_pad_name);
230 g_free (target_element_name);
232 gst_object_unref (target_element);
233 gst_object_unref (target_pad);
236 gst_object_unref (tmp_pad);
240 (dir == GST_PAD_SRC) ? "#ffaaaa" : ((dir ==
241 GST_PAD_SINK) ? "#aaaaff" : "#cccccc");
244 debug_dump_pad (pad, color_name, element_name, details, out, indent);
245 g_free (element_name);
249 string_append_field (GQuark field, const GValue * value, gpointer ptr)
251 GString *str = (GString *) ptr;
252 gchar *value_str = gst_value_serialize (value);
253 gchar *esc_value_str;
255 /* some enums can become really long */
256 if (strlen (value_str) > 25) {
260 value_str[25] = '\0';
262 /* mirror any brackets and quotes */
263 if (value_str[0] == '<')
264 value_str[pos--] = '>';
265 if (value_str[0] == '[')
266 value_str[pos--] = ']';
267 if (value_str[0] == '(')
268 value_str[pos--] = ')';
269 if (value_str[0] == '{')
270 value_str[pos--] = '}';
271 if (value_str[0] == '"')
272 value_str[pos--] = '"';
274 value_str[pos--] = ' ';
276 value_str[pos--] = '.';
277 value_str[pos--] = '.';
278 value_str[pos--] = '.';
280 esc_value_str = g_strescape (value_str, NULL);
282 g_string_append_printf (str, " %18s: %s\\l", g_quark_to_string (field),
286 g_free (esc_value_str);
291 debug_dump_describe_caps (GstCaps * caps, GstDebugGraphDetails details)
295 if (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS) {
297 if (gst_caps_is_any (caps) || gst_caps_is_empty (caps)) {
298 media = gst_caps_to_string (caps);
305 for (i = 0; i < gst_caps_get_size (caps); i++) {
307 STRUCTURE_ESTIMATED_STRING_LEN (gst_caps_get_structure (caps, i));
310 str = g_string_sized_new (slen);
311 for (i = 0; i < gst_caps_get_size (caps); i++) {
312 GstStructure *structure = gst_caps_get_structure (caps, i);
314 g_string_append (str, gst_structure_get_name (structure));
315 g_string_append (str, "\\l");
317 gst_structure_foreach (structure, string_append_field, (gpointer) str);
320 media = g_string_free (str, FALSE);
324 if (GST_CAPS_IS_SIMPLE (caps))
326 g_strdup (gst_structure_get_name (gst_caps_get_structure (caps, 0)));
328 media = g_strdup ("*");
334 debug_dump_element_pad_link (GstPad * pad, GstElement * element,
335 GstDebugGraphDetails details, FILE * out, const gint indent)
337 GstElement *peer_element;
339 GstCaps *caps, *peer_caps;
341 gchar *media_src = NULL, *media_sink = NULL;
342 gchar *pad_name, *element_name;
343 gchar *peer_pad_name, *peer_element_name;
344 const gchar *spc = &spaces[MAX (sizeof (spaces) - (1 + indent * 2), 0)];
346 if ((peer_pad = gst_pad_get_peer (pad))) {
347 if ((details & GST_DEBUG_GRAPH_SHOW_MEDIA_TYPE) ||
348 (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS)
350 caps = gst_pad_get_current_caps (pad);
352 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
353 peer_caps = gst_pad_get_current_caps (peer_pad);
355 peer_caps = gst_caps_copy (gst_pad_get_pad_template_caps (peer_pad));
357 media = debug_dump_describe_caps (caps, details);
358 /* check if peer caps are different */
359 if (peer_caps && !gst_caps_is_equal (caps, peer_caps)) {
362 tmp = debug_dump_describe_caps (peer_caps, details);
363 if (gst_pad_get_direction (pad) == GST_PAD_SRC) {
372 gst_caps_unref (peer_caps);
373 gst_caps_unref (caps);
376 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
378 element_name = debug_dump_make_object_name (GST_OBJECT (element));
380 element_name = g_strdup ("");
382 peer_pad_name = debug_dump_make_object_name (GST_OBJECT (peer_pad));
383 if ((peer_element = gst_pad_get_parent_element (peer_pad))) {
385 debug_dump_make_object_name (GST_OBJECT (peer_element));
387 peer_element_name = g_strdup ("");
392 fprintf (out, "%s%s_%s -> %s_%s [label=\"%s\"]\n", spc,
393 element_name, pad_name, peer_element_name, peer_pad_name, media);
395 } else if (media_src && media_sink) {
396 /* dot has some issues with placement of head and taillabels,
397 * we need an empty label to make space */
398 fprintf (out, "%s%s_%s -> %s_%s [labeldistance=\"10\", labelangle=\"0\", "
400 "headlabel=\"%s\", taillabel=\"%s\"]\n",
401 spc, element_name, pad_name, peer_element_name, peer_pad_name,
402 media_src, media_sink);
406 fprintf (out, "%s%s_%s -> %s_%s\n", spc,
407 element_name, pad_name, peer_element_name, peer_pad_name);
411 g_free (element_name);
412 g_free (peer_pad_name);
413 g_free (peer_element_name);
415 gst_object_unref (peer_element);
416 gst_object_unref (peer_pad);
421 debug_dump_element_pads (GstIterator * pad_iter, GstPad * pad,
422 GstElement * element, GstDebugGraphDetails details, FILE * out,
423 const gint indent, guint * src_pads, guint * sink_pads)
425 GValue item = { 0, };
431 switch (gst_iterator_next (pad_iter, &item)) {
432 case GST_ITERATOR_OK:
433 pad = g_value_get_object (&item);
434 debug_dump_element_pad (pad, element, details, out, indent);
435 dir = gst_pad_get_direction (pad);
436 if (dir == GST_PAD_SRC)
438 else if (dir == GST_PAD_SINK)
440 g_value_reset (&item);
442 case GST_ITERATOR_RESYNC:
443 gst_iterator_resync (pad_iter);
445 case GST_ITERATOR_ERROR:
446 case GST_ITERATOR_DONE:
454 * debug_dump_element:
455 * @bin: the bin that should be analyzed
456 * @out: file to write to
457 * @indent: level of graph indentation
459 * Helper for _gst_debug_bin_to_dot_file() to recursively dump a pipeline.
462 debug_dump_element (GstBin * bin, GstDebugGraphDetails details, FILE * out,
465 GstIterator *element_iter, *pad_iter;
466 gboolean elements_done, pads_done;
467 GValue item = { 0, };
468 GValue item2 = { 0, };
471 guint src_pads, sink_pads;
473 gchar *state_name = NULL;
474 gchar *param_name = NULL;
475 const gchar *spc = &spaces[MAX (sizeof (spaces) - (1 + indent * 2), 0)];
477 element_iter = gst_bin_iterate_elements (bin);
478 elements_done = FALSE;
479 while (!elements_done) {
480 switch (gst_iterator_next (element_iter, &item)) {
481 case GST_ITERATOR_OK:
482 element = g_value_get_object (&item);
483 element_name = debug_dump_make_object_name (GST_OBJECT (element));
485 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
486 state_name = debug_dump_get_element_state (GST_ELEMENT (element));
488 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
489 param_name = debug_dump_get_element_params (GST_ELEMENT (element));
492 fprintf (out, "%ssubgraph cluster_%s {\n", spc, element_name);
493 fprintf (out, "%s fontname=\"Bitstream Vera Sans\";\n", spc);
494 fprintf (out, "%s fontsize=\"8\";\n", spc);
495 fprintf (out, "%s style=filled;\n", spc);
496 fprintf (out, "%s color=black;\n\n", spc);
497 fprintf (out, "%s label=\"%s\\n%s%s%s\";\n", spc,
498 G_OBJECT_TYPE_NAME (element), GST_OBJECT_NAME (element),
499 (state_name ? state_name : ""), (param_name ? param_name : "")
509 g_free (element_name);
511 src_pads = sink_pads = 0;
512 if ((pad_iter = gst_element_iterate_sink_pads (element))) {
513 debug_dump_element_pads (pad_iter, pad, element, details, out, indent,
514 &src_pads, &sink_pads);
515 gst_iterator_free (pad_iter);
517 if ((pad_iter = gst_element_iterate_src_pads (element))) {
518 debug_dump_element_pads (pad_iter, pad, element, details, out, indent,
519 &src_pads, &sink_pads);
520 gst_iterator_free (pad_iter);
522 if (GST_IS_BIN (element)) {
523 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
525 debug_dump_element (GST_BIN (element), details, out, indent + 1);
527 if (src_pads && !sink_pads)
528 fprintf (out, "%s fillcolor=\"#ffaaaa\";\n", spc);
529 else if (!src_pads && sink_pads)
530 fprintf (out, "%s fillcolor=\"#aaaaff\";\n", spc);
531 else if (src_pads && sink_pads)
532 fprintf (out, "%s fillcolor=\"#aaffaa\";\n", spc);
534 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
536 fprintf (out, "%s}\n\n", spc);
537 if ((pad_iter = gst_element_iterate_pads (element))) {
540 switch (gst_iterator_next (pad_iter, &item2)) {
541 case GST_ITERATOR_OK:
542 pad = g_value_get_object (&item2);
543 if (gst_pad_is_linked (pad)) {
544 if (gst_pad_get_direction (pad) == GST_PAD_SRC) {
545 debug_dump_element_pad_link (pad, element, details, out,
548 GstPad *peer_pad = gst_pad_get_peer (pad);
551 if (!GST_IS_GHOST_PAD (peer_pad)
552 && GST_IS_PROXY_PAD (peer_pad)) {
553 debug_dump_element_pad_link (peer_pad, NULL, details,
556 gst_object_unref (peer_pad);
560 g_value_reset (&item2);
562 case GST_ITERATOR_RESYNC:
563 gst_iterator_resync (pad_iter);
565 case GST_ITERATOR_ERROR:
566 case GST_ITERATOR_DONE:
571 g_value_unset (&item2);
572 gst_iterator_free (pad_iter);
574 g_value_reset (&item);
576 case GST_ITERATOR_RESYNC:
577 gst_iterator_resync (element_iter);
579 case GST_ITERATOR_ERROR:
580 case GST_ITERATOR_DONE:
581 elements_done = TRUE;
586 g_value_unset (&item);
587 gst_iterator_free (element_iter);
591 * _gst_debug_bin_to_dot_file:
592 * @bin: the top-level pipeline that should be analyzed
593 * @file_name: output base filename (e.g. "myplayer")
595 * To aid debugging applications one can use this method to write out the whole
596 * network of gstreamer elements that form the pipeline into an dot file.
597 * This file can be processed with graphviz to get an image.
598 * <informalexample><programlisting>
599 * dot -Tpng -oimage.png graph_lowlevel.dot
600 * </programlisting></informalexample>
603 _gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
604 const gchar * file_name)
606 gchar *full_file_name = NULL;
609 g_return_if_fail (GST_IS_BIN (bin));
611 if (G_LIKELY (priv_gst_dump_dot_dir == NULL))
615 file_name = g_get_application_name ();
617 file_name = "unnamed";
620 full_file_name = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.dot",
621 priv_gst_dump_dot_dir, file_name);
623 if ((out = fopen (full_file_name, "wb"))) {
624 gchar *state_name = NULL;
625 gchar *param_name = NULL;
627 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
628 state_name = debug_dump_get_element_state (GST_ELEMENT (bin));
630 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
631 param_name = debug_dump_get_element_params (GST_ELEMENT (bin));
636 "digraph pipeline {\n"
638 " fontname=\"sans\";\n"
639 " fontsize=\"10\";\n"
643 " label=\"<%s>\\n%s%s%s\";\n"
644 " node [style=filled, shape=box, fontsize=\"9\", fontname=\"sans\", margin=\"0.0,0.0\"];\n"
645 " edge [labelfontsize=\"6\", fontsize=\"9\", fontname=\"monospace\"];\n"
646 "\n", G_OBJECT_TYPE_NAME (bin), GST_OBJECT_NAME (bin),
647 (state_name ? state_name : ""), (param_name ? param_name : "")
654 debug_dump_element (bin, details, out, 1);
657 fprintf (out, "}\n");
659 GST_INFO ("wrote bin graph to : '%s'", full_file_name);
661 GST_WARNING ("Failed to open file '%s' for writing: %s", full_file_name,
664 g_free (full_file_name);
668 * _gst_debug_bin_to_dot_file_with_ts:
669 * @bin: the top-level pipeline that should be analyzed
670 * @file_name: output base filename (e.g. "myplayer")
672 * This works like _gst_debug_bin_to_dot_file(), but adds the current timestamp
673 * to the filename, so that it can be used to take multiple snapshots.
676 _gst_debug_bin_to_dot_file_with_ts (GstBin * bin,
677 GstDebugGraphDetails details, const gchar * file_name)
679 gchar *ts_file_name = NULL;
680 GstClockTime elapsed;
682 g_return_if_fail (GST_IS_BIN (bin));
685 file_name = g_get_application_name ();
687 file_name = "unnamed";
691 elapsed = GST_CLOCK_DIFF (_priv_gst_info_start_time,
692 gst_util_get_timestamp ());
694 /* we don't use GST_TIME_FORMAT as such filenames would fail on some
695 * filesystems like fat */
697 g_strdup_printf ("%u.%02u.%02u.%09u-%s", GST_TIME_ARGS (elapsed),
700 _gst_debug_bin_to_dot_file (bin, details, ts_file_name);
701 g_free (ts_file_name);
703 #else /* !GST_DISABLE_GST_DEBUG */
704 #ifndef GST_REMOVE_DISABLED
706 _gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
707 const gchar * file_name)
712 _gst_debug_bin_to_dot_file_with_ts (GstBin * bin, GstDebugGraphDetails details,
713 const gchar * file_name)
716 #endif /* GST_REMOVE_DISABLED */
717 #endif /* GST_DISABLE_GST_DEBUG */