Merge branch 'master' into 0.11
[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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:gstbufferpool
24  * @short_description: Pool for buffers
25  * @see_also: #GstBuffer
26  *
27  */
28
29 #include "gst_private.h"
30 #include "glib-compat-private.h"
31
32 #include <errno.h>
33 #ifdef HAVE_UNISTD_H
34 #  include <unistd.h>
35 #endif
36 #include <sys/types.h>
37
38 #include "gstatomicqueue.h"
39 #include "gstpoll.h"
40 #include "gstinfo.h"
41 #include "gstquark.h"
42 #include "gstvalue.h"
43
44 #include "gstbufferpool.h"
45
46 GST_DEBUG_CATEGORY_STATIC (gst_buffer_pool_debug);
47 #define GST_CAT_DEFAULT gst_buffer_pool_debug
48
49 #define GST_BUFFER_POOL_GET_PRIVATE(obj)  \
50    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BUFFER_POOL, GstBufferPoolPrivate))
51
52 #define GST_BUFFER_POOL_LOCK(pool)   (g_rec_mutex_lock(&pool->priv->rec_lock))
53 #define GST_BUFFER_POOL_UNLOCK(pool) (g_rec_mutex_unlock(&pool->priv->rec_lock))
54
55 struct _GstBufferPoolPrivate
56 {
57   GstAtomicQueue *queue;
58   GstPoll *poll;
59
60   GRecMutex rec_lock;
61
62   gboolean started;
63   gboolean active;
64   gint outstanding;
65
66   gboolean configured;
67   GstStructure *config;
68
69   guint size;
70   guint min_buffers;
71   guint max_buffers;
72   guint prefix;
73   guint align;
74 };
75
76 enum
77 {
78   /* add more above */
79   LAST_SIGNAL
80 };
81
82 static void gst_buffer_pool_finalize (GObject * object);
83
84 G_DEFINE_TYPE (GstBufferPool, gst_buffer_pool, GST_TYPE_OBJECT);
85
86 static gboolean default_start (GstBufferPool * pool);
87 static gboolean default_stop (GstBufferPool * pool);
88 static gboolean default_set_config (GstBufferPool * pool,
89     GstStructure * config);
90 static GstFlowReturn default_alloc_buffer (GstBufferPool * pool,
91     GstBuffer ** buffer, GstBufferPoolParams * params);
92 static GstFlowReturn default_acquire_buffer (GstBufferPool * pool,
93     GstBuffer ** buffer, GstBufferPoolParams * params);
94 static void default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer,
95     GstBufferPoolParams * params);
96 static void default_free_buffer (GstBufferPool * pool, GstBuffer * buffer);
97 static void default_release_buffer (GstBufferPool * pool, GstBuffer * buffer);
98
99 static void
100 gst_buffer_pool_class_init (GstBufferPoolClass * klass)
101 {
102   GObjectClass *gobject_class = (GObjectClass *) klass;
103
104   g_type_class_add_private (klass, sizeof (GstBufferPoolPrivate));
105
106   gobject_class->finalize = gst_buffer_pool_finalize;
107
108   klass->start = default_start;
109   klass->stop = default_stop;
110   klass->set_config = default_set_config;
111   klass->acquire_buffer = default_acquire_buffer;
112   klass->reset_buffer = default_reset_buffer;
113   klass->alloc_buffer = default_alloc_buffer;
114   klass->release_buffer = default_release_buffer;
115   klass->free_buffer = default_free_buffer;
116
117   GST_DEBUG_CATEGORY_INIT (gst_buffer_pool_debug, "bufferpool", 0,
118       "bufferpool debug");
119 }
120
121 static void
122 gst_buffer_pool_init (GstBufferPool * pool)
123 {
124   pool->priv = GST_BUFFER_POOL_GET_PRIVATE (pool);
125
126   g_rec_mutex_init (&pool->priv->rec_lock);
127
128   pool->priv->poll = gst_poll_new_timer ();
129   pool->priv->queue = gst_atomic_queue_new (10);
130   pool->flushing = 1;
131   pool->priv->active = FALSE;
132   pool->priv->configured = FALSE;
133   pool->priv->started = FALSE;
134   pool->priv->config =
135       gst_structure_new_id_empty (GST_QUARK (BUFFER_POOL_CONFIG));
136   gst_buffer_pool_config_set (pool->priv->config, NULL, 0, 0, 0, 0, 0);
137   gst_poll_write_control (pool->priv->poll);
138
139   GST_DEBUG_OBJECT (pool, "created");
140 }
141
142 static void
143 gst_buffer_pool_finalize (GObject * object)
144 {
145   GstBufferPool *pool;
146
147   pool = GST_BUFFER_POOL_CAST (object);
148
149   GST_DEBUG_OBJECT (pool, "finalize");
150
151   gst_buffer_pool_set_active (pool, FALSE);
152   gst_atomic_queue_unref (pool->priv->queue);
153   gst_poll_free (pool->priv->poll);
154   gst_structure_free (pool->priv->config);
155   g_rec_mutex_clear (&pool->priv->rec_lock);
156
157   G_OBJECT_CLASS (gst_buffer_pool_parent_class)->finalize (object);
158 }
159
160 /**
161  * gst_buffer_pool_new:
162  *
163  * Creates a new #GstBufferPool instance.
164  *
165  * Returns: (transfer full): a new #GstBufferPool instance
166  */
167 GstBufferPool *
168 gst_buffer_pool_new (void)
169 {
170   GstBufferPool *result;
171
172   result = g_object_newv (GST_TYPE_BUFFER_POOL, 0, NULL);
173   GST_DEBUG_OBJECT (result, "created new buffer pool");
174
175   return result;
176 }
177
178 static GstFlowReturn
179 default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
180     GstBufferPoolParams * params)
181 {
182   GstBufferPoolPrivate *priv = pool->priv;
183   GstMemory *mem;
184
185   *buffer = gst_buffer_new ();
186
187   mem = gst_allocator_alloc (NULL, priv->size + priv->prefix, priv->align);
188   gst_memory_resize (mem, priv->prefix, priv->size);
189   gst_buffer_take_memory (*buffer, -1, mem);
190
191   return GST_FLOW_OK;
192 }
193
194 static gboolean
195 mark_meta_pooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
196 {
197   GST_META_FLAG_SET (*meta, GST_META_FLAG_POOLED);
198
199   return TRUE;
200 }
201
202 /* the default implementation for preallocating the buffers
203  * in the pool */
204 static gboolean
205 default_start (GstBufferPool * pool)
206 {
207   guint i;
208   GstBufferPoolPrivate *priv = pool->priv;
209   GstBufferPoolClass *pclass;
210
211   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
212
213   /* no alloc function, error */
214   if (G_UNLIKELY (pclass->alloc_buffer == NULL))
215     goto no_alloc;
216
217   /* we need to prealloc buffers */
218   for (i = 0; i < priv->min_buffers; i++) {
219     GstBuffer *buffer;
220
221     if (pclass->alloc_buffer (pool, &buffer, NULL) != GST_FLOW_OK)
222       goto alloc_failed;
223
224     gst_buffer_foreach_meta (buffer, mark_meta_pooled, pool);
225     GST_LOG_OBJECT (pool, "prealloced buffer %d: %p", i, buffer);
226     /* release to the queue, we call the vmethod directly, we don't need to do
227      * the other refcount handling right now. */
228     if (G_LIKELY (pclass->release_buffer))
229       pclass->release_buffer (pool, buffer);
230   }
231   return TRUE;
232
233   /* ERRORS */
234 no_alloc:
235   {
236     GST_WARNING_OBJECT (pool, "no alloc function");
237     return FALSE;
238   }
239 alloc_failed:
240   {
241     GST_WARNING_OBJECT (pool, "alloc function failed");
242     return FALSE;
243   }
244 }
245
246 /* must be called with the lock */
247 static gboolean
248 do_start (GstBufferPool * pool)
249 {
250   if (!pool->priv->started) {
251     GstBufferPoolClass *pclass;
252
253     pclass = GST_BUFFER_POOL_GET_CLASS (pool);
254
255     GST_LOG_OBJECT (pool, "starting");
256     /* start the pool, subclasses should allocate buffers and put them
257      * in the queue */
258     if (G_LIKELY (pclass->start)) {
259       if (!pclass->start (pool))
260         return FALSE;
261     }
262     pool->priv->started = TRUE;
263   }
264   return TRUE;
265 }
266
267
268 static void
269 default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
270 {
271   gst_buffer_unref (buffer);
272 }
273
274 /* must be called with the lock */
275 static gboolean
276 default_stop (GstBufferPool * pool)
277 {
278   GstBuffer *buffer;
279   GstBufferPoolClass *pclass;
280
281   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
282
283   /* clear the pool */
284   while ((buffer = gst_atomic_queue_pop (pool->priv->queue))) {
285     GST_LOG_OBJECT (pool, "freeing %p", buffer);
286     gst_poll_read_control (pool->priv->poll);
287
288     if (G_LIKELY (pclass->free_buffer))
289       pclass->free_buffer (pool, buffer);
290   }
291   return TRUE;
292 }
293
294 /* must be called with the lock */
295 static gboolean
296 do_stop (GstBufferPool * pool)
297 {
298   if (pool->priv->started) {
299     GstBufferPoolClass *pclass;
300
301     pclass = GST_BUFFER_POOL_GET_CLASS (pool);
302
303     GST_LOG_OBJECT (pool, "stopping");
304     if (G_LIKELY (pclass->stop)) {
305       if (!pclass->stop (pool))
306         return FALSE;
307     }
308     pool->priv->started = FALSE;
309   }
310   return TRUE;
311 }
312
313 /**
314  * gst_buffer_pool_set_active:
315  * @pool: a #GstBufferPool
316  * @active: the new active state
317  *
318  * Control the active state of @pool. When the pool is active, new calls to
319  * gst_buffer_pool_acquire_buffer() will return with GST_FLOW_FLUSHING.
320  *
321  * Activating the bufferpool will preallocate all resources in the pool based on
322  * the configuration of the pool.
323  *
324  * Deactivating will free the resources again when there are no outstanding
325  * buffers. When there are outstanding buffers, they will be freed as soon as
326  * they are all returned to the pool.
327  *
328  * Returns: %FALSE when the pool was not configured or when preallocation of the
329  * buffers failed.
330  */
331 gboolean
332 gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
333 {
334   gboolean res = TRUE;
335
336   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
337
338   GST_LOG_OBJECT (pool, "active %d", active);
339
340   GST_BUFFER_POOL_LOCK (pool);
341   /* just return if we are already in the right state */
342   if (pool->priv->active == active)
343     goto was_ok;
344
345   /* we need to be configured */
346   if (!pool->priv->configured)
347     goto not_configured;
348
349   if (active) {
350     if (!do_start (pool))
351       goto start_failed;
352
353     /* unset the flushing state now */
354     gst_poll_read_control (pool->priv->poll);
355     g_atomic_int_set (&pool->flushing, 0);
356   } else {
357     gint outstanding;
358
359     /* set to flushing first */
360     g_atomic_int_set (&pool->flushing, 1);
361     gst_poll_write_control (pool->priv->poll);
362
363     /* when all buffers are in the pool, free them. Else they will be
364      * freed when they are released */
365     outstanding = g_atomic_int_get (&pool->priv->outstanding);
366     GST_LOG_OBJECT (pool, "outstanding buffers %d", outstanding);
367     if (outstanding == 0) {
368       if (!do_stop (pool))
369         goto stop_failed;
370     }
371   }
372   pool->priv->active = active;
373   GST_BUFFER_POOL_UNLOCK (pool);
374
375   return res;
376
377 was_ok:
378   {
379     GST_DEBUG_OBJECT (pool, "pool was in the right state");
380     GST_BUFFER_POOL_UNLOCK (pool);
381     return TRUE;
382   }
383 not_configured:
384   {
385     GST_ERROR_OBJECT (pool, "pool was not configured");
386     GST_BUFFER_POOL_UNLOCK (pool);
387     return FALSE;
388   }
389 start_failed:
390   {
391     GST_ERROR_OBJECT (pool, "start failed");
392     GST_BUFFER_POOL_UNLOCK (pool);
393     return FALSE;
394   }
395 stop_failed:
396   {
397     GST_WARNING_OBJECT (pool, "stop failed");
398     GST_BUFFER_POOL_UNLOCK (pool);
399     return FALSE;
400   }
401 }
402
403 /**
404  * gst_buffer_pool_is_active:
405  * @pool: a #GstBufferPool
406  *
407  * Check if @pool is active. A pool can be activated with the
408  * gst_buffer_pool_set_active() call.
409  *
410  * Returns: %TRUE when the pool is active.
411  */
412 gboolean
413 gst_buffer_pool_is_active (GstBufferPool * pool)
414 {
415   gboolean res;
416
417   GST_BUFFER_POOL_LOCK (pool);
418   res = pool->priv->active;
419   GST_BUFFER_POOL_UNLOCK (pool);
420
421   return res;
422 }
423
424 static gboolean
425 default_set_config (GstBufferPool * pool, GstStructure * config)
426 {
427   GstBufferPoolPrivate *priv = pool->priv;
428   const GstCaps *caps;
429   guint size, min_buffers, max_buffers;
430   guint prefix, align;
431
432   /* parse the config and keep around */
433   if (!gst_buffer_pool_config_get (config, &caps, &size, &min_buffers,
434           &max_buffers, &prefix, &align))
435     goto wrong_config;
436
437   GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
438
439   priv->size = size;
440   priv->min_buffers = min_buffers;
441   priv->max_buffers = max_buffers;
442   priv->prefix = prefix;
443   priv->align = align;
444
445   return TRUE;
446
447 wrong_config:
448   {
449     GST_WARNING_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
450     return FALSE;
451   }
452 }
453
454 /**
455  * gst_buffer_pool_set_config:
456  * @pool: a #GstBufferPool
457  * @config: (transfer full): a #GstStructure
458  *
459  * Set the configuration of the pool. The pool must be inactive and all buffers
460  * allocated form this pool must be returned or else this function will do
461  * nothing and return FALSE.
462  *
463  * @config is a #GstStructure that contains the configuration parameters for
464  * the pool. A default and mandatory set of parameters can be configured with
465  * gst_buffer_pool_config_set(). This function takes ownership of @config.
466  *
467  * Returns: TRUE when the configuration could be set.
468  */
469 gboolean
470 gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
471 {
472   gboolean result;
473   GstBufferPoolClass *pclass;
474
475   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
476   g_return_val_if_fail (config != NULL, FALSE);
477
478   GST_BUFFER_POOL_LOCK (pool);
479   /* can't change the settings when active */
480   if (pool->priv->active)
481     goto was_active;
482
483   /* we can't change when outstanding buffers */
484   if (g_atomic_int_get (&pool->priv->outstanding) != 0)
485     goto have_outstanding;
486
487   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
488
489   /* set the new config */
490   if (G_LIKELY (pclass->set_config))
491     result = pclass->set_config (pool, config);
492   else
493     result = FALSE;
494
495   if (result) {
496     if (pool->priv->config)
497       gst_structure_free (pool->priv->config);
498     pool->priv->config = config;
499
500     /* now we are configured */
501     pool->priv->configured = TRUE;
502   }
503   GST_BUFFER_POOL_UNLOCK (pool);
504
505   return result;
506
507   /* ERRORS */
508 was_active:
509   {
510     GST_WARNING_OBJECT (pool, "can't change config, we are active");
511     GST_BUFFER_POOL_UNLOCK (pool);
512     return FALSE;
513   }
514 have_outstanding:
515   {
516     GST_WARNING_OBJECT (pool, "can't change config, have outstanding buffers");
517     GST_BUFFER_POOL_UNLOCK (pool);
518     return FALSE;
519   }
520 }
521
522 /**
523  * gst_buffer_pool_get_config:
524  * @pool: a #GstBufferPool
525  *
526  * Get a copy of the current configuration of the pool. This configuration
527  * can either be modified and used for the gst_buffer_pool_set_config() call
528  * or it must be freed after usage.
529  *
530  * Returns: (transfer full): a copy of the current configuration of @pool. use
531  * gst_structure_free() after usage or gst_buffer_pool_set_config().
532  */
533 GstStructure *
534 gst_buffer_pool_get_config (GstBufferPool * pool)
535 {
536   GstStructure *result;
537
538   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
539
540   GST_BUFFER_POOL_UNLOCK (pool);
541   result = gst_structure_copy (pool->priv->config);
542   GST_BUFFER_POOL_UNLOCK (pool);
543
544   return result;
545 }
546
547 static const gchar *empty_option[] = { NULL };
548
549 /**
550  * gst_buffer_pool_get_options:
551  * @pool: a #GstBufferPool
552  *
553  * Get a NULL terminated array of string with supported bufferpool options for
554  * @pool. An option would typically be enabled with
555  * gst_buffer_pool_config_add_option().
556  *
557  * Returns: (array zero-terminated=1) (transfer none): a NULL terminated array of strings.
558  */
559 const gchar **
560 gst_buffer_pool_get_options (GstBufferPool * pool)
561 {
562   GstBufferPoolClass *pclass;
563   const gchar **result;
564
565   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
566
567   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
568
569   if (G_LIKELY (pclass->get_options)) {
570     if ((result = pclass->get_options (pool)) == NULL)
571       goto invalid_result;
572   } else
573     result = empty_option;
574
575   return result;
576
577   /* ERROR */
578 invalid_result:
579   {
580     g_warning ("bufferpool subclass returned NULL options");
581     return empty_option;
582   }
583 }
584
585 /**
586  * gst_buffer_pool_has_option:
587  * @pool: a #GstBufferPool
588  * @option: an option
589  *
590  * Check if the bufferpool supports @option.
591  *
592  * Returns: a NULL terminated array of strings.
593  */
594 gboolean
595 gst_buffer_pool_has_option (GstBufferPool * pool, const gchar * option)
596 {
597   guint i;
598   const gchar **options;
599
600   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
601   g_return_val_if_fail (option != NULL, FALSE);
602
603   options = gst_buffer_pool_get_options (pool);
604
605   for (i = 0; options[i]; i++) {
606     if (g_str_equal (options[i], option))
607       return TRUE;
608   }
609   return FALSE;
610 }
611
612 /**
613  * gst_buffer_pool_config_set:
614  * @config: a #GstBufferPool configuration
615  * @caps: caps for the buffers
616  * @size: the size of each buffer, not including prefix
617  * @min_buffers: the minimum amount of buffers to allocate.
618  * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
619  * @prefix: prefix each buffer with this many bytes
620  * @align: alignment of the buffer data.
621  *
622  * Configure @config with the given parameters.
623  */
624 void
625 gst_buffer_pool_config_set (GstStructure * config, const GstCaps * caps,
626     guint size, guint min_buffers, guint max_buffers, guint prefix, guint align)
627 {
628   g_return_if_fail (config != NULL);
629
630   gst_structure_id_set (config,
631       GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
632       GST_QUARK (SIZE), G_TYPE_UINT, size,
633       GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
634       GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
635       GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
636       GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
637 }
638
639 /**
640  * gst_buffer_pool_config_add_option:
641  * @config: a #GstBufferPool configuration
642  * @option: an option to add
643  *
644  * Enabled the option in @config. This will instruct the @bufferpool to enable
645  * the specified option on the buffers that it allocates.
646  *
647  * The supported options by @pool can be retrieved with gst_buffer_pool_get_options().
648  */
649 void
650 gst_buffer_pool_config_add_option (GstStructure * config, const gchar * option)
651 {
652   const GValue *value;
653   GValue option_value = { 0, };
654   guint i, len;
655
656   g_return_if_fail (config != NULL);
657
658   value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
659   if (value) {
660     len = gst_value_array_get_size (value);
661     for (i = 0; i < len; ++i) {
662       const GValue *nth_val = gst_value_array_get_value (value, i);
663
664       if (g_str_equal (option, g_value_get_string (nth_val)))
665         return;
666     }
667   } else {
668     GValue new_array_val = { 0, };
669
670     g_value_init (&new_array_val, GST_TYPE_ARRAY);
671     gst_structure_id_take_value (config, GST_QUARK (OPTIONS), &new_array_val);
672     value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
673   }
674   g_value_init (&option_value, G_TYPE_STRING);
675   g_value_set_string (&option_value, option);
676   gst_value_array_append_value ((GValue *) value, &option_value);
677   g_value_unset (&option_value);
678 }
679
680 /**
681  * gst_buffer_pool_config_n_options:
682  * @config: a #GstBufferPool configuration
683  *
684  * Retrieve the number of values currently stored in the
685  * options array of the @config structure.
686  *
687  * Returns: the options array size as a #guint.
688  */
689 guint
690 gst_buffer_pool_config_n_options (GstStructure * config)
691 {
692   const GValue *value;
693   guint size = 0;
694
695   g_return_val_if_fail (config != NULL, 0);
696
697   value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
698   if (value) {
699     size = gst_value_array_get_size (value);
700   }
701   return size;
702 }
703
704 /**
705  * gst_buffer_pool_config_get_option:
706  * @config: a #GstBufferPool configuration
707  * @index: position in the option array to read
708  *
709  * Parse an available @config and get the option
710  * at @index of the options API array.
711  *
712  * Returns: a #gchar of the option at @index.
713  */
714 const gchar *
715 gst_buffer_pool_config_get_option (GstStructure * config, guint index)
716 {
717   const GValue *value;
718   const gchar *ret = NULL;
719
720   g_return_val_if_fail (config != NULL, 0);
721
722   value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
723   if (value) {
724     const GValue *option_value;
725
726     option_value = gst_value_array_get_value (value, index);
727     if (option_value)
728       ret = g_value_get_string (option_value);
729   }
730   return ret;
731 }
732
733 /**
734  * gst_buffer_pool_config_has_option:
735  * @config: a #GstBufferPool configuration
736  * @option: an option
737  *
738  * Check if @config contains @option
739  *
740  * Returns: TRUE if the options array contains @option.
741  */
742 gboolean
743 gst_buffer_pool_config_has_option (GstStructure * config, const gchar * option)
744 {
745   const GValue *value;
746   guint i, len;
747
748   g_return_val_if_fail (config != NULL, 0);
749
750   value = gst_structure_id_get_value (config, GST_QUARK (OPTIONS));
751   if (value) {
752     len = gst_value_array_get_size (value);
753     for (i = 0; i < len; ++i) {
754       const GValue *nth_val = gst_value_array_get_value (value, i);
755
756       if (g_str_equal (option, g_value_get_string (nth_val)))
757         return TRUE;
758     }
759   }
760   return FALSE;
761 }
762
763 /**
764  * gst_buffer_pool_config_get:
765  * @config: (transfer none): a #GstBufferPool configuration
766  * @caps: (out): the caps of buffers
767  * @size: (out): the size of each buffer, not including prefix
768  * @min_buffers: (out): the minimum amount of buffers to allocate.
769  * @max_buffers: (out): the maximum amount of buffers to allocate or 0 for unlimited.
770  * @prefix: (out): prefix each buffer with this many bytes
771  * @align: (out): alignment of the buffer data.
772  *
773  * Get the configuration values from @config.
774  */
775 gboolean
776 gst_buffer_pool_config_get (GstStructure * config, const GstCaps ** caps,
777     guint * size, guint * min_buffers, guint * max_buffers, guint * prefix,
778     guint * align)
779 {
780   g_return_val_if_fail (config != NULL, FALSE);
781
782   return gst_structure_id_get (config,
783       GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
784       GST_QUARK (SIZE), G_TYPE_UINT, size,
785       GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
786       GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
787       GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
788       GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
789 }
790
791 static GstFlowReturn
792 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
793     GstBufferPoolParams * params)
794 {
795   GstFlowReturn result;
796   GstBufferPoolClass *pclass;
797   GstBufferPoolPrivate *priv = pool->priv;
798
799   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
800
801   while (TRUE) {
802     if (G_UNLIKELY (GST_BUFFER_POOL_IS_FLUSHING (pool)))
803       goto flushing;
804
805     /* try to get a buffer from the queue */
806     *buffer = gst_atomic_queue_pop (pool->priv->queue);
807     if (G_LIKELY (*buffer)) {
808       gst_poll_read_control (pool->priv->poll);
809       result = GST_FLOW_OK;
810       GST_LOG_OBJECT (pool, "acquired buffer %p", *buffer);
811       break;
812     }
813
814     /* no buffer */
815     if (priv->max_buffers == 0) {
816       /* no max_buffers, we allocate some more */
817       if (G_LIKELY (pclass->alloc_buffer)) {
818         result = pclass->alloc_buffer (pool, buffer, params);
819         if (result == GST_FLOW_OK && *buffer)
820           gst_buffer_foreach_meta (*buffer, mark_meta_pooled, pool);
821         else
822           result = GST_FLOW_ERROR;
823       } else
824         result = GST_FLOW_NOT_SUPPORTED;
825       GST_LOG_OBJECT (pool, "alloc buffer %p", *buffer);
826       break;
827     }
828
829     /* check if we need to wait */
830     if (params && (params->flags & GST_BUFFER_POOL_FLAG_DONTWAIT)) {
831       GST_LOG_OBJECT (pool, "no more buffers");
832       result = GST_FLOW_EOS;
833       break;
834     }
835
836     /* now wait */
837     GST_LOG_OBJECT (pool, "waiting for free buffers");
838     gst_poll_wait (pool->priv->poll, GST_CLOCK_TIME_NONE);
839   }
840
841   return result;
842
843   /* ERRORS */
844 flushing:
845   {
846     GST_DEBUG_OBJECT (pool, "we are flushing");
847     return GST_FLOW_FLUSHING;
848   }
849 }
850
851 static inline void
852 dec_outstanding (GstBufferPool * pool)
853 {
854   if (g_atomic_int_dec_and_test (&pool->priv->outstanding)) {
855     /* all buffers are returned to the pool, see if we need to free them */
856     if (GST_BUFFER_POOL_IS_FLUSHING (pool)) {
857       /* take the lock so that set_active is not run concurrently */
858       GST_BUFFER_POOL_LOCK (pool);
859       /* recheck the flushing state in the lock, the pool could have been
860        * set to active again */
861       if (GST_BUFFER_POOL_IS_FLUSHING (pool))
862         do_stop (pool);
863
864       GST_BUFFER_POOL_UNLOCK (pool);
865     }
866   }
867 }
868
869 static gboolean
870 remove_meta_unpooled (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
871 {
872   if (!GST_META_FLAG_IS_SET (*meta, GST_META_FLAG_POOLED))
873     *meta = NULL;
874   return TRUE;
875 }
876
877 static void
878 default_reset_buffer (GstBufferPool * pool, GstBuffer * buffer,
879     GstBufferPoolParams * params)
880 {
881   GST_BUFFER_FLAGS (buffer) = 0;
882
883   GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
884   GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
885   GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
886   GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
887   GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
888
889   /* remove all metadata without the POOLED flag */
890   gst_buffer_foreach_meta (buffer, remove_meta_unpooled, pool);
891 }
892
893 /**
894  * gst_buffer_pool_acquire_buffer:
895  * @pool: a #GstBufferPool
896  * @buffer: (out): a location for a #GstBuffer
897  * @params: (transfer none) (allow-none) parameters.
898  *
899  * Acquire a buffer from @pool. @buffer should point to a memory location that
900  * can hold a pointer to the new buffer.
901  *
902  * @params can be NULL or contain optional parameters to influence the allocation.
903  *
904  * Returns: a #GstFlowReturn such as GST_FLOW_FLUSHING when the pool is
905  * inactive.
906  */
907 GstFlowReturn
908 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
909     GstBufferPoolParams * params)
910 {
911   GstBufferPoolClass *pclass;
912   GstFlowReturn result;
913
914   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), GST_FLOW_ERROR);
915   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
916
917   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
918
919   /* assume we'll have one more outstanding buffer we need to do that so
920    * that concurrent set_active doesn't clear the buffers */
921   g_atomic_int_inc (&pool->priv->outstanding);
922
923   if (G_LIKELY (pclass->acquire_buffer))
924     result = pclass->acquire_buffer (pool, buffer, params);
925   else
926     result = GST_FLOW_NOT_SUPPORTED;
927
928   if (G_LIKELY (result == GST_FLOW_OK)) {
929     /* all buffers from the pool point to the pool and have the refcount of the
930      * pool incremented */
931     (*buffer)->pool = gst_object_ref (pool);
932     /* now reset the buffer when needed */
933     if (G_LIKELY (pclass->reset_buffer))
934       pclass->reset_buffer (pool, *buffer, params);
935   } else {
936     dec_outstanding (pool);
937   }
938
939   return result;
940 }
941
942 static void
943 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
944 {
945   /* keep it around in our queue */
946   GST_LOG_OBJECT (pool, "released buffer %p", buffer);
947   gst_atomic_queue_push (pool->priv->queue, buffer);
948   gst_poll_write_control (pool->priv->poll);
949 }
950
951 /**
952  * gst_buffer_pool_release_buffer:
953  * @pool: a #GstBufferPool
954  * @buffer: (transfer none): a #GstBuffer
955  *
956  * Release @buffer to @pool. @buffer should have previously been allocated from
957  * @pool with gst_buffer_pool_acquire_buffer().
958  *
959  * This function is usually called automatically when the last ref on @buffer
960  * disappears.
961  */
962 void
963 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
964 {
965   GstBufferPoolClass *pclass;
966
967   g_return_if_fail (GST_IS_BUFFER_POOL (pool));
968   g_return_if_fail (buffer != NULL);
969
970   /* check that the buffer is ours, all buffers returned to the pool have the
971    * pool member set to NULL and the pool refcount decreased */
972   if (!g_atomic_pointer_compare_and_exchange (&buffer->pool, pool, NULL))
973     return;
974
975   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
976
977   if (G_LIKELY (pclass->release_buffer))
978     pclass->release_buffer (pool, buffer);
979
980   dec_outstanding (pool);
981
982   /* decrease the refcount that the buffer had to us */
983   gst_object_unref (pool);
984 }