2 * Copyright (C) 2010 Wim Taymans <wim.taymans@gmail.com>
4 * gstbufferpool.c: GstBufferPool baseclass
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:gstbufferpool
24 * @short_description: Pool for buffers
25 * @see_also: #GstBuffer
27 * A #GstBufferPool is an object that can be used to pre-allocate and recycle
28 * buffers of the same size and with the same properties.
30 * A #GstBufferPool is created with gst_buffer_pool_new().
32 * Once a pool is created, it needs to be configured. A call to
33 * gst_buffer_pool_get_config() returns the current configuration structure from
34 * the pool. With gst_buffer_pool_config_set_params() and
35 * gst_buffer_pool_config_set_allocator() the bufferpool parameters and
36 * allocator can be configured. Other properties can be configured in the pool
37 * depending on the pool implementation.
39 * A bufferpool can have extra options that can be enabled with
40 * gst_buffer_pool_config_add_option(). The available options can be retrieved
41 * with gst_buffer_pool_get_options(). Some options allow for additional
42 * configuration properties to be set.
44 * After the configuration structure has been configured,
45 * gst_buffer_pool_set_config() updates the configuration in the pool. This can
46 * fail when the configuration structure is not accepted.
48 * After the a pool has been configured, it can be activated with
49 * gst_buffer_pool_set_active(). This will preallocate the configured resources
52 * When the pool is active, gst_buffer_pool_acquire_buffer() can be used to
53 * retrieve a buffer from the pool.
55 * Buffers allocated from a bufferpool will automatically be returned to the
56 * pool with gst_buffer_pool_release_buffer() when their refcount drops to 0.
58 * The bufferpool can be deactivated again with gst_buffer_pool_set_active().
59 * All further gst_buffer_pool_acquire_buffer() calls will return an error. When
60 * all buffers are returned to the pool they will be freed.
62 * Use gst_object_unref() to release the reference to a bufferpool. If the
63 * refcount of the pool reaches 0, the pool will be freed.
65 * Last reviewed on 2014-01-30 (1.3.0)
68 #include "gst_private.h"
69 #include "glib-compat-private.h"
75 #include <sys/types.h>
77 #include "gstatomicqueue.h"
83 #include "gstbufferpool.h"
85 GST_DEBUG_CATEGORY_STATIC (gst_buffer_pool_debug);
86 #define GST_CAT_DEFAULT gst_buffer_pool_debug
88 #define GST_BUFFER_POOL_GET_PRIVATE(obj) \
89 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BUFFER_POOL, GstBufferPoolPrivate))
91 #define GST_BUFFER_POOL_LOCK(pool) (g_rec_mutex_lock(&pool->priv->rec_lock))
92 #define GST_BUFFER_POOL_UNLOCK(pool) (g_rec_mutex_unlock(&pool->priv->rec_lock))
94 struct _GstBufferPoolPrivate
96 GstAtomicQueue *queue;
103 gint outstanding; /* number of buffers that are in use */
106 GstStructure *config;
112 GstAllocator *allocator;
113 GstAllocationParams params;
116 static void gst_buffer_pool_finalize (GObject * object);
118 G_DEFINE_TYPE (GstBufferPool, gst_buffer_pool, GST_TYPE_OBJECT);
120 static gboolean default_start (GstBufferPool * pool);
121 static gboolean default_stop (GstBufferPool * pool);
122 static gboolean default_set_config (GstBufferPool * pool,
123 GstStructure * config);
124 static GstFlowReturn default_alloc_buffer (GstBufferPool * pool,
125 GstBuffer ** buffer, GstBufferPoolAcquireParams * params);
126 static GstFlowReturn default_acquire_buffer (GstBufferPool * pool,
127 GstBuffer ** buffer, GstBufferPoolAcquireParams * params);
128 static void default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer);
129 static void default_free_buffer (GstBufferPool * pool, GstBuffer * buffer);
130 static void default_release_buffer (GstBufferPool * pool, GstBuffer * buffer);
133 gst_buffer_pool_class_init (GstBufferPoolClass * klass)
135 GObjectClass *gobject_class = (GObjectClass *) klass;
137 g_type_class_add_private (klass, sizeof (GstBufferPoolPrivate));
139 gobject_class->finalize = gst_buffer_pool_finalize;
141 klass->start = default_start;
142 klass->stop = default_stop;
143 klass->set_config = default_set_config;
144 klass->acquire_buffer = default_acquire_buffer;
145 klass->reset_buffer = default_reset_buffer;
146 klass->alloc_buffer = default_alloc_buffer;
147 klass->release_buffer = default_release_buffer;
148 klass->free_buffer = default_free_buffer;
150 GST_DEBUG_CATEGORY_INIT (gst_buffer_pool_debug, "bufferpool", 0,
155 gst_buffer_pool_init (GstBufferPool * pool)
157 GstBufferPoolPrivate *priv;
159 priv = pool->priv = GST_BUFFER_POOL_GET_PRIVATE (pool);
161 g_rec_mutex_init (&priv->rec_lock);
163 priv->poll = gst_poll_new_timer ();
164 priv->queue = gst_atomic_queue_new (16);
166 priv->active = FALSE;
167 priv->configured = FALSE;
168 priv->started = FALSE;
169 priv->config = gst_structure_new_id_empty (GST_QUARK (BUFFER_POOL_CONFIG));
170 gst_buffer_pool_config_set_params (priv->config, NULL, 0, 0, 0);
171 priv->allocator = NULL;
172 gst_allocation_params_init (&priv->params);
173 gst_buffer_pool_config_set_allocator (priv->config, priv->allocator,
175 /* 1 control write for flushing */
176 gst_poll_write_control (priv->poll);
177 /* 1 control write for marking that we are not waiting for poll */
178 gst_poll_write_control (priv->poll);
180 GST_DEBUG_OBJECT (pool, "created");
184 gst_buffer_pool_finalize (GObject * object)
187 GstBufferPoolPrivate *priv;
189 pool = GST_BUFFER_POOL_CAST (object);
192 GST_DEBUG_OBJECT (pool, "finalize");
194 gst_buffer_pool_set_active (pool, FALSE);
195 gst_atomic_queue_unref (priv->queue);
196 gst_poll_free (priv->poll);
197 gst_structure_free (priv->config);
198 g_rec_mutex_clear (&priv->rec_lock);
200 gst_object_unref (priv->allocator);
202 G_OBJECT_CLASS (gst_buffer_pool_parent_class)->finalize (object);
206 * gst_buffer_pool_new:
208 * Creates a new #GstBufferPool instance.
210 * Returns: (transfer floating): a new #GstBufferPool instance
213 gst_buffer_pool_new (void)
215 GstBufferPool *result;
217 result = g_object_newv (GST_TYPE_BUFFER_POOL, 0, NULL);
218 GST_DEBUG_OBJECT (result, "created new buffer pool");
224 default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
225 GstBufferPoolAcquireParams * params)
227 GstBufferPoolPrivate *priv = pool->priv;
230 gst_buffer_new_allocate (priv->allocator, priv->size, &priv->params);
236 mark_meta_pooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
238 GstBufferPool *pool = user_data;
240 GST_DEBUG_OBJECT (pool, "marking meta %p as POOLED in buffer %p", *meta,
242 GST_META_FLAG_SET (*meta, GST_META_FLAG_POOLED);
243 GST_META_FLAG_SET (*meta, GST_META_FLAG_LOCKED);
249 do_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
250 GstBufferPoolAcquireParams * params)
252 GstBufferPoolPrivate *priv = pool->priv;
253 GstFlowReturn result;
254 gint cur_buffers, max_buffers;
255 GstBufferPoolClass *pclass;
257 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
259 if (G_UNLIKELY (!pclass->alloc_buffer))
262 max_buffers = priv->max_buffers;
264 /* increment the allocation counter */
265 cur_buffers = g_atomic_int_add (&priv->cur_buffers, 1);
266 if (max_buffers && cur_buffers >= max_buffers)
269 result = pclass->alloc_buffer (pool, buffer, params);
270 if (G_UNLIKELY (result != GST_FLOW_OK))
273 /* lock all metadata and mark as pooled, we want this to remain on
274 * the buffer and we want to remove any other metadata that gets added
276 gst_buffer_foreach_meta (*buffer, mark_meta_pooled, pool);
278 /* un-tag memory, this is how we expect the buffer when it is
280 GST_BUFFER_FLAG_UNSET (*buffer, GST_BUFFER_FLAG_TAG_MEMORY);
282 GST_LOG_OBJECT (pool, "allocated buffer %d/%d, %p", cur_buffers,
283 max_buffers, buffer);
290 GST_ERROR_OBJECT (pool, "no alloc function");
291 return GST_FLOW_NOT_SUPPORTED;
295 GST_DEBUG_OBJECT (pool, "max buffers reached");
296 g_atomic_int_add (&priv->cur_buffers, -1);
301 GST_WARNING_OBJECT (pool, "alloc function failed");
302 g_atomic_int_add (&priv->cur_buffers, -1);
307 /* the default implementation for preallocating the buffers in the pool */
309 default_start (GstBufferPool * pool)
312 GstBufferPoolPrivate *priv = pool->priv;
313 GstBufferPoolClass *pclass;
315 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
317 /* we need to prealloc buffers */
318 for (i = 0; i < priv->min_buffers; i++) {
321 if (do_alloc_buffer (pool, &buffer, NULL) != GST_FLOW_OK)
324 /* release to the queue, we call the vmethod directly, we don't need to do
325 * the other refcount handling right now. */
326 if (G_LIKELY (pclass->release_buffer))
327 pclass->release_buffer (pool, buffer);
334 GST_WARNING_OBJECT (pool, "failed to allocate buffer");
339 /* must be called with the lock */
341 do_start (GstBufferPool * pool)
343 GstBufferPoolPrivate *priv = pool->priv;
345 if (!priv->started) {
346 GstBufferPoolClass *pclass;
348 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
350 GST_LOG_OBJECT (pool, "starting");
351 /* start the pool, subclasses should allocate buffers and put them
353 if (G_LIKELY (pclass->start)) {
354 if (!pclass->start (pool))
357 priv->started = TRUE;
364 default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
366 gst_buffer_unref (buffer);
370 do_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
372 GstBufferPoolPrivate *priv;
373 GstBufferPoolClass *pclass;
376 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
378 g_atomic_int_add (&priv->cur_buffers, -1);
379 GST_LOG_OBJECT (pool, "freeing buffer %p (%u left)", buffer,
382 if (G_LIKELY (pclass->free_buffer))
383 pclass->free_buffer (pool, buffer);
386 /* must be called with the lock */
388 default_stop (GstBufferPool * pool)
390 GstBufferPoolPrivate *priv = pool->priv;
394 while ((buffer = gst_atomic_queue_pop (priv->queue))) {
395 gst_poll_read_control (priv->poll);
396 do_free_buffer (pool, buffer);
398 return priv->cur_buffers == 0;
401 /* must be called with the lock */
403 do_stop (GstBufferPool * pool)
405 GstBufferPoolPrivate *priv = pool->priv;
408 GstBufferPoolClass *pclass;
410 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
412 GST_LOG_OBJECT (pool, "stopping");
413 if (G_LIKELY (pclass->stop)) {
414 if (!pclass->stop (pool))
417 priv->started = FALSE;
423 * gst_buffer_pool_set_active:
424 * @pool: a #GstBufferPool
425 * @active: the new active state
427 * Control the active state of @pool. When the pool is inactive, new calls to
428 * gst_buffer_pool_acquire_buffer() will return with %GST_FLOW_FLUSHING.
430 * Activating the bufferpool will preallocate all resources in the pool based on
431 * the configuration of the pool.
433 * Deactivating will free the resources again when there are no outstanding
434 * buffers. When there are outstanding buffers, they will be freed as soon as
435 * they are all returned to the pool.
437 * Returns: %FALSE when the pool was not configured or when preallocation of the
441 gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
444 GstBufferPoolPrivate *priv;
446 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
448 GST_LOG_OBJECT (pool, "active %d", active);
452 GST_BUFFER_POOL_LOCK (pool);
453 /* just return if we are already in the right state */
454 if (priv->active == active)
457 /* we need to be configured */
458 if (!priv->configured)
462 if (!do_start (pool))
465 /* unset the flushing state now */
466 gst_poll_read_control (priv->poll);
467 g_atomic_int_set (&pool->flushing, 0);
471 /* set to flushing first */
472 g_atomic_int_set (&pool->flushing, 1);
473 gst_poll_write_control (priv->poll);
475 /* when all buffers are in the pool, free them. Else they will be
476 * freed when they are released */
477 outstanding = g_atomic_int_get (&priv->outstanding);
478 GST_LOG_OBJECT (pool, "outstanding buffers %d", outstanding);
479 if (outstanding == 0) {
484 priv->active = active;
485 GST_BUFFER_POOL_UNLOCK (pool);
491 GST_DEBUG_OBJECT (pool, "pool was in the right state");
492 GST_BUFFER_POOL_UNLOCK (pool);
497 GST_ERROR_OBJECT (pool, "pool was not configured");
498 GST_BUFFER_POOL_UNLOCK (pool);
503 GST_ERROR_OBJECT (pool, "start failed");
504 GST_BUFFER_POOL_UNLOCK (pool);
509 GST_WARNING_OBJECT (pool, "stop failed");
510 GST_BUFFER_POOL_UNLOCK (pool);
516 * gst_buffer_pool_is_active:
517 * @pool: a #GstBufferPool
519 * Check if @pool is active. A pool can be activated with the
520 * gst_buffer_pool_set_active() call.
522 * Returns: %TRUE when the pool is active.
525 gst_buffer_pool_is_active (GstBufferPool * pool)
529 GST_BUFFER_POOL_LOCK (pool);
530 res = pool->priv->active;
531 GST_BUFFER_POOL_UNLOCK (pool);
537 default_set_config (GstBufferPool * pool, GstStructure * config)
539 GstBufferPoolPrivate *priv = pool->priv;
541 guint size, min_buffers, max_buffers;
542 GstAllocator *allocator;
543 GstAllocationParams params;
545 /* parse the config and keep around */
546 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
550 if (!gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms))
553 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
556 priv->min_buffers = min_buffers;
557 priv->max_buffers = max_buffers;
558 priv->cur_buffers = 0;
561 gst_object_unref (priv->allocator);
562 if ((priv->allocator = allocator))
563 gst_object_ref (allocator);
564 priv->params = params;
570 GST_WARNING_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
576 * gst_buffer_pool_set_config:
577 * @pool: a #GstBufferPool
578 * @config: (transfer full): a #GstStructure
580 * Set the configuration of the pool. The pool must be inactive and all buffers
581 * allocated form this pool must be returned or else this function will do
582 * nothing and return FALSE.
584 * @config is a #GstStructure that contains the configuration parameters for
585 * the pool. A default and mandatory set of parameters can be configured with
586 * gst_buffer_pool_config_set_params(), gst_buffer_pool_config_set_allocator()
587 * and gst_buffer_pool_config_add_option().
589 * If the parameters in @config can not be set exactly, this function returns
590 * %FALSE and will try to update as much state as possible. The new state can
591 * then be retrieved and refined with gst_buffer_pool_get_config().
593 * This function takes ownership of @config.
595 * Returns: %TRUE when the configuration could be set.
598 gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
601 GstBufferPoolClass *pclass;
602 GstBufferPoolPrivate *priv;
604 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
605 g_return_val_if_fail (config != NULL, FALSE);
609 GST_BUFFER_POOL_LOCK (pool);
610 /* can't change the settings when active */
614 /* we can't change when outstanding buffers */
615 if (g_atomic_int_get (&priv->outstanding) != 0)
616 goto have_outstanding;
618 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
620 /* set the new config */
621 if (G_LIKELY (pclass->set_config))
622 result = pclass->set_config (pool, config);
628 gst_structure_free (priv->config);
629 priv->config = config;
631 /* now we are configured */
632 priv->configured = TRUE;
634 gst_structure_free (config);
636 GST_BUFFER_POOL_UNLOCK (pool);
643 gst_structure_free (config);
644 GST_WARNING_OBJECT (pool, "can't change config, we are active");
645 GST_BUFFER_POOL_UNLOCK (pool);
650 gst_structure_free (config);
651 GST_WARNING_OBJECT (pool, "can't change config, have outstanding buffers");
652 GST_BUFFER_POOL_UNLOCK (pool);
658 * gst_buffer_pool_get_config:
659 * @pool: a #GstBufferPool
661 * Get a copy of the current configuration of the pool. This configuration
662 * can either be modified and used for the gst_buffer_pool_set_config() call
663 * or it must be freed after usage.
665 * Returns: (transfer full): a copy of the current configuration of @pool. use
666 * gst_structure_free() after usage or gst_buffer_pool_set_config().
669 gst_buffer_pool_get_config (GstBufferPool * pool)
671 GstStructure *result;
673 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
675 GST_BUFFER_POOL_LOCK (pool);
676 result = gst_structure_copy (pool->priv->config);
677 GST_BUFFER_POOL_UNLOCK (pool);
682 static const gchar *empty_option[] = { NULL };
685 * gst_buffer_pool_get_options:
686 * @pool: a #GstBufferPool
688 * Get a %NULL terminated array of string with supported bufferpool options for
689 * @pool. An option would typically be enabled with
690 * gst_buffer_pool_config_add_option().
692 * Returns: (array zero-terminated=1) (transfer none): a %NULL terminated array
696 gst_buffer_pool_get_options (GstBufferPool * pool)
698 GstBufferPoolClass *pclass;
699 const gchar **result;
701 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
703 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
705 if (G_LIKELY (pclass->get_options)) {
706 if ((result = pclass->get_options (pool)) == NULL)
709 result = empty_option;
716 g_warning ("bufferpool subclass returned NULL options");
722 * gst_buffer_pool_has_option:
723 * @pool: a #GstBufferPool
726 * Check if the bufferpool supports @option.
728 * Returns: a %NULL terminated array of strings.
731 gst_buffer_pool_has_option (GstBufferPool * pool, const gchar * option)
734 const gchar **options;
736 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
737 g_return_val_if_fail (option != NULL, FALSE);
739 options = gst_buffer_pool_get_options (pool);
741 for (i = 0; options[i]; i++) {
742 if (g_str_equal (options[i], option))
749 * gst_buffer_pool_config_set_params:
750 * @config: a #GstBufferPool configuration
751 * @caps: caps for the buffers
752 * @size: the size of each buffer, not including prefix and padding
753 * @min_buffers: the minimum amount of buffers to allocate.
754 * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
756 * Configure @config with the given parameters.
759 gst_buffer_pool_config_set_params (GstStructure * config, GstCaps * caps,
760 guint size, guint min_buffers, guint max_buffers)
762 g_return_if_fail (config != NULL);
763 g_return_if_fail (max_buffers == 0 || min_buffers <= max_buffers);
764 g_return_if_fail (caps == NULL || gst_caps_is_fixed (caps));
766 gst_structure_id_set (config,
767 GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
768 GST_QUARK (SIZE), G_TYPE_UINT, size,
769 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
770 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers, NULL);
774 * gst_buffer_pool_config_set_allocator:
775 * @config: a #GstBufferPool configuration
776 * @allocator: a #GstAllocator
777 * @params: #GstAllocationParams
779 * Set the @allocator and @params on @config.
781 * One of @allocator and @params can be %NULL, but not both. When @allocator
782 * is %NULL, the default allocator of the pool will use the values in @param
783 * to perform its allocation. When @param is %NULL, the pool will use the
784 * provided @allocator with its default #GstAllocationParams.
786 * A call to gst_buffer_pool_set_config() can update the allocator and params
787 * with the values that it is able to do. Some pools are, for example, not able
788 * to operate with different allocators or cannot allocate with the values
789 * specified in @params. Use gst_buffer_pool_get_config() to get the currently
793 gst_buffer_pool_config_set_allocator (GstStructure * config,
794 GstAllocator * allocator, const GstAllocationParams * params)
796 g_return_if_fail (config != NULL);
797 g_return_if_fail (allocator != NULL || params != NULL);
799 gst_structure_id_set (config,
800 GST_QUARK (ALLOCATOR), GST_TYPE_ALLOCATOR, allocator,
801 GST_QUARK (PARAMS), GST_TYPE_ALLOCATION_PARAMS, params, NULL);
805 * gst_buffer_pool_config_add_option:
806 * @config: a #GstBufferPool configuration
807 * @option: an option to add
809 * Enabled the option in @config. This will instruct the @bufferpool to enable
810 * the specified option on the buffers that it allocates.
812 * The supported options by @pool can be retrieved with gst_buffer_pool_get_options().
815 gst_buffer_pool_config_add_option (GstStructure * config, const gchar * option)
818 GValue option_value = { 0, };
821 g_return_if_fail (config != NULL);
823 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
825 len = gst_value_array_get_size (value);
826 for (i = 0; i < len; ++i) {
827 const GValue *nth_val = gst_value_array_get_value (value, i);
829 if (g_str_equal (option, g_value_get_string (nth_val)))
833 GValue new_array_val = { 0, };
835 g_value_init (&new_array_val, GST_TYPE_ARRAY);
836 gst_structure_id_take_value (config, GST_QUARK (OPTIONS), &new_array_val);
837 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
839 g_value_init (&option_value, G_TYPE_STRING);
840 g_value_set_string (&option_value, option);
841 gst_value_array_append_and_take_value ((GValue *) value, &option_value);
845 * gst_buffer_pool_config_n_options:
846 * @config: a #GstBufferPool configuration
848 * Retrieve the number of values currently stored in the options array of the
851 * Returns: the options array size as a #guint.
854 gst_buffer_pool_config_n_options (GstStructure * config)
859 g_return_val_if_fail (config != NULL, 0);
861 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
863 size = gst_value_array_get_size (value);
869 * gst_buffer_pool_config_get_option:
870 * @config: a #GstBufferPool configuration
871 * @index: position in the option array to read
873 * Parse an available @config and get the option at @index of the options API
876 * Returns: a #gchar of the option at @index.
879 gst_buffer_pool_config_get_option (GstStructure * config, guint index)
882 const gchar *ret = NULL;
884 g_return_val_if_fail (config != NULL, 0);
886 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
888 const GValue *option_value;
890 option_value = gst_value_array_get_value (value, index);
892 ret = g_value_get_string (option_value);
898 * gst_buffer_pool_config_has_option:
899 * @config: a #GstBufferPool configuration
902 * Check if @config contains @option.
904 * Returns: %TRUE if the options array contains @option.
907 gst_buffer_pool_config_has_option (GstStructure * config, const gchar * option)
912 g_return_val_if_fail (config != NULL, 0);
914 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
916 len = gst_value_array_get_size (value);
917 for (i = 0; i < len; ++i) {
918 const GValue *nth_val = gst_value_array_get_value (value, i);
920 if (g_str_equal (option, g_value_get_string (nth_val)))
928 * gst_buffer_pool_config_get_params:
929 * @config: (transfer none): a #GstBufferPool configuration
930 * @caps: (out) (transfer none) (allow-none): the caps of buffers
931 * @size: (out) (allow-none): the size of each buffer, not including prefix and padding
932 * @min_buffers: (out) (allow-none): the minimum amount of buffers to allocate.
933 * @max_buffers: (out) (allow-none): the maximum amount of buffers to allocate or 0 for unlimited.
935 * Get the configuration values from @config.
937 * Returns: %TRUE if all parameters could be fetched.
940 gst_buffer_pool_config_get_params (GstStructure * config, GstCaps ** caps,
941 guint * size, guint * min_buffers, guint * max_buffers)
943 g_return_val_if_fail (config != NULL, FALSE);
946 *caps = g_value_get_boxed (gst_structure_id_get_value (config,
949 return gst_structure_id_get (config,
950 GST_QUARK (SIZE), G_TYPE_UINT, size,
951 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
952 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers, NULL);
956 * gst_buffer_pool_config_get_allocator:
957 * @config: (transfer none): a #GstBufferPool configuration
958 * @allocator: (transfer none): a #GstAllocator
959 * @params: #GstAllocationParams
961 * Get the @allocator and @params from @config.
963 * Returns: %TRUE, if the values are set.
966 gst_buffer_pool_config_get_allocator (GstStructure * config,
967 GstAllocator ** allocator, GstAllocationParams * params)
969 g_return_val_if_fail (config != NULL, FALSE);
972 *allocator = g_value_get_object (gst_structure_id_get_value (config,
973 GST_QUARK (ALLOCATOR)));
975 GstAllocationParams *p;
977 p = g_value_get_boxed (gst_structure_id_get_value (config,
978 GST_QUARK (PARAMS)));
982 gst_allocation_params_init (params);
989 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
990 GstBufferPoolAcquireParams * params)
992 GstFlowReturn result;
993 GstBufferPoolPrivate *priv = pool->priv;
996 if (G_UNLIKELY (GST_BUFFER_POOL_IS_FLUSHING (pool)))
999 /* try to get a buffer from the queue */
1000 *buffer = gst_atomic_queue_pop (priv->queue);
1001 if (G_LIKELY (*buffer)) {
1002 gst_poll_read_control (priv->poll);
1003 result = GST_FLOW_OK;
1004 GST_LOG_OBJECT (pool, "acquired buffer %p", *buffer);
1008 /* no buffer, try to allocate some more */
1009 GST_LOG_OBJECT (pool, "no buffer, trying to allocate");
1010 result = do_alloc_buffer (pool, buffer, NULL);
1011 if (G_LIKELY (result == GST_FLOW_OK))
1012 /* we have a buffer, return it */
1015 if (G_UNLIKELY (result != GST_FLOW_EOS))
1016 /* something went wrong, return error */
1019 /* check if we need to wait */
1020 if (params && (params->flags & GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT)) {
1021 GST_LOG_OBJECT (pool, "no more buffers");
1025 /* now we release the control socket, we wait for a buffer release or
1027 gst_poll_read_control (pool->priv->poll);
1028 GST_LOG_OBJECT (pool, "waiting for free buffers or flushing");
1029 gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
1030 gst_poll_write_control (pool->priv->poll);
1038 GST_DEBUG_OBJECT (pool, "we are flushing");
1039 return GST_FLOW_FLUSHING;
1044 dec_outstanding (GstBufferPool * pool)
1046 if (g_atomic_int_dec_and_test (&pool->priv->outstanding)) {
1047 /* all buffers are returned to the pool, see if we need to free them */
1048 if (GST_BUFFER_POOL_IS_FLUSHING (pool)) {
1049 /* take the lock so that set_active is not run concurrently */
1050 GST_BUFFER_POOL_LOCK (pool);
1051 /* recheck the flushing state in the lock, the pool could have been
1052 * set to active again */
1053 if (GST_BUFFER_POOL_IS_FLUSHING (pool))
1056 GST_BUFFER_POOL_UNLOCK (pool);
1062 remove_meta_unpooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
1064 if (!GST_META_FLAG_IS_SET (*meta, GST_META_FLAG_POOLED)) {
1065 GST_META_FLAG_UNSET (*meta, GST_META_FLAG_LOCKED);
1072 default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer)
1074 GST_BUFFER_FLAGS (buffer) &= GST_BUFFER_FLAG_TAG_MEMORY;
1076 GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
1077 GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
1078 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
1079 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
1080 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
1082 /* remove all metadata without the POOLED flag */
1083 gst_buffer_foreach_meta (buffer, remove_meta_unpooled, pool);
1087 * gst_buffer_pool_acquire_buffer:
1088 * @pool: a #GstBufferPool
1089 * @buffer: (out): a location for a #GstBuffer
1090 * @params: (transfer none) (allow-none): parameters.
1092 * Acquire a buffer from @pool. @buffer should point to a memory location that
1093 * can hold a pointer to the new buffer.
1095 * @params can be %NULL or contain optional parameters to influence the
1098 * Returns: a #GstFlowReturn such as %GST_FLOW_FLUSHING when the pool is
1102 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
1103 GstBufferPoolAcquireParams * params)
1105 GstBufferPoolClass *pclass;
1106 GstFlowReturn result;
1108 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), GST_FLOW_ERROR);
1109 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
1111 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
1113 /* assume we'll have one more outstanding buffer we need to do that so
1114 * that concurrent set_active doesn't clear the buffers */
1115 g_atomic_int_inc (&pool->priv->outstanding);
1117 if (G_LIKELY (pclass->acquire_buffer))
1118 result = pclass->acquire_buffer (pool, buffer, params);
1120 result = GST_FLOW_NOT_SUPPORTED;
1122 if (G_LIKELY (result == GST_FLOW_OK)) {
1123 /* all buffers from the pool point to the pool and have the refcount of the
1124 * pool incremented */
1125 (*buffer)->pool = gst_object_ref (pool);
1127 dec_outstanding (pool);
1134 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
1136 GST_LOG_OBJECT (pool, "released buffer %p %d", buffer,
1137 GST_MINI_OBJECT_FLAGS (buffer));
1139 /* memory should be untouched */
1140 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY))
1143 /* size should have been reset. This is not a catch all, pool with
1144 * size requirement per memory should do their own check. */
1145 if (gst_buffer_get_size (buffer) != pool->priv->size)
1148 /* all memory should be exclusive to this buffer (and thus be writable) */
1149 if (!gst_buffer_is_all_memory_writable (buffer))
1152 /* keep it around in our queue */
1153 gst_atomic_queue_push (pool->priv->queue, buffer);
1154 gst_poll_write_control (pool->priv->poll);
1160 do_free_buffer (pool, buffer);
1166 * gst_buffer_pool_release_buffer:
1167 * @pool: a #GstBufferPool
1168 * @buffer: (transfer full): a #GstBuffer
1170 * Release @buffer to @pool. @buffer should have previously been allocated from
1171 * @pool with gst_buffer_pool_acquire_buffer().
1173 * This function is usually called automatically when the last ref on @buffer
1177 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
1179 GstBufferPoolClass *pclass;
1181 g_return_if_fail (GST_IS_BUFFER_POOL (pool));
1182 g_return_if_fail (buffer != NULL);
1184 /* check that the buffer is ours, all buffers returned to the pool have the
1185 * pool member set to NULL and the pool refcount decreased */
1186 if (!g_atomic_pointer_compare_and_exchange (&buffer->pool, pool, NULL))
1189 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
1191 /* reset the buffer when needed */
1192 if (G_LIKELY (pclass->reset_buffer))
1193 pclass->reset_buffer (pool, buffer);
1195 if (G_LIKELY (pclass->release_buffer))
1196 pclass->release_buffer (pool, buffer);
1198 dec_outstanding (pool);
1200 /* decrease the refcount that the buffer had to us */
1201 gst_object_unref (pool);