Track source based stream tracker config (#503)
* Track source based stream tracker config * SCREEN_SHARE_AUDIO is an audio source
This commit is contained in:
@@ -160,6 +160,7 @@ func (t *MediaTrack) AddReceiver(receiver *webrtc.RTPReceiver, track *webrtc.Tra
|
|||||||
receiver,
|
receiver,
|
||||||
track,
|
track,
|
||||||
t.PublisherID(),
|
t.PublisherID(),
|
||||||
|
t.params.TrackInfo.Source,
|
||||||
t.params.Logger,
|
t.params.Logger,
|
||||||
sfu.WithPliThrottle(t.params.PLIThrottleConfig),
|
sfu.WithPliThrottle(t.params.PLIThrottleConfig),
|
||||||
sfu.WithLoadBalanceThreshold(20),
|
sfu.WithLoadBalanceThreshold(20),
|
||||||
|
|||||||
+2
-1
@@ -138,6 +138,7 @@ func NewWebRTCReceiver(
|
|||||||
receiver *webrtc.RTPReceiver,
|
receiver *webrtc.RTPReceiver,
|
||||||
track *webrtc.TrackRemote,
|
track *webrtc.TrackRemote,
|
||||||
pid livekit.ParticipantID,
|
pid livekit.ParticipantID,
|
||||||
|
source livekit.TrackSource,
|
||||||
logger logger.Logger,
|
logger logger.Logger,
|
||||||
opts ...ReceiverOpts,
|
opts ...ReceiverOpts,
|
||||||
) *WebRTCReceiver {
|
) *WebRTCReceiver {
|
||||||
@@ -155,7 +156,7 @@ func NewWebRTCReceiver(
|
|||||||
index: make(map[livekit.ParticipantID]int),
|
index: make(map[livekit.ParticipantID]int),
|
||||||
free: make(map[int]struct{}),
|
free: make(map[int]struct{}),
|
||||||
numProcs: runtime.NumCPU(),
|
numProcs: runtime.NumCPU(),
|
||||||
streamTrackerManager: NewStreamTrackerManager(logger),
|
streamTrackerManager: NewStreamTrackerManager(logger, source),
|
||||||
}
|
}
|
||||||
w.streamTrackerManager.OnAvailableLayersChanged(w.downTrackLayerChange)
|
w.streamTrackerManager.OnAvailableLayersChanged(w.downTrackLayerChange)
|
||||||
|
|
||||||
|
|||||||
@@ -1224,8 +1224,6 @@ func (t *Track) SetPriority(priority uint8) bool {
|
|||||||
switch t.source {
|
switch t.source {
|
||||||
case livekit.TrackSource_SCREEN_SHARE:
|
case livekit.TrackSource_SCREEN_SHARE:
|
||||||
priority = PriorityDefaultScreenshare
|
priority = PriorityDefaultScreenshare
|
||||||
case livekit.TrackSource_SCREEN_SHARE_AUDIO:
|
|
||||||
priority = PriorityDefaultScreenshare
|
|
||||||
default:
|
default:
|
||||||
priority = PriorityDefaultVideo
|
priority = PriorityDefaultVideo
|
||||||
}
|
}
|
||||||
@@ -1248,7 +1246,7 @@ func (t *Track) DownTrack() *DownTrack {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Track) IsManaged() bool {
|
func (t *Track) IsManaged() bool {
|
||||||
return (t.source != livekit.TrackSource_SCREEN_SHARE && t.source != livekit.TrackSource_SCREEN_SHARE_AUDIO) || t.isSimulcast
|
return t.source != livekit.TrackSource_SCREEN_SHARE || t.isSimulcast
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Track) ID() livekit.TrackID {
|
func (t *Track) ID() livekit.TrackID {
|
||||||
|
|||||||
+21
-15
@@ -27,14 +27,22 @@ const (
|
|||||||
StreamStatusActive StreamStatus = 1
|
StreamStatusActive StreamStatus = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type StreamTrackerParams struct {
|
||||||
|
// number of samples needed per cycle
|
||||||
|
SamplesRequired uint32
|
||||||
|
|
||||||
|
// number of cycles needed to be active
|
||||||
|
CyclesRequired uint32
|
||||||
|
|
||||||
|
CycleDuration time.Duration
|
||||||
|
|
||||||
|
Logger logger.Logger
|
||||||
|
}
|
||||||
|
|
||||||
// StreamTracker keeps track of packet flow and ensures a particular up track is consistently producing
|
// StreamTracker keeps track of packet flow and ensures a particular up track is consistently producing
|
||||||
// It runs its own goroutine for detection, and fires OnStatusChanged callback
|
// It runs its own goroutine for detection, and fires OnStatusChanged callback
|
||||||
type StreamTracker struct {
|
type StreamTracker struct {
|
||||||
// number of samples needed per cycle
|
params StreamTrackerParams
|
||||||
samplesRequired uint32
|
|
||||||
// number of cycles needed to be active
|
|
||||||
cyclesRequired uint64
|
|
||||||
cycleDuration time.Duration
|
|
||||||
|
|
||||||
onStatusChanged func(status StreamStatus)
|
onStatusChanged func(status StreamStatus)
|
||||||
|
|
||||||
@@ -49,7 +57,7 @@ type StreamTracker struct {
|
|||||||
status StreamStatus
|
status StreamStatus
|
||||||
|
|
||||||
// only access within detectWorker
|
// only access within detectWorker
|
||||||
cycleCount uint64
|
cycleCount uint32
|
||||||
|
|
||||||
// only access by the same goroutine as Observe
|
// only access by the same goroutine as Observe
|
||||||
lastSN uint16
|
lastSN uint16
|
||||||
@@ -59,13 +67,11 @@ type StreamTracker struct {
|
|||||||
isStopped atomic.Bool
|
isStopped atomic.Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStreamTracker(logger logger.Logger, samplesRequired uint32, cyclesRequired uint64, cycleDuration time.Duration) *StreamTracker {
|
func NewStreamTracker(params StreamTrackerParams) *StreamTracker {
|
||||||
s := &StreamTracker{
|
s := &StreamTracker{
|
||||||
samplesRequired: samplesRequired,
|
params: params,
|
||||||
cyclesRequired: cyclesRequired,
|
status: StreamStatusStopped,
|
||||||
cycleDuration: cycleDuration,
|
callbacksQueue: utils.NewOpsQueue(params.Logger),
|
||||||
status: StreamStatusStopped,
|
|
||||||
callbacksQueue: utils.NewOpsQueue(logger),
|
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
@@ -181,7 +187,7 @@ func (s *StreamTracker) Observe(sn uint16) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *StreamTracker) detectWorker(generation uint32) {
|
func (s *StreamTracker) detectWorker(generation uint32) {
|
||||||
ticker := time.NewTicker(s.cycleDuration)
|
ticker := time.NewTicker(s.params.CycleDuration)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
<-ticker.C
|
<-ticker.C
|
||||||
@@ -198,7 +204,7 @@ func (s *StreamTracker) detectChanges() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.countSinceLast.Load() >= s.samplesRequired {
|
if s.countSinceLast.Load() >= s.params.SamplesRequired {
|
||||||
s.cycleCount += 1
|
s.cycleCount += 1
|
||||||
} else {
|
} else {
|
||||||
s.cycleCount = 0
|
s.cycleCount = 0
|
||||||
@@ -207,7 +213,7 @@ func (s *StreamTracker) detectChanges() {
|
|||||||
if s.cycleCount == 0 {
|
if s.cycleCount == 0 {
|
||||||
// flip to stopped
|
// flip to stopped
|
||||||
s.maybeSetStopped()
|
s.maybeSetStopped()
|
||||||
} else if s.cycleCount >= s.cyclesRequired {
|
} else if s.cycleCount >= s.params.CyclesRequired {
|
||||||
// flip to active
|
// flip to active
|
||||||
s.maybeSetActive()
|
s.maybeSetActive()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,10 +12,19 @@ import (
|
|||||||
"github.com/livekit/protocol/logger"
|
"github.com/livekit/protocol/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func newStreamTracker(samplesRequired uint32, cyclesRequired uint32, cycleDuration time.Duration) *StreamTracker {
|
||||||
|
return NewStreamTracker(StreamTrackerParams{
|
||||||
|
SamplesRequired: samplesRequired,
|
||||||
|
CyclesRequired: cyclesRequired,
|
||||||
|
CycleDuration: cycleDuration,
|
||||||
|
Logger: logger.Logger(logger.GetLogger()),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestStreamTracker(t *testing.T) {
|
func TestStreamTracker(t *testing.T) {
|
||||||
t.Run("flips to active on first observe", func(t *testing.T) {
|
t.Run("flips to active on first observe", func(t *testing.T) {
|
||||||
callbackCalled := atomic.NewBool(false)
|
callbackCalled := atomic.NewBool(false)
|
||||||
tracker := NewStreamTracker(logger.Logger(logger.GetLogger()), 5, 60, 500*time.Millisecond)
|
tracker := newStreamTracker(5, 60, 500*time.Millisecond)
|
||||||
tracker.Start()
|
tracker.Start()
|
||||||
tracker.OnStatusChanged(func(status StreamStatus) {
|
tracker.OnStatusChanged(func(status StreamStatus) {
|
||||||
callbackCalled.Store(true)
|
callbackCalled.Store(true)
|
||||||
@@ -40,7 +49,7 @@ func TestStreamTracker(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("flips to inactive immediately", func(t *testing.T) {
|
t.Run("flips to inactive immediately", func(t *testing.T) {
|
||||||
tracker := NewStreamTracker(logger.Logger(logger.GetLogger()), 5, 60, 500*time.Millisecond)
|
tracker := newStreamTracker(5, 60, 500*time.Millisecond)
|
||||||
tracker.Start()
|
tracker.Start()
|
||||||
require.Equal(t, StreamStatusStopped, tracker.Status())
|
require.Equal(t, StreamStatusStopped, tracker.Status())
|
||||||
|
|
||||||
@@ -76,7 +85,7 @@ func TestStreamTracker(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("flips back to active after iterations", func(t *testing.T) {
|
t.Run("flips back to active after iterations", func(t *testing.T) {
|
||||||
tracker := NewStreamTracker(logger.Logger(logger.GetLogger()), 1, 2, 500*time.Millisecond)
|
tracker := newStreamTracker(1, 2, 500*time.Millisecond)
|
||||||
tracker.Start()
|
tracker.Start()
|
||||||
require.Equal(t, StreamStatusStopped, tracker.Status())
|
require.Equal(t, StreamStatusStopped, tracker.Status())
|
||||||
|
|
||||||
@@ -103,7 +112,7 @@ func TestStreamTracker(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("does not change to inactive when paused", func(t *testing.T) {
|
t.Run("does not change to inactive when paused", func(t *testing.T) {
|
||||||
tracker := NewStreamTracker(logger.Logger(logger.GetLogger()), 5, 60, 500*time.Millisecond)
|
tracker := newStreamTracker(5, 60, 500*time.Millisecond)
|
||||||
tracker.Start()
|
tracker.Start()
|
||||||
tracker.Observe(1)
|
tracker.Observe(1)
|
||||||
testutils.WithTimeout(t, func() string {
|
testutils.WithTimeout(t, func() string {
|
||||||
@@ -123,7 +132,7 @@ func TestStreamTracker(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("flips back to active on first observe after reset", func(t *testing.T) {
|
t.Run("flips back to active on first observe after reset", func(t *testing.T) {
|
||||||
callbackCalled := atomic.NewUint32(0)
|
callbackCalled := atomic.NewUint32(0)
|
||||||
tracker := NewStreamTracker(logger.Logger(logger.GetLogger()), 5, 60, 500*time.Millisecond)
|
tracker := newStreamTracker(5, 60, 500*time.Millisecond)
|
||||||
tracker.Start()
|
tracker.Start()
|
||||||
tracker.OnStatusChanged(func(status StreamStatus) {
|
tracker.OnStatusChanged(func(status StreamStatus) {
|
||||||
callbackCalled.Inc()
|
callbackCalled.Inc()
|
||||||
|
|||||||
@@ -5,11 +5,52 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/livekit/protocol/livekit"
|
||||||
"github.com/livekit/protocol/logger"
|
"github.com/livekit/protocol/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ConfigVideo = []StreamTrackerParams{
|
||||||
|
{
|
||||||
|
SamplesRequired: 1,
|
||||||
|
CyclesRequired: 4,
|
||||||
|
CycleDuration: 500 * time.Millisecond,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
SamplesRequired: 5,
|
||||||
|
CyclesRequired: 60,
|
||||||
|
CycleDuration: 500 * time.Millisecond,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
SamplesRequired: 5,
|
||||||
|
CyclesRequired: 60,
|
||||||
|
CycleDuration: 500 * time.Millisecond,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// be very forgiving for screen share to account for cases like static screen where there could be only one packet per second
|
||||||
|
ConfigScreenshare = []StreamTrackerParams{
|
||||||
|
{
|
||||||
|
SamplesRequired: 1,
|
||||||
|
CyclesRequired: 1,
|
||||||
|
CycleDuration: 2 * time.Second,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
SamplesRequired: 1,
|
||||||
|
CyclesRequired: 1,
|
||||||
|
CycleDuration: 2 * time.Second,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
SamplesRequired: 1,
|
||||||
|
CyclesRequired: 1,
|
||||||
|
CycleDuration: 2 * time.Second,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
type StreamTrackerManager struct {
|
type StreamTrackerManager struct {
|
||||||
logger logger.Logger
|
logger logger.Logger
|
||||||
|
source livekit.TrackSource
|
||||||
|
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
|
|
||||||
@@ -21,9 +62,10 @@ type StreamTrackerManager struct {
|
|||||||
onAvailableLayersChanged func(availableLayers []int32)
|
onAvailableLayersChanged func(availableLayers []int32)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStreamTrackerManager(logger logger.Logger) *StreamTrackerManager {
|
func NewStreamTrackerManager(logger logger.Logger, source livekit.TrackSource) *StreamTrackerManager {
|
||||||
return &StreamTrackerManager{
|
return &StreamTrackerManager{
|
||||||
logger: logger,
|
logger: logger,
|
||||||
|
source: source,
|
||||||
maxExpectedLayer: DefaultMaxLayerSpatial,
|
maxExpectedLayer: DefaultMaxLayerSpatial,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -33,16 +75,22 @@ func (s *StreamTrackerManager) OnAvailableLayersChanged(f func(availableLayers [
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *StreamTrackerManager) AddTracker(layer int32) {
|
func (s *StreamTrackerManager) AddTracker(layer int32) {
|
||||||
cycleDuration := 500 * time.Millisecond
|
var params StreamTrackerParams
|
||||||
samplesRequired := uint32(5)
|
if s.source == livekit.TrackSource_SCREEN_SHARE {
|
||||||
cyclesRequired := uint64(60) // 30s of continuous stream
|
if int(layer) >= len(ConfigScreenshare) {
|
||||||
if layer == 0 {
|
return
|
||||||
// be very forgiving for base layer to account for cases like static screen share where there could be only one packet per second
|
}
|
||||||
samplesRequired = 1
|
|
||||||
cyclesRequired = 1 // 1 packet in 2 seconds
|
params = ConfigScreenshare[layer]
|
||||||
cycleDuration = 2 * time.Second
|
} else {
|
||||||
|
if int(layer) >= len(ConfigVideo) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
params = ConfigVideo[layer]
|
||||||
}
|
}
|
||||||
tracker := NewStreamTracker(s.logger, samplesRequired, cyclesRequired, cycleDuration)
|
params.Logger = s.logger
|
||||||
|
tracker := NewStreamTracker(params)
|
||||||
tracker.OnStatusChanged(func(status StreamStatus) {
|
tracker.OnStatusChanged(func(status StreamStatus) {
|
||||||
if status == StreamStatusStopped {
|
if status == StreamStatusStopped {
|
||||||
s.removeAvailableLayer(layer)
|
s.removeAvailableLayer(layer)
|
||||||
|
|||||||
Reference in New Issue
Block a user