From a7e6c7d2a8feb7aedecf91834a4c086262d31198 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Thu, 15 Mar 2012 22:09:02 +0100 Subject: [PATCH] bufferpool: split bufferpool configuration Make separate methods to control the bufferpool and the allocator used by the bufferpool. Make it possible to change the allocator of a pool. --- gst/gstbufferpool.c | 155 ++++++++++++++++++++++--------- gst/gstbufferpool.h | 15 +-- gst/gstmemory.h | 2 +- gst/gstquark.c | 2 +- gst/gstquark.h | 3 +- libs/gst/base/gstbasesrc.c | 4 +- libs/gst/base/gstbasetransform.c | 4 +- win32/common/libgstreamer.def | 6 +- 8 files changed, 131 insertions(+), 60 deletions(-) diff --git a/gst/gstbufferpool.c b/gst/gstbufferpool.c index de484f3d9f..c6ced21e06 100644 --- a/gst/gstbufferpool.c +++ b/gst/gstbufferpool.c @@ -69,6 +69,7 @@ struct _GstBufferPoolPrivate guint size; guint min_buffers; guint max_buffers; + GstAllocator *allocator; GstAllocationParams params; }; @@ -120,20 +121,25 @@ gst_buffer_pool_class_init (GstBufferPoolClass * klass) static void gst_buffer_pool_init (GstBufferPool * pool) { - pool->priv = GST_BUFFER_POOL_GET_PRIVATE (pool); + GstBufferPoolPrivate *priv; - g_rec_mutex_init (&pool->priv->rec_lock); + priv = pool->priv = GST_BUFFER_POOL_GET_PRIVATE (pool); - pool->priv->poll = gst_poll_new_timer (); - pool->priv->queue = gst_atomic_queue_new (10); + g_rec_mutex_init (&priv->rec_lock); + + priv->poll = gst_poll_new_timer (); + priv->queue = gst_atomic_queue_new (10); pool->flushing = 1; - pool->priv->active = FALSE; - pool->priv->configured = FALSE; - pool->priv->started = FALSE; - pool->priv->config = - gst_structure_new_id_empty (GST_QUARK (BUFFER_POOL_CONFIG)); - gst_buffer_pool_config_set (pool->priv->config, NULL, 0, 0, 0, 0, 0, 0); - gst_poll_write_control (pool->priv->poll); + priv->active = FALSE; + priv->configured = FALSE; + priv->started = FALSE; + priv->config = gst_structure_new_id_empty (GST_QUARK (BUFFER_POOL_CONFIG)); + gst_buffer_pool_config_set_params (priv->config, NULL, 0, 0, 0); + priv->allocator = NULL; + gst_allocation_params_init (&priv->params); + gst_buffer_pool_config_set_allocator (priv->config, priv->allocator, + &priv->params); + gst_poll_write_control (priv->poll); GST_DEBUG_OBJECT (pool, "created"); } @@ -142,16 +148,20 @@ static void gst_buffer_pool_finalize (GObject * object) { GstBufferPool *pool; + GstBufferPoolPrivate *priv; pool = GST_BUFFER_POOL_CAST (object); + priv = pool->priv; GST_DEBUG_OBJECT (pool, "finalize"); gst_buffer_pool_set_active (pool, FALSE); - gst_atomic_queue_unref (pool->priv->queue); - gst_poll_free (pool->priv->poll); - gst_structure_free (pool->priv->config); - g_rec_mutex_clear (&pool->priv->rec_lock); + gst_atomic_queue_unref (priv->queue); + gst_poll_free (priv->poll); + gst_structure_free (priv->config); + g_rec_mutex_clear (&priv->rec_lock); + if (priv->allocator) + gst_allocator_unref (priv->allocator); G_OBJECT_CLASS (gst_buffer_pool_parent_class)->finalize (object); } @@ -180,7 +190,8 @@ default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer, { GstBufferPoolPrivate *priv = pool->priv; - *buffer = gst_buffer_new_allocate (NULL, priv->size, &priv->params); + *buffer = + gst_buffer_new_allocate (priv->allocator, priv->size, &priv->params); return GST_FLOW_OK; } @@ -425,11 +436,15 @@ default_set_config (GstBufferPool * pool, GstStructure * config) GstBufferPoolPrivate *priv = pool->priv; const GstCaps *caps; guint size, min_buffers, max_buffers; - guint prefix, padding, align; + GstAllocator *allocator; + GstAllocationParams params; /* parse the config and keep around */ - if (!gst_buffer_pool_config_get (config, &caps, &size, &min_buffers, - &max_buffers, &prefix, &padding, &align)) + if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers, + &max_buffers)) + goto wrong_config; + + if (!gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms)) goto wrong_config; GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config); @@ -437,10 +452,12 @@ default_set_config (GstBufferPool * pool, GstStructure * config) priv->size = size; priv->min_buffers = min_buffers; priv->max_buffers = max_buffers; - gst_allocation_params_init (&priv->params); - priv->params.prefix = prefix; - priv->params.padding = padding; - priv->params.align = align; + + if (priv->allocator) + gst_allocator_unref (priv->allocator); + if ((priv->allocator = allocator)) + gst_allocator_ref (allocator); + priv->params = params; return TRUE; @@ -614,22 +631,18 @@ gst_buffer_pool_has_option (GstBufferPool * pool, const gchar * option) } /** - * gst_buffer_pool_config_set: + * gst_buffer_pool_config_set_params: * @config: a #GstBufferPool configuration * @caps: caps for the buffers * @size: the size of each buffer, not including prefix and padding * @min_buffers: the minimum amount of buffers to allocate. * @max_buffers: the maximum amount of buffers to allocate or 0 for unlimited. - * @prefix: prefix each buffer with this many bytes - * @padding: pad each buffer with this many bytes - * @align: alignment of the buffer data. * * Configure @config with the given parameters. */ void -gst_buffer_pool_config_set (GstStructure * config, const GstCaps * caps, - guint size, guint min_buffers, guint max_buffers, guint prefix, - guint padding, guint align) +gst_buffer_pool_config_set_params (GstStructure * config, const GstCaps * caps, + guint size, guint min_buffers, guint max_buffers) { g_return_if_fail (config != NULL); @@ -637,10 +650,38 @@ gst_buffer_pool_config_set (GstStructure * config, const GstCaps * caps, GST_QUARK (CAPS), GST_TYPE_CAPS, caps, GST_QUARK (SIZE), G_TYPE_UINT, size, GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers, - GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers, - GST_QUARK (PREFIX), G_TYPE_UINT, prefix, - GST_QUARK (PADDING), G_TYPE_UINT, padding, - GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL); + GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers, NULL); +} + +/** + * gst_buffer_pool_config_set_allocator: + * @config: a #GstBufferPool configuration + * @allocator: a #GstAllocator + * @params: #GstAllocationParams + * + * Set the @allocator and @params on @config. + * + * One of @allocator and @params can be NULL, but not both. When @allocator + * is NULL, the default allocator of the pool will use the values in @param + * to perform its allocation. When @param is NULL, the pool will use the + * provided allocator with its default #GstAllocationParams. + * + * A call to gst_buffer_pool_set_config() can update the allocator and params + * with the values that it is able to do. Some pools are, for example, not able + * to operate with different allocators or cannot allocate with the values + * specified in @params. Use gst_buffer_pool_get_config() to get the currently + * used values. + */ +void +gst_buffer_pool_config_set_allocator (GstStructure * config, + GstAllocator * allocator, const GstAllocationParams * params) +{ + g_return_if_fail (config != NULL); + g_return_if_fail (allocator != NULL || params != NULL); + + gst_structure_id_set (config, + GST_QUARK (ALLOCATOR), GST_TYPE_ALLOCATOR, allocator, + GST_QUARK (PARAMS), GST_TYPE_ALLOCATION_PARAMS, params, NULL); } /** @@ -768,22 +809,18 @@ gst_buffer_pool_config_has_option (GstStructure * config, const gchar * option) } /** - * gst_buffer_pool_config_get: + * gst_buffer_pool_config_get_params: * @config: (transfer none): a #GstBufferPool configuration * @caps: (out): the caps of buffers * @size: (out): the size of each buffer, not including prefix and padding * @min_buffers: (out): the minimum amount of buffers to allocate. * @max_buffers: (out): the maximum amount of buffers to allocate or 0 for unlimited. - * @prefix: (out): prefix each buffer with this many bytes - * @padding: (out): pad each buffer with this many bytes - * @align: (out): alignment of the buffer data. * * Get the configuration values from @config. */ gboolean -gst_buffer_pool_config_get (GstStructure * config, const GstCaps ** caps, - guint * size, guint * min_buffers, guint * max_buffers, guint * prefix, - guint * padding, guint * align) +gst_buffer_pool_config_get_params (GstStructure * config, const GstCaps ** caps, + guint * size, guint * min_buffers, guint * max_buffers) { g_return_val_if_fail (config != NULL, FALSE); @@ -791,10 +828,38 @@ gst_buffer_pool_config_get (GstStructure * config, const GstCaps ** caps, GST_QUARK (CAPS), GST_TYPE_CAPS, caps, GST_QUARK (SIZE), G_TYPE_UINT, size, GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers, - GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers, - GST_QUARK (PREFIX), G_TYPE_UINT, prefix, - GST_QUARK (PADDING), G_TYPE_UINT, padding, - GST_QUARK (ALIGN), G_TYPE_UINT, align, NULL); + GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers, NULL); +} + +/** + * gst_buffer_pool_config_get_allocator: + * @config: (transfer none): a #GstBufferPool configuration + * @allocator: (transfer none): a #GstAllocator + * @params: #GstAllocationParams + * + * Get the allocator and params from @config. + */ +gboolean +gst_buffer_pool_config_get_allocator (GstStructure * config, + GstAllocator ** allocator, GstAllocationParams * params) +{ + g_return_val_if_fail (config != NULL, FALSE); + + if (allocator) + *allocator = g_value_get_boxed (gst_structure_id_get_value (config, + GST_QUARK (ALLOCATOR))); + if (params) { + GstAllocationParams *p; + + p = g_value_get_boxed (gst_structure_id_get_value (config, + GST_QUARK (PARAMS))); + if (p) { + *params = *p; + } else { + gst_allocation_params_init (params); + } + } + return TRUE; } static GstFlowReturn diff --git a/gst/gstbufferpool.h b/gst/gstbufferpool.h index f30ccd2a70..f7ff22fbd4 100644 --- a/gst/gstbufferpool.h +++ b/gst/gstbufferpool.h @@ -179,13 +179,16 @@ GstStructure * gst_buffer_pool_get_config (GstBufferPool *pool); const gchar ** gst_buffer_pool_get_options (GstBufferPool *pool); gboolean gst_buffer_pool_has_option (GstBufferPool *pool, const gchar *option); + /* helpers for configuring the config structure */ -void gst_buffer_pool_config_set (GstStructure *config, const GstCaps *caps, - guint size, guint min_buffers, guint max_buffers, - guint prefix, guint padding, guint align); -gboolean gst_buffer_pool_config_get (GstStructure *config, const GstCaps **caps, - guint *size, guint *min_buffers, guint *max_buffers, - guint *prefix, guint *padding, guint *align); +void gst_buffer_pool_config_set_params (GstStructure *config, const GstCaps *caps, + guint size, guint min_buffers, guint max_buffers); +gboolean gst_buffer_pool_config_get_params (GstStructure *config, const GstCaps **caps, + guint *size, guint *min_buffers, guint *max_buffers); +void gst_buffer_pool_config_set_allocator (GstStructure *config, GstAllocator *allocator, + const GstAllocationParams *params); +gboolean gst_buffer_pool_config_get_allocator (GstStructure *config, GstAllocator **allocator, + GstAllocationParams *params); /* options */ guint gst_buffer_pool_config_n_options (GstStructure *config); diff --git a/gst/gstmemory.h b/gst/gstmemory.h index 7f46e3e5b7..219831f0ef 100644 --- a/gst/gstmemory.h +++ b/gst/gstmemory.h @@ -35,7 +35,7 @@ GType gst_memory_get_type(void); #define GST_TYPE_ALLOCATOR (gst_allocator_get_type()) GType gst_allocator_get_type(void); -#define GST_TYPE_ALLOCATOR_PARAMS (gst_allocation_params_get_type()) +#define GST_TYPE_ALLOCATION_PARAMS (gst_allocation_params_get_type()) GType gst_allocation_params_get_type(void); typedef struct _GstMemory GstMemory; diff --git a/gst/gstquark.c b/gst/gstquark.c index 8303a4a939..f7182ccfbe 100644 --- a/gst/gstquark.c +++ b/gst/gstquark.c @@ -56,7 +56,7 @@ static const gchar *_quark_strings[] = { "GstEventReconfigure", "segment", "GstQueryScheduling", "pull-mode", "allocator", "GstEventFlushStop", "options", "GstQueryAcceptCaps", "result", "GstQueryCaps", "filter", "modes", "GstEventStreamConfig", - "setup-data", "stream-headers", "GstEventGap", "GstQueryDrain" + "setup-data", "stream-headers", "GstEventGap", "GstQueryDrain", "params" }; GQuark _priv_gst_quark_table[GST_QUARK_MAX]; diff --git a/gst/gstquark.h b/gst/gstquark.h index 43f80e5617..376a8cbb20 100644 --- a/gst/gstquark.h +++ b/gst/gstquark.h @@ -163,7 +163,8 @@ typedef enum _GstQuarkId GST_QUARK_STREAM_HEADERS = 134, GST_QUARK_EVENT_GAP = 135, GST_QUARK_QUERY_DRAIN = 136, - GST_QUARK_MAX = 137 + GST_QUARK_PARAMS = 137, + GST_QUARK_MAX = 138 } GstQuarkId; extern GQuark _priv_gst_quark_table[GST_QUARK_MAX]; diff --git a/libs/gst/base/gstbasesrc.c b/libs/gst/base/gstbasesrc.c index c8a058fb4e..2abddcde7b 100644 --- a/libs/gst/base/gstbasesrc.c +++ b/libs/gst/base/gstbasesrc.c @@ -2821,8 +2821,8 @@ gst_base_src_prepare_allocation (GstBaseSrc * basesrc, GstCaps * caps) GstStructure *config; config = gst_buffer_pool_get_config (pool); - gst_buffer_pool_config_set (config, caps, size, min, max, params.prefix, - params.padding, params.align); + gst_buffer_pool_config_set_params (config, caps, size, min, max); + gst_buffer_pool_config_set_allocator (config, allocator, ¶ms); gst_buffer_pool_set_config (pool, config); } diff --git a/libs/gst/base/gstbasetransform.c b/libs/gst/base/gstbasetransform.c index 643b18e692..f7abb61ef1 100644 --- a/libs/gst/base/gstbasetransform.c +++ b/libs/gst/base/gstbasetransform.c @@ -899,8 +899,8 @@ gst_base_transform_do_bufferpool (GstBaseTransform * trans, GstCaps * outcaps) GstStructure *config; config = gst_buffer_pool_get_config (pool); - gst_buffer_pool_config_set (config, outcaps, size, min, max, params.prefix, - params.padding, params.align); + gst_buffer_pool_config_set_params (config, outcaps, size, min, max); + gst_buffer_pool_config_set_allocator (config, allocator, ¶ms); gst_buffer_pool_set_config (pool, config); } /* and store */ diff --git a/win32/common/libgstreamer.def b/win32/common/libgstreamer.def index 3123ad17dc..ca592f7602 100644 --- a/win32/common/libgstreamer.def +++ b/win32/common/libgstreamer.def @@ -124,11 +124,13 @@ EXPORTS gst_buffer_pool_acquire_buffer gst_buffer_pool_acquire_flags_get_type gst_buffer_pool_config_add_option - gst_buffer_pool_config_get + gst_buffer_pool_config_get_allocator gst_buffer_pool_config_get_option + gst_buffer_pool_config_get_params gst_buffer_pool_config_has_option gst_buffer_pool_config_n_options - gst_buffer_pool_config_set + gst_buffer_pool_config_set_allocator + gst_buffer_pool_config_set_params gst_buffer_pool_get_config gst_buffer_pool_get_options gst_buffer_pool_get_type