00001
00002
00003
00004 #ifndef _DEVICEREADBUFFER_H_
00005 #define _DEVICEREADBUFFER_H_
00006
00007 #include <unistd.h>
00008
00009 #include <QMutex>
00010 #include <QWaitCondition>
00011 #include <QString>
00012
00013 #include "tspacket.h"
00014 #include "mthread.h"
00015 #include "mythmiscutil.h"
00016
00017 class DeviceReaderCB
00018 {
00019 protected:
00020 virtual ~DeviceReaderCB() {}
00021 public:
00022 virtual void ReaderPaused(int fd) = 0;
00023 virtual void PriorityEvent(int fd) = 0;
00024 };
00025
00033 class DeviceReadBuffer : protected MThread
00034 {
00035 public:
00036 DeviceReadBuffer(DeviceReaderCB *callback,
00037 bool use_poll = true,
00038 bool error_exit_on_poll_timeout = true);
00039 ~DeviceReadBuffer();
00040
00041 bool Setup(const QString &streamName,
00042 int streamfd,
00043 uint readQuanta = sizeof(TSPacket),
00044 uint deviceBufferSize = 0);
00045
00046 void Start(void);
00047 void Reset(const QString &streamName, int streamfd);
00048 void Stop(void);
00049
00050 void SetRequestPause(bool request);
00051 bool IsPaused(void) const;
00052 bool WaitForUnpause(unsigned long timeout);
00053 bool WaitForPaused(unsigned long timeout);
00054
00055 bool IsErrored(void) const;
00056 bool IsEOF(void) const;
00057 bool IsRunning(void) const;
00058
00059 uint Read(unsigned char *buf, uint count);
00060
00061 private:
00062 virtual void run(void);
00063
00064 void SetPaused(bool);
00065 void IncrWritePointer(uint len);
00066 void IncrReadPointer(uint len);
00067
00068 bool HandlePausing(void);
00069 bool Poll(void) const;
00070 void WakePoll(void) const;
00071 uint WaitForUnused(uint bytes_needed) const;
00072 uint WaitForUsed (uint bytes_needed, uint max_wait ) const;
00073
00074 bool IsPauseRequested(void) const;
00075 bool IsOpen(void) const { return _stream_fd >= 0; }
00076 void ClosePipes(void) const;
00077 uint GetUnused(void) const;
00078 uint GetUsed(void) const;
00079 uint GetContiguousUnused(void) const;
00080
00081 bool CheckForErrors(ssize_t read_len, size_t requested_len, uint &err_cnt);
00082 void ReportStats(void);
00083
00084 QString videodevice;
00085 int _stream_fd;
00086 mutable int wake_pipe[2];
00087 mutable long wake_pipe_flags[2];
00088
00089 DeviceReaderCB *readerCB;
00090
00091
00092 mutable QMutex lock;
00093 volatile bool dorun;
00094 bool eof;
00095 mutable bool error;
00096 bool request_pause;
00097 bool paused;
00098 bool using_poll;
00099 bool poll_timeout_is_error;
00100 uint max_poll_wait;
00101
00102 size_t size;
00103 size_t used;
00104 size_t read_quanta;
00105 size_t dev_read_size;
00106 size_t min_read;
00107 unsigned char *buffer;
00108 unsigned char *readPtr;
00109 unsigned char *writePtr;
00110 unsigned char *endPtr;
00111
00112 mutable QWaitCondition dataWait;
00113 QWaitCondition runWait;
00114 QWaitCondition pauseWait;
00115 QWaitCondition unpauseWait;
00116
00117
00118 size_t max_used;
00119 size_t avg_used;
00120 size_t avg_cnt;
00121 MythTimer lastReport;
00122 };
00123
00124 #endif // _DEVICEREADBUFFER_H_
00125
00126
00127
00128