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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:gstbufferpool
24 * @short_description: Pool for buffers
25 * @see_also: #GstBuffer
29 #include "gst_private.h"
30 #include "glib-compat-private.h"
36 #include <sys/types.h>
38 #include "gstatomicqueue.h"
44 #include "gstbufferpool.h"
46 GST_DEBUG_CATEGORY_STATIC (gst_buffer_pool_debug);
47 #define GST_CAT_DEFAULT gst_buffer_pool_debug
49 #define GST_BUFFER_POOL_GET_PRIVATE(obj) \
50 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BUFFER_POOL, GstBufferPoolPrivate))
52 #define GST_BUFFER_POOL_LOCK(pool) (g_rec_mutex_lock(&pool->priv->rec_lock))
53 #define GST_BUFFER_POOL_UNLOCK(pool) (g_rec_mutex_unlock(&pool->priv->rec_lock))
55 struct _GstBufferPoolPrivate
57 GstAtomicQueue *queue;
72 GstAllocator *allocator;
73 GstAllocationParams params;
82 static void gst_buffer_pool_finalize (GObject * object);
84 G_DEFINE_TYPE (GstBufferPool, gst_buffer_pool, GST_TYPE_OBJECT);
86 static gboolean default_start (GstBufferPool * pool);
87 static gboolean default_stop (GstBufferPool * pool);
88 static gboolean default_set_config (GstBufferPool * pool,
89 GstStructure * config);
90 static GstFlowReturn default_alloc_buffer (GstBufferPool * pool,
91 GstBuffer ** buffer, GstBufferPoolAcquireParams * params);
92 static GstFlowReturn default_acquire_buffer (GstBufferPool * pool,
93 GstBuffer ** buffer, GstBufferPoolAcquireParams * params);
94 static void default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer,
95 GstBufferPoolAcquireParams * params);
96 static void default_free_buffer (GstBufferPool * pool, GstBuffer * buffer);
97 static void default_release_buffer (GstBufferPool * pool, GstBuffer * buffer);
100 gst_buffer_pool_class_init (GstBufferPoolClass * klass)
102 GObjectClass *gobject_class = (GObjectClass *) klass;
104 g_type_class_add_private (klass, sizeof (GstBufferPoolPrivate));
106 gobject_class->finalize = gst_buffer_pool_finalize;
108 klass->start = default_start;
109 klass->stop = default_stop;
110 klass->set_config = default_set_config;
111 klass->acquire_buffer = default_acquire_buffer;
112 klass->reset_buffer = default_reset_buffer;
113 klass->alloc_buffer = default_alloc_buffer;
114 klass->release_buffer = default_release_buffer;
115 klass->free_buffer = default_free_buffer;
117 GST_DEBUG_CATEGORY_INIT (gst_buffer_pool_debug, "bufferpool", 0,
122 gst_buffer_pool_init (GstBufferPool * pool)
124 GstBufferPoolPrivate *priv;
126 priv = pool->priv = GST_BUFFER_POOL_GET_PRIVATE (pool);
128 g_rec_mutex_init (&priv->rec_lock);
130 priv->poll = gst_poll_new_timer ();
131 priv->queue = gst_atomic_queue_new (10);
133 priv->active = FALSE;
134 priv->configured = FALSE;
135 priv->started = FALSE;
136 priv->config = gst_structure_new_id_empty (GST_QUARK (BUFFER_POOL_CONFIG));
137 gst_buffer_pool_config_set_params (priv->config, NULL, 0, 0, 0);
138 priv->allocator = NULL;
139 gst_allocation_params_init (&priv->params);
140 gst_buffer_pool_config_set_allocator (priv->config, priv->allocator,
142 gst_poll_write_control (priv->poll);
144 GST_DEBUG_OBJECT (pool, "created");
148 gst_buffer_pool_finalize (GObject * object)
151 GstBufferPoolPrivate *priv;
153 pool = GST_BUFFER_POOL_CAST (object);
156 GST_DEBUG_OBJECT (pool, "finalize");
158 gst_buffer_pool_set_active (pool, FALSE);
159 gst_atomic_queue_unref (priv->queue);
160 gst_poll_free (priv->poll);
161 gst_structure_free (priv->config);
162 g_rec_mutex_clear (&priv->rec_lock);
164 gst_allocator_unref (priv->allocator);
166 G_OBJECT_CLASS (gst_buffer_pool_parent_class)->finalize (object);
170 * gst_buffer_pool_new:
172 * Creates a new #GstBufferPool instance.
174 * Returns: (transfer full): a new #GstBufferPool instance
177 gst_buffer_pool_new (void)
179 GstBufferPool *result;
181 result = g_object_newv (GST_TYPE_BUFFER_POOL, 0, NULL);
182 GST_DEBUG_OBJECT (result, "created new buffer pool");
188 default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
189 GstBufferPoolAcquireParams * params)
191 GstBufferPoolPrivate *priv = pool->priv;
194 gst_buffer_new_allocate (priv->allocator, priv->size, &priv->params);
200 mark_meta_pooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
202 GstBufferPool *pool = user_data;
204 GST_DEBUG_OBJECT (pool, "marking meta %p as POOLED in buffer %p", *meta,
206 GST_META_FLAG_SET (*meta, GST_META_FLAG_POOLED);
211 /* the default implementation for preallocating the buffers
214 default_start (GstBufferPool * pool)
217 GstBufferPoolPrivate *priv = pool->priv;
218 GstBufferPoolClass *pclass;
220 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
222 /* no alloc function, error */
223 if (G_UNLIKELY (pclass->alloc_buffer == NULL))
226 /* we need to prealloc buffers */
227 for (i = 0; i < priv->min_buffers; i++) {
230 if (pclass->alloc_buffer (pool, &buffer, NULL) != GST_FLOW_OK)
233 gst_buffer_foreach_meta (buffer, mark_meta_pooled, pool);
234 GST_LOG_OBJECT (pool, "prealloced buffer %d: %p", i, buffer);
235 /* release to the queue, we call the vmethod directly, we don't need to do
236 * the other refcount handling right now. */
237 if (G_LIKELY (pclass->release_buffer))
238 pclass->release_buffer (pool, buffer);
245 GST_WARNING_OBJECT (pool, "no alloc function");
250 GST_WARNING_OBJECT (pool, "alloc function failed");
255 /* must be called with the lock */
257 do_start (GstBufferPool * pool)
259 if (!pool->priv->started) {
260 GstBufferPoolClass *pclass;
262 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
264 GST_LOG_OBJECT (pool, "starting");
265 /* start the pool, subclasses should allocate buffers and put them
267 if (G_LIKELY (pclass->start)) {
268 if (!pclass->start (pool))
271 pool->priv->started = TRUE;
278 default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
280 gst_buffer_unref (buffer);
283 /* must be called with the lock */
285 default_stop (GstBufferPool * pool)
288 GstBufferPoolClass *pclass;
290 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
293 while ((buffer = gst_atomic_queue_pop (pool->priv->queue))) {
294 GST_LOG_OBJECT (pool, "freeing %p", buffer);
295 gst_poll_read_control (pool->priv->poll);
297 if (G_LIKELY (pclass->free_buffer))
298 pclass->free_buffer (pool, buffer);
303 /* must be called with the lock */
305 do_stop (GstBufferPool * pool)
307 if (pool->priv->started) {
308 GstBufferPoolClass *pclass;
310 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
312 GST_LOG_OBJECT (pool, "stopping");
313 if (G_LIKELY (pclass->stop)) {
314 if (!pclass->stop (pool))
317 pool->priv->started = FALSE;
323 * gst_buffer_pool_set_active:
324 * @pool: a #GstBufferPool
325 * @active: the new active state
327 * Control the active state of @pool. When the pool is active, new calls to
328 * gst_buffer_pool_acquire_buffer() will return with GST_FLOW_FLUSHING.
330 * Activating the bufferpool will preallocate all resources in the pool based on
331 * the configuration of the pool.
333 * Deactivating will free the resources again when there are no outstanding
334 * buffers. When there are outstanding buffers, they will be freed as soon as
335 * they are all returned to the pool.
337 * Returns: %FALSE when the pool was not configured or when preallocation of the
341 gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
345 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
347 GST_LOG_OBJECT (pool, "active %d", active);
349 GST_BUFFER_POOL_LOCK (pool);
350 /* just return if we are already in the right state */
351 if (pool->priv->active == active)
354 /* we need to be configured */
355 if (!pool->priv->configured)
359 if (!do_start (pool))
362 /* unset the flushing state now */
363 gst_poll_read_control (pool->priv->poll);
364 g_atomic_int_set (&pool->flushing, 0);
368 /* set to flushing first */
369 g_atomic_int_set (&pool->flushing, 1);
370 gst_poll_write_control (pool->priv->poll);
372 /* when all buffers are in the pool, free them. Else they will be
373 * freed when they are released */
374 outstanding = g_atomic_int_get (&pool->priv->outstanding);
375 GST_LOG_OBJECT (pool, "outstanding buffers %d", outstanding);
376 if (outstanding == 0) {
381 pool->priv->active = active;
382 GST_BUFFER_POOL_UNLOCK (pool);
388 GST_DEBUG_OBJECT (pool, "pool was in the right state");
389 GST_BUFFER_POOL_UNLOCK (pool);
394 GST_ERROR_OBJECT (pool, "pool was not configured");
395 GST_BUFFER_POOL_UNLOCK (pool);
400 GST_ERROR_OBJECT (pool, "start failed");
401 GST_BUFFER_POOL_UNLOCK (pool);
406 GST_WARNING_OBJECT (pool, "stop failed");
407 GST_BUFFER_POOL_UNLOCK (pool);
413 * gst_buffer_pool_is_active:
414 * @pool: a #GstBufferPool
416 * Check if @pool is active. A pool can be activated with the
417 * gst_buffer_pool_set_active() call.
419 * Returns: %TRUE when the pool is active.
422 gst_buffer_pool_is_active (GstBufferPool * pool)
426 GST_BUFFER_POOL_LOCK (pool);
427 res = pool->priv->active;
428 GST_BUFFER_POOL_UNLOCK (pool);
434 default_set_config (GstBufferPool * pool, GstStructure * config)
436 GstBufferPoolPrivate *priv = pool->priv;
438 guint size, min_buffers, max_buffers;
439 GstAllocator *allocator;
440 GstAllocationParams params;
442 /* parse the config and keep around */
443 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
447 if (!gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms))
450 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
453 priv->min_buffers = min_buffers;
454 priv->max_buffers = max_buffers;
457 gst_allocator_unref (priv->allocator);
458 if ((priv->allocator = allocator))
459 gst_allocator_ref (allocator);
460 priv->params = params;
466 GST_WARNING_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
472 * gst_buffer_pool_set_config:
473 * @pool: a #GstBufferPool
474 * @config: (transfer full): a #GstStructure
476 * Set the configuration of the pool. The pool must be inactive and all buffers
477 * allocated form this pool must be returned or else this function will do
478 * nothing and return FALSE.
480 * @config is a #GstStructure that contains the configuration parameters for
481 * the pool. A default and mandatory set of parameters can be configured with
482 * gst_buffer_pool_config_set(). This function takes ownership of @config.
484 * Returns: TRUE when the configuration could be set.
487 gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
490 GstBufferPoolClass *pclass;
492 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
493 g_return_val_if_fail (config != NULL, FALSE);
495 GST_BUFFER_POOL_LOCK (pool);
496 /* can't change the settings when active */
497 if (pool->priv->active)
500 /* we can't change when outstanding buffers */
501 if (g_atomic_int_get (&pool->priv->outstanding) != 0)
502 goto have_outstanding;
504 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
506 /* set the new config */
507 if (G_LIKELY (pclass->set_config))
508 result = pclass->set_config (pool, config);
513 if (pool->priv->config)
514 gst_structure_free (pool->priv->config);
515 pool->priv->config = config;
517 /* now we are configured */
518 pool->priv->configured = TRUE;
520 gst_structure_free (config);
522 GST_BUFFER_POOL_UNLOCK (pool);
529 gst_structure_free (config);
530 GST_WARNING_OBJECT (pool, "can't change config, we are active");
531 GST_BUFFER_POOL_UNLOCK (pool);
536 gst_structure_free (config);
537 GST_WARNING_OBJECT (pool, "can't change config, have outstanding buffers");
538 GST_BUFFER_POOL_UNLOCK (pool);
544 * gst_buffer_pool_get_config:
545 * @pool: a #GstBufferPool
547 * Get a copy of the current configuration of the pool. This configuration
548 * can either be modified and used for the gst_buffer_pool_set_config() call
549 * or it must be freed after usage.
551 * Returns: (transfer full): a copy of the current configuration of @pool. use
552 * gst_structure_free() after usage or gst_buffer_pool_set_config().
555 gst_buffer_pool_get_config (GstBufferPool * pool)
557 GstStructure *result;
559 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
561 GST_BUFFER_POOL_UNLOCK (pool);
562 result = gst_structure_copy (pool->priv->config);
563 GST_BUFFER_POOL_UNLOCK (pool);
568 static const gchar *empty_option[] = { NULL };
571 * gst_buffer_pool_get_options:
572 * @pool: a #GstBufferPool
574 * Get a NULL terminated array of string with supported bufferpool options for
575 * @pool. An option would typically be enabled with
576 * gst_buffer_pool_config_add_option().
578 * Returns: (array zero-terminated=1) (transfer none): a NULL terminated array of strings.
581 gst_buffer_pool_get_options (GstBufferPool * pool)
583 GstBufferPoolClass *pclass;
584 const gchar **result;
586 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
588 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
590 if (G_LIKELY (pclass->get_options)) {
591 if ((result = pclass->get_options (pool)) == NULL)
594 result = empty_option;
601 g_warning ("bufferpool subclass returned NULL options");
607 * gst_buffer_pool_has_option:
608 * @pool: a #GstBufferPool
611 * Check if the bufferpool supports @option.
613 * Returns: a NULL terminated array of strings.
616 gst_buffer_pool_has_option (GstBufferPool * pool, const gchar * option)
619 const gchar **options;
621 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
622 g_return_val_if_fail (option != NULL, FALSE);
624 options = gst_buffer_pool_get_options (pool);
626 for (i = 0; options[i]; i++) {
627 if (g_str_equal (options[i], option))
634 * gst_buffer_pool_config_set_params:
635 * @config: a #GstBufferPool configuration
636 * @caps: caps for the buffers
637 * @size: the size of each buffer, not including prefix and padding
638 * @min_buffers: the minimum amount of buffers to allocate.
639 * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
641 * Configure @config with the given parameters.
644 gst_buffer_pool_config_set_params (GstStructure * config, const GstCaps * caps,
645 guint size, guint min_buffers, guint max_buffers)
647 g_return_if_fail (config != NULL);
648 g_return_if_fail (max_buffers == 0 || min_buffers <= max_buffers);
650 gst_structure_id_set (config,
651 GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
652 GST_QUARK (SIZE), G_TYPE_UINT, size,
653 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
654 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers, NULL);
658 * gst_buffer_pool_config_set_allocator:
659 * @config: a #GstBufferPool configuration
660 * @allocator: a #GstAllocator
661 * @params: #GstAllocationParams
663 * Set the @allocator and @params on @config.
665 * One of @allocator and @params can be NULL, but not both. When @allocator
666 * is NULL, the default allocator of the pool will use the values in @param
667 * to perform its allocation. When @param is NULL, the pool will use the
668 * provided allocator with its default #GstAllocationParams.
670 * A call to gst_buffer_pool_set_config() can update the allocator and params
671 * with the values that it is able to do. Some pools are, for example, not able
672 * to operate with different allocators or cannot allocate with the values
673 * specified in @params. Use gst_buffer_pool_get_config() to get the currently
677 gst_buffer_pool_config_set_allocator (GstStructure * config,
678 GstAllocator * allocator, const GstAllocationParams * params)
680 g_return_if_fail (config != NULL);
681 g_return_if_fail (allocator != NULL || params != NULL);
683 gst_structure_id_set (config,
684 GST_QUARK (ALLOCATOR), GST_TYPE_ALLOCATOR, allocator,
685 GST_QUARK (PARAMS), GST_TYPE_ALLOCATION_PARAMS, params, NULL);
689 * gst_buffer_pool_config_add_option:
690 * @config: a #GstBufferPool configuration
691 * @option: an option to add
693 * Enabled the option in @config. This will instruct the @bufferpool to enable
694 * the specified option on the buffers that it allocates.
696 * The supported options by @pool can be retrieved with gst_buffer_pool_get_options().
699 gst_buffer_pool_config_add_option (GstStructure * config, const gchar * option)
702 GValue option_value = { 0, };
705 g_return_if_fail (config != NULL);
707 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
709 len = gst_value_array_get_size (value);
710 for (i = 0; i < len; ++i) {
711 const GValue *nth_val = gst_value_array_get_value (value, i);
713 if (g_str_equal (option, g_value_get_string (nth_val)))
717 GValue new_array_val = { 0, };
719 g_value_init (&new_array_val, GST_TYPE_ARRAY);
720 gst_structure_id_take_value (config, GST_QUARK (OPTIONS), &new_array_val);
721 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
723 g_value_init (&option_value, G_TYPE_STRING);
724 g_value_set_string (&option_value, option);
725 gst_value_array_append_value ((GValue *) value, &option_value);
726 g_value_unset (&option_value);
730 * gst_buffer_pool_config_n_options:
731 * @config: a #GstBufferPool configuration
733 * Retrieve the number of values currently stored in the
734 * options array of the @config structure.
736 * Returns: the options array size as a #guint.
739 gst_buffer_pool_config_n_options (GstStructure * config)
744 g_return_val_if_fail (config != NULL, 0);
746 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
748 size = gst_value_array_get_size (value);
754 * gst_buffer_pool_config_get_option:
755 * @config: a #GstBufferPool configuration
756 * @index: position in the option array to read
758 * Parse an available @config and get the option
759 * at @index of the options API array.
761 * Returns: a #gchar of the option at @index.
764 gst_buffer_pool_config_get_option (GstStructure * config, guint index)
767 const gchar *ret = NULL;
769 g_return_val_if_fail (config != NULL, 0);
771 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
773 const GValue *option_value;
775 option_value = gst_value_array_get_value (value, index);
777 ret = g_value_get_string (option_value);
783 * gst_buffer_pool_config_has_option:
784 * @config: a #GstBufferPool configuration
787 * Check if @config contains @option
789 * Returns: TRUE if the options array contains @option.
792 gst_buffer_pool_config_has_option (GstStructure * config, const gchar * option)
797 g_return_val_if_fail (config != NULL, 0);
799 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
801 len = gst_value_array_get_size (value);
802 for (i = 0; i < len; ++i) {
803 const GValue *nth_val = gst_value_array_get_value (value, i);
805 if (g_str_equal (option, g_value_get_string (nth_val)))
813 * gst_buffer_pool_config_get_params:
814 * @config: (transfer none): a #GstBufferPool configuration
815 * @caps: (out): the caps of buffers
816 * @size: (out): the size of each buffer, not including prefix and padding
817 * @min_buffers: (out): the minimum amount of buffers to allocate.
818 * @max_buffers: (out): the maximum amount of buffers to allocate or 0 for unlimited.
820 * Get the configuration values from @config.
823 gst_buffer_pool_config_get_params (GstStructure * config, const GstCaps ** caps,
824 guint * size, guint * min_buffers, guint * max_buffers)
826 g_return_val_if_fail (config != NULL, FALSE);
828 return gst_structure_id_get (config,
829 GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
830 GST_QUARK (SIZE), G_TYPE_UINT, size,
831 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
832 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers, NULL);
836 * gst_buffer_pool_config_get_allocator:
837 * @config: (transfer none): a #GstBufferPool configuration
838 * @allocator: (transfer none): a #GstAllocator
839 * @params: #GstAllocationParams
841 * Get the allocator and params from @config.
844 gst_buffer_pool_config_get_allocator (GstStructure * config,
845 GstAllocator ** allocator, GstAllocationParams * params)
847 g_return_val_if_fail (config != NULL, FALSE);
850 *allocator = g_value_get_boxed (gst_structure_id_get_value (config,
851 GST_QUARK (ALLOCATOR)));
853 GstAllocationParams *p;
855 p = g_value_get_boxed (gst_structure_id_get_value (config,
856 GST_QUARK (PARAMS)));
860 gst_allocation_params_init (params);
867 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
868 GstBufferPoolAcquireParams * params)
870 GstFlowReturn result;
871 GstBufferPoolClass *pclass;
872 GstBufferPoolPrivate *priv = pool->priv;
874 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
877 if (G_UNLIKELY (GST_BUFFER_POOL_IS_FLUSHING (pool)))
880 /* try to get a buffer from the queue */
881 *buffer = gst_atomic_queue_pop (pool->priv->queue);
882 if (G_LIKELY (*buffer)) {
883 gst_poll_read_control (pool->priv->poll);
884 result = GST_FLOW_OK;
885 GST_LOG_OBJECT (pool, "acquired buffer %p", *buffer);
890 if (priv->max_buffers == 0) {
891 /* no max_buffers, we allocate some more */
892 if (G_LIKELY (pclass->alloc_buffer)) {
893 result = pclass->alloc_buffer (pool, buffer, params);
894 if (result == GST_FLOW_OK && *buffer)
895 gst_buffer_foreach_meta (*buffer, mark_meta_pooled, pool);
897 result = GST_FLOW_ERROR;
899 result = GST_FLOW_NOT_SUPPORTED;
900 GST_LOG_OBJECT (pool, "alloc buffer %p", *buffer);
904 /* check if we need to wait */
905 if (params && (params->flags & GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT)) {
906 GST_LOG_OBJECT (pool, "no more buffers");
907 result = GST_FLOW_EOS;
912 GST_LOG_OBJECT (pool, "waiting for free buffers");
913 gst_poll_wait (pool->priv->poll, GST_CLOCK_TIME_NONE);
921 GST_DEBUG_OBJECT (pool, "we are flushing");
922 return GST_FLOW_FLUSHING;
927 dec_outstanding (GstBufferPool * pool)
929 if (g_atomic_int_dec_and_test (&pool->priv->outstanding)) {
930 /* all buffers are returned to the pool, see if we need to free them */
931 if (GST_BUFFER_POOL_IS_FLUSHING (pool)) {
932 /* take the lock so that set_active is not run concurrently */
933 GST_BUFFER_POOL_LOCK (pool);
934 /* recheck the flushing state in the lock, the pool could have been
935 * set to active again */
936 if (GST_BUFFER_POOL_IS_FLUSHING (pool))
939 GST_BUFFER_POOL_UNLOCK (pool);
945 remove_meta_unpooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
947 if (!GST_META_FLAG_IS_SET (*meta, GST_META_FLAG_POOLED))
953 default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer,
954 GstBufferPoolAcquireParams * params)
956 GST_BUFFER_FLAGS (buffer) = 0;
958 GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
959 GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
960 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
961 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
962 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
964 /* remove all metadata without the POOLED flag */
965 gst_buffer_foreach_meta (buffer, remove_meta_unpooled, pool);
969 * gst_buffer_pool_acquire_buffer:
970 * @pool: a #GstBufferPool
971 * @buffer: (out): a location for a #GstBuffer
972 * @params: (transfer none) (allow-none) parameters.
974 * Acquire a buffer from @pool. @buffer should point to a memory location that
975 * can hold a pointer to the new buffer.
977 * @params can be NULL or contain optional parameters to influence the allocation.
979 * Returns: a #GstFlowReturn such as GST_FLOW_FLUSHING when the pool is
983 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
984 GstBufferPoolAcquireParams * params)
986 GstBufferPoolClass *pclass;
987 GstFlowReturn result;
989 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), GST_FLOW_ERROR);
990 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
992 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
994 /* assume we'll have one more outstanding buffer we need to do that so
995 * that concurrent set_active doesn't clear the buffers */
996 g_atomic_int_inc (&pool->priv->outstanding);
998 if (G_LIKELY (pclass->acquire_buffer))
999 result = pclass->acquire_buffer (pool, buffer, params);
1001 result = GST_FLOW_NOT_SUPPORTED;
1003 if (G_LIKELY (result == GST_FLOW_OK)) {
1004 /* all buffers from the pool point to the pool and have the refcount of the
1005 * pool incremented */
1006 (*buffer)->pool = gst_object_ref (pool);
1007 /* now reset the buffer when needed */
1008 if (G_LIKELY (pclass->reset_buffer))
1009 pclass->reset_buffer (pool, *buffer, params);
1011 dec_outstanding (pool);
1018 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
1020 /* keep it around in our queue */
1021 GST_LOG_OBJECT (pool, "released buffer %p", buffer);
1022 gst_atomic_queue_push (pool->priv->queue, buffer);
1023 gst_poll_write_control (pool->priv->poll);
1027 * gst_buffer_pool_release_buffer:
1028 * @pool: a #GstBufferPool
1029 * @buffer: (transfer full): a #GstBuffer
1031 * Release @buffer to @pool. @buffer should have previously been allocated from
1032 * @pool with gst_buffer_pool_acquire_buffer().
1034 * This function is usually called automatically when the last ref on @buffer
1038 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
1040 GstBufferPoolClass *pclass;
1042 g_return_if_fail (GST_IS_BUFFER_POOL (pool));
1043 g_return_if_fail (buffer != NULL);
1045 /* check that the buffer is ours, all buffers returned to the pool have the
1046 * pool member set to NULL and the pool refcount decreased */
1047 if (!g_atomic_pointer_compare_and_exchange (&buffer->pool, pool, NULL))
1050 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
1052 if (G_LIKELY (pclass->release_buffer))
1053 pclass->release_buffer (pool, buffer);
1055 dec_outstanding (pool);
1057 /* decrease the refcount that the buffer had to us */
1058 gst_object_unref (pool);