Add support for RTP stream restart. (#4161)

* Add support for RTP stream restart.

When an unhandled packet is encountered, try a restart sequence.
Restart happens when 5 packets with contiguous sequence numbers and same
or increasing time stamps are received. Note that this does not work for
B-frame type of scenarios, but that is true for receive path handling
even before this. As WebRTC does not use B-frames, it is fine. But,
needs to be looked at again if B-frames are necessary.

It is controlled by a config that is disabled by default.

* clean up

* debug log
This commit is contained in:
Raja Subramanian
2025-12-16 13:21:39 +05:30
committed by GitHub
parent 386f0b3822
commit a26c48304a
13 changed files with 395 additions and 160 deletions
+24 -4
View File
@@ -122,7 +122,7 @@ type TrackReceiver interface {
DeleteDownTrack(participantID livekit.ParticipantID)
GetDownTracks() []TrackSender
DebugInfo() map[string]interface{}
DebugInfo() map[string]any
TrackInfo() *livekit.TrackInfo
UpdateTrackInfo(ti *livekit.TrackInfo)
@@ -156,6 +156,7 @@ type REDTransformer interface {
publisherSRData *livekit.RTCPSenderReportState,
)
ResyncDownTracks()
OnStreamRestart()
CanClose() bool
Close()
}
@@ -166,8 +167,9 @@ var _ TrackReceiver = (*WebRTCReceiver)(nil)
type WebRTCReceiver struct {
logger logger.Logger
pliThrottleConfig PLIThrottleConfig
audioConfig AudioConfig
pliThrottleConfig PLIThrottleConfig
audioConfig AudioConfig
enableRTPStreamRestartDetection bool
trackID livekit.TrackID
streamID string
@@ -228,6 +230,13 @@ func WithAudioConfig(audioConfig AudioConfig) ReceiverOpts {
}
}
func WithEnableRTPStreamRestartDetection(enable bool) ReceiverOpts {
return func(w *WebRTCReceiver) *WebRTCReceiver {
w.enableRTPStreamRestartDetection = enable
return w
}
}
// WithLoadBalanceThreshold enables parallelization of packet writes when downTracks exceeds threshold
// Value should be between 3 and 150.
// For a server handling a few large rooms, use a smaller value (required to handle very large (250+ participant) rooms).
@@ -412,6 +421,7 @@ func (w *WebRTCReceiver) AddUpTrack(track TrackRemote, buff *buffer.Buffer) erro
Config: w.audioConfig.AudioLevelConfig,
})
buff.SetAudioLossProxying(w.audioConfig.EnableLossProxying)
buff.SetStreamRestartDetection(w.enableRTPStreamRestartDetection)
buff.OnRtcpFeedback(w.sendRTCP)
buff.OnRtcpSenderReport(func() {
srData := buff.GetSenderReportData()
@@ -438,7 +448,6 @@ func (w *WebRTCReceiver) AddUpTrack(track TrackRemote, buff *buffer.Buffer) erro
cb()
}
})
if w.Kind() == webrtc.RTPCodecTypeVideo && layer == 0 {
buff.OnCodecChange(w.handleCodecChange)
}
@@ -795,6 +804,17 @@ func (w *WebRTCReceiver) forwardRTP(layer int32, buff *buffer.Buffer) {
}
dequeuedAt := mono.UnixNano()
if pkt.IsRestart {
w.logger.Infow("stream restarted", "layer", layer)
w.downTrackSpreader.Broadcast(func(dt TrackSender) {
dt.ReceiverRestart()
})
if rt := w.redTransformer.Load(); rt != nil {
rt.(REDTransformer).OnStreamRestart()
}
}
if pkt.Packet.PayloadType != uint8(w.codec.PayloadType) {
// drop packets as we don't support codec fallback directly
w.logger.Debugw(