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"
47 static void gst_buffer_pool_finalize (GObject * object);
49 G_DEFINE_TYPE (GstBufferPool, gst_buffer_pool, GST_TYPE_OBJECT);
51 static void default_set_flushing (GstBufferPool * pool, gboolean flushing);
52 static gboolean default_set_config (GstBufferPool * pool,
53 GstBufferPoolConfig * config);
54 static GstFlowReturn default_alloc_buffer (GstBufferPool * pool,
55 GstBuffer ** buffer, GstBufferPoolConfig * config,
56 GstBufferPoolParams * params);
57 static GstFlowReturn default_acquire_buffer (GstBufferPool * pool,
58 GstBuffer ** buffer, GstBufferPoolParams * params);
59 static void default_free_buffer (GstBufferPool * pool, GstBuffer * buffer);
60 static void default_release_buffer (GstBufferPool * pool, GstBuffer * buffer);
63 gst_buffer_pool_class_init (GstBufferPoolClass * klass)
65 GObjectClass *gobject_class = (GObjectClass *) klass;
67 gobject_class->finalize = gst_buffer_pool_finalize;
69 klass->set_flushing = default_set_flushing;
70 klass->set_config = default_set_config;
71 klass->acquire_buffer = default_acquire_buffer;
72 klass->alloc_buffer = default_alloc_buffer;
73 klass->release_buffer = default_release_buffer;
74 klass->free_buffer = default_free_buffer;
78 gst_buffer_pool_init (GstBufferPool * pool)
80 pool->config.align = 1;
81 pool->poll = gst_poll_new_timer ();
82 pool->queue = gst_atomic_queue_new (10);
83 default_set_flushing (pool, TRUE);
85 GST_DEBUG_OBJECT (pool, "created");
89 gst_buffer_pool_finalize (GObject * object)
91 G_OBJECT_CLASS (gst_buffer_pool_parent_class)->finalize (object);
95 * gst_buffer_pool_new:
97 * Creates a new #GstBufferPool instance.
99 * Returns: a new #GstBufferPool instance
102 gst_buffer_pool_new (void)
104 GstBufferPool *result;
106 result = g_object_newv (GST_TYPE_BUFFER_POOL, 0, NULL);
107 GST_DEBUG_OBJECT (result, "created new buffer pool");
113 flush_buffers (GstBufferPool * pool)
116 GstBufferPoolClass *pclass;
118 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
120 while ((buffer = gst_atomic_queue_pop (pool->queue))) {
121 gst_poll_read_control (pool->poll);
122 if (G_LIKELY (pclass->free_buffer))
123 pclass->free_buffer (pool, buffer);
128 default_set_flushing (GstBufferPool * pool, gboolean flushing)
130 g_atomic_int_set (&pool->flushing, flushing);
133 /* write the control socket so that waiters get woken up and can check the
134 * flushing flag we set above */
135 gst_poll_write_control (pool->poll);
136 flush_buffers (pool);
138 gst_poll_read_control (pool->poll);
143 * gst_buffer_pool_set_flushing:
144 * @pool: a #GstBufferPool
145 * @flushing: the new flushing state
147 * Control the flushing state of @pool. When the pool is flushing, new calls to
148 * gst_buffer_pool_acquire_buffer() will return with GST_FLOW_WRONG_STATE.
151 gst_buffer_pool_set_flushing (GstBufferPool * pool, gboolean flushing)
153 GstBufferPoolClass *pclass;
155 g_return_if_fail (GST_IS_BUFFER_POOL (pool));
157 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
159 if (G_LIKELY (pclass->set_flushing))
160 pclass->set_flushing (pool, flushing);
164 default_set_config (GstBufferPool * pool, GstBufferPoolConfig * config)
167 GstBufferPoolClass *pclass;
169 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
171 /* we need to prealloc buffers */
172 for (i = config->min_buffers; i > 0; i--) {
175 if (G_LIKELY (pclass->alloc_buffer)) {
176 if (!pclass->alloc_buffer (pool, &buffer, config, NULL))
181 /* store in the queue */
182 gst_atomic_queue_push (pool->queue, buffer);
183 gst_poll_write_control (pool->poll);
190 * gst_buffer_pool_set_config:
191 * @pool: a #GstBufferPool
192 * @config: a #GstBufferPoolConfig
194 * Set the configuration of the pool. The pool must be flushing or else this
195 * function will do nothing and return FALSE.
197 * Returns: TRUE when the configuration could be set.
200 gst_buffer_pool_set_config (GstBufferPool * pool, GstBufferPoolConfig * config)
203 GstBufferPoolClass *pclass;
205 g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
206 g_return_val_if_fail (config != NULL, FALSE);
208 if (!g_atomic_int_get (&pool->flushing))
211 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
213 /* free the buffer when we are flushing */
214 if (G_LIKELY (pclass->set_config))
215 result = pclass->set_config (pool, config);
220 pool->config = *config;
226 * gst_buffer_pool_get_config:
227 * @pool: a #GstBufferPool
228 * @config: a #GstBufferPoolConfig
230 * Get the current configuration of the pool.
233 gst_buffer_pool_get_config (GstBufferPool * pool, GstBufferPoolConfig * config)
235 *config = pool->config;
239 default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
240 GstBufferPoolConfig * config, GstBufferPoolParams * params)
244 *buffer = gst_buffer_new ();
246 align = config->align - 1;
247 size = config->prefix + config->postfix + config->size + align;
251 memptr = g_malloc (size);
252 GST_BUFFER_MALLOCDATA (*buffer) = memptr;
253 memptr = (guint8 *) ((guintptr) (memptr + align) & ~align);
254 GST_BUFFER_DATA (*buffer) = memptr + config->prefix;
255 GST_BUFFER_SIZE (*buffer) = config->size;
262 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
263 GstBufferPoolParams * params)
265 GstFlowReturn result;
266 GstBufferPoolClass *pclass;
268 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
271 if (g_atomic_int_get (&pool->flushing))
272 return GST_FLOW_WRONG_STATE;
274 /* try to get a buffer from the queue */
275 *buffer = gst_atomic_queue_pop (pool->queue);
277 /* FIXME check the size of the buffer */
278 gst_poll_read_control (pool->poll);
279 result = GST_FLOW_OK;
284 if (pool->config.max_buffers == 0) {
285 /* no max_buffers, we allocate some more */
286 if (G_LIKELY (pclass->alloc_buffer))
287 result = pclass->alloc_buffer (pool, buffer, &pool->config, params);
289 result = GST_FLOW_NOT_SUPPORTED;
293 /* check if we need to wait */
294 if (!(params->flags & GST_BUFFER_POOL_FLAG_WAIT)) {
295 result = GST_FLOW_UNEXPECTED;
300 gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
306 * gst_buffer_pool_acquire_buffer:
307 * @pool: a #GstBufferPool
308 * @buffer: a location for a #GstBuffer
309 * @params: parameters.
311 * Acquire a buffer from @pool. @buffer should point to a memory location that
312 * can hold a pointer to the new buffer.
314 * @params can be NULL or contain optional parameters to influence the allocation.
316 * Returns: a #GstFlowReturn such as GST_FLOW_WRONG_STATE when the pool is
320 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
321 GstBufferPoolParams * params)
323 GstBufferPoolClass *pclass;
324 GstFlowReturn result;
326 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
328 if (G_LIKELY (pclass->acquire_buffer))
329 result = pclass->acquire_buffer (pool, buffer, params);
331 result = GST_FLOW_NOT_SUPPORTED;
337 default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
339 gst_buffer_unref (buffer);
343 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
345 /* keep it around in our queue, we might be flushing but that's ok because we
346 * handle that unlikely case below. */
347 gst_atomic_queue_push (pool->queue, buffer);
348 gst_poll_write_control (pool->poll);
350 if (G_UNLIKELY (g_atomic_int_get (&pool->flushing))) {
351 /* we are flushing, remove the buffers again */
352 flush_buffers (pool);
357 * gst_buffer_pool_release_buffer:
358 * @pool: a #GstBufferPool
359 * @buffer: a #GstBuffer
361 * Release @buffer to @pool. @buffer should have previously been allocated from
362 * @pool with gst_buffer_pool_acquire_buffer().
364 * This function is usually called automatically when the last ref on @buffer
368 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
370 GstBufferPoolClass *pclass;
372 pclass = GST_BUFFER_POOL_GET_CLASS (pool);
374 if (G_LIKELY (pclass->release_buffer))
375 pclass->release_buffer (pool, buffer);