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 #ifndef GST_DISABLE_GST_DEBUG
24 #include "gst_private.h"
31 #include "gstdebugutils.h"
33 #include "gstobject.h"
34 #include "gstghostpad.h"
38 /*** PIPELINE GRAPHS **********************************************************/
40 gboolean _gst_debug_dump_dot_files_on = FALSE;
41 extern GstClockTime _gst_info_start_time;
44 debug_dump_make_object_name (GstObject * element)
46 return g_strcanon (g_strdup_printf ("%s_%p", GST_OBJECT_NAME (element),
47 element), G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "_", '_');
51 debug_dump_get_element_state (GstElement * element)
53 gchar *state_name = NULL;
54 const gchar *state_icons = "~0-=>";
55 GstState state = 0, pending = 0;
57 gst_element_get_state (element, &state, &pending, 0);
58 if (pending == GST_STATE_VOID_PENDING) {
59 state_name = g_strdup_printf ("\\n[%c]", state_icons[state]);
61 state_name = g_strdup_printf ("\\n[%c]->[%c]", state_icons[state],
62 state_icons[pending]);
68 debug_dump_get_element_params (GstElement * element)
70 gchar *param_name = NULL;
71 GParamSpec **properties, *property;
72 GValue value = { 0, };
73 guint i, number_of_properties;
74 gchar *tmp, *value_str;
76 /* get paramspecs and show non-default properties */
78 g_object_class_list_properties (G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS
79 (element)), &number_of_properties);
81 for (i = 0; i < number_of_properties; i++) {
82 property = properties[i];
84 /* ski some properties */
85 if (!(property->flags & G_PARAM_READABLE))
87 if (!strcmp (property->name, "name"))
90 g_value_init (&value, property->value_type);
91 g_object_get_property (G_OBJECT (element), property->name, &value);
92 if (!(g_param_value_defaults (property, &value))) {
93 tmp = g_strdup_value_contents (&value);
94 value_str = g_strescape (tmp, NULL);
98 param_name = g_strdup_printf ("%s\\n%s=%s",
99 tmp, property->name, value_str);
102 param_name = g_strdup_printf ("\\n%s=%s", property->name, value_str);
106 g_value_unset (&value);
114 * debug_dump_element:
115 * @bin: the bin that should be analyzed
116 * @out: file to write to
117 * @indent: level of graph indentation
119 * Helper for _gst_debug_bin_to_dot_file() to recursively dump a pipeline.
122 debug_dump_element (GstBin * bin, GstDebugGraphDetails details, FILE * out,
125 GstIterator *element_iter, *pad_iter;
126 gboolean elements_done, pads_done;
127 GstElement *element, *peer_element, *target_element;
128 GstPad *pad, *peer_pad, *target_pad;
131 GstStructure *structure;
132 gboolean free_caps, free_media;
133 guint src_pads, sink_pads;
135 gchar *pad_name, *element_name;
136 gchar *peer_pad_name, *peer_element_name;
137 gchar *target_pad_name, *target_element_name;
139 gchar *state_name = NULL;
140 gchar *param_name = NULL;
141 gchar spc[1 + indent * 2];
143 memset (spc, 32, indent * 2);
144 spc[indent * 2] = '\0';
146 element_iter = gst_bin_iterate_elements (bin);
147 elements_done = FALSE;
148 while (!elements_done) {
149 switch (gst_iterator_next (element_iter, (gpointer) & element)) {
150 case GST_ITERATOR_OK:
151 element_name = debug_dump_make_object_name (GST_OBJECT (element));
153 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
154 state_name = debug_dump_get_element_state (GST_ELEMENT (element));
156 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
157 param_name = debug_dump_get_element_params (GST_ELEMENT (element));
160 fprintf (out, "%ssubgraph cluster_%s {\n", spc, element_name);
161 fprintf (out, "%s fontname=\"Bitstream Vera Sans\";\n", spc);
162 fprintf (out, "%s fontsize=\"8\";\n", spc);
163 fprintf (out, "%s style=filled;\n", spc);
164 fprintf (out, "%s color=black;\n\n", spc);
165 fprintf (out, "%s label=\"<%s>\\n%s%s%s\";\n", spc,
166 G_OBJECT_TYPE_NAME (element), GST_OBJECT_NAME (element),
167 (state_name ? state_name : ""), (param_name ? param_name : "")
177 g_free (element_name);
179 src_pads = sink_pads = 0;
180 if ((pad_iter = gst_element_iterate_pads (element))) {
183 switch (gst_iterator_next (pad_iter, (gpointer) & pad)) {
184 case GST_ITERATOR_OK:
185 dir = gst_pad_get_direction (pad);
186 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
188 debug_dump_make_object_name (GST_OBJECT (element));
189 if (GST_IS_GHOST_PAD (pad)) {
191 (dir == GST_PAD_SRC) ? "#ffdddd" : ((dir ==
192 GST_PAD_SINK) ? "#ddddff" : "#ffffff");
195 (dir == GST_PAD_SRC) ? "#ffaaaa" : ((dir ==
196 GST_PAD_SINK) ? "#aaaaff" : "#cccccc");
200 "%s %s_%s [color=black, fillcolor=\"%s\", label=\"%s\"];\n",
201 spc, element_name, pad_name, color_name,
202 GST_OBJECT_NAME (pad));
204 if (dir == GST_PAD_SRC)
206 else if (dir == GST_PAD_SINK)
209 g_free (element_name);
210 gst_object_unref (pad);
212 case GST_ITERATOR_RESYNC:
213 gst_iterator_resync (pad_iter);
215 case GST_ITERATOR_ERROR:
216 case GST_ITERATOR_DONE:
221 gst_iterator_free (pad_iter);
223 if (GST_IS_BIN (element)) {
224 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
226 debug_dump_element (GST_BIN (element), details, out, indent + 1);
228 if (src_pads && !sink_pads)
229 fprintf (out, "%s fillcolor=\"#ffaaaa\";\n", spc);
230 else if (!src_pads && sink_pads)
231 fprintf (out, "%s fillcolor=\"#aaaaff\";\n", spc);
232 else if (src_pads && sink_pads)
233 fprintf (out, "%s fillcolor=\"#aaffaa\";\n", spc);
235 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
237 fprintf (out, "%s}\n\n", spc);
238 if ((pad_iter = gst_element_iterate_pads (element))) {
241 switch (gst_iterator_next (pad_iter, (gpointer) & pad)) {
242 case GST_ITERATOR_OK:
243 if (gst_pad_is_linked (pad)
244 && gst_pad_get_direction (pad) == GST_PAD_SRC) {
245 if ((peer_pad = gst_pad_get_peer (pad))) {
247 if ((details & GST_DEBUG_GRAPH_SHOW_MEDIA_TYPE) ||
248 (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS)
250 if ((caps = gst_pad_get_negotiated_caps (pad))) {
256 gst_pad_get_pad_template_caps (pad))) {
257 /* this should not happen */
262 if (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS) {
264 g_strdelimit (gst_caps_to_string (caps), ",",
266 media = g_strescape (tmp, NULL);
270 if (GST_CAPS_IS_SIMPLE (caps)) {
271 structure = gst_caps_get_structure (caps, 0);
273 (gchar *) gst_structure_get_name (structure);
278 gst_caps_unref (caps);
283 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
285 debug_dump_make_object_name (GST_OBJECT (element));
287 debug_dump_make_object_name (GST_OBJECT (peer_pad));
288 if ((peer_element = gst_pad_get_parent_element (peer_pad))) {
290 debug_dump_make_object_name (GST_OBJECT
293 peer_element_name = "";
297 fprintf (out, "%s%s_%s -> %s_%s [label=\"%s\"]\n", spc,
298 element_name, pad_name, peer_element_name,
299 peer_pad_name, media);
304 fprintf (out, "%s%s_%s -> %s_%s\n", spc,
305 element_name, pad_name, peer_element_name,
309 if (GST_IS_GHOST_PAD (pad)) {
311 gst_ghost_pad_get_target (GST_GHOST_PAD (pad)))) {
313 debug_dump_make_object_name (GST_OBJECT
315 if ((target_element =
316 gst_pad_get_parent_element (target_pad))) {
317 target_element_name =
318 debug_dump_make_object_name (GST_OBJECT
321 target_element_name = "";
323 /* src ghostpad relationship */
324 fprintf (out, "%s%s_%s -> %s_%s [style=dashed]\n", spc,
325 target_element_name, target_pad_name, element_name,
328 g_free (target_pad_name);
329 if (target_element) {
330 g_free (target_element_name);
331 gst_object_unref (target_element);
333 gst_object_unref (target_pad);
336 if (GST_IS_GHOST_PAD (peer_pad)) {
338 gst_ghost_pad_get_target (GST_GHOST_PAD
341 debug_dump_make_object_name (GST_OBJECT
343 if ((target_element =
344 gst_pad_get_parent_element (target_pad))) {
345 target_element_name =
346 debug_dump_make_object_name (GST_OBJECT
349 target_element_name = "";
351 /* sink ghostpad relationship */
352 fprintf (out, "%s%s_%s -> %s_%s [style=dashed]\n", spc,
353 peer_element_name, peer_pad_name,
354 target_element_name, target_pad_name);
356 g_free (target_pad_name);
357 if (target_element) {
358 g_free (target_element_name);
359 gst_object_unref (target_element);
361 gst_object_unref (target_pad);
366 g_free (element_name);
367 g_free (peer_pad_name);
369 g_free (peer_element_name);
370 gst_object_unref (peer_element);
372 gst_object_unref (peer_pad);
375 gst_object_unref (pad);
377 case GST_ITERATOR_RESYNC:
378 gst_iterator_resync (pad_iter);
380 case GST_ITERATOR_ERROR:
381 case GST_ITERATOR_DONE:
386 gst_iterator_free (pad_iter);
388 gst_object_unref (element);
390 case GST_ITERATOR_RESYNC:
391 gst_iterator_resync (element_iter);
393 case GST_ITERATOR_ERROR:
394 case GST_ITERATOR_DONE:
395 elements_done = TRUE;
399 gst_iterator_free (element_iter);
403 * _gst_debug_bin_to_dot_file:
404 * @bin: the top-level pipeline that should be analyzed
405 * @file_name: output filename (e.g. "/tmp/metadata.dot")
407 * To aid debugging applications one can use this method to write out the whole
408 * network of gstreamer elements that form the pipeline into an dot file.
409 * This file can be processed with graphviz to get an image.
410 * <informalexample><programlisting>
411 * dot -Tpng -oimage.png graph_lowlevel.dot
412 * </programlisting></informalexample>
415 _gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
416 const gchar * file_name)
420 g_return_if_fail (GST_IS_BIN (bin));
421 g_return_if_fail (file_name != NULL);
423 if (!_gst_debug_dump_dot_files_on)
426 if ((out = fopen (file_name, "wb"))) {
427 gchar *state_name = NULL;
428 gchar *param_name = NULL;
430 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
431 state_name = debug_dump_get_element_state (GST_ELEMENT (bin));
433 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
434 param_name = debug_dump_get_element_params (GST_ELEMENT (bin));
439 "digraph pipeline {\n"
441 " fontname=\"Bitstream Vera Sans\";\n"
445 " label=\"<%s>\\n%s%s%s\";\n"
446 " node [style=filled, shape=box, fontsize=\"7\", fontname=\"Bitstream Vera Sans\"];\n"
447 " edge [labelfontsize=\"7\", fontsize=\"7\", labelfontname=\"Bitstream Vera Sans\", fontname=\"Bitstream Vera Sans\"];\n"
448 "\n", G_OBJECT_TYPE_NAME (bin), GST_OBJECT_NAME (bin),
449 (state_name ? state_name : ""), (param_name ? param_name : "")
456 debug_dump_element (bin, details, out, 1);
459 fprintf (out, "}\n");
465 * _gst_debug_bin_to_dot_file_with_ts:
466 * @bin: the top-level pipeline that should be analyzed
467 * @file_tmpl: output filename template
468 * (e.g. "/tmp/metadata.%" GST_TIME_FORMAT ".dot")
470 * This works like _gst_debug_bin_to_dot_file(), but fills the filename template
471 * with the timestamp, so that it can be used to take multiple snapshots.
474 _gst_debug_bin_to_dot_file_with_ts (GstBin * bin, GstDebugGraphDetails details,
475 const gchar * file_tmpl)
477 gchar *file_name = NULL;
480 GstClockTime elapsed;
483 g_return_if_fail (GST_IS_BIN (bin));
484 g_return_if_fail (file_tmpl != NULL);
486 /* check file-name template */
487 pos = strchr (file_tmpl, '%');
496 pos = strchr (pos, '%');
500 GST_WARNING ("file template has no valid placeholder");
502 } else if (fmt_ct != 4) {
503 GST_WARNING ("file template must have 4 placeholders, but has %d", fmt_ct);
508 g_get_current_time (&now);
509 elapsed = GST_TIMEVAL_TO_TIME (now) - _gst_info_start_time;
510 file_name = g_strdup_printf (file_tmpl, GST_TIME_ARGS (elapsed));
512 _gst_debug_bin_to_dot_file (bin, details, file_name);
516 #endif /* GST_DISABLE_GST_DEBUG */