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