Skip to content

Commit 6fd3374

Browse files
committed
Default to highest Simulcast layer
Better experience then just defaulting to first packet we see
1 parent fc9b5fe commit 6fd3374

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

internal/webrtc/sessions/whep/types.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ type (
2222
PeerConnectionLock sync.RWMutex
2323
PeerConnection *webrtc.PeerConnection
2424

25-
// Protects VideoTrack, VideoTimestamp, VideoPacketsWritten, VideoSequenceNumber
25+
// Protects VideoTrack, VideoTimestamp, VideoPacketsWritten, VideoSequenceNumber,
26+
// and auto video layer selection state.
2627
VideoLock sync.RWMutex
2728
VideoTrack *codecs.TrackMultiCodec
2829
VideoTimestamp uint32
@@ -34,6 +35,8 @@ type (
3435
VideoPacketsDropped atomic.Uint64
3536
VideoSequenceNumber uint16
3637
VideoLayerCurrent atomic.Value
38+
videoLayerPriority int
39+
videoLayerExplicit bool
3740

3841
// Protects AudioTrack, AudioTimestamp, AudioPacketsWritten, AudioSequenceNumber
3942
AudioLock sync.RWMutex

internal/webrtc/sessions/whep/whep.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,13 @@ func (w *WHEPSession) SetAudioLayer(encodingID string) {
113113
// Sets the requested video layer for this WHEP session.
114114
func (w *WHEPSession) SetVideoLayer(encodingID string) {
115115
log.Println("Setting Video Layer")
116+
117+
w.VideoLock.Lock()
116118
w.VideoLayerCurrent.Store(encodingID)
119+
w.videoLayerPriority = 0
120+
w.videoLayerExplicit = encodingID != ""
121+
w.VideoLock.Unlock()
122+
117123
w.IsWaitingForKeyframe.Store(true)
118124
w.SendPLI()
119125
}
@@ -147,16 +153,34 @@ func (w *WHEPSession) updateVideoBitrateLocked(now time.Time) {
147153
w.videoBitrateWindowBytes = w.VideoBytesWritten
148154
}
149155

150-
func (w *WHEPSession) GetVideoLayerOrDefault(defaultLayer string) string {
156+
func (w *WHEPSession) GetVideoLayerOrDefault(defaultLayer string, defaultPriority int) string {
151157
w.VideoLock.Lock()
152158
defer w.VideoLock.Unlock()
153159

154160
currentLayer, _ := w.VideoLayerCurrent.Load().(string)
155-
if currentLayer != "" {
161+
if w.videoLayerExplicit {
156162
return currentLayer
157163
}
158164

159-
w.VideoLayerCurrent.Store(defaultLayer)
160-
w.IsWaitingForKeyframe.Store(true)
161-
return defaultLayer
165+
if currentLayer == "" {
166+
w.VideoLayerCurrent.Store(defaultLayer)
167+
w.videoLayerPriority = defaultPriority
168+
w.IsWaitingForKeyframe.Store(true)
169+
return defaultLayer
170+
}
171+
172+
if currentLayer == defaultLayer {
173+
w.videoLayerPriority = defaultPriority
174+
return currentLayer
175+
}
176+
177+
// Lower numeric priority value means a better simulcast layer.
178+
if w.videoLayerPriority == 0 || defaultPriority < w.videoLayerPriority {
179+
w.VideoLayerCurrent.Store(defaultLayer)
180+
w.videoLayerPriority = defaultPriority
181+
w.IsWaitingForKeyframe.Store(true)
182+
return defaultLayer
183+
}
184+
185+
return currentLayer
162186
}

internal/webrtc/sessions/whip/handlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (w *WHIPSession) onTrackHandler(peerConnection *webrtc.PeerConnection, stre
3535

3636
if strings.HasPrefix(remoteTrack.Codec().MimeType, "audio") {
3737
// Handle audio stream
38-
w.audioWriter(remoteTrack, streamKey, peerConnection)
38+
w.audioWriter(remoteTrack, streamKey)
3939
} else {
4040
// Handle video stream
4141
w.videoWriter(remoteTrack, streamKey, peerConnection)

internal/webrtc/sessions/whip/writers.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
pionCodecs "github.com/pion/rtp/codecs"
1818
)
1919

20-
func (w *WHIPSession) audioWriter(remoteTrack *webrtc.TrackRemote, streamKey string, peerConnection *webrtc.PeerConnection) {
20+
func (w *WHIPSession) audioWriter(remoteTrack *webrtc.TrackRemote, streamKey string) {
2121
id := remoteTrack.RID()
2222

2323
if id == "" {
@@ -31,8 +31,6 @@ func (w *WHIPSession) audioWriter(remoteTrack *webrtc.TrackRemote, streamKey str
3131
return
3232
}
3333

34-
track.Priority = w.getPrioritizedStreamingLayer(id, peerConnection.CurrentRemoteDescription().SDP)
35-
3634
rtpPkt := &rtp.Packet{}
3735
rtpBuf := make([]byte, 1500)
3836
for {
@@ -183,7 +181,7 @@ func (w *WHIPSession) videoWriter(remoteTrack *webrtc.TrackRemote, streamKey str
183181
}
184182

185183
for _, whepSession := range sessions {
186-
if whepSession.GetVideoLayerOrDefault(id) != id {
184+
if whepSession.GetVideoLayerOrDefault(id, track.Priority) != id {
187185
continue
188186
}
189187

0 commit comments

Comments
 (0)