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