tracer: split into tracer and tracerutils
[platform/upstream/gstreamer.git] / gst / gsttracer.c
1 /* GStreamer
2  * Copyright (C) 2013 Stefan Sauer <ensonic@users.sf.net>
3  *
4  * gsttracer.c: tracer base class
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 /**
23  * SECTION:gsttracer
24  * @short_description: Tracing base class
25  *
26  * Tracing modules will subclass #GstTracer and register through
27  * gst_tracer_register(). Modules can attach to various hook-types - see
28  * #GstTracerHook. When invoked they receive hook specific contextual data, 
29  * which they must not modify.
30  */
31
32 #define GST_USE_UNSTABLE_API
33
34 #include "gst_private.h"
35 #include "gstenumtypes.h"
36 #include "gsttracer.h"
37 #include "gsttracerfactory.h"
38
39 GST_DEBUG_CATEGORY_EXTERN (tracer_debug);
40 #define GST_CAT_DEFAULT tracer_debug
41
42 /* tracing plugins base class */
43
44 enum
45 {
46   PROP_0,
47   PROP_PARAMS,
48   PROP_MASK,
49   PROP_LAST
50 };
51
52 static GParamSpec *properties[PROP_LAST];
53
54 static void gst_tracer_set_property (GObject * object, guint prop_id,
55     const GValue * value, GParamSpec * pspec);
56 static void gst_tracer_get_property (GObject * object, guint prop_id,
57     GValue * value, GParamSpec * pspec);
58
59 struct _GstTracerPrivate
60 {
61   const gchar *params;
62   GstTracerHook mask;
63 };
64
65 #define gst_tracer_parent_class parent_class
66 G_DEFINE_ABSTRACT_TYPE (GstTracer, gst_tracer, GST_TYPE_OBJECT);
67
68 static void
69 gst_tracer_class_init (GstTracerClass * klass)
70 {
71   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
72
73   gobject_class->set_property = gst_tracer_set_property;
74   gobject_class->get_property = gst_tracer_get_property;
75
76   properties[PROP_PARAMS] =
77       g_param_spec_string ("params", "Params", "Extra configuration parameters",
78       NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
79
80   properties[PROP_MASK] =
81       g_param_spec_flags ("mask", "Mask", "Event mask", GST_TYPE_TRACER_HOOK,
82       GST_TRACER_HOOK_NONE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
83
84   g_object_class_install_properties (gobject_class, PROP_LAST, properties);
85   g_type_class_add_private (klass, sizeof (GstTracerPrivate));
86 }
87
88 static void
89 gst_tracer_init (GstTracer * tracer)
90 {
91   tracer->priv = G_TYPE_INSTANCE_GET_PRIVATE (tracer, GST_TYPE_TRACER,
92       GstTracerPrivate);
93 }
94
95 static void
96 gst_tracer_set_property (GObject * object, guint prop_id,
97     const GValue * value, GParamSpec * pspec)
98 {
99   GstTracer *self = GST_TRACER_CAST (object);
100
101   switch (prop_id) {
102     case PROP_PARAMS:
103       self->priv->params = g_value_get_string (value);
104       break;
105     case PROP_MASK:
106       self->priv->mask = g_value_get_flags (value);
107       break;
108     default:
109       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
110       break;
111   }
112 }
113
114 static void
115 gst_tracer_get_property (GObject * object, guint prop_id,
116     GValue * value, GParamSpec * pspec)
117 {
118   GstTracer *self = GST_TRACER_CAST (object);
119
120   switch (prop_id) {
121     case PROP_PARAMS:
122       g_value_set_string (value, self->priv->params);
123       break;
124     case PROP_MASK:
125       g_value_set_flags (value, self->priv->mask);
126       break;
127     default:
128       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
129       break;
130   }
131 }
132
133 void
134 gst_tracer_invoke (GstTracer * self, GstTracerHookId hid,
135     GstTracerMessageId mid, va_list var_args)
136 {
137   GstTracerClass *klass = GST_TRACER_GET_CLASS (self);
138
139   g_return_if_fail (klass->invoke);
140
141   klass->invoke (self, hid, mid, var_args);
142 }
143
144 /* tracing modules */
145
146 gboolean
147 gst_tracer_register (GstPlugin * plugin, const gchar * name, GType type)
148 {
149   GstPluginFeature *existing_feature;
150   GstRegistry *registry;
151   GstTracerFactory *factory;
152
153   g_return_val_if_fail (name != NULL, FALSE);
154   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_TRACER), FALSE);
155
156   registry = gst_registry_get ();
157   /* check if feature already exists, if it exists there is no need to update it
158    * when the registry is getting updated, outdated plugins and all their
159    * features are removed and readded.
160    */
161   existing_feature = gst_registry_lookup_feature (registry, name);
162   if (existing_feature) {
163     GST_DEBUG_OBJECT (registry, "update existing feature %p (%s)",
164         existing_feature, name);
165     factory = GST_TRACER_FACTORY_CAST (existing_feature);
166     factory->type = type;
167     existing_feature->loaded = TRUE;
168     //g_type_set_qdata (type, __gst_elementclass_factory, factory);
169     gst_object_unref (existing_feature);
170     return TRUE;
171   }
172
173   factory = g_object_newv (GST_TYPE_TRACER_FACTORY, 0, NULL);
174   GST_DEBUG_OBJECT (factory, "new tracer factory for %s", name);
175
176   gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
177   gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory),
178       GST_RANK_NONE);
179
180   factory->type = type;
181   GST_DEBUG_OBJECT (factory, "tracer factory for %u:%s",
182       (guint) type, g_type_name (type));
183
184   if (plugin && plugin->desc.name) {
185     GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = plugin->desc.name; /* interned string */
186     GST_PLUGIN_FEATURE_CAST (factory)->plugin = plugin;
187     g_object_add_weak_pointer ((GObject *) plugin,
188         (gpointer *) & GST_PLUGIN_FEATURE_CAST (factory)->plugin);
189   } else {
190     GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
191     GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
192   }
193   GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
194
195   gst_registry_add_feature (gst_registry_get (),
196       GST_PLUGIN_FEATURE_CAST (factory));
197
198   return TRUE;
199 }
200
201 /* tracing module helpers */
202
203 void
204 gst_tracer_log_trace (GstStructure * s)
205 {
206   gchar *data;
207
208   // TODO(ensonic): use a GVariant?
209   data = gst_structure_to_string (s);
210   GST_TRACE ("%s", data);
211   g_free (data);
212   gst_structure_free (s);
213 }