Should fix all buffer-related timing issues.

This commit is contained in:
Thulinma 2012-05-24 09:30:00 +02:00
parent 7afbdd1ec3
commit 7936c7efaa

View file

@ -22,10 +22,10 @@ namespace Buffer{
Socket::Server SS; ///< The server socket. Socket::Server SS; ///< The server socket.
/// Gets the current system time in milliseconds. /// Gets the current system time in milliseconds.
unsigned int getNowMS(){ long long int getNowMS(){
timeval t; timeval t;
gettimeofday(&t, 0); gettimeofday(&t, 0);
return t.tv_sec + t.tv_usec/1000; return t.tv_sec * 1000 + t.tv_usec/1000;
}//getNowMS }//getNowMS
@ -105,24 +105,24 @@ namespace Buffer{
/// Loop reading DTSC data from stdin and processing it at the correct speed. /// Loop reading DTSC data from stdin and processing it at the correct speed.
void handleStdin(void * empty){ void handleStdin(void * empty){
if (empty != 0){return;} if (empty != 0){return;}
unsigned int lastPacketTime = 0;//time in MS last packet was parsed long long int timeDiff = 0;//difference between local time and stream time
unsigned int currPacketTime = 0;//time of the last parsed packet (current packet) unsigned int lastPacket = 0;//last parsed packet timestamp
unsigned int prevPacketTime = 0;//time of the previously parsed packet (current packet - 1)
std::string inBuffer; std::string inBuffer;
char charBuffer[1024*10]; char charBuffer[1024*10];
unsigned int charCount; unsigned int charCount;
unsigned int now; long long int now;
while (std::cin.good() && buffer_running){ while (std::cin.good() && buffer_running){
//slow down packet receiving to real-time //slow down packet receiving to real-time
now = getNowMS(); now = getNowMS();
if ((now - lastPacketTime >= currPacketTime - prevPacketTime) || (currPacketTime <= prevPacketTime)){ if ((now - timeDiff >= lastPacket) || (lastPacket - (now - timeDiff) > 5000)){
thisStream->getWriteLock(); thisStream->getWriteLock();
if (thisStream->getStream()->parsePacket(inBuffer)){ if (thisStream->getStream()->parsePacket(inBuffer)){
thisStream->getStream()->outPacket(0); thisStream->getStream()->outPacket(0);
lastPacketTime = now; lastPacket = thisStream->getStream()->getTime();
prevPacketTime = currPacketTime; if ((now - timeDiff - lastPacket) > 5000 || (now - timeDiff - lastPacket < -5000)){
currPacketTime = thisStream->getStream()->getTime(); timeDiff = now - lastPacket;
}
thisStream->dropWriteLock(true); thisStream->dropWriteLock(true);
}else{ }else{
thisStream->dropWriteLock(false); thisStream->dropWriteLock(false);
@ -131,10 +131,10 @@ namespace Buffer{
inBuffer.append(charBuffer, charCount); inBuffer.append(charBuffer, charCount);
} }
}else{ }else{
if (((currPacketTime - prevPacketTime) - (now - lastPacketTime)) > 999){ if ((lastPacket - (now - timeDiff)) > 999){
usleep(999000); usleep(999000);
}else{ }else{
usleep(((currPacketTime - prevPacketTime) - (now - lastPacketTime)) * 999); usleep((lastPacket - (now - timeDiff)) * 1000);
} }
} }
} }