mirror of
https://github.com/livekit/livekit.git
synced 2026-06-06 17:41:54 +00:00
1e1aaeb86b
* Separate from ion-sfu changes: 1. extract pkg/buffer, twcc, sfu, relay, stats, logger 2. to solve cycle import, move ion-sfu/pkg/logger to pkg/sfu/logger 3. replace pion/ion-sfu => ./ reason: will change import pion/ion-sfu/pkg/* to livekit-server/pkg/* after this pr merged. Just not change any code in this pr, because it will confused with the separate code from ion-sfu in review. * Move code from ion-sfu to pkg/sfu * fix build error for resovle conflict Co-authored-by: cnderrauber <zengjie9004@gmail.com>
89 lines
2.1 KiB
Go
89 lines
2.1 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.SwitchSpatialLayer(spatialLayerForQuality(quality), true)
|
|
if err == sfu.ErrSpatialLayerNotFound && quality != livekit.VideoQuality_MEDIUM {
|
|
// try to switch to middle layer
|
|
_ = t.dt.SwitchSpatialLayer(spatialLayerForQuality(livekit.VideoQuality_MEDIUM), true)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|