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 a 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.
63 * Use gst_object_unref() to release the reference to a bufferpool. If the
64 * refcount of the pool reaches 0, the pool will be freed.
67 #include "gst_private.h"
68 #include "glib-compat-private.h"
74 #include <sys/types.h>
76 #include "gstatomicqueue.h"
82 #include "gstbufferpool.h"
86 # define EWOULDBLOCK EAGAIN /* This is just to placate gcc */
88 #endif /* G_OS_WIN32 */
90 GST_DEBUG_CATEGORY_STATIC (gst_buffer_pool_debug);
91 #define GST_CAT_DEFAULT gst_buffer_pool_debug
93 #define GST_BUFFER_POOL_LOCK(pool) (g_rec_mutex_lock(&pool->priv->rec_lock))
94 #define GST_BUFFER_POOL_UNLOCK(pool) (g_rec_mutex_unlock(&pool->priv->rec_lock))
96 struct _GstBufferPoolPrivate
98 GstAtomicQueue *queue;
105 gint outstanding; /* number of buffers that are in use */
108 GstStructure *config;
114 GstAllocator *allocator;
115 GstAllocationParams params;
118 static void gst_buffer_pool_finalize (GObject * object);
120 G_DEFINE_TYPE_WITH_PRIVATE (GstBufferPool, gst_buffer_pool, GST_TYPE_OBJECT);
122 static gboolean default_start (GstBufferPool * pool);
123 static gboolean default_stop (GstBufferPool * pool);
124 static gboolean default_set_config (GstBufferPool * pool,
125 GstStructure * config);
126 static GstFlowReturn default_alloc_buffer (GstBufferPool * pool,
127 GstBuffer ** buffer, GstBufferPoolAcquireParams * params);
128 static GstFlowReturn default_acquire_buffer (GstBufferPool * pool,
129 GstBuffer ** buffer, GstBufferPoolAcquireParams * params);
130 static void default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer);
131 static void default_free_buffer (GstBufferPool * pool, GstBuffer * buffer);
132 static void default_release_buffer (GstBufferPool * pool, GstBuffer * buffer);
135 gst_buffer_pool_class_init (GstBufferPoolClass * klass)
137 GObjectClass *gobject_class = (GObjectClass *) klass;
139 gobject_class->finalize = gst_buffer_pool_finalize;
141 klass->start = default_start;
142 klass->stop = default_stop;
143 klass->set_config = default_set_config;
144 klass->acquire_buffer = default_acquire_buffer;
145 klass->reset_buffer = default_reset_buffer;
146 klass->alloc_buffer = default_alloc_buffer;
147 klass->release_buffer = default_release_buffer;
148 klass->free_buffer = default_free_buffer;
150 GST_DEBUG_CATEGORY_INIT (gst_buffer_pool_debug, "bufferpool", 0,
155 gst_buffer_pool_init (GstBufferPool * pool)
157 GstBufferPoolPrivate *priv;
159 priv = pool->priv = gst_buffer_pool_get_instance_private (pool);
161 g_rec_mutex_init (&priv->rec_lock);
163 priv->poll = gst_poll_new_timer ();
164 priv->queue = gst_atomic_queue_new (16);
166 priv->active = FALSE;
167 priv->configured = FALSE;
168 priv->started = FALSE;
169 priv->config = gst_structure_new_id_empty (GST_QUARK (BUFFER_POOL_CONFIG));
170 gst_buffer_pool_config_set_params (priv->config, NULL, 0, 0, 0);
171 priv->allocator = NULL;
172 gst_allocation_params_init (&priv->params);
173 gst_buffer_pool_config_set_allocator (priv->config, priv->allocator,
175 /* 1 control write for flushing - the flush token */
176 gst_poll_write_control (priv->poll);
177 /* 1 control write for marking that we are not waiting for poll - the wait token */
178 gst_poll_write_control (priv->poll);
180 GST_DEBUG_OBJECT (pool, "created");
184 gst_buffer_pool_finalize (GObject * object)
187 GstBufferPoolPrivate *priv;
189 pool = GST_BUFFER_POOL_CAST (object);
192 GST_DEBUG_OBJECT (pool, "%p finalize", pool);
194 gst_buffer_pool_set_active (pool, FALSE);
195 gst_atomic_queue_unref (priv->queue);
196 gst_poll_free (priv->poll);
197 gst_structure_free (priv->config);
198 g_rec_mutex_clear (&priv->rec_lock);
200 gst_object_unref (priv->allocator);
202 G_OBJECT_CLASS (gst_buffer_pool_parent_class)->finalize (object);
206 * gst_buffer_pool_new:
208 * Creates a new #GstBufferPool instance.
210 * Returns: (transfer full): a new #GstBufferPool instance
213 gst_buffer_pool_new (void)
215 GstBufferPool *result;
217 result = g_object_new (GST_TYPE_BUFFER_POOL, NULL);
218 GST_DEBUG_OBJECT (result, "created new buffer pool");
220 /* Clear floating flag */
221 gst_object_ref_sink (result);
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);
236 return GST_FLOW_ERROR;
242 mark_meta_pooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
244 GST_DEBUG_OBJECT (GST_BUFFER_POOL (user_data),
245 "marking meta %p as POOLED in buffer %p", *meta, buffer);
246 GST_META_FLAG_SET (*meta, GST_META_FLAG_POOLED);
247 GST_META_FLAG_SET (*meta, GST_META_FLAG_LOCKED);
253 do_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
254 GstBufferPoolAcquireParams * params)
256 GstBufferPoolPrivate *priv = pool->priv;
257 GstFlowReturn result;
258 gint cur_buffers, max_buffers;
259 GstBufferPoolClass *pclass;
261 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
263 if (G_UNLIKELY (!pclass->alloc_buffer))
266 max_buffers = priv->max_buffers;
268 /* increment the allocation counter */
269 cur_buffers = g_atomic_int_add (&priv->cur_buffers, 1);
270 if (max_buffers && cur_buffers >= max_buffers)
273 result = pclass->alloc_buffer (pool, buffer, params);
274 if (G_UNLIKELY (result != GST_FLOW_OK))
277 /* lock all metadata and mark as pooled, we want this to remain on
278 * the buffer and we want to remove any other metadata that gets added
280 gst_buffer_foreach_meta (*buffer, mark_meta_pooled, pool);
282 /* un-tag memory, this is how we expect the buffer when it is
284 GST_BUFFER_FLAG_UNSET (*buffer, GST_BUFFER_FLAG_TAG_MEMORY);
286 GST_LOG_OBJECT (pool, "allocated buffer %d/%d, %p", cur_buffers,
287 max_buffers, *buffer);
294 GST_ERROR_OBJECT (pool, "no alloc function");
295 return GST_FLOW_NOT_SUPPORTED;
299 GST_DEBUG_OBJECT (pool, "max buffers reached");
300 g_atomic_int_add (&priv->cur_buffers, -1);
305 GST_WARNING_OBJECT (pool, "alloc function failed");
306 g_atomic_int_add (&priv->cur_buffers, -1);
311 /* the default implementation for preallocating the buffers in the pool */
313 default_start (GstBufferPool * pool)
316 GstBufferPoolPrivate *priv = pool->priv;
317 GstBufferPoolClass *pclass;
319 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
321 /* we need to prealloc buffers */
322 for (i = 0; i < priv->min_buffers; i++) {
325 if (do_alloc_buffer (pool, &buffer, NULL) != GST_FLOW_OK)
328 /* release to the queue, we call the vmethod directly, we don't need to do
329 * the other refcount handling right now. */
330 if (G_LIKELY (pclass->release_buffer))
331 pclass->release_buffer (pool, buffer);
338 GST_WARNING_OBJECT (pool, "failed to allocate buffer");
343 /* must be called with the lock */
345 do_start (GstBufferPool * pool)
347 GstBufferPoolPrivate *priv = pool->priv;
349 if (!priv->started) {
350 GstBufferPoolClass *pclass;
352 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
354 GST_LOG_OBJECT (pool, "starting");
355 /* start the pool, subclasses should allocate buffers and put them
357 if (G_LIKELY (pclass->start)) {
358 if (!pclass->start (pool))
361 priv->started = TRUE;
367 default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
369 gst_buffer_unref (buffer);
373 do_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
375 GstBufferPoolPrivate *priv;
376 GstBufferPoolClass *pclass;
379 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
381 g_atomic_int_add (&priv->cur_buffers, -1);
382 GST_LOG_OBJECT (pool, "freeing buffer %p (%u left)", buffer,
385 if (G_LIKELY (pclass->free_buffer))
386 pclass->free_buffer (pool, buffer);
389 /* must be called with the lock */
391 default_stop (GstBufferPool * pool)
393 GstBufferPoolPrivate *priv = pool->priv;
397 while ((buffer = gst_atomic_queue_pop (priv->queue))) {
398 while (!gst_poll_read_control (priv->poll)) {
399 if (errno == EWOULDBLOCK) {
400 /* We put the buffer into the queue but did not finish writing control
401 * yet, let's wait a bit and retry */
405 /* Critical error but GstPoll already complained */
409 do_free_buffer (pool, buffer);
411 return priv->cur_buffers == 0;
414 /* must be called with the lock */
416 do_stop (GstBufferPool * pool)
418 GstBufferPoolPrivate *priv = pool->priv;
421 GstBufferPoolClass *pclass;
423 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
425 GST_LOG_OBJECT (pool, "stopping");
426 if (G_LIKELY (pclass->stop)) {
427 if (!pclass->stop (pool))
430 priv->started = FALSE;
435 /* must be called with the lock */
437 do_set_flushing (GstBufferPool * pool, gboolean flushing)
439 GstBufferPoolPrivate *priv = pool->priv;
440 GstBufferPoolClass *pclass;
442 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
444 if (GST_BUFFER_POOL_IS_FLUSHING (pool) == flushing)
448 g_atomic_int_set (&pool->flushing, 1);
449 /* Write the flush token to wake up any waiters */
450 gst_poll_write_control (priv->poll);
452 if (pclass->flush_start)
453 pclass->flush_start (pool);
455 if (pclass->flush_stop)
456 pclass->flush_stop (pool);
458 while (!gst_poll_read_control (priv->poll)) {
459 if (errno == EWOULDBLOCK) {
460 /* This should not really happen unless flushing and unflushing
461 * happens on different threads. Let's wait a bit to get back flush
462 * token from the thread that was setting it to flushing */
466 /* Critical error but GstPoll already complained */
471 g_atomic_int_set (&pool->flushing, 0);
476 * gst_buffer_pool_set_active:
477 * @pool: a #GstBufferPool
478 * @active: the new active state
480 * Control the active state of @pool. When the pool is inactive, new calls to
481 * gst_buffer_pool_acquire_buffer() will return with %GST_FLOW_FLUSHING.
483 * Activating the bufferpool will preallocate all resources in the pool based on
484 * the configuration of the pool.
486 * Deactivating will free the resources again when there are no outstanding
487 * buffers. When there are outstanding buffers, they will be freed as soon as
488 * they are all returned to the pool.
490 * Returns: %FALSE when the pool was not configured or when preallocation of the
494 gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
497 GstBufferPoolPrivate *priv;
499 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
501 GST_LOG_OBJECT (pool, "active %d", active);
505 GST_BUFFER_POOL_LOCK (pool);
506 /* just return if we are already in the right state */
507 if (priv->active == active)
510 /* we need to be configured */
511 if (!priv->configured)
515 if (!do_start (pool))
518 /* flush_stop my release buffers, setting to active to avoid running
519 * do_stop while activating the pool */
522 /* unset the flushing state now */
523 do_set_flushing (pool, FALSE);
527 /* set to flushing first */
528 do_set_flushing (pool, TRUE);
530 /* when all buffers are in the pool, free them. Else they will be
531 * freed when they are released */
532 outstanding = g_atomic_int_get (&priv->outstanding);
533 GST_LOG_OBJECT (pool, "outstanding buffers %d", outstanding);
534 if (outstanding == 0) {
539 priv->active = FALSE;
541 GST_BUFFER_POOL_UNLOCK (pool);
547 GST_DEBUG_OBJECT (pool, "pool was in the right state");
548 GST_BUFFER_POOL_UNLOCK (pool);
553 GST_ERROR_OBJECT (pool, "pool was not configured");
554 GST_BUFFER_POOL_UNLOCK (pool);
559 GST_ERROR_OBJECT (pool, "start failed");
560 GST_BUFFER_POOL_UNLOCK (pool);
565 GST_WARNING_OBJECT (pool, "stop failed");
566 GST_BUFFER_POOL_UNLOCK (pool);
572 * gst_buffer_pool_is_active:
573 * @pool: a #GstBufferPool
575 * Check if @pool is active. A pool can be activated with the
576 * gst_buffer_pool_set_active() call.
578 * Returns: %TRUE when the pool is active.
581 gst_buffer_pool_is_active (GstBufferPool * pool)
585 GST_BUFFER_POOL_LOCK (pool);
586 res = pool->priv->active;
587 GST_BUFFER_POOL_UNLOCK (pool);
593 default_set_config (GstBufferPool * pool, GstStructure * config)
595 GstBufferPoolPrivate *priv = pool->priv;
597 guint size, min_buffers, max_buffers;
598 GstAllocator *allocator;
599 GstAllocationParams params;
601 /* parse the config and keep around */
602 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
606 if (!gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms))
609 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
612 priv->min_buffers = min_buffers;
613 priv->max_buffers = max_buffers;
614 priv->cur_buffers = 0;
617 gst_object_unref (priv->allocator);
618 if ((priv->allocator = allocator))
619 gst_object_ref (allocator);
620 priv->params = params;
626 GST_WARNING_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
632 * gst_buffer_pool_set_config:
633 * @pool: a #GstBufferPool
634 * @config: (transfer full): a #GstStructure
636 * Set the configuration of the pool. If the pool is already configured, and
637 * the configuration haven't change, this function will return %TRUE. If the
638 * pool is active, this method will return %FALSE and active configuration
639 * will remain. Buffers allocated form this pool must be returned or else this
640 * function will do nothing and return %FALSE.
642 * @config is a #GstStructure that contains the configuration parameters for
643 * the pool. A default and mandatory set of parameters can be configured with
644 * gst_buffer_pool_config_set_params(), gst_buffer_pool_config_set_allocator()
645 * and gst_buffer_pool_config_add_option().
647 * If the parameters in @config can not be set exactly, this function returns
648 * %FALSE and will try to update as much state as possible. The new state can
649 * then be retrieved and refined with gst_buffer_pool_get_config().
651 * This function takes ownership of @config.
653 * Returns: %TRUE when the configuration could be set.
656 gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
659 GstBufferPoolClass *pclass;
660 GstBufferPoolPrivate *priv;
662 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
663 g_return_val_if_fail (config != NULL, FALSE);
667 GST_BUFFER_POOL_LOCK (pool);
669 /* nothing to do if config is unchanged */
670 if (priv->configured && gst_structure_is_equal (config, priv->config))
671 goto config_unchanged;
673 /* can't change the settings when active */
677 /* we can't change when outstanding buffers */
678 if (g_atomic_int_get (&priv->outstanding) != 0)
679 goto have_outstanding;
681 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
683 /* set the new config */
684 if (G_LIKELY (pclass->set_config))
685 result = pclass->set_config (pool, config);
689 /* save the config regardless of the result so user can read back the
690 * modified config and evaluate if the changes are acceptable */
692 gst_structure_free (priv->config);
693 priv->config = config;
696 /* now we are configured */
697 priv->configured = TRUE;
699 GST_BUFFER_POOL_UNLOCK (pool);
705 gst_structure_free (config);
706 GST_BUFFER_POOL_UNLOCK (pool);
712 gst_structure_free (config);
713 GST_INFO_OBJECT (pool, "can't change config, we are active");
714 GST_BUFFER_POOL_UNLOCK (pool);
719 gst_structure_free (config);
720 GST_WARNING_OBJECT (pool, "can't change config, have outstanding buffers");
721 GST_BUFFER_POOL_UNLOCK (pool);
727 * gst_buffer_pool_get_config:
728 * @pool: a #GstBufferPool
730 * Get a copy of the current configuration of the pool. This configuration
731 * can either be modified and used for the gst_buffer_pool_set_config() call
732 * or it must be freed after usage.
734 * Returns: (transfer full): a copy of the current configuration of @pool. use
735 * gst_structure_free() after usage or gst_buffer_pool_set_config().
738 gst_buffer_pool_get_config (GstBufferPool * pool)
740 GstStructure *result;
742 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
744 GST_BUFFER_POOL_LOCK (pool);
745 result = gst_structure_copy (pool->priv->config);
746 GST_BUFFER_POOL_UNLOCK (pool);
751 static const gchar *empty_option[] = { NULL };
754 * gst_buffer_pool_get_options:
755 * @pool: a #GstBufferPool
757 * Get a %NULL terminated array of string with supported bufferpool options for
758 * @pool. An option would typically be enabled with
759 * gst_buffer_pool_config_add_option().
761 * Returns: (array zero-terminated=1) (transfer none): a %NULL terminated array
765 gst_buffer_pool_get_options (GstBufferPool * pool)
767 GstBufferPoolClass *pclass;
768 const gchar **result;
770 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
772 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
774 if (G_LIKELY (pclass->get_options)) {
775 if ((result = pclass->get_options (pool)) == NULL)
778 result = empty_option;
785 g_warning ("bufferpool subclass returned NULL options");
791 * gst_buffer_pool_has_option:
792 * @pool: a #GstBufferPool
795 * Check if the bufferpool supports @option.
797 * Returns: %TRUE if the buffer pool contains @option.
800 gst_buffer_pool_has_option (GstBufferPool * pool, const gchar * option)
803 const gchar **options;
805 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
806 g_return_val_if_fail (option != NULL, FALSE);
808 options = gst_buffer_pool_get_options (pool);
810 for (i = 0; options[i]; i++) {
811 if (g_str_equal (options[i], option))
818 * gst_buffer_pool_config_set_params:
819 * @config: a #GstBufferPool configuration
820 * @caps: caps for the buffers
821 * @size: the size of each buffer, not including prefix and padding
822 * @min_buffers: the minimum amount of buffers to allocate.
823 * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
825 * Configure @config with the given parameters.
828 gst_buffer_pool_config_set_params (GstStructure * config, GstCaps * caps,
829 guint size, guint min_buffers, guint max_buffers)
831 g_return_if_fail (config != NULL);
832 g_return_if_fail (max_buffers == 0 || min_buffers <= max_buffers);
833 g_return_if_fail (caps == NULL || gst_caps_is_fixed (caps));
835 gst_structure_id_set (config,
836 GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
837 GST_QUARK (SIZE), G_TYPE_UINT, size,
838 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
839 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers, NULL);
843 * gst_buffer_pool_config_set_allocator:
844 * @config: a #GstBufferPool configuration
845 * @allocator: (allow-none): a #GstAllocator
846 * @params: (allow-none): #GstAllocationParams
848 * Set the @allocator and @params on @config.
850 * One of @allocator and @params can be %NULL, but not both. When @allocator
851 * is %NULL, the default allocator of the pool will use the values in @param
852 * to perform its allocation. When @param is %NULL, the pool will use the
853 * provided @allocator with its default #GstAllocationParams.
855 * A call to gst_buffer_pool_set_config() can update the allocator and params
856 * with the values that it is able to do. Some pools are, for example, not able
857 * to operate with different allocators or cannot allocate with the values
858 * specified in @params. Use gst_buffer_pool_get_config() to get the currently
862 gst_buffer_pool_config_set_allocator (GstStructure * config,
863 GstAllocator * allocator, const GstAllocationParams * params)
865 g_return_if_fail (config != NULL);
866 g_return_if_fail (allocator != NULL || params != NULL);
868 gst_structure_id_set (config,
869 GST_QUARK (ALLOCATOR), GST_TYPE_ALLOCATOR, allocator,
870 GST_QUARK (PARAMS), GST_TYPE_ALLOCATION_PARAMS, params, NULL);
874 * gst_buffer_pool_config_add_option:
875 * @config: a #GstBufferPool configuration
876 * @option: an option to add
878 * Enabled the option in @config. This will instruct the @bufferpool to enable
879 * the specified option on the buffers that it allocates.
881 * The supported options by @pool can be retrieved with gst_buffer_pool_get_options().
884 gst_buffer_pool_config_add_option (GstStructure * config, const gchar * option)
887 GValue option_value = { 0, };
890 g_return_if_fail (config != NULL);
892 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
894 len = gst_value_array_get_size (value);
895 for (i = 0; i < len; ++i) {
896 const GValue *nth_val = gst_value_array_get_value (value, i);
898 if (g_str_equal (option, g_value_get_string (nth_val)))
902 GValue new_array_val = { 0, };
904 g_value_init (&new_array_val, GST_TYPE_ARRAY);
905 gst_structure_id_take_value (config, GST_QUARK (OPTIONS), &new_array_val);
906 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
908 g_value_init (&option_value, G_TYPE_STRING);
909 g_value_set_string (&option_value, option);
910 gst_value_array_append_and_take_value ((GValue *) value, &option_value);
914 * gst_buffer_pool_config_n_options:
915 * @config: a #GstBufferPool configuration
917 * Retrieve the number of values currently stored in the options array of the
920 * Returns: the options array size as a #guint.
923 gst_buffer_pool_config_n_options (GstStructure * config)
928 g_return_val_if_fail (config != NULL, 0);
930 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
932 size = gst_value_array_get_size (value);
938 * gst_buffer_pool_config_get_option:
939 * @config: a #GstBufferPool configuration
940 * @index: position in the option array to read
942 * Parse an available @config and get the option at @index of the options API
945 * Returns: a #gchar of the option at @index.
948 gst_buffer_pool_config_get_option (GstStructure * config, guint index)
951 const gchar *ret = NULL;
953 g_return_val_if_fail (config != NULL, 0);
955 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
957 const GValue *option_value;
959 option_value = gst_value_array_get_value (value, index);
961 ret = g_value_get_string (option_value);
967 * gst_buffer_pool_config_has_option:
968 * @config: a #GstBufferPool configuration
971 * Check if @config contains @option.
973 * Returns: %TRUE if the options array contains @option.
976 gst_buffer_pool_config_has_option (GstStructure * config, const gchar * option)
981 g_return_val_if_fail (config != NULL, 0);
983 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
985 len = gst_value_array_get_size (value);
986 for (i = 0; i < len; ++i) {
987 const GValue *nth_val = gst_value_array_get_value (value, i);
989 if (g_str_equal (option, g_value_get_string (nth_val)))
997 * gst_buffer_pool_config_get_params:
998 * @config: (transfer none): a #GstBufferPool configuration
999 * @caps: (out) (transfer none) (allow-none): the caps of buffers
1000 * @size: (out) (allow-none): the size of each buffer, not including prefix and padding
1001 * @min_buffers: (out) (allow-none): the minimum amount of buffers to allocate.
1002 * @max_buffers: (out) (allow-none): the maximum amount of buffers to allocate or 0 for unlimited.
1004 * Get the configuration values from @config.
1006 * Returns: %TRUE if all parameters could be fetched.
1009 gst_buffer_pool_config_get_params (GstStructure * config, GstCaps ** caps,
1010 guint * size, guint * min_buffers, guint * max_buffers)
1012 g_return_val_if_fail (config != NULL, FALSE);
1015 *caps = g_value_get_boxed (gst_structure_id_get_value (config,
1018 return gst_structure_id_get (config,
1019 GST_QUARK (SIZE), G_TYPE_UINT, size,
1020 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
1021 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers, NULL);
1025 * gst_buffer_pool_config_get_allocator:
1026 * @config: (transfer none): a #GstBufferPool configuration
1027 * @allocator: (out) (allow-none) (transfer none): a #GstAllocator, or %NULL
1028 * @params: (out) (allow-none): #GstAllocationParams, or %NULL
1030 * Get the @allocator and @params from @config.
1032 * Returns: %TRUE, if the values are set.
1035 gst_buffer_pool_config_get_allocator (GstStructure * config,
1036 GstAllocator ** allocator, GstAllocationParams * params)
1038 g_return_val_if_fail (config != NULL, FALSE);
1041 *allocator = g_value_get_object (gst_structure_id_get_value (config,
1042 GST_QUARK (ALLOCATOR)));
1044 GstAllocationParams *p;
1046 p = g_value_get_boxed (gst_structure_id_get_value (config,
1047 GST_QUARK (PARAMS)));
1051 gst_allocation_params_init (params);
1058 * gst_buffer_pool_config_validate_params:
1059 * @config: (transfer none): a #GstBufferPool configuration
1060 * @caps: (transfer none): the excepted caps of buffers
1061 * @size: the expected size of each buffer, not including prefix and padding
1062 * @min_buffers: the expected minimum amount of buffers to allocate.
1063 * @max_buffers: the expect maximum amount of buffers to allocate or 0 for unlimited.
1065 * Validate that changes made to @config are still valid in the context of the
1066 * expected parameters. This function is a helper that can be used to validate
1067 * changes made by a pool to a config when gst_buffer_pool_set_config()
1068 * returns %FALSE. This expects that @caps haven't changed and that
1069 * @min_buffers aren't lower then what we initially expected.
1070 * This does not check if options or allocator parameters are still valid,
1071 * won't check if size have changed, since changing the size is valid to adapt
1076 * Returns: %TRUE, if the parameters are valid in this context.
1079 gst_buffer_pool_config_validate_params (GstStructure * config, GstCaps * caps,
1080 guint size, guint min_buffers, G_GNUC_UNUSED guint max_buffers)
1083 guint newsize, newmin;
1084 gboolean ret = FALSE;
1086 g_return_val_if_fail (config != NULL, FALSE);
1088 gst_buffer_pool_config_get_params (config, &newcaps, &newsize, &newmin, NULL);
1090 if (gst_caps_is_equal (caps, newcaps) && (newsize >= size)
1091 && (newmin >= min_buffers))
1097 static GstFlowReturn
1098 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
1099 GstBufferPoolAcquireParams * params)
1101 GstFlowReturn result;
1102 GstBufferPoolPrivate *priv = pool->priv;
1105 if (G_UNLIKELY (GST_BUFFER_POOL_IS_FLUSHING (pool)))
1108 /* try to get a buffer from the queue */
1109 *buffer = gst_atomic_queue_pop (priv->queue);
1110 if (G_LIKELY (*buffer)) {
1111 while (!gst_poll_read_control (priv->poll)) {
1112 if (errno == EWOULDBLOCK) {
1113 /* We put the buffer into the queue but did not finish writing control
1114 * yet, let's wait a bit and retry */
1118 /* Critical error but GstPoll already complained */
1122 result = GST_FLOW_OK;
1123 GST_LOG_OBJECT (pool, "acquired buffer %p", *buffer);
1127 /* no buffer, try to allocate some more */
1128 GST_LOG_OBJECT (pool, "no buffer, trying to allocate");
1129 result = do_alloc_buffer (pool, buffer, params);
1130 if (G_LIKELY (result == GST_FLOW_OK))
1131 /* we have a buffer, return it */
1134 if (G_UNLIKELY (result != GST_FLOW_EOS))
1135 /* something went wrong, return error */
1138 /* check if we need to wait */
1139 if (params && (params->flags & GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT)) {
1140 GST_LOG_OBJECT (pool, "no more buffers");
1144 /* now we release the control socket, we wait for a buffer release or
1146 if (!gst_poll_read_control (pool->priv->poll)) {
1147 if (errno == EWOULDBLOCK) {
1148 /* This means that we have two threads trying to allocate buffers
1149 * already, and the other one already got the wait token. This
1150 * means that we only have to wait for the poll now and not write the
1151 * token afterwards: we will be woken up once the other thread is
1152 * woken up and that one will write the wait token it removed */
1153 GST_LOG_OBJECT (pool, "waiting for free buffers or flushing");
1154 gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
1156 /* This is a critical error, GstPoll already gave a warning */
1157 result = GST_FLOW_ERROR;
1161 /* We're the first thread waiting, we got the wait token and have to
1162 * write it again later
1164 * We're a second thread and just consumed the flush token and block all
1165 * other threads, in which case we must not wait and give it back
1167 if (!GST_BUFFER_POOL_IS_FLUSHING (pool)) {
1168 GST_LOG_OBJECT (pool, "waiting for free buffers or flushing");
1169 gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
1171 gst_poll_write_control (pool->priv->poll);
1180 GST_DEBUG_OBJECT (pool, "we are flushing");
1181 return GST_FLOW_FLUSHING;
1186 dec_outstanding (GstBufferPool * pool)
1188 if (g_atomic_int_dec_and_test (&pool->priv->outstanding)) {
1189 /* all buffers are returned to the pool, see if we need to free them */
1190 if (GST_BUFFER_POOL_IS_FLUSHING (pool)) {
1191 /* take the lock so that set_active is not run concurrently */
1192 GST_BUFFER_POOL_LOCK (pool);
1193 /* now that we have the lock, check if we have been de-activated with
1194 * outstanding buffers */
1195 if (!pool->priv->active)
1198 GST_BUFFER_POOL_UNLOCK (pool);
1204 remove_meta_unpooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
1206 if (!GST_META_FLAG_IS_SET (*meta, GST_META_FLAG_POOLED)) {
1207 GST_META_FLAG_UNSET (*meta, GST_META_FLAG_LOCKED);
1214 default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer)
1216 GST_BUFFER_FLAGS (buffer) &= GST_BUFFER_FLAG_TAG_MEMORY;
1218 GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
1219 GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
1220 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
1221 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
1222 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
1224 /* if the memory is intact reset the size to the full size */
1225 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY))
1226 gst_buffer_resize (buffer, 0, pool->priv->size);
1228 /* remove all metadata without the POOLED flag */
1229 gst_buffer_foreach_meta (buffer, remove_meta_unpooled, pool);
1233 * gst_buffer_pool_acquire_buffer:
1234 * @pool: a #GstBufferPool
1235 * @buffer: (out): a location for a #GstBuffer
1236 * @params: (transfer none) (allow-none): parameters.
1238 * Acquire a buffer from @pool. @buffer should point to a memory location that
1239 * can hold a pointer to the new buffer.
1241 * @params can be %NULL or contain optional parameters to influence the
1244 * Returns: a #GstFlowReturn such as %GST_FLOW_FLUSHING when the pool is
1248 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
1249 GstBufferPoolAcquireParams * params)
1251 GstBufferPoolClass *pclass;
1252 GstFlowReturn result;
1254 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), GST_FLOW_ERROR);
1255 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
1257 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
1259 /* assume we'll have one more outstanding buffer we need to do that so
1260 * that concurrent set_active doesn't clear the buffers */
1261 g_atomic_int_inc (&pool->priv->outstanding);
1263 if (G_LIKELY (pclass->acquire_buffer))
1264 result = pclass->acquire_buffer (pool, buffer, params);
1266 result = GST_FLOW_NOT_SUPPORTED;
1268 if (G_LIKELY (result == GST_FLOW_OK)) {
1269 /* all buffers from the pool point to the pool and have the refcount of the
1270 * pool incremented */
1271 (*buffer)->pool = gst_object_ref (pool);
1273 dec_outstanding (pool);
1280 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
1282 GST_LOG_OBJECT (pool, "released buffer %p %d", buffer,
1283 GST_MINI_OBJECT_FLAGS (buffer));
1285 /* memory should be untouched */
1286 if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY)))
1289 /* size should have been reset. This is not a catch all, pool with
1290 * size requirement per memory should do their own check. */
1291 if (G_UNLIKELY (gst_buffer_get_size (buffer) != pool->priv->size))
1294 /* all memory should be exclusive to this buffer (and thus be writable) */
1295 if (G_UNLIKELY (!gst_buffer_is_all_memory_writable (buffer)))
1298 /* keep it around in our queue */
1299 gst_atomic_queue_push (pool->priv->queue, buffer);
1300 gst_poll_write_control (pool->priv->poll);
1306 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, pool,
1307 "discarding buffer %p: memory tag set", buffer);
1312 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, pool,
1313 "discarding buffer %p: size %" G_GSIZE_FORMAT " != %u",
1314 buffer, gst_buffer_get_size (buffer), pool->priv->size);
1319 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, pool,
1320 "discarding buffer %p: memory not writable", buffer);
1325 do_free_buffer (pool, buffer);
1331 * gst_buffer_pool_release_buffer:
1332 * @pool: a #GstBufferPool
1333 * @buffer: (transfer full): a #GstBuffer
1335 * Release @buffer to @pool. @buffer should have previously been allocated from
1336 * @pool with gst_buffer_pool_acquire_buffer().
1338 * This function is usually called automatically when the last ref on @buffer
1342 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
1344 GstBufferPoolClass *pclass;
1346 g_return_if_fail (GST_IS_BUFFER_POOL (pool));
1347 g_return_if_fail (buffer != NULL);
1349 /* check that the buffer is ours, all buffers returned to the pool have the
1350 * pool member set to NULL and the pool refcount decreased */
1351 if (!g_atomic_pointer_compare_and_exchange (&buffer->pool, pool, NULL))
1354 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
1356 /* reset the buffer when needed */
1357 if (G_LIKELY (pclass->reset_buffer))
1358 pclass->reset_buffer (pool, buffer);
1360 if (G_LIKELY (pclass->release_buffer))
1361 pclass->release_buffer (pool, buffer);
1363 dec_outstanding (pool);
1365 /* decrease the refcount that the buffer had to us */
1366 gst_object_unref (pool);
1370 * gst_buffer_pool_set_flushing:
1371 * @pool: a #GstBufferPool
1372 * @flushing: whether to start or stop flushing
1374 * Enable or disable the flushing state of a @pool without freeing or
1375 * allocating buffers.
1380 gst_buffer_pool_set_flushing (GstBufferPool * pool, gboolean flushing)
1382 GstBufferPoolPrivate *priv;
1384 g_return_if_fail (GST_IS_BUFFER_POOL (pool));
1386 GST_LOG_OBJECT (pool, "flushing %d", flushing);
1390 GST_BUFFER_POOL_LOCK (pool);
1392 if (!priv->active) {
1393 GST_WARNING_OBJECT (pool, "can't change flushing state of inactive pool");
1397 do_set_flushing (pool, flushing);
1400 GST_BUFFER_POOL_UNLOCK (pool);