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