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