2020-12-28 02:25:46 +01:00
|
|
|
package group
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
"os"
|
2020-12-28 04:06:12 +01:00
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
2020-12-28 02:25:46 +01:00
|
|
|
|
|
|
|
"github.com/pion/webrtc/v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ICEServer struct {
|
|
|
|
URLs []string `json:"urls"`
|
|
|
|
Username string `json:"username,omitempty"`
|
|
|
|
Credential interface{} `json:"credential,omitempty"`
|
|
|
|
CredentialType string `json:"credentialType,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type RTCConfiguration struct {
|
|
|
|
ICEServers []ICEServer `json:"iceServers,omitempty"`
|
|
|
|
ICETransportPolicy string `json:"iceTransportPolicy,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var ICEFilename string
|
2020-12-28 02:56:49 +01:00
|
|
|
var ICERelayOnly bool
|
2020-12-28 02:25:46 +01:00
|
|
|
|
2020-12-28 04:06:12 +01:00
|
|
|
type iceConf struct {
|
|
|
|
conf RTCConfiguration
|
|
|
|
timestamp time.Time
|
|
|
|
}
|
2020-12-28 02:25:46 +01:00
|
|
|
|
2020-12-28 04:06:12 +01:00
|
|
|
var iceConfiguration atomic.Value
|
|
|
|
|
|
|
|
func updateICEConfiguration() *iceConf {
|
|
|
|
now := time.Now()
|
|
|
|
var conf RTCConfiguration
|
|
|
|
|
|
|
|
if ICEFilename != "" {
|
2020-12-28 02:25:46 +01:00
|
|
|
file, err := os.Open(ICEFilename)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Open %v: %v", ICEFilename, err)
|
2020-12-28 04:06:12 +01:00
|
|
|
} else {
|
|
|
|
defer file.Close()
|
|
|
|
d := json.NewDecoder(file)
|
|
|
|
err = d.Decode(&conf.ICEServers)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Get ICE configuration: %v", err)
|
|
|
|
}
|
2020-12-28 02:25:46 +01:00
|
|
|
}
|
2020-12-28 04:06:12 +01:00
|
|
|
}
|
2020-12-28 02:25:46 +01:00
|
|
|
|
2020-12-28 04:06:12 +01:00
|
|
|
if ICERelayOnly {
|
|
|
|
conf.ICETransportPolicy = "relay"
|
|
|
|
}
|
|
|
|
|
|
|
|
iceConf := iceConf{
|
|
|
|
conf: conf,
|
|
|
|
timestamp: now,
|
|
|
|
}
|
|
|
|
iceConfiguration.Store(&iceConf)
|
2020-12-28 02:25:46 +01:00
|
|
|
return &iceConf
|
|
|
|
}
|
|
|
|
|
2020-12-28 04:06:12 +01:00
|
|
|
func ICEConfiguration() *RTCConfiguration {
|
|
|
|
conf, ok := iceConfiguration.Load().(*iceConf)
|
|
|
|
if !ok || time.Since(conf.timestamp) > 5*time.Minute {
|
|
|
|
conf = updateICEConfiguration()
|
|
|
|
} else if time.Since(conf.timestamp) > 2*time.Minute {
|
|
|
|
go updateICEConfiguration()
|
|
|
|
}
|
|
|
|
|
|
|
|
return &conf.conf
|
|
|
|
}
|
|
|
|
|
2020-12-28 02:25:46 +01:00
|
|
|
func ToConfiguration(conf *RTCConfiguration) webrtc.Configuration {
|
|
|
|
var iceServers []webrtc.ICEServer
|
|
|
|
for _, s := range conf.ICEServers {
|
|
|
|
tpe := webrtc.ICECredentialTypePassword
|
|
|
|
if s.CredentialType == "oauth" {
|
|
|
|
tpe = webrtc.ICECredentialTypeOauth
|
|
|
|
}
|
|
|
|
iceServers = append(iceServers,
|
|
|
|
webrtc.ICEServer{
|
|
|
|
URLs: s.URLs,
|
|
|
|
Username: s.Username,
|
|
|
|
Credential: s.Credential,
|
|
|
|
CredentialType: tpe,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
policy := webrtc.ICETransportPolicyAll
|
|
|
|
if conf.ICETransportPolicy == "relay" {
|
|
|
|
policy = webrtc.ICETransportPolicyRelay
|
|
|
|
}
|
|
|
|
|
|
|
|
return webrtc.Configuration{
|
|
|
|
ICEServers: iceServers,
|
|
|
|
ICETransportPolicy: policy,
|
|
|
|
}
|
|
|
|
}
|