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 ()
 
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)
 
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, uint 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_stream_fd {-1}
 
int m_wake_pipe [2] {-1,-1}
 
long m_wake_pipe_flags [2] {0,0}
 
DeviceReaderCBm_readerCB {nullptr}
 
QMutex m_lock
 
volatile bool m_dorun {false}
 
bool m_eof {false}
 
bool m_error {false}
 
bool m_request_pause {false}
 
bool m_paused {false}
 
bool m_using_poll {true}
 
bool m_poll_timeout_is_error {true}
 
uint m_max_poll_wait {2500 }
 
size_t m_size {0}
 
size_t m_used {0}
 
size_t m_read_quanta {0}
 
size_t m_dev_buffer_count {1}
 
size_t m_dev_read_size {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_max_used {0}
 
size_t m_avg_used {0}
 
size_t m_avg_buf_write_cnt {0}
 
size_t m_avg_buf_read_cnt {0}
 
size_t m_avg_buf_sleep_cnt {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 ()
 
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=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 (unsigned long time=ULONG_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 sleep (unsigned long time)
 
static void msleep (unsigned long time)
 
static void usleep (unsigned long time)
 
static void ThreadSetup (const QString &)
 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_prolog_executed {true}
 
bool m_epilog_executed {true}
 
- Static Protected Attributes inherited from MThread
static const int kDefaultStartTimeout
 

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 33 of file DeviceReadBuffer.h.

Constructor & Destructor Documentation

◆ DeviceReadBuffer()

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

Definition at line 21 of file DeviceReadBuffer.cpp.

◆ ~DeviceReadBuffer()

DeviceReadBuffer::~DeviceReadBuffer ( )

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 
)

Definition at line 49 of file DeviceReadBuffer.cpp.

Referenced by V4L2encStreamHandler::Open(), and DVBStreamHandler::RunTS().

◆ 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().

◆ Stop()

void DeviceReadBuffer::Stop ( void  )

◆ SetRequestPause()

void DeviceReadBuffer::SetRequestPause ( bool  request)

◆ IsPaused()

bool DeviceReadBuffer::IsPaused ( void  ) const

Definition at line 211 of file DeviceReadBuffer.cpp.

Referenced by HandlePausing().

◆ WaitForUnpause()

bool DeviceReadBuffer::WaitForUnpause ( unsigned long  timeout)

Definition at line 227 of file DeviceReadBuffer.cpp.

◆ WaitForPaused()

bool DeviceReadBuffer::WaitForPaused ( unsigned long  timeout)

Definition at line 217 of file DeviceReadBuffer.cpp.

◆ IsErrored()

bool DeviceReadBuffer::IsErrored ( void  ) const

Definition at line 243 of file DeviceReadBuffer.cpp.

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

◆ IsEOF()

bool DeviceReadBuffer::IsEOF ( void  ) const

Definition at line 249 of file DeviceReadBuffer.cpp.

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

◆ 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 620 of file DeviceReadBuffer.cpp.

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

◆ GetUsed()

uint DeviceReadBuffer::GetUsed ( void  ) const

Definition at line 267 of file DeviceReadBuffer.cpp.

◆ 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 304 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 279 of file DeviceReadBuffer.cpp.

Referenced by run().

◆ IncrReadPointer()

void DeviceReadBuffer::IncrReadPointer ( uint  len)
private

Definition at line 293 of file DeviceReadBuffer.cpp.

Referenced by Read().

◆ HandlePausing()

bool DeviceReadBuffer::HandlePausing ( void  )
private

Definition at line 392 of file DeviceReadBuffer.cpp.

Referenced by run().

◆ Poll()

bool DeviceReadBuffer::Poll ( void  ) const
private

Definition at line 412 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 662 of file DeviceReadBuffer.cpp.

Referenced by run().

◆ WaitForUsed()

uint DeviceReadBuffer::WaitForUsed ( uint  needed,
uint  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 688 of file DeviceReadBuffer.cpp.

Referenced by Read().

◆ IsPauseRequested()

bool DeviceReadBuffer::IsPauseRequested ( void  ) const
private

Definition at line 237 of file DeviceReadBuffer.cpp.

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

◆ IsOpen()

bool DeviceReadBuffer::IsOpen ( void  ) const
inlineprivate

Definition at line 77 of file DeviceReadBuffer.h.

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

◆ ClosePipes()

void DeviceReadBuffer::ClosePipes ( void  ) const
private

Definition at line 198 of file DeviceReadBuffer.cpp.

Referenced by run(), and WakePoll().

◆ GetUnused()

uint DeviceReadBuffer::GetUnused ( void  ) const
private

Definition at line 261 of file DeviceReadBuffer.cpp.

Referenced by WaitForUnused().

◆ GetContiguousUnused()

uint DeviceReadBuffer::GetContiguousUnused ( void  ) const
private

Definition at line 273 of file DeviceReadBuffer.cpp.

◆ CheckForErrors()

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

Definition at line 539 of file DeviceReadBuffer.cpp.

Referenced by run().

◆ ReportStats()

void DeviceReadBuffer::ReportStats ( void  )
private

Definition at line 705 of file DeviceReadBuffer.cpp.

Referenced by Read().

Member Data Documentation

◆ m_videodevice

QString DeviceReadBuffer::m_videodevice
private

Definition at line 85 of file DeviceReadBuffer.h.

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

◆ m_stream_fd

int DeviceReadBuffer::m_stream_fd {-1}
private

Definition at line 86 of file DeviceReadBuffer.h.

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

◆ m_wake_pipe

int DeviceReadBuffer::m_wake_pipe[2] {-1,-1}
mutableprivate

Definition at line 87 of file DeviceReadBuffer.h.

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

◆ m_wake_pipe_flags

long DeviceReadBuffer::m_wake_pipe_flags[2] {0,0}
mutableprivate

Definition at line 88 of file DeviceReadBuffer.h.

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

◆ m_readerCB

DeviceReaderCB* DeviceReadBuffer::m_readerCB {nullptr}
private

Definition at line 90 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 94 of file DeviceReadBuffer.h.

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

◆ m_eof

bool DeviceReadBuffer::m_eof {false}
private

Definition at line 95 of file DeviceReadBuffer.h.

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

◆ m_error

bool DeviceReadBuffer::m_error {false}
mutableprivate

Definition at line 96 of file DeviceReadBuffer.h.

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

◆ m_request_pause

bool DeviceReadBuffer::m_request_pause {false}
private

Definition at line 97 of file DeviceReadBuffer.h.

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

◆ m_paused

bool DeviceReadBuffer::m_paused {false}
private

Definition at line 98 of file DeviceReadBuffer.h.

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

◆ m_using_poll

bool DeviceReadBuffer::m_using_poll {true}
private

Definition at line 99 of file DeviceReadBuffer.h.

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

◆ m_poll_timeout_is_error

bool DeviceReadBuffer::m_poll_timeout_is_error {true}
private

Definition at line 100 of file DeviceReadBuffer.h.

Referenced by Poll().

◆ m_max_poll_wait

uint DeviceReadBuffer::m_max_poll_wait {2500 }
private

Definition at line 101 of file DeviceReadBuffer.h.

Referenced by Poll().

◆ m_size

size_t DeviceReadBuffer::m_size {0}
private

Definition at line 103 of file DeviceReadBuffer.h.

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

◆ m_used

size_t DeviceReadBuffer::m_used {0}
private

◆ m_read_quanta

size_t DeviceReadBuffer::m_read_quanta {0}
private

Definition at line 105 of file DeviceReadBuffer.h.

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

◆ m_dev_buffer_count

size_t DeviceReadBuffer::m_dev_buffer_count {1}
private

Definition at line 106 of file DeviceReadBuffer.h.

Referenced by run(), and Setup().

◆ m_dev_read_size

size_t DeviceReadBuffer::m_dev_read_size {0}
private

Definition at line 107 of file DeviceReadBuffer.h.

Referenced by run(), and Setup().

◆ m_readThreshold

size_t DeviceReadBuffer::m_readThreshold {0}
private

Definition at line 108 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 110 of file DeviceReadBuffer.h.

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

◆ m_writePtr

unsigned char* DeviceReadBuffer::m_writePtr {nullptr}
private

Definition at line 111 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 114 of file DeviceReadBuffer.h.

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

◆ m_runWait

QWaitCondition DeviceReadBuffer::m_runWait
private

Definition at line 115 of file DeviceReadBuffer.h.

Referenced by run(), and Start().

◆ m_pauseWait

QWaitCondition DeviceReadBuffer::m_pauseWait
private

Definition at line 116 of file DeviceReadBuffer.h.

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

◆ m_unpauseWait

QWaitCondition DeviceReadBuffer::m_unpauseWait
private

Definition at line 117 of file DeviceReadBuffer.h.

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

◆ m_max_used

size_t DeviceReadBuffer::m_max_used {0}
private

Definition at line 120 of file DeviceReadBuffer.h.

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

◆ m_avg_used

size_t DeviceReadBuffer::m_avg_used {0}
private

Definition at line 121 of file DeviceReadBuffer.h.

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

◆ m_avg_buf_write_cnt

size_t DeviceReadBuffer::m_avg_buf_write_cnt {0}
private

Definition at line 122 of file DeviceReadBuffer.h.

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

◆ m_avg_buf_read_cnt

size_t DeviceReadBuffer::m_avg_buf_read_cnt {0}
private

Definition at line 123 of file DeviceReadBuffer.h.

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

◆ m_avg_buf_sleep_cnt

size_t DeviceReadBuffer::m_avg_buf_sleep_cnt {0}
private

Definition at line 124 of file DeviceReadBuffer.h.

Referenced by ReportStats(), and Setup().

◆ m_lastReport

MythTimer DeviceReadBuffer::m_lastReport
private

Definition at line 125 of file DeviceReadBuffer.h.

Referenced by ReportStats(), and Setup().


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