Parse incoming signals

This commit is contained in:
Pierre HUBERT 2020-04-11 10:30:28 +02:00
parent 84169242b7
commit 60605e9ada
2 changed files with 74 additions and 0 deletions

66
relay.go Normal file
View File

@ -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)
}

8
ws.go
View File

@ -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
}
}