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
27 #include "gst_private.h"
28 #include "gstdebugutils.h"
30 #ifndef GST_DISABLE_GST_DEBUG
39 #include "gstobject.h"
40 #include "gstghostpad.h"
44 /*** PIPELINE GRAPHS **********************************************************/
46 const gchar *priv_gst_dump_dot_dir; /* NULL *//* set from gst.c */
48 extern GstClockTime _priv_gst_info_start_time;
51 debug_dump_make_object_name (GstObject * element)
53 return g_strcanon (g_strdup_printf ("%s_%p", GST_OBJECT_NAME (element),
54 element), G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "_", '_');
58 debug_dump_get_element_state (GstElement * element)
60 gchar *state_name = NULL;
61 const gchar *state_icons = "~0-=>";
62 GstState state = 0, pending = 0;
64 gst_element_get_state (element, &state, &pending, 0);
65 if (pending == GST_STATE_VOID_PENDING) {
66 state_name = g_strdup_printf ("\\n[%c]", state_icons[state]);
68 state_name = g_strdup_printf ("\\n[%c]->[%c]", state_icons[state],
69 state_icons[pending]);
75 debug_dump_get_element_params (GstElement * element)
77 gchar *param_name = NULL;
78 GParamSpec **properties, *property;
79 GValue value = { 0, };
80 guint i, number_of_properties;
81 gchar *tmp, *value_str;
83 /* get paramspecs and show non-default properties */
85 g_object_class_list_properties (G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS
86 (element)), &number_of_properties);
88 for (i = 0; i < number_of_properties; i++) {
89 property = properties[i];
91 /* ski some properties */
92 if (!(property->flags & G_PARAM_READABLE))
94 if (!strcmp (property->name, "name"))
97 g_value_init (&value, property->value_type);
98 g_object_get_property (G_OBJECT (element), property->name, &value);
99 if (!(g_param_value_defaults (property, &value))) {
100 tmp = g_strdup_value_contents (&value);
101 value_str = g_strescape (tmp, NULL);
105 param_name = g_strdup_printf ("%s\\n%s=%s",
106 tmp, property->name, value_str);
109 param_name = g_strdup_printf ("\\n%s=%s", property->name, value_str);
113 g_value_unset (&value);
121 debug_dump_element_pad (GstPad * pad, GstElement * element,
122 GstDebugGraphDetails details, FILE * out, const gint indent)
124 GstElement *target_element;
125 GstPad *target_pad, *tmp_pad;
127 GstPadTemplate *pad_templ;
128 GstPadPresence presence;
129 gchar *pad_name, *element_name;
130 gchar *target_pad_name, *target_element_name;
131 gchar *color_name, *style_name;
134 spc = g_malloc (1 + indent * 2);
135 memset (spc, 32, indent * 2);
136 spc[indent * 2] = '\0';
138 dir = gst_pad_get_direction (pad);
139 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
140 element_name = debug_dump_make_object_name (GST_OBJECT (element));
141 if (GST_IS_GHOST_PAD (pad)) {
143 (dir == GST_PAD_SRC) ? "#ffdddd" : ((dir ==
144 GST_PAD_SINK) ? "#ddddff" : "#ffffff");
145 /* output target-pad so that it belongs to this element */
146 if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad)))) {
147 if ((target_pad = gst_pad_get_peer (tmp_pad))) {
148 target_pad_name = debug_dump_make_object_name (GST_OBJECT (target_pad));
149 if ((target_element = gst_pad_get_parent_element (target_pad))) {
150 target_element_name =
151 debug_dump_make_object_name (GST_OBJECT (target_element));
153 target_element_name = "";
155 style_name = "filled,solid";
156 if ((pad_templ = gst_pad_get_pad_template (target_pad))) {
157 presence = GST_PAD_TEMPLATE_PRESENCE (pad_templ);
158 if (presence == GST_PAD_SOMETIMES) {
159 style_name = "filled,dotted";
160 } else if (presence == GST_PAD_REQUEST) {
161 style_name = "filled,dashed";
165 "%s %s_%s [color=black, fillcolor=\"%s\", label=\"%s\", height=\"0.2\", style=\"%s\"];\n",
166 spc, target_element_name, target_pad_name, color_name,
167 GST_OBJECT_NAME (target_pad), style_name);
168 g_free (target_pad_name);
169 if (target_element) {
170 g_free (target_element_name);
171 gst_object_unref (target_element);
173 gst_object_unref (target_pad);
175 gst_object_unref (tmp_pad);
179 (dir == GST_PAD_SRC) ? "#ffaaaa" : ((dir ==
180 GST_PAD_SINK) ? "#aaaaff" : "#cccccc");
183 style_name = "filled,solid";
184 if ((pad_templ = gst_pad_get_pad_template (pad))) {
185 presence = GST_PAD_TEMPLATE_PRESENCE (pad_templ);
186 if (presence == GST_PAD_SOMETIMES) {
187 style_name = "filled,dotted";
188 } else if (presence == GST_PAD_REQUEST) {
189 style_name = "filled,dashed";
193 "%s %s_%s [color=black, fillcolor=\"%s\", label=\"%s\", height=\"0.2\", style=\"%s\"];\n",
194 spc, element_name, pad_name, color_name, GST_OBJECT_NAME (pad),
198 g_free (element_name);
203 debug_dump_element_pad_link (GstPad * pad, GstElement * element,
204 GstDebugGraphDetails details, FILE * out, const gint indent)
206 GstElement *peer_element, *target_element;
207 GstPad *peer_pad, *target_pad, *tmp_pad;
209 GstStructure *structure;
210 gboolean free_caps, free_media;
212 gchar *pad_name, *element_name;
213 gchar *peer_pad_name, *peer_element_name;
214 gchar *target_pad_name, *target_element_name;
217 spc = g_malloc (1 + indent * 2);
218 memset (spc, 32, indent * 2);
219 spc[indent * 2] = '\0';
221 if ((peer_pad = gst_pad_get_peer (pad))) {
223 if ((details & GST_DEBUG_GRAPH_SHOW_MEDIA_TYPE) ||
224 (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS)
226 if ((caps = gst_pad_get_negotiated_caps (pad))) {
230 if (!(caps = (GstCaps *)
231 gst_pad_get_pad_template_caps (pad))) {
232 /* this should not happen */
237 if (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS) {
238 gchar *tmp = g_strdelimit (gst_caps_to_string (caps), ",",
241 media = g_strescape (tmp, NULL);
245 if (GST_CAPS_IS_SIMPLE (caps)) {
246 structure = gst_caps_get_structure (caps, 0);
247 media = (gchar *) gst_structure_get_name (structure);
252 gst_caps_unref (caps);
257 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
259 element_name = debug_dump_make_object_name (GST_OBJECT (element));
263 peer_pad_name = debug_dump_make_object_name (GST_OBJECT (peer_pad));
264 if ((peer_element = gst_pad_get_parent_element (peer_pad))) {
266 debug_dump_make_object_name (GST_OBJECT (peer_element));
268 peer_element_name = "";
271 if (GST_IS_GHOST_PAD (pad)) {
272 if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad)))) {
273 if ((target_pad = gst_pad_get_peer (tmp_pad))) {
275 debug_dump_make_object_name (GST_OBJECT (target_pad));
276 if ((target_element = gst_pad_get_parent_element (target_pad))) {
277 target_element_name =
278 debug_dump_make_object_name (GST_OBJECT (target_element));
280 target_element_name = "";
282 /* src ghostpad relationship */
283 fprintf (out, "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
284 target_element_name, target_pad_name, element_name, pad_name);
286 g_free (target_pad_name);
287 if (target_element) {
288 g_free (target_element_name);
289 gst_object_unref (target_element);
291 gst_object_unref (target_pad);
293 gst_object_unref (tmp_pad);
296 if (GST_IS_GHOST_PAD (peer_pad)) {
297 if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (peer_pad)))) {
298 if ((target_pad = gst_pad_get_peer (tmp_pad))) {
300 debug_dump_make_object_name (GST_OBJECT (target_pad));
301 if ((target_element = gst_pad_get_parent_element (target_pad))) {
302 target_element_name =
303 debug_dump_make_object_name (GST_OBJECT (target_element));
305 target_element_name = "";
307 /* sink ghostpad relationship */
308 fprintf (out, "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
309 peer_element_name, peer_pad_name,
310 target_element_name, target_pad_name);
311 /* FIXME: we are missing links from the proxy pad
312 * theoretically we need to:
313 * pad=gst_object_ref(target_pad);
314 * goto line 280: if ((peer_pad = gst_pad_get_peer (pad)))
315 * as this would e ugly we need to refactor ...
317 debug_dump_element_pad_link (target_pad, target_element, details, out,
319 g_free (target_pad_name);
320 if (target_element) {
321 g_free (target_element_name);
322 gst_object_unref (target_element);
324 gst_object_unref (target_pad);
326 gst_object_unref (tmp_pad);
332 fprintf (out, "%s%s_%s -> %s_%s [label=\"%s\"]\n", spc,
333 element_name, pad_name, peer_element_name, peer_pad_name, media);
338 fprintf (out, "%s%s_%s -> %s_%s\n", spc,
339 element_name, pad_name, peer_element_name, peer_pad_name);
344 g_free (element_name);
346 g_free (peer_pad_name);
348 g_free (peer_element_name);
349 gst_object_unref (peer_element);
351 gst_object_unref (peer_pad);
357 * debug_dump_element:
358 * @bin: the bin that should be analyzed
359 * @out: file to write to
360 * @indent: level of graph indentation
362 * Helper for _gst_debug_bin_to_dot_file() to recursively dump a pipeline.
365 debug_dump_element (GstBin * bin, GstDebugGraphDetails details, FILE * out,
368 GstIterator *element_iter, *pad_iter;
369 gboolean elements_done, pads_done;
373 guint src_pads, sink_pads;
375 gchar *state_name = NULL;
376 gchar *param_name = NULL;
379 spc = g_malloc (1 + indent * 2);
380 memset (spc, 32, indent * 2);
381 spc[indent * 2] = '\0';
383 element_iter = gst_bin_iterate_elements (bin);
384 elements_done = FALSE;
385 while (!elements_done) {
386 switch (gst_iterator_next (element_iter, (gpointer) & element)) {
387 case GST_ITERATOR_OK:
388 element_name = debug_dump_make_object_name (GST_OBJECT (element));
390 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
391 state_name = debug_dump_get_element_state (GST_ELEMENT (element));
393 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
394 param_name = debug_dump_get_element_params (GST_ELEMENT (element));
397 fprintf (out, "%ssubgraph cluster_%s {\n", spc, element_name);
398 fprintf (out, "%s fontname=\"Bitstream Vera Sans\";\n", spc);
399 fprintf (out, "%s fontsize=\"8\";\n", spc);
400 fprintf (out, "%s style=filled;\n", spc);
401 fprintf (out, "%s color=black;\n\n", spc);
402 fprintf (out, "%s label=\"%s\\n%s%s%s\";\n", spc,
403 G_OBJECT_TYPE_NAME (element), GST_OBJECT_NAME (element),
404 (state_name ? state_name : ""), (param_name ? param_name : "")
414 g_free (element_name);
416 src_pads = sink_pads = 0;
417 if ((pad_iter = gst_element_iterate_pads (element))) {
420 switch (gst_iterator_next (pad_iter, (gpointer) & pad)) {
421 case GST_ITERATOR_OK:
422 debug_dump_element_pad (pad, element, details, out, indent);
423 dir = gst_pad_get_direction (pad);
424 if (dir == GST_PAD_SRC)
426 else if (dir == GST_PAD_SINK)
428 gst_object_unref (pad);
430 case GST_ITERATOR_RESYNC:
431 gst_iterator_resync (pad_iter);
433 case GST_ITERATOR_ERROR:
434 case GST_ITERATOR_DONE:
439 gst_iterator_free (pad_iter);
441 if (GST_IS_BIN (element)) {
442 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
444 debug_dump_element (GST_BIN (element), details, out, indent + 1);
446 if (src_pads && !sink_pads)
447 fprintf (out, "%s fillcolor=\"#ffaaaa\";\n", spc);
448 else if (!src_pads && sink_pads)
449 fprintf (out, "%s fillcolor=\"#aaaaff\";\n", spc);
450 else if (src_pads && sink_pads)
451 fprintf (out, "%s fillcolor=\"#aaffaa\";\n", spc);
453 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
455 fprintf (out, "%s}\n\n", spc);
456 if ((pad_iter = gst_element_iterate_pads (element))) {
459 switch (gst_iterator_next (pad_iter, (gpointer) & pad)) {
460 case GST_ITERATOR_OK:
461 if (gst_pad_is_linked (pad)
462 && gst_pad_get_direction (pad) == GST_PAD_SRC) {
463 debug_dump_element_pad_link (pad, element, details, out,
466 gst_object_unref (pad);
468 case GST_ITERATOR_RESYNC:
469 gst_iterator_resync (pad_iter);
471 case GST_ITERATOR_ERROR:
472 case GST_ITERATOR_DONE:
477 gst_iterator_free (pad_iter);
479 gst_object_unref (element);
481 case GST_ITERATOR_RESYNC:
482 gst_iterator_resync (element_iter);
484 case GST_ITERATOR_ERROR:
485 case GST_ITERATOR_DONE:
486 elements_done = TRUE;
490 gst_iterator_free (element_iter);
495 * _gst_debug_bin_to_dot_file:
496 * @bin: the top-level pipeline that should be analyzed
497 * @file_name: output base filename (e.g. "myplayer")
499 * To aid debugging applications one can use this method to write out the whole
500 * network of gstreamer elements that form the pipeline into an dot file.
501 * This file can be processed with graphviz to get an image.
502 * <informalexample><programlisting>
503 * dot -Tpng -oimage.png graph_lowlevel.dot
504 * </programlisting></informalexample>
507 _gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
508 const gchar * file_name)
510 gchar *full_file_name = NULL;
513 g_return_if_fail (GST_IS_BIN (bin));
515 if (G_LIKELY (priv_gst_dump_dot_dir == NULL))
519 file_name = g_get_application_name ();
521 file_name = "unnamed";
524 full_file_name = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.dot",
525 priv_gst_dump_dot_dir, file_name);
527 if ((out = fopen (full_file_name, "wb"))) {
528 gchar *state_name = NULL;
529 gchar *param_name = NULL;
531 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
532 state_name = debug_dump_get_element_state (GST_ELEMENT (bin));
534 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
535 param_name = debug_dump_get_element_params (GST_ELEMENT (bin));
540 "digraph pipeline {\n"
542 " fontname=\"Bitstream Vera Sans\";\n"
547 " label=\"<%s>\\n%s%s%s\";\n"
548 " node [style=filled, shape=box, fontsize=\"7\", fontname=\"Bitstream Vera Sans\", margin=\"0.0,0.0\"];\n"
549 " edge [labelfontsize=\"7\", fontsize=\"7\", labelfontname=\"Bitstream Vera Sans\", fontname=\"Bitstream Vera Sans\"];\n"
550 "\n", G_OBJECT_TYPE_NAME (bin), GST_OBJECT_NAME (bin),
551 (state_name ? state_name : ""), (param_name ? param_name : "")
558 debug_dump_element (bin, details, out, 1);
561 fprintf (out, "}\n");
563 GST_INFO ("wrote bin graph to : '%s'", full_file_name);
565 GST_WARNING ("Failed to open file '%s' for writing: %s", full_file_name,
568 g_free (full_file_name);
572 * _gst_debug_bin_to_dot_file_with_ts:
573 * @bin: the top-level pipeline that should be analyzed
574 * @file_name: output base filename (e.g. "myplayer")
576 * This works like _gst_debug_bin_to_dot_file(), but adds the current timestamp
577 * to the filename, so that it can be used to take multiple snapshots.
580 _gst_debug_bin_to_dot_file_with_ts (GstBin * bin, GstDebugGraphDetails details,
581 const gchar * file_name)
583 gchar *ts_file_name = NULL;
584 GstClockTime elapsed;
586 g_return_if_fail (GST_IS_BIN (bin));
589 file_name = g_get_application_name ();
591 file_name = "unnamed";
595 elapsed = GST_CLOCK_DIFF (_priv_gst_info_start_time,
596 gst_util_get_timestamp ());
598 g_strdup_printf ("%" GST_TIME_FORMAT "-%s", GST_TIME_ARGS (elapsed),
601 _gst_debug_bin_to_dot_file (bin, details, ts_file_name);
602 g_free (ts_file_name);
605 #endif /* GST_DISABLE_GST_DEBUG */