From e4ae98e93a8657c65f366370a17426272ca2f111 Mon Sep 17 00:00:00 2001 From: giongto35 Date: Wed, 15 May 2019 02:14:24 +0800 Subject: [PATCH] Add iceCandidates logic --- handler/browser.go | 27 +++++++++------------------ handler/overlord.go | 2 +- static/js/ws.js | 4 +++- webrtc/webrtc.go | 18 +++++++++++++++++- 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/handler/browser.go b/handler/browser.go index 6c174f5f..c8580d83 100644 --- a/handler/browser.go +++ b/handler/browser.go @@ -1,13 +1,11 @@ package handler import ( - "encoding/json" "log" "github.com/giongto35/cloud-game/config" "github.com/giongto35/cloud-game/cws" "github.com/gorilla/websocket" - pionRTC "github.com/pion/webrtc" ) type BrowserClient struct { @@ -16,15 +14,23 @@ type BrowserClient struct { // RouteBrowser are all routes server received from browser func (s *Session) RouteBrowser() { + iceCandidates := [][]byte{} + browserClient := s.BrowserClient browserClient.Receive("heartbeat", func(resp cws.WSPacket) cws.WSPacket { return resp }) + browserClient.Receive("icecandidate", func(resp cws.WSPacket) cws.WSPacket { + log.Println("Received candidates ", resp.Data) + iceCandidates = append(iceCandidates, []byte(resp.Data)) + return cws.EmptyPacket + }) + browserClient.Receive("initwebrtc", func(resp cws.WSPacket) cws.WSPacket { log.Println("Received user SDP") - localSession, err := s.peerconnection.StartClient(resp.Data, config.Width, config.Height) + localSession, err := s.peerconnection.StartClient(resp.Data, iceCandidates, config.Width, config.Height) if err != nil { if err != nil { log.Println("Error: Cannot create new webrtc session", err) @@ -121,21 +127,6 @@ func (s *Session) RouteBrowser() { return req }) - - browserClient.Receive("candidate", func(resp cws.WSPacket) (req cws.WSPacket) { - // Unuse code - hi := pionRTC.ICECandidateInit{} - err := json.Unmarshal([]byte(resp.Data), &hi) - if err != nil { - log.Println("[!] Cannot parse candidate: ", err) - } else { - // webRTC.AddCandidate(hi) - } - req.ID = "candidate" - - return req - }) - } // NewOverlordClient returns a client connecting to browser. This connection exchanges information between clients and server diff --git a/handler/overlord.go b/handler/overlord.go index 3243ec5f..19ed73f5 100644 --- a/handler/overlord.go +++ b/handler/overlord.go @@ -54,7 +54,7 @@ func (s *Session) RouteOverlord() { log.Println("Start peerconnection from the sdp") peerconnection := webrtc.NewWebRTC() // init new peerconnection from sessionID - localSession, err := peerconnection.StartClient(resp.Data, config.Width, config.Height) + localSession, err := peerconnection.StartClient(resp.Data, nil, config.Width, config.Height) oclient.peerconnections[resp.SessionID] = peerconnection if err != nil { diff --git a/static/js/ws.js b/static/js/ws.js index 96629aed..774e1785 100644 --- a/static/js/ws.js +++ b/static/js/ws.js @@ -167,7 +167,6 @@ function startWebRTC() { log(`iceConnectionState: ${pc.iceConnectionState}`); if (pc.iceConnectionState === "connected") { - //conn.send(JSON.stringify({"id": "start", "data": ""})); } else if (pc.iceConnectionState === "disconnected") { @@ -193,6 +192,9 @@ function startWebRTC() { // TODO: Fix curPacketID conn.send(JSON.stringify({"id": "initwebrtc", "data": session, "packet_id": curPacketID})); } else { + //pc.addIceCandidate(event.candidate).catch(e => { + //log("Failure during addIceCandidate(): " + e.name);}); + conn.send(JSON.stringify({"id": "icecandidate", "data": JSON.stringify(event.candidate)})); console.log(JSON.stringify(event.candidate)); } } diff --git a/webrtc/webrtc.go b/webrtc/webrtc.go index f1b0b1cd..2a97f22d 100644 --- a/webrtc/webrtc.go +++ b/webrtc/webrtc.go @@ -92,7 +92,7 @@ type WebRTC struct { } // StartClient start webrtc -func (w *WebRTC) StartClient(remoteSession string, width, height int) (string, error) { +func (w *WebRTC) StartClient(remoteSession string, iceCandidates [][]byte, width, height int) (string, error) { defer func() { if err := recover(); err != nil { log.Println(err) @@ -201,11 +201,27 @@ func (w *WebRTC) StartClient(remoteSession string, width, height int) (string, e return "", err } + // Parse candidates list + for _, bcandidate := range iceCandidates { + iceCandidate := webrtc.ICECandidateInit{} + if err := json.Unmarshal(bcandidate, &iceCandidate); err != nil { + log.Println("Cannot parse ", bcandidate) + continue + } + w.connection.AddICECandidate(iceCandidate) + } + answer, err := w.connection.CreateAnswer(nil) if err != nil { return "", err } + err = w.connection.SetLocalDescription(answer) + if err != nil { + return "", err + } + + // Sendback answer from server localSession := Encode(answer) return localSession, nil }