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 * After the buffer is created, it needs to be configured.
33 * gst_buffer_pool_get_config() get the current configuration structure from the
34 * pool. With gst_buffer_pool_config_set_params() and
35 * gst_buffer_pool_config_set_allocator() the bufferpool parameters and allocator
36 * can be configured. Other properties can be configured in the pool depending
37 * 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 * Buffer allocated from a bufferpool will automatically be returned to the pool
56 * 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 2012-03-28 (0.11.3)
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;
106 GstStructure *config;
112 GstAllocator *allocator;
113 GstAllocationParams params;
122 static void gst_buffer_pool_finalize (GObject * object);
124 G_DEFINE_TYPE (GstBufferPool, gst_buffer_pool, GST_TYPE_OBJECT);
126 static gboolean default_start (GstBufferPool * pool);
127 static gboolean default_stop (GstBufferPool * pool);
128 static gboolean default_set_config (GstBufferPool * pool,
129 GstStructure * config);
130 static GstFlowReturn default_alloc_buffer (GstBufferPool * pool,
131 GstBuffer ** buffer, GstBufferPoolAcquireParams * params);
132 static GstFlowReturn default_acquire_buffer (GstBufferPool * pool,
133 GstBuffer ** buffer, GstBufferPoolAcquireParams * params);
134 static void default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer);
135 static void default_free_buffer (GstBufferPool * pool, GstBuffer * buffer);
136 static void default_release_buffer (GstBufferPool * pool, GstBuffer * buffer);
139 gst_buffer_pool_class_init (GstBufferPoolClass * klass)
141 GObjectClass *gobject_class = (GObjectClass *) klass;
143 g_type_class_add_private (klass, sizeof (GstBufferPoolPrivate));
145 gobject_class->finalize = gst_buffer_pool_finalize;
147 klass->start = default_start;
148 klass->stop = default_stop;
149 klass->set_config = default_set_config;
150 klass->acquire_buffer = default_acquire_buffer;
151 klass->reset_buffer = default_reset_buffer;
152 klass->alloc_buffer = default_alloc_buffer;
153 klass->release_buffer = default_release_buffer;
154 klass->free_buffer = default_free_buffer;
156 GST_DEBUG_CATEGORY_INIT (gst_buffer_pool_debug, "bufferpool", 0,
161 gst_buffer_pool_init (GstBufferPool * pool)
163 GstBufferPoolPrivate *priv;
165 priv = pool->priv = GST_BUFFER_POOL_GET_PRIVATE (pool);
167 g_rec_mutex_init (&priv->rec_lock);
169 priv->poll = gst_poll_new_timer ();
170 priv->queue = gst_atomic_queue_new (10);
172 priv->active = FALSE;
173 priv->configured = FALSE;
174 priv->started = FALSE;
175 priv->config = gst_structure_new_id_empty (GST_QUARK (BUFFER_POOL_CONFIG));
176 gst_buffer_pool_config_set_params (priv->config, NULL, 0, 0, 0);
177 priv->allocator = NULL;
178 gst_allocation_params_init (&priv->params);
179 gst_buffer_pool_config_set_allocator (priv->config, priv->allocator,
181 gst_poll_write_control (priv->poll);
183 GST_DEBUG_OBJECT (pool, "created");
187 gst_buffer_pool_finalize (GObject * object)
190 GstBufferPoolPrivate *priv;
192 pool = GST_BUFFER_POOL_CAST (object);
195 GST_DEBUG_OBJECT (pool, "finalize");
197 gst_buffer_pool_set_active (pool, FALSE);
198 gst_atomic_queue_unref (priv->queue);
199 gst_poll_free (priv->poll);
200 gst_structure_free (priv->config);
201 g_rec_mutex_clear (&priv->rec_lock);
203 gst_object_unref (priv->allocator);
205 G_OBJECT_CLASS (gst_buffer_pool_parent_class)->finalize (object);
209 * gst_buffer_pool_new:
211 * Creates a new #GstBufferPool instance.
213 * Returns: (transfer full): a new #GstBufferPool instance
216 gst_buffer_pool_new (void)
218 GstBufferPool *result;
220 result = g_object_newv (GST_TYPE_BUFFER_POOL, 0, NULL);
221 GST_DEBUG_OBJECT (result, "created new buffer pool");
227 default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
228 GstBufferPoolAcquireParams * params)
230 GstBufferPoolPrivate *priv = pool->priv;
233 gst_buffer_new_allocate (priv->allocator, priv->size, &priv->params);
239 mark_meta_pooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
241 GstBufferPool *pool = user_data;
243 GST_DEBUG_OBJECT (pool, "marking meta %p as POOLED in buffer %p", *meta,
245 GST_META_FLAG_SET (*meta, GST_META_FLAG_POOLED);
246 GST_META_FLAG_SET (*meta, GST_META_FLAG_LOCKED);
252 do_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
253 GstBufferPoolAcquireParams * params)
255 GstBufferPoolPrivate *priv = pool->priv;
256 GstFlowReturn result;
257 gint cur_buffers, max_buffers;
258 GstBufferPoolClass *pclass;
260 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
262 if (G_UNLIKELY (!pclass->alloc_buffer))
265 max_buffers = priv->max_buffers;
267 /* increment the allocation counter */
268 cur_buffers = g_atomic_int_add (&priv->cur_buffers, 1);
269 if (max_buffers && cur_buffers >= max_buffers)
272 result = pclass->alloc_buffer (pool, buffer, params);
273 if (G_UNLIKELY (result != GST_FLOW_OK))
276 gst_buffer_foreach_meta (*buffer, mark_meta_pooled, pool);
278 GST_LOG_OBJECT (pool, "allocated buffer %d/%d, %p", cur_buffers,
279 max_buffers, buffer);
286 GST_ERROR_OBJECT (pool, "no alloc function");
287 return GST_FLOW_NOT_SUPPORTED;
291 GST_DEBUG_OBJECT (pool, "max buffers reached");
292 g_atomic_int_add (&priv->cur_buffers, -1);
297 GST_WARNING_OBJECT (pool, "alloc function failed");
298 g_atomic_int_add (&priv->cur_buffers, -1);
303 /* the default implementation for preallocating the buffers
306 default_start (GstBufferPool * pool)
309 GstBufferPoolPrivate *priv = pool->priv;
310 GstBufferPoolClass *pclass;
312 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
314 /* we need to prealloc buffers */
315 for (i = 0; i < priv->min_buffers; i++) {
318 if (do_alloc_buffer (pool, &buffer, NULL) != GST_FLOW_OK)
321 /* release to the queue, we call the vmethod directly, we don't need to do
322 * the other refcount handling right now. */
323 if (G_LIKELY (pclass->release_buffer))
324 pclass->release_buffer (pool, buffer);
331 GST_WARNING_OBJECT (pool, "failed to allocate buffer");
336 /* must be called with the lock */
338 do_start (GstBufferPool * pool)
340 GstBufferPoolPrivate *priv = pool->priv;
342 if (!priv->started) {
343 GstBufferPoolClass *pclass;
345 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
347 GST_LOG_OBJECT (pool, "starting");
348 /* start the pool, subclasses should allocate buffers and put them
350 if (G_LIKELY (pclass->start)) {
351 if (!pclass->start (pool))
354 priv->started = TRUE;
361 default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
363 gst_buffer_unref (buffer);
366 /* must be called with the lock */
368 default_stop (GstBufferPool * pool)
370 GstBufferPoolPrivate *priv = pool->priv;
372 GstBufferPoolClass *pclass;
374 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
377 while ((buffer = gst_atomic_queue_pop (priv->queue))) {
378 GST_LOG_OBJECT (pool, "freeing %p", buffer);
379 gst_poll_read_control (priv->poll);
381 if (G_LIKELY (pclass->free_buffer))
382 pclass->free_buffer (pool, buffer);
384 priv->cur_buffers = 0;
389 /* must be called with the lock */
391 do_stop (GstBufferPool * pool)
393 GstBufferPoolPrivate *priv = pool->priv;
396 GstBufferPoolClass *pclass;
398 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
400 GST_LOG_OBJECT (pool, "stopping");
401 if (G_LIKELY (pclass->stop)) {
402 if (!pclass->stop (pool))
405 priv->started = FALSE;
411 * gst_buffer_pool_set_active:
412 * @pool: a #GstBufferPool
413 * @active: the new active state
415 * Control the active state of @pool. When the pool is active, new calls to
416 * gst_buffer_pool_acquire_buffer() will return with GST_FLOW_FLUSHING.
418 * Activating the bufferpool will preallocate all resources in the pool based on
419 * the configuration of the pool.
421 * Deactivating will free the resources again when there are no outstanding
422 * buffers. When there are outstanding buffers, they will be freed as soon as
423 * they are all returned to the pool.
425 * Returns: %FALSE when the pool was not configured or when preallocation of the
429 gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
432 GstBufferPoolPrivate *priv;
434 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
436 GST_LOG_OBJECT (pool, "active %d", active);
440 GST_BUFFER_POOL_LOCK (pool);
441 /* just return if we are already in the right state */
442 if (priv->active == active)
445 /* we need to be configured */
446 if (!priv->configured)
450 if (!do_start (pool))
453 /* unset the flushing state now */
454 gst_poll_read_control (priv->poll);
455 g_atomic_int_set (&pool->flushing, 0);
459 /* set to flushing first */
460 g_atomic_int_set (&pool->flushing, 1);
461 gst_poll_write_control (priv->poll);
463 /* when all buffers are in the pool, free them. Else they will be
464 * freed when they are released */
465 outstanding = g_atomic_int_get (&priv->outstanding);
466 GST_LOG_OBJECT (pool, "outstanding buffers %d", outstanding);
467 if (outstanding == 0) {
472 priv->active = active;
473 GST_BUFFER_POOL_UNLOCK (pool);
479 GST_DEBUG_OBJECT (pool, "pool was in the right state");
480 GST_BUFFER_POOL_UNLOCK (pool);
485 GST_ERROR_OBJECT (pool, "pool was not configured");
486 GST_BUFFER_POOL_UNLOCK (pool);
491 GST_ERROR_OBJECT (pool, "start failed");
492 GST_BUFFER_POOL_UNLOCK (pool);
497 GST_WARNING_OBJECT (pool, "stop failed");
498 GST_BUFFER_POOL_UNLOCK (pool);
504 * gst_buffer_pool_is_active:
505 * @pool: a #GstBufferPool
507 * Check if @pool is active. A pool can be activated with the
508 * gst_buffer_pool_set_active() call.
510 * Returns: %TRUE when the pool is active.
513 gst_buffer_pool_is_active (GstBufferPool * pool)
517 GST_BUFFER_POOL_LOCK (pool);
518 res = pool->priv->active;
519 GST_BUFFER_POOL_UNLOCK (pool);
525 default_set_config (GstBufferPool * pool, GstStructure * config)
527 GstBufferPoolPrivate *priv = pool->priv;
529 guint size, min_buffers, max_buffers;
530 GstAllocator *allocator;
531 GstAllocationParams params;
533 /* parse the config and keep around */
534 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
538 if (!gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms))
541 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
544 priv->min_buffers = min_buffers;
545 priv->max_buffers = max_buffers;
546 priv->cur_buffers = 0;
549 gst_object_unref (priv->allocator);
550 if ((priv->allocator = allocator))
551 gst_object_ref (allocator);
552 priv->params = params;
558 GST_WARNING_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
564 * gst_buffer_pool_set_config:
565 * @pool: a #GstBufferPool
566 * @config: (transfer full): a #GstStructure
568 * Set the configuration of the pool. The pool must be inactive and all buffers
569 * allocated form this pool must be returned or else this function will do
570 * nothing and return FALSE.
572 * @config is a #GstStructure that contains the configuration parameters for
573 * the pool. A default and mandatory set of parameters can be configured with
574 * gst_buffer_pool_config_set_params(), gst_buffer_pool_config_set_allocator()
575 * and gst_buffer_pool_config_add_option().
577 * If the parameters in @config can not be set exactly, this function returns
578 * FALSE and will try to update as much state as possible. The new state can
579 * then be retrieved and refined with gst_buffer_pool_get_config().
581 * This function takes ownership of @config.
583 * Returns: TRUE when the configuration could be set.
586 gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
589 GstBufferPoolClass *pclass;
590 GstBufferPoolPrivate *priv;
592 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
593 g_return_val_if_fail (config != NULL, FALSE);
597 GST_BUFFER_POOL_LOCK (pool);
598 /* can't change the settings when active */
602 /* we can't change when outstanding buffers */
603 if (g_atomic_int_get (&priv->outstanding) != 0)
604 goto have_outstanding;
606 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
608 /* set the new config */
609 if (G_LIKELY (pclass->set_config))
610 result = pclass->set_config (pool, config);
616 gst_structure_free (priv->config);
617 priv->config = config;
619 /* now we are configured */
620 priv->configured = TRUE;
622 gst_structure_free (config);
624 GST_BUFFER_POOL_UNLOCK (pool);
631 gst_structure_free (config);
632 GST_WARNING_OBJECT (pool, "can't change config, we are active");
633 GST_BUFFER_POOL_UNLOCK (pool);
638 gst_structure_free (config);
639 GST_WARNING_OBJECT (pool, "can't change config, have outstanding buffers");
640 GST_BUFFER_POOL_UNLOCK (pool);
646 * gst_buffer_pool_get_config:
647 * @pool: a #GstBufferPool
649 * Get a copy of the current configuration of the pool. This configuration
650 * can either be modified and used for the gst_buffer_pool_set_config() call
651 * or it must be freed after usage.
653 * Returns: (transfer full): a copy of the current configuration of @pool. use
654 * gst_structure_free() after usage or gst_buffer_pool_set_config().
657 gst_buffer_pool_get_config (GstBufferPool * pool)
659 GstStructure *result;
661 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
663 GST_BUFFER_POOL_UNLOCK (pool);
664 result = gst_structure_copy (pool->priv->config);
665 GST_BUFFER_POOL_UNLOCK (pool);
670 static const gchar *empty_option[] = { NULL };
673 * gst_buffer_pool_get_options:
674 * @pool: a #GstBufferPool
676 * Get a NULL terminated array of string with supported bufferpool options for
677 * @pool. An option would typically be enabled with
678 * gst_buffer_pool_config_add_option().
680 * Returns: (array zero-terminated=1) (transfer none): a NULL terminated array of strings.
683 gst_buffer_pool_get_options (GstBufferPool * pool)
685 GstBufferPoolClass *pclass;
686 const gchar **result;
688 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
690 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
692 if (G_LIKELY (pclass->get_options)) {
693 if ((result = pclass->get_options (pool)) == NULL)
696 result = empty_option;
703 g_warning ("bufferpool subclass returned NULL options");
709 * gst_buffer_pool_has_option:
710 * @pool: a #GstBufferPool
713 * Check if the bufferpool supports @option.
715 * Returns: a NULL terminated array of strings.
718 gst_buffer_pool_has_option (GstBufferPool * pool, const gchar * option)
721 const gchar **options;
723 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
724 g_return_val_if_fail (option != NULL, FALSE);
726 options = gst_buffer_pool_get_options (pool);
728 for (i = 0; options[i]; i++) {
729 if (g_str_equal (options[i], option))
736 * gst_buffer_pool_config_set_params:
737 * @config: a #GstBufferPool configuration
738 * @caps: caps for the buffers
739 * @size: the size of each buffer, not including prefix and padding
740 * @min_buffers: the minimum amount of buffers to allocate.
741 * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
743 * Configure @config with the given parameters.
746 gst_buffer_pool_config_set_params (GstStructure * config, GstCaps * caps,
747 guint size, guint min_buffers, guint max_buffers)
749 g_return_if_fail (config != NULL);
750 g_return_if_fail (max_buffers == 0 || min_buffers <= max_buffers);
751 g_return_if_fail (caps == NULL || gst_caps_is_fixed (caps));
753 gst_structure_id_set (config,
754 GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
755 GST_QUARK (SIZE), G_TYPE_UINT, size,
756 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
757 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers, NULL);
761 * gst_buffer_pool_config_set_allocator:
762 * @config: a #GstBufferPool configuration
763 * @allocator: a #GstAllocator
764 * @params: #GstAllocationParams
766 * Set the @allocator and @params on @config.
768 * One of @allocator and @params can be NULL, but not both. When @allocator
769 * is NULL, the default allocator of the pool will use the values in @param
770 * to perform its allocation. When @param is NULL, the pool will use the
771 * provided allocator with its default #GstAllocationParams.
773 * A call to gst_buffer_pool_set_config() can update the allocator and params
774 * with the values that it is able to do. Some pools are, for example, not able
775 * to operate with different allocators or cannot allocate with the values
776 * specified in @params. Use gst_buffer_pool_get_config() to get the currently
780 gst_buffer_pool_config_set_allocator (GstStructure * config,
781 GstAllocator * allocator, const GstAllocationParams * params)
783 g_return_if_fail (config != NULL);
784 g_return_if_fail (allocator != NULL || params != NULL);
786 gst_structure_id_set (config,
787 GST_QUARK (ALLOCATOR), GST_TYPE_ALLOCATOR, allocator,
788 GST_QUARK (PARAMS), GST_TYPE_ALLOCATION_PARAMS, params, NULL);
792 * gst_buffer_pool_config_add_option:
793 * @config: a #GstBufferPool configuration
794 * @option: an option to add
796 * Enabled the option in @config. This will instruct the @bufferpool to enable
797 * the specified option on the buffers that it allocates.
799 * The supported options by @pool can be retrieved with gst_buffer_pool_get_options().
802 gst_buffer_pool_config_add_option (GstStructure * config, const gchar * option)
805 GValue option_value = { 0, };
808 g_return_if_fail (config != NULL);
810 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
812 len = gst_value_array_get_size (value);
813 for (i = 0; i < len; ++i) {
814 const GValue *nth_val = gst_value_array_get_value (value, i);
816 if (g_str_equal (option, g_value_get_string (nth_val)))
820 GValue new_array_val = { 0, };
822 g_value_init (&new_array_val, GST_TYPE_ARRAY);
823 gst_structure_id_take_value (config, GST_QUARK (OPTIONS), &new_array_val);
824 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
826 g_value_init (&option_value, G_TYPE_STRING);
827 g_value_set_string (&option_value, option);
828 gst_value_array_append_value ((GValue *) value, &option_value);
829 g_value_unset (&option_value);
833 * gst_buffer_pool_config_n_options:
834 * @config: a #GstBufferPool configuration
836 * Retrieve the number of values currently stored in the
837 * options array of the @config structure.
839 * Returns: the options array size as a #guint.
842 gst_buffer_pool_config_n_options (GstStructure * config)
847 g_return_val_if_fail (config != NULL, 0);
849 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
851 size = gst_value_array_get_size (value);
857 * gst_buffer_pool_config_get_option:
858 * @config: a #GstBufferPool configuration
859 * @index: position in the option array to read
861 * Parse an available @config and get the option
862 * at @index of the options API array.
864 * Returns: a #gchar of the option at @index.
867 gst_buffer_pool_config_get_option (GstStructure * config, guint index)
870 const gchar *ret = NULL;
872 g_return_val_if_fail (config != NULL, 0);
874 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
876 const GValue *option_value;
878 option_value = gst_value_array_get_value (value, index);
880 ret = g_value_get_string (option_value);
886 * gst_buffer_pool_config_has_option:
887 * @config: a #GstBufferPool configuration
890 * Check if @config contains @option
892 * Returns: TRUE if the options array contains @option.
895 gst_buffer_pool_config_has_option (GstStructure * config, const gchar * option)
900 g_return_val_if_fail (config != NULL, 0);
902 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
904 len = gst_value_array_get_size (value);
905 for (i = 0; i < len; ++i) {
906 const GValue *nth_val = gst_value_array_get_value (value, i);
908 if (g_str_equal (option, g_value_get_string (nth_val)))
916 * gst_buffer_pool_config_get_params:
917 * @config: (transfer none): a #GstBufferPool configuration
918 * @caps: (out) (transfer none) (allow-none): the caps of buffers
919 * @size: (out) (allow-none): the size of each buffer, not including prefix and padding
920 * @min_buffers: (out) (allow-none): the minimum amount of buffers to allocate.
921 * @max_buffers: (out) (allow-none): the maximum amount of buffers to allocate or 0 for unlimited.
923 * Get the configuration values from @config.
925 * Returns: %TRUE if all parameters could be fetched.
928 gst_buffer_pool_config_get_params (GstStructure * config, GstCaps ** caps,
929 guint * size, guint * min_buffers, guint * max_buffers)
931 g_return_val_if_fail (config != NULL, FALSE);
934 *caps = g_value_get_boxed (gst_structure_id_get_value (config,
937 return gst_structure_id_get (config,
938 GST_QUARK (SIZE), G_TYPE_UINT, size,
939 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
940 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers, NULL);
944 * gst_buffer_pool_config_get_allocator:
945 * @config: (transfer none): a #GstBufferPool configuration
946 * @allocator: (transfer none): a #GstAllocator
947 * @params: #GstAllocationParams
949 * Get the allocator and params from @config.
952 gst_buffer_pool_config_get_allocator (GstStructure * config,
953 GstAllocator ** allocator, GstAllocationParams * params)
955 g_return_val_if_fail (config != NULL, FALSE);
958 *allocator = g_value_get_object (gst_structure_id_get_value (config,
959 GST_QUARK (ALLOCATOR)));
961 GstAllocationParams *p;
963 p = g_value_get_boxed (gst_structure_id_get_value (config,
964 GST_QUARK (PARAMS)));
968 gst_allocation_params_init (params);
975 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
976 GstBufferPoolAcquireParams * params)
978 GstFlowReturn result;
979 GstBufferPoolPrivate *priv = pool->priv;
982 if (G_UNLIKELY (GST_BUFFER_POOL_IS_FLUSHING (pool)))
985 /* try to get a buffer from the queue */
986 *buffer = gst_atomic_queue_pop (priv->queue);
987 if (G_LIKELY (*buffer)) {
988 gst_poll_read_control (priv->poll);
989 result = GST_FLOW_OK;
990 GST_LOG_OBJECT (pool, "acquired buffer %p", *buffer);
994 /* no buffer, try to allocate some more */
995 GST_LOG_OBJECT (pool, "no buffer, trying to allocate");
996 result = do_alloc_buffer (pool, buffer, NULL);
997 if (G_LIKELY (result == GST_FLOW_OK))
998 /* we have a buffer, return it */
1001 if (G_UNLIKELY (result != GST_FLOW_EOS))
1002 /* something went wrong, return error */
1005 /* check if we need to wait */
1006 if (params && (params->flags & GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT)) {
1007 GST_LOG_OBJECT (pool, "no more buffers");
1012 GST_LOG_OBJECT (pool, "waiting for free buffers");
1013 gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
1021 GST_DEBUG_OBJECT (pool, "we are flushing");
1022 return GST_FLOW_FLUSHING;
1027 dec_outstanding (GstBufferPool * pool)
1029 if (g_atomic_int_dec_and_test (&pool->priv->outstanding)) {
1030 /* all buffers are returned to the pool, see if we need to free them */
1031 if (GST_BUFFER_POOL_IS_FLUSHING (pool)) {
1032 /* take the lock so that set_active is not run concurrently */
1033 GST_BUFFER_POOL_LOCK (pool);
1034 /* recheck the flushing state in the lock, the pool could have been
1035 * set to active again */
1036 if (GST_BUFFER_POOL_IS_FLUSHING (pool))
1039 GST_BUFFER_POOL_UNLOCK (pool);
1045 remove_meta_unpooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
1047 if (!GST_META_FLAG_IS_SET (*meta, GST_META_FLAG_POOLED)) {
1048 GST_META_FLAG_UNSET (*meta, GST_META_FLAG_LOCKED);
1055 default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer)
1057 GST_BUFFER_FLAGS (buffer) = 0;
1059 GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
1060 GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
1061 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
1062 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
1063 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
1065 /* remove all metadata without the POOLED flag */
1066 gst_buffer_foreach_meta (buffer, remove_meta_unpooled, pool);
1070 * gst_buffer_pool_acquire_buffer:
1071 * @pool: a #GstBufferPool
1072 * @buffer: (out): a location for a #GstBuffer
1073 * @params: (transfer none) (allow-none) parameters.
1075 * Acquire a buffer from @pool. @buffer should point to a memory location that
1076 * can hold a pointer to the new buffer.
1078 * @params can be NULL or contain optional parameters to influence the allocation.
1080 * Returns: a #GstFlowReturn such as GST_FLOW_FLUSHING when the pool is
1084 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
1085 GstBufferPoolAcquireParams * params)
1087 GstBufferPoolClass *pclass;
1088 GstFlowReturn result;
1090 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), GST_FLOW_ERROR);
1091 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
1093 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
1095 /* assume we'll have one more outstanding buffer we need to do that so
1096 * that concurrent set_active doesn't clear the buffers */
1097 g_atomic_int_inc (&pool->priv->outstanding);
1099 if (G_LIKELY (pclass->acquire_buffer))
1100 result = pclass->acquire_buffer (pool, buffer, params);
1102 result = GST_FLOW_NOT_SUPPORTED;
1104 if (G_LIKELY (result == GST_FLOW_OK)) {
1105 /* all buffers from the pool point to the pool and have the refcount of the
1106 * pool incremented */
1107 (*buffer)->pool = gst_object_ref (pool);
1109 dec_outstanding (pool);
1116 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
1118 /* keep it around in our queue */
1119 GST_LOG_OBJECT (pool, "released buffer %p", buffer);
1120 gst_atomic_queue_push (pool->priv->queue, buffer);
1121 gst_poll_write_control (pool->priv->poll);
1125 * gst_buffer_pool_release_buffer:
1126 * @pool: a #GstBufferPool
1127 * @buffer: (transfer full): a #GstBuffer
1129 * Release @buffer to @pool. @buffer should have previously been allocated from
1130 * @pool with gst_buffer_pool_acquire_buffer().
1132 * This function is usually called automatically when the last ref on @buffer
1136 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
1138 GstBufferPoolClass *pclass;
1140 g_return_if_fail (GST_IS_BUFFER_POOL (pool));
1141 g_return_if_fail (buffer != NULL);
1143 /* check that the buffer is ours, all buffers returned to the pool have the
1144 * pool member set to NULL and the pool refcount decreased */
1145 if (!g_atomic_pointer_compare_and_exchange (&buffer->pool, pool, NULL))
1148 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
1150 /* reset the buffer when needed */
1151 if (G_LIKELY (pclass->reset_buffer))
1152 pclass->reset_buffer (pool, buffer);
1154 if (G_LIKELY (pclass->release_buffer))
1155 pclass->release_buffer (pool, buffer);
1157 dec_outstanding (pool);
1159 /* decrease the refcount that the buffer had to us */
1160 gst_object_unref (pool);