tracer: add the hook-id to the invoke signature
[platform/upstream/gstreamer.git] / gst / gsttracer.c
1 /* GStreamer
2  * Copyright (C) 2013 Stefan Sauer <ensonic@users.sf.net>
3  *
4  * gsttracer.h: 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 #include "gst_private.h"
23 #include "gstenumtypes.h"
24 #include "gstregistry.h"
25 #include "gsttracer.h"
26 #include "gsttracerfactory.h"
27 #include "gstutils.h"
28
29 #ifndef GST_DISABLE_GST_DEBUG
30
31 GST_DEBUG_CATEGORY_EXTERN (tracer_debug);
32 #define GST_CAT_DEFAULT tracer_debug
33
34 /* tracing plugins base class */
35
36 enum
37 {
38   PROP_0,
39   PROP_PARAMS,
40   PROP_MASK,
41   PROP_LAST
42 };
43
44 static GParamSpec *properties[PROP_LAST];
45
46 static void gst_tracer_set_property (GObject * object, guint prop_id,
47     const GValue * value, GParamSpec * pspec);
48 static void gst_tracer_get_property (GObject * object, guint prop_id,
49     GValue * value, GParamSpec * pspec);
50
51 struct _GstTracerPrivate
52 {
53   const gchar *params;
54   GstTracerHook mask;
55 };
56
57 #define gst_tracer_parent_class parent_class
58 G_DEFINE_ABSTRACT_TYPE (GstTracer, gst_tracer, GST_TYPE_OBJECT);
59
60 static void
61 gst_tracer_class_init (GstTracerClass * klass)
62 {
63   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
64
65   gobject_class->set_property = gst_tracer_set_property;
66   gobject_class->get_property = gst_tracer_get_property;
67
68   properties[PROP_PARAMS] =
69       g_param_spec_string ("params", "Params", "Extra configuration parameters",
70       NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
71
72   properties[PROP_MASK] =
73       g_param_spec_flags ("mask", "Mask", "Event mask", GST_TYPE_TRACER_HOOK,
74       GST_TRACER_HOOK_NONE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
75
76   g_object_class_install_properties (gobject_class, PROP_LAST, properties);
77   g_type_class_add_private (klass, sizeof (GstTracerPrivate));
78 }
79
80 static void
81 gst_tracer_init (GstTracer * tracer)
82 {
83   tracer->priv = G_TYPE_INSTANCE_GET_PRIVATE (tracer, GST_TYPE_TRACER,
84       GstTracerPrivate);
85 }
86
87 static void
88 gst_tracer_set_property (GObject * object, guint prop_id,
89     const GValue * value, GParamSpec * pspec)
90 {
91   GstTracer *self = GST_TRACER_CAST (object);
92
93   switch (prop_id) {
94     case PROP_PARAMS:
95       self->priv->params = g_value_get_string (value);
96       break;
97     case PROP_MASK:
98       self->priv->mask = g_value_get_flags (value);
99       break;
100     default:
101       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
102       break;
103   }
104 }
105
106 static void
107 gst_tracer_get_property (GObject * object, guint prop_id,
108     GValue * value, GParamSpec * pspec)
109 {
110   GstTracer *self = GST_TRACER_CAST (object);
111
112   switch (prop_id) {
113     case PROP_PARAMS:
114       g_value_set_string (value, self->priv->params);
115       break;
116     case PROP_MASK:
117       g_value_set_flags (value, self->priv->mask);
118       break;
119     default:
120       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
121       break;
122   }
123 }
124
125 static void
126 gst_tracer_invoke (GstTracer * self, GstTracerHookId id, GstStructure * s)
127 {
128   GstTracerClass *klass = GST_TRACER_GET_CLASS (self);
129
130   g_return_if_fail (klass->invoke);
131
132   klass->invoke (id, s);
133 }
134
135 /* tracing modules */
136
137 gboolean
138 gst_tracer_register (GstPlugin * plugin, const gchar * name, GType type)
139 {
140   GstPluginFeature *existing_feature;
141   GstRegistry *registry;
142   GstTracerFactory *factory;
143
144   g_return_val_if_fail (name != NULL, FALSE);
145   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_TRACER), FALSE);
146
147   registry = gst_registry_get ();
148   /* check if feature already exists, if it exists there is no need to update it
149    * when the registry is getting updated, outdated plugins and all their
150    * features are removed and readded.
151    */
152   existing_feature = gst_registry_lookup_feature (registry, name);
153   if (existing_feature) {
154     GST_DEBUG_OBJECT (registry, "update existing feature %p (%s)",
155         existing_feature, name);
156     factory = GST_TRACER_FACTORY_CAST (existing_feature);
157     factory->type = type;
158     existing_feature->loaded = TRUE;
159     //g_type_set_qdata (type, __gst_elementclass_factory, factory);
160     gst_object_unref (existing_feature);
161     return TRUE;
162   }
163
164   factory = g_object_newv (GST_TYPE_TRACER_FACTORY, 0, NULL);
165   GST_DEBUG_OBJECT (factory, "new tracer factory for %s", name);
166
167   gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
168   gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory),
169       GST_RANK_NONE);
170
171   factory->type = type;
172   GST_DEBUG_OBJECT (factory, "tracer factory for %u:%s",
173       (guint) type, g_type_name (type));
174
175   if (plugin && plugin->desc.name) {
176     GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = plugin->desc.name; /* interned string */
177     GST_PLUGIN_FEATURE_CAST (factory)->plugin = plugin;
178     g_object_add_weak_pointer ((GObject *) plugin,
179         (gpointer *) & GST_PLUGIN_FEATURE_CAST (factory)->plugin);
180   } else {
181     GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
182     GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
183   }
184   GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
185
186   gst_registry_add_feature (gst_registry_get (),
187       GST_PLUGIN_FEATURE_CAST (factory));
188
189   return TRUE;
190 }
191
192 /* tracing helpers */
193
194 static gboolean tracer_enabled = FALSE;
195
196 static GList *tracers[GST_TRACER_HOOK_ID_LAST] = { NULL, };
197
198 /* Initialize the debugging system */
199 void
200 _priv_gst_tracer_init (void)
201 {
202   const gchar *env = g_getenv ("GST_TRACE");
203
204   if (env != NULL && *env != '\0') {
205     GstRegistry *registry = gst_registry_get ();
206     GstPluginFeature *feature;
207     GstTracerFactory *factory;
208     GstTracerHook mask;
209     GstTracer *tracer;
210     gchar **t = g_strsplit_set (env, ",", 0);
211     gint i = 0, j;
212     gchar *params;
213
214     GST_INFO ("enabling tracers: '%s'", env);
215
216     while (t[i]) {
217       // TODO(ensonic): check t[i] for params
218       params = NULL;
219
220       GST_INFO ("checking tracer: '%s'", t[i]);
221
222       if ((feature = gst_registry_lookup_feature (registry, t[i]))) {
223         factory = GST_TRACER_FACTORY (gst_plugin_feature_load (feature));
224         if (factory) {
225           GST_INFO_OBJECT (factory, "creating tracer: type-id=%u",
226               (guint) factory->type);
227
228           tracer = g_object_new (factory->type, "params", params, NULL);
229           g_object_get (tracer, "mask", &mask, NULL);
230
231           if (mask) {
232             /* add to lists according to mask */
233             j = 0;
234             while (mask && (j < GST_TRACER_HOOK_ID_LAST)) {
235               if (mask & 1) {
236                 tracers[j] = g_list_prepend (tracers[j],
237                     gst_object_ref (tracer));
238                 GST_WARNING_OBJECT (tracer, "added tracer to hook %d", j);
239               }
240               mask >>= 1;
241               j++;
242             }
243
244             tracer_enabled = TRUE;
245           } else {
246             GST_WARNING_OBJECT (tracer,
247                 "tracer with zero mask won't have any effect");
248           }
249           gst_object_unref (tracer);
250         } else {
251           GST_WARNING_OBJECT (feature,
252               "loading plugin containing feature %s failed!", t[i]);
253         }
254       } else {
255         GST_WARNING ("no tracer named '%s'", t[i]);
256       }
257       i++;
258     }
259     g_strfreev (t);
260   }
261 }
262
263 void
264 _priv_gst_tracer_deinit (void)
265 {
266   gint i;
267   GList *node;
268
269   /* shutdown tracers for final reports */
270   for (i = 0; i < GST_TRACER_HOOK_ID_LAST; i++) {
271     for (node = tracers[i]; node; node = g_list_next (node)) {
272       gst_object_unref (node->data);
273     }
274     g_list_free (tracers[i]);
275     tracers[i] = NULL;
276   }
277 }
278
279 gboolean
280 gst_tracer_is_enabled (GstTracerHookId id)
281 {
282   return tracer_enabled && (tracers[id] != NULL);
283 }
284
285 static void
286 dispatch (GstTracerHookId id, GstStructure * s)
287 {
288   GList *node;
289   for (node = tracers[id]; node; node = g_list_next (node)) {
290     gst_tracer_invoke (node->data, id, s);
291   }
292 }
293
294 /* tracing hooks */
295 void
296 gst_tracer_push_buffer_pre (GstPad * pad, GstBuffer * buffer)
297 {
298   // TODO(ensonic): gst_structure_new_id
299   dispatch (GST_TRACER_HOOK_ID_BUFFERS, gst_structure_new ("push_buffer::pre",
300           ".ts", G_TYPE_UINT64, gst_util_get_timestamp (),
301           "pad", GST_TYPE_PAD, pad, "buffer", GST_TYPE_BUFFER, buffer, NULL));
302 }
303
304 void
305 gst_tracer_push_buffer_post (GstPad * pad, GstFlowReturn res)
306 {
307   // TODO(ensonic): gst_structure_new_id
308   dispatch (GST_TRACER_HOOK_ID_BUFFERS, gst_structure_new ("push_buffer::post",
309           ".ts", G_TYPE_UINT64, gst_util_get_timestamp (),
310           "pad", GST_TYPE_PAD, pad, "return", G_TYPE_INT, res, NULL));
311 }
312
313 #endif /* GST_DISABLE_GST_DEBUG */