protected override void ReceiveCallback(IAsyncResult ar) { //check network delay var receiveTime = FStopWatch.Elapsed.TotalSeconds; var netDelay = (receiveTime - FSendTime) * 0.5; netDelay = FNetDelayFilter.Update(netDelay); base.ReceiveCallback(ar); if (FReceiveSuccess) { double serverTime = BitConverter.ToDouble(FReceivedBytes, 0); Debug.WriteLine(serverTime); //estimate offset var offset = serverTime - (receiveTime - netDelay); FTimeOffsetFilter.Update(offset); Debug.WriteLine("offset: " + FTimeOffsetFilter.Value); if (Math.Abs(FTimeOffsetFilter.Value) > 0.01) { Offset = FTimeOffsetFilter.Value; } } }
protected void ClientEvaluate() { var fCount = 5; lock (FLock) { var offset = FIsClient[0] ? FHalfRoundTripDelay : FTimeStamp - FReceivedTimeStamp; var streamDiff = FReceivedStreamTime - FStreamTime + offset + FFineOffset[0] * 0.001; streamDiff = Math.Abs(streamDiff) > FLength[0] * 0.5 ? streamDiff - FLength[0] * Math.Sign(streamDiff) : streamDiff; var doSeek = Math.Abs(streamDiff) > 1; FStreamDiffFilter.Update(streamDiff); FDoSeekOut[0] = doSeek; FSeekTimeOut[0] = FReceivedStreamTime + offset + 0.05; var doAdjust = Math.Abs(FStreamDiffFilter.Value) > 0.001; if (!doSeek && FFrameCounter == 0 && doAdjust) { FAdjustTimeOut[0] = FStreamDiffFilter.Value * 50; } else { FAdjustTimeOut[0] = 0; } FStreamOffsetOut[0] = FStreamDiffFilter.Value; FOffsetOut[0] = offset; FFrameCounter = (++FFrameCounter) % fCount; } }