Table of Contents

Class VersatileBuffer

Namespace
VAST.Common
Assembly
VAST.Common.dll

Represents an abstract buffer for holding media data and general-purpose data.

public abstract class VersatileBuffer
Inheritance
VersatileBuffer
Inherited Members
Extension Methods

Remarks

This class does not provide direct access to the underlying data storage in order to virtualize access and allow wrapping of both managed and unmanaged data. Concrete implementations handle platform-specific memory management.

Fields

EndPointAttribute

ExtraData key for endpoint information.

public static readonly string EndPointAttribute

Field Value

string

FilePositionAttribute

ExtraData key for file position.

public static readonly string FilePositionAttribute

Field Value

string

FlagsAttribute

ExtraData key for buffer flags.

public static readonly string FlagsAttribute

Field Value

string

LocalEndPointAttribute

ExtraData key for local endpoint information.

public static readonly string LocalEndPointAttribute

Field Value

string

MetadataAttribute

ExtraData key for metadata dictionary.

public static readonly string MetadataAttribute

Field Value

string

NewMediaTypeAttribute

ExtraData key for new media type notification.

public static readonly string NewMediaTypeAttribute

Field Value

string

NtpTimestampAttribute

ExtraData key for NTP timestamp.

public static readonly string NtpTimestampAttribute

Field Value

string

PoisAttribute

ExtraData key for points of interest.

public static readonly string PoisAttribute

Field Value

string

ProgramIndexAttribute

ExtraData key for program index in multi-program streams.

public static readonly string ProgramIndexAttribute

Field Value

string

RemoteEndPointAttribute

ExtraData key for remote endpoint information.

public static readonly string RemoteEndPointAttribute

Field Value

string

Properties

ActualSize

Gets or sets the currently used buffer size in bytes.

public virtual int ActualSize { get; set; }

Property Value

int

BufferType

Gets the type of the specific implementation of this buffer.

public VersatileBuffer.BufferType_t BufferType { get; protected set; }

Property Value

VersatileBuffer.BufferType_t

Remarks

Used for fast buffer type detection without involving reflection.

CleanPoint

Gets or sets a value indicating whether this sample is a clean point.

public bool CleanPoint { get; set; }

Property Value

bool

Discontinuity

Gets or sets a value indicating whether this is a discontinuity sample.

public bool Discontinuity { get; set; }

Property Value

bool

Dts

Gets or sets the Decoding Timestamp (DTS) of the media sample.

public long Dts { get; set; }

Property Value

long

Duration

Gets or sets the duration of the media sample.

public long Duration { get; set; }

Property Value

long

ExtraData

Gets the dictionary of extra data associated with this buffer.

public Dictionary<string, object> ExtraData { get; }

Property Value

Dictionary<string, object>

Id

Gets the buffer's unique identifier.

public long Id { get; protected set; }

Property Value

long

IsDirectPointer

Gets a value indicating whether the Pointer property is a direct unmanaged pointer to the actual data storage.

public bool IsDirectPointer { get; protected set; }

Property Value

bool

Remarks

If this property is false, ReleasePointer(bool) must be called immediately after each data usage to synchronize temporary unmanaged data with the actual data storage and to prevent holding a temporary buffer for too long.

this[int]

Gets or sets byte at the specified position of the buffer

public byte this[int position] { get; set; }

Parameters

position int

Position to read byte from or write byte to

Property Value

byte

Byte value at the specified position

KeyFrame

Gets or sets a value indicating whether this sample is a key frame.

public bool KeyFrame { get; set; }

Property Value

bool

Pointer

Gets the unmanaged pointer to the underlying buffer.

public abstract nint Pointer { get; }

Property Value

nint

Remarks

This pointer should only be used when there is no alternative, such as when a pointer must be provided to unmanaged code. A successful call to LockPointer() is mandatory before using this property. The ReleasePointer(bool) must be called when the pointer is no longer needed to prevent managed buffer being pinned for too long.

Position

Gets or sets current position in a buffer. This is just a cursor used by some long lasting operations to store current state.

public virtual int Position { get; set; }

Property Value

int

Pts

Gets or sets the Presentation Timestamp (PTS) of the media sample.

public long Pts { get; set; }

Property Value

long

RefCount

Gets the current reference count of the buffer.

public abstract int RefCount { get; }

Property Value

int

Size

Gets the total buffer size in bytes.

public int Size { get; protected set; }

Property Value

int

StreamIndex

Gets or sets the stream index.

public int StreamIndex { get; set; }

Property Value

int

Methods

AddRef()

Increments the reference count by one.

public abstract int AddRef()

Returns

int

The current reference count after incrementing.

Append(byte[])

Appends data from specified array to current buffer and adjusts actual data size

public abstract void Append(byte[] data)

Parameters

data byte[]

Array to append data from

Append(byte[], int, int)

Appends data from specified array to current buffer and adjusts actual data size

public abstract void Append(byte[] data, int offset, int size)

Parameters

data byte[]

Array to append data from

offset int

Offset to start copying from

size int

Number of bytes to append

Append(Stream, int)

Appends data from specified stream to current buffer and adjusts actual data size

public abstract void Append(Stream stream, int size)

Parameters

stream Stream

Stream to append data from

size int

Number of bytes to append

Append(nint, int)

Appends data from specified unmanaged pointer to current buffer and adjusts actual data size

public abstract void Append(nint data, int size)

Parameters

data nint

Data pointer to append data from

size int

Number of bytes to append

Append(ReadOnlySpan<byte>)

Appends data from specified span to current buffer and adjusts actual data size

public abstract void Append(ReadOnlySpan<byte> data)

Parameters

data ReadOnlySpan<byte>

Span to append data from

Append(EndianBinaryReader, int)

Appends data from specified reader to current buffer and adjusts actual data size

public abstract void Append(EndianBinaryReader reader, int size)

Parameters

reader EndianBinaryReader

Reader to append data from

size int

Number of bytes to append

Append(VersatileBuffer, int, int)

Appends data from specified buffer to current buffer and adjusts actual data size

public abstract void Append(VersatileBuffer buffer, int offset, int size)

Parameters

buffer VersatileBuffer

Buffer to append data from

offset int

Offset to start copying from

size int

Number of bytes to append

CleanUp()

Resets the buffer to its initial empty state.

public virtual void CleanUp()

Clone(bool)

Creates a copy of this buffer.

public abstract VersatileBuffer Clone(bool shallow = false)

Parameters

shallow bool

If true, creates a shallow copy that shares the underlying data; if false, creates a deep copy with its own data.

Returns

VersatileBuffer

A new buffer instance that is a copy of this buffer.

CopyAttributes(VersatileBuffer)

Copies all attributes (excluding data) from this buffer to the specified buffer.

public virtual void CopyAttributes(VersatileBuffer copyTo)

Parameters

copyTo VersatileBuffer

The target buffer to copy attributes to.

CopyTo(int, byte[], int, int)

Copies specified number of bytes from current buffer to specified managed buffer.

public abstract void CopyTo(int fromPosition, byte[] data, int offset, int size)

Parameters

fromPosition int

Internal buffer position to copy data from

data byte[]

Array to copy data to

offset int

Offset to start copying to

size int

Number of bytes to copy

CopyTo(int, Stream, int)

Copies specified number of bytes from current buffer to specified stream.

public abstract void CopyTo(int fromPosition, Stream stream, int size)

Parameters

fromPosition int

Internal buffer position to copy data from

stream Stream

Stream to copy data to

size int

Number of bytes to copy

CopyTo(int, nint, int)

Copies specified number of bytes from current buffer to specified unmanaged pointer.

public abstract void CopyTo(int fromPosition, nint data, int size)

Parameters

fromPosition int

Internal buffer position to copy data from

data nint

Data pointer to copy data to

size int

Number of bytes to copy

CopyTo(int, Span<byte>)

Copies specified number of bytes from current buffer to specified managed buffer.

public abstract void CopyTo(int fromPosition, Span<byte> data)

Parameters

fromPosition int

Internal buffer position to copy data from

data Span<byte>

Buffer to copy data to

Fill(int, int, byte)

Fills specified range with specified value.

public abstract void Fill(int fromPosition, int size, byte fillValue)

Parameters

fromPosition int

Internal buffer position to fill from

size int

Number of bytes to fill

fillValue byte

Value to fill

Flush(int)

Flushes the buffer data before the specified position and moves remaining data to the start of the buffer. Actual data size is adjusted accordingly.

public abstract void Flush(int bytesToRemove = -1)

Parameters

bytesToRemove int

Number of bytes to remove. If it's equal to -1 then current buffer position is used.

GetByte(int)

Gets byte at the specified position of the buffer

public abstract byte GetByte(int position)

Parameters

position int

Position to read byte from

Returns

byte

Byte value at the specified position

Insert(byte[], bool)

Inserts data from specified array to current buffer at current position. Actual data size remains intact.

public abstract void Insert(byte[] data, bool adjustPosition = true)

Parameters

data byte[]

Array to insert data from

adjustPosition bool

Whether to adjust current position after the operation is done

Insert(byte[], int, int, bool)

Inserts data from specified array to current buffer at current position. Actual data size remains intact.

public abstract void Insert(byte[] data, int offset, int size, bool adjustPosition = true)

Parameters

data byte[]

Array to insert data from

offset int

Offset to start copying from

size int

Number of bytes to insert

adjustPosition bool

Whether to adjust current position after the operation is done

Insert(int, byte[], int, int, bool)

Inserts data from specified array to current buffer at specified position. Actual data size remains intact.

public abstract void Insert(int copyToPosition, byte[] data, int offset, int size, bool adjustPosition = true)

Parameters

copyToPosition int

Local buffer position to start insertion from

data byte[]

Array to insert data from

offset int

Offset to start copying from

size int

Number of bytes to insert

adjustPosition bool

Whether to adjust current position after the operation is done

Insert(int, VersatileBuffer, int, int, bool)

Inserts data from specified buffer to current buffer at specified position. Actual data size remains intact.

public abstract void Insert(int copyToPosition, VersatileBuffer buffer, int offset, int size, bool adjustPosition = true)

Parameters

copyToPosition int

Local buffer position to start insertion from

buffer VersatileBuffer

Buffer to insert data from

offset int

Offset to start copying from

size int

Number of bytes to insert

adjustPosition bool

Whether to adjust current position after the operation is done

LockPointer()

Initializes unmanaged pointer to the underlying buffer. This function must only be called if there is no other choice, such as when pointer must be provided to unmanaged code. If the function returns false then pointer has not been initialized and therefore Pointer property must not be used. For example, false can be returned when LOH compaction is in progress.

public abstract bool LockPointer()

Returns

bool

True if operation was successful and pointer has been initialized, false otherwise

ReadFrom(Stream)

Reads data from the specified stream and de-serialize object

public abstract void ReadFrom(Stream stream)

Parameters

stream Stream

Stream to read object from

ReadFrom(EndianBinaryReader)

Reads data using the specified reader and de-serialize object

public abstract void ReadFrom(EndianBinaryReader reader)

Parameters

reader EndianBinaryReader

Reader to read object with

Release()

Decrements the reference count by one.

public abstract int Release()

Returns

int

The current reference count after decrementing.

Remarks

When the reference count reaches zero, the buffer is returned to its allocator.

ReleasePointer(bool)

Releases unmanaged pointer to the underlying buffer.

public abstract void ReleasePointer(bool bufferDirty)

Parameters

bufferDirty bool

Whether unmanaged buffer is dirty. Can be used to copy updated data back to actual buffer if proxy buffer is used to provide unmanaged pointer.

Rotate(long)

Replaces current buffer with another buffer from the same allocator if it's locked for too long. Necessary for long locked buffers to allow allocator to de-allocate memory when possible and subsequent compaction during off-peak hours. Method copies data and attributes from current buffer to the new one.

public abstract VersatileBuffer Rotate(long mediaTime = -9223372036854775808)

Parameters

mediaTime long

Returns

VersatileBuffer

The new buffer if current one was locked for too long or the same buffer otherwise

SetByte(int, byte)

Sets byte at the specified position of the buffer

public abstract void SetByte(int position, byte val)

Parameters

position int

Position to write byte to

val byte

Byte value

WriteTo(Stream)

Serializes object itself and all contained data and writes it into the specified stream

public abstract void WriteTo(Stream stream)

Parameters

stream Stream

Stream to write object to

WriteTo(EndianBinaryWriter)

Serializes object itself and all contained data and writes it using the specified writer

public abstract void WriteTo(EndianBinaryWriter writer)

Parameters

writer EndianBinaryWriter

Writer to write object with