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>
39 #include "gstbufferpool.h"
42 #define GST_BUFFER_POOL_GET_PRIVATE(obj) \
43 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BUFFER_POOL, GstBufferPoolPrivate))
45 struct _GstBufferPoolPrivate
61 static void gst_buffer_pool_finalize (GObject * object);
63 G_DEFINE_TYPE (GstBufferPool, gst_buffer_pool, GST_TYPE_OBJECT);
65 static void default_set_flushing (GstBufferPool * pool, gboolean flushing);
66 static gboolean default_set_config (GstBufferPool * pool,
67 GstStructure * config);
68 static GstFlowReturn default_alloc_buffer (GstBufferPool * pool,
69 GstBuffer ** buffer, GstBufferPoolParams * params);
70 static GstFlowReturn default_acquire_buffer (GstBufferPool * pool,
71 GstBuffer ** buffer, GstBufferPoolParams * params);
72 static void default_free_buffer (GstBufferPool * pool, GstBuffer * buffer);
73 static void default_release_buffer (GstBufferPool * pool, GstBuffer * buffer);
76 gst_buffer_pool_class_init (GstBufferPoolClass * klass)
78 GObjectClass *gobject_class = (GObjectClass *) klass;
80 gobject_class->finalize = gst_buffer_pool_finalize;
82 klass->set_flushing = default_set_flushing;
83 klass->set_config = default_set_config;
84 klass->acquire_buffer = default_acquire_buffer;
85 klass->alloc_buffer = default_alloc_buffer;
86 klass->release_buffer = default_release_buffer;
87 klass->free_buffer = default_free_buffer;
91 gst_buffer_pool_init (GstBufferPool * pool)
93 pool->priv = GST_BUFFER_POOL_GET_PRIVATE (pool);
95 pool->config = gst_structure_new ("GstBufferPoolConfig",
96 "size", G_TYPE_UINT, 0,
97 "min-buffers", G_TYPE_UINT, 0,
98 "max-buffers", G_TYPE_UINT, 0,
99 "prefix", G_TYPE_UINT, 0,
100 "postfix", G_TYPE_UINT, 0, "align", G_TYPE_UINT, 1, NULL);
101 pool->poll = gst_poll_new_timer ();
102 pool->queue = gst_atomic_queue_new (10);
103 default_set_flushing (pool, TRUE);
105 GST_DEBUG_OBJECT (pool, "created");
109 gst_buffer_pool_finalize (GObject * object)
111 G_OBJECT_CLASS (gst_buffer_pool_parent_class)->finalize (object);
115 * gst_buffer_pool_new:
117 * Creates a new #GstBufferPool instance.
119 * Returns: a new #GstBufferPool instance
122 gst_buffer_pool_new (void)
124 GstBufferPool *result;
126 result = g_object_newv (GST_TYPE_BUFFER_POOL, 0, NULL);
127 GST_DEBUG_OBJECT (result, "created new buffer pool");
133 flush_buffers (GstBufferPool * pool)
136 GstBufferPoolClass *pclass;
138 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
140 while ((buffer = gst_atomic_queue_pop (pool->queue))) {
141 gst_poll_read_control (pool->poll);
142 if (G_LIKELY (pclass->free_buffer))
143 pclass->free_buffer (pool, buffer);
148 default_set_flushing (GstBufferPool * pool, gboolean flushing)
150 g_atomic_int_set (&pool->flushing, flushing);
153 /* write the control socket so that waiters get woken up and can check the
154 * flushing flag we set above */
155 gst_poll_write_control (pool->poll);
156 flush_buffers (pool);
158 gst_poll_read_control (pool->poll);
163 * gst_buffer_pool_set_flushing:
164 * @pool: a #GstBufferPool
165 * @flushing: the new flushing state
167 * Control the flushing state of @pool. When the pool is flushing, new calls to
168 * gst_buffer_pool_acquire_buffer() will return with GST_FLOW_WRONG_STATE.
171 gst_buffer_pool_set_flushing (GstBufferPool * pool, gboolean flushing)
173 GstBufferPoolClass *pclass;
175 g_return_if_fail (GST_IS_BUFFER_POOL (pool));
177 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
179 if (G_LIKELY (pclass->set_flushing))
180 pclass->set_flushing (pool, flushing);
184 default_set_config (GstBufferPool * pool, GstStructure * config)
187 GstBufferPoolClass *pclass;
188 GstBufferPoolPrivate *priv = pool->priv;
190 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
192 /* parse the config and keep around */
193 gst_buffer_pool_config_get (config, &priv->size, &priv->min_buffers,
194 &priv->max_buffers, &priv->prefix, &priv->postfix, &priv->align);
196 /* we need to prealloc buffers */
197 for (i = priv->min_buffers; i > 0; i--) {
200 if (G_LIKELY (pclass->alloc_buffer)) {
201 if (!pclass->alloc_buffer (pool, &buffer, NULL))
206 /* store in the queue */
207 gst_atomic_queue_push (pool->queue, buffer);
208 gst_poll_write_control (pool->poll);
215 * gst_buffer_pool_set_config:
216 * @pool: a #GstBufferPool
217 * @config: a #GstStructure
219 * Set the configuration of the pool. The pool must be flushing or else this
220 * function will do nothing and return FALSE.
222 * @condfig is a #GstStructure that contains the configuration parameters for
223 * the pool. A default and mandatory set of parameters can be configured with
224 * gst_buffer_pool_config_set().
226 * Returns: TRUE when the configuration could be set.
229 gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
232 GstBufferPoolClass *pclass;
234 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
235 g_return_val_if_fail (config != NULL, FALSE);
237 if (!g_atomic_int_get (&pool->flushing))
240 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
242 /* free the buffer when we are flushing */
243 if (G_LIKELY (pclass->set_config))
244 result = pclass->set_config (pool, config);
250 gst_structure_free (pool->config);
251 pool->config = config;
258 * gst_buffer_pool_get_config:
259 * @pool: a #GstBufferPool
261 * Get the current configuration of the pool. This configuration is read-only,
262 * use gst_structure_copy() to make a writable copy.
265 gst_buffer_pool_get_config (GstBufferPool * pool)
267 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
274 * gst_buffer_pool_config_set:
275 * @pool: a #GstBufferPool
276 * @size: the size of each buffer, not including pre and post fix
277 * @min_buffers: the minimum amount of buffers to allocate.
278 * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
279 * @prefix: prefix each buffer with this many bytes
280 * @postfix: postfix each buffer with this many bytes
281 * @align: alignment of the buffer data.
283 * Configure @config with the given parameters.
286 gst_buffer_pool_config_set (GstStructure * config, guint size,
287 guint min_buffers, guint max_buffers, guint prefix, guint postfix,
290 g_return_if_fail (config != NULL);
292 gst_structure_set (config,
293 "size", G_TYPE_UINT, size,
294 "min-buffers", G_TYPE_UINT, min_buffers,
295 "max-buffers", G_TYPE_UINT, max_buffers,
296 "prefix", G_TYPE_UINT, prefix,
297 "postfix", G_TYPE_UINT, postfix, "align", G_TYPE_UINT, align, NULL);
301 * gst_buffer_pool_config_get:
302 * @pool: a #GstBufferPool
303 * @size: the size of each buffer, not including pre and post fix
304 * @min_buffers: the minimum amount of buffers to allocate.
305 * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
306 * @prefix: prefix each buffer with this many bytes
307 * @postfix: postfix each buffer with this many bytes
308 * @align: alignment of the buffer data.
310 * Get the configuration values from @config.
313 gst_buffer_pool_config_get (GstStructure * config, guint * size,
314 guint * min_buffers, guint * max_buffers, guint * prefix, guint * postfix,
317 g_return_val_if_fail (config != NULL, FALSE);
319 return gst_structure_get (config,
320 "size", G_TYPE_UINT, size,
321 "min-buffers", G_TYPE_UINT, min_buffers,
322 "max-buffers", G_TYPE_UINT, max_buffers,
323 "prefix", G_TYPE_UINT, prefix,
324 "postfix", G_TYPE_UINT, postfix, "align", G_TYPE_UINT, align, NULL);
328 default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
329 GstBufferPoolParams * params)
332 GstBufferPoolPrivate *priv = pool->priv;
334 *buffer = gst_buffer_new ();
336 align = priv->align - 1;
337 size = priv->prefix + priv->postfix + priv->size + align;
341 memptr = g_malloc (size);
342 GST_BUFFER_MALLOCDATA (*buffer) = memptr;
343 memptr = (guint8 *) ((guintptr) (memptr + align) & ~align);
344 GST_BUFFER_DATA (*buffer) = memptr + priv->prefix;
345 GST_BUFFER_SIZE (*buffer) = priv->size;
352 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
353 GstBufferPoolParams * params)
355 GstFlowReturn result;
356 GstBufferPoolClass *pclass;
357 GstBufferPoolPrivate *priv = pool->priv;
359 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
362 if (g_atomic_int_get (&pool->flushing))
363 return GST_FLOW_WRONG_STATE;
365 /* try to get a buffer from the queue */
366 *buffer = gst_atomic_queue_pop (pool->queue);
368 /* FIXME check the size of the buffer */
369 gst_poll_read_control (pool->poll);
370 result = GST_FLOW_OK;
375 if (priv->max_buffers == 0) {
376 /* no max_buffers, we allocate some more */
377 if (G_LIKELY (pclass->alloc_buffer))
378 result = pclass->alloc_buffer (pool, buffer, params);
380 result = GST_FLOW_NOT_SUPPORTED;
384 /* check if we need to wait */
385 if (!(params->flags & GST_BUFFER_POOL_FLAG_WAIT)) {
386 result = GST_FLOW_UNEXPECTED;
391 gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
397 * gst_buffer_pool_acquire_buffer:
398 * @pool: a #GstBufferPool
399 * @buffer: a location for a #GstBuffer
400 * @params: parameters.
402 * Acquire a buffer from @pool. @buffer should point to a memory location that
403 * can hold a pointer to the new buffer.
405 * @params can be NULL or contain optional parameters to influence the allocation.
407 * Returns: a #GstFlowReturn such as GST_FLOW_WRONG_STATE when the pool is
411 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
412 GstBufferPoolParams * params)
414 GstBufferPoolClass *pclass;
415 GstFlowReturn result;
417 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), GST_FLOW_ERROR);
418 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
420 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
422 if (G_LIKELY (pclass->acquire_buffer))
423 result = pclass->acquire_buffer (pool, buffer, params);
425 result = GST_FLOW_NOT_SUPPORTED;
431 default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
433 gst_buffer_unref (buffer);
437 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
439 /* keep it around in our queue, we might be flushing but that's ok because we
440 * handle that unlikely case below. */
441 gst_atomic_queue_push (pool->queue, buffer);
442 gst_poll_write_control (pool->poll);
444 if (G_UNLIKELY (g_atomic_int_get (&pool->flushing))) {
445 /* we are flushing, remove the buffers again */
446 flush_buffers (pool);
451 * gst_buffer_pool_release_buffer:
452 * @pool: a #GstBufferPool
453 * @buffer: a #GstBuffer
455 * Release @buffer to @pool. @buffer should have previously been allocated from
456 * @pool with gst_buffer_pool_acquire_buffer().
458 * This function is usually called automatically when the last ref on @buffer
462 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
464 GstBufferPoolClass *pclass;
466 g_return_if_fail (GST_IS_BUFFER_POOL (pool));
467 g_return_if_fail (buffer != NULL);
469 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
471 if (G_LIKELY (pclass->release_buffer))
472 pclass->release_buffer (pool, buffer);