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