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