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