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 * @title: GstBufferPool
25 * @short_description: Pool for buffers
26 * @see_also: #GstBuffer
28 * A #GstBufferPool is an object that can be used to pre-allocate and recycle
29 * buffers of the same size and with the same properties.
31 * A #GstBufferPool is created with gst_buffer_pool_new().
33 * Once a pool is created, it needs to be configured. A call to
34 * gst_buffer_pool_get_config() returns the current configuration structure from
35 * the pool. With gst_buffer_pool_config_set_params() and
36 * gst_buffer_pool_config_set_allocator() the bufferpool parameters and
37 * allocator can be configured. Other properties can be configured in the pool
38 * depending on the pool implementation.
40 * A bufferpool can have extra options that can be enabled with
41 * gst_buffer_pool_config_add_option(). The available options can be retrieved
42 * with gst_buffer_pool_get_options(). Some options allow for additional
43 * configuration properties to be set.
45 * After the configuration structure has been configured,
46 * gst_buffer_pool_set_config() updates the configuration in the pool. This can
47 * fail when the configuration structure is not accepted.
49 * After the pool has been configured, it can be activated with
50 * gst_buffer_pool_set_active(). This will preallocate the configured resources
53 * When the pool is active, gst_buffer_pool_acquire_buffer() can be used to
54 * retrieve a buffer from the pool.
56 * Buffers allocated from a bufferpool will automatically be returned to the
57 * pool with gst_buffer_pool_release_buffer() when their refcount drops to 0.
59 * The bufferpool can be deactivated again with gst_buffer_pool_set_active().
60 * All further gst_buffer_pool_acquire_buffer() calls will return an error. When
61 * all buffers are returned to the pool they will be freed.
64 #include "gst_private.h"
65 #include "glib-compat-private.h"
71 #include <sys/types.h>
73 #include "gstatomicqueue.h"
79 #include "gstbufferpool.h"
83 # define EWOULDBLOCK EAGAIN /* This is just to placate gcc */
85 #endif /* G_OS_WIN32 */
87 GST_DEBUG_CATEGORY_STATIC (gst_buffer_pool_debug);
88 #define GST_CAT_DEFAULT gst_buffer_pool_debug
90 #define GST_BUFFER_POOL_LOCK(pool) (g_rec_mutex_lock(&pool->priv->rec_lock))
91 #define GST_BUFFER_POOL_UNLOCK(pool) (g_rec_mutex_unlock(&pool->priv->rec_lock))
93 struct _GstBufferPoolPrivate
95 GstAtomicQueue *queue;
102 gint outstanding; /* number of buffers that are in use */
105 GstStructure *config;
111 GstAllocator *allocator;
112 GstAllocationParams params;
115 static void gst_buffer_pool_finalize (GObject * object);
117 G_DEFINE_TYPE_WITH_PRIVATE (GstBufferPool, gst_buffer_pool, GST_TYPE_OBJECT);
119 static gboolean default_start (GstBufferPool * pool);
120 static gboolean default_stop (GstBufferPool * pool);
121 static gboolean default_set_config (GstBufferPool * pool,
122 GstStructure * config);
123 static GstFlowReturn default_alloc_buffer (GstBufferPool * pool,
124 GstBuffer ** buffer, GstBufferPoolAcquireParams * params);
125 static GstFlowReturn default_acquire_buffer (GstBufferPool * pool,
126 GstBuffer ** buffer, GstBufferPoolAcquireParams * params);
127 static void default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer);
128 static void default_free_buffer (GstBufferPool * pool, GstBuffer * buffer);
129 static void default_release_buffer (GstBufferPool * pool, GstBuffer * buffer);
132 gst_buffer_pool_class_init (GstBufferPoolClass * klass)
134 GObjectClass *gobject_class = (GObjectClass *) klass;
136 gobject_class->finalize = gst_buffer_pool_finalize;
138 klass->start = default_start;
139 klass->stop = default_stop;
140 klass->set_config = default_set_config;
141 klass->acquire_buffer = default_acquire_buffer;
142 klass->reset_buffer = default_reset_buffer;
143 klass->alloc_buffer = default_alloc_buffer;
144 klass->release_buffer = default_release_buffer;
145 klass->free_buffer = default_free_buffer;
147 GST_DEBUG_CATEGORY_INIT (gst_buffer_pool_debug, "bufferpool", 0,
152 gst_buffer_pool_init (GstBufferPool * pool)
154 GstBufferPoolPrivate *priv;
156 priv = pool->priv = gst_buffer_pool_get_instance_private (pool);
158 g_rec_mutex_init (&priv->rec_lock);
160 priv->poll = gst_poll_new_timer ();
161 priv->queue = gst_atomic_queue_new (16);
163 priv->active = FALSE;
164 priv->configured = FALSE;
165 priv->started = FALSE;
166 priv->config = gst_structure_new_id_empty (GST_QUARK (BUFFER_POOL_CONFIG));
167 gst_buffer_pool_config_set_params (priv->config, NULL, 0, 0, 0);
168 priv->allocator = NULL;
169 gst_allocation_params_init (&priv->params);
170 gst_buffer_pool_config_set_allocator (priv->config, priv->allocator,
172 /* 1 control write for flushing - the flush token */
173 gst_poll_write_control (priv->poll);
174 /* 1 control write for marking that we are not waiting for poll - the wait token */
175 gst_poll_write_control (priv->poll);
177 GST_DEBUG_OBJECT (pool, "created");
181 gst_buffer_pool_finalize (GObject * object)
184 GstBufferPoolPrivate *priv;
186 pool = GST_BUFFER_POOL_CAST (object);
189 GST_DEBUG_OBJECT (pool, "%p finalize", pool);
191 gst_buffer_pool_set_active (pool, FALSE);
192 gst_atomic_queue_unref (priv->queue);
193 gst_poll_free (priv->poll);
194 gst_structure_free (priv->config);
195 g_rec_mutex_clear (&priv->rec_lock);
197 gst_object_unref (priv->allocator);
199 G_OBJECT_CLASS (gst_buffer_pool_parent_class)->finalize (object);
203 * gst_buffer_pool_new:
205 * Creates a new #GstBufferPool instance.
207 * Returns: (transfer full): a new #GstBufferPool instance
210 gst_buffer_pool_new (void)
212 GstBufferPool *result;
214 result = g_object_new (GST_TYPE_BUFFER_POOL, NULL);
215 GST_DEBUG_OBJECT (result, "created new buffer pool");
217 /* Clear floating flag */
218 gst_object_ref_sink (result);
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);
233 return GST_FLOW_ERROR;
239 mark_meta_pooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
241 GST_DEBUG_OBJECT (GST_BUFFER_POOL (user_data),
242 "marking meta %p as POOLED in buffer %p", *meta, buffer);
243 GST_META_FLAG_SET (*meta, GST_META_FLAG_POOLED);
244 GST_META_FLAG_SET (*meta, GST_META_FLAG_LOCKED);
250 do_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
251 GstBufferPoolAcquireParams * params)
253 GstBufferPoolPrivate *priv = pool->priv;
254 GstFlowReturn result;
255 gint cur_buffers, max_buffers;
256 GstBufferPoolClass *pclass;
258 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
260 if (G_UNLIKELY (!pclass->alloc_buffer))
263 max_buffers = priv->max_buffers;
265 /* increment the allocation counter */
266 cur_buffers = g_atomic_int_add (&priv->cur_buffers, 1);
267 if (max_buffers && cur_buffers >= max_buffers)
270 result = pclass->alloc_buffer (pool, buffer, params);
271 if (G_UNLIKELY (result != GST_FLOW_OK))
274 /* lock all metadata and mark as pooled, we want this to remain on
275 * the buffer and we want to remove any other metadata that gets added
277 gst_buffer_foreach_meta (*buffer, mark_meta_pooled, pool);
279 /* un-tag memory, this is how we expect the buffer when it is
281 GST_BUFFER_FLAG_UNSET (*buffer, GST_BUFFER_FLAG_TAG_MEMORY);
283 GST_LOG_OBJECT (pool, "allocated buffer %d/%d, %p", cur_buffers,
284 max_buffers, *buffer);
291 GST_ERROR_OBJECT (pool, "no alloc function");
292 return GST_FLOW_NOT_SUPPORTED;
296 GST_DEBUG_OBJECT (pool, "max buffers reached");
297 g_atomic_int_add (&priv->cur_buffers, -1);
302 GST_WARNING_OBJECT (pool, "alloc function failed");
303 g_atomic_int_add (&priv->cur_buffers, -1);
308 /* the default implementation for preallocating the buffers in the pool */
310 default_start (GstBufferPool * pool)
313 GstBufferPoolPrivate *priv = pool->priv;
314 GstBufferPoolClass *pclass;
316 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
318 /* we need to prealloc buffers */
319 for (i = 0; i < priv->min_buffers; i++) {
322 if (do_alloc_buffer (pool, &buffer, NULL) != GST_FLOW_OK)
325 /* release to the queue, we call the vmethod directly, we don't need to do
326 * the other refcount handling right now. */
327 if (G_LIKELY (pclass->release_buffer))
328 pclass->release_buffer (pool, buffer);
335 GST_WARNING_OBJECT (pool, "failed to allocate buffer");
340 /* must be called with the lock */
342 do_start (GstBufferPool * pool)
344 GstBufferPoolPrivate *priv = pool->priv;
346 if (!priv->started) {
347 GstBufferPoolClass *pclass;
349 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
351 GST_LOG_OBJECT (pool, "starting");
352 /* start the pool, subclasses should allocate buffers and put them
354 if (G_LIKELY (pclass->start)) {
355 if (!pclass->start (pool))
358 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 while (!gst_poll_read_control (priv->poll)) {
396 if (errno == EWOULDBLOCK) {
397 /* We put the buffer into the queue but did not finish writing control
398 * yet, let's wait a bit and retry */
402 /* Critical error but GstPoll already complained */
406 do_free_buffer (pool, buffer);
408 return priv->cur_buffers == 0;
411 /* must be called with the lock */
413 do_stop (GstBufferPool * pool)
415 GstBufferPoolPrivate *priv = pool->priv;
418 GstBufferPoolClass *pclass;
420 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
422 GST_LOG_OBJECT (pool, "stopping");
423 if (G_LIKELY (pclass->stop)) {
424 if (!pclass->stop (pool))
427 priv->started = FALSE;
432 /* must be called with the lock */
434 do_set_flushing (GstBufferPool * pool, gboolean flushing)
436 GstBufferPoolPrivate *priv = pool->priv;
437 GstBufferPoolClass *pclass;
439 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
441 if (GST_BUFFER_POOL_IS_FLUSHING (pool) == flushing)
445 g_atomic_int_set (&pool->flushing, 1);
446 /* Write the flush token to wake up any waiters */
447 gst_poll_write_control (priv->poll);
449 if (pclass->flush_start)
450 pclass->flush_start (pool);
452 if (pclass->flush_stop)
453 pclass->flush_stop (pool);
455 while (!gst_poll_read_control (priv->poll)) {
456 if (errno == EWOULDBLOCK) {
457 /* This should not really happen unless flushing and unflushing
458 * happens on different threads. Let's wait a bit to get back flush
459 * token from the thread that was setting it to flushing */
463 /* Critical error but GstPoll already complained */
468 g_atomic_int_set (&pool->flushing, 0);
473 * gst_buffer_pool_set_active:
474 * @pool: a #GstBufferPool
475 * @active: the new active state
477 * Controls the active state of @pool. When the pool is inactive, new calls to
478 * gst_buffer_pool_acquire_buffer() will return with %GST_FLOW_FLUSHING.
480 * Activating the bufferpool will preallocate all resources in the pool based on
481 * the configuration of the pool.
483 * Deactivating will free the resources again when there are no outstanding
484 * buffers. When there are outstanding buffers, they will be freed as soon as
485 * they are all returned to the pool.
487 * Returns: %FALSE when the pool was not configured or when preallocation of the
491 gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
494 GstBufferPoolPrivate *priv;
496 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
498 GST_LOG_OBJECT (pool, "active %d", active);
502 GST_BUFFER_POOL_LOCK (pool);
503 /* just return if we are already in the right state */
504 if (priv->active == active)
507 /* we need to be configured */
508 if (!priv->configured)
512 if (!do_start (pool))
515 /* flush_stop my release buffers, setting to active to avoid running
516 * do_stop while activating the pool */
519 /* unset the flushing state now */
520 do_set_flushing (pool, FALSE);
524 /* set to flushing first */
525 do_set_flushing (pool, TRUE);
527 /* when all buffers are in the pool, free them. Else they will be
528 * freed when they are released */
529 outstanding = g_atomic_int_get (&priv->outstanding);
530 GST_LOG_OBJECT (pool, "outstanding buffers %d", outstanding);
531 if (outstanding == 0) {
536 priv->active = FALSE;
538 GST_BUFFER_POOL_UNLOCK (pool);
544 GST_DEBUG_OBJECT (pool, "pool was in the right state");
545 GST_BUFFER_POOL_UNLOCK (pool);
550 GST_ERROR_OBJECT (pool, "pool was not configured");
551 GST_BUFFER_POOL_UNLOCK (pool);
556 GST_ERROR_OBJECT (pool, "start failed");
557 GST_BUFFER_POOL_UNLOCK (pool);
562 GST_WARNING_OBJECT (pool, "stop failed");
563 GST_BUFFER_POOL_UNLOCK (pool);
569 * gst_buffer_pool_is_active:
570 * @pool: a #GstBufferPool
572 * Checks if @pool is active. A pool can be activated with the
573 * gst_buffer_pool_set_active() call.
575 * Returns: %TRUE when the pool is active.
578 gst_buffer_pool_is_active (GstBufferPool * pool)
582 GST_BUFFER_POOL_LOCK (pool);
583 res = pool->priv->active;
584 GST_BUFFER_POOL_UNLOCK (pool);
590 default_set_config (GstBufferPool * pool, GstStructure * config)
592 GstBufferPoolPrivate *priv = pool->priv;
594 guint size, min_buffers, max_buffers;
595 GstAllocator *allocator;
596 GstAllocationParams params;
598 /* parse the config and keep around */
599 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
603 if (!gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms))
606 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
609 priv->min_buffers = min_buffers;
610 priv->max_buffers = max_buffers;
611 priv->cur_buffers = 0;
614 gst_object_unref (priv->allocator);
615 if ((priv->allocator = allocator))
616 gst_object_ref (allocator);
617 priv->params = params;
623 GST_WARNING_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
629 * gst_buffer_pool_set_config:
630 * @pool: a #GstBufferPool
631 * @config: (transfer full): a #GstStructure
633 * Sets the configuration of the pool. If the pool is already configured, and
634 * the configuration hasn't changed, this function will return %TRUE. If the
635 * pool is active, this method will return %FALSE and active configuration
636 * will remain. Buffers allocated from this pool must be returned or else this
637 * function will do nothing and return %FALSE.
639 * @config is a #GstStructure that contains the configuration parameters for
640 * the pool. A default and mandatory set of parameters can be configured with
641 * gst_buffer_pool_config_set_params(), gst_buffer_pool_config_set_allocator()
642 * and gst_buffer_pool_config_add_option().
644 * If the parameters in @config can not be set exactly, this function returns
645 * %FALSE and will try to update as much state as possible. The new state can
646 * then be retrieved and refined with gst_buffer_pool_get_config().
648 * This function takes ownership of @config.
650 * Returns: %TRUE when the configuration could be set.
653 gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
656 GstBufferPoolClass *pclass;
657 GstBufferPoolPrivate *priv;
659 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
660 g_return_val_if_fail (config != NULL, FALSE);
664 GST_BUFFER_POOL_LOCK (pool);
666 /* nothing to do if config is unchanged */
667 if (priv->configured && gst_structure_is_equal (config, priv->config))
668 goto config_unchanged;
670 /* can't change the settings when active */
674 /* we can't change when outstanding buffers */
675 if (g_atomic_int_get (&priv->outstanding) != 0)
676 goto have_outstanding;
678 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
680 /* set the new config */
681 if (G_LIKELY (pclass->set_config))
682 result = pclass->set_config (pool, config);
686 /* save the config regardless of the result so user can read back the
687 * modified config and evaluate if the changes are acceptable */
689 gst_structure_free (priv->config);
690 priv->config = config;
693 /* now we are configured */
694 priv->configured = TRUE;
696 GST_BUFFER_POOL_UNLOCK (pool);
702 gst_structure_free (config);
703 GST_BUFFER_POOL_UNLOCK (pool);
709 gst_structure_free (config);
710 GST_INFO_OBJECT (pool, "can't change config, we are active");
711 GST_BUFFER_POOL_UNLOCK (pool);
716 gst_structure_free (config);
717 GST_WARNING_OBJECT (pool, "can't change config, have outstanding buffers");
718 GST_BUFFER_POOL_UNLOCK (pool);
724 * gst_buffer_pool_get_config:
725 * @pool: a #GstBufferPool
727 * Gets a copy of the current configuration of the pool. This configuration
728 * can be modified and used for the gst_buffer_pool_set_config() call.
730 * Returns: (transfer full): a copy of the current configuration of @pool.
733 gst_buffer_pool_get_config (GstBufferPool * pool)
735 GstStructure *result;
737 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
739 GST_BUFFER_POOL_LOCK (pool);
740 result = gst_structure_copy (pool->priv->config);
741 GST_BUFFER_POOL_UNLOCK (pool);
746 static const gchar *empty_option[] = { NULL };
749 * gst_buffer_pool_get_options:
750 * @pool: a #GstBufferPool
752 * Gets a %NULL terminated array of string with supported bufferpool options for
753 * @pool. An option would typically be enabled with
754 * gst_buffer_pool_config_add_option().
756 * Returns: (array zero-terminated=1) (transfer none): a %NULL terminated array
760 gst_buffer_pool_get_options (GstBufferPool * pool)
762 GstBufferPoolClass *pclass;
763 const gchar **result;
765 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
767 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
769 if (G_LIKELY (pclass->get_options)) {
770 if ((result = pclass->get_options (pool)) == NULL)
773 result = empty_option;
780 g_warning ("bufferpool subclass returned NULL options");
786 * gst_buffer_pool_has_option:
787 * @pool: a #GstBufferPool
790 * Checks if the bufferpool supports @option.
792 * Returns: %TRUE if the buffer pool contains @option.
795 gst_buffer_pool_has_option (GstBufferPool * pool, const gchar * option)
798 const gchar **options;
800 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
801 g_return_val_if_fail (option != NULL, FALSE);
803 options = gst_buffer_pool_get_options (pool);
805 for (i = 0; options[i]; i++) {
806 if (g_str_equal (options[i], option))
813 * gst_buffer_pool_config_set_params:
814 * @config: a #GstBufferPool configuration
815 * @caps: (nullable): caps for the buffers
816 * @size: the size of each buffer, not including prefix and padding
817 * @min_buffers: the minimum amount of buffers to allocate.
818 * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
820 * Configures @config with the given parameters.
823 gst_buffer_pool_config_set_params (GstStructure * config, GstCaps * caps,
824 guint size, guint min_buffers, guint max_buffers)
826 g_return_if_fail (config != NULL);
827 g_return_if_fail (max_buffers == 0 || min_buffers <= max_buffers);
828 g_return_if_fail (caps == NULL || gst_caps_is_fixed (caps));
830 gst_structure_id_set (config,
831 GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
832 GST_QUARK (SIZE), G_TYPE_UINT, size,
833 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
834 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers, NULL);
838 * gst_buffer_pool_config_set_allocator:
839 * @config: a #GstBufferPool configuration
840 * @allocator: (nullable): a #GstAllocator
841 * @params: (nullable): #GstAllocationParams
843 * Sets the @allocator and @params on @config.
845 * One of @allocator and @params can be %NULL, but not both. When @allocator
846 * is %NULL, the default allocator of the pool will use the values in @param
847 * to perform its allocation. When @param is %NULL, the pool will use the
848 * provided @allocator with its default #GstAllocationParams.
850 * A call to gst_buffer_pool_set_config() can update the allocator and params
851 * with the values that it is able to do. Some pools are, for example, not able
852 * to operate with different allocators or cannot allocate with the values
853 * specified in @params. Use gst_buffer_pool_get_config() to get the currently
857 gst_buffer_pool_config_set_allocator (GstStructure * config,
858 GstAllocator * allocator, const GstAllocationParams * params)
860 g_return_if_fail (config != NULL);
861 g_return_if_fail (allocator != NULL || params != NULL);
863 gst_structure_id_set (config,
864 GST_QUARK (ALLOCATOR), GST_TYPE_ALLOCATOR, allocator,
865 GST_QUARK (PARAMS), GST_TYPE_ALLOCATION_PARAMS, params, NULL);
869 * gst_buffer_pool_config_add_option:
870 * @config: a #GstBufferPool configuration
871 * @option: an option to add
873 * Enables the option in @config. This will instruct the @bufferpool to enable
874 * the specified option on the buffers that it allocates.
876 * The options supported by @pool can be retrieved with gst_buffer_pool_get_options().
879 gst_buffer_pool_config_add_option (GstStructure * config, const gchar * option)
882 GValue option_value = { 0, };
885 g_return_if_fail (config != NULL);
887 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
889 len = gst_value_array_get_size (value);
890 for (i = 0; i < len; ++i) {
891 const GValue *nth_val = gst_value_array_get_value (value, i);
893 if (g_str_equal (option, g_value_get_string (nth_val)))
897 GValue new_array_val = { 0, };
899 g_value_init (&new_array_val, GST_TYPE_ARRAY);
900 gst_structure_id_take_value (config, GST_QUARK (OPTIONS), &new_array_val);
901 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
903 g_value_init (&option_value, G_TYPE_STRING);
904 g_value_set_string (&option_value, option);
905 gst_value_array_append_and_take_value ((GValue *) value, &option_value);
909 * gst_buffer_pool_config_n_options:
910 * @config: a #GstBufferPool configuration
912 * Retrieves the number of values currently stored in the options array of the
915 * Returns: the options array size as a #guint.
918 gst_buffer_pool_config_n_options (GstStructure * config)
923 g_return_val_if_fail (config != NULL, 0);
925 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
927 size = gst_value_array_get_size (value);
933 * gst_buffer_pool_config_get_option:
934 * @config: a #GstBufferPool configuration
935 * @index: position in the option array to read
937 * Parses an available @config and gets the option at @index of the options API
940 * Returns: (nullable): the option at @index.
943 gst_buffer_pool_config_get_option (GstStructure * config, guint index)
946 const gchar *ret = NULL;
948 g_return_val_if_fail (config != NULL, 0);
950 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
952 const GValue *option_value;
954 option_value = gst_value_array_get_value (value, index);
956 ret = g_value_get_string (option_value);
962 * gst_buffer_pool_config_has_option:
963 * @config: a #GstBufferPool configuration
966 * Checks if @config contains @option.
968 * Returns: %TRUE if the options array contains @option.
971 gst_buffer_pool_config_has_option (GstStructure * config, const gchar * option)
976 g_return_val_if_fail (config != NULL, 0);
978 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
980 len = gst_value_array_get_size (value);
981 for (i = 0; i < len; ++i) {
982 const GValue *nth_val = gst_value_array_get_value (value, i);
984 if (g_str_equal (option, g_value_get_string (nth_val)))
992 * gst_buffer_pool_config_get_params:
993 * @config: (transfer none): a #GstBufferPool configuration
994 * @caps: (out) (transfer none) (optional) (nullable): the caps of buffers
995 * @size: (out) (optional): the size of each buffer, not including prefix and padding
996 * @min_buffers: (out) (optional): the minimum amount of buffers to allocate.
997 * @max_buffers: (out) (optional): the maximum amount of buffers to allocate or 0 for unlimited.
999 * Gets the configuration values from @config.
1001 * Returns: %TRUE if all parameters could be fetched.
1004 gst_buffer_pool_config_get_params (GstStructure * config, GstCaps ** caps,
1005 guint * size, guint * min_buffers, guint * max_buffers)
1007 g_return_val_if_fail (config != NULL, FALSE);
1010 *caps = g_value_get_boxed (gst_structure_id_get_value (config,
1013 return gst_structure_id_get (config,
1014 GST_QUARK (SIZE), G_TYPE_UINT, size,
1015 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
1016 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers, NULL);
1020 * gst_buffer_pool_config_get_allocator:
1021 * @config: (transfer none): a #GstBufferPool configuration
1022 * @allocator: (out) (optional) (nullable) (transfer none): a #GstAllocator, or %NULL
1023 * @params: (out caller-allocates) (optional): #GstAllocationParams, or %NULL
1025 * Gets the @allocator and @params from @config.
1027 * Returns: %TRUE, if the values are set.
1030 gst_buffer_pool_config_get_allocator (GstStructure * config,
1031 GstAllocator ** allocator, GstAllocationParams * params)
1033 g_return_val_if_fail (config != NULL, FALSE);
1036 *allocator = g_value_get_object (gst_structure_id_get_value (config,
1037 GST_QUARK (ALLOCATOR)));
1039 GstAllocationParams *p;
1041 p = g_value_get_boxed (gst_structure_id_get_value (config,
1042 GST_QUARK (PARAMS)));
1046 gst_allocation_params_init (params);
1053 * gst_buffer_pool_config_validate_params:
1054 * @config: (transfer none): a #GstBufferPool configuration
1055 * @caps: (nullable) (transfer none): the excepted caps of buffers
1056 * @size: the expected size of each buffer, not including prefix and padding
1057 * @min_buffers: the expected minimum amount of buffers to allocate.
1058 * @max_buffers: the expect maximum amount of buffers to allocate or 0 for unlimited.
1060 * Validates that changes made to @config are still valid in the context of the
1061 * expected parameters. This function is a helper that can be used to validate
1062 * changes made by a pool to a config when gst_buffer_pool_set_config()
1063 * returns %FALSE. This expects that @caps haven't changed and that
1064 * @min_buffers aren't lower then what we initially expected.
1065 * This does not check if options or allocator parameters are still valid,
1066 * won't check if size have changed, since changing the size is valid to adapt
1071 * Returns: %TRUE, if the parameters are valid in this context.
1074 gst_buffer_pool_config_validate_params (GstStructure * config, GstCaps * caps,
1075 guint size, guint min_buffers, G_GNUC_UNUSED guint max_buffers)
1078 guint newsize, newmin;
1079 gboolean ret = FALSE;
1081 g_return_val_if_fail (config != NULL, FALSE);
1083 gst_buffer_pool_config_get_params (config, &newcaps, &newsize, &newmin, NULL);
1085 if (gst_caps_is_equal (caps, newcaps) && (newsize >= size)
1086 && (newmin >= min_buffers))
1092 static GstFlowReturn
1093 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
1094 GstBufferPoolAcquireParams * params)
1096 GstFlowReturn result;
1097 GstBufferPoolPrivate *priv = pool->priv;
1100 if (G_UNLIKELY (GST_BUFFER_POOL_IS_FLUSHING (pool)))
1103 /* try to get a buffer from the queue */
1104 *buffer = gst_atomic_queue_pop (priv->queue);
1105 if (G_LIKELY (*buffer)) {
1106 while (!gst_poll_read_control (priv->poll)) {
1107 if (errno == EWOULDBLOCK) {
1108 /* We put the buffer into the queue but did not finish writing control
1109 * yet, let's wait a bit and retry */
1113 /* Critical error but GstPoll already complained */
1117 result = GST_FLOW_OK;
1118 GST_LOG_OBJECT (pool, "acquired buffer %p", *buffer);
1122 /* no buffer, try to allocate some more */
1123 GST_LOG_OBJECT (pool, "no buffer, trying to allocate");
1124 result = do_alloc_buffer (pool, buffer, params);
1125 if (G_LIKELY (result == GST_FLOW_OK))
1126 /* we have a buffer, return it */
1129 if (G_UNLIKELY (result != GST_FLOW_EOS))
1130 /* something went wrong, return error */
1133 /* check if we need to wait */
1134 if (params && (params->flags & GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT)) {
1135 GST_LOG_OBJECT (pool, "no more buffers");
1139 /* now we release the control socket, we wait for a buffer release or
1141 if (!gst_poll_read_control (pool->priv->poll)) {
1142 if (errno == EWOULDBLOCK) {
1143 /* This means that we have two threads trying to allocate buffers
1144 * already, and the other one already got the wait token. This
1145 * means that we only have to wait for the poll now and not write the
1146 * token afterwards: we will be woken up once the other thread is
1147 * woken up and that one will write the wait token it removed */
1148 GST_LOG_OBJECT (pool, "waiting for free buffers or flushing");
1149 gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
1151 /* This is a critical error, GstPoll already gave a warning */
1152 result = GST_FLOW_ERROR;
1156 /* We're the first thread waiting, we got the wait token and have to
1157 * write it again later
1159 * We're a second thread and just consumed the flush token and block all
1160 * other threads, in which case we must not wait and give it back
1162 if (!GST_BUFFER_POOL_IS_FLUSHING (pool)) {
1163 GST_LOG_OBJECT (pool, "waiting for free buffers or flushing");
1164 gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
1166 gst_poll_write_control (pool->priv->poll);
1175 GST_DEBUG_OBJECT (pool, "we are flushing");
1176 return GST_FLOW_FLUSHING;
1181 dec_outstanding (GstBufferPool * pool)
1183 if (g_atomic_int_dec_and_test (&pool->priv->outstanding)) {
1184 /* all buffers are returned to the pool, see if we need to free them */
1185 if (GST_BUFFER_POOL_IS_FLUSHING (pool)) {
1186 /* take the lock so that set_active is not run concurrently */
1187 GST_BUFFER_POOL_LOCK (pool);
1188 /* now that we have the lock, check if we have been de-activated with
1189 * outstanding buffers */
1190 if (!pool->priv->active)
1193 GST_BUFFER_POOL_UNLOCK (pool);
1199 remove_meta_unpooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
1201 if (!GST_META_FLAG_IS_SET (*meta, GST_META_FLAG_POOLED)) {
1202 GST_META_FLAG_UNSET (*meta, GST_META_FLAG_LOCKED);
1209 default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer)
1211 GST_BUFFER_FLAGS (buffer) &= GST_BUFFER_FLAG_TAG_MEMORY;
1213 GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
1214 GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
1215 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
1216 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
1217 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
1219 /* if the memory is intact reset the size to the full size */
1220 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY)) {
1221 gsize offset, maxsize;
1222 gst_buffer_get_sizes (buffer, &offset, &maxsize);
1223 /* check if we can resize to at least the pool configured size. If not,
1224 * then this will fail internally in gst_buffer_resize().
1225 * default_release_buffer() will drop the buffer from the pool if the
1226 * sizes don't match */
1227 if (maxsize >= pool->priv->size) {
1228 gst_buffer_resize (buffer, -offset, pool->priv->size);
1230 GST_WARNING_OBJECT (pool, "Buffer %p without the memory tag has "
1231 "maxsize (%" G_GSIZE_FORMAT ") that is smaller than the "
1232 "configured buffer pool size (%u). The buffer will be not be "
1233 "reused. This is most likely a bug in this GstBufferPool subclass",
1234 buffer, maxsize, pool->priv->size);
1238 /* remove all metadata without the POOLED flag */
1239 gst_buffer_foreach_meta (buffer, remove_meta_unpooled, pool);
1243 * gst_buffer_pool_acquire_buffer:
1244 * @pool: a #GstBufferPool
1245 * @buffer: (out): a location for a #GstBuffer
1246 * @params: (transfer none) (allow-none): parameters.
1248 * Acquires a buffer from @pool. @buffer should point to a memory location that
1249 * can hold a pointer to the new buffer.
1251 * @params can contain optional parameters to influence the allocation.
1253 * Returns: a #GstFlowReturn such as %GST_FLOW_FLUSHING when the pool is
1257 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
1258 GstBufferPoolAcquireParams * params)
1260 GstBufferPoolClass *pclass;
1261 GstFlowReturn result;
1263 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), GST_FLOW_ERROR);
1264 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
1266 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
1268 /* assume we'll have one more outstanding buffer we need to do that so
1269 * that concurrent set_active doesn't clear the buffers */
1270 g_atomic_int_inc (&pool->priv->outstanding);
1272 if (G_LIKELY (pclass->acquire_buffer))
1273 result = pclass->acquire_buffer (pool, buffer, params);
1275 result = GST_FLOW_NOT_SUPPORTED;
1277 if (G_LIKELY (result == GST_FLOW_OK)) {
1278 /* all buffers from the pool point to the pool and have the refcount of the
1279 * pool incremented */
1280 (*buffer)->pool = gst_object_ref (pool);
1282 dec_outstanding (pool);
1289 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
1291 GST_LOG_OBJECT (pool, "released buffer %p %d", buffer,
1292 GST_MINI_OBJECT_FLAGS (buffer));
1294 /* memory should be untouched */
1295 if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY)))
1298 /* size should have been reset. This is not a catch all, pool with
1299 * size requirement per memory should do their own check. */
1300 if (G_UNLIKELY (gst_buffer_get_size (buffer) != pool->priv->size))
1303 /* all memory should be exclusive to this buffer (and thus be writable) */
1304 if (G_UNLIKELY (!gst_buffer_is_all_memory_writable (buffer)))
1307 /* keep it around in our queue */
1308 gst_atomic_queue_push (pool->priv->queue, buffer);
1309 gst_poll_write_control (pool->priv->poll);
1315 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, pool,
1316 "discarding buffer %p: memory tag set", buffer);
1321 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, pool,
1322 "discarding buffer %p: size %" G_GSIZE_FORMAT " != %u",
1323 buffer, gst_buffer_get_size (buffer), pool->priv->size);
1328 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, pool,
1329 "discarding buffer %p: memory not writable", buffer);
1334 do_free_buffer (pool, buffer);
1335 gst_poll_write_control (pool->priv->poll);
1341 * gst_buffer_pool_release_buffer:
1342 * @pool: a #GstBufferPool
1343 * @buffer: (transfer full): a #GstBuffer
1345 * Releases @buffer to @pool. @buffer should have previously been allocated from
1346 * @pool with gst_buffer_pool_acquire_buffer().
1348 * This function is usually called automatically when the last ref on @buffer
1352 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
1354 GstBufferPoolClass *pclass;
1356 g_return_if_fail (GST_IS_BUFFER_POOL (pool));
1357 g_return_if_fail (buffer != NULL);
1359 /* check that the buffer is ours, all buffers returned to the pool have the
1360 * pool member set to NULL and the pool refcount decreased */
1361 if (!g_atomic_pointer_compare_and_exchange (&buffer->pool, pool, NULL))
1364 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
1366 /* reset the buffer when needed */
1367 if (G_LIKELY (pclass->reset_buffer))
1368 pclass->reset_buffer (pool, buffer);
1370 if (G_LIKELY (pclass->release_buffer))
1371 pclass->release_buffer (pool, buffer);
1373 dec_outstanding (pool);
1375 /* decrease the refcount that the buffer had to us */
1376 gst_object_unref (pool);
1380 * gst_buffer_pool_set_flushing:
1381 * @pool: a #GstBufferPool
1382 * @flushing: whether to start or stop flushing
1384 * Enables or disables the flushing state of a @pool without freeing or
1385 * allocating buffers.
1390 gst_buffer_pool_set_flushing (GstBufferPool * pool, gboolean flushing)
1392 GstBufferPoolPrivate *priv;
1394 g_return_if_fail (GST_IS_BUFFER_POOL (pool));
1396 GST_LOG_OBJECT (pool, "flushing %d", flushing);
1400 GST_BUFFER_POOL_LOCK (pool);
1402 if (!priv->active) {
1403 GST_WARNING_OBJECT (pool, "can't change flushing state of inactive pool");
1407 do_set_flushing (pool, flushing);
1410 GST_BUFFER_POOL_UNLOCK (pool);