Use loss based scoring for screen share tracks. (#771)

* Use loss based scoring for screen share tracks.

* Remove named TODO markers and file issues
This commit is contained in:
Raja Subramanian
2022-06-20 12:08:30 +05:30
committed by GitHub
parent 69800a45cb
commit 1e6a12167b
6 changed files with 155 additions and 35 deletions
+73
View File
@@ -239,6 +239,14 @@ func TestConnectionQuality(t *testing.T) {
return tr return tr
} }
testPublishedScreenshareTrack := func(params connectionquality.TrackScoreParams) *typesfakes.FakeLocalMediaTrack {
tr := &typesfakes.FakeLocalMediaTrack{}
score := connectionquality.ScreenshareTrackScore(params)
t.Log("screen share score: ", score)
tr.GetConnectionScoreReturns(score)
return tr
}
// TODO: this test is rather limited since we cannot mock DownTrack's Target & Max spatial layers // TODO: this test is rather limited since we cannot mock DownTrack's Target & Max spatial layers
// to improve this after split // to improve this after split
@@ -429,6 +437,71 @@ func TestConnectionQuality(t *testing.T) {
require.Equal(t, livekit.ConnectionQuality_POOR, p.GetConnectionQuality().GetQuality()) require.Equal(t, livekit.ConnectionQuality_POOR, p.GetConnectionQuality().GetQuality())
}) })
t.Run("screen share no loss, not reduced quality, should be excellent", func(t *testing.T) {
p := newParticipantForTest("test")
// 20kbps, 2fps
params := connectionquality.TrackScoreParams{
Duration: 1 * time.Second,
PacketsExpected: 100,
PacketsLost: 0,
Bytes: 2500,
Frames: 2,
}
p.UpTrackManager.publishedTracks["ss"] = testPublishedScreenshareTrack(params)
require.Equal(t, livekit.ConnectionQuality_EXCELLENT, p.GetConnectionQuality().GetQuality())
})
t.Run("screen share low loss, not reduced quality, should be excellent", func(t *testing.T) {
p := newParticipantForTest("test")
// 20kbps, 2fps
params := connectionquality.TrackScoreParams{
Duration: 1 * time.Second,
PacketsExpected: 100,
PacketsLost: 1,
Bytes: 2500,
Frames: 2,
}
p.UpTrackManager.publishedTracks["ss"] = testPublishedScreenshareTrack(params)
require.Equal(t, livekit.ConnectionQuality_EXCELLENT, p.GetConnectionQuality().GetQuality())
})
t.Run("screen share high loss, not reduced quality, should be poor", func(t *testing.T) {
p := newParticipantForTest("test")
// 20kbps, 2fps
params := connectionquality.TrackScoreParams{
Duration: 1 * time.Second,
PacketsExpected: 100,
PacketsLost: 5,
Bytes: 2500,
Frames: 2,
}
p.UpTrackManager.publishedTracks["ss"] = testPublishedScreenshareTrack(params)
require.Equal(t, livekit.ConnectionQuality_POOR, p.GetConnectionQuality().GetQuality())
})
t.Run("screen share no loss, but reduced quality, should be good", func(t *testing.T) {
p := newParticipantForTest("test")
// 20kbps, 2fps
params := connectionquality.TrackScoreParams{
Duration: 1 * time.Second,
PacketsExpected: 100,
PacketsLost: 0,
Bytes: 2500,
Frames: 2,
IsReducedQuality: true,
}
p.UpTrackManager.publishedTracks["ss"] = testPublishedScreenshareTrack(params)
require.Equal(t, livekit.ConnectionQuality_GOOD, p.GetConnectionQuality().GetQuality())
})
} }
func TestSubscriberAsPrimary(t *testing.T) { func TestSubscriberAsPrimary(t *testing.T) {
+8
View File
@@ -198,3 +198,11 @@ func (d *DumbReceiver) IsDtxDisabled() bool {
} }
return false return false
} }
func (d *DumbReceiver) TrackSource() livekit.TrackSource {
if r, ok := d.receiver.Load().(sfu.TrackReceiver); ok {
return r.TrackSource()
}
return livekit.TrackSource_UNKNOWN
}
+19 -12
View File
@@ -27,11 +27,13 @@ type ConnectionStatsParams struct {
IsDtxDisabled func() bool IsDtxDisabled func() bool
GetLayerDimension func(int32) (uint32, uint32) GetLayerDimension func(int32) (uint32, uint32)
GetMaxExpectedLayer func() *livekit.VideoLayer GetMaxExpectedLayer func() *livekit.VideoLayer
GetIsReducedQuality func() bool
Logger logger.Logger Logger logger.Logger
} }
type ConnectionStats struct { type ConnectionStats struct {
params ConnectionStatsParams params ConnectionStatsParams
trackSource livekit.TrackSource
onStatsUpdate func(cs *ConnectionStats, stat *livekit.AnalyticsStat) onStatsUpdate func(cs *ConnectionStats, stat *livekit.AnalyticsStat)
@@ -45,9 +47,10 @@ type ConnectionStats struct {
func NewConnectionStats(params ConnectionStatsParams) *ConnectionStats { func NewConnectionStats(params ConnectionStatsParams) *ConnectionStats {
return &ConnectionStats{ return &ConnectionStats{
params: params, params: params,
score: 5.0, trackSource: livekit.TrackSource_UNKNOWN,
done: make(chan struct{}), score: 5.0,
done: make(chan struct{}),
} }
} }
@@ -63,6 +66,10 @@ func (cs *ConnectionStats) Close() {
close(cs.done) close(cs.done)
} }
func (cs *ConnectionStats) SetTrackSource(trackSource livekit.TrackSource) {
cs.trackSource = trackSource
}
func (cs *ConnectionStats) OnStatsUpdate(fn func(cs *ConnectionStats, stat *livekit.AnalyticsStat)) { func (cs *ConnectionStats) OnStatsUpdate(fn func(cs *ConnectionStats, stat *livekit.AnalyticsStat)) {
cs.onStatsUpdate = fn cs.onStatsUpdate = fn
} }
@@ -86,11 +93,6 @@ func (cs *ConnectionStats) updateScore(streams map[uint32]*buffer.StreamStatsWit
} }
cs.lastUpdate = time.Now() cs.lastUpdate = time.Now()
// RAJA-TODO - take mute into account
// RAJA-TODO - maybe a probation period at the start, coming out of mute?
// RAJA-TODO - maybe a probation period when a layer starts/stops?
// RAJA-TODO - maybe layer changes should be notified here directly rather than pulling
// RAJA-TODO - maybe track info should be set here and this can know max published without having to pull every time
maxAvailableLayer, maxAvailableLayerStats := cs.getMaxAvailableLayerStats(streams) maxAvailableLayer, maxAvailableLayerStats := cs.getMaxAvailableLayerStats(streams)
if maxAvailableLayerStats == nil { if maxAvailableLayerStats == nil {
// retain old score as stats will not be available when muted // retain old score as stats will not be available when muted
@@ -108,14 +110,19 @@ func (cs *ConnectionStats) updateScore(streams map[uint32]*buffer.StreamStatsWit
Rtt: maxAvailableLayerStats.RttMax, Rtt: maxAvailableLayerStats.RttMax,
} }
switch cs.params.CodecType { switch {
case webrtc.RTPCodecTypeAudio: case cs.trackSource == livekit.TrackSource_SCREEN_SHARE:
if cs.params.GetIsReducedQuality != nil {
params.IsReducedQuality = cs.params.GetIsReducedQuality()
}
cs.score = ScreenshareTrackScore(params)
case cs.params.CodecType == webrtc.RTPCodecTypeAudio:
if cs.params.IsDtxDisabled != nil { if cs.params.IsDtxDisabled != nil {
params.DtxDisabled = cs.params.IsDtxDisabled() params.DtxDisabled = cs.params.IsDtxDisabled()
} }
cs.score = AudioTrackScore(params) cs.score = AudioTrackScore(params)
case webrtc.RTPCodecTypeVideo: case cs.params.CodecType == webrtc.RTPCodecTypeVideo:
// get tracks expected max layer and dimensions // get tracks expected max layer and dimensions
maxExpectedLayer := cs.params.GetMaxExpectedLayer() maxExpectedLayer := cs.params.GetMaxExpectedLayer()
if maxExpectedLayer == nil || utils.SpatialLayerForQuality(maxExpectedLayer.Quality) == buffer.InvalidLayerSpatial { if maxExpectedLayer == nil || utils.SpatialLayerForQuality(maxExpectedLayer.Quality) == buffer.InvalidLayerSpatial {
+38 -13
View File
@@ -39,19 +39,20 @@ func int32Ptr(x int32) *int32 {
} }
type TrackScoreParams struct { type TrackScoreParams struct {
Duration time.Duration Duration time.Duration
Codec string Codec string
PacketsExpected uint32 PacketsExpected uint32
PacketsLost uint32 PacketsLost uint32
Bytes uint64 Bytes uint64
Frames uint32 Frames uint32
Jitter float64 Jitter float64
Rtt uint32 Rtt uint32
DtxDisabled bool DtxDisabled bool
ActualWidth uint32 ActualWidth uint32
ActualHeight uint32 ActualHeight uint32
ExpectedWidth uint32 ExpectedWidth uint32
ExpectedHeight uint32 ExpectedHeight uint32
IsReducedQuality bool
} }
func getRtcMosStat(params TrackScoreParams) rtcmos.Stat { func getRtcMosStat(params TrackScoreParams) rtcmos.Stat {
@@ -95,3 +96,27 @@ func VideoTrackScore(params TrackScoreParams) float32 {
} }
return 0 return 0
} }
//
// rtcmos gives lower score when screen share content is static.
// Even though the frame rate is low, the bit rate is also low and
// the resolution is high. Till rtcmos model can be adapted to that
// scenario, use loss based scoring.
//
func ScreenshareTrackScore(params TrackScoreParams) float32 {
pctLoss := getLossPercentage(params.PacketsExpected, params.PacketsLost)
// No Loss, excellent
if pctLoss == 0.0 && !params.IsReducedQuality {
return 5.0
}
// default when loss is minimal, but reducedQuality
score := float32(3.5)
// loss is bad
if pctLoss >= 4.0 {
score = 2.0
} else if pctLoss <= 2.0 && !params.IsReducedQuality {
// loss is acceptable and at reduced quality
score = 4.5
}
return score
}
+7 -4
View File
@@ -223,18 +223,20 @@ func NewDownTrack(
d.connectionStats = connectionquality.NewConnectionStats(connectionquality.ConnectionStatsParams{ d.connectionStats = connectionquality.NewConnectionStats(connectionquality.ConnectionStatsParams{
CodecType: kind, CodecType: kind,
CodecName: getCodecNameFromMime(codecs[0].MimeType), // LK-TODO: have to notify on codec change
MimeType: codecs[0].MimeType, // LK-TODO have to notify on codec change
GetDeltaStats: d.getDeltaStats, GetDeltaStats: d.getDeltaStats,
IsDtxDisabled: d.receiver.IsDtxDisabled, IsDtxDisabled: d.receiver.IsDtxDisabled,
GetLayerDimension: d.receiver.GetLayerDimension, GetLayerDimension: d.receiver.GetLayerDimension,
// RAJA-TODO: Check muted, test quality when muted
// RAJA-TODO: this one needs more work as layers are muxed into one layer, maybe need to maitain RTP stats separately for each layer
GetMaxExpectedLayer: func() *livekit.VideoLayer { GetMaxExpectedLayer: func() *livekit.VideoLayer {
maxLayer := d.forwarder.MaxLayers().Spatial maxLayer := d.forwarder.MaxLayers().Spatial
width, height := d.receiver.GetLayerDimension(maxLayer) width, height := d.receiver.GetLayerDimension(maxLayer)
return &livekit.VideoLayer{Quality: utils.QualityForSpatialLayer(maxLayer), Width: width, Height: height} return &livekit.VideoLayer{Quality: utils.QualityForSpatialLayer(maxLayer), Width: width, Height: height}
}, },
Logger: d.logger, GetIsReducedQuality: func() bool {
CodecName: getCodecNameFromMime(codecs[0].MimeType), return d.GetForwardingStatus() != ForwardingStatusOptimal
},
Logger: d.logger,
}) })
d.connectionStats.OnStatsUpdate(func(_cs *connectionquality.ConnectionStats, stat *livekit.AnalyticsStat) { d.connectionStats.OnStatsUpdate(func(_cs *connectionquality.ConnectionStats, stat *livekit.AnalyticsStat) {
if d.onStatsUpdate != nil { if d.onStatsUpdate != nil {
@@ -295,6 +297,7 @@ func (d *DownTrack) Bind(t webrtc.TrackLocalContext) (webrtc.RTPCodecParameters,
} }
d.bound.Store(true) d.bound.Store(true)
d.connectionStats.SetTrackSource(d.receiver.TrackSource())
d.connectionStats.Start() d.connectionStats.Start()
d.logger.Debugw("bound") d.logger.Debugw("bound")
+10 -6
View File
@@ -53,6 +53,7 @@ type TrackReceiver interface {
DebugInfo() map[string]interface{} DebugInfo() map[string]interface{}
TrackSource() livekit.TrackSource
GetLayerDimension(layer int32) (uint32, uint32) GetLayerDimension(layer int32) (uint32, uint32)
IsDtxDisabled() bool IsDtxDisabled() bool
} }
@@ -203,13 +204,16 @@ func NewWebRTCReceiver(
w.connectionStats = connectionquality.NewConnectionStats(connectionquality.ConnectionStatsParams{ w.connectionStats = connectionquality.NewConnectionStats(connectionquality.ConnectionStatsParams{
CodecType: w.kind, CodecType: w.kind,
CodecName: getCodecNameFromMime(w.codec.MimeType),
MimeType: w.codec.MimeType,
GetDeltaStats: w.getDeltaStats, GetDeltaStats: w.getDeltaStats,
IsDtxDisabled: w.IsDtxDisabled, IsDtxDisabled: w.IsDtxDisabled,
GetLayerDimension: w.GetLayerDimension, GetLayerDimension: w.GetLayerDimension,
GetMaxExpectedLayer: w.getMaxExpectedLayer, GetMaxExpectedLayer: w.streamTrackerManager.GetMaxExpectedLayer,
GetIsReducedQuality: w.streamTrackerManager.IsReducedQuality,
Logger: w.logger, Logger: w.logger,
CodecName: getCodecNameFromMime(w.codec.MimeType),
}) })
w.connectionStats.SetTrackSource(w.trackInfo.Source)
w.connectionStats.OnStatsUpdate(func(_cs *connectionquality.ConnectionStats, stat *livekit.AnalyticsStat) { w.connectionStats.OnStatsUpdate(func(_cs *connectionquality.ConnectionStats, stat *livekit.AnalyticsStat) {
if w.onStatsUpdate != nil { if w.onStatsUpdate != nil {
w.onStatsUpdate(w, stat) w.onStatsUpdate(w, stat)
@@ -220,6 +224,10 @@ func NewWebRTCReceiver(
return w return w
} }
func (w *WebRTCReceiver) TrackSource() livekit.TrackSource {
return w.trackInfo.Source
}
func (w *WebRTCReceiver) IsDtxDisabled() bool { func (w *WebRTCReceiver) IsDtxDisabled() bool {
return w.trackInfo.DisableDtx return w.trackInfo.DisableDtx
} }
@@ -228,10 +236,6 @@ func (w *WebRTCReceiver) GetLayerDimension(layer int32) (uint32, uint32) {
return w.streamTrackerManager.GetLayerDimension(layer) return w.streamTrackerManager.GetLayerDimension(layer)
} }
func (w *WebRTCReceiver) getMaxExpectedLayer() *livekit.VideoLayer {
return w.streamTrackerManager.GetMaxExpectedLayer()
}
func (w *WebRTCReceiver) OnStatsUpdate(fn func(w *WebRTCReceiver, stat *livekit.AnalyticsStat)) { func (w *WebRTCReceiver) OnStatsUpdate(fn func(w *WebRTCReceiver, stat *livekit.AnalyticsStat)) {
w.onStatsUpdate = fn w.onStatsUpdate = fn
} }