2 * Copyright (C) 2013 Stefan Sauer <ensonic@users.sf.net>
4 * gsttracer.h: tracing subsystem
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.
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.
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.
22 #include "gst_private.h"
23 #include "gstenumtypes.h"
25 #include "gstregistry.h"
26 #include "gsttracer.h"
27 #include "gsttracerfactory.h"
30 #ifndef GST_DISABLE_GST_DEBUG
32 GST_DEBUG_CATEGORY_EXTERN (tracer_debug);
33 #define GST_CAT_DEFAULT tracer_debug
35 /* tracing plugins base class */
45 static GParamSpec *properties[PROP_LAST];
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);
52 struct _GstTracerPrivate
58 #define gst_tracer_parent_class parent_class
59 G_DEFINE_ABSTRACT_TYPE (GstTracer, gst_tracer, GST_TYPE_OBJECT);
62 gst_tracer_class_init (GstTracerClass * klass)
64 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
66 gobject_class->set_property = gst_tracer_set_property;
67 gobject_class->get_property = gst_tracer_get_property;
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);
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);
77 g_object_class_install_properties (gobject_class, PROP_LAST, properties);
78 g_type_class_add_private (klass, sizeof (GstTracerPrivate));
82 gst_tracer_init (GstTracer * tracer)
84 tracer->priv = G_TYPE_INSTANCE_GET_PRIVATE (tracer, GST_TYPE_TRACER,
89 gst_tracer_set_property (GObject * object, guint prop_id,
90 const GValue * value, GParamSpec * pspec)
92 GstTracer *self = GST_TRACER_CAST (object);
96 self->priv->params = g_value_get_string (value);
99 self->priv->mask = g_value_get_flags (value);
102 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
108 gst_tracer_get_property (GObject * object, guint prop_id,
109 GValue * value, GParamSpec * pspec)
111 GstTracer *self = GST_TRACER_CAST (object);
115 g_value_set_string (value, self->priv->params);
118 g_value_set_flags (value, self->priv->mask);
121 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
127 gst_tracer_invoke (GstTracer * self, GstTracerHookId id, guint64 ts,
130 GstTracerClass *klass = GST_TRACER_GET_CLASS (self);
132 g_return_if_fail (klass->invoke);
134 klass->invoke (self, id, ts, s);
137 /* tracing modules */
140 gst_tracer_register (GstPlugin * plugin, const gchar * name, GType type)
142 GstPluginFeature *existing_feature;
143 GstRegistry *registry;
144 GstTracerFactory *factory;
146 g_return_val_if_fail (name != NULL, FALSE);
147 g_return_val_if_fail (g_type_is_a (type, GST_TYPE_TRACER), FALSE);
149 registry = gst_registry_get ();
150 /* check if feature already exists, if it exists there is no need to update it
151 * when the registry is getting updated, outdated plugins and all their
152 * features are removed and readded.
154 existing_feature = gst_registry_lookup_feature (registry, name);
155 if (existing_feature) {
156 GST_DEBUG_OBJECT (registry, "update existing feature %p (%s)",
157 existing_feature, name);
158 factory = GST_TRACER_FACTORY_CAST (existing_feature);
159 factory->type = type;
160 existing_feature->loaded = TRUE;
161 //g_type_set_qdata (type, __gst_elementclass_factory, factory);
162 gst_object_unref (existing_feature);
166 factory = g_object_newv (GST_TYPE_TRACER_FACTORY, 0, NULL);
167 GST_DEBUG_OBJECT (factory, "new tracer factory for %s", name);
169 gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
170 gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory),
173 factory->type = type;
174 GST_DEBUG_OBJECT (factory, "tracer factory for %u:%s",
175 (guint) type, g_type_name (type));
177 if (plugin && plugin->desc.name) {
178 GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = plugin->desc.name; /* interned string */
179 GST_PLUGIN_FEATURE_CAST (factory)->plugin = plugin;
180 g_object_add_weak_pointer ((GObject *) plugin,
181 (gpointer *) & GST_PLUGIN_FEATURE_CAST (factory)->plugin);
183 GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
184 GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
186 GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
188 gst_registry_add_feature (gst_registry_get (),
189 GST_PLUGIN_FEATURE_CAST (factory));
194 /* tracing helpers */
196 static gboolean tracer_enabled = FALSE;
198 static GList *tracers[GST_TRACER_HOOK_ID_LAST] = { NULL, };
200 /* Initialize the debugging system */
202 _priv_gst_tracer_init (void)
204 const gchar *env = g_getenv ("GST_TRACE");
206 if (env != NULL && *env != '\0') {
207 GstRegistry *registry = gst_registry_get ();
208 GstPluginFeature *feature;
209 GstTracerFactory *factory;
212 gchar **t = g_strsplit_set (env, ",", 0);
216 GST_INFO ("enabling tracers: '%s'", env);
219 // TODO(ensonic): check t[i] for params
222 GST_INFO ("checking tracer: '%s'", t[i]);
224 if ((feature = gst_registry_lookup_feature (registry, t[i]))) {
225 factory = GST_TRACER_FACTORY (gst_plugin_feature_load (feature));
227 GST_INFO_OBJECT (factory, "creating tracer: type-id=%u",
228 (guint) factory->type);
230 tracer = g_object_new (factory->type, "params", params, NULL);
231 g_object_get (tracer, "mask", &mask, NULL);
234 /* add to lists according to mask */
236 while (mask && (j < GST_TRACER_HOOK_ID_LAST)) {
238 tracers[j] = g_list_prepend (tracers[j],
239 gst_object_ref (tracer));
240 GST_WARNING_OBJECT (tracer, "added tracer to hook %d", j);
246 tracer_enabled = TRUE;
248 GST_WARNING_OBJECT (tracer,
249 "tracer with zero mask won't have any effect");
251 gst_object_unref (tracer);
253 GST_WARNING_OBJECT (feature,
254 "loading plugin containing feature %s failed!", t[i]);
257 GST_WARNING ("no tracer named '%s'", t[i]);
266 _priv_gst_tracer_deinit (void)
271 /* shutdown tracers for final reports */
272 for (i = 0; i < GST_TRACER_HOOK_ID_LAST; i++) {
273 for (node = tracers[i]; node; node = g_list_next (node)) {
274 gst_object_unref (node->data);
276 g_list_free (tracers[i]);
282 gst_tracer_is_enabled (GstTracerHookId id)
284 return tracer_enabled && (tracers[id] != NULL);
288 dispatch (GstTracerHookId id, guint64 ts, GstStructure * s)
291 for (node = tracers[id]; node; node = g_list_next (node)) {
292 gst_tracer_invoke (node->data, id, ts, s);
298 gst_tracer_push_buffer_pre (guint64 ts, GstPad * pad, GstBuffer * buffer)
300 dispatch (GST_TRACER_HOOK_ID_BUFFERS, ts,
301 gst_structure_new_id (GST_QUARK (PUSH_BUFFER_PRE),
302 GST_QUARK (PAD), GST_TYPE_PAD, pad,
303 GST_QUARK (BUFFER), GST_TYPE_BUFFER, buffer, NULL));
307 gst_tracer_push_buffer_post (guint64 ts, GstPad * pad, GstFlowReturn res)
309 dispatch (GST_TRACER_HOOK_ID_BUFFERS, ts,
310 gst_structure_new_id (GST_QUARK (PUSH_BUFFER_POST),
311 GST_QUARK (PAD), GST_TYPE_PAD, pad,
312 GST_QUARK (RETURN), G_TYPE_INT, res, NULL));
316 gst_tracer_push_buffer_list_pre (guint64 ts, GstPad * pad, GstBufferList * list)
318 dispatch (GST_TRACER_HOOK_ID_BUFFERS, ts,
319 gst_structure_new_id (GST_QUARK (PUSH_BUFFER_LIST_PRE),
320 GST_QUARK (PAD), GST_TYPE_PAD, pad,
321 GST_QUARK (LIST), GST_TYPE_BUFFER_LIST, list, NULL));
325 gst_tracer_push_buffer_list_post (guint64 ts, GstPad * pad, GstFlowReturn res)
327 dispatch (GST_TRACER_HOOK_ID_BUFFERS, ts,
328 gst_structure_new_id (GST_QUARK (PUSH_BUFFER_LIST_POST),
329 GST_QUARK (PAD), GST_TYPE_PAD, pad,
330 GST_QUARK (RETURN), G_TYPE_INT, res, NULL));
333 #endif /* GST_DISABLE_GST_DEBUG */