Buffer

Buffers are the basic unit of data transfer in GStreamer. They contain the timing and offset along with other arbitrary metadata that is associated with the GstMemory blocks that the buffer contains.

Buffers are usually created with gst_buffer_new(). After a buffer has been created one will typically allocate memory for it and add it to the buffer. The following example creates a buffer that can hold a given video frame with a given width, height and bits per plane.

Alternatively, use gst_buffer_new_allocate() to create a buffer with preallocated data of a given size.

Buffers can contain a list of GstMemory objects. You can retrieve how many memory objects with gst_buffer_n_memory() and you can get a pointer to memory with gst_buffer_peek_memory()

A buffer will usually have timestamps, and a duration, but neither of these are guaranteed (they may be set to GST_CLOCK_TIME_NONE). Whenever a meaningful value can be given for these, they should be set. The timestamps and duration are measured in nanoseconds (they are GstClockTime values).

The buffer DTS refers to the timestamp when the buffer should be decoded and is usually monotonically increasing. The buffer PTS refers to the timestamp when the buffer content should be presented to the user and is not always monotonically increasing.

A buffer can also have one or both of a start and an end offset. These are media-type specific. For video buffers, the start offset will generally be the frame number. For audio buffers, it will be the number of samples produced so far. For compressed data, it could be the byte offset in a source or destination file. Likewise, the end offset will be the offset of the end of the buffer. These can only be meaningfully interpreted if you know the media type of the buffer (the preceeding CAPS event). Either or both can be set to GST_BUFFER_OFFSET_NONE.

gst_buffer_ref() is used to increase the refcount of a buffer. This must be done when you want to keep a handle to the buffer after pushing it to the next element. The buffer refcount determines the writability of the buffer, a buffer is only writable when the refcount is exactly 1, i.e. when the caller has the only reference to the buffer.

To efficiently create a smaller buffer out of an existing one, you can use gst_buffer_copy_region(). This method tries to share the memory objects between the two buffers.

If a plug-in wants to modify the buffer data or metadata in-place, it should first obtain a buffer that is safe to modify by using gst_buffer_make_writable(). This function is optimized so that a copy will only be made when it is necessary.

Several flags of the buffer can be set and unset with the GST_BUFFER_FLAG_SET() and GST_BUFFER_FLAG_UNSET() macros. Use GST_BUFFER_FLAG_IS_SET() to test if a certain GstBufferFlag is set.

Buffers can be efficiently merged into a larger buffer with gst_buffer_append(). Copying of memory will only be done when absolutely needed.

Arbitrary extra metadata can be set on a buffer with gst_buffer_add_meta(). Metadata can be retrieved with gst_buffer_get_meta(). See also GstMeta

An element should either unref the buffer or push it out on a src pad using gst_pad_push() (see GstPad).

Buffers are usually freed by unreffing them with gst_buffer_unref(). When the refcount drops to 0, any memory and metadata pointed to by the buffer is unreffed as well. Buffers allocated from a GstBufferPool will be returned to the pool when the refcount drops to 0.

Last reviewed on 2012-03-28 (0.11.3)

class Buffer {}

Constructors

this
this(GstBuffer* gstBuffer)

Sets our main struct and passes it to the parent class

this
this()

Creates a newly allocated buffer without any data. MT safe.

this
this(Allocator allocator, gsize size, GstAllocationParams* params)

Tries to create a newly allocated buffer with data of the given size and extra parameters from allocator. If the requested amount of memory can't be allocated, NULL will be returned. The allocated buffer memory is not cleared. When allocator is NULL, the default memory allocator will be used. Note that when size == 0, the buffer will not have memory associated with it. MT safe.

this
this(void[] data)

Creates a new buffer that wraps the given data. The memory will be freed with g_free and will be marked writable. MT safe.

this
this(GstMemoryFlags flags, void* data, gsize maxsize, gsize offset, gsize size, void* userData, GDestroyNotify notify)

Allocate a new buffer that wraps the given memory. data must point to maxsize of memory, the wrapped buffer will have the region from offset and size visible. When the buffer is destroyed, notify will be called with user_data. The prefix/padding must be filled with 0 if flags contains GST_MEMORY_FLAG_ZERO_PREFIXED and GST_MEMORY_FLAG_ZERO_PADDED respectively.

Members

Functions

addMeta
GstMeta* addMeta(GstMetaInfo* info, void* params)

Add metadata for info to buffer using the parameters in params.

append
Buffer append(Buffer buf2)

Append all the memory from buf2 to buf1. The result buffer will contain a concatenation of the memory of buf1 and buf2.

appendMemory
void appendMemory(Memory mem)

Append the memory block mem to buffer. This function takes ownership of mem and thus doesn't increase its refcount. This function is identical to gst_buffer_insert_memory() with an index of -1. See gst_buffer_insert_memory() for more details.

appendRegion
Buffer appendRegion(Buffer buf2, gssize offset, gssize size)

Append size bytes at offset from buf2 to buf1. The result buffer will contain a concatenation of the memory of buf1 and the requested region of buf2.

copy
Buffer copy()

Create a copy of the given buffer. This will also make a newly allocated copy of the data the source buffer contains.

copyInto
int copyInto(Buffer src, GstBufferCopyFlags flags, gsize offset, gsize size)

Copies the information from src into dest. If dest already contains memory and flags contains GST_BUFFER_COPY_MEMORY, the memory from src will be appended to dest. flags indicate which fields will be copied.

copyRegion
Buffer copyRegion(GstBufferCopyFlags flags, gsize offset, gsize size)

Creates a sub-buffer from parent at offset and size. This sub-buffer uses the actual memory space of the parent buffer. This function will copy the offset and timestamp fields when the offset is 0. If not, they will be set to GST_CLOCK_TIME_NONE and GST_BUFFER_OFFSET_NONE. If offset equals 0 and size equals the total size of buffer, the duration and offset end fields are also copied. If not they will be set to GST_CLOCK_TIME_NONE and GST_BUFFER_OFFSET_NONE. MT safe.

doref
Buffer doref()

Increases the refcount of the given buffer by one. Note that the refcount affects the writeability of buf and its metadata, see gst_buffer_is_writable(). It is important to note that keeping additional references to GstBuffer instances can potentially increase the number of memcpy operations in a pipeline.

extract
gsize extract(gsize offset, void[] dest)

Copy size bytes starting from offset in buffer to dest.

extractDup
void extractDup(gsize offset, gsize size, void[] dest)

Extracts a copy of at most size bytes the data at offset into a GBytes. dest must be freed using g_free() when done. Since 1.0.10

fill
gsize fill(gsize offset, void[] src)

Copy size bytes from src to buffer at offset.

findMemory
int findMemory(gsize offset, gsize size, uint idx, uint length, gsize skip)

Find the memory blocks that span size bytes starting from offset in buffer. When this function returns TRUE, idx will contain the index of the first memory bock where the byte for offset can be found and length contains the number of memory blocks containing the size remaining bytes. skip contains the number of bytes to skip in the memory bock at idx to get to the byte for offset. size can be -1 to get all the memory blocks after idx.

foreachMeta
int foreachMeta(GstBufferForeachMetaFunc func, void* userData)

Call func with user_data for each meta in buffer. func can modify the passed meta pointer or its contents. The return value of func define if this function returns or if the remaining metadata items in the buffer should be skipped.

getAllMemory
Memory getAllMemory()

Get all the memory block in buffer. The memory blocks will be merged into one large GstMemory.

getBufferStruct
GstBuffer* getBufferStruct()
Undocumented in source. Be warned that the author may not have intended to support it.
getMemory
Memory getMemory(uint idx)

Get the memory block at index idx in buffer.

getMemoryRange
Memory getMemoryRange(uint idx, int length)

Get length memory blocks in buffer starting at idx. The memory blocks will be merged into one large GstMemory. If length is -1, all memory starting from idx is merged.

getMeta
GstMeta* getMeta(GType api)

Get the metadata for api on buffer. When there is no such metadata, NULL is returned.

getSize
gsize getSize()

Get the total size of the memory blocks in buffer.

getSizes
gsize getSizes(gsize offset, gsize maxsize)

Get the total size of the memory blocks in b. When not NULL, offset will contain the offset of the data in the first memory block in buffer and maxsize will contain the sum of the size and offset and the amount of extra padding on the last memory block. offset and maxsize can be used to resize the buffer memory blocks with gst_buffer_resize().

getSizesRange
gsize getSizesRange(uint idx, int length, gsize offset, gsize maxsize)

Get the total size of length memory blocks stating from idx in buffer. When not NULL, offset will contain the offset of the data in the memory block in buffer at idx and maxsize will contain the sum of the size and offset and the amount of extra padding on the memory block at idx + length -1. offset and maxsize can be used to resize the buffer memory blocks with gst_buffer_resize_range().

getStruct
void* getStruct()

the main Gtk struct as a void*

insertMemory
void insertMemory(int idx, Memory mem)

Insert the memory block mem to buffer at idx. This function takes ownership of mem and thus doesn't increase its refcount. Only gst_buffer_get_max_memory() can be added to a buffer. If more memory is added, existing memory blocks will automatically be merged to make room for the new memory.

iterateMeta
GstMeta* iterateMeta(void** state)

Retrieve the next GstMeta after current. If state points to NULL, the first metadata is returned. state will be updated with an opage state pointer

map
int map(GstMapInfo info, GstMapFlags flags)

This function fills info with the GstMapInfo of all merged memory blocks in buffer. flags describe the desired access of the memory. When flags is GST_MAP_WRITE, buffer should be writable (as returned from gst_buffer_is_writable()). When buffer is writable but the memory isn't, a writable copy will automatically be created and returned. The readonly copy of the buffer memory will then also be replaced with this writable copy. The memory in info should be unmapped with gst_buffer_unmap() after usage.

mapRange
int mapRange(uint idx, int length, GstMapInfo info, GstMapFlags flags)

This function fills info with the GstMapInfo of length merged memory blocks starting at idx in buffer. When length is -1, all memory blocks starting from idx are merged and mapped. flags describe the desired access of the memory. When flags is GST_MAP_WRITE, buffer should be writable (as returned from gst_buffer_is_writable()). When buffer is writable but the memory isn't, a writable copy will automatically be created and returned. The readonly copy of the buffer memory will then also be replaced with this writable copy. The memory in info should be unmapped with gst_buffer_unmap() after usage.

memcmp
int memcmp(gsize offset, void[] mem)

Compare size bytes starting from offset in buffer with the memory in mem.

memset
gsize memset(gsize offset, ubyte val, gsize size)

Fill buf with size bytes with val starting from offset.

nMemory
uint nMemory()

Get the amount of memory blocks that this buffer has. This amount is never larger than what gst_buffer_get_max_memory() returns.

peekMemory
Memory peekMemory(uint idx)

Get the memory block at idx in buffer. The memory block stays valid until the memory block in buffer is removed, replaced or merged, typically with any call that modifies the memory in buffer. Since this call does not influence the refcount of the memory, gst_memory_is_writable() can be used to check if buffer is the sole owner of the returned memory.

prependMemory
void prependMemory(Memory mem)

Prepend the memory block mem to buffer. This function takes ownership of mem and thus doesn't increase its refcount. This function is identical to gst_buffer_insert_memory() with an index of 0. See gst_buffer_insert_memory() for more details.

removeAllMemory
void removeAllMemory()

Remove all the memory blocks in buffer.

removeMemory
void removeMemory(uint idx)

Remove the memory block in b at index i.

removeMemoryRange
void removeMemoryRange(uint idx, int length)

Remove length memory blocks in buffer starting from idx. length can be -1, in which case all memory starting from idx is removed.

removeMeta
int removeMeta(GstMeta* meta)

Remove the metadata for meta on buffer.

replaceAllMemory
void replaceAllMemory(Memory mem)

Replaces all memory in buffer with mem.

replaceMemory
void replaceMemory(uint idx, Memory mem)

Replaces the memory block at index idx in buffer with mem.

replaceMemoryRange
void replaceMemoryRange(uint idx, int length, Memory mem)

Replaces length memory blocks in buffer starting at idx with mem. If length is -1, all memory starting from idx will be removed and replaced with mem. buffer should be writable.

resize
void resize(gssize offset, gssize size)

Set the offset and total size of the memory blocks in buffer.

resizeRange
int resizeRange(uint idx, int length, gssize offset, gssize size)

Set the total size of the length memory blocks starting at idx in buffer

setSize
void setSize(gssize size)

Set the total size of the memory blocks in buffer.

unmap
void unmap(GstMapInfo info)

Release the memory previously mapped with gst_buffer_map().

unref
void unref()

Decreases the refcount of the buffer. If the refcount reaches 0, the buffer with the associated metadata and memory will be freed.

Static functions

replace
int replace(Buffer obuf, Buffer nbuf)

Modifies a pointer to a GstBuffer to point to a different GstBuffer. The modification is done atomically (so this is useful for ensuring thread safety in some cases), and the reference counts are updated appropriately (the old buffer is unreffed, the new is reffed). Either nbuf or the GstBuffer pointed to by obuf may be NULL.

Variables

gstBuffer
GstBuffer* gstBuffer;

the main Gtk struct

Meta