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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, 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 extern const gchar *priv_gst_dump_dot_dir; /* NULL *//* set from gst.c */
59 const gchar spaces[] = {
67 debug_dump_make_object_name (GstObject * obj)
69 return g_strcanon (g_strdup_printf ("%s_%p", GST_OBJECT_NAME (obj), obj),
70 G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "_", '_');
74 debug_dump_get_element_state (GstElement * element)
76 gchar *state_name = NULL;
77 const gchar *state_icons = "~0-=>";
78 GstState state = GST_STATE_VOID_PENDING, pending = GST_STATE_VOID_PENDING;
80 gst_element_get_state (element, &state, &pending, 0);
81 if (pending == GST_STATE_VOID_PENDING) {
82 gboolean is_locked = gst_element_is_locked_state (element);
83 state_name = g_strdup_printf ("\\n[%c]%s", state_icons[state],
84 (is_locked ? "(locked)" : ""));
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 /* skip 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, GString * str,
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 gst_object_unref (pad_templ);
156 if (presence == GST_PAD_SOMETIMES) {
157 style_name = "filled,dotted";
158 } else if (presence == GST_PAD_REQUEST) {
159 style_name = "filled,dashed";
162 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
164 const gchar *activation_mode = "-><";
165 const gchar *task_mode = "";
168 GST_OBJECT_LOCK (pad);
169 task = GST_PAD_TASK (pad);
171 switch (gst_task_get_state (task)) {
172 case GST_TASK_STARTED:
175 case GST_TASK_PAUSED:
179 /* Invalid task state, ignoring */
183 GST_OBJECT_UNLOCK (pad);
185 /* check if pad flags */
187 GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_BLOCKED) ? 'B' : 'b';
189 GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_FLUSHING) ? 'F' : 'f';
191 GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_BLOCKING) ? 'B' : 'b';
194 g_string_append_printf (str,
195 "%s %s_%s [color=black, fillcolor=\"%s\", label=\"%s\\n[%c][%s]%s\", height=\"0.2\", style=\"%s\"];\n",
196 spc, element_name, pad_name, color_name, GST_OBJECT_NAME (pad),
197 activation_mode[pad->mode], pad_flags, task_mode, style_name);
199 g_string_append_printf (str,
200 "%s %s_%s [color=black, fillcolor=\"%s\", label=\"%s\", height=\"0.2\", style=\"%s\"];\n",
201 spc, element_name, pad_name, color_name, GST_OBJECT_NAME (pad),
209 debug_dump_element_pad (GstPad * pad, GstElement * element,
210 GstDebugGraphDetails details, GString * str, const gint indent)
212 GstElement *target_element;
213 GstPad *target_pad, *tmp_pad;
216 gchar *target_element_name;
217 const gchar *color_name;
219 dir = gst_pad_get_direction (pad);
220 element_name = debug_dump_make_object_name (GST_OBJECT (element));
221 if (GST_IS_GHOST_PAD (pad)) {
223 (dir == GST_PAD_SRC) ? "#ffdddd" : ((dir ==
224 GST_PAD_SINK) ? "#ddddff" : "#ffffff");
225 /* output target-pad so that it belongs to this element */
226 if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad)))) {
227 if ((target_pad = gst_pad_get_peer (tmp_pad))) {
228 gchar *pad_name, *target_pad_name;
229 const gchar *spc = &spaces[MAX (sizeof (spaces) - (1 + indent * 2), 0)];
231 if ((target_element = gst_pad_get_parent_element (target_pad))) {
232 target_element_name =
233 debug_dump_make_object_name (GST_OBJECT (target_element));
235 target_element_name = g_strdup ("");
237 debug_dump_pad (target_pad, color_name, target_element_name, details,
239 /* src ghostpad relationship */
240 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
241 target_pad_name = debug_dump_make_object_name (GST_OBJECT (target_pad));
242 if (dir == GST_PAD_SRC) {
243 g_string_append_printf (str,
244 "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
245 target_element_name, target_pad_name, element_name, pad_name);
247 g_string_append_printf (str,
248 "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
249 element_name, pad_name, target_element_name, target_pad_name);
251 g_free (target_pad_name);
252 g_free (target_element_name);
254 gst_object_unref (target_element);
255 gst_object_unref (target_pad);
258 gst_object_unref (tmp_pad);
262 (dir == GST_PAD_SRC) ? "#ffaaaa" : ((dir ==
263 GST_PAD_SINK) ? "#aaaaff" : "#cccccc");
266 debug_dump_pad (pad, color_name, element_name, details, str, indent);
267 g_free (element_name);
271 string_append_field (GQuark field, const GValue * value, gpointer ptr)
273 GString *str = (GString *) ptr;
274 gchar *value_str = gst_value_serialize (value);
275 gchar *esc_value_str;
277 if (value_str == NULL) {
278 g_string_append_printf (str, " %18s: NULL\\l", g_quark_to_string (field));
282 /* some enums can become really long */
283 if (strlen (value_str) > 25) {
287 value_str[25] = '\0';
289 /* mirror any brackets and quotes */
290 if (value_str[0] == '<')
291 value_str[pos--] = '>';
292 if (value_str[0] == '[')
293 value_str[pos--] = ']';
294 if (value_str[0] == '(')
295 value_str[pos--] = ')';
296 if (value_str[0] == '{')
297 value_str[pos--] = '}';
298 if (value_str[0] == '"')
299 value_str[pos--] = '"';
301 value_str[pos--] = ' ';
303 value_str[pos--] = '.';
304 value_str[pos--] = '.';
305 value_str[pos--] = '.';
307 esc_value_str = g_strescape (value_str, NULL);
309 g_string_append_printf (str, " %18s: %s\\l", g_quark_to_string (field),
313 g_free (esc_value_str);
318 debug_dump_describe_caps (GstCaps * caps, GstDebugGraphDetails details)
322 if (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS) {
324 if (gst_caps_is_any (caps) || gst_caps_is_empty (caps)) {
325 media = gst_caps_to_string (caps);
332 for (i = 0; i < gst_caps_get_size (caps); i++) {
334 STRUCTURE_ESTIMATED_STRING_LEN (gst_caps_get_structure (caps, i));
337 str = g_string_sized_new (slen);
338 for (i = 0; i < gst_caps_get_size (caps); i++) {
339 GstStructure *structure = gst_caps_get_structure (caps, i);
341 g_string_append (str, gst_structure_get_name (structure));
342 g_string_append (str, "\\l");
344 gst_structure_foreach (structure, string_append_field, (gpointer) str);
347 media = g_string_free (str, FALSE);
351 if (GST_CAPS_IS_SIMPLE (caps))
353 g_strdup (gst_structure_get_name (gst_caps_get_structure (caps, 0)));
355 media = g_strdup ("*");
361 debug_dump_element_pad_link (GstPad * pad, GstElement * element,
362 GstDebugGraphDetails details, GString * str, const gint indent)
364 GstElement *peer_element;
366 GstCaps *caps, *peer_caps;
368 gchar *media_src = NULL, *media_sink = NULL;
369 gchar *pad_name, *element_name;
370 gchar *peer_pad_name, *peer_element_name;
371 const gchar *spc = &spaces[MAX (sizeof (spaces) - (1 + indent * 2), 0)];
373 if ((peer_pad = gst_pad_get_peer (pad))) {
374 if ((details & GST_DEBUG_GRAPH_SHOW_MEDIA_TYPE) ||
375 (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS)
377 caps = gst_pad_get_current_caps (pad);
379 caps = gst_pad_get_pad_template_caps (pad);
380 peer_caps = gst_pad_get_current_caps (peer_pad);
382 peer_caps = gst_pad_get_pad_template_caps (peer_pad);
384 media = debug_dump_describe_caps (caps, details);
385 /* check if peer caps are different */
386 if (peer_caps && !gst_caps_is_equal (caps, peer_caps)) {
389 tmp = debug_dump_describe_caps (peer_caps, details);
390 if (gst_pad_get_direction (pad) == GST_PAD_SRC) {
399 gst_caps_unref (peer_caps);
400 gst_caps_unref (caps);
403 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
405 element_name = debug_dump_make_object_name (GST_OBJECT (element));
407 element_name = g_strdup ("");
409 peer_pad_name = debug_dump_make_object_name (GST_OBJECT (peer_pad));
410 if ((peer_element = gst_pad_get_parent_element (peer_pad))) {
412 debug_dump_make_object_name (GST_OBJECT (peer_element));
414 peer_element_name = g_strdup ("");
419 g_string_append_printf (str, "%s%s_%s -> %s_%s [label=\"%s\"]\n", spc,
420 element_name, pad_name, peer_element_name, peer_pad_name, media);
422 } else if (media_src && media_sink) {
423 /* dot has some issues with placement of head and taillabels,
424 * we need an empty label to make space */
425 g_string_append_printf (str,
426 "%s%s_%s -> %s_%s [labeldistance=\"10\", labelangle=\"0\", "
428 "taillabel=\"%s\", headlabel=\"%s\"]\n",
429 spc, element_name, pad_name, peer_element_name, peer_pad_name,
430 media_src, media_sink);
434 g_string_append_printf (str, "%s%s_%s -> %s_%s\n", spc,
435 element_name, pad_name, peer_element_name, peer_pad_name);
439 g_free (element_name);
440 g_free (peer_pad_name);
441 g_free (peer_element_name);
443 gst_object_unref (peer_element);
444 gst_object_unref (peer_pad);
449 debug_dump_element_pads (GstIterator * pad_iter, GstPad * pad,
450 GstElement * element, GstDebugGraphDetails details, GString * str,
451 const gint indent, guint * src_pads, guint * sink_pads)
453 GValue item = { 0, };
459 switch (gst_iterator_next (pad_iter, &item)) {
460 case GST_ITERATOR_OK:
461 pad = g_value_get_object (&item);
462 debug_dump_element_pad (pad, element, details, str, indent);
463 dir = gst_pad_get_direction (pad);
464 if (dir == GST_PAD_SRC)
466 else if (dir == GST_PAD_SINK)
468 g_value_reset (&item);
470 case GST_ITERATOR_RESYNC:
471 gst_iterator_resync (pad_iter);
473 case GST_ITERATOR_ERROR:
474 case GST_ITERATOR_DONE:
482 * debug_dump_element:
483 * @bin: the bin that should be analyzed
484 * @out: file to write to
485 * @indent: level of graph indentation
487 * Helper for gst_debug_bin_to_dot_file() to recursively dump a pipeline.
490 debug_dump_element (GstBin * bin, GstDebugGraphDetails details,
491 GString * str, const gint indent)
493 GstIterator *element_iter, *pad_iter;
494 gboolean elements_done, pads_done;
495 GValue item = { 0, };
496 GValue item2 = { 0, };
499 guint src_pads, sink_pads;
501 gchar *state_name = NULL;
502 gchar *param_name = NULL;
503 const gchar *spc = &spaces[MAX (sizeof (spaces) - (1 + indent * 2), 0)];
505 element_iter = gst_bin_iterate_elements (bin);
506 elements_done = FALSE;
507 while (!elements_done) {
508 switch (gst_iterator_next (element_iter, &item)) {
509 case GST_ITERATOR_OK:
510 element = g_value_get_object (&item);
511 element_name = debug_dump_make_object_name (GST_OBJECT (element));
513 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
514 state_name = debug_dump_get_element_state (GST_ELEMENT (element));
516 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
517 param_name = debug_dump_get_element_params (GST_ELEMENT (element));
520 g_string_append_printf (str, "%ssubgraph cluster_%s {\n", spc,
522 g_string_append_printf (str, "%s fontname=\"Bitstream Vera Sans\";\n",
524 g_string_append_printf (str, "%s fontsize=\"8\";\n", spc);
525 g_string_append_printf (str, "%s style=filled;\n", spc);
526 g_string_append_printf (str, "%s color=black;\n\n", spc);
527 g_string_append_printf (str, "%s label=\"%s\\n%s%s%s\";\n", spc,
528 G_OBJECT_TYPE_NAME (element), GST_OBJECT_NAME (element),
529 (state_name ? state_name : ""), (param_name ? param_name : "")
539 g_free (element_name);
541 src_pads = sink_pads = 0;
542 if ((pad_iter = gst_element_iterate_sink_pads (element))) {
543 debug_dump_element_pads (pad_iter, pad, element, details, str,
544 indent, &src_pads, &sink_pads);
545 gst_iterator_free (pad_iter);
547 if ((pad_iter = gst_element_iterate_src_pads (element))) {
548 debug_dump_element_pads (pad_iter, pad, element, details, str,
549 indent, &src_pads, &sink_pads);
550 gst_iterator_free (pad_iter);
552 if (GST_IS_BIN (element)) {
553 g_string_append_printf (str, "%s fillcolor=\"#ffffff\";\n", spc);
555 debug_dump_element (GST_BIN (element), details, str, indent + 1);
557 if (src_pads && !sink_pads)
558 g_string_append_printf (str, "%s fillcolor=\"#ffaaaa\";\n", spc);
559 else if (!src_pads && sink_pads)
560 g_string_append_printf (str, "%s fillcolor=\"#aaaaff\";\n", spc);
561 else if (src_pads && sink_pads)
562 g_string_append_printf (str, "%s fillcolor=\"#aaffaa\";\n", spc);
564 g_string_append_printf (str, "%s fillcolor=\"#ffffff\";\n", spc);
566 g_string_append_printf (str, "%s}\n\n", spc);
567 if ((pad_iter = gst_element_iterate_pads (element))) {
570 switch (gst_iterator_next (pad_iter, &item2)) {
571 case GST_ITERATOR_OK:
572 pad = g_value_get_object (&item2);
573 if (gst_pad_is_linked (pad)) {
574 if (gst_pad_get_direction (pad) == GST_PAD_SRC) {
575 debug_dump_element_pad_link (pad, element, details, str,
578 GstPad *peer_pad = gst_pad_get_peer (pad);
581 if (!GST_IS_GHOST_PAD (peer_pad)
582 && GST_IS_PROXY_PAD (peer_pad)) {
583 debug_dump_element_pad_link (peer_pad, NULL, details,
586 gst_object_unref (peer_pad);
590 g_value_reset (&item2);
592 case GST_ITERATOR_RESYNC:
593 gst_iterator_resync (pad_iter);
595 case GST_ITERATOR_ERROR:
596 case GST_ITERATOR_DONE:
601 g_value_unset (&item2);
602 gst_iterator_free (pad_iter);
604 g_value_reset (&item);
606 case GST_ITERATOR_RESYNC:
607 gst_iterator_resync (element_iter);
609 case GST_ITERATOR_ERROR:
610 case GST_ITERATOR_DONE:
611 elements_done = TRUE;
616 g_value_unset (&item);
617 gst_iterator_free (element_iter);
621 debug_dump_header (GstBin * bin, GstDebugGraphDetails details, GString * str)
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));
634 g_string_append_printf (str,
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"
648 " margin=\"0.05,0.05\",\n"
649 " label=\"Legend\\lElement-States: [~] void-pending, [0] null, [-] ready, [=] paused, [>] playing\\lPad-Activation: [-] none, [>] push, [<] pull\\lPad-Flags: [b]locked, [f]lushing, [b]locking; upper-case is set\\lPad-Task: [T] has started task, [t] has paused task\\l\"\n,"
651 "\n", G_OBJECT_TYPE_NAME (bin), GST_OBJECT_NAME (bin),
652 (state_name ? state_name : ""), (param_name ? param_name : "")
662 debug_dump_footer (GString * str)
664 g_string_append_printf (str, "}\n");
668 * gst_debug_bin_to_dot_data:
669 * @bin: the top-level pipeline that should be analyzed
671 * To aid debugging applications one can use this method to obtain the whole
672 * network of gstreamer elements that form the pipeline into an dot file.
673 * This data can be processed with graphviz to get an image.
675 * Returns: (transfer full): a string containing the pipeline in graphviz
679 gst_debug_bin_to_dot_data (GstBin * bin, GstDebugGraphDetails details)
683 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
685 str = g_string_new (NULL);
687 debug_dump_header (bin, details, str);
688 debug_dump_element (bin, details, str, 1);
689 debug_dump_footer (str);
691 return g_string_free (str, FALSE);
695 * gst_debug_bin_to_dot_file:
696 * @bin: the top-level pipeline that should be analyzed
697 * @file_name: output base filename (e.g. "myplayer")
699 * To aid debugging applications one can use this method to write out the whole
700 * network of gstreamer elements that form the pipeline into an dot file.
701 * This file can be processed with graphviz to get an image.
702 * <informalexample><programlisting>
703 * dot -Tpng -oimage.png graph_lowlevel.dot
704 * </programlisting></informalexample>
707 gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
708 const gchar * file_name)
710 gchar *full_file_name = NULL;
713 g_return_if_fail (GST_IS_BIN (bin));
715 if (G_LIKELY (priv_gst_dump_dot_dir == NULL))
719 file_name = g_get_application_name ();
721 file_name = "unnamed";
724 full_file_name = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.dot",
725 priv_gst_dump_dot_dir, file_name);
727 if ((out = fopen (full_file_name, "wb"))) {
730 buf = gst_debug_bin_to_dot_data (bin, details);
736 GST_INFO ("wrote bin graph to : '%s'", full_file_name);
738 GST_WARNING ("Failed to open file '%s' for writing: %s", full_file_name,
741 g_free (full_file_name);
745 * gst_debug_bin_to_dot_file_with_ts:
746 * @bin: the top-level pipeline that should be analyzed
747 * @file_name: output base filename (e.g. "myplayer")
749 * This works like gst_debug_bin_to_dot_file(), but adds the current timestamp
750 * to the filename, so that it can be used to take multiple snapshots.
753 gst_debug_bin_to_dot_file_with_ts (GstBin * bin,
754 GstDebugGraphDetails details, const gchar * file_name)
756 gchar *ts_file_name = NULL;
757 GstClockTime elapsed;
759 g_return_if_fail (GST_IS_BIN (bin));
762 file_name = g_get_application_name ();
764 file_name = "unnamed";
768 elapsed = GST_CLOCK_DIFF (_priv_gst_info_start_time,
769 gst_util_get_timestamp ());
771 /* we don't use GST_TIME_FORMAT as such filenames would fail on some
772 * filesystems like fat */
774 g_strdup_printf ("%u.%02u.%02u.%09u-%s", GST_TIME_ARGS (elapsed),
777 gst_debug_bin_to_dot_file (bin, details, ts_file_name);
778 g_free (ts_file_name);
780 #else /* !GST_DISABLE_GST_DEBUG */
781 #ifndef GST_REMOVE_DISABLED
783 gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
784 const gchar * file_name)
789 gst_debug_bin_to_dot_file_with_ts (GstBin * bin, GstDebugGraphDetails details,
790 const gchar * file_name)
793 #endif /* GST_REMOVE_DISABLED */
794 #endif /* GST_DISABLE_GST_DEBUG */