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 * @title: GstTracer
25 * @short_description: Tracing base class
26 *
27 * Tracing modules will subclass #GstTracer and register through
28 * gst_tracer_register(). Modules can attach to various hook-types - see
29 * gst_tracing_register_hook(). When invoked they receive hook specific
30 * contextual data, which they must not modify.
31 *
32 * Since: 1.8
33 */
34
35 #include "gst_private.h"
36 #include "gstenumtypes.h"
37 #include "gsttracer.h"
38 #include "gsttracerfactory.h"
39 #include "gsttracerutils.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_WITH_PRIVATE (GstTracer, gst_tracer, GST_TYPE_OBJECT);
67
68 static void
gst_tracer_dispose(GObject * object)69 gst_tracer_dispose (GObject * object)
70 {
71 GstTracer *tracer = GST_TRACER (object);
72 g_free (tracer->priv->params);
73 }
74
75 static void
gst_tracer_class_init(GstTracerClass * klass)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 }
90
91 static void
gst_tracer_init(GstTracer * tracer)92 gst_tracer_init (GstTracer * tracer)
93 {
94 tracer->priv = gst_tracer_get_instance_private (tracer);
95 }
96
97 static void
gst_tracer_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)98 gst_tracer_set_property (GObject * object, guint prop_id,
99 const GValue * value, GParamSpec * pspec)
100 {
101 GstTracer *self = GST_TRACER_CAST (object);
102
103 switch (prop_id) {
104 case PROP_PARAMS:
105 self->priv->params = g_value_dup_string (value);
106 break;
107 default:
108 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
109 break;
110 }
111 }
112
113 static void
gst_tracer_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)114 gst_tracer_get_property (GObject * object, guint prop_id,
115 GValue * value, GParamSpec * pspec)
116 {
117 GstTracer *self = GST_TRACER_CAST (object);
118
119 switch (prop_id) {
120 case PROP_PARAMS:
121 g_value_set_string (value, self->priv->params);
122 break;
123 default:
124 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
125 break;
126 }
127 }
128
129 /* tracing modules */
130
131 /**
132 * gst_tracer_register:
133 * @plugin: (allow-none): A #GstPlugin, or %NULL for a static typefind function
134 * @name: The name for registering
135 * @type: GType of tracer to register
136 *
137 * Create a new tracer-factory capable of instantiating objects of the
138 * @type and add the factory to @plugin.
139 *
140 * Returns: %TRUE, if the registering succeeded, %FALSE on error
141 */
142 gboolean
gst_tracer_register(GstPlugin * plugin,const gchar * name,GType type)143 gst_tracer_register (GstPlugin * plugin, const gchar * name, GType type)
144 {
145 GstPluginFeature *existing_feature;
146 GstRegistry *registry;
147 GstTracerFactory *factory;
148
149 g_return_val_if_fail (name != NULL, FALSE);
150 g_return_val_if_fail (g_type_is_a (type, GST_TYPE_TRACER), FALSE);
151
152 registry = gst_registry_get ();
153 /* check if feature already exists, if it exists there is no need to update it
154 * when the registry is getting updated, outdated plugins and all their
155 * features are removed and readded.
156 */
157 existing_feature = gst_registry_lookup_feature (registry, name);
158 if (existing_feature) {
159 GST_DEBUG_OBJECT (registry, "update existing feature %p (%s)",
160 existing_feature, name);
161 factory = GST_TRACER_FACTORY_CAST (existing_feature);
162 factory->type = type;
163 existing_feature->loaded = TRUE;
164 gst_object_unref (existing_feature);
165 return TRUE;
166 }
167
168 factory = g_object_new (GST_TYPE_TRACER_FACTORY, NULL);
169 GST_DEBUG_OBJECT (factory, "new tracer factory for %s", name);
170
171 gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
172 gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory),
173 GST_RANK_NONE);
174
175 factory->type = type;
176 GST_DEBUG_OBJECT (factory, "tracer factory for %u:%s",
177 (guint) type, g_type_name (type));
178
179 if (plugin && plugin->desc.name) {
180 GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = plugin->desc.name; /* interned string */
181 GST_PLUGIN_FEATURE_CAST (factory)->plugin = plugin;
182 g_object_add_weak_pointer ((GObject *) plugin,
183 (gpointer *) & GST_PLUGIN_FEATURE_CAST (factory)->plugin);
184 } else {
185 GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
186 GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
187 }
188 GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
189
190 gst_registry_add_feature (gst_registry_get (),
191 GST_PLUGIN_FEATURE_CAST (factory));
192
193 return TRUE;
194 }
195