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