bufferpool: only release buffers with writable memory
[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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 /**
23  * SECTION:gstbufferpool
24  * @short_description: Pool for buffers
25  * @see_also: #GstBuffer
26  *
27  * A #GstBufferPool is an object that can be used to pre-allocate and recycle
28  * buffers of the same size and with the same properties.
29  *
30  * A #GstBufferPool is created with gst_buffer_pool_new().
31  *
32  * Once a pool is created, it needs to be configured. A call to
33  * gst_buffer_pool_get_config() returns the current configuration structure from
34  * the pool. With gst_buffer_pool_config_set_params() and
35  * gst_buffer_pool_config_set_allocator() the bufferpool parameters and
36  * allocator can be configured. Other properties can be configured in the pool
37  * depending on the pool implementation.
38  *
39  * A bufferpool can have extra options that can be enabled with
40  * gst_buffer_pool_config_add_option(). The available options can be retrieved
41  * with gst_buffer_pool_get_options(). Some options allow for additional
42  * configuration properties to be set.
43  *
44  * After the configuration structure has been configured,
45  * gst_buffer_pool_set_config() updates the configuration in the pool. This can
46  * fail when the configuration structure is not accepted.
47  *
48  * After the a pool has been configured, it can be activated with
49  * gst_buffer_pool_set_active(). This will preallocate the configured resources
50  * in the pool.
51  *
52  * When the pool is active, gst_buffer_pool_acquire_buffer() can be used to
53  * retrieve a buffer from the pool.
54  *
55  * Buffers allocated from a bufferpool will automatically be returned to the
56  * pool with gst_buffer_pool_release_buffer() when their refcount drops to 0.
57  *
58  * The bufferpool can be deactivated again with gst_buffer_pool_set_active().
59  * All further gst_buffer_pool_acquire_buffer() calls will return an error. When
60  * all buffers are returned to the pool they will be freed.
61  *
62  * Use gst_object_unref() to release the reference to a bufferpool. If the
63  * refcount of the pool reaches 0, the pool will be freed.
64  *
65  * Last reviewed on 2014-01-30 (1.3.0)
66  */
67
68 #include "gst_private.h"
69 #include "glib-compat-private.h"
70
71 #include <errno.h>
72 #ifdef HAVE_UNISTD_H
73 #  include <unistd.h>
74 #endif
75 #include <sys/types.h>
76
77 #include "gstatomicqueue.h"
78 #include "gstpoll.h"
79 #include "gstinfo.h"
80 #include "gstquark.h"
81 #include "gstvalue.h"
82
83 #include "gstbufferpool.h"
84
85 GST_DEBUG_CATEGORY_STATIC (gst_buffer_pool_debug);
86 #define GST_CAT_DEFAULT gst_buffer_pool_debug
87
88 #define GST_BUFFER_POOL_GET_PRIVATE(obj)  \
89    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BUFFER_POOL, GstBufferPoolPrivate))
90
91 #define GST_BUFFER_POOL_LOCK(pool)   (g_rec_mutex_lock(&pool->priv->rec_lock))
92 #define GST_BUFFER_POOL_UNLOCK(pool) (g_rec_mutex_unlock(&pool->priv->rec_lock))
93
94 struct _GstBufferPoolPrivate
95 {
96   GstAtomicQueue *queue;
97   GstPoll *poll;
98
99   GRecMutex rec_lock;
100
101   gboolean started;
102   gboolean active;
103   gint outstanding;             /* number of buffers that are in use */
104
105   gboolean configured;
106   GstStructure *config;
107
108   guint size;
109   guint min_buffers;
110   guint max_buffers;
111   guint cur_buffers;
112   GstAllocator *allocator;
113   GstAllocationParams params;
114 };
115
116 static void gst_buffer_pool_finalize (GObject * object);
117
118 G_DEFINE_TYPE (GstBufferPool, gst_buffer_pool, GST_TYPE_OBJECT);
119
120 static gboolean default_start (GstBufferPool * pool);
121 static gboolean default_stop (GstBufferPool * pool);
122 static gboolean default_set_config (GstBufferPool * pool,
123     GstStructure * config);
124 static GstFlowReturn default_alloc_buffer (GstBufferPool * pool,
125     GstBuffer ** buffer, GstBufferPoolAcquireParams * params);
126 static GstFlowReturn default_acquire_buffer (GstBufferPool * pool,
127     GstBuffer ** buffer, GstBufferPoolAcquireParams * params);
128 static void default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer);
129 static void default_free_buffer (GstBufferPool * pool, GstBuffer * buffer);
130 static void default_release_buffer (GstBufferPool * pool, GstBuffer * buffer);
131
132 static void
133 gst_buffer_pool_class_init (GstBufferPoolClass * klass)
134 {
135   GObjectClass *gobject_class = (GObjectClass *) klass;
136
137   g_type_class_add_private (klass, sizeof (GstBufferPoolPrivate));
138
139   gobject_class->finalize = gst_buffer_pool_finalize;
140
141   klass->start = default_start;
142   klass->stop = default_stop;
143   klass->set_config = default_set_config;
144   klass->acquire_buffer = default_acquire_buffer;
145   klass->reset_buffer = default_reset_buffer;
146   klass->alloc_buffer = default_alloc_buffer;
147   klass->release_buffer = default_release_buffer;
148   klass->free_buffer = default_free_buffer;
149
150   GST_DEBUG_CATEGORY_INIT (gst_buffer_pool_debug, "bufferpool", 0,
151       "bufferpool debug");
152 }
153
154 static void
155 gst_buffer_pool_init (GstBufferPool * pool)
156 {
157   GstBufferPoolPrivate *priv;
158
159   priv = pool->priv = GST_BUFFER_POOL_GET_PRIVATE (pool);
160
161   g_rec_mutex_init (&priv->rec_lock);
162
163   priv->poll = gst_poll_new_timer ();
164   priv->queue = gst_atomic_queue_new (16);
165   pool->flushing = 1;
166   priv->active = FALSE;
167   priv->configured = FALSE;
168   priv->started = FALSE;
169   priv->config = gst_structure_new_id_empty (GST_QUARK (BUFFER_POOL_CONFIG));
170   gst_buffer_pool_config_set_params (priv->config, NULL, 0, 0, 0);
171   priv->allocator = NULL;
172   gst_allocation_params_init (&priv->params);
173   gst_buffer_pool_config_set_allocator (priv->config, priv->allocator,
174       &priv->params);
175   /* 1 control write for flushing */
176   gst_poll_write_control (priv->poll);
177   /* 1 control write for marking that we are not waiting for poll */
178   gst_poll_write_control (priv->poll);
179
180   GST_DEBUG_OBJECT (pool, "created");
181 }
182
183 static void
184 gst_buffer_pool_finalize (GObject * object)
185 {
186   GstBufferPool *pool;
187   GstBufferPoolPrivate *priv;
188
189   pool = GST_BUFFER_POOL_CAST (object);
190   priv = pool->priv;
191
192   GST_DEBUG_OBJECT (pool, "finalize");
193
194   gst_buffer_pool_set_active (pool, FALSE);
195   gst_atomic_queue_unref (priv->queue);
196   gst_poll_free (priv->poll);
197   gst_structure_free (priv->config);
198   g_rec_mutex_clear (&priv->rec_lock);
199   if (priv->allocator)
200     gst_object_unref (priv->allocator);
201
202   G_OBJECT_CLASS (gst_buffer_pool_parent_class)->finalize (object);
203 }
204
205 /**
206  * gst_buffer_pool_new:
207  *
208  * Creates a new #GstBufferPool instance.
209  *
210  * Returns: (transfer floating): a new #GstBufferPool instance
211  */
212 GstBufferPool *
213 gst_buffer_pool_new (void)
214 {
215   GstBufferPool *result;
216
217   result = g_object_newv (GST_TYPE_BUFFER_POOL, 0, NULL);
218   GST_DEBUG_OBJECT (result, "created new buffer pool");
219
220   return result;
221 }
222
223 static GstFlowReturn
224 default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
225     GstBufferPoolAcquireParams * params)
226 {
227   GstBufferPoolPrivate *priv = pool->priv;
228
229   *buffer =
230       gst_buffer_new_allocate (priv->allocator, priv->size, &priv->params);
231
232   return GST_FLOW_OK;
233 }
234
235 static gboolean
236 mark_meta_pooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
237 {
238   GstBufferPool *pool = user_data;
239
240   GST_DEBUG_OBJECT (pool, "marking meta %p as POOLED in buffer %p", *meta,
241       buffer);
242   GST_META_FLAG_SET (*meta, GST_META_FLAG_POOLED);
243   GST_META_FLAG_SET (*meta, GST_META_FLAG_LOCKED);
244
245   return TRUE;
246 }
247
248 static GstFlowReturn
249 do_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
250     GstBufferPoolAcquireParams * params)
251 {
252   GstBufferPoolPrivate *priv = pool->priv;
253   GstFlowReturn result;
254   gint cur_buffers, max_buffers;
255   GstBufferPoolClass *pclass;
256
257   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
258
259   if (G_UNLIKELY (!pclass->alloc_buffer))
260     goto no_function;
261
262   max_buffers = priv->max_buffers;
263
264   /* increment the allocation counter */
265   cur_buffers = g_atomic_int_add (&priv->cur_buffers, 1);
266   if (max_buffers && cur_buffers >= max_buffers)
267     goto max_reached;
268
269   result = pclass->alloc_buffer (pool, buffer, params);
270   if (G_UNLIKELY (result != GST_FLOW_OK))
271     goto alloc_failed;
272
273   /* lock all metadata and mark as pooled, we want this to remain on
274    * the buffer and we want to remove any other metadata that gets added
275    * later */
276   gst_buffer_foreach_meta (*buffer, mark_meta_pooled, pool);
277   /* tag memory, this is how we expect the buffer when it is
278    * released again */
279   GST_BUFFER_FLAG_SET (*buffer, GST_BUFFER_FLAG_TAG_MEMORY);
280
281   GST_LOG_OBJECT (pool, "allocated buffer %d/%d, %p", cur_buffers,
282       max_buffers, buffer);
283
284   return result;
285
286   /* ERRORS */
287 no_function:
288   {
289     GST_ERROR_OBJECT (pool, "no alloc function");
290     return GST_FLOW_NOT_SUPPORTED;
291   }
292 max_reached:
293   {
294     GST_DEBUG_OBJECT (pool, "max buffers reached");
295     g_atomic_int_add (&priv->cur_buffers, -1);
296     return GST_FLOW_EOS;
297   }
298 alloc_failed:
299   {
300     GST_WARNING_OBJECT (pool, "alloc function failed");
301     g_atomic_int_add (&priv->cur_buffers, -1);
302     return result;
303   }
304 }
305
306 /* the default implementation for preallocating the buffers in the pool */
307 static gboolean
308 default_start (GstBufferPool * pool)
309 {
310   guint i;
311   GstBufferPoolPrivate *priv = pool->priv;
312   GstBufferPoolClass *pclass;
313
314   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
315
316   /* we need to prealloc buffers */
317   for (i = 0; i < priv->min_buffers; i++) {
318     GstBuffer *buffer;
319
320     if (do_alloc_buffer (pool, &buffer, NULL) != GST_FLOW_OK)
321       goto alloc_failed;
322
323     /* release to the queue, we call the vmethod directly, we don't need to do
324      * the other refcount handling right now. */
325     if (G_LIKELY (pclass->release_buffer))
326       pclass->release_buffer (pool, buffer);
327   }
328   return TRUE;
329
330   /* ERRORS */
331 alloc_failed:
332   {
333     GST_WARNING_OBJECT (pool, "failed to allocate buffer");
334     return FALSE;
335   }
336 }
337
338 /* must be called with the lock */
339 static gboolean
340 do_start (GstBufferPool * pool)
341 {
342   GstBufferPoolPrivate *priv = pool->priv;
343
344   if (!priv->started) {
345     GstBufferPoolClass *pclass;
346
347     pclass = GST_BUFFER_POOL_GET_CLASS (pool);
348
349     GST_LOG_OBJECT (pool, "starting");
350     /* start the pool, subclasses should allocate buffers and put them
351      * in the queue */
352     if (G_LIKELY (pclass->start)) {
353       if (!pclass->start (pool))
354         return FALSE;
355     }
356     priv->started = TRUE;
357   }
358   return TRUE;
359 }
360
361
362 static void
363 default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
364 {
365   gst_buffer_unref (buffer);
366 }
367
368 static void
369 do_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
370 {
371   GstBufferPoolPrivate *priv;
372   GstBufferPoolClass *pclass;
373
374   priv = pool->priv;
375   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
376
377   g_atomic_int_add (&priv->cur_buffers, -1);
378   GST_LOG_OBJECT (pool, "freeing buffer %p (%u left)", buffer,
379       priv->cur_buffers);
380
381   if (G_LIKELY (pclass->free_buffer))
382     pclass->free_buffer (pool, buffer);
383 }
384
385 /* must be called with the lock */
386 static gboolean
387 default_stop (GstBufferPool * pool)
388 {
389   GstBufferPoolPrivate *priv = pool->priv;
390   GstBuffer *buffer;
391
392   /* clear the pool */
393   while ((buffer = gst_atomic_queue_pop (priv->queue))) {
394     gst_poll_read_control (priv->poll);
395     do_free_buffer (pool, buffer);
396   }
397   return priv->cur_buffers == 0;
398 }
399
400 /* must be called with the lock */
401 static gboolean
402 do_stop (GstBufferPool * pool)
403 {
404   GstBufferPoolPrivate *priv = pool->priv;
405
406   if (priv->started) {
407     GstBufferPoolClass *pclass;
408
409     pclass = GST_BUFFER_POOL_GET_CLASS (pool);
410
411     GST_LOG_OBJECT (pool, "stopping");
412     if (G_LIKELY (pclass->stop)) {
413       if (!pclass->stop (pool))
414         return FALSE;
415     }
416     priv->started = FALSE;
417   }
418   return TRUE;
419 }
420
421 /**
422  * gst_buffer_pool_set_active:
423  * @pool: a #GstBufferPool
424  * @active: the new active state
425  *
426  * Control the active state of @pool. When the pool is inactive, new calls to
427  * gst_buffer_pool_acquire_buffer() will return with %GST_FLOW_FLUSHING.
428  *
429  * Activating the bufferpool will preallocate all resources in the pool based on
430  * the configuration of the pool.
431  *
432  * Deactivating will free the resources again when there are no outstanding
433  * buffers. When there are outstanding buffers, they will be freed as soon as
434  * they are all returned to the pool.
435  *
436  * Returns: %FALSE when the pool was not configured or when preallocation of the
437  * buffers failed.
438  */
439 gboolean
440 gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
441 {
442   gboolean res = TRUE;
443   GstBufferPoolPrivate *priv;
444
445   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
446
447   GST_LOG_OBJECT (pool, "active %d", active);
448
449   priv = pool->priv;
450
451   GST_BUFFER_POOL_LOCK (pool);
452   /* just return if we are already in the right state */
453   if (priv->active == active)
454     goto was_ok;
455
456   /* we need to be configured */
457   if (!priv->configured)
458     goto not_configured;
459
460   if (active) {
461     if (!do_start (pool))
462       goto start_failed;
463
464     /* unset the flushing state now */
465     gst_poll_read_control (priv->poll);
466     g_atomic_int_set (&pool->flushing, 0);
467   } else {
468     gint outstanding;
469
470     /* set to flushing first */
471     g_atomic_int_set (&pool->flushing, 1);
472     gst_poll_write_control (priv->poll);
473
474     /* when all buffers are in the pool, free them. Else they will be
475      * freed when they are released */
476     outstanding = g_atomic_int_get (&priv->outstanding);
477     GST_LOG_OBJECT (pool, "outstanding buffers %d", outstanding);
478     if (outstanding == 0) {
479       if (!do_stop (pool))
480         goto stop_failed;
481     }
482   }
483   priv->active = active;
484   GST_BUFFER_POOL_UNLOCK (pool);
485
486   return res;
487
488 was_ok:
489   {
490     GST_DEBUG_OBJECT (pool, "pool was in the right state");
491     GST_BUFFER_POOL_UNLOCK (pool);
492     return TRUE;
493   }
494 not_configured:
495   {
496     GST_ERROR_OBJECT (pool, "pool was not configured");
497     GST_BUFFER_POOL_UNLOCK (pool);
498     return FALSE;
499   }
500 start_failed:
501   {
502     GST_ERROR_OBJECT (pool, "start failed");
503     GST_BUFFER_POOL_UNLOCK (pool);
504     return FALSE;
505   }
506 stop_failed:
507   {
508     GST_WARNING_OBJECT (pool, "stop failed");
509     GST_BUFFER_POOL_UNLOCK (pool);
510     return FALSE;
511   }
512 }
513
514 /**
515  * gst_buffer_pool_is_active:
516  * @pool: a #GstBufferPool
517  *
518  * Check if @pool is active. A pool can be activated with the
519  * gst_buffer_pool_set_active() call.
520  *
521  * Returns: %TRUE when the pool is active.
522  */
523 gboolean
524 gst_buffer_pool_is_active (GstBufferPool * pool)
525 {
526   gboolean res;
527
528   GST_BUFFER_POOL_LOCK (pool);
529   res = pool->priv->active;
530   GST_BUFFER_POOL_UNLOCK (pool);
531
532   return res;
533 }
534
535 static gboolean
536 default_set_config (GstBufferPool * pool, GstStructure * config)
537 {
538   GstBufferPoolPrivate *priv = pool->priv;
539   GstCaps *caps;
540   guint size, min_buffers, max_buffers;
541   GstAllocator *allocator;
542   GstAllocationParams params;
543
544   /* parse the config and keep around */
545   if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
546           &max_buffers))
547     goto wrong_config;
548
549   if (!gst_buffer_pool_config_get_allocator (config, &allocator, &params))
550     goto wrong_config;
551
552   GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
553
554   priv->size = size;
555   priv->min_buffers = min_buffers;
556   priv->max_buffers = max_buffers;
557   priv->cur_buffers = 0;
558
559   if (priv->allocator)
560     gst_object_unref (priv->allocator);
561   if ((priv->allocator = allocator))
562     gst_object_ref (allocator);
563   priv->params = params;
564
565   return TRUE;
566
567 wrong_config:
568   {
569     GST_WARNING_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
570     return FALSE;
571   }
572 }
573
574 /**
575  * gst_buffer_pool_set_config:
576  * @pool: a #GstBufferPool
577  * @config: (transfer full): a #GstStructure
578  *
579  * Set the configuration of the pool. The pool must be inactive and all buffers
580  * allocated form this pool must be returned or else this function will do
581  * nothing and return FALSE.
582  *
583  * @config is a #GstStructure that contains the configuration parameters for
584  * the pool. A default and mandatory set of parameters can be configured with
585  * gst_buffer_pool_config_set_params(), gst_buffer_pool_config_set_allocator()
586  * and gst_buffer_pool_config_add_option().
587  *
588  * If the parameters in @config can not be set exactly, this function returns
589  * %FALSE and will try to update as much state as possible. The new state can
590  * then be retrieved and refined with gst_buffer_pool_get_config().
591  *
592  * This function takes ownership of @config.
593  *
594  * Returns: %TRUE when the configuration could be set.
595  */
596 gboolean
597 gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
598 {
599   gboolean result;
600   GstBufferPoolClass *pclass;
601   GstBufferPoolPrivate *priv;
602
603   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
604   g_return_val_if_fail (config != NULL, FALSE);
605
606   priv = pool->priv;
607
608   GST_BUFFER_POOL_LOCK (pool);
609   /* can't change the settings when active */
610   if (priv->active)
611     goto was_active;
612
613   /* we can't change when outstanding buffers */
614   if (g_atomic_int_get (&priv->outstanding) != 0)
615     goto have_outstanding;
616
617   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
618
619   /* set the new config */
620   if (G_LIKELY (pclass->set_config))
621     result = pclass->set_config (pool, config);
622   else
623     result = FALSE;
624
625   if (result) {
626     if (priv->config)
627       gst_structure_free (priv->config);
628     priv->config = config;
629
630     /* now we are configured */
631     priv->configured = TRUE;
632   } else {
633     gst_structure_free (config);
634   }
635   GST_BUFFER_POOL_UNLOCK (pool);
636
637   return result;
638
639   /* ERRORS */
640 was_active:
641   {
642     gst_structure_free (config);
643     GST_WARNING_OBJECT (pool, "can't change config, we are active");
644     GST_BUFFER_POOL_UNLOCK (pool);
645     return FALSE;
646   }
647 have_outstanding:
648   {
649     gst_structure_free (config);
650     GST_WARNING_OBJECT (pool, "can't change config, have outstanding buffers");
651     GST_BUFFER_POOL_UNLOCK (pool);
652     return FALSE;
653   }
654 }
655
656 /**
657  * gst_buffer_pool_get_config:
658  * @pool: a #GstBufferPool
659  *
660  * Get a copy of the current configuration of the pool. This configuration
661  * can either be modified and used for the gst_buffer_pool_set_config() call
662  * or it must be freed after usage.
663  *
664  * Returns: (transfer full): a copy of the current configuration of @pool. use
665  * gst_structure_free() after usage or gst_buffer_pool_set_config().
666  */
667 GstStructure *
668 gst_buffer_pool_get_config (GstBufferPool * pool)
669 {
670   GstStructure *result;
671
672   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
673
674   GST_BUFFER_POOL_LOCK (pool);
675   result = gst_structure_copy (pool->priv->config);
676   GST_BUFFER_POOL_UNLOCK (pool);
677
678   return result;
679 }
680
681 static const gchar *empty_option[] = { NULL };
682
683 /**
684  * gst_buffer_pool_get_options:
685  * @pool: a #GstBufferPool
686  *
687  * Get a %NULL terminated array of string with supported bufferpool options for
688  * @pool. An option would typically be enabled with
689  * gst_buffer_pool_config_add_option().
690  *
691  * Returns: (array zero-terminated=1) (transfer none): a %NULL terminated array
692  *          of strings.
693  */
694 const gchar **
695 gst_buffer_pool_get_options (GstBufferPool * pool)
696 {
697   GstBufferPoolClass *pclass;
698   const gchar **result;
699
700   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
701
702   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
703
704   if (G_LIKELY (pclass->get_options)) {
705     if ((result = pclass->get_options (pool)) == NULL)
706       goto invalid_result;
707   } else
708     result = empty_option;
709
710   return result;
711
712   /* ERROR */
713 invalid_result:
714   {
715     g_warning ("bufferpool subclass returned NULL options");
716     return empty_option;
717   }
718 }
719
720 /**
721  * gst_buffer_pool_has_option:
722  * @pool: a #GstBufferPool
723  * @option: an option
724  *
725  * Check if the bufferpool supports @option.
726  *
727  * Returns: a %NULL terminated array of strings.
728  */
729 gboolean
730 gst_buffer_pool_has_option (GstBufferPool * pool, const gchar * option)
731 {
732   guint i;
733   const gchar **options;
734
735   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
736   g_return_val_if_fail (option != NULL, FALSE);
737
738   options = gst_buffer_pool_get_options (pool);
739
740   for (i = 0; options[i]; i++) {
741     if (g_str_equal (options[i], option))
742       return TRUE;
743   }
744   return FALSE;
745 }
746
747 /**
748  * gst_buffer_pool_config_set_params:
749  * @config: a #GstBufferPool configuration
750  * @caps: caps for the buffers
751  * @size: the size of each buffer, not including prefix and padding
752  * @min_buffers: the minimum amount of buffers to allocate.
753  * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
754  *
755  * Configure @config with the given parameters.
756  */
757 void
758 gst_buffer_pool_config_set_params (GstStructure * config, GstCaps * caps,
759     guint size, guint min_buffers, guint max_buffers)
760 {
761   g_return_if_fail (config != NULL);
762   g_return_if_fail (max_buffers == 0 || min_buffers <= max_buffers);
763   g_return_if_fail (caps == NULL || gst_caps_is_fixed (caps));
764
765   gst_structure_id_set (config,
766       GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
767       GST_QUARK (SIZE), G_TYPE_UINT, size,
768       GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
769       GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers, NULL);
770 }
771
772 /**
773  * gst_buffer_pool_config_set_allocator:
774  * @config: a #GstBufferPool configuration
775  * @allocator: a #GstAllocator
776  * @params: #GstAllocationParams
777  *
778  * Set the @allocator and @params on @config.
779  *
780  * One of @allocator and @params can be %NULL, but not both. When @allocator
781  * is %NULL, the default allocator of the pool will use the values in @param
782  * to perform its allocation. When @param is %NULL, the pool will use the
783  * provided @allocator with its default #GstAllocationParams.
784  *
785  * A call to gst_buffer_pool_set_config() can update the allocator and params
786  * with the values that it is able to do. Some pools are, for example, not able
787  * to operate with different allocators or cannot allocate with the values
788  * specified in @params. Use gst_buffer_pool_get_config() to get the currently
789  * used values.
790  */
791 void
792 gst_buffer_pool_config_set_allocator (GstStructure * config,
793     GstAllocator * allocator, const GstAllocationParams * params)
794 {
795   g_return_if_fail (config != NULL);
796   g_return_if_fail (allocator != NULL || params != NULL);
797
798   gst_structure_id_set (config,
799       GST_QUARK (ALLOCATOR), GST_TYPE_ALLOCATOR, allocator,
800       GST_QUARK (PARAMS), GST_TYPE_ALLOCATION_PARAMS, params, NULL);
801 }
802
803 /**
804  * gst_buffer_pool_config_add_option:
805  * @config: a #GstBufferPool configuration
806  * @option: an option to add
807  *
808  * Enabled the option in @config. This will instruct the @bufferpool to enable
809  * the specified option on the buffers that it allocates.
810  *
811  * The supported options by @pool can be retrieved with gst_buffer_pool_get_options().
812  */
813 void
814 gst_buffer_pool_config_add_option (GstStructure * config, const gchar * option)
815 {
816   const GValue *value;
817   GValue option_value = { 0, };
818   guint i, len;
819
820   g_return_if_fail (config != NULL);
821
822   value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
823   if (value) {
824     len = gst_value_array_get_size (value);
825     for (i = 0; i < len; ++i) {
826       const GValue *nth_val = gst_value_array_get_value (value, i);
827
828       if (g_str_equal (option, g_value_get_string (nth_val)))
829         return;
830     }
831   } else {
832     GValue new_array_val = { 0, };
833
834     g_value_init (&new_array_val, GST_TYPE_ARRAY);
835     gst_structure_id_take_value (config, GST_QUARK (OPTIONS), &new_array_val);
836     value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
837   }
838   g_value_init (&option_value, G_TYPE_STRING);
839   g_value_set_string (&option_value, option);
840   gst_value_array_append_and_take_value ((GValue *) value, &option_value);
841 }
842
843 /**
844  * gst_buffer_pool_config_n_options:
845  * @config: a #GstBufferPool configuration
846  *
847  * Retrieve the number of values currently stored in the options array of the
848  * @config structure.
849  *
850  * Returns: the options array size as a #guint.
851  */
852 guint
853 gst_buffer_pool_config_n_options (GstStructure * config)
854 {
855   const GValue *value;
856   guint size = 0;
857
858   g_return_val_if_fail (config != NULL, 0);
859
860   value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
861   if (value) {
862     size = gst_value_array_get_size (value);
863   }
864   return size;
865 }
866
867 /**
868  * gst_buffer_pool_config_get_option:
869  * @config: a #GstBufferPool configuration
870  * @index: position in the option array to read
871  *
872  * Parse an available @config and get the option at @index of the options API
873  * array.
874  *
875  * Returns: a #gchar of the option at @index.
876  */
877 const gchar *
878 gst_buffer_pool_config_get_option (GstStructure * config, guint index)
879 {
880   const GValue *value;
881   const gchar *ret = NULL;
882
883   g_return_val_if_fail (config != NULL, 0);
884
885   value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
886   if (value) {
887     const GValue *option_value;
888
889     option_value = gst_value_array_get_value (value, index);
890     if (option_value)
891       ret = g_value_get_string (option_value);
892   }
893   return ret;
894 }
895
896 /**
897  * gst_buffer_pool_config_has_option:
898  * @config: a #GstBufferPool configuration
899  * @option: an option
900  *
901  * Check if @config contains @option.
902  *
903  * Returns: %TRUE if the options array contains @option.
904  */
905 gboolean
906 gst_buffer_pool_config_has_option (GstStructure * config, const gchar * option)
907 {
908   const GValue *value;
909   guint i, len;
910
911   g_return_val_if_fail (config != NULL, 0);
912
913   value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
914   if (value) {
915     len = gst_value_array_get_size (value);
916     for (i = 0; i < len; ++i) {
917       const GValue *nth_val = gst_value_array_get_value (value, i);
918
919       if (g_str_equal (option, g_value_get_string (nth_val)))
920         return TRUE;
921     }
922   }
923   return FALSE;
924 }
925
926 /**
927  * gst_buffer_pool_config_get_params:
928  * @config: (transfer none): a #GstBufferPool configuration
929  * @caps: (out) (transfer none) (allow-none): the caps of buffers
930  * @size: (out) (allow-none): the size of each buffer, not including prefix and padding
931  * @min_buffers: (out) (allow-none): the minimum amount of buffers to allocate.
932  * @max_buffers: (out) (allow-none): the maximum amount of buffers to allocate or 0 for unlimited.
933  *
934  * Get the configuration values from @config.
935  *
936  * Returns: %TRUE if all parameters could be fetched.
937  */
938 gboolean
939 gst_buffer_pool_config_get_params (GstStructure * config, GstCaps ** caps,
940     guint * size, guint * min_buffers, guint * max_buffers)
941 {
942   g_return_val_if_fail (config != NULL, FALSE);
943
944   if (caps) {
945     *caps = g_value_get_boxed (gst_structure_id_get_value (config,
946             GST_QUARK (CAPS)));
947   }
948   return gst_structure_id_get (config,
949       GST_QUARK (SIZE), G_TYPE_UINT, size,
950       GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
951       GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers, NULL);
952 }
953
954 /**
955  * gst_buffer_pool_config_get_allocator:
956  * @config: (transfer none): a #GstBufferPool configuration
957  * @allocator: (transfer none): a #GstAllocator
958  * @params: #GstAllocationParams
959  *
960  * Get the @allocator and @params from @config.
961  *
962  * Returns: %TRUE, if the values are set. 
963  */
964 gboolean
965 gst_buffer_pool_config_get_allocator (GstStructure * config,
966     GstAllocator ** allocator, GstAllocationParams * params)
967 {
968   g_return_val_if_fail (config != NULL, FALSE);
969
970   if (allocator)
971     *allocator = g_value_get_object (gst_structure_id_get_value (config,
972             GST_QUARK (ALLOCATOR)));
973   if (params) {
974     GstAllocationParams *p;
975
976     p = g_value_get_boxed (gst_structure_id_get_value (config,
977             GST_QUARK (PARAMS)));
978     if (p) {
979       *params = *p;
980     } else {
981       gst_allocation_params_init (params);
982     }
983   }
984   return TRUE;
985 }
986
987 static GstFlowReturn
988 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
989     GstBufferPoolAcquireParams * params)
990 {
991   GstFlowReturn result;
992   GstBufferPoolPrivate *priv = pool->priv;
993
994   while (TRUE) {
995     if (G_UNLIKELY (GST_BUFFER_POOL_IS_FLUSHING (pool)))
996       goto flushing;
997
998     /* try to get a buffer from the queue */
999     *buffer = gst_atomic_queue_pop (priv->queue);
1000     if (G_LIKELY (*buffer)) {
1001       gst_poll_read_control (priv->poll);
1002       result = GST_FLOW_OK;
1003       GST_LOG_OBJECT (pool, "acquired buffer %p", *buffer);
1004       break;
1005     }
1006
1007     /* no buffer, try to allocate some more */
1008     GST_LOG_OBJECT (pool, "no buffer, trying to allocate");
1009     result = do_alloc_buffer (pool, buffer, NULL);
1010     if (G_LIKELY (result == GST_FLOW_OK))
1011       /* we have a buffer, return it */
1012       break;
1013
1014     if (G_UNLIKELY (result != GST_FLOW_EOS))
1015       /* something went wrong, return error */
1016       break;
1017
1018     /* check if we need to wait */
1019     if (params && (params->flags & GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT)) {
1020       GST_LOG_OBJECT (pool, "no more buffers");
1021       break;
1022     }
1023
1024     /* now we release the control socket, we wait for a buffer release or
1025      * flushing */
1026     gst_poll_read_control (pool->priv->poll);
1027     GST_LOG_OBJECT (pool, "waiting for free buffers or flushing");
1028     gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
1029     gst_poll_write_control (pool->priv->poll);
1030   }
1031
1032   return result;
1033
1034   /* ERRORS */
1035 flushing:
1036   {
1037     GST_DEBUG_OBJECT (pool, "we are flushing");
1038     return GST_FLOW_FLUSHING;
1039   }
1040 }
1041
1042 static inline void
1043 dec_outstanding (GstBufferPool * pool)
1044 {
1045   if (g_atomic_int_dec_and_test (&pool->priv->outstanding)) {
1046     /* all buffers are returned to the pool, see if we need to free them */
1047     if (GST_BUFFER_POOL_IS_FLUSHING (pool)) {
1048       /* take the lock so that set_active is not run concurrently */
1049       GST_BUFFER_POOL_LOCK (pool);
1050       /* recheck the flushing state in the lock, the pool could have been
1051        * set to active again */
1052       if (GST_BUFFER_POOL_IS_FLUSHING (pool))
1053         do_stop (pool);
1054
1055       GST_BUFFER_POOL_UNLOCK (pool);
1056     }
1057   }
1058 }
1059
1060 static gboolean
1061 remove_meta_unpooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
1062 {
1063   if (!GST_META_FLAG_IS_SET (*meta, GST_META_FLAG_POOLED)) {
1064     GST_META_FLAG_UNSET (*meta, GST_META_FLAG_LOCKED);
1065     *meta = NULL;
1066   }
1067   return TRUE;
1068 }
1069
1070 static void
1071 default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer)
1072 {
1073   GST_BUFFER_FLAGS (buffer) &= GST_BUFFER_FLAG_TAG_MEMORY;
1074
1075   GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
1076   GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
1077   GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
1078   GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
1079   GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
1080
1081   /* remove all metadata without the POOLED flag */
1082   gst_buffer_foreach_meta (buffer, remove_meta_unpooled, pool);
1083 }
1084
1085 /**
1086  * gst_buffer_pool_acquire_buffer:
1087  * @pool: a #GstBufferPool
1088  * @buffer: (out): a location for a #GstBuffer
1089  * @params: (transfer none) (allow-none) parameters.
1090  *
1091  * Acquire a buffer from @pool. @buffer should point to a memory location that
1092  * can hold a pointer to the new buffer.
1093  *
1094  * @params can be %NULL or contain optional parameters to influence the
1095  * allocation.
1096  *
1097  * Returns: a #GstFlowReturn such as %GST_FLOW_FLUSHING when the pool is
1098  * inactive.
1099  */
1100 GstFlowReturn
1101 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
1102     GstBufferPoolAcquireParams * params)
1103 {
1104   GstBufferPoolClass *pclass;
1105   GstFlowReturn result;
1106
1107   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), GST_FLOW_ERROR);
1108   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
1109
1110   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
1111
1112   /* assume we'll have one more outstanding buffer we need to do that so
1113    * that concurrent set_active doesn't clear the buffers */
1114   g_atomic_int_inc (&pool->priv->outstanding);
1115
1116   if (G_LIKELY (pclass->acquire_buffer))
1117     result = pclass->acquire_buffer (pool, buffer, params);
1118   else
1119     result = GST_FLOW_NOT_SUPPORTED;
1120
1121   if (G_LIKELY (result == GST_FLOW_OK)) {
1122     /* all buffers from the pool point to the pool and have the refcount of the
1123      * pool incremented */
1124     (*buffer)->pool = gst_object_ref (pool);
1125   } else {
1126     dec_outstanding (pool);
1127   }
1128
1129   return result;
1130 }
1131
1132 static void
1133 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
1134 {
1135   GST_LOG_OBJECT (pool, "released buffer %p %d", buffer,
1136       GST_MINI_OBJECT_FLAGS (buffer));
1137
1138   /* memory should be untouched */
1139   if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY))
1140     goto discard;
1141
1142   /* all memory should be exclusive to this buffer (and thus be writable) */
1143   if (!gst_buffer_is_all_memory_writable (buffer))
1144     goto discard;
1145
1146   /* keep it around in our queue */
1147   gst_atomic_queue_push (pool->priv->queue, buffer);
1148   gst_poll_write_control (pool->priv->poll);
1149
1150   return;
1151
1152 discard:
1153   {
1154     do_free_buffer (pool, buffer);
1155     return;
1156   }
1157 }
1158
1159 /**
1160  * gst_buffer_pool_release_buffer:
1161  * @pool: a #GstBufferPool
1162  * @buffer: (transfer full): a #GstBuffer
1163  *
1164  * Release @buffer to @pool. @buffer should have previously been allocated from
1165  * @pool with gst_buffer_pool_acquire_buffer().
1166  *
1167  * This function is usually called automatically when the last ref on @buffer
1168  * disappears.
1169  */
1170 void
1171 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
1172 {
1173   GstBufferPoolClass *pclass;
1174
1175   g_return_if_fail (GST_IS_BUFFER_POOL (pool));
1176   g_return_if_fail (buffer != NULL);
1177
1178   /* check that the buffer is ours, all buffers returned to the pool have the
1179    * pool member set to NULL and the pool refcount decreased */
1180   if (!g_atomic_pointer_compare_and_exchange (&buffer->pool, pool, NULL))
1181     return;
1182
1183   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
1184
1185   /* reset the buffer when needed */
1186   if (G_LIKELY (pclass->reset_buffer))
1187     pclass->reset_buffer (pool, buffer);
1188
1189   if (G_LIKELY (pclass->release_buffer))
1190     pclass->release_buffer (pool, buffer);
1191
1192   dec_outstanding (pool);
1193
1194   /* decrease the refcount that the buffer had to us */
1195   gst_object_unref (pool);
1196 }