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