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