tracer: add element-change-state-pre/post hook
[platform/upstream/gstreamer.git] / gst / gsttracerutils.c
1 /* GStreamer
2  * Copyright (C) 2013 Stefan Sauer <ensonic@users.sf.net>
3  *
4  * gsttracerutils.c: tracing subsystem
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
22 /**
23  * SECTION:gsttracerutils
24  * @short_description: Tracing subsystem
25  *
26  * The tracing subsystem provides hooks in the core library and API for modules
27  * to attach to them.
28  *
29  * The user can activate tracers by setting the environment variable GST_TRACE
30  * to a ';' separated list of tracers.
31  */
32
33 #define GST_USE_UNSTABLE_API
34
35 #include "gst_private.h"
36 #include "gsttracer.h"
37 #include "gsttracerfactory.h"
38 #include "gsttracerutils.h"
39
40 #ifndef GST_DISABLE_GST_DEBUG
41
42 /* tracer quarks */
43
44 /* These strings must match order and number declared in the GstTracerQuarkId
45  * enum in gsttracerutils.h! */
46 static const gchar *_quark_strings[] = {
47   "pad-push-pre", "pad-push-post", "pad-push-list-pre", "pad-push-list-post",
48   "pad-pull-range-pre", "pad-pull-range-post", "pad-push-event-pre",
49   "pad-push-event-post", "pad-query-pre", "pad-query-post",
50   "element-post-message-pre",
51   "element-post-message-post", "element-query-pre", "element-query-post",
52   "element-new", "element-add-pad", "element-remove-pad",
53   "bin-add-pre", "bin-add-post", "bin-remove-pre", "bin-remove-post",
54   "pad-link-pre", "pad-link-post", "pad-unlink-pre", "pad-unlink-post",
55   "element-change-state-pre", "element-change-state-post"
56 };
57
58 GQuark _priv_gst_tracer_quark_table[GST_TRACER_QUARK_MAX];
59
60 /* tracing helpers */
61
62 gboolean _priv_tracer_enabled = FALSE;
63 GHashTable *_priv_tracers = NULL;
64
65 /* Initialize the tracing system */
66 void
67 _priv_gst_tracing_init (void)
68 {
69   const gchar *env = g_getenv ("GST_TRACER_PLUGINS");
70
71   if (env != NULL && *env != '\0') {
72     GstRegistry *registry = gst_registry_get ();
73     GstPluginFeature *feature;
74     GstTracerFactory *factory;
75     gchar **t = g_strsplit_set (env, ";", 0);
76     gint i = 0;
77     gchar *params;
78
79     GST_INFO ("enabling tracers: '%s'", env);
80
81     if (G_N_ELEMENTS (_quark_strings) != GST_TRACER_QUARK_MAX)
82       g_warning ("the quark table is not consistent! %d != %d",
83           (gint) G_N_ELEMENTS (_quark_strings), GST_TRACER_QUARK_MAX);
84
85     for (i = 0; i < GST_TRACER_QUARK_MAX; i++) {
86       _priv_gst_tracer_quark_table[i] =
87           g_quark_from_static_string (_quark_strings[i]);
88     }
89
90     _priv_tracers = g_hash_table_new (NULL, NULL);
91
92     i = 0;
93     while (t[i]) {
94       // check t[i] for params
95       if ((params = strchr (t[i], '('))) {
96         gchar *end = strchr (&params[1], ')');
97         *params = '\0';
98         params++;
99         if (end)
100           *end = '\0';
101       } else {
102         params = NULL;
103       }
104
105       GST_INFO ("checking tracer: '%s'", t[i]);
106
107       if ((feature = gst_registry_lookup_feature (registry, t[i]))) {
108         factory = GST_TRACER_FACTORY (gst_plugin_feature_load (feature));
109         if (factory) {
110           GST_INFO_OBJECT (factory, "creating tracer: type-id=%u",
111               (guint) factory->type);
112
113           /* tracers register them self to the hooks */
114           gst_object_unref (g_object_new (factory->type, "params", params,
115                   NULL));
116         } else {
117           GST_WARNING_OBJECT (feature,
118               "loading plugin containing feature %s failed!", t[i]);
119         }
120       } else {
121         GST_WARNING ("no tracer named '%s'", t[i]);
122       }
123       i++;
124     }
125     g_strfreev (t);
126   }
127 }
128
129 void
130 _priv_gst_tracing_deinit (void)
131 {
132   GList *h_list, *h_node, *t_node;
133   GstTracerHook *hook;
134
135   _priv_tracer_enabled = FALSE;
136   if (!_priv_tracers)
137     return;
138
139   /* shutdown tracers for final reports */
140   h_list = g_hash_table_get_values (_priv_tracers);
141   for (h_node = h_list; h_node; h_node = g_list_next (h_node)) {
142     for (t_node = h_node->data; t_node; t_node = g_list_next (t_node)) {
143       hook = (GstTracerHook *) t_node->data;
144       gst_object_unref (hook->tracer);
145       g_slice_free (GstTracerHook, hook);
146     }
147     g_list_free (h_node->data);
148   }
149   g_list_free (h_list);
150   g_hash_table_destroy (_priv_tracers);
151   _priv_tracers = NULL;
152 }
153
154 /**
155  * gst_tracing_register_hook_id:
156  * @tracer: the tracer
157  * @detail: the detailed hook
158  * @func: (scope async): the callback
159  *
160  * Register @func to be called when the trace hook @detail is getting invoked.
161  */
162 void
163 gst_tracing_register_hook_id (GstTracer * tracer, GQuark detail, GCallback func)
164 {
165   gpointer key = GINT_TO_POINTER (detail);
166   GList *list = g_hash_table_lookup (_priv_tracers, key);
167   GstTracerHook *hook = g_slice_new0 (GstTracerHook);
168   hook->tracer = gst_object_ref (tracer);
169   hook->func = func;
170
171   list = g_list_prepend (list, hook);
172   g_hash_table_replace (_priv_tracers, key, list);
173   GST_DEBUG ("registering tracer for '%s', list.len=%d",
174       (detail ? g_quark_to_string (detail) : "*"), g_list_length (list));
175   _priv_tracer_enabled = TRUE;
176 }
177
178 /**
179  * gst_tracing_register_hook:
180  * @tracer: the tracer
181  * @detail: the detailed hook
182  * @func: (scope async): the callback
183  *
184  * Register @func to be called when the trace hook @detail is getting invoked.
185  */
186 void
187 gst_tracing_register_hook (GstTracer * tracer, const gchar * detail,
188     GCallback func)
189 {
190   gst_tracing_register_hook_id (tracer, g_quark_try_string (detail), func);
191 }
192
193 #endif /* GST_DISABLE_GST_DEBUG */