Automatic update of common submodule
[platform/upstream/gstreamer.git] / gst / gstutils.c
index 9bbbdcb..076de1c 100644 (file)
@@ -150,7 +150,7 @@ gst_util_set_object_arg (GObject * object, const gchar * name,
   g_value_init (&v, value_type);
 
   /* special case for element <-> xml (de)serialisation */
-  if (GST_VALUE_HOLDS_STRUCTURE (&v) && strcmp (value, "NULL") == 0) {
+  if (value_type == GST_TYPE_STRUCTURE && strcmp (value, "NULL") == 0) {
     g_value_set_boxed (&v, NULL);
     goto done;
   }
@@ -773,8 +773,8 @@ gst_element_create_all_pads (GstElement * element)
  * Retrieves a pad template from @element that is compatible with @compattempl.
  * Pads from compatible templates can be linked together.
  *
- * Returns: (transfer none): a compatible #GstPadTemplate, or %NULL if none
- *     was found. No unreferencing is necessary.
+ * Returns: (transfer none) (nullable): a compatible #GstPadTemplate,
+ *   or %NULL if none was found. No unreferencing is necessary.
  */
 GstPadTemplate *
 gst_element_get_compatible_pad_template (GstElement * element,
@@ -850,8 +850,8 @@ gst_element_get_compatible_pad_template (GstElement * element,
  * #GST_PAD_REQUEST, requests a new pad. Can return %NULL for #GST_PAD_SOMETIMES
  * templates.
  *
- * Returns: (transfer full): the #GstPad, or NULL if one could not be found
- *     or created.
+ * Returns: (transfer full) (nullable): the #GstPad, or %NULL if one
+ *   could not be found or created.
  */
 static GstPad *
 gst_element_get_pad_from_template (GstElement * element, GstPadTemplate * templ)
@@ -891,7 +891,8 @@ gst_element_get_pad_from_template (GstElement * element, GstPadTemplate * templ)
  * Requests a pad from @element. The returned pad should be unlinked and
  * compatible with @templ. Might return an existing pad, or request a new one.
  *
- * Returns: a #GstPad, or %NULL if one could not be found or created.
+ * Returns: (nullable): a #GstPad, or %NULL if one could not be found
+ *   or created.
  */
 static GstPad *
 gst_element_request_compatible_pad (GstElement * element,
@@ -923,11 +924,6 @@ gst_element_request_compatible_pad (GstElement * element,
 static gboolean
 gst_pad_check_link (GstPad * srcpad, GstPad * sinkpad)
 {
-  /* FIXME This function is gross.  It's almost a direct copy of
-   * gst_pad_link_filtered().  Any decent programmer would attempt
-   * to merge the two functions, which I will do some day. --ds
-   */
-
   /* generic checks */
   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
@@ -935,7 +931,6 @@ gst_pad_check_link (GstPad * srcpad, GstPad * sinkpad)
   GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
 
-  /* FIXME: shouldn't we convert this to g_return_val_if_fail? */
   if (GST_PAD_PEER (srcpad) != NULL) {
     GST_CAT_INFO (GST_CAT_PADS, "Source pad %s:%s has a peer, failed",
         GST_DEBUG_PAD_NAME (srcpad));
@@ -984,8 +979,9 @@ gst_pad_check_link (GstPad * srcpad, GstPad * sinkpad)
  * and if none can be found, it will request a compatible REQUEST pad by looking
  * at the templates of @element.
  *
- * Returns: (transfer full): the #GstPad to which a link can be made, or %NULL
- *     if one cannot be found. gst_object_unref() after usage.
+ * Returns: (transfer full) (nullable): the #GstPad to which a link
+ *   can be made, or %NULL if one cannot be found. gst_object_unref()
+ *   after usage.
  */
 GstPad *
 gst_element_get_compatible_pad (GstElement * element, GstPad * pad,
@@ -1103,8 +1099,13 @@ gst_element_get_compatible_pad (GstElement * element, GstPad * pad,
 
   /* try to create a new one */
   /* requesting is a little crazy, we need a template. Let's create one */
-  /* FIXME: why not gst_pad_get_pad_template (pad); */
   templcaps = gst_pad_query_caps (pad, NULL);
+  if (caps) {
+    GstCaps *inter = gst_caps_intersect (templcaps, caps);
+
+    gst_caps_unref (templcaps);
+    templcaps = inter;
+  }
   templ = gst_pad_template_new ((gchar *) GST_PAD_NAME (pad),
       GST_PAD_DIRECTION (pad), GST_PAD_ALWAYS, templcaps);
   gst_caps_unref (templcaps);
@@ -1450,8 +1451,24 @@ prepare_link_maybe_ghosting (GstPad ** src, GstPad ** sink,
   /* we need to setup some ghost pads */
   root = find_common_root (e1, e2);
   if (!root) {
-    g_warning ("Trying to connect elements that don't share a common "
-        "ancestor: %s and %s", GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2));
+    if (GST_OBJECT_PARENT (e1) == NULL)
+      g_warning ("Trying to link elements %s and %s that don't share a common "
+          "ancestor: %s hasn't been added to a bin or pipeline, but %s is in %s",
+          GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2),
+          GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2),
+          GST_ELEMENT_NAME (GST_OBJECT_PARENT (e2)));
+    else if (GST_OBJECT_PARENT (e2) == NULL)
+      g_warning ("Trying to link elements %s and %s that don't share a common "
+          "ancestor: %s hasn't been added to a bin or pipeline, and %s is in %s",
+          GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2),
+          GST_ELEMENT_NAME (e2), GST_ELEMENT_NAME (e1),
+          GST_ELEMENT_NAME (GST_OBJECT_PARENT (e1)));
+    else
+      g_warning ("Trying to link elements %s and %s that don't share a common "
+          "ancestor: %s is in %s, and %s is in %s",
+          GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2),
+          GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (GST_OBJECT_PARENT (e1)),
+          GST_ELEMENT_NAME (e2), GST_ELEMENT_NAME (GST_OBJECT_PARENT (e2)));
     return FALSE;
   }
 
@@ -1501,6 +1518,16 @@ pad_link_maybe_ghosting (GstPad * src, GstPad * sink, GstPadLinkCheck flags)
   return ret;
 }
 
+static void
+release_and_unref_pad (GstElement * element, GstPad * pad, gboolean requestpad)
+{
+  if (pad) {
+    if (requestpad)
+      gst_element_release_request_pad (element, pad);
+    gst_object_unref (pad);
+  }
+}
+
 /**
  * gst_element_link_pads_full:
  * @src: a #GstElement containing the source pad.
@@ -1532,6 +1559,7 @@ gst_element_link_pads_full (GstElement * src, const gchar * srcpadname,
   GstPad *srcpad, *destpad;
   GstPadTemplate *srctempl, *desttempl;
   GstElementClass *srcclass, *destclass;
+  gboolean srcrequest, destrequest;
 
   /* checks */
   g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
@@ -1542,11 +1570,16 @@ gst_element_link_pads_full (GstElement * src, const gchar * srcpadname,
       srcpadname ? srcpadname : "(any)", GST_ELEMENT_NAME (dest),
       destpadname ? destpadname : "(any)");
 
+  srcrequest = FALSE;
+  destrequest = FALSE;
+
   /* get a src pad */
   if (srcpadname) {
     /* name specified, look it up */
-    if (!(srcpad = gst_element_get_static_pad (src, srcpadname)))
-      srcpad = gst_element_get_request_pad (src, srcpadname);
+    if (!(srcpad = gst_element_get_static_pad (src, srcpadname))) {
+      if ((srcpad = gst_element_get_request_pad (src, srcpadname)))
+        srcrequest = TRUE;
+    }
     if (!srcpad) {
       GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
           GST_ELEMENT_NAME (src), srcpadname);
@@ -1555,13 +1588,15 @@ gst_element_link_pads_full (GstElement * src, const gchar * srcpadname,
       if (!(GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC)) {
         GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no src pad",
             GST_DEBUG_PAD_NAME (srcpad));
-        gst_object_unref (srcpad);
+        release_and_unref_pad (src, srcpad, srcrequest);
         return FALSE;
       }
       if (GST_PAD_PEER (srcpad) != NULL) {
         GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
             "pad %s:%s is already linked to %s:%s", GST_DEBUG_PAD_NAME (srcpad),
             GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
+        /* already linked request pads look like static pads, so the request pad
+         * was never requested a second time above, so no need to release it */
         gst_object_unref (srcpad);
         return FALSE;
       }
@@ -1580,17 +1615,21 @@ gst_element_link_pads_full (GstElement * src, const gchar * srcpadname,
   /* get a destination pad */
   if (destpadname) {
     /* name specified, look it up */
-    if (!(destpad = gst_element_get_static_pad (dest, destpadname)))
-      destpad = gst_element_get_request_pad (dest, destpadname);
+    if (!(destpad = gst_element_get_static_pad (dest, destpadname))) {
+      if ((destpad = gst_element_get_request_pad (dest, destpadname)))
+        destrequest = TRUE;
+    }
     if (!destpad) {
       GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
           GST_ELEMENT_NAME (dest), destpadname);
+      release_and_unref_pad (src, srcpad, srcrequest);
       return FALSE;
     } else {
       if (!(GST_PAD_DIRECTION (destpad) == GST_PAD_SINK)) {
         GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no sink pad",
             GST_DEBUG_PAD_NAME (destpad));
-        gst_object_unref (destpad);
+        release_and_unref_pad (src, srcpad, srcrequest);
+        release_and_unref_pad (dest, destpad, destrequest);
         return FALSE;
       }
       if (GST_PAD_PEER (destpad) != NULL) {
@@ -1598,6 +1637,9 @@ gst_element_link_pads_full (GstElement * src, const gchar * srcpadname,
             "pad %s:%s is already linked to %s:%s",
             GST_DEBUG_PAD_NAME (destpad),
             GST_DEBUG_PAD_NAME (GST_PAD_PEER (destpad)));
+        release_and_unref_pad (src, srcpad, srcrequest);
+        /* already linked request pads look like static pads, so the request pad
+         * was never requested a second time above, so no need to release it */
         gst_object_unref (destpad);
         return FALSE;
       }
@@ -1619,9 +1661,13 @@ gst_element_link_pads_full (GstElement * src, const gchar * srcpadname,
     /* two explicitly specified pads */
     result = pad_link_maybe_ghosting (srcpad, destpad, flags);
 
-    gst_object_unref (srcpad);
-    gst_object_unref (destpad);
-
+    if (result) {
+      gst_object_unref (srcpad);
+      gst_object_unref (destpad);
+    } else {
+      release_and_unref_pad (src, srcpad, srcrequest);
+      release_and_unref_pad (dest, destpad, destrequest);
+    }
     return result;
   }
 
@@ -1635,6 +1681,7 @@ gst_element_link_pads_full (GstElement * src, const gchar * srcpadname,
           GST_DEBUG_PAD_NAME (srcpad));
       if ((GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC) &&
           (GST_PAD_PEER (srcpad) == NULL)) {
+        gboolean temprequest = FALSE;
         GstPad *temp;
 
         if (destpadname) {
@@ -1642,6 +1689,11 @@ gst_element_link_pads_full (GstElement * src, const gchar * srcpadname,
           gst_object_ref (temp);
         } else {
           temp = gst_element_get_compatible_pad (dest, srcpad, NULL);
+          if (temp && GST_PAD_PAD_TEMPLATE (temp)
+              && GST_PAD_TEMPLATE_PRESENCE (GST_PAD_PAD_TEMPLATE (temp)) ==
+              GST_PAD_REQUEST) {
+            temprequest = TRUE;
+          }
         }
 
         if (temp && pad_link_maybe_ghosting (srcpad, temp, flags)) {
@@ -1655,6 +1707,8 @@ gst_element_link_pads_full (GstElement * src, const gchar * srcpadname,
         }
 
         if (temp) {
+          if (temprequest)
+            gst_element_release_request_pad (dest, temp);
           gst_object_unref (temp);
         }
       }
@@ -1672,13 +1726,16 @@ gst_element_link_pads_full (GstElement * src, const gchar * srcpadname,
   if (srcpadname) {
     GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s:%s to %s",
         GST_DEBUG_PAD_NAME (srcpad), GST_ELEMENT_NAME (dest));
+    /* no need to release any request pad as both src- and destpadname must be
+     * set to end up here, but this case has already been taken care of above */
     if (destpad)
       gst_object_unref (destpad);
     destpad = NULL;
   }
-  if (srcpad)
-    gst_object_unref (srcpad);
-  srcpad = NULL;
+  if (srcpad) {
+    release_and_unref_pad (src, srcpad, srcrequest);
+    srcpad = NULL;
+  }
 
   if (destpad) {
     /* loop through the existing pads in the destination */
@@ -1688,6 +1745,13 @@ gst_element_link_pads_full (GstElement * src, const gchar * srcpadname,
       if ((GST_PAD_DIRECTION (destpad) == GST_PAD_SINK) &&
           (GST_PAD_PEER (destpad) == NULL)) {
         GstPad *temp = gst_element_get_compatible_pad (src, destpad, NULL);
+        gboolean temprequest = FALSE;
+
+        if (temp && GST_PAD_PAD_TEMPLATE (temp)
+            && GST_PAD_TEMPLATE_PRESENCE (GST_PAD_PAD_TEMPLATE (temp)) ==
+            GST_PAD_REQUEST) {
+          temprequest = TRUE;
+        }
 
         if (temp && pad_link_maybe_ghosting (temp, destpad, flags)) {
           GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
@@ -1696,9 +1760,8 @@ gst_element_link_pads_full (GstElement * src, const gchar * srcpadname,
           gst_object_unref (destpad);
           return TRUE;
         }
-        if (temp) {
-          gst_object_unref (temp);
-        }
+
+        release_and_unref_pad (src, temp, temprequest);
       }
       if (destpads) {
         destpads = g_list_next (destpads);
@@ -1714,11 +1777,15 @@ gst_element_link_pads_full (GstElement * src, const gchar * srcpadname,
   if (destpadname) {
     GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s:%s",
         GST_ELEMENT_NAME (src), GST_DEBUG_PAD_NAME (destpad));
-    gst_object_unref (destpad);
+    release_and_unref_pad (dest, destpad, destrequest);
     return FALSE;
   } else {
-    if (destpad)
+    /* no need to release any request pad as the case of unset destpatname and
+     * destpad being a requst pad has already been taken care of when looking
+     * though the destination pads above */
+    if (destpad) {
       gst_object_unref (destpad);
+    }
     destpad = NULL;
   }
 
@@ -1761,10 +1828,14 @@ gst_element_link_pads_full (GstElement * src, const gchar * srcpadname,
                 return TRUE;
               }
               /* it failed, so we release the request pads */
-              if (srcpad)
+              if (srcpad) {
                 gst_element_release_request_pad (src, srcpad);
-              if (destpad)
+                gst_object_unref (srcpad);
+              }
+              if (destpad) {
                 gst_element_release_request_pad (dest, destpad);
+                gst_object_unref (destpad);
+              }
             }
             gst_caps_unref (srccaps);
             gst_caps_unref (destcaps);
@@ -2311,8 +2382,9 @@ gst_pad_use_fixed_caps (GstPad * pad)
  * Gets the parent of @pad, cast to a #GstElement. If a @pad has no parent or
  * its parent is not an element, return %NULL.
  *
- * Returns: (transfer full): the parent of the pad. The caller has a
- * reference on the parent, so unref when you're finished with it.
+ * Returns: (transfer full) (nullable): the parent of the pad. The
+ * caller has a reference on the parent, so unref when you're finished
+ * with it.
  *
  * MT safe.
  */
@@ -2957,7 +3029,8 @@ element_find_unlinked_pad (GstElement * element, GstPadDirection direction)
  * owns a reference to it and should use gst_object_unref() on the
  * pad when it is not needed any longer.
  *
- * Returns: (transfer full): unlinked pad of the given direction, %NULL.
+ * Returns: (transfer full) (nullable): unlinked pad of the given
+ * direction, %NULL.
  */
 GstPad *
 gst_bin_find_unlinked_pad (GstBin * bin, GstPadDirection direction)
@@ -3000,6 +3073,58 @@ gst_bin_find_unlinked_pad (GstBin * bin, GstPadDirection direction)
   return pad;
 }
 
+static void
+gst_bin_sync_children_states_foreach (const GValue * value, gpointer user_data)
+{
+  gboolean *success = user_data;
+  GstElement *element = g_value_get_object (value);
+
+  if (gst_element_is_locked_state (element)) {
+    *success = TRUE;
+  } else {
+    *success = *success && gst_element_sync_state_with_parent (element);
+
+    if (GST_IS_BIN (element))
+      *success = *success
+          && gst_bin_sync_children_states (GST_BIN_CAST (element));
+  }
+}
+
+/**
+ * gst_bin_sync_children_states:
+ * @bin: a #GstBin
+ *
+ * Synchronizes the state of every child of @bin with the state
+ * of @bin. See also gst_element_sync_state_with_parent().
+ *
+ * Returns: %TRUE if syncing the state was successful for all children,
+ *  otherwise %FALSE.
+ *
+ * Since: 1.6
+ */
+gboolean
+gst_bin_sync_children_states (GstBin * bin)
+{
+  GstIterator *it;
+  GstIteratorResult res = GST_ITERATOR_OK;
+  gboolean success = TRUE;
+
+  it = gst_bin_iterate_sorted (bin);
+
+  do {
+    if (res == GST_ITERATOR_RESYNC) {
+      success = TRUE;
+      gst_iterator_resync (it);
+    }
+    res =
+        gst_iterator_foreach (it, gst_bin_sync_children_states_foreach,
+        &success);
+  } while (res == GST_ITERATOR_RESYNC);
+  gst_iterator_free (it);
+
+  return success;
+}
+
 /**
  * gst_parse_bin_from_description:
  * @bin_description: command line describing the bin
@@ -3017,8 +3142,8 @@ gst_bin_find_unlinked_pad (GstBin * bin, GstPadDirection direction)
  * and want them all ghosted, you will have to create the ghost pads
  * yourself).
  *
- * Returns: (transfer floating) (type Gst.Bin): a newly-created bin,
- *   or %NULL if an error occurred.
+ * Returns: (transfer floating) (type Gst.Bin) (nullable): a
+ *   newly-created bin, or %NULL if an error occurred.
  */
 GstElement *
 gst_parse_bin_from_description (const gchar * bin_description,
@@ -3159,7 +3284,8 @@ gst_util_get_timestamp (void)
  *
  * The complexity of this search function is O(log (num_elements)).
  *
- * Returns: (transfer none): The address of the found element or %NULL if nothing was found
+ * Returns: (transfer none) (nullable): The address of the found
+ * element or %NULL if nothing was found
  */
 gpointer
 gst_util_array_binary_search (gpointer array, guint num_elements,
@@ -3616,15 +3742,17 @@ gst_pad_create_stream_id_internal (GstPad * pad, GstElement * parent,
    * here is for source elements */
   if (!upstream_stream_id) {
     GstQuery *query;
+    gchar *uri = NULL;
 
     /* Try to generate one from the URI query and
      * if it fails take a random number instead */
     query = gst_query_new_uri ();
     if (gst_element_query (parent, query)) {
-      GChecksum *cs;
-      gchar *uri;
-
       gst_query_parse_uri (query, &uri);
+    }
+
+    if (uri) {
+      GChecksum *cs;
 
       /* And then generate an SHA256 sum of the URI */
       cs = g_checksum_new (G_CHECKSUM_SHA256);
@@ -3778,8 +3906,9 @@ gst_pad_create_stream_id (GstPad * pad, GstElement * parent,
  * The returned stream-id string should be treated as an opaque string, its
  * contents should not be interpreted.
  *
- * Returns: a newly-allocated copy of the stream-id for @pad, or %NULL.
- *     g_free() the returned string when no longer needed.
+ * Returns: (nullable): a newly-allocated copy of the stream-id for
+ *     @pad, or %NULL.  g_free() the returned string when no longer
+ *     needed.
  *
  * Since: 1.2
  */