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