docs: convert NULL, TRUE, and FALSE to %NULL, %TRUE, and %FALSE
[platform/upstream/gstreamer.git] / gst / gstbufferpool.c
index 0f03753..6c14ce4 100644 (file)
@@ -61,8 +61,6 @@
  *
  * Use gst_object_unref() to release the reference to a bufferpool. If the
  * refcount of the pool reaches 0, the pool will be freed.
- *
- * Last reviewed on 2014-01-30 (1.3.0)
  */
 
 #include "gst_private.h"
@@ -274,12 +272,13 @@ do_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
    * the buffer and we want to remove any other metadata that gets added
    * later */
   gst_buffer_foreach_meta (*buffer, mark_meta_pooled, pool);
-  /* tag memory, this is how we expect the buffer when it is
+
+  /* un-tag memory, this is how we expect the buffer when it is
    * released again */
-  GST_BUFFER_FLAG_SET (*buffer, GST_BUFFER_FLAG_TAG_MEMORY);
+  GST_BUFFER_FLAG_UNSET (*buffer, GST_BUFFER_FLAG_TAG_MEMORY);
 
   GST_LOG_OBJECT (pool, "allocated buffer %d/%d, %p", cur_buffers,
-      max_buffers, buffer);
+      max_buffers, *buffer);
 
   return result;
 
@@ -358,7 +357,6 @@ do_start (GstBufferPool * pool)
   return TRUE;
 }
 
-
 static void
 default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
 {
@@ -418,6 +416,33 @@ do_stop (GstBufferPool * pool)
   return TRUE;
 }
 
+/* must be called with the lock */
+static void
+do_set_flushing (GstBufferPool * pool, gboolean flushing)
+{
+  GstBufferPoolPrivate *priv = pool->priv;
+  GstBufferPoolClass *pclass;
+
+  pclass = GST_BUFFER_POOL_GET_CLASS (pool);
+
+  if (GST_BUFFER_POOL_IS_FLUSHING (pool) == flushing)
+    return;
+
+  if (flushing) {
+    g_atomic_int_set (&pool->flushing, 1);
+    gst_poll_write_control (priv->poll);
+
+    if (pclass->flush_start)
+      pclass->flush_start (pool);
+  } else {
+    if (pclass->flush_stop)
+      pclass->flush_stop (pool);
+
+    gst_poll_read_control (priv->poll);
+    g_atomic_int_set (&pool->flushing, 0);
+  }
+}
+
 /**
  * gst_buffer_pool_set_active:
  * @pool: a #GstBufferPool
@@ -461,15 +486,17 @@ gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
     if (!do_start (pool))
       goto start_failed;
 
+    /* flush_stop my release buffers, setting to active to avoid running
+     * do_stop while activating the pool */
+    priv->active = TRUE;
+
     /* unset the flushing state now */
-    gst_poll_read_control (priv->poll);
-    g_atomic_int_set (&pool->flushing, 0);
+    do_set_flushing (pool, FALSE);
   } else {
     gint outstanding;
 
     /* set to flushing first */
-    g_atomic_int_set (&pool->flushing, 1);
-    gst_poll_write_control (priv->poll);
+    do_set_flushing (pool, TRUE);
 
     /* when all buffers are in the pool, free them. Else they will be
      * freed when they are released */
@@ -479,8 +506,9 @@ gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
       if (!do_stop (pool))
         goto stop_failed;
     }
+
+    priv->active = FALSE;
   }
-  priv->active = active;
   GST_BUFFER_POOL_UNLOCK (pool);
 
   return res;
@@ -576,9 +604,11 @@ wrong_config:
  * @pool: a #GstBufferPool
  * @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.
+ * Set the configuration of the pool. If the pool is already configured, and
+ * the configuration haven't change, this function will return %TRUE. If the
+ * pool is active, this function will try deactivating it. Buffers allocated
+ * form this pool must be returned or else this function will do nothing and
+ * return %FALSE.
  *
  * @config is a #GstStructure that contains the configuration parameters for
  * the pool. A default and mandatory set of parameters can be configured with
@@ -606,9 +636,24 @@ gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
   priv = pool->priv;
 
   GST_BUFFER_POOL_LOCK (pool);
+
+  /* nothing to do if config is unchanged */
+  if (priv->configured && gst_structure_is_equal (config, priv->config))
+    goto config_unchanged;
+
   /* can't change the settings when active */
-  if (priv->active)
-    goto was_active;
+  if (priv->active) {
+    GST_BUFFER_POOL_UNLOCK (pool);
+    if (!gst_buffer_pool_set_active (pool, FALSE)) {
+      GST_BUFFER_POOL_LOCK (pool);
+      goto was_active;
+    }
+    GST_BUFFER_POOL_LOCK (pool);
+
+    /* not likely but as we released the lock */
+    if (priv->active)
+      goto was_active;
+  }
 
   /* we can't change when outstanding buffers */
   if (g_atomic_int_get (&priv->outstanding) != 0)
@@ -622,20 +667,26 @@ gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
   else
     result = FALSE;
 
-  if (result) {
-    if (priv->config)
-      gst_structure_free (priv->config);
-    priv->config = config;
+  /* save the config regardless of the result so user can read back the
+   * modified config and evaluate if the changes are acceptable */
+  if (priv->config)
+    gst_structure_free (priv->config);
+  priv->config = config;
 
+  if (result) {
     /* now we are configured */
     priv->configured = TRUE;
-  } else {
-    gst_structure_free (config);
   }
   GST_BUFFER_POOL_UNLOCK (pool);
 
   return result;
 
+config_unchanged:
+  {
+    gst_structure_free (config);
+    GST_BUFFER_POOL_UNLOCK (pool);
+    return TRUE;
+  }
   /* ERRORS */
 was_active:
   {
@@ -724,7 +775,7 @@ invalid_result:
  *
  * Check if the bufferpool supports @option.
  *
- * Returns: a %NULL terminated array of strings.
+ * Returns: %TRUE if the buffer pool contains @option.
  */
 gboolean
 gst_buffer_pool_has_option (GstBufferPool * pool, const gchar * option)
@@ -984,6 +1035,44 @@ gst_buffer_pool_config_get_allocator (GstStructure * config,
   return TRUE;
 }
 
+/**
+ * gst_buffer_pool_config_validate_params:
+ * @config: (transfer none): a #GstBufferPool configuration
+ * @caps: (transfer none): the excepted caps of buffers
+ * @size: the expected size of each buffer, not including prefix and padding
+ * @min_buffers: the expected minimum amount of buffers to allocate.
+ * @max_buffers: the expect maximum amount of buffers to allocate or 0 for unlimited.
+ *
+ * Validate that changes made to @config are still valid in the context of the
+ * expected parameters. This function is a helper that can be used to validate
+ * changes made by a pool to a config when gst_buffer_pool_set_config()
+ * returns %FALSE. This expects that @caps and @size haven't changed, and that
+ * @min_buffers aren't lower then what we initially expected. This does not check
+ * if options or allocator parameters.
+ *
+ * Since: 1.4
+ *
+ * Returns: %TRUE, if the parameters are valid in this context.
+ */
+gboolean
+gst_buffer_pool_config_validate_params (GstStructure * config, GstCaps * caps,
+    guint size, guint min_buffers, G_GNUC_UNUSED guint max_buffers)
+{
+  GstCaps *newcaps;
+  guint newsize, newmin;
+  gboolean ret = FALSE;
+
+  g_return_val_if_fail (config != NULL, FALSE);
+
+  gst_buffer_pool_config_get_params (config, &newcaps, &newsize, &newmin, NULL);
+
+  if (gst_caps_is_equal (caps, newcaps) && (size == newsize)
+      && (newmin >= min_buffers))
+    ret = TRUE;
+
+  return ret;
+}
+
 static GstFlowReturn
 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
     GstBufferPoolAcquireParams * params)
@@ -1047,9 +1136,9 @@ dec_outstanding (GstBufferPool * pool)
     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 (GST_BUFFER_POOL_IS_FLUSHING (pool))
+      /* now that we have the lock, check if we have been de-activated with
+       * outstanding buffers */
+      if (!pool->priv->active)
         do_stop (pool);
 
       GST_BUFFER_POOL_UNLOCK (pool);
@@ -1086,7 +1175,7 @@ default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer)
  * gst_buffer_pool_acquire_buffer:
  * @pool: a #GstBufferPool
  * @buffer: (out): a location for a #GstBuffer
- * @params: (transfer none) (allow-none) parameters.
+ * @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.
@@ -1135,12 +1224,29 @@ default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
   GST_LOG_OBJECT (pool, "released buffer %p %d", buffer,
       GST_MINI_OBJECT_FLAGS (buffer));
 
-  if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY)) {
-    /* keep it around in our queue */
-    gst_atomic_queue_push (pool->priv->queue, buffer);
-    gst_poll_write_control (pool->priv->poll);
-  } else {
+  /* memory should be untouched */
+  if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY))
+    goto discard;
+
+  /* size should have been reset. This is not a catch all, pool with
+   * size requirement per memory should do their own check. */
+  if (gst_buffer_get_size (buffer) != pool->priv->size)
+    goto discard;
+
+  /* all memory should be exclusive to this buffer (and thus be writable) */
+  if (!gst_buffer_is_all_memory_writable (buffer))
+    goto discard;
+
+  /* keep it around in our queue */
+  gst_atomic_queue_push (pool->priv->queue, buffer);
+  gst_poll_write_control (pool->priv->poll);
+
+  return;
+
+discard:
+  {
     do_free_buffer (pool, buffer);
+    return;
   }
 }
 
@@ -1182,3 +1288,37 @@ gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
   /* decrease the refcount that the buffer had to us */
   gst_object_unref (pool);
 }
+
+/**
+ * gst_buffer_pool_set_flushing:
+ * @pool: a #GstBufferPool
+ * @flushing: whether to start or stop flushing
+ *
+ * Enabled or disable the flushing state of a @pool without freeing or
+ * allocating buffers.
+ *
+ * Since: 1.4
+ */
+void
+gst_buffer_pool_set_flushing (GstBufferPool * pool, gboolean flushing)
+{
+  GstBufferPoolPrivate *priv;
+
+  g_return_if_fail (GST_IS_BUFFER_POOL (pool));
+
+  GST_LOG_OBJECT (pool, "flushing %d", flushing);
+
+  priv = pool->priv;
+
+  GST_BUFFER_POOL_LOCK (pool);
+
+  if (!priv->active) {
+    GST_WARNING_OBJECT (pool, "can't change flushing state of inactive pool");
+    goto done;
+  }
+
+  do_set_flushing (pool, flushing);
+
+done:
+  GST_BUFFER_POOL_UNLOCK (pool);
+}