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
69 static void gst_buffer_pool_finalize (GObject * object);
71 G_DEFINE_TYPE (GstBufferPool, gst_buffer_pool, GST_TYPE_OBJECT);
73 static gboolean default_start (GstBufferPool * pool);
74 static gboolean default_stop (GstBufferPool * pool);
75 static gboolean default_set_config (GstBufferPool * pool,
76 GstStructure * config);
77 static GstFlowReturn default_alloc_buffer (GstBufferPool * pool,
78 GstBuffer ** buffer, GstBufferPoolParams * params);
79 static GstFlowReturn default_acquire_buffer (GstBufferPool * pool,
80 GstBuffer ** buffer, GstBufferPoolParams * params);
81 static void default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer,
82 GstBufferPoolParams * params);
83 static void default_free_buffer (GstBufferPool * pool, GstBuffer * buffer);
84 static void default_release_buffer (GstBufferPool * pool, GstBuffer * buffer);
87 gst_buffer_pool_class_init (GstBufferPoolClass * klass)
89 GObjectClass *gobject_class = (GObjectClass *) klass;
91 g_type_class_add_private (klass, sizeof (GstBufferPoolPrivate));
93 gobject_class->finalize = gst_buffer_pool_finalize;
95 klass->start = default_start;
96 klass->stop = default_stop;
97 klass->set_config = default_set_config;
98 klass->acquire_buffer = default_acquire_buffer;
99 klass->reset_buffer = default_reset_buffer;
100 klass->alloc_buffer = default_alloc_buffer;
101 klass->release_buffer = default_release_buffer;
102 klass->free_buffer = default_free_buffer;
104 GST_DEBUG_CATEGORY_INIT (gst_buffer_pool_debug, "bufferpool", 0,
109 gst_buffer_pool_init (GstBufferPool * pool)
111 pool->priv = GST_BUFFER_POOL_GET_PRIVATE (pool);
113 g_rec_mutex_init (&pool->priv->rec_lock);
115 pool->poll = gst_poll_new_timer ();
116 pool->queue = gst_atomic_queue_new (10);
117 pool->flushing = TRUE;
118 pool->active = FALSE;
119 pool->configured = FALSE;
120 pool->started = FALSE;
121 pool->config = gst_structure_new_id_empty (GST_QUARK (BUFFER_POOL_CONFIG));
122 gst_buffer_pool_config_set (pool->config, NULL, 0, 0, 0, 0, 0);
123 gst_poll_write_control (pool->poll);
125 GST_DEBUG_OBJECT (pool, "created");
129 gst_buffer_pool_finalize (GObject * object)
133 pool = GST_BUFFER_POOL_CAST (object);
135 GST_DEBUG_OBJECT (pool, "finalize");
137 gst_buffer_pool_set_active (pool, FALSE);
138 gst_atomic_queue_unref (pool->queue);
139 gst_poll_free (pool->poll);
140 gst_structure_free (pool->config);
141 g_rec_mutex_clear (&pool->priv->rec_lock);
143 G_OBJECT_CLASS (gst_buffer_pool_parent_class)->finalize (object);
147 * gst_buffer_pool_new:
149 * Creates a new #GstBufferPool instance.
151 * Returns: (transfer full): a new #GstBufferPool instance
154 gst_buffer_pool_new (void)
156 GstBufferPool *result;
158 result = g_object_newv (GST_TYPE_BUFFER_POOL, 0, NULL);
159 GST_DEBUG_OBJECT (result, "created new buffer pool");
165 default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
166 GstBufferPoolParams * params)
168 GstBufferPoolPrivate *priv = pool->priv;
171 *buffer = gst_buffer_new ();
173 mem = gst_allocator_alloc (NULL, priv->size + priv->prefix, priv->align);
174 gst_memory_resize (mem, priv->prefix, priv->size);
175 gst_buffer_take_memory (*buffer, -1, mem);
181 mark_meta_pooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
183 GST_META_FLAG_SET (*meta, GST_META_FLAG_POOLED);
188 /* the default implementation for preallocating the buffers
191 default_start (GstBufferPool * pool)
194 GstBufferPoolPrivate *priv = pool->priv;
195 GstBufferPoolClass *pclass;
197 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
199 /* no alloc function, error */
200 if (G_UNLIKELY (pclass->alloc_buffer == NULL))
203 /* we need to prealloc buffers */
204 for (i = 0; i < priv->min_buffers; i++) {
207 if (pclass->alloc_buffer (pool, &buffer, NULL) != GST_FLOW_OK)
210 gst_buffer_foreach_meta (buffer, mark_meta_pooled, pool);
211 GST_LOG_OBJECT (pool, "prealloced buffer %d: %p", i, buffer);
212 /* release to the queue, we call the vmethod directly, we don't need to do
213 * the other refcount handling right now. */
214 if (G_LIKELY (pclass->release_buffer))
215 pclass->release_buffer (pool, buffer);
222 GST_WARNING_OBJECT (pool, "no alloc function");
227 GST_WARNING_OBJECT (pool, "alloc function failed");
232 /* must be called with the lock */
234 do_start (GstBufferPool * pool)
236 if (!pool->started) {
237 GstBufferPoolClass *pclass;
239 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
241 GST_LOG_OBJECT (pool, "starting");
242 /* start the pool, subclasses should allocate buffers and put them
244 if (G_LIKELY (pclass->start)) {
245 if (!pclass->start (pool))
248 pool->started = TRUE;
255 default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
257 gst_buffer_unref (buffer);
260 /* must be called with the lock */
262 default_stop (GstBufferPool * pool)
265 GstBufferPoolClass *pclass;
267 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
270 while ((buffer = gst_atomic_queue_pop (pool->queue))) {
271 GST_LOG_OBJECT (pool, "freeing %p", buffer);
272 gst_poll_read_control (pool->poll);
274 if (G_LIKELY (pclass->free_buffer))
275 pclass->free_buffer (pool, buffer);
280 /* must be called with the lock */
282 do_stop (GstBufferPool * pool)
285 GstBufferPoolClass *pclass;
287 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
289 GST_LOG_OBJECT (pool, "stopping");
290 if (G_LIKELY (pclass->stop)) {
291 if (!pclass->stop (pool))
294 pool->started = FALSE;
300 * gst_buffer_pool_set_active:
301 * @pool: a #GstBufferPool
302 * @active: the new active state
304 * Control the active state of @pool. When the pool is active, new calls to
305 * gst_buffer_pool_acquire_buffer() will return with GST_FLOW_FLUSHING.
307 * Activating the bufferpool will preallocate all resources in the pool based on
308 * the configuration of the pool.
310 * Deactivating will free the resources again when there are no outstanding
311 * buffers. When there are outstanding buffers, they will be freed as soon as
312 * they are all returned to the pool.
314 * Returns: %FALSE when the pool was not configured or when preallocation of the
318 gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
322 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
324 GST_LOG_OBJECT (pool, "active %d", active);
326 GST_BUFFER_POOL_LOCK (pool);
327 /* just return if we are already in the right state */
328 if (pool->active == active)
331 /* we need to be configured */
332 if (!pool->configured)
336 if (!do_start (pool))
339 /* unset the flushing state now */
340 gst_poll_read_control (pool->poll);
341 g_atomic_int_set (&pool->flushing, FALSE);
345 /* set to flushing first */
346 g_atomic_int_set (&pool->flushing, TRUE);
347 gst_poll_write_control (pool->poll);
349 /* when all buffers are in the pool, free them. Else they will be
350 * freed when they are released */
351 outstanding = g_atomic_int_get (&pool->outstanding);
352 GST_LOG_OBJECT (pool, "outstanding buffers %d", outstanding);
353 if (outstanding == 0) {
358 pool->active = active;
359 GST_BUFFER_POOL_UNLOCK (pool);
365 GST_DEBUG_OBJECT (pool, "pool was in the right state");
366 GST_BUFFER_POOL_UNLOCK (pool);
371 GST_ERROR_OBJECT (pool, "pool was not configured");
372 GST_BUFFER_POOL_UNLOCK (pool);
377 GST_ERROR_OBJECT (pool, "start failed");
378 GST_BUFFER_POOL_UNLOCK (pool);
383 GST_WARNING_OBJECT (pool, "stop failed");
384 GST_BUFFER_POOL_UNLOCK (pool);
390 * gst_buffer_pool_is_active:
391 * @pool: a #GstBufferPool
393 * Check if @pool is active. A pool can be activated with the
394 * gst_buffer_pool_set_active() call.
396 * Returns: %TRUE when the pool is active.
399 gst_buffer_pool_is_active (GstBufferPool * pool)
403 GST_BUFFER_POOL_LOCK (pool);
405 GST_BUFFER_POOL_UNLOCK (pool);
411 default_set_config (GstBufferPool * pool, GstStructure * config)
413 GstBufferPoolPrivate *priv = pool->priv;
415 guint size, min_buffers, max_buffers;
418 /* parse the config and keep around */
419 if (!gst_buffer_pool_config_get (config, &caps, &size, &min_buffers,
420 &max_buffers, &prefix, &align))
423 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
426 priv->min_buffers = min_buffers;
427 priv->max_buffers = max_buffers;
428 priv->prefix = prefix;
435 GST_WARNING_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
441 * gst_buffer_pool_set_config:
442 * @pool: a #GstBufferPool
443 * @config: (transfer full): a #GstStructure
445 * Set the configuration of the pool. The pool must be inactive and all buffers
446 * allocated form this pool must be returned or else this function will do
447 * nothing and return FALSE.
449 * @config is a #GstStructure that contains the configuration parameters for
450 * the pool. A default and mandatory set of parameters can be configured with
451 * gst_buffer_pool_config_set(). This function takes ownership of @config.
453 * Returns: TRUE when the configuration could be set.
456 gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
459 GstBufferPoolClass *pclass;
461 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
462 g_return_val_if_fail (config != NULL, FALSE);
464 GST_BUFFER_POOL_LOCK (pool);
465 /* can't change the settings when active */
469 /* we can't change when outstanding buffers */
470 if (g_atomic_int_get (&pool->outstanding) != 0)
471 goto have_outstanding;
473 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
475 /* set the new config */
476 if (G_LIKELY (pclass->set_config))
477 result = pclass->set_config (pool, config);
483 gst_structure_free (pool->config);
484 pool->config = config;
486 /* now we are configured */
487 pool->configured = TRUE;
489 GST_BUFFER_POOL_UNLOCK (pool);
496 GST_WARNING_OBJECT (pool, "can't change config, we are active");
497 GST_BUFFER_POOL_UNLOCK (pool);
502 GST_WARNING_OBJECT (pool, "can't change config, have outstanding buffers");
503 GST_BUFFER_POOL_UNLOCK (pool);
509 * gst_buffer_pool_get_config:
510 * @pool: a #GstBufferPool
512 * Get a copy of the current configuration of the pool. This configuration
513 * can either be modified and used for the gst_buffer_pool_set_config() call
514 * or it must be freed after usage.
516 * Returns: (transfer full): a copy of the current configuration of @pool. use
517 * gst_structure_free() after usage or gst_buffer_pool_set_config().
520 gst_buffer_pool_get_config (GstBufferPool * pool)
522 GstStructure *result;
524 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
526 GST_BUFFER_POOL_UNLOCK (pool);
527 result = gst_structure_copy (pool->config);
528 GST_BUFFER_POOL_UNLOCK (pool);
533 static const gchar *empty_option[] = { NULL };
536 * gst_buffer_pool_get_options:
537 * @pool: a #GstBufferPool
539 * Get a NULL terminated array of string with supported bufferpool options for
540 * @pool. An option would typically be enabled with
541 * gst_buffer_pool_config_add_option().
543 * Returns: (array zero-terminated=1) (transfer none): a NULL terminated array of strings.
546 gst_buffer_pool_get_options (GstBufferPool * pool)
548 GstBufferPoolClass *pclass;
549 const gchar **result;
551 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
553 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
555 if (G_LIKELY (pclass->get_options)) {
556 if ((result = pclass->get_options (pool)) == NULL)
559 result = empty_option;
566 g_warning ("bufferpool subclass returned NULL options");
572 * gst_buffer_pool_has_option:
573 * @pool: a #GstBufferPool
576 * Check if the bufferpool supports @option.
578 * Returns: a NULL terminated array of strings.
581 gst_buffer_pool_has_option (GstBufferPool * pool, const gchar * option)
584 const gchar **options;
586 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
587 g_return_val_if_fail (option != NULL, FALSE);
589 options = gst_buffer_pool_get_options (pool);
591 for (i = 0; options[i]; i++) {
592 if (g_str_equal (options[i], option))
599 * gst_buffer_pool_config_set:
600 * @config: a #GstBufferPool configuration
601 * @caps: caps for the buffers
602 * @size: the size of each buffer, not including prefix
603 * @min_buffers: the minimum amount of buffers to allocate.
604 * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
605 * @prefix: prefix each buffer with this many bytes
606 * @align: alignment of the buffer data.
608 * Configure @config with the given parameters.
611 gst_buffer_pool_config_set (GstStructure * config, const GstCaps * caps,
612 guint size, guint min_buffers, guint max_buffers, guint prefix, guint align)
614 g_return_if_fail (config != NULL);
616 gst_structure_id_set (config,
617 GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
618 GST_QUARK (SIZE), G_TYPE_UINT, size,
619 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
620 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
621 GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
622 GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
626 * gst_buffer_pool_config_add_option:
627 * @config: a #GstBufferPool configuration
628 * @option: an option to add
630 * Enabled the option in @config. This will instruct the @bufferpool to enable
631 * the specified option on the buffers that it allocates.
633 * The supported options by @pool can be retrieved with gst_buffer_pool_get_options().
636 gst_buffer_pool_config_add_option (GstStructure * config, const gchar * option)
639 GValue option_value = { 0, };
642 g_return_if_fail (config != NULL);
644 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
646 len = gst_value_array_get_size (value);
647 for (i = 0; i < len; ++i) {
648 const GValue *nth_val = gst_value_array_get_value (value, i);
650 if (g_str_equal (option, g_value_get_string (nth_val)))
654 GValue new_array_val = { 0, };
656 g_value_init (&new_array_val, GST_TYPE_ARRAY);
657 gst_structure_id_take_value (config, GST_QUARK (OPTIONS), &new_array_val);
658 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
660 g_value_init (&option_value, G_TYPE_STRING);
661 g_value_set_string (&option_value, option);
662 gst_value_array_append_value ((GValue *) value, &option_value);
663 g_value_unset (&option_value);
667 * gst_buffer_pool_config_n_options:
668 * @config: a #GstBufferPool configuration
670 * Retrieve the number of values currently stored in the
671 * options array of the @config structure.
673 * Returns: the options array size as a #guint.
676 gst_buffer_pool_config_n_options (GstStructure * config)
681 g_return_val_if_fail (config != NULL, 0);
683 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
685 size = gst_value_array_get_size (value);
691 * gst_buffer_pool_config_get_option:
692 * @config: a #GstBufferPool configuration
693 * @index: position in the option array to read
695 * Parse an available @config and get the option
696 * at @index of the options API array.
698 * Returns: a #gchar of the option at @index.
701 gst_buffer_pool_config_get_option (GstStructure * config, guint index)
704 const gchar *ret = NULL;
706 g_return_val_if_fail (config != NULL, 0);
708 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
710 const GValue *option_value;
712 option_value = gst_value_array_get_value (value, index);
714 ret = g_value_get_string (option_value);
720 * gst_buffer_pool_config_has_option:
721 * @config: a #GstBufferPool configuration
724 * Check if @config contains @option
726 * Returns: TRUE if the options array contains @option.
729 gst_buffer_pool_config_has_option (GstStructure * config, const gchar * option)
734 g_return_val_if_fail (config != NULL, 0);
736 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
738 len = gst_value_array_get_size (value);
739 for (i = 0; i < len; ++i) {
740 const GValue *nth_val = gst_value_array_get_value (value, i);
742 if (g_str_equal (option, g_value_get_string (nth_val)))
750 * gst_buffer_pool_config_get:
751 * @config: (transfer none): a #GstBufferPool configuration
752 * @caps: (out): the caps of buffers
753 * @size: (out): the size of each buffer, not including prefix
754 * @min_buffers: (out): the minimum amount of buffers to allocate.
755 * @max_buffers: (out): the maximum amount of buffers to allocate or 0 for unlimited.
756 * @prefix: (out): prefix each buffer with this many bytes
757 * @align: (out): alignment of the buffer data.
759 * Get the configuration values from @config.
762 gst_buffer_pool_config_get (GstStructure * config, const GstCaps ** caps,
763 guint * size, guint * min_buffers, guint * max_buffers, guint * prefix,
766 g_return_val_if_fail (config != NULL, FALSE);
768 return gst_structure_id_get (config,
769 GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
770 GST_QUARK (SIZE), G_TYPE_UINT, size,
771 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
772 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
773 GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
774 GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
778 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
779 GstBufferPoolParams * params)
781 GstFlowReturn result;
782 GstBufferPoolClass *pclass;
783 GstBufferPoolPrivate *priv = pool->priv;
785 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
788 if (G_UNLIKELY (g_atomic_int_get (&pool->flushing)))
791 /* try to get a buffer from the queue */
792 *buffer = gst_atomic_queue_pop (pool->queue);
793 if (G_LIKELY (*buffer)) {
794 gst_poll_read_control (pool->poll);
795 result = GST_FLOW_OK;
796 GST_LOG_OBJECT (pool, "acquired buffer %p", *buffer);
801 if (priv->max_buffers == 0) {
802 /* no max_buffers, we allocate some more */
803 if (G_LIKELY (pclass->alloc_buffer)) {
804 result = pclass->alloc_buffer (pool, buffer, params);
805 if (result == GST_FLOW_OK && *buffer)
806 gst_buffer_foreach_meta (*buffer, mark_meta_pooled, pool);
808 result = GST_FLOW_ERROR;
810 result = GST_FLOW_NOT_SUPPORTED;
811 GST_LOG_OBJECT (pool, "alloc buffer %p", *buffer);
815 /* check if we need to wait */
816 if (params && (params->flags & GST_BUFFER_POOL_FLAG_DONTWAIT)) {
817 GST_LOG_OBJECT (pool, "no more buffers");
818 result = GST_FLOW_EOS;
823 GST_LOG_OBJECT (pool, "waiting for free buffers");
824 gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
832 GST_DEBUG_OBJECT (pool, "we are flushing");
833 return GST_FLOW_FLUSHING;
838 dec_outstanding (GstBufferPool * pool)
840 if (g_atomic_int_dec_and_test (&pool->outstanding)) {
841 /* all buffers are returned to the pool, see if we need to free them */
842 if (g_atomic_int_get (&pool->flushing)) {
843 /* take the lock so that set_active is not run concurrently */
844 GST_BUFFER_POOL_LOCK (pool);
845 /* recheck the flushing state in the lock, the pool could have been
846 * set to active again */
847 if (g_atomic_int_get (&pool->flushing))
850 GST_BUFFER_POOL_UNLOCK (pool);
856 remove_meta_unpooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
858 if (!GST_META_FLAG_IS_SET (*meta, GST_META_FLAG_POOLED))
864 default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer,
865 GstBufferPoolParams * params)
867 GST_BUFFER_FLAGS (buffer) = 0;
869 GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
870 GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
871 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
872 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
873 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
875 /* remove all metadata without the POOLED flag */
876 gst_buffer_foreach_meta (buffer, remove_meta_unpooled, pool);
880 * gst_buffer_pool_acquire_buffer:
881 * @pool: a #GstBufferPool
882 * @buffer: (out): a location for a #GstBuffer
883 * @params: (transfer none) (allow-none) parameters.
885 * Acquire a buffer from @pool. @buffer should point to a memory location that
886 * can hold a pointer to the new buffer.
888 * @params can be NULL or contain optional parameters to influence the allocation.
890 * Returns: a #GstFlowReturn such as GST_FLOW_FLUSHING when the pool is
894 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
895 GstBufferPoolParams * params)
897 GstBufferPoolClass *pclass;
898 GstFlowReturn result;
900 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), GST_FLOW_ERROR);
901 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
903 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
905 /* assume we'll have one more outstanding buffer we need to do that so
906 * that concurrent set_active doesn't clear the buffers */
907 g_atomic_int_inc (&pool->outstanding);
909 if (G_LIKELY (pclass->acquire_buffer))
910 result = pclass->acquire_buffer (pool, buffer, params);
912 result = GST_FLOW_NOT_SUPPORTED;
914 if (G_LIKELY (result == GST_FLOW_OK)) {
915 /* all buffers from the pool point to the pool and have the refcount of the
916 * pool incremented */
917 (*buffer)->pool = gst_object_ref (pool);
918 /* now reset the buffer when needed */
919 if (G_LIKELY (pclass->reset_buffer))
920 pclass->reset_buffer (pool, *buffer, params);
922 dec_outstanding (pool);
929 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
931 /* keep it around in our queue */
932 GST_LOG_OBJECT (pool, "released buffer %p", buffer);
933 gst_atomic_queue_push (pool->queue, buffer);
934 gst_poll_write_control (pool->poll);
938 * gst_buffer_pool_release_buffer:
939 * @pool: a #GstBufferPool
940 * @buffer: (transfer none): a #GstBuffer
942 * Release @buffer to @pool. @buffer should have previously been allocated from
943 * @pool with gst_buffer_pool_acquire_buffer().
945 * This function is usually called automatically when the last ref on @buffer
949 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
951 GstBufferPoolClass *pclass;
953 g_return_if_fail (GST_IS_BUFFER_POOL (pool));
954 g_return_if_fail (buffer != NULL);
956 /* check that the buffer is ours, all buffers returned to the pool have the
957 * pool member set to NULL and the pool refcount decreased */
958 if (!g_atomic_pointer_compare_and_exchange (&buffer->pool, pool, NULL))
961 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
963 if (G_LIKELY (pclass->release_buffer))
964 pclass->release_buffer (pool, buffer);
966 dec_outstanding (pool);
968 /* decrease the refcount that the buffer had to us */
969 gst_object_unref (pool);