1
0
mirror of https://github.com/VDR4Arch/vdr.git synced 2023-10-10 13:36:52 +02:00
vdr/ringbuffer.h

141 lines
4.4 KiB
C
Raw Permalink Normal View History

/*
* ringbuffer.h: A ring buffer
*
* See the main source file 'vdr.c' for copyright information and
* how to reach the author.
*
* $Id: ringbuffer.h 1.17 2005/12/10 10:54:51 kls Exp $
*/
#ifndef __RINGBUFFER_H
#define __RINGBUFFER_H
#include "thread.h"
#include "tools.h"
class cRingBuffer {
private:
2004-10-16 09:36:28 +02:00
cCondWait readyForPut, readyForGet;
int putTimeout;
int getTimeout;
2001-08-05 12:23:24 +02:00
int size;
time_t lastOverflowReport;
int overflowCount;
int overflowBytes;
protected:
tThreadId getThreadTid;
2001-08-05 12:23:24 +02:00
int maxFill;//XXX
int lastPercent;
2001-08-05 12:23:24 +02:00
bool statistics;//XXX
2004-10-16 09:36:28 +02:00
void UpdatePercentage(int Fill);
void WaitForPut(void);
void WaitForGet(void);
void EnablePut(void);
void EnableGet(void);
2001-08-05 12:23:24 +02:00
virtual void Clear(void) = 0;
virtual int Available(void) = 0;
2004-10-16 09:36:28 +02:00
virtual int Free(void) { return Size() - Available() - 1; }
2001-08-05 12:23:24 +02:00
int Size(void) { return size; }
public:
cRingBuffer(int Size, bool Statistics = false);
virtual ~cRingBuffer();
void SetTimeouts(int PutTimeout, int GetTimeout);
void ReportOverflow(int Bytes);
};
2001-08-05 12:23:24 +02:00
class cRingBufferLinear : public cRingBuffer {
2004-10-16 09:36:28 +02:00
//#define DEBUGRINGBUFFERS
#ifdef DEBUGRINGBUFFERS
private:
int lastHead, lastTail;
int lastPut, lastGet;
static cRingBufferLinear *RBLS[];
static void AddDebugRBL(cRingBufferLinear *RBL);
static void DelDebugRBL(cRingBufferLinear *RBL);
public:
static void PrintDebugRBL(void);
#endif
2001-08-05 12:23:24 +02:00
private:
int margin, head, tail;
2004-10-16 09:36:28 +02:00
int gotten;
2001-08-05 12:23:24 +02:00
uchar *buffer;
2004-10-16 09:36:28 +02:00
char *description;
public:
2004-10-16 09:36:28 +02:00
cRingBufferLinear(int Size, int Margin = 0, bool Statistics = false, const char *Description = NULL);
///< Creates a linear ring buffer.
///< The buffer will be able to hold at most Size-Margin-1 bytes of data, and will
///< be guaranteed to return at least Margin bytes in one consecutive block.
2004-10-16 09:36:28 +02:00
///< The optional Description is used for debugging only.
virtual ~cRingBufferLinear();
2001-08-05 12:23:24 +02:00
virtual int Available(void);
2004-10-16 09:36:28 +02:00
virtual int Free(void) { return Size() - Available() - 1 - margin; }
2001-08-05 12:23:24 +02:00
virtual void Clear(void);
///< Immediately clears the ring buffer.
2004-10-16 09:36:28 +02:00
int Read(int FileHandle, int Max = 0);
///< Reads at most Max bytes from FileHandle and stores them in the
///< ring buffer. If Max is 0, reads as many bytes as possible.
///< Only one actual read() call is done.
///< \return Returns the number of bytes actually read and stored, or
///< an error value from the actual read() call.
2001-08-05 12:23:24 +02:00
int Put(const uchar *Data, int Count);
///< Puts at most Count bytes of Data into the ring buffer.
///< \return Returns the number of bytes actually stored.
uchar *Get(int &Count);
///< Gets data from the ring buffer.
///< The data will remain in the buffer until a call to Del() deletes it.
///< \return Returns a pointer to the data, and stores the number of bytes
2004-10-16 09:36:28 +02:00
///< actually available in Count. If the returned pointer is NULL, Count has no meaning.
void Del(int Count);
///< Deletes at most Count bytes from the ring buffer.
///< Count must be less or equal to the number that was returned by a previous
///< call to Get().
2001-08-05 12:23:24 +02:00
};
enum eFrameType { ftUnknown, ftVideo, ftAudio, ftDolby };
2001-08-05 12:23:24 +02:00
class cFrame {
friend class cRingBufferFrame;
private:
cFrame *next;
uchar *data;
int count;
eFrameType type;
2001-08-05 12:23:24 +02:00
int index;
public:
cFrame(const uchar *Data, int Count, eFrameType = ftUnknown, int Index = -1);
///< Creates a new cFrame object.
///< If Count is negative, the cFrame object will take ownership of the given
///< Data. Otherwise it will allocate Count bytes of memory and copy Data.
2001-08-05 12:23:24 +02:00
~cFrame();
uchar *Data(void) const { return data; }
2001-08-05 12:23:24 +02:00
int Count(void) const { return count; }
eFrameType Type(void) const { return type; }
2001-08-05 12:23:24 +02:00
int Index(void) const { return index; }
};
class cRingBufferFrame : public cRingBuffer {
private:
2004-10-16 09:36:28 +02:00
cMutex mutex;
2001-08-05 12:23:24 +02:00
cFrame *head;
int currentFill;
void Delete(cFrame *Frame);
2004-10-16 09:36:28 +02:00
void Lock(void) { mutex.Lock(); }
void Unlock(void) { mutex.Unlock(); }
public:
cRingBufferFrame(int Size, bool Statistics = false);
virtual ~cRingBufferFrame();
2001-08-05 12:23:24 +02:00
virtual int Available(void);
virtual void Clear(void);
// Immediately clears the ring buffer.
bool Put(cFrame *Frame);
// Puts the Frame into the ring buffer.
// Returns true if this was possible.
cFrame *Get(void);
2001-08-05 12:23:24 +02:00
// Gets the next frame from the ring buffer.
// The actual data still remains in the buffer until Drop() is called.
void Drop(cFrame *Frame);
2001-08-05 12:23:24 +02:00
// Drops the Frame that has just been fetched with Get().
};
#endif // __RINGBUFFER_H