bufferpool: Rework buffer management a little
[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
43 #define GST_BUFFER_POOL_GET_PRIVATE(obj)  \
44    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BUFFER_POOL, GstBufferPoolPrivate))
45
46 #define GST_BUFFER_POOL_LOCK(pool)   (g_static_rec_mutex_lock(&pool->priv->rec_lock))
47 #define GST_BUFFER_POOL_UNLOCK(pool) (g_static_rec_mutex_unlock(&pool->priv->rec_lock))
48
49 struct _GstBufferPoolPrivate
50 {
51   GStaticRecMutex rec_lock;
52   guint min_buffers;
53   guint max_buffers;
54   guint size;
55   guint prefix;
56   guint postfix;
57   guint align;
58 };
59
60 enum
61 {
62   /* add more above */
63   LAST_SIGNAL
64 };
65
66 static void gst_buffer_pool_finalize (GObject * object);
67
68 G_DEFINE_TYPE (GstBufferPool, gst_buffer_pool, GST_TYPE_OBJECT);
69
70 static gboolean default_start (GstBufferPool * pool);
71 static gboolean default_stop (GstBufferPool * pool);
72 static gboolean default_set_config (GstBufferPool * pool,
73     GstStructure * config);
74 static GstFlowReturn default_alloc_buffer (GstBufferPool * pool,
75     GstBuffer ** buffer, GstBufferPoolParams * params);
76 static GstFlowReturn default_acquire_buffer (GstBufferPool * pool,
77     GstBuffer ** buffer, GstBufferPoolParams * params);
78 static void default_free_buffer (GstBufferPool * pool, GstBuffer * buffer);
79 static void default_release_buffer (GstBufferPool * pool, GstBuffer * buffer);
80
81 static void
82 gst_buffer_pool_class_init (GstBufferPoolClass * klass)
83 {
84   GObjectClass *gobject_class = (GObjectClass *) klass;
85
86   gobject_class->finalize = gst_buffer_pool_finalize;
87
88   klass->start = default_start;
89   klass->stop = default_stop;
90   klass->set_config = default_set_config;
91   klass->acquire_buffer = default_acquire_buffer;
92   klass->alloc_buffer = default_alloc_buffer;
93   klass->release_buffer = default_release_buffer;
94   klass->free_buffer = default_free_buffer;
95 }
96
97 static void
98 gst_buffer_pool_init (GstBufferPool * pool)
99 {
100   pool->priv = GST_BUFFER_POOL_GET_PRIVATE (pool);
101
102   g_static_rec_mutex_init (&pool->priv->rec_lock);
103
104   pool->poll = gst_poll_new_timer ();
105   pool->queue = gst_atomic_queue_new (10);
106   pool->flushing = TRUE;
107   pool->active = FALSE;
108   pool->configured = FALSE;
109   pool->started = FALSE;
110   pool->config = gst_structure_id_empty_new (GST_QUARK (BUFFER_POOL_CONFIG));
111   gst_buffer_pool_config_set (pool->config, 0, 0, 0, 0, 0, 1);
112
113   GST_DEBUG_OBJECT (pool, "created");
114 }
115
116 static void
117 gst_buffer_pool_finalize (GObject * object)
118 {
119   GstBufferPool *pool;
120
121   pool = GST_BUFFER_POOL_CAST (object);
122
123   GST_DEBUG_OBJECT (pool, "finalize");
124
125   gst_buffer_pool_set_active (pool, FALSE);
126   gst_atomic_queue_unref (pool->queue);
127   gst_poll_free (pool->poll);
128   gst_structure_free (pool->config);
129   g_static_rec_mutex_free (&pool->priv->rec_lock);
130
131   G_OBJECT_CLASS (gst_buffer_pool_parent_class)->finalize (object);
132 }
133
134 /**
135  * gst_buffer_pool_new:
136  *
137  * Creates a new #GstBufferPool instance.
138  *
139  * Returns: a new #GstBufferPool instance
140  */
141 GstBufferPool *
142 gst_buffer_pool_new (void)
143 {
144   GstBufferPool *result;
145
146   result = g_object_newv (GST_TYPE_BUFFER_POOL, 0, NULL);
147   GST_DEBUG_OBJECT (result, "created new buffer pool");
148
149   return result;
150 }
151
152 /* the default implementation for preallocating the buffers
153  * in the pool */
154 static gboolean
155 default_start (GstBufferPool * pool)
156 {
157   guint i;
158   GstBufferPoolPrivate *priv = pool->priv;
159   GstBufferPoolClass *pclass;
160
161   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
162
163   /* no alloc function, error */
164   if (G_UNLIKELY (pclass->alloc_buffer == NULL))
165     goto no_alloc;
166
167   /* we need to prealloc buffers */
168   for (i = 0; i < priv->min_buffers; i++) {
169     GstBuffer *buffer;
170
171     if (pclass->alloc_buffer (pool, &buffer, NULL) != GST_FLOW_OK)
172       goto alloc_failed;
173
174     /* store in the queue */
175     gst_atomic_queue_push (pool->queue, buffer);
176     gst_poll_write_control (pool->poll);
177   }
178   return TRUE;
179
180   /* ERRORS */
181 no_alloc:
182   {
183     GST_WARNING_OBJECT (pool, "no alloc function");
184     return FALSE;
185   }
186 alloc_failed:
187   {
188     GST_WARNING_OBJECT (pool, "alloc function failed");
189     return FALSE;
190   }
191 }
192
193 static void
194 default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
195 {
196   gst_buffer_unref (buffer);
197 }
198
199 static gboolean
200 default_stop (GstBufferPool * pool)
201 {
202   GstBuffer *buffer;
203   GstBufferPoolClass *pclass;
204
205   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
206
207   /* clear the pool */
208   while ((buffer = gst_atomic_queue_pop (pool->queue))) {
209     gst_poll_read_control (pool->poll);
210
211     if (G_LIKELY (pclass->free_buffer))
212       pclass->free_buffer (pool, buffer);
213   }
214   return TRUE;
215 }
216
217 static gboolean
218 do_start (GstBufferPool * pool)
219 {
220
221   if (!pool->started) {
222     GstBufferPoolClass *pclass;
223
224     pclass = GST_BUFFER_POOL_GET_CLASS (pool);
225
226     /* start the pool, subclasses should allocate buffers and put them
227      * in the queue */
228     if (G_LIKELY (pclass->start)) {
229       if (!pclass->start (pool))
230         return FALSE;
231     }
232     pool->started = TRUE;
233   }
234   return TRUE;
235 }
236
237 static gboolean
238 do_stop (GstBufferPool * pool)
239 {
240   if (pool->started) {
241     GstBufferPoolClass *pclass;
242
243     pclass = GST_BUFFER_POOL_GET_CLASS (pool);
244
245     if (G_LIKELY (pclass->stop)) {
246       if (!pclass->stop (pool))
247         return FALSE;
248     }
249     pool->started = FALSE;
250   }
251   return TRUE;
252 }
253
254 /**
255  * gst_buffer_pool_set_active:
256  * @pool: a #GstBufferPool
257  * @active: the new active state
258  *
259  * Control the active state of @pool. When the pool is active, new calls to
260  * gst_buffer_pool_acquire_buffer() will return with GST_FLOW_WRONG_STATE.
261  *
262  * Returns: %FALSE when the pool was not configured or when preallocation of the
263  * buffers failed.
264  */
265 gboolean
266 gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
267 {
268   GstBufferPoolClass *pclass;
269   gboolean res = TRUE;
270
271   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
272
273   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
274
275   GST_BUFFER_POOL_LOCK (pool);
276   /* just return if we are already in the right state */
277   if (pool->active == active)
278     goto was_ok;
279
280   /* we need to be configured */
281   if (!pool->configured)
282     goto not_configured;
283
284   if (active) {
285     if (!do_start (pool))
286       goto start_failed;
287
288     /* unset the flushing state now */
289     gst_poll_read_control (pool->poll);
290     g_atomic_int_set (&pool->flushing, FALSE);
291   } else {
292     /* set to flushing first */
293     g_atomic_int_set (&pool->flushing, TRUE);
294     gst_poll_write_control (pool->poll);
295
296     /* when all buffers are in the pool, free them. Else they will be
297      * freed when they are released */
298     if (g_atomic_int_get (&pool->outstanding) == 0) {
299       if (!do_stop (pool))
300         goto stop_failed;
301     }
302   }
303   pool->active = active;
304   GST_BUFFER_POOL_UNLOCK (pool);
305
306   return res;
307
308 was_ok:
309   {
310     GST_DEBUG_OBJECT (pool, "pool was in the right state");
311     GST_BUFFER_POOL_UNLOCK (pool);
312     return TRUE;
313   }
314 not_configured:
315   {
316     GST_ERROR_OBJECT (pool, "pool was not configured");
317     GST_BUFFER_POOL_UNLOCK (pool);
318     return FALSE;
319   }
320 start_failed:
321   {
322     GST_ERROR_OBJECT (pool, "start failed");
323     GST_BUFFER_POOL_UNLOCK (pool);
324     return FALSE;
325   }
326 stop_failed:
327   {
328     GST_WARNING_OBJECT (pool, "stop failed");
329     GST_BUFFER_POOL_UNLOCK (pool);
330     return FALSE;
331   }
332 }
333
334 static gboolean
335 default_set_config (GstBufferPool * pool, GstStructure * config)
336 {
337   GstBufferPoolPrivate *priv = pool->priv;
338
339   /* parse the config and keep around */
340   gst_buffer_pool_config_get (config, &priv->size, &priv->min_buffers,
341       &priv->max_buffers, &priv->prefix, &priv->postfix, &priv->align);
342
343   return TRUE;
344 }
345
346 /**
347  * gst_buffer_pool_set_config:
348  * @pool: a #GstBufferPool
349  * @config: a #GstStructure
350  *
351  * Set the configuration of the pool. The pool must be inactive and all buffers
352  * allocated form this pool must be returned or else this function will do
353  * nothing and return FALSE.
354  *
355  * @condfig is a #GstStructure that contains the configuration parameters for
356  * the pool. A default and mandatory set of parameters can be configured with
357  * gst_buffer_pool_config_set(). This function takes ownership of @config.
358  *
359  * Returns: TRUE when the configuration could be set.
360  */
361 gboolean
362 gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
363 {
364   gboolean result;
365   GstBufferPoolClass *pclass;
366
367   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
368   g_return_val_if_fail (config != NULL, FALSE);
369
370   GST_BUFFER_POOL_LOCK (pool);
371   /* can't change the settings when active */
372   if (pool->active)
373     goto was_active;
374
375   /* we can't change when outstanding buffers */
376   if (g_atomic_int_get (&pool->outstanding) != 0)
377     goto have_outstanding;
378
379   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
380
381   /* set the new config */
382   if (G_LIKELY (pclass->set_config))
383     result = pclass->set_config (pool, config);
384   else
385     result = FALSE;
386
387   if (result) {
388     if (pool->config)
389       gst_structure_free (pool->config);
390     pool->config = config;
391
392     /* now we are configured */
393     pool->configured = TRUE;
394   }
395   GST_BUFFER_POOL_UNLOCK (pool);
396
397   return result;
398
399   /* ERRORS */
400 was_active:
401   {
402     GST_WARNING_OBJECT (pool, "can't change config, we are active");
403     GST_BUFFER_POOL_UNLOCK (pool);
404     return FALSE;
405   }
406 have_outstanding:
407   {
408     GST_WARNING_OBJECT (pool, "can't change config, have outstanding buffers");
409     GST_BUFFER_POOL_UNLOCK (pool);
410     return FALSE;
411   }
412 }
413
414 /**
415  * gst_buffer_pool_get_config:
416  * @pool: a #GstBufferPool
417  *
418  * Get a copy of the current configuration of the pool. This configuration
419  * can either be modified and used for the gst_buffer_pool_set_config() call
420  * or it must be freed after usage.
421  *
422  * Returns: a copy of the current configuration of @pool. use
423  * gst_structure_free() after usage or gst_buffer_pool_set_config().
424  */
425 GstStructure *
426 gst_buffer_pool_get_config (GstBufferPool * pool)
427 {
428   GstStructure *result;
429
430   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
431
432   GST_BUFFER_POOL_UNLOCK (pool);
433   result = gst_structure_copy (pool->config);
434   GST_BUFFER_POOL_UNLOCK (pool);
435
436   return result;
437 }
438
439 /**
440  * gst_buffer_pool_config_set:
441  * @pool: a #GstBufferPool
442  * @size: the size of each buffer, not including pre and post fix
443  * @min_buffers: the minimum amount of buffers to allocate.
444  * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
445  * @prefix: prefix each buffer with this many bytes
446  * @postfix: postfix each buffer with this many bytes
447  * @align: alignment of the buffer data.
448  *
449  * Configure @config with the given parameters.
450  */
451 void
452 gst_buffer_pool_config_set (GstStructure * config, guint size,
453     guint min_buffers, guint max_buffers, guint prefix, guint postfix,
454     guint align)
455 {
456   g_return_if_fail (config != NULL);
457
458   gst_structure_id_set (config,
459       GST_QUARK (SIZE), G_TYPE_UINT, size,
460       GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
461       GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
462       GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
463       GST_QUARK (POSTFIX), G_TYPE_UINT, postfix,
464       GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
465 }
466
467 /**
468  * gst_buffer_pool_config_get:
469  * @pool: a #GstBufferPool
470  * @size: the size of each buffer, not including pre and post fix
471  * @min_buffers: the minimum amount of buffers to allocate.
472  * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
473  * @prefix: prefix each buffer with this many bytes
474  * @postfix: postfix each buffer with this many bytes
475  * @align: alignment of the buffer data.
476  *
477  * Get the configuration values from @config.
478  */
479 gboolean
480 gst_buffer_pool_config_get (GstStructure * config, guint * size,
481     guint * min_buffers, guint * max_buffers, guint * prefix, guint * postfix,
482     guint * align)
483 {
484   g_return_val_if_fail (config != NULL, FALSE);
485
486   return gst_structure_id_get (config,
487       GST_QUARK (SIZE), G_TYPE_UINT, size,
488       GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
489       GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
490       GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
491       GST_QUARK (POSTFIX), G_TYPE_UINT, postfix,
492       GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
493 }
494
495 static GstFlowReturn
496 default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
497     GstBufferPoolParams * params)
498 {
499   guint size, align;
500   GstBufferPoolPrivate *priv = pool->priv;
501
502   *buffer = gst_buffer_new ();
503
504   align = priv->align - 1;
505   size = priv->prefix + priv->postfix + priv->size + align;
506   if (size > 0) {
507     guint8 *memptr;
508
509     memptr = g_malloc (size);
510     GST_BUFFER_MALLOCDATA (*buffer) = memptr;
511     memptr = (guint8 *) ((guintptr) (memptr + align) & ~align);
512     GST_BUFFER_DATA (*buffer) = memptr + priv->prefix;
513     GST_BUFFER_SIZE (*buffer) = priv->size;
514   }
515
516   return GST_FLOW_OK;
517 }
518
519 static GstFlowReturn
520 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
521     GstBufferPoolParams * params)
522 {
523   GstFlowReturn result;
524   GstBufferPoolClass *pclass;
525   GstBufferPoolPrivate *priv = pool->priv;
526
527   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
528
529   while (TRUE) {
530     if (g_atomic_int_get (&pool->flushing))
531       return GST_FLOW_WRONG_STATE;
532
533     /* try to get a buffer from the queue */
534     *buffer = gst_atomic_queue_pop (pool->queue);
535     if (*buffer) {
536       gst_poll_read_control (pool->poll);
537       result = GST_FLOW_OK;
538       break;
539     }
540
541     /* no buffer */
542     if (priv->max_buffers == 0) {
543       /* no max_buffers, we allocate some more */
544       if (G_LIKELY (pclass->alloc_buffer)) {
545         result = pclass->alloc_buffer (pool, buffer, params);
546       } else
547         result = GST_FLOW_NOT_SUPPORTED;
548       break;
549     }
550
551     /* check if we need to wait */
552     if (params && !(params->flags & GST_BUFFER_POOL_FLAG_WAIT)) {
553       result = GST_FLOW_UNEXPECTED;
554       break;
555     }
556
557     /* now wait */
558     gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
559   }
560
561   return result;
562 }
563
564 /**
565  * gst_buffer_pool_acquire_buffer:
566  * @pool: a #GstBufferPool
567  * @buffer: a location for a #GstBuffer
568  * @params: parameters.
569  *
570  * Acquire a buffer from @pool. @buffer should point to a memory location that
571  * can hold a pointer to the new buffer.
572  *
573  * @params can be NULL or contain optional parameters to influence the allocation.
574  *
575  * Returns: a #GstFlowReturn such as GST_FLOW_WRONG_STATE when the pool is
576  * inactive.
577  */
578 GstFlowReturn
579 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
580     GstBufferPoolParams * params)
581 {
582   GstBufferPoolClass *pclass;
583   GstFlowReturn result;
584
585   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), GST_FLOW_ERROR);
586   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
587
588   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
589
590   if (G_LIKELY (pclass->acquire_buffer))
591     result = pclass->acquire_buffer (pool, buffer, params);
592   else
593     result = GST_FLOW_NOT_SUPPORTED;
594
595   if (G_LIKELY (result == GST_FLOW_OK && *buffer))
596     g_atomic_int_inc (&pool->outstanding);
597
598   return result;
599 }
600
601 static void
602 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
603 {
604   /* keep it around in our queue */
605   gst_atomic_queue_push (pool->queue, buffer);
606   gst_poll_write_control (pool->poll);
607 }
608
609 /**
610  * gst_buffer_pool_release_buffer:
611  * @pool: a #GstBufferPool
612  * @buffer: a #GstBuffer
613  *
614  * Release @buffer to @pool. @buffer should have previously been allocated from
615  * @pool with gst_buffer_pool_acquire_buffer().
616  *
617  * This function is usually called automatically when the last ref on @buffer
618  * disappears.
619  */
620 void
621 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
622 {
623   GstBufferPoolClass *pclass;
624
625   g_return_if_fail (GST_IS_BUFFER_POOL (pool));
626   g_return_if_fail (buffer != NULL);
627
628   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
629
630   if (G_LIKELY (pclass->release_buffer))
631     pclass->release_buffer (pool, buffer);
632
633   if (G_UNLIKELY (g_atomic_int_get (&pool->flushing))) {
634     if (g_atomic_int_dec_and_test (&pool->outstanding)) {
635       /* take the lock so that set_active is not run concurrently */
636       GST_BUFFER_POOL_LOCK (pool);
637       /* recheck the flushing state in the lock, the pool could have been
638        * set to active again */
639       if (g_atomic_int_get (&pool->flushing)) {
640         if (!do_stop (pool))
641           goto stop_failed;
642       }
643       GST_BUFFER_POOL_UNLOCK (pool);
644     }
645   }
646   return;
647
648 stop_failed:
649   {
650     GST_WARNING_OBJECT (pool, "stop failed");
651     GST_BUFFER_POOL_UNLOCK (pool);
652     return;
653   }
654 }