Revert "bin: Hold the state lock while removing elements from a bin"
[platform/upstream/gstreamer.git] / gst / gsttypefind.c
index 032e723..5c2b0d1 100644 (file)
@@ -1,8 +1,7 @@
 /* GStreamer
- * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
- *                    2000 Wim Taymans <wtay@chello.be>
+ * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
  *
- * gsttypefind.c: 
+ * 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.
  */
 
+/**
+ * SECTION:gsttypefind
+ * @title: GstTypefind
+ * @short_description: Stream type detection
+ *
+ * The following functions allow you to detect the media type of an unknown
+ * stream.
+ */
 
 #include "gst_private.h"
-
-#include "gsttype.h"
-
-#include "gstlog.h"
+#include "gstinfo.h"
 #include "gsttypefind.h"
+#include "gstregistry.h"
+#include "gsttypefindfactory.h"
+
+GST_DEBUG_CATEGORY_EXTERN (type_find_debug);
+#define GST_CAT_DEFAULT type_find_debug
+
+G_DEFINE_POINTER_TYPE (GstTypeFind, gst_type_find);
+
+/**
+ * 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
+ * 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
+ */
+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;
 
-/* #define GST_DEBUG_ENABLED */
-
-GstElementDetails gst_type_find_details = {
-  "TypeFind",
-  "Generic",
-  "Finds the media type of a stream",
-  VERSION,
-  "Erik Walthinsen <omega@cse.ogi.edu>"
-  "Wim Taymans <wim.taymans@chello.be>",
-  "(C) 1999",
-};
-
-
-/* TypeFind signals and args */
-enum {
-  HAVE_TYPE,
-  LAST_SIGNAL
-};
-
-enum {
-  ARG_0,
-  ARG_CAPS,
-};
-
+  g_return_val_if_fail (name != NULL, FALSE);
 
-static void    gst_type_find_class_init                (GstTypeFindClass *klass);
-static void    gst_type_find_init              (GstTypeFind *typefind);
+  GST_INFO ("registering typefind function for %s", name);
 
-static void    gst_type_find_set_property      (GObject *object, guint prop_id, 
-                                                const GValue *value, GParamSpec *pspec);
-static void    gst_type_find_get_property      (GObject *object, guint prop_id, 
-                                                GValue *value, GParamSpec *pspec);
+  factory = g_object_new (GST_TYPE_TYPE_FIND_FACTORY, NULL);
+  GST_DEBUG_OBJECT (factory, "using new typefind factory for %s", name);
 
-static void    gst_type_find_chain             (GstPad *pad, GstBuffer *buf);
+  gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
+  gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory), rank);
 
-static GstElementClass *parent_class = NULL;
-static guint gst_type_find_signals[LAST_SIGNAL] = { 0 };
+  if (extensions)
+    factory->extensions = g_strsplit (extensions, ",", -1);
 
-GType
-gst_type_find_get_type (void)
-{
-  static GType typefind_type = 0;
-
-  if (!typefind_type) {
-    static const GTypeInfo typefind_info = {
-      sizeof(GstTypeFindClass),
-      NULL,
-      NULL,
-      (GClassInitFunc)gst_type_find_class_init,
-      NULL,
-      NULL,
-      sizeof(GstTypeFind),
-      0,
-      (GInstanceInitFunc)gst_type_find_init,
-      NULL
-    };
-    typefind_type = g_type_register_static (GST_TYPE_ELEMENT, "GstTypeFind", &typefind_info, 0);
+  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;
   }
-  return typefind_type;
-}
+  GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
 
-static void
-gst_type_find_class_init (GstTypeFindClass *klass)
-{
-  GObjectClass *gobject_class;
-
-  gobject_class = (GObjectClass*)klass;
+  gst_registry_add_feature (gst_registry_get (),
+      GST_PLUGIN_FEATURE_CAST (factory));
 
-  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
+  return TRUE;
+}
 
-  g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_CAPS,
-    g_param_spec_pointer("caps", "Caps", "Found capabilities", G_PARAM_READABLE));
+/*** typefind function interface **********************************************/
 
-  gst_type_find_signals[HAVE_TYPE] =
-      g_signal_new ("have_type", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
-                     G_STRUCT_OFFSET (GstTypeFindClass, have_type), NULL, NULL,
-                     g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
-                     G_TYPE_POINTER);
+/**
+ * gst_type_find_peek:
+ * @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) (nullable): the
+ *     requested data, or %NULL if that data is not available.
+ */
+const guint8 *
+gst_type_find_peek (GstTypeFind * find, gint64 offset, guint size)
+{
+  g_return_val_if_fail (find->peek != NULL, NULL);
 
-  gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_type_find_set_property);
-  gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_type_find_get_property);
+  return find->peek (find->data, offset, size);
 }
 
-static void
-gst_type_find_init (GstTypeFind *typefind)
+/**
+ * gst_type_find_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 #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.
+ */
+void
+gst_type_find_suggest (GstTypeFind * find, guint probability, GstCaps * caps)
 {
-  typefind->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
-  gst_element_add_pad (GST_ELEMENT (typefind), typefind->sinkpad);
-  gst_pad_set_chain_function (typefind->sinkpad, gst_type_find_chain);
+  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));
+
+  find->suggest (find->data, probability, caps);
 }
 
-static void
-gst_type_find_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
+/**
+ * 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, ...)
 {
-  GstTypeFind *typefind;
-
-  /* it's not null if we got it, but it might not be ours */
-  g_return_if_fail (GST_IS_TYPE_FIND (object));
+  GstStructure *structure;
+  va_list var_args;
+  GstCaps *caps;
 
-  typefind = GST_TYPE_FIND (object);
+  g_return_if_fail (find->suggest != NULL);
+  g_return_if_fail (probability <= 100);
+  g_return_if_fail (media_type != NULL);
 
-  switch (prop_id) {
-    default:
-      break;
-  }
-}
+  caps = gst_caps_new_empty ();
 
-static void
-gst_type_find_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
-{
-  GstTypeFind *typefind;
+  va_start (var_args, fieldname);
+  structure = gst_structure_new_valist (media_type, fieldname, var_args);
+  va_end (var_args);
 
-  /* it's not null if we got it, but it might not be ours */
-  g_return_if_fail (GST_IS_TYPE_FIND (object));
+  gst_caps_append_structure (caps, structure);
+  g_return_if_fail (gst_caps_is_fixed (caps));
 
-  typefind = GST_TYPE_FIND (object);
-
-  switch (prop_id) {
-    case ARG_CAPS:
-      g_value_set_pointer(value, typefind->caps);
-      break;
-    default:
-      break;
-  }
+  find->suggest (find->data, probability, caps);
+  gst_caps_unref (caps);
 }
 
-static void
-gst_type_find_chain (GstPad *pad, GstBuffer *buf)
+/**
+ * gst_type_find_get_length:
+ * @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.
+ */
+guint64
+gst_type_find_get_length (GstTypeFind * find)
 {
-  GstTypeFind *typefind;
-  GList *type_list;
-  GstType *type;
-
-  g_return_if_fail (pad != NULL);
-  g_return_if_fail (GST_IS_PAD (pad));
-  g_return_if_fail (buf != NULL);
-
-  typefind = GST_TYPE_FIND (GST_OBJECT_PARENT (pad));
-  GST_DEBUG (0,"got buffer of %d bytes in '%s'",
-        GST_BUFFER_SIZE (buf), GST_OBJECT_NAME (typefind));
-
-  type_list = (GList *) gst_type_get_list ();
-
-  while (type_list) {
-    GSList *factories;
-    type = (GstType *)type_list->data;
-
-    factories = type->factories;
-
-    while (factories) {
-      GstTypeFactory *factory = GST_TYPE_FACTORY (factories->data);
-      GstTypeFindFunc typefindfunc = (GstTypeFindFunc)factory->typefindfunc;
-      GstCaps *caps;
-
-      GST_DEBUG (0,"try type (%p) :%d \"%s\" %p", factory, type->id, type->mime, typefindfunc);
-      if (typefindfunc && (caps = typefindfunc (buf, factory))) {
-        GST_DEBUG (0,"found type :%d \"%s\" \"%s\"", caps->id, type->mime, 
-                       gst_caps_get_name (caps));
-       typefind->caps = caps;
-
-       if (!gst_pad_try_set_caps (pad, caps)) {
-          g_warning ("typefind: found type but peer didn't accept it");
-       }
-
-       {
-          /* int oldstate = GST_STATE(typefind);*/
-         gst_object_ref (GST_OBJECT (typefind));
-          g_signal_emit (G_OBJECT (typefind), gst_type_find_signals[HAVE_TYPE], 0,
-                             typefind->caps);
-/*          if (GST_STATE(typefind) != oldstate) {
-            GST_DEBUG(0, "state changed during signal, aborting");
-           gst_element_interrupt (GST_ELEMENT (typefind));
-            } */
-         gst_object_unref (GST_OBJECT (typefind));
-
-          goto end;
-       }
-      }
-      factories = g_slist_next (factories);
-    }
-
-    type_list = g_list_next (type_list);
-  }
-end:
-  gst_buffer_unref (buf);
+  if (find->get_length == NULL)
+    return 0;
+
+  return find->get_length (find->data);
 }