Normalize known rids. (#3779)

Normalize the rids in SDP to known patterns.
Currently,
- LK protocol uses q;h;f
- Sean's OBS WHIP uses 0;1;2

As the ordering in SDP could be different, normalize to known order.

For RIDs not in the known set, just use it as is.
This commit is contained in:
Raja Subramanian
2025-07-06 13:32:21 +05:30
committed by GitHub
parent ddd923296b
commit 40028dc33c
3 changed files with 205 additions and 106 deletions
+57 -11
View File
@@ -15,20 +15,28 @@
package buffer
import (
"slices"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
)
const (
QuarterResolution = "q"
HalfResolution = "h"
FullResolution = "f"
quarterResolutionQ = "q"
halfResolutionH = "h"
fullResolutionF = "f"
quarterResolution0 = "0"
halfResolution1 = "1"
fullResolution2 = "2"
)
type VideoLayersRid [DefaultMaxLayerSpatial + 1]string
var (
DefaultVideoLayersRid = VideoLayersRid{QuarterResolution, HalfResolution, FullResolution}
videoLayersRidQHF = VideoLayersRid{quarterResolutionQ, halfResolutionH, fullResolutionF}
videoLayersRid012 = VideoLayersRid{quarterResolution0, halfResolution1, fullResolution2}
DefaultVideoLayersRid = videoLayersRidQHF
)
// SIMULCAST-CODEC-TODO: these need to be codec mime aware if and when each codec suppports different layers
@@ -54,11 +62,11 @@ func RidToSpatialLayer(rid string, trackInfo *livekit.TrackInfo, ridSpace VideoL
lp := LayerPresenceFromTrackInfo(trackInfo)
if lp == nil {
switch rid {
case QuarterResolution:
case quarterResolutionQ:
return 0
case HalfResolution:
case halfResolutionH:
return 1
case FullResolution:
case fullResolutionF:
return 2
default:
return 0
@@ -129,13 +137,13 @@ func SpatialLayerToRid(layer int32, trackInfo *livekit.TrackInfo, ridSpace Video
if lp == nil {
switch layer {
case 0:
return QuarterResolution
return quarterResolutionQ
case 1:
return HalfResolution
return halfResolutionH
case 2:
return FullResolution
return fullResolutionF
default:
return QuarterResolution
return quarterResolutionQ
}
}
@@ -435,3 +443,41 @@ func GetVideoQualityForSpatialLayer(spatialLayer int32, ti *livekit.TrackInfo) l
return livekit.VideoQuality_OFF
}
func isVideoLayersRidKnown(rids VideoLayersRid, knownRids VideoLayersRid) bool {
for _, rid := range rids {
if rid == "" {
continue
}
if !slices.Contains(knownRids[:], rid) {
return false
}
}
return true
}
func NormalizeVideoLayersRid(rids VideoLayersRid) VideoLayersRid {
out := rids
normalize := func(knownRids VideoLayersRid) {
idx := 0
for _, known := range knownRids {
if slices.Contains(rids[:], known) {
out[idx] = known
idx++
}
}
}
if isVideoLayersRidKnown(rids, videoLayersRidQHF) {
normalize(videoLayersRidQHF)
}
if isVideoLayersRidKnown(rids, videoLayersRid012) {
normalize(videoLayersRid012)
}
return out
}