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_flushing (GstBufferPool * pool, gboolean flushing);
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_flushing = default_set_flushing;
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_flushing (pool, TRUE);
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);
150 default_set_flushing (GstBufferPool * pool, gboolean flushing)
152 g_atomic_int_set (&pool->flushing, flushing);
155 /* write the control socket so that waiters get woken up and can check the
156 * flushing flag we set above */
157 gst_poll_write_control (pool->poll);
158 flush_buffers (pool);
160 gst_poll_read_control (pool->poll);
165 * gst_buffer_pool_set_flushing:
166 * @pool: a #GstBufferPool
167 * @flushing: the new flushing state
169 * Control the flushing state of @pool. When the pool is flushing, new calls to
170 * gst_buffer_pool_acquire_buffer() will return with GST_FLOW_WRONG_STATE.
173 gst_buffer_pool_set_flushing (GstBufferPool * pool, gboolean flushing)
175 GstBufferPoolClass *pclass;
177 g_return_if_fail (GST_IS_BUFFER_POOL (pool));
179 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
181 if (G_LIKELY (pclass->set_flushing))
182 pclass->set_flushing (pool, flushing);
186 default_set_config (GstBufferPool * pool, GstStructure * config)
189 GstBufferPoolClass *pclass;
190 GstBufferPoolPrivate *priv = pool->priv;
192 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
194 /* parse the config and keep around */
195 gst_buffer_pool_config_get (config, &priv->size, &priv->min_buffers,
196 &priv->max_buffers, &priv->prefix, &priv->postfix, &priv->align);
198 /* we need to prealloc buffers */
199 for (i = priv->min_buffers; i > 0; i--) {
202 if (G_LIKELY (pclass->alloc_buffer)) {
203 if (!pclass->alloc_buffer (pool, &buffer, NULL))
208 /* store in the queue */
209 gst_atomic_queue_push (pool->queue, buffer);
210 gst_poll_write_control (pool->poll);
217 * gst_buffer_pool_set_config:
218 * @pool: a #GstBufferPool
219 * @config: a #GstStructure
221 * Set the configuration of the pool. The pool must be flushing or else this
222 * function will do nothing and return FALSE.
224 * @condfig is a #GstStructure that contains the configuration parameters for
225 * the pool. A default and mandatory set of parameters can be configured with
226 * gst_buffer_pool_config_set().
228 * Returns: TRUE when the configuration could be set.
231 gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
234 GstBufferPoolClass *pclass;
236 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
237 g_return_val_if_fail (config != NULL, FALSE);
239 if (!g_atomic_int_get (&pool->flushing))
242 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
244 /* free the buffer when we are flushing */
245 if (G_LIKELY (pclass->set_config))
246 result = pclass->set_config (pool, config);
252 gst_structure_free (pool->config);
253 pool->config = config;
260 * gst_buffer_pool_get_config:
261 * @pool: a #GstBufferPool
263 * Get the current configuration of the pool. This configuration is read-only,
264 * use gst_structure_copy() to make a writable copy.
267 gst_buffer_pool_get_config (GstBufferPool * pool)
269 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
276 * gst_buffer_pool_config_set:
277 * @pool: a #GstBufferPool
278 * @size: the size of each buffer, not including pre and post fix
279 * @min_buffers: the minimum amount of buffers to allocate.
280 * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
281 * @prefix: prefix each buffer with this many bytes
282 * @postfix: postfix each buffer with this many bytes
283 * @align: alignment of the buffer data.
285 * Configure @config with the given parameters.
288 gst_buffer_pool_config_set (GstStructure * config, guint size,
289 guint min_buffers, guint max_buffers, guint prefix, guint postfix,
292 g_return_if_fail (config != NULL);
294 gst_structure_id_set (config,
295 GST_QUARK (SIZE), G_TYPE_UINT, size,
296 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
297 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
298 GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
299 GST_QUARK (POSTFIX), G_TYPE_UINT, postfix,
300 GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
304 * gst_buffer_pool_config_get:
305 * @pool: a #GstBufferPool
306 * @size: the size of each buffer, not including pre and post fix
307 * @min_buffers: the minimum amount of buffers to allocate.
308 * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
309 * @prefix: prefix each buffer with this many bytes
310 * @postfix: postfix each buffer with this many bytes
311 * @align: alignment of the buffer data.
313 * Get the configuration values from @config.
316 gst_buffer_pool_config_get (GstStructure * config, guint * size,
317 guint * min_buffers, guint * max_buffers, guint * prefix, guint * postfix,
320 g_return_val_if_fail (config != NULL, FALSE);
322 return gst_structure_id_get (config,
323 GST_QUARK (SIZE), G_TYPE_UINT, size,
324 GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
325 GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
326 GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
327 GST_QUARK (POSTFIX), G_TYPE_UINT, postfix,
328 GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
332 default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
333 GstBufferPoolParams * params)
336 GstBufferPoolPrivate *priv = pool->priv;
338 *buffer = gst_buffer_new ();
340 align = priv->align - 1;
341 size = priv->prefix + priv->postfix + priv->size + align;
345 memptr = g_malloc (size);
346 GST_BUFFER_MALLOCDATA (*buffer) = memptr;
347 memptr = (guint8 *) ((guintptr) (memptr + align) & ~align);
348 GST_BUFFER_DATA (*buffer) = memptr + priv->prefix;
349 GST_BUFFER_SIZE (*buffer) = priv->size;
356 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
357 GstBufferPoolParams * params)
359 GstFlowReturn result;
360 GstBufferPoolClass *pclass;
361 GstBufferPoolPrivate *priv = pool->priv;
363 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
366 if (g_atomic_int_get (&pool->flushing))
367 return GST_FLOW_WRONG_STATE;
369 /* try to get a buffer from the queue */
370 *buffer = gst_atomic_queue_pop (pool->queue);
372 /* FIXME check the size of the buffer */
373 gst_poll_read_control (pool->poll);
374 result = GST_FLOW_OK;
379 if (priv->max_buffers == 0) {
380 /* no max_buffers, we allocate some more */
381 if (G_LIKELY (pclass->alloc_buffer))
382 result = pclass->alloc_buffer (pool, buffer, params);
384 result = GST_FLOW_NOT_SUPPORTED;
388 /* check if we need to wait */
389 if (!(params->flags & GST_BUFFER_POOL_FLAG_WAIT)) {
390 result = GST_FLOW_UNEXPECTED;
395 gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
401 * gst_buffer_pool_acquire_buffer:
402 * @pool: a #GstBufferPool
403 * @buffer: a location for a #GstBuffer
404 * @params: parameters.
406 * Acquire a buffer from @pool. @buffer should point to a memory location that
407 * can hold a pointer to the new buffer.
409 * @params can be NULL or contain optional parameters to influence the allocation.
411 * Returns: a #GstFlowReturn such as GST_FLOW_WRONG_STATE when the pool is
415 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
416 GstBufferPoolParams * params)
418 GstBufferPoolClass *pclass;
419 GstFlowReturn result;
421 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), GST_FLOW_ERROR);
422 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
424 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
426 if (G_LIKELY (pclass->acquire_buffer))
427 result = pclass->acquire_buffer (pool, buffer, params);
429 result = GST_FLOW_NOT_SUPPORTED;
435 default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
437 gst_buffer_unref (buffer);
441 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
443 /* keep it around in our queue, we might be flushing but that's ok because we
444 * handle that unlikely case below. */
445 gst_atomic_queue_push (pool->queue, buffer);
446 gst_poll_write_control (pool->poll);
448 if (G_UNLIKELY (g_atomic_int_get (&pool->flushing))) {
449 /* we are flushing, remove the buffers again */
450 flush_buffers (pool);
455 * gst_buffer_pool_release_buffer:
456 * @pool: a #GstBufferPool
457 * @buffer: a #GstBuffer
459 * Release @buffer to @pool. @buffer should have previously been allocated from
460 * @pool with gst_buffer_pool_acquire_buffer().
462 * This function is usually called automatically when the last ref on @buffer
466 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
468 GstBufferPoolClass *pclass;
470 g_return_if_fail (GST_IS_BUFFER_POOL (pool));
471 g_return_if_fail (buffer != NULL);
473 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
475 if (G_LIKELY (pclass->release_buffer))
476 pclass->release_buffer (pool, buffer);