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
32 #include "gst_private.h"
33 #include "gstdebugutils.h"
35 #ifndef GST_DISABLE_GST_DEBUG
44 #include "gstobject.h"
45 #include "gstghostpad.h"
49 /*** PIPELINE GRAPHS **********************************************************/
51 const gchar *priv_gst_dump_dot_dir; /* NULL *//* set from gst.c */
53 extern GstClockTime _priv_gst_info_start_time;
56 debug_dump_make_object_name (GstObject * element)
58 return g_strcanon (g_strdup_printf ("%s_%p", GST_OBJECT_NAME (element),
59 element), G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "_", '_');
63 debug_dump_get_element_state (GstElement * element)
65 gchar *state_name = NULL;
66 const gchar *state_icons = "~0-=>";
67 GstState state = 0, pending = 0;
69 gst_element_get_state (element, &state, &pending, 0);
70 if (pending == GST_STATE_VOID_PENDING) {
71 state_name = g_strdup_printf ("\\n[%c]", state_icons[state]);
73 state_name = g_strdup_printf ("\\n[%c]->[%c]", state_icons[state],
74 state_icons[pending]);
80 debug_dump_get_element_params (GstElement * element)
82 gchar *param_name = NULL;
83 GParamSpec **properties, *property;
84 GValue value = { 0, };
85 guint i, number_of_properties;
86 gchar *tmp, *value_str;
88 /* get paramspecs and show non-default properties */
90 g_object_class_list_properties (G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS
91 (element)), &number_of_properties);
93 for (i = 0; i < number_of_properties; i++) {
94 property = properties[i];
96 /* ski some properties */
97 if (!(property->flags & G_PARAM_READABLE))
99 if (!strcmp (property->name, "name"))
102 g_value_init (&value, property->value_type);
103 g_object_get_property (G_OBJECT (element), property->name, &value);
104 if (!(g_param_value_defaults (property, &value))) {
105 tmp = g_strdup_value_contents (&value);
106 value_str = g_strescape (tmp, NULL);
110 param_name = g_strdup_printf ("%s\\n%s=%s",
111 tmp, property->name, value_str);
114 param_name = g_strdup_printf ("\\n%s=%s", property->name, value_str);
118 g_value_unset (&value);
126 debug_dump_element_pad (GstPad * pad, GstElement * element,
127 GstDebugGraphDetails details, FILE * out, const gint indent)
129 GstElement *target_element;
130 GstPad *target_pad, *tmp_pad;
132 GstPadTemplate *pad_templ;
133 GstPadPresence presence;
134 gchar *pad_name, *element_name;
135 gchar *target_pad_name, *target_element_name;
136 gchar *color_name, *style_name;
139 spc = g_malloc (1 + indent * 2);
140 memset (spc, 32, indent * 2);
141 spc[indent * 2] = '\0';
143 dir = gst_pad_get_direction (pad);
144 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
145 element_name = debug_dump_make_object_name (GST_OBJECT (element));
146 if (GST_IS_GHOST_PAD (pad)) {
148 (dir == GST_PAD_SRC) ? "#ffdddd" : ((dir ==
149 GST_PAD_SINK) ? "#ddddff" : "#ffffff");
150 /* output target-pad so that it belongs to this element */
151 if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad)))) {
152 if ((target_pad = gst_pad_get_peer (tmp_pad))) {
153 target_pad_name = debug_dump_make_object_name (GST_OBJECT (target_pad));
154 if ((target_element = gst_pad_get_parent_element (target_pad))) {
155 target_element_name =
156 debug_dump_make_object_name (GST_OBJECT (target_element));
158 target_element_name = "";
160 style_name = "filled,solid";
161 if ((pad_templ = gst_pad_get_pad_template (target_pad))) {
162 presence = GST_PAD_TEMPLATE_PRESENCE (pad_templ);
163 if (presence == GST_PAD_SOMETIMES) {
164 style_name = "filled,dotted";
165 } else if (presence == GST_PAD_REQUEST) {
166 style_name = "filled,dashed";
170 "%s %s_%s [color=black, fillcolor=\"%s\", label=\"%s\", height=\"0.2\", style=\"%s\"];\n",
171 spc, target_element_name, target_pad_name, color_name,
172 GST_OBJECT_NAME (target_pad), style_name);
173 g_free (target_pad_name);
174 if (target_element) {
175 g_free (target_element_name);
176 gst_object_unref (target_element);
178 gst_object_unref (target_pad);
180 gst_object_unref (tmp_pad);
184 (dir == GST_PAD_SRC) ? "#ffaaaa" : ((dir ==
185 GST_PAD_SINK) ? "#aaaaff" : "#cccccc");
188 style_name = "filled,solid";
189 if ((pad_templ = gst_pad_get_pad_template (pad))) {
190 presence = GST_PAD_TEMPLATE_PRESENCE (pad_templ);
191 if (presence == GST_PAD_SOMETIMES) {
192 style_name = "filled,dotted";
193 } else if (presence == GST_PAD_REQUEST) {
194 style_name = "filled,dashed";
198 "%s %s_%s [color=black, fillcolor=\"%s\", label=\"%s\", height=\"0.2\", style=\"%s\"];\n",
199 spc, element_name, pad_name, color_name, GST_OBJECT_NAME (pad),
203 g_free (element_name);
208 debug_dump_describe_caps (GstCaps * caps, GstDebugGraphDetails details,
209 gboolean * need_free)
213 if (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS) {
214 gchar *tmp = gst_caps_to_string (caps);
215 gboolean in_bracket = FALSE;
216 gint i, sl = strlen (tmp);
218 for (i = 0; i < sl; i++) {
243 media = g_strescape (tmp, NULL);
248 if (GST_CAPS_IS_SIMPLE (caps))
250 (gchar *) gst_structure_get_name (gst_caps_get_structure (caps, 0));
259 debug_dump_element_pad_link (GstPad * pad, GstElement * element,
260 GstDebugGraphDetails details, FILE * out, const gint indent)
262 GstElement *peer_element, *target_element;
263 GstPad *peer_pad, *target_pad, *tmp_pad;
264 GstCaps *caps, *peer_caps;
265 gboolean free_caps, free_peer_caps, free_media;
267 gchar *pad_name, *element_name;
268 gchar *peer_pad_name, *peer_element_name;
269 gchar *target_pad_name, *target_element_name;
272 spc = g_malloc (1 + indent * 2);
273 memset (spc, 32, indent * 2);
274 spc[indent * 2] = '\0';
276 if ((peer_pad = gst_pad_get_peer (pad))) {
278 if ((details & GST_DEBUG_GRAPH_SHOW_MEDIA_TYPE) ||
279 (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS)
281 if ((caps = gst_pad_get_negotiated_caps (pad))) {
285 if (!(caps = (GstCaps *)
286 gst_pad_get_pad_template_caps (pad))) {
287 /* this should not happen */
291 if ((peer_caps = gst_pad_get_negotiated_caps (peer_pad))) {
292 free_peer_caps = TRUE;
294 free_peer_caps = FALSE;
295 peer_caps = (GstCaps *) gst_pad_get_pad_template_caps (peer_pad);
298 media = debug_dump_describe_caps (caps, details, &free_media);
299 /* check if peer caps are different */
300 if (peer_caps && !gst_caps_is_equal (caps, peer_caps)) {
301 gchar *old_media = media;
305 tmp = debug_dump_describe_caps (peer_caps, details, &free_tmp);
306 if (gst_pad_get_direction (pad) == GST_PAD_SRC) {
307 media = g_strdup_printf ("%s\\n---\\n%s", media, tmp);
309 media = g_strdup_printf ("%s\\n---\\n%s", tmp, media);
318 gst_caps_unref (caps);
321 if (free_peer_caps && peer_caps) {
322 gst_caps_unref (peer_caps);
326 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
328 element_name = debug_dump_make_object_name (GST_OBJECT (element));
332 peer_pad_name = debug_dump_make_object_name (GST_OBJECT (peer_pad));
333 if ((peer_element = gst_pad_get_parent_element (peer_pad))) {
335 debug_dump_make_object_name (GST_OBJECT (peer_element));
337 peer_element_name = "";
340 if (GST_IS_GHOST_PAD (pad)) {
341 if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad)))) {
342 if ((target_pad = gst_pad_get_peer (tmp_pad))) {
344 debug_dump_make_object_name (GST_OBJECT (target_pad));
345 if ((target_element = gst_pad_get_parent_element (target_pad))) {
346 target_element_name =
347 debug_dump_make_object_name (GST_OBJECT (target_element));
349 target_element_name = "";
351 /* src ghostpad relationship */
352 fprintf (out, "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
353 target_element_name, target_pad_name, element_name, pad_name);
355 g_free (target_pad_name);
356 if (target_element) {
357 g_free (target_element_name);
358 gst_object_unref (target_element);
360 gst_object_unref (target_pad);
362 gst_object_unref (tmp_pad);
365 if (GST_IS_GHOST_PAD (peer_pad)) {
366 if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (peer_pad)))) {
367 if ((target_pad = gst_pad_get_peer (tmp_pad))) {
369 debug_dump_make_object_name (GST_OBJECT (target_pad));
370 if ((target_element = gst_pad_get_parent_element (target_pad))) {
371 target_element_name =
372 debug_dump_make_object_name (GST_OBJECT (target_element));
374 target_element_name = "";
376 /* sink ghostpad relationship */
377 fprintf (out, "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
378 peer_element_name, peer_pad_name,
379 target_element_name, target_pad_name);
380 /* FIXME: we are missing links from the proxy pad
381 * theoretically we need to:
382 * pad=gst_object_ref(target_pad);
383 * goto line 280: if ((peer_pad = gst_pad_get_peer (pad)))
384 * as this would e ugly we need to refactor ...
386 debug_dump_element_pad_link (target_pad, target_element, details, out,
388 g_free (target_pad_name);
389 if (target_element) {
390 g_free (target_element_name);
391 gst_object_unref (target_element);
393 gst_object_unref (target_pad);
395 gst_object_unref (tmp_pad);
401 fprintf (out, "%s%s_%s -> %s_%s [label=\"%s\"]\n", spc,
402 element_name, pad_name, peer_element_name, peer_pad_name, media);
407 fprintf (out, "%s%s_%s -> %s_%s\n", spc,
408 element_name, pad_name, peer_element_name, peer_pad_name);
413 g_free (element_name);
415 g_free (peer_pad_name);
417 g_free (peer_element_name);
418 gst_object_unref (peer_element);
420 gst_object_unref (peer_pad);
426 * debug_dump_element:
427 * @bin: the bin that should be analyzed
428 * @out: file to write to
429 * @indent: level of graph indentation
431 * Helper for _gst_debug_bin_to_dot_file() to recursively dump a pipeline.
434 debug_dump_element (GstBin * bin, GstDebugGraphDetails details, FILE * out,
437 GstIterator *element_iter, *pad_iter;
438 gboolean elements_done, pads_done;
442 guint src_pads, sink_pads;
444 gchar *state_name = NULL;
445 gchar *param_name = NULL;
448 spc = g_malloc (1 + indent * 2);
449 memset (spc, 32, indent * 2);
450 spc[indent * 2] = '\0';
452 element_iter = gst_bin_iterate_elements (bin);
453 elements_done = FALSE;
454 while (!elements_done) {
455 switch (gst_iterator_next (element_iter, (gpointer) & element)) {
456 case GST_ITERATOR_OK:
457 element_name = debug_dump_make_object_name (GST_OBJECT (element));
459 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
460 state_name = debug_dump_get_element_state (GST_ELEMENT (element));
462 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
463 param_name = debug_dump_get_element_params (GST_ELEMENT (element));
466 fprintf (out, "%ssubgraph cluster_%s {\n", spc, element_name);
467 fprintf (out, "%s fontname=\"Bitstream Vera Sans\";\n", spc);
468 fprintf (out, "%s fontsize=\"8\";\n", spc);
469 fprintf (out, "%s style=filled;\n", spc);
470 fprintf (out, "%s color=black;\n\n", spc);
471 fprintf (out, "%s label=\"%s\\n%s%s%s\";\n", spc,
472 G_OBJECT_TYPE_NAME (element), GST_OBJECT_NAME (element),
473 (state_name ? state_name : ""), (param_name ? param_name : "")
483 g_free (element_name);
485 src_pads = sink_pads = 0;
486 if ((pad_iter = gst_element_iterate_pads (element))) {
489 switch (gst_iterator_next (pad_iter, (gpointer) & pad)) {
490 case GST_ITERATOR_OK:
491 debug_dump_element_pad (pad, element, details, out, indent);
492 dir = gst_pad_get_direction (pad);
493 if (dir == GST_PAD_SRC)
495 else if (dir == GST_PAD_SINK)
497 gst_object_unref (pad);
499 case GST_ITERATOR_RESYNC:
500 gst_iterator_resync (pad_iter);
502 case GST_ITERATOR_ERROR:
503 case GST_ITERATOR_DONE:
508 gst_iterator_free (pad_iter);
510 if (GST_IS_BIN (element)) {
511 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
513 debug_dump_element (GST_BIN (element), details, out, indent + 1);
515 if (src_pads && !sink_pads)
516 fprintf (out, "%s fillcolor=\"#ffaaaa\";\n", spc);
517 else if (!src_pads && sink_pads)
518 fprintf (out, "%s fillcolor=\"#aaaaff\";\n", spc);
519 else if (src_pads && sink_pads)
520 fprintf (out, "%s fillcolor=\"#aaffaa\";\n", spc);
522 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
524 fprintf (out, "%s}\n\n", spc);
525 if ((pad_iter = gst_element_iterate_pads (element))) {
528 switch (gst_iterator_next (pad_iter, (gpointer) & pad)) {
529 case GST_ITERATOR_OK:
530 if (gst_pad_is_linked (pad)
531 && gst_pad_get_direction (pad) == GST_PAD_SRC) {
532 debug_dump_element_pad_link (pad, element, details, out,
535 gst_object_unref (pad);
537 case GST_ITERATOR_RESYNC:
538 gst_iterator_resync (pad_iter);
540 case GST_ITERATOR_ERROR:
541 case GST_ITERATOR_DONE:
546 gst_iterator_free (pad_iter);
548 gst_object_unref (element);
550 case GST_ITERATOR_RESYNC:
551 gst_iterator_resync (element_iter);
553 case GST_ITERATOR_ERROR:
554 case GST_ITERATOR_DONE:
555 elements_done = TRUE;
559 gst_iterator_free (element_iter);
564 * _gst_debug_bin_to_dot_file:
565 * @bin: the top-level pipeline that should be analyzed
566 * @file_name: output base filename (e.g. "myplayer")
568 * To aid debugging applications one can use this method to write out the whole
569 * network of gstreamer elements that form the pipeline into an dot file.
570 * This file can be processed with graphviz to get an image.
571 * <informalexample><programlisting>
572 * dot -Tpng -oimage.png graph_lowlevel.dot
573 * </programlisting></informalexample>
576 _gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
577 const gchar * file_name)
579 gchar *full_file_name = NULL;
582 g_return_if_fail (GST_IS_BIN (bin));
584 if (G_LIKELY (priv_gst_dump_dot_dir == NULL))
588 file_name = g_get_application_name ();
590 file_name = "unnamed";
593 full_file_name = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.dot",
594 priv_gst_dump_dot_dir, file_name);
596 if ((out = fopen (full_file_name, "wb"))) {
597 gchar *state_name = NULL;
598 gchar *param_name = NULL;
600 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
601 state_name = debug_dump_get_element_state (GST_ELEMENT (bin));
603 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
604 param_name = debug_dump_get_element_params (GST_ELEMENT (bin));
609 "digraph pipeline {\n"
611 " fontname=\"Bitstream Vera Sans\";\n"
616 " label=\"<%s>\\n%s%s%s\";\n"
617 " node [style=filled, shape=box, fontsize=\"7\", fontname=\"Bitstream Vera Sans\", margin=\"0.0,0.0\"];\n"
618 " edge [labelfontsize=\"7\", fontsize=\"7\", labelfontname=\"Bitstream Vera Sans\", fontname=\"Bitstream Vera Sans\"];\n"
619 "\n", G_OBJECT_TYPE_NAME (bin), GST_OBJECT_NAME (bin),
620 (state_name ? state_name : ""), (param_name ? param_name : "")
627 debug_dump_element (bin, details, out, 1);
630 fprintf (out, "}\n");
632 GST_INFO ("wrote bin graph to : '%s'", full_file_name);
634 GST_WARNING ("Failed to open file '%s' for writing: %s", full_file_name,
637 g_free (full_file_name);
641 * _gst_debug_bin_to_dot_file_with_ts:
642 * @bin: the top-level pipeline that should be analyzed
643 * @file_name: output base filename (e.g. "myplayer")
645 * This works like _gst_debug_bin_to_dot_file(), but adds the current timestamp
646 * to the filename, so that it can be used to take multiple snapshots.
649 _gst_debug_bin_to_dot_file_with_ts (GstBin * bin, GstDebugGraphDetails details,
650 const gchar * file_name)
652 gchar *ts_file_name = NULL;
653 GstClockTime elapsed;
655 g_return_if_fail (GST_IS_BIN (bin));
658 file_name = g_get_application_name ();
660 file_name = "unnamed";
664 elapsed = GST_CLOCK_DIFF (_priv_gst_info_start_time,
665 gst_util_get_timestamp ());
667 /* we don't use GST_TIME_FORMAT as such filenames would fail on some
668 * filesystems like fat */
670 g_strdup_printf ("%u.%02u.%02u.%09u-%s", GST_TIME_ARGS (elapsed),
673 _gst_debug_bin_to_dot_file (bin, details, ts_file_name);
674 g_free (ts_file_name);
677 #endif /* GST_DISABLE_GST_DEBUG */