Refactor DynacastQuality & MediaLossProxy into separate modules (#894)
* WIP commit * Refactor media loss proxy * Use DynacastQuality and MediaLossProxy from MediaTrack * fix test * Remove unused param * Remove unused interfaces * Move interface methods to local * Split out DynacastManager * have to add codec to dynacast manager * RUnlock * fix restart * Adding API to force quality and also maintain closed state * Address PR comments
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/pion/rtcp"
|
||||
"github.com/pion/webrtc/v3"
|
||||
@@ -15,7 +14,6 @@ import (
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/config"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||
"github.com/livekit/livekit-server/pkg/sfu"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/buffer"
|
||||
@@ -24,7 +22,6 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
downLostUpdateDelta = time.Second
|
||||
layerSelectionTolerance = 0.9
|
||||
)
|
||||
|
||||
@@ -51,7 +48,6 @@ type MediaTrackReceiverParams struct {
|
||||
BufferFactory *buffer.Factory
|
||||
ReceiverConfig ReceiverConfig
|
||||
SubscriberConfig DirectionConfig
|
||||
VideoConfig config.VideoConfig
|
||||
Telemetry telemetry.TelemetryService
|
||||
Logger logger.Logger
|
||||
}
|
||||
@@ -69,14 +65,10 @@ type MediaTrackReceiver struct {
|
||||
potentialCodecs []webrtc.RTPCodecParameters
|
||||
pendingSubscribeOp map[livekit.ParticipantID]int
|
||||
|
||||
// track audio fraction lost
|
||||
downFracLostLock sync.Mutex
|
||||
maxDownFracLost uint8
|
||||
maxDownFracLostTs time.Time
|
||||
onMediaLossUpdate func(fractionalLoss uint8)
|
||||
onVideoLayerUpdate func(layers []*livekit.VideoLayer)
|
||||
|
||||
onClose []func()
|
||||
onSetupReceiver func(mime string)
|
||||
onMediaLossFeedback func(dt *sfu.DownTrack, report *rtcp.ReceiverReport)
|
||||
onVideoLayerUpdate func(layers []*livekit.VideoLayer)
|
||||
onClose []func()
|
||||
|
||||
*MediaTrackSubscriptions
|
||||
}
|
||||
@@ -94,7 +86,6 @@ func NewMediaTrackReceiver(params MediaTrackReceiverParams) *MediaTrackReceiver
|
||||
BufferFactory: params.BufferFactory,
|
||||
ReceiverConfig: params.ReceiverConfig,
|
||||
SubscriberConfig: params.SubscriberConfig,
|
||||
VideoConfig: t.params.VideoConfig,
|
||||
Telemetry: params.Telemetry,
|
||||
Logger: params.Logger,
|
||||
})
|
||||
@@ -124,8 +115,12 @@ func (t *MediaTrackReceiver) Restart() {
|
||||
for _, receiver := range receivers {
|
||||
receiver.SetMaxExpectedSpatialLayer(utils.SpatialLayerForQuality(livekit.VideoQuality_HIGH))
|
||||
}
|
||||
}
|
||||
|
||||
t.MediaTrackSubscriptions.Restart()
|
||||
func (t *MediaTrackReceiver) OnSetupReceiver(f func(mime string)) {
|
||||
t.lock.Lock()
|
||||
t.onSetupReceiver = f
|
||||
t.lock.Unlock()
|
||||
}
|
||||
|
||||
func (t *MediaTrackReceiver) SetupReceiver(receiver sfu.TrackReceiver, priority int, mid string) {
|
||||
@@ -172,12 +167,13 @@ func (t *MediaTrackReceiver) SetupReceiver(receiver sfu.TrackReceiver, priority
|
||||
|
||||
t.shadowReceiversLocked()
|
||||
|
||||
onSetupReceiver := t.onSetupReceiver
|
||||
t.params.Logger.Debugw("setup receiver", "mime", receiver.Codec().MimeType, "priority", priority, "receivers", t.receiversShadow)
|
||||
t.lock.Unlock()
|
||||
|
||||
t.MediaTrackSubscriptions.AddCodec(receiver.Codec().MimeType)
|
||||
|
||||
t.MediaTrackSubscriptions.Start()
|
||||
if onSetupReceiver != nil {
|
||||
onSetupReceiver(receiver.Codec().MimeType)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *MediaTrackReceiver) SetPotentialCodecs(codecs []webrtc.RTPCodecParameters, headers []webrtc.RTPHeaderExtensionParameter) {
|
||||
@@ -238,13 +234,7 @@ func (t *MediaTrackReceiver) ClearReceiver(mime string) {
|
||||
}
|
||||
|
||||
t.shadowReceiversLocked()
|
||||
|
||||
stopSubscription := len(t.receiversShadow) == 0
|
||||
t.lock.Unlock()
|
||||
|
||||
if stopSubscription {
|
||||
t.MediaTrackSubscriptions.Stop()
|
||||
}
|
||||
}
|
||||
|
||||
func (t *MediaTrackReceiver) ClearAllReceivers() {
|
||||
@@ -252,11 +242,10 @@ func (t *MediaTrackReceiver) ClearAllReceivers() {
|
||||
t.receivers = t.receivers[:0]
|
||||
t.receiversShadow = nil
|
||||
t.lock.Unlock()
|
||||
t.MediaTrackSubscriptions.Stop()
|
||||
}
|
||||
|
||||
func (t *MediaTrackReceiver) OnMediaLossUpdate(f func(fractionalLoss uint8)) {
|
||||
t.onMediaLossUpdate = f
|
||||
func (t *MediaTrackReceiver) OnMediaLossFeedback(f func(dt *sfu.DownTrack, rr *rtcp.ReceiverReport)) {
|
||||
t.onMediaLossFeedback = f
|
||||
}
|
||||
|
||||
func (t *MediaTrackReceiver) OnVideoLayerUpdate(f func(layers []*livekit.VideoLayer)) {
|
||||
@@ -280,7 +269,6 @@ func (t *MediaTrackReceiver) Close() {
|
||||
onclose := t.onClose
|
||||
t.lock.RUnlock()
|
||||
|
||||
t.MediaTrackSubscriptions.Close()
|
||||
for _, f := range onclose {
|
||||
f()
|
||||
}
|
||||
@@ -662,53 +650,11 @@ func (t *MediaTrackReceiver) GetAudioLevel() (float64, bool) {
|
||||
|
||||
func (t *MediaTrackReceiver) onDownTrackCreated(downTrack *sfu.DownTrack) {
|
||||
if t.Kind() == livekit.TrackType_AUDIO {
|
||||
downTrack.AddReceiverReportListener(t.handleMaxLossFeedback)
|
||||
}
|
||||
}
|
||||
|
||||
// handles max loss for audio streams
|
||||
func (t *MediaTrackReceiver) handleMaxLossFeedback(_ *sfu.DownTrack, report *rtcp.ReceiverReport) {
|
||||
t.downFracLostLock.Lock()
|
||||
for _, rr := range report.Reports {
|
||||
if t.maxDownFracLost < rr.FractionLost {
|
||||
t.maxDownFracLost = rr.FractionLost
|
||||
}
|
||||
}
|
||||
t.downFracLostLock.Unlock()
|
||||
|
||||
t.maybeUpdateLoss()
|
||||
}
|
||||
|
||||
func (t *MediaTrackReceiver) NotifySubscriberNodeMediaLoss(_nodeID livekit.NodeID, fractionalLoss uint8) {
|
||||
t.downFracLostLock.Lock()
|
||||
if t.maxDownFracLost < fractionalLoss {
|
||||
t.maxDownFracLost = fractionalLoss
|
||||
}
|
||||
t.downFracLostLock.Unlock()
|
||||
|
||||
t.maybeUpdateLoss()
|
||||
}
|
||||
|
||||
func (t *MediaTrackReceiver) maybeUpdateLoss() {
|
||||
var (
|
||||
shouldUpdate bool
|
||||
maxLost uint8
|
||||
)
|
||||
|
||||
t.downFracLostLock.Lock()
|
||||
now := time.Now()
|
||||
if now.Sub(t.maxDownFracLostTs) > downLostUpdateDelta {
|
||||
shouldUpdate = true
|
||||
maxLost = t.maxDownFracLost
|
||||
t.maxDownFracLost = 0
|
||||
t.maxDownFracLostTs = now
|
||||
}
|
||||
t.downFracLostLock.Unlock()
|
||||
|
||||
if shouldUpdate {
|
||||
if t.onMediaLossUpdate != nil {
|
||||
t.onMediaLossUpdate(maxLost)
|
||||
}
|
||||
downTrack.AddReceiverReportListener(func(dt *sfu.DownTrack, rr *rtcp.ReceiverReport) {
|
||||
if t.onMediaLossFeedback != nil {
|
||||
t.onMediaLossFeedback(dt, rr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -782,20 +728,6 @@ func (t *MediaTrackReceiver) SetRTT(rtt uint32) {
|
||||
}
|
||||
}
|
||||
|
||||
func (t *MediaTrackReceiver) OnSubscribedMaxQualityChange(f func(trackID livekit.TrackID, subscribedQualities []*livekit.SubscribedCodec, maxSubscribedQualities []types.SubscribedCodecQuality) error) {
|
||||
t.MediaTrackSubscriptions.OnSubscribedMaxQualityChange(func(subscribedQualities []*livekit.SubscribedCodec, maxSubscribedQualities []types.SubscribedCodecQuality) {
|
||||
if f != nil && !t.IsMuted() {
|
||||
_ = f(t.ID(), subscribedQualities, maxSubscribedQualities)
|
||||
}
|
||||
for _, q := range maxSubscribedQualities {
|
||||
receiver := t.Receiver(q.CodecMime)
|
||||
if receiver != nil {
|
||||
receiver.SetMaxExpectedSpatialLayer(utils.SpatialLayerForQuality(q.Quality))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------
|
||||
|
||||
func VideoQualityToRID(q livekit.VideoQuality) string {
|
||||
|
||||
Reference in New Issue
Block a user