Files
livekit/pkg/rtc/plithrottle.go
cnderrauber 1e1aaeb86b Separate from ion-sfu (#171)
* Separate from ion-sfu

changes:
1. extract pkg/buffer, twcc, sfu, relay, stats, logger

2. to solve cycle import, move ion-sfu/pkg/logger to pkg/sfu/logger

3. replace pion/ion-sfu => ./
reason: will change import pion/ion-sfu/pkg/* to livekit-server/pkg/*
after this pr merged. Just not change any code in this pr, because it
will confused with the separate code from ion-sfu in review.

* Move code from ion-sfu to pkg/sfu

* fix build error for resovle conflict

Co-authored-by: cnderrauber <zengjie9004@gmail.com>
2021-11-09 12:03:16 +08:00

65 lines
1.2 KiB
Go

package rtc
import (
"sync"
"time"
"github.com/livekit/livekit-server/pkg/config"
)
type pliThrottle struct {
config config.PLIThrottleConfig
mu sync.RWMutex
periods map[uint32]int64
lastSent map[uint32]int64
}
// github.com/livekit/livekit-server/pkg/sfu/simulcast.go
const (
fullResolution = "f"
halfResolution = "h"
quarterResolution = "q"
)
func newPLIThrottle(conf config.PLIThrottleConfig) *pliThrottle {
return &pliThrottle{
config: conf,
periods: make(map[uint32]int64),
lastSent: make(map[uint32]int64),
}
}
func (t *pliThrottle) addTrack(ssrc uint32, rid string) {
t.mu.Lock()
defer t.mu.Unlock()
var duration time.Duration
switch rid {
case fullResolution:
duration = t.config.HighQuality
case halfResolution:
duration = t.config.MidQuality
case quarterResolution:
duration = t.config.LowQuality
default:
duration = t.config.MidQuality
}
t.periods[ssrc] = duration.Nanoseconds()
}
func (t *pliThrottle) canSend(ssrc uint32) bool {
t.mu.Lock()
defer t.mu.Unlock()
if period, ok := t.periods[ssrc]; ok {
if n := time.Now().UnixNano(); n-t.lastSent[ssrc] > period {
t.lastSent[ssrc] = n
return true
} else {
return false
}
}
return true
}