ComunicRTCProxy/relay.go

67 lines
1.3 KiB
Go

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