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");
152 /* the default implementation for preallocating the buffers
155 default_start (GstBufferPool * pool)
158 GstBufferPoolPrivate *priv = pool->priv;
159 GstBufferPoolClass *pclass;
161 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
163 /* no alloc function, error */
164 if (G_UNLIKELY (pclass->alloc_buffer == NULL))
167 /* we need to prealloc buffers */
168 for (i = 0; i < priv->min_buffers; i++) {
171 if (pclass->alloc_buffer (pool, &buffer, NULL) != GST_FLOW_OK)
174 /* store in the queue */
175 gst_atomic_queue_push (pool->queue, buffer);
176 gst_poll_write_control (pool->poll);
183 GST_WARNING_OBJECT (pool, "no alloc function");
188 GST_WARNING_OBJECT (pool, "alloc function failed");
194 default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
196 gst_buffer_unref (buffer);
200 default_stop (GstBufferPool * pool)
203 GstBufferPoolClass *pclass;
205 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
208 while ((buffer = gst_atomic_queue_pop (pool->queue))) {
209 gst_poll_read_control (pool->poll);
211 if (G_LIKELY (pclass->free_buffer))
212 pclass->free_buffer (pool, buffer);
218 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;
238 do_stop (GstBufferPool * pool)
241 GstBufferPoolClass *pclass;
243 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
245 if (G_LIKELY (pclass->stop)) {
246 if (!pclass->stop (pool))
249 pool->started = FALSE;
255 * gst_buffer_pool_set_active:
256 * @pool: a #GstBufferPool
257 * @active: the new active state
259 * Control the active state of @pool. When the pool is active, new calls to
260 * gst_buffer_pool_acquire_buffer() will return with GST_FLOW_WRONG_STATE.
262 * Returns: %FALSE when the pool was not configured or when preallocation of the
266 gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
268 GstBufferPoolClass *pclass;
271 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
273 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
275 GST_BUFFER_POOL_LOCK (pool);
276 /* just return if we are already in the right state */
277 if (pool->active == active)
280 /* we need to be configured */
281 if (!pool->configured)
285 if (!do_start (pool))
288 /* unset the flushing state now */
289 gst_poll_read_control (pool->poll);
290 g_atomic_int_set (&pool->flushing, FALSE);
292 /* set to flushing first */
293 g_atomic_int_set (&pool->flushing, TRUE);
294 gst_poll_write_control (pool->poll);
296 /* when all buffers are in the pool, free them. Else they will be
297 * freed when they are released */
298 if (g_atomic_int_get (&pool->outstanding) == 0) {
303 pool->active = active;
304 GST_BUFFER_POOL_UNLOCK (pool);
310 GST_DEBUG_OBJECT (pool, "pool was in the right state");
311 GST_BUFFER_POOL_UNLOCK (pool);
316 GST_ERROR_OBJECT (pool, "pool was not configured");
317 GST_BUFFER_POOL_UNLOCK (pool);
322 GST_ERROR_OBJECT (pool, "start failed");
323 GST_BUFFER_POOL_UNLOCK (pool);
328 GST_WARNING_OBJECT (pool, "stop failed");
329 GST_BUFFER_POOL_UNLOCK (pool);
335 default_set_config (GstBufferPool * pool, GstStructure * config)
337 GstBufferPoolPrivate *priv = pool->priv;
339 /* parse the config and keep around */
340 gst_buffer_pool_config_get (config, &priv->size, &priv->min_buffers,
341 &priv->max_buffers, &priv->prefix, &priv->postfix, &priv->align);
347 * gst_buffer_pool_set_config:
348 * @pool: a #GstBufferPool
349 * @config: a #GstStructure
351 * Set the configuration of the pool. The pool must be inactive and all buffers
352 * allocated form this pool must be returned or else this function will do
353 * nothing and return FALSE.
355 * @condfig is a #GstStructure that contains the configuration parameters for
356 * the pool. A default and mandatory set of parameters can be configured with
357 * gst_buffer_pool_config_set(). This function takes ownership of @config.
359 * Returns: TRUE when the configuration could be set.
362 gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
365 GstBufferPoolClass *pclass;
367 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
368 g_return_val_if_fail (config != NULL, FALSE);
370 GST_BUFFER_POOL_LOCK (pool);
371 /* can't change the settings when active */
375 /* we can't change when outstanding buffers */
376 if (g_atomic_int_get (&pool->outstanding) != 0)
377 goto have_outstanding;
379 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
381 /* set the new config */
382 if (G_LIKELY (pclass->set_config))
383 result = pclass->set_config (pool, config);
389 gst_structure_free (pool->config);
390 pool->config = config;
392 /* now we are configured */
393 pool->configured = TRUE;
395 GST_BUFFER_POOL_UNLOCK (pool);
402 GST_WARNING_OBJECT (pool, "can't change config, we are active");
403 GST_BUFFER_POOL_UNLOCK (pool);
408 GST_WARNING_OBJECT (pool, "can't change config, have outstanding buffers");
409 GST_BUFFER_POOL_UNLOCK (pool);
415 * gst_buffer_pool_get_config:
416 * @pool: a #GstBufferPool
418 * Get a copy of the current configuration of the pool. This configuration
419 * can either be modified and used for the gst_buffer_pool_set_config() call
420 * or it must be freed after usage.
422 * Returns: a copy of the current configuration of @pool. use
423 * gst_structure_free() after usage or gst_buffer_pool_set_config().
426 gst_buffer_pool_get_config (GstBufferPool * pool)
428 GstStructure *result;
430 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
432 GST_BUFFER_POOL_UNLOCK (pool);
433 result = gst_structure_copy (pool->config);
434 GST_BUFFER_POOL_UNLOCK (pool);
440 * gst_buffer_pool_config_set:
441 * @pool: a #GstBufferPool
442 * @size: the size of each buffer, not including pre and post fix
443 * @min_buffers: the minimum amount of buffers to allocate.
444 * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
445 * @prefix: prefix each buffer with this many bytes
446 * @postfix: postfix each buffer with this many bytes
447 * @align: alignment of the buffer data.
449 * Configure @config with the given parameters.
452 gst_buffer_pool_config_set (GstStructure * config, guint size,
453 guint min_buffers, guint max_buffers, guint prefix, guint postfix,
456 g_return_if_fail (config != NULL);
458 gst_structure_id_set (config,
459 GST_QUARK (SIZE), G_TYPE_UINT, size,
460 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
461 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
462 GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
463 GST_QUARK (POSTFIX), G_TYPE_UINT, postfix,
464 GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
468 * gst_buffer_pool_config_get:
469 * @pool: a #GstBufferPool
470 * @size: the size of each buffer, not including pre and post fix
471 * @min_buffers: the minimum amount of buffers to allocate.
472 * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
473 * @prefix: prefix each buffer with this many bytes
474 * @postfix: postfix each buffer with this many bytes
475 * @align: alignment of the buffer data.
477 * Get the configuration values from @config.
480 gst_buffer_pool_config_get (GstStructure * config, guint * size,
481 guint * min_buffers, guint * max_buffers, guint * prefix, guint * postfix,
484 g_return_val_if_fail (config != NULL, FALSE);
486 return gst_structure_id_get (config,
487 GST_QUARK (SIZE), G_TYPE_UINT, size,
488 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
489 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
490 GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
491 GST_QUARK (POSTFIX), G_TYPE_UINT, postfix,
492 GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
496 default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
497 GstBufferPoolParams * params)
500 GstBufferPoolPrivate *priv = pool->priv;
502 *buffer = gst_buffer_new ();
504 align = priv->align - 1;
505 size = priv->prefix + priv->postfix + priv->size + align;
509 memptr = g_malloc (size);
510 GST_BUFFER_MALLOCDATA (*buffer) = memptr;
511 memptr = (guint8 *) ((guintptr) (memptr + align) & ~align);
512 GST_BUFFER_DATA (*buffer) = memptr + priv->prefix;
513 GST_BUFFER_SIZE (*buffer) = priv->size;
520 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
521 GstBufferPoolParams * params)
523 GstFlowReturn result;
524 GstBufferPoolClass *pclass;
525 GstBufferPoolPrivate *priv = pool->priv;
527 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
530 if (g_atomic_int_get (&pool->flushing))
531 return GST_FLOW_WRONG_STATE;
533 /* try to get a buffer from the queue */
534 *buffer = gst_atomic_queue_pop (pool->queue);
536 gst_poll_read_control (pool->poll);
537 result = GST_FLOW_OK;
542 if (priv->max_buffers == 0) {
543 /* no max_buffers, we allocate some more */
544 if (G_LIKELY (pclass->alloc_buffer)) {
545 result = pclass->alloc_buffer (pool, buffer, params);
547 result = GST_FLOW_NOT_SUPPORTED;
551 /* check if we need to wait */
552 if (params && !(params->flags & GST_BUFFER_POOL_FLAG_WAIT)) {
553 result = GST_FLOW_UNEXPECTED;
558 gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
565 * gst_buffer_pool_acquire_buffer:
566 * @pool: a #GstBufferPool
567 * @buffer: a location for a #GstBuffer
568 * @params: parameters.
570 * Acquire a buffer from @pool. @buffer should point to a memory location that
571 * can hold a pointer to the new buffer.
573 * @params can be NULL or contain optional parameters to influence the allocation.
575 * Returns: a #GstFlowReturn such as GST_FLOW_WRONG_STATE when the pool is
579 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
580 GstBufferPoolParams * params)
582 GstBufferPoolClass *pclass;
583 GstFlowReturn result;
585 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), GST_FLOW_ERROR);
586 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
588 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
590 if (G_LIKELY (pclass->acquire_buffer))
591 result = pclass->acquire_buffer (pool, buffer, params);
593 result = GST_FLOW_NOT_SUPPORTED;
595 if (G_LIKELY (result == GST_FLOW_OK && *buffer))
596 g_atomic_int_inc (&pool->outstanding);
602 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
604 /* keep it around in our queue */
605 gst_atomic_queue_push (pool->queue, buffer);
606 gst_poll_write_control (pool->poll);
610 * gst_buffer_pool_release_buffer:
611 * @pool: a #GstBufferPool
612 * @buffer: a #GstBuffer
614 * Release @buffer to @pool. @buffer should have previously been allocated from
615 * @pool with gst_buffer_pool_acquire_buffer().
617 * This function is usually called automatically when the last ref on @buffer
621 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
623 GstBufferPoolClass *pclass;
625 g_return_if_fail (GST_IS_BUFFER_POOL (pool));
626 g_return_if_fail (buffer != NULL);
628 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
630 if (G_LIKELY (pclass->release_buffer))
631 pclass->release_buffer (pool, buffer);
633 if (G_UNLIKELY (g_atomic_int_get (&pool->flushing))) {
634 if (g_atomic_int_dec_and_test (&pool->outstanding)) {
635 /* take the lock so that set_active is not run concurrently */
636 GST_BUFFER_POOL_LOCK (pool);
637 /* recheck the flushing state in the lock, the pool could have been
638 * set to active again */
639 if (g_atomic_int_get (&pool->flushing)) {
643 GST_BUFFER_POOL_UNLOCK (pool);
650 GST_WARNING_OBJECT (pool, "stop failed");
651 GST_BUFFER_POOL_UNLOCK (pool);