Avoid stream allocator event data cast to interface and back. (#4471)
This commit is contained in:
@@ -135,13 +135,45 @@ func (s streamAllocatorSignal) String() string {
|
|||||||
|
|
||||||
type Event struct {
|
type Event struct {
|
||||||
*StreamAllocator
|
*StreamAllocator
|
||||||
Signal streamAllocatorSignal
|
signal streamAllocatorSignal
|
||||||
TrackID livekit.TrackID
|
|
||||||
Data any
|
// only one of these fields will be set based on event type
|
||||||
|
trackID livekit.TrackID
|
||||||
|
allowPause bool
|
||||||
|
channelCapacity int64
|
||||||
|
twccFeedback *rtcp.TransportLayerCC
|
||||||
|
congestionStateChangeData congestionStateChangeData
|
||||||
|
probeBytesToSend int
|
||||||
|
probeClusterId ccutils.ProbeClusterId
|
||||||
|
probeClusterInfo ccutils.ProbeClusterInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e Event) String() string {
|
func (e Event) String() string {
|
||||||
return fmt.Sprintf("StreamAllocator:Event{signal: %s, trackID: %s, data: %+v}", e.Signal, e.TrackID, e.Data)
|
value := ""
|
||||||
|
switch e.signal {
|
||||||
|
case streamAllocatorSignalAllocateTrack, streamAllocatorSignalResume:
|
||||||
|
value = fmt.Sprintf("trackID: %s", e.trackID)
|
||||||
|
case streamAllocatorSignalEstimate, streamAllocatorSignalSetChannelCapacity:
|
||||||
|
value = fmt.Sprintf("channelCapacity: %d", e.channelCapacity)
|
||||||
|
case streamAllocatorSignalFeedback:
|
||||||
|
value = fmt.Sprintf("twccFeedback: %+v", e.twccFeedback)
|
||||||
|
case streamAllocatorSignalProbeClusterSwitch:
|
||||||
|
value = fmt.Sprintf("probeClusterInfo: %+v", e.probeClusterInfo)
|
||||||
|
case streamAllocatorSignalSendProbe:
|
||||||
|
value = fmt.Sprintf("probeBytesToSend: %d", e.probeBytesToSend)
|
||||||
|
case streamAllocatorSignalPacerProbeObserverClusterComplete:
|
||||||
|
value = fmt.Sprintf("probeClusterId: %v", e.probeClusterId)
|
||||||
|
case streamAllocatorSignalSetAllowPause:
|
||||||
|
value = fmt.Sprintf("allowPause: %v", e.allowPause)
|
||||||
|
case streamAllocatorSignalCongestionStateChange:
|
||||||
|
value = fmt.Sprintf("congestStatecChangeData: %+v", e.congestionStateChangeData)
|
||||||
|
}
|
||||||
|
|
||||||
|
if value == "" {
|
||||||
|
return fmt.Sprintf("StreamAllocator:Event{signal: %s}", e.signal)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("StreamAllocator:Event{signal: %s, %s}", e.signal, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -321,7 +353,7 @@ func (s *StreamAllocator) RemoveTrack(downTrack *sfu.DownTrack) {
|
|||||||
|
|
||||||
// STREAM-ALLOCATOR-TODO: use any saved bandwidth to re-distribute
|
// STREAM-ALLOCATOR-TODO: use any saved bandwidth to re-distribute
|
||||||
s.postEvent(Event{
|
s.postEvent(Event{
|
||||||
Signal: streamAllocatorSignalAdjustState,
|
signal: streamAllocatorSignalAdjustState,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -333,7 +365,7 @@ func (s *StreamAllocator) SetTrackPriority(downTrack *sfu.DownTrack, priority ui
|
|||||||
// do a full allocation on a track priority change to keep it simple
|
// do a full allocation on a track priority change to keep it simple
|
||||||
s.isAllocateAllPending = true
|
s.isAllocateAllPending = true
|
||||||
s.postEvent(Event{
|
s.postEvent(Event{
|
||||||
Signal: streamAllocatorSignalAllocateAllTracks,
|
signal: streamAllocatorSignalAllocateAllTracks,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -342,15 +374,15 @@ func (s *StreamAllocator) SetTrackPriority(downTrack *sfu.DownTrack, priority ui
|
|||||||
|
|
||||||
func (s *StreamAllocator) SetAllowPause(allowPause bool) {
|
func (s *StreamAllocator) SetAllowPause(allowPause bool) {
|
||||||
s.postEvent(Event{
|
s.postEvent(Event{
|
||||||
Signal: streamAllocatorSignalSetAllowPause,
|
signal: streamAllocatorSignalSetAllowPause,
|
||||||
Data: allowPause,
|
allowPause: allowPause,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StreamAllocator) SetChannelCapacity(channelCapacity int64) {
|
func (s *StreamAllocator) SetChannelCapacity(channelCapacity int64) {
|
||||||
s.postEvent(Event{
|
s.postEvent(Event{
|
||||||
Signal: streamAllocatorSignalSetChannelCapacity,
|
signal: streamAllocatorSignalSetChannelCapacity,
|
||||||
Data: channelCapacity,
|
channelCapacity: channelCapacity,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -432,24 +464,24 @@ func (s *StreamAllocator) OnREMB(downTrack *sfu.DownTrack, remb *rtcp.ReceiverEs
|
|||||||
s.videoTracksMu.Unlock()
|
s.videoTracksMu.Unlock()
|
||||||
|
|
||||||
s.postEvent(Event{
|
s.postEvent(Event{
|
||||||
Signal: streamAllocatorSignalEstimate,
|
signal: streamAllocatorSignalEstimate,
|
||||||
Data: int64(remb.Bitrate),
|
channelCapacity: int64(remb.Bitrate),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// called when a new transport-cc feedback is received
|
// called when a new transport-cc feedback is received
|
||||||
func (s *StreamAllocator) OnTransportCCFeedback(downTrack *sfu.DownTrack, fb *rtcp.TransportLayerCC) {
|
func (s *StreamAllocator) OnTransportCCFeedback(downTrack *sfu.DownTrack, fb *rtcp.TransportLayerCC) {
|
||||||
s.postEvent(Event{
|
s.postEvent(Event{
|
||||||
Signal: streamAllocatorSignalFeedback,
|
signal: streamAllocatorSignalFeedback,
|
||||||
Data: fb,
|
twccFeedback: fb,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// called when target bitrate changes (send side bandwidth estimation)
|
// called when target bitrate changes (send side bandwidth estimation)
|
||||||
func (s *StreamAllocator) onTargetBitrateChange(bitrate int) {
|
func (s *StreamAllocator) onTargetBitrateChange(bitrate int) {
|
||||||
s.postEvent(Event{
|
s.postEvent(Event{
|
||||||
Signal: streamAllocatorSignalEstimate,
|
signal: streamAllocatorSignalEstimate,
|
||||||
Data: int64(bitrate),
|
channelCapacity: int64(bitrate),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -463,8 +495,8 @@ type congestionStateChangeData struct {
|
|||||||
// BWEListener implementation
|
// BWEListener implementation
|
||||||
func (s *StreamAllocator) OnCongestionStateChange(fromState bwe.CongestionState, toState bwe.CongestionState, estimatedAvailableChannelCapacity int64) {
|
func (s *StreamAllocator) OnCongestionStateChange(fromState bwe.CongestionState, toState bwe.CongestionState, estimatedAvailableChannelCapacity int64) {
|
||||||
s.postEvent(Event{
|
s.postEvent(Event{
|
||||||
Signal: streamAllocatorSignalCongestionStateChange,
|
signal: streamAllocatorSignalCongestionStateChange,
|
||||||
Data: congestionStateChangeData{fromState, toState, estimatedAvailableChannelCapacity},
|
congestionStateChangeData: congestionStateChangeData{fromState, toState, estimatedAvailableChannelCapacity},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -506,8 +538,8 @@ func (s *StreamAllocator) OnSubscribedLayerChanged(downTrack *sfu.DownTrack, lay
|
|||||||
|
|
||||||
if shouldPost {
|
if shouldPost {
|
||||||
s.postEvent(Event{
|
s.postEvent(Event{
|
||||||
Signal: streamAllocatorSignalAllocateTrack,
|
signal: streamAllocatorSignalAllocateTrack,
|
||||||
TrackID: livekit.TrackID(downTrack.ID()),
|
trackID: livekit.TrackID(downTrack.ID()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -515,32 +547,32 @@ func (s *StreamAllocator) OnSubscribedLayerChanged(downTrack *sfu.DownTrack, lay
|
|||||||
// called when forwarder resumes a track
|
// called when forwarder resumes a track
|
||||||
func (s *StreamAllocator) OnResume(downTrack *sfu.DownTrack) {
|
func (s *StreamAllocator) OnResume(downTrack *sfu.DownTrack) {
|
||||||
s.postEvent(Event{
|
s.postEvent(Event{
|
||||||
Signal: streamAllocatorSignalResume,
|
signal: streamAllocatorSignalResume,
|
||||||
TrackID: livekit.TrackID(downTrack.ID()),
|
trackID: livekit.TrackID(downTrack.ID()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// called when probe cluster changes
|
// called when probe cluster changes
|
||||||
func (s *StreamAllocator) OnProbeClusterSwitch(pci ccutils.ProbeClusterInfo) {
|
func (s *StreamAllocator) OnProbeClusterSwitch(pci ccutils.ProbeClusterInfo) {
|
||||||
s.postEvent(Event{
|
s.postEvent(Event{
|
||||||
Signal: streamAllocatorSignalProbeClusterSwitch,
|
signal: streamAllocatorSignalProbeClusterSwitch,
|
||||||
Data: pci,
|
probeClusterInfo: pci,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// called when prober wants to send packet(s)
|
// called when prober wants to send packet(s)
|
||||||
func (s *StreamAllocator) OnSendProbe(bytesToSend int) {
|
func (s *StreamAllocator) OnSendProbe(bytesToSend int) {
|
||||||
s.postEvent(Event{
|
s.postEvent(Event{
|
||||||
Signal: streamAllocatorSignalSendProbe,
|
signal: streamAllocatorSignalSendProbe,
|
||||||
Data: bytesToSend,
|
probeBytesToSend: bytesToSend,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// called when pacer probe observer observes a cluster completion
|
// called when pacer probe observer observes a cluster completion
|
||||||
func (s *StreamAllocator) OnPacerProbeObserverClusterComplete(probeClusterId ccutils.ProbeClusterId) {
|
func (s *StreamAllocator) OnPacerProbeObserverClusterComplete(probeClusterId ccutils.ProbeClusterId) {
|
||||||
s.postEvent(Event{
|
s.postEvent(Event{
|
||||||
Signal: streamAllocatorSignalPacerProbeObserverClusterComplete,
|
signal: streamAllocatorSignalPacerProbeObserverClusterComplete,
|
||||||
Data: probeClusterId,
|
probeClusterId: probeClusterId,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -586,8 +618,8 @@ func (s *StreamAllocator) maybePostEventAllocateTrack(downTrack *sfu.DownTrack)
|
|||||||
|
|
||||||
if shouldPost {
|
if shouldPost {
|
||||||
s.postEvent(Event{
|
s.postEvent(Event{
|
||||||
Signal: streamAllocatorSignalAllocateTrack,
|
signal: streamAllocatorSignalAllocateTrack,
|
||||||
TrackID: livekit.TrackID(downTrack.ID()),
|
trackID: livekit.TrackID(downTrack.ID()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -603,7 +635,7 @@ func (s *StreamAllocator) ping(pingGeneration uint32, interval time.Duration) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
s.postEvent(Event{
|
s.postEvent(Event{
|
||||||
Signal: streamAllocatorSignalPeriodicPing,
|
signal: streamAllocatorSignalPeriodicPing,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -611,7 +643,7 @@ func (s *StreamAllocator) ping(pingGeneration uint32, interval time.Duration) {
|
|||||||
func (s *StreamAllocator) postEvent(event Event) {
|
func (s *StreamAllocator) postEvent(event Event) {
|
||||||
event.StreamAllocator = s
|
event.StreamAllocator = s
|
||||||
s.eventsQueue.Enqueue(func(event Event) {
|
s.eventsQueue.Enqueue(func(event Event) {
|
||||||
switch event.Signal {
|
switch event.signal {
|
||||||
case streamAllocatorSignalAllocateTrack:
|
case streamAllocatorSignalAllocateTrack:
|
||||||
event.handleSignalAllocateTrack(event)
|
event.handleSignalAllocateTrack(event)
|
||||||
case streamAllocatorSignalAllocateAllTracks:
|
case streamAllocatorSignalAllocateAllTracks:
|
||||||
@@ -644,7 +676,7 @@ func (s *StreamAllocator) postEvent(event Event) {
|
|||||||
|
|
||||||
func (s *StreamAllocator) handleSignalAllocateTrack(event Event) {
|
func (s *StreamAllocator) handleSignalAllocateTrack(event Event) {
|
||||||
s.videoTracksMu.Lock()
|
s.videoTracksMu.Lock()
|
||||||
track := s.videoTracks[event.TrackID]
|
track := s.videoTracks[event.trackID]
|
||||||
if track != nil {
|
if track != nil {
|
||||||
track.SetDirty(false)
|
track.SetDirty(false)
|
||||||
}
|
}
|
||||||
@@ -670,13 +702,11 @@ func (s *StreamAllocator) handleSignalAdjustState(Event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *StreamAllocator) handleSignalEstimate(event Event) {
|
func (s *StreamAllocator) handleSignalEstimate(event Event) {
|
||||||
receivedEstimate := event.Data.(int64)
|
|
||||||
|
|
||||||
// always update NACKs
|
// always update NACKs
|
||||||
packetDelta, repeatedNackDelta := s.getNackDelta()
|
packetDelta, repeatedNackDelta := s.getNackDelta()
|
||||||
|
|
||||||
s.params.BWE.HandleREMB(
|
s.params.BWE.HandleREMB(
|
||||||
receivedEstimate,
|
event.channelCapacity,
|
||||||
s.getExpectedBandwidthUsage(),
|
s.getExpectedBandwidthUsage(),
|
||||||
packetDelta,
|
packetDelta,
|
||||||
repeatedNackDelta,
|
repeatedNackDelta,
|
||||||
@@ -684,7 +714,7 @@ func (s *StreamAllocator) handleSignalEstimate(event Event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *StreamAllocator) handleSignalFeedback(event Event) {
|
func (s *StreamAllocator) handleSignalFeedback(event Event) {
|
||||||
fb := event.Data.(*rtcp.TransportLayerCC)
|
fb := event.twccFeedback
|
||||||
if s.sendSideBWEInterceptor != nil {
|
if s.sendSideBWEInterceptor != nil {
|
||||||
s.sendSideBWEInterceptor.WriteRTCP([]rtcp.Packet{fb}, nil)
|
s.sendSideBWEInterceptor.WriteRTCP([]rtcp.Packet{fb}, nil)
|
||||||
}
|
}
|
||||||
@@ -752,7 +782,7 @@ func (s *StreamAllocator) handleSignalPeriodicPing(Event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *StreamAllocator) handleSignalProbeClusterSwitch(event Event) {
|
func (s *StreamAllocator) handleSignalProbeClusterSwitch(event Event) {
|
||||||
pci := event.Data.(ccutils.ProbeClusterInfo)
|
pci := event.probeClusterInfo
|
||||||
s.activeProbeClusterId = pci.Id
|
s.activeProbeClusterId = pci.Id
|
||||||
s.activeProbeGoalReached = false
|
s.activeProbeGoalReached = false
|
||||||
s.activeProbeCongesting = false
|
s.activeProbeCongesting = false
|
||||||
@@ -767,7 +797,7 @@ func (s *StreamAllocator) handleSignalProbeClusterSwitch(event Event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *StreamAllocator) handleSignalSendProbe(event Event) {
|
func (s *StreamAllocator) handleSignalSendProbe(event Event) {
|
||||||
bytesToSend := event.Data.(int)
|
bytesToSend := event.probeBytesToSend
|
||||||
if bytesToSend <= 0 {
|
if bytesToSend <= 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -786,8 +816,7 @@ func (s *StreamAllocator) handleSignalSendProbe(event Event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *StreamAllocator) handleSignalPacerProbeObserverClusterComplete(event Event) {
|
func (s *StreamAllocator) handleSignalPacerProbeObserverClusterComplete(event Event) {
|
||||||
probeClusterId, _ := event.Data.(ccutils.ProbeClusterId)
|
pci := s.params.Pacer.EndProbeCluster(event.probeClusterId)
|
||||||
pci := s.params.Pacer.EndProbeCluster(probeClusterId)
|
|
||||||
|
|
||||||
for _, t := range s.getVideoTracks() {
|
for _, t := range s.getVideoTracks() {
|
||||||
t.DownTrack().SwapProbeClusterId(pci.Id, ccutils.ProbeClusterIdInvalid)
|
t.DownTrack().SwapProbeClusterId(pci.Id, ccutils.ProbeClusterIdInvalid)
|
||||||
@@ -799,7 +828,7 @@ func (s *StreamAllocator) handleSignalPacerProbeObserverClusterComplete(event Ev
|
|||||||
|
|
||||||
func (s *StreamAllocator) handleSignalResume(event Event) {
|
func (s *StreamAllocator) handleSignalResume(event Event) {
|
||||||
s.videoTracksMu.Lock()
|
s.videoTracksMu.Lock()
|
||||||
track := s.videoTracks[event.TrackID]
|
track := s.videoTracks[event.trackID]
|
||||||
updated := track != nil && track.SetStreamState(StreamStateActive)
|
updated := track != nil && track.SetStreamState(StreamStateActive)
|
||||||
s.videoTracksMu.Unlock()
|
s.videoTracksMu.Unlock()
|
||||||
|
|
||||||
@@ -811,11 +840,11 @@ func (s *StreamAllocator) handleSignalResume(event Event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *StreamAllocator) handleSignalSetAllowPause(event Event) {
|
func (s *StreamAllocator) handleSignalSetAllowPause(event Event) {
|
||||||
s.allowPause = event.Data.(bool)
|
s.allowPause = event.allowPause
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StreamAllocator) handleSignalSetChannelCapacity(event Event) {
|
func (s *StreamAllocator) handleSignalSetChannelCapacity(event Event) {
|
||||||
s.overriddenChannelCapacity = event.Data.(int64)
|
s.overriddenChannelCapacity = event.channelCapacity
|
||||||
if s.overriddenChannelCapacity > 0 {
|
if s.overriddenChannelCapacity > 0 {
|
||||||
s.params.Logger.Infow("allocating on override channel capacity", "override", s.overriddenChannelCapacity)
|
s.params.Logger.Infow("allocating on override channel capacity", "override", s.overriddenChannelCapacity)
|
||||||
s.allocateAllTracks()
|
s.allocateAllTracks()
|
||||||
@@ -825,7 +854,7 @@ func (s *StreamAllocator) handleSignalSetChannelCapacity(event Event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *StreamAllocator) handleSignalCongestionStateChange(event Event) {
|
func (s *StreamAllocator) handleSignalCongestionStateChange(event Event) {
|
||||||
cscd := event.Data.(congestionStateChangeData)
|
cscd := event.congestionStateChangeData
|
||||||
if cscd.toState != bwe.CongestionStateNone {
|
if cscd.toState != bwe.CongestionStateNone {
|
||||||
// end/abort any running probe if channel is not clear
|
// end/abort any running probe if channel is not clear
|
||||||
s.maybeStopProbe()
|
s.maybeStopProbe()
|
||||||
|
|||||||
Reference in New Issue
Block a user