downstream allocator (#155)

* WIP branch to hash out down stream allocator.

* Plug more bits of stream allocator

* update protocol

* remove SignalRequest_Simulcast (#154)

* Plug more bits of stream allocator

* Handle simulcast track available layers change

* WIP branch to hash out down stream allocator.

* Plug more bits of stream allocator

* Handle simulcast track available layers change

* Adopt signature of call to AdjustAllocation

* Move StreamAllocator to PCTransport and allocate only for subscriber.

* Move streamallocator to ion-sfu

* Start/Stop of streamallocator

* Use StreamAllocator for subscriber bandwidth management.

* Do not allocate in ADD_TRACK

* Set payload in constructor

* - Add some logging
- Protocol message to notify clients of paused/resumed streams

* named return

* oops correct sense of isPausing

* Update pkg/sfu/streamallocator.go

Committing David's suggestion.

Co-authored-by: David Zhao <david@davidzhao.com>

* - Log estimate changes/commits.
- Catch more than epsilon change oscillating for long time.

Co-authored-by: David Colburn <xero73@gmail.com>
Co-authored-by: David Zhao <david@davidzhao.com>
This commit is contained in:
Raja Subramanian
2021-11-26 09:11:04 +05:30
committed by GitHub
co-authored by David Colburn David Zhao
parent 9aff9af690
commit 98695cdd22
11 changed files with 470 additions and 318 deletions
+4 -16
View File
@@ -293,7 +293,7 @@ func (t *MediaTrack) AddSubscriber(sub types.Participant) error {
t.subscribedTracks[sub.ID()] = subTrack
subTrack.SetPublisherMuted(t.IsMuted())
t.receiver.AddDownTrack(downTrack, t.shouldStartWithBestQuality())
t.receiver.AddDownTrack(downTrack)
// since sub will lock, run it in a goroutine to avoid deadlocks
go func() {
sub.AddSubscribedTrack(subTrack)
@@ -306,16 +306,10 @@ func (t *MediaTrack) AddSubscriber(sub types.Participant) error {
func (t *MediaTrack) NumUpTracks() (uint32, uint32) {
numRegistered := atomic.LoadUint32(&t.numUpTracks)
numPublishing := uint32(0)
var numPublishing uint32
if t.simulcasted.Get() {
t.lock.RLock()
if t.receiver != nil {
for i := int32(0); i < 3; i++ {
if t.receiver.HasSpatialLayer(i) {
numPublishing += 1
}
}
}
numPublishing = uint32(t.receiver.NumAvailableSpatialLayers())
t.lock.RUnlock()
} else {
numPublishing = 1
@@ -392,7 +386,7 @@ func (t *MediaTrack) AddReceiver(receiver *webrtc.RTPReceiver, track *webrtc.Tra
}
}
t.receiver.AddUpTrack(track, buff, t.shouldStartWithBestQuality())
t.receiver.AddUpTrack(track, buff)
t.params.Telemetry.AddUpTrack(t.params.ParticipantID, buff)
atomic.AddUint32(&t.numUpTracks, 1)
@@ -463,12 +457,6 @@ func (t *MediaTrack) GetQualityForDimension(width, height uint32) livekit.VideoQ
return quality
}
// LK-TODO: this should probably left up to auto size management and StreamAllocator
// this function assumes caller holds lock
func (t *MediaTrack) shouldStartWithBestQuality() bool {
return len(t.subscribedTracks) < 10
}
// TODO: send for all downtracks from the source participant
// https://tools.ietf.org/html/rfc7941
func (t *MediaTrack) sendDownTrackBindingReports(sub types.Participant) {
+41
View File
@@ -180,6 +180,8 @@ func NewParticipant(params ParticipantParams) (*ParticipantImpl, error) {
p.subscriber.OnOffer(p.onOffer)
p.subscriber.OnStreamedTracksChange(p.onStreamedTracksChange)
return p, nil
}
@@ -804,6 +806,8 @@ func (p *ParticipantImpl) AddSubscribedTrack(subTrack types.SubscribedTrack) {
p.lock.Lock()
p.subscribedTracks[subTrack.ID()] = subTrack
p.lock.Unlock()
p.subscriber.AddTrack(subTrack)
p.subscribedTo.Store(subTrack.PublisherIdentity(), struct{}{})
}
@@ -811,6 +815,9 @@ func (p *ParticipantImpl) AddSubscribedTrack(subTrack types.SubscribedTrack) {
func (p *ParticipantImpl) RemoveSubscribedTrack(subTrack types.SubscribedTrack) {
p.params.Logger.Debugw("removed subscribedTrack", "publisher", subTrack.PublisherIdentity(),
"participant", p.Identity(), "track", subTrack.ID(), "kind", subTrack.DownTrack().Kind())
p.subscriber.RemoveTrack(subTrack)
p.lock.Lock()
delete(p.subscribedTracks, subTrack.ID())
// remove from subscribed map
@@ -1307,6 +1314,40 @@ func (p *ParticipantImpl) configureReceiverDTX() {
}
}
func (p *ParticipantImpl) onStreamedTracksChange(paused map[string][]string, resumed map[string][]string) error {
if len(paused) == 0 && len(resumed) == 0 {
return nil
}
streamedTracksUpdate := &livekit.StreamedTracksUpdate{}
if len(paused) != 0 {
for participantId, trackIds := range paused {
for _, trackId := range trackIds {
streamedTracksUpdate.Paused = append(streamedTracksUpdate.Paused, &livekit.StreamedTrack{
ParticipantSid: participantId,
TrackSid: trackId,
})
}
}
}
if len(resumed) != 0 {
for participantId, trackIds := range paused {
for _, trackId := range trackIds {
streamedTracksUpdate.Resumed = append(streamedTracksUpdate.Resumed, &livekit.StreamedTrack{
ParticipantSid: participantId,
TrackSid: trackId,
})
}
}
}
return p.writeMessage(&livekit.SignalResponse{
Message: &livekit.SignalResponse_StreamedTracksUpdate{
StreamedTracksUpdate: streamedTracksUpdate,
},
})
}
func (p *ParticipantImpl) DebugInfo() map[string]interface{} {
info := map[string]interface{}{
"ID": p.id,
+1 -12
View File
@@ -62,18 +62,7 @@ func (t *SubscribedTrack) UpdateSubscriberSettings(enabled bool, quality livekit
t.subMuted.TrySet(!enabled)
t.updateDownTrackMute()
if enabled && t.dt.Kind() == webrtc.RTPCodecTypeVideo {
err := t.dt.SetMaxSpatialLayer(spatialLayerForQuality(quality))
// LK-TODO-START
// For now, this set max calls into switchSpatialLayer and returns the result.
// When StreamAllocator becomes the one true way to allocate layers,
// there will not a ErrSpatialLayerNotFound error as the allocation
// logic will automatically the best available layer under the max layer.
// So, this check should be removed when enabling StreamAllocator
// LK-TODO-END
if err == sfu.ErrSpatialLayerNotFound && quality != livekit.VideoQuality_MEDIUM {
// try to switch to middle layer
_ = t.dt.SetMaxSpatialLayer(spatialLayerForQuality(livekit.VideoQuality_MEDIUM))
}
t.dt.SetMaxSpatialLayer(spatialLayerForQuality(quality))
}
})
}
+42 -1
View File
@@ -10,6 +10,8 @@ import (
"github.com/pion/interceptor"
"github.com/pion/webrtc/v3"
"github.com/livekit/livekit-server/pkg/rtc/types"
"github.com/livekit/livekit-server/pkg/sfu"
"github.com/livekit/livekit-server/pkg/telemetry"
"github.com/livekit/livekit-server/pkg/telemetry/prometheus"
)
@@ -37,7 +39,11 @@ type PCTransport struct {
onOffer func(offer webrtc.SessionDescription)
restartAfterGathering bool
negotiationState int
logger logger.Logger
// stream allocator for subscriber PC
streamAllocator *sfu.StreamAllocator
logger logger.Logger
}
type TransportParams struct {
@@ -92,6 +98,13 @@ func NewPCTransport(params TransportParams) (*PCTransport, error) {
negotiationState: negotiationStateNone,
logger: params.Logger,
}
if params.Target == livekit.SignalTarget_SUBSCRIBER {
t.streamAllocator = sfu.NewStreamAllocator(sfu.StreamAllocatorParams{
ParticipantID: params.ParticipantID,
Logger: params.Logger,
})
t.streamAllocator.Start()
}
t.pc.OnICEGatheringStateChange(func(state webrtc.ICEGathererState) {
if state == webrtc.ICEGathererStateComplete {
go func() {
@@ -126,6 +139,10 @@ func (t *PCTransport) PeerConnection() *webrtc.PeerConnection {
}
func (t *PCTransport) Close() {
if t.streamAllocator != nil {
t.streamAllocator.Stop()
}
_ = t.pc.Close()
}
@@ -238,3 +255,27 @@ func (t *PCTransport) createAndSendOffer(options *webrtc.OfferOptions) error {
go t.onOffer(offer)
return nil
}
func (t *PCTransport) OnStreamedTracksChange(f func(paused map[string][]string, resumed map[string][]string) error) {
if t.streamAllocator == nil {
return
}
t.streamAllocator.OnStreamedTracksChange(f)
}
func (t *PCTransport) AddTrack(subTrack types.SubscribedTrack) {
if t.streamAllocator == nil {
return
}
t.streamAllocator.AddTrack(subTrack.DownTrack(), subTrack.PublisherIdentity())
}
func (t *PCTransport) RemoveTrack(subTrack types.SubscribedTrack) {
if t.streamAllocator == nil {
return
}
t.streamAllocator.RemoveTrack(subTrack.DownTrack())
}