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;
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_free_buffer (GstBufferPool * pool, GstBuffer * buffer);
81 static void default_release_buffer (GstBufferPool * pool, GstBuffer * buffer);
84 gst_buffer_pool_class_init (GstBufferPoolClass * klass)
86 GObjectClass *gobject_class = (GObjectClass *) klass;
88 g_type_class_add_private (klass, sizeof (GstBufferPoolPrivate));
90 gobject_class->finalize = gst_buffer_pool_finalize;
92 klass->start = default_start;
93 klass->stop = default_stop;
94 klass->set_config = default_set_config;
95 klass->acquire_buffer = default_acquire_buffer;
96 klass->alloc_buffer = default_alloc_buffer;
97 klass->release_buffer = default_release_buffer;
98 klass->free_buffer = default_free_buffer;
100 GST_DEBUG_CATEGORY_INIT (gst_buffer_pool_debug, "bufferpool", 0,
105 gst_buffer_pool_init (GstBufferPool * pool)
107 pool->priv = GST_BUFFER_POOL_GET_PRIVATE (pool);
109 g_static_rec_mutex_init (&pool->priv->rec_lock);
111 pool->poll = gst_poll_new_timer ();
112 pool->queue = gst_atomic_queue_new (10);
113 pool->flushing = TRUE;
114 pool->active = FALSE;
115 pool->configured = FALSE;
116 pool->started = FALSE;
117 pool->config = gst_structure_id_empty_new (GST_QUARK (BUFFER_POOL_CONFIG));
118 gst_buffer_pool_config_set (pool->config, NULL, 0, 0, 0, 0, 0, 1);
120 GST_DEBUG_OBJECT (pool, "created");
124 gst_buffer_pool_finalize (GObject * object)
128 pool = GST_BUFFER_POOL_CAST (object);
130 GST_DEBUG_OBJECT (pool, "finalize");
132 gst_buffer_pool_set_active (pool, FALSE);
133 gst_atomic_queue_unref (pool->queue);
134 gst_poll_free (pool->poll);
135 gst_structure_free (pool->config);
136 g_static_rec_mutex_free (&pool->priv->rec_lock);
138 G_OBJECT_CLASS (gst_buffer_pool_parent_class)->finalize (object);
142 * gst_buffer_pool_new:
144 * Creates a new #GstBufferPool instance.
146 * Returns: a new #GstBufferPool instance
149 gst_buffer_pool_new (void)
151 GstBufferPool *result;
153 result = g_object_newv (GST_TYPE_BUFFER_POOL, 0, NULL);
154 GST_DEBUG_OBJECT (result, "created new buffer pool");
160 default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
161 GstBufferPoolParams * params)
164 GstBufferPoolPrivate *priv = pool->priv;
166 *buffer = gst_buffer_new ();
168 align = priv->align - 1;
170 gst_buffer_take_memory (*buffer, gst_memory_new_alloc (priv->size, align));
175 /* the default implementation for preallocating the buffers
178 default_start (GstBufferPool * pool)
181 GstBufferPoolPrivate *priv = pool->priv;
182 GstBufferPoolClass *pclass;
184 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
186 /* no alloc function, error */
187 if (G_UNLIKELY (pclass->alloc_buffer == NULL))
190 /* we need to prealloc buffers */
191 for (i = 0; i < priv->min_buffers; i++) {
194 if (pclass->alloc_buffer (pool, &buffer, NULL) != GST_FLOW_OK)
197 GST_LOG_OBJECT (pool, "prealloced buffer %d: %p", i, buffer);
198 /* store in the queue */
199 gst_atomic_queue_push (pool->queue, buffer);
200 gst_poll_write_control (pool->poll);
207 GST_WARNING_OBJECT (pool, "no alloc function");
212 GST_WARNING_OBJECT (pool, "alloc function failed");
217 /* must be called with the lock */
219 do_start (GstBufferPool * pool)
221 if (!pool->started) {
222 GstBufferPoolClass *pclass;
224 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
226 GST_LOG_OBJECT (pool, "starting");
227 /* start the pool, subclasses should allocate buffers and put them
229 if (G_LIKELY (pclass->start)) {
230 if (!pclass->start (pool))
233 pool->started = TRUE;
240 default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
242 gst_buffer_unref (buffer);
245 /* must be called with the lock */
247 default_stop (GstBufferPool * pool)
250 GstBufferPoolClass *pclass;
252 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
255 while ((buffer = gst_atomic_queue_pop (pool->queue))) {
256 gst_poll_read_control (pool->poll);
258 if (G_LIKELY (pclass->free_buffer))
259 pclass->free_buffer (pool, buffer);
264 /* must be called with the lock */
266 do_stop (GstBufferPool * pool)
269 GstBufferPoolClass *pclass;
271 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
273 GST_LOG_OBJECT (pool, "stopping");
274 if (G_LIKELY (pclass->stop)) {
275 if (!pclass->stop (pool))
278 pool->started = FALSE;
284 * gst_buffer_pool_set_active:
285 * @pool: a #GstBufferPool
286 * @active: the new active state
288 * Control the active state of @pool. When the pool is active, new calls to
289 * gst_buffer_pool_acquire_buffer() will return with GST_FLOW_WRONG_STATE.
291 * Returns: %FALSE when the pool was not configured or when preallocation of the
295 gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
297 GstBufferPoolClass *pclass;
300 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
302 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
304 GST_LOG_OBJECT (pool, "active %d", active);
306 GST_BUFFER_POOL_LOCK (pool);
307 /* just return if we are already in the right state */
308 if (pool->active == active)
311 /* we need to be configured */
312 if (!pool->configured)
316 if (!do_start (pool))
319 /* unset the flushing state now */
320 gst_poll_read_control (pool->poll);
321 g_atomic_int_set (&pool->flushing, FALSE);
323 /* set to flushing first */
324 g_atomic_int_set (&pool->flushing, TRUE);
325 gst_poll_write_control (pool->poll);
327 /* when all buffers are in the pool, free them. Else they will be
328 * freed when they are released */
329 if (g_atomic_int_get (&pool->outstanding) == 0) {
334 pool->active = active;
335 GST_BUFFER_POOL_UNLOCK (pool);
341 GST_DEBUG_OBJECT (pool, "pool was in the right state");
342 GST_BUFFER_POOL_UNLOCK (pool);
347 GST_ERROR_OBJECT (pool, "pool was not configured");
348 GST_BUFFER_POOL_UNLOCK (pool);
353 GST_ERROR_OBJECT (pool, "start failed");
354 GST_BUFFER_POOL_UNLOCK (pool);
359 GST_WARNING_OBJECT (pool, "stop failed");
360 GST_BUFFER_POOL_UNLOCK (pool);
366 default_set_config (GstBufferPool * pool, GstStructure * config)
368 GstBufferPoolPrivate *priv = pool->priv;
370 guint size, min_buffers, max_buffers;
371 guint prefix, postfix, align;
373 /* parse the config and keep around */
374 if (!gst_buffer_pool_config_get (config, &caps, &size, &min_buffers,
375 &max_buffers, &prefix, &postfix, &align))
379 priv->min_buffers = min_buffers;
380 priv->max_buffers = max_buffers;
381 priv->prefix = prefix;
382 priv->postfix = postfix;
389 GST_WARNING_OBJECT (pool, "invalid config");
395 * gst_buffer_pool_set_config:
396 * @pool: a #GstBufferPool
397 * @config: a #GstStructure
399 * Set the configuration of the pool. The pool must be inactive and all buffers
400 * allocated form this pool must be returned or else this function will do
401 * nothing and return FALSE.
403 * @condfig is a #GstStructure that contains the configuration parameters for
404 * the pool. A default and mandatory set of parameters can be configured with
405 * gst_buffer_pool_config_set(). This function takes ownership of @config.
407 * Returns: TRUE when the configuration could be set.
410 gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
413 GstBufferPoolClass *pclass;
415 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
416 g_return_val_if_fail (config != NULL, FALSE);
418 GST_BUFFER_POOL_LOCK (pool);
419 /* can't change the settings when active */
423 /* we can't change when outstanding buffers */
424 if (g_atomic_int_get (&pool->outstanding) != 0)
425 goto have_outstanding;
427 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
429 /* set the new config */
430 if (G_LIKELY (pclass->set_config))
431 result = pclass->set_config (pool, config);
437 gst_structure_free (pool->config);
438 pool->config = config;
440 /* now we are configured */
441 pool->configured = TRUE;
443 GST_BUFFER_POOL_UNLOCK (pool);
450 GST_WARNING_OBJECT (pool, "can't change config, we are active");
451 GST_BUFFER_POOL_UNLOCK (pool);
456 GST_WARNING_OBJECT (pool, "can't change config, have outstanding buffers");
457 GST_BUFFER_POOL_UNLOCK (pool);
463 * gst_buffer_pool_get_config:
464 * @pool: a #GstBufferPool
466 * Get a copy of the current configuration of the pool. This configuration
467 * can either be modified and used for the gst_buffer_pool_set_config() call
468 * or it must be freed after usage.
470 * Returns: a copy of the current configuration of @pool. use
471 * gst_structure_free() after usage or gst_buffer_pool_set_config().
474 gst_buffer_pool_get_config (GstBufferPool * pool)
476 GstStructure *result;
478 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
480 GST_BUFFER_POOL_UNLOCK (pool);
481 result = gst_structure_copy (pool->config);
482 GST_BUFFER_POOL_UNLOCK (pool);
488 * gst_buffer_pool_config_set:
489 * @pool: a #GstBufferPool
490 * @size: the size of each buffer, not including pre and post fix
491 * @min_buffers: the minimum amount of buffers to allocate.
492 * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
493 * @prefix: prefix each buffer with this many bytes
494 * @postfix: postfix each buffer with this many bytes
495 * @align: alignment of the buffer data.
497 * Configure @config with the given parameters.
500 gst_buffer_pool_config_set (GstStructure * config, const GstCaps * caps,
501 guint size, guint min_buffers, guint max_buffers, guint prefix,
502 guint postfix, guint align)
504 g_return_if_fail (config != NULL);
506 gst_structure_id_set (config,
507 GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
508 GST_QUARK (SIZE), G_TYPE_UINT, size,
509 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
510 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
511 GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
512 GST_QUARK (POSTFIX), G_TYPE_UINT, postfix,
513 GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
517 * gst_buffer_pool_config_get:
518 * @pool: a #GstBufferPool
519 * @size: the size of each buffer, not including pre and post fix
520 * @min_buffers: the minimum amount of buffers to allocate.
521 * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
522 * @prefix: prefix each buffer with this many bytes
523 * @postfix: postfix each buffer with this many bytes
524 * @align: alignment of the buffer data.
526 * Get the configuration values from @config.
529 gst_buffer_pool_config_get (GstStructure * config, const GstCaps ** caps,
530 guint * size, guint * min_buffers, guint * max_buffers, guint * prefix,
531 guint * postfix, guint * align)
533 g_return_val_if_fail (config != NULL, FALSE);
535 return gst_structure_id_get (config,
536 GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
537 GST_QUARK (SIZE), G_TYPE_UINT, size,
538 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
539 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
540 GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
541 GST_QUARK (POSTFIX), G_TYPE_UINT, postfix,
542 GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
546 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
547 GstBufferPoolParams * params)
549 GstFlowReturn result;
550 GstBufferPoolClass *pclass;
551 GstBufferPoolPrivate *priv = pool->priv;
553 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
556 if (G_UNLIKELY (g_atomic_int_get (&pool->flushing)))
559 /* try to get a buffer from the queue */
560 *buffer = gst_atomic_queue_pop (pool->queue);
561 if (G_LIKELY (*buffer)) {
562 gst_poll_read_control (pool->poll);
563 result = GST_FLOW_OK;
564 GST_LOG_OBJECT (pool, "acquired buffer %p", *buffer);
569 if (priv->max_buffers == 0) {
570 /* no max_buffers, we allocate some more */
571 if (G_LIKELY (pclass->alloc_buffer)) {
572 result = pclass->alloc_buffer (pool, buffer, params);
574 result = GST_FLOW_NOT_SUPPORTED;
575 GST_LOG_OBJECT (pool, "alloc buffer %p", *buffer);
579 /* check if we need to wait */
580 if (params && !(params->flags & GST_BUFFER_POOL_FLAG_WAIT)) {
581 GST_LOG_OBJECT (pool, "no more buffers");
582 result = GST_FLOW_UNEXPECTED;
587 GST_LOG_OBJECT (pool, "waiting for free buffers");
588 gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
596 GST_DEBUG_OBJECT (pool, "we are flushing");
597 return GST_FLOW_WRONG_STATE;
602 dec_outstanding (GstBufferPool * pool)
604 if (g_atomic_int_dec_and_test (&pool->outstanding)) {
605 /* all buffers are returned to the pool, see if we need to free them */
606 if (g_atomic_int_get (&pool->flushing)) {
607 /* take the lock so that set_active is not run concurrently */
608 GST_BUFFER_POOL_LOCK (pool);
609 /* recheck the flushing state in the lock, the pool could have been
610 * set to active again */
611 if (g_atomic_int_get (&pool->flushing))
614 GST_BUFFER_POOL_UNLOCK (pool);
620 * gst_buffer_pool_acquire_buffer:
621 * @pool: a #GstBufferPool
622 * @buffer: a location for a #GstBuffer
623 * @params: parameters.
625 * Acquire a buffer from @pool. @buffer should point to a memory location that
626 * can hold a pointer to the new buffer.
628 * @params can be NULL or contain optional parameters to influence the allocation.
630 * Returns: a #GstFlowReturn such as GST_FLOW_WRONG_STATE when the pool is
634 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
635 GstBufferPoolParams * params)
637 GstBufferPoolClass *pclass;
638 GstFlowReturn result;
640 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), GST_FLOW_ERROR);
641 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
643 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
645 /* assume we'll have one more outstanding buffer we need to do that so
646 * that concurrent set_active doesn't clear the buffers */
647 g_atomic_int_inc (&pool->outstanding);
649 if (G_LIKELY (pclass->acquire_buffer))
650 result = pclass->acquire_buffer (pool, buffer, params);
652 result = GST_FLOW_NOT_SUPPORTED;
654 if (G_LIKELY (result == GST_FLOW_OK)) {
655 /* all buffers from the pool point to the pool and have the refcount of the
656 * pool incremented */
657 (*buffer)->pool = gst_object_ref (pool);
659 dec_outstanding (pool);
666 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
668 /* keep it around in our queue */
669 GST_LOG_OBJECT (pool, "released buffer %p", buffer);
670 gst_atomic_queue_push (pool->queue, buffer);
671 gst_poll_write_control (pool->poll);
675 * gst_buffer_pool_release_buffer:
676 * @pool: a #GstBufferPool
677 * @buffer: a #GstBuffer
679 * Release @buffer to @pool. @buffer should have previously been allocated from
680 * @pool with gst_buffer_pool_acquire_buffer().
682 * This function is usually called automatically when the last ref on @buffer
686 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
688 GstBufferPoolClass *pclass;
690 g_return_if_fail (GST_IS_BUFFER_POOL (pool));
691 g_return_if_fail (buffer != NULL);
693 /* check that the buffer is ours, all buffers returned to the pool have the
694 * pool member set to NULL and the pool refcount decreased */
695 if (!g_atomic_pointer_compare_and_exchange ((gpointer *) & buffer->pool,
699 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
701 if (G_LIKELY (pclass->release_buffer))
702 pclass->release_buffer (pool, buffer);
704 dec_outstanding (pool);
706 /* decrease the refcount that the buffer had to us */
707 gst_object_unref (pool);