JackTrip
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
RingBuffer Class Reference

Provides a ring-buffer (or circular-buffer) that can be written to and read from asynchronously (blocking) or synchronously (non-blocking). More...

#include <RingBuffer.h>

Inheritance diagram for RingBuffer:
Inheritance graph
[legend]

Public Member Functions

 RingBuffer (int SlotSize, int NumSlots)
 The class constructor.
virtual ~RingBuffer ()
 The class destructor.
void insertSlotBlocking (const int8_t *ptrToSlot)
 Insert a slot into the RingBuffer from ptrToSlot. This method will block until there's space in the buffer.
void readSlotBlocking (int8_t *ptrToReadSlot)
 Read a slot from the RingBuffer into ptrToReadSlot. This method will block until there's space in the buffer.
void insertSlotNonBlocking (const int8_t *ptrToSlot)
 Same as insertSlotBlocking but non-blocking (asynchronous)
void readSlotNonBlocking (int8_t *ptrToReadSlot)
 Same as readSlotBlocking but non-blocking (asynchronous)

Protected Member Functions

virtual void setUnderrunReadSlot (int8_t *ptrToReadSlot)
 Sets the memory in the Read Slot when uderrun occurs. By default, this sets it to 0. Override this method in a subclass for a different behavior.
virtual void setMemoryInReadSlotWithLastReadSlot (int8_t *ptrToReadSlot)
 Uses the last read slot to set the memory in the Read Slot.

Private Member Functions

void underrunReset ()
 Resets the ring buffer for reads under-runs non-blocking.
void overflowReset ()
 Resets the ring buffer for writes over-flows non-blocking.
void debugDump () const
 Helper method to debug, prints member variables to terminal.

Private Attributes

const int mSlotSize
 The size of one slot in byes.
const int mNumSlots
 Number of Slots.
const int mTotalSize
 Total size of the mRingBuffer = mSlotSize*mNumSlotss.
int mReadPosition
 Read Positions in the RingBuffer (Tail)
int mWritePosition
 Write Position in the RingBuffer (Head)
int mFullSlots
 Number of used (full) slots, in slot-size.
int8_tmRingBuffer
 8-bit array of data (1-byte)
int8_tmLastReadSlot
 Last slot read.
QMutex mMutex
 Mutex to protect read and write operations.
QWaitCondition mBufferIsNotFull
 Buffer not full condition to monitor threads.
QWaitCondition mBufferIsNotEmpty
 Buffer not empty condition to monitor threads.

Detailed Description

Provides a ring-buffer (or circular-buffer) that can be written to and read from asynchronously (blocking) or synchronously (non-blocking).

The RingBuffer is an array of NumSlots slots of memory each of which is of size SlotSize bytes (8-bits). Slots can be read and written asynchronously/synchronously by multiple threads.

Constructor & Destructor Documentation

RingBuffer::RingBuffer ( int  SlotSize,
int  NumSlots 
)

The class constructor.

Parameters
SlotSizeSize of one slot in bytes
NumSlotsNumber of slots
RingBuffer::~RingBuffer ( )
virtual

The class destructor.

Member Function Documentation

void RingBuffer::debugDump ( ) const
private

Helper method to debug, prints member variables to terminal.

void RingBuffer::insertSlotBlocking ( const int8_t ptrToSlot)

Insert a slot into the RingBuffer from ptrToSlot. This method will block until there's space in the buffer.

The caller is responsible to make sure sizeof(WriteSlot) = SlotSize. This method should be use when the caller can block against its output, like sending/receiving UDP packets. It shouldn't be used by audio. For that, use the insertSlotNonBlocking.

Parameters
ptrToSlotPointer to slot to insert into the RingBuffer
void RingBuffer::insertSlotNonBlocking ( const int8_t ptrToSlot)

Same as insertSlotBlocking but non-blocking (asynchronous)

Parameters
ptrToSlotPointer to slot to insert into the RingBuffer
Todo:
It may be better here to insert the slot anyways, instead of not writing anything
void RingBuffer::overflowReset ( )
private

Resets the ring buffer for writes over-flows non-blocking.

void RingBuffer::readSlotBlocking ( int8_t ptrToReadSlot)

Read a slot from the RingBuffer into ptrToReadSlot. This method will block until there's space in the buffer.

The caller is responsible to make sure sizeof(ptrToReadSlot) = SlotSize. This method should be use when the caller can block against its input, like sending/receiving UDP packets. It shouldn't be used by audio. For that, use the readSlotNonBlocking.

Parameters
ptrToReadSlotPointer to read slot from the RingBuffer
void RingBuffer::readSlotNonBlocking ( int8_t ptrToReadSlot)

Same as readSlotBlocking but non-blocking (asynchronous)

Parameters
ptrToReadSlotPointer to read slot from the RingBuffer
void RingBuffer::setMemoryInReadSlotWithLastReadSlot ( int8_t ptrToReadSlot)
protectedvirtual

Uses the last read slot to set the memory in the Read Slot.

The last read slot is the last packet that arrived, so if no new packets are received, it keeps looping the same packet.

Parameters
ptrToReadSlotPointer to read slot from the RingBuffer
void RingBuffer::setUnderrunReadSlot ( int8_t ptrToReadSlot)
protectedvirtual

Sets the memory in the Read Slot when uderrun occurs. By default, this sets it to 0. Override this method in a subclass for a different behavior.

Parameters
ptrToReadSlotPointer to read slot from the RingBuffer

Reimplemented in RingBufferWavetable.

void RingBuffer::underrunReset ( )
private

Resets the ring buffer for reads under-runs non-blocking.

Member Data Documentation

QWaitCondition RingBuffer::mBufferIsNotEmpty
private

Buffer not empty condition to monitor threads.

QWaitCondition RingBuffer::mBufferIsNotFull
private

Buffer not full condition to monitor threads.

int RingBuffer::mFullSlots
private

Number of used (full) slots, in slot-size.

int8_t* RingBuffer::mLastReadSlot
private

Last slot read.

QMutex RingBuffer::mMutex
private

Mutex to protect read and write operations.

const int RingBuffer::mNumSlots
private

Number of Slots.

int RingBuffer::mReadPosition
private

Read Positions in the RingBuffer (Tail)

int8_t* RingBuffer::mRingBuffer
private

8-bit array of data (1-byte)

const int RingBuffer::mSlotSize
private

The size of one slot in byes.

const int RingBuffer::mTotalSize
private

Total size of the mRingBuffer = mSlotSize*mNumSlotss.

int RingBuffer::mWritePosition
private

Write Position in the RingBuffer (Head)


The documentation for this class was generated from the following files: