Stream Allocator Try 3 (#257)

* Stream Allocator Try 3

Making an intermediate PR to do
- Special treatment for screen share tracks
- When allocating all tracks,
  o try to stream all tracks by starting with the lowest layer
  o multi-pass across tracks to get a more even distribution

Not yet done:
-------------
In deficient state,
o Allocate a specific track on a change
o Steal from other tracks

* Correct sense of managed track

* have to range to copy

* generate

* fix VideoLayers compare

* Use t.simulcasted
This commit is contained in:
Raja Subramanian
2021-12-14 12:48:09 +05:30
committed by GitHub
parent 1be3a3986d
commit e54ce4f674
25 changed files with 1168 additions and 856 deletions
+10
View File
@@ -96,6 +96,7 @@ func NewMediaTrack(track *webrtc.TrackRemote, params MediaTrackParams) *MediaTra
if params.TrackInfo != nil && t.Kind() == livekit.TrackType_VIDEO {
t.UpdateVideoLayers(params.TrackInfo.Layers)
// LK-TODO: maybe use this or simulcast flag in TrackInfo to set simulcasted here
}
return t
}
@@ -116,6 +117,14 @@ func (t *MediaTrack) Kind() livekit.TrackType {
return t.params.TrackInfo.Type
}
func (t *MediaTrack) Source() livekit.TrackSource {
return t.params.TrackInfo.Source
}
func (t *MediaTrack) IsSimulcast() bool {
return t.simulcasted.Get()
}
func (t *MediaTrack) Name() string {
return t.params.TrackInfo.Name
}
@@ -391,6 +400,7 @@ func (t *MediaTrack) AddReceiver(receiver *webrtc.RTPReceiver, track *webrtc.Tra
t.params.Telemetry.AddUpTrack(t.params.ParticipantID, buff)
atomic.AddUint32(&t.numUpTracks, 1)
// LK-TODO: can remove this completely when VideoLayers protocol becomes the default as it has info from client or if we decide to use TrackInfo.Simulcast
if atomic.LoadUint32(&t.numUpTracks) > 1 || track.RID() != "" {
// cannot only rely on numUpTracks since we fire metadata events immediately after the first layer
t.simulcasted.TrySet(true)
+4
View File
@@ -48,6 +48,10 @@ func (t *SubscribedTrack) DownTrack() *sfu.DownTrack {
return t.dt
}
func (t *SubscribedTrack) PublishedTrack() types.MediaTrack {
return t.publishedTrack
}
func (t *SubscribedTrack) SubscribeLossPercentage() uint32 {
return FixedPointToPercent(t.DownTrack().CurrentMaxLossFraction())
}
+3 -1
View File
@@ -268,7 +268,9 @@ func (t *PCTransport) AddTrack(subTrack types.SubscribedTrack) {
return
}
t.streamAllocator.AddTrack(subTrack.DownTrack())
source := subTrack.PublishedTrack().Source()
isManaged := (source != livekit.TrackSource_SCREEN_SHARE && source != livekit.TrackSource_SCREEN_SHARE_AUDIO) || subTrack.PublishedTrack().IsSimulcast()
t.streamAllocator.AddTrack(subTrack.DownTrack(), isManaged)
}
func (t *PCTransport) RemoveTrack(subTrack types.SubscribedTrack) {
+3
View File
@@ -106,6 +106,8 @@ type MediaTrack interface {
IsMuted() bool
SetMuted(muted bool)
UpdateVideoLayers(layers []*livekit.VideoLayer)
Source() livekit.TrackSource
IsSimulcast() bool
// subscribers
AddSubscriber(participant Participant) error
@@ -140,6 +142,7 @@ type SubscribedTrack interface {
ID() string
PublisherIdentity() string
DownTrack() *sfu.DownTrack
PublishedTrack() MediaTrack
IsMuted() bool
SetPublisherMuted(muted bool)
UpdateSubscriberSettings(settings *livekit.UpdateTrackSettings)
@@ -52,6 +52,16 @@ type FakeMediaTrack struct {
isMutedReturnsOnCall map[int]struct {
result1 bool
}
IsSimulcastStub func() bool
isSimulcastMutex sync.RWMutex
isSimulcastArgsForCall []struct {
}
isSimulcastReturns struct {
result1 bool
}
isSimulcastReturnsOnCall map[int]struct {
result1 bool
}
IsSubscriberStub func(string) bool
isSubscriberMutex sync.RWMutex
isSubscriberArgsForCall []struct {
@@ -97,6 +107,16 @@ type FakeMediaTrack struct {
setMutedArgsForCall []struct {
arg1 bool
}
SourceStub func() livekit.TrackSource
sourceMutex sync.RWMutex
sourceArgsForCall []struct {
}
sourceReturns struct {
result1 livekit.TrackSource
}
sourceReturnsOnCall map[int]struct {
result1 livekit.TrackSource
}
UpdateVideoLayersStub func([]*livekit.VideoLayer)
updateVideoLayersMutex sync.RWMutex
updateVideoLayersArgsForCall []struct {
@@ -335,6 +355,59 @@ func (fake *FakeMediaTrack) IsMutedReturnsOnCall(i int, result1 bool) {
}{result1}
}
func (fake *FakeMediaTrack) IsSimulcast() bool {
fake.isSimulcastMutex.Lock()
ret, specificReturn := fake.isSimulcastReturnsOnCall[len(fake.isSimulcastArgsForCall)]
fake.isSimulcastArgsForCall = append(fake.isSimulcastArgsForCall, struct {
}{})
stub := fake.IsSimulcastStub
fakeReturns := fake.isSimulcastReturns
fake.recordInvocation("IsSimulcast", []interface{}{})
fake.isSimulcastMutex.Unlock()
if stub != nil {
return stub()
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakeMediaTrack) IsSimulcastCallCount() int {
fake.isSimulcastMutex.RLock()
defer fake.isSimulcastMutex.RUnlock()
return len(fake.isSimulcastArgsForCall)
}
func (fake *FakeMediaTrack) IsSimulcastCalls(stub func() bool) {
fake.isSimulcastMutex.Lock()
defer fake.isSimulcastMutex.Unlock()
fake.IsSimulcastStub = stub
}
func (fake *FakeMediaTrack) IsSimulcastReturns(result1 bool) {
fake.isSimulcastMutex.Lock()
defer fake.isSimulcastMutex.Unlock()
fake.IsSimulcastStub = nil
fake.isSimulcastReturns = struct {
result1 bool
}{result1}
}
func (fake *FakeMediaTrack) IsSimulcastReturnsOnCall(i int, result1 bool) {
fake.isSimulcastMutex.Lock()
defer fake.isSimulcastMutex.Unlock()
fake.IsSimulcastStub = nil
if fake.isSimulcastReturnsOnCall == nil {
fake.isSimulcastReturnsOnCall = make(map[int]struct {
result1 bool
})
}
fake.isSimulcastReturnsOnCall[i] = struct {
result1 bool
}{result1}
}
func (fake *FakeMediaTrack) IsSubscriber(arg1 string) bool {
fake.isSubscriberMutex.Lock()
ret, specificReturn := fake.isSubscriberReturnsOnCall[len(fake.isSubscriberArgsForCall)]
@@ -590,6 +663,59 @@ func (fake *FakeMediaTrack) SetMutedArgsForCall(i int) bool {
return argsForCall.arg1
}
func (fake *FakeMediaTrack) Source() livekit.TrackSource {
fake.sourceMutex.Lock()
ret, specificReturn := fake.sourceReturnsOnCall[len(fake.sourceArgsForCall)]
fake.sourceArgsForCall = append(fake.sourceArgsForCall, struct {
}{})
stub := fake.SourceStub
fakeReturns := fake.sourceReturns
fake.recordInvocation("Source", []interface{}{})
fake.sourceMutex.Unlock()
if stub != nil {
return stub()
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakeMediaTrack) SourceCallCount() int {
fake.sourceMutex.RLock()
defer fake.sourceMutex.RUnlock()
return len(fake.sourceArgsForCall)
}
func (fake *FakeMediaTrack) SourceCalls(stub func() livekit.TrackSource) {
fake.sourceMutex.Lock()
defer fake.sourceMutex.Unlock()
fake.SourceStub = stub
}
func (fake *FakeMediaTrack) SourceReturns(result1 livekit.TrackSource) {
fake.sourceMutex.Lock()
defer fake.sourceMutex.Unlock()
fake.SourceStub = nil
fake.sourceReturns = struct {
result1 livekit.TrackSource
}{result1}
}
func (fake *FakeMediaTrack) SourceReturnsOnCall(i int, result1 livekit.TrackSource) {
fake.sourceMutex.Lock()
defer fake.sourceMutex.Unlock()
fake.SourceStub = nil
if fake.sourceReturnsOnCall == nil {
fake.sourceReturnsOnCall = make(map[int]struct {
result1 livekit.TrackSource
})
}
fake.sourceReturnsOnCall[i] = struct {
result1 livekit.TrackSource
}{result1}
}
func (fake *FakeMediaTrack) UpdateVideoLayers(arg1 []*livekit.VideoLayer) {
var arg1Copy []*livekit.VideoLayer
if arg1 != nil {
@@ -638,6 +764,8 @@ func (fake *FakeMediaTrack) Invocations() map[string][][]interface{} {
defer fake.iDMutex.RUnlock()
fake.isMutedMutex.RLock()
defer fake.isMutedMutex.RUnlock()
fake.isSimulcastMutex.RLock()
defer fake.isSimulcastMutex.RUnlock()
fake.isSubscriberMutex.RLock()
defer fake.isSubscriberMutex.RUnlock()
fake.kindMutex.RLock()
@@ -650,6 +778,8 @@ func (fake *FakeMediaTrack) Invocations() map[string][][]interface{} {
defer fake.removeSubscriberMutex.RUnlock()
fake.setMutedMutex.RLock()
defer fake.setMutedMutex.RUnlock()
fake.sourceMutex.RLock()
defer fake.sourceMutex.RUnlock()
fake.updateVideoLayersMutex.RLock()
defer fake.updateVideoLayersMutex.RUnlock()
copiedInvocations := map[string][][]interface{}{}
@@ -58,6 +58,16 @@ type FakePublishedTrack struct {
isMutedReturnsOnCall map[int]struct {
result1 bool
}
IsSimulcastStub func() bool
isSimulcastMutex sync.RWMutex
isSimulcastArgsForCall []struct {
}
isSimulcastReturns struct {
result1 bool
}
isSimulcastReturnsOnCall map[int]struct {
result1 bool
}
IsSubscriberStub func(string) bool
isSubscriberMutex sync.RWMutex
isSubscriberArgsForCall []struct {
@@ -155,6 +165,16 @@ type FakePublishedTrack struct {
signalCidReturnsOnCall map[int]struct {
result1 string
}
SourceStub func() livekit.TrackSource
sourceMutex sync.RWMutex
sourceArgsForCall []struct {
}
sourceReturns struct {
result1 livekit.TrackSource
}
sourceReturnsOnCall map[int]struct {
result1 livekit.TrackSource
}
ToProtoStub func() *livekit.TrackInfo
toProtoMutex sync.RWMutex
toProtoArgsForCall []struct {
@@ -435,6 +455,59 @@ func (fake *FakePublishedTrack) IsMutedReturnsOnCall(i int, result1 bool) {
}{result1}
}
func (fake *FakePublishedTrack) IsSimulcast() bool {
fake.isSimulcastMutex.Lock()
ret, specificReturn := fake.isSimulcastReturnsOnCall[len(fake.isSimulcastArgsForCall)]
fake.isSimulcastArgsForCall = append(fake.isSimulcastArgsForCall, struct {
}{})
stub := fake.IsSimulcastStub
fakeReturns := fake.isSimulcastReturns
fake.recordInvocation("IsSimulcast", []interface{}{})
fake.isSimulcastMutex.Unlock()
if stub != nil {
return stub()
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakePublishedTrack) IsSimulcastCallCount() int {
fake.isSimulcastMutex.RLock()
defer fake.isSimulcastMutex.RUnlock()
return len(fake.isSimulcastArgsForCall)
}
func (fake *FakePublishedTrack) IsSimulcastCalls(stub func() bool) {
fake.isSimulcastMutex.Lock()
defer fake.isSimulcastMutex.Unlock()
fake.IsSimulcastStub = stub
}
func (fake *FakePublishedTrack) IsSimulcastReturns(result1 bool) {
fake.isSimulcastMutex.Lock()
defer fake.isSimulcastMutex.Unlock()
fake.IsSimulcastStub = nil
fake.isSimulcastReturns = struct {
result1 bool
}{result1}
}
func (fake *FakePublishedTrack) IsSimulcastReturnsOnCall(i int, result1 bool) {
fake.isSimulcastMutex.Lock()
defer fake.isSimulcastMutex.Unlock()
fake.IsSimulcastStub = nil
if fake.isSimulcastReturnsOnCall == nil {
fake.isSimulcastReturnsOnCall = make(map[int]struct {
result1 bool
})
}
fake.isSimulcastReturnsOnCall[i] = struct {
result1 bool
}{result1}
}
func (fake *FakePublishedTrack) IsSubscriber(arg1 string) bool {
fake.isSubscriberMutex.Lock()
ret, specificReturn := fake.isSubscriberReturnsOnCall[len(fake.isSubscriberArgsForCall)]
@@ -958,6 +1031,59 @@ func (fake *FakePublishedTrack) SignalCidReturnsOnCall(i int, result1 string) {
}{result1}
}
func (fake *FakePublishedTrack) Source() livekit.TrackSource {
fake.sourceMutex.Lock()
ret, specificReturn := fake.sourceReturnsOnCall[len(fake.sourceArgsForCall)]
fake.sourceArgsForCall = append(fake.sourceArgsForCall, struct {
}{})
stub := fake.SourceStub
fakeReturns := fake.sourceReturns
fake.recordInvocation("Source", []interface{}{})
fake.sourceMutex.Unlock()
if stub != nil {
return stub()
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakePublishedTrack) SourceCallCount() int {
fake.sourceMutex.RLock()
defer fake.sourceMutex.RUnlock()
return len(fake.sourceArgsForCall)
}
func (fake *FakePublishedTrack) SourceCalls(stub func() livekit.TrackSource) {
fake.sourceMutex.Lock()
defer fake.sourceMutex.Unlock()
fake.SourceStub = stub
}
func (fake *FakePublishedTrack) SourceReturns(result1 livekit.TrackSource) {
fake.sourceMutex.Lock()
defer fake.sourceMutex.Unlock()
fake.SourceStub = nil
fake.sourceReturns = struct {
result1 livekit.TrackSource
}{result1}
}
func (fake *FakePublishedTrack) SourceReturnsOnCall(i int, result1 livekit.TrackSource) {
fake.sourceMutex.Lock()
defer fake.sourceMutex.Unlock()
fake.SourceStub = nil
if fake.sourceReturnsOnCall == nil {
fake.sourceReturnsOnCall = make(map[int]struct {
result1 livekit.TrackSource
})
}
fake.sourceReturnsOnCall[i] = struct {
result1 livekit.TrackSource
}{result1}
}
func (fake *FakePublishedTrack) ToProto() *livekit.TrackInfo {
fake.toProtoMutex.Lock()
ret, specificReturn := fake.toProtoReturnsOnCall[len(fake.toProtoArgsForCall)]
@@ -1061,6 +1187,8 @@ func (fake *FakePublishedTrack) Invocations() map[string][][]interface{} {
defer fake.iDMutex.RUnlock()
fake.isMutedMutex.RLock()
defer fake.isMutedMutex.RUnlock()
fake.isSimulcastMutex.RLock()
defer fake.isSimulcastMutex.RUnlock()
fake.isSubscriberMutex.RLock()
defer fake.isSubscriberMutex.RUnlock()
fake.kindMutex.RLock()
@@ -1083,6 +1211,8 @@ func (fake *FakePublishedTrack) Invocations() map[string][][]interface{} {
defer fake.setMutedMutex.RUnlock()
fake.signalCidMutex.RLock()
defer fake.signalCidMutex.RUnlock()
fake.sourceMutex.RLock()
defer fake.sourceMutex.RUnlock()
fake.toProtoMutex.RLock()
defer fake.toProtoMutex.RUnlock()
fake.updateVideoLayersMutex.RLock()
-74
View File
@@ -8,17 +8,6 @@ import (
)
type FakeRoom struct {
GetParticipantStub func(string) types.Participant
getParticipantMutex sync.RWMutex
getParticipantArgsForCall []struct {
arg1 string
}
getParticipantReturns struct {
result1 types.Participant
}
getParticipantReturnsOnCall map[int]struct {
result1 types.Participant
}
NameStub func() string
nameMutex sync.RWMutex
nameArgsForCall []struct {
@@ -46,67 +35,6 @@ type FakeRoom struct {
invocationsMutex sync.RWMutex
}
func (fake *FakeRoom) GetParticipant(arg1 string) types.Participant {
fake.getParticipantMutex.Lock()
ret, specificReturn := fake.getParticipantReturnsOnCall[len(fake.getParticipantArgsForCall)]
fake.getParticipantArgsForCall = append(fake.getParticipantArgsForCall, struct {
arg1 string
}{arg1})
stub := fake.GetParticipantStub
fakeReturns := fake.getParticipantReturns
fake.recordInvocation("GetParticipant", []interface{}{arg1})
fake.getParticipantMutex.Unlock()
if stub != nil {
return stub(arg1)
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakeRoom) GetParticipantCallCount() int {
fake.getParticipantMutex.RLock()
defer fake.getParticipantMutex.RUnlock()
return len(fake.getParticipantArgsForCall)
}
func (fake *FakeRoom) GetParticipantCalls(stub func(string) types.Participant) {
fake.getParticipantMutex.Lock()
defer fake.getParticipantMutex.Unlock()
fake.GetParticipantStub = stub
}
func (fake *FakeRoom) GetParticipantArgsForCall(i int) string {
fake.getParticipantMutex.RLock()
defer fake.getParticipantMutex.RUnlock()
argsForCall := fake.getParticipantArgsForCall[i]
return argsForCall.arg1
}
func (fake *FakeRoom) GetParticipantReturns(result1 types.Participant) {
fake.getParticipantMutex.Lock()
defer fake.getParticipantMutex.Unlock()
fake.GetParticipantStub = nil
fake.getParticipantReturns = struct {
result1 types.Participant
}{result1}
}
func (fake *FakeRoom) GetParticipantReturnsOnCall(i int, result1 types.Participant) {
fake.getParticipantMutex.Lock()
defer fake.getParticipantMutex.Unlock()
fake.GetParticipantStub = nil
if fake.getParticipantReturnsOnCall == nil {
fake.getParticipantReturnsOnCall = make(map[int]struct {
result1 types.Participant
})
}
fake.getParticipantReturnsOnCall[i] = struct {
result1 types.Participant
}{result1}
}
func (fake *FakeRoom) Name() string {
fake.nameMutex.Lock()
ret, specificReturn := fake.nameReturnsOnCall[len(fake.nameArgsForCall)]
@@ -231,8 +159,6 @@ func (fake *FakeRoom) UpdateSubscriptionsReturnsOnCall(i int, result1 error) {
func (fake *FakeRoom) Invocations() map[string][][]interface{} {
fake.invocationsMutex.RLock()
defer fake.invocationsMutex.RUnlock()
fake.getParticipantMutex.RLock()
defer fake.getParticipantMutex.RUnlock()
fake.nameMutex.RLock()
defer fake.nameMutex.RUnlock()
fake.updateSubscriptionsMutex.RLock()
@@ -40,6 +40,16 @@ type FakeSubscribedTrack struct {
isMutedReturnsOnCall map[int]struct {
result1 bool
}
PublishedTrackStub func() types.MediaTrack
publishedTrackMutex sync.RWMutex
publishedTrackArgsForCall []struct {
}
publishedTrackReturns struct {
result1 types.MediaTrack
}
publishedTrackReturnsOnCall map[int]struct {
result1 types.MediaTrack
}
PublisherIdentityStub func() string
publisherIdentityMutex sync.RWMutex
publisherIdentityArgsForCall []struct {
@@ -237,6 +247,59 @@ func (fake *FakeSubscribedTrack) IsMutedReturnsOnCall(i int, result1 bool) {
}{result1}
}
func (fake *FakeSubscribedTrack) PublishedTrack() types.MediaTrack {
fake.publishedTrackMutex.Lock()
ret, specificReturn := fake.publishedTrackReturnsOnCall[len(fake.publishedTrackArgsForCall)]
fake.publishedTrackArgsForCall = append(fake.publishedTrackArgsForCall, struct {
}{})
stub := fake.PublishedTrackStub
fakeReturns := fake.publishedTrackReturns
fake.recordInvocation("PublishedTrack", []interface{}{})
fake.publishedTrackMutex.Unlock()
if stub != nil {
return stub()
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakeSubscribedTrack) PublishedTrackCallCount() int {
fake.publishedTrackMutex.RLock()
defer fake.publishedTrackMutex.RUnlock()
return len(fake.publishedTrackArgsForCall)
}
func (fake *FakeSubscribedTrack) PublishedTrackCalls(stub func() types.MediaTrack) {
fake.publishedTrackMutex.Lock()
defer fake.publishedTrackMutex.Unlock()
fake.PublishedTrackStub = stub
}
func (fake *FakeSubscribedTrack) PublishedTrackReturns(result1 types.MediaTrack) {
fake.publishedTrackMutex.Lock()
defer fake.publishedTrackMutex.Unlock()
fake.PublishedTrackStub = nil
fake.publishedTrackReturns = struct {
result1 types.MediaTrack
}{result1}
}
func (fake *FakeSubscribedTrack) PublishedTrackReturnsOnCall(i int, result1 types.MediaTrack) {
fake.publishedTrackMutex.Lock()
defer fake.publishedTrackMutex.Unlock()
fake.PublishedTrackStub = nil
if fake.publishedTrackReturnsOnCall == nil {
fake.publishedTrackReturnsOnCall = make(map[int]struct {
result1 types.MediaTrack
})
}
fake.publishedTrackReturnsOnCall[i] = struct {
result1 types.MediaTrack
}{result1}
}
func (fake *FakeSubscribedTrack) PublisherIdentity() string {
fake.publisherIdentityMutex.Lock()
ret, specificReturn := fake.publisherIdentityReturnsOnCall[len(fake.publisherIdentityArgsForCall)]
@@ -440,6 +503,8 @@ func (fake *FakeSubscribedTrack) Invocations() map[string][][]interface{} {
defer fake.iDMutex.RUnlock()
fake.isMutedMutex.RLock()
defer fake.isMutedMutex.RUnlock()
fake.publishedTrackMutex.RLock()
defer fake.publishedTrackMutex.RUnlock()
fake.publisherIdentityMutex.RLock()
defer fake.publisherIdentityMutex.RUnlock()
fake.setPublisherMutedMutex.RLock()