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