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