Fix to clock skew bug

This commit is contained in:
Neale Pickett 2020-06-29 19:28:51 -06:00
parent bbc788d829
commit dcf10eb123
1 changed files with 21 additions and 10 deletions

View File

@ -294,6 +294,7 @@ class Vail {
this.clockOffset = 0 // How badly our clock is off of the server's this.clockOffset = 0 // How badly our clock is off of the server's
this.rxDelay = 0 // Milliseconds to add to incoming timestamps this.rxDelay = 0 // Milliseconds to add to incoming timestamps
this.beginTxTime = null // Time when we began transmitting this.beginTxTime = null // Time when we began transmitting
this.debug = localStorage.debug
this.openSocket() this.openSocket()
@ -460,7 +461,7 @@ class Vail {
} }
wsSend(time, duration) { wsSend(time, duration) {
let msg = [time + this.clockOffset, duration] let msg = [time - this.clockOffset, duration]
let jmsg = JSON.stringify(msg) let jmsg = JSON.stringify(msg)
this.socket.send(jmsg) this.socket.send(jmsg)
this.sent.push(jmsg) this.sent.push(jmsg)
@ -480,16 +481,10 @@ class Vail {
let beginTxTime = msg[0] let beginTxTime = msg[0]
let durations = msg.slice(1) let durations = msg.slice(1)
// Server is telling us the current time if (this.debug) {
if (durations.length == 0) { console.log("recv", beginTxTime, durations)
let offset = now - beginTxTime
if (this.clockOffset == 0) {
this.clockOffset = offset
this.updateReadings()
}
return
} }
let sent = this.sent.filter(e => e != jmsg) let sent = this.sent.filter(e => e != jmsg)
if (sent.length < this.sent.length) { if (sent.length < this.sent.length) {
// We're getting our own message back, which tells us our lag. // We're getting our own message back, which tells us our lag.
@ -500,9 +495,25 @@ class Vail {
return return
} }
// Server is telling us the current time
if (durations.length == 0) {
let offset = now - beginTxTime
if (this.clockOffset == 0) {
this.clockOffset = offset
this.updateReadings()
}
return
}
// Why is this happening?
if (beginTxTime == 0) {
return
}
// Add rxDelay
let adjustedTxTime = beginTxTime+this.rxDelay let adjustedTxTime = beginTxTime+this.rxDelay
if (adjustedTxTime < now) { if (adjustedTxTime < now) {
console.log("adjustedTxTime: ", adjustedTxTime, " now: ", now)
this.error("Packet requested playback " + (now - adjustedTxTime) + "ms in the past. Increase receive delay!") this.error("Packet requested playback " + (now - adjustedTxTime) + "ms in the past. Increase receive delay!")
return return
} }