MythTV  master
Public Member Functions | Private Member Functions | Private Attributes | List of all members
DeviceReadBuffer Class Reference

Buffers reads from device files. More...

#include <DeviceReadBuffer.h>

Inheritance diagram for DeviceReadBuffer:
Inheritance graph
[legend]
Collaboration diagram for DeviceReadBuffer:
Collaboration graph
[legend]

Public Member Functions

 DeviceReadBuffer (DeviceReaderCB *cb, bool use_poll=true, bool error_exit_on_poll_timeout=true)
 
 ~DeviceReadBuffer () override
 
bool Setup (const QString &streamName, int streamfd, uint readQuanta=sizeof(TSPacket), uint deviceBufferSize=0, uint deviceBufferCount=1)
 
void Start (void)
 
void Reset (const QString &streamName, int streamfd)
 
void Stop (void)
 
void SetRequestPause (bool request)
 
bool IsPaused (void) const
 
bool WaitForUnpause (unsigned long timeout)
 
bool WaitForPaused (unsigned long timeout)
 
bool IsErrored (void) const
 
bool IsEOF (void) const
 
bool IsRunning (void) const
 
uint Read (unsigned char *buf, uint count)
 Try to Read count bytes from into buffer. More...
 
uint GetUsed (void) const
 

Private Member Functions

void run (void) override
 Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead. More...
 
void SetPaused (bool val)
 
void IncrWritePointer (uint len)
 
void IncrReadPointer (uint len)
 
bool HandlePausing (void)
 
bool Poll (void) const
 
void WakePoll (void) const
 
uint WaitForUnused (uint needed) const
 
uint WaitForUsed (uint needed, std::chrono::milliseconds max_wait) const
 
bool IsPauseRequested (void) const
 
bool IsOpen (void) const
 
void ClosePipes (void) const
 
uint GetUnused (void) const
 
uint GetContiguousUnused (void) const
 
bool CheckForErrors (ssize_t read_len, size_t requested_len, uint &errcnt)
 
void ReportStats (void)
 

Private Attributes

QString m_videoDevice
 
int m_streamFd {-1}
 
pipe_fd_array m_wakePipe {-1,-1}
 
pipe_flag_array m_wakePipeFlags {0,0}
 
DeviceReaderCBm_readerCB {nullptr}
 
QMutex m_lock
 
volatile bool m_doRun {false}
 
bool m_eof {false}
 
bool m_error {false}
 
bool m_requestPause {false}
 
bool m_paused {false}
 
bool m_usingPoll {true}
 
bool m_pollTimeoutIsError {true}
 
std::chrono::milliseconds m_maxPollWait {2500ms}
 
size_t m_size {0}
 
size_t m_used {0}
 
size_t m_readQuanta {0}
 
size_t m_devBufferCount {1}
 
size_t m_devReadSize {0}
 
size_t m_readThreshold {0}
 
unsigned char * m_buffer {nullptr}
 
unsigned char * m_readPtr {nullptr}
 
unsigned char * m_writePtr {nullptr}
 
unsigned char * m_endPtr {nullptr}
 
QWaitCondition m_dataWait
 
QWaitCondition m_runWait
 
QWaitCondition m_pauseWait
 
QWaitCondition m_unpauseWait
 
size_t m_maxUsed {0}
 
size_t m_avgUsed {0}
 
size_t m_avgBufWriteCnt {0}
 
size_t m_avgBufReadCnt {0}
 
size_t m_avgBufSleepCnt {0}
 
MythTimer m_lastReport
 

Additional Inherited Members

- Protected Member Functions inherited from MThread
int exec (void)
 Enters the qt event loop. call exit or quit to exit thread. More...
 
 MThread (const QString &objectName)
 Standard constructor. More...
 
 MThread (const QString &objectName, QRunnable *runnable)
 Use this constructor if you want the default run() method to run the QRunnable's run() method instead of entering the Qt event loop. More...
 
virtual ~MThread ()
 
 MThread (const MThread &)=delete
 
MThreadoperator= (const MThread &)=delete
 
void RunProlog (void)
 Sets up a thread, call this if you reimplement run(). More...
 
void RunEpilog (void)
 Cleans up a thread's resources, call this if you reimplement run(). More...
 
QThread * qthread (void)
 Returns the thread, this will always return the same pointer no matter how often you restart the thread. More...
 
void setObjectName (const QString &name)
 
QString objectName (void) const
 
void setPriority (QThread::Priority priority)
 
QThread::Priority priority (void) const
 
bool isFinished (void) const
 
bool isRunning (void) const
 
void setStackSize (uint stackSize)
 
uint stackSize (void) const
 
void exit (int retcode=0)
 Use this to exit from the thread if you are using a Qt event loop. More...
 
void start (QThread::Priority p=QThread::InheritPriority)
 Tell MThread to start running the thread in the near future. More...
 
void terminate (void)
 Kill a thread unsafely. More...
 
void quit (void)
 calls exit(0) More...
 
bool wait (std::chrono::milliseconds time=std::chrono::milliseconds::max())
 Wait for the MThread to exit, with a maximum timeout. More...
 
- Static Protected Member Functions inherited from MThread
static void setTerminationEnabled (bool enabled=true)
 
static void usleep (std::chrono::microseconds time)
 
template<typename R , typename P >
static std::enable_if_t< std::chrono::treat_as_floating_point< R >::value, void > usleep (std::chrono::duration< R, P > time)
 
static void ThreadSetup (const QString &name)
 This is to be called on startup in those few threads that haven't been ported to MThread. More...
 
static void ThreadCleanup (void)
 This is to be called on exit in those few threads that haven't been ported to MThread. More...
 
static void Cleanup (void)
 This will print out all the running threads, call exit(1) on each and then wait up to 5 seconds total for all the threads to exit. More...
 
static void GetAllThreadNames (QStringList &list)
 
static void GetAllRunningThreadNames (QStringList &list)
 
- Protected Attributes inherited from MThread
MThreadInternalm_thread {nullptr}
 
QRunnable * m_runnable {nullptr}
 
bool m_prologExecuted {true}
 
bool m_epilogExecuted {true}
 

Detailed Description

Buffers reads from device files.

This allows us to read the device regularly even in the presence of long blocking conditions on writing to disk or accessing the database.

Definition at line 35 of file DeviceReadBuffer.h.

Constructor & Destructor Documentation

◆ DeviceReadBuffer()

DeviceReadBuffer::DeviceReadBuffer ( DeviceReaderCB cb,
bool  use_poll = true,
bool  error_exit_on_poll_timeout = true 
)
explicit

Definition at line 21 of file DeviceReadBuffer.cpp.

◆ ~DeviceReadBuffer()

DeviceReadBuffer::~DeviceReadBuffer ( )
override

Definition at line 39 of file DeviceReadBuffer.cpp.

Member Function Documentation

◆ Setup()

bool DeviceReadBuffer::Setup ( const QString &  streamName,
int  streamfd,
uint  readQuanta = sizeof(TSPacket),
uint  deviceBufferSize = 0,
uint  deviceBufferCount = 1 
)

◆ Start()

void DeviceReadBuffer::Start ( void  )

◆ Reset()

void DeviceReadBuffer::Reset ( const QString &  streamName,
int  streamfd 
)

Definition at line 134 of file DeviceReadBuffer.cpp.

Referenced by HandlePausing(), and MpegRecorder::StartEncoding().

◆ Stop()

void DeviceReadBuffer::Stop ( void  )

◆ SetRequestPause()

void DeviceReadBuffer::SetRequestPause ( bool  request)

◆ IsPaused()

bool DeviceReadBuffer::IsPaused ( void  ) const

Definition at line 210 of file DeviceReadBuffer.cpp.

Referenced by HandlePausing().

◆ WaitForUnpause()

bool DeviceReadBuffer::WaitForUnpause ( unsigned long  timeout)

Definition at line 226 of file DeviceReadBuffer.cpp.

◆ WaitForPaused()

bool DeviceReadBuffer::WaitForPaused ( unsigned long  timeout)

Definition at line 216 of file DeviceReadBuffer.cpp.

◆ IsErrored()

bool DeviceReadBuffer::IsErrored ( void  ) const

◆ IsEOF()

bool DeviceReadBuffer::IsEOF ( void  ) const

◆ IsRunning()

bool DeviceReadBuffer::IsRunning ( void  ) const

◆ Read()

uint DeviceReadBuffer::Read ( unsigned char *  buf,
uint  count 
)

Try to Read count bytes from into buffer.

Parameters
bufBuffer to put data in
countNumber of bytes to attempt to read
Returns
number of bytes actually read

Definition at line 616 of file DeviceReadBuffer.cpp.

Referenced by MpegRecorder::run(), V4L2encStreamHandler::run(), and DVBStreamHandler::RunTS().

◆ GetUsed()

uint DeviceReadBuffer::GetUsed ( void  ) const

Definition at line 266 of file DeviceReadBuffer.cpp.

Referenced by V4L2encStreamHandler::AvailCount().

◆ run()

void DeviceReadBuffer::run ( void  )
overrideprivatevirtual

Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead.

Note
If you override this method you must call RunProlog before you do any work and RunEpilog before you exit the run method.

Reimplemented from MThread.

Definition at line 303 of file DeviceReadBuffer.cpp.

◆ SetPaused()

void DeviceReadBuffer::SetPaused ( bool  val)
private

Definition at line 170 of file DeviceReadBuffer.cpp.

Referenced by HandlePausing().

◆ IncrWritePointer()

void DeviceReadBuffer::IncrWritePointer ( uint  len)
private

Definition at line 278 of file DeviceReadBuffer.cpp.

Referenced by run().

◆ IncrReadPointer()

void DeviceReadBuffer::IncrReadPointer ( uint  len)
private

Definition at line 292 of file DeviceReadBuffer.cpp.

Referenced by Read().

◆ HandlePausing()

bool DeviceReadBuffer::HandlePausing ( void  )
private

Definition at line 389 of file DeviceReadBuffer.cpp.

Referenced by run().

◆ Poll()

bool DeviceReadBuffer::Poll ( void  ) const
private

Definition at line 409 of file DeviceReadBuffer.cpp.

Referenced by run().

◆ WakePoll()

void DeviceReadBuffer::WakePoll ( void  ) const
private

Definition at line 181 of file DeviceReadBuffer.cpp.

Referenced by SetRequestPause(), Start(), and Stop().

◆ WaitForUnused()

uint DeviceReadBuffer::WaitForUnused ( uint  needed) const
private
Parameters
neededNumber of bytes we want to write
Returns
bytes available for writing

Definition at line 658 of file DeviceReadBuffer.cpp.

Referenced by run().

◆ WaitForUsed()

uint DeviceReadBuffer::WaitForUsed ( uint  needed,
std::chrono::milliseconds  max_wait 
) const
private
Parameters
neededNumber of bytes we want to read
max_waitNumber of milliseconds to wait for the needed data
Returns
bytes available for reading

Definition at line 684 of file DeviceReadBuffer.cpp.

Referenced by Read().

◆ IsPauseRequested()

bool DeviceReadBuffer::IsPauseRequested ( void  ) const
private

Definition at line 236 of file DeviceReadBuffer.cpp.

Referenced by HandlePausing(), Poll(), and WaitForUnused().

◆ IsOpen()

bool DeviceReadBuffer::IsOpen ( void  ) const
inlineprivate

Definition at line 79 of file DeviceReadBuffer.h.

Referenced by Poll(), run(), and WaitForUnused().

◆ ClosePipes()

void DeviceReadBuffer::ClosePipes ( void  ) const
private

Definition at line 197 of file DeviceReadBuffer.cpp.

Referenced by run(), and WakePoll().

◆ GetUnused()

uint DeviceReadBuffer::GetUnused ( void  ) const
private

Definition at line 260 of file DeviceReadBuffer.cpp.

Referenced by WaitForUnused().

◆ GetContiguousUnused()

uint DeviceReadBuffer::GetContiguousUnused ( void  ) const
private

Definition at line 272 of file DeviceReadBuffer.cpp.

◆ CheckForErrors()

bool DeviceReadBuffer::CheckForErrors ( ssize_t  read_len,
size_t  requested_len,
uint errcnt 
)
private

Definition at line 535 of file DeviceReadBuffer.cpp.

Referenced by run().

◆ ReportStats()

void DeviceReadBuffer::ReportStats ( void  )
private

Definition at line 701 of file DeviceReadBuffer.cpp.

Referenced by Read().

Member Data Documentation

◆ m_videoDevice

QString DeviceReadBuffer::m_videoDevice
private

Definition at line 87 of file DeviceReadBuffer.h.

Referenced by HandlePausing(), Reset(), and Setup().

◆ m_streamFd

int DeviceReadBuffer::m_streamFd {-1}
private

Definition at line 88 of file DeviceReadBuffer.h.

Referenced by CheckForErrors(), HandlePausing(), IsOpen(), Poll(), Reset(), run(), and Setup().

◆ m_wakePipe

pipe_fd_array DeviceReadBuffer::m_wakePipe {-1,-1}
mutableprivate

Definition at line 89 of file DeviceReadBuffer.h.

Referenced by ClosePipes(), Poll(), run(), and WakePoll().

◆ m_wakePipeFlags

pipe_flag_array DeviceReadBuffer::m_wakePipeFlags {0,0}
mutableprivate

Definition at line 90 of file DeviceReadBuffer.h.

Referenced by ClosePipes(), Poll(), and run().

◆ m_readerCB

DeviceReaderCB* DeviceReadBuffer::m_readerCB {nullptr}
private

Definition at line 92 of file DeviceReadBuffer.h.

Referenced by HandlePausing(), and Poll().

◆ m_lock

QMutex DeviceReadBuffer::m_lock
mutableprivate

◆ m_doRun

volatile bool DeviceReadBuffer::m_doRun {false}
private

Definition at line 96 of file DeviceReadBuffer.h.

Referenced by Poll(), run(), Start(), Stop(), and WaitForUnused().

◆ m_eof

bool DeviceReadBuffer::m_eof {false}
private

Definition at line 97 of file DeviceReadBuffer.h.

Referenced by CheckForErrors(), IsEOF(), run(), Setup(), Start(), and WaitForUsed().

◆ m_error

bool DeviceReadBuffer::m_error {false}
mutableprivate

Definition at line 98 of file DeviceReadBuffer.h.

Referenced by CheckForErrors(), IsErrored(), Poll(), Reset(), run(), Setup(), Start(), and WaitForUsed().

◆ m_requestPause

bool DeviceReadBuffer::m_requestPause {false}
private

Definition at line 99 of file DeviceReadBuffer.h.

Referenced by IsPauseRequested(), SetRequestPause(), Setup(), and WaitForUsed().

◆ m_paused

bool DeviceReadBuffer::m_paused {false}
private

Definition at line 100 of file DeviceReadBuffer.h.

Referenced by IsPaused(), SetPaused(), Setup(), WaitForPaused(), and WaitForUnpause().

◆ m_usingPoll

bool DeviceReadBuffer::m_usingPoll {true}
private

Definition at line 101 of file DeviceReadBuffer.h.

Referenced by DeviceReadBuffer(), run(), and Setup().

◆ m_pollTimeoutIsError

bool DeviceReadBuffer::m_pollTimeoutIsError {true}
private

Definition at line 102 of file DeviceReadBuffer.h.

Referenced by Poll().

◆ m_maxPollWait

std::chrono::milliseconds DeviceReadBuffer::m_maxPollWait {2500ms}
private

Definition at line 103 of file DeviceReadBuffer.h.

Referenced by Poll().

◆ m_size

size_t DeviceReadBuffer::m_size {0}
private

Definition at line 105 of file DeviceReadBuffer.h.

Referenced by GetUnused(), ReportStats(), and Setup().

◆ m_used

size_t DeviceReadBuffer::m_used {0}
private

◆ m_readQuanta

size_t DeviceReadBuffer::m_readQuanta {0}
private

Definition at line 107 of file DeviceReadBuffer.h.

Referenced by run(), Setup(), and WaitForUnused().

◆ m_devBufferCount

size_t DeviceReadBuffer::m_devBufferCount {1}
private

Definition at line 108 of file DeviceReadBuffer.h.

Referenced by run(), and Setup().

◆ m_devReadSize

size_t DeviceReadBuffer::m_devReadSize {0}
private

Definition at line 109 of file DeviceReadBuffer.h.

Referenced by run(), and Setup().

◆ m_readThreshold

size_t DeviceReadBuffer::m_readThreshold {0}
private

Definition at line 110 of file DeviceReadBuffer.h.

Referenced by Read(), and Setup().

◆ m_buffer

unsigned char* DeviceReadBuffer::m_buffer {nullptr}
private

◆ m_readPtr

unsigned char* DeviceReadBuffer::m_readPtr {nullptr}
private

Definition at line 112 of file DeviceReadBuffer.h.

Referenced by IncrReadPointer(), Read(), Reset(), and Setup().

◆ m_writePtr

unsigned char* DeviceReadBuffer::m_writePtr {nullptr}
private

Definition at line 113 of file DeviceReadBuffer.h.

Referenced by GetContiguousUnused(), IncrWritePointer(), Reset(), run(), and Setup().

◆ m_endPtr

unsigned char* DeviceReadBuffer::m_endPtr {nullptr}
private

◆ m_dataWait

QWaitCondition DeviceReadBuffer::m_dataWait
mutableprivate

Definition at line 116 of file DeviceReadBuffer.h.

Referenced by IncrWritePointer(), run(), and WaitForUsed().

◆ m_runWait

QWaitCondition DeviceReadBuffer::m_runWait
private

Definition at line 117 of file DeviceReadBuffer.h.

Referenced by run(), and Start().

◆ m_pauseWait

QWaitCondition DeviceReadBuffer::m_pauseWait
private

Definition at line 118 of file DeviceReadBuffer.h.

Referenced by run(), SetPaused(), and WaitForPaused().

◆ m_unpauseWait

QWaitCondition DeviceReadBuffer::m_unpauseWait
private

Definition at line 119 of file DeviceReadBuffer.h.

Referenced by run(), SetPaused(), and WaitForUnpause().

◆ m_maxUsed

size_t DeviceReadBuffer::m_maxUsed {0}
private

Definition at line 122 of file DeviceReadBuffer.h.

Referenced by IncrWritePointer(), ReportStats(), and Setup().

◆ m_avgUsed

size_t DeviceReadBuffer::m_avgUsed {0}
private

Definition at line 123 of file DeviceReadBuffer.h.

Referenced by IncrWritePointer(), ReportStats(), and Setup().

◆ m_avgBufWriteCnt

size_t DeviceReadBuffer::m_avgBufWriteCnt {0}
private

Definition at line 124 of file DeviceReadBuffer.h.

Referenced by IncrWritePointer(), ReportStats(), and Setup().

◆ m_avgBufReadCnt

size_t DeviceReadBuffer::m_avgBufReadCnt {0}
private

Definition at line 125 of file DeviceReadBuffer.h.

Referenced by IncrReadPointer(), ReportStats(), and Setup().

◆ m_avgBufSleepCnt

size_t DeviceReadBuffer::m_avgBufSleepCnt {0}
private

Definition at line 126 of file DeviceReadBuffer.h.

Referenced by ReportStats(), and Setup().

◆ m_lastReport

MythTimer DeviceReadBuffer::m_lastReport
private

Definition at line 127 of file DeviceReadBuffer.h.

Referenced by ReportStats(), and Setup().


The documentation for this class was generated from the following files: