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