Send stream state paused only when it is paused due to bandwidth limitation. (#1391)

* WIP commit

* Send stream state paused only when it is paused due to bandwidth limitation.

When stream is resumed after a stream is paused, an active update is
sent. Note that this means if there are intervening events like
mute/unmute between pause and resume, resume will be sent.
This commit is contained in:
Raja Subramanian
2023-02-08 20:03:32 +05:30
committed by GitHub
parent 95bc5b9849
commit 8a7478fd0e
5 changed files with 194 additions and 206 deletions
+69 -13
View File
@@ -96,6 +96,7 @@ const (
streamAllocatorSignalPeriodicPing
streamAllocatorSignalSendProbe
streamAllocatorSignalProbeClusterDone
streamAllocatorSignalTargetLayerFound
)
func (s streamAllocatorSignal) String() string {
@@ -114,6 +115,8 @@ func (s streamAllocatorSignal) String() string {
return "SEND_PROBE"
case streamAllocatorSignalProbeClusterDone:
return "PROBE_CLUSTER_DONE"
case streamAllocatorSignalTargetLayerFound:
return "TARGET_LAYER_FOUND"
default:
return fmt.Sprintf("%d", int(s))
}
@@ -240,6 +243,7 @@ func (s *StreamAllocator) AddTrack(downTrack *DownTrack, params AddTrackParams)
downTrack.OnSubscriptionChanged(s.onSubscriptionChanged)
downTrack.OnSubscribedLayersChanged(s.onSubscribedLayersChanged)
downTrack.OnPacketSent(s.onPacketSent)
downTrack.OnTargetLayerFound(s.onTargetLayerFound)
s.maybePostEventAllocateTrack(downTrack)
}
@@ -429,6 +433,14 @@ func (s *StreamAllocator) onProbeClusterDone(info ProbeClusterInfo) {
})
}
// called when forwarder finds a target layer
func (s *StreamAllocator) onTargetLayerFound(downTrack *DownTrack) {
s.postEvent(Event{
Signal: streamAllocatorSignalTargetLayerFound,
TrackID: livekit.TrackID(downTrack.ID()),
})
}
func (s *StreamAllocator) maybePostEventAllocateTrack(downTrack *DownTrack) {
shouldPost := false
s.videoTracksMu.Lock()
@@ -500,6 +512,8 @@ func (s *StreamAllocator) handleEvent(event *Event) {
s.handleSignalSendProbe(event)
case streamAllocatorSignalProbeClusterDone:
s.handleSignalProbeClusterDone(event)
case streamAllocatorSignalTargetLayerFound:
s.handleSignalTargetLayerFound(event)
}
}
@@ -599,6 +613,20 @@ func (s *StreamAllocator) handleSignalProbeClusterDone(event *Event) {
s.probeEndTime = s.lastProbeStartTime.Add(queueWait)
}
func (s *StreamAllocator) handleSignalTargetLayerFound(event *Event) {
s.videoTracksMu.Lock()
track := s.videoTracks[event.TrackID]
s.videoTracksMu.Unlock()
if track != nil {
update := NewStreamStateUpdate()
if track.SetPaused(false) {
update.HandleStreamingChange(false, track)
}
s.maybeSendUpdate(update)
}
}
func (s *StreamAllocator) setState(state streamAllocatorState) {
if s.state == state {
return
@@ -719,7 +747,9 @@ func (s *StreamAllocator) allocateTrack(track *Track) {
if !s.params.Config.Enabled || s.state == streamAllocatorStateStable || !track.IsManaged() {
update := NewStreamStateUpdate()
allocation := track.AllocateOptimal(FlagAllowOvershootWhileOptimal)
update.HandleStreamingChange(allocation.change, track)
if allocation.pauseReason == VideoPauseReasonBandwidth && track.SetPaused(true) {
update.HandleStreamingChange(true, track)
}
s.maybeSendUpdate(update)
return
}
@@ -744,7 +774,9 @@ func (s *StreamAllocator) allocateTrack(track *Track) {
allocation := track.ProvisionalAllocateCommit()
update := NewStreamStateUpdate()
update.HandleStreamingChange(allocation.change, track)
if allocation.pauseReason == VideoPauseReasonBandwidth && track.SetPaused(true) {
update.HandleStreamingChange(true, track)
}
s.maybeSendUpdate(update)
s.adjustState()
@@ -785,7 +817,9 @@ func (s *StreamAllocator) allocateTrack(track *Track) {
// commit the tracks that contributed
for _, t := range contributingTracks {
allocation := t.ProvisionalAllocateCommit()
update.HandleStreamingChange(allocation.change, t)
if allocation.pauseReason == VideoPauseReasonBandwidth && track.SetPaused(true) {
update.HandleStreamingChange(true, t)
}
}
// LK-TODO if got too much extra, can potentially give it to some deficient track
@@ -794,7 +828,9 @@ func (s *StreamAllocator) allocateTrack(track *Track) {
// commit the track that needs change if enough could be acquired or pause not allowed
if !s.params.Config.AllowPause || bandwidthAcquired >= transition.bandwidthDelta {
allocation := track.ProvisionalAllocateCommit()
update.HandleStreamingChange(allocation.change, track)
if allocation.pauseReason == VideoPauseReasonBandwidth && track.SetPaused(true) {
update.HandleStreamingChange(true, track)
}
}
s.maybeSendUpdate(update)
@@ -864,7 +900,9 @@ func (s *StreamAllocator) maybeBoostDeficientTracks() {
continue
}
update.HandleStreamingChange(allocation.change, track)
if allocation.pauseReason == VideoPauseReasonBandwidth && track.SetPaused(true) {
update.HandleStreamingChange(true, track)
}
availableChannelCapacity -= allocation.bandwidthDelta
if availableChannelCapacity <= 0 {
@@ -918,7 +956,9 @@ func (s *StreamAllocator) allocateAllTracks() {
}
allocation := track.AllocateOptimal(FlagAllowOvershootExemptTrackWhileDeficient)
update.HandleStreamingChange(allocation.change, track)
if allocation.pauseReason == VideoPauseReasonBandwidth && track.SetPaused(true) {
update.HandleStreamingChange(true, track)
}
// LK-TODO: optimistic allocation before bitrate is available will return 0. How to account for that?
availableChannelCapacity -= allocation.bandwidthRequested
@@ -935,7 +975,9 @@ func (s *StreamAllocator) allocateAllTracks() {
}
allocation := track.Pause()
update.HandleStreamingChange(allocation.change, track)
if allocation.pauseReason == VideoPauseReasonBandwidth && track.SetPaused(true) {
update.HandleStreamingChange(true, track)
}
}
} else {
sorted := s.getSorted()
@@ -962,7 +1004,9 @@ func (s *StreamAllocator) allocateAllTracks() {
for _, track := range sorted {
allocation := track.ProvisionalAllocateCommit()
update.HandleStreamingChange(allocation.change, track)
if allocation.pauseReason == VideoPauseReasonBandwidth && track.SetPaused(true) {
update.HandleStreamingChange(true, track)
}
}
}
@@ -1113,7 +1157,9 @@ func (s *StreamAllocator) maybeProbeWithMedia() {
}
update := NewStreamStateUpdate()
update.HandleStreamingChange(allocation.change, track)
if allocation.pauseReason == VideoPauseReasonBandwidth && track.SetPaused(true) {
update.HandleStreamingChange(true, track)
}
s.maybeSendUpdate(update)
s.lastProbeStartTime = time.Now()
@@ -1235,15 +1281,14 @@ func NewStreamStateUpdate() *StreamStateUpdate {
return &StreamStateUpdate{}
}
func (s *StreamStateUpdate) HandleStreamingChange(change VideoStreamingChange, track *Track) {
switch change {
case VideoStreamingChangePausing:
func (s *StreamStateUpdate) HandleStreamingChange(isPaused bool, track *Track) {
if isPaused {
s.StreamStates = append(s.StreamStates, &StreamStateInfo{
ParticipantID: track.PublisherID(),
TrackID: track.ID(),
State: StreamStatePaused,
})
case VideoStreamingChangeResuming:
} else {
s.StreamStates = append(s.StreamStates, &StreamStateInfo{
ParticipantID: track.PublisherID(),
TrackID: track.ID(),
@@ -1272,6 +1317,8 @@ type Track struct {
totalRepeatedNacks uint32
isDirty bool
isPaused bool
}
func newTrack(
@@ -1303,6 +1350,15 @@ func (t *Track) SetDirty(isDirty bool) bool {
return true
}
func (t *Track) SetPaused(isPaused bool) bool {
if t.isPaused == isPaused {
return false
}
t.isPaused = isPaused
return true
}
func (t *Track) SetPriority(priority uint8) bool {
if priority == 0 {
switch t.source {