From d697c08b028809e2a0f6485f69a09faa9d399d28 Mon Sep 17 00:00:00 2001 From: giongto35 Date: Sun, 8 Sep 2019 15:25:02 +0800 Subject: [PATCH] Update encoding checking at webrtc initialization --- config/config.go | 4 ---- static/js/ws.js | 5 ++--- util/util.go | 12 ++++++++++++ webrtc/webrtc.go | 6 ++++-- worker/overlord.go | 28 +++++++++++++++------------- 5 files changed, 33 insertions(+), 22 deletions(-) diff --git a/config/config.go b/config/config.go index a450af55..81d4a852 100644 --- a/config/config.go +++ b/config/config.go @@ -25,10 +25,6 @@ var ProdEnv = "prod" const NumKeys = 10 -var Codec = CODEC_H264 - -//var Codec = CODEC_VP8 - var FileTypeToEmulator = map[string]string{ "gba": "gba", "cue": "pcsx", diff --git a/static/js/ws.js b/static/js/ws.js index 9c6d1eec..3f06be06 100644 --- a/static/js/ws.js +++ b/static/js/ws.js @@ -209,8 +209,7 @@ function startWebRTC(iceservers) { session = btoa(JSON.stringify(pc.localDescription)); log("Send SDP to remote peer"); // TODO: Fix curPacketID - //conn.send(JSON.stringify({"id": "initwebrtc", "data": session, "packet_id": curPacketID})); - conn.send(JSON.stringify({"id": "initwebrtc", "data": session})); + conn.send(JSON.stringify({"id": "initwebrtc", "data": JSON.stringify({"sdp": session, "is_mobile": isMobileDevice()})})); iceSent = true } } else { @@ -221,7 +220,7 @@ function startWebRTC(iceservers) { if (!iceSent) { log("Ice gathering timeout, send anyway") session = btoa(JSON.stringify(pc.localDescription)); - conn.send(JSON.stringify({"id": "initwebrtc", "data": session})); + conn.send(JSON.stringify({"id": "initwebrtc", "data": JSON.stringify({"sdp": session, "is_mobile": isMobileDevice()})})); iceSent = true; } }, ICE_TIMEOUT) diff --git a/util/util.go b/util/util.go index f83dda6d..c042c99c 100644 --- a/util/util.go +++ b/util/util.go @@ -6,6 +6,8 @@ import ( "log" "os/user" "unsafe" + + "github.com/giongto35/cloud-game/config" ) // https://stackoverflow.com/questions/9465815/rgb-to-yuv420-algorithm-efficiency @@ -78,3 +80,13 @@ func GetSavePath(roomID string) string { func savePath(hash string) string { return homeDir + "/.cr/save/" + hash + ".dat" } + +// GetVideoEncoder returns video encoder based on some qualification. +// Actually Android is only supporting VP8 but H264 has better encoding performance +// TODO: Better use useragent attribute from frontend +func GetVideoEncoder(isMobile bool) string { + if isMobile == true { + return config.CODEC_VP8 + } + return config.CODEC_H264 +} diff --git a/webrtc/webrtc.go b/webrtc/webrtc.go index 82b82551..d4918a90 100644 --- a/webrtc/webrtc.go +++ b/webrtc/webrtc.go @@ -11,6 +11,7 @@ import ( "time" "github.com/giongto35/cloud-game/config" + "github.com/giongto35/cloud-game/util" "github.com/gofrs/uuid" "github.com/pion/webrtc/v2" "github.com/pion/webrtc/v2/pkg/media" @@ -92,7 +93,7 @@ type WebRTC struct { } // StartClient start webrtc -func (w *WebRTC) StartClient(remoteSession string, iceCandidates []string) (string, error) { +func (w *WebRTC) StartClient(remoteSession string, isMobile bool, iceCandidates []string) (string, error) { defer func() { if err := recover(); err != nil { log.Println(err) @@ -114,7 +115,7 @@ func (w *WebRTC) StartClient(remoteSession string, iceCandidates []string) (stri return "", err } - if config.Codec == config.CODEC_H264 { + if util.GetVideoEncoder(isMobile) == config.CODEC_H264 { videoTrack, err = w.connection.NewTrack(webrtc.DefaultPayloadTypeH264, rand.Uint32(), "video", "pion2") } else { videoTrack, err = w.connection.NewTrack(webrtc.DefaultPayloadTypeVP8, rand.Uint32(), "video", "pion2") @@ -122,6 +123,7 @@ func (w *WebRTC) StartClient(remoteSession string, iceCandidates []string) (stri if err != nil { return "", err } + _, err = w.connection.AddTrack(videoTrack) if err != nil { return "", err diff --git a/worker/overlord.go b/worker/overlord.go index ba05e026..f2db0448 100644 --- a/worker/overlord.go +++ b/worker/overlord.go @@ -2,10 +2,11 @@ package worker import ( "encoding/json" + "fmt" "log" - "github.com/giongto35/cloud-game/config" "github.com/giongto35/cloud-game/cws" + "github.com/giongto35/cloud-game/util" "github.com/giongto35/cloud-game/webrtc" "github.com/giongto35/cloud-game/worker/room" "github.com/gorilla/websocket" @@ -50,8 +51,19 @@ func (h *Handler) RouteOverlord() { "initwebrtc", func(resp cws.WSPacket) (req cws.WSPacket) { log.Println("Received relay SDP of a browser from overlord") + peerconnection := webrtc.NewWebRTC() - localSession, err := peerconnection.StartClient(resp.Data, iceCandidates[resp.SessionID]) + var initPacket struct { + SDP string `json:"sdp"` + IsMobile bool `json:"is_mobile"` + } + fmt.Println("HIHIHIHI!!!!", resp.Data) + err := json.Unmarshal([]byte(resp.Data), &initPacket) + if err != nil { + panic(err) + } + fmt.Println("HIHIHIHI!!!!", initPacket) + localSession, err := peerconnection.StartClient(initPacket.SDP, initPacket.IsMobile, iceCandidates[resp.SessionID]) //h.peerconnections[resp.SessionID] = peerconnection // Create new sessions when we have new peerconnection initialized @@ -92,7 +104,7 @@ func (h *Handler) RouteOverlord() { panic(err) } - room := h.startGameHandler(startPacket.GameName, resp.RoomID, resp.PlayerIndex, peerconnection, getVideoEncoder(startPacket.IsMobile)) + room := h.startGameHandler(startPacket.GameName, resp.RoomID, resp.PlayerIndex, peerconnection, util.GetVideoEncoder(startPacket.IsMobile)) session.RoomID = room.ID // TODO: can data race h.rooms[room.ID] = room @@ -206,16 +218,6 @@ func getServerIDOfRoom(oc *OverlordClient, roomID string) string { return packet.Data } -// getVideoEncoder returns video encoder based on some qualification. -// Actually Android is only supporting VP8 but H264 has better encoding performance -// TODO: Better use useragent attribute from frontend -func getVideoEncoder(isMobile bool) string { - if isMobile == true { - return config.CODEC_VP8 - } - return config.CODEC_H264 -} - func (h *Handler) startGameHandler(gameName, roomID string, playerIndex int, peerconnection *webrtc.WebRTC, videoEncoderType string) *room.Room { log.Println("Starting game", gameName) // If we are connecting to overlord, request corresponding serverID based on roomID