MythTV  master
DeviceReadBuffer.h
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 /* Device Buffer written by John Poet */
3 
4 #ifndef DEVICEREADBUFFER_H
5 #define DEVICEREADBUFFER_H
6 
7 #include <unistd.h>
8 
9 #include <QMutex>
10 #include <QWaitCondition>
11 #include <QString>
12 
13 #include "mythtimer.h"
14 #include "tspacket.h"
15 #include "mthread.h"
16 
18 {
19  protected:
20  virtual ~DeviceReaderCB() = default;
21  public:
22  virtual void ReaderPaused(int fd) = 0;
23  virtual void PriorityEvent(int fd) = 0;
24 };
25 
33 class DeviceReadBuffer : protected MThread
34 {
35  public:
36  explicit DeviceReadBuffer(DeviceReaderCB *cb,
37  bool use_poll = true,
38  bool error_exit_on_poll_timeout = true);
39  ~DeviceReadBuffer() override;
40 
41  bool Setup(const QString &streamName,
42  int streamfd,
43  uint readQuanta = sizeof(TSPacket),
44  uint deviceBufferSize = 0,
45  uint deviceBufferCount = 1);
46 
47  void Start(void);
48  void Reset(const QString &streamName, int streamfd);
49  void Stop(void);
50 
51  void SetRequestPause(bool request);
52  bool IsPaused(void) const;
53  bool WaitForUnpause(unsigned long timeout);
54  bool WaitForPaused(unsigned long timeout);
55 
56  bool IsErrored(void) const;
57  bool IsEOF(void) const;
58  bool IsRunning(void) const;
59 
60  uint Read(unsigned char *buf, uint count);
61  uint GetUsed(void) const;
62 
63  private:
64  void run(void) override; // MThread
65 
66  void SetPaused(bool val);
67  void IncrWritePointer(uint len);
68  void IncrReadPointer(uint len);
69 
70  bool HandlePausing(void);
71  bool Poll(void) const;
72  void WakePoll(void) const;
73  uint WaitForUnused(uint needed) const;
74  uint WaitForUsed (uint needed, uint max_wait /*ms*/) const;
75 
76  bool IsPauseRequested(void) const;
77  bool IsOpen(void) const { return m_streamFd >= 0; }
78  void ClosePipes(void) const;
79  uint GetUnused(void) const;
80  uint GetContiguousUnused(void) const;
81 
82  bool CheckForErrors(ssize_t read_len, size_t requested_len, uint &errcnt);
83  void ReportStats(void);
84 
85  QString m_videoDevice;
86  int m_streamFd {-1};
87  mutable int m_wakePipe[2] {-1,-1};
88  mutable long m_wakePipeFlags[2] {0,0};
89 
91 
92  // Data for managing the device ringbuffer
93  mutable QMutex m_lock;
94  volatile bool m_doRun {false};
95  bool m_eof {false};
96  mutable bool m_error {false};
97  bool m_requestPause {false};
98  bool m_paused {false};
99  bool m_usingPoll {true};
100  bool m_pollTimeoutIsError {true};
101  uint m_maxPollWait {2500 /*ms*/};
102 
103  size_t m_size {0};
104  size_t m_used {0};
105  size_t m_readQuanta {0};
106  size_t m_devBufferCount {1};
107  size_t m_devReadSize {0};
108  size_t m_readThreshold {0};
109  unsigned char *m_buffer {nullptr};
110  unsigned char *m_readPtr {nullptr};
111  unsigned char *m_writePtr {nullptr};
112  unsigned char *m_endPtr {nullptr};
113 
114  mutable QWaitCondition m_dataWait;
115  QWaitCondition m_runWait;
116  QWaitCondition m_pauseWait;
117  QWaitCondition m_unpauseWait;
118 
119  // statistics
120  size_t m_maxUsed {0};
121  size_t m_avgUsed {0};
122  size_t m_avgBufWriteCnt {0};
123  size_t m_avgBufReadCnt {0};
124  size_t m_avgBufSleepCnt {0};
126 };
127 
128 #endif // DEVICEREADBUFFER_H
129 
130 /*
131  * vim:ts=4:sw=4:ai:et:si:sts=4
132  */
DeviceReadBuffer::WaitForUsed
uint WaitForUsed(uint needed, uint max_wait) const
Definition: DeviceReadBuffer.cpp:686
DeviceReadBuffer::m_streamFd
int m_streamFd
Definition: DeviceReadBuffer.h:86
DeviceReadBuffer::m_devReadSize
size_t m_devReadSize
Definition: DeviceReadBuffer.h:107
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
DeviceReadBuffer::m_readPtr
unsigned char * m_readPtr
Definition: DeviceReadBuffer.h:110
DeviceReadBuffer::m_pauseWait
QWaitCondition m_pauseWait
Definition: DeviceReadBuffer.h:116
DeviceReadBuffer::m_maxUsed
size_t m_maxUsed
Definition: DeviceReadBuffer.h:120
DeviceReadBuffer::Start
void Start(void)
Definition: DeviceReadBuffer.cpp:106
DeviceReadBuffer::m_used
size_t m_used
Definition: DeviceReadBuffer.h:104
DeviceReadBuffer::WakePoll
void WakePoll(void) const
Definition: DeviceReadBuffer.cpp:181
DeviceReadBuffer::m_wakePipeFlags
long m_wakePipeFlags[2]
Definition: DeviceReadBuffer.h:88
DeviceReadBuffer::Setup
bool Setup(const QString &streamName, int streamfd, uint readQuanta=sizeof(TSPacket), uint deviceBufferSize=0, uint deviceBufferCount=1)
Definition: DeviceReadBuffer.cpp:49
DeviceReaderCB::~DeviceReaderCB
virtual ~DeviceReaderCB()=default
DeviceReadBuffer::m_devBufferCount
size_t m_devBufferCount
Definition: DeviceReadBuffer.h:106
MythTimer
A QElapsedTimer based timer to replace use of QTime as a timer.
Definition: mythtimer.h:13
DeviceReadBuffer::IsPaused
bool IsPaused(void) const
Definition: DeviceReadBuffer.cpp:211
DeviceReadBuffer::m_maxPollWait
uint m_maxPollWait
Definition: DeviceReadBuffer.h:101
DeviceReadBuffer::WaitForPaused
bool WaitForPaused(unsigned long timeout)
Definition: DeviceReadBuffer.cpp:217
DeviceReadBuffer::m_runWait
QWaitCondition m_runWait
Definition: DeviceReadBuffer.h:115
DeviceReadBuffer::Read
uint Read(unsigned char *buf, uint count)
Try to Read count bytes from into buffer.
Definition: DeviceReadBuffer.cpp:618
DeviceReadBuffer::m_avgBufWriteCnt
size_t m_avgBufWriteCnt
Definition: DeviceReadBuffer.h:122
DeviceReadBuffer::IsPauseRequested
bool IsPauseRequested(void) const
Definition: DeviceReadBuffer.cpp:237
DeviceReadBuffer
Buffers reads from device files.
Definition: DeviceReadBuffer.h:33
DeviceReadBuffer::WaitForUnpause
bool WaitForUnpause(unsigned long timeout)
Definition: DeviceReadBuffer.cpp:227
DeviceReaderCB
Definition: DeviceReadBuffer.h:17
DeviceReadBuffer::GetContiguousUnused
uint GetContiguousUnused(void) const
Definition: DeviceReadBuffer.cpp:273
DeviceReadBuffer::m_requestPause
bool m_requestPause
Definition: DeviceReadBuffer.h:97
DeviceReaderCB::ReaderPaused
virtual void ReaderPaused(int fd)=0
DeviceReadBuffer::m_eof
bool m_eof
Definition: DeviceReadBuffer.h:95
DeviceReadBuffer::WaitForUnused
uint WaitForUnused(uint needed) const
Definition: DeviceReadBuffer.cpp:660
DeviceReadBuffer::CheckForErrors
bool CheckForErrors(ssize_t read_len, size_t requested_len, uint &errcnt)
Definition: DeviceReadBuffer.cpp:537
DeviceReadBuffer::m_pollTimeoutIsError
bool m_pollTimeoutIsError
Definition: DeviceReadBuffer.h:100
DeviceReadBuffer::m_doRun
volatile bool m_doRun
Definition: DeviceReadBuffer.h:94
DeviceReadBuffer::m_readerCB
DeviceReaderCB * m_readerCB
Definition: DeviceReadBuffer.h:90
DeviceReadBuffer::m_readQuanta
size_t m_readQuanta
Definition: DeviceReadBuffer.h:105
TSPacket
Used to access the data of a Transport Stream packet.
Definition: tspacket.h:166
DeviceReadBuffer::m_videoDevice
QString m_videoDevice
Definition: DeviceReadBuffer.h:85
DeviceReadBuffer::IsRunning
bool IsRunning(void) const
Definition: DeviceReadBuffer.cpp:255
DeviceReadBuffer::m_usingPoll
bool m_usingPoll
Definition: DeviceReadBuffer.h:99
tspacket.h
DeviceReadBuffer::m_buffer
unsigned char * m_buffer
Definition: DeviceReadBuffer.h:109
DeviceReadBuffer::IsOpen
bool IsOpen(void) const
Definition: DeviceReadBuffer.h:77
DeviceReadBuffer::Stop
void Stop(void)
Definition: DeviceReadBuffer.cpp:149
DeviceReadBuffer::m_unpauseWait
QWaitCondition m_unpauseWait
Definition: DeviceReadBuffer.h:117
DeviceReadBuffer::m_readThreshold
size_t m_readThreshold
Definition: DeviceReadBuffer.h:108
uint
unsigned int uint
Definition: compat.h:140
DeviceReadBuffer::ClosePipes
void ClosePipes(void) const
Definition: DeviceReadBuffer.cpp:198
DeviceReaderCB::PriorityEvent
virtual void PriorityEvent(int fd)=0
DeviceReadBuffer::m_lock
QMutex m_lock
Definition: DeviceReadBuffer.h:93
DeviceReadBuffer::m_size
size_t m_size
Definition: DeviceReadBuffer.h:103
DeviceReadBuffer::m_avgUsed
size_t m_avgUsed
Definition: DeviceReadBuffer.h:121
DeviceReadBuffer::GetUnused
uint GetUnused(void) const
Definition: DeviceReadBuffer.cpp:261
DeviceReadBuffer::~DeviceReadBuffer
~DeviceReadBuffer() override
Definition: DeviceReadBuffer.cpp:39
DeviceReadBuffer::ReportStats
void ReportStats(void)
Definition: DeviceReadBuffer.cpp:703
DeviceReadBuffer::SetPaused
void SetPaused(bool val)
Definition: DeviceReadBuffer.cpp:170
DeviceReadBuffer::m_writePtr
unsigned char * m_writePtr
Definition: DeviceReadBuffer.h:111
DeviceReadBuffer::Poll
bool Poll(void) const
Definition: DeviceReadBuffer.cpp:410
DeviceReadBuffer::HandlePausing
bool HandlePausing(void)
Definition: DeviceReadBuffer.cpp:390
DeviceReadBuffer::DeviceReadBuffer
DeviceReadBuffer(DeviceReaderCB *cb, bool use_poll=true, bool error_exit_on_poll_timeout=true)
Definition: DeviceReadBuffer.cpp:21
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:48
DeviceReadBuffer::m_avgBufSleepCnt
size_t m_avgBufSleepCnt
Definition: DeviceReadBuffer.h:124
mthread.h
DeviceReadBuffer::m_error
bool m_error
Definition: DeviceReadBuffer.h:96
DeviceReadBuffer::IsErrored
bool IsErrored(void) const
Definition: DeviceReadBuffer.cpp:243
DeviceReadBuffer::run
void run(void) override
Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead.
Definition: DeviceReadBuffer.cpp:304
DeviceReadBuffer::IncrWritePointer
void IncrWritePointer(uint len)
Definition: DeviceReadBuffer.cpp:279
mythtimer.h
DeviceReadBuffer::m_avgBufReadCnt
size_t m_avgBufReadCnt
Definition: DeviceReadBuffer.h:123
DeviceReadBuffer::IsEOF
bool IsEOF(void) const
Definition: DeviceReadBuffer.cpp:249
DeviceReadBuffer::m_wakePipe
int m_wakePipe[2]
Definition: DeviceReadBuffer.h:87
DeviceReadBuffer::m_paused
bool m_paused
Definition: DeviceReadBuffer.h:98
DeviceReadBuffer::SetRequestPause
void SetRequestPause(bool request)
Definition: DeviceReadBuffer.cpp:163
DeviceReadBuffer::m_dataWait
QWaitCondition m_dataWait
Definition: DeviceReadBuffer.h:114
DeviceReadBuffer::m_lastReport
MythTimer m_lastReport
Definition: DeviceReadBuffer.h:125
DeviceReadBuffer::IncrReadPointer
void IncrReadPointer(uint len)
Definition: DeviceReadBuffer.cpp:293
DeviceReadBuffer::m_endPtr
unsigned char * m_endPtr
Definition: DeviceReadBuffer.h:112
DeviceReadBuffer::Reset
void Reset(const QString &streamName, int streamfd)
Definition: DeviceReadBuffer.cpp:134
DeviceReadBuffer::GetUsed
uint GetUsed(void) const
Definition: DeviceReadBuffer.cpp:267