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