element: Enforce that elements created by gst_element_factory_create/make() are floating
[platform/upstream/gstreamer.git] / gst / gstelementfactory.c
index aa0c5af..3ccfc14 100644 (file)
@@ -23,6 +23,7 @@
 
 /**
  * SECTION:gstelementfactory
+ * @title: GstElementFactory
  * @short_description: Create GstElements from a factory
  * @see_also: #GstElement, #GstPlugin, #GstPluginFeature, #GstPadTemplate.
  *
@@ -36,9 +37,8 @@
  *
  * The following code example shows you how to create a GstFileSrc element.
  *
- * <example>
- * <title>Using an element factory</title>
- * <programlisting language="c">
+ * ## Using an element factory
+ * |[<!-- language="C" -->
  *   #include &lt;gst/gst.h&gt;
  *
  *   GstElement *src;
@@ -51,8 +51,7 @@
  *   src = gst_element_factory_create (srcfactory, "src");
  *   g_return_if_fail (src != NULL);
  *   ...
- * </programlisting>
- * </example>
+ * ]|
  */
 
 #include "gst_private.h"
@@ -123,7 +122,8 @@ gst_element_factory_finalize (GObject * object)
  * Search for an element factory of the given name. Refs the returned
  * element factory; caller is responsible for unreffing.
  *
- * Returns: (transfer full): #GstElementFactory if found, %NULL otherwise
+ * Returns: (transfer full) (nullable): #GstElementFactory if found,
+ * %NULL otherwise
  */
 GstElementFactory *
 gst_element_factory_find (const gchar * name)
@@ -230,9 +230,7 @@ gst_element_register (GstPlugin * plugin, const gchar * name, guint rank,
     return TRUE;
   }
 
-  factory =
-      GST_ELEMENT_FACTORY_CAST (g_object_newv (GST_TYPE_ELEMENT_FACTORY, 0,
-          NULL));
+  factory = g_object_new (GST_TYPE_ELEMENT_FACTORY, NULL);
   gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
   GST_LOG_OBJECT (factory, "Created new elementfactory for type %s",
       g_type_name (type));
@@ -335,8 +333,8 @@ detailserror:
  * It will be given the name supplied, since all elements require a name as
  * their first argument.
  *
- * Returns: (transfer floating): new #GstElement or %NULL if the element couldn't
- *     be created
+ * Returns: (transfer floating) (nullable): new #GstElement or %NULL
+ *     if the element couldn't be created
  */
 GstElement *
 gst_element_factory_create (GstElementFactory * factory, const gchar * name)
@@ -369,10 +367,9 @@ gst_element_factory_create (GstElementFactory * factory, const gchar * name)
    * also set name as early as we can
    */
   if (name)
-    element =
-        GST_ELEMENT_CAST (g_object_new (factory->type, "name", name, NULL));
+    element = g_object_new (factory->type, "name", name, NULL);
   else
-    element = GST_ELEMENT_CAST (g_object_newv (factory->type, 0, NULL));
+    element = g_object_new (factory->type, NULL);
   if (G_UNLIKELY (element == NULL))
     goto no_element;
 
@@ -385,6 +382,15 @@ gst_element_factory_create (GstElementFactory * factory, const gchar * name)
   if (!g_atomic_pointer_compare_and_exchange (&oclass->elementfactory, NULL,
           factory))
     gst_object_unref (factory);
+  else
+    /* This ref will never be dropped as the class is never destroyed */
+    GST_OBJECT_FLAG_SET (factory, GST_OBJECT_FLAG_MAY_BE_LEAKED);
+
+  /* Ensure that the reference is floating. Bindings might have a hard time
+   * making sure that the reference is indeed still floating after returning
+   * here */
+  if (element)
+    g_object_force_floating ((GObject *) element);
 
   GST_DEBUG ("created element \"%s\"", GST_OBJECT_NAME (factory));
 
@@ -422,7 +428,8 @@ no_element:
  * consisting of the element factory name and a number.
  * If name is given, it will be given the name supplied.
  *
- * Returns: (transfer floating): new #GstElement or %NULL if unable to create element
+ * Returns: (transfer floating) (nullable): new #GstElement or %NULL
+ * if unable to create element
  */
 GstElement *
 gst_element_factory_make (const gchar * factoryname, const gchar * name)
@@ -446,12 +453,13 @@ gst_element_factory_make (const gchar * factoryname, const gchar * name)
     goto create_failed;
 
   gst_object_unref (factory);
+
   return element;
 
   /* ERRORS */
 no_factory:
   {
-    GST_INFO ("no such element factory \"%s\"!", factoryname);
+    GST_WARNING ("no such element factory \"%s\"!", factoryname);
     return NULL;
   }
 create_failed:
@@ -500,13 +508,15 @@ gst_element_factory_get_element_type (GstElementFactory * factory)
  *
  * Get the metadata on @factory with @key.
  *
- * Returns: the metadata with @key on @factory or %NULL when there was no
- * metadata with the given @key.
+ * Returns: (nullable): the metadata with @key on @factory or %NULL
+ * when there was no metadata with the given @key.
  */
 const gchar *
 gst_element_factory_get_metadata (GstElementFactory * factory,
     const gchar * key)
 {
+  g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
+
   return gst_structure_get_string ((GstStructure *) factory->metadata, key);
 }
 
@@ -516,7 +526,7 @@ gst_element_factory_get_metadata (GstElementFactory * factory,
  *
  * Get the available keys for the metadata on @factory.
  *
- * Returns: (transfer full) (element-type utf8) (array zero-terminated=1):
+ * Returns: (transfer full) (element-type utf8) (array zero-terminated=1) (nullable):
  * a %NULL-terminated array of key strings, or %NULL when there is no
  * metadata. Free with g_strfreev() when no longer needed.
  */
@@ -727,6 +737,12 @@ gst_element_factory_list_is_type (GstElementFactory * factory,
   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_FORMATTER))
     res = (strstr (klass, "Formatter") != NULL);
 
+  if (!res && (type & GST_ELEMENT_FACTORY_TYPE_DECRYPTOR))
+    res = (strstr (klass, "Decryptor") != NULL);
+
+  if (!res && (type & GST_ELEMENT_FACTORY_TYPE_ENCRYPTOR))
+    res = (strstr (klass, "Encryptor") != NULL);
+
   /* Filter by media type now, we only test if it
    * matched any of the types above or only checking the media
    * type was requested. */