mirror of
https://gitlab.com/comunic/ComunicRTCProxy
synced 2024-12-25 13:08:54 +00:00
Parse incoming signals
This commit is contained in:
parent
84169242b7
commit
60605e9ada
66
relay.go
Normal file
66
relay.go
Normal 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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user