bufferpool: refactor free_buffer
authorWim Taymans <wtaymans@redhat.com>
Tue, 25 Feb 2014 16:46:49 +0000 (17:46 +0100)
committerWim Taymans <wtaymans@redhat.com>
Thu, 27 Feb 2014 14:43:13 +0000 (15:43 +0100)
Make a do_free_buffer method to also decrements the number of allocated
buffers. Stop will now be successful when all buffers are freed.

gst/gstbufferpool.c

index 6e25e46..b0cffaf 100644 (file)
@@ -359,27 +359,36 @@ default_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
   gst_buffer_unref (buffer);
 }
 
+static void
+do_free_buffer (GstBufferPool * pool, GstBuffer * buffer)
+{
+  GstBufferPoolPrivate *priv;
+  GstBufferPoolClass *pclass;
+
+  priv = pool->priv;
+  pclass = GST_BUFFER_POOL_GET_CLASS (pool);
+
+  g_atomic_int_add (&priv->cur_buffers, -1);
+  GST_LOG_OBJECT (pool, "freeing buffer %p (%u left)", buffer,
+      priv->cur_buffers);
+
+  if (G_LIKELY (pclass->free_buffer))
+    pclass->free_buffer (pool, buffer);
+}
+
 /* must be called with the lock */
 static gboolean
 default_stop (GstBufferPool * pool)
 {
   GstBufferPoolPrivate *priv = pool->priv;
   GstBuffer *buffer;
-  GstBufferPoolClass *pclass;
-
-  pclass = GST_BUFFER_POOL_GET_CLASS (pool);
 
   /* clear the pool */
   while ((buffer = gst_atomic_queue_pop (priv->queue))) {
-    GST_LOG_OBJECT (pool, "freeing buffer %p", buffer);
     gst_poll_read_control (priv->poll);
-
-    if (G_LIKELY (pclass->free_buffer))
-      pclass->free_buffer (pool, buffer);
+    do_free_buffer (pool, buffer);
   }
-  priv->cur_buffers = 0;
-
-  return TRUE;
+  return priv->cur_buffers == 0;
 }
 
 /* must be called with the lock */