gst/gstdebugutils.c: Squeeze ghost-pad links and remove <> from classname labels...
[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
27 #include "gst_private.h"
28 #include "gstdebugutils.h"
29
30 #ifndef GST_DISABLE_GST_DEBUG
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <errno.h>
36
37 #include "gstinfo.h"
38 #include "gstbin.h"
39 #include "gstobject.h"
40 #include "gstghostpad.h"
41 #include "gstpad.h"
42 #include "gstutils.h"
43
44 /*** PIPELINE GRAPHS **********************************************************/
45
46 const gchar *priv_gst_dump_dot_dir;     /* NULL *//* set from gst.c */
47
48 extern GstClockTime _priv_gst_info_start_time;
49
50 static gchar *
51 debug_dump_make_object_name (GstObject * element)
52 {
53   return g_strcanon (g_strdup_printf ("%s_%p", GST_OBJECT_NAME (element),
54           element), G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "_", '_');
55 }
56
57 static gchar *
58 debug_dump_get_element_state (GstElement * element)
59 {
60   gchar *state_name = NULL;
61   const gchar *state_icons = "~0-=>";
62   GstState state = 0, pending = 0;
63
64   gst_element_get_state (element, &state, &pending, 0);
65   if (pending == GST_STATE_VOID_PENDING) {
66     state_name = g_strdup_printf ("\\n[%c]", state_icons[state]);
67   } else {
68     state_name = g_strdup_printf ("\\n[%c]->[%c]", state_icons[state],
69         state_icons[pending]);
70   }
71   return state_name;
72 }
73
74 static gchar *
75 debug_dump_get_element_params (GstElement * element)
76 {
77   gchar *param_name = NULL;
78   GParamSpec **properties, *property;
79   GValue value = { 0, };
80   guint i, number_of_properties;
81   gchar *tmp, *value_str;
82
83   /* get paramspecs and show non-default properties */
84   properties =
85       g_object_class_list_properties (G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS
86           (element)), &number_of_properties);
87   if (properties) {
88     for (i = 0; i < number_of_properties; i++) {
89       property = properties[i];
90
91       /* ski some properties */
92       if (!(property->flags & G_PARAM_READABLE))
93         continue;
94       if (!strcmp (property->name, "name"))
95         continue;
96
97       g_value_init (&value, property->value_type);
98       g_object_get_property (G_OBJECT (element), property->name, &value);
99       if (!(g_param_value_defaults (property, &value))) {
100         tmp = g_strdup_value_contents (&value);
101         value_str = g_strescape (tmp, NULL);
102         g_free (tmp);
103         if (param_name) {
104           tmp = param_name;
105           param_name = g_strdup_printf ("%s\\n%s=%s",
106               tmp, property->name, value_str);
107           g_free (tmp);
108         } else {
109           param_name = g_strdup_printf ("\\n%s=%s", property->name, value_str);
110         }
111         g_free (value_str);
112       }
113       g_value_unset (&value);
114     }
115     g_free (properties);
116   }
117   return param_name;
118 }
119
120 static void
121 debug_dump_element_pad (GstPad * pad, GstElement * element,
122     GstDebugGraphDetails details, FILE * out, const gint indent)
123 {
124   GstElement *target_element;
125   GstPad *target_pad, *tmp_pad;
126   GstPadDirection dir;
127   GstPadTemplate *pad_templ;
128   GstPadPresence presence;
129   gchar *pad_name, *element_name;
130   gchar *target_pad_name, *target_element_name;
131   gchar *color_name, *style_name;
132   gchar *spc = NULL;
133
134   spc = g_malloc (1 + indent * 2);
135   memset (spc, 32, indent * 2);
136   spc[indent * 2] = '\0';
137
138   dir = gst_pad_get_direction (pad);
139   pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
140   element_name = debug_dump_make_object_name (GST_OBJECT (element));
141   if (GST_IS_GHOST_PAD (pad)) {
142     color_name =
143         (dir == GST_PAD_SRC) ? "#ffdddd" : ((dir ==
144             GST_PAD_SINK) ? "#ddddff" : "#ffffff");
145     /* output target-pad so that it belongs to this element */
146     if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad)))) {
147       if ((target_pad = gst_pad_get_peer (tmp_pad))) {
148         target_pad_name = debug_dump_make_object_name (GST_OBJECT (target_pad));
149         if ((target_element = gst_pad_get_parent_element (target_pad))) {
150           target_element_name =
151               debug_dump_make_object_name (GST_OBJECT (target_element));
152         } else {
153           target_element_name = "";
154         }
155         style_name = "filled,solid";
156         if ((pad_templ = gst_pad_get_pad_template (target_pad))) {
157           presence = GST_PAD_TEMPLATE_PRESENCE (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         fprintf (out,
165             "%s  %s_%s [color=black, fillcolor=\"%s\", label=\"%s\", height=\"0.2\", style=\"%s\"];\n",
166             spc, target_element_name, target_pad_name, color_name,
167             GST_OBJECT_NAME (target_pad), style_name);
168         g_free (target_pad_name);
169         if (target_element) {
170           g_free (target_element_name);
171           gst_object_unref (target_element);
172         }
173         gst_object_unref (target_pad);
174       }
175       gst_object_unref (tmp_pad);
176     }
177   } else {
178     color_name =
179         (dir == GST_PAD_SRC) ? "#ffaaaa" : ((dir ==
180             GST_PAD_SINK) ? "#aaaaff" : "#cccccc");
181   }
182   /* pads */
183   style_name = "filled,solid";
184   if ((pad_templ = gst_pad_get_pad_template (pad))) {
185     presence = GST_PAD_TEMPLATE_PRESENCE (pad_templ);
186     if (presence == GST_PAD_SOMETIMES) {
187       style_name = "filled,dotted";
188     } else if (presence == GST_PAD_REQUEST) {
189       style_name = "filled,dashed";
190     }
191   }
192   fprintf (out,
193       "%s  %s_%s [color=black, fillcolor=\"%s\", label=\"%s\", height=\"0.2\", style=\"%s\"];\n",
194       spc, element_name, pad_name, color_name, GST_OBJECT_NAME (pad),
195       style_name);
196
197   g_free (pad_name);
198   g_free (element_name);
199   g_free (spc);
200 }
201
202 static void
203 debug_dump_element_pad_link (GstPad * pad, GstElement * element,
204     GstDebugGraphDetails details, FILE * out, const gint indent)
205 {
206   GstElement *peer_element, *target_element;
207   GstPad *peer_pad, *target_pad, *tmp_pad;
208   GstCaps *caps;
209   GstStructure *structure;
210   gboolean free_caps, free_media;
211   gchar *media = NULL;
212   gchar *pad_name, *element_name;
213   gchar *peer_pad_name, *peer_element_name;
214   gchar *target_pad_name, *target_element_name;
215   gchar *spc = NULL;
216
217   spc = g_malloc (1 + indent * 2);
218   memset (spc, 32, indent * 2);
219   spc[indent * 2] = '\0';
220
221   if ((peer_pad = gst_pad_get_peer (pad))) {
222     free_media = FALSE;
223     if ((details & GST_DEBUG_GRAPH_SHOW_MEDIA_TYPE) ||
224         (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS)
225         ) {
226       if ((caps = gst_pad_get_negotiated_caps (pad))) {
227         free_caps = TRUE;
228       } else {
229         free_caps = FALSE;
230         if (!(caps = (GstCaps *)
231                 gst_pad_get_pad_template_caps (pad))) {
232           /* this should not happen */
233           media = "?";
234         }
235       }
236       if (caps) {
237         if (details & GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS) {
238           gchar *tmp = g_strdelimit (gst_caps_to_string (caps), ",",
239               '\n');
240
241           media = g_strescape (tmp, NULL);
242           free_media = TRUE;
243           g_free (tmp);
244         } else {
245           if (GST_CAPS_IS_SIMPLE (caps)) {
246             structure = gst_caps_get_structure (caps, 0);
247             media = (gchar *) gst_structure_get_name (structure);
248           } else
249             media = "*";
250         }
251         if (free_caps) {
252           gst_caps_unref (caps);
253         }
254       }
255     }
256
257     pad_name = debug_dump_make_object_name (GST_OBJECT (pad));
258     if (element) {
259       element_name = debug_dump_make_object_name (GST_OBJECT (element));
260     } else {
261       element_name = "";
262     }
263     peer_pad_name = debug_dump_make_object_name (GST_OBJECT (peer_pad));
264     if ((peer_element = gst_pad_get_parent_element (peer_pad))) {
265       peer_element_name =
266           debug_dump_make_object_name (GST_OBJECT (peer_element));
267     } else {
268       peer_element_name = "";
269     }
270
271     if (GST_IS_GHOST_PAD (pad)) {
272       if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad)))) {
273         if ((target_pad = gst_pad_get_peer (tmp_pad))) {
274           target_pad_name =
275               debug_dump_make_object_name (GST_OBJECT (target_pad));
276           if ((target_element = gst_pad_get_parent_element (target_pad))) {
277             target_element_name =
278                 debug_dump_make_object_name (GST_OBJECT (target_element));
279           } else {
280             target_element_name = "";
281           }
282           /* src ghostpad relationship */
283           fprintf (out, "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
284               target_element_name, target_pad_name, element_name, pad_name);
285
286           g_free (target_pad_name);
287           if (target_element) {
288             g_free (target_element_name);
289             gst_object_unref (target_element);
290           }
291           gst_object_unref (target_pad);
292         }
293         gst_object_unref (tmp_pad);
294       }
295     }
296     if (GST_IS_GHOST_PAD (peer_pad)) {
297       if ((tmp_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (peer_pad)))) {
298         if ((target_pad = gst_pad_get_peer (tmp_pad))) {
299           target_pad_name =
300               debug_dump_make_object_name (GST_OBJECT (target_pad));
301           if ((target_element = gst_pad_get_parent_element (target_pad))) {
302             target_element_name =
303                 debug_dump_make_object_name (GST_OBJECT (target_element));
304           } else {
305             target_element_name = "";
306           }
307           /* sink ghostpad relationship */
308           fprintf (out, "%s%s_%s -> %s_%s [style=dashed, minlen=0]\n", spc,
309               peer_element_name, peer_pad_name,
310               target_element_name, target_pad_name);
311           /* FIXME: we are missing links from the proxy pad
312            * theoretically we need to:
313            * pad=gst_object_ref(target_pad);
314            * goto line 280: if ((peer_pad = gst_pad_get_peer (pad)))
315            * as this would e ugly we need to refactor ...
316            */
317           debug_dump_element_pad_link (target_pad, target_element, details, out,
318               indent);
319           g_free (target_pad_name);
320           if (target_element) {
321             g_free (target_element_name);
322             gst_object_unref (target_element);
323           }
324           gst_object_unref (target_pad);
325         }
326         gst_object_unref (tmp_pad);
327       }
328     }
329
330     /* pad link */
331     if (media) {
332       fprintf (out, "%s%s_%s -> %s_%s [label=\"%s\"]\n", spc,
333           element_name, pad_name, peer_element_name, peer_pad_name, media);
334       if (free_media) {
335         g_free (media);
336       }
337     } else {
338       fprintf (out, "%s%s_%s -> %s_%s\n", spc,
339           element_name, pad_name, peer_element_name, peer_pad_name);
340     }
341
342     g_free (pad_name);
343     if (element) {
344       g_free (element_name);
345     }
346     g_free (peer_pad_name);
347     if (peer_element) {
348       g_free (peer_element_name);
349       gst_object_unref (peer_element);
350     }
351     gst_object_unref (peer_pad);
352   }
353   g_free (spc);
354 }
355
356 /*
357  * debug_dump_element:
358  * @bin: the bin that should be analyzed
359  * @out: file to write to
360  * @indent: level of graph indentation
361  *
362  * Helper for _gst_debug_bin_to_dot_file() to recursively dump a pipeline.
363  */
364 static void
365 debug_dump_element (GstBin * bin, GstDebugGraphDetails details, FILE * out,
366     const gint indent)
367 {
368   GstIterator *element_iter, *pad_iter;
369   gboolean elements_done, pads_done;
370   GstElement *element;
371   GstPad *pad;
372   GstPadDirection dir;
373   guint src_pads, sink_pads;
374   gchar *element_name;
375   gchar *state_name = NULL;
376   gchar *param_name = NULL;
377   gchar *spc = NULL;
378
379   spc = g_malloc (1 + indent * 2);
380   memset (spc, 32, indent * 2);
381   spc[indent * 2] = '\0';
382
383   element_iter = gst_bin_iterate_elements (bin);
384   elements_done = FALSE;
385   while (!elements_done) {
386     switch (gst_iterator_next (element_iter, (gpointer) & element)) {
387       case GST_ITERATOR_OK:
388         element_name = debug_dump_make_object_name (GST_OBJECT (element));
389
390         if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
391           state_name = debug_dump_get_element_state (GST_ELEMENT (element));
392         }
393         if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
394           param_name = debug_dump_get_element_params (GST_ELEMENT (element));
395         }
396         /* elements */
397         fprintf (out, "%ssubgraph cluster_%s {\n", spc, element_name);
398         fprintf (out, "%s  fontname=\"Bitstream Vera Sans\";\n", spc);
399         fprintf (out, "%s  fontsize=\"8\";\n", spc);
400         fprintf (out, "%s  style=filled;\n", spc);
401         fprintf (out, "%s  color=black;\n\n", spc);
402         fprintf (out, "%s  label=\"%s\\n%s%s%s\";\n", spc,
403             G_OBJECT_TYPE_NAME (element), GST_OBJECT_NAME (element),
404             (state_name ? state_name : ""), (param_name ? param_name : "")
405             );
406         if (state_name) {
407           g_free (state_name);
408           state_name = NULL;
409         }
410         if (param_name) {
411           g_free (param_name);
412           param_name = NULL;
413         }
414         g_free (element_name);
415
416         src_pads = sink_pads = 0;
417         if ((pad_iter = gst_element_iterate_pads (element))) {
418           pads_done = FALSE;
419           while (!pads_done) {
420             switch (gst_iterator_next (pad_iter, (gpointer) & pad)) {
421               case GST_ITERATOR_OK:
422                 debug_dump_element_pad (pad, element, details, out, indent);
423                 dir = gst_pad_get_direction (pad);
424                 if (dir == GST_PAD_SRC)
425                   src_pads++;
426                 else if (dir == GST_PAD_SINK)
427                   sink_pads++;
428                 gst_object_unref (pad);
429                 break;
430               case GST_ITERATOR_RESYNC:
431                 gst_iterator_resync (pad_iter);
432                 break;
433               case GST_ITERATOR_ERROR:
434               case GST_ITERATOR_DONE:
435                 pads_done = TRUE;
436                 break;
437             }
438           }
439           gst_iterator_free (pad_iter);
440         }
441         if (GST_IS_BIN (element)) {
442           fprintf (out, "%s  fillcolor=\"#ffffff\";\n", spc);
443           /* recurse */
444           debug_dump_element (GST_BIN (element), details, out, indent + 1);
445         } else {
446           if (src_pads && !sink_pads)
447             fprintf (out, "%s  fillcolor=\"#ffaaaa\";\n", spc);
448           else if (!src_pads && sink_pads)
449             fprintf (out, "%s  fillcolor=\"#aaaaff\";\n", spc);
450           else if (src_pads && sink_pads)
451             fprintf (out, "%s  fillcolor=\"#aaffaa\";\n", spc);
452           else
453             fprintf (out, "%s  fillcolor=\"#ffffff\";\n", spc);
454         }
455         fprintf (out, "%s}\n\n", spc);
456         if ((pad_iter = gst_element_iterate_pads (element))) {
457           pads_done = FALSE;
458           while (!pads_done) {
459             switch (gst_iterator_next (pad_iter, (gpointer) & pad)) {
460               case GST_ITERATOR_OK:
461                 if (gst_pad_is_linked (pad)
462                     && gst_pad_get_direction (pad) == GST_PAD_SRC) {
463                   debug_dump_element_pad_link (pad, element, details, out,
464                       indent);
465                 }
466                 gst_object_unref (pad);
467                 break;
468               case GST_ITERATOR_RESYNC:
469                 gst_iterator_resync (pad_iter);
470                 break;
471               case GST_ITERATOR_ERROR:
472               case GST_ITERATOR_DONE:
473                 pads_done = TRUE;
474                 break;
475             }
476           }
477           gst_iterator_free (pad_iter);
478         }
479         gst_object_unref (element);
480         break;
481       case GST_ITERATOR_RESYNC:
482         gst_iterator_resync (element_iter);
483         break;
484       case GST_ITERATOR_ERROR:
485       case GST_ITERATOR_DONE:
486         elements_done = TRUE;
487         break;
488     }
489   }
490   gst_iterator_free (element_iter);
491   g_free (spc);
492 }
493
494 /*
495  * _gst_debug_bin_to_dot_file:
496  * @bin: the top-level pipeline that should be analyzed
497  * @file_name: output base filename (e.g. "myplayer")
498  *
499  * To aid debugging applications one can use this method to write out the whole
500  * network of gstreamer elements that form the pipeline into an dot file.
501  * This file can be processed with graphviz to get an image.
502  * <informalexample><programlisting>
503  *  dot -Tpng -oimage.png graph_lowlevel.dot
504  * </programlisting></informalexample>
505  */
506 void
507 _gst_debug_bin_to_dot_file (GstBin * bin, GstDebugGraphDetails details,
508     const gchar * file_name)
509 {
510   gchar *full_file_name = NULL;
511   FILE *out;
512
513   g_return_if_fail (GST_IS_BIN (bin));
514
515   if (G_LIKELY (priv_gst_dump_dot_dir == NULL))
516     return;
517
518   if (!file_name) {
519     file_name = g_get_application_name ();
520     if (!file_name)
521       file_name = "unnamed";
522   }
523
524   full_file_name = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.dot",
525       priv_gst_dump_dot_dir, file_name);
526
527   if ((out = fopen (full_file_name, "wb"))) {
528     gchar *state_name = NULL;
529     gchar *param_name = NULL;
530
531     if (details & GST_DEBUG_GRAPH_SHOW_STATES) {
532       state_name = debug_dump_get_element_state (GST_ELEMENT (bin));
533     }
534     if (details & GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS) {
535       param_name = debug_dump_get_element_params (GST_ELEMENT (bin));
536     }
537
538     /* write header */
539     fprintf (out,
540         "digraph pipeline {\n"
541         "  rankdir=LR;\n"
542         "  fontname=\"Bitstream Vera Sans\";\n"
543         "  fontsize=\"8\";\n"
544         "  labelloc=t;\n"
545         "  nodesep=.1;\n"
546         "  ranksep=.2;\n"
547         "  label=\"<%s>\\n%s%s%s\";\n"
548         "  node [style=filled, shape=box, fontsize=\"7\", fontname=\"Bitstream Vera Sans\", margin=\"0.0,0.0\"];\n"
549         "  edge [labelfontsize=\"7\", fontsize=\"7\", labelfontname=\"Bitstream Vera Sans\", fontname=\"Bitstream Vera Sans\"];\n"
550         "\n", G_OBJECT_TYPE_NAME (bin), GST_OBJECT_NAME (bin),
551         (state_name ? state_name : ""), (param_name ? param_name : "")
552         );
553     if (state_name)
554       g_free (state_name);
555     if (param_name)
556       g_free (param_name);
557
558     debug_dump_element (bin, details, out, 1);
559
560     /* write footer */
561     fprintf (out, "}\n");
562     fclose (out);
563     GST_INFO ("wrote bin graph to : '%s'", full_file_name);
564   } else {
565     GST_WARNING ("Failed to open file '%s' for writing: %s", full_file_name,
566         g_strerror (errno));
567   }
568   g_free (full_file_name);
569 }
570
571 /*
572  * _gst_debug_bin_to_dot_file_with_ts:
573  * @bin: the top-level pipeline that should be analyzed
574  * @file_name: output base filename (e.g. "myplayer")
575  *
576  * This works like _gst_debug_bin_to_dot_file(), but adds the current timestamp
577  * to the filename, so that it can be used to take multiple snapshots.
578  */
579 void
580 _gst_debug_bin_to_dot_file_with_ts (GstBin * bin, GstDebugGraphDetails details,
581     const gchar * file_name)
582 {
583   gchar *ts_file_name = NULL;
584   GstClockTime elapsed;
585
586   g_return_if_fail (GST_IS_BIN (bin));
587
588   if (!file_name) {
589     file_name = g_get_application_name ();
590     if (!file_name)
591       file_name = "unnamed";
592   }
593
594   /* add timestamp */
595   elapsed = GST_CLOCK_DIFF (_priv_gst_info_start_time,
596       gst_util_get_timestamp ());
597   ts_file_name =
598       g_strdup_printf ("%" GST_TIME_FORMAT "-%s", GST_TIME_ARGS (elapsed),
599       file_name);
600
601   _gst_debug_bin_to_dot_file (bin, details, ts_file_name);
602   g_free (ts_file_name);
603 }
604
605 #endif /* GST_DISABLE_GST_DEBUG */