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>
41 #include "gstbufferpool.h"
43 GST_DEBUG_CATEGORY_STATIC (gst_buffer_pool_debug);
44 #define GST_CAT_DEFAULT gst_buffer_pool_debug
46 #define GST_BUFFER_POOL_GET_PRIVATE(obj) \
47 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BUFFER_POOL, GstBufferPoolPrivate))
49 #define GST_BUFFER_POOL_LOCK(pool) (g_static_rec_mutex_lock(&pool->priv->rec_lock))
50 #define GST_BUFFER_POOL_UNLOCK(pool) (g_static_rec_mutex_unlock(&pool->priv->rec_lock))
52 struct _GstBufferPoolPrivate
54 GStaticRecMutex rec_lock;
68 static void gst_buffer_pool_finalize (GObject * object);
70 G_DEFINE_TYPE (GstBufferPool, gst_buffer_pool, GST_TYPE_OBJECT);
72 static gboolean default_start (GstBufferPool * pool);
73 static gboolean default_stop (GstBufferPool * pool);
74 static gboolean default_set_config (GstBufferPool * pool,
75 GstStructure * config);
76 static GstFlowReturn default_alloc_buffer (GstBufferPool * pool,
77 GstBuffer ** buffer, GstBufferPoolParams * params);
78 static GstFlowReturn default_acquire_buffer (GstBufferPool * pool,
79 GstBuffer ** buffer, GstBufferPoolParams * params);
80 static void default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer,
81 GstBufferPoolParams * params);
82 static void default_free_buffer (GstBufferPool * pool, GstBuffer * buffer);
83 static void default_release_buffer (GstBufferPool * pool, GstBuffer * buffer);
86 gst_buffer_pool_class_init (GstBufferPoolClass * klass)
88 GObjectClass *gobject_class = (GObjectClass *) klass;
90 g_type_class_add_private (klass, sizeof (GstBufferPoolPrivate));
92 gobject_class->finalize = gst_buffer_pool_finalize;
94 klass->start = default_start;
95 klass->stop = default_stop;
96 klass->set_config = default_set_config;
97 klass->acquire_buffer = default_acquire_buffer;
98 klass->reset_buffer = default_reset_buffer;
99 klass->alloc_buffer = default_alloc_buffer;
100 klass->release_buffer = default_release_buffer;
101 klass->free_buffer = default_free_buffer;
103 GST_DEBUG_CATEGORY_INIT (gst_buffer_pool_debug, "bufferpool", 0,
108 gst_buffer_pool_init (GstBufferPool * pool)
110 pool->priv = GST_BUFFER_POOL_GET_PRIVATE (pool);
112 g_static_rec_mutex_init (&pool->priv->rec_lock);
114 pool->poll = gst_poll_new_timer ();
115 pool->queue = gst_atomic_queue_new (10);
116 pool->flushing = TRUE;
117 pool->active = FALSE;
118 pool->configured = FALSE;
119 pool->started = FALSE;
120 pool->config = gst_structure_new_id_empty (GST_QUARK (BUFFER_POOL_CONFIG));
121 gst_buffer_pool_config_set (pool->config, NULL, 0, 0, 0, 0, 0);
122 gst_poll_write_control (pool->poll);
124 GST_DEBUG_OBJECT (pool, "created");
128 gst_buffer_pool_finalize (GObject * object)
132 pool = GST_BUFFER_POOL_CAST (object);
134 GST_DEBUG_OBJECT (pool, "finalize");
136 gst_buffer_pool_set_active (pool, FALSE);
137 gst_atomic_queue_unref (pool->queue);
138 gst_poll_free (pool->poll);
139 gst_structure_free (pool->config);
140 g_static_rec_mutex_free (&pool->priv->rec_lock);
142 G_OBJECT_CLASS (gst_buffer_pool_parent_class)->finalize (object);
146 * gst_buffer_pool_new:
148 * Creates a new #GstBufferPool instance.
150 * Returns: (transfer full): a new #GstBufferPool instance
153 gst_buffer_pool_new (void)
155 GstBufferPool *result;
157 result = g_object_newv (GST_TYPE_BUFFER_POOL, 0, NULL);
158 GST_DEBUG_OBJECT (result, "created new buffer pool");
164 default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
165 GstBufferPoolParams * params)
167 GstBufferPoolPrivate *priv = pool->priv;
170 *buffer = gst_buffer_new ();
172 mem = gst_allocator_alloc (NULL, priv->size + priv->prefix, priv->align);
173 gst_memory_resize (mem, priv->prefix, priv->size);
174 gst_buffer_take_memory (*buffer, -1, mem);
180 mark_meta_pooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
182 GST_META_FLAG_SET (*meta, GST_META_FLAG_POOLED);
187 /* the default implementation for preallocating the buffers
190 default_start (GstBufferPool * pool)
193 GstBufferPoolPrivate *priv = pool->priv;
194 GstBufferPoolClass *pclass;
196 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
198 /* no alloc function, error */
199 if (G_UNLIKELY (pclass->alloc_buffer == NULL))
202 /* we need to prealloc buffers */
203 for (i = 0; i < priv->min_buffers; i++) {
206 if (pclass->alloc_buffer (pool, &buffer, NULL) != GST_FLOW_OK)
209 gst_buffer_foreach_meta (buffer, mark_meta_pooled, pool);
210 GST_LOG_OBJECT (pool, "prealloced buffer %d: %p", i, buffer);
211 /* release to the queue, we call the vmethod directly, we don't need to do
212 * the other refcount handling right now. */
213 if (G_LIKELY (pclass->release_buffer))
214 pclass->release_buffer (pool, buffer);
221 GST_WARNING_OBJECT (pool, "no alloc function");
226 GST_WARNING_OBJECT (pool, "alloc function failed");
231 /* must be called with the lock */
233 do_start (GstBufferPool * pool)
235 if (!pool->started) {
236 GstBufferPoolClass *pclass;
238 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
240 GST_LOG_OBJECT (pool, "starting");
241 /* start the pool, subclasses should allocate buffers and put them
243 if (G_LIKELY (pclass->start)) {
244 if (!pclass->start (pool))
247 pool->started = TRUE;
254 default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
256 gst_buffer_unref (buffer);
259 /* must be called with the lock */
261 default_stop (GstBufferPool * pool)
264 GstBufferPoolClass *pclass;
266 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
269 while ((buffer = gst_atomic_queue_pop (pool->queue))) {
270 GST_LOG_OBJECT (pool, "freeing %p", buffer);
271 gst_poll_read_control (pool->poll);
273 if (G_LIKELY (pclass->free_buffer))
274 pclass->free_buffer (pool, buffer);
279 /* must be called with the lock */
281 do_stop (GstBufferPool * pool)
284 GstBufferPoolClass *pclass;
286 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
288 GST_LOG_OBJECT (pool, "stopping");
289 if (G_LIKELY (pclass->stop)) {
290 if (!pclass->stop (pool))
293 pool->started = FALSE;
299 * gst_buffer_pool_set_active:
300 * @pool: a #GstBufferPool
301 * @active: the new active state
303 * Control the active state of @pool. When the pool is active, new calls to
304 * gst_buffer_pool_acquire_buffer() will return with GST_FLOW_WRONG_STATE.
306 * Activating the bufferpool will preallocate all resources in the pool based on
307 * the configuration of the pool.
309 * Deactivating will free the resources again when there are no outstanding
310 * buffers. When there are outstanding buffers, they will be freed as soon as
311 * they are all returned to the pool.
313 * Returns: %FALSE when the pool was not configured or when preallocation of the
317 gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
321 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
323 GST_LOG_OBJECT (pool, "active %d", active);
325 GST_BUFFER_POOL_LOCK (pool);
326 /* just return if we are already in the right state */
327 if (pool->active == active)
330 /* we need to be configured */
331 if (!pool->configured)
335 if (!do_start (pool))
338 /* unset the flushing state now */
339 gst_poll_read_control (pool->poll);
340 g_atomic_int_set (&pool->flushing, FALSE);
344 /* set to flushing first */
345 g_atomic_int_set (&pool->flushing, TRUE);
346 gst_poll_write_control (pool->poll);
348 /* when all buffers are in the pool, free them. Else they will be
349 * freed when they are released */
350 outstanding = g_atomic_int_get (&pool->outstanding);
351 GST_LOG_OBJECT (pool, "outstanding buffers %d", outstanding);
352 if (outstanding == 0) {
357 pool->active = active;
358 GST_BUFFER_POOL_UNLOCK (pool);
364 GST_DEBUG_OBJECT (pool, "pool was in the right state");
365 GST_BUFFER_POOL_UNLOCK (pool);
370 GST_ERROR_OBJECT (pool, "pool was not configured");
371 GST_BUFFER_POOL_UNLOCK (pool);
376 GST_ERROR_OBJECT (pool, "start failed");
377 GST_BUFFER_POOL_UNLOCK (pool);
382 GST_WARNING_OBJECT (pool, "stop failed");
383 GST_BUFFER_POOL_UNLOCK (pool);
389 * gst_buffer_pool_is_active:
390 * @pool: a #GstBufferPool
392 * Check if @pool is active. A pool can be activated with the
393 * gst_buffer_pool_set_active() call.
395 * Returns: %TRUE when the pool is active.
398 gst_buffer_pool_is_active (GstBufferPool * pool)
402 GST_BUFFER_POOL_LOCK (pool);
404 GST_BUFFER_POOL_UNLOCK (pool);
410 default_set_config (GstBufferPool * pool, GstStructure * config)
412 GstBufferPoolPrivate *priv = pool->priv;
414 guint size, min_buffers, max_buffers;
417 /* parse the config and keep around */
418 if (!gst_buffer_pool_config_get (config, &caps, &size, &min_buffers,
419 &max_buffers, &prefix, &align))
422 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
425 priv->min_buffers = min_buffers;
426 priv->max_buffers = max_buffers;
427 priv->prefix = prefix;
434 GST_WARNING_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
440 * gst_buffer_pool_set_config:
441 * @pool: a #GstBufferPool
442 * @config: (transfer full): a #GstStructure
444 * Set the configuration of the pool. The pool must be inactive and all buffers
445 * allocated form this pool must be returned or else this function will do
446 * nothing and return FALSE.
448 * @config is a #GstStructure that contains the configuration parameters for
449 * the pool. A default and mandatory set of parameters can be configured with
450 * gst_buffer_pool_config_set(). This function takes ownership of @config.
452 * Returns: TRUE when the configuration could be set.
455 gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
458 GstBufferPoolClass *pclass;
460 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
461 g_return_val_if_fail (config != NULL, FALSE);
463 GST_BUFFER_POOL_LOCK (pool);
464 /* can't change the settings when active */
468 /* we can't change when outstanding buffers */
469 if (g_atomic_int_get (&pool->outstanding) != 0)
470 goto have_outstanding;
472 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
474 /* set the new config */
475 if (G_LIKELY (pclass->set_config))
476 result = pclass->set_config (pool, config);
482 gst_structure_free (pool->config);
483 pool->config = config;
485 /* now we are configured */
486 pool->configured = TRUE;
488 GST_BUFFER_POOL_UNLOCK (pool);
495 GST_WARNING_OBJECT (pool, "can't change config, we are active");
496 GST_BUFFER_POOL_UNLOCK (pool);
501 GST_WARNING_OBJECT (pool, "can't change config, have outstanding buffers");
502 GST_BUFFER_POOL_UNLOCK (pool);
508 * gst_buffer_pool_get_config:
509 * @pool: a #GstBufferPool
511 * Get a copy of the current configuration of the pool. This configuration
512 * can either be modified and used for the gst_buffer_pool_set_config() call
513 * or it must be freed after usage.
515 * Returns: (transfer full): a copy of the current configuration of @pool. use
516 * gst_structure_free() after usage or gst_buffer_pool_set_config().
519 gst_buffer_pool_get_config (GstBufferPool * pool)
521 GstStructure *result;
523 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
525 GST_BUFFER_POOL_UNLOCK (pool);
526 result = gst_structure_copy (pool->config);
527 GST_BUFFER_POOL_UNLOCK (pool);
532 static const gchar *empty_option[] = { NULL };
535 * gst_buffer_pool_get_options:
536 * @pool: a #GstBufferPool
538 * Get a NULL terminated array of string with supported bufferpool options for
539 * @pool. An option would typically be enabled with
540 * gst_buffer_pool_config_add_option().
542 * Returns: (array zero-terminated=1) (transfer none): a NULL terminated array of strings.
545 gst_buffer_pool_get_options (GstBufferPool * pool)
547 GstBufferPoolClass *pclass;
548 const gchar **result;
550 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
552 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
554 if (G_LIKELY (pclass->get_options)) {
555 if ((result = pclass->get_options (pool)) == NULL)
558 result = empty_option;
565 g_warning ("bufferpool subclass returned NULL options");
571 * gst_buffer_pool_has_option:
572 * @pool: a #GstBufferPool
575 * Check if the bufferpool supports @option.
577 * Returns: a NULL terminated array of strings.
580 gst_buffer_pool_has_option (GstBufferPool * pool, const gchar * option)
583 const gchar **options;
585 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
586 g_return_val_if_fail (option != NULL, FALSE);
588 options = gst_buffer_pool_get_options (pool);
590 for (i = 0; options[i]; i++) {
591 if (g_str_equal (options[i], option))
598 * gst_buffer_pool_config_set:
599 * @config: a #GstBufferPool configuration
600 * @caps: caps for the buffers
601 * @size: the size of each buffer, not including prefix
602 * @min_buffers: the minimum amount of buffers to allocate.
603 * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
604 * @prefix: prefix each buffer with this many bytes
605 * @align: alignment of the buffer data.
607 * Configure @config with the given parameters.
610 gst_buffer_pool_config_set (GstStructure * config, const GstCaps * caps,
611 guint size, guint min_buffers, guint max_buffers, guint prefix, guint align)
613 g_return_if_fail (config != NULL);
615 gst_structure_id_set (config,
616 GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
617 GST_QUARK (SIZE), G_TYPE_UINT, size,
618 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
619 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
620 GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
621 GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
625 * gst_buffer_pool_config_add_option:
626 * @config: a #GstBufferPool configuration
627 * @option: an option to add
629 * Enabled the option in @config. This will instruct the @bufferpool to enable
630 * the specified option on the buffers that it allocates.
632 * The supported options by @pool can be retrieved with gst_buffer_pool_get_options().
635 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 array = (GValueArray *) g_value_get_boxed (value);
648 GValue new_array_val = { 0, };
650 array = g_value_array_new (0);
652 g_value_init (&new_array_val, G_TYPE_VALUE_ARRAY);
653 g_value_take_boxed (&new_array_val, array);
655 gst_structure_id_take_value (config, GST_QUARK (OPTIONS), &new_array_val);
657 for (i = 0; i < array->n_values; i++) {
658 value = g_value_array_get_nth (array, i);
659 if (g_str_equal (option, g_value_get_string (value)))
662 g_value_init (&option_value, G_TYPE_STRING);
663 g_value_set_string (&option_value, option);
664 g_value_array_append (array, &option_value);
665 g_value_unset (&option_value);
669 * gst_buffer_pool_config_n_options:
670 * @config: a #GstBufferPool configuration
672 * Retrieve the number of values currently stored in the
673 * options array of the @config structure.
675 * Returns: the options array size as a #guint.
678 gst_buffer_pool_config_n_options (GstStructure * config)
684 g_return_val_if_fail (config != NULL, 0);
686 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
688 array = (GValueArray *) g_value_get_boxed (value);
689 size = array->n_values;
695 * gst_buffer_pool_config_get_option:
696 * @config: a #GstBufferPool configuration
697 * @index: position in the option array to read
699 * Parse an available @config and get the option
700 * at @index of the options API array.
702 * Returns: a #gchar of the option at @index.
705 gst_buffer_pool_config_get_option (GstStructure * config, guint index)
708 const gchar *ret = NULL;
710 g_return_val_if_fail (config != NULL, 0);
712 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
715 GValue *option_value;
717 array = (GValueArray *) g_value_get_boxed (value);
718 option_value = g_value_array_get_nth (array, index);
721 ret = g_value_get_string (option_value);
727 * gst_buffer_pool_config_has_option:
728 * @config: a #GstBufferPool configuration
731 * Check if @config contains @option
733 * Returns: TRUE if the options array contains @option.
736 gst_buffer_pool_config_has_option (GstStructure * config, const gchar * option)
740 g_return_val_if_fail (config != NULL, 0);
742 value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
745 GValue *option_value;
748 array = (GValueArray *) g_value_get_boxed (value);
749 for (i = 0; i < array->n_values; i++) {
750 option_value = g_value_array_get_nth (array, i);
751 if (g_str_equal (option, g_value_get_string (option_value)))
759 * gst_buffer_pool_config_get:
760 * @config: (transfer none): a #GstBufferPool configuration
761 * @caps: (out): the caps of buffers
762 * @size: (out): the size of each buffer, not including prefix
763 * @min_buffers: (out): the minimum amount of buffers to allocate.
764 * @max_buffers: (out): the maximum amount of buffers to allocate or 0 for unlimited.
765 * @prefix: (out): prefix each buffer with this many bytes
766 * @align: (out): alignment of the buffer data.
768 * Get the configuration values from @config.
771 gst_buffer_pool_config_get (GstStructure * config, const GstCaps ** caps,
772 guint * size, guint * min_buffers, guint * max_buffers, guint * prefix,
775 g_return_val_if_fail (config != NULL, FALSE);
777 return gst_structure_id_get (config,
778 GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
779 GST_QUARK (SIZE), G_TYPE_UINT, size,
780 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
781 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
782 GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
783 GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
787 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
788 GstBufferPoolParams * params)
790 GstFlowReturn result;
791 GstBufferPoolClass *pclass;
792 GstBufferPoolPrivate *priv = pool->priv;
794 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
797 if (G_UNLIKELY (g_atomic_int_get (&pool->flushing)))
800 /* try to get a buffer from the queue */
801 *buffer = gst_atomic_queue_pop (pool->queue);
802 if (G_LIKELY (*buffer)) {
803 gst_poll_read_control (pool->poll);
804 result = GST_FLOW_OK;
805 GST_LOG_OBJECT (pool, "acquired buffer %p", *buffer);
810 if (priv->max_buffers == 0) {
811 /* no max_buffers, we allocate some more */
812 if (G_LIKELY (pclass->alloc_buffer)) {
813 result = pclass->alloc_buffer (pool, buffer, params);
814 if (result == GST_FLOW_OK && *buffer)
815 gst_buffer_foreach_meta (*buffer, mark_meta_pooled, pool);
817 result = GST_FLOW_ERROR;
819 result = GST_FLOW_NOT_SUPPORTED;
820 GST_LOG_OBJECT (pool, "alloc buffer %p", *buffer);
824 /* check if we need to wait */
825 if (params && (params->flags & GST_BUFFER_POOL_FLAG_DONTWAIT)) {
826 GST_LOG_OBJECT (pool, "no more buffers");
827 result = GST_FLOW_EOS;
832 GST_LOG_OBJECT (pool, "waiting for free buffers");
833 gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
841 GST_DEBUG_OBJECT (pool, "we are flushing");
842 return GST_FLOW_WRONG_STATE;
847 dec_outstanding (GstBufferPool * pool)
849 if (g_atomic_int_dec_and_test (&pool->outstanding)) {
850 /* all buffers are returned to the pool, see if we need to free them */
851 if (g_atomic_int_get (&pool->flushing)) {
852 /* take the lock so that set_active is not run concurrently */
853 GST_BUFFER_POOL_LOCK (pool);
854 /* recheck the flushing state in the lock, the pool could have been
855 * set to active again */
856 if (g_atomic_int_get (&pool->flushing))
859 GST_BUFFER_POOL_UNLOCK (pool);
865 remove_meta_unpooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
867 if (!GST_META_FLAG_IS_SET (*meta, GST_META_FLAG_POOLED))
873 default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer,
874 GstBufferPoolParams * params)
876 GST_BUFFER_FLAGS (buffer) = 0;
878 GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
879 GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
880 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
881 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
882 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
884 /* remove all metadata without the POOLED flag */
885 gst_buffer_foreach_meta (buffer, remove_meta_unpooled, pool);
889 * gst_buffer_pool_acquire_buffer:
890 * @pool: a #GstBufferPool
891 * @buffer: (out): a location for a #GstBuffer
892 * @params: (transfer none) (allow-none) parameters.
894 * Acquire a buffer from @pool. @buffer should point to a memory location that
895 * can hold a pointer to the new buffer.
897 * @params can be NULL or contain optional parameters to influence the allocation.
899 * Returns: a #GstFlowReturn such as GST_FLOW_WRONG_STATE when the pool is
903 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
904 GstBufferPoolParams * params)
906 GstBufferPoolClass *pclass;
907 GstFlowReturn result;
909 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), GST_FLOW_ERROR);
910 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
912 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
914 /* assume we'll have one more outstanding buffer we need to do that so
915 * that concurrent set_active doesn't clear the buffers */
916 g_atomic_int_inc (&pool->outstanding);
918 if (G_LIKELY (pclass->acquire_buffer))
919 result = pclass->acquire_buffer (pool, buffer, params);
921 result = GST_FLOW_NOT_SUPPORTED;
923 if (G_LIKELY (result == GST_FLOW_OK)) {
924 /* all buffers from the pool point to the pool and have the refcount of the
925 * pool incremented */
926 (*buffer)->pool = gst_object_ref (pool);
927 /* now reset the buffer when needed */
928 if (G_LIKELY (pclass->reset_buffer))
929 pclass->reset_buffer (pool, *buffer, params);
931 dec_outstanding (pool);
938 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
940 /* keep it around in our queue */
941 GST_LOG_OBJECT (pool, "released buffer %p", buffer);
942 gst_atomic_queue_push (pool->queue, buffer);
943 gst_poll_write_control (pool->poll);
947 * gst_buffer_pool_release_buffer:
948 * @pool: a #GstBufferPool
949 * @buffer: (transfer none): a #GstBuffer
951 * Release @buffer to @pool. @buffer should have previously been allocated from
952 * @pool with gst_buffer_pool_acquire_buffer().
954 * This function is usually called automatically when the last ref on @buffer
958 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
960 GstBufferPoolClass *pclass;
962 g_return_if_fail (GST_IS_BUFFER_POOL (pool));
963 g_return_if_fail (buffer != NULL);
965 /* check that the buffer is ours, all buffers returned to the pool have the
966 * pool member set to NULL and the pool refcount decreased */
967 if (!G_ATOMIC_POINTER_COMPARE_AND_EXCHANGE (&buffer->pool, pool, NULL))
970 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
972 if (G_LIKELY (pclass->release_buffer))
973 pclass->release_buffer (pool, buffer);
975 dec_outstanding (pool);
977 /* decrease the refcount that the buffer had to us */
978 gst_object_unref (pool);