From 11c7b04221d0954f123a483b502f5e2d4292eab0 Mon Sep 17 00:00:00 2001 From: Benjamin Pracht Date: Wed, 15 Nov 2023 17:13:49 -0800 Subject: [PATCH] ReplaceTrack resets all track encodings This PR addresses an issue where calling RTPSender.ReplaceTrack with a nil parameter on a sender with more than 1 encoding (simulcast) would only cause the 1st encoding to be unbound, breaking common publisher reconnection workflows with simulcast enabled. --- rtpsender.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/rtpsender.go b/rtpsender.go index cbff6f82614..9a2629f9009 100644 --- a/rtpsender.go +++ b/rtpsender.go @@ -237,21 +237,26 @@ func (r *RTPSender) ReplaceTrack(track TrackLocal) error { var replacedTrack TrackLocal var context *baseTrackLocalContext - if len(r.trackEncodings) != 0 { - replacedTrack = r.trackEncodings[0].track - context = r.trackEncodings[0].context - } - if r.hasSent() && replacedTrack != nil { - if err := replacedTrack.Unbind(context); err != nil { - return err + for _, e := range r.trackEncodings { + replacedTrack = e.track + context = e.context + + if r.hasSent() && replacedTrack != nil { + if err := replacedTrack.Unbind(context); err != nil { + return err + } + } + + if !r.hasSent() || track == nil { + e.track = track } } if !r.hasSent() || track == nil { - r.trackEncodings[0].track = track return nil } + // If we reach this point in the routine, there is only 1 track encoding codec, err := track.Bind(&baseTrackLocalContext{ id: context.ID(), params: r.api.mediaEngine.getRTPParametersByKind(track.Kind(), []RTPTransceiverDirection{RTPTransceiverDirectionSendonly}), @@ -274,6 +279,7 @@ func (r *RTPSender) ReplaceTrack(track TrackLocal) error { } r.trackEncodings[0].track = track + return nil }