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