Skip to content

Commit f369fda

Browse files
biglittlebigbendavidzhao
authored andcommitted
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.
1 parent 367caa0 commit f369fda

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

rtpsender.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,21 +243,26 @@ func (r *RTPSender) ReplaceTrack(track TrackLocal) error {
243243

244244
var replacedTrack TrackLocal
245245
var context *baseTrackLocalContext
246-
if len(r.trackEncodings) != 0 {
247-
replacedTrack = r.trackEncodings[0].track
248-
context = r.trackEncodings[0].context
249-
}
250-
if r.hasSent() && replacedTrack != nil {
251-
if err := replacedTrack.Unbind(context); err != nil {
252-
return err
246+
for _, e := range r.trackEncodings {
247+
replacedTrack = e.track
248+
context = e.context
249+
250+
if r.hasSent() && replacedTrack != nil {
251+
if err := replacedTrack.Unbind(context); err != nil {
252+
return err
253+
}
254+
}
255+
256+
if !r.hasSent() || track == nil {
257+
e.track = track
253258
}
254259
}
255260

256261
if !r.hasSent() || track == nil {
257-
r.trackEncodings[0].track = track
258262
return nil
259263
}
260264

265+
// If we reach this point in the routine, there is only 1 track encoding
261266
codec, err := track.Bind(&baseTrackLocalContext{
262267
id: context.ID(),
263268
params: r.api.mediaEngine.getRTPParametersByKind(track.Kind(), []RTPTransceiverDirection{RTPTransceiverDirectionSendonly}),
@@ -280,6 +285,7 @@ func (r *RTPSender) ReplaceTrack(track TrackLocal) error {
280285
}
281286

282287
r.trackEncodings[0].track = track
288+
283289
return nil
284290
}
285291

0 commit comments

Comments
 (0)