debug: Dump pad properties values
[platform/upstream/gstreamer.git] / gst / gstdebugutils.c
1 /* GStreamer
2  * Copyright (C) 2007 Stefan Kost <ensonic@users.sf.net>
3  *
4  * gstdebugutils.c: debugging and analysis utillities
5  *
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.
10  *
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.
15  *
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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 /* TODO:
22  * edge [ constraint=false ];
23  *   this creates strange graphs ("minlen=0" is better)
24  * try puting src/sink ghostpads for each bin into invisible clusters
25  *
26  * for more compact nodes, try
27  * - changing node-shape from box into record
28  * - use labels like : element [ label="{element | <src> src | <sink> sink}"]
29  * - point to record-connectors : element1:src -> element2:sink
30  * - we use head/tail labels for pad-caps right now
31  *   - this does not work well, as dot seems to not look at their size when
32  *     doing the layout
33  *   - we could add the caps to the pad itself, then we should use one line per
34  *     caps (simple caps = one line)
35  */
36
37 #include "gst_private.h"
38 #include "gstdebugutils.h"
39
40 #ifndef GST_DISABLE_GST_DEBUG
41
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <errno.h>
46
47 #include "gstinfo.h"
48 #include "gstbin.h"
49 #include "gstobject.h"
50 #include "gstghostpad.h"
51 #include "gstpad.h"
52 #include "gstutils.h"
53 #include "gstvalue.h"
54
55 /*** PIPELINE GRAPHS **********************************************************/
56
57 extern const gchar *priv_gst_dump_dot_dir;      /* NULL *//* set from gst.c */
58
59 #define PARAM_MAX_LENGTH 80
60
61 const gchar spaces[] = {
62   "                                "    /* 32 */
63       "                                "        /* 64 */
64       "                                "        /* 96 */
65       "                                "        /* 128 */
66 };
67
68 #define MAKE_INDENT(indent) \
69   &spaces[MAX (sizeof (spaces) - (1 + (indent) * 2), 0)]
70
71 static gchar *
72 debug_dump_make_object_name (GstObject * obj)
73 {
74   return g_strcanon (g_strdup_printf ("%s_%p", GST_OBJECT_NAME (obj), obj),
75       G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "_", '_');
76 }
77
78 static gchar *
79 debug_dump_get_element_state (GstElement * element)
80 {
81   gchar *state_name = NULL;
82   const gchar *state_icons = "~0-=>";
83   GstState state = GST_STATE_VOID_PENDING, pending = GST_STATE_VOID_PENDING;
84
85   gst_element_get_state (element, &state, &pending, 0);
86   if (pending == GST_STATE_VOID_PENDING) {
87     gboolean is_locked = gst_element_is_locked_state (element);
88     state_name = g_strdup_printf ("\\n[%c]%s", state_icons[state],
89         (is_locked ? "(locked)" : ""));
90   } else {
91     state_name = g_strdup_printf ("\\n[%c] -> [%c]", state_icons[state],
92         state_icons[pending]);
93   }
94   return state_name;
95 }
96
97 static gchar *
98 debug_dump_get_object_params (GObject * object,
99     GstDebugGraphDetails details, const char *const *ignored_propnames)
100 {
101   gchar *param_name = NULL;
102   GParamSpec **properties, *property;
103   GValue value = { 0, };
104   guint i, number_of_properties;
105   gchar *tmp, *value_str;
106   const gchar *ellipses;
107
108   /* get paramspecs and show non-default properties */
109   properties =
110       g_object_class_list_properties (G_OBJECT_GET_CLASS (object),
111       &number_of_properties);
112   if (properties) {
113     for (i = 0; i < number_of_properties; i++) {
114       gint j;
115       gboolean ignore = FALSE;
116       property = properties[i];
117
118       /* skip some properties */
119       if (!(property->flags & G_PARAM_READABLE))
120         continue;
121       if (!strcmp (property->name, "name"))
122         continue;
123
124       if (ignored_propnames)
125         for (j = 0; ignored_propnames[j]; j++)
126           if (!g_strcmp0 (ignored_propnames[j], property->name))
127             ignore = TRUE;
128
129       if (ignore)
130         continue;
131
132       g_value_init (&value, property->value_type);
133       g_object_get_property (G_OBJECT (object), property->name, &value);
134       if (!(g_param_value_defaults (property, &value))) {
135         tmp = g_strdup_value_contents (&value);
136         value_str = g_strescape (tmp, NULL);
137         g_free (tmp);
138
139         /* too long, ellipsize */
140         if (!(details & GST_DEBUG_GRAPH_SHOW_FULL_PARAMS) &&
141             strlen (value_str) > PARAM_MAX_LENGTH)
142           ellipses = "…";
143         else
144           ellipses = "";
145
146         if (param_name)
147           tmp = param_name;
148         else
149           tmp = (char *) "";
150
151         if (details & GST_DEBUG_GRAPH_SHOW_FULL_PARAMS) {
152           param_name = g_strdup_printf ("%s\\n%s=%s", tmp, property->name,
153               value_str);
154         } else {
155           param_name = g_strdup_printf ("%s\\n%s=%."
156               G_STRINGIFY (PARAM_MAX_LENGTH) "s%s", tmp, property->name,
157               value_str, ellipses);
158         }
159
160         if (tmp[0] != '\0')
161           g_free (tmp);
162
163         g_free (value_str);
164       }
165       g_value_unset (&value);
166     }
167     g_free (properties);
168   }
169   return param_name;
170 }
171
172 static void
173 debug_dump_pad (GstPad * pad, const gchar * color_name,
174     const gchar * element_name, GstDebugGraphDetails details, GString * str,
175     const gint indent)
176 {
177   GstPadTemplate *pad_templ;
178   GstPadPresence presence;
179   gchar *pad_name, *param_name = NULL;
180   const gchar *style_name;
181   static const char *const ignore_propnames[] =
182       { "parent", "direction", "template",
183     "caps", NULL
184   };
185   const gchar *spc = MAKE_INDENT (indent);
186
187   pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
188
189   /* pad availability */
190   style_name = "filled,solid";
191   if ((pad_templ = gst_pad_get_pad_template (pad))) {
192     presence = GST_PAD_TEMPLATE_PRESENCE (pad_templ);
193     gst_object_unref (pad_templ);
194     if (presence == GST_PAD_SOMETIMES) {
195       style_name = "filled,dotted";
196     } else if (presence == GST_PAD_REQUEST) {
197       style_name = "filled,dashed";
198     }
199   }
200
201   param_name =
202       debug_dump_get_object_params (G_OBJECT (pad), details, ignore_propnames);
203   if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
204     gchar pad_flags[4];
205     const gchar *activation_mode = "-><";
206     const gchar *task_mode = "";
207     GstTask *task;
208
209     GST_OBJECT_LOCK (pad);
210     task = GST_PAD_TASK (pad);
211     if (task) {
212       switch (gst_task_get_state (task)) {
213         case GST_TASK_STARTED:
214           task_mode = "[T]";
215           break;
216         case GST_TASK_PAUSED:
217           task_mode = "[t]";
218           break;
219         default:
220           /* Invalid task state, ignoring */
221           break;
222       }
223     }
224     GST_OBJECT_UNLOCK (pad);
225
226     /* check if pad flags */
227     pad_flags[0] =
228         GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_BLOCKED) ? 'B' : 'b';
229     pad_flags[1] =
230         GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_FLUSHING) ? 'F' : 'f';
231     pad_flags[2] =
232         GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_BLOCKING) ? 'B' : 'b';
233     pad_flags[3] = '\0';
234
235     g_string_append_printf (str,
236         "%s  %s_%s [color=black, fillcolor=\"%s\", label=\"%s%s\\n[%c][%s]%s\", height=\"0.2\", style=\"%s\"];\n",
237         spc, element_name, pad_name, color_name, GST_OBJECT_NAME (pad),
238         (param_name ? param_name : ""),
239         activation_mode[pad->mode], pad_flags, task_mode, style_name);
240   } else {
241     g_string_append_printf (str,
242         "%s  %s_%s [color=black, fillcolor=\"%s\", label=\"%s%s\", height=\"0.2\", style=\"%s\"];\n",
243         spc, element_name, pad_name, color_name, GST_OBJECT_NAME (pad),
244         (param_name ? param_name : ""), style_name);
245   }
246
247   g_free (pad_name);
248 }
249
250 static void
251 debug_dump_element_pad (GstPad * pad, GstElement * element,
252     GstDebugGraphDetails details, GString * str, const gint indent)
253 {
254   GstElement *target_element;
255   GstPad *target_pad, *tmp_pad;
256   GstPadDirection dir;
257   gchar *element_name;
258   gchar *target_element_name;
259   const gchar *color_name;
260
261   dir = gst_pad_get_direction (pad);
262   element_name = debug_dump_make_object_name (GST_OBJECT (element));
263   if (GST_IS_GHOST_PAD (pad)) {
264     color_name =
265         (dir == GST_PAD_SRC) ? "#ffdddd" : ((dir ==
266             GST_PAD_SINK) ? "#ddddff" : "#ffffff");
267     /* output target-pad so that it belongs to this element */
268     if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad)))) {
269       if ((target_pad = gst_pad_get_peer (tmp_pad))) {
270         gchar *pad_name, *target_pad_name;
271         const gchar *spc = MAKE_INDENT (indent);
272
273         if ((target_element = gst_pad_get_parent_element (target_pad))) {
274           target_element_name =
275               debug_dump_make_object_name (GST_OBJECT (target_element));
276         } else {
277           target_element_name = g_strdup ("");
278         }
279         debug_dump_pad (target_pad, color_name, target_element_name, details,
280             str, indent);
281         /* src ghostpad relationship */
282         pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
283         target_pad_name = debug_dump_make_object_name (GST_OBJECT (target_pad));
284         if (dir == GST_PAD_SRC) {
285           g_string_append_printf (str,
286               "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
287               target_element_name, target_pad_name, element_name, pad_name);
288         } else {
289           g_string_append_printf (str,
290               "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
291               element_name, pad_name, target_element_name, target_pad_name);
292         }
293         g_free (target_pad_name);
294         g_free (target_element_name);
295         if (target_element)
296           gst_object_unref (target_element);
297         gst_object_unref (target_pad);
298         g_free (pad_name);
299       }
300       gst_object_unref (tmp_pad);
301     }
302   } else {
303     color_name =
304         (dir == GST_PAD_SRC) ? "#ffaaaa" : ((dir ==
305             GST_PAD_SINK) ? "#aaaaff" : "#cccccc");
306   }
307   /* pads */
308   debug_dump_pad (pad, color_name, element_name, details, str, indent);
309   g_free (element_name);
310 }
311
312 static gboolean
313 string_append_field (GQuark field, const GValue * value, gpointer ptr)
314 {
315   GString *str = (GString *) ptr;
316   gchar *value_str = gst_value_serialize (value);
317   gchar *esc_value_str;
318
319   if (value_str == NULL) {
320     g_string_append_printf (str, "  %18s: NULL\\l", g_quark_to_string (field));
321     return TRUE;
322   }
323
324   /* some enums can become really long */
325   if (strlen (value_str) > 25) {
326     gint pos = 24;
327
328     /* truncate */
329     value_str[25] = '\0';
330
331     /* mirror any brackets and quotes */
332     if (value_str[0] == '<')
333       value_str[pos--] = '>';
334     if (value_str[0] == '[')
335       value_str[pos--] = ']';
336     if (value_str[0] == '(')
337       value_str[pos--] = ')';
338     if (value_str[0] == '{')
339       value_str[pos--] = '}';
340     if (value_str[0] == '"')
341       value_str[pos--] = '"';
342     if (pos != 24)
343       value_str[pos--] = ' ';
344     /* elippsize */
345     value_str[pos--] = '.';
346     value_str[pos--] = '.';
347     value_str[pos--] = '.';
348   }
349   esc_value_str = g_strescape (value_str, NULL);
350
351   g_string_append_printf (str, "  %18s: %s\\l", g_quark_to_string (field),
352       esc_value_str);
353
354   g_free (value_str);
355   g_free (esc_value_str);
356   return TRUE;
357 }
358
359 static gchar *
360 debug_dump_describe_caps (GstCaps * caps, GstDebugGraphDetails details)
361 {
362   gchar *media = NULL;
363
364   if (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS) {
365
366     if (gst_caps_is_any (caps) || gst_caps_is_empty (caps)) {
367       media = gst_caps_to_string (caps);
368
369     } else {
370       GString *str = NULL;
371       guint i;
372       guint slen = 0;
373
374       for (i = 0; i < gst_caps_get_size (caps); i++) {
375         slen += 25 +
376             STRUCTURE_ESTIMATED_STRING_LEN (gst_caps_get_structure (caps, i));
377       }
378
379       str = g_string_sized_new (slen);
380       for (i = 0; i < gst_caps_get_size (caps); i++) {
381         GstCapsFeatures *features = __gst_caps_get_features_unchecked (caps, i);
382         GstStructure *structure = gst_caps_get_structure (caps, i);
383
384         g_string_append (str, gst_structure_get_name (structure));
385
386         if (features && (gst_caps_features_is_any (features)
387                 || !gst_caps_features_is_equal (features,
388                     GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY))) {
389           g_string_append_c (str, '(');
390           priv_gst_caps_features_append_to_gstring (features, str);
391           g_string_append_c (str, ')');
392         }
393         g_string_append (str, "\\l");
394
395         gst_structure_foreach (structure, string_append_field, (gpointer) str);
396       }
397
398       media = g_string_free (str, FALSE);
399     }
400
401   } else {
402     if (GST_CAPS_IS_SIMPLE (caps))
403       media =
404           g_strdup (gst_structure_get_name (gst_caps_get_structure (caps, 0)));
405     else
406       media = g_strdup ("*");
407   }
408   return media;
409 }
410
411 static void
412 debug_dump_element_pad_link (GstPad * pad, GstElement * element,
413     GstDebugGraphDetails details, GString * str, const gint indent)
414 {
415   GstElement *peer_element;
416   GstPad *peer_pad;
417   GstCaps *caps, *peer_caps;
418   gchar *media = NULL;
419   gchar *media_src = NULL, *media_sink = NULL;
420   gchar *pad_name, *element_name;
421   gchar *peer_pad_name, *peer_element_name;
422   const gchar *spc = MAKE_INDENT (indent);
423
424   if ((peer_pad = gst_pad_get_peer (pad))) {
425     if ((details & GST_DEBUG_GRAPH_SHOW_MEDIA_TYPE) ||
426         (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS)
427         ) {
428       caps = gst_pad_get_current_caps (pad);
429       if (!caps)
430         caps = gst_pad_get_pad_template_caps (pad);
431       peer_caps = gst_pad_get_current_caps (peer_pad);
432       if (!peer_caps)
433         peer_caps = gst_pad_get_pad_template_caps (peer_pad);
434
435       media = debug_dump_describe_caps (caps, details);
436       /* check if peer caps are different */
437       if (peer_caps && !gst_caps_is_equal (caps, peer_caps)) {
438         gchar *tmp;
439
440         tmp = debug_dump_describe_caps (peer_caps, details);
441         if (gst_pad_get_direction (pad) == GST_PAD_SRC) {
442           media_src = media;
443           media_sink = tmp;
444         } else {
445           media_src = tmp;
446           media_sink = media;
447         }
448         media = NULL;
449       }
450       gst_caps_unref (peer_caps);
451       gst_caps_unref (caps);
452     }
453
454     pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
455     if (element) {
456       element_name = debug_dump_make_object_name (GST_OBJECT (element));
457     } else {
458       element_name = g_strdup ("");
459     }
460     peer_pad_name = debug_dump_make_object_name (GST_OBJECT (peer_pad));
461     if ((peer_element = gst_pad_get_parent_element (peer_pad))) {
462       peer_element_name =
463           debug_dump_make_object_name (GST_OBJECT (peer_element));
464     } else {
465       peer_element_name = g_strdup ("");
466     }
467
468     /* pad link */
469     if (media) {
470       g_string_append_printf (str, "%s%s_%s -> %s_%s [label=\"%s\"]\n", spc,
471           element_name, pad_name, peer_element_name, peer_pad_name, media);
472       g_free (media);
473     } else if (media_src && media_sink) {
474       /* dot has some issues with placement of head and taillabels,
475        * we need an empty label to make space */
476       g_string_append_printf (str,
477           "%s%s_%s -> %s_%s [labeldistance=\"10\", labelangle=\"0\", "
478           "label=\"                                                  \", "
479           "taillabel=\"%s\", headlabel=\"%s\"]\n",
480           spc, element_name, pad_name, peer_element_name, peer_pad_name,
481           media_src, media_sink);
482       g_free (media_src);
483       g_free (media_sink);
484     } else {
485       g_string_append_printf (str, "%s%s_%s -> %s_%s\n", spc,
486           element_name, pad_name, peer_element_name, peer_pad_name);
487     }
488
489     g_free (pad_name);
490     g_free (element_name);
491     g_free (peer_pad_name);
492     g_free (peer_element_name);
493     if (peer_element)
494       gst_object_unref (peer_element);
495     gst_object_unref (peer_pad);
496   }
497 }
498
499 static void
500 debug_dump_element_pads (GstIterator * pad_iter, GstPad * pad,
501     GstElement * element, GstDebugGraphDetails details, GString * str,
502     const gint indent, guint * num_pads, gchar * cluster_name,
503     gchar ** first_pad_name)
504 {
505   GValue item = { 0, };
506   gboolean pads_done;
507   const gchar *spc = MAKE_INDENT (indent);
508
509   pads_done = FALSE;
510   while (!pads_done) {
511     switch (gst_iterator_next (pad_iter, &item)) {
512       case GST_ITERATOR_OK:
513         pad = g_value_get_object (&item);
514         if (!*num_pads) {
515           g_string_append_printf (str, "%ssubgraph cluster_%s {\n", spc,
516               cluster_name);
517           g_string_append_printf (str, "%s  label=\"\";\n", spc);
518           g_string_append_printf (str, "%s  style=\"invis\";\n", spc);
519           (*first_pad_name) = debug_dump_make_object_name (GST_OBJECT (pad));
520         }
521         debug_dump_element_pad (pad, element, details, str, indent);
522         (*num_pads)++;
523         g_value_reset (&item);
524         break;
525       case GST_ITERATOR_RESYNC:
526         gst_iterator_resync (pad_iter);
527         break;
528       case GST_ITERATOR_ERROR:
529       case GST_ITERATOR_DONE:
530         pads_done = TRUE;
531         break;
532     }
533   }
534   if (*num_pads) {
535     g_string_append_printf (str, "%s}\n\n", spc);
536   }
537 }
538
539 /*
540  * debug_dump_element:
541  * @bin: the bin that should be analyzed
542  * @out: file to write to
543  * @indent: level of graph indentation
544  *
545  * Helper for gst_debug_bin_to_dot_file() to recursively dump a pipeline.
546  */
547 static void
548 debug_dump_element (GstBin * bin, GstDebugGraphDetails details,
549     GString * str, const gint indent)
550 {
551   GstIterator *element_iter, *pad_iter;
552   gboolean elements_done, pads_done;
553   GValue item = { 0, };
554   GValue item2 = { 0, };
555   GstElement *element;
556   GstPad *pad = NULL;
557   guint src_pads, sink_pads;
558   gchar *src_pad_name = NULL, *sink_pad_name = NULL;
559   gchar *element_name;
560   gchar *state_name = NULL;
561   gchar *param_name = NULL;
562   const gchar *spc = MAKE_INDENT (indent);
563
564   element_iter = gst_bin_iterate_elements (bin);
565   elements_done = FALSE;
566   while (!elements_done) {
567     switch (gst_iterator_next (element_iter, &item)) {
568       case GST_ITERATOR_OK:
569         element = g_value_get_object (&item);
570         element_name = debug_dump_make_object_name (GST_OBJECT (element));
571
572         if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
573           state_name = debug_dump_get_element_state (GST_ELEMENT (element));
574         }
575         if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
576           param_name = debug_dump_get_object_params (G_OBJECT (element),
577               details, NULL);
578         }
579         /* elements */
580         g_string_append_printf (str, "%ssubgraph cluster_%s {\n", spc,
581             element_name);
582         g_string_append_printf (str, "%s  fontname=\"Bitstream Vera Sans\";\n",
583             spc);
584         g_string_append_printf (str, "%s  fontsize=\"8\";\n", spc);
585         g_string_append_printf (str, "%s  style=\"filled,rounded\";\n", spc);
586         g_string_append_printf (str, "%s  color=black;\n", spc);
587         g_string_append_printf (str, "%s  label=\"%s\\n%s%s%s\";\n", spc,
588             G_OBJECT_TYPE_NAME (element), GST_OBJECT_NAME (element),
589             (state_name ? state_name : ""), (param_name ? param_name : "")
590             );
591         if (state_name) {
592           g_free (state_name);
593           state_name = NULL;
594         }
595         if (param_name) {
596           g_free (param_name);
597           param_name = NULL;
598         }
599
600         src_pads = sink_pads = 0;
601         if ((pad_iter = gst_element_iterate_sink_pads (element))) {
602           gchar *cluster_name = g_strdup_printf ("%s_sink", element_name);
603           debug_dump_element_pads (pad_iter, pad, element, details, str,
604               indent + 1, &sink_pads, cluster_name, &sink_pad_name);
605           g_free (cluster_name);
606           gst_iterator_free (pad_iter);
607         }
608         if ((pad_iter = gst_element_iterate_src_pads (element))) {
609           gchar *cluster_name = g_strdup_printf ("%s_src", element_name);
610           debug_dump_element_pads (pad_iter, pad, element, details, str,
611               indent + 1, &src_pads, cluster_name, &src_pad_name);
612           g_free (cluster_name);
613           gst_iterator_free (pad_iter);
614         }
615         if (sink_pads && src_pads) {
616           /* add invisible link from first sink to first src pad */
617           g_string_append_printf (str,
618               "%s  %s_%s -> %s_%s [style=\"invis\"];\n",
619               spc, element_name, sink_pad_name, element_name, src_pad_name);
620         }
621         g_free (sink_pad_name);
622         g_free (src_pad_name);
623         g_free (element_name);
624         sink_pad_name = src_pad_name = NULL;
625         if (GST_IS_BIN (element)) {
626           g_string_append_printf (str, "%s  fillcolor=\"#ffffff\";\n", spc);
627           /* recurse */
628           debug_dump_element (GST_BIN (element), details, str, indent + 1);
629         } else {
630           if (src_pads && !sink_pads)
631             g_string_append_printf (str, "%s  fillcolor=\"#ffaaaa\";\n", spc);
632           else if (!src_pads && sink_pads)
633             g_string_append_printf (str, "%s  fillcolor=\"#aaaaff\";\n", spc);
634           else if (src_pads && sink_pads)
635             g_string_append_printf (str, "%s  fillcolor=\"#aaffaa\";\n", spc);
636           else
637             g_string_append_printf (str, "%s  fillcolor=\"#ffffff\";\n", spc);
638         }
639         g_string_append_printf (str, "%s}\n\n", spc);
640         if ((pad_iter = gst_element_iterate_pads (element))) {
641           pads_done = FALSE;
642           while (!pads_done) {
643             switch (gst_iterator_next (pad_iter, &item2)) {
644               case GST_ITERATOR_OK:
645                 pad = g_value_get_object (&item2);
646                 if (gst_pad_is_linked (pad)) {
647                   if (gst_pad_get_direction (pad) == GST_PAD_SRC) {
648                     debug_dump_element_pad_link (pad, element, details, str,
649                         indent);
650                   } else {
651                     GstPad *peer_pad = gst_pad_get_peer (pad);
652
653                     if (peer_pad) {
654                       if (!GST_IS_GHOST_PAD (peer_pad)
655                           && GST_IS_PROXY_PAD (peer_pad)) {
656                         debug_dump_element_pad_link (peer_pad, NULL, details,
657                             str, indent);
658                       }
659                       gst_object_unref (peer_pad);
660                     }
661                   }
662                 }
663                 g_value_reset (&item2);
664                 break;
665               case GST_ITERATOR_RESYNC:
666                 gst_iterator_resync (pad_iter);
667                 break;
668               case GST_ITERATOR_ERROR:
669               case GST_ITERATOR_DONE:
670                 pads_done = TRUE;
671                 break;
672             }
673           }
674           g_value_unset (&item2);
675           gst_iterator_free (pad_iter);
676         }
677         g_value_reset (&item);
678         break;
679       case GST_ITERATOR_RESYNC:
680         gst_iterator_resync (element_iter);
681         break;
682       case GST_ITERATOR_ERROR:
683       case GST_ITERATOR_DONE:
684         elements_done = TRUE;
685         break;
686     }
687   }
688
689   g_value_unset (&item);
690   gst_iterator_free (element_iter);
691 }
692
693 static void
694 debug_dump_header (GstBin * bin, GstDebugGraphDetails details, GString * str)
695 {
696   gchar *state_name = NULL;
697   gchar *param_name = NULL;
698
699   if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
700     state_name = debug_dump_get_element_state (GST_ELEMENT (bin));
701   }
702   if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
703     param_name = debug_dump_get_object_params (G_OBJECT (bin), details, NULL);
704   }
705
706   /* write header */
707   g_string_append_printf (str,
708       "digraph pipeline {\n"
709       "  rankdir=LR;\n"
710       "  fontname=\"sans\";\n"
711       "  fontsize=\"10\";\n"
712       "  labelloc=t;\n"
713       "  nodesep=.1;\n"
714       "  ranksep=.2;\n"
715       "  label=\"<%s>\\n%s%s%s\";\n"
716       "  node [style=\"filled,rounded\", shape=box, fontsize=\"9\", fontname=\"sans\", margin=\"0.0,0.0\"];\n"
717       "  edge [labelfontsize=\"6\", fontsize=\"9\", fontname=\"monospace\"];\n"
718       "  \n"
719       "  legend [\n"
720       "    pos=\"0,0!\",\n"
721       "    margin=\"0.05,0.05\",\n"
722       "    style=\"filled\",\n"
723       "    label=\"Legend\\lElement-States: [~] void-pending, [0] null, [-] ready, [=] paused, [>] playing\\lPad-Activation: [-] none, [>] push, [<] pull\\lPad-Flags: [b]locked, [f]lushing, [b]locking; upper-case is set\\lPad-Task: [T] has started task, [t] has paused task\\l\",\n"
724       "  ];"
725       "\n", G_OBJECT_TYPE_NAME (bin), GST_OBJECT_NAME (bin),
726       (state_name ? state_name : ""), (param_name ? param_name : "")
727       );
728
729   if (state_name)
730     g_free (state_name);
731   if (param_name)
732     g_free (param_name);
733 }
734
735 static void
736 debug_dump_footer (GString * str)
737 {
738   g_string_append_printf (str, "}\n");
739 }
740
741 /*
742  * gst_debug_bin_to_dot_data:
743  * @bin: the top-level pipeline that should be analyzed
744  *
745  * To aid debugging applications one can use this method to obtain the whole
746  * network of gstreamer elements that form the pipeline into an dot file.
747  * This data can be processed with graphviz to get an image.
748  *
749  * Returns: (transfer full): a string containing the pipeline in graphviz
750  * dot format.
751  */
752 gchar *
753 gst_debug_bin_to_dot_data (GstBin * bin, GstDebugGraphDetails details)
754 {
755   GString *str;
756
757   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
758
759   str = g_string_new (NULL);
760
761   debug_dump_header (bin, details, str);
762   debug_dump_element (bin, details, str, 1);
763   debug_dump_footer (str);
764
765   return g_string_free (str, FALSE);
766 }
767
768 /*
769  * gst_debug_bin_to_dot_file:
770  * @bin: the top-level pipeline that should be analyzed
771  * @file_name: output base filename (e.g. "myplayer")
772  *
773  * To aid debugging applications one can use this method to write out the whole
774  * network of gstreamer elements that form the pipeline into an dot file.
775  * This file can be processed with graphviz to get an image.
776  * <informalexample><programlisting>
777  *  dot -Tpng -oimage.png graph_lowlevel.dot
778  * </programlisting></informalexample>
779  */
780 void
781 gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
782     const gchar * file_name)
783 {
784   gchar *full_file_name = NULL;
785   FILE *out;
786
787   g_return_if_fail (GST_IS_BIN (bin));
788
789   if (G_LIKELY (priv_gst_dump_dot_dir == NULL))
790     return;
791
792   if (!file_name) {
793     file_name = g_get_application_name ();
794     if (!file_name)
795       file_name = "unnamed";
796   }
797
798   full_file_name = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.dot",
799       priv_gst_dump_dot_dir, file_name);
800
801   if ((out = fopen (full_file_name, "wb"))) {
802     gchar *buf;
803
804     buf = gst_debug_bin_to_dot_data (bin, details);
805     fputs (buf, out);
806
807     g_free (buf);
808     fclose (out);
809
810     GST_INFO ("wrote bin graph to : '%s'", full_file_name);
811   } else {
812     GST_WARNING ("Failed to open file '%s' for writing: %s", full_file_name,
813         g_strerror (errno));
814   }
815   g_free (full_file_name);
816 }
817
818 /*
819  * gst_debug_bin_to_dot_file_with_ts:
820  * @bin: the top-level pipeline that should be analyzed
821  * @file_name: output base filename (e.g. "myplayer")
822  *
823  * This works like gst_debug_bin_to_dot_file(), but adds the current timestamp
824  * to the filename, so that it can be used to take multiple snapshots.
825  */
826 void
827 gst_debug_bin_to_dot_file_with_ts (GstBin * bin,
828     GstDebugGraphDetails details, const gchar * file_name)
829 {
830   gchar *ts_file_name = NULL;
831   GstClockTime elapsed;
832
833   g_return_if_fail (GST_IS_BIN (bin));
834
835   if (!file_name) {
836     file_name = g_get_application_name ();
837     if (!file_name)
838       file_name = "unnamed";
839   }
840
841   /* add timestamp */
842   elapsed = GST_CLOCK_DIFF (_priv_gst_info_start_time,
843       gst_util_get_timestamp ());
844
845   /* we don't use GST_TIME_FORMAT as such filenames would fail on some
846    * filesystems like fat */
847   ts_file_name =
848       g_strdup_printf ("%u.%02u.%02u.%09u-%s", GST_TIME_ARGS (elapsed),
849       file_name);
850
851   gst_debug_bin_to_dot_file (bin, details, ts_file_name);
852   g_free (ts_file_name);
853 }
854 #else /* !GST_DISABLE_GST_DEBUG */
855 #ifndef GST_REMOVE_DISABLED
856 void
857 gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
858     const gchar * file_name)
859 {
860 }
861
862 void
863 gst_debug_bin_to_dot_file_with_ts (GstBin * bin, GstDebugGraphDetails details,
864     const gchar * file_name)
865 {
866 }
867 #endif /* GST_REMOVE_DISABLED */
868 #endif /* GST_DISABLE_GST_DEBUG */