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