uri: fix wrong G_GNUC_MALLOC
[platform/upstream/gstreamer.git] / gst / gsttypefind.c
index 87cd74d..3827265 100644 (file)
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */
+
 /**
  * SECTION:gsttypefind
  * @short_description: Stream type detection
  *
+ * The following functions allow you to detect the media type of an unknown
+ * stream.
+ *
+ * Last reviewed on 2005-11-09 (0.9.4)
  */
 
 #include "gst_private.h"
 #include "gstregistry.h"
 #include "gsttypefindfactory.h"
 
-GST_DEBUG_CATEGORY_EXTERN (gst_type_find_debug);
-#define GST_CAT_DEFAULT gst_type_find_debug
+GST_DEBUG_CATEGORY_EXTERN (type_find_debug);
+#define GST_CAT_DEFAULT type_find_debug
+
+GType
+gst_type_find_get_type (void)
+{
+  static GType typefind_type = 0;
+
+  if (G_UNLIKELY (typefind_type == 0)) {
+    typefind_type = g_pointer_type_register_static ("GstTypeFind");
+  }
+  return typefind_type;
+}
 
 /**
  * gst_type_find_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.
- *
- * Registers a new typefind function to be used for typefinding. After 
+ * @plugin: A #GstPlugin, or NULL for a static typefind function (note that
+ *    passing NULL only works in GStreamer 0.10.16 and later)
+ * @name: The name for registering
+ * @rank: The rank (or importance) of this typefind function
+ * @func: The #GstTypeFindFunction to use
+ * @extensions: (transfer none) (array zero-terminated=1) (element-type utf8):
+ *     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.
+ * @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
  * registering this function will be available for typefinding.
  * This function is typically called during an element's plugin initialization.
  *
@@ -53,30 +74,42 @@ GST_DEBUG_CATEGORY_EXTERN (gst_type_find_debug);
 gboolean
 gst_type_find_register (GstPlugin * plugin, const gchar * name, guint rank,
     GstTypeFindFunction func, gchar ** extensions,
-    const GstCaps * possible_caps, gpointer data)
+    const GstCaps * possible_caps, gpointer data, GDestroyNotify data_notify)
 {
   GstTypeFindFactory *factory;
 
-  g_return_val_if_fail (plugin != NULL, FALSE);
   g_return_val_if_fail (name != NULL, FALSE);
-  g_return_val_if_fail (func != NULL, FALSE);
 
   GST_INFO ("registering typefind function for %s", name);
 
-  factory = g_object_new (GST_TYPE_TYPE_FIND_FACTORY, NULL);
+  factory = g_object_newv (GST_TYPE_TYPE_FIND_FACTORY, 0, 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);
-  gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
 
-  gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE (factory), rank);
+  gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
+  gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory), rank);
+
   if (factory->extensions)
     g_strfreev (factory->extensions);
-
   factory->extensions = g_strdupv (extensions);
+
   gst_caps_replace (&factory->caps, (GstCaps *) 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_default (),
+      GST_PLUGIN_FEATURE_CAST (factory));
 
   return TRUE;
 }
@@ -85,18 +118,18 @@ gst_type_find_register (GstPlugin * plugin, const gchar * name, guint rank,
 
 /**
  * gst_type_find_peek:
- * @find: the find 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 requested data or NULL if that data is not available.
+ * @find: The #GstTypeFind object the function was called with
+ * @offset: The offset
+ * @size: The number of bytes to return
+ *
+ * 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: (transfer none) (array length=size): the requested data, or NULL
+ *     if that data is not available.
  */
 guint8 *
 gst_type_find_peek (GstTypeFind * find, gint64 offset, guint size)
@@ -108,14 +141,14 @@ gst_type_find_peek (GstTypeFind * find, gint64 offset, guint 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,
@@ -130,12 +163,63 @@ gst_type_find_suggest (GstTypeFind * find, guint probability,
 }
 
 /**
+ * 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: 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).
+ *
+ * Since: 0.10.20
+ */
+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));
+
+  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)