00001
00002
00003 #ifndef RINGBUFFER
00004 #define RINGBUFFER
00005
00006 #include <qstring.h>
00007 #include <qwaitcondition.h>
00008 #include <qmutex.h>
00009 #include <pthread.h>
00010
00011 extern "C" {
00012 #include "avcodec.h"
00013 }
00014
00015 #include "mythexp.h"
00016
00017 class RemoteFile;
00018 class RemoteEncoder;
00019 class ThreadedFileWriter;
00020 class DVDRingBufferPriv;
00021 class LiveTVChain;
00022
00023 class MPUBLIC RingBuffer
00024 {
00025 public:
00026 RingBuffer(const QString &lfilename, bool write,
00027 bool usereadahead = true, uint read_retries = 12);
00028 ~RingBuffer();
00029
00030
00031 void SetWriteBufferSize(int newSize);
00032 void SetWriteBufferMinWriteSize(int newMinSize);
00033 void UpdateRawBitrate(uint rawbitrate);
00034 void UpdatePlaySpeed(float playspeed);
00035
00036
00038 QString GetFilename(void) const { return filename; }
00040 int DataInReadAhead(void) const { return ReadBufAvail(); }
00043 bool GetStopReads(void) const { return stopreads; }
00046 bool isPaused(void) const
00047 { return (!readaheadrunning) ? true : readaheadpaused; }
00048 long long GetReadPosition(void) const;
00049 long long GetWritePosition(void) const;
00050 long long GetRealFileSize(void) const;
00051 uint GetBitrate(void) const;
00052 uint GetReadBlockSize(void) const;
00053 bool IsOpen(void) const;
00054
00055
00056 void OpenFile(const QString &lfilename, uint retryCount = 12);
00057 int Read(void *buf, int count);
00058 int Peek(void *buf, int count);
00059
00060 void Reset(bool full = false,
00061 bool toAdjust = false,
00062 bool resetInternal = false);
00063
00064
00065 long long Seek(long long pos, int whence);
00066
00067
00068 void Pause(void);
00069 void Unpause(void);
00070 void WaitForPause(void);
00071
00072
00073 void Start(void);
00074 void StopReads(void);
00075 void StartReads(void);
00076
00077
00078 bool LiveMode(void) const;
00079 void SetLiveMode(LiveTVChain *chain);
00081 void IgnoreLiveEOF(bool ignore) { ignoreliveeof = ignore; }
00082
00083
00084 int Write(const void *buf, uint count);
00085 bool IsIOBound(void) const;
00086 void WriterFlush(void);
00087 void Sync(void);
00088 long long WriterSeek(long long pos, int whence);
00089
00090
00092 inline bool isDVD(void) const { return dvdPriv; }
00093 DVDRingBufferPriv *DVD() { return dvdPriv; }
00094 bool InDVDMenuOrStillFrame(void);
00095
00096 long long SetAdjustFilesize(void);
00097 void SetTimeout(bool fast) { oldfile = fast; }
00098
00099 protected:
00100 static void *StartReader(void *type);
00101 void ReadAheadThread(void);
00102
00103 private:
00104 void CalcReadAheadThresh(void);
00105 int safe_read_dvd(void *data, uint sz);
00106 int safe_read(int fd, void *data, uint sz);
00107 int safe_read(RemoteFile *rf, void *data, uint sz);
00108
00109 int ReadFromBuf(void *buf, int count, bool peek = false);
00110
00111 int ReadBufFree(void) const;
00112 int ReadBufAvail(void) const;
00113
00114 void StartupReadAheadThread(void);
00115 void ResetReadAhead(long long newinternal);
00116 void KillReadAheadThread(void);
00117
00118 private:
00119 QString filename;
00120
00121 ThreadedFileWriter *tfw;
00122 int fd2;
00123
00124 bool writemode;
00125
00126 long long readpos;
00127 long long writepos;
00128
00129 bool stopreads;
00130
00131 mutable pthread_rwlock_t rwlock;
00132
00133 int recorder_num;
00134 RemoteEncoder *remoteencoder;
00135 RemoteFile *remotefile;
00136
00137 mutable QMutex readAheadLock;
00138 pthread_t reader;
00139
00140 bool startreadahead;
00141 char *readAheadBuffer;
00142 bool readaheadrunning;
00143 bool readaheadpaused;
00144 bool pausereadthread;
00145 int rbrpos;
00146 int rbwpos;
00147 long long internalreadpos;
00148 bool ateof;
00149 bool readsallowed;
00150 volatile bool wantseek;
00151 bool setswitchtonext;
00152
00153 mutable QMutex bitratelock;
00154 uint rawbitrate;
00155 float playspeed;
00156 int fill_threshold;
00157 int fill_min;
00158 int readblocksize;
00159
00160 QWaitCondition pauseWait;
00161
00162 int wanttoread;
00163 QWaitCondition availWait;
00164 QMutex availWaitMutex;
00165
00166 QWaitCondition readsAllowedWait;
00167
00168 int numfailures;
00169
00170 bool commserror;
00171
00172 DVDRingBufferPriv *dvdPriv;
00173
00174 bool oldfile;
00175
00176 LiveTVChain *livetvchain;
00177 bool ignoreliveeof;
00178
00179 long long readAdjust;
00180
00182 QWaitCondition readAheadRunningCond;
00183 QMutex readAheadRunningCondLock;
00184
00185
00186 static const uint kBufferSize;
00187 static const uint kReadTestSize;
00188 };
00189
00190 #endif