diff --git a/rtpsender.go b/rtpsender.go index 7e47acab56c..5b1b5d58554 100644 --- a/rtpsender.go +++ b/rtpsender.go @@ -243,21 +243,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}), @@ -280,6 +285,7 @@ func (r *RTPSender) ReplaceTrack(track TrackLocal) error { } r.trackEncodings[0].track = track + return nil }