Revert "bin: Hold the state lock while removing elements from a bin"
[platform/upstream/gstreamer.git] / gst / gsttypefind.c
index b87d4fd..5c2b0d1 100644 (file)
@@ -1,7 +1,7 @@
 /* GStreamer
  * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
  *
- * gsttypefind.h: typefinding subsystem
+ * gsttypefind.c: typefinding subsystem
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
  *
  * You should have received a copy of the GNU Library General Public
  * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
  */
 
-#include "gstinfo.h"
-#include "gsttypefind.h"
-#include "gstregistrypool.h"
-
-GST_DEBUG_CATEGORY_STATIC (gst_type_find_debug);
-#define GST_CAT_DEFAULT gst_type_find_debug
-
-static void            gst_type_find_factory_class_init        (gpointer               g_class,
-                                                                gpointer               class_data);
-static void            gst_type_find_factory_init              (GTypeInstance *        instance,
-                                                                gpointer               g_class);
-static void            gst_type_find_factory_dispose           (GObject *              object);
-
-static void            gst_type_find_factory_unload_thyself    (GstPluginFeature *     feature);
-
-static void            gst_type_find_load_plugin               (GstTypeFind *          find,
-                                                                gpointer               data);
-
-static GstPluginFeatureClass *parent_class = NULL;
-
-GType
-gst_type_find_factory_get_type (void)
-{
-  static GType typefind_type = 0;
-    
-  if (!typefind_type) {
-    static const GTypeInfo typefind_info = {
-      sizeof (GstTypeFindFactoryClass),
-      NULL,
-      NULL,
-      gst_type_find_factory_class_init,
-      NULL,
-      NULL,
-      sizeof (GstTypeFindFactory),
-      0,
-      gst_type_find_factory_init,
-      NULL
-    };
-    typefind_type = g_type_register_static (GST_TYPE_PLUGIN_FEATURE,
-                                           "GstTypeFindFactory",
-                                           &typefind_info, 0);
-    GST_DEBUG_CATEGORY_INIT (gst_type_find_debug, "GST_TYPEFIND", 
-                            GST_DEBUG_FG_GREEN, "typefinding subsystem");
-  }
-
-  return typefind_type;
-}
-static void
-gst_type_find_factory_class_init (gpointer g_class, gpointer class_data)
-{
-  GstPluginFeatureClass *gstpluginfeature_class = GST_PLUGIN_FEATURE_CLASS (g_class);
-  GObjectClass *object_class = G_OBJECT_CLASS (g_class);
-    
-  parent_class = g_type_class_peek_parent (g_class);
-  
-  object_class->dispose = gst_type_find_factory_dispose;
-  
-  gstpluginfeature_class->unload_thyself = GST_DEBUG_FUNCPTR (gst_type_find_factory_unload_thyself);
-}
-static void
-gst_type_find_factory_init (GTypeInstance *instance, gpointer g_class)
-{
-  GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (instance);
-
-  factory->user_data = factory;
-  factory->function = gst_type_find_load_plugin;
-}
-static void
-gst_type_find_factory_dispose (GObject *object)
-{
-  GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (object);
-
-  if (factory->caps) {
-    gst_caps_unref (factory->caps);
-    factory->caps = NULL;
-  }
-  if (factory->extensions) {
-    g_strfreev (factory->extensions);
-    factory->extensions = NULL;
-  }
-}
-static void
-gst_type_find_factory_unload_thyself (GstPluginFeature *feature)
-{
-  GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (feature);
-
-  factory->function = gst_type_find_load_plugin;
-  factory->user_data = factory;
-}
-static void
-gst_type_find_load_plugin (GstTypeFind *find, gpointer data)
-{
-  GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (data);
-
-  GST_DEBUG_OBJECT (factory, "need to load typefind function %s",  GST_PLUGIN_FEATURE_NAME (factory));
-  
-  if (gst_plugin_feature_ensure_loaded (GST_PLUGIN_FEATURE (factory))) {
-    if (factory->function == gst_type_find_load_plugin) {
-      /* looks like we didn't get a real typefind function */
-      g_warning ("could not load valid typefind function for feature '%s'\n", GST_PLUGIN_FEATURE_NAME (factory));
-    } else {
-      g_assert (factory->function);
-      gst_type_find_factory_call_function (factory, find);
-    }
-  }
-}
-/**
- * gst_type_find_factory_get_list:
- *
- * Gets the list of all registered typefind factories. You must free the
- * list using g_list_free.
- * 
- * Returns: the list of all registered typefind factories
- */
-GList *
-gst_type_find_factory_get_list (void)
-{
-  return gst_registry_pool_feature_list (GST_TYPE_TYPE_FIND_FACTORY);
-}
 /**
- * gst_type_find_factory_get_caps:
- * @factory: a factory
- * 
- * Gets the caps associated with a typefind factory.
+ * SECTION:gsttypefind
+ * @title: GstTypefind
+ * @short_description: Stream type detection
  *
- * Returns: the #GstCaps associated with this factory
+ * The following functions allow you to detect the media type of an unknown
+ * stream.
  */
-GstCaps *
-gst_type_find_factory_get_caps (const GstTypeFindFactory *factory)
-{
-  g_return_val_if_fail (GST_IS_TYPE_FIND_FACTORY (factory), NULL);
 
-  return factory->caps;
-}
-/**
- * gst_type_find_factory_get_extensions:
- * @factory: a factory
- * 
- * Gets the extensions associated with a typefind factory. The returned
- * array should not be changed. If you need to change stuff in it, you should
- * copy it using g_stdupv().  This function may return NULL to indicate
- * a 0-length list.
- *
- * Returns: a NULL-terminated array of extensions associated with this factory
- */
-gchar **
-gst_type_find_factory_get_extensions (const GstTypeFindFactory *factory)
-{
-  g_return_val_if_fail (GST_IS_TYPE_FIND_FACTORY (factory), NULL);
+#include "gst_private.h"
+#include "gstinfo.h"
+#include "gsttypefind.h"
+#include "gstregistry.h"
+#include "gsttypefindfactory.h"
 
-  return factory->extensions;
-}
-/**
- * gst_type_find_factory_call_function:
- * @factory: a factory
- * @find: a properly setup #GstTypeFind entry. The get_data and suggest_type
- *        members must be set.
- * 
- * Calls the typefinding function associated with this factory.
- */
-void
-gst_type_find_factory_call_function (const GstTypeFindFactory *factory, GstTypeFind *find)
-{
-  g_return_if_fail (GST_IS_TYPE_FIND_FACTORY (factory));
-  g_return_if_fail (find != NULL);
-  g_return_if_fail (find->peek != NULL);
-  g_return_if_fail (find->suggest != NULL);
+GST_DEBUG_CATEGORY_EXTERN (type_find_debug);
+#define GST_CAT_DEFAULT type_find_debug
 
-  /* should never happen */
-  g_assert (factory->function != NULL);
+G_DEFINE_POINTER_TYPE (GstTypeFind, gst_type_find);
 
-  factory->function (find, factory->user_data);
-}
 /**
- * gst_type_find_factory_register:
- * @plugin: the GstPlugin to register with
- * @name: the name for registering
- * @rank: rank (or importance) of this typefind function
- * @func: the function to use for typefinding
- * @extensions: optional extensions that could belong to this type
- * @possible_caps: optionally the caps that could be returned when typefinding succeeds
- * @data: optional user data. This user data must be available until the plugin 
- *       is unloaded.
+ * gst_type_find_register:
+ * @plugin: (allow-none): A #GstPlugin, or %NULL for a static typefind function
+ * @name: The name for registering
+ * @rank: The rank (or importance) of this typefind function
+ * @func: The #GstTypeFindFunction to use
+ * @extensions: (allow-none): Optional comma-separated list of extensions
+ *     that could belong to this type
+ * @possible_caps: Optionally the caps that could be returned when typefinding
+ *                 succeeds
+ * @data: Optional user data. This user data must be available until the plugin
+ *        is unloaded.
+ * @data_notify: a #GDestroyNotify that will be called on @data when the plugin
+ *        is unloaded.
  *
- * Registers a new typefind function to be used for typefinding. After 
+ * Registers a new typefind function to be used for typefinding. After
  * registering this function will be available for typefinding.
  * This function is typically called during an element's plugin initialization.
  *
- * Returns: TRUE on success, FALSE otherwise
+ * Returns: %TRUE on success, %FALSE otherwise
  */
-void
-gst_type_find_factory_register (GstPlugin *plugin, const gchar *name, guint rank,
-                               GstTypeFindFunction func, gchar **extensions, GstCaps *possible_caps,
-                               gpointer data)
+gboolean
+gst_type_find_register (GstPlugin * plugin, const gchar * name, guint rank,
+    GstTypeFindFunction func, const gchar * extensions,
+    GstCaps * possible_caps, gpointer data, GDestroyNotify data_notify)
 {
   GstTypeFindFactory *factory;
-  
-  g_return_if_fail (plugin != NULL);
-  g_return_if_fail (name != NULL);
-  g_return_if_fail (func != NULL);
+
+  g_return_val_if_fail (name != NULL, FALSE);
 
   GST_INFO ("registering typefind function for %s", name);
-  factory = GST_TYPE_FIND_FACTORY (gst_registry_pool_find_feature (name, GST_TYPE_TYPE_FIND_FACTORY));
-  if (!factory) {
-    factory = g_object_new (GST_TYPE_TYPE_FIND_FACTORY, NULL);
-    GST_DEBUG_OBJECT (factory, "using new typefind factory for %s", name);
-    g_assert (GST_IS_TYPE_FIND_FACTORY (factory));
-    gst_plugin_feature_set_name (GST_PLUGIN_FEATURE (factory), name);
-  } else {
-    GST_DEBUG_OBJECT (factory, "using old typefind factory for %s", name);
-  }
 
-  gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE (factory), rank);
-  if (factory->extensions)
-    g_strfreev (factory->extensions);
+  factory = g_object_new (GST_TYPE_TYPE_FIND_FACTORY, NULL);
+  GST_DEBUG_OBJECT (factory, "using new typefind factory for %s", name);
+
+  gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
+  gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory), rank);
+
+  if (extensions)
+    factory->extensions = g_strsplit (extensions, ",", -1);
 
-  factory->extensions = g_strdupv (extensions);
   gst_caps_replace (&factory->caps, possible_caps);
   factory->function = func;
   factory->user_data = data;
+  factory->user_data_notify = data_notify;
+  if (plugin && plugin->desc.name) {
+    GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = plugin->desc.name; /* interned string */
+    GST_PLUGIN_FEATURE_CAST (factory)->plugin = plugin;
+    g_object_add_weak_pointer ((GObject *) plugin,
+        (gpointer *) & GST_PLUGIN_FEATURE_CAST (factory)->plugin);
+  } else {
+    GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
+    GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
+  }
+  GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
+
+  gst_registry_add_feature (gst_registry_get (),
+      GST_PLUGIN_FEATURE_CAST (factory));
 
-  gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
+  return TRUE;
 }
 
 /*** typefind function interface **********************************************/
 
 /**
  * gst_type_find_peek:
- * @find: the find object the function was called with
- * @offset: the offset
- * @size: the number of bytes to return
+ * @find: The #GstTypeFind object the function was called with
+ * @offset: The offset
+ * @size: The number of bytes to return
  *
- * Returns size bytes of the stream to identify beginning at offset. If offset 
- * is a positive number, the offset is relative to the beginning of the stream,
- * if offset is a negative number the offset is relative to the end of the 
- * stream. The returned memory is valid until the typefinding function returns
- * and must not be freed.
- * If NULL is returned, that data is not available.
+ * Returns the @size bytes of the stream to identify beginning at offset. If
+ * offset is a positive number, the offset is relative to the beginning of the
+ * stream, if offset is a negative number the offset is relative to the end of
+ * the stream. The returned memory is valid until the typefinding function
+ * returns and must not be freed.
  *
- * Returns: the requested data or NULL if that data is not available.
+ * Returns: (transfer none) (array length=size) (nullable): the
+ *     requested data, or %NULL if that data is not available.
  */
-guint8 *
-gst_type_find_peek (GstTypeFind *find, gint64 offset, guint size)
+const guint8 *
+gst_type_find_peek (GstTypeFind * find, gint64 offset, guint size)
 {
   g_return_val_if_fail (find->peek != NULL, NULL);
 
   return find->peek (find->data, offset, size);
 }
+
 /**
  * gst_type_find_suggest:
- * @find: the find object the function was called with
- * @probability: the probability in percent that the suggestion is right
- * @caps: the fixed caps to suggest
+ * @find: The #GstTypeFind object the function was called with
+ * @probability: The probability in percent that the suggestion is right
+ * @caps: The fixed #GstCaps to suggest
  *
- * If a typefind function calls this function it suggests the caps with the
- * given probability. A typefind function may supply different suggestions
+ * If a #GstTypeFindFunction calls this function it suggests the caps with the
+ * given probability. A #GstTypeFindFunction may supply different suggestions
  * in one call.
- * It is up to the caller of the typefind function to interpret these values.
+ * It is up to the caller of the #GstTypeFindFunction to interpret these values.
  */
 void
-gst_type_find_suggest (GstTypeFind *find, guint probability, GstCaps *caps)
+gst_type_find_suggest (GstTypeFind * find, guint probability, GstCaps * caps)
 {
   g_return_if_fail (find->suggest != NULL);
   g_return_if_fail (probability <= 100);
   g_return_if_fail (caps != NULL);
-  g_return_if_fail (GST_CAPS_IS_FIXED (caps));
+  g_return_if_fail (gst_caps_is_fixed (caps));
+
+  find->suggest (find->data, probability, caps);
+}
+
+/**
+ * gst_type_find_suggest_simple:
+ * @find: The #GstTypeFind object the function was called with
+ * @probability: The probability in percent that the suggestion is right
+ * @media_type: the media type of the suggested caps
+ * @fieldname: (allow-none): first field of the suggested caps, or %NULL
+ * @...: additional arguments to the suggested caps in the same format as the
+ *     arguments passed to gst_structure_new() (ie. triplets of field name,
+ *     field GType and field value)
+ *
+ * If a #GstTypeFindFunction calls this function it suggests the caps with the
+ * given probability. A #GstTypeFindFunction may supply different suggestions
+ * in one call. It is up to the caller of the #GstTypeFindFunction to interpret
+ * these values.
+ *
+ * This function is similar to gst_type_find_suggest(), only that instead of
+ * passing a #GstCaps argument you can create the caps on the fly in the same
+ * way as you can with gst_caps_new_simple().
+ *
+ * Make sure you terminate the list of arguments with a %NULL argument and that
+ * the values passed have the correct type (in terms of width in bytes when
+ * passed to the vararg function - this applies particularly to gdouble and
+ * guint64 arguments).
+ */
+void
+gst_type_find_suggest_simple (GstTypeFind * find, guint probability,
+    const char *media_type, const char *fieldname, ...)
+{
+  GstStructure *structure;
+  va_list var_args;
+  GstCaps *caps;
+
+  g_return_if_fail (find->suggest != NULL);
+  g_return_if_fail (probability <= 100);
+  g_return_if_fail (media_type != NULL);
+
+  caps = gst_caps_new_empty ();
+
+  va_start (var_args, fieldname);
+  structure = gst_structure_new_valist (media_type, fieldname, var_args);
+  va_end (var_args);
+
+  gst_caps_append_structure (caps, structure);
+  g_return_if_fail (gst_caps_is_fixed (caps));
 
-  gst_caps_ref (caps);
-  gst_caps_sink (caps);
   find->suggest (find->data, probability, caps);
   gst_caps_unref (caps);
 }
+
 /**
  * gst_type_find_get_length:
- * @find: the find object the function was called with
+ * @find: The #GstTypeFind the function was called with
  *
  * Get the length of the data stream.
- * 
- * Returns: the length of the data stream or 0 if it is not available.
+ *
+ * Returns: The length of the data stream, or 0 if it is not available.
  */
 guint64
-gst_type_find_get_length (GstTypeFind *find)
+gst_type_find_get_length (GstTypeFind * find)
 {
   if (find->get_length == NULL)
     return 0;
 
-  return find->get_length(find->data);
+  return find->get_length (find->data);
 }
-