diff --git a/index.html b/index.html index ed7e7757..9bc1a4bd 100644 --- a/index.html +++ b/index.html @@ -62,6 +62,7 @@ conn.onmessage = e => { startWebRTC(); break; case "sdp": + log("Got remote sdp"); pc.setRemoteDescription(new RTCSessionDescription(JSON.parse(atob(d["data"])))); break; } @@ -206,7 +207,8 @@ function endInput() { } pc.oniceconnectionstatechange = e => { - log(pc.iceConnectionState); + log(`iceConnectionState: ${pc.iceConnectionState}`); + if (pc.iceConnectionState === "connected") { conn.send(JSON.stringify({"id": "start", "data": ""})); startInput(); @@ -239,6 +241,7 @@ pc.onicecandidate = event => { // postSession(session) } else { console.log(JSON.stringify(event.candidate)); + conn.send(JSON.stringify({"id": "candidate", "data": JSON.stringify(event.candidate)})); } } @@ -250,6 +253,7 @@ function startWebRTC() { // send to ws session = btoa(JSON.stringify(pc.localDescription)); localSessionDescription = session; + log("Send SDP to remote peer"); conn.send(JSON.stringify({"id": "sdp", "data": session})); }); diff --git a/main.go b/main.go index 06f16f50..e2e29a66 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + pionRTC "github.com/pions/webrtc" "fmt" "image" "io/ioutil" @@ -112,6 +113,13 @@ func ws(w http.ResponseWriter, r *http.Request) { res.Data = localSession case "candidate": + hi := pionRTC.ICECandidateInit{} + err = json.Unmarshal([]byte(req.Data), &hi) + if err != nil { + fmt.Println("[!] Cannot parse candidate: ", err) + } else { + webRTC.AddCandidate(hi) + } res.ID = "candidate" case "start": diff --git a/webrtc/webrtc.go b/webrtc/webrtc.go index 82545ecd..3c53aa0d 100644 --- a/webrtc/webrtc.go +++ b/webrtc/webrtc.go @@ -208,6 +208,13 @@ func (w *WebRTC) StartClient(remoteSession string, width, height int) (string, e return localSession, nil } +func (w *WebRTC) AddCandidate(candidate webrtc.ICECandidateInit) { + err := w.connection.AddICECandidate(candidate) + if err != nil { + fmt.Println("Cannot add candidate: ", err) + } +} + // StopClient disconnect func (w *WebRTC) StopClient() { fmt.Println("===StopClient===")