diff --git a/main.go b/main.go index 07ebb72f..70fd7f87 100644 --- a/main.go +++ b/main.go @@ -203,6 +203,10 @@ func ws(w http.ResponseWriter, r *http.Request) { //log.Println("Ping from server with game:", gameName) //res.ID = "pong" + case "pingpong": + res.ID = "pingpong" + res.Data = req.Data + case "initwebrtc": log.Println("Received user SDP") localSession, err := webRTC.StartClient(req.Data, width, height) diff --git a/static/js/ws.js b/static/js/ws.js index afcf04c3..d236d1eb 100644 --- a/static/js/ws.js +++ b/static/js/ws.js @@ -33,6 +33,7 @@ conn.onmessage = e => { //startWebRTC(); break; case "pingpong": + console.log("Ping: ", Date.now() - d["data"]) // TODO: Calc time break; case "start": @@ -51,7 +52,7 @@ conn.onmessage = e => { function sendPing() { // TODO: format the package with time - conn.send(JSON.stringify({"id": "pingpong", "data": "pingpong"})); + conn.send(JSON.stringify({"id": "pingpong", "data": Date.now().toString()})); } function startWebRTC() { diff --git a/vpx-encoder/encoder.go b/vpx-encoder/encoder.go index 24393e75..6a5c6b7b 100644 --- a/vpx-encoder/encoder.go +++ b/vpx-encoder/encoder.go @@ -2,6 +2,8 @@ package vpxencoder import ( "fmt" + "log" + "time" "unsafe" ) @@ -113,6 +115,8 @@ func (v *VpxEncoder) init() error { func (v *VpxEncoder) startLooping() { go func() { for { + beginEncoding := time.Now() + yuv := <-v.Input // Add Image v.vpxCodexIter = nil @@ -140,6 +144,8 @@ func (v *VpxEncoder) startLooping() { } v.Output <- bs } + + log.Println("Encoding time: ", time.Now().Sub(beginEncoding)) } }() } diff --git a/webrtc/webrtc.go b/webrtc/webrtc.go index c1e828ff..56464933 100644 --- a/webrtc/webrtc.go +++ b/webrtc/webrtc.go @@ -99,6 +99,11 @@ func NewWebRTC() *WebRTC { return w } +type InputDataPair struct { + data int + time time.Time +} + // WebRTC connection type WebRTC struct { connection *webrtc.PeerConnection @@ -109,7 +114,9 @@ type WebRTC struct { ImageChannel chan []byte InputChannel chan int - Done chan struct{} + Done chan struct{} + lastTime time.Time + curFPS int RoomID string } @@ -183,7 +190,12 @@ func (w *WebRTC) StartClient(remoteSession string, width, height int) (string, e // Register text message handling d.OnMessage(func(msg webrtc.DataChannelMessage) { + //layout .:= "2006-01-02T15:04:05.000Z" + //if t, err := time.Parse(layout, string(msg.Data[1])); err == nil { + //fmt.Println("Delay ", time.Now().Sub(t)) + //} else { w.InputChannel <- int(msg.Data[0]) + //} }) d.OnClose(func() { @@ -262,7 +274,16 @@ func (w *WebRTC) startStreaming(vp8Track *webrtc.Track) { go func() { for w.isConnected { bs := <-w.encoder.Output + log.Println("FPS : ", w.calculateFPS()) vp8Track.WriteSample(media.Sample{Data: bs, Samples: 1}) } }() } + +func (w *WebRTC) calculateFPS() int { + elapsedTime := time.Now().Sub(w.lastTime) + w.lastTime = time.Now() + curFPS := time.Second / elapsedTime + w.curFPS = int(float32(w.curFPS)*0.9 + float32(curFPS)*0.1) + return w.curFPS +}