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
40 #include "gstobject.h"
41 #include "gstghostpad.h"
45 /*** PIPELINE GRAPHS **********************************************************/
47 const gchar *priv_gst_dump_dot_dir; /* NULL *//* set from gst.c */
49 extern GstClockTime _priv_gst_info_start_time;
52 debug_dump_make_object_name (GstObject * element)
54 return g_strcanon (g_strdup_printf ("%s_%p", GST_OBJECT_NAME (element),
55 element), G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "_", '_');
59 debug_dump_get_element_state (GstElement * element)
61 gchar *state_name = NULL;
62 const gchar *state_icons = "~0-=>";
63 GstState state = 0, pending = 0;
65 gst_element_get_state (element, &state, &pending, 0);
66 if (pending == GST_STATE_VOID_PENDING) {
67 state_name = g_strdup_printf ("\\n[%c]", state_icons[state]);
69 state_name = g_strdup_printf ("\\n[%c]->[%c]", state_icons[state],
70 state_icons[pending]);
76 debug_dump_get_element_params (GstElement * element)
78 gchar *param_name = NULL;
79 GParamSpec **properties, *property;
80 GValue value = { 0, };
81 guint i, number_of_properties;
82 gchar *tmp, *value_str;
84 /* get paramspecs and show non-default properties */
86 g_object_class_list_properties (G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS
87 (element)), &number_of_properties);
89 for (i = 0; i < number_of_properties; i++) {
90 property = properties[i];
92 /* ski some properties */
93 if (!(property->flags & G_PARAM_READABLE))
95 if (!strcmp (property->name, "name"))
98 g_value_init (&value, property->value_type);
99 g_object_get_property (G_OBJECT (element), property->name, &value);
100 if (!(g_param_value_defaults (property, &value))) {
101 tmp = g_strdup_value_contents (&value);
102 value_str = g_strescape (tmp, NULL);
106 param_name = g_strdup_printf ("%s\\n%s=%s",
107 tmp, property->name, value_str);
110 param_name = g_strdup_printf ("\\n%s=%s", property->name, value_str);
114 g_value_unset (&value);
122 * debug_dump_element:
123 * @bin: the bin that should be analyzed
124 * @out: file to write to
125 * @indent: level of graph indentation
127 * Helper for _gst_debug_bin_to_dot_file() to recursively dump a pipeline.
130 debug_dump_element (GstBin * bin, GstDebugGraphDetails details, FILE * out,
133 GstIterator *element_iter, *pad_iter;
134 gboolean elements_done, pads_done;
135 GstElement *element, *peer_element, *target_element;
136 GstPad *pad, *peer_pad, *target_pad;
139 GstStructure *structure;
140 gboolean free_caps, free_media;
141 guint src_pads, sink_pads;
143 gchar *pad_name, *element_name;
144 gchar *peer_pad_name, *peer_element_name;
145 gchar *target_pad_name, *target_element_name;
147 gchar *state_name = NULL;
148 gchar *param_name = NULL;
151 spc = g_malloc (1 + indent * 2);
152 memset (spc, 32, indent * 2);
153 spc[indent * 2] = '\0';
155 element_iter = gst_bin_iterate_elements (bin);
156 elements_done = FALSE;
157 while (!elements_done) {
158 switch (gst_iterator_next (element_iter, (gpointer) & element)) {
159 case GST_ITERATOR_OK:
160 element_name = debug_dump_make_object_name (GST_OBJECT (element));
162 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
163 state_name = debug_dump_get_element_state (GST_ELEMENT (element));
165 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
166 param_name = debug_dump_get_element_params (GST_ELEMENT (element));
169 fprintf (out, "%ssubgraph cluster_%s {\n", spc, element_name);
170 fprintf (out, "%s fontname=\"Bitstream Vera Sans\";\n", spc);
171 fprintf (out, "%s fontsize=\"8\";\n", spc);
172 fprintf (out, "%s style=filled;\n", spc);
173 fprintf (out, "%s color=black;\n\n", spc);
174 fprintf (out, "%s label=\"<%s>\\n%s%s%s\";\n", spc,
175 G_OBJECT_TYPE_NAME (element), GST_OBJECT_NAME (element),
176 (state_name ? state_name : ""), (param_name ? param_name : "")
186 g_free (element_name);
188 src_pads = sink_pads = 0;
189 if ((pad_iter = gst_element_iterate_pads (element))) {
192 switch (gst_iterator_next (pad_iter, (gpointer) & pad)) {
193 case GST_ITERATOR_OK:
194 dir = gst_pad_get_direction (pad);
195 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
197 debug_dump_make_object_name (GST_OBJECT (element));
198 if (GST_IS_GHOST_PAD (pad)) {
200 (dir == GST_PAD_SRC) ? "#ffdddd" : ((dir ==
201 GST_PAD_SINK) ? "#ddddff" : "#ffffff");
204 (dir == GST_PAD_SRC) ? "#ffaaaa" : ((dir ==
205 GST_PAD_SINK) ? "#aaaaff" : "#cccccc");
209 "%s %s_%s [color=black, fillcolor=\"%s\", label=\"%s\"];\n",
210 spc, element_name, pad_name, color_name,
211 GST_OBJECT_NAME (pad));
213 if (dir == GST_PAD_SRC)
215 else if (dir == GST_PAD_SINK)
218 g_free (element_name);
219 gst_object_unref (pad);
221 case GST_ITERATOR_RESYNC:
222 gst_iterator_resync (pad_iter);
224 case GST_ITERATOR_ERROR:
225 case GST_ITERATOR_DONE:
230 gst_iterator_free (pad_iter);
232 if (GST_IS_BIN (element)) {
233 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
235 debug_dump_element (GST_BIN (element), details, out, indent + 1);
237 if (src_pads && !sink_pads)
238 fprintf (out, "%s fillcolor=\"#ffaaaa\";\n", spc);
239 else if (!src_pads && sink_pads)
240 fprintf (out, "%s fillcolor=\"#aaaaff\";\n", spc);
241 else if (src_pads && sink_pads)
242 fprintf (out, "%s fillcolor=\"#aaffaa\";\n", spc);
244 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
246 fprintf (out, "%s}\n\n", spc);
247 if ((pad_iter = gst_element_iterate_pads (element))) {
250 switch (gst_iterator_next (pad_iter, (gpointer) & pad)) {
251 case GST_ITERATOR_OK:
252 if (gst_pad_is_linked (pad)
253 && gst_pad_get_direction (pad) == GST_PAD_SRC) {
254 if ((peer_pad = gst_pad_get_peer (pad))) {
256 if ((details & GST_DEBUG_GRAPH_SHOW_MEDIA_TYPE) ||
257 (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS)
259 if ((caps = gst_pad_get_negotiated_caps (pad))) {
263 if (!(caps = (GstCaps *)
264 gst_pad_get_pad_template_caps (pad))) {
265 /* this should not happen */
270 if (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS) {
272 g_strdelimit (gst_caps_to_string (caps), ",",
275 media = g_strescape (tmp, NULL);
279 if (GST_CAPS_IS_SIMPLE (caps)) {
280 structure = gst_caps_get_structure (caps, 0);
282 (gchar *) gst_structure_get_name (structure);
287 gst_caps_unref (caps);
292 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
294 debug_dump_make_object_name (GST_OBJECT (element));
296 debug_dump_make_object_name (GST_OBJECT (peer_pad));
297 if ((peer_element = gst_pad_get_parent_element (peer_pad))) {
299 debug_dump_make_object_name (GST_OBJECT
302 peer_element_name = "";
306 fprintf (out, "%s%s_%s -> %s_%s [label=\"%s\"]\n", spc,
307 element_name, pad_name, peer_element_name,
308 peer_pad_name, media);
313 fprintf (out, "%s%s_%s -> %s_%s\n", spc,
314 element_name, pad_name, peer_element_name,
318 if (GST_IS_GHOST_PAD (pad)) {
320 gst_ghost_pad_get_target (GST_GHOST_PAD (pad)))) {
322 debug_dump_make_object_name (GST_OBJECT
324 if ((target_element =
325 gst_pad_get_parent_element (target_pad))) {
326 target_element_name =
327 debug_dump_make_object_name (GST_OBJECT
330 target_element_name = "";
332 /* src ghostpad relationship */
333 fprintf (out, "%s%s_%s -> %s_%s [style=dashed]\n", spc,
334 target_element_name, target_pad_name, element_name,
337 g_free (target_pad_name);
338 if (target_element) {
339 g_free (target_element_name);
340 gst_object_unref (target_element);
342 gst_object_unref (target_pad);
345 if (GST_IS_GHOST_PAD (peer_pad)) {
347 gst_ghost_pad_get_target (GST_GHOST_PAD
350 debug_dump_make_object_name (GST_OBJECT
352 if ((target_element =
353 gst_pad_get_parent_element (target_pad))) {
354 target_element_name =
355 debug_dump_make_object_name (GST_OBJECT
358 target_element_name = "";
360 /* sink ghostpad relationship */
361 fprintf (out, "%s%s_%s -> %s_%s [style=dashed]\n", spc,
362 peer_element_name, peer_pad_name,
363 target_element_name, target_pad_name);
365 g_free (target_pad_name);
366 if (target_element) {
367 g_free (target_element_name);
368 gst_object_unref (target_element);
370 gst_object_unref (target_pad);
375 g_free (element_name);
376 g_free (peer_pad_name);
378 g_free (peer_element_name);
379 gst_object_unref (peer_element);
381 gst_object_unref (peer_pad);
384 gst_object_unref (pad);
386 case GST_ITERATOR_RESYNC:
387 gst_iterator_resync (pad_iter);
389 case GST_ITERATOR_ERROR:
390 case GST_ITERATOR_DONE:
395 gst_iterator_free (pad_iter);
397 gst_object_unref (element);
399 case GST_ITERATOR_RESYNC:
400 gst_iterator_resync (element_iter);
402 case GST_ITERATOR_ERROR:
403 case GST_ITERATOR_DONE:
404 elements_done = TRUE;
408 gst_iterator_free (element_iter);
413 * _gst_debug_bin_to_dot_file:
414 * @bin: the top-level pipeline that should be analyzed
415 * @file_name: output base filename (e.g. "myplayer")
417 * To aid debugging applications one can use this method to write out the whole
418 * network of gstreamer elements that form the pipeline into an dot file.
419 * This file can be processed with graphviz to get an image.
420 * <informalexample><programlisting>
421 * dot -Tpng -oimage.png graph_lowlevel.dot
422 * </programlisting></informalexample>
425 _gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
426 const gchar * file_name)
428 gchar *full_file_name = NULL;
431 g_return_if_fail (GST_IS_BIN (bin));
433 if (G_LIKELY (priv_gst_dump_dot_dir == NULL))
437 file_name = g_get_application_name ();
439 file_name = "unnamed";
442 full_file_name = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.dot",
443 priv_gst_dump_dot_dir, file_name);
445 if ((out = fopen (full_file_name, "wb"))) {
446 gchar *state_name = NULL;
447 gchar *param_name = NULL;
449 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
450 state_name = debug_dump_get_element_state (GST_ELEMENT (bin));
452 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
453 param_name = debug_dump_get_element_params (GST_ELEMENT (bin));
458 "digraph pipeline {\n"
460 " fontname=\"Bitstream Vera Sans\";\n"
465 " label=\"<%s>\\n%s%s%s\";\n"
466 " node [style=filled, shape=box, fontsize=\"7\", fontname=\"Bitstream Vera Sans\"];\n"
467 " edge [labelfontsize=\"7\", fontsize=\"7\", labelfontname=\"Bitstream Vera Sans\", fontname=\"Bitstream Vera Sans\"];\n"
468 "\n", G_OBJECT_TYPE_NAME (bin), GST_OBJECT_NAME (bin),
469 (state_name ? state_name : ""), (param_name ? param_name : "")
476 debug_dump_element (bin, details, out, 1);
479 fprintf (out, "}\n");
482 GST_INFO ("wrote bin graph to : '%s'", full_file_name);
483 g_free (full_file_name);
487 * _gst_debug_bin_to_dot_file_with_ts:
488 * @bin: the top-level pipeline that should be analyzed
489 * @file_name: output base filename (e.g. "myplayer")
491 * This works like _gst_debug_bin_to_dot_file(), but adds the current timestamp
492 * to the filename, so that it can be used to take multiple snapshots.
495 _gst_debug_bin_to_dot_file_with_ts (GstBin * bin, GstDebugGraphDetails details,
496 const gchar * file_name)
498 gchar *ts_file_name = NULL;
499 GstClockTime now, elapsed;
501 g_return_if_fail (GST_IS_BIN (bin));
504 file_name = g_get_application_name ();
506 file_name = "unnamed";
510 GST_GET_CURRENT_TIME (now);
511 elapsed = GST_CLOCK_DIFF (now, _priv_gst_info_start_time);
513 g_strdup_printf ("%" GST_TIME_FORMAT "-%s", GST_TIME_ARGS (elapsed),
516 _gst_debug_bin_to_dot_file (bin, details, ts_file_name);
517 g_free (ts_file_name);
520 #endif /* GST_DISABLE_GST_DEBUG */