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