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