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_element_pad_link (GstPad * pad, GstElement * element,
209 GstDebugGraphDetails details, FILE * out, const gint indent)
211 GstElement *peer_element, *target_element;
212 GstPad *peer_pad, *target_pad, *tmp_pad;
214 GstStructure *structure;
215 gboolean free_caps, free_media;
217 gchar *pad_name, *element_name;
218 gchar *peer_pad_name, *peer_element_name;
219 gchar *target_pad_name, *target_element_name;
222 spc = g_malloc (1 + indent * 2);
223 memset (spc, 32, indent * 2);
224 spc[indent * 2] = '\0';
226 if ((peer_pad = gst_pad_get_peer (pad))) {
228 if ((details & GST_DEBUG_GRAPH_SHOW_MEDIA_TYPE) ||
229 (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS)
231 if ((caps = gst_pad_get_negotiated_caps (pad))) {
235 if (!(caps = (GstCaps *)
236 gst_pad_get_pad_template_caps (pad))) {
237 /* this should not happen */
242 if (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS) {
243 gchar *tmp = g_strdelimit (gst_caps_to_string (caps), ",",
246 media = g_strescape (tmp, NULL);
250 if (GST_CAPS_IS_SIMPLE (caps)) {
251 structure = gst_caps_get_structure (caps, 0);
252 media = (gchar *) gst_structure_get_name (structure);
257 gst_caps_unref (caps);
262 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
264 element_name = debug_dump_make_object_name (GST_OBJECT (element));
268 peer_pad_name = debug_dump_make_object_name (GST_OBJECT (peer_pad));
269 if ((peer_element = gst_pad_get_parent_element (peer_pad))) {
271 debug_dump_make_object_name (GST_OBJECT (peer_element));
273 peer_element_name = "";
276 if (GST_IS_GHOST_PAD (pad)) {
277 if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad)))) {
278 if ((target_pad = gst_pad_get_peer (tmp_pad))) {
280 debug_dump_make_object_name (GST_OBJECT (target_pad));
281 if ((target_element = gst_pad_get_parent_element (target_pad))) {
282 target_element_name =
283 debug_dump_make_object_name (GST_OBJECT (target_element));
285 target_element_name = "";
287 /* src ghostpad relationship */
288 fprintf (out, "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
289 target_element_name, target_pad_name, element_name, pad_name);
291 g_free (target_pad_name);
292 if (target_element) {
293 g_free (target_element_name);
294 gst_object_unref (target_element);
296 gst_object_unref (target_pad);
298 gst_object_unref (tmp_pad);
301 if (GST_IS_GHOST_PAD (peer_pad)) {
302 if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (peer_pad)))) {
303 if ((target_pad = gst_pad_get_peer (tmp_pad))) {
305 debug_dump_make_object_name (GST_OBJECT (target_pad));
306 if ((target_element = gst_pad_get_parent_element (target_pad))) {
307 target_element_name =
308 debug_dump_make_object_name (GST_OBJECT (target_element));
310 target_element_name = "";
312 /* sink ghostpad relationship */
313 fprintf (out, "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
314 peer_element_name, peer_pad_name,
315 target_element_name, target_pad_name);
316 /* FIXME: we are missing links from the proxy pad
317 * theoretically we need to:
318 * pad=gst_object_ref(target_pad);
319 * goto line 280: if ((peer_pad = gst_pad_get_peer (pad)))
320 * as this would e ugly we need to refactor ...
322 debug_dump_element_pad_link (target_pad, target_element, details, out,
324 g_free (target_pad_name);
325 if (target_element) {
326 g_free (target_element_name);
327 gst_object_unref (target_element);
329 gst_object_unref (target_pad);
331 gst_object_unref (tmp_pad);
337 fprintf (out, "%s%s_%s -> %s_%s [label=\"%s\"]\n", spc,
338 element_name, pad_name, peer_element_name, peer_pad_name, media);
343 fprintf (out, "%s%s_%s -> %s_%s\n", spc,
344 element_name, pad_name, peer_element_name, peer_pad_name);
349 g_free (element_name);
351 g_free (peer_pad_name);
353 g_free (peer_element_name);
354 gst_object_unref (peer_element);
356 gst_object_unref (peer_pad);
362 * debug_dump_element:
363 * @bin: the bin that should be analyzed
364 * @out: file to write to
365 * @indent: level of graph indentation
367 * Helper for _gst_debug_bin_to_dot_file() to recursively dump a pipeline.
370 debug_dump_element (GstBin * bin, GstDebugGraphDetails details, FILE * out,
373 GstIterator *element_iter, *pad_iter;
374 gboolean elements_done, pads_done;
378 guint src_pads, sink_pads;
380 gchar *state_name = NULL;
381 gchar *param_name = NULL;
384 spc = g_malloc (1 + indent * 2);
385 memset (spc, 32, indent * 2);
386 spc[indent * 2] = '\0';
388 element_iter = gst_bin_iterate_elements (bin);
389 elements_done = FALSE;
390 while (!elements_done) {
391 switch (gst_iterator_next (element_iter, (gpointer) & element)) {
392 case GST_ITERATOR_OK:
393 element_name = debug_dump_make_object_name (GST_OBJECT (element));
395 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
396 state_name = debug_dump_get_element_state (GST_ELEMENT (element));
398 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
399 param_name = debug_dump_get_element_params (GST_ELEMENT (element));
402 fprintf (out, "%ssubgraph cluster_%s {\n", spc, element_name);
403 fprintf (out, "%s fontname=\"Bitstream Vera Sans\";\n", spc);
404 fprintf (out, "%s fontsize=\"8\";\n", spc);
405 fprintf (out, "%s style=filled;\n", spc);
406 fprintf (out, "%s color=black;\n\n", spc);
407 fprintf (out, "%s label=\"%s\\n%s%s%s\";\n", spc,
408 G_OBJECT_TYPE_NAME (element), GST_OBJECT_NAME (element),
409 (state_name ? state_name : ""), (param_name ? param_name : "")
419 g_free (element_name);
421 src_pads = sink_pads = 0;
422 if ((pad_iter = gst_element_iterate_pads (element))) {
425 switch (gst_iterator_next (pad_iter, (gpointer) & pad)) {
426 case GST_ITERATOR_OK:
427 debug_dump_element_pad (pad, element, details, out, indent);
428 dir = gst_pad_get_direction (pad);
429 if (dir == GST_PAD_SRC)
431 else if (dir == GST_PAD_SINK)
433 gst_object_unref (pad);
435 case GST_ITERATOR_RESYNC:
436 gst_iterator_resync (pad_iter);
438 case GST_ITERATOR_ERROR:
439 case GST_ITERATOR_DONE:
444 gst_iterator_free (pad_iter);
446 if (GST_IS_BIN (element)) {
447 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
449 debug_dump_element (GST_BIN (element), details, out, indent + 1);
451 if (src_pads && !sink_pads)
452 fprintf (out, "%s fillcolor=\"#ffaaaa\";\n", spc);
453 else if (!src_pads && sink_pads)
454 fprintf (out, "%s fillcolor=\"#aaaaff\";\n", spc);
455 else if (src_pads && sink_pads)
456 fprintf (out, "%s fillcolor=\"#aaffaa\";\n", spc);
458 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
460 fprintf (out, "%s}\n\n", spc);
461 if ((pad_iter = gst_element_iterate_pads (element))) {
464 switch (gst_iterator_next (pad_iter, (gpointer) & pad)) {
465 case GST_ITERATOR_OK:
466 if (gst_pad_is_linked (pad)
467 && gst_pad_get_direction (pad) == GST_PAD_SRC) {
468 debug_dump_element_pad_link (pad, element, details, out,
471 gst_object_unref (pad);
473 case GST_ITERATOR_RESYNC:
474 gst_iterator_resync (pad_iter);
476 case GST_ITERATOR_ERROR:
477 case GST_ITERATOR_DONE:
482 gst_iterator_free (pad_iter);
484 gst_object_unref (element);
486 case GST_ITERATOR_RESYNC:
487 gst_iterator_resync (element_iter);
489 case GST_ITERATOR_ERROR:
490 case GST_ITERATOR_DONE:
491 elements_done = TRUE;
495 gst_iterator_free (element_iter);
500 * _gst_debug_bin_to_dot_file:
501 * @bin: the top-level pipeline that should be analyzed
502 * @file_name: output base filename (e.g. "myplayer")
504 * To aid debugging applications one can use this method to write out the whole
505 * network of gstreamer elements that form the pipeline into an dot file.
506 * This file can be processed with graphviz to get an image.
507 * <informalexample><programlisting>
508 * dot -Tpng -oimage.png graph_lowlevel.dot
509 * </programlisting></informalexample>
512 _gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
513 const gchar * file_name)
515 gchar *full_file_name = NULL;
518 g_return_if_fail (GST_IS_BIN (bin));
520 if (G_LIKELY (priv_gst_dump_dot_dir == NULL))
524 file_name = g_get_application_name ();
526 file_name = "unnamed";
529 full_file_name = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.dot",
530 priv_gst_dump_dot_dir, file_name);
532 if ((out = fopen (full_file_name, "wb"))) {
533 gchar *state_name = NULL;
534 gchar *param_name = NULL;
536 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
537 state_name = debug_dump_get_element_state (GST_ELEMENT (bin));
539 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
540 param_name = debug_dump_get_element_params (GST_ELEMENT (bin));
545 "digraph pipeline {\n"
547 " fontname=\"Bitstream Vera Sans\";\n"
552 " label=\"<%s>\\n%s%s%s\";\n"
553 " node [style=filled, shape=box, fontsize=\"7\", fontname=\"Bitstream Vera Sans\", margin=\"0.0,0.0\"];\n"
554 " edge [labelfontsize=\"7\", fontsize=\"7\", labelfontname=\"Bitstream Vera Sans\", fontname=\"Bitstream Vera Sans\"];\n"
555 "\n", G_OBJECT_TYPE_NAME (bin), GST_OBJECT_NAME (bin),
556 (state_name ? state_name : ""), (param_name ? param_name : "")
563 debug_dump_element (bin, details, out, 1);
566 fprintf (out, "}\n");
568 GST_INFO ("wrote bin graph to : '%s'", full_file_name);
570 GST_WARNING ("Failed to open file '%s' for writing: %s", full_file_name,
573 g_free (full_file_name);
577 * _gst_debug_bin_to_dot_file_with_ts:
578 * @bin: the top-level pipeline that should be analyzed
579 * @file_name: output base filename (e.g. "myplayer")
581 * This works like _gst_debug_bin_to_dot_file(), but adds the current timestamp
582 * to the filename, so that it can be used to take multiple snapshots.
585 _gst_debug_bin_to_dot_file_with_ts (GstBin * bin, GstDebugGraphDetails details,
586 const gchar * file_name)
588 gchar *ts_file_name = NULL;
589 GstClockTime elapsed;
591 g_return_if_fail (GST_IS_BIN (bin));
594 file_name = g_get_application_name ();
596 file_name = "unnamed";
600 elapsed = GST_CLOCK_DIFF (_priv_gst_info_start_time,
601 gst_util_get_timestamp ());
603 g_strdup_printf ("%" GST_TIME_FORMAT "-%s", GST_TIME_ARGS (elapsed),
606 _gst_debug_bin_to_dot_file (bin, details, ts_file_name);
607 g_free (ts_file_name);
610 #endif /* GST_DISABLE_GST_DEBUG */