Initialize Tizen 2.3
[framework/multimedia/gstreamer0.10.git] / mobile / gst / gstpluginfeature.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstpluginfeature.c: Abstract base class for all plugin features
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /**
24  * SECTION:gstpluginfeature
25  * @short_description: Base class for contents of a GstPlugin
26  * @see_also: #GstPlugin
27  *
28  * This is a base class for anything that can be added to a #GstPlugin.
29  */
30
31 #include "gst_private.h"
32
33 #include "gstpluginfeature.h"
34 #include "gstplugin.h"
35 #include "gstregistry.h"
36 #include "gstinfo.h"
37
38 #include <stdio.h>
39 #include <string.h>
40
41 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
42
43 static void gst_plugin_feature_finalize (GObject * object);
44
45 /* static guint gst_plugin_feature_signals[LAST_SIGNAL] = { 0 }; */
46
47 G_DEFINE_ABSTRACT_TYPE (GstPluginFeature, gst_plugin_feature, GST_TYPE_OBJECT);
48
49 static void
50 gst_plugin_feature_class_init (GstPluginFeatureClass * klass)
51 {
52   G_OBJECT_CLASS (klass)->finalize = gst_plugin_feature_finalize;
53 }
54
55 static void
56 gst_plugin_feature_init (GstPluginFeature * feature)
57 {
58   /* do nothing, needed because of G_DEFINE_TYPE */
59 }
60
61 static void
62 gst_plugin_feature_finalize (GObject * object)
63 {
64   GstPluginFeature *feature = GST_PLUGIN_FEATURE_CAST (object);
65
66   GST_DEBUG ("finalizing feature %p: '%s'", feature,
67       GST_PLUGIN_FEATURE_NAME (feature));
68
69   if (feature->plugin != NULL) {
70     g_object_remove_weak_pointer ((GObject *) feature->plugin,
71         (gpointer *) & feature->plugin);
72   }
73
74   G_OBJECT_CLASS (gst_plugin_feature_parent_class)->finalize (object);
75 }
76
77 /**
78  * gst_plugin_feature_load:
79  * @feature: (transfer none): the plugin feature to check
80  *
81  * Loads the plugin containing @feature if it's not already loaded. @feature is
82  * unaffected; use the return value instead.
83  *
84  * Normally this function is used like this:
85  * |[
86  * GstPluginFeature *loaded_feature;
87  *
88  * loaded_feature = gst_plugin_feature_load (feature);
89  * // presumably, we're no longer interested in the potentially-unloaded feature
90  * gst_object_unref (feature);
91  * feature = loaded_feature;
92  * ]|
93  *
94  * Returns: (transfer full): a reference to the loaded feature, or NULL on error
95  */
96 GstPluginFeature *
97 gst_plugin_feature_load (GstPluginFeature * feature)
98 {
99   GstPlugin *plugin;
100   GstPluginFeature *real_feature;
101
102   g_return_val_if_fail (feature != NULL, FALSE);
103   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
104
105   GST_DEBUG ("loading plugin for feature %p; '%s'", feature,
106       GST_PLUGIN_FEATURE_NAME (feature));
107   if (feature->loaded)
108     return gst_object_ref (feature);
109
110   GST_DEBUG ("loading plugin %s", feature->plugin_name);
111   plugin = gst_plugin_load_by_name (feature->plugin_name);
112   if (!plugin)
113     goto load_failed;
114
115   GST_DEBUG ("loaded plugin %s", feature->plugin_name);
116   gst_object_unref (plugin);
117
118   real_feature =
119       gst_registry_lookup_feature (gst_registry_get_default (), feature->name);
120
121   if (real_feature == NULL)
122     goto disappeared;
123   else if (!real_feature->loaded)
124     goto not_found;
125
126   return real_feature;
127
128   /* ERRORS */
129 load_failed:
130   {
131     GST_WARNING ("Failed to load plugin containing feature '%s'.",
132         GST_PLUGIN_FEATURE_NAME (feature));
133     return NULL;
134   }
135 disappeared:
136   {
137     GST_INFO
138         ("Loaded plugin containing feature '%s', but feature disappeared.",
139         feature->name);
140     return NULL;
141   }
142 not_found:
143   {
144     GST_INFO ("Tried to load plugin containing feature '%s', but feature was "
145         "not found.", real_feature->name);
146     return NULL;
147   }
148 }
149
150 /**
151  * gst_plugin_feature_type_name_filter:
152  * @feature: the #GstPluginFeature
153  * @data: (in): the type and name to check against
154  *
155  * Compares type and name of plugin feature. Can be used with gst_filter_run().
156  *
157  * Returns: TRUE if equal.
158  */
159 #ifndef GST_REMOVE_DEPRECATED
160 #ifdef GST_DISABLE_DEPRECATED
161 typedef struct
162 {
163   const gchar *name;
164   GType type;
165 } GstTypeNameData;
166 gboolean gst_plugin_feature_type_name_filter (GstPluginFeature * feature,
167     GstTypeNameData * data);
168 #endif
169 gboolean
170 gst_plugin_feature_type_name_filter (GstPluginFeature * feature,
171     GstTypeNameData * data)
172 {
173   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
174
175   return ((data->type == 0 || data->type == G_OBJECT_TYPE (feature)) &&
176       (data->name == NULL
177           || !strcmp (data->name, GST_PLUGIN_FEATURE_NAME (feature))));
178 }
179 #endif /* GST_REMOVE_DEPRECATED */
180
181 /**
182  * gst_plugin_feature_set_name:
183  * @feature: a feature
184  * @name: the name to set
185  *
186  * Sets the name of a plugin feature. The name uniquely identifies a feature
187  * within all features of the same type. Renaming a plugin feature is not
188  * allowed. A copy is made of the name so you should free the supplied @name
189  * after calling this function.
190  */
191 void
192 gst_plugin_feature_set_name (GstPluginFeature * feature, const gchar * name)
193 {
194   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
195   g_return_if_fail (name != NULL);
196
197   if (G_UNLIKELY (feature->name)) {
198     g_return_if_fail (strcmp (feature->name, name) == 0);
199   } else {
200     gst_object_set_name (GST_OBJECT (feature), name);
201     feature->name = GST_OBJECT_NAME (GST_OBJECT (feature));
202   }
203 }
204
205 /**
206  * gst_plugin_feature_get_name:
207  * @feature: a feature
208  *
209  * Gets the name of a plugin feature.
210  *
211  * Returns: the name
212  */
213 const gchar *
214 gst_plugin_feature_get_name (GstPluginFeature * feature)
215 {
216   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), NULL);
217
218   return feature->name;
219 }
220
221 /**
222  * gst_plugin_feature_set_rank:
223  * @feature: feature to rank
224  * @rank: rank value - higher number means more priority rank
225  *
226  * Specifies a rank for a plugin feature, so that autoplugging uses
227  * the most appropriate feature.
228  */
229 void
230 gst_plugin_feature_set_rank (GstPluginFeature * feature, guint rank)
231 {
232   g_return_if_fail (feature != NULL);
233   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
234
235   feature->rank = rank;
236 }
237
238 /**
239  * gst_plugin_feature_get_rank:
240  * @feature: a feature
241  *
242  * Gets the rank of a plugin feature.
243  *
244  * Returns: The rank of the feature
245  */
246 guint
247 gst_plugin_feature_get_rank (GstPluginFeature * feature)
248 {
249   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), GST_RANK_NONE);
250
251   return feature->rank;
252 }
253
254 /**
255  * gst_plugin_feature_list_free:
256  * @list: (transfer full) (element-type Gst.PluginFeature): list
257  *     of #GstPluginFeature
258  *
259  * Unrefs each member of @list, then frees the list.
260  */
261 void
262 gst_plugin_feature_list_free (GList * list)
263 {
264   GList *g;
265
266   for (g = list; g; g = g->next) {
267     GstPluginFeature *feature = GST_PLUGIN_FEATURE_CAST (g->data);
268
269     gst_object_unref (feature);
270   }
271   g_list_free (list);
272 }
273
274 /**
275  * gst_plugin_feature_list_copy:
276  * @list: (transfer none) (element-type Gst.PluginFeature): list
277  *     of #GstPluginFeature
278  *
279  * Copies the list of features. Caller should call @gst_plugin_feature_list_free
280  * when done with the list.
281  *
282  * Returns: (transfer full) (element-type Gst.PluginFeature): a copy of @list,
283  *     with each feature's reference count incremented.
284  *
285  * Since: 0.10.26
286  */
287 GList *
288 gst_plugin_feature_list_copy (GList * list)
289 {
290   GList *new_list = NULL;
291
292   if (G_LIKELY (list)) {
293     GList *last;
294
295     new_list = g_list_alloc ();
296     new_list->data = g_object_ref ((GObject *) list->data);
297     new_list->prev = NULL;
298     last = new_list;
299     list = list->next;
300     while (list) {
301       last->next = g_list_alloc ();
302       last->next->prev = last;
303       last = last->next;
304       last->data = g_object_ref ((GObject *) list->data);
305       list = list->next;
306     }
307     last->next = NULL;
308   }
309
310   return new_list;
311 }
312
313 /**
314  * gst_plugin_feature_list_debug:
315  * @list: (transfer none) (element-type Gst.PluginFeature): a #GList of
316  *     plugin features
317  *
318  * Debug the plugin feature names in @list.
319  *
320  * Since: 0.10.31
321  */
322 void
323 gst_plugin_feature_list_debug (GList * list)
324 {
325 #ifndef GST_DISABLE_GST_DEBUG
326   while (list) {
327     GST_DEBUG ("%s",
328         gst_plugin_feature_get_name ((GstPluginFeature *) list->data));
329     list = list->next;
330   }
331 #endif
332 }
333
334 /**
335  * gst_plugin_feature_check_version:
336  * @feature: a feature
337  * @min_major: minimum required major version
338  * @min_minor: minimum required minor version
339  * @min_micro: minimum required micro version
340  *
341  * Checks whether the given plugin feature is at least
342  *  the required version
343  *
344  * Returns: #TRUE if the plugin feature has at least
345  *  the required version, otherwise #FALSE.
346  */
347 gboolean
348 gst_plugin_feature_check_version (GstPluginFeature * feature,
349     guint min_major, guint min_minor, guint min_micro)
350 {
351   GstRegistry *registry;
352   GstPlugin *plugin;
353   gboolean ret = FALSE;
354
355   g_return_val_if_fail (feature != NULL, FALSE);
356   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
357
358   GST_DEBUG ("Looking up plugin '%s' containing plugin feature '%s'",
359       feature->plugin_name, feature->name);
360
361   registry = gst_registry_get_default ();
362   plugin = gst_registry_find_plugin (registry, feature->plugin_name);
363
364   if (plugin) {
365     const gchar *ver_str;
366     guint major, minor, micro, nano;
367     gint nscan;
368
369     ver_str = gst_plugin_get_version (plugin);
370     g_return_val_if_fail (ver_str != NULL, FALSE);
371
372     nscan = sscanf (ver_str, "%u.%u.%u.%u", &major, &minor, &micro, &nano);
373     GST_DEBUG ("version string '%s' parsed to %d values", ver_str, nscan);
374
375     if (nscan >= 3) {
376       if (major > min_major)
377         ret = TRUE;
378       else if (major < min_major)
379         ret = FALSE;
380       else if (minor > min_minor)
381         ret = TRUE;
382       else if (minor < min_minor)
383         ret = FALSE;
384       else if (micro > min_micro)
385         ret = TRUE;
386       /* micro is 1 smaller but we have a nano version, this is the upcoming
387        * release of the requested version and we're ok then */
388       else if (nscan == 4 && nano > 0 && (micro + 1 == min_micro))
389         ret = TRUE;
390       else
391         ret = (micro == min_micro);
392
393       GST_DEBUG ("Checking whether %u.%u.%u >= %u.%u.%u? %s", major, minor,
394           micro, min_major, min_minor, min_micro, (ret) ? "yes" : "no");
395     } else {
396       GST_WARNING ("Could not parse version string '%s' of plugin '%s'",
397           ver_str, feature->plugin_name);
398     }
399
400     gst_object_unref (plugin);
401   } else {
402     GST_DEBUG ("Could not find plugin '%s'", feature->plugin_name);
403   }
404
405   return ret;
406 }
407
408 /**
409  * gst_plugin_feature_rank_compare_func:
410  * @p1: a #GstPluginFeature
411  * @p2: a #GstPluginFeature
412  *
413  * Compares the two given #GstPluginFeature instances. This function can be
414  * used as a #GCompareFunc when sorting by rank and then by name.
415  *
416  * Returns: negative value if the rank of p1 > the rank of p2 or the ranks are
417  * equal but the name of p1 comes before the name of p2; zero if the rank
418  * and names are equal; positive value if the rank of p1 < the rank of p2 or the
419  * ranks are equal but the name of p2 comes after the name of p1
420  *
421  * Since: 0.10.31
422  */
423 gint
424 gst_plugin_feature_rank_compare_func (gconstpointer p1, gconstpointer p2)
425 {
426   GstPluginFeature *f1, *f2;
427   gint diff;
428
429   f1 = (GstPluginFeature *) p1;
430   f2 = (GstPluginFeature *) p2;
431
432   diff = f2->rank - f1->rank;
433   if (diff != 0)
434     return diff;
435
436   diff = strcmp (f2->name, f1->name);
437
438   return diff;
439 }