Commit 06f2ecec authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Update the list of public groups when expiring.

Thanks to Jeroen van Veen for suggesting the feature.
parent f837c59d
...@@ -114,7 +114,8 @@ func main() { ...@@ -114,7 +114,8 @@ func main() {
ice.ICEFilename = filepath.Join(dataDir, "ice-servers.json") ice.ICEFilename = filepath.Join(dataDir, "ice-servers.json")
go group.ReadPublicGroups() // make sure the list of public groups is updated early
go group.Update()
// causes the built-in server to start if required // causes the built-in server to start if required
ice.Update() ice.Update()
...@@ -143,7 +144,7 @@ func main() { ...@@ -143,7 +144,7 @@ func main() {
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
go group.Expire() go group.Update()
case <-slowTicker.C: case <-slowTicker.C:
go relayTest() go relayTest()
case <-terminate: case <-terminate:
......
...@@ -129,6 +129,15 @@ func (g *Group) DisplayName() string { ...@@ -129,6 +129,15 @@ func (g *Group) DisplayName() string {
return g.description.DisplayName return g.description.DisplayName
} }
func (g *Group) EmptyTime() time.Duration {
g.mu.Lock()
defer g.mu.Unlock()
if len(g.clients) > 0 {
return 0
}
return time.Since(g.timestamp)
}
var groups struct { var groups struct {
mu sync.Mutex mu sync.Mutex
groups map[string]*Group groups map[string]*Group
...@@ -162,7 +171,7 @@ func CodecPayloadType(codec webrtc.RTPCodecCapability) (webrtc.PayloadType, erro ...@@ -162,7 +171,7 @@ func CodecPayloadType(codec webrtc.RTPCodecCapability) (webrtc.PayloadType, erro
return 96, nil return 96, nil
case "video/vp9": case "video/vp9":
profile := fmtpValue(codec.SDPFmtpLine, "profile-id") profile := fmtpValue(codec.SDPFmtpLine, "profile-id")
switch(profile) { switch profile {
case "0": case "0":
return 98, nil return 98, nil
case "2": case "2":
...@@ -177,7 +186,7 @@ func CodecPayloadType(codec webrtc.RTPCodecCapability) (webrtc.PayloadType, erro ...@@ -177,7 +186,7 @@ func CodecPayloadType(codec webrtc.RTPCodecCapability) (webrtc.PayloadType, erro
if len(profile) < 4 { if len(profile) < 4 {
return 0, errors.New("malforned H.264 profile") return 0, errors.New("malforned H.264 profile")
} }
switch(strings.ToLower(profile[:4])) { switch strings.ToLower(profile[:4]) {
case "4200": case "4200":
return 102, nil return 102, nil
case "42e0": case "42e0":
...@@ -499,34 +508,6 @@ func deleteUnlocked(g *Group) bool { ...@@ -499,34 +508,6 @@ func deleteUnlocked(g *Group) bool {
return true return true
} }
func Expire() {
names := GetNames()
now := time.Now()
for _, name := range names {
g := Get(name)
if g == nil {
continue
}
old := false
g.mu.Lock()
empty := len(g.clients) == 0
if empty && !g.description.Public {
age := now.Sub(g.timestamp)
old = age > maxHistoryAge(g.description)
}
// We cannot take groups.mu at this point without a deadlock.
g.mu.Unlock()
if empty && old {
// Delete will check if the group is still empty
Delete(name)
}
}
}
func AddClient(group string, c Client) (*Group, error) { func AddClient(group string, c Client) (*Group, error) {
g, err := Add(group, nil) g, err := Add(group, nil)
if err != nil { if err != nil {
...@@ -1029,7 +1010,30 @@ func GetPublic() []Public { ...@@ -1029,7 +1010,30 @@ func GetPublic() []Public {
return gs return gs
} }
func ReadPublicGroups() { // Update checks that all in-memory groups are up-to-date and updates the
// list of public groups. It also removes from memory any non-public
// groups that haven't been accessed in maxHistoryAge.
func Update() {
names := GetNames()
for _, name := range names {
g := Get(name)
if g == nil {
continue
}
deleted := false
historyAge := maxHistoryAge(g.description)
if !g.description.Public && g.EmptyTime() > historyAge {
// Delete checks if the group is still empty
deleted = Delete(name)
}
if !deleted && descriptionChanged(name, nil) {
Add(name, nil)
}
}
err := filepath.Walk( err := filepath.Walk(
Directory, Directory,
func(path string, fi os.FileInfo, err error) error { func(path string, fi os.FileInfo, err error) error {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment