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"
43 #define GST_BUFFER_POOL_GET_PRIVATE(obj) \
44 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BUFFER_POOL, GstBufferPoolPrivate))
46 #define GST_BUFFER_POOL_LOCK(pool) (g_static_rec_mutex_lock(&pool->priv->rec_lock))
47 #define GST_BUFFER_POOL_UNLOCK(pool) (g_static_rec_mutex_unlock(&pool->priv->rec_lock))
49 struct _GstBufferPoolPrivate
51 GStaticRecMutex rec_lock;
66 static void gst_buffer_pool_finalize (GObject * object);
68 G_DEFINE_TYPE (GstBufferPool, gst_buffer_pool, GST_TYPE_OBJECT);
70 static gboolean default_start (GstBufferPool * pool);
71 static gboolean default_stop (GstBufferPool * pool);
72 static gboolean default_set_config (GstBufferPool * pool,
73 GstStructure * config);
74 static GstFlowReturn default_alloc_buffer (GstBufferPool * pool,
75 GstBuffer ** buffer, GstBufferPoolParams * params);
76 static GstFlowReturn default_acquire_buffer (GstBufferPool * pool,
77 GstBuffer ** buffer, GstBufferPoolParams * params);
78 static void default_free_buffer (GstBufferPool * pool, GstBuffer * buffer);
79 static void default_release_buffer (GstBufferPool * pool, GstBuffer * buffer);
82 gst_buffer_pool_class_init (GstBufferPoolClass * klass)
84 GObjectClass *gobject_class = (GObjectClass *) klass;
86 gobject_class->finalize = gst_buffer_pool_finalize;
88 klass->start = default_start;
89 klass->stop = default_stop;
90 klass->set_config = default_set_config;
91 klass->acquire_buffer = default_acquire_buffer;
92 klass->alloc_buffer = default_alloc_buffer;
93 klass->release_buffer = default_release_buffer;
94 klass->free_buffer = default_free_buffer;
98 gst_buffer_pool_init (GstBufferPool * pool)
100 pool->priv = GST_BUFFER_POOL_GET_PRIVATE (pool);
102 g_static_rec_mutex_init (&pool->priv->rec_lock);
104 pool->poll = gst_poll_new_timer ();
105 pool->queue = gst_atomic_queue_new (10);
106 pool->flushing = TRUE;
107 pool->active = FALSE;
108 pool->configured = FALSE;
109 pool->started = FALSE;
110 pool->config = gst_structure_id_empty_new (GST_QUARK (BUFFER_POOL_CONFIG));
111 gst_buffer_pool_config_set (pool->config, 0, 0, 0, 0, 0, 1);
113 GST_DEBUG_OBJECT (pool, "created");
117 gst_buffer_pool_finalize (GObject * object)
121 pool = GST_BUFFER_POOL_CAST (object);
123 GST_DEBUG_OBJECT (pool, "finalize");
125 gst_buffer_pool_set_active (pool, FALSE);
126 gst_atomic_queue_unref (pool->queue);
127 gst_poll_free (pool->poll);
128 gst_structure_free (pool->config);
129 g_static_rec_mutex_free (&pool->priv->rec_lock);
131 G_OBJECT_CLASS (gst_buffer_pool_parent_class)->finalize (object);
135 * gst_buffer_pool_new:
137 * Creates a new #GstBufferPool instance.
139 * Returns: a new #GstBufferPool instance
142 gst_buffer_pool_new (void)
144 GstBufferPool *result;
146 result = g_object_newv (GST_TYPE_BUFFER_POOL, 0, NULL);
147 GST_DEBUG_OBJECT (result, "created new buffer pool");
153 default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
154 GstBufferPoolParams * params)
157 GstBufferPoolPrivate *priv = pool->priv;
159 *buffer = gst_buffer_new ();
161 align = priv->align - 1;
162 size = priv->prefix + priv->postfix + priv->size + align;
166 memptr = g_malloc (size);
167 GST_BUFFER_MALLOCDATA (*buffer) = memptr;
168 memptr = (guint8 *) ((guintptr) (memptr + align) & ~align);
169 GST_BUFFER_DATA (*buffer) = memptr + priv->prefix;
170 GST_BUFFER_SIZE (*buffer) = priv->size;
176 /* the default implementation for preallocating the buffers
179 default_start (GstBufferPool * pool)
182 GstBufferPoolPrivate *priv = pool->priv;
183 GstBufferPoolClass *pclass;
185 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
187 /* no alloc function, error */
188 if (G_UNLIKELY (pclass->alloc_buffer == NULL))
191 /* we need to prealloc buffers */
192 for (i = 0; i < priv->min_buffers; i++) {
195 if (pclass->alloc_buffer (pool, &buffer, NULL) != GST_FLOW_OK)
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 /* 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 if (G_LIKELY (pclass->stop)) {
273 if (!pclass->stop (pool))
276 pool->started = FALSE;
282 * gst_buffer_pool_set_active:
283 * @pool: a #GstBufferPool
284 * @active: the new active state
286 * Control the active state of @pool. When the pool is active, new calls to
287 * gst_buffer_pool_acquire_buffer() will return with GST_FLOW_WRONG_STATE.
289 * Returns: %FALSE when the pool was not configured or when preallocation of the
293 gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
295 GstBufferPoolClass *pclass;
298 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
300 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
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;
365 guint size, min_buffers, max_buffers;
366 guint prefix, postfix, align;
368 /* parse the config and keep around */
369 if (!gst_buffer_pool_config_get (config, &size, &min_buffers,
370 &max_buffers, &prefix, &postfix, &align))
374 priv->min_buffers = min_buffers;
375 priv->max_buffers = max_buffers;
376 priv->prefix = prefix;
377 priv->postfix = postfix;
384 GST_WARNING_OBJECT (pool, "invalid config");
390 * gst_buffer_pool_set_config:
391 * @pool: a #GstBufferPool
392 * @config: a #GstStructure
394 * Set the configuration of the pool. The pool must be inactive and all buffers
395 * allocated form this pool must be returned or else this function will do
396 * nothing and return FALSE.
398 * @condfig is a #GstStructure that contains the configuration parameters for
399 * the pool. A default and mandatory set of parameters can be configured with
400 * gst_buffer_pool_config_set(). This function takes ownership of @config.
402 * Returns: TRUE when the configuration could be set.
405 gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
408 GstBufferPoolClass *pclass;
410 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
411 g_return_val_if_fail (config != NULL, FALSE);
413 GST_BUFFER_POOL_LOCK (pool);
414 /* can't change the settings when active */
418 /* we can't change when outstanding buffers */
419 if (g_atomic_int_get (&pool->outstanding) != 0)
420 goto have_outstanding;
422 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
424 /* set the new config */
425 if (G_LIKELY (pclass->set_config))
426 result = pclass->set_config (pool, config);
432 gst_structure_free (pool->config);
433 pool->config = config;
435 /* now we are configured */
436 pool->configured = TRUE;
438 GST_BUFFER_POOL_UNLOCK (pool);
445 GST_WARNING_OBJECT (pool, "can't change config, we are active");
446 GST_BUFFER_POOL_UNLOCK (pool);
451 GST_WARNING_OBJECT (pool, "can't change config, have outstanding buffers");
452 GST_BUFFER_POOL_UNLOCK (pool);
458 * gst_buffer_pool_get_config:
459 * @pool: a #GstBufferPool
461 * Get a copy of the current configuration of the pool. This configuration
462 * can either be modified and used for the gst_buffer_pool_set_config() call
463 * or it must be freed after usage.
465 * Returns: a copy of the current configuration of @pool. use
466 * gst_structure_free() after usage or gst_buffer_pool_set_config().
469 gst_buffer_pool_get_config (GstBufferPool * pool)
471 GstStructure *result;
473 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
475 GST_BUFFER_POOL_UNLOCK (pool);
476 result = gst_structure_copy (pool->config);
477 GST_BUFFER_POOL_UNLOCK (pool);
483 * gst_buffer_pool_config_set:
484 * @pool: a #GstBufferPool
485 * @size: the size of each buffer, not including pre and post fix
486 * @min_buffers: the minimum amount of buffers to allocate.
487 * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
488 * @prefix: prefix each buffer with this many bytes
489 * @postfix: postfix each buffer with this many bytes
490 * @align: alignment of the buffer data.
492 * Configure @config with the given parameters.
495 gst_buffer_pool_config_set (GstStructure * config, guint size,
496 guint min_buffers, guint max_buffers, guint prefix, guint postfix,
499 g_return_if_fail (config != NULL);
501 gst_structure_id_set (config,
502 GST_QUARK (SIZE), G_TYPE_UINT, size,
503 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
504 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
505 GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
506 GST_QUARK (POSTFIX), G_TYPE_UINT, postfix,
507 GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
511 * gst_buffer_pool_config_get:
512 * @pool: a #GstBufferPool
513 * @size: the size of each buffer, not including pre and post fix
514 * @min_buffers: the minimum amount of buffers to allocate.
515 * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
516 * @prefix: prefix each buffer with this many bytes
517 * @postfix: postfix each buffer with this many bytes
518 * @align: alignment of the buffer data.
520 * Get the configuration values from @config.
523 gst_buffer_pool_config_get (GstStructure * config, guint * size,
524 guint * min_buffers, guint * max_buffers, guint * prefix, guint * postfix,
527 g_return_val_if_fail (config != NULL, FALSE);
529 return gst_structure_id_get (config,
530 GST_QUARK (SIZE), G_TYPE_UINT, size,
531 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
532 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
533 GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
534 GST_QUARK (POSTFIX), G_TYPE_UINT, postfix,
535 GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
539 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
540 GstBufferPoolParams * params)
542 GstFlowReturn result;
543 GstBufferPoolClass *pclass;
544 GstBufferPoolPrivate *priv = pool->priv;
546 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
549 if (G_UNLIKELY (g_atomic_int_get (&pool->flushing)))
550 return GST_FLOW_WRONG_STATE;
552 /* try to get a buffer from the queue */
553 *buffer = gst_atomic_queue_pop (pool->queue);
554 if (G_LIKELY (*buffer)) {
555 gst_poll_read_control (pool->poll);
556 result = GST_FLOW_OK;
561 if (priv->max_buffers == 0) {
562 /* no max_buffers, we allocate some more */
563 if (G_LIKELY (pclass->alloc_buffer)) {
564 result = pclass->alloc_buffer (pool, buffer, params);
566 result = GST_FLOW_NOT_SUPPORTED;
570 /* check if we need to wait */
571 if (params && !(params->flags & GST_BUFFER_POOL_FLAG_WAIT)) {
572 result = GST_FLOW_UNEXPECTED;
577 gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
584 dec_outstanding (GstBufferPool * pool)
586 if (g_atomic_int_dec_and_test (&pool->outstanding)) {
587 /* all buffers are returned to the pool, see if we need to free them */
588 if (g_atomic_int_get (&pool->flushing)) {
589 /* take the lock so that set_active is not run concurrently */
590 GST_BUFFER_POOL_LOCK (pool);
591 /* recheck the flushing state in the lock, the pool could have been
592 * set to active again */
593 if (g_atomic_int_get (&pool->flushing))
596 GST_BUFFER_POOL_UNLOCK (pool);
602 * gst_buffer_pool_acquire_buffer:
603 * @pool: a #GstBufferPool
604 * @buffer: a location for a #GstBuffer
605 * @params: parameters.
607 * Acquire a buffer from @pool. @buffer should point to a memory location that
608 * can hold a pointer to the new buffer.
610 * @params can be NULL or contain optional parameters to influence the allocation.
612 * Returns: a #GstFlowReturn such as GST_FLOW_WRONG_STATE when the pool is
616 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
617 GstBufferPoolParams * params)
619 GstBufferPoolClass *pclass;
620 GstFlowReturn result;
622 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), GST_FLOW_ERROR);
623 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
625 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
627 /* assume we'll have one more outstanding buffer we need to do that so
628 * that concurrent set_active doesn't clear the buffers */
629 g_atomic_int_inc (&pool->outstanding);
631 if (G_LIKELY (pclass->acquire_buffer))
632 result = pclass->acquire_buffer (pool, buffer, params);
634 result = GST_FLOW_NOT_SUPPORTED;
636 if (G_UNLIKELY (result != GST_FLOW_OK))
637 dec_outstanding (pool);
643 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
645 /* keep it around in our queue */
646 gst_atomic_queue_push (pool->queue, buffer);
647 gst_poll_write_control (pool->poll);
651 * gst_buffer_pool_release_buffer:
652 * @pool: a #GstBufferPool
653 * @buffer: a #GstBuffer
655 * Release @buffer to @pool. @buffer should have previously been allocated from
656 * @pool with gst_buffer_pool_acquire_buffer().
658 * This function is usually called automatically when the last ref on @buffer
662 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
664 GstBufferPoolClass *pclass;
666 g_return_if_fail (GST_IS_BUFFER_POOL (pool));
667 g_return_if_fail (buffer != NULL);
669 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
671 if (G_LIKELY (pclass->release_buffer))
672 pclass->release_buffer (pool, buffer);
674 dec_outstanding (pool);