mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-22 09:37:09 +00:00
Add Monitor
This commit is contained in:
parent
97e4986f40
commit
fca3a2cac4
4 changed files with 34 additions and 2 deletions
4
main.go
4
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)
|
||||
|
|
|
|||
3
static/js/ws.js
vendored
3
static/js/ws.js
vendored
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue