bufferpool: rename 'flushing' to 'active'
[platform/upstream/gstreamer.git] / gst / gstbufferpool.c
1 /* GStreamer
2  * Copyright (C) 2010 Wim Taymans <wim.taymans@gmail.com>
3  *
4  * gstbufferpool.c: GstBufferPool baseclass
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 /**
23  * SECTION:gstbufferpool
24  * @short_description: Pool for buffers
25  * @see_also: #GstBuffer
26  *
27  */
28
29 #include "gst_private.h"
30
31 #include <errno.h>
32 #ifdef HAVE_UNISTD_H
33 #  include <unistd.h>
34 #endif
35 #include <sys/types.h>
36
37 #include "gstinfo.h"
38 #include "gstquark.h"
39
40 #include "gstbufferpool.h"
41
42
43 #define GST_BUFFER_POOL_GET_PRIVATE(obj)  \
44    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BUFFER_POOL, GstBufferPoolPrivate))
45
46 struct _GstBufferPoolPrivate
47 {
48   guint min_buffers;
49   guint max_buffers;
50   guint size;
51   guint prefix;
52   guint postfix;
53   guint align;
54 };
55
56 enum
57 {
58   /* add more above */
59   LAST_SIGNAL
60 };
61
62 static void gst_buffer_pool_finalize (GObject * object);
63
64 G_DEFINE_TYPE (GstBufferPool, gst_buffer_pool, GST_TYPE_OBJECT);
65
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);
75
76 static void
77 gst_buffer_pool_class_init (GstBufferPoolClass * klass)
78 {
79   GObjectClass *gobject_class = (GObjectClass *) klass;
80
81   gobject_class->finalize = gst_buffer_pool_finalize;
82
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;
89 }
90
91 static void
92 gst_buffer_pool_init (GstBufferPool * pool)
93 {
94   pool->priv = GST_BUFFER_POOL_GET_PRIVATE (pool);
95
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);
106
107   GST_DEBUG_OBJECT (pool, "created");
108 }
109
110 static void
111 gst_buffer_pool_finalize (GObject * object)
112 {
113   G_OBJECT_CLASS (gst_buffer_pool_parent_class)->finalize (object);
114 }
115
116 /**
117  * gst_buffer_pool_new:
118  *
119  * Creates a new #GstBufferPool instance.
120  *
121  * Returns: a new #GstBufferPool instance
122  */
123 GstBufferPool *
124 gst_buffer_pool_new (void)
125 {
126   GstBufferPool *result;
127
128   result = g_object_newv (GST_TYPE_BUFFER_POOL, 0, NULL);
129   GST_DEBUG_OBJECT (result, "created new buffer pool");
130
131   return result;
132 }
133
134 static void
135 flush_buffers (GstBufferPool * pool)
136 {
137   GstBuffer *buffer;
138   GstBufferPoolClass *pclass;
139
140   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
141
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);
146   }
147 }
148
149 /* the default implementation for allocating and freeing the
150  * buffers when changing the active state */
151 static void
152 default_set_active (GstBufferPool * pool, gboolean active)
153 {
154   guint i;
155   GstBufferPoolPrivate *priv = pool->priv;
156
157   if (!active) {
158     /* clear the pool */
159     flush_buffers (pool);
160   } else {
161     GstBufferPoolClass *pclass;
162
163     pclass = GST_BUFFER_POOL_GET_CLASS (pool);
164
165     if (G_LIKELY (pclass->alloc_buffer)) {
166       /* we need to prealloc buffers */
167       for (i = priv->min_buffers; i > 0; i--) {
168         GstBuffer *buffer;
169
170         if (pclass->alloc_buffer (pool, &buffer, NULL) == GST_FLOW_OK) {
171           /* store in the queue */
172           gst_buffer_pool_release_buffer (pool, buffer);
173         }
174       }
175     }
176   }
177 }
178
179 /**
180  * gst_buffer_pool_set_active:
181  * @pool: a #GstBufferPool
182  * @active: the new active state
183  *
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.
186  */
187 void
188 gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
189 {
190   GstBufferPoolClass *pclass;
191
192   g_return_if_fail (GST_IS_BUFFER_POOL (pool));
193
194   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
195
196   if (!g_atomic_int_compare_and_exchange (&pool->active, !active, active))
197     return;
198
199   if (!active)
200     gst_poll_write_control (pool->poll);
201
202   if (G_LIKELY (pclass->set_active))
203     pclass->set_active (pool, active);
204
205   if (active)
206     gst_poll_read_control (pool->poll);
207 }
208
209 static gboolean
210 default_set_config (GstBufferPool * pool, GstStructure * config)
211 {
212   GstBufferPoolPrivate *priv = pool->priv;
213
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);
217
218   return TRUE;
219 }
220
221 /**
222  * gst_buffer_pool_set_config:
223  * @pool: a #GstBufferPool
224  * @config: a #GstStructure
225  *
226  * Set the configuration of the pool. The pool must be inactive or else this
227  * function will do nothing and return FALSE.
228  *
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().
232  *
233  * Returns: TRUE when the configuration could be set.
234  */
235 gboolean
236 gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
237 {
238   gboolean result;
239   GstBufferPoolClass *pclass;
240
241   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
242   g_return_val_if_fail (config != NULL, FALSE);
243
244   if (g_atomic_int_get (&pool->active))
245     return FALSE;
246
247   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
248
249   /* free the buffer when we are inactive */
250   if (G_LIKELY (pclass->set_config))
251     result = pclass->set_config (pool, config);
252   else
253     result = FALSE;
254
255   if (result) {
256     if (pool->config)
257       gst_structure_free (pool->config);
258     pool->config = config;
259   }
260
261   return result;
262 }
263
264 /**
265  * gst_buffer_pool_get_config:
266  * @pool: a #GstBufferPool
267  *
268  * Get the current configuration of the pool. This configuration is read-only,
269  * use gst_structure_copy() to make a writable copy.
270  */
271 const GstStructure *
272 gst_buffer_pool_get_config (GstBufferPool * pool)
273 {
274   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
275
276   return pool->config;
277 }
278
279
280 /**
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.
289  *
290  * Configure @config with the given parameters.
291  */
292 void
293 gst_buffer_pool_config_set (GstStructure * config, guint size,
294     guint min_buffers, guint max_buffers, guint prefix, guint postfix,
295     guint align)
296 {
297   g_return_if_fail (config != NULL);
298
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);
306 }
307
308 /**
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.
317  *
318  * Get the configuration values from @config.
319  */
320 gboolean
321 gst_buffer_pool_config_get (GstStructure * config, guint * size,
322     guint * min_buffers, guint * max_buffers, guint * prefix, guint * postfix,
323     guint * align)
324 {
325   g_return_val_if_fail (config != NULL, FALSE);
326
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);
334 }
335
336 static GstFlowReturn
337 default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
338     GstBufferPoolParams * params)
339 {
340   guint size, align;
341   GstBufferPoolPrivate *priv = pool->priv;
342
343   *buffer = gst_buffer_new ();
344
345   align = priv->align - 1;
346   size = priv->prefix + priv->postfix + priv->size + align;
347   if (size > 0) {
348     guint8 *memptr;
349
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;
355   }
356
357   return GST_FLOW_OK;
358 }
359
360 static GstFlowReturn
361 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
362     GstBufferPoolParams * params)
363 {
364   GstFlowReturn result;
365   GstBufferPoolClass *pclass;
366   GstBufferPoolPrivate *priv = pool->priv;
367
368   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
369
370   while (TRUE) {
371     if (!g_atomic_int_get (&pool->active))
372       return GST_FLOW_WRONG_STATE;
373
374     /* try to get a buffer from the queue */
375     *buffer = gst_atomic_queue_pop (pool->queue);
376     if (*buffer) {
377       /* FIXME check the size of the buffer */
378       gst_poll_read_control (pool->poll);
379       result = GST_FLOW_OK;
380       break;
381     }
382
383     /* no buffer */
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);
388       else
389         result = GST_FLOW_NOT_SUPPORTED;
390       break;
391     }
392
393     /* check if we need to wait */
394     if (!(params->flags & GST_BUFFER_POOL_FLAG_WAIT)) {
395       result = GST_FLOW_UNEXPECTED;
396       break;
397     }
398
399     /* now wait */
400     gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
401   }
402   return result;
403 }
404
405 /**
406  * gst_buffer_pool_acquire_buffer:
407  * @pool: a #GstBufferPool
408  * @buffer: a location for a #GstBuffer
409  * @params: parameters.
410  *
411  * Acquire a buffer from @pool. @buffer should point to a memory location that
412  * can hold a pointer to the new buffer.
413  *
414  * @params can be NULL or contain optional parameters to influence the allocation.
415  *
416  * Returns: a #GstFlowReturn such as GST_FLOW_WRONG_STATE when the pool is
417  * inactive.
418  */
419 GstFlowReturn
420 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
421     GstBufferPoolParams * params)
422 {
423   GstBufferPoolClass *pclass;
424   GstFlowReturn result;
425
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);
428
429   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
430
431   if (G_LIKELY (pclass->acquire_buffer))
432     result = pclass->acquire_buffer (pool, buffer, params);
433   else
434     result = GST_FLOW_NOT_SUPPORTED;
435
436   return result;
437 }
438
439 static void
440 default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
441 {
442   gst_buffer_unref (buffer);
443 }
444
445 static void
446 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
447 {
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);
452
453   if (G_UNLIKELY (!g_atomic_int_get (&pool->active))) {
454     /* we are inactive, remove the buffers again */
455     flush_buffers (pool);
456   }
457 }
458
459 /**
460  * gst_buffer_pool_release_buffer:
461  * @pool: a #GstBufferPool
462  * @buffer: a #GstBuffer
463  *
464  * Release @buffer to @pool. @buffer should have previously been allocated from
465  * @pool with gst_buffer_pool_acquire_buffer().
466  *
467  * This function is usually called automatically when the last ref on @buffer
468  * disappears.
469  */
470 void
471 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
472 {
473   GstBufferPoolClass *pclass;
474
475   g_return_if_fail (GST_IS_BUFFER_POOL (pool));
476   g_return_if_fail (buffer != NULL);
477
478   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
479
480   if (G_LIKELY (pclass->release_buffer))
481     pclass->release_buffer (pool, buffer);
482 }