vdr  2.2.0
ringbuffer.h
Go to the documentation of this file.
1 /*
2  * ringbuffer.h: A ring buffer
3  *
4  * See the main source file 'vdr.c' for copyright information and
5  * how to reach the author.
6  *
7  * $Id: ringbuffer.h 3.0 2013/02/16 15:20:37 kls Exp $
8  */
9 
10 #ifndef __RINGBUFFER_H
11 #define __RINGBUFFER_H
12 
13 #include "thread.h"
14 #include "tools.h"
15 
16 class cRingBuffer {
17 private:
21  int size;
26 protected:
28  int maxFill;//XXX
30  bool statistics;//XXX
31  void UpdatePercentage(int Fill);
32  void WaitForPut(void);
33  void WaitForGet(void);
34  void EnablePut(void);
35  void EnableGet(void);
36  virtual void Clear(void) = 0;
37  virtual int Available(void) = 0;
38  virtual int Free(void) { return Size() - Available() - 1; }
39  int Size(void) { return size; }
40 public:
41  cRingBuffer(int Size, bool Statistics = false);
42  virtual ~cRingBuffer();
43  void SetTimeouts(int PutTimeout, int GetTimeout);
44  void SetIoThrottle(void);
45  void ReportOverflow(int Bytes);
46  };
47 
49 //#define DEBUGRINGBUFFERS
50 #ifdef DEBUGRINGBUFFERS
51 private:
52  int lastHead, lastTail;
53  int lastPut, lastGet;
54  static cRingBufferLinear *RBLS[];
55  static void AddDebugRBL(cRingBufferLinear *RBL);
56  static void DelDebugRBL(cRingBufferLinear *RBL);
57 public:
58  static void PrintDebugRBL(void);
59 #endif
60 private:
61  int margin, head, tail;
62  int gotten;
64  char *description;
65 protected:
66  virtual int DataReady(const uchar *Data, int Count);
72 public:
73  cRingBufferLinear(int Size, int Margin = 0, bool Statistics = false, const char *Description = NULL);
78  virtual ~cRingBufferLinear();
79  virtual int Available(void);
80  virtual int Free(void) { return Size() - Available() - 1 - margin; }
81  virtual void Clear(void);
83  int Read(int FileHandle, int Max = 0);
89  int Read(cUnbufferedFile *File, int Max = 0);
91  int Put(const uchar *Data, int Count);
94  uchar *Get(int &Count);
99  void Del(int Count);
103  };
104 
106 
107 class cFrame {
108  friend class cRingBufferFrame;
109 private:
112  int count;
114  int index;
115  uint32_t pts;
116 public:
117  cFrame(const uchar *Data, int Count, eFrameType = ftUnknown, int Index = -1, uint32_t Pts = 0);
121  ~cFrame();
122  uchar *Data(void) const { return data; }
123  int Count(void) const { return count; }
124  eFrameType Type(void) const { return type; }
125  int Index(void) const { return index; }
126  uint32_t Pts(void) const { return pts; }
127  };
128 
130 private:
134  void Delete(cFrame *Frame);
135  void Lock(void) { mutex.Lock(); }
136  void Unlock(void) { mutex.Unlock(); }
137 public:
138  cRingBufferFrame(int Size, bool Statistics = false);
139  virtual ~cRingBufferFrame();
140  virtual int Available(void);
141  virtual void Clear(void);
142  // Immediately clears the ring buffer.
143  bool Put(cFrame *Frame);
144  // Puts the Frame into the ring buffer.
145  // Returns true if this was possible.
146  cFrame *Get(void);
147  // Gets the next frame from the ring buffer.
148  // The actual data still remains in the buffer until Drop() is called.
149  void Drop(cFrame *Frame);
150  // Drops the Frame that has just been fetched with Get().
151  };
152 
153 #endif // __RINGBUFFER_H
void EnableGet(void)
Definition: ringbuffer.c:83
unsigned char uchar
Definition: tools.h:30
void Lock(void)
Definition: thread.c:191
uint32_t pts
Definition: ringbuffer.h:115
int putTimeout
Definition: ringbuffer.h:19
int overflowBytes
Definition: ringbuffer.h:24
void EnablePut(void)
Definition: ringbuffer.c:77
eFrameType
Definition: ringbuffer.h:105
int lastPercent
Definition: ringbuffer.h:29
void Unlock(void)
Definition: ringbuffer.h:136
int Count(void) const
Definition: ringbuffer.h:123
cCondWait readyForGet
Definition: ringbuffer.h:18
void UpdatePercentage(int Fill)
Definition: ringbuffer.c:46
cFrame * next
Definition: ringbuffer.h:110
cUnbufferedFile is used for large files that are mainly written or read in a streaming manner...
Definition: tools.h:418
virtual void Clear(void)=0
bool statistics
Definition: ringbuffer.h:30
uchar * data
Definition: ringbuffer.h:111
uchar * Data(void) const
Definition: ringbuffer.h:122
int getTimeout
Definition: ringbuffer.h:20
void WaitForGet(void)
Definition: ringbuffer.c:71
virtual int Free(void)
Definition: ringbuffer.h:38
virtual int Free(void)
Definition: ringbuffer.h:80
virtual int Available(void)=0
tThreadId getThreadTid
Definition: ringbuffer.h:27
eFrameType type
Definition: ringbuffer.h:113
uint32_t Pts(void) const
Definition: ringbuffer.h:126
pid_t tThreadId
Definition: thread.h:75
Definition: thread.h:63
void SetIoThrottle(void)
Definition: ringbuffer.c:95
int index
Definition: ringbuffer.h:114
int Index(void) const
Definition: ringbuffer.h:125
void Lock(void)
Definition: ringbuffer.h:135
int Size(void)
Definition: ringbuffer.h:39
cCondWait readyForPut
Definition: ringbuffer.h:18
int count
Definition: ringbuffer.h:112
eFrameType Type(void) const
Definition: ringbuffer.h:124
cRingBuffer(int Size, bool Statistics=false)
Definition: ringbuffer.c:26
cIoThrottle * ioThrottle
Definition: ringbuffer.h:25
void WaitForPut(void)
Definition: ringbuffer.c:65
int overflowCount
Definition: ringbuffer.h:23
virtual ~cRingBuffer()
Definition: ringbuffer.c:39
void SetTimeouts(int PutTimeout, int GetTimeout)
Definition: ringbuffer.c:89
char * description
Definition: ringbuffer.h:64
void ReportOverflow(int Bytes)
Definition: ringbuffer.c:101
time_t lastOverflowReport
Definition: ringbuffer.h:22
void Unlock(void)
Definition: thread.c:197