memory: rename GstMemoryAllocator -> GstAllocator
[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 GST_DEBUG_CATEGORY_STATIC (gst_buffer_pool_debug);
43 #define GST_CAT_DEFAULT gst_buffer_pool_debug
44
45 #define GST_BUFFER_POOL_GET_PRIVATE(obj)  \
46    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BUFFER_POOL, GstBufferPoolPrivate))
47
48 #define GST_BUFFER_POOL_LOCK(pool)   (g_static_rec_mutex_lock(&pool->priv->rec_lock))
49 #define GST_BUFFER_POOL_UNLOCK(pool) (g_static_rec_mutex_unlock(&pool->priv->rec_lock))
50
51 struct _GstBufferPoolPrivate
52 {
53   GStaticRecMutex rec_lock;
54   guint size;
55   guint min_buffers;
56   guint max_buffers;
57   guint prefix;
58   guint align;
59 };
60
61 enum
62 {
63   /* add more above */
64   LAST_SIGNAL
65 };
66
67 static void gst_buffer_pool_finalize (GObject * object);
68
69 G_DEFINE_TYPE (GstBufferPool, gst_buffer_pool, GST_TYPE_OBJECT);
70
71 static gboolean default_start (GstBufferPool * pool);
72 static gboolean default_stop (GstBufferPool * pool);
73 static gboolean default_set_config (GstBufferPool * pool,
74     GstStructure * config);
75 static GstFlowReturn default_alloc_buffer (GstBufferPool * pool,
76     GstBuffer ** buffer, GstBufferPoolParams * params);
77 static GstFlowReturn default_acquire_buffer (GstBufferPool * pool,
78     GstBuffer ** buffer, GstBufferPoolParams * params);
79 static void default_free_buffer (GstBufferPool * pool, GstBuffer * buffer);
80 static void default_release_buffer (GstBufferPool * pool, GstBuffer * buffer);
81
82 static void
83 gst_buffer_pool_class_init (GstBufferPoolClass * klass)
84 {
85   GObjectClass *gobject_class = (GObjectClass *) klass;
86
87   g_type_class_add_private (klass, sizeof (GstBufferPoolPrivate));
88
89   gobject_class->finalize = gst_buffer_pool_finalize;
90
91   klass->start = default_start;
92   klass->stop = default_stop;
93   klass->set_config = default_set_config;
94   klass->acquire_buffer = default_acquire_buffer;
95   klass->alloc_buffer = default_alloc_buffer;
96   klass->release_buffer = default_release_buffer;
97   klass->free_buffer = default_free_buffer;
98
99   GST_DEBUG_CATEGORY_INIT (gst_buffer_pool_debug, "bufferpool", 0,
100       "bufferpool debug");
101 }
102
103 static void
104 gst_buffer_pool_init (GstBufferPool * pool)
105 {
106   pool->priv = GST_BUFFER_POOL_GET_PRIVATE (pool);
107
108   g_static_rec_mutex_init (&pool->priv->rec_lock);
109
110   pool->poll = gst_poll_new_timer ();
111   pool->queue = gst_atomic_queue_new (10);
112   pool->flushing = TRUE;
113   pool->active = FALSE;
114   pool->configured = FALSE;
115   pool->started = FALSE;
116   pool->config = gst_structure_id_empty_new (GST_QUARK (BUFFER_POOL_CONFIG));
117   gst_buffer_pool_config_set (pool->config, NULL, 0, 0, 0, 0, 1);
118
119   GST_DEBUG_OBJECT (pool, "created");
120 }
121
122 static void
123 gst_buffer_pool_finalize (GObject * object)
124 {
125   GstBufferPool *pool;
126
127   pool = GST_BUFFER_POOL_CAST (object);
128
129   GST_DEBUG_OBJECT (pool, "finalize");
130
131   gst_buffer_pool_set_active (pool, FALSE);
132   gst_atomic_queue_unref (pool->queue);
133   gst_poll_free (pool->poll);
134   gst_structure_free (pool->config);
135   g_static_rec_mutex_free (&pool->priv->rec_lock);
136
137   G_OBJECT_CLASS (gst_buffer_pool_parent_class)->finalize (object);
138 }
139
140 /**
141  * gst_buffer_pool_new:
142  *
143  * Creates a new #GstBufferPool instance.
144  *
145  * Returns: a new #GstBufferPool instance
146  */
147 GstBufferPool *
148 gst_buffer_pool_new (void)
149 {
150   GstBufferPool *result;
151
152   result = g_object_newv (GST_TYPE_BUFFER_POOL, 0, NULL);
153   GST_DEBUG_OBJECT (result, "created new buffer pool");
154
155   return result;
156 }
157
158 static GstFlowReturn
159 default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
160     GstBufferPoolParams * params)
161 {
162   GstBufferPoolPrivate *priv = pool->priv;
163   GstMemory *mem;
164
165   *buffer = gst_buffer_new ();
166
167   mem = gst_allocator_alloc (NULL, priv->size + priv->prefix, priv->align);
168   gst_memory_resize (mem, priv->prefix, priv->size);
169   gst_buffer_take_memory (*buffer, -1, mem);
170
171   return GST_FLOW_OK;
172 }
173
174 /* the default implementation for preallocating the buffers
175  * in the pool */
176 static gboolean
177 default_start (GstBufferPool * pool)
178 {
179   guint i;
180   GstBufferPoolPrivate *priv = pool->priv;
181   GstBufferPoolClass *pclass;
182
183   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
184
185   /* no alloc function, error */
186   if (G_UNLIKELY (pclass->alloc_buffer == NULL))
187     goto no_alloc;
188
189   /* we need to prealloc buffers */
190   for (i = 0; i < priv->min_buffers; i++) {
191     GstBuffer *buffer;
192
193     if (pclass->alloc_buffer (pool, &buffer, NULL) != GST_FLOW_OK)
194       goto alloc_failed;
195
196     GST_LOG_OBJECT (pool, "prealloced buffer %d: %p", i, buffer);
197     /* store in the queue */
198     gst_atomic_queue_push (pool->queue, buffer);
199     gst_poll_write_control (pool->poll);
200   }
201   return TRUE;
202
203   /* ERRORS */
204 no_alloc:
205   {
206     GST_WARNING_OBJECT (pool, "no alloc function");
207     return FALSE;
208   }
209 alloc_failed:
210   {
211     GST_WARNING_OBJECT (pool, "alloc function failed");
212     return FALSE;
213   }
214 }
215
216 /* must be called with the lock */
217 static gboolean
218 do_start (GstBufferPool * pool)
219 {
220   if (!pool->started) {
221     GstBufferPoolClass *pclass;
222
223     pclass = GST_BUFFER_POOL_GET_CLASS (pool);
224
225     GST_LOG_OBJECT (pool, "starting");
226     /* start the pool, subclasses should allocate buffers and put them
227      * in the queue */
228     if (G_LIKELY (pclass->start)) {
229       if (!pclass->start (pool))
230         return FALSE;
231     }
232     pool->started = TRUE;
233   }
234   return TRUE;
235 }
236
237
238 static void
239 default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
240 {
241   gst_buffer_unref (buffer);
242 }
243
244 /* must be called with the lock */
245 static gboolean
246 default_stop (GstBufferPool * pool)
247 {
248   GstBuffer *buffer;
249   GstBufferPoolClass *pclass;
250
251   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
252
253   /* clear the pool */
254   while ((buffer = gst_atomic_queue_pop (pool->queue))) {
255     gst_poll_read_control (pool->poll);
256
257     if (G_LIKELY (pclass->free_buffer))
258       pclass->free_buffer (pool, buffer);
259   }
260   return TRUE;
261 }
262
263 /* must be called with the lock */
264 static gboolean
265 do_stop (GstBufferPool * pool)
266 {
267   if (pool->started) {
268     GstBufferPoolClass *pclass;
269
270     pclass = GST_BUFFER_POOL_GET_CLASS (pool);
271
272     GST_LOG_OBJECT (pool, "stopping");
273     if (G_LIKELY (pclass->stop)) {
274       if (!pclass->stop (pool))
275         return FALSE;
276     }
277     pool->started = FALSE;
278   }
279   return TRUE;
280 }
281
282 /**
283  * gst_buffer_pool_set_active:
284  * @pool: a #GstBufferPool
285  * @active: the new active state
286  *
287  * Control the active state of @pool. When the pool is active, new calls to
288  * gst_buffer_pool_acquire_buffer() will return with GST_FLOW_WRONG_STATE.
289  *
290  * Returns: %FALSE when the pool was not configured or when preallocation of the
291  * buffers failed.
292  */
293 gboolean
294 gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
295 {
296   gboolean res = TRUE;
297
298   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
299
300   GST_LOG_OBJECT (pool, "active %d", active);
301
302   GST_BUFFER_POOL_LOCK (pool);
303   /* just return if we are already in the right state */
304   if (pool->active == active)
305     goto was_ok;
306
307   /* we need to be configured */
308   if (!pool->configured)
309     goto not_configured;
310
311   if (active) {
312     if (!do_start (pool))
313       goto start_failed;
314
315     /* unset the flushing state now */
316     gst_poll_read_control (pool->poll);
317     g_atomic_int_set (&pool->flushing, FALSE);
318   } else {
319     /* set to flushing first */
320     g_atomic_int_set (&pool->flushing, TRUE);
321     gst_poll_write_control (pool->poll);
322
323     /* when all buffers are in the pool, free them. Else they will be
324      * freed when they are released */
325     if (g_atomic_int_get (&pool->outstanding) == 0) {
326       if (!do_stop (pool))
327         goto stop_failed;
328     }
329   }
330   pool->active = active;
331   GST_BUFFER_POOL_UNLOCK (pool);
332
333   return res;
334
335 was_ok:
336   {
337     GST_DEBUG_OBJECT (pool, "pool was in the right state");
338     GST_BUFFER_POOL_UNLOCK (pool);
339     return TRUE;
340   }
341 not_configured:
342   {
343     GST_ERROR_OBJECT (pool, "pool was not configured");
344     GST_BUFFER_POOL_UNLOCK (pool);
345     return FALSE;
346   }
347 start_failed:
348   {
349     GST_ERROR_OBJECT (pool, "start failed");
350     GST_BUFFER_POOL_UNLOCK (pool);
351     return FALSE;
352   }
353 stop_failed:
354   {
355     GST_WARNING_OBJECT (pool, "stop failed");
356     GST_BUFFER_POOL_UNLOCK (pool);
357     return FALSE;
358   }
359 }
360
361 static gboolean
362 default_set_config (GstBufferPool * pool, GstStructure * config)
363 {
364   GstBufferPoolPrivate *priv = pool->priv;
365   const GstCaps *caps;
366   guint size, min_buffers, max_buffers;
367   guint prefix, align;
368
369   /* parse the config and keep around */
370   if (!gst_buffer_pool_config_get (config, &caps, &size, &min_buffers,
371           &max_buffers, &prefix, &align))
372     goto wrong_config;
373
374   GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
375
376   priv->size = size;
377   priv->min_buffers = min_buffers;
378   priv->max_buffers = max_buffers;
379   priv->prefix = prefix;
380   priv->align = align;
381
382   return TRUE;
383
384 wrong_config:
385   {
386     GST_WARNING_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
387     return FALSE;
388   }
389 }
390
391 /**
392  * gst_buffer_pool_set_config:
393  * @pool: a #GstBufferPool
394  * @config: a #GstStructure
395  *
396  * Set the configuration of the pool. The pool must be inactive and all buffers
397  * allocated form this pool must be returned or else this function will do
398  * nothing and return FALSE.
399  *
400  * @condfig is a #GstStructure that contains the configuration parameters for
401  * the pool. A default and mandatory set of parameters can be configured with
402  * gst_buffer_pool_config_set(). This function takes ownership of @config.
403  *
404  * Returns: TRUE when the configuration could be set.
405  */
406 gboolean
407 gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
408 {
409   gboolean result;
410   GstBufferPoolClass *pclass;
411
412   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
413   g_return_val_if_fail (config != NULL, FALSE);
414
415   GST_BUFFER_POOL_LOCK (pool);
416   /* can't change the settings when active */
417   if (pool->active)
418     goto was_active;
419
420   /* we can't change when outstanding buffers */
421   if (g_atomic_int_get (&pool->outstanding) != 0)
422     goto have_outstanding;
423
424   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
425
426   /* set the new config */
427   if (G_LIKELY (pclass->set_config))
428     result = pclass->set_config (pool, config);
429   else
430     result = FALSE;
431
432   if (result) {
433     if (pool->config)
434       gst_structure_free (pool->config);
435     pool->config = config;
436
437     /* now we are configured */
438     pool->configured = TRUE;
439   }
440   GST_BUFFER_POOL_UNLOCK (pool);
441
442   return result;
443
444   /* ERRORS */
445 was_active:
446   {
447     GST_WARNING_OBJECT (pool, "can't change config, we are active");
448     GST_BUFFER_POOL_UNLOCK (pool);
449     return FALSE;
450   }
451 have_outstanding:
452   {
453     GST_WARNING_OBJECT (pool, "can't change config, have outstanding buffers");
454     GST_BUFFER_POOL_UNLOCK (pool);
455     return FALSE;
456   }
457 }
458
459 /**
460  * gst_buffer_pool_get_config:
461  * @pool: a #GstBufferPool
462  *
463  * Get a copy of the current configuration of the pool. This configuration
464  * can either be modified and used for the gst_buffer_pool_set_config() call
465  * or it must be freed after usage.
466  *
467  * Returns: a copy of the current configuration of @pool. use
468  * gst_structure_free() after usage or gst_buffer_pool_set_config().
469  */
470 GstStructure *
471 gst_buffer_pool_get_config (GstBufferPool * pool)
472 {
473   GstStructure *result;
474
475   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
476
477   GST_BUFFER_POOL_UNLOCK (pool);
478   result = gst_structure_copy (pool->config);
479   GST_BUFFER_POOL_UNLOCK (pool);
480
481   return result;
482 }
483
484 static const gchar *empty_meta[] = { NULL };
485
486 /**
487  * gst_buffer_pool_get_metas:
488  * @pool: a #GstBufferPool
489  *
490  * Get a NULL terminated array of string with supported #GstMeta apis for
491  * @pool. The requested api would typically be added to the config with
492  * gst_buffer_pool_config_add_meta().
493  *
494  * Returns: a NULL terminated array of strings.
495  */
496 const gchar **
497 gst_buffer_pool_get_metas (GstBufferPool * pool)
498 {
499   GstBufferPoolClass *pclass;
500   const gchar **result;
501
502   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
503
504   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
505
506   if (G_LIKELY (pclass->get_metas))
507     result = pclass->get_metas (pool);
508   else
509     result = empty_meta;
510
511   return result;
512 }
513
514 /**
515  * gst_buffer_pool_config_set:
516  * @config: a #GstBufferPool configuration
517  * @caps: caps for the buffers
518  * @size: the size of each buffer, not including prefix
519  * @min_buffers: the minimum amount of buffers to allocate.
520  * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
521  * @prefix: prefix each buffer with this many bytes
522  * @align: alignment of the buffer data.
523  *
524  * Configure @config with the given parameters.
525  */
526 void
527 gst_buffer_pool_config_set (GstStructure * config, const GstCaps * caps,
528     guint size, guint min_buffers, guint max_buffers, guint prefix, guint align)
529 {
530   g_return_if_fail (config != NULL);
531
532   gst_structure_id_set (config,
533       GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
534       GST_QUARK (SIZE), G_TYPE_UINT, size,
535       GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
536       GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
537       GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
538       GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
539 }
540
541 /**
542  * gst_buffer_pool_config_add_meta:
543  * @config: a #GstBufferPool configuration
544  * @api: an API to add
545  *
546  * Adds the metadata api to @config. This will instruct the @bufferpool to use
547  * the specified metadata api on the buffers that it allocates.
548  *
549  * The supported API by @pool can be retrieved with gst_buffer_pool_get_metas().
550  */
551 void
552 gst_buffer_pool_config_add_meta (GstStructure * config, const gchar * api)
553 {
554   GValueArray *array;
555   const GValue *value;
556   GValue api_value = { 0 };
557
558   g_return_if_fail (config != NULL);
559
560   value = gst_structure_id_get_value (config, GST_QUARK (META));
561   if (value) {
562     array = (GValueArray *) g_value_get_boxed (value);
563   } else {
564     GValue new_array_val = { 0, };
565
566     array = g_value_array_new (0);
567
568     g_value_init (&new_array_val, G_TYPE_VALUE_ARRAY);
569     g_value_take_boxed (&new_array_val, array);
570
571     gst_structure_id_take_value (config, GST_QUARK (META), &new_array_val);
572   }
573
574   g_value_init (&api_value, G_TYPE_STRING);
575   g_value_set_string (&api_value, api);
576   g_value_array_append (array, &api_value);
577   g_value_unset (&api_value);
578 }
579
580 /**
581  * gst_buffer_pool_config_n_metas:
582  * @config: a #GstBufferPool configuration
583  *
584  * Retrieve the number of values currently stored in the
585  * meta API array of the @config structure.
586  *
587  * Returns: the metadata API array size as a #guint.
588  */
589 guint
590 gst_buffer_pool_config_n_metas (GstStructure * config)
591 {
592   GValueArray *array;
593   const GValue *value;
594   guint size = 0;
595
596   g_return_val_if_fail (config != NULL, 0);
597
598   value = gst_structure_id_get_value (config, GST_QUARK (META));
599   if (value) {
600     array = (GValueArray *) g_value_get_boxed (value);
601     size = array->n_values;
602   }
603   return size;
604 }
605
606 /**
607  * gst_buffer_pool_config_get_meta:
608  * @config: a #GstBufferPool configuration
609  * @index: position in the metadata API array to read
610  *
611  * Parse an available @config and get the metadata API
612  * at @index of the metadata API array.
613  *
614  * Returns: a #gchar of the metadata API at @index.
615  */
616 const gchar *
617 gst_buffer_pool_config_get_meta (GstStructure * config, guint index)
618 {
619   const GValue *value;
620   const gchar *ret = NULL;
621
622   g_return_val_if_fail (config != NULL, 0);
623
624   value = gst_structure_id_get_value (config, GST_QUARK (META));
625   if (value) {
626     GValueArray *meta;
627     GValue *api_value;
628
629     meta = (GValueArray *) g_value_get_boxed (value);
630     api_value = g_value_array_get_nth (meta, index);
631
632     if (api_value)
633       ret = g_value_get_string (api_value);
634   }
635   return ret;
636 }
637
638 /**
639  * gst_buffer_pool_config_has_meta:
640  * @config: a #GstBufferPool configuration
641  * @api: a metadata api
642  *
643  * Check if @config contains @api metadata.
644  *
645  * Returns: TRUE if the metadata array contain @api.
646  */
647 gboolean
648 gst_buffer_pool_config_has_meta (GstStructure * config, const gchar * api)
649 {
650   const GValue *value;
651
652   g_return_val_if_fail (config != NULL, 0);
653
654   value = gst_structure_id_get_value (config, GST_QUARK (META));
655   if (value) {
656     GValueArray *array;
657     GValue *api_value;
658     gint i;
659
660     array = (GValueArray *) g_value_get_boxed (value);
661     for (i = 0; i < array->n_values; i++) {
662       api_value = g_value_array_get_nth (array, i);
663       if (!strcmp (api, g_value_get_string (api_value)))
664         return TRUE;
665     }
666   }
667   return FALSE;
668 }
669
670 /**
671  * gst_buffer_pool_config_get:
672  * @config: a #GstBufferPool configuration
673  * @caps: the caps of buffers
674  * @size: the size of each buffer, not including prefix
675  * @min_buffers: the minimum amount of buffers to allocate.
676  * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
677  * @prefix: prefix each buffer with this many bytes
678  * @align: alignment of the buffer data.
679  *
680  * Get the configuration values from @config.
681  */
682 gboolean
683 gst_buffer_pool_config_get (GstStructure * config, const GstCaps ** caps,
684     guint * size, guint * min_buffers, guint * max_buffers, guint * prefix,
685     guint * align)
686 {
687   g_return_val_if_fail (config != NULL, FALSE);
688
689   return gst_structure_id_get (config,
690       GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
691       GST_QUARK (SIZE), G_TYPE_UINT, size,
692       GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
693       GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
694       GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
695       GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
696 }
697
698 static GstFlowReturn
699 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
700     GstBufferPoolParams * params)
701 {
702   GstFlowReturn result;
703   GstBufferPoolClass *pclass;
704   GstBufferPoolPrivate *priv = pool->priv;
705
706   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
707
708   while (TRUE) {
709     if (G_UNLIKELY (g_atomic_int_get (&pool->flushing)))
710       goto flushing;
711
712     /* try to get a buffer from the queue */
713     *buffer = gst_atomic_queue_pop (pool->queue);
714     if (G_LIKELY (*buffer)) {
715       gst_poll_read_control (pool->poll);
716       result = GST_FLOW_OK;
717       GST_LOG_OBJECT (pool, "acquired buffer %p", *buffer);
718       break;
719     }
720
721     /* no buffer */
722     if (priv->max_buffers == 0) {
723       /* no max_buffers, we allocate some more */
724       if (G_LIKELY (pclass->alloc_buffer)) {
725         result = pclass->alloc_buffer (pool, buffer, params);
726       } else
727         result = GST_FLOW_NOT_SUPPORTED;
728       GST_LOG_OBJECT (pool, "alloc buffer %p", *buffer);
729       break;
730     }
731
732     /* check if we need to wait */
733     if (params && (params->flags & GST_BUFFER_POOL_FLAG_DONTWAIT)) {
734       GST_LOG_OBJECT (pool, "no more buffers");
735       result = GST_FLOW_UNEXPECTED;
736       break;
737     }
738
739     /* now wait */
740     GST_LOG_OBJECT (pool, "waiting for free buffers");
741     gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
742   }
743
744   return result;
745
746   /* ERRORS */
747 flushing:
748   {
749     GST_DEBUG_OBJECT (pool, "we are flushing");
750     return GST_FLOW_WRONG_STATE;
751   }
752 }
753
754 static inline void
755 dec_outstanding (GstBufferPool * pool)
756 {
757   if (g_atomic_int_dec_and_test (&pool->outstanding)) {
758     /* all buffers are returned to the pool, see if we need to free them */
759     if (g_atomic_int_get (&pool->flushing)) {
760       /* take the lock so that set_active is not run concurrently */
761       GST_BUFFER_POOL_LOCK (pool);
762       /* recheck the flushing state in the lock, the pool could have been
763        * set to active again */
764       if (g_atomic_int_get (&pool->flushing))
765         do_stop (pool);
766
767       GST_BUFFER_POOL_UNLOCK (pool);
768     }
769   }
770 }
771
772 /**
773  * gst_buffer_pool_acquire_buffer:
774  * @pool: a #GstBufferPool
775  * @buffer: a location for a #GstBuffer
776  * @params: parameters.
777  *
778  * Acquire a buffer from @pool. @buffer should point to a memory location that
779  * can hold a pointer to the new buffer.
780  *
781  * @params can be NULL or contain optional parameters to influence the allocation.
782  *
783  * Returns: a #GstFlowReturn such as GST_FLOW_WRONG_STATE when the pool is
784  * inactive.
785  */
786 GstFlowReturn
787 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
788     GstBufferPoolParams * params)
789 {
790   GstBufferPoolClass *pclass;
791   GstFlowReturn result;
792
793   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), GST_FLOW_ERROR);
794   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
795
796   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
797
798   /* assume we'll have one more outstanding buffer we need to do that so
799    * that concurrent set_active doesn't clear the buffers */
800   g_atomic_int_inc (&pool->outstanding);
801
802   if (G_LIKELY (pclass->acquire_buffer))
803     result = pclass->acquire_buffer (pool, buffer, params);
804   else
805     result = GST_FLOW_NOT_SUPPORTED;
806
807   if (G_LIKELY (result == GST_FLOW_OK)) {
808     /* all buffers from the pool point to the pool and have the refcount of the
809      * pool incremented */
810     (*buffer)->pool = gst_object_ref (pool);
811   } else {
812     dec_outstanding (pool);
813   }
814
815   return result;
816 }
817
818 static void
819 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
820 {
821   /* keep it around in our queue */
822   GST_LOG_OBJECT (pool, "released buffer %p", buffer);
823   gst_atomic_queue_push (pool->queue, buffer);
824   gst_poll_write_control (pool->poll);
825 }
826
827 /**
828  * gst_buffer_pool_release_buffer:
829  * @pool: a #GstBufferPool
830  * @buffer: a #GstBuffer
831  *
832  * Release @buffer to @pool. @buffer should have previously been allocated from
833  * @pool with gst_buffer_pool_acquire_buffer().
834  *
835  * This function is usually called automatically when the last ref on @buffer
836  * disappears.
837  */
838 void
839 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
840 {
841   GstBufferPoolClass *pclass;
842
843   g_return_if_fail (GST_IS_BUFFER_POOL (pool));
844   g_return_if_fail (buffer != NULL);
845
846   /* check that the buffer is ours, all buffers returned to the pool have the
847    * pool member set to NULL and the pool refcount decreased */
848   if (!g_atomic_pointer_compare_and_exchange ((gpointer *) & buffer->pool,
849           pool, NULL))
850     return;
851
852   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
853
854   if (G_LIKELY (pclass->release_buffer))
855     pclass->release_buffer (pool, buffer);
856
857   dec_outstanding (pool);
858
859   /* decrease the refcount that the buffer had to us */
860   gst_object_unref (pool);
861 }