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