StreamAllocator - tracking more things (#1652)

* WIP commit

* Add a probe cluster mode

* better variable naming

* fix units

* WIP commit

* WIP commit

* WIP commit

* new file

* WIP commit

* Maintain history of a few things

* correct signal

* fix typo

* WIP commmit

* gofmt

* rate not sum

* adjust edges of rate monitor

* fmt

* remove debug
This commit is contained in:
Raja Subramanian
2023-04-25 07:31:22 +05:30
committed by GitHub
parent b4ea4de5c0
commit 8ed193b231
9 changed files with 625 additions and 57 deletions
+114 -5
View File
@@ -66,7 +66,7 @@ var (
Name: "non-probe",
EstimateRequiredSamples: 8,
EstimateDownwardTrendThreshold: -0.5,
EstimateCollapseThreshold: 500 * time.Millisecond,
EstimateCollapseThreshold: 250 * time.Millisecond,
NackWindowMinDuration: 1 * time.Second,
NackWindowMaxDuration: 2 * time.Second,
NackRatioThreshold: 0.08,
@@ -108,6 +108,8 @@ const (
streamAllocatorSignalResume
streamAllocatorSignalSetAllowPause
streamAllocatorSignalSetChannelCapacity
streamAllocatorSignalNACK
streamAllocatorSignalRTCPReceiverReport
)
func (s streamAllocatorSignal) String() string {
@@ -132,6 +134,10 @@ func (s streamAllocatorSignal) String() string {
return "SET_ALLOW_PAUSE"
case streamAllocatorSignalSetChannelCapacity:
return "SET_CHANNEL_CAPACITY"
case streamAllocatorSignalNACK:
return "NACK"
case streamAllocatorSignalRTCPReceiverReport:
return "RTCP_RECEIVER_REPORT"
default:
return fmt.Sprintf("%d", int(s))
}
@@ -180,6 +186,7 @@ type StreamAllocator struct {
prober *Prober
channelObserver *ChannelObserver
rateMonitor *RateMonitor
videoTracksMu sync.RWMutex
videoTracks map[livekit.TrackID]*Track
@@ -201,8 +208,9 @@ func NewStreamAllocator(params StreamAllocatorParams) *StreamAllocator {
prober: NewProber(ProberParams{
Logger: params.Logger,
}),
rateMonitor: NewRateMonitor(),
videoTracks: make(map[livekit.TrackID]*Track),
eventCh: make(chan Event, 200),
eventCh: make(chan Event, 1000),
}
s.resetState()
@@ -463,11 +471,30 @@ func (s *StreamAllocator) OnResume(downTrack *sfu.DownTrack) {
})
}
// called when a video DownTrack sends a packet
// called by a video DownTrack to report packet send
func (s *StreamAllocator) OnPacketsSent(downTrack *sfu.DownTrack, size int) {
s.prober.PacketsSent(size)
}
// called by a video DownTrack when it processes NACKs
func (s *StreamAllocator) OnNACK(downTrack *sfu.DownTrack, nackInfos []sfu.NackInfo) {
s.postEvent(Event{
Signal: streamAllocatorSignalNACK,
TrackID: livekit.TrackID(downTrack.ID()),
Data: nackInfos,
})
}
// called by a video DownTrack when it receives an RTCP Receiver Report
// STREAM-ALLOCATOR-TODO: this should probably be done for audio tracks also
func (s *StreamAllocator) OnRTCPReceiverReport(downTrack *sfu.DownTrack, rr rtcp.ReceptionReport) {
s.postEvent(Event{
Signal: streamAllocatorSignalRTCPReceiverReport,
TrackID: livekit.TrackID(downTrack.ID()),
Data: rr,
})
}
// called when prober wants to send packet(s)
func (s *StreamAllocator) OnSendProbe(bytesToSend int) {
s.postEvent(Event{
@@ -538,7 +565,7 @@ func (s *StreamAllocator) processEvents() {
}
func (s *StreamAllocator) ping() {
ticker := time.NewTicker(time.Second)
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
for {
@@ -575,6 +602,10 @@ func (s *StreamAllocator) handleEvent(event *Event) {
s.handleSignalSetAllowPause(event)
case streamAllocatorSignalSetChannelCapacity:
s.handleSignalSetChannelCapacity(event)
case streamAllocatorSignalNACK:
s.handleSignalNACK(event)
case streamAllocatorSignalRTCPReceiverReport:
s.handleSignalRTCPReceiverReport(event)
}
}
@@ -608,6 +639,7 @@ func (s *StreamAllocator) handleSignalAdjustState(event *Event) {
func (s *StreamAllocator) handleSignalEstimate(event *Event) {
receivedEstimate, _ := event.Data.(int64)
s.lastReceivedEstimate = receivedEstimate
s.monitorRate(receivedEstimate)
// while probing, maintain estimate separately to enable keeping current committed estimate if probe fails
if s.isInProbe() {
@@ -627,6 +659,8 @@ func (s *StreamAllocator) handleSignalPeriodicPing(event *Event) {
if s.state == streamAllocatorStateDeficient {
s.maybeProbe()
}
s.updateTracksHistory()
}
func (s *StreamAllocator) handleSignalSendProbe(event *Event) {
@@ -702,6 +736,30 @@ func (s *StreamAllocator) handleSignalSetChannelCapacity(event *Event) {
}
}
func (s *StreamAllocator) handleSignalNACK(event *Event) {
nackInfos := event.Data.([]sfu.NackInfo)
s.videoTracksMu.Lock()
track := s.videoTracks[event.TrackID]
s.videoTracksMu.Unlock()
if track != nil {
track.UpdateNack(nackInfos)
}
}
func (s *StreamAllocator) handleSignalRTCPReceiverReport(event *Event) {
rr := event.Data.(rtcp.ReceptionReport)
s.videoTracksMu.Lock()
track := s.videoTracks[event.TrackID]
s.videoTracksMu.Unlock()
if track != nil {
track.ProcessRTCPReceiverReport(rr)
}
}
func (s *StreamAllocator) setState(state streamAllocatorState) {
if s.state == state {
return
@@ -801,6 +859,13 @@ func (s *StreamAllocator) handleNewEstimateInNonProbe() {
"expectedUsage(bps)", expectedBandwidthUsage,
"channel", s.channelObserver.ToString(),
)
s.params.Logger.Infow(
"stream allocator: channel congestion detected, updating channel capacity: experimental",
"rateHistory", s.rateMonitor.GetHistory(),
"expectedQueuing", s.rateMonitor.GetQueuingGuess(),
"nackHistory", s.channelObserver.GetNackHistory(),
"trackHistory", s.getTracksHistory(),
)
s.committedChannelCapacity = estimateToCommit
// reset to get new set of samples for next trend
@@ -919,7 +984,7 @@ func (s *StreamAllocator) finalizeProbe() {
//
// Reset estimator at the end of a probe irrespective of probe result to get fresh readings.
// With a failed probe, the latest estimate would be lower than committed estimate.
// With a failed probe, the latest estimate could be lower than committed estimate.
// As bandwidth estimator (remote in REMB case, local in TWCC case) holds state,
// subsequent estimates could start from the lower point. That should not trigger a
// downward trend and get latched to committed estimate as that would trigger a re-allocation.
@@ -1179,6 +1244,7 @@ func (s *StreamAllocator) initProbe(probeGoalDeltaBps int64) {
s.channelObserver.SeedEstimate(s.lastReceivedEstimate)
s.probeClusterId = s.prober.AddCluster(
ProbeClusterModeUniform,
int(s.probeGoalBps),
int(expectedBandwidthUsage),
ProbeMinDuration,
@@ -1342,4 +1408,47 @@ func (s *StreamAllocator) getMaxDistanceSortedDeficient() MaxDistanceSorter {
return maxDistanceSorter
}
// STREAM-ALLOCATOR-EXPERIMENTAL-TODO
// Monitor sent rate vs estimate to figure out queuing on congestion.
// Idea here is to pause all managed tracks on congestion detection immediately till queue drains.
// That will allow channel to clear up without more traffic added and a re-allocation can start afresh.
// Some bits to work out
// - how good is queuing estimate?
// - should we pause unmanaged tracks also? But, they will restart at highest layer and request a key frame.
// - what should be the channel capacity to use when resume re-allocation happens?
func (s *StreamAllocator) monitorRate(estimate int64) {
managedBytesSent := uint32(0)
managedBytesRetransmitted := uint32(0)
unmanagedBytesSent := uint32(0)
unmanagedBytesRetransmitted := uint32(0)
for _, track := range s.getTracks() {
b, r := track.GetAndResetBytesSent()
if track.IsManaged() {
managedBytesSent += b
managedBytesRetransmitted += r
} else {
unmanagedBytesSent += b
unmanagedBytesRetransmitted += r
}
}
s.rateMonitor.Update(estimate, managedBytesSent, managedBytesRetransmitted, unmanagedBytesSent, unmanagedBytesRetransmitted)
}
func (s *StreamAllocator) updateTracksHistory() {
for _, track := range s.getTracks() {
track.UpdateHistory()
}
}
func (s *StreamAllocator) getTracksHistory() map[livekit.TrackID]string {
tracks := s.getTracks()
history := make(map[livekit.TrackID]string, len(tracks))
for _, track := range tracks {
history[track.ID()] = track.GetHistory()
}
return history
}
// ------------------------------------------------