MediaBuffer

The MediaBuffer class provides a buffer for media data.

MediaBuffer has a capacity (size of the buffer) and a data size (size of valid data in the buffer). The buffer is a continuous block of memory, and valid data is also a continuous block that can start at any offset in the buffer.

Class

class avblocks.MediaBuffer(data: bytes | bytearray | None = None, buffer_size: int | None = None)

Bases: object

A buffer for media data.

MediaBuffer has a capacity which is the size of the buffer and data size which is the size of the actual data in the buffer. The buffer is a continuous block of memory. The valid data is also a continuous block. The data can start at any offset in the buffer.

attach(buffer: bytes | bytearray, set_data: bool = True) bool

Attaches an external buffer storage.

Parameters:
  • buffer – A byte array that should be used as external storage

  • set_data – Specifies whether to auto set data with a size equal to the buffer size. If True, the data size is set to the buffer size. If False, data size is 0.

Returns:

True if the buffer is attached successfully, False otherwise

property capacity: int

The total capacity of the buffer.

clone() MediaBuffer

Creates a deep copy of this object.

Returns:

A new MediaBuffer object

Remarks:

Only the reference to the external buffer is copied when MediaBuffer does not own its data buffer (uses external/attached data). When MediaBuffer owns its data, a new copy of the data is created.

property data: memoryview | None

Returns a view of the valid data portion of the buffer. This is effectively the same as start[data_offset:data_offset+data_size]

property data_offset: int

The offset at which valid data starts in the data buffer.

property data_size: int

The size of the data that is still valid in the data buffer.

detach() bytes | bytearray | None

Detaches an external data buffer.

Returns:

A reference to the detached buffer or None if no external buffer has been attached

property external: bool

Indicates that MediaBuffer uses an external data buffer.

reset_data()

Reset data offset and size to zero.

set_data(data_offset: int, data_size: int) bool

This is a convenience method that sets buffer data offset and size at once.

Parameters:
  • data_offset – The offset in the buffer where valid data starts

  • data_size – The size in bytes of the valid data in the buffer

Returns:

True if data_offset and data_size are accepted and valid data is set, False otherwise

property start: bytes | bytearray | None

Data buffer.