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"
24 #include "gstregistry.h"
25 #include "gsttracer.h"
26 #include "gsttracerfactory.h"
29 #ifndef GST_DISABLE_GST_DEBUG
31 GST_DEBUG_CATEGORY_EXTERN (tracer_debug);
32 #define GST_CAT_DEFAULT tracer_debug
34 /* tracing plugins base class */
44 static GParamSpec *properties[PROP_LAST];
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);
51 struct _GstTracerPrivate
57 #define gst_tracer_parent_class parent_class
58 G_DEFINE_ABSTRACT_TYPE (GstTracer, gst_tracer, GST_TYPE_OBJECT);
61 gst_tracer_class_init (GstTracerClass * klass)
63 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
65 gobject_class->set_property = gst_tracer_set_property;
66 gobject_class->get_property = gst_tracer_get_property;
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);
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);
76 g_object_class_install_properties (gobject_class, PROP_LAST, properties);
77 g_type_class_add_private (klass, sizeof (GstTracerPrivate));
81 gst_tracer_init (GstTracer * tracer)
83 tracer->priv = G_TYPE_INSTANCE_GET_PRIVATE (tracer, GST_TYPE_TRACER,
88 gst_tracer_set_property (GObject * object, guint prop_id,
89 const GValue * value, GParamSpec * pspec)
91 GstTracer *self = GST_TRACER_CAST (object);
95 self->priv->params = g_value_get_string (value);
98 self->priv->mask = g_value_get_flags (value);
101 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
107 gst_tracer_get_property (GObject * object, guint prop_id,
108 GValue * value, GParamSpec * pspec)
110 GstTracer *self = GST_TRACER_CAST (object);
114 g_value_set_string (value, self->priv->params);
117 g_value_set_flags (value, self->priv->mask);
120 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
126 gst_tracer_invoke (GstTracer * self, GstTracerHookId hid,
127 GstTracerMessageId mid, va_list var_args)
129 GstTracerClass *klass = GST_TRACER_GET_CLASS (self);
131 g_return_if_fail (klass->invoke);
133 klass->invoke (self, hid, mid, var_args);
136 /* tracing modules */
139 gst_tracer_register (GstPlugin * plugin, const gchar * name, GType type)
141 GstPluginFeature *existing_feature;
142 GstRegistry *registry;
143 GstTracerFactory *factory;
145 g_return_val_if_fail (name != NULL, FALSE);
146 g_return_val_if_fail (g_type_is_a (type, GST_TYPE_TRACER), FALSE);
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.
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);
165 factory = g_object_newv (GST_TYPE_TRACER_FACTORY, 0, NULL);
166 GST_DEBUG_OBJECT (factory, "new tracer factory for %s", name);
168 gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
169 gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory),
172 factory->type = type;
173 GST_DEBUG_OBJECT (factory, "tracer factory for %u:%s",
174 (guint) type, g_type_name (type));
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);
182 GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
183 GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
185 GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
187 gst_registry_add_feature (gst_registry_get (),
188 GST_PLUGIN_FEATURE_CAST (factory));
193 /* tracing helpers */
195 gboolean _priv_tracer_enabled = FALSE;
196 /* TODO(ensonic): use GPtrArray ? */
197 GList *_priv_tracers[GST_TRACER_HOOK_ID_LAST] = { NULL, };
199 /* Initialize the debugging system */
201 _priv_gst_tracer_init (void)
203 const gchar *env = g_getenv ("GST_TRACE");
205 if (env != NULL && *env != '\0') {
206 GstRegistry *registry = gst_registry_get ();
207 GstPluginFeature *feature;
208 GstTracerFactory *factory;
211 gchar **t = g_strsplit_set (env, ";", 0);
215 GST_INFO ("enabling tracers: '%s'", env);
218 // check t[i] for params
219 if ((params = strchr (t[i], '('))) {
220 gchar *end = strchr (¶ms[1], ')');
229 GST_INFO ("checking tracer: '%s'", t[i]);
231 if ((feature = gst_registry_lookup_feature (registry, t[i]))) {
232 factory = GST_TRACER_FACTORY (gst_plugin_feature_load (feature));
234 GST_INFO_OBJECT (factory, "creating tracer: type-id=%u",
235 (guint) factory->type);
237 tracer = g_object_new (factory->type, "params", params, NULL);
238 g_object_get (tracer, "mask", &mask, NULL);
241 /* add to lists according to mask */
243 while (mask && (j < GST_TRACER_HOOK_ID_LAST)) {
245 _priv_tracers[j] = g_list_prepend (_priv_tracers[j],
246 gst_object_ref (tracer));
247 GST_WARNING_OBJECT (tracer, "added tracer to hook %d", j);
253 _priv_tracer_enabled = TRUE;
255 GST_WARNING_OBJECT (tracer,
256 "tracer with zero mask won't have any effect");
258 gst_object_unref (tracer);
260 GST_WARNING_OBJECT (feature,
261 "loading plugin containing feature %s failed!", t[i]);
264 GST_WARNING ("no tracer named '%s'", t[i]);
273 _priv_gst_tracer_deinit (void)
278 /* shutdown tracers for final reports */
279 for (i = 0; i < GST_TRACER_HOOK_ID_LAST; i++) {
280 for (node = _priv_tracers[i]; node; node = g_list_next (node)) {
281 gst_object_unref (node->data);
283 g_list_free (_priv_tracers[i]);
284 _priv_tracers[i] = NULL;
286 _priv_tracer_enabled = FALSE;
290 gst_tracer_dispatch (GstTracerHookId hid, GstTracerMessageId mid, ...)
295 for (node = _priv_tracers[hid]; node; node = g_list_next (node)) {
296 va_start (var_args, mid);
297 gst_tracer_invoke (node->data, hid, mid, var_args);
302 /* tracing module helpers */
305 gst_tracer_log_trace (GstStructure * s)
309 // TODO(ensonic): use a GVariant?
310 data = gst_structure_to_string (s);
311 GST_TRACE ("%s", data);
313 gst_structure_free (s);
316 #endif /* GST_DISABLE_GST_DEBUG */