Alex Bikfalvi
SimStream Documentation
StreamBufferSegment.cpp
00001 #include "Headers.h" 00002 #include "StreamBufferSegment.h" 00003 00004 CStreamBufferSegment::CStreamBufferSegment( 00005 __uint32 size, 00006 __uint32 sizeHistory, 00007 __uint32 sizeSegment 00008 ) 00009 { 00010 assert(size); 00011 assert(sizeHistory < size); 00012 00013 this->size = size; 00014 this->sizeHistory = sizeHistory; 00015 this->sizePlay = this->size - this->sizeHistory; 00016 this->sizeSegment = sizeSegment; 00017 00018 this->buffer = new CStreamSegment*[this->size]; 00019 for(__uint32 index = 0; index < this->size; index++) 00020 { 00021 this->buffer[index] = new CStreamSegment(this->sizeSegment); 00022 } 00023 00024 this->currentIndexSegment = 0; 00025 this->currentIndexFrame = 0; 00026 00027 this->ptrFirst = 0; 00028 this->ptrLast = 0; 00029 00030 this->stream = 0xFFFFFFFF; 00031 } 00032 00033 CStreamBufferSegment::~CStreamBufferSegment() 00034 { 00035 // Clean the buffer 00036 for(__uint32 index = 0; index < this->size; index++) 00037 delete this->buffer[index]; 00038 delete[] this->buffer; 00039 } 00040 00041 void CStreamBufferSegment::Reset(__uint32 stream, __uint32 indexSegment) 00042 { 00043 // Resets the buffer starting from the specified segment and adds the segment on the first position 00044 this->ptrLast = this->size - 1; 00045 this->ptrFirst = this->sizeHistory; 00046 00047 this->currentIndexSegment = indexSegment; // Segment index 00048 this->currentIndexFrame = 0; // Frame index within the segment 00049 00050 this->stream = stream; 00051 00052 // Reset the buffer segments 00053 00054 // Reset the buffer segments 00055 for(__uint32 index = (indexSegment < this->sizeHistory)?(this->sizeHistory - indexSegment):0; index < this->size; index++) 00056 this->buffer[index]->Reset(this->stream, indexSegment + index - this->sizeHistory); 00057 } 00058 00059 bool CStreamBufferSegment::Add(CStreamFrame frame) 00060 { 00061 if(frame.Stream() != this->stream) return false; 00062 00063 // Add the segment if it fits in the buffer 00064 if((frame.Segment() >= ((this->currentIndexSegment > this->sizeHistory)?(this->currentIndexSegment - this->sizeHistory):0)) && 00065 (frame.Segment() < this->currentIndexSegment + this->sizePlay)) 00066 { 00067 __uint32 idx = (this->size + this->ptrFirst + frame.Segment() - this->currentIndexSegment) % this->size; 00068 00069 // Check the segment 00070 assert(this->buffer[idx]->Stream() == this->stream); 00071 assert(this->buffer[idx]->Index() == frame.Segment()); 00072 00073 // Add the frame to the segment 00074 this->buffer[idx]->Add(frame); 00075 00076 return true; 00077 } 00078 else return false; 00079 } 00080 00081 void CStreamBufferSegment::ShiftFrame() 00082 { 00083 // Shift the frame 00084 this->currentIndexFrame = (this->currentIndexFrame + 1) % this->sizeSegment; 00085 00086 // If current frame moved to next segment, shift the segment 00087 if(0 == this->currentIndexFrame) this->ShiftSegment(); 00088 } 00089 00090 void CStreamBufferSegment::ShiftSegment() 00091 { 00092 // Shift both the first and last pointers 00093 this->ptrFirst = (this->ptrFirst + 1) % this->size; 00094 this->ptrLast = (this->ptrLast + 1) % this->size; 00095 00096 // Increment the current segment 00097 this->currentIndexSegment++; 00098 00099 // Calculate the index of the last segment 00100 __uint32 index = this->currentIndexSegment + ((this->ptrLast + this->size - this->ptrFirst) % this->size); 00101 00102 // Reset the last segment 00103 this->buffer[this->ptrLast]->Reset(this->stream, index); 00104 } 00105 00106 bool CStreamBufferSegment::HasSegment(__uint32 index) 00107 { 00108 return ((index >= ((this->currentIndexSegment > this->sizeHistory)?(this->currentIndexSegment - this->sizeHistory):0)) && 00109 (index < this->currentIndexSegment + this->sizePlay)); 00110 } 00111 00112 bool CStreamBufferSegment::IsSegmentReceiving(__uint32 index) 00113 { 00114 if((index >= ((this->currentIndexSegment > this->sizeHistory)?(this->currentIndexSegment - this->sizeHistory):0)) && 00115 (index < this->currentIndexSegment + this->sizePlay)) 00116 { 00117 __uint32 idx = (this->size + this->ptrFirst + index - this->currentIndexSegment) % this->size; 00118 return this->buffer[idx]->IsReceiving(); 00119 } 00120 else return false; 00121 } 00122 00123 bool CStreamBufferSegment::IsSegmentComplete(__uint32 index) 00124 { 00125 if((index >= ((this->currentIndexSegment > this->sizeHistory)?(this->currentIndexSegment - this->sizeHistory):0)) && 00126 (index < this->currentIndexSegment + this->sizePlay)) 00127 { 00128 __uint32 idx = (this->size + this->ptrFirst + index - this->currentIndexSegment) % this->size; 00129 return this->buffer[idx]->IsComplete(); 00130 } 00131 else return false; 00132 } 00133 00134 CStreamSegment* CStreamBufferSegment::GetSegment(__uint32 index) 00135 { 00136 assert((index >= ((this->currentIndexSegment > this->sizeHistory)?(this->currentIndexSegment - this->sizeHistory):0)) && 00137 (index < this->currentIndexSegment + this->sizePlay)); 00138 00139 __uint32 idx = (this->size + this->ptrFirst + index - this->currentIndexSegment) % this->size; 00140 return this->buffer[idx]; 00141 } 00142 00143 00144 void CStreamBufferSegment::SegmentReceiving(__uint32 index, bool receiving) 00145 { 00146 if((index >= ((this->currentIndexSegment > this->sizeHistory)?(this->currentIndexSegment - this->sizeHistory):0)) && 00147 (index < this->currentIndexSegment + this->sizePlay)) 00148 { 00149 __uint32 idx = (this->size + this->ptrFirst + index - this->currentIndexSegment) % this->size; 00150 this->buffer[idx]->Receiving(receiving); 00151 } 00152 } 00153 00154 bool CStreamBufferSegment::HasFrame(__uint32 index) 00155 { 00156 __uint32 indexSegment = index / this->sizeSegment; 00157 00158 if((indexSegment >= ((this->currentIndexSegment > this->sizeHistory)?(this->currentIndexSegment - this->sizeHistory):0)) && 00159 (indexSegment < this->currentIndexSegment + this->sizePlay)) 00160 { 00161 __uint32 idx = (this->size + this->ptrFirst + indexSegment - this->currentIndexSegment) % this->size; 00162 __uint32 indexFrame = index % this->sizeSegment; 00163 00164 if(this->buffer[idx] != NULL) return (*this->buffer[idx])[indexFrame].Valid(); 00165 else return false; 00166 } 00167 else return false; 00168 } 00169 00170 bool CStreamBufferSegment::HasCurrentFrame() 00171 { 00172 return (*this->buffer[this->ptrFirst])[this->currentIndexFrame].Valid(); 00173 } 00174 00175 CStreamFrame CStreamBufferSegment::CurrentFrame() 00176 { 00177 return (*this->buffer[this->ptrFirst])[this->currentIndexFrame]; 00178 } 00179 00180 bool CStreamBufferSegment::IsCurrentFrameDecodable() 00181 { 00182 if(this->buffer[this->ptrFirst] != NULL) 00183 { 00184 CStreamFrame* frame = &(*this->buffer[this->ptrFirst])[this->currentIndexFrame]; 00185 00186 if(frame->Valid()) 00187 { 00188 return 00189 (frame->AnchorP()?this->IsFrameDecodable(frame->Index() + frame->AnchorP()):1) && 00190 (frame->AnchorB()?this->IsFrameDecodable(frame->Index() + frame->AnchorB()):1); 00191 } 00192 else return false; 00193 } 00194 else return false; 00195 } 00196 00197 bool CStreamBufferSegment::IsFrameDecodable(__uint32 index) 00198 { 00199 __uint32 indexSegment = index / this->sizeSegment; 00200 00201 if((indexSegment >= ((this->currentIndexSegment > this->sizeHistory)?(this->currentIndexSegment - this->sizeHistory):0)) && 00202 (indexSegment < this->currentIndexSegment + this->sizePlay)) 00203 { 00204 __uint32 idx = (this->size + this->ptrFirst + indexSegment - this->currentIndexSegment) % this->size; 00205 00206 if(this->buffer[idx] != NULL) 00207 { 00208 __uint32 indexFrame = index % this->sizeSegment; 00209 CStreamFrame* frame = &(*this->buffer[idx])[indexFrame]; 00210 00211 if(frame->Valid()) 00212 { 00213 return 00214 (frame->AnchorP()?this->IsFrameDecodable(frame->Index() + frame->AnchorP()):1) && 00215 (frame->AnchorB()?this->IsFrameDecodable(frame->Index() + frame->AnchorB()):1); 00216 } 00217 else return false; 00218 } 00219 else return false; 00220 } 00221 else return false; 00222 }
Last updated: February 8, 2011