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