gst-inspect: fix unused-const-variable error in windows
[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 utilities
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 putting 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 #define PARAM_MAX_LENGTH 80
60
61 static const gchar spaces[] = {
62   "                                "    /* 32 */
63       "                                "        /* 64 */
64       "                                "        /* 96 */
65       "                                "        /* 128 */
66 };
67
68 #define MAKE_INDENT(indent) \
69   &spaces[MAX (sizeof (spaces) - (1 + (indent) * 2), 0)]
70
71 static gchar *
72 debug_dump_make_object_name (GstObject * obj)
73 {
74   return g_strcanon (g_strdup_printf ("%s_%p", GST_OBJECT_NAME (obj), obj),
75       G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "_", '_');
76 }
77
78 static gchar *
79 debug_dump_get_element_state (GstElement * element)
80 {
81   gchar *state_name = NULL;
82   const gchar *state_icons = "~0-=>";
83   GstState state = GST_STATE_VOID_PENDING, pending = GST_STATE_VOID_PENDING;
84
85   gst_element_get_state (element, &state, &pending, 0);
86   if (pending == GST_STATE_VOID_PENDING) {
87     gboolean is_locked = gst_element_is_locked_state (element);
88     state_name = g_strdup_printf ("\\n[%c]%s", state_icons[state],
89         (is_locked ? "(locked)" : ""));
90   } else {
91     state_name = g_strdup_printf ("\\n[%c] -> [%c]", state_icons[state],
92         state_icons[pending]);
93   }
94   return state_name;
95 }
96
97 static gchar *
98 debug_dump_get_object_params (GObject * object,
99     GstDebugGraphDetails details, const char *const *ignored_propnames)
100 {
101   gchar *param_name = NULL;
102   GParamSpec **properties, *property;
103   GValue value = { 0, };
104   guint i, number_of_properties;
105   gchar *tmp, *value_str;
106   const gchar *ellipses;
107
108   /* get paramspecs and show non-default properties */
109   properties =
110       g_object_class_list_properties (G_OBJECT_GET_CLASS (object),
111       &number_of_properties);
112   if (properties) {
113     for (i = 0; i < number_of_properties; i++) {
114       gint j;
115       gboolean ignore = FALSE;
116       property = properties[i];
117
118       /* skip some properties */
119       if (!(property->flags & G_PARAM_READABLE))
120         continue;
121       if (!strcmp (property->name, "name"))
122         continue;
123
124       if (ignored_propnames)
125         for (j = 0; ignored_propnames[j]; j++)
126           if (!g_strcmp0 (ignored_propnames[j], property->name))
127             ignore = TRUE;
128
129       if (ignore)
130         continue;
131
132       g_value_init (&value, property->value_type);
133       g_object_get_property (G_OBJECT (object), property->name, &value);
134       if (!(g_param_value_defaults (property, &value))) {
135         /* we need to serialise enums and flags ourselves to make sure the
136          * enum/flag nick is used and not the enum/flag name, which would be the
137          * C header enum/flag for public enums/flags, but for element-specific
138          * enums/flags we abuse the name field for the property description,
139          * and we don't want to print that in the dot file. The nick will
140          * always work, and it's also shorter. */
141         if (G_VALUE_HOLDS_ENUM (&value)) {
142           GEnumClass *e_class = g_type_class_ref (G_VALUE_TYPE (&value));
143           gint idx, e_val;
144
145           tmp = NULL;
146           e_val = g_value_get_enum (&value);
147           for (idx = 0; idx < e_class->n_values; ++idx) {
148             if (e_class->values[idx].value == e_val) {
149               tmp = g_strdup (e_class->values[idx].value_nick);
150               break;
151             }
152           }
153           if (tmp == NULL) {
154             g_value_unset (&value);
155             continue;
156           }
157         } else if (G_VALUE_HOLDS_FLAGS (&value)) {
158           GFlagsClass *f_class = g_type_class_ref (G_VALUE_TYPE (&value));
159           GFlagsValue *vals = f_class->values;
160           GString *s = NULL;
161           guint idx, flags_left;
162
163           s = g_string_new (NULL);
164
165           /* we assume the values are sorted from lowest to highest value */
166           flags_left = g_value_get_flags (&value);
167           idx = f_class->n_values;
168           while (idx > 0) {
169             --idx;
170             if (vals[idx].value != 0
171                 && (flags_left & vals[idx].value) == vals[idx].value) {
172               if (s->len > 0)
173                 g_string_prepend_c (s, '+');
174               g_string_prepend (s, vals[idx].value_nick);
175               flags_left -= vals[idx].value;
176               if (flags_left == 0)
177                 break;
178             }
179           }
180
181           if (s->len == 0)
182             g_string_assign (s, "(none)");
183
184           tmp = g_string_free (s, FALSE);
185         } else {
186           tmp = g_strdup_value_contents (&value);
187         }
188         value_str = g_strescape (tmp, NULL);
189         g_free (tmp);
190
191         /* too long, ellipsize */
192         if (!(details & GST_DEBUG_GRAPH_SHOW_FULL_PARAMS) &&
193             strlen (value_str) > PARAM_MAX_LENGTH)
194           ellipses = "…";
195         else
196           ellipses = "";
197
198         if (param_name)
199           tmp = param_name;
200         else
201           tmp = (char *) "";
202
203         if (details & GST_DEBUG_GRAPH_SHOW_FULL_PARAMS) {
204           param_name = g_strdup_printf ("%s\\n%s=%s", tmp, property->name,
205               value_str);
206         } else {
207           param_name = g_strdup_printf ("%s\\n%s=%."
208               G_STRINGIFY (PARAM_MAX_LENGTH) "s%s", tmp, property->name,
209               value_str, ellipses);
210         }
211
212         if (tmp[0] != '\0')
213           g_free (tmp);
214
215         g_free (value_str);
216       }
217       g_value_unset (&value);
218     }
219     g_free (properties);
220   }
221   return param_name;
222 }
223
224 static void
225 debug_dump_pad (GstPad * pad, const gchar * color_name,
226     const gchar * element_name, GstDebugGraphDetails details, GString * str,
227     const gint indent)
228 {
229   GstPadTemplate *pad_templ;
230   GstPadPresence presence;
231   gchar *pad_name, *param_name = NULL;
232   const gchar *style_name;
233   static const char *const ignore_propnames[] =
234       { "parent", "direction", "template",
235     "caps", NULL
236   };
237   const gchar *spc = MAKE_INDENT (indent);
238
239   pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
240
241   /* pad availability */
242   style_name = "filled,solid";
243   if ((pad_templ = gst_pad_get_pad_template (pad))) {
244     presence = GST_PAD_TEMPLATE_PRESENCE (pad_templ);
245     gst_object_unref (pad_templ);
246     if (presence == GST_PAD_SOMETIMES) {
247       style_name = "filled,dotted";
248     } else if (presence == GST_PAD_REQUEST) {
249       style_name = "filled,dashed";
250     }
251   }
252
253   param_name =
254       debug_dump_get_object_params (G_OBJECT (pad), details, ignore_propnames);
255   if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
256     gchar pad_flags[5];
257     const gchar *activation_mode = "-><";
258     const gchar *task_mode = "";
259     GstTask *task;
260
261     GST_OBJECT_LOCK (pad);
262     task = GST_PAD_TASK (pad);
263     if (task) {
264       switch (gst_task_get_state (task)) {
265         case GST_TASK_STARTED:
266           task_mode = "[T]";
267           break;
268         case GST_TASK_PAUSED:
269           task_mode = "[t]";
270           break;
271         default:
272           /* Invalid task state, ignoring */
273           break;
274       }
275     }
276     GST_OBJECT_UNLOCK (pad);
277
278     /* check if pad flags */
279     pad_flags[0] =
280         GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_BLOCKED) ? 'B' : 'b';
281     pad_flags[1] =
282         GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_FLUSHING) ? 'F' : 'f';
283     pad_flags[2] =
284         GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_BLOCKING) ? 'B' : 'b';
285     pad_flags[3] = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_EOS) ? 'E' : '\0';
286     pad_flags[4] = '\0';
287
288     g_string_append_printf (str,
289         "%s  %s_%s [color=black, fillcolor=\"%s\", label=\"%s%s\\n[%c][%s]%s\", height=\"0.2\", style=\"%s\"];\n",
290         spc, element_name, pad_name, color_name, GST_OBJECT_NAME (pad),
291         (param_name ? param_name : ""),
292         activation_mode[pad->mode], pad_flags, task_mode, style_name);
293   } else {
294     g_string_append_printf (str,
295         "%s  %s_%s [color=black, fillcolor=\"%s\", label=\"%s%s\", height=\"0.2\", style=\"%s\"];\n",
296         spc, element_name, pad_name, color_name, GST_OBJECT_NAME (pad),
297         (param_name ? param_name : ""), style_name);
298   }
299
300   g_free (param_name);
301   g_free (pad_name);
302 }
303
304 static void
305 debug_dump_element_pad (GstPad * pad, GstElement * element,
306     GstDebugGraphDetails details, GString * str, const gint indent)
307 {
308   GstElement *target_element;
309   GstPad *target_pad, *tmp_pad;
310   GstPadDirection dir;
311   gchar *element_name;
312   gchar *target_element_name;
313   const gchar *color_name;
314
315   dir = gst_pad_get_direction (pad);
316   element_name = debug_dump_make_object_name (GST_OBJECT (element));
317   if (GST_IS_GHOST_PAD (pad)) {
318     color_name =
319         (dir == GST_PAD_SRC) ? "#ffdddd" : ((dir ==
320             GST_PAD_SINK) ? "#ddddff" : "#ffffff");
321     /* output target-pad so that it belongs to this element */
322     if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad)))) {
323       if ((target_pad = gst_pad_get_peer (tmp_pad))) {
324         gchar *pad_name, *target_pad_name;
325         const gchar *spc = MAKE_INDENT (indent);
326
327         if ((target_element = gst_pad_get_parent_element (target_pad))) {
328           target_element_name =
329               debug_dump_make_object_name (GST_OBJECT (target_element));
330         } else {
331           target_element_name = g_strdup ("");
332         }
333         debug_dump_pad (target_pad, color_name, target_element_name, details,
334             str, indent);
335         /* src ghostpad relationship */
336         pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
337         target_pad_name = debug_dump_make_object_name (GST_OBJECT (target_pad));
338         if (dir == GST_PAD_SRC) {
339           g_string_append_printf (str,
340               "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
341               target_element_name, target_pad_name, element_name, pad_name);
342         } else {
343           g_string_append_printf (str,
344               "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
345               element_name, pad_name, target_element_name, target_pad_name);
346         }
347         g_free (target_pad_name);
348         g_free (target_element_name);
349         if (target_element)
350           gst_object_unref (target_element);
351         gst_object_unref (target_pad);
352         g_free (pad_name);
353       }
354       gst_object_unref (tmp_pad);
355     }
356   } else {
357     color_name =
358         (dir == GST_PAD_SRC) ? "#ffaaaa" : ((dir ==
359             GST_PAD_SINK) ? "#aaaaff" : "#cccccc");
360   }
361   /* pads */
362   debug_dump_pad (pad, color_name, element_name, details, str, indent);
363   g_free (element_name);
364 }
365
366 static gboolean
367 string_append_field (GQuark field, const GValue * value, gpointer ptr)
368 {
369   GString *str = (GString *) ptr;
370   gchar *value_str = gst_value_serialize (value);
371   gchar *esc_value_str;
372
373   if (value_str == NULL) {
374     g_string_append_printf (str, "  %18s: NULL\\l", g_quark_to_string (field));
375     return TRUE;
376   }
377
378   /* some enums can become really long */
379   if (strlen (value_str) > 25) {
380     gint pos = 24;
381
382     /* truncate */
383     value_str[25] = '\0';
384
385     /* mirror any brackets and quotes */
386     if (value_str[0] == '<')
387       value_str[pos--] = '>';
388     if (value_str[0] == '[')
389       value_str[pos--] = ']';
390     if (value_str[0] == '(')
391       value_str[pos--] = ')';
392     if (value_str[0] == '{')
393       value_str[pos--] = '}';
394     if (value_str[0] == '"')
395       value_str[pos--] = '"';
396     if (pos != 24)
397       value_str[pos--] = ' ';
398     /* elippsize */
399     value_str[pos--] = '.';
400     value_str[pos--] = '.';
401     value_str[pos--] = '.';
402   }
403   esc_value_str = g_strescape (value_str, NULL);
404
405   g_string_append_printf (str, "  %18s: %s\\l", g_quark_to_string (field),
406       esc_value_str);
407
408   g_free (value_str);
409   g_free (esc_value_str);
410   return TRUE;
411 }
412
413 static gchar *
414 debug_dump_describe_caps (GstCaps * caps, GstDebugGraphDetails details)
415 {
416   gchar *media = NULL;
417
418   if (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS) {
419
420     if (gst_caps_is_any (caps) || gst_caps_is_empty (caps)) {
421       media = gst_caps_to_string (caps);
422
423     } else {
424       GString *str = NULL;
425       guint i;
426       guint slen = 0;
427
428       for (i = 0; i < gst_caps_get_size (caps); i++) {
429         slen += 25 +
430             STRUCTURE_ESTIMATED_STRING_LEN (gst_caps_get_structure (caps, i));
431       }
432
433       str = g_string_sized_new (slen);
434       for (i = 0; i < gst_caps_get_size (caps); i++) {
435         GstCapsFeatures *features = __gst_caps_get_features_unchecked (caps, i);
436         GstStructure *structure = gst_caps_get_structure (caps, i);
437
438         g_string_append (str, gst_structure_get_name (structure));
439
440         if (features && (gst_caps_features_is_any (features)
441                 || !gst_caps_features_is_equal (features,
442                     GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY))) {
443           g_string_append_c (str, '(');
444           priv_gst_caps_features_append_to_gstring (features, str);
445           g_string_append_c (str, ')');
446         }
447         g_string_append (str, "\\l");
448
449         gst_structure_foreach (structure, string_append_field, (gpointer) str);
450       }
451
452       media = g_string_free (str, FALSE);
453     }
454
455   } else {
456     if (GST_CAPS_IS_SIMPLE (caps))
457       media =
458           g_strdup (gst_structure_get_name (gst_caps_get_structure (caps, 0)));
459     else
460       media = g_strdup ("*");
461   }
462   return media;
463 }
464
465 static void
466 debug_dump_element_pad_link (GstPad * pad, GstElement * element,
467     GstDebugGraphDetails details, GString * str, const gint indent)
468 {
469   GstElement *peer_element;
470   GstPad *peer_pad;
471   GstCaps *caps, *peer_caps;
472   gchar *media = NULL;
473   gchar *media_src = NULL, *media_sink = NULL;
474   gchar *pad_name, *element_name;
475   gchar *peer_pad_name, *peer_element_name;
476   const gchar *spc = MAKE_INDENT (indent);
477
478   if ((peer_pad = gst_pad_get_peer (pad))) {
479     if ((details & GST_DEBUG_GRAPH_SHOW_MEDIA_TYPE) ||
480         (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS)
481         ) {
482       caps = gst_pad_get_current_caps (pad);
483       if (!caps)
484         caps = gst_pad_get_pad_template_caps (pad);
485       peer_caps = gst_pad_get_current_caps (peer_pad);
486       if (!peer_caps)
487         peer_caps = gst_pad_get_pad_template_caps (peer_pad);
488
489       media = debug_dump_describe_caps (caps, details);
490       /* check if peer caps are different */
491       if (peer_caps && !gst_caps_is_equal (caps, peer_caps)) {
492         gchar *tmp;
493
494         tmp = debug_dump_describe_caps (peer_caps, details);
495         if (gst_pad_get_direction (pad) == GST_PAD_SRC) {
496           media_src = media;
497           media_sink = tmp;
498         } else {
499           media_src = tmp;
500           media_sink = media;
501         }
502         media = NULL;
503       }
504       gst_caps_unref (peer_caps);
505       gst_caps_unref (caps);
506     }
507
508     pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
509     if (element) {
510       element_name = debug_dump_make_object_name (GST_OBJECT (element));
511     } else {
512       element_name = g_strdup ("");
513     }
514     peer_pad_name = debug_dump_make_object_name (GST_OBJECT (peer_pad));
515     if ((peer_element = gst_pad_get_parent_element (peer_pad))) {
516       peer_element_name =
517           debug_dump_make_object_name (GST_OBJECT (peer_element));
518     } else {
519       peer_element_name = g_strdup ("");
520     }
521
522     /* pad link */
523     if (media) {
524       g_string_append_printf (str, "%s%s_%s -> %s_%s [label=\"%s\"]\n", spc,
525           element_name, pad_name, peer_element_name, peer_pad_name, media);
526       g_free (media);
527     } else if (media_src && media_sink) {
528       /* dot has some issues with placement of head and taillabels,
529        * we need an empty label to make space */
530       g_string_append_printf (str,
531           "%s%s_%s -> %s_%s [labeldistance=\"10\", labelangle=\"0\", "
532           "label=\"                                                  \", "
533           "taillabel=\"%s\", headlabel=\"%s\"]\n",
534           spc, element_name, pad_name, peer_element_name, peer_pad_name,
535           media_src, media_sink);
536       g_free (media_src);
537       g_free (media_sink);
538     } else {
539       g_string_append_printf (str, "%s%s_%s -> %s_%s\n", spc,
540           element_name, pad_name, peer_element_name, peer_pad_name);
541     }
542
543     g_free (pad_name);
544     g_free (element_name);
545     g_free (peer_pad_name);
546     g_free (peer_element_name);
547     if (peer_element)
548       gst_object_unref (peer_element);
549     gst_object_unref (peer_pad);
550   }
551 }
552
553 static void
554 debug_dump_element_pads (GstIterator * pad_iter, GstPad * pad,
555     GstElement * element, GstDebugGraphDetails details, GString * str,
556     const gint indent, guint * num_pads, gchar * cluster_name,
557     gchar ** first_pad_name)
558 {
559   GValue item = { 0, };
560   gboolean pads_done;
561   const gchar *spc = MAKE_INDENT (indent);
562
563   pads_done = FALSE;
564   while (!pads_done) {
565     switch (gst_iterator_next (pad_iter, &item)) {
566       case GST_ITERATOR_OK:
567         pad = g_value_get_object (&item);
568         if (!*num_pads) {
569           g_string_append_printf (str, "%ssubgraph cluster_%s {\n", spc,
570               cluster_name);
571           g_string_append_printf (str, "%s  label=\"\";\n", spc);
572           g_string_append_printf (str, "%s  style=\"invis\";\n", spc);
573           (*first_pad_name) = debug_dump_make_object_name (GST_OBJECT (pad));
574         }
575         debug_dump_element_pad (pad, element, details, str, indent);
576         (*num_pads)++;
577         g_value_reset (&item);
578         break;
579       case GST_ITERATOR_RESYNC:
580         gst_iterator_resync (pad_iter);
581         break;
582       case GST_ITERATOR_ERROR:
583       case GST_ITERATOR_DONE:
584         pads_done = TRUE;
585         break;
586     }
587   }
588   if (*num_pads) {
589     g_string_append_printf (str, "%s}\n\n", spc);
590   }
591 }
592
593 /*
594  * debug_dump_element:
595  * @bin: the bin that should be analyzed
596  * @out: file to write to
597  * @indent: level of graph indentation
598  *
599  * Helper for gst_debug_bin_to_dot_file() to recursively dump a pipeline.
600  */
601 static void
602 debug_dump_element (GstBin * bin, GstDebugGraphDetails details,
603     GString * str, const gint indent)
604 {
605   GstIterator *element_iter, *pad_iter;
606   gboolean elements_done, pads_done;
607   GValue item = { 0, };
608   GValue item2 = { 0, };
609   GstElement *element;
610   GstPad *pad = NULL;
611   guint src_pads, sink_pads;
612   gchar *src_pad_name = NULL, *sink_pad_name = NULL;
613   gchar *element_name;
614   gchar *state_name = NULL;
615   gchar *param_name = NULL;
616   const gchar *spc = MAKE_INDENT (indent);
617
618   element_iter = gst_bin_iterate_elements (bin);
619   elements_done = FALSE;
620   while (!elements_done) {
621     switch (gst_iterator_next (element_iter, &item)) {
622       case GST_ITERATOR_OK:
623         element = g_value_get_object (&item);
624         element_name = debug_dump_make_object_name (GST_OBJECT (element));
625
626         if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
627           state_name = debug_dump_get_element_state (GST_ELEMENT (element));
628         }
629         if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
630           param_name = debug_dump_get_object_params (G_OBJECT (element),
631               details, NULL);
632         }
633         /* elements */
634         g_string_append_printf (str, "%ssubgraph cluster_%s {\n", spc,
635             element_name);
636         g_string_append_printf (str, "%s  fontname=\"Bitstream Vera Sans\";\n",
637             spc);
638         g_string_append_printf (str, "%s  fontsize=\"8\";\n", spc);
639         g_string_append_printf (str, "%s  style=\"filled,rounded\";\n", spc);
640         g_string_append_printf (str, "%s  color=black;\n", spc);
641         g_string_append_printf (str, "%s  label=\"%s\\n%s%s%s\";\n", spc,
642             G_OBJECT_TYPE_NAME (element), GST_OBJECT_NAME (element),
643             (state_name ? state_name : ""), (param_name ? param_name : "")
644             );
645         if (state_name) {
646           g_free (state_name);
647           state_name = NULL;
648         }
649         if (param_name) {
650           g_free (param_name);
651           param_name = NULL;
652         }
653
654         src_pads = sink_pads = 0;
655         if ((pad_iter = gst_element_iterate_sink_pads (element))) {
656           gchar *cluster_name = g_strdup_printf ("%s_sink", element_name);
657           debug_dump_element_pads (pad_iter, pad, element, details, str,
658               indent + 1, &sink_pads, cluster_name, &sink_pad_name);
659           g_free (cluster_name);
660           gst_iterator_free (pad_iter);
661         }
662         if ((pad_iter = gst_element_iterate_src_pads (element))) {
663           gchar *cluster_name = g_strdup_printf ("%s_src", element_name);
664           debug_dump_element_pads (pad_iter, pad, element, details, str,
665               indent + 1, &src_pads, cluster_name, &src_pad_name);
666           g_free (cluster_name);
667           gst_iterator_free (pad_iter);
668         }
669         if (sink_pads && src_pads) {
670           /* add invisible link from first sink to first src pad */
671           g_string_append_printf (str,
672               "%s  %s_%s -> %s_%s [style=\"invis\"];\n",
673               spc, element_name, sink_pad_name, element_name, src_pad_name);
674         }
675         g_free (sink_pad_name);
676         g_free (src_pad_name);
677         g_free (element_name);
678         sink_pad_name = src_pad_name = NULL;
679         if (GST_IS_BIN (element)) {
680           g_string_append_printf (str, "%s  fillcolor=\"#ffffff\";\n", spc);
681           /* recurse */
682           debug_dump_element (GST_BIN (element), details, str, indent + 1);
683         } else {
684           if (src_pads && !sink_pads)
685             g_string_append_printf (str, "%s  fillcolor=\"#ffaaaa\";\n", spc);
686           else if (!src_pads && sink_pads)
687             g_string_append_printf (str, "%s  fillcolor=\"#aaaaff\";\n", spc);
688           else if (src_pads && sink_pads)
689             g_string_append_printf (str, "%s  fillcolor=\"#aaffaa\";\n", spc);
690           else
691             g_string_append_printf (str, "%s  fillcolor=\"#ffffff\";\n", spc);
692         }
693         g_string_append_printf (str, "%s}\n\n", spc);
694         if ((pad_iter = gst_element_iterate_pads (element))) {
695           pads_done = FALSE;
696           while (!pads_done) {
697             switch (gst_iterator_next (pad_iter, &item2)) {
698               case GST_ITERATOR_OK:
699                 pad = g_value_get_object (&item2);
700                 if (gst_pad_is_linked (pad)) {
701                   if (gst_pad_get_direction (pad) == GST_PAD_SRC) {
702                     debug_dump_element_pad_link (pad, element, details, str,
703                         indent);
704                   } else {
705                     GstPad *peer_pad = gst_pad_get_peer (pad);
706
707                     if (peer_pad) {
708                       if (!GST_IS_GHOST_PAD (peer_pad)
709                           && GST_IS_PROXY_PAD (peer_pad)) {
710                         debug_dump_element_pad_link (peer_pad, NULL, details,
711                             str, indent);
712                       }
713                       gst_object_unref (peer_pad);
714                     }
715                   }
716                 }
717                 g_value_reset (&item2);
718                 break;
719               case GST_ITERATOR_RESYNC:
720                 gst_iterator_resync (pad_iter);
721                 break;
722               case GST_ITERATOR_ERROR:
723               case GST_ITERATOR_DONE:
724                 pads_done = TRUE;
725                 break;
726             }
727           }
728           g_value_unset (&item2);
729           gst_iterator_free (pad_iter);
730         }
731         g_value_reset (&item);
732         break;
733       case GST_ITERATOR_RESYNC:
734         gst_iterator_resync (element_iter);
735         break;
736       case GST_ITERATOR_ERROR:
737       case GST_ITERATOR_DONE:
738         elements_done = TRUE;
739         break;
740     }
741   }
742
743   g_value_unset (&item);
744   gst_iterator_free (element_iter);
745 }
746
747 static void
748 debug_dump_header (GstBin * bin, GstDebugGraphDetails details, GString * str)
749 {
750   gchar *state_name = NULL;
751   gchar *param_name = NULL;
752
753   if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
754     state_name = debug_dump_get_element_state (GST_ELEMENT (bin));
755   }
756   if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
757     param_name = debug_dump_get_object_params (G_OBJECT (bin), details, NULL);
758   }
759
760   /* write header */
761   g_string_append_printf (str,
762       "digraph pipeline {\n"
763       "  rankdir=LR;\n"
764       "  fontname=\"sans\";\n"
765       "  fontsize=\"10\";\n"
766       "  labelloc=t;\n"
767       "  nodesep=.1;\n"
768       "  ranksep=.2;\n"
769       "  label=\"<%s>\\n%s%s%s\";\n"
770       "  node [style=\"filled,rounded\", shape=box, fontsize=\"9\", fontname=\"sans\", margin=\"0.0,0.0\"];\n"
771       "  edge [labelfontsize=\"6\", fontsize=\"9\", fontname=\"monospace\"];\n"
772       "  \n"
773       "  legend [\n"
774       "    pos=\"0,0!\",\n"
775       "    margin=\"0.05,0.05\",\n"
776       "    style=\"filled\",\n"
777       "    label=\"Legend\\lElement-States: [~] void-pending, [0] null, [-] ready, [=] paused, [>] playing\\lPad-Activation: [-] none, [>] push, [<] pull\\lPad-Flags: [b]locked, [f]lushing, [b]locking, [E]OS; upper-case is set\\lPad-Task: [T] has started task, [t] has paused task\\l\",\n"
778       "  ];"
779       "\n", G_OBJECT_TYPE_NAME (bin), GST_OBJECT_NAME (bin),
780       (state_name ? state_name : ""), (param_name ? param_name : "")
781       );
782
783   if (state_name)
784     g_free (state_name);
785   if (param_name)
786     g_free (param_name);
787 }
788
789 static void
790 debug_dump_footer (GString * str)
791 {
792   g_string_append_printf (str, "}\n");
793 }
794
795 /**
796  * gst_debug_bin_to_dot_data:
797  * @bin: the top-level pipeline that should be analyzed
798  * @details: type of #GstDebugGraphDetails to use
799  *
800  * To aid debugging applications one can use this method to obtain the whole
801  * network of gstreamer elements that form the pipeline into an dot file.
802  * This data can be processed with graphviz to get an image.
803  *
804  * Returns: (transfer full): a string containing the pipeline in graphviz
805  * dot format.
806  */
807 gchar *
808 gst_debug_bin_to_dot_data (GstBin * bin, GstDebugGraphDetails details)
809 {
810   GString *str;
811
812   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
813
814   str = g_string_new (NULL);
815
816   debug_dump_header (bin, details, str);
817   debug_dump_element (bin, details, str, 1);
818   debug_dump_footer (str);
819
820   return g_string_free (str, FALSE);
821 }
822
823 /**
824  * gst_debug_bin_to_dot_file:
825  * @bin: the top-level pipeline that should be analyzed
826  * @details: type of #GstDebugGraphDetails to use
827  * @file_name: (type filename): output base filename (e.g. "myplayer")
828  *
829  * To aid debugging applications one can use this method to write out the whole
830  * network of gstreamer elements that form the pipeline into an dot file.
831  * This file can be processed with graphviz to get an image.
832  * <informalexample><programlisting>
833  *  dot -Tpng -oimage.png graph_lowlevel.dot
834  * </programlisting></informalexample>
835  */
836 void
837 gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
838     const gchar * file_name)
839 {
840   gchar *full_file_name = NULL;
841   FILE *out;
842
843   g_return_if_fail (GST_IS_BIN (bin));
844
845   if (G_LIKELY (priv_gst_dump_dot_dir == NULL))
846     return;
847
848   if (!file_name) {
849     file_name = g_get_application_name ();
850     if (!file_name)
851       file_name = "unnamed";
852   }
853
854   full_file_name = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.dot",
855       priv_gst_dump_dot_dir, file_name);
856
857   if ((out = fopen (full_file_name, "wb"))) {
858     gchar *buf;
859
860     buf = gst_debug_bin_to_dot_data (bin, details);
861     fputs (buf, out);
862
863     g_free (buf);
864     fclose (out);
865
866     GST_INFO ("wrote bin graph to : '%s'", full_file_name);
867   } else {
868     GST_WARNING ("Failed to open file '%s' for writing: %s", full_file_name,
869         g_strerror (errno));
870   }
871   g_free (full_file_name);
872 }
873
874 /**
875  * gst_debug_bin_to_dot_file_with_ts:
876  * @bin: the top-level pipeline that should be analyzed
877  * @details: type of #GstDebugGraphDetails to use
878  * @file_name: (type filename): output base filename (e.g. "myplayer")
879  *
880  * This works like gst_debug_bin_to_dot_file(), but adds the current timestamp
881  * to the filename, so that it can be used to take multiple snapshots.
882  */
883 void
884 gst_debug_bin_to_dot_file_with_ts (GstBin * bin,
885     GstDebugGraphDetails details, const gchar * file_name)
886 {
887   gchar *ts_file_name = NULL;
888   GstClockTime elapsed;
889
890   g_return_if_fail (GST_IS_BIN (bin));
891
892   if (!file_name) {
893     file_name = g_get_application_name ();
894     if (!file_name)
895       file_name = "unnamed";
896   }
897
898   /* add timestamp */
899   elapsed = GST_CLOCK_DIFF (_priv_gst_start_time, gst_util_get_timestamp ());
900
901   /* we don't use GST_TIME_FORMAT as such filenames would fail on some
902    * filesystems like fat */
903   ts_file_name =
904       g_strdup_printf ("%u.%02u.%02u.%09u-%s", GST_TIME_ARGS (elapsed),
905       file_name);
906
907   gst_debug_bin_to_dot_file (bin, details, ts_file_name);
908   g_free (ts_file_name);
909 }
910 #else /* !GST_DISABLE_GST_DEBUG */
911 #ifndef GST_REMOVE_DISABLED
912 void
913 gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
914     const gchar * file_name)
915 {
916 }
917
918 void
919 gst_debug_bin_to_dot_file_with_ts (GstBin * bin, GstDebugGraphDetails details,
920     const gchar * file_name)
921 {
922 }
923 #endif /* GST_REMOVE_DISABLED */
924 #endif /* GST_DISABLE_GST_DEBUG */