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 struct _GstBufferPoolPrivate
62 static void gst_buffer_pool_finalize (GObject * object);
64 G_DEFINE_TYPE (GstBufferPool, gst_buffer_pool, GST_TYPE_OBJECT);
66 static void default_set_active (GstBufferPool * pool, gboolean active);
67 static gboolean default_set_config (GstBufferPool * pool,
68 GstStructure * config);
69 static GstFlowReturn default_alloc_buffer (GstBufferPool * pool,
70 GstBuffer ** buffer, GstBufferPoolParams * params);
71 static GstFlowReturn default_acquire_buffer (GstBufferPool * pool,
72 GstBuffer ** buffer, GstBufferPoolParams * params);
73 static void default_free_buffer (GstBufferPool * pool, GstBuffer * buffer);
74 static void default_release_buffer (GstBufferPool * pool, GstBuffer * buffer);
77 gst_buffer_pool_class_init (GstBufferPoolClass * klass)
79 GObjectClass *gobject_class = (GObjectClass *) klass;
81 gobject_class->finalize = gst_buffer_pool_finalize;
83 klass->set_active = default_set_active;
84 klass->set_config = default_set_config;
85 klass->acquire_buffer = default_acquire_buffer;
86 klass->alloc_buffer = default_alloc_buffer;
87 klass->release_buffer = default_release_buffer;
88 klass->free_buffer = default_free_buffer;
92 gst_buffer_pool_init (GstBufferPool * pool)
94 pool->priv = GST_BUFFER_POOL_GET_PRIVATE (pool);
96 pool->config = gst_structure_id_new (GST_QUARK (BUFFER_POOL_CONFIG),
97 GST_QUARK (SIZE), G_TYPE_UINT, 0,
98 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, 0,
99 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, 0,
100 GST_QUARK (PREFIX), G_TYPE_UINT, 0,
101 GST_QUARK (POSTFIX), G_TYPE_UINT, 0,
102 GST_QUARK (ALIGN), G_TYPE_UINT, 1, NULL);
103 pool->poll = gst_poll_new_timer ();
104 pool->queue = gst_atomic_queue_new (10);
105 default_set_active (pool, FALSE);
107 GST_DEBUG_OBJECT (pool, "created");
111 gst_buffer_pool_finalize (GObject * object)
113 G_OBJECT_CLASS (gst_buffer_pool_parent_class)->finalize (object);
117 * gst_buffer_pool_new:
119 * Creates a new #GstBufferPool instance.
121 * Returns: a new #GstBufferPool instance
124 gst_buffer_pool_new (void)
126 GstBufferPool *result;
128 result = g_object_newv (GST_TYPE_BUFFER_POOL, 0, NULL);
129 GST_DEBUG_OBJECT (result, "created new buffer pool");
135 flush_buffers (GstBufferPool * pool)
138 GstBufferPoolClass *pclass;
140 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
142 while ((buffer = gst_atomic_queue_pop (pool->queue))) {
143 gst_poll_read_control (pool->poll);
144 if (G_LIKELY (pclass->free_buffer))
145 pclass->free_buffer (pool, buffer);
149 /* the default implementation for allocating and freeing the
150 * buffers when changing the active state */
152 default_set_active (GstBufferPool * pool, gboolean active)
155 GstBufferPoolPrivate *priv = pool->priv;
159 flush_buffers (pool);
161 GstBufferPoolClass *pclass;
163 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
165 if (G_LIKELY (pclass->alloc_buffer)) {
166 /* we need to prealloc buffers */
167 for (i = priv->min_buffers; i > 0; i--) {
170 if (pclass->alloc_buffer (pool, &buffer, NULL) == GST_FLOW_OK) {
171 /* store in the queue */
172 gst_buffer_pool_release_buffer (pool, buffer);
180 * gst_buffer_pool_set_active:
181 * @pool: a #GstBufferPool
182 * @active: the new active state
184 * Control the active state of @pool. When the pool is active, new calls to
185 * gst_buffer_pool_acquire_buffer() will return with GST_FLOW_WRONG_STATE.
188 gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
190 GstBufferPoolClass *pclass;
192 g_return_if_fail (GST_IS_BUFFER_POOL (pool));
194 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
196 if (!g_atomic_int_compare_and_exchange (&pool->active, !active, active))
200 gst_poll_write_control (pool->poll);
202 if (G_LIKELY (pclass->set_active))
203 pclass->set_active (pool, active);
206 gst_poll_read_control (pool->poll);
210 default_set_config (GstBufferPool * pool, GstStructure * config)
212 GstBufferPoolPrivate *priv = pool->priv;
214 /* parse the config and keep around */
215 gst_buffer_pool_config_get (config, &priv->size, &priv->min_buffers,
216 &priv->max_buffers, &priv->prefix, &priv->postfix, &priv->align);
222 * gst_buffer_pool_set_config:
223 * @pool: a #GstBufferPool
224 * @config: a #GstStructure
226 * Set the configuration of the pool. The pool must be inactive or else this
227 * function will do nothing and return FALSE.
229 * @condfig is a #GstStructure that contains the configuration parameters for
230 * the pool. A default and mandatory set of parameters can be configured with
231 * gst_buffer_pool_config_set().
233 * Returns: TRUE when the configuration could be set.
236 gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
239 GstBufferPoolClass *pclass;
241 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
242 g_return_val_if_fail (config != NULL, FALSE);
244 if (g_atomic_int_get (&pool->active))
247 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
249 /* free the buffer when we are inactive */
250 if (G_LIKELY (pclass->set_config))
251 result = pclass->set_config (pool, config);
257 gst_structure_free (pool->config);
258 pool->config = config;
265 * gst_buffer_pool_get_config:
266 * @pool: a #GstBufferPool
268 * Get the current configuration of the pool. This configuration is read-only,
269 * use gst_structure_copy() to make a writable copy.
272 gst_buffer_pool_get_config (GstBufferPool * pool)
274 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
281 * gst_buffer_pool_config_set:
282 * @pool: a #GstBufferPool
283 * @size: the size of each buffer, not including pre and post fix
284 * @min_buffers: the minimum amount of buffers to allocate.
285 * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
286 * @prefix: prefix each buffer with this many bytes
287 * @postfix: postfix each buffer with this many bytes
288 * @align: alignment of the buffer data.
290 * Configure @config with the given parameters.
293 gst_buffer_pool_config_set (GstStructure * config, guint size,
294 guint min_buffers, guint max_buffers, guint prefix, guint postfix,
297 g_return_if_fail (config != NULL);
299 gst_structure_id_set (config,
300 GST_QUARK (SIZE), G_TYPE_UINT, size,
301 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
302 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
303 GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
304 GST_QUARK (POSTFIX), G_TYPE_UINT, postfix,
305 GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
309 * gst_buffer_pool_config_get:
310 * @pool: a #GstBufferPool
311 * @size: the size of each buffer, not including pre and post fix
312 * @min_buffers: the minimum amount of buffers to allocate.
313 * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
314 * @prefix: prefix each buffer with this many bytes
315 * @postfix: postfix each buffer with this many bytes
316 * @align: alignment of the buffer data.
318 * Get the configuration values from @config.
321 gst_buffer_pool_config_get (GstStructure * config, guint * size,
322 guint * min_buffers, guint * max_buffers, guint * prefix, guint * postfix,
325 g_return_val_if_fail (config != NULL, FALSE);
327 return gst_structure_id_get (config,
328 GST_QUARK (SIZE), G_TYPE_UINT, size,
329 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
330 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
331 GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
332 GST_QUARK (POSTFIX), G_TYPE_UINT, postfix,
333 GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
337 default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
338 GstBufferPoolParams * params)
341 GstBufferPoolPrivate *priv = pool->priv;
343 *buffer = gst_buffer_new ();
345 align = priv->align - 1;
346 size = priv->prefix + priv->postfix + priv->size + align;
350 memptr = g_malloc (size);
351 GST_BUFFER_MALLOCDATA (*buffer) = memptr;
352 memptr = (guint8 *) ((guintptr) (memptr + align) & ~align);
353 GST_BUFFER_DATA (*buffer) = memptr + priv->prefix;
354 GST_BUFFER_SIZE (*buffer) = priv->size;
361 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
362 GstBufferPoolParams * params)
364 GstFlowReturn result;
365 GstBufferPoolClass *pclass;
366 GstBufferPoolPrivate *priv = pool->priv;
368 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
371 if (!g_atomic_int_get (&pool->active))
372 return GST_FLOW_WRONG_STATE;
374 /* try to get a buffer from the queue */
375 *buffer = gst_atomic_queue_pop (pool->queue);
377 /* FIXME check the size of the buffer */
378 gst_poll_read_control (pool->poll);
379 result = GST_FLOW_OK;
384 if (priv->max_buffers == 0) {
385 /* no max_buffers, we allocate some more */
386 if (G_LIKELY (pclass->alloc_buffer))
387 result = pclass->alloc_buffer (pool, buffer, params);
389 result = GST_FLOW_NOT_SUPPORTED;
393 /* check if we need to wait */
394 if (!(params->flags & GST_BUFFER_POOL_FLAG_WAIT)) {
395 result = GST_FLOW_UNEXPECTED;
400 gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
406 * gst_buffer_pool_acquire_buffer:
407 * @pool: a #GstBufferPool
408 * @buffer: a location for a #GstBuffer
409 * @params: parameters.
411 * Acquire a buffer from @pool. @buffer should point to a memory location that
412 * can hold a pointer to the new buffer.
414 * @params can be NULL or contain optional parameters to influence the allocation.
416 * Returns: a #GstFlowReturn such as GST_FLOW_WRONG_STATE when the pool is
420 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
421 GstBufferPoolParams * params)
423 GstBufferPoolClass *pclass;
424 GstFlowReturn result;
426 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), GST_FLOW_ERROR);
427 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
429 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
431 if (G_LIKELY (pclass->acquire_buffer))
432 result = pclass->acquire_buffer (pool, buffer, params);
434 result = GST_FLOW_NOT_SUPPORTED;
440 default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
442 gst_buffer_unref (buffer);
446 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
448 /* keep it around in our queue, we might be inactive but that's ok because we
449 * handle that unlikely case below. */
450 gst_atomic_queue_push (pool->queue, buffer);
451 gst_poll_write_control (pool->poll);
453 if (G_UNLIKELY (!g_atomic_int_get (&pool->active))) {
454 /* we are inactive, remove the buffers again */
455 flush_buffers (pool);
460 * gst_buffer_pool_release_buffer:
461 * @pool: a #GstBufferPool
462 * @buffer: a #GstBuffer
464 * Release @buffer to @pool. @buffer should have previously been allocated from
465 * @pool with gst_buffer_pool_acquire_buffer().
467 * This function is usually called automatically when the last ref on @buffer
471 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
473 GstBufferPoolClass *pclass;
475 g_return_if_fail (GST_IS_BUFFER_POOL (pool));
476 g_return_if_fail (buffer != NULL);
478 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
480 if (G_LIKELY (pclass->release_buffer))
481 pclass->release_buffer (pool, buffer);