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.
66 #include "gst_private.h"
67 #include "glib-compat-private.h"
73 #include <sys/types.h>
75 #include "gstatomicqueue.h"
81 #include "gstbufferpool.h"
83 GST_DEBUG_CATEGORY_STATIC (gst_buffer_pool_debug);
84 #define GST_CAT_DEFAULT gst_buffer_pool_debug
86 #define GST_BUFFER_POOL_GET_PRIVATE(obj) \
87 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BUFFER_POOL, GstBufferPoolPrivate))
89 #define GST_BUFFER_POOL_LOCK(pool) (g_rec_mutex_lock(&pool->priv->rec_lock))
90 #define GST_BUFFER_POOL_UNLOCK(pool) (g_rec_mutex_unlock(&pool->priv->rec_lock))
92 struct _GstBufferPoolPrivate
94 GstAtomicQueue *queue;
101 gint outstanding; /* number of buffers that are in use */
104 GstStructure *config;
110 GstAllocator *allocator;
111 GstAllocationParams params;
114 static void gst_buffer_pool_finalize (GObject * object);
116 G_DEFINE_TYPE (GstBufferPool, gst_buffer_pool, GST_TYPE_OBJECT);
118 static gboolean default_start (GstBufferPool * pool);
119 static gboolean default_stop (GstBufferPool * pool);
120 static gboolean default_set_config (GstBufferPool * pool,
121 GstStructure * config);
122 static GstFlowReturn default_alloc_buffer (GstBufferPool * pool,
123 GstBuffer ** buffer, GstBufferPoolAcquireParams * params);
124 static GstFlowReturn default_acquire_buffer (GstBufferPool * pool,
125 GstBuffer ** buffer, GstBufferPoolAcquireParams * params);
126 static void default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer);
127 static void default_free_buffer (GstBufferPool * pool, GstBuffer * buffer);
128 static void default_release_buffer (GstBufferPool * pool, GstBuffer * buffer);
131 gst_buffer_pool_class_init (GstBufferPoolClass * klass)
133 GObjectClass *gobject_class = (GObjectClass *) klass;
135 g_type_class_add_private (klass, sizeof (GstBufferPoolPrivate));
137 gobject_class->finalize = gst_buffer_pool_finalize;
139 klass->start = default_start;
140 klass->stop = default_stop;
141 klass->set_config = default_set_config;
142 klass->acquire_buffer = default_acquire_buffer;
143 klass->reset_buffer = default_reset_buffer;
144 klass->alloc_buffer = default_alloc_buffer;
145 klass->release_buffer = default_release_buffer;
146 klass->free_buffer = default_free_buffer;
148 GST_DEBUG_CATEGORY_INIT (gst_buffer_pool_debug, "bufferpool", 0,
153 gst_buffer_pool_init (GstBufferPool * pool)
155 GstBufferPoolPrivate *priv;
157 priv = pool->priv = GST_BUFFER_POOL_GET_PRIVATE (pool);
159 g_rec_mutex_init (&priv->rec_lock);
161 priv->poll = gst_poll_new_timer ();
162 priv->queue = gst_atomic_queue_new (16);
164 priv->active = FALSE;
165 priv->configured = FALSE;
166 priv->started = FALSE;
167 priv->config = gst_structure_new_id_empty (GST_QUARK (BUFFER_POOL_CONFIG));
168 gst_buffer_pool_config_set_params (priv->config, NULL, 0, 0, 0);
169 priv->allocator = NULL;
170 gst_allocation_params_init (&priv->params);
171 gst_buffer_pool_config_set_allocator (priv->config, priv->allocator,
173 /* 1 control write for flushing */
174 gst_poll_write_control (priv->poll);
175 /* 1 control write for marking that we are not waiting for poll */
176 gst_poll_write_control (priv->poll);
178 GST_DEBUG_OBJECT (pool, "created");
182 gst_buffer_pool_finalize (GObject * object)
185 GstBufferPoolPrivate *priv;
187 pool = GST_BUFFER_POOL_CAST (object);
190 GST_DEBUG_OBJECT (pool, "finalize");
192 gst_buffer_pool_set_active (pool, FALSE);
193 gst_atomic_queue_unref (priv->queue);
194 gst_poll_free (priv->poll);
195 gst_structure_free (priv->config);
196 g_rec_mutex_clear (&priv->rec_lock);
198 gst_object_unref (priv->allocator);
200 G_OBJECT_CLASS (gst_buffer_pool_parent_class)->finalize (object);
204 * gst_buffer_pool_new:
206 * Creates a new #GstBufferPool instance.
208 * Returns: (transfer floating): a new #GstBufferPool instance
211 gst_buffer_pool_new (void)
213 GstBufferPool *result;
215 result = g_object_newv (GST_TYPE_BUFFER_POOL, 0, NULL);
216 GST_DEBUG_OBJECT (result, "created new buffer pool");
222 default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
223 GstBufferPoolAcquireParams * params)
225 GstBufferPoolPrivate *priv = pool->priv;
228 gst_buffer_new_allocate (priv->allocator, priv->size, &priv->params);
234 mark_meta_pooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
236 GstBufferPool *pool = user_data;
238 GST_DEBUG_OBJECT (pool, "marking meta %p as POOLED in buffer %p", *meta,
240 GST_META_FLAG_SET (*meta, GST_META_FLAG_POOLED);
241 GST_META_FLAG_SET (*meta, GST_META_FLAG_LOCKED);
247 do_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
248 GstBufferPoolAcquireParams * params)
250 GstBufferPoolPrivate *priv = pool->priv;
251 GstFlowReturn result;
252 gint cur_buffers, max_buffers;
253 GstBufferPoolClass *pclass;
255 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
257 if (G_UNLIKELY (!pclass->alloc_buffer))
260 max_buffers = priv->max_buffers;
262 /* increment the allocation counter */
263 cur_buffers = g_atomic_int_add (&priv->cur_buffers, 1);
264 if (max_buffers && cur_buffers >= max_buffers)
267 result = pclass->alloc_buffer (pool, buffer, params);
268 if (G_UNLIKELY (result != GST_FLOW_OK))
271 /* lock all metadata and mark as pooled, we want this to remain on
272 * the buffer and we want to remove any other metadata that gets added
274 gst_buffer_foreach_meta (*buffer, mark_meta_pooled, pool);
276 /* un-tag memory, this is how we expect the buffer when it is
278 GST_BUFFER_FLAG_UNSET (*buffer, GST_BUFFER_FLAG_TAG_MEMORY);
280 GST_LOG_OBJECT (pool, "allocated buffer %d/%d, %p", cur_buffers,
281 max_buffers, *buffer);
288 GST_ERROR_OBJECT (pool, "no alloc function");
289 return GST_FLOW_NOT_SUPPORTED;
293 GST_DEBUG_OBJECT (pool, "max buffers reached");
294 g_atomic_int_add (&priv->cur_buffers, -1);
299 GST_WARNING_OBJECT (pool, "alloc function failed");
300 g_atomic_int_add (&priv->cur_buffers, -1);
305 /* the default implementation for preallocating the buffers in the pool */
307 default_start (GstBufferPool * pool)
310 GstBufferPoolPrivate *priv = pool->priv;
311 GstBufferPoolClass *pclass;
313 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
315 /* we need to prealloc buffers */
316 for (i = 0; i < priv->min_buffers; i++) {
319 if (do_alloc_buffer (pool, &buffer, NULL) != GST_FLOW_OK)
322 /* release to the queue, we call the vmethod directly, we don't need to do
323 * the other refcount handling right now. */
324 if (G_LIKELY (pclass->release_buffer))
325 pclass->release_buffer (pool, buffer);
332 GST_WARNING_OBJECT (pool, "failed to allocate buffer");
337 /* must be called with the lock */
339 do_start (GstBufferPool * pool)
341 GstBufferPoolPrivate *priv = pool->priv;
343 if (!priv->started) {
344 GstBufferPoolClass *pclass;
346 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
348 GST_LOG_OBJECT (pool, "starting");
349 /* start the pool, subclasses should allocate buffers and put them
351 if (G_LIKELY (pclass->start)) {
352 if (!pclass->start (pool))
355 priv->started = TRUE;
361 default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
363 gst_buffer_unref (buffer);
367 do_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
369 GstBufferPoolPrivate *priv;
370 GstBufferPoolClass *pclass;
373 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
375 g_atomic_int_add (&priv->cur_buffers, -1);
376 GST_LOG_OBJECT (pool, "freeing buffer %p (%u left)", buffer,
379 if (G_LIKELY (pclass->free_buffer))
380 pclass->free_buffer (pool, buffer);
383 /* must be called with the lock */
385 default_stop (GstBufferPool * pool)
387 GstBufferPoolPrivate *priv = pool->priv;
391 while ((buffer = gst_atomic_queue_pop (priv->queue))) {
392 gst_poll_read_control (priv->poll);
393 do_free_buffer (pool, buffer);
395 return priv->cur_buffers == 0;
398 /* must be called with the lock */
400 do_stop (GstBufferPool * pool)
402 GstBufferPoolPrivate *priv = pool->priv;
405 GstBufferPoolClass *pclass;
407 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
409 GST_LOG_OBJECT (pool, "stopping");
410 if (G_LIKELY (pclass->stop)) {
411 if (!pclass->stop (pool))
414 priv->started = FALSE;
419 /* must be called with the lock */
421 do_set_flushing (GstBufferPool * pool, gboolean flushing)
423 GstBufferPoolPrivate *priv = pool->priv;
424 GstBufferPoolClass *pclass;
426 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
428 if (GST_BUFFER_POOL_IS_FLUSHING (pool) == flushing)
432 g_atomic_int_set (&pool->flushing, 1);
433 gst_poll_write_control (priv->poll);
435 if (pclass->flush_start)
436 pclass->flush_start (pool);
438 if (pclass->flush_stop)
439 pclass->flush_stop (pool);
441 gst_poll_read_control (priv->poll);
442 g_atomic_int_set (&pool->flushing, 0);
447 * gst_buffer_pool_set_active:
448 * @pool: a #GstBufferPool
449 * @active: the new active state
451 * Control the active state of @pool. When the pool is inactive, new calls to
452 * gst_buffer_pool_acquire_buffer() will return with %GST_FLOW_FLUSHING.
454 * Activating the bufferpool will preallocate all resources in the pool based on
455 * the configuration of the pool.
457 * Deactivating will free the resources again when there are no outstanding
458 * buffers. When there are outstanding buffers, they will be freed as soon as
459 * they are all returned to the pool.
461 * Returns: %FALSE when the pool was not configured or when preallocation of the
465 gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
468 GstBufferPoolPrivate *priv;
470 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
472 GST_LOG_OBJECT (pool, "active %d", active);
476 GST_BUFFER_POOL_LOCK (pool);
477 /* just return if we are already in the right state */
478 if (priv->active == active)
481 /* we need to be configured */
482 if (!priv->configured)
486 if (!do_start (pool))
489 /* flush_stop my release buffers, setting to active to avoid running
490 * do_stop while activating the pool */
493 /* unset the flushing state now */
494 do_set_flushing (pool, FALSE);
498 /* set to flushing first */
499 do_set_flushing (pool, TRUE);
501 /* when all buffers are in the pool, free them. Else they will be
502 * freed when they are released */
503 outstanding = g_atomic_int_get (&priv->outstanding);
504 GST_LOG_OBJECT (pool, "outstanding buffers %d", outstanding);
505 if (outstanding == 0) {
510 priv->active = FALSE;
512 GST_BUFFER_POOL_UNLOCK (pool);
518 GST_DEBUG_OBJECT (pool, "pool was in the right state");
519 GST_BUFFER_POOL_UNLOCK (pool);
524 GST_ERROR_OBJECT (pool, "pool was not configured");
525 GST_BUFFER_POOL_UNLOCK (pool);
530 GST_ERROR_OBJECT (pool, "start failed");
531 GST_BUFFER_POOL_UNLOCK (pool);
536 GST_WARNING_OBJECT (pool, "stop failed");
537 GST_BUFFER_POOL_UNLOCK (pool);
543 * gst_buffer_pool_is_active:
544 * @pool: a #GstBufferPool
546 * Check if @pool is active. A pool can be activated with the
547 * gst_buffer_pool_set_active() call.
549 * Returns: %TRUE when the pool is active.
552 gst_buffer_pool_is_active (GstBufferPool * pool)
556 GST_BUFFER_POOL_LOCK (pool);
557 res = pool->priv->active;
558 GST_BUFFER_POOL_UNLOCK (pool);
564 default_set_config (GstBufferPool * pool, GstStructure * config)
566 GstBufferPoolPrivate *priv = pool->priv;
568 guint size, min_buffers, max_buffers;
569 GstAllocator *allocator;
570 GstAllocationParams params;
572 /* parse the config and keep around */
573 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
577 if (!gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms))
580 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
583 priv->min_buffers = min_buffers;
584 priv->max_buffers = max_buffers;
585 priv->cur_buffers = 0;
588 gst_object_unref (priv->allocator);
589 if ((priv->allocator = allocator))
590 gst_object_ref (allocator);
591 priv->params = params;
597 GST_WARNING_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
603 * gst_buffer_pool_set_config:
604 * @pool: a #GstBufferPool
605 * @config: (transfer full): a #GstStructure
607 * Set the configuration of the pool. If the pool is already configured, and
608 * the configuration haven't change, this function will return %TRUE. If the
609 * pool is active, this function will try deactivating it. Buffers allocated
610 * form this pool must be returned or else this function will do nothing and
613 * @config is a #GstStructure that contains the configuration parameters for
614 * the pool. A default and mandatory set of parameters can be configured with
615 * gst_buffer_pool_config_set_params(), gst_buffer_pool_config_set_allocator()
616 * and gst_buffer_pool_config_add_option().
618 * If the parameters in @config can not be set exactly, this function returns
619 * %FALSE and will try to update as much state as possible. The new state can
620 * then be retrieved and refined with gst_buffer_pool_get_config().
622 * This function takes ownership of @config.
624 * Returns: %TRUE when the configuration could be set.
627 gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
630 GstBufferPoolClass *pclass;
631 GstBufferPoolPrivate *priv;
633 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
634 g_return_val_if_fail (config != NULL, FALSE);
638 GST_BUFFER_POOL_LOCK (pool);
640 /* nothing to do if config is unchanged */
641 if (priv->configured && gst_structure_is_equal (config, priv->config))
642 goto config_unchanged;
644 /* can't change the settings when active */
646 GST_BUFFER_POOL_UNLOCK (pool);
647 if (!gst_buffer_pool_set_active (pool, FALSE)) {
648 GST_BUFFER_POOL_LOCK (pool);
651 GST_BUFFER_POOL_LOCK (pool);
653 /* not likely but as we released the lock */
658 /* we can't change when outstanding buffers */
659 if (g_atomic_int_get (&priv->outstanding) != 0)
660 goto have_outstanding;
662 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
664 /* set the new config */
665 if (G_LIKELY (pclass->set_config))
666 result = pclass->set_config (pool, config);
670 /* save the config regardless of the result so user can read back the
671 * modified config and evaluate if the changes are acceptable */
673 gst_structure_free (priv->config);
674 priv->config = config;
677 /* now we are configured */
678 priv->configured = TRUE;
680 GST_BUFFER_POOL_UNLOCK (pool);
686 gst_structure_free (config);
687 GST_BUFFER_POOL_UNLOCK (pool);
693 gst_structure_free (config);
694 GST_WARNING_OBJECT (pool, "can't change config, we are active");
695 GST_BUFFER_POOL_UNLOCK (pool);
700 gst_structure_free (config);
701 GST_WARNING_OBJECT (pool, "can't change config, have outstanding buffers");
702 GST_BUFFER_POOL_UNLOCK (pool);
708 * gst_buffer_pool_get_config:
709 * @pool: a #GstBufferPool
711 * Get a copy of the current configuration of the pool. This configuration
712 * can either be modified and used for the gst_buffer_pool_set_config() call
713 * or it must be freed after usage.
715 * Returns: (transfer full): a copy of the current configuration of @pool. use
716 * gst_structure_free() after usage or gst_buffer_pool_set_config().
719 gst_buffer_pool_get_config (GstBufferPool * pool)
721 GstStructure *result;
723 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
725 GST_BUFFER_POOL_LOCK (pool);
726 result = gst_structure_copy (pool->priv->config);
727 GST_BUFFER_POOL_UNLOCK (pool);
732 static const gchar *empty_option[] = { NULL };
735 * gst_buffer_pool_get_options:
736 * @pool: a #GstBufferPool
738 * Get a %NULL terminated array of string with supported bufferpool options for
739 * @pool. An option would typically be enabled with
740 * gst_buffer_pool_config_add_option().
742 * Returns: (array zero-terminated=1) (transfer none): a %NULL terminated array
746 gst_buffer_pool_get_options (GstBufferPool * pool)
748 GstBufferPoolClass *pclass;
749 const gchar **result;
751 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
753 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
755 if (G_LIKELY (pclass->get_options)) {
756 if ((result = pclass->get_options (pool)) == NULL)
759 result = empty_option;
766 g_warning ("bufferpool subclass returned NULL options");
772 * gst_buffer_pool_has_option:
773 * @pool: a #GstBufferPool
776 * Check if the bufferpool supports @option.
778 * Returns: %TRUE if the buffer pool contains @option.
781 gst_buffer_pool_has_option (GstBufferPool * pool, const gchar * option)
784 const gchar **options;
786 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
787 g_return_val_if_fail (option != NULL, FALSE);
789 options = gst_buffer_pool_get_options (pool);
791 for (i = 0; options[i]; i++) {
792 if (g_str_equal (options[i], option))
799 * gst_buffer_pool_config_set_params:
800 * @config: a #GstBufferPool configuration
801 * @caps: caps for the buffers
802 * @size: the size of each buffer, not including prefix and padding
803 * @min_buffers: the minimum amount of buffers to allocate.
804 * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
806 * Configure @config with the given parameters.
809 gst_buffer_pool_config_set_params (GstStructure * config, GstCaps * caps,
810 guint size, guint min_buffers, guint max_buffers)
812 g_return_if_fail (config != NULL);
813 g_return_if_fail (max_buffers == 0 || min_buffers <= max_buffers);
814 g_return_if_fail (caps == NULL || gst_caps_is_fixed (caps));
816 gst_structure_id_set (config,
817 GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
818 GST_QUARK (SIZE), G_TYPE_UINT, size,
819 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
820 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers, NULL);
824 * gst_buffer_pool_config_set_allocator:
825 * @config: a #GstBufferPool configuration
826 * @allocator: a #GstAllocator
827 * @params: #GstAllocationParams
829 * Set the @allocator and @params on @config.
831 * One of @allocator and @params can be %NULL, but not both. When @allocator
832 * is %NULL, the default allocator of the pool will use the values in @param
833 * to perform its allocation. When @param is %NULL, the pool will use the
834 * provided @allocator with its default #GstAllocationParams.
836 * A call to gst_buffer_pool_set_config() can update the allocator and params
837 * with the values that it is able to do. Some pools are, for example, not able
838 * to operate with different allocators or cannot allocate with the values
839 * specified in @params. Use gst_buffer_pool_get_config() to get the currently
843 gst_buffer_pool_config_set_allocator (GstStructure * config,
844 GstAllocator * allocator, const GstAllocationParams * params)
846 g_return_if_fail (config != NULL);
847 g_return_if_fail (allocator != NULL || params != NULL);
849 gst_structure_id_set (config,
850 GST_QUARK (ALLOCATOR), GST_TYPE_ALLOCATOR, allocator,
851 GST_QUARK (PARAMS), GST_TYPE_ALLOCATION_PARAMS, params, NULL);
855 * gst_buffer_pool_config_add_option:
856 * @config: a #GstBufferPool configuration
857 * @option: an option to add
859 * Enabled the option in @config. This will instruct the @bufferpool to enable
860 * the specified option on the buffers that it allocates.
862 * The supported options by @pool can be retrieved with gst_buffer_pool_get_options().
865 gst_buffer_pool_config_add_option (GstStructure * config, const gchar * option)
868 GValue option_value = { 0, };
871 g_return_if_fail (config != NULL);
873 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
875 len = gst_value_array_get_size (value);
876 for (i = 0; i < len; ++i) {
877 const GValue *nth_val = gst_value_array_get_value (value, i);
879 if (g_str_equal (option, g_value_get_string (nth_val)))
883 GValue new_array_val = { 0, };
885 g_value_init (&new_array_val, GST_TYPE_ARRAY);
886 gst_structure_id_take_value (config, GST_QUARK (OPTIONS), &new_array_val);
887 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
889 g_value_init (&option_value, G_TYPE_STRING);
890 g_value_set_string (&option_value, option);
891 gst_value_array_append_and_take_value ((GValue *) value, &option_value);
895 * gst_buffer_pool_config_n_options:
896 * @config: a #GstBufferPool configuration
898 * Retrieve the number of values currently stored in the options array of the
901 * Returns: the options array size as a #guint.
904 gst_buffer_pool_config_n_options (GstStructure * config)
909 g_return_val_if_fail (config != NULL, 0);
911 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
913 size = gst_value_array_get_size (value);
919 * gst_buffer_pool_config_get_option:
920 * @config: a #GstBufferPool configuration
921 * @index: position in the option array to read
923 * Parse an available @config and get the option at @index of the options API
926 * Returns: a #gchar of the option at @index.
929 gst_buffer_pool_config_get_option (GstStructure * config, guint index)
932 const gchar *ret = NULL;
934 g_return_val_if_fail (config != NULL, 0);
936 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
938 const GValue *option_value;
940 option_value = gst_value_array_get_value (value, index);
942 ret = g_value_get_string (option_value);
948 * gst_buffer_pool_config_has_option:
949 * @config: a #GstBufferPool configuration
952 * Check if @config contains @option.
954 * Returns: %TRUE if the options array contains @option.
957 gst_buffer_pool_config_has_option (GstStructure * config, const gchar * option)
962 g_return_val_if_fail (config != NULL, 0);
964 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
966 len = gst_value_array_get_size (value);
967 for (i = 0; i < len; ++i) {
968 const GValue *nth_val = gst_value_array_get_value (value, i);
970 if (g_str_equal (option, g_value_get_string (nth_val)))
978 * gst_buffer_pool_config_get_params:
979 * @config: (transfer none): a #GstBufferPool configuration
980 * @caps: (out) (transfer none) (allow-none): the caps of buffers
981 * @size: (out) (allow-none): the size of each buffer, not including prefix and padding
982 * @min_buffers: (out) (allow-none): the minimum amount of buffers to allocate.
983 * @max_buffers: (out) (allow-none): the maximum amount of buffers to allocate or 0 for unlimited.
985 * Get the configuration values from @config.
987 * Returns: %TRUE if all parameters could be fetched.
990 gst_buffer_pool_config_get_params (GstStructure * config, GstCaps ** caps,
991 guint * size, guint * min_buffers, guint * max_buffers)
993 g_return_val_if_fail (config != NULL, FALSE);
996 *caps = g_value_get_boxed (gst_structure_id_get_value (config,
999 return gst_structure_id_get (config,
1000 GST_QUARK (SIZE), G_TYPE_UINT, size,
1001 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
1002 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers, NULL);
1006 * gst_buffer_pool_config_get_allocator:
1007 * @config: (transfer none): a #GstBufferPool configuration
1008 * @allocator: (transfer none): a #GstAllocator
1009 * @params: #GstAllocationParams
1011 * Get the @allocator and @params from @config.
1013 * Returns: %TRUE, if the values are set.
1016 gst_buffer_pool_config_get_allocator (GstStructure * config,
1017 GstAllocator ** allocator, GstAllocationParams * params)
1019 g_return_val_if_fail (config != NULL, FALSE);
1022 *allocator = g_value_get_object (gst_structure_id_get_value (config,
1023 GST_QUARK (ALLOCATOR)));
1025 GstAllocationParams *p;
1027 p = g_value_get_boxed (gst_structure_id_get_value (config,
1028 GST_QUARK (PARAMS)));
1032 gst_allocation_params_init (params);
1039 * gst_buffer_pool_config_validate_params:
1040 * @config: (transfer none): a #GstBufferPool configuration
1041 * @caps: (transfer none): the excepted caps of buffers
1042 * @size: the expected size of each buffer, not including prefix and padding
1043 * @min_buffers: the expected minimum amount of buffers to allocate.
1044 * @max_buffers: the expect maximum amount of buffers to allocate or 0 for unlimited.
1046 * Validate that changes made to @config are still valid in the context of the
1047 * expected parameters. This function is a helper that can be used to validate
1048 * changes made by a pool to a config when gst_buffer_pool_set_config()
1049 * returns %FALSE. This expects that @caps and @size haven't changed, and that
1050 * @min_buffers aren't lower then what we initially expected. This does not check
1051 * if options or allocator parameters.
1055 * Returns: %TRUE, if the parameters are valid in this context.
1058 gst_buffer_pool_config_validate_params (GstStructure * config, GstCaps * caps,
1059 guint size, guint min_buffers, G_GNUC_UNUSED guint max_buffers)
1062 guint newsize, newmin;
1063 gboolean ret = FALSE;
1065 g_return_val_if_fail (config != NULL, FALSE);
1067 gst_buffer_pool_config_get_params (config, &newcaps, &newsize, &newmin, NULL);
1069 if (gst_caps_is_equal (caps, newcaps) && (size == newsize)
1070 && (newmin >= min_buffers))
1076 static GstFlowReturn
1077 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
1078 GstBufferPoolAcquireParams * params)
1080 GstFlowReturn result;
1081 GstBufferPoolPrivate *priv = pool->priv;
1084 if (G_UNLIKELY (GST_BUFFER_POOL_IS_FLUSHING (pool)))
1087 /* try to get a buffer from the queue */
1088 *buffer = gst_atomic_queue_pop (priv->queue);
1089 if (G_LIKELY (*buffer)) {
1090 gst_poll_read_control (priv->poll);
1091 result = GST_FLOW_OK;
1092 GST_LOG_OBJECT (pool, "acquired buffer %p", *buffer);
1096 /* no buffer, try to allocate some more */
1097 GST_LOG_OBJECT (pool, "no buffer, trying to allocate");
1098 result = do_alloc_buffer (pool, buffer, NULL);
1099 if (G_LIKELY (result == GST_FLOW_OK))
1100 /* we have a buffer, return it */
1103 if (G_UNLIKELY (result != GST_FLOW_EOS))
1104 /* something went wrong, return error */
1107 /* check if we need to wait */
1108 if (params && (params->flags & GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT)) {
1109 GST_LOG_OBJECT (pool, "no more buffers");
1113 /* now we release the control socket, we wait for a buffer release or
1115 gst_poll_read_control (pool->priv->poll);
1116 GST_LOG_OBJECT (pool, "waiting for free buffers or flushing");
1117 gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
1118 gst_poll_write_control (pool->priv->poll);
1126 GST_DEBUG_OBJECT (pool, "we are flushing");
1127 return GST_FLOW_FLUSHING;
1132 dec_outstanding (GstBufferPool * pool)
1134 if (g_atomic_int_dec_and_test (&pool->priv->outstanding)) {
1135 /* all buffers are returned to the pool, see if we need to free them */
1136 if (GST_BUFFER_POOL_IS_FLUSHING (pool)) {
1137 /* take the lock so that set_active is not run concurrently */
1138 GST_BUFFER_POOL_LOCK (pool);
1139 /* now that we have the lock, check if we have been de-activated with
1140 * outstanding buffers */
1141 if (!pool->priv->active)
1144 GST_BUFFER_POOL_UNLOCK (pool);
1150 remove_meta_unpooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
1152 if (!GST_META_FLAG_IS_SET (*meta, GST_META_FLAG_POOLED)) {
1153 GST_META_FLAG_UNSET (*meta, GST_META_FLAG_LOCKED);
1160 default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer)
1162 GST_BUFFER_FLAGS (buffer) &= GST_BUFFER_FLAG_TAG_MEMORY;
1164 GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
1165 GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
1166 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
1167 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
1168 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
1170 /* remove all metadata without the POOLED flag */
1171 gst_buffer_foreach_meta (buffer, remove_meta_unpooled, pool);
1175 * gst_buffer_pool_acquire_buffer:
1176 * @pool: a #GstBufferPool
1177 * @buffer: (out): a location for a #GstBuffer
1178 * @params: (transfer none) (allow-none): parameters.
1180 * Acquire a buffer from @pool. @buffer should point to a memory location that
1181 * can hold a pointer to the new buffer.
1183 * @params can be %NULL or contain optional parameters to influence the
1186 * Returns: a #GstFlowReturn such as %GST_FLOW_FLUSHING when the pool is
1190 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
1191 GstBufferPoolAcquireParams * params)
1193 GstBufferPoolClass *pclass;
1194 GstFlowReturn result;
1196 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), GST_FLOW_ERROR);
1197 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
1199 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
1201 /* assume we'll have one more outstanding buffer we need to do that so
1202 * that concurrent set_active doesn't clear the buffers */
1203 g_atomic_int_inc (&pool->priv->outstanding);
1205 if (G_LIKELY (pclass->acquire_buffer))
1206 result = pclass->acquire_buffer (pool, buffer, params);
1208 result = GST_FLOW_NOT_SUPPORTED;
1210 if (G_LIKELY (result == GST_FLOW_OK)) {
1211 /* all buffers from the pool point to the pool and have the refcount of the
1212 * pool incremented */
1213 (*buffer)->pool = gst_object_ref (pool);
1215 dec_outstanding (pool);
1222 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
1224 GST_LOG_OBJECT (pool, "released buffer %p %d", buffer,
1225 GST_MINI_OBJECT_FLAGS (buffer));
1227 /* memory should be untouched */
1228 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY))
1231 /* size should have been reset. This is not a catch all, pool with
1232 * size requirement per memory should do their own check. */
1233 if (gst_buffer_get_size (buffer) != pool->priv->size)
1236 /* all memory should be exclusive to this buffer (and thus be writable) */
1237 if (!gst_buffer_is_all_memory_writable (buffer))
1240 /* keep it around in our queue */
1241 gst_atomic_queue_push (pool->priv->queue, buffer);
1242 gst_poll_write_control (pool->priv->poll);
1248 do_free_buffer (pool, buffer);
1254 * gst_buffer_pool_release_buffer:
1255 * @pool: a #GstBufferPool
1256 * @buffer: (transfer full): a #GstBuffer
1258 * Release @buffer to @pool. @buffer should have previously been allocated from
1259 * @pool with gst_buffer_pool_acquire_buffer().
1261 * This function is usually called automatically when the last ref on @buffer
1265 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
1267 GstBufferPoolClass *pclass;
1269 g_return_if_fail (GST_IS_BUFFER_POOL (pool));
1270 g_return_if_fail (buffer != NULL);
1272 /* check that the buffer is ours, all buffers returned to the pool have the
1273 * pool member set to NULL and the pool refcount decreased */
1274 if (!g_atomic_pointer_compare_and_exchange (&buffer->pool, pool, NULL))
1277 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
1279 /* reset the buffer when needed */
1280 if (G_LIKELY (pclass->reset_buffer))
1281 pclass->reset_buffer (pool, buffer);
1283 if (G_LIKELY (pclass->release_buffer))
1284 pclass->release_buffer (pool, buffer);
1286 dec_outstanding (pool);
1288 /* decrease the refcount that the buffer had to us */
1289 gst_object_unref (pool);
1293 * gst_buffer_pool_set_flushing:
1294 * @pool: a #GstBufferPool
1295 * @flushing: whether to start or stop flushing
1297 * Enabled or disable the flushing state of a @pool without freeing or
1298 * allocating buffers.
1303 gst_buffer_pool_set_flushing (GstBufferPool * pool, gboolean flushing)
1305 GstBufferPoolPrivate *priv;
1307 g_return_if_fail (GST_IS_BUFFER_POOL (pool));
1309 GST_LOG_OBJECT (pool, "flushing %d", flushing);
1313 GST_BUFFER_POOL_LOCK (pool);
1315 if (!priv->active) {
1316 GST_WARNING_OBJECT (pool, "can't change flushing state of inactive pool");
1320 do_set_flushing (pool, flushing);
1323 GST_BUFFER_POOL_UNLOCK (pool);