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 const gchar *priv_gst_dump_dot_dir; /* NULL *//* set from gst.c */
42 extern GstClockTime _priv_gst_info_start_time;
45 debug_dump_make_object_name (GstObject * element)
47 return g_strcanon (g_strdup_printf ("%s_%p", GST_OBJECT_NAME (element),
48 element), G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "_", '_');
52 debug_dump_get_element_state (GstElement * element)
54 gchar *state_name = NULL;
55 const gchar *state_icons = "~0-=>";
56 GstState state = 0, pending = 0;
58 gst_element_get_state (element, &state, &pending, 0);
59 if (pending == GST_STATE_VOID_PENDING) {
60 state_name = g_strdup_printf ("\\n[%c]", state_icons[state]);
62 state_name = g_strdup_printf ("\\n[%c]->[%c]", state_icons[state],
63 state_icons[pending]);
69 debug_dump_get_element_params (GstElement * element)
71 gchar *param_name = NULL;
72 GParamSpec **properties, *property;
73 GValue value = { 0, };
74 guint i, number_of_properties;
75 gchar *tmp, *value_str;
77 /* get paramspecs and show non-default properties */
79 g_object_class_list_properties (G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS
80 (element)), &number_of_properties);
82 for (i = 0; i < number_of_properties; i++) {
83 property = properties[i];
85 /* ski some properties */
86 if (!(property->flags & G_PARAM_READABLE))
88 if (!strcmp (property->name, "name"))
91 g_value_init (&value, property->value_type);
92 g_object_get_property (G_OBJECT (element), property->name, &value);
93 if (!(g_param_value_defaults (property, &value))) {
94 tmp = g_strdup_value_contents (&value);
95 value_str = g_strescape (tmp, NULL);
99 param_name = g_strdup_printf ("%s\\n%s=%s",
100 tmp, property->name, value_str);
103 param_name = g_strdup_printf ("\\n%s=%s", property->name, value_str);
107 g_value_unset (&value);
115 * debug_dump_element:
116 * @bin: the bin that should be analyzed
117 * @out: file to write to
118 * @indent: level of graph indentation
120 * Helper for _gst_debug_bin_to_dot_file() to recursively dump a pipeline.
123 debug_dump_element (GstBin * bin, GstDebugGraphDetails details, FILE * out,
126 GstIterator *element_iter, *pad_iter;
127 gboolean elements_done, pads_done;
128 GstElement *element, *peer_element, *target_element;
129 GstPad *pad, *peer_pad, *target_pad;
132 GstStructure *structure;
133 gboolean free_caps, free_media;
134 guint src_pads, sink_pads;
136 gchar *pad_name, *element_name;
137 gchar *peer_pad_name, *peer_element_name;
138 gchar *target_pad_name, *target_element_name;
140 gchar *state_name = NULL;
141 gchar *param_name = NULL;
144 spc = g_malloc (1 + indent * 2);
145 memset (spc, 32, indent * 2);
146 spc[indent * 2] = '\0';
148 element_iter = gst_bin_iterate_elements (bin);
149 elements_done = FALSE;
150 while (!elements_done) {
151 switch (gst_iterator_next (element_iter, (gpointer) & element)) {
152 case GST_ITERATOR_OK:
153 element_name = debug_dump_make_object_name (GST_OBJECT (element));
155 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
156 state_name = debug_dump_get_element_state (GST_ELEMENT (element));
158 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
159 param_name = debug_dump_get_element_params (GST_ELEMENT (element));
162 fprintf (out, "%ssubgraph cluster_%s {\n", spc, element_name);
163 fprintf (out, "%s fontname=\"Bitstream Vera Sans\";\n", spc);
164 fprintf (out, "%s fontsize=\"8\";\n", spc);
165 fprintf (out, "%s style=filled;\n", spc);
166 fprintf (out, "%s color=black;\n\n", spc);
167 fprintf (out, "%s label=\"<%s>\\n%s%s%s\";\n", spc,
168 G_OBJECT_TYPE_NAME (element), GST_OBJECT_NAME (element),
169 (state_name ? state_name : ""), (param_name ? param_name : "")
179 g_free (element_name);
181 src_pads = sink_pads = 0;
182 if ((pad_iter = gst_element_iterate_pads (element))) {
185 switch (gst_iterator_next (pad_iter, (gpointer) & pad)) {
186 case GST_ITERATOR_OK:
187 dir = gst_pad_get_direction (pad);
188 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
190 debug_dump_make_object_name (GST_OBJECT (element));
191 if (GST_IS_GHOST_PAD (pad)) {
193 (dir == GST_PAD_SRC) ? "#ffdddd" : ((dir ==
194 GST_PAD_SINK) ? "#ddddff" : "#ffffff");
197 (dir == GST_PAD_SRC) ? "#ffaaaa" : ((dir ==
198 GST_PAD_SINK) ? "#aaaaff" : "#cccccc");
202 "%s %s_%s [color=black, fillcolor=\"%s\", label=\"%s\"];\n",
203 spc, element_name, pad_name, color_name,
204 GST_OBJECT_NAME (pad));
206 if (dir == GST_PAD_SRC)
208 else if (dir == GST_PAD_SINK)
211 g_free (element_name);
212 gst_object_unref (pad);
214 case GST_ITERATOR_RESYNC:
215 gst_iterator_resync (pad_iter);
217 case GST_ITERATOR_ERROR:
218 case GST_ITERATOR_DONE:
223 gst_iterator_free (pad_iter);
225 if (GST_IS_BIN (element)) {
226 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
228 debug_dump_element (GST_BIN (element), details, out, indent + 1);
230 if (src_pads && !sink_pads)
231 fprintf (out, "%s fillcolor=\"#ffaaaa\";\n", spc);
232 else if (!src_pads && sink_pads)
233 fprintf (out, "%s fillcolor=\"#aaaaff\";\n", spc);
234 else if (src_pads && sink_pads)
235 fprintf (out, "%s fillcolor=\"#aaffaa\";\n", spc);
237 fprintf (out, "%s fillcolor=\"#ffffff\";\n", spc);
239 fprintf (out, "%s}\n\n", spc);
240 if ((pad_iter = gst_element_iterate_pads (element))) {
243 switch (gst_iterator_next (pad_iter, (gpointer) & pad)) {
244 case GST_ITERATOR_OK:
245 if (gst_pad_is_linked (pad)
246 && gst_pad_get_direction (pad) == GST_PAD_SRC) {
247 if ((peer_pad = gst_pad_get_peer (pad))) {
249 if ((details & GST_DEBUG_GRAPH_SHOW_MEDIA_TYPE) ||
250 (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS)
252 if ((caps = gst_pad_get_negotiated_caps (pad))) {
256 if (!(caps = (GstCaps *)
257 gst_pad_get_pad_template_caps (pad))) {
258 /* this should not happen */
263 if (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS) {
265 g_strdelimit (gst_caps_to_string (caps), ",",
268 media = g_strescape (tmp, NULL);
272 if (GST_CAPS_IS_SIMPLE (caps)) {
273 structure = gst_caps_get_structure (caps, 0);
275 (gchar *) gst_structure_get_name (structure);
280 gst_caps_unref (caps);
285 pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
287 debug_dump_make_object_name (GST_OBJECT (element));
289 debug_dump_make_object_name (GST_OBJECT (peer_pad));
290 if ((peer_element = gst_pad_get_parent_element (peer_pad))) {
292 debug_dump_make_object_name (GST_OBJECT
295 peer_element_name = "";
299 fprintf (out, "%s%s_%s -> %s_%s [label=\"%s\"]\n", spc,
300 element_name, pad_name, peer_element_name,
301 peer_pad_name, media);
306 fprintf (out, "%s%s_%s -> %s_%s\n", spc,
307 element_name, pad_name, peer_element_name,
311 if (GST_IS_GHOST_PAD (pad)) {
313 gst_ghost_pad_get_target (GST_GHOST_PAD (pad)))) {
315 debug_dump_make_object_name (GST_OBJECT
317 if ((target_element =
318 gst_pad_get_parent_element (target_pad))) {
319 target_element_name =
320 debug_dump_make_object_name (GST_OBJECT
323 target_element_name = "";
325 /* src ghostpad relationship */
326 fprintf (out, "%s%s_%s -> %s_%s [style=dashed]\n", spc,
327 target_element_name, target_pad_name, element_name,
330 g_free (target_pad_name);
331 if (target_element) {
332 g_free (target_element_name);
333 gst_object_unref (target_element);
335 gst_object_unref (target_pad);
338 if (GST_IS_GHOST_PAD (peer_pad)) {
340 gst_ghost_pad_get_target (GST_GHOST_PAD
343 debug_dump_make_object_name (GST_OBJECT
345 if ((target_element =
346 gst_pad_get_parent_element (target_pad))) {
347 target_element_name =
348 debug_dump_make_object_name (GST_OBJECT
351 target_element_name = "";
353 /* sink ghostpad relationship */
354 fprintf (out, "%s%s_%s -> %s_%s [style=dashed]\n", spc,
355 peer_element_name, peer_pad_name,
356 target_element_name, target_pad_name);
358 g_free (target_pad_name);
359 if (target_element) {
360 g_free (target_element_name);
361 gst_object_unref (target_element);
363 gst_object_unref (target_pad);
368 g_free (element_name);
369 g_free (peer_pad_name);
371 g_free (peer_element_name);
372 gst_object_unref (peer_element);
374 gst_object_unref (peer_pad);
377 gst_object_unref (pad);
379 case GST_ITERATOR_RESYNC:
380 gst_iterator_resync (pad_iter);
382 case GST_ITERATOR_ERROR:
383 case GST_ITERATOR_DONE:
388 gst_iterator_free (pad_iter);
390 gst_object_unref (element);
392 case GST_ITERATOR_RESYNC:
393 gst_iterator_resync (element_iter);
395 case GST_ITERATOR_ERROR:
396 case GST_ITERATOR_DONE:
397 elements_done = TRUE;
401 gst_iterator_free (element_iter);
406 * _gst_debug_bin_to_dot_file:
407 * @bin: the top-level pipeline that should be analyzed
408 * @file_name: output base filename (e.g. "myplayer")
410 * To aid debugging applications one can use this method to write out the whole
411 * network of gstreamer elements that form the pipeline into an dot file.
412 * This file can be processed with graphviz to get an image.
413 * <informalexample><programlisting>
414 * dot -Tpng -oimage.png graph_lowlevel.dot
415 * </programlisting></informalexample>
418 _gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
419 const gchar * file_name)
421 gchar *full_file_name = NULL;
424 g_return_if_fail (GST_IS_BIN (bin));
426 if (G_LIKELY (priv_gst_dump_dot_dir == NULL))
430 file_name = g_get_application_name ();
432 file_name = "unnamed";
435 full_file_name = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.dot",
436 priv_gst_dump_dot_dir, file_name);
438 if ((out = fopen (full_file_name, "wb"))) {
439 gchar *state_name = NULL;
440 gchar *param_name = NULL;
442 if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
443 state_name = debug_dump_get_element_state (GST_ELEMENT (bin));
445 if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
446 param_name = debug_dump_get_element_params (GST_ELEMENT (bin));
451 "digraph pipeline {\n"
453 " fontname=\"Bitstream Vera Sans\";\n"
457 " label=\"<%s>\\n%s%s%s\";\n"
458 " node [style=filled, shape=box, fontsize=\"7\", fontname=\"Bitstream Vera Sans\"];\n"
459 " edge [labelfontsize=\"7\", fontsize=\"7\", labelfontname=\"Bitstream Vera Sans\", fontname=\"Bitstream Vera Sans\"];\n"
460 "\n", G_OBJECT_TYPE_NAME (bin), GST_OBJECT_NAME (bin),
461 (state_name ? state_name : ""), (param_name ? param_name : "")
468 debug_dump_element (bin, details, out, 1);
471 fprintf (out, "}\n");
474 GST_INFO ("wrote bin graph to : '%s'", full_file_name);
475 g_free (full_file_name);
479 * _gst_debug_bin_to_dot_file_with_ts:
480 * @bin: the top-level pipeline that should be analyzed
481 * @file_name: output base filename (e.g. "myplayer")
483 * This works like _gst_debug_bin_to_dot_file(), but adds the current timestamp
484 * to the filename, so that it can be used to take multiple snapshots.
487 _gst_debug_bin_to_dot_file_with_ts (GstBin * bin, GstDebugGraphDetails details,
488 const gchar * file_name)
490 gchar *ts_file_name = NULL;
492 GstClockTime elapsed;
494 g_return_if_fail (GST_IS_BIN (bin));
497 file_name = g_get_application_name ();
499 file_name = "unnamed";
503 g_get_current_time (&now);
504 elapsed = GST_TIMEVAL_TO_TIME (now) - _priv_gst_info_start_time;
506 g_strdup_printf ("%" GST_TIME_FORMAT "-%s", GST_TIME_ARGS (elapsed),
509 _gst_debug_bin_to_dot_file (bin, details, ts_file_name);
510 g_free (ts_file_name);
513 #endif /* GST_DISABLE_GST_DEBUG */