1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-14 04:35:57 +01:00

Check ICE configuration periodically.

This commit is contained in:
Juliusz Chroboczek 2020-12-28 04:06:12 +01:00
parent 89a9e6c738
commit 307c834b09

View file

@ -4,7 +4,8 @@ import (
"encoding/json" "encoding/json"
"log" "log"
"os" "os"
"sync" "sync/atomic"
"time"
"github.com/pion/webrtc/v3" "github.com/pion/webrtc/v3"
) )
@ -24,35 +25,54 @@ type RTCConfiguration struct {
var ICEFilename string var ICEFilename string
var ICERelayOnly bool var ICERelayOnly bool
var iceConf RTCConfiguration type iceConf struct {
var iceOnce sync.Once conf RTCConfiguration
timestamp time.Time
}
func ICEConfiguration() *RTCConfiguration { var iceConfiguration atomic.Value
iceOnce.Do(func() {
var iceServers []ICEServer func updateICEConfiguration() *iceConf {
now := time.Now()
var conf RTCConfiguration
if ICEFilename != "" {
file, err := os.Open(ICEFilename) file, err := os.Open(ICEFilename)
if err != nil { if err != nil {
log.Printf("Open %v: %v", ICEFilename, err) log.Printf("Open %v: %v", ICEFilename, err)
return } else {
}
defer file.Close() defer file.Close()
d := json.NewDecoder(file) d := json.NewDecoder(file)
err = d.Decode(&iceServers) err = d.Decode(&conf.ICEServers)
if err != nil { if err != nil {
log.Printf("Get ICE configuration: %v", err) log.Printf("Get ICE configuration: %v", err)
return
} }
iceConf = RTCConfiguration{
ICEServers: iceServers,
} }
if ICERelayOnly {
iceConf.ICETransportPolicy = "relay"
} }
})
if ICERelayOnly {
conf.ICETransportPolicy = "relay"
}
iceConf := iceConf{
conf: conf,
timestamp: now,
}
iceConfiguration.Store(&iceConf)
return &iceConf return &iceConf
} }
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
}
func ToConfiguration(conf *RTCConfiguration) webrtc.Configuration { func ToConfiguration(conf *RTCConfiguration) webrtc.Configuration {
var iceServers []webrtc.ICEServer var iceServers []webrtc.ICEServer
for _, s := range conf.ICEServers { for _, s := range conf.ICEServers {