Files
livekit-3d/pkg/rtc/subscribedtrack.go
T
Raja Subramanian 391e2f8b31 Separate out max layer setting (#201)
Small step on the way to making StreamAllocator prioritization of tracks.
With the new callback into StreamAllocator, the idea is to use the
max layer information to do track prioritization.

Testing:
--------
Sanity check that sample app works
2021-11-22 14:01:25 +05:30

96 lines
2.5 KiB
Go

package rtc
import (
"time"
"github.com/bep/debounce"
"github.com/livekit/livekit-server/pkg/sfu"
livekit "github.com/livekit/protocol/proto"
"github.com/livekit/protocol/utils"
"github.com/pion/webrtc/v3"
)
const (
subscriptionDebounceInterval = 100 * time.Millisecond
)
type SubscribedTrack struct {
dt *sfu.DownTrack
publisherIdentity string
subMuted utils.AtomicFlag
pubMuted utils.AtomicFlag
debouncer func(func())
}
func NewSubscribedTrack(publisherIdentity string, dt *sfu.DownTrack) *SubscribedTrack {
return &SubscribedTrack{
publisherIdentity: publisherIdentity,
dt: dt,
debouncer: debounce.New(subscriptionDebounceInterval),
}
}
func (t *SubscribedTrack) ID() string {
return t.dt.ID()
}
func (t *SubscribedTrack) PublisherIdentity() string {
return t.publisherIdentity
}
func (t *SubscribedTrack) DownTrack() *sfu.DownTrack {
return t.dt
}
func (t *SubscribedTrack) SubscribeLossPercentage() uint32 {
return FixedPointToPercent(t.DownTrack().CurrentMaxLossFraction())
}
// has subscriber indicated it wants to mute this track
func (t *SubscribedTrack) IsMuted() bool {
return t.subMuted.Get()
}
func (t *SubscribedTrack) SetPublisherMuted(muted bool) {
t.pubMuted.TrySet(muted)
t.updateDownTrackMute()
}
func (t *SubscribedTrack) UpdateSubscriberSettings(enabled bool, quality livekit.VideoQuality) {
t.debouncer(func() {
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))
}
}
})
}
func (t *SubscribedTrack) updateDownTrackMute() {
muted := t.subMuted.Get() || t.pubMuted.Get()
t.dt.Mute(muted)
}
func spatialLayerForQuality(quality livekit.VideoQuality) int32 {
switch quality {
case livekit.VideoQuality_LOW:
return 0
case livekit.VideoQuality_MEDIUM:
return 1
default:
return 2
}
}