Skip to content

Commit e507d46

Browse files
aalekseevxSean-Der
authored andcommitted
Improve checkNegotiationNeeded replaceTrack
replaceTrack with nil would cause a crash
1 parent 2225386 commit e507d46

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

peerconnection.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,14 @@ func (pc *PeerConnection) checkNegotiationNeeded() bool { //nolint:gocognit
400400
return true
401401
}
402402
track := sender.Track()
403+
if track == nil {
404+
// Situation when sender's track is nil could happen when
405+
// a) replaceTrack(nil) is called
406+
// b) removeTrack() is called, changing the transceiver's direction to inactive
407+
// As t.Direction() in this branch is either sendrecv or sendonly, we believe (a) option is the case
408+
// As calling replaceTrack does not require renegotiation, we skip check for this transceiver
409+
continue
410+
}
403411
if !okMsid || descMsid != track.StreamID()+" "+track.ID() {
404412
return true
405413
}

peerconnection_renegotiation_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,3 +1323,29 @@ func TestNegotiationNeededWithRecvonlyTrack(t *testing.T) {
13231323

13241324
closePairNow(t, pcOffer, pcAnswer)
13251325
}
1326+
1327+
func TestNegotiationNotNeededAfterReplaceTrackNil(t *testing.T) {
1328+
lim := test.TimeOut(time.Second * 30)
1329+
defer lim.Stop()
1330+
1331+
report := test.CheckRoutines(t)
1332+
defer report()
1333+
1334+
pcOffer, err := NewPeerConnection(Configuration{})
1335+
assert.NoError(t, err)
1336+
1337+
pcAnswer, err := NewPeerConnection(Configuration{})
1338+
assert.NoError(t, err)
1339+
1340+
tr, err := pcOffer.AddTransceiverFromKind(RTPCodecTypeAudio)
1341+
assert.NoError(t, err)
1342+
1343+
assert.NoError(t, signalPair(pcOffer, pcAnswer))
1344+
1345+
assert.NoError(t, tr.Sender().ReplaceTrack(nil))
1346+
1347+
assert.False(t, pcOffer.checkNegotiationNeeded())
1348+
1349+
assert.NoError(t, pcOffer.Close())
1350+
assert.NoError(t, pcAnswer.Close())
1351+
}

0 commit comments

Comments
 (0)