tee: First deactivate the pad and then remove it when releasing pads
[platform/upstream/gstreamer.git] / plugins / elements / gsttee.c
index 906775d..0ff7527 100644 (file)
@@ -23,6 +23,7 @@
 
 /**
  * SECTION:element-tee
+ * @title: tee
  * @see_also: #GstIdentity
  *
  * Split data to multiple pads. Branching the data flow is useful when e.g.
  * provide separate threads for each branch. Otherwise a blocked dataflow in one
  * branch would stall the other branches.
  *
- * <refsect2>
- * <title>Example launch line</title>
+ * ## Example launch line
  * |[
  * gst-launch-1.0 filesrc location=song.ogg ! decodebin ! tee name=t ! queue ! audioconvert ! audioresample ! autoaudiosink t. ! queue ! audioconvert ! goom ! videoconvert ! autovideosink
- * ]| Play song.ogg audio file which must be in the current working directory
+ * ]|
+ *
+ * Play song.ogg audio file which must be in the current working directory
  * and render visualisations using the goom element (this can be easier done
  * using the playbin element, this is just an example pipeline).
- * </refsect2>
  */
 
 #ifdef HAVE_CONFIG_H
@@ -262,8 +263,9 @@ gst_tee_class_init (GstTeeClass * klass)
           DEFAULT_PULL_MODE,
           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   pspec_alloc_pad = g_param_spec_object ("alloc-pad", "Allocation Src Pad",
-      "The pad ALLOCATION queries will be proxied to (unused)", GST_TYPE_PAD,
-      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
+      "The pad ALLOCATION queries will be proxied to (DEPRECATED, has no effect)",
+      GST_TYPE_PAD,
+      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED);
   g_object_class_install_property (gobject_class, PROP_ALLOC_PAD,
       pspec_alloc_pad);
 
@@ -458,12 +460,8 @@ gst_tee_release_pad (GstElement * element, GstPad * pad)
   }
   GST_OBJECT_UNLOCK (tee);
 
-  gst_object_ref (pad);
-  gst_element_remove_pad (GST_ELEMENT_CAST (tee), pad);
-
   gst_pad_set_active (pad, FALSE);
-
-  gst_object_unref (pad);
+  gst_element_remove_pad (GST_ELEMENT_CAST (tee), pad);
 
   if (changed) {
     gst_tee_notify_alloc_pad (tee);
@@ -563,12 +561,253 @@ gst_tee_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
   return res;
 }
 
+struct AllocQueryCtx
+{
+  GstTee *tee;
+  GstQuery *query;
+  GstAllocationParams params;
+  guint size;
+  guint min_buffers;
+  gboolean first_query;
+  guint num_pads;
+};
+
+/* This function will aggregate some of the allocation query information with
+ * the strategy to force upstream allocation. Depending on downstream
+ * allocation would otherwise make dynamic pipelines much more complicated as
+ * application would need to now drain buffer in certain cases before getting
+ * rid of a tee branch. */
+static gboolean
+gst_tee_query_allocation (const GValue * item, GValue * ret, gpointer user_data)
+{
+  struct AllocQueryCtx *ctx = user_data;
+  GstPad *src_pad = g_value_get_object (item);
+  GstPad *peer_pad;
+  GstCaps *caps;
+  GstQuery *query;
+  guint count, i, size, min;
+
+  GST_DEBUG_OBJECT (ctx->tee, "Aggregating allocation from pad %s:%s",
+      GST_DEBUG_PAD_NAME (src_pad));
+
+  peer_pad = gst_pad_get_peer (src_pad);
+  if (!peer_pad) {
+    if (ctx->tee->allow_not_linked) {
+      GST_DEBUG_OBJECT (ctx->tee, "Pad %s:%s has no peer, but allowed.",
+          GST_DEBUG_PAD_NAME (src_pad));
+      return TRUE;
+    } else {
+      GST_DEBUG_OBJECT (ctx->tee, "Pad %s:%s has no peer, ignoring allocation.",
+          GST_DEBUG_PAD_NAME (src_pad));
+      g_value_set_boolean (ret, FALSE);
+      return FALSE;
+    }
+  }
+
+  gst_query_parse_allocation (ctx->query, &caps, NULL);
+
+  query = gst_query_new_allocation (caps, FALSE);
+  if (!gst_pad_query (peer_pad, query)) {
+    GST_DEBUG_OBJECT (ctx->tee,
+        "Allocation query failed on pad %s, ignoring allocation",
+        GST_PAD_NAME (src_pad));
+    g_value_set_boolean (ret, FALSE);
+    gst_query_unref (query);
+    gst_object_unref (peer_pad);
+    return FALSE;
+  }
+
+  gst_object_unref (peer_pad);
+
+  /* Allocation Params:
+   * store the maximum alignment, prefix and padding, but ignore the
+   * allocators and the flags which are tied to downstream allocation */
+  count = gst_query_get_n_allocation_params (query);
+  for (i = 0; i < count; i++) {
+    GstAllocationParams params = { 0, };
+
+    gst_query_parse_nth_allocation_param (query, i, NULL, &params);
+
+    GST_DEBUG_OBJECT (ctx->tee, "Aggregating AllocationParams align=%"
+        G_GSIZE_FORMAT " prefix=%" G_GSIZE_FORMAT " padding=%"
+        G_GSIZE_FORMAT, params.align, params.prefix, params.padding);
+
+    if (ctx->params.align < params.align)
+      ctx->params.align = params.align;
+
+    if (ctx->params.prefix < params.prefix)
+      ctx->params.prefix = params.prefix;
+
+    if (ctx->params.padding < params.padding)
+      ctx->params.padding = params.padding;
+  }
+
+  /* Allocation Pool:
+   * We want to keep the biggest size and biggest minimum number of buffers to
+   * make sure downstream requirement can be satisfied. We don't really care
+   * about the maximum, as this is a parameter of the downstream provided
+   * pool. We only read the first allocation pool as the minimum number of
+   * buffers is normally constant regardless of the pool being used. */
+  if (gst_query_get_n_allocation_pools (query) > 0) {
+    gst_query_parse_nth_allocation_pool (query, 0, NULL, &size, &min, NULL);
+
+    GST_DEBUG_OBJECT (ctx->tee,
+        "Aggregating allocation pool size=%u min_buffers=%u", size, min);
+
+    if (ctx->size < size)
+      ctx->size = size;
+
+    if (ctx->min_buffers < min)
+      ctx->min_buffers = min;
+  }
+
+  /* Allocation Meta:
+   * For allocation meta, we'll need to aggregate the argument using the new
+   * GstMetaInfo::agggregate_func */
+  count = gst_query_get_n_allocation_metas (query);
+  for (i = 0; i < count; i++) {
+    guint ctx_index;
+    GType api;
+    const GstStructure *param;
+
+    api = gst_query_parse_nth_allocation_meta (query, i, &param);
+
+    /* For the first query, copy all metas */
+    if (ctx->first_query) {
+      gst_query_add_allocation_meta (ctx->query, api, param);
+      continue;
+    }
+
+    /* Afterward, aggregate the common params */
+    if (gst_query_find_allocation_meta (ctx->query, api, &ctx_index)) {
+      const GstStructure *ctx_param;
+
+      gst_query_parse_nth_allocation_meta (ctx->query, ctx_index, &ctx_param);
+
+      /* Keep meta which has no params */
+      if (ctx_param == NULL && param == NULL)
+        continue;
+
+      GST_DEBUG_OBJECT (ctx->tee, "Dropping allocation meta %s",
+          g_type_name (api));
+      gst_query_remove_nth_allocation_meta (ctx->query, ctx_index);
+    }
+  }
+
+  /* Finally, cleanup metas from the stored query that aren't support on this
+   * pad. */
+  count = gst_query_get_n_allocation_metas (ctx->query);
+  for (i = 0; i < count;) {
+    GType api = gst_query_parse_nth_allocation_meta (ctx->query, i, NULL);
+
+    if (!gst_query_find_allocation_meta (query, api, NULL)) {
+      GST_DEBUG_OBJECT (ctx->tee, "Dropping allocation meta %s",
+          g_type_name (api));
+      gst_query_remove_nth_allocation_meta (ctx->query, i);
+      count--;
+      continue;
+    }
+
+    i++;
+  }
+
+  ctx->first_query = FALSE;
+  ctx->num_pads++;
+  gst_query_unref (query);
+
+  return TRUE;
+}
+
+
+static void
+gst_tee_clear_query_allocation_meta (GstQuery * query)
+{
+  guint count = gst_query_get_n_allocation_metas (query);
+  guint i;
+
+  for (i = 1; i <= count; i++)
+    gst_query_remove_nth_allocation_meta (query, count - i);
+}
+
 static gboolean
 gst_tee_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
 {
+  GstTee *tee = GST_TEE (parent);
   gboolean res;
 
   switch (GST_QUERY_TYPE (query)) {
+    case GST_QUERY_ALLOCATION:
+    {
+      GstIterator *iter;
+      GValue ret = G_VALUE_INIT;
+      struct AllocQueryCtx ctx = { tee, query, };
+
+      g_value_init (&ret, G_TYPE_BOOLEAN);
+      g_value_set_boolean (&ret, TRUE);
+
+      ctx.first_query = TRUE;
+      gst_allocation_params_init (&ctx.params);
+
+      iter = gst_element_iterate_src_pads (GST_ELEMENT (tee));
+      while (GST_ITERATOR_RESYNC ==
+          gst_iterator_fold (iter, gst_tee_query_allocation, &ret, &ctx)) {
+        gst_iterator_resync (iter);
+        ctx.first_query = TRUE;
+        gst_allocation_params_init (&ctx.params);
+        ctx.size = 0;
+        ctx.min_buffers = 0;
+        ctx.num_pads = 0;
+        gst_tee_clear_query_allocation_meta (query);
+      }
+
+      gst_iterator_free (iter);
+      res = g_value_get_boolean (&ret);
+      g_value_unset (&ret);
+
+      if (res) {
+        GST_DEBUG_OBJECT (tee, "Aggregated AllocationParams to align=%"
+            G_GSIZE_FORMAT " prefix=%" G_GSIZE_FORMAT " padding=%"
+            G_GSIZE_FORMAT, ctx.params.align, ctx.params.prefix,
+            ctx.params.padding);
+
+        GST_DEBUG_OBJECT (tee,
+            "Aggregated allocation pools size=%u min_buffers=%u", ctx.size,
+            ctx.min_buffers);
+
+#ifndef GST_DISABLE_GST_DEBUG
+        {
+          guint count = gst_query_get_n_allocation_metas (query);
+          guint i;
+
+          GST_DEBUG_OBJECT (tee, "Aggregated %u allocation meta:", count);
+
+          for (i = 0; i < count; i++)
+            GST_DEBUG_OBJECT (tee, "    + aggregated allocation meta %s",
+                g_type_name (gst_query_parse_nth_allocation_meta (ctx.query, i,
+                        NULL)));
+        }
+#endif
+
+        /* Allocate one more buffers when multiplexing so we don't starve the
+         * downstream threads. */
+        if (ctx.num_pads > 1)
+          ctx.min_buffers++;
+
+        /* Check that we actually have parameters besides the defaults. */
+        if (ctx.params.align || ctx.params.prefix || ctx.params.padding) {
+          gst_query_add_allocation_param (ctx.query, NULL, &ctx.params);
+        }
+        /* When size == 0, buffers created from this pool would have no memory
+         * allocated. */
+        if (ctx.size) {
+          gst_query_add_allocation_pool (ctx.query, NULL, ctx.size,
+              ctx.min_buffers, 0);
+        }
+      } else {
+        gst_tee_clear_query_allocation_meta (query);
+      }
+      break;
+    }
     default:
       res = gst_pad_query_default (pad, parent, query);
       break;
@@ -656,12 +895,14 @@ gst_tee_handle_data (GstTee * tee, gpointer data, gboolean is_list)
       ret = gst_pad_push (pad, GST_BUFFER_CAST (data));
     }
 
+    GST_OBJECT_LOCK (tee);
     if (GST_TEE_PAD_CAST (pad)->removed)
       ret = GST_FLOW_NOT_LINKED;
 
     if (ret == GST_FLOW_NOT_LINKED && tee->allow_not_linked) {
       ret = GST_FLOW_OK;
     }
+    GST_OBJECT_UNLOCK (tee);
 
     gst_object_unref (pad);
 
@@ -700,6 +941,8 @@ restart:
 
       GST_OBJECT_LOCK (tee);
       /* keep track of which pad we pushed and the result value */
+      if (GST_TEE_PAD_CAST (pad)->removed)
+        ret = GST_FLOW_NOT_LINKED;
       GST_TEE_PAD_CAST (pad)->pushed = TRUE;
       GST_TEE_PAD_CAST (pad)->result = ret;
       gst_object_unref (pad);
@@ -971,8 +1214,10 @@ gst_tee_pull_eos (GstTee * tee)
   GstIterator *iter;
 
   iter = gst_element_iterate_src_pads (GST_ELEMENT (tee));
-  gst_iterator_foreach (iter, (GstIteratorForeachFunction) gst_tee_push_eos,
-      tee);
+  while (gst_iterator_foreach (iter,
+          (GstIteratorForeachFunction) gst_tee_push_eos,
+          tee) == GST_ITERATOR_RESYNC)
+    gst_iterator_resync (iter);
   gst_iterator_free (iter);
 }