aggregator: Assert if the sink/src pad type that is to be used is not a GstAggregator...
[platform/upstream/gstreamer.git] / libs / gst / base / gstflowcombiner.c
index e0efa7a..8b64a46 100644 (file)
@@ -23,6 +23,7 @@
 
 /**
  * SECTION:gstflowcombiner
+ * @title: GstFlowCombiner
  * @short_description: Utility to combine multiple flow returns into one
  *
  * Utility struct to help handling #GstFlowReturn combination. Useful for
@@ -41,9 +42,7 @@
  * Please be aware that this struct isn't thread safe as its designed to be
  *  used by demuxers, those usually will have a single thread operating it.
  *
- * None of these functions will take refs on the passed #GstPad<!-- -->s, it
- * is the caller's responsibility to make sure that the #GstPad exists as long
- * as this struct exists.
+ * These functions will take refs on the passed #GstPad<!-- -->s.
  *
  * Aside from reducing the user's code size, the main advantage of using this
  * helper struct is to follow the standard rules for #GstFlowReturn combination.
  * * %GST_FLOW_OK: otherwise
  *
  * %GST_FLOW_ERROR or below, GST_FLOW_NOT_NEGOTIATED and GST_FLOW_FLUSHING are
- * returned immediatelly from the gst_flow_combiner_update_flow() function.
+ * returned immediately from the gst_flow_combiner_update_flow() function.
  *
  * Since: 1.4
  */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 
 #include <gst/gst.h>
 #include "gstflowcombiner.h"
@@ -70,8 +72,18 @@ struct _GstFlowCombiner
   GQueue pads;
 
   GstFlowReturn last_ret;
+  volatile gint ref_count;
 };
 
+GST_DEBUG_CATEGORY_STATIC (flowcombiner_dbg);
+#define GST_CAT_DEFAULT flowcombiner_dbg
+
+G_DEFINE_BOXED_TYPE_WITH_CODE (GstFlowCombiner, gst_flow_combiner,
+    (GBoxedCopyFunc) gst_flow_combiner_ref,
+    (GBoxedFreeFunc) gst_flow_combiner_unref,
+    GST_DEBUG_CATEGORY_INIT (flowcombiner_dbg, "flowcombiner", 0,
+        "Flow Combiner"));
+
 /**
  * gst_flow_combiner_new:
  *
@@ -87,6 +99,10 @@ gst_flow_combiner_new (void)
 
   g_queue_init (&combiner->pads);
   combiner->last_ret = GST_FLOW_OK;
+  combiner->ref_count = 1;
+
+  /* Make sure debug category is initialised */
+  gst_flow_combiner_get_type ();
 
   return combiner;
 }
@@ -102,10 +118,95 @@ gst_flow_combiner_new (void)
 void
 gst_flow_combiner_free (GstFlowCombiner * combiner)
 {
+  gst_flow_combiner_unref (combiner);
+}
+
+/**
+ * gst_flow_combiner_ref:
+ * @combiner: the #GstFlowCombiner to add a reference to.
+ *
+ * Increments the reference count on the #GstFlowCombiner.
+ *
+ * Returns: the #GstFlowCombiner.
+ *
+ * Since: 1.12.1
+ */
+GstFlowCombiner *
+gst_flow_combiner_ref (GstFlowCombiner * combiner)
+{
+  g_return_val_if_fail (combiner != NULL, NULL);
+
+  g_atomic_int_inc (&combiner->ref_count);
+
+  return combiner;
+}
+
+/**
+ * gst_flow_combiner_unref:
+ * @combiner: the #GstFlowCombiner to unreference.
+ *
+ * Decrements the reference count on the #GstFlowCombiner.
+ *
+ * Since: 1.12.1
+ */
+void
+gst_flow_combiner_unref (GstFlowCombiner * combiner)
+{
+  g_return_if_fail (combiner != NULL);
+  g_return_if_fail (combiner->ref_count > 0);
+
+  if (g_atomic_int_dec_and_test (&combiner->ref_count)) {
+    GstPad *pad;
+
+    while ((pad = g_queue_pop_head (&combiner->pads)))
+      gst_object_unref (pad);
+
+    g_slice_free (GstFlowCombiner, combiner);
+  }
+}
+
+/**
+ * gst_flow_combiner_clear:
+ * @combiner: the #GstFlowCombiner to clear
+ *
+ * Removes all pads from a #GstFlowCombiner and resets it to its initial state.
+ *
+ * Since: 1.6
+ */
+void
+gst_flow_combiner_clear (GstFlowCombiner * combiner)
+{
+  GstPad *pad;
+
+  g_return_if_fail (combiner != NULL);
+
+  while ((pad = g_queue_pop_head (&combiner->pads)))
+    gst_object_unref (pad);
+  combiner->last_ret = GST_FLOW_OK;
+}
+
+/**
+ * gst_flow_combiner_reset:
+ * @combiner: the #GstFlowCombiner to clear
+ *
+ * Reset flow combiner and all pads to their initial state without removing pads.
+ *
+ * Since: 1.6
+ */
+void
+gst_flow_combiner_reset (GstFlowCombiner * combiner)
+{
+  GList *iter;
+
   g_return_if_fail (combiner != NULL);
 
-  g_queue_clear (&combiner->pads);
-  g_slice_free (GstFlowCombiner, combiner);
+  GST_DEBUG ("Reset flow returns");
+
+  for (iter = combiner->pads.head; iter; iter = iter->next) {
+    GST_PAD_LAST_FLOW_RETURN (iter->data) = GST_FLOW_OK;
+  }
+
+  combiner->last_ret = GST_FLOW_OK;
 }
 
 static GstFlowReturn
@@ -150,7 +251,7 @@ done:
  *
  * Computes the combined flow return for the pads in it.
  *
- * The #GstFlowReturn paramter should be the last flow return update for a pad
+ * The #GstFlowReturn parameter should be the last flow return update for a pad
  * in this #GstFlowCombiner. It will use this value to be able to shortcut some
  * combinations and avoid looking over all pads again. e.g. The last combined
  * return is the same as the latest obtained #GstFlowReturn.
@@ -179,9 +280,37 @@ gst_flow_combiner_update_flow (GstFlowCombiner * combiner, GstFlowReturn fret)
 }
 
 /**
+ * gst_flow_combiner_update_pad_flow:
+ * @combiner: the #GstFlowCombiner
+ * @pad: the #GstPad whose #GstFlowReturn to update
+ * @fret: the latest #GstFlowReturn received for a pad in this #GstFlowCombiner
+ *
+ * Sets the provided pad's last flow return to provided value and computes
+ * the combined flow return for the pads in it.
+ *
+ * The #GstFlowReturn parameter should be the last flow return update for a pad
+ * in this #GstFlowCombiner. It will use this value to be able to shortcut some
+ * combinations and avoid looking over all pads again. e.g. The last combined
+ * return is the same as the latest obtained #GstFlowReturn.
+ *
+ * Returns: The combined #GstFlowReturn
+ * Since: 1.6
+ */
+GstFlowReturn
+gst_flow_combiner_update_pad_flow (GstFlowCombiner * combiner, GstPad * pad,
+    GstFlowReturn fret)
+{
+  g_return_val_if_fail (pad != NULL, GST_FLOW_ERROR);
+
+  GST_PAD_LAST_FLOW_RETURN (pad) = fret;
+
+  return gst_flow_combiner_update_flow (combiner, fret);
+}
+
+/**
  * gst_flow_combiner_add_pad:
  * @combiner: the #GstFlowCombiner
- * @pad: (transfer-none): the #GstPad that is being added
+ * @pad: (transfer none): the #GstPad that is being added
  *
  * Adds a new #GstPad to the #GstFlowCombiner.
  *
@@ -193,13 +322,13 @@ gst_flow_combiner_add_pad (GstFlowCombiner * combiner, GstPad * pad)
   g_return_if_fail (combiner != NULL);
   g_return_if_fail (pad != NULL);
 
-  g_queue_push_head (&combiner->pads, pad);
+  g_queue_push_head (&combiner->pads, gst_object_ref (pad));
 }
 
 /**
  * gst_flow_combiner_remove_pad:
  * @combiner: the #GstFlowCombiner
- * @pad: (transfer-none): the #GstPad to remove
+ * @pad: (transfer none): the #GstPad to remove
  *
  * Removes a #GstPad from the #GstFlowCombiner.
  *
@@ -211,5 +340,6 @@ gst_flow_combiner_remove_pad (GstFlowCombiner * combiner, GstPad * pad)
   g_return_if_fail (combiner != NULL);
   g_return_if_fail (pad != NULL);
 
-  g_queue_remove (&combiner->pads, pad);
+  if (g_queue_remove (&combiner->pads, pad))
+    gst_object_unref (pad);
 }