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