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 llok at their sizen 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);
238 /* some enums can become really long */
239 if (strlen (value_str) > 25) {
243 value_str[25] = '\0';
245 /* mirror any brackets */
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--] = '}';
255 value_str[pos--] = ' ';
257 value_str[pos--] = '.';
258 value_str[pos--] = '.';
259 value_str[pos--] = '.';
261 g_string_append_printf (str, " %18s: %s\\l", g_quark_to_string (field),
269 debug_dump_describe_caps (GstCaps * caps, GstDebugGraphDetails details)
273 if (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS) {
275 if (gst_caps_is_any (caps) || gst_caps_is_empty (caps)) {
276 media = gst_caps_to_string (caps);
283 for (i = 0; i < gst_caps_get_size (caps); i++) {
285 STRUCTURE_ESTIMATED_STRING_LEN (gst_caps_get_structure (caps, i));
288 str = g_string_sized_new (slen);
289 for (i = 0; i < gst_caps_get_size (caps); i++) {
290 GstStructure *structure = gst_caps_get_structure (caps, i);
292 g_string_append (str, gst_structure_get_name (structure));
293 g_string_append (str, "\\l");
295 gst_structure_foreach (structure, string_append_field, (gpointer) str);
298 media = g_string_free (str, FALSE);
302 if (GST_CAPS_IS_SIMPLE (caps))
304 g_strdup (gst_structure_get_name (gst_caps_get_structure (caps, 0)));
306 media = g_strdup ("*");
312 debug_dump_element_pad_link (GstPad * pad, GstElement * element,
313 GstDebugGraphDetails details, FILE * out, const gint indent)
315 GstElement *peer_element, *target_element;
316 GstPad *peer_pad, *target_pad, *tmp_pad;
317 GstCaps *caps, *peer_caps;
319 gchar *media_src = NULL, *media_sink = NULL;
320 gchar *pad_name, *element_name;
321 gchar *peer_pad_name, *peer_element_name;
322 gchar *target_pad_name, *target_element_name;
323 const gchar *spc = &spaces[MAX (sizeof (spaces) - (1 + indent * 2), 0)];
325 if ((peer_pad = gst_pad_get_peer (pad))) {
326 if ((details & GST_DEBUG_GRAPH_SHOW_MEDIA_TYPE) ||
327 (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS)
329 caps = gst_pad_get_negotiated_caps (pad);
331 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
332 peer_caps = gst_pad_get_negotiated_caps (peer_pad);
334 peer_caps = gst_caps_copy (gst_pad_get_pad_template_caps (peer_pad));
336 media = debug_dump_describe_caps (caps, details);
337 /* check if peer caps are different */
338 if (peer_caps && !gst_caps_is_equal (caps, peer_caps)) {
341 tmp = debug_dump_describe_caps (peer_caps, details);
342 if (gst_pad_get_direction (pad) == GST_PAD_SRC) {
351 gst_caps_unref (peer_caps);
352 gst_caps_unref (caps);
355 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
357 element_name = debug_dump_make_object_name (GST_OBJECT (element));
359 element_name = g_strdup ("");
361 peer_pad_name = debug_dump_make_object_name (GST_OBJECT (peer_pad));
362 if ((peer_element = gst_pad_get_parent_element (peer_pad))) {
364 debug_dump_make_object_name (GST_OBJECT (peer_element));
366 peer_element_name = g_strdup ("");
369 if (GST_IS_GHOST_PAD (pad)) {
370 if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad)))) {
371 if ((target_pad = gst_pad_get_peer (tmp_pad))) {
373 debug_dump_make_object_name (GST_OBJECT (target_pad));
374 if ((target_element = gst_pad_get_parent_element (target_pad))) {
375 target_element_name =
376 debug_dump_make_object_name (GST_OBJECT (target_element));
378 target_element_name = g_strdup ("");
380 /* src ghostpad relationship */
381 fprintf (out, "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
382 target_element_name, target_pad_name, element_name, pad_name);
384 g_free (target_pad_name);
385 g_free (target_element_name);
387 gst_object_unref (target_element);
388 gst_object_unref (target_pad);
390 gst_object_unref (tmp_pad);
393 if (GST_IS_GHOST_PAD (peer_pad)) {
394 if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (peer_pad)))) {
395 if ((target_pad = gst_pad_get_peer (tmp_pad))) {
397 debug_dump_make_object_name (GST_OBJECT (target_pad));
398 if ((target_element = gst_pad_get_parent_element (target_pad))) {
399 target_element_name =
400 debug_dump_make_object_name (GST_OBJECT (target_element));
402 target_element_name = g_strdup ("");
404 /* sink ghostpad relationship */
405 fprintf (out, "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
406 peer_element_name, peer_pad_name,
407 target_element_name, target_pad_name);
408 /* FIXME: we are missing links from the proxy pad
409 * theoretically we need to:
410 * pad=gst_object_ref(target_pad);
411 * goto line 280: if ((peer_pad = gst_pad_get_peer (pad)))
412 * as this would be ugly we need to refactor ...
414 debug_dump_element_pad_link (target_pad, target_element, details, out,
416 g_free (target_pad_name);
417 g_free (target_element_name);
419 gst_object_unref (target_element);
420 gst_object_unref (target_pad);
422 gst_object_unref (tmp_pad);
428 fprintf (out, "%s%s_%s -> %s_%s [label=\"%s\"]\n", spc,
429 element_name, pad_name, peer_element_name, peer_pad_name, media);
431 } else if (media_src && media_sink) {
432 /* dot has some issues with placement of head and taillabels,
433 * we need an empty label to make space */
434 fprintf (out, "%s%s_%s -> %s_%s [labeldistance=\"10\", labelangle=\"0\", "
436 "headlabel=\"%s\", taillabel=\"%s\"]\n",
437 spc, element_name, pad_name, peer_element_name, peer_pad_name,
438 media_src, media_sink);
442 fprintf (out, "%s%s_%s -> %s_%s\n", spc,
443 element_name, pad_name, peer_element_name, peer_pad_name);
447 g_free (element_name);
448 g_free (peer_pad_name);
449 g_free (peer_element_name);
451 gst_object_unref (peer_element);
452 gst_object_unref (peer_pad);
457 * debug_dump_element:
458 * @bin: the bin that should be analyzed
459 * @out: file to write to
460 * @indent: level of graph indentation
462 * Helper for _gst_debug_bin_to_dot_file() to recursively dump a pipeline.
465 debug_dump_element (GstBin * bin, GstDebugGraphDetails details, FILE * out,
468 GstIterator *element_iter, *pad_iter;
469 gboolean elements_done, pads_done;
473 guint src_pads, sink_pads;
475 gchar *state_name = NULL;
476 gchar *param_name = NULL;
477 const gchar *spc = &spaces[MAX (sizeof (spaces) - (1 + indent * 2), 0)];
479 element_iter = gst_bin_iterate_elements (bin);
480 elements_done = FALSE;
481 while (!elements_done) {
482 switch (gst_iterator_next (element_iter, (gpointer) & element)) {
483 case GST_ITERATOR_OK:
484 element_name = debug_dump_make_object_name (GST_OBJECT (element));
486 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
487 state_name = debug_dump_get_element_state (GST_ELEMENT (element));
489 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
490 param_name = debug_dump_get_element_params (GST_ELEMENT (element));
493 fprintf (out, "%ssubgraph cluster_%s {\n", spc, element_name);
494 fprintf (out, "%s fontname=\"Bitstream Vera Sans\";\n", spc);
495 fprintf (out, "%s fontsize=\"8\";\n", spc);
496 fprintf (out, "%s style=filled;\n", spc);
497 fprintf (out, "%s color=black;\n\n", spc);
498 fprintf (out, "%s label=\"%s\\n%s%s%s\";\n", spc,
499 G_OBJECT_TYPE_NAME (element), GST_OBJECT_NAME (element),
500 (state_name ? state_name : ""), (param_name ? param_name : "")
510 g_free (element_name);
512 src_pads = sink_pads = 0;
513 if ((pad_iter = gst_element_iterate_pads (element))) {
516 switch (gst_iterator_next (pad_iter, (gpointer) & pad)) {
517 case GST_ITERATOR_OK:
518 debug_dump_element_pad (pad, element, details, out, indent);
519 dir = gst_pad_get_direction (pad);
520 if (dir == GST_PAD_SRC)
522 else if (dir == GST_PAD_SINK)
524 gst_object_unref (pad);
526 case GST_ITERATOR_RESYNC:
527 gst_iterator_resync (pad_iter);
529 case GST_ITERATOR_ERROR:
530 case GST_ITERATOR_DONE:
535 gst_iterator_free (pad_iter);
537 if (GST_IS_BIN (element)) {
538 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
540 debug_dump_element (GST_BIN (element), details, out, indent + 1);
542 if (src_pads && !sink_pads)
543 fprintf (out, "%s fillcolor=\"#ffaaaa\";\n", spc);
544 else if (!src_pads && sink_pads)
545 fprintf (out, "%s fillcolor=\"#aaaaff\";\n", spc);
546 else if (src_pads && sink_pads)
547 fprintf (out, "%s fillcolor=\"#aaffaa\";\n", spc);
549 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
551 fprintf (out, "%s}\n\n", spc);
552 if ((pad_iter = gst_element_iterate_pads (element))) {
555 switch (gst_iterator_next (pad_iter, (gpointer) & pad)) {
556 case GST_ITERATOR_OK:
557 if (gst_pad_is_linked (pad)
558 && gst_pad_get_direction (pad) == GST_PAD_SRC) {
559 debug_dump_element_pad_link (pad, element, details, out,
562 gst_object_unref (pad);
564 case GST_ITERATOR_RESYNC:
565 gst_iterator_resync (pad_iter);
567 case GST_ITERATOR_ERROR:
568 case GST_ITERATOR_DONE:
573 gst_iterator_free (pad_iter);
575 gst_object_unref (element);
577 case GST_ITERATOR_RESYNC:
578 gst_iterator_resync (element_iter);
580 case GST_ITERATOR_ERROR:
581 case GST_ITERATOR_DONE:
582 elements_done = TRUE;
586 gst_iterator_free (element_iter);
590 * _gst_debug_bin_to_dot_file:
591 * @bin: the top-level pipeline that should be analyzed
592 * @file_name: output base filename (e.g. "myplayer")
594 * To aid debugging applications one can use this method to write out the whole
595 * network of gstreamer elements that form the pipeline into an dot file.
596 * This file can be processed with graphviz to get an image.
597 * <informalexample><programlisting>
598 * dot -Tpng -oimage.png graph_lowlevel.dot
599 * </programlisting></informalexample>
602 _gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
603 const gchar * file_name)
605 gchar *full_file_name = NULL;
608 g_return_if_fail (GST_IS_BIN (bin));
610 if (G_LIKELY (priv_gst_dump_dot_dir == NULL))
614 file_name = g_get_application_name ();
616 file_name = "unnamed";
619 full_file_name = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.dot",
620 priv_gst_dump_dot_dir, file_name);
622 if ((out = fopen (full_file_name, "wb"))) {
623 gchar *state_name = NULL;
624 gchar *param_name = NULL;
626 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
627 state_name = debug_dump_get_element_state (GST_ELEMENT (bin));
629 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
630 param_name = debug_dump_get_element_params (GST_ELEMENT (bin));
635 "digraph pipeline {\n"
637 " fontname=\"sans\";\n"
638 " fontsize=\"10\";\n"
642 " label=\"<%s>\\n%s%s%s\";\n"
643 " node [style=filled, shape=box, fontsize=\"9\", fontname=\"sans\", margin=\"0.0,0.0\"];\n"
644 " edge [labelfontsize=\"6\", fontsize=\"9\", fontname=\"monospace\"];\n"
645 "\n", G_OBJECT_TYPE_NAME (bin), GST_OBJECT_NAME (bin),
646 (state_name ? state_name : ""), (param_name ? param_name : "")
653 debug_dump_element (bin, details, out, 1);
656 fprintf (out, "}\n");
658 GST_INFO ("wrote bin graph to : '%s'", full_file_name);
660 GST_WARNING ("Failed to open file '%s' for writing: %s", full_file_name,
663 g_free (full_file_name);
667 * _gst_debug_bin_to_dot_file_with_ts:
668 * @bin: the top-level pipeline that should be analyzed
669 * @file_name: output base filename (e.g. "myplayer")
671 * This works like _gst_debug_bin_to_dot_file(), but adds the current timestamp
672 * to the filename, so that it can be used to take multiple snapshots.
675 _gst_debug_bin_to_dot_file_with_ts (GstBin * bin, GstDebugGraphDetails details,
676 const gchar * file_name)
678 gchar *ts_file_name = NULL;
679 GstClockTime elapsed;
681 g_return_if_fail (GST_IS_BIN (bin));
684 file_name = g_get_application_name ();
686 file_name = "unnamed";
690 elapsed = GST_CLOCK_DIFF (_priv_gst_info_start_time,
691 gst_util_get_timestamp ());
693 /* we don't use GST_TIME_FORMAT as such filenames would fail on some
694 * filesystems like fat */
696 g_strdup_printf ("%u.%02u.%02u.%09u-%s", GST_TIME_ARGS (elapsed),
699 _gst_debug_bin_to_dot_file (bin, details, ts_file_name);
700 g_free (ts_file_name);
702 #else /* !GST_DISABLE_GST_DEBUG */
703 #ifndef GST_REMOVE_DISABLED
705 _gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
706 const gchar * file_name)
711 _gst_debug_bin_to_dot_file_with_ts (GstBin * bin, GstDebugGraphDetails details,
712 const gchar * file_name)
715 #endif /* GST_REMOVE_DISABLED */
716 #endif /* GST_DISABLE_GST_DEBUG */