Smoother dynacast transition (#390)
This commit is contained in:
+50
-6
@@ -134,6 +134,9 @@ type DownTrack struct {
|
||||
|
||||
// update stats
|
||||
onStatsUpdate func(dt *DownTrack, stat *livekit.AnalyticsStat)
|
||||
|
||||
// when max subscribed layer changes
|
||||
onMaxLayerChanged func(dt *DownTrack, layer int32)
|
||||
}
|
||||
|
||||
// NewDownTrack returns a DownTrack.
|
||||
@@ -336,6 +339,10 @@ func (d *DownTrack) WriteRTP(extPkt *buffer.ExtPacket, layer int32) error {
|
||||
f(d, pktSize)
|
||||
}
|
||||
|
||||
if tp.isSwitchingToMaxLayer && d.onMaxLayerChanged != nil && d.kind == webrtc.RTPCodecTypeVideo {
|
||||
d.onMaxLayerChanged(d, layer)
|
||||
}
|
||||
|
||||
d.UpdatePrimaryStats(uint32(pktSize))
|
||||
} else {
|
||||
d.logger.Errorw("writing rtp packet err", err)
|
||||
@@ -369,7 +376,7 @@ func (d *DownTrack) WritePaddingRTP(bytesToSend int) int {
|
||||
// can be sent only on frame boundaries, writing on disabled tracks
|
||||
// will give more options.
|
||||
// LK-TODO-END
|
||||
if d.forwarder.Muted() {
|
||||
if d.forwarder.IsMuted() {
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -440,12 +447,25 @@ func (d *DownTrack) WritePaddingRTP(bytesToSend int) int {
|
||||
}
|
||||
|
||||
// Mute enables or disables media forwarding
|
||||
func (d *DownTrack) Mute(val bool) {
|
||||
changed := d.forwarder.Mute(val)
|
||||
func (d *DownTrack) Mute(muted bool) {
|
||||
changed, maxLayers := d.forwarder.Mute(muted)
|
||||
if !changed {
|
||||
return
|
||||
}
|
||||
|
||||
if d.onMaxLayerChanged != nil && d.kind == webrtc.RTPCodecTypeVideo {
|
||||
if muted {
|
||||
d.onMaxLayerChanged(d, InvalidLayerSpatial)
|
||||
} else {
|
||||
//
|
||||
// When unmuting, don't wait for layer lock as
|
||||
// client might need to be notified to start layers
|
||||
// before locking can happen in the forwarder.
|
||||
//
|
||||
d.onMaxLayerChanged(d, maxLayers.spatial)
|
||||
}
|
||||
}
|
||||
|
||||
if d.onSubscriptionChanged != nil {
|
||||
d.onSubscriptionChanged(d)
|
||||
}
|
||||
@@ -478,6 +498,10 @@ func (d *DownTrack) CloseWithFlush(flush bool) {
|
||||
|
||||
d.connectionStats.Close()
|
||||
|
||||
if d.onMaxLayerChanged != nil && d.kind == webrtc.RTPCodecTypeVideo {
|
||||
d.onMaxLayerChanged(d, InvalidLayerSpatial)
|
||||
}
|
||||
|
||||
if d.onCloseHandler != nil {
|
||||
d.onCloseHandler()
|
||||
}
|
||||
@@ -485,18 +509,29 @@ func (d *DownTrack) CloseWithFlush(flush bool) {
|
||||
}
|
||||
|
||||
func (d *DownTrack) SetMaxSpatialLayer(spatialLayer int32) {
|
||||
changed, maxLayers := d.forwarder.SetMaxSpatialLayer(spatialLayer)
|
||||
changed, maxLayers, currentLayers := d.forwarder.SetMaxSpatialLayer(spatialLayer)
|
||||
if !changed {
|
||||
return
|
||||
}
|
||||
|
||||
if d.onMaxLayerChanged != nil && d.kind == webrtc.RTPCodecTypeVideo && maxLayers.SpatialGreaterThanOrEqual(currentLayers) {
|
||||
//
|
||||
// Notify when new max is
|
||||
// 1. Equal to current -> already locked to the new max
|
||||
// 2. Greater than current -> two scenarios
|
||||
// a. is higher than previous max -> client may need to start higher layer before forwarder can lock
|
||||
// b. is lower than previous max -> client can stop higher layer(s)
|
||||
//
|
||||
d.onMaxLayerChanged(d, maxLayers.spatial)
|
||||
}
|
||||
|
||||
if d.onSubscribedLayersChanged != nil {
|
||||
d.onSubscribedLayersChanged(d, maxLayers)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DownTrack) SetMaxTemporalLayer(temporalLayer int32) {
|
||||
changed, maxLayers := d.forwarder.SetMaxTemporalLayer(temporalLayer)
|
||||
changed, maxLayers, _ := d.forwarder.SetMaxTemporalLayer(temporalLayer)
|
||||
if !changed {
|
||||
return
|
||||
}
|
||||
@@ -570,6 +605,15 @@ func (d *DownTrack) OnStatsUpdate(fn func(dt *DownTrack, stat *livekit.Analytics
|
||||
d.onStatsUpdate = fn
|
||||
}
|
||||
|
||||
func (d *DownTrack) OnMaxLayerChanged(fn func(dt *DownTrack, layer int32)) {
|
||||
d.onMaxLayerChanged = fn
|
||||
|
||||
// have to send this immediately to set initial values
|
||||
if fn != nil && d.kind == webrtc.RTPCodecTypeVideo {
|
||||
go fn(d, d.forwarder.MaxLayers().spatial)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DownTrack) IsDeficient() bool {
|
||||
return d.forwarder.IsDeficient()
|
||||
}
|
||||
@@ -1041,7 +1085,7 @@ func (d *DownTrack) DebugInfo() map[string]interface{} {
|
||||
"SSRC": d.ssrc,
|
||||
"MimeType": d.codec.MimeType,
|
||||
"Bound": d.bound.get(),
|
||||
"Muted": d.forwarder.Muted(),
|
||||
"Muted": d.forwarder.IsMuted(),
|
||||
"CurrentSpatialLayer": d.forwarder.CurrentLayers().spatial,
|
||||
"Stats": stats,
|
||||
}
|
||||
|
||||
+23
-15
@@ -117,11 +117,12 @@ const (
|
||||
)
|
||||
|
||||
type TranslationParams struct {
|
||||
shouldDrop bool
|
||||
isDroppingRelevant bool
|
||||
shouldSendPLI bool
|
||||
rtp *TranslationParamsRTP
|
||||
vp8 *TranslationParamsVP8
|
||||
shouldDrop bool
|
||||
isDroppingRelevant bool
|
||||
shouldSendPLI bool
|
||||
isSwitchingToMaxLayer bool
|
||||
rtp *TranslationParamsRTP
|
||||
vp8 *TranslationParamsVP8
|
||||
}
|
||||
|
||||
type VideoLayers struct {
|
||||
@@ -137,6 +138,10 @@ func (v VideoLayers) GreaterThan(v2 VideoLayers) bool {
|
||||
return v.spatial > v2.spatial || (v.spatial == v2.spatial && v.temporal > v2.temporal)
|
||||
}
|
||||
|
||||
func (v VideoLayers) SpatialGreaterThanOrEqual(v2 VideoLayers) bool {
|
||||
return v.spatial >= v2.spatial
|
||||
}
|
||||
|
||||
const (
|
||||
InvalidLayerSpatial = int32(-1)
|
||||
InvalidLayerTemporal = int32(-1)
|
||||
@@ -211,49 +216,49 @@ func NewForwarder(codec webrtc.RTPCodecCapability, kind webrtc.RTPCodecType, log
|
||||
return f
|
||||
}
|
||||
|
||||
func (f *Forwarder) Mute(val bool) bool {
|
||||
func (f *Forwarder) Mute(val bool) (bool, VideoLayers) {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
if f.muted == val {
|
||||
return false
|
||||
return false, f.maxLayers
|
||||
}
|
||||
|
||||
f.muted = val
|
||||
return true
|
||||
return true, f.maxLayers
|
||||
}
|
||||
|
||||
func (f *Forwarder) Muted() bool {
|
||||
func (f *Forwarder) IsMuted() bool {
|
||||
f.lock.RLock()
|
||||
defer f.lock.RUnlock()
|
||||
|
||||
return f.muted
|
||||
}
|
||||
|
||||
func (f *Forwarder) SetMaxSpatialLayer(spatialLayer int32) (bool, VideoLayers) {
|
||||
func (f *Forwarder) SetMaxSpatialLayer(spatialLayer int32) (bool, VideoLayers, VideoLayers) {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
if f.kind == webrtc.RTPCodecTypeAudio || spatialLayer == f.maxLayers.spatial {
|
||||
return false, InvalidLayers
|
||||
return false, f.maxLayers, f.currentLayers
|
||||
}
|
||||
|
||||
f.maxLayers.spatial = spatialLayer
|
||||
|
||||
return true, f.maxLayers
|
||||
return true, f.maxLayers, f.currentLayers
|
||||
}
|
||||
|
||||
func (f *Forwarder) SetMaxTemporalLayer(temporalLayer int32) (bool, VideoLayers) {
|
||||
func (f *Forwarder) SetMaxTemporalLayer(temporalLayer int32) (bool, VideoLayers, VideoLayers) {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
if f.kind == webrtc.RTPCodecTypeAudio || temporalLayer == f.maxLayers.temporal {
|
||||
return false, InvalidLayers
|
||||
return false, f.maxLayers, f.currentLayers
|
||||
}
|
||||
|
||||
f.maxLayers.temporal = temporalLayer
|
||||
|
||||
return true, f.maxLayers
|
||||
return true, f.maxLayers, f.currentLayers
|
||||
}
|
||||
|
||||
func (f *Forwarder) MaxLayers() VideoLayers {
|
||||
@@ -1118,6 +1123,9 @@ func (f *Forwarder) getTranslationParamsVideo(extPkt *buffer.ExtPacket, layer in
|
||||
if extPkt.KeyFrame {
|
||||
// lock to target layer
|
||||
f.currentLayers.spatial = f.targetLayers.spatial
|
||||
if f.currentLayers.spatial == f.maxLayers.spatial {
|
||||
tp.isSwitchingToMaxLayer = true
|
||||
}
|
||||
} else {
|
||||
tp.shouldSendPLI = true
|
||||
}
|
||||
|
||||
+39
-19
@@ -24,13 +24,16 @@ func newForwarder(codec webrtc.RTPCodecCapability, kind webrtc.RTPCodecType) *Fo
|
||||
|
||||
func TestForwarderMute(t *testing.T) {
|
||||
f := newForwarder(testutils.TestOpusCodec, webrtc.RTPCodecTypeAudio)
|
||||
require.False(t, f.Muted())
|
||||
require.False(t, f.Mute(false)) // no change in mute state
|
||||
require.False(t, f.Muted())
|
||||
require.True(t, f.Mute(true))
|
||||
require.True(t, f.Muted())
|
||||
require.True(t, f.Mute(false))
|
||||
require.False(t, f.Muted())
|
||||
require.False(t, f.IsMuted())
|
||||
muted, _ := f.Mute(false)
|
||||
require.False(t, muted) // no change in mute state
|
||||
require.False(t, f.IsMuted())
|
||||
muted, _ = f.Mute(true)
|
||||
require.True(t, muted)
|
||||
require.True(t, f.IsMuted())
|
||||
muted, _ = f.Mute(false)
|
||||
require.True(t, muted)
|
||||
require.False(t, f.IsMuted())
|
||||
}
|
||||
|
||||
func TestForwarderLayersAudio(t *testing.T) {
|
||||
@@ -41,13 +44,15 @@ func TestForwarderLayersAudio(t *testing.T) {
|
||||
require.Equal(t, InvalidLayers, f.CurrentLayers())
|
||||
require.Equal(t, InvalidLayers, f.TargetLayers())
|
||||
|
||||
changed, layers := f.SetMaxSpatialLayer(1)
|
||||
changed, maxLayers, currentLayers := f.SetMaxSpatialLayer(1)
|
||||
require.False(t, changed)
|
||||
require.Equal(t, InvalidLayers, layers)
|
||||
require.Equal(t, InvalidLayers, maxLayers)
|
||||
require.Equal(t, InvalidLayers, currentLayers)
|
||||
|
||||
changed, layers = f.SetMaxTemporalLayer(1)
|
||||
changed, maxLayers, currentLayers = f.SetMaxTemporalLayer(1)
|
||||
require.False(t, changed)
|
||||
require.Equal(t, InvalidLayers, layers)
|
||||
require.Equal(t, InvalidLayers, maxLayers)
|
||||
require.Equal(t, InvalidLayers, currentLayers)
|
||||
|
||||
require.Equal(t, InvalidLayers, f.MaxLayers())
|
||||
}
|
||||
@@ -65,31 +70,46 @@ func TestForwarderLayersVideo(t *testing.T) {
|
||||
require.Equal(t, InvalidLayers, f.CurrentLayers())
|
||||
require.Equal(t, InvalidLayers, f.TargetLayers())
|
||||
|
||||
changed, layers := f.SetMaxSpatialLayer(DefaultMaxLayerSpatial)
|
||||
changed, maxLayers, currentLayers := f.SetMaxSpatialLayer(DefaultMaxLayerSpatial)
|
||||
require.False(t, changed)
|
||||
require.Equal(t, InvalidLayers, layers)
|
||||
require.Equal(t, expectedLayers, maxLayers)
|
||||
require.Equal(t, InvalidLayers, currentLayers)
|
||||
|
||||
changed, layers = f.SetMaxSpatialLayer(DefaultMaxLayerSpatial - 1)
|
||||
changed, maxLayers, currentLayers = f.SetMaxSpatialLayer(DefaultMaxLayerSpatial - 1)
|
||||
require.True(t, changed)
|
||||
expectedLayers = VideoLayers{
|
||||
spatial: DefaultMaxLayerSpatial - 1,
|
||||
temporal: DefaultMaxLayerTemporal,
|
||||
}
|
||||
require.Equal(t, expectedLayers, layers)
|
||||
require.Equal(t, expectedLayers, maxLayers)
|
||||
require.Equal(t, expectedLayers, f.MaxLayers())
|
||||
require.Equal(t, InvalidLayers, currentLayers)
|
||||
|
||||
changed, layers = f.SetMaxTemporalLayer(DefaultMaxLayerTemporal)
|
||||
f.currentLayers = VideoLayers{spatial: 0, temporal: 1}
|
||||
changed, maxLayers, currentLayers = f.SetMaxSpatialLayer(DefaultMaxLayerSpatial - 1)
|
||||
require.False(t, changed)
|
||||
require.Equal(t, InvalidLayers, layers)
|
||||
expectedLayers = VideoLayers{
|
||||
spatial: DefaultMaxLayerSpatial - 1,
|
||||
temporal: DefaultMaxLayerTemporal,
|
||||
}
|
||||
require.Equal(t, expectedLayers, maxLayers)
|
||||
require.Equal(t, expectedLayers, f.MaxLayers())
|
||||
require.Equal(t, VideoLayers{spatial: 0, temporal: 1}, currentLayers)
|
||||
|
||||
changed, layers = f.SetMaxTemporalLayer(DefaultMaxLayerTemporal - 1)
|
||||
changed, maxLayers, currentLayers = f.SetMaxTemporalLayer(DefaultMaxLayerTemporal)
|
||||
require.False(t, changed)
|
||||
require.Equal(t, expectedLayers, maxLayers)
|
||||
require.Equal(t, VideoLayers{spatial: 0, temporal: 1}, currentLayers)
|
||||
|
||||
changed, maxLayers, currentLayers = f.SetMaxTemporalLayer(DefaultMaxLayerTemporal - 1)
|
||||
require.True(t, changed)
|
||||
expectedLayers = VideoLayers{
|
||||
spatial: DefaultMaxLayerSpatial - 1,
|
||||
temporal: DefaultMaxLayerTemporal - 1,
|
||||
}
|
||||
require.Equal(t, expectedLayers, layers)
|
||||
require.Equal(t, expectedLayers, maxLayers)
|
||||
require.Equal(t, expectedLayers, f.MaxLayers())
|
||||
require.Equal(t, VideoLayers{spatial: 0, temporal: 1}, currentLayers)
|
||||
}
|
||||
|
||||
func TestForwarderGetForwardingStatus(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user