fix(video): determine svc/simulcast from SDP for advanced codecs (#3549)

* fix(video): determine svc/simulcast from SDP for advanced codecs

* fix(explicit-svc): cleanup

* fix(explicit-svc): remove from list on close/remove

* fix(explicit-svc): reorder VLS selection, cleanup

* fix(explicit-svc): todo comments for temporal layer selector

* fix(explicit-svc): remove from simulcastTrackIds even if client does not support unpublish
This commit is contained in:
Anunay Maheshwari
2025-03-26 16:22:32 +05:30
committed by GitHub
parent 2b6a46f402
commit 15f565510c
14 changed files with 168 additions and 216 deletions
+37
View File
@@ -37,6 +37,7 @@ import (
"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
sdpHelper "github.com/livekit/protocol/sdp"
"github.com/livekit/protocol/utils"
"github.com/livekit/protocol/utils/guid"
@@ -206,6 +207,8 @@ type ParticipantImpl struct {
pendingPublishingTracks map[livekit.TrackID]*pendingTrackInfo
pendingRemoteTracks []*pendingRemoteTrack
simulcastTrackIds sync.Map
// supported codecs
enabledPublishCodecs []*livekit.Codec
enabledSubscribeCodecs []*livekit.Codec
@@ -870,10 +873,29 @@ func (p *ParticipantImpl) synthesizeAddTrackRequests(offer webrtc.SessionDescrip
return nil
}
func (p *ParticipantImpl) findAndStoreSimulcastTrackIds(offer webrtc.SessionDescription) {
parsed, err := offer.Unmarshal()
if err != nil {
return
}
for _, m := range parsed.MediaDescriptions {
if m.MediaName.Media != "video" {
continue
}
if sdpHelper.IsMediaDescriptionSimulcast(m) {
trackID := sdpHelper.GetTrackIDFromMediaDescription(m)
p.simulcastTrackIds.LoadOrStore(trackID, true)
}
}
}
// HandleOffer an offer from remote participant, used when clients make the initial connection
func (p *ParticipantImpl) HandleOffer(offer webrtc.SessionDescription) error {
p.pubLogger.Debugw("received offer", "transport", livekit.SignalTarget_PUBLISHER, "offer", offer)
p.findAndStoreSimulcastTrackIds(offer)
if p.params.UseOneShotSignallingMode {
if err := p.synthesizeAddTrackRequests(offer); err != nil {
return err
@@ -1059,6 +1081,7 @@ func (p *ParticipantImpl) Close(sendLeave bool, reason types.ParticipantCloseRea
p.pendingTracksLock.Lock()
p.pendingTracks = make(map[string]*pendingTrackInfo)
p.pendingPublishingTracks = make(map[livekit.TrackID]*pendingTrackInfo)
p.simulcastTrackIds.Clear()
p.pendingTracksLock.Unlock()
p.UpTrackManager.Close(isExpectedToResume)
@@ -1737,6 +1760,11 @@ func (p *ParticipantImpl) onSubscriberOffer(offer webrtc.SessionDescription) err
func (p *ParticipantImpl) removePublishedTrack(track types.MediaTrack) {
p.RemovePublishedTrack(track, false, true)
if lmt, ok := track.(types.LocalMediaTrack); ok {
p.simulcastTrackIds.Delete(lmt.SdpCid())
}
if p.ProtocolVersion().SupportsUnpublish() {
p.sendTrackUnpublished(track.ID())
} else {
@@ -2455,6 +2483,7 @@ func (p *ParticipantImpl) mediaTrackReceived(track sfu.TrackRemote, rtpReceiver
// only assign version on a fresh publish, i. e. avoid updating version in scenarios like migration
ti.Version = p.params.VersionGenerator.Next().ToProto()
}
mt = p.addMediaTrack(signalCid, track.ID(), ti)
newTrack = true
@@ -2468,6 +2497,12 @@ func (p *ParticipantImpl) mediaTrackReceived(track sfu.TrackRemote, rtpReceiver
p.dirty.Store(true)
}
isSimulcast, ok := p.simulcastTrackIds.Load(track.ID())
if !ok {
isSimulcast = false
}
mt.SetSimulcast(isSimulcast.(bool))
p.pendingTracksLock.Unlock()
mt.AddReceiver(rtpReceiver, track, mid)
@@ -2621,6 +2656,8 @@ func (p *ParticipantImpl) addMediaTrack(signalCid string, sdpCid string, ti *liv
)
}
p.simulcastTrackIds.Delete(mt.SdpCid())
p.pendingTracksLock.Lock()
if pti := p.pendingTracks[signalCid]; pti != nil {
p.sendTrackPublished(signalCid, pti.trackInfos[0])