meta: add LOCKED flag
[platform/upstream/gstreamer.git] / gst / gstbufferpool.c
1 /* GStreamer
2  * Copyright (C) 2010 Wim Taymans <wim.taymans@gmail.com>
3  *
4  * gstbufferpool.c: GstBufferPool baseclass
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:gstbufferpool
24  * @short_description: Pool for buffers
25  * @see_also: #GstBuffer
26  *
27  * 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   GstAllocator *allocator;
112   GstAllocationParams params;
113 };
114
115 enum
116 {
117   /* add more above */
118   LAST_SIGNAL
119 };
120
121 static void gst_buffer_pool_finalize (GObject * object);
122
123 G_DEFINE_TYPE (GstBufferPool, gst_buffer_pool, GST_TYPE_OBJECT);
124
125 static gboolean default_start (GstBufferPool * pool);
126 static gboolean default_stop (GstBufferPool * pool);
127 static gboolean default_set_config (GstBufferPool * pool,
128     GstStructure * config);
129 static GstFlowReturn default_alloc_buffer (GstBufferPool * pool,
130     GstBuffer ** buffer, GstBufferPoolAcquireParams * params);
131 static GstFlowReturn default_acquire_buffer (GstBufferPool * pool,
132     GstBuffer ** buffer, GstBufferPoolAcquireParams * params);
133 static void default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer,
134     GstBufferPoolAcquireParams * params);
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_allocator_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 /* the default implementation for preallocating the buffers
252  * in the pool */
253 static gboolean
254 default_start (GstBufferPool * pool)
255 {
256   guint i;
257   GstBufferPoolPrivate *priv = pool->priv;
258   GstBufferPoolClass *pclass;
259
260   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
261
262   /* no alloc function, error */
263   if (G_UNLIKELY (pclass->alloc_buffer == NULL))
264     goto no_alloc;
265
266   /* we need to prealloc buffers */
267   for (i = 0; i < priv->min_buffers; i++) {
268     GstBuffer *buffer;
269
270     if (pclass->alloc_buffer (pool, &buffer, NULL) != GST_FLOW_OK)
271       goto alloc_failed;
272
273     gst_buffer_foreach_meta (buffer, mark_meta_pooled, pool);
274     GST_LOG_OBJECT (pool, "prealloced buffer %d: %p", i, buffer);
275     /* release to the queue, we call the vmethod directly, we don't need to do
276      * the other refcount handling right now. */
277     if (G_LIKELY (pclass->release_buffer))
278       pclass->release_buffer (pool, buffer);
279   }
280   return TRUE;
281
282   /* ERRORS */
283 no_alloc:
284   {
285     GST_WARNING_OBJECT (pool, "no alloc function");
286     return FALSE;
287   }
288 alloc_failed:
289   {
290     GST_WARNING_OBJECT (pool, "alloc function failed");
291     return FALSE;
292   }
293 }
294
295 /* must be called with the lock */
296 static gboolean
297 do_start (GstBufferPool * pool)
298 {
299   if (!pool->priv->started) {
300     GstBufferPoolClass *pclass;
301
302     pclass = GST_BUFFER_POOL_GET_CLASS (pool);
303
304     GST_LOG_OBJECT (pool, "starting");
305     /* start the pool, subclasses should allocate buffers and put them
306      * in the queue */
307     if (G_LIKELY (pclass->start)) {
308       if (!pclass->start (pool))
309         return FALSE;
310     }
311     pool->priv->started = TRUE;
312   }
313   return TRUE;
314 }
315
316
317 static void
318 default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
319 {
320   gst_buffer_unref (buffer);
321 }
322
323 /* must be called with the lock */
324 static gboolean
325 default_stop (GstBufferPool * pool)
326 {
327   GstBuffer *buffer;
328   GstBufferPoolClass *pclass;
329
330   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
331
332   /* clear the pool */
333   while ((buffer = gst_atomic_queue_pop (pool->priv->queue))) {
334     GST_LOG_OBJECT (pool, "freeing %p", buffer);
335     gst_poll_read_control (pool->priv->poll);
336
337     if (G_LIKELY (pclass->free_buffer))
338       pclass->free_buffer (pool, buffer);
339   }
340   return TRUE;
341 }
342
343 /* must be called with the lock */
344 static gboolean
345 do_stop (GstBufferPool * pool)
346 {
347   if (pool->priv->started) {
348     GstBufferPoolClass *pclass;
349
350     pclass = GST_BUFFER_POOL_GET_CLASS (pool);
351
352     GST_LOG_OBJECT (pool, "stopping");
353     if (G_LIKELY (pclass->stop)) {
354       if (!pclass->stop (pool))
355         return FALSE;
356     }
357     pool->priv->started = FALSE;
358   }
359   return TRUE;
360 }
361
362 /**
363  * gst_buffer_pool_set_active:
364  * @pool: a #GstBufferPool
365  * @active: the new active state
366  *
367  * Control the active state of @pool. When the pool is active, new calls to
368  * gst_buffer_pool_acquire_buffer() will return with GST_FLOW_FLUSHING.
369  *
370  * Activating the bufferpool will preallocate all resources in the pool based on
371  * the configuration of the pool.
372  *
373  * Deactivating will free the resources again when there are no outstanding
374  * buffers. When there are outstanding buffers, they will be freed as soon as
375  * they are all returned to the pool.
376  *
377  * Returns: %FALSE when the pool was not configured or when preallocation of the
378  * buffers failed.
379  */
380 gboolean
381 gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
382 {
383   gboolean res = TRUE;
384
385   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
386
387   GST_LOG_OBJECT (pool, "active %d", active);
388
389   GST_BUFFER_POOL_LOCK (pool);
390   /* just return if we are already in the right state */
391   if (pool->priv->active == active)
392     goto was_ok;
393
394   /* we need to be configured */
395   if (!pool->priv->configured)
396     goto not_configured;
397
398   if (active) {
399     if (!do_start (pool))
400       goto start_failed;
401
402     /* unset the flushing state now */
403     gst_poll_read_control (pool->priv->poll);
404     g_atomic_int_set (&pool->flushing, 0);
405   } else {
406     gint outstanding;
407
408     /* set to flushing first */
409     g_atomic_int_set (&pool->flushing, 1);
410     gst_poll_write_control (pool->priv->poll);
411
412     /* when all buffers are in the pool, free them. Else they will be
413      * freed when they are released */
414     outstanding = g_atomic_int_get (&pool->priv->outstanding);
415     GST_LOG_OBJECT (pool, "outstanding buffers %d", outstanding);
416     if (outstanding == 0) {
417       if (!do_stop (pool))
418         goto stop_failed;
419     }
420   }
421   pool->priv->active = active;
422   GST_BUFFER_POOL_UNLOCK (pool);
423
424   return res;
425
426 was_ok:
427   {
428     GST_DEBUG_OBJECT (pool, "pool was in the right state");
429     GST_BUFFER_POOL_UNLOCK (pool);
430     return TRUE;
431   }
432 not_configured:
433   {
434     GST_ERROR_OBJECT (pool, "pool was not configured");
435     GST_BUFFER_POOL_UNLOCK (pool);
436     return FALSE;
437   }
438 start_failed:
439   {
440     GST_ERROR_OBJECT (pool, "start failed");
441     GST_BUFFER_POOL_UNLOCK (pool);
442     return FALSE;
443   }
444 stop_failed:
445   {
446     GST_WARNING_OBJECT (pool, "stop failed");
447     GST_BUFFER_POOL_UNLOCK (pool);
448     return FALSE;
449   }
450 }
451
452 /**
453  * gst_buffer_pool_is_active:
454  * @pool: a #GstBufferPool
455  *
456  * Check if @pool is active. A pool can be activated with the
457  * gst_buffer_pool_set_active() call.
458  *
459  * Returns: %TRUE when the pool is active.
460  */
461 gboolean
462 gst_buffer_pool_is_active (GstBufferPool * pool)
463 {
464   gboolean res;
465
466   GST_BUFFER_POOL_LOCK (pool);
467   res = pool->priv->active;
468   GST_BUFFER_POOL_UNLOCK (pool);
469
470   return res;
471 }
472
473 static gboolean
474 default_set_config (GstBufferPool * pool, GstStructure * config)
475 {
476   GstBufferPoolPrivate *priv = pool->priv;
477   GstCaps *caps;
478   guint size, min_buffers, max_buffers;
479   GstAllocator *allocator;
480   GstAllocationParams params;
481
482   /* parse the config and keep around */
483   if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
484           &max_buffers))
485     goto wrong_config;
486
487   if (!gst_buffer_pool_config_get_allocator (config, &allocator, &params))
488     goto wrong_config;
489
490   GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
491
492   priv->size = size;
493   priv->min_buffers = min_buffers;
494   priv->max_buffers = max_buffers;
495
496   if (priv->allocator)
497     gst_allocator_unref (priv->allocator);
498   if ((priv->allocator = allocator))
499     gst_allocator_ref (allocator);
500   priv->params = params;
501
502   return TRUE;
503
504 wrong_config:
505   {
506     GST_WARNING_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
507     return FALSE;
508   }
509 }
510
511 /**
512  * gst_buffer_pool_set_config:
513  * @pool: a #GstBufferPool
514  * @config: (transfer full): a #GstStructure
515  *
516  * Set the configuration of the pool. The pool must be inactive and all buffers
517  * allocated form this pool must be returned or else this function will do
518  * nothing and return FALSE.
519  *
520  * @config is a #GstStructure that contains the configuration parameters for
521  * the pool. A default and mandatory set of parameters can be configured with
522  * gst_buffer_pool_config_set(). This function takes ownership of @config.
523  *
524  * Returns: TRUE when the configuration could be set.
525  */
526 gboolean
527 gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
528 {
529   gboolean result;
530   GstBufferPoolClass *pclass;
531
532   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
533   g_return_val_if_fail (config != NULL, FALSE);
534
535   GST_BUFFER_POOL_LOCK (pool);
536   /* can't change the settings when active */
537   if (pool->priv->active)
538     goto was_active;
539
540   /* we can't change when outstanding buffers */
541   if (g_atomic_int_get (&pool->priv->outstanding) != 0)
542     goto have_outstanding;
543
544   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
545
546   /* set the new config */
547   if (G_LIKELY (pclass->set_config))
548     result = pclass->set_config (pool, config);
549   else
550     result = FALSE;
551
552   if (result) {
553     if (pool->priv->config)
554       gst_structure_free (pool->priv->config);
555     pool->priv->config = config;
556
557     /* now we are configured */
558     pool->priv->configured = TRUE;
559   } else {
560     gst_structure_free (config);
561   }
562   GST_BUFFER_POOL_UNLOCK (pool);
563
564   return result;
565
566   /* ERRORS */
567 was_active:
568   {
569     gst_structure_free (config);
570     GST_WARNING_OBJECT (pool, "can't change config, we are active");
571     GST_BUFFER_POOL_UNLOCK (pool);
572     return FALSE;
573   }
574 have_outstanding:
575   {
576     gst_structure_free (config);
577     GST_WARNING_OBJECT (pool, "can't change config, have outstanding buffers");
578     GST_BUFFER_POOL_UNLOCK (pool);
579     return FALSE;
580   }
581 }
582
583 /**
584  * gst_buffer_pool_get_config:
585  * @pool: a #GstBufferPool
586  *
587  * Get a copy of the current configuration of the pool. This configuration
588  * can either be modified and used for the gst_buffer_pool_set_config() call
589  * or it must be freed after usage.
590  *
591  * Returns: (transfer full): a copy of the current configuration of @pool. use
592  * gst_structure_free() after usage or gst_buffer_pool_set_config().
593  */
594 GstStructure *
595 gst_buffer_pool_get_config (GstBufferPool * pool)
596 {
597   GstStructure *result;
598
599   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
600
601   GST_BUFFER_POOL_UNLOCK (pool);
602   result = gst_structure_copy (pool->priv->config);
603   GST_BUFFER_POOL_UNLOCK (pool);
604
605   return result;
606 }
607
608 static const gchar *empty_option[] = { NULL };
609
610 /**
611  * gst_buffer_pool_get_options:
612  * @pool: a #GstBufferPool
613  *
614  * Get a NULL terminated array of string with supported bufferpool options for
615  * @pool. An option would typically be enabled with
616  * gst_buffer_pool_config_add_option().
617  *
618  * Returns: (array zero-terminated=1) (transfer none): a NULL terminated array of strings.
619  */
620 const gchar **
621 gst_buffer_pool_get_options (GstBufferPool * pool)
622 {
623   GstBufferPoolClass *pclass;
624   const gchar **result;
625
626   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
627
628   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
629
630   if (G_LIKELY (pclass->get_options)) {
631     if ((result = pclass->get_options (pool)) == NULL)
632       goto invalid_result;
633   } else
634     result = empty_option;
635
636   return result;
637
638   /* ERROR */
639 invalid_result:
640   {
641     g_warning ("bufferpool subclass returned NULL options");
642     return empty_option;
643   }
644 }
645
646 /**
647  * gst_buffer_pool_has_option:
648  * @pool: a #GstBufferPool
649  * @option: an option
650  *
651  * Check if the bufferpool supports @option.
652  *
653  * Returns: a NULL terminated array of strings.
654  */
655 gboolean
656 gst_buffer_pool_has_option (GstBufferPool * pool, const gchar * option)
657 {
658   guint i;
659   const gchar **options;
660
661   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
662   g_return_val_if_fail (option != NULL, FALSE);
663
664   options = gst_buffer_pool_get_options (pool);
665
666   for (i = 0; options[i]; i++) {
667     if (g_str_equal (options[i], option))
668       return TRUE;
669   }
670   return FALSE;
671 }
672
673 /**
674  * gst_buffer_pool_config_set_params:
675  * @config: a #GstBufferPool configuration
676  * @caps: caps for the buffers
677  * @size: the size of each buffer, not including prefix and padding
678  * @min_buffers: the minimum amount of buffers to allocate.
679  * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
680  *
681  * Configure @config with the given parameters.
682  */
683 void
684 gst_buffer_pool_config_set_params (GstStructure * config, GstCaps * caps,
685     guint size, guint min_buffers, guint max_buffers)
686 {
687   g_return_if_fail (config != NULL);
688   g_return_if_fail (max_buffers == 0 || min_buffers <= max_buffers);
689
690   gst_structure_id_set (config,
691       GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
692       GST_QUARK (SIZE), G_TYPE_UINT, size,
693       GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
694       GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers, NULL);
695 }
696
697 /**
698  * gst_buffer_pool_config_set_allocator:
699  * @config: a #GstBufferPool configuration
700  * @allocator: a #GstAllocator
701  * @params: #GstAllocationParams
702  *
703  * Set the @allocator and @params on @config.
704  *
705  * One of @allocator and @params can be NULL, but not both. When @allocator
706  * is NULL, the default allocator of the pool will use the values in @param
707  * to perform its allocation. When @param is NULL, the pool will use the
708  * provided allocator with its default #GstAllocationParams.
709  *
710  * A call to gst_buffer_pool_set_config() can update the allocator and params
711  * with the values that it is able to do. Some pools are, for example, not able
712  * to operate with different allocators or cannot allocate with the values
713  * specified in @params. Use gst_buffer_pool_get_config() to get the currently
714  * used values.
715  */
716 void
717 gst_buffer_pool_config_set_allocator (GstStructure * config,
718     GstAllocator * allocator, const GstAllocationParams * params)
719 {
720   g_return_if_fail (config != NULL);
721   g_return_if_fail (allocator != NULL || params != NULL);
722
723   gst_structure_id_set (config,
724       GST_QUARK (ALLOCATOR), GST_TYPE_ALLOCATOR, allocator,
725       GST_QUARK (PARAMS), GST_TYPE_ALLOCATION_PARAMS, params, NULL);
726 }
727
728 /**
729  * gst_buffer_pool_config_add_option:
730  * @config: a #GstBufferPool configuration
731  * @option: an option to add
732  *
733  * Enabled the option in @config. This will instruct the @bufferpool to enable
734  * the specified option on the buffers that it allocates.
735  *
736  * The supported options by @pool can be retrieved with gst_buffer_pool_get_options().
737  */
738 void
739 gst_buffer_pool_config_add_option (GstStructure * config, const gchar * option)
740 {
741   const GValue *value;
742   GValue option_value = { 0, };
743   guint i, len;
744
745   g_return_if_fail (config != NULL);
746
747   value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
748   if (value) {
749     len = gst_value_array_get_size (value);
750     for (i = 0; i < len; ++i) {
751       const GValue *nth_val = gst_value_array_get_value (value, i);
752
753       if (g_str_equal (option, g_value_get_string (nth_val)))
754         return;
755     }
756   } else {
757     GValue new_array_val = { 0, };
758
759     g_value_init (&new_array_val, GST_TYPE_ARRAY);
760     gst_structure_id_take_value (config, GST_QUARK (OPTIONS), &new_array_val);
761     value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
762   }
763   g_value_init (&option_value, G_TYPE_STRING);
764   g_value_set_string (&option_value, option);
765   gst_value_array_append_value ((GValue *) value, &option_value);
766   g_value_unset (&option_value);
767 }
768
769 /**
770  * gst_buffer_pool_config_n_options:
771  * @config: a #GstBufferPool configuration
772  *
773  * Retrieve the number of values currently stored in the
774  * options array of the @config structure.
775  *
776  * Returns: the options array size as a #guint.
777  */
778 guint
779 gst_buffer_pool_config_n_options (GstStructure * config)
780 {
781   const GValue *value;
782   guint size = 0;
783
784   g_return_val_if_fail (config != NULL, 0);
785
786   value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
787   if (value) {
788     size = gst_value_array_get_size (value);
789   }
790   return size;
791 }
792
793 /**
794  * gst_buffer_pool_config_get_option:
795  * @config: a #GstBufferPool configuration
796  * @index: position in the option array to read
797  *
798  * Parse an available @config and get the option
799  * at @index of the options API array.
800  *
801  * Returns: a #gchar of the option at @index.
802  */
803 const gchar *
804 gst_buffer_pool_config_get_option (GstStructure * config, guint index)
805 {
806   const GValue *value;
807   const gchar *ret = NULL;
808
809   g_return_val_if_fail (config != NULL, 0);
810
811   value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
812   if (value) {
813     const GValue *option_value;
814
815     option_value = gst_value_array_get_value (value, index);
816     if (option_value)
817       ret = g_value_get_string (option_value);
818   }
819   return ret;
820 }
821
822 /**
823  * gst_buffer_pool_config_has_option:
824  * @config: a #GstBufferPool configuration
825  * @option: an option
826  *
827  * Check if @config contains @option
828  *
829  * Returns: TRUE if the options array contains @option.
830  */
831 gboolean
832 gst_buffer_pool_config_has_option (GstStructure * config, const gchar * option)
833 {
834   const GValue *value;
835   guint i, len;
836
837   g_return_val_if_fail (config != NULL, 0);
838
839   value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
840   if (value) {
841     len = gst_value_array_get_size (value);
842     for (i = 0; i < len; ++i) {
843       const GValue *nth_val = gst_value_array_get_value (value, i);
844
845       if (g_str_equal (option, g_value_get_string (nth_val)))
846         return TRUE;
847     }
848   }
849   return FALSE;
850 }
851
852 /**
853  * gst_buffer_pool_config_get_params:
854  * @config: (transfer none): a #GstBufferPool configuration
855  * @caps: (out) (transfer none) (allow-none): the caps of buffers
856  * @size: (out) (allow-none): the size of each buffer, not including prefix and padding
857  * @min_buffers: (out) (allow-none): the minimum amount of buffers to allocate.
858  * @max_buffers: (out) (allow-none): the maximum amount of buffers to allocate or 0 for unlimited.
859  *
860  * Get the configuration values from @config.
861  *
862  * Returns: %TRUE if all parameters could be fetched.
863  */
864 gboolean
865 gst_buffer_pool_config_get_params (GstStructure * config, GstCaps ** caps,
866     guint * size, guint * min_buffers, guint * max_buffers)
867 {
868   g_return_val_if_fail (config != NULL, FALSE);
869
870   if (caps) {
871     *caps = g_value_get_boxed (gst_structure_id_get_value (config,
872             GST_QUARK (CAPS)));
873   }
874   return gst_structure_id_get (config,
875       GST_QUARK (SIZE), G_TYPE_UINT, size,
876       GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
877       GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers, NULL);
878 }
879
880 /**
881  * gst_buffer_pool_config_get_allocator:
882  * @config: (transfer none): a #GstBufferPool configuration
883  * @allocator: (transfer none): a #GstAllocator
884  * @params: #GstAllocationParams
885  *
886  * Get the allocator and params from @config.
887  */
888 gboolean
889 gst_buffer_pool_config_get_allocator (GstStructure * config,
890     GstAllocator ** allocator, GstAllocationParams * params)
891 {
892   g_return_val_if_fail (config != NULL, FALSE);
893
894   if (allocator)
895     *allocator = g_value_get_boxed (gst_structure_id_get_value (config,
896             GST_QUARK (ALLOCATOR)));
897   if (params) {
898     GstAllocationParams *p;
899
900     p = g_value_get_boxed (gst_structure_id_get_value (config,
901             GST_QUARK (PARAMS)));
902     if (p) {
903       *params = *p;
904     } else {
905       gst_allocation_params_init (params);
906     }
907   }
908   return TRUE;
909 }
910
911 static GstFlowReturn
912 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
913     GstBufferPoolAcquireParams * params)
914 {
915   GstFlowReturn result;
916   GstBufferPoolClass *pclass;
917   GstBufferPoolPrivate *priv = pool->priv;
918
919   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
920
921   while (TRUE) {
922     if (G_UNLIKELY (GST_BUFFER_POOL_IS_FLUSHING (pool)))
923       goto flushing;
924
925     /* try to get a buffer from the queue */
926     *buffer = gst_atomic_queue_pop (pool->priv->queue);
927     if (G_LIKELY (*buffer)) {
928       gst_poll_read_control (pool->priv->poll);
929       result = GST_FLOW_OK;
930       GST_LOG_OBJECT (pool, "acquired buffer %p", *buffer);
931       break;
932     }
933
934     /* no buffer */
935     if (priv->max_buffers == 0) {
936       /* no max_buffers, we allocate some more */
937       if (G_LIKELY (pclass->alloc_buffer)) {
938         result = pclass->alloc_buffer (pool, buffer, params);
939         if (result == GST_FLOW_OK && *buffer)
940           gst_buffer_foreach_meta (*buffer, mark_meta_pooled, pool);
941         else
942           result = GST_FLOW_ERROR;
943       } else
944         result = GST_FLOW_NOT_SUPPORTED;
945       GST_LOG_OBJECT (pool, "alloc buffer %p", *buffer);
946       break;
947     }
948
949     /* check if we need to wait */
950     if (params && (params->flags & GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT)) {
951       GST_LOG_OBJECT (pool, "no more buffers");
952       result = GST_FLOW_EOS;
953       break;
954     }
955
956     /* now wait */
957     GST_LOG_OBJECT (pool, "waiting for free buffers");
958     gst_poll_wait (pool->priv->poll, GST_CLOCK_TIME_NONE);
959   }
960
961   return result;
962
963   /* ERRORS */
964 flushing:
965   {
966     GST_DEBUG_OBJECT (pool, "we are flushing");
967     return GST_FLOW_FLUSHING;
968   }
969 }
970
971 static inline void
972 dec_outstanding (GstBufferPool * pool)
973 {
974   if (g_atomic_int_dec_and_test (&pool->priv->outstanding)) {
975     /* all buffers are returned to the pool, see if we need to free them */
976     if (GST_BUFFER_POOL_IS_FLUSHING (pool)) {
977       /* take the lock so that set_active is not run concurrently */
978       GST_BUFFER_POOL_LOCK (pool);
979       /* recheck the flushing state in the lock, the pool could have been
980        * set to active again */
981       if (GST_BUFFER_POOL_IS_FLUSHING (pool))
982         do_stop (pool);
983
984       GST_BUFFER_POOL_UNLOCK (pool);
985     }
986   }
987 }
988
989 static gboolean
990 remove_meta_unpooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
991 {
992   if (!GST_META_FLAG_IS_SET (*meta, GST_META_FLAG_POOLED)) {
993     GST_META_FLAG_UNSET (*meta, GST_META_FLAG_LOCKED);
994     *meta = NULL;
995   }
996   return TRUE;
997 }
998
999 static void
1000 default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer,
1001     GstBufferPoolAcquireParams * params)
1002 {
1003   GST_BUFFER_FLAGS (buffer) = 0;
1004
1005   GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
1006   GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
1007   GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
1008   GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
1009   GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
1010
1011   /* remove all metadata without the POOLED flag */
1012   gst_buffer_foreach_meta (buffer, remove_meta_unpooled, pool);
1013 }
1014
1015 /**
1016  * gst_buffer_pool_acquire_buffer:
1017  * @pool: a #GstBufferPool
1018  * @buffer: (out): a location for a #GstBuffer
1019  * @params: (transfer none) (allow-none) parameters.
1020  *
1021  * Acquire a buffer from @pool. @buffer should point to a memory location that
1022  * can hold a pointer to the new buffer.
1023  *
1024  * @params can be NULL or contain optional parameters to influence the allocation.
1025  *
1026  * Returns: a #GstFlowReturn such as GST_FLOW_FLUSHING when the pool is
1027  * inactive.
1028  */
1029 GstFlowReturn
1030 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
1031     GstBufferPoolAcquireParams * params)
1032 {
1033   GstBufferPoolClass *pclass;
1034   GstFlowReturn result;
1035
1036   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), GST_FLOW_ERROR);
1037   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
1038
1039   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
1040
1041   /* assume we'll have one more outstanding buffer we need to do that so
1042    * that concurrent set_active doesn't clear the buffers */
1043   g_atomic_int_inc (&pool->priv->outstanding);
1044
1045   if (G_LIKELY (pclass->acquire_buffer))
1046     result = pclass->acquire_buffer (pool, buffer, params);
1047   else
1048     result = GST_FLOW_NOT_SUPPORTED;
1049
1050   if (G_LIKELY (result == GST_FLOW_OK)) {
1051     /* all buffers from the pool point to the pool and have the refcount of the
1052      * pool incremented */
1053     (*buffer)->pool = gst_object_ref (pool);
1054     /* now reset the buffer when needed */
1055     if (G_LIKELY (pclass->reset_buffer))
1056       pclass->reset_buffer (pool, *buffer, params);
1057   } else {
1058     dec_outstanding (pool);
1059   }
1060
1061   return result;
1062 }
1063
1064 static void
1065 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
1066 {
1067   /* keep it around in our queue */
1068   GST_LOG_OBJECT (pool, "released buffer %p", buffer);
1069   gst_atomic_queue_push (pool->priv->queue, buffer);
1070   gst_poll_write_control (pool->priv->poll);
1071 }
1072
1073 /**
1074  * gst_buffer_pool_release_buffer:
1075  * @pool: a #GstBufferPool
1076  * @buffer: (transfer full): a #GstBuffer
1077  *
1078  * Release @buffer to @pool. @buffer should have previously been allocated from
1079  * @pool with gst_buffer_pool_acquire_buffer().
1080  *
1081  * This function is usually called automatically when the last ref on @buffer
1082  * disappears.
1083  */
1084 void
1085 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
1086 {
1087   GstBufferPoolClass *pclass;
1088
1089   g_return_if_fail (GST_IS_BUFFER_POOL (pool));
1090   g_return_if_fail (buffer != NULL);
1091
1092   /* check that the buffer is ours, all buffers returned to the pool have the
1093    * pool member set to NULL and the pool refcount decreased */
1094   if (!g_atomic_pointer_compare_and_exchange (&buffer->pool, pool, NULL))
1095     return;
1096
1097   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
1098
1099   if (G_LIKELY (pclass->release_buffer))
1100     pclass->release_buffer (pool, buffer);
1101
1102   dec_outstanding (pool);
1103
1104   /* decrease the refcount that the buffer had to us */
1105   gst_object_unref (pool);
1106 }