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"
35 #include <sys/types.h>
40 #include "gstbufferpool.h"
42 GST_DEBUG_CATEGORY_STATIC (gst_buffer_pool_debug);
43 #define GST_CAT_DEFAULT gst_buffer_pool_debug
45 #define GST_BUFFER_POOL_GET_PRIVATE(obj) \
46 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BUFFER_POOL, GstBufferPoolPrivate))
48 #define GST_BUFFER_POOL_LOCK(pool) (g_static_rec_mutex_lock(&pool->priv->rec_lock))
49 #define GST_BUFFER_POOL_UNLOCK(pool) (g_static_rec_mutex_unlock(&pool->priv->rec_lock))
51 struct _GstBufferPoolPrivate
53 GStaticRecMutex rec_lock;
67 static void gst_buffer_pool_finalize (GObject * object);
69 G_DEFINE_TYPE (GstBufferPool, gst_buffer_pool, GST_TYPE_OBJECT);
71 static gboolean default_start (GstBufferPool * pool);
72 static gboolean default_stop (GstBufferPool * pool);
73 static gboolean default_set_config (GstBufferPool * pool,
74 GstStructure * config);
75 static GstFlowReturn default_alloc_buffer (GstBufferPool * pool,
76 GstBuffer ** buffer, GstBufferPoolParams * params);
77 static GstFlowReturn default_acquire_buffer (GstBufferPool * pool,
78 GstBuffer ** buffer, GstBufferPoolParams * params);
79 static void default_free_buffer (GstBufferPool * pool, GstBuffer * buffer);
80 static void default_release_buffer (GstBufferPool * pool, GstBuffer * buffer);
83 gst_buffer_pool_class_init (GstBufferPoolClass * klass)
85 GObjectClass *gobject_class = (GObjectClass *) klass;
87 g_type_class_add_private (klass, sizeof (GstBufferPoolPrivate));
89 gobject_class->finalize = gst_buffer_pool_finalize;
91 klass->start = default_start;
92 klass->stop = default_stop;
93 klass->set_config = default_set_config;
94 klass->acquire_buffer = default_acquire_buffer;
95 klass->alloc_buffer = default_alloc_buffer;
96 klass->release_buffer = default_release_buffer;
97 klass->free_buffer = default_free_buffer;
99 GST_DEBUG_CATEGORY_INIT (gst_buffer_pool_debug, "bufferpool", 0,
104 gst_buffer_pool_init (GstBufferPool * pool)
106 pool->priv = GST_BUFFER_POOL_GET_PRIVATE (pool);
108 g_static_rec_mutex_init (&pool->priv->rec_lock);
110 pool->poll = gst_poll_new_timer ();
111 pool->queue = gst_atomic_queue_new (10);
112 pool->flushing = TRUE;
113 pool->active = FALSE;
114 pool->configured = FALSE;
115 pool->started = FALSE;
116 pool->config = gst_structure_id_empty_new (GST_QUARK (BUFFER_POOL_CONFIG));
117 gst_buffer_pool_config_set (pool->config, NULL, 0, 0, 0, 0, 1);
119 GST_DEBUG_OBJECT (pool, "created");
123 gst_buffer_pool_finalize (GObject * object)
127 pool = GST_BUFFER_POOL_CAST (object);
129 GST_DEBUG_OBJECT (pool, "finalize");
131 gst_buffer_pool_set_active (pool, FALSE);
132 gst_atomic_queue_unref (pool->queue);
133 gst_poll_free (pool->poll);
134 gst_structure_free (pool->config);
135 g_static_rec_mutex_free (&pool->priv->rec_lock);
137 G_OBJECT_CLASS (gst_buffer_pool_parent_class)->finalize (object);
141 * gst_buffer_pool_new:
143 * Creates a new #GstBufferPool instance.
145 * Returns: a new #GstBufferPool instance
148 gst_buffer_pool_new (void)
150 GstBufferPool *result;
152 result = g_object_newv (GST_TYPE_BUFFER_POOL, 0, NULL);
153 GST_DEBUG_OBJECT (result, "created new buffer pool");
159 default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
160 GstBufferPoolParams * params)
162 GstBufferPoolPrivate *priv = pool->priv;
165 *buffer = gst_buffer_new ();
167 mem = gst_allocator_alloc (NULL, priv->size + priv->prefix, priv->align);
168 gst_memory_resize (mem, priv->prefix, priv->size);
169 gst_buffer_take_memory (*buffer, -1, mem);
174 /* the default implementation for preallocating the buffers
177 default_start (GstBufferPool * pool)
180 GstBufferPoolPrivate *priv = pool->priv;
181 GstBufferPoolClass *pclass;
183 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
185 /* no alloc function, error */
186 if (G_UNLIKELY (pclass->alloc_buffer == NULL))
189 /* we need to prealloc buffers */
190 for (i = 0; i < priv->min_buffers; i++) {
193 if (pclass->alloc_buffer (pool, &buffer, NULL) != GST_FLOW_OK)
196 GST_LOG_OBJECT (pool, "prealloced buffer %d: %p", i, buffer);
197 /* store in the queue */
198 gst_atomic_queue_push (pool->queue, buffer);
199 gst_poll_write_control (pool->poll);
206 GST_WARNING_OBJECT (pool, "no alloc function");
211 GST_WARNING_OBJECT (pool, "alloc function failed");
216 /* must be called with the lock */
218 do_start (GstBufferPool * pool)
220 if (!pool->started) {
221 GstBufferPoolClass *pclass;
223 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
225 GST_LOG_OBJECT (pool, "starting");
226 /* start the pool, subclasses should allocate buffers and put them
228 if (G_LIKELY (pclass->start)) {
229 if (!pclass->start (pool))
232 pool->started = TRUE;
239 default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
241 gst_buffer_unref (buffer);
244 /* must be called with the lock */
246 default_stop (GstBufferPool * pool)
249 GstBufferPoolClass *pclass;
251 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
254 while ((buffer = gst_atomic_queue_pop (pool->queue))) {
255 gst_poll_read_control (pool->poll);
257 if (G_LIKELY (pclass->free_buffer))
258 pclass->free_buffer (pool, buffer);
263 /* must be called with the lock */
265 do_stop (GstBufferPool * pool)
268 GstBufferPoolClass *pclass;
270 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
272 GST_LOG_OBJECT (pool, "stopping");
273 if (G_LIKELY (pclass->stop)) {
274 if (!pclass->stop (pool))
277 pool->started = FALSE;
283 * gst_buffer_pool_set_active:
284 * @pool: a #GstBufferPool
285 * @active: the new active state
287 * Control the active state of @pool. When the pool is active, new calls to
288 * gst_buffer_pool_acquire_buffer() will return with GST_FLOW_WRONG_STATE.
290 * Returns: %FALSE when the pool was not configured or when preallocation of the
294 gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
298 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
300 GST_LOG_OBJECT (pool, "active %d", active);
302 GST_BUFFER_POOL_LOCK (pool);
303 /* just return if we are already in the right state */
304 if (pool->active == active)
307 /* we need to be configured */
308 if (!pool->configured)
312 if (!do_start (pool))
315 /* unset the flushing state now */
316 gst_poll_read_control (pool->poll);
317 g_atomic_int_set (&pool->flushing, FALSE);
319 /* set to flushing first */
320 g_atomic_int_set (&pool->flushing, TRUE);
321 gst_poll_write_control (pool->poll);
323 /* when all buffers are in the pool, free them. Else they will be
324 * freed when they are released */
325 if (g_atomic_int_get (&pool->outstanding) == 0) {
330 pool->active = active;
331 GST_BUFFER_POOL_UNLOCK (pool);
337 GST_DEBUG_OBJECT (pool, "pool was in the right state");
338 GST_BUFFER_POOL_UNLOCK (pool);
343 GST_ERROR_OBJECT (pool, "pool was not configured");
344 GST_BUFFER_POOL_UNLOCK (pool);
349 GST_ERROR_OBJECT (pool, "start failed");
350 GST_BUFFER_POOL_UNLOCK (pool);
355 GST_WARNING_OBJECT (pool, "stop failed");
356 GST_BUFFER_POOL_UNLOCK (pool);
362 default_set_config (GstBufferPool * pool, GstStructure * config)
364 GstBufferPoolPrivate *priv = pool->priv;
366 guint size, min_buffers, max_buffers;
369 /* parse the config and keep around */
370 if (!gst_buffer_pool_config_get (config, &caps, &size, &min_buffers,
371 &max_buffers, &prefix, &align))
374 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
377 priv->min_buffers = min_buffers;
378 priv->max_buffers = max_buffers;
379 priv->prefix = prefix;
386 GST_WARNING_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
392 * gst_buffer_pool_set_config:
393 * @pool: a #GstBufferPool
394 * @config: a #GstStructure
396 * Set the configuration of the pool. The pool must be inactive and all buffers
397 * allocated form this pool must be returned or else this function will do
398 * nothing and return FALSE.
400 * @condfig is a #GstStructure that contains the configuration parameters for
401 * the pool. A default and mandatory set of parameters can be configured with
402 * gst_buffer_pool_config_set(). This function takes ownership of @config.
404 * Returns: TRUE when the configuration could be set.
407 gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
410 GstBufferPoolClass *pclass;
412 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
413 g_return_val_if_fail (config != NULL, FALSE);
415 GST_BUFFER_POOL_LOCK (pool);
416 /* can't change the settings when active */
420 /* we can't change when outstanding buffers */
421 if (g_atomic_int_get (&pool->outstanding) != 0)
422 goto have_outstanding;
424 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
426 /* set the new config */
427 if (G_LIKELY (pclass->set_config))
428 result = pclass->set_config (pool, config);
434 gst_structure_free (pool->config);
435 pool->config = config;
437 /* now we are configured */
438 pool->configured = TRUE;
440 GST_BUFFER_POOL_UNLOCK (pool);
447 GST_WARNING_OBJECT (pool, "can't change config, we are active");
448 GST_BUFFER_POOL_UNLOCK (pool);
453 GST_WARNING_OBJECT (pool, "can't change config, have outstanding buffers");
454 GST_BUFFER_POOL_UNLOCK (pool);
460 * gst_buffer_pool_get_config:
461 * @pool: a #GstBufferPool
463 * Get a copy of the current configuration of the pool. This configuration
464 * can either be modified and used for the gst_buffer_pool_set_config() call
465 * or it must be freed after usage.
467 * Returns: a copy of the current configuration of @pool. use
468 * gst_structure_free() after usage or gst_buffer_pool_set_config().
471 gst_buffer_pool_get_config (GstBufferPool * pool)
473 GstStructure *result;
475 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
477 GST_BUFFER_POOL_UNLOCK (pool);
478 result = gst_structure_copy (pool->config);
479 GST_BUFFER_POOL_UNLOCK (pool);
484 static const gchar *empty_meta[] = { NULL };
487 * gst_buffer_pool_get_metas:
488 * @pool: a #GstBufferPool
490 * Get a NULL terminated array of string with supported #GstMeta apis for
491 * @pool. The requested api would typically be added to the config with
492 * gst_buffer_pool_config_add_meta().
494 * Returns: a NULL terminated array of strings.
497 gst_buffer_pool_get_metas (GstBufferPool * pool)
499 GstBufferPoolClass *pclass;
500 const gchar **result;
502 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
504 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
506 if (G_LIKELY (pclass->get_metas))
507 result = pclass->get_metas (pool);
515 * gst_buffer_pool_config_set:
516 * @config: a #GstBufferPool configuration
517 * @caps: caps for the buffers
518 * @size: the size of each buffer, not including prefix
519 * @min_buffers: the minimum amount of buffers to allocate.
520 * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
521 * @prefix: prefix each buffer with this many bytes
522 * @align: alignment of the buffer data.
524 * Configure @config with the given parameters.
527 gst_buffer_pool_config_set (GstStructure * config, const GstCaps * caps,
528 guint size, guint min_buffers, guint max_buffers, guint prefix, guint align)
530 g_return_if_fail (config != NULL);
532 gst_structure_id_set (config,
533 GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
534 GST_QUARK (SIZE), G_TYPE_UINT, size,
535 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
536 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
537 GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
538 GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
542 * gst_buffer_pool_config_add_meta:
543 * @config: a #GstBufferPool configuration
544 * @api: an API to add
546 * Adds the metadata api to @config. This will instruct the @bufferpool to use
547 * the specified metadata api on the buffers that it allocates.
549 * The supported API by @pool can be retrieved with gst_buffer_pool_get_metas().
552 gst_buffer_pool_config_add_meta (GstStructure * config, const gchar * api)
556 GValue api_value = { 0 };
558 g_return_if_fail (config != NULL);
560 value = gst_structure_id_get_value (config, GST_QUARK (META));
562 array = (GValueArray *) g_value_get_boxed (value);
564 GValue new_array_val = { 0, };
566 array = g_value_array_new (0);
568 g_value_init (&new_array_val, G_TYPE_VALUE_ARRAY);
569 g_value_take_boxed (&new_array_val, array);
571 gst_structure_id_take_value (config, GST_QUARK (META), &new_array_val);
574 g_value_init (&api_value, G_TYPE_STRING);
575 g_value_set_string (&api_value, api);
576 g_value_array_append (array, &api_value);
577 g_value_unset (&api_value);
581 * gst_buffer_pool_config_n_metas:
582 * @config: a #GstBufferPool configuration
584 * Retrieve the number of values currently stored in the
585 * meta API array of the @config structure.
587 * Returns: the metadata API array size as a #guint.
590 gst_buffer_pool_config_n_metas (GstStructure * config)
596 g_return_val_if_fail (config != NULL, 0);
598 value = gst_structure_id_get_value (config, GST_QUARK (META));
600 array = (GValueArray *) g_value_get_boxed (value);
601 size = array->n_values;
607 * gst_buffer_pool_config_get_meta:
608 * @config: a #GstBufferPool configuration
609 * @index: position in the metadata API array to read
611 * Parse an available @config and get the metadata API
612 * at @index of the metadata API array.
614 * Returns: a #gchar of the metadata API at @index.
617 gst_buffer_pool_config_get_meta (GstStructure * config, guint index)
620 const gchar *ret = NULL;
622 g_return_val_if_fail (config != NULL, 0);
624 value = gst_structure_id_get_value (config, GST_QUARK (META));
629 meta = (GValueArray *) g_value_get_boxed (value);
630 api_value = g_value_array_get_nth (meta, index);
633 ret = g_value_get_string (api_value);
639 * gst_buffer_pool_config_has_meta:
640 * @config: a #GstBufferPool configuration
641 * @api: a metadata api
643 * Check if @config contains @api metadata.
645 * Returns: TRUE if the metadata array contain @api.
648 gst_buffer_pool_config_has_meta (GstStructure * config, const gchar * api)
652 g_return_val_if_fail (config != NULL, 0);
654 value = gst_structure_id_get_value (config, GST_QUARK (META));
660 array = (GValueArray *) g_value_get_boxed (value);
661 for (i = 0; i < array->n_values; i++) {
662 api_value = g_value_array_get_nth (array, i);
663 if (!strcmp (api, g_value_get_string (api_value)))
671 * gst_buffer_pool_config_get:
672 * @config: a #GstBufferPool configuration
673 * @caps: the caps of buffers
674 * @size: the size of each buffer, not including prefix
675 * @min_buffers: the minimum amount of buffers to allocate.
676 * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
677 * @prefix: prefix each buffer with this many bytes
678 * @align: alignment of the buffer data.
680 * Get the configuration values from @config.
683 gst_buffer_pool_config_get (GstStructure * config, const GstCaps ** caps,
684 guint * size, guint * min_buffers, guint * max_buffers, guint * prefix,
687 g_return_val_if_fail (config != NULL, FALSE);
689 return gst_structure_id_get (config,
690 GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
691 GST_QUARK (SIZE), G_TYPE_UINT, size,
692 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
693 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
694 GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
695 GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
699 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
700 GstBufferPoolParams * params)
702 GstFlowReturn result;
703 GstBufferPoolClass *pclass;
704 GstBufferPoolPrivate *priv = pool->priv;
706 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
709 if (G_UNLIKELY (g_atomic_int_get (&pool->flushing)))
712 /* try to get a buffer from the queue */
713 *buffer = gst_atomic_queue_pop (pool->queue);
714 if (G_LIKELY (*buffer)) {
715 gst_poll_read_control (pool->poll);
716 result = GST_FLOW_OK;
717 GST_LOG_OBJECT (pool, "acquired buffer %p", *buffer);
722 if (priv->max_buffers == 0) {
723 /* no max_buffers, we allocate some more */
724 if (G_LIKELY (pclass->alloc_buffer)) {
725 result = pclass->alloc_buffer (pool, buffer, params);
727 result = GST_FLOW_NOT_SUPPORTED;
728 GST_LOG_OBJECT (pool, "alloc buffer %p", *buffer);
732 /* check if we need to wait */
733 if (params && (params->flags & GST_BUFFER_POOL_FLAG_DONTWAIT)) {
734 GST_LOG_OBJECT (pool, "no more buffers");
735 result = GST_FLOW_UNEXPECTED;
740 GST_LOG_OBJECT (pool, "waiting for free buffers");
741 gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
749 GST_DEBUG_OBJECT (pool, "we are flushing");
750 return GST_FLOW_WRONG_STATE;
755 dec_outstanding (GstBufferPool * pool)
757 if (g_atomic_int_dec_and_test (&pool->outstanding)) {
758 /* all buffers are returned to the pool, see if we need to free them */
759 if (g_atomic_int_get (&pool->flushing)) {
760 /* take the lock so that set_active is not run concurrently */
761 GST_BUFFER_POOL_LOCK (pool);
762 /* recheck the flushing state in the lock, the pool could have been
763 * set to active again */
764 if (g_atomic_int_get (&pool->flushing))
767 GST_BUFFER_POOL_UNLOCK (pool);
773 * gst_buffer_pool_acquire_buffer:
774 * @pool: a #GstBufferPool
775 * @buffer: a location for a #GstBuffer
776 * @params: parameters.
778 * Acquire a buffer from @pool. @buffer should point to a memory location that
779 * can hold a pointer to the new buffer.
781 * @params can be NULL or contain optional parameters to influence the allocation.
783 * Returns: a #GstFlowReturn such as GST_FLOW_WRONG_STATE when the pool is
787 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
788 GstBufferPoolParams * params)
790 GstBufferPoolClass *pclass;
791 GstFlowReturn result;
793 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), GST_FLOW_ERROR);
794 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
796 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
798 /* assume we'll have one more outstanding buffer we need to do that so
799 * that concurrent set_active doesn't clear the buffers */
800 g_atomic_int_inc (&pool->outstanding);
802 if (G_LIKELY (pclass->acquire_buffer))
803 result = pclass->acquire_buffer (pool, buffer, params);
805 result = GST_FLOW_NOT_SUPPORTED;
807 if (G_LIKELY (result == GST_FLOW_OK)) {
808 /* all buffers from the pool point to the pool and have the refcount of the
809 * pool incremented */
810 (*buffer)->pool = gst_object_ref (pool);
812 dec_outstanding (pool);
819 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
821 /* keep it around in our queue */
822 GST_LOG_OBJECT (pool, "released buffer %p", buffer);
823 gst_atomic_queue_push (pool->queue, buffer);
824 gst_poll_write_control (pool->poll);
828 * gst_buffer_pool_release_buffer:
829 * @pool: a #GstBufferPool
830 * @buffer: a #GstBuffer
832 * Release @buffer to @pool. @buffer should have previously been allocated from
833 * @pool with gst_buffer_pool_acquire_buffer().
835 * This function is usually called automatically when the last ref on @buffer
839 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
841 GstBufferPoolClass *pclass;
843 g_return_if_fail (GST_IS_BUFFER_POOL (pool));
844 g_return_if_fail (buffer != NULL);
846 /* check that the buffer is ours, all buffers returned to the pool have the
847 * pool member set to NULL and the pool refcount decreased */
848 if (!g_atomic_pointer_compare_and_exchange ((gpointer *) & buffer->pool,
852 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
854 if (G_LIKELY (pclass->release_buffer))
855 pclass->release_buffer (pool, buffer);
857 dec_outstanding (pool);
859 /* decrease the refcount that the buffer had to us */
860 gst_object_unref (pool);