vdr  2.4.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 4.2 2017/03/19 13:11:39 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);
85  int Read(int FileHandle, int Max = 0);
91  int Read(cUnbufferedFile *File, int Max = 0);
93  int Put(const uchar *Data, int Count);
96  uchar *Get(int &Count);
101  void Del(int Count);
105  };
106 
108 
109 class cFrame {
110  friend class cRingBufferFrame;
111 private:
114  int count;
116  int index;
117  uint32_t pts;
119 public:
120  cFrame(const uchar *Data, int Count, eFrameType = ftUnknown, int Index = -1, uint32_t Pts = 0, bool independent = false);
124  ~cFrame();
125  uchar *Data(void) const { return data; }
126  int Count(void) const { return count; }
127  eFrameType Type(void) const { return type; }
128  int Index(void) const { return index; }
129  uint32_t Pts(void) const { return pts; }
130  bool Independent(void) const { return independent; }
131  };
132 
134 private:
138  void Delete(cFrame *Frame);
139  void Lock(void) { mutex.Lock(); }
140  void Unlock(void) { mutex.Unlock(); }
141 public:
142  cRingBufferFrame(int Size, bool Statistics = false);
143  virtual ~cRingBufferFrame();
144  virtual int Available(void);
145  virtual void Clear(void);
146  // Immediately clears the ring buffer.
147  bool Put(cFrame *Frame);
148  // Puts the Frame into the ring buffer.
149  // Returns true if this was possible.
150  cFrame *Get(void);
151  // Gets the next frame from the ring buffer.
152  // The actual data still remains in the buffer until Drop() is called.
153  void Drop(cFrame *Frame);
154  // Drops the Frame that has just been fetched with Get().
155  };
156 
157 #endif // __RINGBUFFER_H
void EnableGet(void)
Definition: ringbuffer.c:83
unsigned char uchar
Definition: tools.h:31
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...
Definition: ringbuffer.c:206
bool independent
Definition: ringbuffer.h:118
void Lock(void)
Definition: thread.c:222
uint32_t pts
Definition: ringbuffer.h:117
int putTimeout
Definition: ringbuffer.h:19
virtual void Clear(void)
Immediately clears the ring buffer.
Definition: ringbuffer.c:217
int overflowBytes
Definition: ringbuffer.h:24
void EnablePut(void)
Definition: ringbuffer.c:77
eFrameType
Definition: ringbuffer.h:107
virtual int Available(void)
Definition: ringbuffer.c:498
virtual void Clear(void)
Definition: ringbuffer.c:432
cFrame(const uchar *Data, int Count, eFrameType=ftUnknown, int Index=-1, uint32_t Pts=0, bool independent=false)
Creates a new cFrame object.
Definition: ringbuffer.c:394
int lastPercent
Definition: ringbuffer.h:29
void Unlock(void)
Definition: ringbuffer.h:140
cRingBufferFrame(int Size, bool Statistics=false)
Definition: ringbuffer.c:420
bool Independent(void) const
Definition: ringbuffer.h:130
void Drop(cFrame *Frame)
Definition: ringbuffer.c:477
cRingBufferLinear(int Size, int Margin=0, bool Statistics=false, const char *Description=NULL)
Creates a linear ring buffer.
Definition: ringbuffer.c:170
cCondWait readyForGet
Definition: ringbuffer.h:18
int Put(const uchar *Data, int Count)
Puts at most Count bytes of Data into the ring buffer.
Definition: ringbuffer.c:306
void UpdatePercentage(int Fill)
Definition: ringbuffer.c:46
cFrame * next
Definition: ringbuffer.h:112
cUnbufferedFile is used for large files that are mainly written or read in a streaming manner...
Definition: tools.h:457
virtual void Clear(void)=0
bool statistics
Definition: ringbuffer.h:30
uchar * data
Definition: ringbuffer.h:113
int getTimeout
Definition: ringbuffer.h:20
void WaitForGet(void)
Definition: ringbuffer.c:71
virtual int Free(void)
Definition: ringbuffer.h:38
int Read(int FileHandle, int Max=0)
Reads at most Max bytes from FileHandle and stores them in the ring buffer.
Definition: ringbuffer.c:230
virtual int Free(void)
Definition: ringbuffer.h:80
virtual int Available(void)=0
tThreadId getThreadTid
Definition: ringbuffer.h:27
cFrame * Get(void)
Definition: ringbuffer.c:463
int Index(void) const
Definition: ringbuffer.h:128
eFrameType type
Definition: ringbuffer.h:115
pid_t tThreadId
Definition: thread.h:17
Definition: thread.h:67
~cFrame()
Definition: ringbuffer.c:413
uint32_t Pts(void) const
Definition: ringbuffer.h:129
void Del(int Count)
Deletes at most Count bytes from the ring buffer.
Definition: ringbuffer.c:371
bool Put(cFrame *Frame)
Definition: ringbuffer.c:443
void SetIoThrottle(void)
Definition: ringbuffer.c:95
int index
Definition: ringbuffer.h:116
eFrameType Type(void) const
Definition: ringbuffer.h:127
virtual ~cRingBufferLinear()
Definition: ringbuffer.c:197
uchar * Get(int &Count)
Gets data from the ring buffer.
Definition: ringbuffer.c:346
void Lock(void)
Definition: ringbuffer.h:139
int Size(void)
Definition: ringbuffer.h:39
cCondWait readyForPut
Definition: ringbuffer.h:18
int count
Definition: ringbuffer.h:114
void Delete(cFrame *Frame)
Definition: ringbuffer.c:471
int Count(void) const
Definition: ringbuffer.h:126
cRingBuffer(int Size, bool Statistics=false)
Definition: ringbuffer.c:26
cIoThrottle * ioThrottle
Definition: ringbuffer.h:25
void WaitForPut(void)
Definition: ringbuffer.c:65
virtual ~cRingBufferFrame()
Definition: ringbuffer.c:427
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
uchar * Data(void) const
Definition: ringbuffer.h:125
void Unlock(void)
Definition: thread.c:228
virtual int Available(void)
Definition: ringbuffer.c:211