SimpleStatistics.hpp

00001 #ifndef WINCE 00002 00003 #ifndef VITOOKI_SIMPLE_STATISTICS_HPP 00004 #define VITOOKI_SIMPLE_STATISTICS_HPP 00005 #include "global.hpp" 00006 00007 class ESInfo; 00008 00009 #define VITOOKI_SIMPLESTATISTICS_MAXSECONDS 128 00010 class SimpleStatistics 00011 { 00012 public: 00013 typedef struct StatEntry{ 00014 // streamoutsec in absolute time! 00015 u32 second; 00016 u32 numPacketsIn; 00017 u32 numBytesIn; 00018 u32 numFramesIn; 00019 u32 numPacketsOut; 00020 u32 numBytesOut; 00021 u32 numFramesOut; 00022 u32 packetsDropped; 00023 u32 packetsLost; 00025 u32 msBuffered; 00026 u32 packetsBuffered; 00027 u32 bytesBuffered; 00028 u32 firstTS; 00029 u32 lastTS; 00030 u32 firstCTS; 00031 u32 lastCTS; 00032 00033 StatEntry() { 00034 reset(0); 00035 } 00036 StatEntry(u32 sec) { 00037 reset(sec); 00038 }; 00039 void reset(u32 sec) { 00040 second=sec; 00041 numPacketsIn=numBytesIn=numFramesIn=0; 00042 numPacketsOut=numBytesOut=numFramesOut=0; 00043 msBuffered=packetsBuffered=bytesBuffered=0; 00044 firstTS=0xffffffff; lastTS=0; 00045 firstCTS=0xffffffff; lastCTS=0; 00046 packetsDropped=packetsLost=0; 00047 }; 00049 void operator+=(const StatEntry &t) { 00050 if(second>t.second) { 00051 second=t.second; 00052 } 00053 numPacketsIn+= t.numPacketsIn; 00054 numBytesIn+= t.numBytesIn; 00055 numFramesIn+= t.numFramesIn; 00056 numPacketsOut+= t.numPacketsOut; 00057 numBytesOut+= t.numBytesOut; 00058 numFramesOut+= t.numFramesOut; 00059 packetsDropped+= t.packetsDropped; 00060 packetsLost+= t.packetsLost; 00061 msBuffered+= t.msBuffered; 00062 packetsBuffered+= t.packetsBuffered; 00063 bytesBuffered+= t.bytesBuffered; 00064 if(firstTS>t.firstTS) 00065 firstTS=t.firstTS; 00066 if(lastTS<t.lastTS) 00067 lastTS=t.lastTS; 00068 if(firstCTS>t.firstCTS) 00069 firstCTS=t.firstCTS; 00070 if(lastCTS<t.lastCTS) 00071 lastCTS=t.lastCTS; 00072 }; 00074 static StatEntry *createFromString(const char* tmp) { 00075 StatEntry* s=new StatEntry(); 00076 int res=sscanf(tmp,"%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u", 00077 &s->second,&s->numPacketsIn,&s->numBytesIn,&s->numFramesIn, 00078 &s->numPacketsOut,&s->numBytesOut,&s->numFramesOut, 00079 &s->packetsBuffered,&s->bytesBuffered,&s->msBuffered, 00080 &s->packetsDropped,&s->firstTS,&s->lastTS,&s->packetsLost,&s->firstCTS,&s->lastCTS); 00081 if(res!=16) { 00082 delete s; 00083 s=NULL; 00084 } 00085 return s; 00086 }; 00090 static bool createFromString(const char* tmp, StatEntry& s) { 00091 int res=sscanf(tmp,"%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u", 00092 &s.second,&s.numPacketsIn,&s.numBytesIn,&s.numFramesIn, 00093 &s.numPacketsOut,&s.numBytesOut,&s.numFramesOut, 00094 &s.packetsBuffered,&s.bytesBuffered,&s.msBuffered, 00095 &s.packetsDropped,&s.firstTS,&s.lastTS,&s.packetsLost,&s.firstCTS,&s.lastCTS); 00096 return (res==16); 00097 }; 00099 bool convertToString(char* tmp,int stringSize) const{ 00100 int res=snprintf(tmp,stringSize,"%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u", 00101 second,numPacketsIn,numBytesIn,numFramesIn, 00102 numPacketsOut,numBytesOut,numFramesOut, 00103 packetsBuffered,bytesBuffered,msBuffered, 00104 packetsDropped,firstTS,lastTS,packetsLost,firstCTS,lastCTS); 00105 return (res<stringSize); 00106 }; 00107 } StatEntry; 00108 00111 SimpleStatistics(const char* saveToFileName,const ESInfo* es, bool serverMode, 00112 bool overwriteFileIfExists=true); 00113 ~SimpleStatistics(); 00114 00115 void insertFrame(u32 cts, u32 bytes,u32 seqNrStart,u32 seqNrEnd); 00116 void insertPacket(u32 cts, u32 bytes, u32 seqNr, bool firstPacketOfFrame); 00117 void getFrame(u32 cts,u32 bytes,u32 seqNrStart,u32 seqNrEnd); 00120 void dropInsertedPacket(u32 bytes, u32 seqNr, u32 cts,bool noOtherPacketsWereDroppedForThisFrame); 00121 00123 void notifyLostPacket(u32 seqNr); 00126 void play(u32 cts,bool clearBuffers=true); 00127 void pause(); 00128 void teardown(); 00129 u32 getStartUpDelayInMS() const { return startUpDelayInMS;}; 00131 u32 getNumBytesOutLastNSeconds(int n); 00134 StatEntry getSumOfLastNSeconds(int n); 00135 static u32 getCurrentSecond(); 00136 static u32 getCurrentMilliSecond(); 00137 static s32 calcDiffInMS(const struct timeval &oldTime, const struct timeval &newTime); 00138 00147 static bool createGlobalLog(const char* outFile, const char* srcDir, const char* key1, 00148 const char* key2=NULL, const char* key3=NULL); 00149 00150 struct timeval* getstartTime() 00151 { 00152 return &startTime; 00153 } 00154 00155 char* getfileName() 00156 { 00157 return fileName; 00158 } 00159 00160 protected: 00163 void saveToFile(int startPos,int endPos); 00164 static void saveOneStatEntry(FILE* fp, const StatEntry& stat); 00165 void detectNewSecond(); 00166 00167 00169 int currentPos; 00171 int lastNotSavedPos; 00173 struct timeval startTime; 00175 u32 lastCTS; 00177 u32 highestCTSSeen; 00179 u32 lastGetCTS; 00181 struct timeval pauseTime; 00183 u32 startUpDelayInMS; 00184 const ESInfo* es; 00185 u32 lastSecSaved; 00186 StatEntry stats[VITOOKI_SIMPLESTATISTICS_MAXSECONDS]; 00187 char fileName[MAX_STR_LEN]; 00189 bool playCalled; 00190 bool serverMode; 00191 bool teardownCalled; 00192 }; 00193 #endif 00194 00195 #endif //wince