Skip to content

Commit

Permalink
Improve checkNegotiationNeeded replaceTrack
Browse files Browse the repository at this point in the history
replaceTrack with nil would cause a crash
  • Loading branch information
aalekseevx authored and Sean-Der committed Sep 8, 2023
1 parent 2225386 commit e7b9b07
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
8 changes: 8 additions & 0 deletions peerconnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,14 @@ func (pc *PeerConnection) checkNegotiationNeeded() bool { //nolint:gocognit
return true
}
track := sender.Track()
if track == nil {
// Situation when sender's track is nil could happen when
// a) replaceTrack(nil) is called
// b) removeTrack() is called, changing the transceiver's direction to inactive
// As t.Direction() in this branch is either sendrecv or sendonly, we believe (a) option is the case
// As calling replaceTrack does not require renegotiation, we skip check for this transceiver
continue
}
if !okMsid || descMsid != track.StreamID()+" "+track.ID() {
return true
}
Expand Down
26 changes: 26 additions & 0 deletions peerconnection_renegotiation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1323,3 +1323,29 @@ func TestNegotiationNeededWithRecvonlyTrack(t *testing.T) {

closePairNow(t, pcOffer, pcAnswer)
}

func TestNegotiationNotNeededAfterReplaceTrackNil(t *testing.T) {
lim := test.TimeOut(time.Second * 30)
defer lim.Stop()

report := test.CheckRoutines(t)
defer report()

pcOffer, err := NewPeerConnection(Configuration{})
assert.NoError(t, err)

pcAnswer, err := NewPeerConnection(Configuration{})
assert.NoError(t, err)

tr, err := pcOffer.AddTransceiverFromKind(RTPCodecTypeAudio)
assert.NoError(t, err)

assert.NoError(t, signalPair(pcOffer, pcAnswer))

assert.NoError(t, tr.Sender().ReplaceTrack(nil))

assert.False(t, pcOffer.checkNegotiationNeeded())

assert.NoError(t, pcOffer.Close())
assert.NoError(t, pcAnswer.Close())
}

0 comments on commit e7b9b07

Please sign in to comment.