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