From e0a6ce2c6c4d569eff64e31907e3f041cf0ea9fa Mon Sep 17 00:00:00 2001 From: giongto35 Date: Sun, 26 May 2019 22:18:41 +0800 Subject: [PATCH] Fix icecandidate setup (#44) * Use own stun server * Add ice candidate * Add turn server * Up main_test * Fix datachannel negotiated --- cmd/main_test.go | 4 +++- overlord/browser.go | 12 ++++++++---- static/js/ws.js | 16 ++++++++++++++-- webrtc/webrtc.go | 23 ++++++++++++----------- worker/overlord.go | 14 ++++++++++++-- 5 files changed, 49 insertions(+), 20 deletions(-) diff --git a/cmd/main_test.go b/cmd/main_test.go index ea58f64b..00348ea4 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -23,7 +23,9 @@ var host = "http://localhost:8000" // Test is in cmd, so gamePath is in parent path var testGamePath = "../games" -var webrtcconfig = webrtc.Configuration{ICEServers: []webrtc.ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}}} +var webrtcconfig = webrtc.Configuration{ICEServers: []webrtc.ICEServer{{ + URLs: []string{"stun:stun.l.google.com:19302"}, +}}} func initOverlord() (*httptest.Server, *httptest.Server) { server := overlord.NewServer() diff --git a/overlord/browser.go b/overlord/browser.go index 33eea0ed..2171ca6b 100644 --- a/overlord/browser.go +++ b/overlord/browser.go @@ -13,8 +13,6 @@ type BrowserClient struct { // RouteBrowser are all routes server accepts for browser func (s *Session) RouteBrowser() { - iceCandidates := [][]byte{} - browserClient := s.BrowserClient browserClient.Receive("heartbeat", func(resp cws.WSPacket) cws.WSPacket { @@ -22,9 +20,15 @@ func (s *Session) RouteBrowser() { }) browserClient.Receive("icecandidate", func(resp cws.WSPacket) cws.WSPacket { - log.Println("Received candidates ", resp.Data) + log.Println("Overlord: Received icecandidate from a browser", resp.Data) + log.Println("Overlord: Relay icecandidate from a browser to worker") + + wc, ok := s.handler.workerClients[s.ServerID] + if !ok { + return cws.EmptyPacket + } + wc.Send(resp, nil) - iceCandidates = append(iceCandidates, []byte(resp.Data)) return cws.EmptyPacket }) diff --git a/static/js/ws.js b/static/js/ws.js index 48e5d265..e80d13f6 100644 --- a/static/js/ws.js +++ b/static/js/ws.js @@ -96,7 +96,15 @@ function sendPing() { function startWebRTC() { // webrtc - pc = new RTCPeerConnection({iceServers: [{urls: ['stun:stun.l.google.com:19302']}]}) + pc = new RTCPeerConnection({iceServers: [{ + urls: 'stun:159.65.141.209:3478', + username: "root", + credential: "root" + }, { + urls: "turn:159.65.141.209:3478", + username: "root", + credential: "root" + }]}) // input channel, ordered + reliable, id 0 inputChannel = pc.createDataChannel('a', { @@ -173,11 +181,15 @@ function startWebRTC() { if (pc.iceConnectionState === "connected") { gameReady = true iceSuccess = true - //conn.send(JSON.stringify({"id": "start", "data": ""})); + conn.send(JSON.stringify({"id": "icecandidate", "data": e.candidate})); } else if (pc.iceConnectionState === "failed") { gameReady = false iceSuccess = false + log(`failed. Retry...`) + pc.createOffer({iceRestart: true }).then(d => { + pc.setLocalDescription(d).catch(log); + }).catch(log); } else if (pc.iceConnectionState === "disconnected") { stopInputTimer(); diff --git a/webrtc/webrtc.go b/webrtc/webrtc.go index 3d1befe9..750c150c 100644 --- a/webrtc/webrtc.go +++ b/webrtc/webrtc.go @@ -16,7 +16,7 @@ import ( "github.com/pion/webrtc/v2/pkg/media" ) -var webrtcconfig = webrtc.Configuration{ICEServers: []webrtc.ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}}} +var webrtcconfig = webrtc.Configuration{ICEServers: []webrtc.ICEServer{{URLs: []string{"stun:159.65.141.209:3478"}}}} // Allows compressing offer/answer to bypass terminal input limits. const compress = false @@ -92,7 +92,7 @@ type WebRTC struct { } // StartClient start webrtc -func (w *WebRTC) StartClient(remoteSession string, iceCandidates [][]byte, width, height int) (string, error) { +func (w *WebRTC) StartClient(remoteSession string, iceCandidates []string, width, height int) (string, error) { defer func() { if err := recover(); err != nil { log.Println(err) @@ -146,7 +146,7 @@ func (w *WebRTC) StartClient(remoteSession string, iceCandidates [][]byte, width // input channel inputTrack, err := w.connection.CreateDataChannel("a", &webrtc.DataChannelInit{ Ordered: &dfalse, - Negotiated: &dfalse, + Negotiated: &dtrue, ID: &d0, }) @@ -197,14 +197,15 @@ func (w *WebRTC) StartClient(remoteSession string, iceCandidates [][]byte, width // Parse candidates list // This logic is wrong - //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) - //} + for _, bcandidate := range iceCandidates { + iceCandidate := webrtc.ICECandidateInit{} + if err := json.Unmarshal([]byte(bcandidate), &iceCandidate); err != nil { + log.Println("Cannot parse ", bcandidate) + continue + } + log.Println("Add iceCandidate: ", iceCandidate) + w.connection.AddICECandidate(iceCandidate) + } answer, err := w.connection.CreateAnswer(nil) if err != nil { diff --git a/worker/overlord.go b/worker/overlord.go index 33efaa4b..1e44ccda 100644 --- a/worker/overlord.go +++ b/worker/overlord.go @@ -32,7 +32,7 @@ func NewOverlordClient(oc *websocket.Conn) *OverlordClient { // RouteOverlord are all routes server received from overlord func (h *Handler) RouteOverlord() { - iceCandidates := [][]byte{} + iceCandidates := map[string][]string{} oClient := h.oClient // Received from overlord the serverID @@ -52,7 +52,7 @@ func (h *Handler) RouteOverlord() { 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, config.Width, config.Height) + localSession, err := peerconnection.StartClient(resp.Data, iceCandidates[resp.SessionID], config.Width, config.Height) //h.peerconnections[resp.SessionID] = peerconnection // Create new sessions when we have new peerconnection initialized @@ -160,6 +160,16 @@ func (h *Handler) RouteOverlord() { return req }) + oClient.Receive( + "icecandidate", + func(resp cws.WSPacket) (req cws.WSPacket) { + log.Println("Received a icecandidate from overlord: ", resp.Data) + iceCandidates[resp.SessionID] = append(iceCandidates[resp.SessionID], resp.Data) + + return cws.EmptyPacket + }, + ) + oClient.Receive( "terminateSession", func(resp cws.WSPacket) (req cws.WSPacket) {