vdr 2.8.3
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 5.3 2026/09/03 13:44:36 kls Exp $
8 */
9
10#ifndef __RINGBUFFER_H
11#define __RINGBUFFER_H
12
13#include <atomic>
14#include "thread.h"
15#include "tools.h"
16
18private:
22 int size;
27protected:
28 std::atomic<tThreadId> getThreadTid; // set by the reading thread, logged by the writing one
29 int maxFill;//XXX
31 bool statistics;//XXX
32 void UpdatePercentage(int Fill);
33 void WaitForPut(void);
34 void WaitForGet(void);
35 void EnablePut(void);
36 void EnableGet(void);
37 virtual void Clear(void) = 0;
38 virtual int Available(void) = 0;
39 virtual int Free(void) { return Size() - Available() - 1; }
40 int Size(void) { return size; }
41public:
42 cRingBuffer(int Size, bool Statistics = false);
43 virtual ~cRingBuffer();
44 void SetTimeouts(int PutTimeout, int GetTimeout);
45 void SetIoThrottle(void);
46 void ReportOverflow(int Bytes);
47 };
48
50//#define DEBUGRINGBUFFERS
51#ifdef DEBUGRINGBUFFERS
52private:
53 int lastHead, lastTail;
54 int lastPut, lastGet;
55 static cRingBufferLinear *RBLS[];
56 static void AddDebugRBL(cRingBufferLinear *RBL);
57 static void DelDebugRBL(cRingBufferLinear *RBL);
58public:
59 static void PrintDebugRBL(void);
60#endif
61private:
62 int margin;
63 std::atomic<int> head, tail;
64 int gotten;
67protected:
68 virtual int DataReady(const uchar *Data, int Count);
74public:
75 cRingBufferLinear(int Size, int Margin = 0, bool Statistics = false, const char *Description = NULL);
80 virtual ~cRingBufferLinear() override;
81 virtual int Available(void) override;
82 virtual int Free(void) override { return Size() - Available() - 1 - margin; }
83 virtual void Clear(void) override;
87 int Read(int FileHandle, int Max = 0);
93 int Read(cUnbufferedFile *File, int Max = 0);
95 int Put(const uchar *Data, int Count);
98 uchar *Get(int &Count);
103 void Del(int Count);
107 };
108
110
111class cFrame {
112 friend class cRingBufferFrame;
113private:
116 int count;
118 int index;
119 uint32_t pts;
121public:
122 cFrame(const uchar *Data, int Count, eFrameType = ftUnknown, int Index = -1, uint32_t Pts = 0, bool independent = false);
126 ~cFrame();
127 uchar *Data(void) const { return data; }
128 int Count(void) const { return count; }
129 eFrameType Type(void) const { return type; }
130 int Index(void) const { return index; }
131 uint32_t Pts(void) const { return pts; }
132 bool Independent(void) const { return independent; }
133 };
134
136private:
140 void Delete(cFrame *Frame);
141 void Lock(void) { mutex.Lock(); }
142 void Unlock(void) { mutex.Unlock(); }
143public:
144 cRingBufferFrame(int Size, bool Statistics = false);
145 virtual ~cRingBufferFrame() override;
146 virtual int Available(void) override;
147 virtual void Clear(void) override;
148 // Immediately clears the ring buffer.
149 bool Put(cFrame *Frame);
150 // Puts the Frame into the ring buffer.
151 // Returns true if this was possible.
152 cFrame *Get(void);
153 // Gets the next frame from the ring buffer.
154 // The actual data still remains in the buffer until Drop() is called.
155 void Drop(cFrame *Frame);
156 // Drops the Frame that has just been fetched with Get().
157 };
158
159#endif //__RINGBUFFER_H
uchar * Data(void) const
Definition ringbuffer.h:127
eFrameType type
Definition ringbuffer.h:117
uint32_t pts
Definition ringbuffer.h:119
uint32_t Pts(void) const
Definition ringbuffer.h:131
bool independent
Definition ringbuffer.h:120
int Index(void) const
Definition ringbuffer.h:130
eFrameType Type(void) const
Definition ringbuffer.h:129
uchar * data
Definition ringbuffer.h:115
int index
Definition ringbuffer.h:118
cFrame * next
Definition ringbuffer.h:114
bool Independent(void) const
Definition ringbuffer.h:132
friend class cRingBufferFrame
Definition ringbuffer.h:112
int count
Definition ringbuffer.h:116
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 Count(void) const
Definition ringbuffer.h:128
bool Lock(int TimeoutMs=0, bool *TimedOut=NULL)
Returns true if the mutex was successfully locked.
Definition thread.c:228
void Unlock(void)
Definition thread.c:252
virtual void Clear(void) override
Definition ringbuffer.c:432
void Unlock(void)
Definition ringbuffer.h:142
cFrame * Get(void)
Definition ringbuffer.c:463
virtual int Available(void) override
Definition ringbuffer.c:498
void Delete(cFrame *Frame)
Definition ringbuffer.c:471
bool Put(cFrame *Frame)
Definition ringbuffer.c:443
cRingBufferFrame(int Size, bool Statistics=false)
Definition ringbuffer.c:420
void Lock(void)
Definition ringbuffer.h:141
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
std::atomic< int > tail
Definition ringbuffer.h:63
void Del(int Count)
Deletes at most Count bytes from the ring buffer.
Definition ringbuffer.c:371
virtual void Clear(void) override
Immediately clears the ring buffer.
Definition ringbuffer.c:217
virtual int Free(void) override
Definition ringbuffer.h:82
int Put(const uchar *Data, int Count)
Puts at most Count bytes of Data into the ring buffer.
Definition ringbuffer.c:306
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
std::atomic< int > head
Definition ringbuffer.h:63
uchar * Get(int &Count)
Gets data from the ring buffer.
Definition ringbuffer.c:346
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 Available(void) override
Definition ringbuffer.c:211
int Size(void)
Definition ringbuffer.h:40
void WaitForGet(void)
Definition ringbuffer.c:71
int overflowCount
Definition ringbuffer.h:24
void SetTimeouts(int PutTimeout, int GetTimeout)
Definition ringbuffer.c:89
void SetIoThrottle(void)
Definition ringbuffer.c:95
int lastPercent
Definition ringbuffer.h:30
time_t lastOverflowReport
Definition ringbuffer.h:23
cRingBuffer(int Size, bool Statistics=false)
Definition ringbuffer.c:26
void EnablePut(void)
Definition ringbuffer.c:77
bool statistics
Definition ringbuffer.h:31
void WaitForPut(void)
Definition ringbuffer.c:65
void EnableGet(void)
Definition ringbuffer.c:83
void UpdatePercentage(int Fill)
Definition ringbuffer.c:46
std::atomic< tThreadId > getThreadTid
Definition ringbuffer.h:28
void ReportOverflow(int Bytes)
Definition ringbuffer.c:101
cIoThrottle * ioThrottle
Definition ringbuffer.h:26
virtual int Free(void)
Definition ringbuffer.h:39
int overflowBytes
Definition ringbuffer.h:25
virtual void Clear(void)=0
cCondWait readyForGet
Definition ringbuffer.h:19
virtual int Available(void)=0
cCondWait readyForPut
Definition ringbuffer.h:19
cUnbufferedFile is used for large files that are mainly written or read in a streaming manner,...
Definition tools.h:513
eFrameType
Definition ringbuffer.h:109
@ ftUnknown
Definition ringbuffer.h:109
@ ftAudio
Definition ringbuffer.h:109
@ ftDolby
Definition ringbuffer.h:109
@ ftVideo
Definition ringbuffer.h:109
unsigned char uchar
Definition tools.h:31