remove some useless includes in .h
[platform/upstream/gstreamer.git] / gst / gstbufferpool.c
index 4797d38..658c518 100644 (file)
 #endif
 #include <sys/types.h>
 
+#include "gstatomicqueue.h"
+#include "gstpoll.h"
 #include "gstinfo.h"
 #include "gstquark.h"
+#include "gstvalue.h"
 
 #include "gstbufferpool.h"
 
@@ -46,12 +49,23 @@ GST_DEBUG_CATEGORY_STATIC (gst_buffer_pool_debug);
 #define GST_BUFFER_POOL_GET_PRIVATE(obj)  \
    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BUFFER_POOL, GstBufferPoolPrivate))
 
-#define GST_BUFFER_POOL_LOCK(pool)   (g_static_rec_mutex_lock(&pool->priv->rec_lock))
-#define GST_BUFFER_POOL_UNLOCK(pool) (g_static_rec_mutex_unlock(&pool->priv->rec_lock))
+#define GST_BUFFER_POOL_LOCK(pool)   (g_rec_mutex_lock(&pool->priv->rec_lock))
+#define GST_BUFFER_POOL_UNLOCK(pool) (g_rec_mutex_unlock(&pool->priv->rec_lock))
 
 struct _GstBufferPoolPrivate
 {
-  GStaticRecMutex rec_lock;
+  GstAtomicQueue *queue;
+  GstPoll *poll;
+
+  GRecMutex rec_lock;
+
+  gboolean started;
+  gboolean active;
+  gint outstanding;
+
+  gboolean configured;
+  GstStructure *config;
+
   guint size;
   guint min_buffers;
   guint max_buffers;
@@ -109,17 +123,18 @@ gst_buffer_pool_init (GstBufferPool * pool)
 {
   pool->priv = GST_BUFFER_POOL_GET_PRIVATE (pool);
 
-  g_static_rec_mutex_init (&pool->priv->rec_lock);
+  g_rec_mutex_init (&pool->priv->rec_lock);
 
-  pool->poll = gst_poll_new_timer ();
-  pool->queue = gst_atomic_queue_new (10);
-  pool->flushing = TRUE;
-  pool->active = FALSE;
-  pool->configured = FALSE;
-  pool->started = FALSE;
-  pool->config = gst_structure_id_empty_new (GST_QUARK (BUFFER_POOL_CONFIG));
-  gst_buffer_pool_config_set (pool->config, NULL, 0, 0, 0, 0, 0);
-  gst_poll_write_control (pool->poll);
+  pool->priv->poll = gst_poll_new_timer ();
+  pool->priv->queue = gst_atomic_queue_new (10);
+  pool->flushing = 1;
+  pool->priv->active = FALSE;
+  pool->priv->configured = FALSE;
+  pool->priv->started = FALSE;
+  pool->priv->config =
+      gst_structure_new_id_empty (GST_QUARK (BUFFER_POOL_CONFIG));
+  gst_buffer_pool_config_set (pool->priv->config, NULL, 0, 0, 0, 0, 0);
+  gst_poll_write_control (pool->priv->poll);
 
   GST_DEBUG_OBJECT (pool, "created");
 }
@@ -134,10 +149,10 @@ gst_buffer_pool_finalize (GObject * object)
   GST_DEBUG_OBJECT (pool, "finalize");
 
   gst_buffer_pool_set_active (pool, FALSE);
-  gst_atomic_queue_unref (pool->queue);
-  gst_poll_free (pool->poll);
-  gst_structure_free (pool->config);
-  g_static_rec_mutex_free (&pool->priv->rec_lock);
+  gst_atomic_queue_unref (pool->priv->queue);
+  gst_poll_free (pool->priv->poll);
+  gst_structure_free (pool->priv->config);
+  g_rec_mutex_clear (&pool->priv->rec_lock);
 
   G_OBJECT_CLASS (gst_buffer_pool_parent_class)->finalize (object);
 }
@@ -147,7 +162,7 @@ gst_buffer_pool_finalize (GObject * object)
  *
  * Creates a new #GstBufferPool instance.
  *
- * Returns: a new #GstBufferPool instance
+ * Returns: (transfer full): a new #GstBufferPool instance
  */
 GstBufferPool *
 gst_buffer_pool_new (void)
@@ -176,6 +191,14 @@ default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
   return GST_FLOW_OK;
 }
 
+static gboolean
+mark_meta_pooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
+{
+  GST_META_FLAG_SET (*meta, GST_META_FLAG_POOLED);
+
+  return TRUE;
+}
+
 /* the default implementation for preallocating the buffers
  * in the pool */
 static gboolean
@@ -198,6 +221,7 @@ default_start (GstBufferPool * pool)
     if (pclass->alloc_buffer (pool, &buffer, NULL) != GST_FLOW_OK)
       goto alloc_failed;
 
+    gst_buffer_foreach_meta (buffer, mark_meta_pooled, pool);
     GST_LOG_OBJECT (pool, "prealloced buffer %d: %p", i, buffer);
     /* release to the queue, we call the vmethod directly, we don't need to do
      * the other refcount handling right now. */
@@ -223,7 +247,7 @@ alloc_failed:
 static gboolean
 do_start (GstBufferPool * pool)
 {
-  if (!pool->started) {
+  if (!pool->priv->started) {
     GstBufferPoolClass *pclass;
 
     pclass = GST_BUFFER_POOL_GET_CLASS (pool);
@@ -235,7 +259,7 @@ do_start (GstBufferPool * pool)
       if (!pclass->start (pool))
         return FALSE;
     }
-    pool->started = TRUE;
+    pool->priv->started = TRUE;
   }
   return TRUE;
 }
@@ -257,9 +281,9 @@ default_stop (GstBufferPool * pool)
   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
 
   /* clear the pool */
-  while ((buffer = gst_atomic_queue_pop (pool->queue))) {
+  while ((buffer = gst_atomic_queue_pop (pool->priv->queue))) {
     GST_LOG_OBJECT (pool, "freeing %p", buffer);
-    gst_poll_read_control (pool->poll);
+    gst_poll_read_control (pool->priv->poll);
 
     if (G_LIKELY (pclass->free_buffer))
       pclass->free_buffer (pool, buffer);
@@ -271,7 +295,7 @@ default_stop (GstBufferPool * pool)
 static gboolean
 do_stop (GstBufferPool * pool)
 {
-  if (pool->started) {
+  if (pool->priv->started) {
     GstBufferPoolClass *pclass;
 
     pclass = GST_BUFFER_POOL_GET_CLASS (pool);
@@ -281,7 +305,7 @@ do_stop (GstBufferPool * pool)
       if (!pclass->stop (pool))
         return FALSE;
     }
-    pool->started = FALSE;
+    pool->priv->started = FALSE;
   }
   return TRUE;
 }
@@ -292,7 +316,7 @@ do_stop (GstBufferPool * pool)
  * @active: the new active state
  *
  * Control the active state of @pool. When the pool is active, new calls to
- * gst_buffer_pool_acquire_buffer() will return with GST_FLOW_WRONG_STATE.
+ * gst_buffer_pool_acquire_buffer() will return with GST_FLOW_FLUSHING.
  *
  * Activating the bufferpool will preallocate all resources in the pool based on
  * the configuration of the pool.
@@ -315,11 +339,11 @@ gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
 
   GST_BUFFER_POOL_LOCK (pool);
   /* just return if we are already in the right state */
-  if (pool->active == active)
+  if (pool->priv->active == active)
     goto was_ok;
 
   /* we need to be configured */
-  if (!pool->configured)
+  if (!pool->priv->configured)
     goto not_configured;
 
   if (active) {
@@ -327,25 +351,25 @@ gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
       goto start_failed;
 
     /* unset the flushing state now */
-    gst_poll_read_control (pool->poll);
-    g_atomic_int_set (&pool->flushing, FALSE);
+    gst_poll_read_control (pool->priv->poll);
+    g_atomic_int_set (&pool->flushing, 0);
   } else {
     gint outstanding;
 
     /* set to flushing first */
-    g_atomic_int_set (&pool->flushing, TRUE);
-    gst_poll_write_control (pool->poll);
+    g_atomic_int_set (&pool->flushing, 1);
+    gst_poll_write_control (pool->priv->poll);
 
     /* when all buffers are in the pool, free them. Else they will be
      * freed when they are released */
-    outstanding = g_atomic_int_get (&pool->outstanding);
+    outstanding = g_atomic_int_get (&pool->priv->outstanding);
     GST_LOG_OBJECT (pool, "outstanding buffers %d", outstanding);
     if (outstanding == 0) {
       if (!do_stop (pool))
         goto stop_failed;
     }
   }
-  pool->active = active;
+  pool->priv->active = active;
   GST_BUFFER_POOL_UNLOCK (pool);
 
   return res;
@@ -391,7 +415,7 @@ gst_buffer_pool_is_active (GstBufferPool * pool)
   gboolean res;
 
   GST_BUFFER_POOL_LOCK (pool);
-  res = pool->active;
+  res = pool->priv->active;
   GST_BUFFER_POOL_UNLOCK (pool);
 
   return res;
@@ -430,7 +454,7 @@ wrong_config:
 /**
  * gst_buffer_pool_set_config:
  * @pool: a #GstBufferPool
- * @config: a #GstStructure
+ * @config: (transfer full): a #GstStructure
  *
  * Set the configuration of the pool. The pool must be inactive and all buffers
  * allocated form this pool must be returned or else this function will do
@@ -453,11 +477,11 @@ gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
 
   GST_BUFFER_POOL_LOCK (pool);
   /* can't change the settings when active */
-  if (pool->active)
+  if (pool->priv->active)
     goto was_active;
 
   /* we can't change when outstanding buffers */
-  if (g_atomic_int_get (&pool->outstanding) != 0)
+  if (g_atomic_int_get (&pool->priv->outstanding) != 0)
     goto have_outstanding;
 
   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
@@ -469,12 +493,12 @@ gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
     result = FALSE;
 
   if (result) {
-    if (pool->config)
-      gst_structure_free (pool->config);
-    pool->config = config;
+    if (pool->priv->config)
+      gst_structure_free (pool->priv->config);
+    pool->priv->config = config;
 
     /* now we are configured */
-    pool->configured = TRUE;
+    pool->priv->configured = TRUE;
   }
   GST_BUFFER_POOL_UNLOCK (pool);
 
@@ -503,7 +527,7 @@ have_outstanding:
  * can either be modified and used for the gst_buffer_pool_set_config() call
  * or it must be freed after usage.
  *
- * Returns: a copy of the current configuration of @pool. use
+ * Returns: (transfer full): a copy of the current configuration of @pool. use
  * gst_structure_free() after usage or gst_buffer_pool_set_config().
  */
 GstStructure *
@@ -514,7 +538,7 @@ gst_buffer_pool_get_config (GstBufferPool * pool)
   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
 
   GST_BUFFER_POOL_UNLOCK (pool);
-  result = gst_structure_copy (pool->config);
+  result = gst_structure_copy (pool->priv->config);
   GST_BUFFER_POOL_UNLOCK (pool);
 
   return result;
@@ -625,34 +649,31 @@ gst_buffer_pool_config_set (GstStructure * config, const GstCaps * caps,
 void
 gst_buffer_pool_config_add_option (GstStructure * config, const gchar * option)
 {
-  GValueArray *array;
   const GValue *value;
-  GValue option_value = { 0 };
-  gint i;
+  GValue option_value = { 0, };
+  guint i, len;
 
   g_return_if_fail (config != NULL);
 
   value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
   if (value) {
-    array = (GValueArray *) g_value_get_boxed (value);
+    len = gst_value_array_get_size (value);
+    for (i = 0; i < len; ++i) {
+      const GValue *nth_val = gst_value_array_get_value (value, i);
+
+      if (g_str_equal (option, g_value_get_string (nth_val)))
+        return;
+    }
   } else {
     GValue new_array_val = { 0, };
 
-    array = g_value_array_new (0);
-
-    g_value_init (&new_array_val, G_TYPE_VALUE_ARRAY);
-    g_value_take_boxed (&new_array_val, array);
-
+    g_value_init (&new_array_val, GST_TYPE_ARRAY);
     gst_structure_id_take_value (config, GST_QUARK (OPTIONS), &new_array_val);
-  }
-  for (i = 0; i < array->n_values; i++) {
-    value = g_value_array_get_nth (array, i);
-    if (g_str_equal (option, g_value_get_string (value)))
-      return;
+    value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
   }
   g_value_init (&option_value, G_TYPE_STRING);
   g_value_set_string (&option_value, option);
-  g_value_array_append (array, &option_value);
+  gst_value_array_append_value ((GValue *) value, &option_value);
   g_value_unset (&option_value);
 }
 
@@ -668,7 +689,6 @@ gst_buffer_pool_config_add_option (GstStructure * config, const gchar * option)
 guint
 gst_buffer_pool_config_n_options (GstStructure * config)
 {
-  GValueArray *array;
   const GValue *value;
   guint size = 0;
 
@@ -676,8 +696,7 @@ gst_buffer_pool_config_n_options (GstStructure * config)
 
   value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
   if (value) {
-    array = (GValueArray *) g_value_get_boxed (value);
-    size = array->n_values;
+    size = gst_value_array_get_size (value);
   }
   return size;
 }
@@ -702,12 +721,9 @@ gst_buffer_pool_config_get_option (GstStructure * config, guint index)
 
   value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
   if (value) {
-    GValueArray *array;
-    GValue *option_value;
-
-    array = (GValueArray *) g_value_get_boxed (value);
-    option_value = g_value_array_get_nth (array, index);
+    const GValue *option_value;
 
+    option_value = gst_value_array_get_value (value, index);
     if (option_value)
       ret = g_value_get_string (option_value);
   }
@@ -727,19 +743,17 @@ gboolean
 gst_buffer_pool_config_has_option (GstStructure * config, const gchar * option)
 {
   const GValue *value;
+  guint i, len;
 
   g_return_val_if_fail (config != NULL, 0);
 
   value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
   if (value) {
-    GValueArray *array;
-    GValue *option_value;
-    gint i;
-
-    array = (GValueArray *) g_value_get_boxed (value);
-    for (i = 0; i < array->n_values; i++) {
-      option_value = g_value_array_get_nth (array, i);
-      if (g_str_equal (option, g_value_get_string (option_value)))
+    len = gst_value_array_get_size (value);
+    for (i = 0; i < len; ++i) {
+      const GValue *nth_val = gst_value_array_get_value (value, i);
+
+      if (g_str_equal (option, g_value_get_string (nth_val)))
         return TRUE;
     }
   }
@@ -748,13 +762,13 @@ gst_buffer_pool_config_has_option (GstStructure * config, const gchar * option)
 
 /**
  * gst_buffer_pool_config_get:
- * @config: a #GstBufferPool configuration
- * @caps: the caps of buffers
- * @size: the size of each buffer, not including prefix
- * @min_buffers: the minimum amount of buffers to allocate.
- * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
- * @prefix: prefix each buffer with this many bytes
- * @align: alignment of the buffer data.
+ * @config: (transfer none): a #GstBufferPool configuration
+ * @caps: (out): the caps of buffers
+ * @size: (out): the size of each buffer, not including prefix
+ * @min_buffers: (out): the minimum amount of buffers to allocate.
+ * @max_buffers: (out): the maximum amount of buffers to allocate or 0 for unlimited.
+ * @prefix: (out): prefix each buffer with this many bytes
+ * @align: (out): alignment of the buffer data.
  *
  * Get the configuration values from @config.
  */
@@ -785,13 +799,13 @@ default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
 
   while (TRUE) {
-    if (G_UNLIKELY (g_atomic_int_get (&pool->flushing)))
+    if (G_UNLIKELY (GST_BUFFER_POOL_IS_FLUSHING (pool)))
       goto flushing;
 
     /* try to get a buffer from the queue */
-    *buffer = gst_atomic_queue_pop (pool->queue);
+    *buffer = gst_atomic_queue_pop (pool->priv->queue);
     if (G_LIKELY (*buffer)) {
-      gst_poll_read_control (pool->poll);
+      gst_poll_read_control (pool->priv->poll);
       result = GST_FLOW_OK;
       GST_LOG_OBJECT (pool, "acquired buffer %p", *buffer);
       break;
@@ -802,6 +816,10 @@ default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
       /* no max_buffers, we allocate some more */
       if (G_LIKELY (pclass->alloc_buffer)) {
         result = pclass->alloc_buffer (pool, buffer, params);
+        if (result == GST_FLOW_OK && *buffer)
+          gst_buffer_foreach_meta (*buffer, mark_meta_pooled, pool);
+        else
+          result = GST_FLOW_ERROR;
       } else
         result = GST_FLOW_NOT_SUPPORTED;
       GST_LOG_OBJECT (pool, "alloc buffer %p", *buffer);
@@ -817,7 +835,7 @@ default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
 
     /* now wait */
     GST_LOG_OBJECT (pool, "waiting for free buffers");
-    gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
+    gst_poll_wait (pool->priv->poll, GST_CLOCK_TIME_NONE);
   }
 
   return result;
@@ -826,21 +844,21 @@ default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
 flushing:
   {
     GST_DEBUG_OBJECT (pool, "we are flushing");
-    return GST_FLOW_WRONG_STATE;
+    return GST_FLOW_FLUSHING;
   }
 }
 
 static inline void
 dec_outstanding (GstBufferPool * pool)
 {
-  if (g_atomic_int_dec_and_test (&pool->outstanding)) {
+  if (g_atomic_int_dec_and_test (&pool->priv->outstanding)) {
     /* all buffers are returned to the pool, see if we need to free them */
-    if (g_atomic_int_get (&pool->flushing)) {
+    if (GST_BUFFER_POOL_IS_FLUSHING (pool)) {
       /* take the lock so that set_active is not run concurrently */
       GST_BUFFER_POOL_LOCK (pool);
       /* recheck the flushing state in the lock, the pool could have been
        * set to active again */
-      if (g_atomic_int_get (&pool->flushing))
+      if (GST_BUFFER_POOL_IS_FLUSHING (pool))
         do_stop (pool);
 
       GST_BUFFER_POOL_UNLOCK (pool);
@@ -848,6 +866,14 @@ dec_outstanding (GstBufferPool * pool)
   }
 }
 
+static gboolean
+remove_meta_unpooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
+{
+  if (!GST_META_FLAG_IS_SET (*meta, GST_META_FLAG_POOLED))
+    *meta = NULL;
+  return TRUE;
+}
+
 static void
 default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer,
     GstBufferPoolParams * params)
@@ -859,20 +885,23 @@ default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer,
   GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
   GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
   GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
+
+  /* remove all metadata without the POOLED flag */
+  gst_buffer_foreach_meta (buffer, remove_meta_unpooled, pool);
 }
 
 /**
  * gst_buffer_pool_acquire_buffer:
  * @pool: a #GstBufferPool
- * @buffer: a location for a #GstBuffer
- * @params: parameters.
+ * @buffer: (out): a location for a #GstBuffer
+ * @params: (transfer none) (allow-none) parameters.
  *
  * Acquire a buffer from @pool. @buffer should point to a memory location that
  * can hold a pointer to the new buffer.
  *
  * @params can be NULL or contain optional parameters to influence the allocation.
  *
- * Returns: a #GstFlowReturn such as GST_FLOW_WRONG_STATE when the pool is
+ * Returns: a #GstFlowReturn such as GST_FLOW_FLUSHING when the pool is
  * inactive.
  */
 GstFlowReturn
@@ -889,7 +918,7 @@ gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
 
   /* assume we'll have one more outstanding buffer we need to do that so
    * that concurrent set_active doesn't clear the buffers */
-  g_atomic_int_inc (&pool->outstanding);
+  g_atomic_int_inc (&pool->priv->outstanding);
 
   if (G_LIKELY (pclass->acquire_buffer))
     result = pclass->acquire_buffer (pool, buffer, params);
@@ -915,14 +944,14 @@ default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
 {
   /* keep it around in our queue */
   GST_LOG_OBJECT (pool, "released buffer %p", buffer);
-  gst_atomic_queue_push (pool->queue, buffer);
-  gst_poll_write_control (pool->poll);
+  gst_atomic_queue_push (pool->priv->queue, buffer);
+  gst_poll_write_control (pool->priv->poll);
 }
 
 /**
  * gst_buffer_pool_release_buffer:
  * @pool: a #GstBufferPool
- * @buffer: a #GstBuffer
+ * @buffer: (transfer none): a #GstBuffer
  *
  * Release @buffer to @pool. @buffer should have previously been allocated from
  * @pool with gst_buffer_pool_acquire_buffer().
@@ -940,7 +969,7 @@ gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
 
   /* check that the buffer is ours, all buffers returned to the pool have the
    * pool member set to NULL and the pool refcount decreased */
-  if (!G_ATOMIC_POINTER_COMPARE_AND_EXCHANGE (&buffer->pool, pool, NULL))
+  if (!g_atomic_pointer_compare_and_exchange (&buffer->pool, pool, NULL))
     return;
 
   pclass = GST_BUFFER_POOL_GET_CLASS (pool);