Merge remote-tracking branch 'origin/master' into 0.11
[platform/upstream/gstreamer.git] / gst / gstbufferpool.c
index a43b426..66b6e05 100644 (file)
@@ -27,6 +27,7 @@
  */
 
 #include "gst_private.h"
+#include "glib-compat-private.h"
 
 #include <errno.h>
 #ifdef HAVE_UNISTD_H
@@ -55,7 +56,6 @@ struct _GstBufferPoolPrivate
   guint min_buffers;
   guint max_buffers;
   guint prefix;
-  guint postfix;
   guint align;
 };
 
@@ -77,6 +77,8 @@ static GstFlowReturn default_alloc_buffer (GstBufferPool * pool,
     GstBuffer ** buffer, GstBufferPoolParams * params);
 static GstFlowReturn default_acquire_buffer (GstBufferPool * pool,
     GstBuffer ** buffer, GstBufferPoolParams * params);
+static void default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer,
+    GstBufferPoolParams * params);
 static void default_free_buffer (GstBufferPool * pool, GstBuffer * buffer);
 static void default_release_buffer (GstBufferPool * pool, GstBuffer * buffer);
 
@@ -93,6 +95,7 @@ gst_buffer_pool_class_init (GstBufferPoolClass * klass)
   klass->stop = default_stop;
   klass->set_config = default_set_config;
   klass->acquire_buffer = default_acquire_buffer;
+  klass->reset_buffer = default_reset_buffer;
   klass->alloc_buffer = default_alloc_buffer;
   klass->release_buffer = default_release_buffer;
   klass->free_buffer = default_free_buffer;
@@ -114,8 +117,9 @@ gst_buffer_pool_init (GstBufferPool * pool)
   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, 1);
+  pool->config = gst_structure_new_id_empty (GST_QUARK (BUFFER_POOL_CONFIG));
+  gst_buffer_pool_config_set (pool->config, NULL, 0, 0, 0, 0, 0);
+  gst_poll_write_control (pool->poll);
 
   GST_DEBUG_OBJECT (pool, "created");
 }
@@ -143,7 +147,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)
@@ -160,15 +164,14 @@ static GstFlowReturn
 default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
     GstBufferPoolParams * params)
 {
-  guint align;
   GstBufferPoolPrivate *priv = pool->priv;
+  GstMemory *mem;
 
   *buffer = gst_buffer_new ();
 
-  align = priv->align - 1;
-
-  gst_buffer_take_memory (*buffer, gst_memory_allocator_alloc (NULL, priv->size,
-          align));
+  mem = gst_allocator_alloc (NULL, priv->size + priv->prefix, priv->align);
+  gst_memory_resize (mem, priv->prefix, priv->size);
+  gst_buffer_take_memory (*buffer, -1, mem);
 
   return GST_FLOW_OK;
 }
@@ -196,9 +199,10 @@ default_start (GstBufferPool * pool)
       goto alloc_failed;
 
     GST_LOG_OBJECT (pool, "prealloced buffer %d: %p", i, buffer);
-    /* store in the queue */
-    gst_atomic_queue_push (pool->queue, buffer);
-    gst_poll_write_control (pool->poll);
+    /* release to the queue, we call the vmethod directly, we don't need to do
+     * the other refcount handling right now. */
+    if (G_LIKELY (pclass->release_buffer))
+      pclass->release_buffer (pool, buffer);
   }
   return TRUE;
 
@@ -254,6 +258,7 @@ default_stop (GstBufferPool * pool)
 
   /* clear the pool */
   while ((buffer = gst_atomic_queue_pop (pool->queue))) {
+    GST_LOG_OBJECT (pool, "freeing %p", buffer);
     gst_poll_read_control (pool->poll);
 
     if (G_LIKELY (pclass->free_buffer))
@@ -289,6 +294,13 @@ do_stop (GstBufferPool * pool)
  * 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.
  *
+ * Activating the bufferpool will preallocate all resources in the pool based on
+ * the configuration of the pool.
+ *
+ * Deactivating will free the resources again when there are no outstanding
+ * buffers. When there are outstanding buffers, they will be freed as soon as
+ * they are all returned to the pool.
+ *
  * Returns: %FALSE when the pool was not configured or when preallocation of the
  * buffers failed.
  */
@@ -318,13 +330,17 @@ gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
     gst_poll_read_control (pool->poll);
     g_atomic_int_set (&pool->flushing, FALSE);
   } else {
+    gint outstanding;
+
     /* set to flushing first */
     g_atomic_int_set (&pool->flushing, TRUE);
     gst_poll_write_control (pool->poll);
 
     /* when all buffers are in the pool, free them. Else they will be
      * freed when they are released */
-    if (g_atomic_int_get (&pool->outstanding) == 0) {
+    outstanding = g_atomic_int_get (&pool->outstanding);
+    GST_LOG_OBJECT (pool, "outstanding buffers %d", outstanding);
+    if (outstanding == 0) {
       if (!do_stop (pool))
         goto stop_failed;
     }
@@ -360,17 +376,38 @@ stop_failed:
   }
 }
 
+/**
+ * gst_buffer_pool_is_active:
+ * @pool: a #GstBufferPool
+ *
+ * Check if @pool is active. A pool can be activated with the
+ * gst_buffer_pool_set_active() call.
+ *
+ * Returns: %TRUE when the pool is active.
+ */
+gboolean
+gst_buffer_pool_is_active (GstBufferPool * pool)
+{
+  gboolean res;
+
+  GST_BUFFER_POOL_LOCK (pool);
+  res = pool->active;
+  GST_BUFFER_POOL_UNLOCK (pool);
+
+  return res;
+}
+
 static gboolean
 default_set_config (GstBufferPool * pool, GstStructure * config)
 {
   GstBufferPoolPrivate *priv = pool->priv;
   const GstCaps *caps;
   guint size, min_buffers, max_buffers;
-  guint prefix, postfix, align;
+  guint prefix, align;
 
   /* parse the config and keep around */
   if (!gst_buffer_pool_config_get (config, &caps, &size, &min_buffers,
-          &max_buffers, &prefix, &postfix, &align))
+          &max_buffers, &prefix, &align))
     goto wrong_config;
 
   GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
@@ -379,7 +416,6 @@ default_set_config (GstBufferPool * pool, GstStructure * config)
   priv->min_buffers = min_buffers;
   priv->max_buffers = max_buffers;
   priv->prefix = prefix;
-  priv->postfix = postfix;
   priv->align = align;
 
   return TRUE;
@@ -394,13 +430,13 @@ 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
  * nothing and return FALSE.
  *
- * @condfig is a #GstStructure that contains the configuration parameters for
+ * @config is a #GstStructure that contains the configuration parameters for
  * the pool. A default and mandatory set of parameters can be configured with
  * gst_buffer_pool_config_set(). This function takes ownership of @config.
  *
@@ -467,7 +503,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 *
@@ -484,23 +520,86 @@ gst_buffer_pool_get_config (GstBufferPool * pool)
   return result;
 }
 
+static const gchar *empty_option[] = { NULL };
+
+/**
+ * gst_buffer_pool_get_options:
+ * @pool: a #GstBufferPool
+ *
+ * Get a NULL terminated array of string with supported bufferpool options for
+ * @pool. An option would typically be enabled with
+ * gst_buffer_pool_config_add_option().
+ *
+ * Returns: (array zero-terminated=1) (transfer none): a NULL terminated array of strings.
+ */
+const gchar **
+gst_buffer_pool_get_options (GstBufferPool * pool)
+{
+  GstBufferPoolClass *pclass;
+  const gchar **result;
+
+  g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
+
+  pclass = GST_BUFFER_POOL_GET_CLASS (pool);
+
+  if (G_LIKELY (pclass->get_options)) {
+    if ((result = pclass->get_options (pool)) == NULL)
+      goto invalid_result;
+  } else
+    result = empty_option;
+
+  return result;
+
+  /* ERROR */
+invalid_result:
+  {
+    g_warning ("bufferpool subclass returned NULL options");
+    return empty_option;
+  }
+}
+
+/**
+ * gst_buffer_pool_has_option:
+ * @pool: a #GstBufferPool
+ * @option: an option
+ *
+ * Check if the bufferpool supports @option.
+ *
+ * Returns: a NULL terminated array of strings.
+ */
+gboolean
+gst_buffer_pool_has_option (GstBufferPool * pool, const gchar * option)
+{
+  guint i;
+  const gchar **options;
+
+  g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
+  g_return_val_if_fail (option != NULL, FALSE);
+
+  options = gst_buffer_pool_get_options (pool);
+
+  for (i = 0; options[i]; i++) {
+    if (g_str_equal (options[i], option))
+      return TRUE;
+  }
+  return FALSE;
+}
+
 /**
  * gst_buffer_pool_config_set:
- * @config: a #GstBufferPool
+ * @config: a #GstBufferPool configuration
  * @caps: caps for the buffers
- * @size: the size of each buffer, not including pre and post fix
+ * @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
- * @postfix: postfix each buffer with this many bytes
  * @align: alignment of the buffer data.
  *
  * Configure @config with the given parameters.
  */
 void
 gst_buffer_pool_config_set (GstStructure * config, const GstCaps * caps,
-    guint size, guint min_buffers, guint max_buffers, guint prefix,
-    guint postfix, guint align)
+    guint size, guint min_buffers, guint max_buffers, guint prefix, guint align)
 {
   g_return_if_fail (config != NULL);
 
@@ -510,27 +609,159 @@ gst_buffer_pool_config_set (GstStructure * config, const GstCaps * caps,
       GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
       GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
       GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
-      GST_QUARK (POSTFIX), G_TYPE_UINT, postfix,
       GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
 }
 
 /**
+ * gst_buffer_pool_config_add_option:
+ * @config: a #GstBufferPool configuration
+ * @option: an option to add
+ *
+ * Enabled the option in @config. This will instruct the @bufferpool to enable
+ * the specified option on the buffers that it allocates.
+ *
+ * The supported options by @pool can be retrieved with gst_buffer_pool_get_options().
+ */
+void
+gst_buffer_pool_config_add_option (GstStructure * config, const gchar * option)
+{
+  GValueArray *array;
+  const GValue *value;
+  GValue option_value = { 0 };
+  gint i;
+
+  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);
+  } 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);
+
+    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;
+  }
+  g_value_init (&option_value, G_TYPE_STRING);
+  g_value_set_string (&option_value, option);
+  g_value_array_append (array, &option_value);
+  g_value_unset (&option_value);
+}
+
+/**
+ * gst_buffer_pool_config_n_options:
+ * @config: a #GstBufferPool configuration
+ *
+ * Retrieve the number of values currently stored in the
+ * options array of the @config structure.
+ *
+ * Returns: the options array size as a #guint.
+ */
+guint
+gst_buffer_pool_config_n_options (GstStructure * config)
+{
+  GValueArray *array;
+  const GValue *value;
+  guint size = 0;
+
+  g_return_val_if_fail (config != NULL, 0);
+
+  value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
+  if (value) {
+    array = (GValueArray *) g_value_get_boxed (value);
+    size = array->n_values;
+  }
+  return size;
+}
+
+/**
+ * gst_buffer_pool_config_get_option:
+ * @config: a #GstBufferPool configuration
+ * @index: position in the option array to read
+ *
+ * Parse an available @config and get the option
+ * at @index of the options API array.
+ *
+ * Returns: a #gchar of the option at @index.
+ */
+const gchar *
+gst_buffer_pool_config_get_option (GstStructure * config, guint index)
+{
+  const GValue *value;
+  const gchar *ret = NULL;
+
+  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;
+
+    array = (GValueArray *) g_value_get_boxed (value);
+    option_value = g_value_array_get_nth (array, index);
+
+    if (option_value)
+      ret = g_value_get_string (option_value);
+  }
+  return ret;
+}
+
+/**
+ * gst_buffer_pool_config_has_option:
+ * @config: a #GstBufferPool configuration
+ * @option: an option
+ *
+ * Check if @config contains @option
+ *
+ * Returns: TRUE if the options array contains @option.
+ */
+gboolean
+gst_buffer_pool_config_has_option (GstStructure * config, const gchar * option)
+{
+  const GValue *value;
+
+  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)))
+        return TRUE;
+    }
+  }
+  return FALSE;
+}
+
+/**
  * gst_buffer_pool_config_get:
- * @config: a #GstBufferPool
- * @caps: the caps of buffers
- * @size: the size of each buffer, not including pre and post fix
- * @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
- * @postfix: postfix 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.
  */
 gboolean
 gst_buffer_pool_config_get (GstStructure * config, const GstCaps ** caps,
     guint * size, guint * min_buffers, guint * max_buffers, guint * prefix,
-    guint * postfix, guint * align)
+    guint * align)
 {
   g_return_val_if_fail (config != NULL, FALSE);
 
@@ -540,7 +771,6 @@ gst_buffer_pool_config_get (GstStructure * config, const GstCaps ** caps,
       GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
       GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
       GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
-      GST_QUARK (POSTFIX), G_TYPE_UINT, postfix,
       GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
 }
 
@@ -581,7 +811,7 @@ default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
     /* check if we need to wait */
     if (params && (params->flags & GST_BUFFER_POOL_FLAG_DONTWAIT)) {
       GST_LOG_OBJECT (pool, "no more buffers");
-      result = GST_FLOW_UNEXPECTED;
+      result = GST_FLOW_EOS;
       break;
     }
 
@@ -618,11 +848,24 @@ dec_outstanding (GstBufferPool * pool)
   }
 }
 
+static void
+default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer,
+    GstBufferPoolParams * params)
+{
+  GST_BUFFER_FLAGS (buffer) = 0;
+
+  GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
+  GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
+  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;
+}
+
 /**
  * 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.
@@ -657,6 +900,9 @@ gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
     /* all buffers from the pool point to the pool and have the refcount of the
      * pool incremented */
     (*buffer)->pool = gst_object_ref (pool);
+    /* now reset the buffer when needed */
+    if (G_LIKELY (pclass->reset_buffer))
+      pclass->reset_buffer (pool, *buffer, params);
   } else {
     dec_outstanding (pool);
   }
@@ -676,7 +922,7 @@ default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
 /**
  * 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().
@@ -694,8 +940,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 ((gpointer *) & buffer->pool,
-          pool, NULL))
+  if (!G_ATOMIC_POINTER_COMPARE_AND_EXCHANGE (&buffer->pool, pool, NULL))
     return;
 
   pclass = GST_BUFFER_POOL_GET_CLASS (pool);