00001 // -*- Mode: c++ -*- 00002 /* Device Buffer written by John Poet */ 00003 00004 #ifndef _DEVICEREADBUFFER_H_ 00005 #define _DEVICEREADBUFFER_H_ 00006 00007 #include <unistd.h> 00008 #include <pthread.h> 00009 00010 #include <qmutex.h> 00011 #include <qwaitcondition.h> 00012 #include <qstring.h> 00013 00014 #include "util.h" 00015 00016 class ReaderPausedCB 00017 { 00018 protected: 00019 virtual ~ReaderPausedCB() {} 00020 public: 00021 virtual void ReaderPaused(int fd) = 0; 00022 }; 00023 00031 class DeviceReadBuffer 00032 { 00033 public: 00034 DeviceReadBuffer(ReaderPausedCB *callback, bool use_poll = true); 00035 ~DeviceReadBuffer(); 00036 00037 bool Setup(const QString &streamName, int streamfd); 00038 00039 void Start(void); 00040 void Reset(const QString &streamName, int streamfd); 00041 void Stop(void); 00042 00043 void SetRequestPause(bool request); 00044 bool IsPaused(void) const; 00045 bool WaitForUnpause(int timeout); 00046 00047 bool IsErrored(void) const { return error; } 00048 bool IsEOF(void) const { return eof; } 00049 bool IsRunning(void) const; 00050 00051 uint Read(unsigned char *buf, uint count); 00052 00053 private: 00054 static void *boot_ringbuffer(void *); 00055 void fill_ringbuffer(void); 00056 00057 void SetPaused(bool); 00058 void IncrWritePointer(uint len); 00059 void IncrReadPointer(uint len); 00060 00061 bool HandlePausing(void); 00062 bool Poll(void) const; 00063 uint WaitForUnused(uint bytes_needed) const; 00064 uint WaitForUsed (uint bytes_needed) const; 00065 00066 bool IsPauseRequested(void) const; 00067 bool IsOpen(void) const { return _stream_fd >= 0; } 00068 uint GetUnused(void) const; 00069 uint GetUsed(void) const; 00070 uint GetContiguousUnused(void) const; 00071 00072 bool CheckForErrors(ssize_t read_len, uint &err_cnt); 00073 void ReportStats(void); 00074 00075 QString videodevice; 00076 int _stream_fd; 00077 00078 ReaderPausedCB *readerPausedCB; 00079 pthread_t thread; 00080 00081 // Data for managing the device ringbuffer 00082 mutable QMutex lock; 00083 bool run; 00084 bool running; 00085 bool eof; 00086 bool error; 00087 bool request_pause; 00088 bool paused; 00089 bool using_poll; 00090 00091 size_t size; 00092 size_t used; 00093 size_t dev_read_size; 00094 size_t min_read; 00095 unsigned char *buffer; 00096 unsigned char *readPtr; 00097 unsigned char *writePtr; 00098 unsigned char *endPtr; 00099 00100 QWaitCondition pauseWait; 00101 QWaitCondition unpauseWait; 00102 00103 // statistics 00104 size_t max_used; 00105 size_t avg_used; 00106 size_t avg_cnt; 00107 MythTimer lastReport; 00108 }; 00109 00110 #endif // _DEVICEREADBUFFER_H_
1.5.5