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:
124 * @bin: the bin that should be analyzed
125 * @out: file to write to
126 * @indent: level of graph indentation
128 * Helper for _gst_debug_bin_to_dot_file() to recursively dump a pipeline.
131 debug_dump_element (GstBin * bin, GstDebugGraphDetails details, FILE * out,
134 GstIterator *element_iter, *pad_iter;
135 gboolean elements_done, pads_done;
136 GstElement *element, *peer_element, *target_element;
137 GstPad *pad, *peer_pad, *target_pad;
140 GstStructure *structure;
141 gboolean free_caps, free_media;
142 guint src_pads, sink_pads;
144 gchar *pad_name, *element_name;
145 gchar *peer_pad_name, *peer_element_name;
146 gchar *target_pad_name, *target_element_name;
148 gchar *state_name = NULL;
149 gchar *param_name = NULL;
152 spc = g_malloc (1 + indent * 2);
153 memset (spc, 32, indent * 2);
154 spc[indent * 2] = '\0';
156 element_iter = gst_bin_iterate_elements (bin);
157 elements_done = FALSE;
158 while (!elements_done) {
159 switch (gst_iterator_next (element_iter, (gpointer) & element)) {
160 case GST_ITERATOR_OK:
161 element_name = debug_dump_make_object_name (GST_OBJECT (element));
163 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
164 state_name = debug_dump_get_element_state (GST_ELEMENT (element));
166 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
167 param_name = debug_dump_get_element_params (GST_ELEMENT (element));
170 fprintf (out, "%ssubgraph cluster_%s {\n", spc, element_name);
171 fprintf (out, "%s fontname=\"Bitstream Vera Sans\";\n", spc);
172 fprintf (out, "%s fontsize=\"8\";\n", spc);
173 fprintf (out, "%s style=filled;\n", spc);
174 fprintf (out, "%s color=black;\n\n", spc);
175 fprintf (out, "%s label=\"<%s>\\n%s%s%s\";\n", spc,
176 G_OBJECT_TYPE_NAME (element), GST_OBJECT_NAME (element),
177 (state_name ? state_name : ""), (param_name ? param_name : "")
187 g_free (element_name);
189 src_pads = sink_pads = 0;
190 if ((pad_iter = gst_element_iterate_pads (element))) {
193 switch (gst_iterator_next (pad_iter, (gpointer) & pad)) {
194 case GST_ITERATOR_OK:
195 dir = gst_pad_get_direction (pad);
196 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
198 debug_dump_make_object_name (GST_OBJECT (element));
199 if (GST_IS_GHOST_PAD (pad)) {
201 (dir == GST_PAD_SRC) ? "#ffdddd" : ((dir ==
202 GST_PAD_SINK) ? "#ddddff" : "#ffffff");
205 (dir == GST_PAD_SRC) ? "#ffaaaa" : ((dir ==
206 GST_PAD_SINK) ? "#aaaaff" : "#cccccc");
210 "%s %s_%s [color=black, fillcolor=\"%s\", label=\"%s\"];\n",
211 spc, element_name, pad_name, color_name,
212 GST_OBJECT_NAME (pad));
214 if (dir == GST_PAD_SRC)
216 else if (dir == GST_PAD_SINK)
219 g_free (element_name);
220 gst_object_unref (pad);
222 case GST_ITERATOR_RESYNC:
223 gst_iterator_resync (pad_iter);
225 case GST_ITERATOR_ERROR:
226 case GST_ITERATOR_DONE:
231 gst_iterator_free (pad_iter);
233 if (GST_IS_BIN (element)) {
234 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
236 debug_dump_element (GST_BIN (element), details, out, indent + 1);
238 if (src_pads && !sink_pads)
239 fprintf (out, "%s fillcolor=\"#ffaaaa\";\n", spc);
240 else if (!src_pads && sink_pads)
241 fprintf (out, "%s fillcolor=\"#aaaaff\";\n", spc);
242 else if (src_pads && sink_pads)
243 fprintf (out, "%s fillcolor=\"#aaffaa\";\n", spc);
245 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
247 fprintf (out, "%s}\n\n", spc);
248 if ((pad_iter = gst_element_iterate_pads (element))) {
251 switch (gst_iterator_next (pad_iter, (gpointer) & pad)) {
252 case GST_ITERATOR_OK:
253 if (gst_pad_is_linked (pad)
254 && gst_pad_get_direction (pad) == GST_PAD_SRC) {
255 if ((peer_pad = gst_pad_get_peer (pad))) {
257 if ((details & GST_DEBUG_GRAPH_SHOW_MEDIA_TYPE) ||
258 (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS)
260 if ((caps = gst_pad_get_negotiated_caps (pad))) {
264 if (!(caps = (GstCaps *)
265 gst_pad_get_pad_template_caps (pad))) {
266 /* this should not happen */
271 if (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS) {
273 g_strdelimit (gst_caps_to_string (caps), ",",
276 media = g_strescape (tmp, NULL);
280 if (GST_CAPS_IS_SIMPLE (caps)) {
281 structure = gst_caps_get_structure (caps, 0);
283 (gchar *) gst_structure_get_name (structure);
288 gst_caps_unref (caps);
293 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
295 debug_dump_make_object_name (GST_OBJECT (element));
297 debug_dump_make_object_name (GST_OBJECT (peer_pad));
298 if ((peer_element = gst_pad_get_parent_element (peer_pad))) {
300 debug_dump_make_object_name (GST_OBJECT
303 peer_element_name = "";
307 fprintf (out, "%s%s_%s -> %s_%s [label=\"%s\"]\n", spc,
308 element_name, pad_name, peer_element_name,
309 peer_pad_name, media);
314 fprintf (out, "%s%s_%s -> %s_%s\n", spc,
315 element_name, pad_name, peer_element_name,
319 if (GST_IS_GHOST_PAD (pad)) {
321 gst_ghost_pad_get_target (GST_GHOST_PAD (pad)))) {
323 debug_dump_make_object_name (GST_OBJECT
325 if ((target_element =
326 gst_pad_get_parent_element (target_pad))) {
327 target_element_name =
328 debug_dump_make_object_name (GST_OBJECT
331 target_element_name = "";
333 /* src ghostpad relationship */
334 fprintf (out, "%s%s_%s -> %s_%s [style=dashed]\n", spc,
335 target_element_name, target_pad_name, element_name,
338 g_free (target_pad_name);
339 if (target_element) {
340 g_free (target_element_name);
341 gst_object_unref (target_element);
343 gst_object_unref (target_pad);
346 if (GST_IS_GHOST_PAD (peer_pad)) {
348 gst_ghost_pad_get_target (GST_GHOST_PAD
351 debug_dump_make_object_name (GST_OBJECT
353 if ((target_element =
354 gst_pad_get_parent_element (target_pad))) {
355 target_element_name =
356 debug_dump_make_object_name (GST_OBJECT
359 target_element_name = "";
361 /* sink ghostpad relationship */
362 fprintf (out, "%s%s_%s -> %s_%s [style=dashed]\n", spc,
363 peer_element_name, peer_pad_name,
364 target_element_name, target_pad_name);
366 g_free (target_pad_name);
367 if (target_element) {
368 g_free (target_element_name);
369 gst_object_unref (target_element);
371 gst_object_unref (target_pad);
376 g_free (element_name);
377 g_free (peer_pad_name);
379 g_free (peer_element_name);
380 gst_object_unref (peer_element);
382 gst_object_unref (peer_pad);
385 gst_object_unref (pad);
387 case GST_ITERATOR_RESYNC:
388 gst_iterator_resync (pad_iter);
390 case GST_ITERATOR_ERROR:
391 case GST_ITERATOR_DONE:
396 gst_iterator_free (pad_iter);
398 gst_object_unref (element);
400 case GST_ITERATOR_RESYNC:
401 gst_iterator_resync (element_iter);
403 case GST_ITERATOR_ERROR:
404 case GST_ITERATOR_DONE:
405 elements_done = TRUE;
409 gst_iterator_free (element_iter);
414 * _gst_debug_bin_to_dot_file:
415 * @bin: the top-level pipeline that should be analyzed
416 * @file_name: output base filename (e.g. "myplayer")
418 * To aid debugging applications one can use this method to write out the whole
419 * network of gstreamer elements that form the pipeline into an dot file.
420 * This file can be processed with graphviz to get an image.
421 * <informalexample><programlisting>
422 * dot -Tpng -oimage.png graph_lowlevel.dot
423 * </programlisting></informalexample>
426 _gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
427 const gchar * file_name)
429 gchar *full_file_name = NULL;
432 g_return_if_fail (GST_IS_BIN (bin));
434 if (G_LIKELY (priv_gst_dump_dot_dir == NULL))
438 file_name = g_get_application_name ();
440 file_name = "unnamed";
443 full_file_name = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.dot",
444 priv_gst_dump_dot_dir, file_name);
446 if ((out = fopen (full_file_name, "wb"))) {
447 gchar *state_name = NULL;
448 gchar *param_name = NULL;
450 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
451 state_name = debug_dump_get_element_state (GST_ELEMENT (bin));
453 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
454 param_name = debug_dump_get_element_params (GST_ELEMENT (bin));
459 "digraph pipeline {\n"
461 " fontname=\"Bitstream Vera Sans\";\n"
466 " label=\"<%s>\\n%s%s%s\";\n"
467 " node [style=filled, shape=box, fontsize=\"7\", fontname=\"Bitstream Vera Sans\"];\n"
468 " edge [labelfontsize=\"7\", fontsize=\"7\", labelfontname=\"Bitstream Vera Sans\", fontname=\"Bitstream Vera Sans\"];\n"
469 "\n", G_OBJECT_TYPE_NAME (bin), GST_OBJECT_NAME (bin),
470 (state_name ? state_name : ""), (param_name ? param_name : "")
477 debug_dump_element (bin, details, out, 1);
480 fprintf (out, "}\n");
482 GST_INFO ("wrote bin graph to : '%s'", full_file_name);
484 GST_WARNING ("Failed to open file '%s' for writing: %s", full_file_name,
487 g_free (full_file_name);
491 * _gst_debug_bin_to_dot_file_with_ts:
492 * @bin: the top-level pipeline that should be analyzed
493 * @file_name: output base filename (e.g. "myplayer")
495 * This works like _gst_debug_bin_to_dot_file(), but adds the current timestamp
496 * to the filename, so that it can be used to take multiple snapshots.
499 _gst_debug_bin_to_dot_file_with_ts (GstBin * bin, GstDebugGraphDetails details,
500 const gchar * file_name)
502 gchar *ts_file_name = NULL;
503 GstClockTime elapsed;
505 g_return_if_fail (GST_IS_BIN (bin));
508 file_name = g_get_application_name ();
510 file_name = "unnamed";
514 elapsed = GST_CLOCK_DIFF (_priv_gst_info_start_time,
515 gst_util_get_timestamp ());
517 g_strdup_printf ("%" GST_TIME_FORMAT "-%s", GST_TIME_ARGS (elapsed),
520 _gst_debug_bin_to_dot_file (bin, details, ts_file_name);
521 g_free (ts_file_name);
524 #endif /* GST_DISABLE_GST_DEBUG */