diff --git a/relay.go b/relay.go new file mode 100644 index 0000000..f4b02f8 --- /dev/null +++ b/relay.go @@ -0,0 +1,66 @@ +/// RTC Relay +/// +/// @author Pierre Hubert + +package main + +import ( + "encoding/json" + "fmt" + "log" + + "github.com/pion/webrtc/v2" +) + +const ( + // SDP This is a SDP signal + SDP = iota + + // CANDIDATE This is a candidate + CANDIDATE = iota +) + +type receivedSignal struct { + sigType uint + offer webrtc.SessionDescription + candidate webrtc.ICECandidateInit +} + +/// We keep for each connection its channel +var connections = make(map[string]chan receivedSignal) + +/// Process incoming messages +func onSignal(callHash, peerID string, data map[string]interface{}) { + + // Decode received signal + newSignal := receivedSignal{} + + if data["type"] == "SDP" { + newSignal.sigType = SDP + newSignal.offer.Type = webrtc.SDPTypeOffer + newSignal.offer.SDP = data["data"].(map[string]interface{})["sdp"].(string) + + } else if data["type"] == "CANDIDATE" { + newSignal.sigType = CANDIDATE + + // I have to re-encode data to initialize ICECandidate + var enc []byte + enc, err := json.Marshal(data["data"]) + if err != nil { + log.Printf("Could not re-encode candidate ! %s", err) + return + } + + err = json.Unmarshal(enc, &newSignal.candidate) + if err != nil { + log.Printf("Discarding invalid candidate: %s", err) + return + } + + } else { + log.Fatalf("Invalid signal type: %s !", data["type"]) + } + + println("Signal successfully processed!") + fmt.Println("sig:", newSignal) +} diff --git a/ws.go b/ws.go index a521b4f..8bfa278 100644 --- a/ws.go +++ b/ws.go @@ -69,6 +69,14 @@ func openWs(conf *Config) { setCallConfig(msg.Data.(map[string]interface{})) break + // Remote signal + case "signal": + onSignal(msg.CallHash, msg.PeerID, msg.Data.(map[string]interface{})) + break + + default: + println("Received unkown message type!") + break } }