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