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