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 ];
24 * does not create spacial dependency
25 * node [ margin="0.02,0.01" ];
26 * space surrounding the label
29 #include "gst_private.h"
30 #include "gstdebugutils.h"
32 #ifndef GST_DISABLE_GST_DEBUG
41 #include "gstobject.h"
42 #include "gstghostpad.h"
46 /*** PIPELINE GRAPHS **********************************************************/
48 const gchar *priv_gst_dump_dot_dir; /* NULL *//* set from gst.c */
50 extern GstClockTime _priv_gst_info_start_time;
53 debug_dump_make_object_name (GstObject * element)
55 return g_strcanon (g_strdup_printf ("%s_%p", GST_OBJECT_NAME (element),
56 element), G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "_", '_');
60 debug_dump_get_element_state (GstElement * element)
62 gchar *state_name = NULL;
63 const gchar *state_icons = "~0-=>";
64 GstState state = 0, pending = 0;
66 gst_element_get_state (element, &state, &pending, 0);
67 if (pending == GST_STATE_VOID_PENDING) {
68 state_name = g_strdup_printf ("\\n[%c]", state_icons[state]);
70 state_name = g_strdup_printf ("\\n[%c]->[%c]", state_icons[state],
71 state_icons[pending]);
77 debug_dump_get_element_params (GstElement * element)
79 gchar *param_name = NULL;
80 GParamSpec **properties, *property;
81 GValue value = { 0, };
82 guint i, number_of_properties;
83 gchar *tmp, *value_str;
85 /* get paramspecs and show non-default properties */
87 g_object_class_list_properties (G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS
88 (element)), &number_of_properties);
90 for (i = 0; i < number_of_properties; i++) {
91 property = properties[i];
93 /* ski some properties */
94 if (!(property->flags & G_PARAM_READABLE))
96 if (!strcmp (property->name, "name"))
99 g_value_init (&value, property->value_type);
100 g_object_get_property (G_OBJECT (element), property->name, &value);
101 if (!(g_param_value_defaults (property, &value))) {
102 tmp = g_strdup_value_contents (&value);
103 value_str = g_strescape (tmp, NULL);
107 param_name = g_strdup_printf ("%s\\n%s=%s",
108 tmp, property->name, value_str);
111 param_name = g_strdup_printf ("\\n%s=%s", property->name, value_str);
115 g_value_unset (&value);
123 debug_dump_element_pad (GstPad * pad, GstElement * element,
124 GstDebugGraphDetails details, FILE * out, const gint indent)
126 GstElement *target_element;
127 GstPad *target_pad, *tmp_pad;
129 gchar *pad_name, *element_name;
130 gchar *target_pad_name, *target_element_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 = "";
156 "%s %s_%s [color=black, fillcolor=\"%s\", label=\"%s\"];\n",
157 spc, target_element_name, target_pad_name, color_name,
158 GST_OBJECT_NAME (target_pad));
159 g_free (target_pad_name);
160 if (target_element) {
161 g_free (target_element_name);
162 gst_object_unref (target_element);
164 gst_object_unref (target_pad);
166 gst_object_unref (tmp_pad);
170 (dir == GST_PAD_SRC) ? "#ffaaaa" : ((dir ==
171 GST_PAD_SINK) ? "#aaaaff" : "#cccccc");
175 "%s %s_%s [color=black, fillcolor=\"%s\", label=\"%s\"];\n",
176 spc, element_name, pad_name, color_name, GST_OBJECT_NAME (pad));
179 g_free (element_name);
184 debug_dump_element_pad_link (GstPad * pad, GstElement * element,
185 GstDebugGraphDetails details, FILE * out, const gint indent)
187 GstElement *peer_element, *target_element;
188 GstPad *peer_pad, *target_pad, *tmp_pad;
190 GstStructure *structure;
191 gboolean free_caps, free_media;
193 gchar *pad_name, *element_name;
194 gchar *peer_pad_name, *peer_element_name;
195 gchar *target_pad_name, *target_element_name;
198 spc = g_malloc (1 + indent * 2);
199 memset (spc, 32, indent * 2);
200 spc[indent * 2] = '\0';
202 if ((peer_pad = gst_pad_get_peer (pad))) {
204 if ((details & GST_DEBUG_GRAPH_SHOW_MEDIA_TYPE) ||
205 (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS)
207 if ((caps = gst_pad_get_negotiated_caps (pad))) {
211 if (!(caps = (GstCaps *)
212 gst_pad_get_pad_template_caps (pad))) {
213 /* this should not happen */
218 if (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS) {
219 gchar *tmp = g_strdelimit (gst_caps_to_string (caps), ",",
222 media = g_strescape (tmp, NULL);
226 if (GST_CAPS_IS_SIMPLE (caps)) {
227 structure = gst_caps_get_structure (caps, 0);
228 media = (gchar *) gst_structure_get_name (structure);
233 gst_caps_unref (caps);
238 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
240 element_name = debug_dump_make_object_name (GST_OBJECT (element));
244 peer_pad_name = debug_dump_make_object_name (GST_OBJECT (peer_pad));
245 if ((peer_element = gst_pad_get_parent_element (peer_pad))) {
247 debug_dump_make_object_name (GST_OBJECT (peer_element));
249 peer_element_name = "";
252 if (GST_IS_GHOST_PAD (pad)) {
253 if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad)))) {
254 if ((target_pad = gst_pad_get_peer (tmp_pad))) {
256 debug_dump_make_object_name (GST_OBJECT (target_pad));
257 if ((target_element = gst_pad_get_parent_element (target_pad))) {
258 target_element_name =
259 debug_dump_make_object_name (GST_OBJECT (target_element));
261 target_element_name = "";
263 /* src ghostpad relationship */
264 fprintf (out, "%s%s_%s -> %s_%s [style=dashed]\n", spc,
265 target_element_name, target_pad_name, element_name, pad_name);
267 g_free (target_pad_name);
268 if (target_element) {
269 g_free (target_element_name);
270 gst_object_unref (target_element);
272 gst_object_unref (target_pad);
274 gst_object_unref (tmp_pad);
277 if (GST_IS_GHOST_PAD (peer_pad)) {
278 if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (peer_pad)))) {
279 if ((target_pad = gst_pad_get_peer (tmp_pad))) {
281 debug_dump_make_object_name (GST_OBJECT (target_pad));
282 if ((target_element = gst_pad_get_parent_element (target_pad))) {
283 target_element_name =
284 debug_dump_make_object_name (GST_OBJECT (target_element));
286 target_element_name = "";
288 /* sink ghostpad relationship */
289 fprintf (out, "%s%s_%s -> %s_%s [style=dashed]\n", spc,
290 peer_element_name, peer_pad_name,
291 target_element_name, target_pad_name);
292 /* FIXME: we are missing links from the proxy pad
293 * theoretically we need to:
294 * pad=gst_object_ref(target_pad);
295 * goto line 280: if ((peer_pad = gst_pad_get_peer (pad)))
296 * as this would e ugly we need to refactor ...
298 debug_dump_element_pad_link (target_pad, target_element, details, out,
300 g_free (target_pad_name);
301 if (target_element) {
302 g_free (target_element_name);
303 gst_object_unref (target_element);
305 gst_object_unref (target_pad);
307 gst_object_unref (tmp_pad);
313 fprintf (out, "%s%s_%s -> %s_%s [label=\"%s\"]\n", spc,
314 element_name, pad_name, peer_element_name, peer_pad_name, media);
319 fprintf (out, "%s%s_%s -> %s_%s\n", spc,
320 element_name, pad_name, peer_element_name, peer_pad_name);
325 g_free (element_name);
327 g_free (peer_pad_name);
329 g_free (peer_element_name);
330 gst_object_unref (peer_element);
332 gst_object_unref (peer_pad);
337 * debug_dump_element:
338 * @bin: the bin that should be analyzed
339 * @out: file to write to
340 * @indent: level of graph indentation
342 * Helper for _gst_debug_bin_to_dot_file() to recursively dump a pipeline.
345 debug_dump_element (GstBin * bin, GstDebugGraphDetails details, FILE * out,
348 GstIterator *element_iter, *pad_iter;
349 gboolean elements_done, pads_done;
353 guint src_pads, sink_pads;
355 gchar *state_name = NULL;
356 gchar *param_name = NULL;
359 spc = g_malloc (1 + indent * 2);
360 memset (spc, 32, indent * 2);
361 spc[indent * 2] = '\0';
363 element_iter = gst_bin_iterate_elements (bin);
364 elements_done = FALSE;
365 while (!elements_done) {
366 switch (gst_iterator_next (element_iter, (gpointer) & element)) {
367 case GST_ITERATOR_OK:
368 element_name = debug_dump_make_object_name (GST_OBJECT (element));
370 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
371 state_name = debug_dump_get_element_state (GST_ELEMENT (element));
373 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
374 param_name = debug_dump_get_element_params (GST_ELEMENT (element));
377 fprintf (out, "%ssubgraph cluster_%s {\n", spc, element_name);
378 fprintf (out, "%s fontname=\"Bitstream Vera Sans\";\n", spc);
379 fprintf (out, "%s fontsize=\"8\";\n", spc);
380 fprintf (out, "%s style=filled;\n", spc);
381 fprintf (out, "%s color=black;\n\n", spc);
382 fprintf (out, "%s label=\"<%s>\\n%s%s%s\";\n", spc,
383 G_OBJECT_TYPE_NAME (element), GST_OBJECT_NAME (element),
384 (state_name ? state_name : ""), (param_name ? param_name : "")
394 g_free (element_name);
396 src_pads = sink_pads = 0;
397 if ((pad_iter = gst_element_iterate_pads (element))) {
400 switch (gst_iterator_next (pad_iter, (gpointer) & pad)) {
401 case GST_ITERATOR_OK:
402 debug_dump_element_pad (pad, element, details, out, indent);
403 dir = gst_pad_get_direction (pad);
404 if (dir == GST_PAD_SRC)
406 else if (dir == GST_PAD_SINK)
408 gst_object_unref (pad);
410 case GST_ITERATOR_RESYNC:
411 gst_iterator_resync (pad_iter);
413 case GST_ITERATOR_ERROR:
414 case GST_ITERATOR_DONE:
419 gst_iterator_free (pad_iter);
421 if (GST_IS_BIN (element)) {
422 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
424 debug_dump_element (GST_BIN (element), details, out, indent + 1);
426 if (src_pads && !sink_pads)
427 fprintf (out, "%s fillcolor=\"#ffaaaa\";\n", spc);
428 else if (!src_pads && sink_pads)
429 fprintf (out, "%s fillcolor=\"#aaaaff\";\n", spc);
430 else if (src_pads && sink_pads)
431 fprintf (out, "%s fillcolor=\"#aaffaa\";\n", spc);
433 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
435 fprintf (out, "%s}\n\n", spc);
436 if ((pad_iter = gst_element_iterate_pads (element))) {
439 switch (gst_iterator_next (pad_iter, (gpointer) & pad)) {
440 case GST_ITERATOR_OK:
441 if (gst_pad_is_linked (pad)
442 && gst_pad_get_direction (pad) == GST_PAD_SRC) {
443 debug_dump_element_pad_link (pad, element, details, out,
446 gst_object_unref (pad);
448 case GST_ITERATOR_RESYNC:
449 gst_iterator_resync (pad_iter);
451 case GST_ITERATOR_ERROR:
452 case GST_ITERATOR_DONE:
457 gst_iterator_free (pad_iter);
459 gst_object_unref (element);
461 case GST_ITERATOR_RESYNC:
462 gst_iterator_resync (element_iter);
464 case GST_ITERATOR_ERROR:
465 case GST_ITERATOR_DONE:
466 elements_done = TRUE;
470 gst_iterator_free (element_iter);
475 * _gst_debug_bin_to_dot_file:
476 * @bin: the top-level pipeline that should be analyzed
477 * @file_name: output base filename (e.g. "myplayer")
479 * To aid debugging applications one can use this method to write out the whole
480 * network of gstreamer elements that form the pipeline into an dot file.
481 * This file can be processed with graphviz to get an image.
482 * <informalexample><programlisting>
483 * dot -Tpng -oimage.png graph_lowlevel.dot
484 * </programlisting></informalexample>
487 _gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
488 const gchar * file_name)
490 gchar *full_file_name = NULL;
493 g_return_if_fail (GST_IS_BIN (bin));
495 if (G_LIKELY (priv_gst_dump_dot_dir == NULL))
499 file_name = g_get_application_name ();
501 file_name = "unnamed";
504 full_file_name = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.dot",
505 priv_gst_dump_dot_dir, file_name);
507 if ((out = fopen (full_file_name, "wb"))) {
508 gchar *state_name = NULL;
509 gchar *param_name = NULL;
511 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
512 state_name = debug_dump_get_element_state (GST_ELEMENT (bin));
514 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
515 param_name = debug_dump_get_element_params (GST_ELEMENT (bin));
520 "digraph pipeline {\n"
522 " fontname=\"Bitstream Vera Sans\";\n"
527 " label=\"<%s>\\n%s%s%s\";\n"
528 " node [style=filled, shape=box, fontsize=\"7\", fontname=\"Bitstream Vera Sans\"];\n"
529 " edge [labelfontsize=\"7\", fontsize=\"7\", labelfontname=\"Bitstream Vera Sans\", fontname=\"Bitstream Vera Sans\"];\n"
530 "\n", G_OBJECT_TYPE_NAME (bin), GST_OBJECT_NAME (bin),
531 (state_name ? state_name : ""), (param_name ? param_name : "")
538 debug_dump_element (bin, details, out, 1);
541 fprintf (out, "}\n");
543 GST_INFO ("wrote bin graph to : '%s'", full_file_name);
545 GST_WARNING ("Failed to open file '%s' for writing: %s", full_file_name,
548 g_free (full_file_name);
552 * _gst_debug_bin_to_dot_file_with_ts:
553 * @bin: the top-level pipeline that should be analyzed
554 * @file_name: output base filename (e.g. "myplayer")
556 * This works like _gst_debug_bin_to_dot_file(), but adds the current timestamp
557 * to the filename, so that it can be used to take multiple snapshots.
560 _gst_debug_bin_to_dot_file_with_ts (GstBin * bin, GstDebugGraphDetails details,
561 const gchar * file_name)
563 gchar *ts_file_name = NULL;
564 GstClockTime elapsed;
566 g_return_if_fail (GST_IS_BIN (bin));
569 file_name = g_get_application_name ();
571 file_name = "unnamed";
575 elapsed = GST_CLOCK_DIFF (_priv_gst_info_start_time,
576 gst_util_get_timestamp ());
578 g_strdup_printf ("%" GST_TIME_FORMAT "-%s", GST_TIME_ARGS (elapsed),
581 _gst_debug_bin_to_dot_file (bin, details, ts_file_name);
582 g_free (ts_file_name);
585 #endif /* GST_DISABLE_GST_DEBUG */