2001-03-31 08:42:27 +02:00
|
|
|
/*
|
2002-06-16 12:57:31 +02:00
|
|
|
* ringbuffer.h: A ring buffer
|
2001-03-31 08:42:27 +02:00
|
|
|
*
|
|
|
|
* See the main source file 'vdr.c' for copyright information and
|
|
|
|
* how to reach the author.
|
|
|
|
*
|
2007-11-17 13:59:08 +01:00
|
|
|
* $Id: ringbuffer.h 1.18 2007/11/17 13:49:34 kls Exp $
|
2001-03-31 08:42:27 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __RINGBUFFER_H
|
|
|
|
#define __RINGBUFFER_H
|
|
|
|
|
|
|
|
#include "thread.h"
|
2002-08-04 14:57:29 +02:00
|
|
|
#include "tools.h"
|
2001-03-31 08:42:27 +02:00
|
|
|
|
|
|
|
class cRingBuffer {
|
|
|
|
private:
|
2004-10-16 09:36:28 +02:00
|
|
|
cCondWait readyForPut, readyForGet;
|
2003-05-12 17:46:34 +02:00
|
|
|
int putTimeout;
|
|
|
|
int getTimeout;
|
2001-08-05 12:23:24 +02:00
|
|
|
int size;
|
2004-03-07 14:41:45 +01:00
|
|
|
time_t lastOverflowReport;
|
|
|
|
int overflowCount;
|
|
|
|
int overflowBytes;
|
2001-03-31 08:42:27 +02:00
|
|
|
protected:
|
2005-12-11 12:10:28 +01:00
|
|
|
tThreadId getThreadTid;
|
2001-08-05 12:23:24 +02:00
|
|
|
int maxFill;//XXX
|
2002-06-16 12:57:31 +02:00
|
|
|
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);
|
2003-05-12 17:46:34 +02:00
|
|
|
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; }
|
2001-03-31 08:42:27 +02:00
|
|
|
public:
|
2001-06-02 10:47:40 +02:00
|
|
|
cRingBuffer(int Size, bool Statistics = false);
|
2001-03-31 08:42:27 +02:00
|
|
|
virtual ~cRingBuffer();
|
2003-05-12 17:46:34 +02:00
|
|
|
void SetTimeouts(int PutTimeout, int GetTimeout);
|
2004-03-07 14:41:45 +01:00
|
|
|
void ReportOverflow(int Bytes);
|
2001-03-31 08:42:27 +02:00
|
|
|
};
|
|
|
|
|
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:
|
2003-01-26 09:59:35 +01:00
|
|
|
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;
|
2007-11-17 13:59:08 +01:00
|
|
|
protected:
|
|
|
|
virtual int DataReady(const uchar *Data, int Count);
|
|
|
|
///< By default a ring buffer has data ready as soon as there are at least
|
|
|
|
///< 'margin' bytes available. A derived class can reimplement this function
|
|
|
|
///< if it has other conditions that define when data is ready.
|
|
|
|
///< The return value is either 0 if there is not yet enough data available,
|
|
|
|
///< or the number of bytes from the beginning of Data that are "ready".
|
2002-06-16 12:57:31 +02:00
|
|
|
public:
|
2004-10-16 09:36:28 +02:00
|
|
|
cRingBufferLinear(int Size, int Margin = 0, bool Statistics = false, const char *Description = NULL);
|
2003-01-26 09:59:35 +01:00
|
|
|
///< Creates a linear ring buffer.
|
2004-06-19 10:34:27 +02:00
|
|
|
///< The buffer will be able to hold at most Size-Margin-1 bytes of data, and will
|
2003-01-26 09:59:35 +01:00
|
|
|
///< 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.
|
2002-06-16 12:57:31 +02:00
|
|
|
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);
|
2003-01-26 09:59:35 +01:00
|
|
|
///< 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);
|
2003-01-26 09:59:35 +01:00
|
|
|
///< Puts at most Count bytes of Data into the ring buffer.
|
|
|
|
///< \return Returns the number of bytes actually stored.
|
2003-04-27 09:55:53 +02:00
|
|
|
uchar *Get(int &Count);
|
2003-01-26 09:59:35 +01:00
|
|
|
///< 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.
|
2003-01-26 09:59:35 +01:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2001-11-03 11:05:15 +01: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;
|
2001-11-03 11:05:15 +01:00
|
|
|
eFrameType type;
|
2001-08-05 12:23:24 +02:00
|
|
|
int index;
|
|
|
|
public:
|
2001-11-03 11:05:15 +01:00
|
|
|
cFrame(const uchar *Data, int Count, eFrameType = ftUnknown, int Index = -1);
|
2003-01-19 15:43:58 +01:00
|
|
|
///< 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();
|
2003-04-27 09:55:53 +02:00
|
|
|
uchar *Data(void) const { return data; }
|
2001-08-05 12:23:24 +02:00
|
|
|
int Count(void) const { return count; }
|
2001-11-03 11:05:15 +01:00
|
|
|
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;
|
2003-04-27 09:55:53 +02:00
|
|
|
void Delete(cFrame *Frame);
|
2004-10-16 09:36:28 +02:00
|
|
|
void Lock(void) { mutex.Lock(); }
|
|
|
|
void Unlock(void) { mutex.Unlock(); }
|
2002-06-16 12:57:31 +02:00
|
|
|
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.
|
2003-04-27 09:55:53 +02:00
|
|
|
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.
|
2003-04-27 09:55:53 +02:00
|
|
|
void Drop(cFrame *Frame);
|
2001-08-05 12:23:24 +02:00
|
|
|
// Drops the Frame that has just been fetched with Get().
|
|
|
|
};
|
|
|
|
|
2001-03-31 08:42:27 +02:00
|
|
|
#endif // __RINGBUFFER_H
|