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