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