docs: fix some warnings and add some since markers
[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  * Since: 1.8
32  */
33
34 #define GST_USE_UNSTABLE_API
35
36 #include "gst_private.h"
37 #include "gstenumtypes.h"
38 #include "gsttracer.h"
39 #include "gsttracerfactory.h"
40
41 GST_DEBUG_CATEGORY_EXTERN (tracer_debug);
42 #define GST_CAT_DEFAULT tracer_debug
43
44 /* tracing plugins base class */
45
46 enum
47 {
48   PROP_0,
49   PROP_PARAMS,
50   PROP_LAST
51 };
52
53 static GParamSpec *properties[PROP_LAST];
54
55 static void gst_tracer_set_property (GObject * object, guint prop_id,
56     const GValue * value, GParamSpec * pspec);
57 static void gst_tracer_get_property (GObject * object, guint prop_id,
58     GValue * value, GParamSpec * pspec);
59
60 struct _GstTracerPrivate
61 {
62   gchar *params;
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_dispose (GObject * object)
70 {
71   GstTracer *tracer = GST_TRACER (object);
72   g_free (tracer->priv->params);
73 }
74
75 static void
76 gst_tracer_class_init (GstTracerClass * klass)
77 {
78   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
79
80   gobject_class->set_property = gst_tracer_set_property;
81   gobject_class->get_property = gst_tracer_get_property;
82   gobject_class->dispose = gst_tracer_dispose;
83
84   properties[PROP_PARAMS] =
85       g_param_spec_string ("params", "Params", "Extra configuration parameters",
86       NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
87
88   g_object_class_install_properties (gobject_class, PROP_LAST, properties);
89   g_type_class_add_private (klass, sizeof (GstTracerPrivate));
90 }
91
92 static void
93 gst_tracer_init (GstTracer * tracer)
94 {
95   tracer->priv = G_TYPE_INSTANCE_GET_PRIVATE (tracer, GST_TYPE_TRACER,
96       GstTracerPrivate);
97 }
98
99 static void
100 gst_tracer_set_property (GObject * object, guint prop_id,
101     const GValue * value, GParamSpec * pspec)
102 {
103   GstTracer *self = GST_TRACER_CAST (object);
104
105   switch (prop_id) {
106     case PROP_PARAMS:
107       self->priv->params = g_value_dup_string (value);
108       break;
109     default:
110       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
111       break;
112   }
113 }
114
115 static void
116 gst_tracer_get_property (GObject * object, guint prop_id,
117     GValue * value, GParamSpec * pspec)
118 {
119   GstTracer *self = GST_TRACER_CAST (object);
120
121   switch (prop_id) {
122     case PROP_PARAMS:
123       g_value_set_string (value, self->priv->params);
124       break;
125     default:
126       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
127       break;
128   }
129 }
130
131 /* tracing modules */
132
133 gboolean
134 gst_tracer_register (GstPlugin * plugin, const gchar * name, GType type)
135 {
136   GstPluginFeature *existing_feature;
137   GstRegistry *registry;
138   GstTracerFactory *factory;
139
140   g_return_val_if_fail (name != NULL, FALSE);
141   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_TRACER), FALSE);
142
143   registry = gst_registry_get ();
144   /* check if feature already exists, if it exists there is no need to update it
145    * when the registry is getting updated, outdated plugins and all their
146    * features are removed and readded.
147    */
148   existing_feature = gst_registry_lookup_feature (registry, name);
149   if (existing_feature) {
150     GST_DEBUG_OBJECT (registry, "update existing feature %p (%s)",
151         existing_feature, name);
152     factory = GST_TRACER_FACTORY_CAST (existing_feature);
153     factory->type = type;
154     existing_feature->loaded = TRUE;
155     gst_object_unref (existing_feature);
156     return TRUE;
157   }
158
159   factory = g_object_newv (GST_TYPE_TRACER_FACTORY, 0, NULL);
160   GST_DEBUG_OBJECT (factory, "new tracer factory for %s", name);
161
162   gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
163   gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory),
164       GST_RANK_NONE);
165
166   factory->type = type;
167   GST_DEBUG_OBJECT (factory, "tracer factory for %u:%s",
168       (guint) type, g_type_name (type));
169
170   if (plugin && plugin->desc.name) {
171     GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = plugin->desc.name; /* interned string */
172     GST_PLUGIN_FEATURE_CAST (factory)->plugin = plugin;
173     g_object_add_weak_pointer ((GObject *) plugin,
174         (gpointer *) & GST_PLUGIN_FEATURE_CAST (factory)->plugin);
175   } else {
176     GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
177     GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
178   }
179   GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
180
181   gst_registry_add_feature (gst_registry_get (),
182       GST_PLUGIN_FEATURE_CAST (factory));
183
184   return TRUE;
185 }
186
187 /* tracing module helpers */
188
189 void
190 gst_tracer_log_trace (GstStructure * s)
191 {
192   GST_TRACE ("%" GST_PTR_FORMAT, s);
193   /* expands to:
194      gst_debug_log_valist (
195      GST_CAT_DEFAULT, GST_LEVEL_TRACE,
196      file, func, line, object
197      "%" GST_PTR_FORMAT, s);
198      // does it make sense to use the {file, line, func} from the tracer hook?
199      // a)
200      // - we'd need to pass them in the macros to gst_tracer_dispatch()
201      // - and each tracer needs to grab them from the va_list and pass them here
202      // b)
203      // - we create a content in dispatch, pass that to the tracer
204      // - and the tracer will pass that here
205      // ideally we also use *our* ts instead of the one that
206      // gst_debug_log_default() will pick
207    */
208   gst_structure_free (s);
209 }