BufferPool
Superclasses: Object
, InitiallyUnowned
, Object
A BufferPool
is an object that can be used to pre-allocate and recycle
buffers of the same size and with the same properties.
A BufferPool
is created with new()
.
Once a pool is created, it needs to be configured. A call to
get_config()
returns the current configuration structure from
the pool. With config_set_params()
and
config_set_allocator()
the bufferpool parameters and
allocator can be configured. Other properties can be configured in the pool
depending on the pool implementation.
A bufferpool can have extra options that can be enabled with
config_add_option()
. The available options can be retrieved
with get_options()
. Some options allow for additional
configuration properties to be set.
After the configuration structure has been configured,
set_config()
updates the configuration in the pool. This can
fail when the configuration structure is not accepted.
After the pool has been configured, it can be activated with
set_active()
. This will preallocate the configured resources
in the pool.
When the pool is active, acquire_buffer()
can be used to
retrieve a buffer from the pool.
Buffers allocated from a bufferpool will automatically be returned to the
pool with release_buffer()
when their refcount drops to 0.
The bufferpool can be deactivated again with set_active()
.
All further acquire_buffer()
calls will return an error. When
all buffers are returned to the pool they will be freed.
Constructors
- class BufferPool
- classmethod new() BufferPool
Creates a new
BufferPool
instance.
Methods
- class BufferPool
- acquire_buffer(params: BufferPoolAcquireParams | None = None) tuple[FlowReturn, Buffer]
Acquires a buffer from
pool
.buffer
should point to a memory location that can hold a pointer to the new buffer. When the pool is empty, this function will by default block until a buffer is released into the pool again or when the pool is set to flushing or deactivated.params
can contain optional parameters to influence the allocation.- Parameters:
params – parameters.
- config_add_option(config: Structure, option: str) None
Enables the option in
config
. This will instruct thebufferpool
to enable the specified option on the buffers that it allocates.The options supported by
pool
can be retrieved withget_options()
.- Parameters:
config – a
BufferPool
configurationoption – an option to add
- config_get_allocator(config: Structure) tuple[bool, Allocator, AllocationParams]
Gets the
allocator
andparams
fromconfig
.- Parameters:
config – a
BufferPool
configuration
- config_get_option(config: Structure, index: int) str | None
Parses an available
config
and gets the option atindex
of the options API array.- Parameters:
config – a
BufferPool
configurationindex – position in the option array to read
- config_get_params(config: Structure) tuple[bool, Caps, int, int, int]
Gets the configuration values from
config
.- Parameters:
config – a
BufferPool
configuration
- config_has_option(config: Structure, option: str) bool
Checks if
config
containsoption
.- Parameters:
config – a
BufferPool
configurationoption – an option
- config_n_options(config: Structure) int
Retrieves the number of values currently stored in the options array of the
config
structure.- Parameters:
config – a
BufferPool
configuration
- config_set_allocator(config: Structure, allocator: Allocator | None = None, params: AllocationParams | None = None) None
Sets the
allocator
andparams
onconfig
.One of
allocator
andparams
can beNone
, but not both. Whenallocator
isNone
, the default allocator of the pool will use the values inparam
to perform its allocation. Whenparam
isNone
, the pool will use the providedallocator
with its defaultAllocationParams
.A call to
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 inparams
. Useget_config()
to get the currently used values.- Parameters:
config – a
BufferPool
configurationallocator – a
Allocator
params –
AllocationParams
- config_set_params(config: Structure, caps: Caps | None, size: int, min_buffers: int, max_buffers: int) None
Configures
config
with the given parameters.- Parameters:
config – a
BufferPool
configurationcaps – 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.
- config_validate_params(config: Structure, caps: Caps | None, size: int, min_buffers: int, max_buffers: int) bool
Validates that changes made to
config
are still valid in the context of the expected parameters. This function is a helper that can be used to validate changes made by a pool to a config whenset_config()
returnsFalse
. This expects thatcaps
haven’t changed and thatmin_buffers
aren’t lower then what we initially expected. This does not check if options or allocator parameters are still valid, won’t check if size have changed, since changing the size is valid to adapt padding.Added in version 1.4.
- Parameters:
config – a
BufferPool
configurationcaps – the excepted caps of buffers
size – the expected size of each buffer, not including prefix and padding
min_buffers – the expected minimum amount of buffers to allocate.
max_buffers – the expect maximum amount of buffers to allocate or 0 for unlimited.
- get_config() Structure
Gets a copy of the current configuration of the pool. This configuration can be modified and used for the
set_config()
call.
- get_options() list[str]
Gets a
None
terminated array of string with supported bufferpool options forpool
. An option would typically be enabled withconfig_add_option()
.
- has_option(option: str) bool
Checks if the bufferpool supports
option
.- Parameters:
option – an option
- is_active() bool
Checks if
pool
is active. A pool can be activated with theset_active()
call.
- release_buffer(buffer: Buffer) None
Releases
buffer
topool
.buffer
should have previously been allocated frompool
withacquire_buffer()
.This function is usually called automatically when the last ref on
buffer
disappears.- Parameters:
buffer – a
Buffer
- set_active(active: bool) bool
Controls the active state of
pool
. When the pool is inactive, new calls toacquire_buffer()
will return withFLUSHING
.Activating the bufferpool will preallocate all resources in the pool based on the configuration of the pool.
Deactivating will free the resources again when there are no outstanding buffers. When there are outstanding buffers, they will be freed as soon as they are all returned to the pool.
- Parameters:
active – the new active state
- set_config(config: Structure) bool
Sets the configuration of the pool. If the pool is already configured, and the configuration hasn’t changed, this function will return
True
. If the pool is active, this method will returnFalse
and active configuration will remain. Buffers allocated from this pool must be returned or else this function will do nothing and returnFalse
.config
is aStructure
that contains the configuration parameters for the pool. A default and mandatory set of parameters can be configured withconfig_set_params()
,config_set_allocator()
andconfig_add_option()
.If the parameters in
config
can not be set exactly, this function returnsFalse
and will try to update as much state as possible. The new state can then be retrieved and refined withget_config()
.This function takes ownership of
config
.- Parameters:
config – a
Structure
Virtual Methods
- class BufferPool
- do_acquire_buffer(params: BufferPoolAcquireParams | None = None) tuple[FlowReturn, Buffer]
Acquires a buffer from
pool
.buffer
should point to a memory location that can hold a pointer to the new buffer. When the pool is empty, this function will by default block until a buffer is released into the pool again or when the pool is set to flushing or deactivated.params
can contain optional parameters to influence the allocation.- Parameters:
params – parameters.
- do_alloc_buffer(params: BufferPoolAcquireParams | None = None) tuple[FlowReturn, Buffer]
Allocate a buffer. the default implementation allocates buffers from the configured memory allocator and with the configured parameters. All metadata that is present on the allocated buffer will be marked as
GST_META_FLAG_POOLED
andGST_META_FLAG_LOCKED
and will not be removed from the buffer inGstBufferPoolClass
::reset_buffer. The buffer should have theGST_BUFFER_FLAG_TAG_MEMORY
cleared.- Parameters:
params – parameters.
- do_free_buffer(buffer: Buffer) None
Free a buffer. The default implementation unrefs the buffer.
- Parameters:
buffer – the
Buffer
to free
- do_get_options() list[str]
Gets a
None
terminated array of string with supported bufferpool options forpool
. An option would typically be enabled withconfig_add_option()
.
- do_release_buffer(buffer: Buffer) None
Releases
buffer
topool
.buffer
should have previously been allocated frompool
withacquire_buffer()
.This function is usually called automatically when the last ref on
buffer
disappears.- Parameters:
buffer – a
Buffer
- do_reset_buffer(buffer: Buffer) None
Reset the buffer to its state when it was freshly allocated. The default implementation will clear the flags, timestamps and will remove the metadata without the
GST_META_FLAG_POOLED
flag (even the metadata withGST_META_FLAG_LOCKED
). If theGST_BUFFER_FLAG_TAG_MEMORY
was set, this function can also try to restore the memory and clear theGST_BUFFER_FLAG_TAG_MEMORY
again.- Parameters:
buffer – the
Buffer
to reset
- do_set_config(config: Structure) bool
Sets the configuration of the pool. If the pool is already configured, and the configuration hasn’t changed, this function will return
True
. If the pool is active, this method will returnFalse
and active configuration will remain. Buffers allocated from this pool must be returned or else this function will do nothing and returnFalse
.config
is aStructure
that contains the configuration parameters for the pool. A default and mandatory set of parameters can be configured withconfig_set_params()
,config_set_allocator()
andconfig_add_option()
.If the parameters in
config
can not be set exactly, this function returnsFalse
and will try to update as much state as possible. The new state can then be retrieved and refined withget_config()
.This function takes ownership of
config
.- Parameters:
config – a
Structure