bufferpool: Refactor stopping of the pool
[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 size;
53   guint min_buffers;
54   guint max_buffers;
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 static GstFlowReturn
153 default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
154     GstBufferPoolParams * params)
155 {
156   guint size, align;
157   GstBufferPoolPrivate *priv = pool->priv;
158
159   *buffer = gst_buffer_new ();
160
161   align = priv->align - 1;
162   size = priv->prefix + priv->postfix + priv->size + align;
163   if (size > 0) {
164     guint8 *memptr;
165
166     memptr = g_malloc (size);
167     GST_BUFFER_MALLOCDATA (*buffer) = memptr;
168     memptr = (guint8 *) ((guintptr) (memptr + align) & ~align);
169     GST_BUFFER_DATA (*buffer) = memptr + priv->prefix;
170     GST_BUFFER_SIZE (*buffer) = priv->size;
171   }
172
173   return GST_FLOW_OK;
174 }
175
176 /* the default implementation for preallocating the buffers
177  * in the pool */
178 static gboolean
179 default_start (GstBufferPool * pool)
180 {
181   guint i;
182   GstBufferPoolPrivate *priv = pool->priv;
183   GstBufferPoolClass *pclass;
184
185   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
186
187   /* no alloc function, error */
188   if (G_UNLIKELY (pclass->alloc_buffer == NULL))
189     goto no_alloc;
190
191   /* we need to prealloc buffers */
192   for (i = 0; i < priv->min_buffers; i++) {
193     GstBuffer *buffer;
194
195     if (pclass->alloc_buffer (pool, &buffer, NULL) != GST_FLOW_OK)
196       goto alloc_failed;
197
198     /* store in the queue */
199     gst_atomic_queue_push (pool->queue, buffer);
200     gst_poll_write_control (pool->poll);
201   }
202   return TRUE;
203
204   /* ERRORS */
205 no_alloc:
206   {
207     GST_WARNING_OBJECT (pool, "no alloc function");
208     return FALSE;
209   }
210 alloc_failed:
211   {
212     GST_WARNING_OBJECT (pool, "alloc function failed");
213     return FALSE;
214   }
215 }
216
217 /* must be called with the lock */
218 static gboolean
219 do_start (GstBufferPool * pool)
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
238 static void
239 default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
240 {
241   gst_buffer_unref (buffer);
242 }
243
244 /* must be called with the lock */
245 static gboolean
246 default_stop (GstBufferPool * pool)
247 {
248   GstBuffer *buffer;
249   GstBufferPoolClass *pclass;
250
251   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
252
253   /* clear the pool */
254   while ((buffer = gst_atomic_queue_pop (pool->queue))) {
255     gst_poll_read_control (pool->poll);
256
257     if (G_LIKELY (pclass->free_buffer))
258       pclass->free_buffer (pool, buffer);
259   }
260   return TRUE;
261 }
262
263 /* must be called with the lock */
264 static gboolean
265 do_stop (GstBufferPool * pool)
266 {
267   if (pool->started) {
268     GstBufferPoolClass *pclass;
269
270     pclass = GST_BUFFER_POOL_GET_CLASS (pool);
271
272     if (G_LIKELY (pclass->stop)) {
273       if (!pclass->stop (pool))
274         return FALSE;
275     }
276     pool->started = FALSE;
277   }
278   return TRUE;
279 }
280
281 /**
282  * gst_buffer_pool_set_active:
283  * @pool: a #GstBufferPool
284  * @active: the new active state
285  *
286  * Control the active state of @pool. When the pool is active, new calls to
287  * gst_buffer_pool_acquire_buffer() will return with GST_FLOW_WRONG_STATE.
288  *
289  * Returns: %FALSE when the pool was not configured or when preallocation of the
290  * buffers failed.
291  */
292 gboolean
293 gst_buffer_pool_set_active (GstBufferPool * pool, gboolean active)
294 {
295   GstBufferPoolClass *pclass;
296   gboolean res = TRUE;
297
298   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
299
300   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
301
302   GST_BUFFER_POOL_LOCK (pool);
303   /* just return if we are already in the right state */
304   if (pool->active == active)
305     goto was_ok;
306
307   /* we need to be configured */
308   if (!pool->configured)
309     goto not_configured;
310
311   if (active) {
312     if (!do_start (pool))
313       goto start_failed;
314
315     /* unset the flushing state now */
316     gst_poll_read_control (pool->poll);
317     g_atomic_int_set (&pool->flushing, FALSE);
318   } else {
319     /* set to flushing first */
320     g_atomic_int_set (&pool->flushing, TRUE);
321     gst_poll_write_control (pool->poll);
322
323     /* when all buffers are in the pool, free them. Else they will be
324      * freed when they are released */
325     if (g_atomic_int_get (&pool->outstanding) == 0) {
326       if (!do_stop (pool))
327         goto stop_failed;
328     }
329   }
330   pool->active = active;
331   GST_BUFFER_POOL_UNLOCK (pool);
332
333   return res;
334
335 was_ok:
336   {
337     GST_DEBUG_OBJECT (pool, "pool was in the right state");
338     GST_BUFFER_POOL_UNLOCK (pool);
339     return TRUE;
340   }
341 not_configured:
342   {
343     GST_ERROR_OBJECT (pool, "pool was not configured");
344     GST_BUFFER_POOL_UNLOCK (pool);
345     return FALSE;
346   }
347 start_failed:
348   {
349     GST_ERROR_OBJECT (pool, "start failed");
350     GST_BUFFER_POOL_UNLOCK (pool);
351     return FALSE;
352   }
353 stop_failed:
354   {
355     GST_WARNING_OBJECT (pool, "stop failed");
356     GST_BUFFER_POOL_UNLOCK (pool);
357     return FALSE;
358   }
359 }
360
361 static gboolean
362 default_set_config (GstBufferPool * pool, GstStructure * config)
363 {
364   GstBufferPoolPrivate *priv = pool->priv;
365   guint size, min_buffers, max_buffers;
366   guint prefix, postfix, align;
367
368   /* parse the config and keep around */
369   if (!gst_buffer_pool_config_get (config, &size, &min_buffers,
370           &max_buffers, &prefix, &postfix, &align))
371     goto wrong_config;
372
373   priv->size = size;
374   priv->min_buffers = min_buffers;
375   priv->max_buffers = max_buffers;
376   priv->prefix = prefix;
377   priv->postfix = postfix;
378   priv->align = align;
379
380   return TRUE;
381
382 wrong_config:
383   {
384     GST_WARNING_OBJECT (pool, "invalid config");
385     return FALSE;
386   }
387 }
388
389 /**
390  * gst_buffer_pool_set_config:
391  * @pool: a #GstBufferPool
392  * @config: a #GstStructure
393  *
394  * Set the configuration of the pool. The pool must be inactive and all buffers
395  * allocated form this pool must be returned or else this function will do
396  * nothing and return FALSE.
397  *
398  * @condfig is a #GstStructure that contains the configuration parameters for
399  * the pool. A default and mandatory set of parameters can be configured with
400  * gst_buffer_pool_config_set(). This function takes ownership of @config.
401  *
402  * Returns: TRUE when the configuration could be set.
403  */
404 gboolean
405 gst_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
406 {
407   gboolean result;
408   GstBufferPoolClass *pclass;
409
410   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), FALSE);
411   g_return_val_if_fail (config != NULL, FALSE);
412
413   GST_BUFFER_POOL_LOCK (pool);
414   /* can't change the settings when active */
415   if (pool->active)
416     goto was_active;
417
418   /* we can't change when outstanding buffers */
419   if (g_atomic_int_get (&pool->outstanding) != 0)
420     goto have_outstanding;
421
422   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
423
424   /* set the new config */
425   if (G_LIKELY (pclass->set_config))
426     result = pclass->set_config (pool, config);
427   else
428     result = FALSE;
429
430   if (result) {
431     if (pool->config)
432       gst_structure_free (pool->config);
433     pool->config = config;
434
435     /* now we are configured */
436     pool->configured = TRUE;
437   }
438   GST_BUFFER_POOL_UNLOCK (pool);
439
440   return result;
441
442   /* ERRORS */
443 was_active:
444   {
445     GST_WARNING_OBJECT (pool, "can't change config, we are active");
446     GST_BUFFER_POOL_UNLOCK (pool);
447     return FALSE;
448   }
449 have_outstanding:
450   {
451     GST_WARNING_OBJECT (pool, "can't change config, have outstanding buffers");
452     GST_BUFFER_POOL_UNLOCK (pool);
453     return FALSE;
454   }
455 }
456
457 /**
458  * gst_buffer_pool_get_config:
459  * @pool: a #GstBufferPool
460  *
461  * Get a copy of the current configuration of the pool. This configuration
462  * can either be modified and used for the gst_buffer_pool_set_config() call
463  * or it must be freed after usage.
464  *
465  * Returns: a copy of the current configuration of @pool. use
466  * gst_structure_free() after usage or gst_buffer_pool_set_config().
467  */
468 GstStructure *
469 gst_buffer_pool_get_config (GstBufferPool * pool)
470 {
471   GstStructure *result;
472
473   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), NULL);
474
475   GST_BUFFER_POOL_UNLOCK (pool);
476   result = gst_structure_copy (pool->config);
477   GST_BUFFER_POOL_UNLOCK (pool);
478
479   return result;
480 }
481
482 /**
483  * gst_buffer_pool_config_set:
484  * @pool: a #GstBufferPool
485  * @size: the size of each buffer, not including pre and post fix
486  * @min_buffers: the minimum amount of buffers to allocate.
487  * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
488  * @prefix: prefix each buffer with this many bytes
489  * @postfix: postfix each buffer with this many bytes
490  * @align: alignment of the buffer data.
491  *
492  * Configure @config with the given parameters.
493  */
494 void
495 gst_buffer_pool_config_set (GstStructure * config, guint size,
496     guint min_buffers, guint max_buffers, guint prefix, guint postfix,
497     guint align)
498 {
499   g_return_if_fail (config != NULL);
500
501   gst_structure_id_set (config,
502       GST_QUARK (SIZE), G_TYPE_UINT, size,
503       GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
504       GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
505       GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
506       GST_QUARK (POSTFIX), G_TYPE_UINT, postfix,
507       GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
508 }
509
510 /**
511  * gst_buffer_pool_config_get:
512  * @pool: a #GstBufferPool
513  * @size: the size of each buffer, not including pre and post fix
514  * @min_buffers: the minimum amount of buffers to allocate.
515  * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited.
516  * @prefix: prefix each buffer with this many bytes
517  * @postfix: postfix each buffer with this many bytes
518  * @align: alignment of the buffer data.
519  *
520  * Get the configuration values from @config.
521  */
522 gboolean
523 gst_buffer_pool_config_get (GstStructure * config, guint * size,
524     guint * min_buffers, guint * max_buffers, guint * prefix, guint * postfix,
525     guint * align)
526 {
527   g_return_val_if_fail (config != NULL, FALSE);
528
529   return gst_structure_id_get (config,
530       GST_QUARK (SIZE), G_TYPE_UINT, size,
531       GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
532       GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
533       GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
534       GST_QUARK (POSTFIX), G_TYPE_UINT, postfix,
535       GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL);
536 }
537
538 static GstFlowReturn
539 default_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
540     GstBufferPoolParams * params)
541 {
542   GstFlowReturn result;
543   GstBufferPoolClass *pclass;
544   GstBufferPoolPrivate *priv = pool->priv;
545
546   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
547
548   while (TRUE) {
549     if (G_UNLIKELY (g_atomic_int_get (&pool->flushing)))
550       return GST_FLOW_WRONG_STATE;
551
552     /* try to get a buffer from the queue */
553     *buffer = gst_atomic_queue_pop (pool->queue);
554     if (G_LIKELY (*buffer)) {
555       gst_poll_read_control (pool->poll);
556       result = GST_FLOW_OK;
557       break;
558     }
559
560     /* no buffer */
561     if (priv->max_buffers == 0) {
562       /* no max_buffers, we allocate some more */
563       if (G_LIKELY (pclass->alloc_buffer)) {
564         result = pclass->alloc_buffer (pool, buffer, params);
565       } else
566         result = GST_FLOW_NOT_SUPPORTED;
567       break;
568     }
569
570     /* check if we need to wait */
571     if (params && !(params->flags & GST_BUFFER_POOL_FLAG_WAIT)) {
572       result = GST_FLOW_UNEXPECTED;
573       break;
574     }
575
576     /* now wait */
577     gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
578   }
579
580   return result;
581 }
582
583 static inline void
584 dec_outstanding (GstBufferPool * pool)
585 {
586   if (g_atomic_int_dec_and_test (&pool->outstanding)) {
587     /* all buffers are returned to the pool, see if we need to free them */
588     if (g_atomic_int_get (&pool->flushing)) {
589       /* take the lock so that set_active is not run concurrently */
590       GST_BUFFER_POOL_LOCK (pool);
591       /* recheck the flushing state in the lock, the pool could have been
592        * set to active again */
593       if (g_atomic_int_get (&pool->flushing))
594         do_stop (pool);
595
596       GST_BUFFER_POOL_UNLOCK (pool);
597     }
598   }
599 }
600
601 /**
602  * gst_buffer_pool_acquire_buffer:
603  * @pool: a #GstBufferPool
604  * @buffer: a location for a #GstBuffer
605  * @params: parameters.
606  *
607  * Acquire a buffer from @pool. @buffer should point to a memory location that
608  * can hold a pointer to the new buffer.
609  *
610  * @params can be NULL or contain optional parameters to influence the allocation.
611  *
612  * Returns: a #GstFlowReturn such as GST_FLOW_WRONG_STATE when the pool is
613  * inactive.
614  */
615 GstFlowReturn
616 gst_buffer_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
617     GstBufferPoolParams * params)
618 {
619   GstBufferPoolClass *pclass;
620   GstFlowReturn result;
621
622   g_return_val_if_fail (GST_IS_BUFFER_POOL (pool), GST_FLOW_ERROR);
623   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
624
625   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
626
627   /* assume we'll have one more outstanding buffer we need to do that so
628    * that concurrent set_active doesn't clear the buffers */
629   g_atomic_int_inc (&pool->outstanding);
630
631   if (G_LIKELY (pclass->acquire_buffer))
632     result = pclass->acquire_buffer (pool, buffer, params);
633   else
634     result = GST_FLOW_NOT_SUPPORTED;
635
636   if (G_UNLIKELY (result != GST_FLOW_OK))
637     dec_outstanding (pool);
638
639   return result;
640 }
641
642 static void
643 default_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
644 {
645   /* keep it around in our queue */
646   gst_atomic_queue_push (pool->queue, buffer);
647   gst_poll_write_control (pool->poll);
648 }
649
650 /**
651  * gst_buffer_pool_release_buffer:
652  * @pool: a #GstBufferPool
653  * @buffer: a #GstBuffer
654  *
655  * Release @buffer to @pool. @buffer should have previously been allocated from
656  * @pool with gst_buffer_pool_acquire_buffer().
657  *
658  * This function is usually called automatically when the last ref on @buffer
659  * disappears.
660  */
661 void
662 gst_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
663 {
664   GstBufferPoolClass *pclass;
665
666   g_return_if_fail (GST_IS_BUFFER_POOL (pool));
667   g_return_if_fail (buffer != NULL);
668
669   pclass = GST_BUFFER_POOL_GET_CLASS (pool);
670
671   if (G_LIKELY (pclass->release_buffer))
672     pclass->release_buffer (pool, buffer);
673
674   dec_outstanding (pool);
675 }