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>
42 #include "gstbufferpool.h"
44 GST_DEBUG_CATEGORY_STATIC (gst_buffer_pool_debug);
45 #define GST_CAT_DEFAULT gst_buffer_pool_debug
47 #define GST_BUFFER_POOL_GET_PRIVATE(obj) \
48 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BUFFER_POOL, GstBufferPoolPrivate))
50 #define GST_BUFFER_POOL_LOCK(pool) (g_rec_mutex_lock(&pool->priv->rec_lock))
51 #define GST_BUFFER_POOL_UNLOCK(pool) (g_rec_mutex_unlock(&pool->priv->rec_lock))
53 struct _GstBufferPoolPrivate
55 GstAtomicQueue *queue;
80 static void gst_buffer_pool_finalize (GObject * object);
82 G_DEFINE_TYPE (GstBufferPool, gst_buffer_pool, GST_TYPE_OBJECT);
84 static gboolean default_start (GstBufferPool * pool);
85 static gboolean default_stop (GstBufferPool * pool);
86 static gboolean default_set_config (GstBufferPool * pool,
87 GstStructure * config);
88 static GstFlowReturn default_alloc_buffer (GstBufferPool * pool,
89 GstBuffer ** buffer, GstBufferPoolParams * params);
90 static GstFlowReturn default_acquire_buffer (GstBufferPool * pool,
91 GstBuffer ** buffer, GstBufferPoolParams * params);
92 static void default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer,
93 GstBufferPoolParams * params);
94 static void default_free_buffer (GstBufferPool * pool, GstBuffer * buffer);
95 static void default_release_buffer (GstBufferPool * pool, GstBuffer * buffer);
98 gst_buffer_pool_class_init (GstBufferPoolClass * klass)
100 GObjectClass *gobject_class = (GObjectClass *) klass;
102 g_type_class_add_private (klass, sizeof (GstBufferPoolPrivate));
104 gobject_class->finalize = gst_buffer_pool_finalize;
106 klass->start = default_start;
107 klass->stop = default_stop;
108 klass->set_config = default_set_config;
109 klass->acquire_buffer = default_acquire_buffer;
110 klass->reset_buffer = default_reset_buffer;
111 klass->alloc_buffer = default_alloc_buffer;
112 klass->release_buffer = default_release_buffer;
113 klass->free_buffer = default_free_buffer;
115 GST_DEBUG_CATEGORY_INIT (gst_buffer_pool_debug, "bufferpool", 0,
120 gst_buffer_pool_init (GstBufferPool * pool)
122 pool->priv = GST_BUFFER_POOL_GET_PRIVATE (pool);
124 g_rec_mutex_init (&pool->priv->rec_lock);
126 pool->priv->poll = gst_poll_new_timer ();
127 pool->priv->queue = gst_atomic_queue_new (10);
129 pool->priv->active = FALSE;
130 pool->priv->configured = FALSE;
131 pool->priv->started = FALSE;
133 gst_structure_new_id_empty (GST_QUARK (BUFFER_POOL_CONFIG));
134 gst_buffer_pool_config_set (pool->priv->config, NULL, 0, 0, 0, 0, 0);
135 gst_poll_write_control (pool->priv->poll);
137 GST_DEBUG_OBJECT (pool, "created");
141 gst_buffer_pool_finalize (GObject * object)
145 pool = GST_BUFFER_POOL_CAST (object);
147 GST_DEBUG_OBJECT (pool, "finalize");
149 gst_buffer_pool_set_active (pool, FALSE);
150 gst_atomic_queue_unref (pool->priv->queue);
151 gst_poll_free (pool->priv->poll);
152 gst_structure_free (pool->priv->config);
153 g_rec_mutex_clear (&pool->priv->rec_lock);
155 G_OBJECT_CLASS (gst_buffer_pool_parent_class)->finalize (object);
159 * gst_buffer_pool_new:
161 * Creates a new #GstBufferPool instance.
163 * Returns: (transfer full): a new #GstBufferPool instance
166 gst_buffer_pool_new (void)
168 GstBufferPool *result;
170 result = g_object_newv (GST_TYPE_BUFFER_POOL, 0, NULL);
171 GST_DEBUG_OBJECT (result, "created new buffer pool");
177 default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
178 GstBufferPoolParams * params)
180 GstBufferPoolPrivate *priv = pool->priv;
183 *buffer = gst_buffer_new ();
185 mem = gst_allocator_alloc (NULL, priv->size + priv->prefix, priv->align);
186 gst_memory_resize (mem, priv->prefix, priv->size);
187 gst_buffer_take_memory (*buffer, -1, mem);
193 mark_meta_pooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
195 GST_META_FLAG_SET (*meta, GST_META_FLAG_POOLED);
200 /* the default implementation for preallocating the buffers
203 default_start (GstBufferPool * pool)
206 GstBufferPoolPrivate *priv = pool->priv;
207 GstBufferPoolClass *pclass;
209 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
211 /* no alloc function, error */
212 if (G_UNLIKELY (pclass->alloc_buffer == NULL))
215 /* we need to prealloc buffers */
216 for (i = 0; i < priv->min_buffers; i++) {
219 if (pclass->alloc_buffer (pool, &buffer, NULL) != GST_FLOW_OK)
222 gst_buffer_foreach_meta (buffer, mark_meta_pooled, pool);
223 GST_LOG_OBJECT (pool, "prealloced buffer %d: %p", i, buffer);
224 /* release to the queue, we call the vmethod directly, we don't need to do
225 * the other refcount handling right now. */
226 if (G_LIKELY (pclass->release_buffer))
227 pclass->release_buffer (pool, buffer);
234 GST_WARNING_OBJECT (pool, "no alloc function");
239 GST_WARNING_OBJECT (pool, "alloc function failed");
244 /* must be called with the lock */
246 do_start (GstBufferPool * pool)
248 if (!pool->priv->started) {
249 GstBufferPoolClass *pclass;
251 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
253 GST_LOG_OBJECT (pool, "starting");
254 /* start the pool, subclasses should allocate buffers and put them
256 if (G_LIKELY (pclass->start)) {
257 if (!pclass->start (pool))
260 pool->priv->started = TRUE;
267 default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
269 gst_buffer_unref (buffer);
272 /* must be called with the lock */
274 default_stop (GstBufferPool * pool)
277 GstBufferPoolClass *pclass;
279 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
282 while ((buffer = gst_atomic_queue_pop (pool->priv->queue))) {
283 GST_LOG_OBJECT (pool, "freeing %p", buffer);
284 gst_poll_read_control (pool->priv->poll);
286 if (G_LIKELY (pclass->free_buffer))
287 pclass->free_buffer (pool, buffer);
292 /* must be called with the lock */
294 do_stop (GstBufferPool * pool)
296 if (pool->priv->started) {
297 GstBufferPoolClass *pclass;
299 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
301 GST_LOG_OBJECT (pool, "stopping");
302 if (G_LIKELY (pclass->stop)) {
303 if (!pclass->stop (pool))
306 pool->priv->started = FALSE;
312 * gst_buffer_pool_set_active:
313 * @pool: a #GstBufferPool
314 * @active: the new active state
316 * Control the active state of @pool. When the pool is active, new calls to
317 * gst_buffer_pool_acquire_buffer() will return with GST_FLOW_FLUSHING.
319 * Activating the bufferpool will preallocate all resources in the pool based on
320 * the configuration of the pool.
322 * Deactivating will free the resources again when there are no outstanding
323 * buffers. When there are outstanding buffers, they will be freed as soon as
324 * they are all returned to the pool.
326 * Returns: %FALSE when the pool was not configured or when preallocation of the
330 gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
334 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
336 GST_LOG_OBJECT (pool, "active %d", active);
338 GST_BUFFER_POOL_LOCK (pool);
339 /* just return if we are already in the right state */
340 if (pool->priv->active == active)
343 /* we need to be configured */
344 if (!pool->priv->configured)
348 if (!do_start (pool))
351 /* unset the flushing state now */
352 gst_poll_read_control (pool->priv->poll);
353 g_atomic_int_set (&pool->flushing, 0);
357 /* set to flushing first */
358 g_atomic_int_set (&pool->flushing, 1);
359 gst_poll_write_control (pool->priv->poll);
361 /* when all buffers are in the pool, free them. Else they will be
362 * freed when they are released */
363 outstanding = g_atomic_int_get (&pool->priv->outstanding);
364 GST_LOG_OBJECT (pool, "outstanding buffers %d", outstanding);
365 if (outstanding == 0) {
370 pool->priv->active = active;
371 GST_BUFFER_POOL_UNLOCK (pool);
377 GST_DEBUG_OBJECT (pool, "pool was in the right state");
378 GST_BUFFER_POOL_UNLOCK (pool);
383 GST_ERROR_OBJECT (pool, "pool was not configured");
384 GST_BUFFER_POOL_UNLOCK (pool);
389 GST_ERROR_OBJECT (pool, "start failed");
390 GST_BUFFER_POOL_UNLOCK (pool);
395 GST_WARNING_OBJECT (pool, "stop failed");
396 GST_BUFFER_POOL_UNLOCK (pool);
402 * gst_buffer_pool_is_active:
403 * @pool: a #GstBufferPool
405 * Check if @pool is active. A pool can be activated with the
406 * gst_buffer_pool_set_active() call.
408 * Returns: %TRUE when the pool is active.
411 gst_buffer_pool_is_active (GstBufferPool * pool)
415 GST_BUFFER_POOL_LOCK (pool);
416 res = pool->priv->active;
417 GST_BUFFER_POOL_UNLOCK (pool);
423 default_set_config (GstBufferPool * pool, GstStructure * config)
425 GstBufferPoolPrivate *priv = pool->priv;
427 guint size, min_buffers, max_buffers;
430 /* parse the config and keep around */
431 if (!gst_buffer_pool_config_get (config, &caps, &size, &min_buffers,
432 &max_buffers, &prefix, &align))
435 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
438 priv->min_buffers = min_buffers;
439 priv->max_buffers = max_buffers;
440 priv->prefix = prefix;
447 GST_WARNING_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
453 * gst_buffer_pool_set_config:
454 * @pool: a #GstBufferPool
455 * @config: (transfer full): a #GstStructure
457 * Set the configuration of the pool. The pool must be inactive and all buffers
458 * allocated form this pool must be returned or else this function will do
459 * nothing and return FALSE.
461 * @config is a #GstStructure that contains the configuration parameters for
462 * the pool. A default and mandatory set of parameters can be configured with
463 * gst_buffer_pool_config_set(). This function takes ownership of @config.
465 * Returns: TRUE when the configuration could be set.
468 gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
471 GstBufferPoolClass *pclass;
473 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
474 g_return_val_if_fail (config != NULL, FALSE);
476 GST_BUFFER_POOL_LOCK (pool);
477 /* can't change the settings when active */
478 if (pool->priv->active)
481 /* we can't change when outstanding buffers */
482 if (g_atomic_int_get (&pool->priv->outstanding) != 0)
483 goto have_outstanding;
485 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
487 /* set the new config */
488 if (G_LIKELY (pclass->set_config))
489 result = pclass->set_config (pool, config);
494 if (pool->priv->config)
495 gst_structure_free (pool->priv->config);
496 pool->priv->config = config;
498 /* now we are configured */
499 pool->priv->configured = TRUE;
501 GST_BUFFER_POOL_UNLOCK (pool);
508 GST_WARNING_OBJECT (pool, "can't change config, we are active");
509 GST_BUFFER_POOL_UNLOCK (pool);
514 GST_WARNING_OBJECT (pool, "can't change config, have outstanding buffers");
515 GST_BUFFER_POOL_UNLOCK (pool);
521 * gst_buffer_pool_get_config:
522 * @pool: a #GstBufferPool
524 * Get a copy of the current configuration of the pool. This configuration
525 * can either be modified and used for the gst_buffer_pool_set_config() call
526 * or it must be freed after usage.
528 * Returns: (transfer full): a copy of the current configuration of @pool. use
529 * gst_structure_free() after usage or gst_buffer_pool_set_config().
532 gst_buffer_pool_get_config (GstBufferPool * pool)
534 GstStructure *result;
536 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
538 GST_BUFFER_POOL_UNLOCK (pool);
539 result = gst_structure_copy (pool->priv->config);
540 GST_BUFFER_POOL_UNLOCK (pool);
545 static const gchar *empty_option[] = { NULL };
548 * gst_buffer_pool_get_options:
549 * @pool: a #GstBufferPool
551 * Get a NULL terminated array of string with supported bufferpool options for
552 * @pool. An option would typically be enabled with
553 * gst_buffer_pool_config_add_option().
555 * Returns: (array zero-terminated=1) (transfer none): a NULL terminated array of strings.
558 gst_buffer_pool_get_options (GstBufferPool * pool)
560 GstBufferPoolClass *pclass;
561 const gchar **result;
563 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
565 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
567 if (G_LIKELY (pclass->get_options)) {
568 if ((result = pclass->get_options (pool)) == NULL)
571 result = empty_option;
578 g_warning ("bufferpool subclass returned NULL options");
584 * gst_buffer_pool_has_option:
585 * @pool: a #GstBufferPool
588 * Check if the bufferpool supports @option.
590 * Returns: a NULL terminated array of strings.
593 gst_buffer_pool_has_option (GstBufferPool * pool, const gchar * option)
596 const gchar **options;
598 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
599 g_return_val_if_fail (option != NULL, FALSE);
601 options = gst_buffer_pool_get_options (pool);
603 for (i = 0; options[i]; i++) {
604 if (g_str_equal (options[i], option))
611 * gst_buffer_pool_config_set:
612 * @config: a #GstBufferPool configuration
613 * @caps: caps for the buffers
614 * @size: the size of each buffer, not including prefix
615 * @min_buffers: the minimum amount of buffers to allocate.
616 * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
617 * @prefix: prefix each buffer with this many bytes
618 * @align: alignment of the buffer data.
620 * Configure @config with the given parameters.
623 gst_buffer_pool_config_set (GstStructure * config, const GstCaps * caps,
624 guint size, guint min_buffers, guint max_buffers, guint prefix, guint align)
626 g_return_if_fail (config != NULL);
628 gst_structure_id_set (config,
629 GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
630 GST_QUARK (SIZE), G_TYPE_UINT, size,
631 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
632 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
633 GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
634 GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
638 * gst_buffer_pool_config_add_option:
639 * @config: a #GstBufferPool configuration
640 * @option: an option to add
642 * Enabled the option in @config. This will instruct the @bufferpool to enable
643 * the specified option on the buffers that it allocates.
645 * The supported options by @pool can be retrieved with gst_buffer_pool_get_options().
648 gst_buffer_pool_config_add_option (GstStructure * config, const gchar * option)
651 GValue option_value = { 0, };
654 g_return_if_fail (config != NULL);
656 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
658 len = gst_value_array_get_size (value);
659 for (i = 0; i < len; ++i) {
660 const GValue *nth_val = gst_value_array_get_value (value, i);
662 if (g_str_equal (option, g_value_get_string (nth_val)))
666 GValue new_array_val = { 0, };
668 g_value_init (&new_array_val, GST_TYPE_ARRAY);
669 gst_structure_id_take_value (config, GST_QUARK (OPTIONS), &new_array_val);
670 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
672 g_value_init (&option_value, G_TYPE_STRING);
673 g_value_set_string (&option_value, option);
674 gst_value_array_append_value ((GValue *) value, &option_value);
675 g_value_unset (&option_value);
679 * gst_buffer_pool_config_n_options:
680 * @config: a #GstBufferPool configuration
682 * Retrieve the number of values currently stored in the
683 * options array of the @config structure.
685 * Returns: the options array size as a #guint.
688 gst_buffer_pool_config_n_options (GstStructure * config)
693 g_return_val_if_fail (config != NULL, 0);
695 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
697 size = gst_value_array_get_size (value);
703 * gst_buffer_pool_config_get_option:
704 * @config: a #GstBufferPool configuration
705 * @index: position in the option array to read
707 * Parse an available @config and get the option
708 * at @index of the options API array.
710 * Returns: a #gchar of the option at @index.
713 gst_buffer_pool_config_get_option (GstStructure * config, guint index)
716 const gchar *ret = NULL;
718 g_return_val_if_fail (config != NULL, 0);
720 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
722 const GValue *option_value;
724 option_value = gst_value_array_get_value (value, index);
726 ret = g_value_get_string (option_value);
732 * gst_buffer_pool_config_has_option:
733 * @config: a #GstBufferPool configuration
736 * Check if @config contains @option
738 * Returns: TRUE if the options array contains @option.
741 gst_buffer_pool_config_has_option (GstStructure * config, const gchar * option)
746 g_return_val_if_fail (config != NULL, 0);
748 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
750 len = gst_value_array_get_size (value);
751 for (i = 0; i < len; ++i) {
752 const GValue *nth_val = gst_value_array_get_value (value, i);
754 if (g_str_equal (option, g_value_get_string (nth_val)))
762 * gst_buffer_pool_config_get:
763 * @config: (transfer none): a #GstBufferPool configuration
764 * @caps: (out): the caps of buffers
765 * @size: (out): the size of each buffer, not including prefix
766 * @min_buffers: (out): the minimum amount of buffers to allocate.
767 * @max_buffers: (out): the maximum amount of buffers to allocate or 0 for unlimited.
768 * @prefix: (out): prefix each buffer with this many bytes
769 * @align: (out): alignment of the buffer data.
771 * Get the configuration values from @config.
774 gst_buffer_pool_config_get (GstStructure * config, const GstCaps ** caps,
775 guint * size, guint * min_buffers, guint * max_buffers, guint * prefix,
778 g_return_val_if_fail (config != NULL, FALSE);
780 return gst_structure_id_get (config,
781 GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
782 GST_QUARK (SIZE), G_TYPE_UINT, size,
783 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
784 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
785 GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
786 GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
790 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
791 GstBufferPoolParams * params)
793 GstFlowReturn result;
794 GstBufferPoolClass *pclass;
795 GstBufferPoolPrivate *priv = pool->priv;
797 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
800 if (G_UNLIKELY (GST_BUFFER_POOL_IS_FLUSHING (pool)))
803 /* try to get a buffer from the queue */
804 *buffer = gst_atomic_queue_pop (pool->priv->queue);
805 if (G_LIKELY (*buffer)) {
806 gst_poll_read_control (pool->priv->poll);
807 result = GST_FLOW_OK;
808 GST_LOG_OBJECT (pool, "acquired buffer %p", *buffer);
813 if (priv->max_buffers == 0) {
814 /* no max_buffers, we allocate some more */
815 if (G_LIKELY (pclass->alloc_buffer)) {
816 result = pclass->alloc_buffer (pool, buffer, params);
817 if (result == GST_FLOW_OK && *buffer)
818 gst_buffer_foreach_meta (*buffer, mark_meta_pooled, pool);
820 result = GST_FLOW_ERROR;
822 result = GST_FLOW_NOT_SUPPORTED;
823 GST_LOG_OBJECT (pool, "alloc buffer %p", *buffer);
827 /* check if we need to wait */
828 if (params && (params->flags & GST_BUFFER_POOL_FLAG_DONTWAIT)) {
829 GST_LOG_OBJECT (pool, "no more buffers");
830 result = GST_FLOW_EOS;
835 GST_LOG_OBJECT (pool, "waiting for free buffers");
836 gst_poll_wait (pool->priv->poll, GST_CLOCK_TIME_NONE);
844 GST_DEBUG_OBJECT (pool, "we are flushing");
845 return GST_FLOW_FLUSHING;
850 dec_outstanding (GstBufferPool * pool)
852 if (g_atomic_int_dec_and_test (&pool->priv->outstanding)) {
853 /* all buffers are returned to the pool, see if we need to free them */
854 if (GST_BUFFER_POOL_IS_FLUSHING (pool)) {
855 /* take the lock so that set_active is not run concurrently */
856 GST_BUFFER_POOL_LOCK (pool);
857 /* recheck the flushing state in the lock, the pool could have been
858 * set to active again */
859 if (GST_BUFFER_POOL_IS_FLUSHING (pool))
862 GST_BUFFER_POOL_UNLOCK (pool);
868 remove_meta_unpooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
870 if (!GST_META_FLAG_IS_SET (*meta, GST_META_FLAG_POOLED))
876 default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer,
877 GstBufferPoolParams * params)
879 GST_BUFFER_FLAGS (buffer) = 0;
881 GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
882 GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
883 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
884 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
885 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
887 /* remove all metadata without the POOLED flag */
888 gst_buffer_foreach_meta (buffer, remove_meta_unpooled, pool);
892 * gst_buffer_pool_acquire_buffer:
893 * @pool: a #GstBufferPool
894 * @buffer: (out): a location for a #GstBuffer
895 * @params: (transfer none) (allow-none) parameters.
897 * Acquire a buffer from @pool. @buffer should point to a memory location that
898 * can hold a pointer to the new buffer.
900 * @params can be NULL or contain optional parameters to influence the allocation.
902 * Returns: a #GstFlowReturn such as GST_FLOW_FLUSHING when the pool is
906 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
907 GstBufferPoolParams * params)
909 GstBufferPoolClass *pclass;
910 GstFlowReturn result;
912 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), GST_FLOW_ERROR);
913 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
915 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
917 /* assume we'll have one more outstanding buffer we need to do that so
918 * that concurrent set_active doesn't clear the buffers */
919 g_atomic_int_inc (&pool->priv->outstanding);
921 if (G_LIKELY (pclass->acquire_buffer))
922 result = pclass->acquire_buffer (pool, buffer, params);
924 result = GST_FLOW_NOT_SUPPORTED;
926 if (G_LIKELY (result == GST_FLOW_OK)) {
927 /* all buffers from the pool point to the pool and have the refcount of the
928 * pool incremented */
929 (*buffer)->pool = gst_object_ref (pool);
930 /* now reset the buffer when needed */
931 if (G_LIKELY (pclass->reset_buffer))
932 pclass->reset_buffer (pool, *buffer, params);
934 dec_outstanding (pool);
941 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
943 /* keep it around in our queue */
944 GST_LOG_OBJECT (pool, "released buffer %p", buffer);
945 gst_atomic_queue_push (pool->priv->queue, buffer);
946 gst_poll_write_control (pool->priv->poll);
950 * gst_buffer_pool_release_buffer:
951 * @pool: a #GstBufferPool
952 * @buffer: (transfer none): a #GstBuffer
954 * Release @buffer to @pool. @buffer should have previously been allocated from
955 * @pool with gst_buffer_pool_acquire_buffer().
957 * This function is usually called automatically when the last ref on @buffer
961 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
963 GstBufferPoolClass *pclass;
965 g_return_if_fail (GST_IS_BUFFER_POOL (pool));
966 g_return_if_fail (buffer != NULL);
968 /* check that the buffer is ours, all buffers returned to the pool have the
969 * pool member set to NULL and the pool refcount decreased */
970 if (!g_atomic_pointer_compare_and_exchange (&buffer->pool, pool, NULL))
973 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
975 if (G_LIKELY (pclass->release_buffer))
976 pclass->release_buffer (pool, buffer);
978 dec_outstanding (pool);
980 /* decrease the refcount that the buffer had to us */
981 gst_object_unref (pool);