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.
|
|
|
|
*
|
2002-06-16 12:57:31 +02:00
|
|
|
* $Id: ringbuffer.h 1.6 2002/06/16 11:30:07 kls Exp $
|
2001-03-31 08:42:27 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __RINGBUFFER_H
|
|
|
|
#define __RINGBUFFER_H
|
|
|
|
|
|
|
|
#include "thread.h"
|
|
|
|
|
2002-06-16 12:57:31 +02:00
|
|
|
typedef unsigned char uchar;//XXX+
|
2001-03-31 08:42:27 +02:00
|
|
|
|
|
|
|
class cRingBuffer {
|
|
|
|
private:
|
|
|
|
cMutex mutex;
|
2001-08-05 12:23:24 +02:00
|
|
|
cCondVar readyForPut, readyForGet;
|
|
|
|
cMutex putMutex, getMutex;
|
|
|
|
int size;
|
2001-03-31 08:42:27 +02:00
|
|
|
protected:
|
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
|
|
|
|
void WaitForPut(void);
|
|
|
|
void WaitForGet(void);
|
|
|
|
void EnablePut(void);
|
|
|
|
void EnableGet(void);
|
|
|
|
virtual void Clear(void) = 0;
|
|
|
|
virtual int Available(void) = 0;
|
|
|
|
int Free(void) { return size - Available() - 1; }
|
2001-06-02 10:47:40 +02:00
|
|
|
void Lock(void) { mutex.Lock(); }
|
|
|
|
void Unlock(void) { mutex.Unlock(); }
|
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();
|
|
|
|
};
|
|
|
|
|
2001-08-05 12:23:24 +02:00
|
|
|
class cRingBufferLinear : public cRingBuffer {
|
|
|
|
private:
|
|
|
|
int head, tail;
|
|
|
|
uchar *buffer;
|
2002-06-16 12:57:31 +02:00
|
|
|
pid_t getThreadPid;
|
|
|
|
public:
|
|
|
|
cRingBufferLinear(int Size, bool Statistics = false);
|
|
|
|
virtual ~cRingBufferLinear();
|
2001-08-05 12:23:24 +02:00
|
|
|
virtual int Available(void);
|
|
|
|
virtual void Clear(void);
|
|
|
|
// Immediately clears the ring buffer.
|
|
|
|
int Put(const uchar *Data, int Count);
|
|
|
|
// Puts at most Count bytes of Data into the ring buffer.
|
|
|
|
// Returns the number of bytes actually stored.
|
|
|
|
int Get(uchar *Data, int Count);
|
|
|
|
// Gets at most Count bytes of Data from the ring buffer.
|
|
|
|
// Returns the number of bytes actually retrieved.
|
|
|
|
};
|
|
|
|
|
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);
|
2001-08-05 12:23:24 +02:00
|
|
|
~cFrame();
|
|
|
|
const uchar *Data(void) const { return data; }
|
|
|
|
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:
|
|
|
|
cFrame *head;
|
|
|
|
int currentFill;
|
|
|
|
void Delete(const cFrame *Frame);
|
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.
|
2002-06-16 12:57:31 +02:00
|
|
|
const 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(const cFrame *Frame);
|
|
|
|
// Drops the Frame that has just been fetched with Get().
|
|
|
|
};
|
|
|
|
|
2001-03-31 08:42:27 +02:00
|
|
|
#endif // __RINGBUFFER_H
|