ringbuffer.h 5.38 KB
Newer Older
Klaus Schmidinger's avatar
Klaus Schmidinger committed
1
/*
Klaus Schmidinger's avatar
Klaus Schmidinger committed
2
 * ringbuffer.h: A ring buffer
Klaus Schmidinger's avatar
Klaus Schmidinger committed
3 4 5 6
 *
 * See the main source file 'vdr.c' for copyright information and
 * how to reach the author.
 *
7
 * $Id: ringbuffer.h 4.2 2017/03/19 13:11:39 kls Exp $
Klaus Schmidinger's avatar
Klaus Schmidinger committed
8 9 10 11 12 13
 */

#ifndef __RINGBUFFER_H
#define __RINGBUFFER_H

#include "thread.h"
Klaus Schmidinger's avatar
Klaus Schmidinger committed
14
#include "tools.h"
Klaus Schmidinger's avatar
Klaus Schmidinger committed
15 16 17

class cRingBuffer {
private:
Klaus Schmidinger's avatar
Klaus Schmidinger committed
18
  cCondWait readyForPut, readyForGet;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
19 20
  int putTimeout;
  int getTimeout;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
21
  int size;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
22 23 24
  time_t lastOverflowReport;
  int overflowCount;
  int overflowBytes;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
25
  cIoThrottle *ioThrottle;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
26
protected:
Klaus Schmidinger's avatar
Klaus Schmidinger committed
27
  tThreadId getThreadTid;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
28
  int maxFill;//XXX
Klaus Schmidinger's avatar
Klaus Schmidinger committed
29
  int lastPercent;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
30
  bool statistics;//XXX
Klaus Schmidinger's avatar
Klaus Schmidinger committed
31
  void UpdatePercentage(int Fill);
Klaus Schmidinger's avatar
Klaus Schmidinger committed
32 33 34 35
  void WaitForPut(void);
  void WaitForGet(void);
  void EnablePut(void);
  void EnableGet(void);
Klaus Schmidinger's avatar
Klaus Schmidinger committed
36 37
  virtual void Clear(void) = 0;
  virtual int Available(void) = 0;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
38
  virtual int Free(void) { return Size() - Available() - 1; }
Klaus Schmidinger's avatar
Klaus Schmidinger committed
39
  int Size(void) { return size; }
Klaus Schmidinger's avatar
Klaus Schmidinger committed
40
public:
Klaus Schmidinger's avatar
Klaus Schmidinger committed
41
  cRingBuffer(int Size, bool Statistics = false);
Klaus Schmidinger's avatar
Klaus Schmidinger committed
42
  virtual ~cRingBuffer();
Klaus Schmidinger's avatar
Klaus Schmidinger committed
43
  void SetTimeouts(int PutTimeout, int GetTimeout);
Klaus Schmidinger's avatar
Klaus Schmidinger committed
44
  void SetIoThrottle(void);
Klaus Schmidinger's avatar
Klaus Schmidinger committed
45
  void ReportOverflow(int Bytes);
Klaus Schmidinger's avatar
Klaus Schmidinger committed
46 47
  };

Klaus Schmidinger's avatar
Klaus Schmidinger committed
48
class cRingBufferLinear : public cRingBuffer {
Klaus Schmidinger's avatar
Klaus Schmidinger committed
49 50 51 52 53 54 55 56 57 58 59
//#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
Klaus Schmidinger's avatar
Klaus Schmidinger committed
60
private:
Klaus Schmidinger's avatar
Klaus Schmidinger committed
61
  int margin, head, tail;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
62
  int gotten;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
63
  uchar *buffer;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
64
  char *description;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
65 66 67 68 69 70 71
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".
Klaus Schmidinger's avatar
Klaus Schmidinger committed
72
public:
Klaus Schmidinger's avatar
Klaus Schmidinger committed
73
  cRingBufferLinear(int Size, int Margin = 0, bool Statistics = false, const char *Description = NULL);
Klaus Schmidinger's avatar
Klaus Schmidinger committed
74
    ///< Creates a linear ring buffer.
Klaus Schmidinger's avatar
Klaus Schmidinger committed
75
    ///< The buffer will be able to hold at most Size-Margin-1 bytes of data, and will
Klaus Schmidinger's avatar
Klaus Schmidinger committed
76
    ///< be guaranteed to return at least Margin bytes in one consecutive block.
Klaus Schmidinger's avatar
Klaus Schmidinger committed
77
    ///< The optional Description is used for debugging only.
Klaus Schmidinger's avatar
Klaus Schmidinger committed
78
  virtual ~cRingBufferLinear();
Klaus Schmidinger's avatar
Klaus Schmidinger committed
79
  virtual int Available(void);
Klaus Schmidinger's avatar
Klaus Schmidinger committed
80
  virtual int Free(void) { return Size() - Available() - 1 - margin; }
Klaus Schmidinger's avatar
Klaus Schmidinger committed
81
  virtual void Clear(void);
Klaus Schmidinger's avatar
Klaus Schmidinger committed
82
    ///< Immediately clears the ring buffer.
83 84
    ///< This function may safely be called from the reading thread without additional
    ///< locking. If called from the writing thread, proper locking must be used.
Klaus Schmidinger's avatar
Klaus Schmidinger committed
85 86 87 88
  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.
Klaus Schmidinger's avatar
Klaus Schmidinger committed
89
    ///< Returns the number of bytes actually read and stored, or
Klaus Schmidinger's avatar
Klaus Schmidinger committed
90
    ///< an error value from the actual read() call.
Klaus Schmidinger's avatar
Klaus Schmidinger committed
91
  int Read(cUnbufferedFile *File, int Max = 0);
Klaus Schmidinger's avatar
Klaus Schmidinger committed
92
    ///< Like Read(int FileHandle, int Max), but reads from a cUnbufferedFile).
Klaus Schmidinger's avatar
Klaus Schmidinger committed
93
  int Put(const uchar *Data, int Count);
Klaus Schmidinger's avatar
Klaus Schmidinger committed
94
    ///< Puts at most Count bytes of Data into the ring buffer.
Klaus Schmidinger's avatar
Klaus Schmidinger committed
95
    ///< Returns the number of bytes actually stored.
Klaus Schmidinger's avatar
Klaus Schmidinger committed
96
  uchar *Get(int &Count);
Klaus Schmidinger's avatar
Klaus Schmidinger committed
97 98
    ///< Gets data from the ring buffer.
    ///< The data will remain in the buffer until a call to Del() deletes it.
Klaus Schmidinger's avatar
Klaus Schmidinger committed
99
    ///< Returns a pointer to the data, and stores the number of bytes
Klaus Schmidinger's avatar
Klaus Schmidinger committed
100
    ///< actually available in Count. If the returned pointer is NULL, Count has no meaning.
Klaus Schmidinger's avatar
Klaus Schmidinger committed
101 102 103 104
  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().
Klaus Schmidinger's avatar
Klaus Schmidinger committed
105 106
  };

Klaus Schmidinger's avatar
Klaus Schmidinger committed
107 108
enum eFrameType { ftUnknown, ftVideo, ftAudio, ftDolby };

Klaus Schmidinger's avatar
Klaus Schmidinger committed
109 110 111 112 113 114
class cFrame {
  friend class cRingBufferFrame;
private:
  cFrame *next;
  uchar *data;
  int count;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
115
  eFrameType type;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
116
  int index;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
117
  uint32_t pts;
118
  bool independent;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
119
public:
120
  cFrame(const uchar *Data, int Count, eFrameType = ftUnknown, int Index = -1, uint32_t Pts = 0, bool independent = false);
Klaus Schmidinger's avatar
Klaus Schmidinger committed
121 122 123
    ///< 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.
Klaus Schmidinger's avatar
Klaus Schmidinger committed
124
  ~cFrame();
Klaus Schmidinger's avatar
Klaus Schmidinger committed
125
  uchar *Data(void) const { return data; }
Klaus Schmidinger's avatar
Klaus Schmidinger committed
126
  int Count(void) const { return count; }
Klaus Schmidinger's avatar
Klaus Schmidinger committed
127
  eFrameType Type(void) const { return type; }
Klaus Schmidinger's avatar
Klaus Schmidinger committed
128
  int Index(void) const { return index; }
Klaus Schmidinger's avatar
Klaus Schmidinger committed
129
  uint32_t Pts(void) const { return pts; }
130
  bool Independent(void) const { return independent; }
Klaus Schmidinger's avatar
Klaus Schmidinger committed
131 132 133 134
  };

class cRingBufferFrame : public cRingBuffer {
private:
Klaus Schmidinger's avatar
Klaus Schmidinger committed
135
  cMutex mutex;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
136 137
  cFrame *head;
  int currentFill;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
138
  void Delete(cFrame *Frame);
Klaus Schmidinger's avatar
Klaus Schmidinger committed
139 140
  void Lock(void) { mutex.Lock(); }
  void Unlock(void) { mutex.Unlock(); }
Klaus Schmidinger's avatar
Klaus Schmidinger committed
141 142 143
public:
  cRingBufferFrame(int Size, bool Statistics = false);
  virtual ~cRingBufferFrame();
Klaus Schmidinger's avatar
Klaus Schmidinger committed
144 145 146 147 148 149
  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.
Klaus Schmidinger's avatar
Klaus Schmidinger committed
150
  cFrame *Get(void);
Klaus Schmidinger's avatar
Klaus Schmidinger committed
151 152
    // Gets the next frame from the ring buffer.
    // The actual data still remains in the buffer until Drop() is called.
Klaus Schmidinger's avatar
Klaus Schmidinger committed
153
  void Drop(cFrame *Frame);
Klaus Schmidinger's avatar
Klaus Schmidinger committed
154 155 156
    // Drops the Frame that has just been fetched with Get().
  };

Klaus Schmidinger's avatar
Klaus Schmidinger committed
157
#endif // __RINGBUFFER_H