Skip to content

Commit

Permalink
Pass nil candidate into pion/ice
Browse files Browse the repository at this point in the history
Pass empty string candidate.Candidate into pion/ice to handle.

Co-authored-by: JooYoung <[email protected]>

Resolves #1212
  • Loading branch information
scorpionknifes authored and Sean-Der committed Dec 10, 2020
1 parent 236b6c4 commit 3eb51d8
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 23 deletions.
27 changes: 14 additions & 13 deletions icetransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ func (t *ICETransport) SetRemoteCandidates(remoteCandidates []ICECandidate) erro
if err != nil {
return err
}
err = agent.AddRemoteCandidate(i)
if err != nil {

if err = agent.AddRemoteCandidate(i); err != nil {
return err
}
}
Expand All @@ -244,30 +244,31 @@ func (t *ICETransport) SetRemoteCandidates(remoteCandidates []ICECandidate) erro
}

// AddRemoteCandidate adds a candidate associated with the remote ICETransport.
func (t *ICETransport) AddRemoteCandidate(remoteCandidate ICECandidate) error {
func (t *ICETransport) AddRemoteCandidate(remoteCandidate *ICECandidate) error {
t.lock.RLock()
defer t.lock.RUnlock()

if err := t.ensureGatherer(); err != nil {
var (
c ice.Candidate
err error
)

if err = t.ensureGatherer(); err != nil {
return err
}

c, err := remoteCandidate.toICE()
if err != nil {
return err
if remoteCandidate != nil {
if c, err = remoteCandidate.toICE(); err != nil {
return err
}
}

agent := t.gatherer.getAgent()
if agent == nil {
return fmt.Errorf("%w: unable to add remote candidates", errICEAgentNotExist)
}

err = agent.AddRemoteCandidate(c)
if err != nil {
return err
}

return nil
return agent.AddRemoteCandidate(c)
}

// State returns the current ice transport state.
Expand Down
25 changes: 15 additions & 10 deletions peerconnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -1053,8 +1053,8 @@ func (pc *PeerConnection) SetRemoteDescription(desc SessionDescription) error {
}
}

for _, c := range candidates {
if err = pc.iceTransport.AddRemoteCandidate(c); err != nil {
for i := range candidates {
if err = pc.iceTransport.AddRemoteCandidate(&candidates[i]); err != nil {
return err
}
}
Expand Down Expand Up @@ -1454,21 +1454,26 @@ func (pc *PeerConnection) RemoteDescription() *SessionDescription {
}

// AddICECandidate accepts an ICE candidate string and adds it
// to the existing set of candidates
// to the existing set of candidates.
func (pc *PeerConnection) AddICECandidate(candidate ICECandidateInit) error {
if pc.RemoteDescription() == nil {
return &rtcerr.InvalidStateError{Err: ErrNoRemoteDescription}
}

candidateValue := strings.TrimPrefix(candidate.Candidate, "candidate:")
c, err := ice.UnmarshalCandidate(candidateValue)
if err != nil {
return err
}

iceCandidate, err := newICECandidateFromICE(c)
if err != nil {
return err
var iceCandidate *ICECandidate
if candidateValue != "" {
candidate, err := ice.UnmarshalCandidate(candidateValue)
if err != nil {
return err
}

c, err := newICECandidateFromICE(candidate)
if err != nil {
return err
}
iceCandidate = &c
}

return pc.iceTransport.AddRemoteCandidate(iceCandidate)
Expand Down
37 changes: 37 additions & 0 deletions peerconnection_go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1158,3 +1158,40 @@ func TestPeerConnection_MassiveTracks(t *testing.T) {
assert.NoError(t, offerPC.Close())
assert.NoError(t, answerPC.Close())
}

func TestEmptyCandidate(t *testing.T) {
testCases := []struct {
ICECandidate ICECandidateInit
expectError bool
}{
{ICECandidateInit{"", nil, nil, nil}, false},
{ICECandidateInit{
"211962667 1 udp 2122194687 10.0.3.1 40864 typ host generation 0",
nil, nil, nil,
}, false},
{ICECandidateInit{
"1234567",
nil, nil, nil,
}, true},
}

for i, testCase := range testCases {
peerConn, err := NewPeerConnection(Configuration{})
if err != nil {
t.Errorf("Case %d: got error: %v", i, err)
}

err = peerConn.SetRemoteDescription(SessionDescription{Type: SDPTypeOffer, SDP: minimalOffer})
if err != nil {
t.Errorf("Case %d: got error: %v", i, err)
}

if testCase.expectError {
assert.Error(t, peerConn.AddICECandidate(testCase.ICECandidate))
} else {
assert.NoError(t, peerConn.AddICECandidate(testCase.ICECandidate))
}

assert.NoError(t, peerConn.Close())
}
}

0 comments on commit 3eb51d8

Please sign in to comment.