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