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